From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796373474; b=ie+XninSVoweNJBFdNWm5t5lB6HzxJl4t9gsiemxAlBohnizE7ilkVJmGe1OfWH2D7Te dhPbvGE4cjuXQf83CFdUvmkr4LXypuQvhg/+VmN7NuDzExW6bKtUwXxT2twKPS9R29zbS Po/L+R9c+JCspf1x3Hl+hvwBEtoq+8k5N0zOPxGjJWM51XUQnFFyCjokWrWe0igLsUPsO ae7EkdlhkqPPor2QpFzA2fPGcp+xJytxGRW1o+qGg6EEKNkH+wtA826RJsgkf8yCyzmRh Znf7mguXcaEXONmA4EKxbWkD3pyenTxptq18JDKjMGj/ts0LXPuopuTGFAYQ0lA+uLQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796373474; 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=dvpeoXXEogWc3DT62riA7hSFFpY6TdwB+4z1UZGARGwV4s+p/gc1r80x8s7MG+/xY3AI 4XavzXGBgQwwWjpT6mAuE7SfX2BXn0lS/o8WuSyuUtLlaMkRoxXThGKP3orKJc86NlzkA 7knar7GoIyHZ9fOlZ6TGDKlTOIRUsH2N0MmV7vQUdcJhDHhvjHqFDMh3qlbefxOuEy4dS 7YXWxql7tIKs7ltd0oMHjiDFI1n03hSovefzGZhMowBkDsysiUC76HbQLtXalDMw61Fsb YhsFrPNMwyRQoE9wnxczO1Qwaq0pXK8hJ/5ZltJXFPckn3fGp3UTvwKi+aljIysjeWw== 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.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 7151A606A6AB for ; Fri, 4 Dec 2026 08:37:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7151A606A6AB 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-f43.google.com with SMTP id 5b1f17b1804b1-4b467581b98so4748675e9.0 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=TfeFtZCOya9wqWjAizRxGFN/cYi1Cl+maj58ajB4hSMox0uhqWslG8wi1Xwsy3Zj3P 0xTkl0I6MW/HCGPHmnr+lYR2WTWhlIs/6WXRoWqufNcXlq9p1A6czH4j7Z0WA54ERHPh keP+e6znnHTqdAiQ906r55Gxpc3KpfyV5Gwc4iJeZGZ7odvYY1qkXPvvJxBOryakqy2X XkSxdkqHfh6eUOPcadloxMt/Ckny1WHJyyJ7jz6/+i0ENGu8UJbDhqXWr/6+EL+fuFtq ckkKP82lOwFatqW9MdZS2sTz8Ba8saQifB6aybcWmzOMAILYZh+UnN6QpzEhYU0LxSGn VeRA== X-Forwarded-Encrypted: i=1; AL+hErFmo0QdeL7PBcRSczWHUgUpZW4HvFJdmgOplW3OcRYkwang2zjfcr0Bx3DyTd7nm07cJEF1PTVf@vger.kernel.org X-Gm-Message-State: APuU4Z8tsLBJdHUREKooCV0fswhFetB7OXGJEqRb85S6yHAue2B3Sk1D UH+WQiqwLwfymMbQwLRtrkqm/kThZEHx+i7L6cVPGakb21qW8TPuGP+u X-Gm-Gg: AZFdTZeF1i7pbYHF+5ax6htA/CNfnzSTVh+06oMh/fjQ7redYaU9pt2PDNAvJEcmWof PsxyPivKILdYMAtEU3UnZcRUXSxv9fBHC2gURUesI7v2MIpqVL12f8tJ5yjCdVgCAZC/IWfOMm/ rW62sbWnbBWU7HHiaykXpAoyuAGRcIu5LZdvxVU7/f+DYNH2+RKZWsZuks5fjuKQCQYGSou8M2Q Br95RAw5ITnHDcbuuhO8UBh/cBjiDHUkucOHU5sqID1cUZB9Gx1MX4Lpc15Aa+TNKYdPHYLIG7o TZNEEjEwBuPzCv4h3wDwsmmm+/SxFSa+GyLydGVnGBz2fU6gnj0QYMHw/J17Su1cKqLgEDCDIJ9 cTe+rfJVJOdUGk2JciCArAa3cpIpVGW4sKnLwAO83ejJcfp9OoMLU+PIJWM48ywFKXMqN2fIXA1 uu8c+a/L3aPuOHGDQWbcaG9QQ9iYwQk2ia+cXrfGDJTOy1j6SlWO9uZneDxkb6dmz+zFrxH2Ibu szgjkuD4cwNNEc1lejmE0v9OiTSfnwHEl6AAMspKjHZXg4F8SStikPjAkjgXuRH04S1IE2Eovi3 hdLOb4+LTNHbeBPRTtGoXYspmfRADD2MvFrcX5hIG7h81g== 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: cgroups@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