Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zhan Xusheng" <zhanxusheng1024@gmail.com>, tj@kernel.org
Cc: hannes@cmpxchg.org, mkoutny@suse.com, mhocko@suse.com,
	roman.gushchin@linux.dev, shakeel.butt@linux.dev,
	muchun.song@linux.dev, akpm@linux-foundation.org,
	kolyshkin@gmail.com, chris@chrisdown.name, linux-mm@kvack.org,
	zhanxusheng@xiaomi.com
Subject: Re: [PATCH 1/2] cgroup: list a process whose leader has exited where its threads are
Date: Wed, 25 Nov 2026 03:36:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-232471@kernel.org> (raw)
In-Reply-To: <20261124033547.2373352-2-zhanxusheng1024@gmail.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] cgroup: use-after-free on group_leader in css_task_iter_next()
  and procs_stand_in()
- [Medium] cgroup: missing cg_leader_left update in
  cgroup_update_dfl_csses()
- [Medium] cgroup: __cgroup_procs_write() checks permissions against
  left-behind leader cgroup
- [Medium] cgroup: quadratic thread traversal under css_set_lock in
  procs_stand_in()
- [Low] cgroup: procs_live_thread() fallback to leader resurrects
  process in old cgroup
--

commit e947916a597995307d4cc64d14f765928b1b3027
Author: Zhan Xusheng <zhanxusheng@xiaomi.com>

cgroup: list a process whose leader has exited where its threads are

This commit visits a thread group leader that was left behind during a
cgroup.procs migration at the first live thread of the process during
CSS_TASK_ITER_PROCS walks.

Link: https://lore.kernel.org/all/305c5803b904a57c188b505d6ed9e1e4@kernel.org/

> diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
> index b3657e72db99e..ab442f94e29d2 100644
> --- a/kernel/cgroup/cgroup.c
> +++ b/kernel/cgroup/cgroup.c
> @@ -3081,6 +3081,9 @@ int cgroup_migrate(struct task_struct *leader, bool threadgroup,
>  		if (!threadgroup)
>  			break;
>  	} while_each_thread(leader, task);
> +	/* see procs_live_thread() */
> +	if (threadgroup && (leader->flags & PF_EXITING))
> +		WRITE_ONCE(leader->cg_leader_left, true);
>  	spin_unlock_irq(&css_set_lock);
>  
>  	return cgroup_migrate_execute(mgctx);

[Severity: Medium]
Does setting cg_leader_left only in cgroup_migrate() miss migrations driven
by cgroup_update_dfl_csses() and cgroup_transfer_tasks()?

When a controller such as memory is enabled or disabled via
cgroup.subtree_control, cgroup_update_dfl_csses() calls
cgroup_migrate_add_task() and cgroup_migrate_execute() directly without
going through cgroup_migrate().  If a process in a descendant cgroup
already has an exited leader, the live threads move to the new css_set
while the leader stays on the old css_set with cg_leader_left still false,
so controller-scoped CSS_TASK_ITER_PROCS walks such as
mem_cgroup_scan_tasks() will still miss the process.

Should __cgroup_procs_write() also resolve src_cgrp from the live thread's
css_set rather than task_cgroup_from_root(task, &cgrp_dfl_root)?  For
threadgroup migrations, task is the group leader, so after one migration
leaves the exited leader behind, a second cgroup.procs migration checks
cgroup_attach_permissions() against the stale cgroup where the leader
exited rather than the cgroup currently holding the live threads.

> @@ -5200,6 +5203,40 @@ static void css_task_iter_skip(struct css_task_iter *it,
>  	}
>  }
>  
> +/*
> + * A cgroup.procs migration skips a leader that is already exiting, as the
> + * controllers don't deal with exiting tasks, so the leader stays on its
> + * css_set while the rest of its process moves.  CSS_TASK_ITER_PROCS walks
> + * visit such a leader at the first live thread of the process instead, if
> + * that thread is on a different css_set.
> + *
> + * PF_EXITING isn't set under css_set_lock, so a walk that races with a
> + * thread starting to exit may visit the process twice or miss it.
> + */
> +static struct task_struct *procs_live_thread(struct task_struct *leader)
> +{
> +	struct task_struct *t;
> +
> +	if (!READ_ONCE(leader->cg_leader_left))
> +		return leader;
> +
> +	for_each_thread(leader, t) {\n> +		if (!(t->flags & PF_EXITING)) {
> +			if (task_css_set(t) == task_css_set(leader))
> +				return leader;
> +			return t;
> +		}
> +	}
> +	return leader;
> +}

[Severity: Low]
When the last live thread of a migrated process enters do_exit(), can
falling back to returning leader at the end of procs_live_thread() make the
process transiently reappear in its old cgroup?

In exit_signals(), a dying thread sets PF_EXITING before decrementing
signal->live.  If a CSS_TASK_ITER_PROCS walk on the old cgroup visits the
left-behind leader on dying_tasks during that window, every thread already
has PF_EXITING set while signal->live is still non-zero, so
procs_live_thread(leader) returns leader and css_task_iter_advance() does
not skip it.

> +
> +static bool procs_stand_in(struct task_struct *task)
> +{
> +	struct task_struct *leader = task->group_leader;
> +
> +	return leader != task && procs_live_thread(leader) == task;
> +}

[Severity: Medium]
Can calling procs_stand_in() for every non-leader thread cause quadratic
thread-list traversal under css_set_lock?

When an N-thread process with cg_leader_left set is exiting, threads that
have set PF_EXITING in exit_signals() remain on cset->tasks and
signal->thread_head until they pass exit_notify() and do_cgroup_task_dead().
Because neither css_task_iter_advance() nor procs_stand_in() filters out
tasks with PF_EXITING or tasks on the same css_set as their leader before
calling procs_live_thread(leader), a single css_task_iter_advance() call
under spin_lock_irqsave(&css_set_lock) walks all N threads on cset->tasks
and rescans across the exiting threads at the head of signal->thread_head
each time.  In addition, cgroup_migrate() sets cg_leader_left even when no
tasks move or cgroup_migrate_execute() fails, enabling this scan when all
threads are still on the leader's css_set.

> +
>  static void css_task_iter_advance(struct css_task_iter *it)
>  {
>  	struct task_struct *task;
> @@ -5246,8 +5283,18 @@ static void css_task_iter_advance(struct css_task_iter *it)
>  		goto repeat;
>  
>  	if (it->flags & CSS_TASK_ITER_PROCS) {
> -		/* if PROCS, skip over tasks which aren't group leaders */
> -		if (!thread_group_leader(task))
> +		/*
> +		 * If PROCS, skip over tasks which aren't group leaders, except
> +		 * for the thread a left-behind leader is visited at.
> +		 */
> +		if (!thread_group_leader(task)) {
> +			if (!procs_stand_in(task))
> +				goto repeat;
> +			return;
> +		}
> +
> +		/* and leaders visited at another thread */
> +		if (procs_live_thread(task) != task)
>  			goto repeat;
>  
>  		/* and dying leaders w/o live member threads */
> @@ -5322,6 +5369,9 @@ struct task_struct *css_task_iter_next(struct css_task_iter *it)
>  	while (it->task_pos && !it->cur_task) {
>  		it->cur_task = list_entry(it->task_pos, struct task_struct,
>  					  cg_list);
> +		/* a left-behind leader is visited at a live thread */
> +		if (it->flags & CSS_TASK_ITER_PROCS)
> +			it->cur_task = it->cur_task->group_leader;
>  		/* a task on dying_tasks with zero refcount is only valid for
>  		 * RCU readers, not even interesting for
>  		 * CSS_TASK_ITER_WITH_DEAD, find another one */

[Severity: High]
Can css_task_iter_next() and procs_stand_in() dereference a freed
group_leader after the stand-in thread exits?

A non-leader thread T does not hold a reference on its group_leader L, and
T->cg_list remains linked on dst_cset->tasks until the end of do_exit() in
do_task_dead() -> cgroup_task_dead() (or the deferred irq_work on
CONFIG_PREEMPT_RT).  Once T passes release_task(T) -> __exit_signal(T), L
can be reaped and RCU-freed while T is still on dst_cset->tasks, while
cgroup_task_free(L) only skips iterators on L's own src_cset:

CPU0                                  CPU1
css_task_iter_start()
  css_task_iter_advance()
    /* parks it->task_pos at T */
  spin_unlock_irqrestore(&css_set_lock)
                                      do_exit() /* last live thread T */
                                        exit_notify()
                                          release_task(T)
                                          /* zap_leader or parent wait */
                                          release_task(L)
                                            put_task_struct_rcu_user(L)
                                        /* RCU grace period elapses */
                                        delayed_put_task_struct(L)
                                          free_task(L)
css_task_iter_next()
  spin_lock_irqsave(&css_set_lock)
  it->cur_task = T->group_leader
  tryget_task_struct(L)  <-- use-after-free

The same stale T->group_leader is also dereferenced in
css_task_iter_advance() -> procs_stand_in(T) -> procs_live_thread(L) if a
sibling thread T2 calls execve() -> de_thread() after T passes
__exit_signal(T): de_thread() reaps and RCU-frees L without updating
T->group_leader (because T was already unlinked from signal->thread_head),
and signal->live remains non-zero because T2 is alive, so
css_task_iter_advance() does not skip T before calling procs_stand_in(T).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124033547.2373352-1-zhanxusheng1024@gmail.com?part=1


  reply	other threads:[~2026-11-25  3:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  3:35 [PATCH 0/2] cgroup: list processes whose leader has exited where their threads are Zhan Xusheng
2026-11-24  3:35 ` [PATCH 1/2] cgroup: list a process whose leader has exited where its " Zhan Xusheng
2026-11-25  3:36   ` sashiko-bot [this message]
2026-11-24  3:35 ` [PATCH 2/2] mm: memcontrol: find the OOM group of a process " Zhan Xusheng
2026-11-25  3:36   ` sashiko-bot
2026-11-24 18:47 ` [PATCH 0/2] cgroup: list processes whose leader has exited where their " Tejun Heo

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-232471@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=chris@chrisdown.name \
    --cc=hannes@cmpxchg.org \
    --cc=kolyshkin@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=zhanxusheng1024@gmail.com \
    --cc=zhanxusheng@xiaomi.com \
    /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