mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
Kernel: Acquire reference to waitee before trying to block in sys$waitid
Previously, we would try to acquire a reference to the all processes lock or other contended resources while holding both the scheduler lock and the thread's blocker lock. This could lead to a deadlock if we actually have to block on those other resources.
This commit is contained in:
parent
dea62fe93c
commit
54161bf5b4
4 changed files with 43 additions and 66 deletions
|
@ -14,6 +14,7 @@
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Userspace.h>
|
#include <AK/Userspace.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
#include <AK/Weakable.h>
|
#include <AK/Weakable.h>
|
||||||
#include <Kernel/API/Syscall.h>
|
#include <Kernel/API/Syscall.h>
|
||||||
|
@ -541,7 +542,7 @@ private:
|
||||||
KResult do_killall(int signal);
|
KResult do_killall(int signal);
|
||||||
KResult do_killself(int signal);
|
KResult do_killself(int signal);
|
||||||
|
|
||||||
KResultOr<siginfo_t> do_waitid(idtype_t idtype, int id, int options);
|
KResultOr<siginfo_t> do_waitid(Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, int options);
|
||||||
|
|
||||||
KResultOr<NonnullOwnPtr<KString>> get_syscall_path_argument(Userspace<const char*> user_path, size_t path_length) const;
|
KResultOr<NonnullOwnPtr<KString>> get_syscall_path_argument(Userspace<const char*> user_path, size_t path_length) const;
|
||||||
KResultOr<NonnullOwnPtr<KString>> get_syscall_path_argument(const Syscall::StringArgument&) const;
|
KResultOr<NonnullOwnPtr<KString>> get_syscall_path_argument(const Syscall::StringArgument&) const;
|
||||||
|
|
|
@ -4,15 +4,16 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <Kernel/Debug.h>
|
#include <Kernel/Debug.h>
|
||||||
#include <Kernel/Process.h>
|
#include <Kernel/Process.h>
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
KResultOr<siginfo_t> Process::do_waitid(idtype_t idtype, int id, int options)
|
KResultOr<siginfo_t> Process::do_waitid(Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, int options)
|
||||||
{
|
{
|
||||||
KResultOr<siginfo_t> result = KResult(KSuccess);
|
KResultOr<siginfo_t> result = KResult(KSuccess);
|
||||||
if (Thread::current()->block<Thread::WaitBlocker>({}, options, idtype, id, result).was_interrupted())
|
if (Thread::current()->block<Thread::WaitBlocker>({}, options, move(waitee), result).was_interrupted())
|
||||||
return EINTR;
|
return EINTR;
|
||||||
VERIFY(!result.is_error() || (options & WNOHANG) || result.error() != KSuccess);
|
VERIFY(!result.is_error() || (options & WNOHANG) || result.error() != KSuccess);
|
||||||
return result;
|
return result;
|
||||||
|
@ -27,18 +28,34 @@ KResultOr<FlatPtr> Process::sys$waitid(Userspace<const Syscall::SC_waitid_params
|
||||||
if (!copy_from_user(¶ms, user_params))
|
if (!copy_from_user(¶ms, user_params))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
|
|
||||||
|
Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee = Empty {};
|
||||||
switch (params.idtype) {
|
switch (params.idtype) {
|
||||||
case P_ALL:
|
case P_ALL:
|
||||||
case P_PID:
|
waitee = Empty {};
|
||||||
case P_PGID:
|
|
||||||
break;
|
break;
|
||||||
|
case P_PID: {
|
||||||
|
auto waitee_process = Process::from_pid(params.id);
|
||||||
|
if (!waitee_process || waitee_process->ppid() != Process::current().pid()) {
|
||||||
|
return ECHILD;
|
||||||
|
}
|
||||||
|
waitee = waitee_process.release_nonnull();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case P_PGID: {
|
||||||
|
auto waitee_group = ProcessGroup::from_pgid(params.id);
|
||||||
|
if (!waitee_group) {
|
||||||
|
return ECHILD;
|
||||||
|
}
|
||||||
|
waitee = waitee_group.release_nonnull();
|
||||||
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
dbgln_if(PROCESS_DEBUG, "sys$waitid({}, {}, {}, {})", params.idtype, params.id, params.infop, params.options);
|
dbgln_if(PROCESS_DEBUG, "sys$waitid({}, {}, {}, {})", params.idtype, params.id, params.infop, params.options);
|
||||||
|
|
||||||
auto siginfo_or_error = do_waitid(static_cast<idtype_t>(params.idtype), params.id, params.options);
|
auto siginfo_or_error = do_waitid(move(waitee), params.options);
|
||||||
if (siginfo_or_error.is_error())
|
if (siginfo_or_error.is_error())
|
||||||
return siginfo_or_error.error();
|
return siginfo_or_error.error();
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/TemporaryChange.h>
|
#include <AK/TemporaryChange.h>
|
||||||
#include <AK/Time.h>
|
#include <AK/Time.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
#include <AK/Weakable.h>
|
#include <AK/Weakable.h>
|
||||||
|
@ -705,7 +706,7 @@ public:
|
||||||
Disowned
|
Disowned
|
||||||
};
|
};
|
||||||
|
|
||||||
WaitBlocker(int wait_options, idtype_t id_type, pid_t id, KResultOr<siginfo_t>& result);
|
WaitBlocker(int wait_options, Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, KResultOr<siginfo_t>& result);
|
||||||
virtual StringView state_string() const override { return "Waiting"sv; }
|
virtual StringView state_string() const override { return "Waiting"sv; }
|
||||||
virtual Type blocker_type() const override { return Type::Wait; }
|
virtual Type blocker_type() const override { return Type::Wait; }
|
||||||
virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override;
|
virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override;
|
||||||
|
@ -720,13 +721,9 @@ public:
|
||||||
void do_set_result(const siginfo_t&);
|
void do_set_result(const siginfo_t&);
|
||||||
|
|
||||||
const int m_wait_options;
|
const int m_wait_options;
|
||||||
const idtype_t m_id_type;
|
|
||||||
const pid_t m_waitee_id;
|
|
||||||
KResultOr<siginfo_t>& m_result;
|
KResultOr<siginfo_t>& m_result;
|
||||||
RefPtr<Process> m_waitee;
|
Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> m_waitee;
|
||||||
RefPtr<ProcessGroup> m_waitee_group;
|
|
||||||
bool m_did_unblock { false };
|
bool m_did_unblock { false };
|
||||||
bool m_error { false };
|
|
||||||
bool m_got_sigchild { false };
|
bool m_got_sigchild { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -601,55 +601,23 @@ void Thread::WaitBlockerSet::finalize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread::WaitBlocker::WaitBlocker(int wait_options, idtype_t id_type, pid_t id, KResultOr<siginfo_t>& result)
|
Thread::WaitBlocker::WaitBlocker(int wait_options, Variant<Empty, NonnullRefPtr<Process>, NonnullRefPtr<ProcessGroup>> waitee, KResultOr<siginfo_t>& result)
|
||||||
: m_wait_options(wait_options)
|
: m_wait_options(wait_options)
|
||||||
, m_id_type(id_type)
|
|
||||||
, m_waitee_id(id)
|
|
||||||
, m_result(result)
|
, m_result(result)
|
||||||
|
, m_waitee(move(waitee))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Thread::WaitBlocker::setup_blocker()
|
bool Thread::WaitBlocker::setup_blocker()
|
||||||
{
|
{
|
||||||
switch (m_id_type) {
|
|
||||||
case P_PID: {
|
|
||||||
m_waitee = Process::from_pid(m_waitee_id);
|
|
||||||
if (!m_waitee || m_waitee->ppid() != Process::current().pid()) {
|
|
||||||
m_result = ECHILD;
|
|
||||||
m_error = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case P_PGID: {
|
|
||||||
m_waitee_group = ProcessGroup::from_pgid(m_waitee_id);
|
|
||||||
if (!m_waitee_group) {
|
|
||||||
m_result = ECHILD;
|
|
||||||
m_error = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case P_ALL:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_error)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (m_wait_options & WNOHANG)
|
if (m_wait_options & WNOHANG)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// NOTE: unblock may be called within add_to_blocker_set, in which
|
|
||||||
// case it means that we already have a match without having to block.
|
|
||||||
// In that case add_to_blocker_set will return false.
|
|
||||||
return add_to_blocker_set(Process::current().wait_blocker_set());
|
return add_to_blocker_set(Process::current().wait_blocker_set());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thread::WaitBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason)
|
void Thread::WaitBlocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason)
|
||||||
{
|
{
|
||||||
if (!m_error)
|
Process::current().wait_blocker_set().try_unblock(*this);
|
||||||
Process::current().wait_blocker_set().try_unblock(*this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thread::WaitBlocker::was_unblocked(bool)
|
void Thread::WaitBlocker::was_unblocked(bool)
|
||||||
|
@ -700,26 +668,20 @@ bool Thread::WaitBlocker::unblock(Process& process, UnblockFlags flags, u8 signa
|
||||||
{
|
{
|
||||||
VERIFY(flags != UnblockFlags::Terminated || signal == 0); // signal argument should be ignored for Terminated
|
VERIFY(flags != UnblockFlags::Terminated || signal == 0); // signal argument should be ignored for Terminated
|
||||||
|
|
||||||
switch (m_id_type) {
|
bool do_not_unblock = m_waitee.visit(
|
||||||
case P_PID:
|
[&](NonnullRefPtr<Process> const& waitee_process) {
|
||||||
VERIFY(m_waitee);
|
return &process != waitee_process;
|
||||||
if (process.pid() != m_waitee_id)
|
},
|
||||||
return false;
|
[&](NonnullRefPtr<ProcessGroup> const& waitee_process_group) {
|
||||||
break;
|
return waitee_process_group->pgid() != process.pgid();
|
||||||
case P_PGID:
|
},
|
||||||
VERIFY(m_waitee_group);
|
[&](Empty const&) {
|
||||||
if (process.pgid() != m_waitee_group->pgid())
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
case P_ALL:
|
|
||||||
if (flags == UnblockFlags::Disowned) {
|
|
||||||
// Generic waiter won't be unblocked by disown
|
// Generic waiter won't be unblocked by disown
|
||||||
return false;
|
return flags == UnblockFlags::Disowned;
|
||||||
}
|
});
|
||||||
break;
|
|
||||||
default:
|
if (do_not_unblock)
|
||||||
VERIFY_NOT_REACHED();
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
switch (flags) {
|
switch (flags) {
|
||||||
case UnblockFlags::Terminated:
|
case UnblockFlags::Terminated:
|
||||||
|
|
Loading…
Add table
Reference in a new issue