Skip to content

Commit 0aacb6d

Browse files
Sebastian Andrzej Siewiorgregkh
authored andcommitted
futex: Prevent lockup in requeue-PI during signal/ timeout wakeup
[ Upstream commit bc7304f ] During wait-requeue-pi (task A) and requeue-PI (task B) the following race can happen: Task A Task B futex_wait_requeue_pi() futex_setup_timer() futex_do_wait() futex_requeue() CLASS(hb, hb1)(&key1); CLASS(hb, hb2)(&key2); *timeout* futex_requeue_pi_wakeup_sync() requeue_state = Q_REQUEUE_PI_IGNORE *blocks on hb->lock* futex_proxy_trylock_atomic() futex_requeue_pi_prepare() Q_REQUEUE_PI_IGNORE => -EAGAIN double_unlock_hb(hb1, hb2) *retry* Task B acquires both hb locks and attempts to acquire the PI-lock of the top most waiter (task B). Task A is leaving early due to a signal/ timeout and started removing itself from the queue. It updates its requeue_state but can not remove it from the list because this requires the hb lock which is owned by task B. Usually task A is able to swoop the lock after task B unlocked it. However if task B is of higher priority then task A may not be able to wake up in time and acquire the lock before task B gets it again. Especially on a UP system where A is never scheduled. As a result task A blocks on the lock and task B busy loops, trying to make progress but live locks the system instead. Tragic. This can be fixed by removing the top most waiter from the list in this case. This allows task B to grab the next top waiter (if any) in the next iteration and make progress. Remove the top most waiter if futex_requeue_pi_prepare() fails. Let the waiter conditionally remove itself from the list in handle_early_requeue_pi_wakeup(). Fixes: 07d91ef ("futex: Prevent requeue_pi() lock nesting issue on RT") Reported-by: Moritz Klammler <Moritz.Klammler@ferchau.com> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Thomas Gleixner <tglx@kernel.org> Link: https://patch.msgid.link/20260428103425.dywXyPd3@linutronix.de Closes: https://lore.kernel.org/all/VE1PR06MB6894BE61C173D802365BE19DFF4CA@VE1PR06MB6894.eurprd06.prod.outlook.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 0ad5470 commit 0aacb6d

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

kernel/futex/requeue.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1,
309309
return -EINVAL;
310310

311311
/* Ensure that this does not race against an early wakeup */
312-
if (!futex_requeue_pi_prepare(top_waiter, NULL))
312+
if (!futex_requeue_pi_prepare(top_waiter, NULL)) {
313+
plist_del(&top_waiter->list, &hb1->chain);
314+
futex_hb_waiters_dec(hb1);
313315
return -EAGAIN;
316+
}
314317

315318
/*
316319
* Try to take the lock for top_waiter and set the FUTEX_WAITERS bit
@@ -711,10 +714,12 @@ int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
711714

712715
/*
713716
* We were woken prior to requeue by a timeout or a signal.
714-
* Unqueue the futex_q and determine which it was.
717+
* Conditionally unqueue the futex_q and determine which it was.
715718
*/
716-
plist_del(&q->list, &hb->chain);
717-
futex_hb_waiters_dec(hb);
719+
if (!plist_node_empty(&q->list)) {
720+
plist_del(&q->list, &hb->chain);
721+
futex_hb_waiters_dec(hb);
722+
}
718723

719724
/* Handle spurious wakeups gracefully */
720725
ret = -EWOULDBLOCK;

0 commit comments

Comments
 (0)