Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shakeel Butt" <shakeel.butt@linux.dev>, "Tejun Heo" <tj@kernel.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutny" <mkoutny@suse.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Christian Brauner" <brauner@kernel.org>,
	"Oleg Nesterov" <oleg@redhat.com>,
	"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"T . J . Mercier" <tjmercier@google.com>,
	"Usama Arif" <usama.arif@linux.dev>,
	"Meta kernel team" <kernel-team@meta.com>,
	linux-mm@kvack.org
Subject: Re: [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze
Date: Tue, 01 Dec 2026 17:36:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-243868@kernel.org> (raw)
In-Reply-To: <20261130173425.4117363-12-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] cgroup/freezer: css_rightmost_descendant() skip race in
  cgroup_freeze()
--

commit 48267b7abae601cb0b19d3c5740a69c447f762a6
Author: Shakeel Butt <shakeel.butt@linux.dev>

cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze

This commit replaces cgroup_mutex in the cgroup.freeze write path with a
dedicated cgroup_freeze_mutex, css_set_lock protection for freeze state
transitions, and a two-pass RCU walk over the cgroup subtree and its tasks.

> diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
> index b905df5e416bf..41c8e22668cdd 100644
> --- a/kernel/cgroup/freezer.c
> +++ b/kernel/cgroup/freezer.c

[ ... ]

> @@ -319,5 +366,34 @@ void cgroup_freeze(struct cgroup *cgrp, bool freeze)
>  		TRACE_CGROUP_PATH(notify_frozen, cgrp,
>  				  test_bit(CGRP_FROZEN, &cgrp->flags));
>  		cgroup_file_notify(&cgrp->events_file);
> +		goto out_unlock;
> +	}
> +
> +	/*
> +	 * Do change actual state: freeze or unfreeze the tasks of the cgroups
> +	 * changed above.  A descendant with its own freeze set kept its state,
> +	 * and so did its subtree.  Only writers, which hold
> +	 * cgroup_freeze_mutex, change freeze.
> +	 */
> +	rcu_read_lock();
> +	css_for_each_descendant_pre(css, &cgrp->self) {
> +		if (css != &cgrp->self && css->cgroup->freezer.freeze) {
> +			css = css_rightmost_descendant(css);
> +			continue;
> +		}
> +		if (!css_tryget_online(css))
> +			continue;
> +		rcu_read_unlock();
> +
> +		cgroup_do_freeze(css->cgroup, freeze);
> +		cond_resched();
> +
> +		rcu_read_lock();
> +		css_put(css);
>  	}
> +	rcu_read_unlock();

[Severity: Medium]
Can the second pass step into a newly created cgroup inside a skipped
subtree and run cgroup_do_freeze() on it?

Without cgroup_mutex, cgroup_create() can append a child to a descendant D
(where D->freezer.freeze is true) after css_rightmost_descendant() has
scanned that level:

CPU0                                  CPU1
cgroup_freeze(cgrp, false)
  // 2nd pass reaches D (D->freezer.freeze == true)
  css = css_rightmost_descendant(&D->self)
    // returns R (rightmost under D)
                                      cgroup_create(R, ...)
                                        spin_lock_irq(&css_set_lock)
                                        E->freezer.e_freeze = true
                                        set_bit(CGRP_FREEZE, &E->flags)
                                        set_bit(CGRP_FROZEN, &E->flags)
                                        list_add_tail_rcu(&E->self.sibling,
                                                          &R->self.children)
                                        spin_unlock_irq(&css_set_lock)
  // loop step:
  css = css_next_descendant_pre(R, &cgrp->self)
    // sees E via css_next_child(NULL, R)
    // returns &E->self
  if (E->freezer.freeze) // false
  cgroup_do_freeze(E, false)  <-- runs on still-frozen child E

Unlike the first pass, which checks the inherited e_freeze state on every
visited cgroup, the second pass only checks css->cgroup->freezer.freeze.
Because E->freezer.freeze is false on the new child, the walk does not skip E
and calls cgroup_do_freeze(E, freeze) with the outer walk's freeze argument.

Although cgroup_do_freeze() sets each task according to its own cgroup's
CGRP_FREEZE bit, it still emits a false cgroup_unfreeze (or duplicate
cgroup_freeze) trace event for E and calls cgroup_update_frozen(E), which can
prematurely clear CGRP_FROZEN and fire a cgroup.events notification if a task
moved or forked into E has not yet entered the freezer trap.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130173425.4117363-1-shakeel.butt@linux.dev?part=11


  reply	other threads:[~2026-12-01 17:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 17:34 [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 05/11] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 07/11] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-01 17:36   ` sashiko-bot
2026-12-01 17:55   ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-01 17:36   ` sashiko-bot
2026-12-01 17:55   ` Andrew Morton
2026-12-01 17:56   ` Andrew Morton
2026-11-30 17:34 ` [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-01 17:36   ` sashiko-bot [this message]
2026-12-01 17:56   ` Andrew Morton
2026-12-01 17:55 ` [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Andrew Morton

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=sashiko-outbox-243868@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=brauner@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    --cc=usama.arif@linux.dev \
    /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