futex: clone(CLONE_VM) can fail with -EBUSY while PR_FUTEX_HASH switches to the global hash
From: Zhenwen Liang
Date: Wed Nov 25 2026 - 21:35:45 EST
Hi,
A thread-creating clone() can fail with -EBUSY when it races with
prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS, 0) in the same process.
Line numbers are kernel/futex/core.c on mainline ebb7a8dc1e74.
copy_process() calls futex_hash_allocate_default() for CLONE_VM and
aborts the clone on any non-zero return (kernel/fork.c:2450-2453). For a
process that still has a small default private hash,
futex_hash_allocate_default() asks for a bigger one with
futex_hash_allocate(buckets, 0) (core.c:2016).
futex_hash_allocate() first checks, under RCU, whether the global hash
is already in use, and returns 0 for a non-custom request in that case
(core.c:1861-1868). It then allocates the new hash and takes
mm->futex.phash.lock. If the mm has switched to the global hash in
between, the check under the lock returns -EBUSY regardless of FH_CUSTOM
(core.c:1939-1947):
if (cur && !cur->hash_mask) {
/*
* If two threads simultaneously request the global
* hash then the first one performs the switch,
* the second one returns here.
*/
free = fph;
mm->futex.phash.hash_new = new;
return -EBUSY;
}
The comment covers two global-hash (custom) requests. It does not cover
a non-custom default resize from clone(), which gets 0 if it arrives a
little later (core.c:1863-1867), but -EBUSY if it arrives in this
window.
Sequence (one process, >= 5 threads, >= 5 online CPUs, default 16-slot
hash):
T1: pthread_create() -> clone(CLONE_VM)
-> futex_hash_allocate_default()
-> futex_hash_allocate(32, 0): passes the RCU check, allocates
T2: prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS, 0, 0)
-> futex_hash_allocate(0, FH_CUSTOM): drops the old hash,
waits for the pivot, installs the global hash
(hash_mask == 0)
T1: takes phash.lock, sees cur->hash_mask == 0 -> -EBUSY
-> pthread_create() fails with EAGAIN/EBUSY.
The window is the allocation in T1 plus the RCU grace periods of T2's
pivot, so it is narrow but needs no privilege. A possible fix is to
return 0 at that point when the request is not FH_CUSTOM, as the
lockless check already does.
Affected: the check under the lock comes from 69a14d146f3b ("futex:
Verify under the lock if hash can be replaced", 2025-06); unchanged in
mainline ebb7a8dc1e74 and next-20261125. Impact: a spurious
thread-creation failure (functional; no memory-safety effect).
Found by a formal analysis tool we are developing that uses AI
assistance; I have reviewed the analysis and this report. The tool runs
verbatim copies of the kernel functions with stubs for the rest of the
kernel; it found the interleaving above, and the interleaving was
replayed natively on that harness under the tool's schedule. Functions
involved: futex_hash_allocate_default(), futex_hash_allocate(),
futex_hash_prctl(), __futex_pivot_hash() (RCU, wait_var_event and the
futex reference transition stubbed). It has not been reproduced on a
booted kernel.
Thanks,
Zhenwen Liang