Skip to content

Commit d25c83c

Browse files
pmladekakpm00
authored andcommitted
kthread: make it clear that kthread_create_on_node() might be terminated by any fatal signal
The comments in kernel/kthread.c create a feeling that only SIGKILL is able to terminate the creation of kernel kthreads by kthread_create()/_on_node()/_on_cpu() APIs. In reality, wait_for_completion_killable() might be killed by any fatal signal that does not have a custom handler: (!siginmask(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \ (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL) static inline void signal_wake_up(struct task_struct *t, bool resume) { signal_wake_up_state(t, resume ? TASK_WAKEKILL : 0); } static void complete_signal(int sig, struct task_struct *p, enum pid_type type) { [...] /* * Found a killable thread. If the signal will be fatal, * then start taking the whole group down immediately. */ if (sig_fatal(p, sig) ...) { if (!sig_kernel_coredump(sig)) { [...] do { task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); sigaddset(&t->pending.signal, SIGKILL); signal_wake_up(t, 1); } while_each_thread(p, t); return; } } } Update the comments in kernel/kthread.c to make this more obvious. The motivation for this change was debugging why a module initialization failed. The module was being loaded from initrd. It "magically" failed when systemd was switching to the real root. The clean up operations sent SIGTERM to various pending processed that were started from initrd. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Petr Mladek <[email protected]> Reviewed-by: "Eric W. Biederman" <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Kees Cook <[email protected]> Cc: Marco Elver <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 3173346 commit d25c83c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

kernel/kthread.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static int kthread(void *_create)
340340

341341
self = to_kthread(current);
342342

343-
/* If user was SIGKILLed, I release the structure. */
343+
/* Release the structure when caller killed by a fatal signal. */
344344
done = xchg(&create->done, NULL);
345345
if (!done) {
346346
kfree(create);
@@ -398,7 +398,7 @@ static void create_kthread(struct kthread_create_info *create)
398398
/* We want our own signal handler (we take no signals by default). */
399399
pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
400400
if (pid < 0) {
401-
/* If user was SIGKILLed, I release the structure. */
401+
/* Release the structure when caller killed by a fatal signal. */
402402
struct completion *done = xchg(&create->done, NULL);
403403

404404
if (!done) {
@@ -440,9 +440,9 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),
440440
*/
441441
if (unlikely(wait_for_completion_killable(&done))) {
442442
/*
443-
* If I was SIGKILLed before kthreadd (or new kernel thread)
444-
* calls complete(), leave the cleanup of this structure to
445-
* that thread.
443+
* If I was killed by a fatal signal before kthreadd (or new
444+
* kernel thread) calls complete(), leave the cleanup of this
445+
* structure to that thread.
446446
*/
447447
if (xchg(&create->done, NULL))
448448
return ERR_PTR(-EINTR);
@@ -876,7 +876,7 @@ __kthread_create_worker(int cpu, unsigned int flags,
876876
*
877877
* Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
878878
* when the needed structures could not get allocated, and ERR_PTR(-EINTR)
879-
* when the worker was SIGKILLed.
879+
* when the caller was killed by a fatal signal.
880880
*/
881881
struct kthread_worker *
882882
kthread_create_worker(unsigned int flags, const char namefmt[], ...)
@@ -925,7 +925,7 @@ EXPORT_SYMBOL(kthread_create_worker);
925925
* Return:
926926
* The pointer to the allocated worker on success, ERR_PTR(-ENOMEM)
927927
* when the needed structures could not get allocated, and ERR_PTR(-EINTR)
928-
* when the worker was SIGKILLed.
928+
* when the caller was killed by a fatal signal.
929929
*/
930930
struct kthread_worker *
931931
kthread_create_worker_on_cpu(int cpu, unsigned int flags,

0 commit comments

Comments
 (0)