ke/mutex: fix possible use-after-free

A thread might get reaped between loading the owner and its state,
leading to a possible use-after-free.
This commit is contained in:
Loïc Grégoire
2026-07-08 11:15:31 -04:00
parent 898ceb171e
commit 5fbc61ecb1
+8 -14
View File
@@ -29,21 +29,15 @@ pub const Mutex = struct {
return;
}
// Fast path: if the owner is running, try to acquire without a turnstile by spinning a bit.
if (cur_owner.?.?.state.load(.monotonic) == .Running) {
for (0..optimistic_spins) |_| {
if (m.owner.cmpxchgStrong(null, curtd, .acquire, .monotonic)) |maybe_owner| {
const owner = maybe_owner.?;
if (owner.state.load(.monotonic) != .Running) break;
std.atomic.spinLoopHint();
continue;
}
ke.ipl.lower(ipl);
return;
// Fast path: Try to acquire without a turnstile by spinning a bit.
for (0..optimistic_spins) |_| {
if (m.owner.cmpxchgWeak(null, curtd, .acquire, .monotonic) != null) {
std.atomic.spinLoopHint();
continue;
}
ke.ipl.lower(ipl);
return;
}
// Slow path: contended, block on a turnstile.