From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94D4F446C1E for ; Tue, 11 Aug 2026 14:03:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786457003; cv=none; b=RfZ1fcmdIObFgSkbW7Bako8NflYL1YA4dJ5rR1RuvHmMPikLFn8RhpLyZ6mBDBGBw1u6AiPCT0THodByXm7mQZ7OnCh6UKRBnshntKbpIqR/VeeOmWoXxlCDSEiv+qt8vrY3YBBcIjHbNBaVaJ5Pd/t7Gi+lZBxGEM1FDo0n+Xo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786457003; c=relaxed/simple; bh=k/JIMW32dniOKONAZItRLFgYUZi7l6Z7QE6pGb8FzbE=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=e1znQhH3X7nHL85PzL/acMpNP+Cohlkxqh4elZZeT371KJV7RVk/ge2ofETZaGsWUhDBhB5w1HgX7VJU+5qLJvp+P4zw5Kr4tHeb0zjsGDEEtQJ9aqiSb/3rpgMe0qbLxFKKbXPTlaLgp53VZ7Nogy3kt8MF0nRqNKKTsTGeE8I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AMbopZFA; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AMbopZFA" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-8485bd28dd0so914634b3a.2 for ; Tue, 11 Aug 2026 07:03:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786457001; x=1787061801; darn=vger.kernel.org; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=yvN38GKVxnN9MLaRqdllbsXYuN4sPvzA8n+S8qz7gdQ=; b=AMbopZFAfYJX1gkXDj/AHF7+FCBOY8gDb3NTCu0INJqgznVHd7i6168JPjsUA7x0Gr Xi7c/9TLGq4+K6xtsDsxkieWttly05QH1ESUxL9FIctepvn6GTRcvALZBvJx2HZbJ91i hNgGjy9/WGOXrc2j3F1yAjCScb0CG9N3YBex1ivBYOYKuWwWWJtx6rgDabsqqzs5Uj4K uFNLUbX3zF2+e4nR3E/5h8uiMBmwBSAYKLrRFGUCjDo0LrpopjzCPu70m0FwG2q2w4H3 UnTmmbG/oye8JGr6tha+4kE4TlQ4Th88y7zfKQliH0wtJAAl7LSxAJbofwynmgtUb7t7 cJPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786457001; x=1787061801; h=content-disposition:content-type:mime-version:message-id:subject:cc :to:from:date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=yvN38GKVxnN9MLaRqdllbsXYuN4sPvzA8n+S8qz7gdQ=; b=lLKhMJ8a7ykelmgiZdeorTWlbcV21xfuR2TKx/GbwcVj5xNOr6Fo9A44Yg+yo/9V0F 01azJHnSrH1em8GtIxtBQWE/6Da0s7KzSC90MrPrnsDjKUeFJk5SNqlEnGipJuskwgYz o2JHogmNp0zBfjBY64MBxWgRTlzWNSzpbh6KfYBOp5kJ1nudGqSaLjEc+PEm7q8AS9ds o1hB0Rf0B2lkq5BJHF9k3YjC3ZpROqn0WcW9YafzxVlURPP39M0l+vpIGyfu15tZrIDi j8Q73GscuvVf4fKoNmhOqtCuPB3VAcHS/AEFQ9pjkhvvQslNL3FFT6/lPUYh5xjHoxJw 6z1Q== X-Gm-Message-State: AOJu0YwLbNu9y93/Eu7gw+FwP3Sfe+6mgqH8X5S8Hv3gd8Werzi5Dm+z /g7mQIQAyo7ehPrKZ2DVSzNOdiCFZdBujaSiDV1v4pUCkoa4M/cXJ+/9 X-Gm-Gg: AR+sD12MJMv5ah4/IPoVh73mt59T+wRvQlxt674AbV27+1QVDUUAdfwPed50PoiNNJi QxviTZuHMUsRgCpN1tn+FSoibMnqycqr/XFQXKDcVh1Avw9wmptwNpP1PkdlVF3Jkc5aTK//eIF G3Oh9Pr4HP91+JeGNCgjh9snmpYJap9BVvQtXrg/hA87uZr6umXivETt3N3Q4Tw5dLoHpKqfp+7 03i7CVMg8G7gwu3T6Kxp13sMqXPmACrMn1FRbWsfRePi6pT1wJ8wCNmLOtoSSTWygtIG68vphO1 WvF9XGKMCtPuPFb5QH68WfeBJTtAFS4c0vEpYkZLYWpL+tYH8lwf6qFxdHBtu0EtJWmBBJaom8P cZPjyjK1pOGEoZxjVataJAglyFYMeYhUer1tNM06qaAEMseBogBiZn1Dp4fmCU6FRb0ZHotWWbl nHalwbb1L74sg9j8PGd3K4+ZayO6zRQ0ygsy+qOxaPDw5WZNDmsKQDg/EeKnXVaAeFoKkSb0VtS Nt5im2Q X-Received: by 2002:a05:6a00:244a:b0:848:4d4f:d477 with SMTP id d2e1a72fcca58-84faf9a829bmr441032b3a.18.1786457000555; Tue, 11 Aug 2026 07:03:20 -0700 (PDT) Received: from v4bel ([58.123.110.97]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84fa91b5ba1sm887005b3a.20.2026.08.11.07.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2026 07:03:20 -0700 (PDT) Date: Tue, 11 Aug 2026 23:03:16 +0900 From: Hyunwoo Kim To: tglx@kernel.org, mingo@redhat.com, peterz@infradead.org, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com Cc: linux-kernel@vger.kernel.org, imv4bel@gmail.com Subject: [PATCH] futex: Fix race on the initial mm->futex.phash.ref allocation Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline futex_hash_allocate() allocates mm->futex.phash.ref without any locking. Commit d9b05321e21e ("futex: Move futex_hash_free() back to __mmput()") moved the allocation here and assumed that the process has just a single thread at this point. Commit ee9dce44362b ("futex: Drop CLONE_THREAD requirement for private default hash alloc") widened need_futex_hash_allocate_default() to cover any CLONE_VM clone, but left out vfork because the parent is suspended and cannot race. That no longer holds once vfork is nested. If a vfork child calls vfork again and is then killed with SIGKILL, the parent is released from its vfork wait and runs concurrently with the grandchild in the same mm. Neither of them went through futex_hash_allocate_default(). When both call prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS) at the same time, each one sees mm->futex.phash.ref as NULL and stores its own percpu counter. Only the last store survives. The counter stored first is no longer reachable from the mm, so the references on it are not seen by __futex_ref_atomic_end(). A private hash that still has references is then considered dead and freed, and a task that still holds one of its buckets writes into freed memory in futex_q_lock(). Store the counter once with cmpxchg() and let the loser free_percpu() its own. The initial reference has to be taken before the store, otherwise another task can install a private hash while the counter is still 0. Fixes: d9b05321e21e ("futex: Move futex_hash_free() back to __mmput()") Cc: stable@vger.kernel.org Signed-off-by: Hyunwoo Kim --- kernel/futex/core.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/futex/core.c b/kernel/futex/core.c index 128c5752f225c2..806576978fa84c 100644 --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -1842,14 +1842,18 @@ static int futex_hash_allocate(unsigned int hash_slots, unsigned int flags) } if (!mm->futex.phash.ref) { + unsigned int __percpu *ref = alloc_percpu(unsigned int); + + if (!ref) + return -ENOMEM; + /* - * This will always be allocated by the first thread and - * therefore requires no locking. + * Tasks sharing the mm can run this concurrently, so take the + * initial reference before publishing the counter. */ - mm->futex.phash.ref = alloc_percpu(unsigned int); - if (!mm->futex.phash.ref) - return -ENOMEM; - this_cpu_inc(*mm->futex.phash.ref); /* 0 -> 1 */ + this_cpu_inc(*ref); /* 0 -> 1 */ + if (cmpxchg(&mm->futex.phash.ref, NULL, ref)) + free_percpu(ref); } fph = kvzalloc(struct_size(fph, queues, hash_slots), -- 2.43.0