mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +00:00
Kernel: Disable interrupts while setting up a thread blocker
There was a race window between instantiating a WaitQueueBlocker and setting the thread state to Blocked. If a thread was preempted between those steps, someone else might try to wake the wait queue and find an unblocked thread in a wait queue, which is not sane.
This commit is contained in:
parent
f067730f6b
commit
9ed272ce98
Notes:
sideshowbarker
2024-07-19 11:00:39 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/9ed272ce985
2 changed files with 24 additions and 3 deletions
|
@ -289,6 +289,21 @@ private:
|
|||
u32 m_flags;
|
||||
};
|
||||
|
||||
inline bool cli_and_save_interrupt_flag()
|
||||
{
|
||||
u32 flags = cpu_flags();
|
||||
cli();
|
||||
return flags & 0x200;
|
||||
}
|
||||
|
||||
inline void restore_interrupt_flag(bool flag)
|
||||
{
|
||||
if (flag)
|
||||
sti();
|
||||
else
|
||||
cli();
|
||||
}
|
||||
|
||||
class InterruptDisabler {
|
||||
public:
|
||||
InterruptDisabler()
|
||||
|
|
|
@ -274,11 +274,17 @@ public:
|
|||
ASSERT(state() == Thread::Running);
|
||||
ASSERT(m_blocker == nullptr);
|
||||
|
||||
T t(AK::forward<Args>(args)...);
|
||||
// NOTE: We disable interrupts here to avoid the situation where a WaitQueueBlocker
|
||||
// adds the current thread to a WaitQueue, and then someone wakes up before
|
||||
// we set the state to Blocked decides to wake the queue. They would find
|
||||
// unblocked threads in a wait queue, which would not be good. We can't go
|
||||
// into Blocked state earlier, since that would prevent this thread from
|
||||
// getting scheduled.
|
||||
auto saved_if = cli_and_save_interrupt_flag();
|
||||
T t(forward<Args>(args)...);
|
||||
m_blocker = &t;
|
||||
|
||||
// Enter blocked state.
|
||||
set_state(Thread::Blocked);
|
||||
restore_interrupt_flag(saved_if);
|
||||
|
||||
// Yield to the scheduler, and wait for us to resume unblocked.
|
||||
if (beneficiary) {
|
||||
|
|
Loading…
Reference in a new issue