mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhenwen Liang <zhenwenliang@google.com>
To: Thomas Gleixner <tglx@kernel.org>, Peter Zijlstra <peterz@infradead.org>
Cc: "Ingo Molnar" <mingo@redhat.com>,
	"Darren Hart" <dvhart@infradead.org>,
	"Davidlohr Bueso" <dave@stgolabs.net>,
	"André Almeida" <andrealmeid@igalia.com>,
	linux-kernel@vger.kernel.org
Subject: futex: clone(CLONE_VM) can fail with -EBUSY while PR_FUTEX_HASH switches to the global hash
Date: Thu, 26 Nov 2026 02:35:40 +0000	[thread overview]
Message-ID: <20261126023540.4161546-1-zhenwenliang@google.com> (raw)

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

                 reply	other threads:[~2026-11-26  2:35 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=20261126023540.4161546-1-zhenwenliang@google.com \
    --to=zhenwenliang@google.com \
    --cc=andrealmeid@igalia.com \
    --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®