From: Michal Hocko <mhocko@kernel.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>, Tejun Heo <tj@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Koutny <mkoutny@suse.com>, <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>,
cgroups mailinglist <cgroups@vger.kernel.org>,
Chris Mason <mason@kernel.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
Michal Hocko <mhocko@suse.com>
Subject: [PATCH] futex: Allocate private hash before cgroup_can_fork()
Date: Fri, 4 Dec 2026 09:37:31 +0100 [thread overview]
Message-ID: <20261204083731.564924-1-mhocko@kernel.org> (raw)
From: Chris Mason <mason@kernel.org>
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 <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Michal Hocko <mhocko@suse.com>
---
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
reply other threads:[~2026-12-04 8:37 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=20261204083731.564924-1-mhocko@kernel.org \
--to=mhocko@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mason@kernel.org \
--cc=mhocko@suse.com \
--cc=mkoutny@suse.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=tj@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