cgroup: fix cgroup.kill for exited thread group leader
cover cgroup: fix cgroup.kill for exited thread group leader
cgroup.kill fails to kill a process whose thread group leader has exited while other threads are still running, leaving the cgroup populated. This can be used by a process in a container to survive the container being killed, including the case of a container with a private PID namespace, where such a process is the container's init.
Patch 1 fixes __cgroup_kill() to walk all threads and to send a thread-group-directed SIGKILL. Patch 2 adds selftests for both scenarios described in patch 1; they fail without the fix.
Tested on v7.3-rc7 (x86_64 VM): test_kill passes (30 runs), other cgroup selftests show no changes compared to the unpatched kernel.
The issue was found in crun, see patch 1 for details.
---
Kir Kolyshkin (2):
cgroup: fix cgroup.kill for exited thread group leader selftests/cgroup: test cgroup.kill with an exited thread group leader
kernel/cgroup/cgroup.c | 4 +- tools/testing/selftests/cgroup/test_kill.c | 158 +++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 2 deletions(-)
---
base-commit: f909e534f5e34feb2ea2701d54df63e21aa506fc
change-id: 20261018-cgroup-kill-exited-leader-a15159e8c230
Best regards,
Signature · 1 line
Kir Kolyshkin <[email protected]>
PATCH 1/2 cgroup: fix cgroup.kill for exited thread group leader
cgroup.kill does not work in case a process' thread group leader is gone but other threads are still running. This is because __cgroup_kill() only walks the thread group leaders and sends SIGKILL to each of them with send_sig(), which uses PIDTYPE_PID:
1. The exited leader is still listed by CSS_TASK_ITER_PROCS as long as the process has live threads, but a signal directed at a single exiting task never takes effect: complete_signal() does not look for another thread to deliver it to, and __send_signal_locked() now drops it altogether. The process keeps running.
2. The live threads of such a process can be migrated to another cgroup by writing any of their IDs to cgroup.procs, while the exited leader stays behind. The destination cgroup has no thread group leader to iterate, so writing to its cgroup.kill does nothing at all.
In both cases the cgroup stays populated after cgroup.kill, and a process inside a container can use this to survive the container being killed. This also works with a private PID namespace when the init of the container is the process, as the PID namespace is not torn down while init has live threads.
Case 1 can be fixed by using do_send_sig_info(..., PIDTYPE_TGID) instead of send_sig(), so that the signal is taken by a live thread even if the task found by the iterator is exiting. Note it was pointed out by Eric in [1] while reviewing v2 of cgroup.kill patchset, and v3 was meant to switch to it, but the change was only made in cgroup_post_fork(), leaving __cgroup_kill() with send_sig().
Case 2 can only be fixed by iterating over all threads, so this is what the fix also does. Once a thread group is being killed, its other threads are skipped since they already have a fatal signal pending.
I ran some performance checks and the added cost is ~60 ns per every thread on my laptop (and less than a millisecond for 10K threads), which seems low. The time to "populated 0" seems unchanged.
The issue was found in crun by Anthropic's OSS Scanner, and crun is getting a workaround for it [2] (which does what we needed to do before cgroup.kill was added).
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/containers/crun/pull/2351
--- kernel/cgroup/cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/cgroup/cgroup.c +2 -2
@@ -4289,7 +4289,7 @@ static void __cgroup_kill(struct cgroup *cgrp) cgrp->kill_seq++;spin_unlock_irq (&css_set_lock); -css_task_iter_start (&cgrp->self,CSS_TASK_ITER_PROCS |CSS_TASK_ITER_THREADED , &it); +css_task_iter_start (&cgrp->self,CSS_TASK_ITER_THREADED , &it);while ((task =css_task_iter_next (&it))) {/* Ignore kernel threads here. */ if (task->flags &PF_KTHREAD )
@@ -4299,7 +4299,7 @@ static void __cgroup_kill(struct cgroup *cgrp)if (__fatal_signal_pending (task))continue ; -send_sig (SIGKILL , task,0 ); +do_send_sig_info (SIGKILL ,SEND_SIG_NOINFO , task,PIDTYPE_TGID ); }css_task_iter_end (&it); }
Signature · 1 line
2.55.0
Quoted 16 lines · Kir Kolyshkin
cgroup.kill does not work in case a process' thread group leader is gone but other threads are still running. This is because
__cgroup_kill()only walks the thread group leaders and sends SIGKILL to each of them withsend_sig(), which usesPIDTYPE_PID:1. The exited leader is still listed by
CSS_TASK_ITER_PROCSas long as the process has live threads, but a signal directed at a single exiting task never takes effect:complete_signal()does not look for another thread to deliver it to, and__send_signal_locked()now drops it altogether. The process keeps running.2. The live threads of such a process can be migrated to another cgroup by writing any of their IDs to cgroup.procs, while the exited leader stays behind. The destination cgroup has no thread group leader to iterate, so writing to its cgroup.kill does nothing at all.
...
Quoted 10 lines
Case 1 can be fixed by using
do_send_sig_info(..., PIDTYPE_TGID)instead ofsend_sig(), so that the signal is taken by a live thread even if the task found by the iterator is exiting. Note it was pointed out by Eric in [1] while reviewing v2 of cgroup.kill patchset, and v3 was meant to switch to it, but the change was only made incgroup_post_fork(), leaving__cgroup_kill()withsend_sig().Case 2 can only be fixed by iterating over all threads, so this is what the fix also does. Once a thread group is being killed, its other threads are skipped since they already have a fatal signal pending.
...
kernel/cgroup/cgroup.c static void __cgroup_kill(struct cgroup *cgrp) · line 4292
Quoted 8 lines
kernel/cgroup/cgroup.c +1
-1@@ -4289,7 +4289,7 @@ static void __cgroup_kill(struct cgroup *cgrp) cgrp->kill_seq++;spin_unlock_irq (&css_set_lock); -css_task_iter_start (&cgrp->self,CSS_TASK_ITER_PROCS |CSS_TASK_ITER_THREADED , &it); +css_task_iter_start (&cgrp->self,CSS_TASK_ITER_THREADED , &it);
I guess this change alone is enough to fix both problems?
But even if I am right,
kernel/cgroup/cgroup.c static void __cgroup_kill(struct cgroup *cgrp) · line 4302
Quoted 6 lines
kernel/cgroup/cgroup.c +1
-1@@ -4299,7 +4299,7 @@ static void __cgroup_kill(struct cgroup *cgrp)if (__fatal_signal_pending (task))continue ; -send_sig (SIGKILL , task,0 ); +do_send_sig_info (SIGKILL ,SEND_SIG_NOINFO , task,PIDTYPE_TGID );
I agree that do_send_sig_info(PIDTYPE_TGID) makes more sense.
Either way, I am not sure about SEND_SIG_NOINFO. Perhaps __cgroup_kill() should use SEND_SIG_PRIV? I dunno.
Oleg.
Quoted 40 lines · Oleg Nesterov
Quoted 16 lines · Kir Kolyshkin
cgroup.kill does not work in case a process' thread group leader is gone but other threads are still running. This is because
__cgroup_kill()only walks the thread group leaders and sends SIGKILL to each of them withsend_sig(), which usesPIDTYPE_PID:1. The exited leader is still listed by
CSS_TASK_ITER_PROCSas long as the process has live threads, but a signal directed at a single exiting task never takes effect:complete_signal()does not look for another thread to deliver it to, and__send_signal_locked()now drops it altogether. The process keeps running.2. The live threads of such a process can be migrated to another cgroup by writing any of their IDs to cgroup.procs, while the exited leader stays behind. The destination cgroup has no thread group leader to iterate, so writing to its cgroup.kill does nothing at all.
...
Quoted 10 lines
Case 1 can be fixed by using
do_send_sig_info(..., PIDTYPE_TGID)instead ofsend_sig(), so that the signal is taken by a live thread even if the task found by the iterator is exiting. Note it was pointed out by Eric in [1] while reviewing v2 of cgroup.kill patchset, and v3 was meant to switch to it, but the change was only made incgroup_post_fork(), leaving__cgroup_kill()withsend_sig().Case 2 can only be fixed by iterating over all threads, so this is what the fix also does. Once a thread group is being killed, its other threads are skipped since they already have a fatal signal pending.
...
Quoted 6 lines
kernel/cgroup/cgroup.c +0
-0@@ -4289,7 +4289,7 @@ static void __cgroup_kill(struct cgroup *cgrp) cgrp->kill_seq++;spin_unlock_irq (&css_set_lock);
Quoted 4 lines
kernel/cgroup/cgroup.c static void __cgroup_kill(struct cgroup *cgrp) · line 4292
Quoted 2 lines
kernel/cgroup/cgroup.c +1
-1-css_task_iter_start (&cgrp->self,CSS_TASK_ITER_PROCS |CSS_TASK_ITER_THREADED , &it); +css_task_iter_start (&cgrp->self,CSS_TASK_ITER_THREADED , &it);I guess this change alone is enough to fix both problems?
Correct; my initial version only had this change, and it fixes both issues.
Quoted 11 lines
But even if I am right,
kernel/cgroup/cgroup.c static void __cgroup_kill(struct cgroup *cgrp) · line 4302
Quoted 6 lines
kernel/cgroup/cgroup.c +1
-1@@ -4299,7 +4299,7 @@ static void __cgroup_kill(struct cgroup *cgrp)if (__fatal_signal_pending (task))continue ; -send_sig (SIGKILL , task,0 ); +do_send_sig_info (SIGKILL ,SEND_SIG_NOINFO , task,PIDTYPE_TGID );I agree that
do_send_sig_info(PIDTYPE_TGID)makes more sense.
Indeed, I added it for "making sense" (rather than for fixing the issue) after reading through the initial cgroup.kill patchset reviews. Eric flagged both places in v2, but only one was fixed in v3.
Quoted 9 lines
Acked-by Oleg Nesterov <[email protected]>Either way, I am not sure about
SEND_SIG_NOINFO. Perhaps__cgroup_kill()should useSEND_SIG_PRIV? I dunno.Oleg.
PATCH 2/2 selftests/cgroup: test cgroup.kill with an exited thread group leader
In reply to Kir KolyshkinAdd two tests for cgroup.kill and a process whose thread group leader has exited while another thread is still running:
- test_cgkill_exited_leader kills the cgroup the process is in;
- test_cgkill_exited_leader_migrated moves the remaining thread to another cgroup, where the process is not listed in cgroup.procs, and kills that cgroup.
Both check that the cgroup is no longer populated afterwards.
--- tools/testing/selftests/cgroup/test_kill.c | 158 +++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+)
tools/testing/selftests/cgroup/test_kill.c +158 -0
@@ -2,10 +2,13 @@#include <errno.h> #include <linux/limits.h> +#include <sched.h> +#include <signal.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/syscall.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h>
@@ -315,6 +318,159 @@ static int test_cgkill_clone_into_killed(const char *root)return ret; } +static int sleep_forever (void *arg) +{ +for (;;) +pause (); +return 0 ; +} + +/* +* Start a thread and exit the main thread only, leaving behind a zombie +* thread group leader with a live thread. +*/ +static int exit_leader_fn (const char *cgroup,void *arg) +{ +const size_t stack_size =64 *1024 ; +char *stack =malloc (stack_size); + +if (!stack) +return EXIT_FAILURE ; + +if (clone (sleep_forever, stack + stack_size, +CLONE_VM |CLONE_FS |CLONE_FILES |CLONE_SIGHAND | +CLONE_THREAD |CLONE_SYSVSEM ,NULL ) <0 ) +return EXIT_FAILURE ; + +syscall (SYS_exit,0 ); +return EXIT_FAILURE ; +} + +static int wait_for_zombie_leader (pid_t pid) +{ +int i; + +for (i =0 ; i <100 ; i++) { +if (!proc_read_strstr (pid, false,"stat" ,") Z " )) +return 0 ; +usleep (10000 ); + } + +return -1 ; +} + +/* +* Test that cgroup.kill kills a process whose thread group leader has +* already exited while its other threads are still running. +*/ +static int test_cgkill_exited_leader (const char *root) +{ +int ret =KSFT_FAIL ; +char *cgroup =NULL ; +pid_t pid = -ESRCH ; + + cgroup =cg_name (root,"cg_test_exited_leader" ); +if (!cgroup) + goto cleanup; + +if (cg_create (cgroup)) + goto cleanup; + + pid =cg_run_nowait (cgroup, exit_leader_fn,NULL ); +if (pid <0 ) + goto cleanup; + +if (wait_for_zombie_leader (pid)) + goto cleanup; + +if (cg_read_strcmp (cgroup,"cgroup.events" ,"populated 1\n" )) + goto cleanup; + +if (cg_write (cgroup,"cgroup.kill" ,"1" )) + goto cleanup; + +if (cg_read_strcmp_wait (cgroup,"cgroup.events" ,"populated 0\n" )) + goto cleanup; + + ret =KSFT_PASS ; + +cleanup: +if (pid >0 ) { +kill (pid,SIGKILL ); +wait_for_pid (pid); + } +if (cgroup) +cg_destroy (cgroup); +free (cgroup); +return ret; +} + +/* +* Test that cgroup.kill kills the threads of a process that were migrated +* to the cgroup after their thread group leader had exited, and so are +* not listed in its cgroup.procs. +*/ +static int test_cgkill_exited_leader_migrated (const char *root) +{ +char *parent =NULL , *src =NULL , *dst =NULL ; +int ret =KSFT_FAIL ; +pid_t pid = -ESRCH ; + + parent =cg_name (root,"cg_test_exited_leader_migrated" ); +if (!parent) + goto cleanup; + + src =cg_name (parent,"src" ); +if (!src) + goto cleanup; + + dst =cg_name (parent,"dst" ); +if (!dst) + goto cleanup; + +if (cg_create (parent) ||cg_create (src) ||cg_create (dst)) + goto cleanup; + + pid =cg_run_nowait (src, exit_leader_fn,NULL ); +if (pid <0 ) + goto cleanup; + +if (wait_for_zombie_leader (pid)) + goto cleanup; + +/* The exiting leader stays behind, only the live thread is moved. */ +if (cg_enter (dst, pid)) + goto cleanup; + +if (cg_read_strcmp (dst,"cgroup.events" ,"populated 1\n" ) || +cg_read_strcmp (dst,"cgroup.procs" ,"" )) + goto cleanup; + +if (cg_write (dst,"cgroup.kill" ,"1" )) + goto cleanup; + +if (cg_read_strcmp_wait (dst,"cgroup.events" ,"populated 0\n" )) + goto cleanup; + + ret =KSFT_PASS ; + +cleanup: +if (pid >0 ) { +kill (pid,SIGKILL ); +wait_for_pid (pid); + } +if (dst) +cg_destroy (dst); +if (src) +cg_destroy (src); +if (parent) +cg_destroy (parent); +free (dst); +free (src); +free (parent); +return ret; +} +#define T (x) { x, #x }struct cgkill_test {int (*fn)(const char *root);
@@ -324,6 +480,8 @@ struct cgkill_test {T (test_cgkill_tree),T (test_cgkill_forkbomb),T (test_cgkill_clone_into_killed), +T (test_cgkill_exited_leader), + T(test_cgkill_exited_leader_migrated), };#undef T
Signature · 1 line
2.55.0
Quoted 3 lines
Kir Kolyshkin (2):
cgroup: fix cgroup.kill for exited thread group leader selftests/cgroup: test cgroup.kill with an exited thread group leader
Applied 1-2 to cgroup/for-7.4-fixes with the following tags added to patch 1:
Thanks.
--
tejun
PATCH 1/1 cgroup: don't kill migrated processes through their exiting threads
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.
---
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(-)
kernel/cgroup/cgroup.c +1 -1
@@ -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); }
Signature · 1 line
2.53.0
Re: [PATCH] cgroup: don't kill migrated processes through their exiting threads
Applied to cgroup/for-7.4-fixes with the following tag added:
Cc: [email protected] # v5.14+
Thanks.
--
tejun