mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jiwoo Kim <arpe1618@gmail.com>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>
Cc: "Peter Zijlstra" <peterz@infradead.org>,
	"Darren Hart" <dvhart@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"André Almeida" <andrealmeid@igalia.com>,
	"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] futex: Don't check lock_ptr of an unqueued requeue-PI waiter
Date: Wed, 18 Nov 2026 00:24:28 +0900	[thread overview]
Message-ID: <20261117152428.1477-1-arpe1618@gmail.com> (raw)

Since commit bc7304f3ae20 ("futex: Prevent lockup in requeue-PI during
signal/ timeout wakeup"), futex_proxy_trylock_atomic() unqueues a top
waiter in Q_REQUEUE_PI_IGNORE state from hb1 itself, and
handle_early_requeue_pi_wakeup() only unqueues the futex_q when it is
still queued.

A futex_q unqueued this way keeps q->lock_ptr pointing to the lock of
its hash bucket. futex_rehash_private() only moves futex_qs which are
on a hash bucket chain, so a resize of the private hash before the
waiter gets to handle_early_requeue_pi_wakeup() leaves q->lock_ptr
pointing into the old hash:

  Task A (waiter)                 Task B (requeue), Task C (resize)
  futex_wait_requeue_pi()
    times out, requeue_state
    = Q_REQUEUE_PI_IGNORE
                                  B: futex_proxy_trylock_atomic()
                                       unqueues A from hb1
                                  C: futex_rehash_private()
                                       does not move A
    locks its hash bucket in
    the new hash
    handle_early_requeue_pi_wakeup()
      WARN_ON_ONCE(&hb->lock != q->lock_ptr)

The warning triggers, although the futex_q is not queued anymore and
q->lock_ptr is not used. Besides PR_FUTEX_HASH_SET_SLOTS, the private
hash is also resized when threads are created, see
futex_hash_allocate_default().

This was observed as an unprivileged user in a QEMU guest, with threads
of one process doing FUTEX_WAIT_REQUEUE_PI with short timeouts,
FUTEX_CMP_REQUEUE_PI and PR_FUTEX_HASH_SET_SLOTS at the same time:

  WARNING: kernel/futex/requeue.c:734 at futex_wait_requeue_pi+0x67a/0x6f0

It did not trigger with the requeue or the resize left out.

Only check q->lock_ptr while the futex_q is queued.

This was found with an AI coding assistant, which also drafted this
patch and its changelog. The problem was reproduced in a QEMU guest,
and the fix was tested there.

Fixes: bc7304f3ae20 ("futex: Prevent lockup in requeue-PI during signal/ timeout wakeup")
Cc: stable@vger.kernel.org
Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
---
This is a different case from the one in [1], which was about unlocking
through q.lock_ptr after a requeue. Locking through q->lock_ptr would
not help here, as it can point into the old hash, which is freed after
the resize.

A reproducer is available on request.

[1] https://lore.kernel.org/all/20260919221730.3707261-1-benquike@gmail.com/

 kernel/futex/requeue.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/kernel/futex/requeue.c b/kernel/futex/requeue.c
index 842d852302dd..6e3557a9f409 100644
--- a/kernel/futex/requeue.c
+++ b/kernel/futex/requeue.c
@@ -730,14 +730,17 @@ int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
 	 * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
 	 * It can't be requeued from uaddr2 to something else since we don't
 	 * support a PI aware source futex for requeue.
-	 */
-	WARN_ON_ONCE(&hb->lock != q->lock_ptr);
-
-	/*
+	 *
 	 * We were woken prior to requeue by a timeout or a signal.
 	 * Conditionally unqueue the futex_q and determine which it was.
+	 *
+	 * If futex_proxy_trylock_atomic() unqueued the futex_q already, a
+	 * resize of the private hash since then did not move it, so
+	 * q->lock_ptr can still point into the old hash, which may have
+	 * been freed. Only check it while the futex_q is queued.
 	 */
 	if (!plist_node_empty(&q->list)) {
+		WARN_ON_ONCE(&hb->lock != q->lock_ptr);
 		plist_del(&q->list, &hb->chain);
 		futex_hb_waiters_dec(hb);
 	}

base-commit: 408b129aac48b574c0c9d11137a89eefba825d5d
-- 
2.43.0


                 reply	other threads:[~2026-11-17 15:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117152428.1477-1-arpe1618@gmail.com \
    --to=arpe1618@gmail.com \
    --cc=andrealmeid@igalia.com \
    --cc=bigeasy@linutronix.de \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®