From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796373477; b=tY1/CMGghfEE8dezJsEHPpUPcXmJif6gNytiCXDEblquugnJfbIaUYsN3x4hXYmENiib AGY+XEdlXdEAHB7Gsee3WdY3lSnreAfmPJEgcijyPS6E3PIdjYP6REJgbNZBYMSEQ6+sr dnv5vFm+j23AKuQJsqCxeLyBlKSiUDIasHog6JqXLo7cOvaDHkvIcm0dqAhq3TXzSa1Li xQ1eLGvibj7AeNmJmuUK8/S1s/h14z2vevxOubVwTr/OqZrDdjcoiJwdP3x1WDC2QHOlg PeB3ezL4751MStGHK4sADMUuQ2cAQVVJMy92SgINXLpj7UZqycNt0WGZqrqJ5df+KlQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796373477; h=DMARC-Filter:Received:X-Google-DKIM-Signature:X-Forwarded-Encrypted: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=eP329/ppGmfRjGNU2z9HGuftaFeAeNchjvFcG5YpBkQ=; b=4ek9yDSqVKcmU3vJ0tb55+1g7zUgJxshYoIaIXKARtgtIICHEWOYmlvFr8V0l0QFOnGD Tu9dfFY3BLK9+eIWORcSJYm4pJNXUnISe8ZKIeLwbIQ+TYLkq+Nu5KtJHd4/NDWYZjXg8 y607POO5HkvNBpf5tGxCMwi/LwQMzll9bZ5DlhLpi7/QN/W0iAUgMFcubbP6ZEdlHmQ6s YIZSdhNphf2GCGgAwZIFELYCMOwn3gGZZ/RdCqsSbH7e2iABknGlssQZgtMp9HGVY2X8o g1Y+oYR4bZrCqrzCnRA1rAHNKwzkNpNNR/nmZz9m7trgfnS91cFrsVfpT4uBm6l7+uA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.remote-ip=209.85.128.50 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F3D1606A33F for ; Fri, 4 Dec 2026 08:37:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6F3D1606A33F Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4bb64ef5a00so4053995e9.3 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=nVHoWZfJJw619OWCpih1VxUVt172FTlffLlQ9/wjydwmpsIt6xuFVUkG9BRonzpWat x4KmBB03UpayXA6nUjGMCpbfErxE69UR26qFJbvOuMfJueWt6YYxbM/Mm2NHfFaZnyeO ZDhdcBMMgueZCaVIWxp+vmv0xQl5Ux6MCmPNouhNhIOf2HcwcZe4OYgEwwQyuLuM2qOE 2EH8mk/TXIVLnmeYdNwjrqkrDfI00BpuT/zAfRSSxLPgqIwcuLa4MOGKyR2XiTnve0WV WbTbgh/Bzqerl1orsJN2b3LPbMBK4C1K4yTnJNujhYP2PoxaMO7hNrqN0m839ZMla3kg hU/g== X-Forwarded-Encrypted: i=1; AL+hErHgqfAi8svNsyvI+Vudr95kLsYf7z4mLHJgJ6MJS04mqqM09eT6xejUAuEo0+w45uUmKjYekLN1RcDvMhk=@vger.kernel.org X-Gm-Message-State: APuU4Z/41kBnolFeVSeyDYzsMdxGCR7Ln/em4CE/SsTSfHVExGYpKT/a C8LePE6gsaeko2U7VbHWMPH/OGgQ08IBb1JPR+nvWT3QyWxrSzOE9FF0 X-Gm-Gg: AZFdTZeKGfajZvJ396xOK1zFNsQzNdk0WzBg1RGBMXbNb89QysDyn7+w7pJoKkNeV2e ziO1H6d2tvXMy7Kqf/7CqRHGXhFT5zEAag1hnDCk8kAgncwgGT7W0nAd+++uTLNg5MLfmYXk0U6 hq2OTZ8KY1vxihVqPBv9NzQlQgYFUVAfMiSHVNgSAHn+72DZxAAfDyJTIQAlkW22iSW97hwbt7U 7oWSN5ZHwIL3ljwt6O7G+mpb/S9b5UwFWkBoXAg8KLpg86uSTZJ9aJ0hG/eZP/wGyNF973jp8di wfqoI7lGjqVoNn51EW61L5Gpkfu9hq3VjI/Cctz7eWC7ytIx7PLAkVU/9DJ9vfymMsW6NtbcqAF FA3oiOXSJmurJmhpkYg5JDGGnDP9Fmn0tBSp/fvqAcfsL5XnuLeRO+nOer2cOkkqLlb/6rsKfGD FFJpEksA61RPtdb1NqI3VrhYtSNOivA8Rcpo8stS3jAkex/7vGCs7LPeQrGXUZ2jdAm20sC9Je/ AJUoD28L2gqAU1x2Ny3arodEKP1y0K0I3+yDJ+rPp3kg6rlARvIGLUm/dj7roGKIjjxBkclbxCM t2bMxpUlAMM1NkDoVs+hlUdmUiv790XMGbgGQ3ORti1wOg== 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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