Skip to contentSkip to conversation

futex: fix hb lock mismatch in futex_wait_requeue_pi() and CLONE_VFORK hash init

PATCH In review Hui Peng 2 participants 2 messages Sep 19 – Sep 21

Subsystems (4)
lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 futex: fix hb lock mismatch in futex_wait_requeue_pi() and CLONE_VFORK hash init

Fix two issues in futex and fork initialization:

1. In futex_wait_requeue_pi(), if futex_wait_queue() returns with q.lock_ptr updated tohb2's lock after a requeue timeout or signal race, unlocking the original hb1 lock instead of q.lock_ptr leaves hb2 locked and unlocks an unheld spinlock. Always unlock via q.lock_ptr.
2. In copy_mm() (kernel/fork.c), initialize mm->futex_phash properly when cloning so private futex hash state is consistent across CLONE_VFORK.

Fixes e5c6828493b5 ("futex: Split out requeue")
Assisted-by LLM

---

kernel/fork.c +2 -2
index a5934a317634..8f320f51cae8 100644
@@ -1996,9 +1996,9 @@ static bool need_futex_hash_allocate_default(u64 clone_flags)
 {
 	/*
 	 * Allocate a default futex hash for any sibling that will
-	 * share the parent's mm, except vfork.
+	 * share the parent's mm.
 	 */
-	return (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM;
+	return (clone_flags & CLONE_VM) != 0;
 }
 
 /*
kernel/futex/requeue.c +5 -4
index b3f4a4bccb12..72b954d625c4 100644
@@ -842,12 +842,13 @@ int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
 	switch (futex_requeue_pi_wakeup_sync(&q)) {
 	case Q_REQUEUE_PI_IGNORE:
 		{
-			CLASS(hbr, hbr)(&q.key);
-			auto hb = hbr.hb;
+			struct futex_hash_bucket *hb;
+
 			/* The waiter is still on uaddr1 */
-			spin_lock(&hb->lock);
+			futex_q_lockptr_lock(&q);
+			hb = container_of(q.lock_ptr, struct futex_hash_bucket, lock);
 			ret = handle_early_requeue_pi_wakeup(hb, &q, to);
-			spin_unlock(&hb->lock);
+			spin_unlock(q.lock_ptr);
 		}
 		break;
 
#1 lore raw

Hi Hui Peng,

Em 19/09/2026 19:17, Hui Peng escreveu:

Quoted 3 lines

Fix two issues in futex and fork initialization:

1. In futex_wait_requeue_pi(), if futex_wait_queue() returns with

I see an outdated comment with futex_wait_queue(), but I don't think this function exists anymore.

Quoted 8 lines

q.lock_ptr updated tohb2's lock after a requeue timeout or signal race, unlocking the original hb1 lock instead of q.lock_ptr leaves hb2 locked and unlocks an unheld spinlock. Always unlock via q.lock_ptr.
2. In copy_mm() (kernel/fork.c), initialize mm->futex_phash properly when cloning so private futex hash state is consistent across CLONE_VFORK.

This part was already addressed:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b61b6f95d6722ddbbbd09e689fa41b55fd36f9a5

Quoted 2 lines
Fixes e5c6828493b5 ("futex: Split out requeue")
Assisted-by LLM

Please always double check in the current mainline code if the robots ideas are up to date.

Quoted 1 line