mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: Tejun Heo <tj@kernel.org>, Kir Kolyshkin <kolyshkin@gmail.com>,
	Oleg Nesterov <oleg@redhat.com>
Cc: "Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Koutný" <mkoutny@suse.com>,
	cgroups@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] cgroup: don't kill migrated processes through their exiting threads
Date: Fri, 23 Oct 2026 15:46:23 -0400	[thread overview]
Message-ID: <20261023194623.3566511-1-sashal@kernel.org> (raw)
In-Reply-To: <6cfe7cba3720e25400118d43643e55a6@kernel.org>

Since commit da13e0e72a73 ("cgroup: fix cgroup.kill for exited thread
group leader"), __cgroup_kill() visits every thread in the cgroup and
sends SIGKILL with PIDTYPE_TGID, so each visited thread takes down its
whole thread group.

A thread that has started exiting can stay in a cgroup after its process
has been moved out of it. cgroup_migrate_add_task() skips PF_EXITING
tasks, and the thread is only unlinked by cgroup_task_dead() after its
final context switch. mm_release() clears the thread's clear_child_tid
word and wakes the joiner well before that, so pthread_join() can return
while the thread is still in the old cgroup.

If cgroup.kill is written in that window, __cgroup_kill() finds the
exiting thread and complete_signal() passes the group-wide SIGKILL on to
a live thread, killing a process that already left the cgroup. The cgroup
selftest test_stress.sh runs into this: test_cgcore_proc_migration()
joins its threads, moves itself back to the root cgroup, and cg_destroy()
falls back to cgroup.kill on the old cgroup when rmdir() fails with
EBUSY, so test_core kills itself. KernelCI has been reporting
"with_stress.sh: line 94: ... Killed" on several arm64 boards since the
commit reached linux-next.

Keep visiting every thread, but go back to sending SIGKILL to the thread
that was found. A SIGKILL sent to a live thread still kills its whole
thread group, so both cases fixed by that commit stay fixed, and
task_can_queue_signal() drops one sent to a thread that is already
exiting.

Fixes: da13e0e72a73 ("cgroup: fix cgroup.kill for exited thread group leader")
Link: https://lore.kernel.org/all/179272264722.329188.4773475583351189022@6e52bff8000c/
Signed-off-by: Sasha Levin <sashal@kernel.org>
---

da13e0e72a73 is in cgroup/for-7.4-fixes and not in mainline yet, so this
can also be folded into it. Skipping PF_EXITING tasks in the loop also
fixes it, if keeping PIDTYPE_TGID is preferred.

To reproduce without waiting for the race in test_stress.sh, fork a child
that moves itself into a cgroup, creates and joins a dozen threads, moves
back to the root cgroup and writes 1 to the old cgroup's cgroup.kill. A
thread doing mmap()/munmap() in a loop widens the window. In a 2 CPU KVM
guest running next-20261022, 436 of 20000 children killed themselves this
way, and none did with this patch. test_kill passes with and without it,
including the two tests added with da13e0e72a73.

 kernel/cgroup/cgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index faccc793325e5..a284225f8dc95 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -4313,7 +4313,7 @@ static void __cgroup_kill(struct cgroup *cgrp)
 		if (__fatal_signal_pending(task))
 			continue;
 
-		do_send_sig_info(SIGKILL, SEND_SIG_NOINFO, task, PIDTYPE_TGID);
+		send_sig(SIGKILL, task, 0);
 	}
 	css_task_iter_end(&it);
 }
-- 
2.53.0


  reply	other threads:[~2026-10-23 19:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18 22:04 [PATCH 0/2] cgroup: fix cgroup.kill for exited thread group leader Kir Kolyshkin
2026-10-18 22:04 ` [PATCH 1/2] " Kir Kolyshkin
2026-10-19 12:57   ` Oleg Nesterov
2026-10-20  6:22     ` Kirill Kolyshkin
2026-10-18 22:04 ` [PATCH 2/2] selftests/cgroup: test cgroup.kill with an " Kir Kolyshkin
2026-10-19 23:27 ` [PATCH 0/2] cgroup: fix cgroup.kill for " Tejun Heo
2026-10-23 19:46   ` Sasha Levin [this message]
2026-10-23 20:26     ` [PATCH] cgroup: don't kill migrated processes through their exiting threads 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=20261023194623.3566511-1-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kolyshkin@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=tj@kernel.org \
    /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

all inboxes | Powered by JetHome®