cgroup: Wait for dying tasks to leave on rmdir
Tejun Heo
maintainerPATCH 1/1 cgroup: Wait for dying tasks to leave on rmdir
a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup") hid PF_EXITING tasks from cgroup.procs so that systemd doesn't see tasks that have already been reaped via waitpid(). However, the populated counter (nr_populated_csets) is only decremented when the task later passes through cgroup_task_dead() in finish_task_switch(). This means cgroup.procs can appear empty while the cgroup is still populated, causing rmdir to fail with -EBUSY.
Fix this by making cgroup_rmdir() wait for dying tasks to fully leave. If the cgroup is populated but all remaining tasks have PF_EXITING set (the task iterator returns none due to the existing filter), wait for a kick from cgroup_task_dead() and retry. The wait is brief as tasks are removed from the cgroup's css_set between PF_EXITING assertion in do_exit() and cgroup_task_dead() in finish_task_switch().
--- include/linux/cgroup-defs.h | 3 ++ kernel/cgroup/cgroup.c | 73 +++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 3 deletions(-)
include/linux/cgroup-defs.h +3 -0
@@ -609,6 +609,9 @@ struct cgroup {/* used to wait for offlining of csses */ wait_queue_head_t offline_waitq; +/* used by cgroup_rmdir() to wait for dying tasks to leave */ +wait_queue_head_t dying_populated_waitq; +/* used to schedule release agent */ struct work_struct release_agent_work;
kernel/cgroup/cgroup.c +70 -3
103 lines not on this page · raw message
Signature · 1 line
2.53.0
Quoted 3 lines · Tejun Heo
kernel/cgroup/cgroup.c +0
-0@@ -6224,6 +6225,63 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
…
Quoted 17 lines
kernel/cgroup/cgroup.c +17
-0+static int cgroup_drain_dying (struct cgroup *cgrp) + __releases(&cgroup_mutex) __acquires(&cgroup_mutex) +{ +struct css_task_iter it; +struct task_struct *task; +DEFINE_WAIT (wait); + +lockdep_assert_held (&cgroup_mutex); +retry: +if (!cgroup_is_populated (cgrp)) +return 0 ; + +/* Same iterator as cgroup.threads - if any task is visible, it's busy */ +css_task_iter_start (&cgrp->self,0 , &it); + task =css_task_iter_next (&it); +css_task_iter_end (&it); +
kernel/cgroup/cgroup.c static int cgroup_destroy_locked(struct cgroup *cgrp) · line 6278
Quoted 10 lines
kernel/cgroup/cgroup.c +10
-0+if (task) +return -EBUSY ; + +/* +* All remaining tasks are PF_EXITING and will pass through +* cgroup_task_dead() shortly. Wait for a kick and retry. +*/ +prepare_to_wait (&cgrp->dying_populated_waitq, &wait, +TASK_UNINTERRUPTIBLE ); +mutex_unlock (&cgroup_mutex);
I had to add here
if (cgroup_is_populated(cgrp))
kernel/cgroup/cgroup.c static int cgroup_destroy_locked(struct cgroup *cgrp) · line 6279
Quoted 1 line
kernel/cgroup/cgroup.c +1
-0+schedule ();
I saw instances on PREEMPT_RT where the above cgroup_is_populated() reported true due to cgrp->nr_populated_csets = 1, the following iterator returned NULL but in that time do_cgroup_task_dead() saw no waiter and continued without a wake_up and then the following schedule() hung.
There is no serialisation between this wait/ check and latter wake. An alternative would be to check and prepare_to_wait() under css_set_lock.
kernel/cgroup/cgroup.c static int cgroup_destroy_locked(struct cgroup *cgrp) · line 6283
Quoted 4 lines
kernel/cgroup/cgroup.c +4
-0+finish_wait (&cgrp->dying_populated_waitq, &wait); +mutex_lock (&cgroup_mutex); + goto retry; +}
Then I added my RCU patch. This led to a problem already during boot up (didn't manage to get to the test suite).
systemd-1 places modprobe-1044 in a cgroup, then destroys the cgroup. It hangs in cgroup_drain_dying() because nr_populated_csets is still 1. modprobe-1044 is still there in Z so the cgroup removal didn't get there yet. That irq_work was quicker than RCU in this case. This can be reproduced without RCU by
- irq_work_queue(this_cpu_ptr(&cgrp_dead_tasks_iwork));
+ schedule_delayed_work(this_cpu_ptr(&cgrp_delayed_tasks_iwork), HZ);
So there is always a one second delay. If I give up waiting after 10secs then it boots eventually and there are no zombies around. The test_core seems to complete…
Having the irq_work as-is, then the "cgroup_dead()" happens on the HZ tick. test_core then complains just with
| not ok 7 test_cgcore_populated
everything else passes. With schedule_work() (as in right away) all tests pass including test_stress.sh
Is there another race lurking?
Sebastian
Tejun Heo
maintainerHello,
On Mon, Mar 23, 2026 at 12:32:52PM +0100, Sebastian Andrzej Siewior wrote:
...
Quoted 5 lines
I saw instances on
PREEMPT_RTwhere the abovecgroup_is_populated()reported true due tocgrp->nr_populated_csets= 1, the following iterator returned NULL but in that timedo_cgroup_task_dead()saw no waiter and continued without awake_upand then the followingschedule()hung.
Ah, right, false->true is protected by cgroup_mutex but true->false is only by css_set_lock. It should check populated again with css_set_lock held and then do prepare_to_wait().
Quoted 2 lines
There is no serialisation between this wait/ check and latter wake. An alternative would be to check and
prepare_to_wait()undercss_set_lock.
Yeap.
Quoted 7 lines
kernel/cgroup/cgroup.c static int cgroup_destroy_locked(struct cgroup *cgrp) · line 6283
Quoted 4 lines
kernel/cgroup/cgroup.c +4
-0+finish_wait (&cgrp->dying_populated_waitq, &wait); +mutex_lock (&cgroup_mutex); + goto retry; +}Then I added my RCU patch. This led to a problem already during boot up (didn't manage to get to the test suite).
Is that the patch to move cgroup_task_dead() to delayed_put_task_struct()? I don't think we can delay populated state update till usage count reaches zero. e.g. bpf_task_acquire() can be used by arbitrary bpf programs and will pin the usage count indefinitely delaying populated state update. Similar to delaying the event to free path, you can construct a deadlock scenario too.
Quoted 5 lines
systemd-1 places modprobe-1044 in a cgroup, then destroys the cgroup. It hangs in
cgroup_drain_dying()becausenr_populated_csetsis still 1. modprobe-1044 is still there in Z so the cgroup removal didn't get there yet. Thatirq_workwas quicker than RCU in this case. This can be reproduced without RCU by
Isn't this the exact scenario? systemd is the one who should reap and drop the usage count but it's waiting for rmdir() to finish which can't finish due to the usage count which hasn't been reapted by systemd? We can't interlock these two. They have to make progress independently.
Quoted 10 lines
(quoted diff) +1
-1- irq_work_queue(this_cpu_ptr(&cgrp_dead_tasks_iwork)); + schedule_delayed_work(this_cpu_ptr(&cgrp_delayed_tasks_iwork), HZ);So there is always a one second delay. If I give up waiting after 10secs then it boots eventually and there are no zombies around. The
test_coreseems to complete…Having the
irq_workas-is, then the "cgroup_dead()" happens on the HZ tick.test_corethen complains just with
| not ok 7test_cgcore_populated
The test is assuming that waitpid() success guarantees cgroup !populated event. While before all these changes, that held, it wasn't intentional and the test just picked up on arbitrary ordering. I'll just remove that particular test.
Thanks.
Signature · 1 line
tejun
Quoted 1 line · Tejun Heo
Hello,
Hi,
Quoted 8 lines
Quoted 2 lines
Then I added my RCU patch. This led to a problem already during boot up (didn't manage to get to the test suite).
Is that the patch to move
cgroup_task_dead()todelayed_put_task_struct()? I don't think we can delay populated state update till usage count reaches zero. e.g.bpf_task_acquire()can be used by arbitrary bpf programs and will pin the usage count indefinitely delaying populated state update. Similar to delaying the event to free path, you can construct a deadlock scenario too.
Okay, then. I expected it to be limited window within a bpf program or the sched_ext.
Quoted 10 lines
Quoted 5 lines
systemd-1 places modprobe-1044 in a cgroup, then destroys the cgroup. It hangs in
cgroup_drain_dying()becausenr_populated_csetsis still 1. modprobe-1044 is still there in Z so the cgroup removal didn't get there yet. Thatirq_workwas quicker than RCU in this case. This can be reproduced without RCU byIsn't this the exact scenario? systemd is the one who should reap and drop the usage count but it's waiting for
rmdir()to finish which can't finish due to the usage count which hasn't been reapted by systemd? We can't interlock these two. They have to make progress independently.
But nobody is holding it back. For some reason systemd-1 did not reap modprobe-1044 first but went first for the rmdir(). I noticed it with RCU first but it was also there after delayed the cleanup by one second without RCU.
Quoted 7 lines
Quoted 7 lines
(quoted diff) +1
-1- irq_work_queue(this_cpu_ptr(&cgrp_dead_tasks_iwork)); + schedule_delayed_work(this_cpu_ptr(&cgrp_delayed_tasks_iwork), HZ);So there is always a one second delay. If I give up waiting after 10secs then it boots eventually and there are no zombies around. The
test_coreseems to complete…
Quoted 8 lines
Quoted 3 lines
Having the
irq_workas-is, then the "cgroup_dead()" happens on the HZ tick.test_corethen complains just with
| not ok 7test_cgcore_populatedThe test is assuming that
waitpid()success guarantees cgroup !populated event. While before all these changes, that held, it wasn't intentional and the test just picked up on arbitrary ordering. I'll just remove that particular test.
okay. Thanks.
Quoted 1 line
Thanks.
Sebastian