Re: [PATCH v2] cgroup: Wait for dying tasks to leave on rmdir
From: Sebastian Andrzej Siewior
Date: Tue Mar 24 2026 - 05:18:24 EST
On 2026-03-23 10:02:05 [-1000], Tejun Heo wrote:
> 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().
>
> v2: cgroup_is_populated() true to false transition happens under css_set_lock
> not cgroup_mutex, so retest under css_set_lock before sleeping to avoid
> missed wakeups (Sebastian).
>
> Fixes: a72f73c4dd9b ("cgroup: Don't expose dead tasks in cgroup")
> Reported-by: kernel test robot <oliver.sang@xxxxxxxxx>
> Closes: https://lore.kernel.org/oe-lkp/202603222104.2c81684e-lkp@xxxxxxxxx
> Reported-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
> Signed-off-by: Tejun Heo <tj@xxxxxxxxxx>
> Cc: Bert Karwatzki <spasswolf@xxxxxx>
> Cc: Michal Koutny <mkoutny@xxxxxxxx>
> Cc: cgroups@xxxxxxxxxxxxxxx
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
As mentioned in the other email, if I
- irq_work_queue(this_cpu_ptr(&cgrp_dead_tasks_iwork));
+ schedule_delayed_work(this_cpu_ptr(&cgrp_delayed_tasks_iwork), 1 * HZ);
then I hung at boot because it rmdir() a cgroup with a task in Z. It
might suggest a race because systemd might missed a task.
But this fixes the other issue so.
Sebastian