[PATCH] futex: Allocate private hash before cgroup_can_fork()
From: Michal Hocko
Date: Fri Dec 04 2026 - 03:38:16 EST
From: Chris Mason <mason@xxxxxxxxxx>
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@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
Signed-off-by: Michal Hocko <mhocko@xxxxxxxx>
---
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