Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7198175rwi; Mon, 24 Oct 2022 11:05:34 -0700 (PDT) X-Google-Smtp-Source: AMsMyM649ebd4TgT4zya61DWNQpGKgv1P2LMD8wryKy1/Sx/9Rf9PZ7qOzfnOOjeHzjUl3znaTZZ X-Received: by 2002:a63:1d5a:0:b0:46e:d157:39ef with SMTP id d26-20020a631d5a000000b0046ed15739efmr13023795pgm.231.1666634734041; Mon, 24 Oct 2022 11:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666634734; cv=none; d=google.com; s=arc-20160816; b=icnhRHCrEtE3splRyRYg8PACaCP2XFkiztpuG94JKoCJCL7u4utIAiok1XA1peQ1Fa BMnkh9rr/LbkLrLjyBYmHbkjM3g3qWLZBmyZ5T7rIxR/CNQZB2D/L+eLY6ZZ/EwgQhqK V7OCm1rR8uAoumOrtD5qCxrFnuunDv/5qkQXQ4GR0+GvZxZLkcnozIfZ35U7lBJr3kJd ecR9VvA7Yreks7BRFNOhponH2GzfXUnLTIzAR4fj4iuSaI6UkXRLCULaY2dibXutHgfo sES/Re3+AgwVnmlCXDIZo+RizT53dTfci+5eOn/8TGt4yyb4nIUk0LTfEhIU0gUlQ6fq FogQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JFgntZZJ1nTWcozDedFKzvJWX7kZ9Nh58+BqQjLLfpw=; b=vI/2cPht/I8DgcZqtOdVuA5aRNysW7dHd5h+yydn35JTns0BO6S+rchyBnIXHNYbqX quPZ6m2aj/TnyWFTORGRIDi36VC0FvLfZvyKhlIMcqVyB1HEvVWLAezmgnTGUtElAkxR BN/RISvVXZs+iYNaPsf7t7Lwtn/Srn+qi7khMXivqZhynQcLPZJcugH0E6mptHXTCM4b dcQhRagV1f1XTGJ2n/JS8empnFDEVek3UiVrlP5zK5E0/nZUV9VMDJi8dbiGgV/zH2ox AqBuLrpVyKu+dWYpSF5Crzq/TyqTq7Bt5CVGO+5t6nhDOLNcR9W13zLT3jIG7sQNcjOj 9fzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e3hA5o7m; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a170903200300b00186940d7eaasi183982pla.31.2022.10.24.11.05.22; Mon, 24 Oct 2022 11:05:34 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=e3hA5o7m; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231984AbiJXRcX (ORCPT + 99 others); Mon, 24 Oct 2022 13:32:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233324AbiJXRbr (ORCPT ); Mon, 24 Oct 2022 13:31:47 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E489CFBCE6; Mon, 24 Oct 2022 09:07:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 381A9B819F1; Mon, 24 Oct 2022 12:50:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B760C433C1; Mon, 24 Oct 2022 12:50:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666615811; bh=Kztvx2zNIxVQA6r9PyOav8ArBXDUZOl/9LjRaq1bitw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e3hA5o7miPOt5DtD/fTA+q5Ae25dFEEl1luKufkzKan9BlX3XYPezwPQV0zbZOrDO rYgR4oeNvufdSq+9Bvemj7UGorKZBxqv8eSnMKVQFG5LbQXEri0ZEqfNNnh7Ix/WQr gxyigctpTdMETJWKDchFZfk00B7c6tt8uW2ZV3e8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ingo Molnar , Rob Herring , Masahiro Yamada , Nicolas Schier , Sasha Levin Subject: [PATCH 5.15 391/530] kbuild: remove the target in signal traps when interrupted Date: Mon, 24 Oct 2022 13:32:15 +0200 Message-Id: <20221024113102.768106214@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113044.976326639@linuxfoundation.org> References: <20221024113044.976326639@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 From: Masahiro Yamada [ Upstream commit a7f3257da8a86b96fb9bf1bba40ae0bbd7f1885a ] When receiving some signal, GNU Make automatically deletes the target if it has already been changed by the interrupted recipe. If the target is possibly incomplete due to interruption, it must be deleted so that it will be remade from scratch on the next run of make. Otherwise, the target would remain corrupted permanently because its timestamp had already been updated. Thanks to this behavior of Make, you can stop the build any time by pressing Ctrl-C, and just run 'make' to resume it. Kbuild also relies on this feature, but it is equivalently important for any build systems that make decisions based on timestamps (if you want to support Ctrl-C reliably). However, this does not always work as claimed; Make immediately dies with Ctrl-C if its stderr goes into a pipe. [Test Makefile] foo: echo hello > $@ sleep 3 echo world >> $@ [Test Result] $ make # hit Ctrl-C echo hello > foo sleep 3 ^Cmake: *** Deleting file 'foo' make: *** [Makefile:3: foo] Interrupt $ make 2>&1 | cat # hit Ctrl-C echo hello > foo sleep 3 ^C$ # 'foo' is often left-over The reason is because SIGINT is sent to the entire process group. In this example, SIGINT kills 'cat', and 'make' writes the message to the closed pipe, then dies with SIGPIPE before cleaning the target. A typical bad scenario (as reported by [1], [2]) is to save build log by using the 'tee' command: $ make 2>&1 | tee log This can be problematic for any build systems based on Make, so I hope it will be fixed in GNU Make. The maintainer of GNU Make stated this is a long-standing issue and difficult to fix [3]. It has not been fixed yet as of writing. So, we cannot rely on Make cleaning the target. We can do it by ourselves, in signal traps. As far as I understand, Make takes care of SIGHUP, SIGINT, SIGQUIT, and SITERM for the target removal. I added the traps for them, and also for SIGPIPE just in case cmd_* rule prints something to stdout or stderr (but I did not observe an actual case where SIGPIPE was triggered). [Note 1] The trap handler might be worth explaining. rm -f $@; trap - $(sig); kill -s $(sig) $$ This lets the shell kill itself by the signal it caught, so the parent process can tell the child has exited on the signal. Generally, this is a proper manner for handling signals, in case the calling program (like Bash) may monitor WIFSIGNALED() and WTERMSIG() for WCE although this may not be a big deal here because GNU Make handles SIGHUP, SIGINT, SIGQUIT in WUE and SIGTERM in IUE. IUE - Immediate Unconditional Exit WUE - Wait and Unconditional Exit WCE - Wait and Cooperative Exit For details, see "Proper handling of SIGINT/SIGQUIT" [4]. [Note 2] Reverting 392885ee82d3 ("kbuild: let fixdep directly write to .*.cmd files") would directly address [1], but it only saves if_changed_dep. As reported in [2], all commands that use redirection can potentially leave an empty (i.e. broken) target. [Note 3] Another (even safer) approach might be to always write to a temporary file, and rename it to $@ at the end of the recipe. > $(tmp-target) mv $(tmp-target) $@ It would require a lot of Makefile changes, and result in ugly code, so I did not take it. [Note 4] A little more thoughts about a pattern rule with multiple targets (or a grouped target). %.x %.y: %.z When interrupted, GNU Make deletes both %.x and %.y, while this solution only deletes $@. Probably, this is not a big deal. The next run of make will execute the rule again to create $@ along with the other files. [1]: https://lore.kernel.org/all/YLeot94yAaM4xbMY@gmail.com/ [2]: https://lore.kernel.org/all/20220510221333.2770571-1-robh@kernel.org/ [3]: https://lists.gnu.org/archive/html/help-make/2021-06/msg00001.html [4]: https://www.cons.org/cracauer/sigint.html Fixes: 392885ee82d3 ("kbuild: let fixdep directly write to .*.cmd files") Reported-by: Ingo Molnar Reported-by: Rob Herring Signed-off-by: Masahiro Yamada Tested-by: Ingo Molnar Reviewed-by: Nicolas Schier Signed-off-by: Sasha Levin --- scripts/Kbuild.include | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index cdec22088423..914ea5eb16a8 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -96,8 +96,29 @@ echo-cmd = $(if $($(quiet)cmd_$(1)),\ quiet_redirect := silent_redirect := exec >/dev/null; +# Delete the target on interruption +# +# GNU Make automatically deletes the target if it has already been changed by +# the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make +# will delete incomplete targets), and resume it later. +# +# However, this does not work when the stderr is piped to another program, like +# $ make >&2 | tee log +# Make dies with SIGPIPE before cleaning the targets. +# +# To address it, we clean the target in signal traps. +# +# Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM. +# So, we cover them, and also SIGPIPE just in case. +# +# Of course, this is unneeded for phony targets. +delete-on-interrupt = \ + $(if $(filter-out $(PHONY), $@), \ + $(foreach sig, HUP INT QUIT TERM PIPE, \ + trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);)) + # printing commands -cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(cmd_$(1)) +cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1)) ### # if_changed - execute command if any prerequisite is newer than -- 2.35.1