Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1180559rdb; Wed, 20 Sep 2023 01:46:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHKGhWyd89vrP6Zr4AjmRipyvDiKmYLAXQfX1Ym4K6hZY2Bz2zb4ibFbGYWsAwF1CJyZe6R X-Received: by 2002:a05:6358:33a3:b0:13c:cdef:25ab with SMTP id i35-20020a05635833a300b0013ccdef25abmr2654850rwd.24.1695199615580; Wed, 20 Sep 2023 01:46:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695199615; cv=none; d=google.com; s=arc-20160816; b=BdB7vCbCCVoae/EkfbrfHY0vjUYhM5ehoVttbifD2BKGy0qbVoSl2w5KatuwsBtwZb ycjvdPG6Mpuw6IM1/4g+hJXdcE1K84KQ7BekvrcYB3xdxxFqqLz9jTNrf8qdKHJFmUW9 x3rEYzhTiUJupsJLuPX9niZ85Po1h9F+N3HAHIgCICEbbGJY1mctMVermTjl4IZ1NRMh Wtch5qL97ooLShXiRVNIjzW0cCNm7WYNgMbovyOowXlt2LARYTj7gGQeFRMJlk1Zk76C RtR8EtkN2tIqwLyeyDKm5EDXtqeaiQGOSzWzbVxt7Zjj/oICXo3gQLA+W46x78SIwylM IjXw== 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=2k78cqDo2n7jFkD6ttaAHGW5bS6OdfJnhB22UhQNM+I=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=d/GKuuQQ76UdhstA3Gl9MrSUvZHblKbQsoFlpdOgHpCz4eMu+20Iw05ytoSs5TGgxZ A9UI9IoxoEIFwEHWcltXksdXcwd3ju2Xgi5Iv83sfTP6+eqM5N5DyM8+8dy7iZ0IcN27 X+v8uccJrN+MJCAd2saKoANR8KeTlpTaoVZrQ50k0C8Pi4qT3hoh5VHvR+41sxPeWLc9 UiuLFomVerjv9hoNIEYIa56kz6gd52SUdWnZvOODT4q05Xned1l2ATS4JiEdDp8P5NCw GX1aU/WrRFVievMBdzFBnTwvFLYwgVuVZoBJddzT0TQ/60w5ZkPTxT57WrElL7QwSOQW tmmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wWn2bTaD; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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. [23.128.96.34]) by mx.google.com with ESMTPS id l71-20020a63884a000000b00578a7f53bacsi3263545pgd.777.2023.09.20.01.46.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 01:46:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=wWn2bTaD; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 C4459827AFAC; Tue, 19 Sep 2023 16:09:29 -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 S233660AbjISXJa (ORCPT + 99 others); Tue, 19 Sep 2023 19:09:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233381AbjISXJU (ORCPT ); Tue, 19 Sep 2023 19:09:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AABD1C4 for ; Tue, 19 Sep 2023 16:09:13 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1695164951; 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=2k78cqDo2n7jFkD6ttaAHGW5bS6OdfJnhB22UhQNM+I=; b=wWn2bTaDLV85/e0NSwumigrW7EOYaHluwC1GA+TskQsBYIcgwTfiQTT1XIGsc4e7Lolo/J 8bfGthQvsDUkyYJHSpwZZRachIyaYxbumFVeA9NmNJHH/CwAWXf4w5AL0YU5147hNqZWMs cwXkq9TzbOcZ4M/fL9C8Alrocb9w10J/rM2a/kTH67o+rfbeo417CYM13QSNtkNqncxTvA 8uvDQ31BqcYhvRsKc6borUdAN5uZfCq3XKVP2c97K3K2gSlMitvIcoiNJHDkF+kaBawyd8 KYgteUKbOiAH+X4OLMG7cjGMHlRSXbXe10CxRsccwqCYVPs7hbvfiniuIJZoiw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1695164951; 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=2k78cqDo2n7jFkD6ttaAHGW5bS6OdfJnhB22UhQNM+I=; b=OXehBxL7RFUdwevTFoCOQzppzyc04cD606hS5sOvQwxbLMn0eblxrkUeq0gSede7p1JRcf lr4ReQILCVn5jiBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 05/11] printk: nbcon: Provide function for atomic flushing Date: Wed, 20 Sep 2023 01:14:50 +0206 Message-Id: <20230919230856.661435-6-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:09:30 -0700 (PDT) From: Thomas Gleixner Provide nbcon_atomic_flush() to perform atomic write flushing of all registered nbcon consoles. Like with legacy consoles, the nbcon consoles are flushed one record per console. This allows all nbcon consoles to generate pseudo-simultaneously, rather than one console waiting for the full ringbuffer to dump to another console before printing anything. Note that if the current CPU is in a nested elevated priority state (EMERGENCY/PANIC), nbcon_atomic_flush() does nothing. This is in case the printing itself generates urgent messages (OOPS/WARN/PANIC), that those messages are fully stored into the ringbuffer before any printing resumes. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/printk.h | 6 +++ kernel/printk/nbcon.c | 101 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 8ef499ab3c1e..58e5f34d6df1 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -192,6 +192,7 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +extern void nbcon_atomic_flush_all(void); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -271,6 +272,11 @@ static inline void dump_stack(void) static inline void printk_trigger_flush(void) { } + +static inline void nbcon_atomic_flush_all(void) +{ +} + #endif #ifdef CONFIG_SMP diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 9359906b575b..2a9ff18fc78c 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -571,7 +571,6 @@ static struct printk_buffers panic_nbcon_pbufs; * in an unsafe state. Otherwise, on success the caller may assume * the console is not in an unsafe state. */ -__maybe_unused static bool nbcon_context_try_acquire(struct nbcon_context *ctxt) { unsigned int cpu = smp_processor_id(); @@ -873,7 +872,6 @@ EXPORT_SYMBOL_GPL(nbcon_exit_unsafe); * When true is returned, @wctxt->ctxt.backlog indicates whether there are * still records pending in the ringbuffer, */ -__maybe_unused static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) { struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); @@ -988,6 +986,105 @@ static __ref struct nbcon_cpu_state *nbcon_get_cpu_state(void) return this_cpu_ptr(&nbcon_pcpu_state); } +/** + * nbcon_atomic_emit_one - Print one record for a console in atomic mode + * @wctxt: An initialized write context struct to use + * for this context + * + * Returns false if the given console could not print a record or there are + * no more records to print, otherwise true. + * + * This is an internal helper to handle the locking of the console before + * calling nbcon_emit_next_record(). + */ +static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(wctxt)) + return false; + + nbcon_context_release(ctxt); + + return prb_read_valid(prb, ctxt->seq, NULL); +} + +/** + * __nbcon_atomic_flush_all - Flush all nbcon consoles in atomic mode + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers + */ +static void __nbcon_atomic_flush_all(bool allow_unsafe_takeover) +{ + struct nbcon_write_context wctxt = { }; + struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt); + struct nbcon_cpu_state *cpu_state; + struct console *con; + bool any_progress; + int cookie; + + cpu_state = nbcon_get_cpu_state(); + + /* + * Let the outermost flush of this priority print. This avoids + * nasty hackery for nested WARN() where the printing itself + * generates one and ensures such nested messages are stored to + * the ringbuffer before any printing resumes. + * + * cpu_state->prio <= NBCON_PRIO_NORMAL is not subject to nesting + * and can proceed in order to allow any atomic printing for + * regular kernel messages. + */ + if (cpu_state->prio > NBCON_PRIO_NORMAL && + cpu_state->nesting[cpu_state->prio] != 1) + return; + + do { + any_progress = false; + + cookie = console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags = console_srcu_read_flags(con); + bool progress; + + if (!(flags & CON_NBCON)) + continue; + + if (!console_is_usable(con, flags)) + continue; + + memset(ctxt, 0, sizeof(*ctxt)); + ctxt->console = con; + ctxt->spinwait_max_us = 2000; + ctxt->prio = cpu_state->prio; + ctxt->allow_unsafe_takeover = allow_unsafe_takeover; + + progress = nbcon_atomic_emit_one(&wctxt); + if (!progress) + continue; + any_progress = true; + } + console_srcu_read_unlock(cookie); + } while (any_progress); +} + +/** + * nbcon_atomic_flush_all - Flush all nbcon consoles in atomic mode + * + * Context: Any context where migration is disabled. + */ +void nbcon_atomic_flush_all(void) +{ + __nbcon_atomic_flush_all(false); +} + /** * nbcon_atomic_enter - Enter a context that enforces atomic printing * @prio: Priority of the context -- 2.39.2