1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +00: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:
Andrew Kaster 2021-08-22 22:01:04 -06:00 committed by Andreas Kling
parent dea62fe93c
commit 54161bf5b4
4 changed files with 43 additions and 66 deletions

View file

@ -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_id_type(id_type)
, m_waitee_id(id)
, m_result(result)
, m_waitee(move(waitee))
{
}
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)
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());
}
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)
@ -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
switch (m_id_type) {
case P_PID:
VERIFY(m_waitee);
if (process.pid() != m_waitee_id)
return false;
break;
case P_PGID:
VERIFY(m_waitee_group);
if (process.pgid() != m_waitee_group->pgid())
return false;
break;
case P_ALL:
if (flags == UnblockFlags::Disowned) {
bool do_not_unblock = m_waitee.visit(
[&](NonnullRefPtr<Process> const& waitee_process) {
return &process != waitee_process;
},
[&](NonnullRefPtr<ProcessGroup> const& waitee_process_group) {
return waitee_process_group->pgid() != process.pgid();
},
[&](Empty const&) {
// Generic waiter won't be unblocked by disown
return false;
}
break;
default:
VERIFY_NOT_REACHED();
}
return flags == UnblockFlags::Disowned;
});
if (do_not_unblock)
return false;
switch (flags) {
case UnblockFlags::Terminated: