Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1115487rdb; Tue, 19 Sep 2023 23:07:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHp2TL7PtoJATP1cOnvqhcpxOrPib3E7Wfu9yVCJWY2NxHdO4DM+k1Dbayf0HX1pOcimEcU X-Received: by 2002:a81:ab45:0:b0:583:b186:d817 with SMTP id d5-20020a81ab45000000b00583b186d817mr1437693ywk.27.1695190052672; Tue, 19 Sep 2023 23:07:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695190052; cv=none; d=google.com; s=arc-20160816; b=UdiU2NvAGZMMOms5ArTK1WpzULuZPZWRWcse7HM8PDv+ORys7WdtC+1JCUCf3ZtkLc Fxzh1mA2RrNiy5NQ4nrCBqxtnv+nJr1Z7XbpAnoA3TVz1QNdXNwRT+M7iUiTKnJDk1OE VKkyVMjhDAbA0aWOAjH1lZLFpe4SrYLJ2qWgIrXIzLX/s13jApUTUa7NOn2idPwfQcy5 0TtG9OKpNAVV/xmjLMz/iKuTD3aWoYLzyR8DGKvYSwkzPT6fKfaMOz8Orxyr0OzV0wJs kOuEUU8HFxoOWd+QykS5zYPzSQMzvHiMSTPa5TFt5AAGFBzaCSlWbm6IwYS1MNx8sMmc dxAQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=3M6IXxDMcErVEoXMIDzEHrM+hhZey9Pc7y7dowbbNtk=; fh=3gevDrX7WuWUbena6o12/5hyTTSGeHznSST72uYLt3c=; b=RJkWIdtnK+Z3W01INSojVs/Z9zqkZWY4b9ZxucKhQd+olXS8hSSqSrAWlarFeb4Pug jSTeg6BknXI9o5ydcJPN4B8Gln7DR9ay0uBqyyp2bVQ1ZxhcRMIkQMbRBzUsV6TsKgJc wuhPfmk/bLL9LtxYQD6fwz/Teq7o4D4G2cFoMACAift+qr0KCODarZ9AZZ9OTxnLIok2 S0wCQy0ZueE3jlkYU/+GivhaT/ZUs7AdiBu5Ga25uA2UTiCI6M5VN/zmwN+YMYT0Au19 ApeLoVklOJHre8kscbDgT899HhIJB+DHpKsOjiecwiXod9K/VO0W0qhIVblfpYIYQs/S XnMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ykzLmHkU; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id f16-20020a63e310000000b0057458518e21si11101633pgh.630.2023.09.19.23.07.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 23:07:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ykzLmHkU; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B7CFB82CFA5C; Tue, 19 Sep 2023 16:10:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233708AbjISXKJ (ORCPT + 99 others); Tue, 19 Sep 2023 19:10:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233578AbjISXJ1 (ORCPT ); Tue, 19 Sep 2023 19:09:27 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4637BF1 for ; Tue, 19 Sep 2023 16:09:18 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1695164956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3M6IXxDMcErVEoXMIDzEHrM+hhZey9Pc7y7dowbbNtk=; b=ykzLmHkUV4QAIDn9PfSIf+5hV7yYHau/MwfSByro1+/RRo7xJ+fEdTKVzcdEcGiflpDxuO kjhSt3Zfya8Jid1zZi845nZJDhzpuhJEb0q3/Fcq++wddKi7Tl1uLEi4FT1haWi+es+l9Y dzyLYYnM4K/b0K0XJqsKPIPvLEv9kpe8Co4LCXg3656Db/fUAyCMJtb0z9Quatqcew/2r9 i1kjEQuv0D67WRfbU8ew0zqcZe5bBTk+IuMcU6gwgqjCNgnIZqAPxazLb5yAWxaixS3aCt QwchMoJlviiIAzHllzE1v4+PtMxza/htLXQLUVyF0UKwidvdTK9D5mLcm5cnUw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1695164956; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3M6IXxDMcErVEoXMIDzEHrM+hhZey9Pc7y7dowbbNtk=; b=NhLLwrLA38y5dN+KiT8IFLeDnAO76+WTX2vBcMJuxRtcbkK7T7OaHXmxLEaVWISZQUzkwD jr7HtmziecRcpUCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Kees Cook , Luis Chamberlain , Andrew Morton , Peter Zijlstra , Josh Poimboeuf , Arnd Bergmann , "Guilherme G. Piccoli" , Andy Shevchenko Subject: [PATCH printk v2 09/11] panic: Add atomic write enforcement to oops Date: Wed, 20 Sep 2023 01:14:54 +0206 Message-Id: <20230919230856.661435-10-john.ogness@linutronix.de> In-Reply-To: <20230919230856.661435-1-john.ogness@linutronix.de> References: <20230919230856.661435-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 19 Sep 2023 16:10:11 -0700 (PDT) Invoke the atomic write enforcement functions for oops to ensure that the information gets out to the consoles. Since there is no single general function that calls both oops_enter() and oops_exit(), the nesting feature of atomic write sections is taken advantage of in order to guarantee full coverage between the first oops_enter() and the last oops_exit(). It is important to note that if there are any legacy consoles registered, they will be attempting to directly print from the printk-caller context, which may jeopardize the reliability of the atomic consoles. Optimally there should be no legacy consoles registered. Signed-off-by: John Ogness --- kernel/panic.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index 86ed71ba8c4d..e2879098645d 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -614,6 +614,10 @@ bool oops_may_print(void) return pause_on_oops_flag == 0; } +static atomic_t oops_cpu = ATOMIC_INIT(-1); +static int oops_nesting; +static enum nbcon_prio oops_prev_prio; + /* * Called when the architecture enters its oops handler, before it prints * anything. If this is the first CPU to oops, and it's oopsing the first @@ -630,6 +634,36 @@ bool oops_may_print(void) */ void oops_enter(void) { + enum nbcon_prio prev_prio; + int cpu = -1; + + /* + * If this turns out to be the first CPU in oops, this is the + * beginning of the outermost atomic section. Otherwise it is + * the beginning of an inner atomic section. + */ + prev_prio = nbcon_atomic_enter(NBCON_PRIO_EMERGENCY); + + if (atomic_try_cmpxchg_relaxed(&oops_cpu, &cpu, smp_processor_id())) { + /* + * This is the first CPU in oops. Save the outermost + * @prev_prio in order to restore it on the outermost + * matching oops_exit(), when @oops_nesting == 0. + */ + oops_prev_prio = prev_prio; + + /* + * Enter an inner atomic section that ends at the end of this + * function. In this case, the nbcon_atomic_enter() above + * began the outermost atomic section. + */ + prev_prio = nbcon_atomic_enter(NBCON_PRIO_EMERGENCY); + } + + /* Track nesting when this CPU is the owner. */ + if (cpu == -1 || cpu == smp_processor_id()) + oops_nesting++; + tracing_off(); /* can't trust the integrity of the kernel anymore: */ debug_locks_off(); @@ -637,6 +671,9 @@ void oops_enter(void) if (sysctl_oops_all_cpu_backtrace) trigger_all_cpu_backtrace(); + + /* Exit inner atomic section. */ + nbcon_atomic_exit(NBCON_PRIO_EMERGENCY, prev_prio); } static void print_oops_end_marker(void) @@ -652,6 +689,18 @@ void oops_exit(void) { do_oops_enter_exit(); print_oops_end_marker(); + + if (atomic_read(&oops_cpu) == smp_processor_id()) { + oops_nesting--; + if (oops_nesting == 0) { + atomic_set(&oops_cpu, -1); + + /* Exit outmost atomic section. */ + nbcon_atomic_exit(NBCON_PRIO_EMERGENCY, oops_prev_prio); + } + } + put_cpu(); + kmsg_dump(KMSG_DUMP_OOPS); } -- 2.39.2