Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1303503pxp; Thu, 10 Mar 2022 02:53:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJwTkgfYUl+jjm/aLUFRsYr1Lz9OsrlgldXYOF+egVbYhnamTFiJ5P7ofyDMWWnXuZqHHA7s X-Received: by 2002:a17:906:5c4:b0:6cd:8d9c:3c7d with SMTP id t4-20020a17090605c400b006cd8d9c3c7dmr3760829ejt.554.1646909598260; Thu, 10 Mar 2022 02:53:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646909598; cv=none; d=google.com; s=arc-20160816; b=F68MSaOObWQgRdV5km3Jka6HrHr3GdshEpx66ree+Ty8avgiyrE6S8EwnpzOE0z5HF ZiG3WnIuF9JRnyG0UcCNxhTkjjUgv82kB1JDSpm9zo6gcmjdkPMO7mY9OONAF2bWXu/p hf0T6Wqv2qjZ4AyFjOA5OWhRDNPnLFFVsuOgynAktDUDlvvenN7wTXNgMPvXRQrpbung 8q7Wc47xUmc8/WrOuCKO3oHkthDStNVQxkWzLCKxOHsWsX02gkcaQ6iTWXjIE22jwLl5 tH+xdyvkOchxqjN0Ag2hz+O7VNq/zvignwsWZBopISQaFjpjqcoWIDAYUkiBvVRuyqhx YCBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=vvwr9IQ/HApVfG5EbXjqoEechaWCEohW8qhJvhEvg9w=; b=0aOX2RvKjMJTUqXkGXBHtZDq1VKd4HWxx7x5+rPeO+u4PzqW6aDgjMHVz3FcPzlC35 G/pRuo/f1z0yaQMCjf6c9XRRN5pZChRRIjLg9As+7PXeH7hA0jfYm/GlWYs7o99hT9+a VATfXf6a7scMlV8ST4JqdQ1UD4nuSbUEV/gCY6EZCcWGrLrVAnluiKuEMVS2EbttJsr5 SVDUSqOfEgFeo9Z05m+crVoXx/NoCr/Uoov1uiHytelKR2E9z/pSP8PzjtpstNeKkEWG AYtnFyg119d/usBXslgpaPV142MzAaTn9Rs2fUhMLDAyLIwcKNvPyfKV/cL0U4NLHboc 4dLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="TpUEa9/r"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h3-20020a056402280300b004165e1b7b0csi3156095ede.214.2022.03.10.02.52.55; Thu, 10 Mar 2022 02:53:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b="TpUEa9/r"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240860AbiCJJgZ (ORCPT + 99 others); Thu, 10 Mar 2022 04:36:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230107AbiCJJgY (ORCPT ); Thu, 10 Mar 2022 04:36:24 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B75F8139CEF for ; Thu, 10 Mar 2022 01:35:23 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 1EBA01F441; Thu, 10 Mar 2022 09:35:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1646904922; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=vvwr9IQ/HApVfG5EbXjqoEechaWCEohW8qhJvhEvg9w=; b=TpUEa9/rR3ex7LW6Oy+jnIlV4MER3qTYYp4wCy0wsl4cbS6tIfwC7GrHW7ymYV5DOpIQCm L3NK5u4ovG0QWvAAGWwEz+JTrEp42VudIBROM3fvkAhS8mtfO7up1AFU8vKKjuSqNkOqMV ZHSLHbUB5CUaF7IFPp/aoHmSh4b/LHY= Received: from alley.suse.cz (unknown [10.100.224.162]) by relay2.suse.de (Postfix) with ESMTP id 9793EA3B81; Thu, 10 Mar 2022 09:35:21 +0000 (UTC) From: Petr Mladek To: Andrew Morton , "Eric W . Biederman" Cc: Peter Zijlstra , Mathieu Desnoyers , Kees Cook , Marco Elver , Jens Axboe , Thomas Gleixner , linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH] kthread: Make it clear that create_kthread() might be terminated by any fatal signal Date: Thu, 10 Mar 2022 10:34:55 +0100 Message-Id: <20220310093455.15176-1-pmladek@suse.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The comments in kernel/kthread.c create a feeling that only SIGKILL is able to break create_kthread(). 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. Signed-off-by: Petr Mladek --- kernel/kthread.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index 38c6dd822da8..6f994c354adb 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -341,7 +341,7 @@ static int kthread(void *_create) self = to_kthread(current); - /* If user was SIGKILLed, I release the structure. */ + /* Release the structure when caller killed by a fatal signal. */ done = xchg(&create->done, NULL); if (!done) { kfree(create); @@ -399,7 +399,7 @@ static void create_kthread(struct kthread_create_info *create) /* We want our own signal handler (we take no signals by default). */ pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD); if (pid < 0) { - /* If user was SIGKILLed, I release the structure. */ + /* Release the structure when caller killed by a fatal signal. */ struct completion *done = xchg(&create->done, NULL); if (!done) { @@ -441,9 +441,9 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), */ if (unlikely(wait_for_completion_killable(&done))) { /* - * If I was SIGKILLed before kthreadd (or new kernel thread) - * calls complete(), leave the cleanup of this structure to - * that thread. + * If I was killed by a fatal signal before kthreadd (or new + * kernel thread) calls complete(), leave the cleanup of this + * structure to that thread. */ if (xchg(&create->done, NULL)) return ERR_PTR(-EINTR); @@ -877,7 +877,7 @@ __kthread_create_worker(int cpu, unsigned int flags, * * Returns a pointer to the allocated worker on success, ERR_PTR(-ENOMEM) * when the needed structures could not get allocated, and ERR_PTR(-EINTR) - * when the worker was SIGKILLed. + * when the caller was killed by a fatal signal. */ struct kthread_worker * kthread_create_worker(unsigned int flags, const char namefmt[], ...) @@ -926,7 +926,7 @@ EXPORT_SYMBOL(kthread_create_worker); * Return: * The pointer to the allocated worker on success, ERR_PTR(-ENOMEM) * when the needed structures could not get allocated, and ERR_PTR(-EINTR) - * when the worker was SIGKILLed. + * when the caller was killed by a fatal signal. */ struct kthread_worker * kthread_create_worker_on_cpu(int cpu, unsigned int flags, -- 2.26.2