From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F4E0606A682 for ; Fri, 4 Dec 2026 08:37:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA70A6B05B2; Fri, 4 Dec 2026 03:37:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B588C6B05B3; Fri, 4 Dec 2026 03:37:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6D7B6B05B4; Fri, 4 Dec 2026 03:37:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7E1736B05B2 for ; Fri, 4 Dec 2026 03:37:49 -0500 (EST) Received: from smtpin30.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0ED05802C0 for ; Fri, 4 Dec 2026 08:37:49 +0000 (UTC) X-FDA: 85505647458.30.DE9C5D0 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) by imf10.hostedemail.com (Postfix) with ESMTP id 4617DC000E for ; Fri, 4 Dec 2026 08:37:47 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of mstsxfx@gmail.com designates 209.85.128.54 as permitted sender) smtp.mailfrom=mstsxfx@gmail.com; dmarc=fail reason="SPF not aligned (relaxed)" header.from=kernel.org (policy=quarantine) ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796373467; b=eYLU6sTwflEwwqYLpZUAX9tcnL8pWH3RsXUWtEVechqZxnQ+5N9hntVkzRP0NxIZZKXmN/ tEnsE9AXFXkRlcvKA6cqpLJygfHo9WqYX5SiuDPYNVnLEIae2kr8J2Lb6BeKgQrk+TQ0lz eL4yNg0DnIKFOsbmI5gQ3A4UNXc+QWo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796373467; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=eP329/ppGmfRjGNU2z9HGuftaFeAeNchjvFcG5YpBkQ=; b=VWhWq43xIeTDTmR6bERmrqQ9L89nG3pdjdMRww/gRAcD43J6+wMb/r13Y74LNjSnYdoSXk kZuBVgKZfiooZ7/H6TRu3r85cEaDosKSmgolQidY8j8xdQ32jZ0HCZ3LKBlxJUv57WTFg/ Yej69tW95cCl8pwPvwTnw2FiH0DrMgM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; spf=pass (imf10.hostedemail.com: domain of mstsxfx@gmail.com designates 209.85.128.54 as permitted sender) smtp.mailfrom=mstsxfx@gmail.com; dmarc=fail reason="SPF not aligned (relaxed)" header.from=kernel.org (policy=quarantine) Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4bba5e17728so4147345e9.2 for ; Fri, 04 Dec 2026 00:37:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796373466; x=1796978266; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=eP329/ppGmfRjGNU2z9HGuftaFeAeNchjvFcG5YpBkQ=; b=E8mK3yv0mc9HDXiZwnt4qANwOt/wdCUWX0Dxvyx6S6r4tMLhQh1oBLxU6rEHqc3P+s CTTopQyE1S6BMfECCRfiFMnG8l0ZGc3lTyAdyQ2BSgsWDQWBKkCtGssGOzT9+6eVyGyV cRH4Xk4jslclPFSLMxzzt3LZraiZSTJSgt3bUIxRM5B3/yQ4UqImfxpBg5UMKyAUEgKZ 2BKzrKHWuE/izomNoE9sJTbI5TutZtH4BITmcpd458Xkb7rejf4jM+GT+mRqWU6lz8HD GXVQQ6l04d8CzQPt8iIrupd9eph38gNliiZFV0k+Z6dEGixOh28RiI4j2XGvkjlgGKhS lvPw== X-Forwarded-Encrypted: i=1; AL+hErEU9YqbDtPIDLEHJ9X/kC3pqmjpTeRHhum16Kavvp4S+ZTYdt7K34F+KraFO3UYPVjGbERYvymuRQ==@kvack.org X-Gm-Message-State: APuU4Z8rrNoFT6S0H+qS/w3dAhirkNHy4wyuiXvxvvMqouUuLAiGO9of Lj4yKYWDzWELLvkUTBvkYkG/ADvTf+7h6dYDKlp3w9ONbUp/8aI4kBNP X-Gm-Gg: AZFdTZcw72KkNCSnHxN9SjZhAJXRQ6CLwsEzYQ2V1eqU5AB8NVGk3JU1G9+5rIyR1cJ agwE1zE2sdD5k40NZNLx8v+AHBT+GQJ4UytCB5zbZh1MRwaYauSP/fcAC2UepS7JTMHBdmK26jt uIP9rK3ZyLKAzLXiI/qFL23SUqcKW9pW/KugZhOXt8JnVPM17dcgJ9OrDVtbqMOGcIggVEH06zT y33ru43V8b5759H41Qfr6CygzsZi2G5vpTckQzrAh54EUFchO8gncxkq7VPfCl84dJ22DPu716H h+MTXKG/eTfd8mYl+xBhCzUbvwYB9sP1pPJMRmXWgHQ6Esm+jYzXXKepBKSoMloyl+gc1MAjLbm 2KuWrDi8qHK+RRK7H8PdlXXOlVbE/ksPAj6SP+KtcOYD3KhKSsghbX84T9BEGRYKqeOBq3/XOnN DOuPsBQKd6+IZTcyrhcmd/qN6SrJBzMn65DElDs2V7DoVMlFLbAS/EWH0+7r9LkZ1jWqPCn2iaf styaL64zyg7ft5O7qFCybW6+Oe+GdrvpcGVI9G5ZvgziGtnanRfKK+iG9+b+pZSoWNObxaZiRxd jqJ7Ln5KQLecXKgcPBxMPqDdYVefu1c34iMYa2KS6q1FsQ== X-Received: by 2002:a05:600c:4f89:b0:4bb:6847:7927 with SMTP id 5b1f17b1804b1-4bbeb8cf403mr22373585e9.7.1796373465673; Fri, 04 Dec 2026 00:37:45 -0800 (PST) Received: from tiehlicka (109-81-27-207.rct.o2.cz. [109.81.27.207]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbea2a0484sm37951965e9.11.2026.12.04.00.37.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 00:37:45 -0800 (PST) From: Michal Hocko To: Thomas Gleixner Cc: Peter Zijlstra , Tejun Heo , Johannes Weiner , Michal Koutny , , LKML , cgroups mailinglist , Chris Mason , "David Hildenbrand (Arm)" , Michal Hocko Subject: [PATCH] futex: Allocate private hash before cgroup_can_fork() Date: Fri, 4 Dec 2026 09:37:31 +0100 Message-ID: <20261204083731.564924-1-mhocko@kernel.org> X-Mailer: git-send-email 2.56.1 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4617DC000E X-Stat-Signature: 98zezjpr69q5eniqeqzdzz9qzcwa7kqp X-Rspam-User: X-Rspamd-Pre-Result: action=add header; module=dmarc; Action set by DMARC X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam08 X-Rspam: Yes X-HE-Tag: 1796373467-66851 X-HE-Meta: U2FsdGVkX18B2ctU0G/KWVO5dduzyUdsXw5mNJ81kfWHi9+lcc+UGSQobD7NBve1lLgqszUdHXMDNvvKHkGquf397Fq4Vit79TQYLFygRkU63+m+bY/jy9wK0WUam/BFGpD5rE2v7B6zM5YO9iBqbQGepnETMUlyoKD4cJ2sv9A4qbTDbn9MIqElqchXHNzyCm/FESCremAPSifmEU8wnc0xIHZ3sz+VtE9nTAm5ODuB6ogdBt4B/6wEfQev90UzhmEJcM/+Xvw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vno3MLenzopUaPyGdglMN6bLR8dmzPIL1onODm5uUeuSbX9RtPBu65I9P7cfAwSnCkH6sFeipn/HvNkuXyKC2dDXOIF0x4uqYlJbEhp8B5/ybweFIXmczqKsrYAPt9ZAtU5blP/jOaq4Bf+l/sm+KQuhDXxMzls5n2ChPU14y7l11CHNUoWCltTxy3rIkZxm3g9IYYlUdwK5EfB8VDArYCR4vqwoZj6lhzxlCq3zNHZqU0V0Jj+ENzYpYM= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Chris Mason A clone(CLONE_VM) that allocates a private futex hash can reclaim or sleep at a memory cgroup limit. A task migration between cgroups that starts meanwhile has to wait for the clone(), and every fork and exit on the machine can then stall behind the migration until the allocation finishes. copy_process() holds cgroup_threadgroup_rwsem for reading from cgroup_can_fork() until cgroup_post_fork(). With CLONE_VM it calls futex_hash_allocate_default() in between. If the mm has no private hash yet, or the thread group has outgrown the default one, that call makes a GFP_KERNEL_ACCOUNT allocation under the lock. Every fork and exit takes the same lock for reading, and a waiting writer blocks new readers: clone(CLONE_VM) holds the lock for reading, in memcg reclaim cgroup.procs write waits for that reader every fork and exit waits behind the writer With CLONE_INTO_CGROUP, copy_process() also holds cgroup_mutex across the allocation, so other cgroup operations wait as well. The fix used here is to allocate the hash before cgroup_can_fork() takes these locks. Fixes: 7c4f75a21f63 ("futex: Allow automatic allocation of process wide futex hash") Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Michal Hocko --- kernel/fork.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/kernel/fork.c b/kernel/fork.c index d55a7d21a8fb..154bfc8e1be9 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2427,6 +2427,23 @@ __latent_entropy struct task_struct *copy_process( p->rethooks.first = NULL; #endif + /* + * The futex hash is charged to current's memory cgroup, and the charge + * can reclaim. So allocate it before cgroup_can_fork() takes + * cgroup_threadgroup_rwsem for reading, which every fork and exit + * needs, and, with CLONE_INTO_CGROUP, cgroup_mutex. + */ + if (need_futex_hash_allocate_default(clone_flags)) { + retval = futex_hash_allocate_default(); + if (retval) + goto bad_fork_put_pidfd; + /* + * If we fail beyond this point we don't free the allocated + * futex hash map. We assume that another thread will be created + * and makes use of it. The hash map is freed with the mm. + */ + } + /* * Ensure that the cgroup subsystem policies allow the new process to be * forked. It should be noted that the new process's css_set can be changed @@ -2450,17 +2467,6 @@ __latent_entropy struct task_struct *copy_process( if (retval) goto bad_fork_cancel_cgroup; - if (need_futex_hash_allocate_default(clone_flags)) { - retval = futex_hash_allocate_default(); - if (retval) - goto bad_fork_cancel_cgroup; - /* - * If we fail beyond this point we don't free the allocated - * futex hash map. We assume that another thread will be created - * and makes use of it. The hash map will be freed once the main - * thread terminates. - */ - } /* * From this point on we must avoid any synchronous user-space * communication until we take the tasklist-lock. In particular, we do -- 2.56.1