[PATCH v2 2/2] futex/requeue: Prevent rcuwait use-after-free during requeue PI
From: Yao Kai
Date: Wed Jul 22 2026 - 04:35:43 EST
On PREEMPT_RT, FUTEX_CMP_REQUEUE_PI can trigger a KASAN report:
BUG: KASAN: slab-out-of-bounds in _raw_spin_lock_irqsave+0x76/0xe0
Call Trace:
_raw_spin_lock_irqsave+0x76/0xe0
try_to_wake_up+0xab/0x1540
rcuwait_wake_up+0x39/0x60
futex_requeue+0x18c3/0x1e10
The futex_q used by futex_wait_requeue_pi() is allocated on the waiter's
stack. An early wakeup can race with a PI requeue as follows:
waiter requeue task
------ ------------
futex_wait_requeue_pi()
futex_do_wait()
schedule()
* timeout/signal wakes waiter *
futex_requeue_pi_wakeup_sync()
IN_PROGRESS -> WAIT
rcuwait_wait_event()
requeue_pi_wake_futex()
task = READ_ONCE(q->task)
futex_requeue_pi_complete()
WAIT -> LOCKED
return LOCKED
return
// q lifetime ends
rcuwait_wake_up()
futex_requeue_pi_complete() publishes LOCKED before calling
rcuwait_wake_up(). Once the waiter observes LOCKED, it can return from
futex_wait_requeue_pi() and let q go out of scope before rcuwait_wake_up()
reads q->requeue_wait.task and passes the stale pointer to
try_to_wake_up().
Skip rcuwait_wake_up() for Q_REQUEUE_PI_LOCKED. This state is only
published by requeue_pi_wake_futex(), which saves q->task before
futex_requeue_pi_complete() and calls wake_up_state(task, TASK_NORMAL)
afterwards. If the waiter is already blocked in rcuwait_wait_event(),
TASK_NORMAL includes TASK_UNINTERRUPTIBLE and wakes it. If the wakeup
runs before the waiter blocks, the waiter observes LOCKED and does not
schedule. The other completion states retain their rcuwait wakeup.
Fixes: 07d91ef510fb1 ("futex: Prevent requeue_pi() lock nesting issue on RT")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yao Kai <yaokai34@xxxxxxxxxx>
---
kernel/futex/requeue.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/kernel/futex/requeue.c b/kernel/futex/requeue.c
index f7889fb2fce4..9b32f19f320a 100644
--- a/kernel/futex/requeue.c
+++ b/kernel/futex/requeue.c
@@ -155,8 +155,16 @@ static inline void futex_requeue_pi_complete(struct futex_q *q, int locked)
} while (!atomic_try_cmpxchg(&q->requeue_state, &old, new));
#ifdef CONFIG_PREEMPT_RT
- /* If the waiter interleaved with the requeue let it know */
- if (unlikely(old == Q_REQUEUE_PI_WAIT))
+ /*
+ * If the waiter interleaved with the requeue, let it know. For LOCKED,
+ * q may be invalid as soon as the state is published. Only
+ * requeue_pi_wake_futex() publishes LOCKED; it saves q->task before
+ * futex_requeue_pi_complete() and follows it with
+ * wake_up_state(TASK_NORMAL), which also wakes the TASK_UNINTERRUPTIBLE
+ * rcuwait waiter.
+ */
+ if (unlikely(old == Q_REQUEUE_PI_WAIT) &&
+ new != Q_REQUEUE_PI_LOCKED)
rcuwait_wake_up(&q->requeue_wait);
#endif
}
--
2.43.0