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 v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze
Date: Sat, 05 Dec 2026 18:42:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-251809@kernel.org> (raw)
In-Reply-To: <20261204184041.1489976-13-shakeel.butt@linux.dev>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] cgroup/freezer: stale CGRP_FROZEN on depopulation during
  cgroup_freeze()
- [High] cgroup/freezer: cgroup_freeze() pass 2 skips cgroups
  depopulated after pass 1
- [Medium] cgroup/freezer: unbroken rcu_read_lock() in cgroup_freeze()
  pass 1 walk
- [Medium] cgroup/freezer: cgroup_do_freeze() holds css_set_lock across
  task wakeups
- [Medium] cgroup/freezer: cgroup_freeze() locks css_set_lock before
  cgroup_is_dead()
- [Low] cgroup/freezer: 32-bit cgroup_freeze_seq wraparound collides
  with walk_seq
- [Low] cgroup/freezer: pass 2 scans all children on empty root in
  cgroup_freeze()
--

commit 1daf17f41b09f5eb26f17c686d2813abbc4f604d
Author: Shakeel Butt <shakeel.butt@linux.dev>

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

This commit replaces cgroup_mutex in cgroup.freeze with a dedicated
cgroup_freeze_mutex and css_set_lock.  It splits the subtree update into an
RCU pass that updates cgroup freeze state and a second pass that signals
tasks in populated cgroups.

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

[ ... ]

> @@ -202,7 +212,17 @@ static void cgroup_do_freeze(struct cgroup *cgrp, bool freeze, u64 ts_nsec)
>  		 */
>  		if (task->flags & PF_KTHREAD)
>  			continue;
> -		cgroup_freeze_task(task, freeze);
> +
> +		/*
> +		 * Without cgroup_mutex, @task can have migrated since the
> +		 * iterator returned it, and cgroup_freezer_migrate_task() has
> +		 * already set it to its new cgroup's state.  Set it to the
> +		 * state of the cgroup it is in now.
> +		 */
> +		spin_lock_irq(&css_set_lock);
> +		tcgrp = task_dfl_cgroup(task);
> +		cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &tcgrp->flags));
> +		spin_unlock_irq(&css_set_lock);
>  	}
>  	css_task_iter_end(&it);

[Severity: Medium]
Does holding global css_set_lock across cgroup_freeze_task() create heavy
lock contention when freezing or thawing a populated cgroup?

css_task_iter_next() already acquires and drops css_set_lock for each task,
and re-acquiring it here holds the IRQ-disabled spinlock across
lock_task_sighand() and signal_wake_up() or wake_up_process() (taking
pi_lock, rq->lock, and sending reschedule IPIs).  Each signalled or woken
task then immediately contends on css_set_lock in cgroup_enter_frozen() or
cgroup_leave_frozen(), compounding contention with host-wide fork and exit.

Only reading task_dfl_cgroup(), testing CGRP_FREEZE, and updating
JOBCTL_TRAP_FREEZE (plus setting TIF_SIGPENDING on freeze) need
css_set_lock; the wakeup or kick can run after releasing siglock and
css_set_lock.

[ ... ]

> @@ -257,55 +284,96 @@ void cgroup_freezer_migrate_task(struct task_struct *task,
>  	cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &dst->flags));
>  }
>  
> -void cgroup_freeze(struct cgroup *cgrp, bool freeze)
> +/*
> + * Serializes cgroup.freeze writers.  The freeze state is protected by
> + * css_set_lock, which cgroup_create() uses to inherit it, so cgroup_mutex is
> + * not needed.
> + */
> +static DEFINE_MUTEX(cgroup_freeze_mutex);
> +
> +/* Numbers the cgroup_freeze() calls, protected by cgroup_freeze_mutex */
> +static unsigned int cgroup_freeze_seq;
> +
> +int cgroup_freeze(struct cgroup *cgrp, bool freeze)

[ ... ]

> +	WRITE_ONCE(cgrp->freezer.freeze, freeze);
> +	spin_unlock_irq(&css_set_lock);
> +	seq = ++cgroup_freeze_seq;

[Severity: Low]
Can 32-bit cgroup_freeze_seq wrap around to 0 and collide with walk_seq?

cgroup_create() zero-initializes walk_seq, and cgroup_freeze_seq increments
without skipping 0.  After 2^32 state-changing writes, pass 2 will falsely
match untouched or newly created populated cgroups (or long-lived cgroups
holding an earlier matching walk_seq) and call cgroup_do_freeze() on
subtrees that pass 1 skipped, emitting wrong freeze/unfreeze trace events
and doing extra task walks and wakeups.  Using u64 for cgroup_freeze_seq
and walk_seq avoids wraparound.

[ ... ]

> +	rcu_read_lock();
>  	css_for_each_descendant_pre(css, &cgrp->self) {
> -		dsct = css->cgroup;
> +		struct cgroup *dsct = css->cgroup;
> +		bool e_freeze;
>  
> -		if (cgroup_is_dead(dsct))
> +		spin_lock_irq(&css_set_lock);
> +		if (cgroup_is_dead(dsct)) {
> +			spin_unlock_irq(&css_set_lock);
> +			css = css_rightmost_descendant(css);
>  			continue;
> +		}

[Severity: Medium]
Should pass 1 check cgroup_is_dead(dsct) before acquiring css_set_lock?

Dying cgroups remain on parent->self.children until subsystem references
such as memcg drain, so a subtree with many lingering dead cgroups will
repeatedly bounce global css_set_lock in a tight loop and contend with
host-wide fork and exit.  Because CSS_ONLINE only transitions 1->0 and
cgroup_is_dead() already uses READ_ONCE(), a lockless fast-path check
before spin_lock_irq(&css_set_lock) (keeping the locked re-check for
concurrent rmdir) avoids taking the lock for dead cgroups.

[ ... ]

> +		dsct->freezer.e_freeze = e_freeze;
> +		dsct->freezer.walk_seq = seq;
> +		cgroup_set_freeze(dsct, e_freeze);
>  
>  		/*
> -		 * Do change actual state: freeze or unfreeze.
> +		 * No task in an empty subtree needs to be signalled.  Update its
> +		 * frozen state now, so that the second pass can skip the whole
> +		 * branch.  Tasks that enter it later get the new CGRP_FREEZE
> +		 * state when they fork or migrate.
>  		 */
> -		cgroup_do_freeze(dsct, freeze, ts_nsec);
> +		if (!cgroup_is_populated(dsct)) {
> +			if (e_freeze)
> +				TRACE_CGROUP_PATH(freeze, dsct);
> +			else
> +				TRACE_CGROUP_PATH(unfreeze, dsct);
> +			cgroup_update_frozen(dsct);
> +		}
> +		spin_unlock_irq(&css_set_lock);
>  		applied = true;
>  	}
> +	rcu_read_unlock();

[Severity: Medium]
Can this unbroken rcu_read_lock() stall RCU grace periods or trigger
softlockups on large or deep hierarchies?

max_descendants and max_depth default to INT_MAX, and each iteration takes
css_set_lock, formats tracepoint paths, and for unpopulated cgroups calls
cgroup_update_frozen(), which walks all ancestors up to the root (O(D^2)
steps when unfreezing a chain of depth D).  Periodically pinning css,
dropping rcu_read_lock(), and calling cond_resched() on need_resched(), as
pass 2 already does, would avoid stalling RCU and preemption on
!CONFIG_PREEMPT_RCU kernels.

[ ... ]

> +	rcu_read_lock();
> +	css_for_each_descendant_pre(css, &cgrp->self) {
> +		if (css->cgroup->freezer.walk_seq != seq ||
> +		    !cgroup_is_populated(css->cgroup)) {
> +			css = css_rightmost_descendant(css);
> +			continue;
> +		}

[Severity: High]
Can skipping !cgroup_is_populated() here leave CGRP_FROZEN permanently
stale if a cgroup becomes unpopulated between pass 1 and pass 2?

Suppose parent P has no direct tasks and an independently frozen child C
(C->freezer.freeze == 1, CGRP_FROZEN == 1) with a frozen task T:

CPU0                              CPU1
cgroup_freeze(P, true/false)
  pass 1:
    cgroup_set_freeze(P, ...)
    cgroup_is_populated(P) == true
    (skips cgroup_update_frozen(P))
    (skips C: e_freeze unchanged)
                                  cgroup_migrate_execute() (T: C -> U)
                                    css_set_move_task()
                                      (depopulates C and P)
                                    cgroup_freezer_migrate_task()
                                      cgroup_update_frozen(C)
                                        (C stays frozen, delta == 0;
                                         returns without updating P)
  pass 2:
    !cgroup_is_populated(P)
    css = css_rightmost_descendant()  <-- skips cgroup_update_frozen(P)

On freeze, P stays permanently stuck at CGRP_FROZEN == 0 and userspace
waiting on cgroup.events hangs; on unfreeze, P stays stuck at
CGRP_FROZEN == 1 with ancestors' nr_frozen_descendants permanently
inflated by 1.

In the opposite direction, because pass 1 sets walk_seq even when
!cgroup_is_populated(dsct), a task entering dsct before pass 2 causes pass
2 to run cgroup_do_freeze() and emit a duplicate freeze/unfreeze trace
event.  Setting walk_seq = seq only when cgroup_is_populated(dsct) in pass
1 and checking only walk_seq != seq in pass 2 would fix both races.

[Severity: High]
Can an exiting task finishing in a dying child also depopulate a parent
between pass 1 and pass 2 and leave its CGRP_FROZEN permanently stale?

If child D of P was removed by rmdir while an exiting task T was past
exit_signals(), D keeps P populated until do_cgroup_task_dead():

CPU0                              CPU1
cgroup_freeze(P, true/false)
  pass 1:
    cgroup_set_freeze(P, ...)
    cgroup_is_populated(P) == true
    (skips cgroup_update_frozen(P))
    (skips dead child D)
                                  do_cgroup_task_dead(T)
                                    css_set_move_task()
                                      (depopulates D and P)
                                    cgroup_update_frozen(D)
                                      cgroup_is_dead(D) -> returns
                                      (P is never updated)
  pass 2:
    !cgroup_is_populated(P)
    css = css_rightmost_descendant()  <-- skips cgroup_update_frozen(P)

Because cgroup_update_frozen() returns immediately for a dead cgroup, P
never gets its CGRP_FROZEN flag or ancestor nr_frozen_descendants updated.

[Severity: Low]
Does pass 2 still perform an O(N) walk of all immediate children when the
entire subtree is empty?

When cgrp has no tasks in its subtree, pass 1 handles every changed cgroup
and sets applied = true, so pass 2 still starts at &cgrp->self, sees
!cgroup_is_populated(cgrp), and calls css_rightmost_descendant(&cgrp->self).
Because ->prev is not RCU-safe, css_rightmost_descendant() linearly walks
every child in cgrp->self.children before exiting the loop.  Tracking
whether pass 1 encountered any populated changed cgroup (or skipping pass 2
when !cgroup_is_populated(cgrp)) avoids this traversal.

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


  reply	other threads:[~2026-12-05 18:42 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:40 [PATCH v3 00/12] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 01/12] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-12-05  7:54   ` Tejun Heo
2026-12-04 18:40 ` [PATCH v3 02/12] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 03/12] cgroup: refuse cgroup.kill below a threaded cgroup Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 04/12] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 05/12] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 06/12] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 07/12] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 08/12] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 09/12] selftests/cgroup: test cgroup.kill in and below a threaded cgroup Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 10/12] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 11/12] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-05 18:42   ` sashiko-bot
2026-12-04 18:40 ` [PATCH v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-05 18:42   ` sashiko-bot [this message]
2026-12-04 18:54 ` [PATCH v3 00/12] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
  -- strict thread matches above, loose matches on Subject: below --
2026-12-04 23:16 [PATCH v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Espen Tveit
2026-12-04 23:35 ` Shakeel Butt
2026-12-05  2:10   ` Espen Tveit

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-251809@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