Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1831639pxp; Thu, 10 Mar 2022 13:04:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJzl1PGDrPEI15jPpRCXFb9FEaPf/2ViFfUKkgsuA3thuKDajo90LbzIlumlB5Q7YPDJW+vh X-Received: by 2002:a05:6870:f2a5:b0:da:b3f:2b50 with SMTP id u37-20020a056870f2a500b000da0b3f2b50mr9548388oap.239.1646946264846; Thu, 10 Mar 2022 13:04:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646946264; cv=none; d=google.com; s=arc-20160816; b=E3xui1XQNyOCMnm4FkQ6ZC9mrLNXFBLFy6QGkXL+0VxbhXEyNwVX3HObVRRbUryh8A 3iCnaIoRCaMKRDPcPrE2VVa/tOEMLSejkuh3LK2Hx456vijEMzoJ86zYXeOWAwBlKpZI 2ZlopyK1NHcjiGRbDwgkE8Tueji0JFxzVv+seGVE/ny+3EcoQ9hd67wELoATopRBtye5 gGUhR9JW6Tflg0rpkxcWQTrtWyy6zeGJhAdf/9ys4LMmpYfi7kCSPXrPeQSY2WrMnCVx GzHR6HTCrKiHVM/oqrxlE0ekkoEE8oWINh3t5f0BT7eYScDqbbKjYRZ2MAKNP5mVwyQR fhjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:mime-version:user-agent:message-id :in-reply-to:date:references:cc:to:from; bh=lM2p1RRCFUe5dR20I7oWj3KYmVjDINe/9UypvvM64VM=; b=vzmeg02IxQpnGhhllAc6jlE6FGTumXG6c0xl6XZ1tULxHGhJwHxgsNAOljAV90HSGF Rq6hw0AX8KzuNUcIOHzwsXa6E88xnds1x0O1wKpHnJgzvFLCsNc1tKbC9PzfUl7rKXh+ XND+d00v+BV+EAFnRvyvcLjtLHLVTbpssaR4UaKAvVbr/PJjGIZ5t6IqsIP+WguZSY5E ac7fXgv34+1wDSQqLmdxEALdVVYv1W5nCWTS/CNZbQ26YA7+mUzbxACwyqeBS+Whaz15 Ps9RaCJCml6cQRjZP5tO3L/XMZEgSnIvwyE/9xyXkxRUkuziFhFvfQ/vo6oMJ8vPTDPh b/5g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t27-20020a056870601b00b000da716e6246si2138263oaa.207.2022.03.10.13.04.07; Thu, 10 Mar 2022 13:04:24 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235814AbiCJSQF (ORCPT + 99 others); Thu, 10 Mar 2022 13:16:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231704AbiCJSQE (ORCPT ); Thu, 10 Mar 2022 13:16:04 -0500 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7486613858A for ; Thu, 10 Mar 2022 10:15:01 -0800 (PST) Received: from in01.mta.xmission.com ([166.70.13.51]:51786) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nSNJS-00F8a5-Qx; Thu, 10 Mar 2022 11:14:58 -0700 Received: from ip68-227-174-4.om.om.cox.net ([68.227.174.4]:34804 helo=email.froward.int.ebiederm.org.xmission.com) by in01.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1nSNJQ-004pU3-Go; Thu, 10 Mar 2022 11:14:58 -0700 From: "Eric W. Biederman" To: Petr Mladek Cc: Andrew Morton , Peter Zijlstra , Mathieu Desnoyers , Kees Cook , Marco Elver , Jens Axboe , Thomas Gleixner , linux-kernel@vger.kernel.org References: <20220310093455.15176-1-pmladek@suse.com> Date: Thu, 10 Mar 2022 12:14:28 -0600 In-Reply-To: <20220310093455.15176-1-pmladek@suse.com> (Petr Mladek's message of "Thu, 10 Mar 2022 10:34:55 +0100") Message-ID: <871qz9brp7.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1nSNJQ-004pU3-Go;;;mid=<871qz9brp7.fsf@email.froward.int.ebiederm.org>;;;hst=in01.mta.xmission.com;;;ip=68.227.174.4;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX19tyasZu15blocjvhMTlyqAb97udrw89cE= X-SA-Exim-Connect-IP: 68.227.174.4 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-DCC: XMission; sa06 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;Petr Mladek X-Spam-Relay-Country: X-Spam-Timing: total 1704 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 11 (0.6%), b_tie_ro: 9 (0.5%), parse: 1.52 (0.1%), extract_message_metadata: 17 (1.0%), get_uri_detail_list: 2.6 (0.2%), tests_pri_-1000: 23 (1.4%), tests_pri_-950: 1.26 (0.1%), tests_pri_-900: 1.00 (0.1%), tests_pri_-90: 166 (9.7%), check_bayes: 157 (9.2%), b_tokenize: 11 (0.6%), b_tok_get_all: 9 (0.5%), b_comp_prob: 2.6 (0.2%), b_tok_touch_all: 131 (7.7%), b_finish: 1.00 (0.1%), tests_pri_0: 1457 (85.5%), check_dkim_signature: 0.82 (0.0%), check_dkim_adsp: 3.0 (0.2%), poll_dns_idle: 0.67 (0.0%), tests_pri_10: 4.1 (0.2%), tests_pri_500: 18 (1.0%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH] kthread: Make it clear that create_kthread() might be terminated by any fatal signal X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in01.mta.xmission.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Petr Mladek writes: > The comments in kernel/kthread.c create a feeling that only SIGKILL > is able to break create_kthread(). ^^^^^^^^^^^^^^^^ __kthread_create_on_node Signals can't affect create_kthread in any was as it is only called by kthreadd. It is __kthread_create_on_node which gets interrupted by fatal signals. > > 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. Except for the minor nit in the change description. Reviewed-by: "Eric W. Biederman" > 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,