Re: [PATCH] cgroup: fix race between fork and cgroup.kill

From: Michal Koutný
Date: Fri Jan 31 2025 - 04:33:51 EST


On Thu, Jan 30, 2025 at 04:05:42PM -0800, Shakeel Butt <shakeel.butt@xxxxxxxxx> wrote:
> @@ -6668,6 +6668,7 @@ void cgroup_post_fork(struct task_struct *child,
> struct kernel_clone_args *kargs)
> __releases(&cgroup_threadgroup_rwsem) __releases(&cgroup_mutex)
> {
> + unsigned int cgrp_kill_seq = 0;

This initialization is only needed for the extra "init tasks" branch
(the value should equal to whatever init_css_set.dfl_cgroup has, i.e.
0).

> unsigned long cgrp_flags = 0;
> bool kill = false;
> struct cgroup_subsys *ss;
> @@ -6681,10 +6682,13 @@ void cgroup_post_fork(struct task_struct *child,
>
> /* init tasks are special, only link regular threads */
> if (likely(child->pid)) {
> - if (kargs->cgrp)
> + if (kargs->cgrp) {
> cgrp_flags = kargs->cgrp->flags;
> - else
> + cgrp_kill_seq = kargs->cgrp->kill_seq;
> + } else {

This should not be strictly necessary thanks to cgroup_mutex during
cgroup_kill and CLONE_INTO_CGROUP.

> cgrp_flags = cset->dfl_cgrp->flags;
> + cgrp_kill_seq = cset->dfl_cgrp->kill_seq;
> + }
>
> WARN_ON_ONCE(!list_empty(&child->cg_list));
> cset->nr_tasks++;
> @@ -6719,7 +6723,7 @@ void cgroup_post_fork(struct task_struct *child,
> * child down right after we finished preparing it for
> * userspace.
> */
> - kill = test_bit(CGRP_KILL, &cgrp_flags);
> + kill = kargs->kill_seq != cgrp_kill_seq;
> }
>
> spin_unlock_irq(&css_set_lock);

The above are only notes for better understanding, I don't think the fix
needs changes in that regard. Thanks

Reviewed-by: Michal Koutný <mkoutny@xxxxxxxx>

Attachment: signature.asc
Description: PGP signature