From patchwork Tue Sep 19 23:08:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 142189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3836558vqi; Tue, 19 Sep 2023 20:00:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDGFZEALQr0Frna/6QhaJPldVWZgqOpQset6BGQfiRfRigrpK1c8iNmCywqRVlP8HaIUWs X-Received: by 2002:a17:90a:7c01:b0:273:f584:40ca with SMTP id v1-20020a17090a7c0100b00273f58440camr1463265pjf.16.1695178854971; Tue, 19 Sep 2023 20:00:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695178854; cv=none; d=google.com; s=arc-20160816; b=c6WdtmSmuCRiF3bjASgWUreVuggriQqX9/wbRMobew7R9M0bwVCwxqrnA+TPg+u9Ks 5fkhu+Js1F6qnz3dgGV+FqSxqq09ozhonP6fVafr0St2PXtdgAgc4iumJbRXPXyJjBrw 9I4fwxaRgMO4C9mpFg6szG6QBc/KCoNvZdY2uAaPJtWYYrtIRwofCqgbVOfrlG7R4aJM s6aYjMWF2DnHydq1bBriaEJY5zIvI+Teqjzgf2fe18EQw8z1nyTwpYWjtDVCZt3y4daa yQgw0sJB4W0lhL4zDvEUwcBl5PU7wHKw7JpRGoIdL0UExI+Onm1uDYxPLBLOhdxTE6YD zE0A== 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.b=OXehBxL7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id n3-20020a17090ac68300b00274dc876792si568392pjt.98.2023.09.19.20.00.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 20:00:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; 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.b=OXehBxL7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id DA3C881CE846; Tue, 19 Sep 2023 16:09:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233472AbjISXJa (ORCPT + 26 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 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email 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 (pete.vger.email [0.0.0.0]); Tue, 19 Sep 2023 16:09:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777523863347067642 X-GMAIL-MSGID: 1777523863347067642 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