From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15D914EB5E for ; Tue, 27 Aug 2024 04:43:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; cv=none; b=M5e4I2rgN3AnUU3krm226jXmVgOcJV7lU2XXbG++ps4yjIbg5bLwjzfCko+RnVkfRBOrHkO2aIK4NRul/GcAgw6bw0A0StuqC1vMP1n2oyde3Xiq47HdO0x2thchfffwW+jwdJO0s8KTJnFOEfbLFv8o1hyjOmjVS+tqMFTwk7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; c=relaxed/simple; bh=uYu4mQMFuI8yeG9XvXlfOKjSdd3b0lBbmmwodCwqC2k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JrFN/2snkqOSwuXaZs+QAu6hDwQm+K6/dPkn+nlsAtwjPF+e2WUjR2F3yfGLIKmWbIxdBm7Qm3Fe550KbQmh8IqnCmtTBawNRh+VFrHi69escKjM69zBTpPo3hAJAZ+c0ae/QTFVGDmCgBEOzNc8bxjogUj+CRXS4pw23tOBYnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jW2TK+0F; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=D6xIT9O1; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jW2TK+0F"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="D6xIT9O1" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733814; 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=Yf4jbljXWY8PAPTbbDHBJy4C3txbuHDOpF+lqNl4ktA=; b=jW2TK+0FRq5XXsLNJbp1didRdjDUrRmDEp3DoeD0ykDmMg2JkfFa3PJK13wOguxiM3I3vQ nueQ+naKQwOq8xke4iVbN7/FGbVXemcwjluq6euMp11v3qAZu2y01mQPdXmGE0lWHd57aw A/+tlkbSCGIO5Ve+HszWdegT0FfCkrg+hHlnXWRn/5+vts5p1qcoWwOwaLzwEPoBZ5az87 /YmVIaIo86jfFZYXZl8odlGJ7BqfQQ6TaflnMxxlL1av8A1NVdfsqvqt8kRolh+jpLiApP th8YiP13MRrY72/TbaKiP4a5BSqQPo/fuSHSwJlXX9DycGF0/MioJUBM13OYuQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733814; 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=Yf4jbljXWY8PAPTbbDHBJy4C3txbuHDOpF+lqNl4ktA=; b=D6xIT9O1Zta8ss+J8ltHJzNlZnX1xVF9U3Q6XwOTjmWZ06RhoKNQIxY5+6zcAsTsXfYQEj 47d5HnbHLOBSQoDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 01/17] printk: nbcon: Add function for printers to reacquire ownership Date: Tue, 27 Aug 2024 06:49:17 +0206 Message-Id: <20240827044333.88596-2-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Since ownership can be lost at any time due to handover or takeover, a printing context _must_ be prepared to back out immediately and carefully. However, there are scenarios where the printing context must reacquire ownership in order to finalize or revert hardware changes. One such example is when interrupts are disabled during printing. No other context will automagically re-enable the interrupts. For this case, the disabling context _must_ reacquire nbcon ownership so that it can re-enable the interrupts. Provide nbcon_reacquire_nobuf() for exactly this purpose. It allows a printing context to reacquire ownership using the same priority as its previous ownership. Note that after a successful reacquire the printing context will have no output buffer because that has been lost. This function cannot be used to resume printing. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 6 ++++ kernel/printk/nbcon.c | 74 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 9a13f91b0c43..88050d30a9cc 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -366,6 +366,10 @@ struct console { * * The callback should allow the takeover whenever it is safe. It * increases the chance to see messages when the system is in trouble. + * If the driver must reacquire ownership in order to finalize or + * revert hardware changes, nbcon_reacquire_nobuf() can be used. + * However, on reacquire the buffer content is no longer available. A + * reacquire cannot be used to resume printing. * * The callback can be called from any context (including NMI). * Therefore it must avoid usage of any locking and instead rely @@ -558,12 +562,14 @@ extern void nbcon_cpu_emergency_exit(void); extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); +extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt); #else static inline void nbcon_cpu_emergency_enter(void) { } static inline void nbcon_cpu_emergency_exit(void) { } static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { = return false; } static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) {= return false; } static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { = return false; } +static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt= ) { } #endif =20 extern int console_set_on_cmdline; diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 92ac5c590927..d1c640e7e645 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -830,6 +830,19 @@ static bool __nbcon_context_update_unsafe(struct nbcon= _context *ctxt, bool unsaf return nbcon_context_can_proceed(ctxt, &cur); } =20 +static void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt, + char *buf, unsigned int len) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con =3D ctxt->console; + struct nbcon_state cur; + + wctxt->outbuf =3D buf; + wctxt->len =3D len; + nbcon_state_read(con, &cur); + wctxt->unsafe_takeover =3D cur.unsafe_takeover; +} + /** * nbcon_enter_unsafe - Enter an unsafe region in the driver * @wctxt: The write context that was handed to the write function @@ -845,8 +858,12 @@ static bool __nbcon_context_update_unsafe(struct nbcon= _context *ctxt, bool unsaf bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + bool is_owner; =20 - return nbcon_context_enter_unsafe(ctxt); + is_owner =3D nbcon_context_enter_unsafe(ctxt); + if (!is_owner) + nbcon_write_context_set_buf(wctxt, NULL, 0); + return is_owner; } EXPORT_SYMBOL_GPL(nbcon_enter_unsafe); =20 @@ -865,11 +882,43 @@ EXPORT_SYMBOL_GPL(nbcon_enter_unsafe); bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + bool ret; =20 - return nbcon_context_exit_unsafe(ctxt); + ret =3D nbcon_context_exit_unsafe(ctxt); + if (!ret) + nbcon_write_context_set_buf(wctxt, NULL, 0); + return ret; } EXPORT_SYMBOL_GPL(nbcon_exit_unsafe); =20 +/** + * nbcon_reacquire_nobuf - Reacquire a console after losing ownership + * while printing + * @wctxt: The write context that was handed to the write callback + * + * Since ownership can be lost at any time due to handover or takeover, a + * printing context _must_ be prepared to back out immediately and + * carefully. However, there are scenarios where the printing context must + * reacquire ownership in order to finalize or revert hardware changes. + * + * This function allows a printing context to reacquire ownership using the + * same priority as its previous ownership. + * + * Note that after a successful reacquire the printing context will have no + * output buffer because that has been lost. This function cannot be used = to + * resume printing. + */ +void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + + while (!nbcon_context_try_acquire(ctxt)) + cpu_relax(); + + nbcon_write_context_set_buf(wctxt, NULL, 0); +} +EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); + /** * nbcon_emit_next_record - Emit a record in the acquired context * @wctxt: The write context that will be handed to the write function @@ -895,7 +944,6 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt) .pbufs =3D ctxt->pbufs, }; unsigned long con_dropped; - struct nbcon_state cur; unsigned long dropped; =20 /* @@ -930,10 +978,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) goto update_con; =20 /* Initialize the write context for driver callbacks. */ - wctxt->outbuf =3D &pmsg.pbufs->outbuf[0]; - wctxt->len =3D pmsg.outbuf_len; - nbcon_state_read(con, &cur); - wctxt->unsafe_takeover =3D cur.unsafe_takeover; + nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 if (con->write_atomic) { con->write_atomic(con, wctxt); @@ -947,6 +992,21 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return false; } =20 + if (!wctxt->outbuf) { + /* + * Ownership was lost and reacquired by the driver. Handle it + * as if ownership was lost. + */ + nbcon_context_release(ctxt); + return false; + } + + /* + * Ownership may have been lost but _not_ reacquired by the driver. + * This case is detected and handled when entering unsafe to update + * dropped/seq values. + */ + /* * Since any dropped message was successfully output, reset the * dropped count for the console. --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D58554648 for ; Tue, 27 Aug 2024 04:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; cv=none; b=sUSyrF2hmX2eHJ2zRAqM8YEsQahc9+lAr/HRPG1TYaJTnLqXxXIQHssgidHfKiu6gavozZFBXGhhYHFey7T7KbZrWO0/x2zrzRI/Ih70Snqkww+5X5gQVB9JrvZlQxWRNYg2z3HKwCcKDsKv0Bo2TZVeOSPbuu9c+Aic7tyC90I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; c=relaxed/simple; bh=WxJn38aQwzgVssvSr9SkazK1C6E06LBK31hfQqtQBiE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uenkWgXngf5GxYlGEMZo0FErUIt5TKGeArUt2Pt7ALxr9iXvKzLjNA89ucsLaP9A+C5bsWKJb53JIXl2X06q8YoWLh2lm2HQ4YcaEUx9TE7dX6se38hN/6KEBbqx0LEpq01RWq2hz7lYsV1UMPm7H5STNtrZDM4OFeFy9r+twR4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1PSLr6ji; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BRULf9li; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1PSLr6ji"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BRULf9li" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733814; 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=J912AwN71BiOtVlEylsZ3qn7Oo+IxLpwqnkVptIsJ08=; b=1PSLr6jiNFs3S/tun844IZrN3cBlnbijOvQpF2KFGnD7IJQWq2CrH1hqs38/UYUmMKZy9Q NKMEVF+iU5ge78jp8lO7xchsde621nG8NWerGA2gC6wgJ0YElroEyzKE5PZ7LB7XKXHZO5 d8Qvf2wwuSyvND2lFaiLrLQHPDAoSxzbjzyCFbHZMr5A+rVnVT4+To3r4XpIxq3udNN8H5 KCUAWDB0Bg1OXmuczmppJJhyRUn0eoR0XHRFSX+pkl1QrD+LYJSS+T3bYJDDeuDY57bFQz 1cGlySssPv/BkGIIjzjst71xZXlXb9jsGYL6z6Bev3kHj/PYgKtmfpjQE/TUCw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733814; 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=J912AwN71BiOtVlEylsZ3qn7Oo+IxLpwqnkVptIsJ08=; b=BRULf9lix9z0aTjTh2pxjzZFyKfjCXmu6DhpHXz6e406EoUZrvSqGgb0ii7T6TQ7B9rHzu 8P7bjg/aBAn6KSDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 02/17] printk: Fail pr_flush() if before SYSTEM_SCHEDULING Date: Tue, 27 Aug 2024 06:49:18 +0206 Message-Id: <20240827044333.88596-3-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A follow-up change adds pr_flush() to console unregistration. However, with boot consoles unregistration can happen very early if there are also regular consoles registering as well. In this case the pr_flush() is not important because all consoles are flushed when checking the initial console sequence number. Allow pr_flush() to fail if @system_state has not yet reached SYSTEM_SCHEDULING. This avoids might_sleep() and msleep() explosions that would otherwise occur: [ 0.436739][ T0] printk: legacy console [ttyS0] enabled [ 0.439820][ T0] printk: legacy bootconsole [earlyser0] disabled [ 0.446822][ T0] BUG: scheduling while atomic: swapper/0/0/0x00000002 [ 0.450491][ T0] 1 lock held by swapper/0/0: [ 0.457897][ T0] #0: ffffffff82ae5f88 (console_mutex){+.+.}-{4:4}, a= t: console_list_lock+0x20/0x70 [ 0.463141][ T0] Modules linked in: [ 0.465307][ T0] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.10.0-rc1= + #372 [ 0.469394][ T0] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996= ), BIOS 1.14.0-2 04/01/2014 [ 0.474402][ T0] Call Trace: [ 0.476246][ T0] [ 0.481473][ T0] dump_stack_lvl+0x93/0xb0 [ 0.483949][ T0] dump_stack+0x10/0x20 [ 0.486256][ T0] __schedule_bug+0x68/0x90 [ 0.488753][ T0] __schedule+0xb9b/0xd80 [ 0.491179][ T0] ? lock_release+0xb5/0x270 [ 0.493732][ T0] schedule+0x43/0x170 [ 0.495998][ T0] schedule_timeout+0xc5/0x1e0 [ 0.498634][ T0] ? __pfx_process_timeout+0x10/0x10 [ 0.501522][ T0] ? msleep+0x13/0x50 [ 0.503728][ T0] msleep+0x3c/0x50 [ 0.505847][ T0] __pr_flush.constprop.0.isra.0+0x56/0x500 [ 0.509050][ T0] ? _printk+0x58/0x80 [ 0.511332][ T0] ? lock_is_held_type+0x9c/0x110 [ 0.514106][ T0] unregister_console_locked+0xe1/0x450 [ 0.517144][ T0] register_console+0x509/0x620 [ 0.519827][ T0] ? __pfx_univ8250_console_init+0x10/0x10 [ 0.523042][ T0] univ8250_console_init+0x24/0x40 [ 0.525845][ T0] console_init+0x43/0x210 [ 0.528280][ T0] start_kernel+0x493/0x980 [ 0.530773][ T0] x86_64_start_reservations+0x18/0x30 [ 0.533755][ T0] x86_64_start_kernel+0xae/0xc0 [ 0.536473][ T0] common_startup_64+0x12c/0x138 [ 0.539210][ T0] And then the kernel goes into an infinite loop complaining about: 1. releasing a pinned lock 2. unpinning an unpinned lock 3. bad: scheduling from the idle thread! 4. goto 1 Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6accd1704e73..acf668001096 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3991,6 +3991,10 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre u64 diff; u64 seq; =20 + /* Sorry, pr_flush() will not work this early. */ + if (system_state < SYSTEM_SCHEDULING) + return false; + might_sleep(); =20 seq =3D prb_next_reserve_seq(prb); --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D5C456458 for ; Tue, 27 Aug 2024 04:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; cv=none; b=Dr8fcT7QTUNmNtUYHprN2FDBZ3ZcF6SVs8wro+DlWGJeAGvIkSZgBDAqtUBImr31BWY6RRGcpbyfuLPLIFttX5jNMnROHFO0mN2LNzH8jwH6H1gxdwFyNGimB8Ak5p9QhP9r1yMf86jR/TDbFae3dPAYU4lCQRT68WOogzLexzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; c=relaxed/simple; bh=hO2oomLoxf/bUX1WgRwfUv7yEovKt+lixG0W+vCa7/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AYMvsM5iuxOSz9gBjRyLPdKzbKHGze/LjI+KA+AaNF3uG31Ugl8uraBkY6o24L533U4IJQqFOZFtrTHDxMcbjLi7Fy4vpIjR0LOuYtTnnO/TROb+VHim+Q+W7t2h26hZbOw6Lgx7VpX5S8zU+eW4/ddB4iQ+mK8ySrtBBCXdbDw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MQ1zuCj6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kDS3R9wb; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MQ1zuCj6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kDS3R9wb" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733814; 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=fRZKVWxZPmNsRkG/dsm1SB0+wvFuf66EXj0bGb54vtI=; b=MQ1zuCj6BHvtiS0v32ZNi74j/4Hhbxg/NUGtYqRLBx43SPMvEOJswDGHMrTNlMBS1lJ0/D o9x83rQq0t/9HUKkSmAYXq/pShiM3Zuw60UyVWsUVtoj3h7J5aGkME6dw9md47hxt51aiO VzSrkFDcb/+pL1DfudoR0em6BRq5VDOkavaAobGIfpTTQWZ4HB9vP16limXHmKKj78KRPG qes588SjaVzWHNiCbSC0SfFwYdtGIOX8rvHY2IF0oTpb+3xOTf8sF7KRNCtjQ+oiW10e/c oUNHnc1SfrVtrMbc7smJ0wXyQWcgJfOIsbpxYh3ys/szY/t5AKc4BfPvPJxP4Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733815; 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=fRZKVWxZPmNsRkG/dsm1SB0+wvFuf66EXj0bGb54vtI=; b=kDS3R9wb4qOx0/BXyHuvie1s2i76XVWrvYleWfFfJe/zyFg1EVo36MkKa3wSyBppHi64rw Llfht/gwPLE4oUAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 03/17] printk: Flush console on unregister_console() Date: Tue, 27 Aug 2024 06:49:19 +0206 Message-Id: <20240827044333.88596-4-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Ensure consoles have flushed pending records before unregistering. The console should print up to at least its related "console disabled" record. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index acf668001096..c79e962b822a 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3771,11 +3771,16 @@ static int unregister_console_locked(struct console= *console) if (res > 0) return 0; =20 + if (!console_is_registered_locked(console)) + res =3D -ENODEV; + else if (console_is_usable(console, console->flags)) + __pr_flush(console, 1000, true); + /* Disable it unconditionally */ console_srcu_write_flags(console, console->flags & ~CON_ENABLED); =20 - if (!console_is_registered_locked(console)) - return -ENODEV; + if (res < 0) + return res; =20 /* * Use the driver synchronization to ensure that the hardware is not --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E02D05A4D5 for ; Tue, 27 Aug 2024 04:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; cv=none; b=HEdge2E/0S54niHAAKjVZ8IYKLm6oY9OiW974JpEp/AwI19UYBZrEaO3MTmU+INt6v9rruP1RSR/GO6BunRh47VteSSNQhnVl4iOrx49szmS76CicVem7z5sdgSgDX5l58poiQKOI802Ks0kbmmD7e30tkdDDbH5y9KV3OAzh2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733818; c=relaxed/simple; bh=CbFsGyHVClMvGt6qKSP3h17q8ilW3/7SuhYwURG4p2Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=m6DxUfBkrqlARRA57mdQhZ/44Dx44MnrSw3E8d9WWD2/arSyL+wlhDkd70ymdwFNq5oGdLJTKT/5QXsuGmAM5ryGRB42PGuPRVqtfwl24jc9sj5TXpDP772Phb76idwHXjHSq/9PXKKXEi5fsLgsZcZQ7eSCSyBdam65R7zCSbQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tJXeOIBR; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jwQAz4Xw; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tJXeOIBR"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jwQAz4Xw" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733815; 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=Uc1DpkMZMf3xeK5PE6XCQrymc1EeZpAAWNqJhRXdseA=; b=tJXeOIBRWvG18QaZdEWFIpEagCTVCkgW+SvIaw9pI8TH55ipVZH1qmOzD/5RwTViBUjkSX CuxikW9Ra8AVkQQ1+5zXbB5fCpblUsprxxy2YcO3asPKYXRtZjRHrcCVAFtOjYjA44x7fJ HWqyOcRV5ua1ptX8kTW6Qu5xjndw0uI1nIcdgJW8rJryBaRxW+ptbLvNlgZJZJoh5Qzb3k FcgBKq29OkAr91GsKI12e8k3bziKzNH66bwfkBVt/gRJKj0YYmGo6T+dSdiR5IU9IO3oEH VrPgBtmfTzrpQQYZK2BWDpW6xGtuZPiXNQ+Dn3OzNvtXw72B+sp3hjXJnbu/dA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733815; 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=Uc1DpkMZMf3xeK5PE6XCQrymc1EeZpAAWNqJhRXdseA=; b=jwQAz4Xwb7IM+uoHXk2PS/4hddiAjj/9ue/o2rBtmCHdBgNk4KDd0oR5P5xV6uhOHdy26q Qt3zjYZGzvOE3uCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 04/17] printk: nbcon: Add context to usable() and emit() Date: Tue, 27 Aug 2024 06:49:20 +0206 Message-Id: <20240827044333.88596-5-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The nbcon consoles will have two callbacks to be used for different contexts. In order to determine if an nbcon console is usable, console_is_usable() must know if it is a context that will need to use the optional write_atomic() callback. Also, nbcon_emit_next_record() must know which callback it needs to call. Add an extra parameter @use_atomic to console_is_usable() and nbcon_emit_next_record() to specify this. Since so far only the write_atomic() callback exists, @use_atomic is set to true for all call sites. For legacy consoles, @use_atomic is not used. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 8 +++++--- kernel/printk/nbcon.c | 34 +++++++++++++++++++--------------- kernel/printk/printk.c | 6 +++--- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 8e36d8695f81..ad631824e7e5 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -97,7 +97,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, b= ool *handover, * which can also play a role in deciding if @con can be used to print * records. */ -static inline bool console_is_usable(struct console *con, short flags) +static inline bool console_is_usable(struct console *con, short flags, boo= l use_atomic) { if (!(flags & CON_ENABLED)) return false; @@ -106,7 +106,8 @@ static inline bool console_is_usable(struct console *co= n, short flags) return false; =20 if (flags & CON_NBCON) { - if (!con->write_atomic) + /* The write_atomic() callback is optional. */ + if (use_atomic && !con->write_atomic) return false; } else { if (!con->write) @@ -149,7 +150,8 @@ static inline void nbcon_atomic_flush_pending(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, int cookie) { return false; } =20 -static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } +static inline bool console_is_usable(struct console *con, short flags, + bool use_atomic) { return false; } =20 #endif /* CONFIG_PRINTK */ =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index d1c640e7e645..de6faf34e114 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -922,6 +922,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); /** * nbcon_emit_next_record - Emit a record in the acquired context * @wctxt: The write context that will be handed to the write function + * @use_atomic: True if the write_atomic() callback is to be used * * Return: True if this context still owns the console. False if * ownership was handed over or taken. @@ -935,7 +936,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); * When true is returned, @wctxt->ctxt.backlog indicates whether there are * still records pending in the ringbuffer, */ -static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) +static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool= use_atomic) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); struct console *con =3D ctxt->console; @@ -946,6 +947,18 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) unsigned long con_dropped; unsigned long dropped; =20 + /* + * This function should never be called for consoles that have not + * implemented the necessary callback for writing: i.e. legacy + * consoles and, when atomic, nbcon consoles with no write_atomic(). + * Handle it as if ownership was lost and try to continue. + */ + if (WARN_ON_ONCE((use_atomic && !con->write_atomic) || + !(console_srcu_read_flags(con) & CON_NBCON))) { + nbcon_context_release(ctxt); + return false; + } + /* * The printk buffers are filled within an unsafe section. This * prevents NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY from @@ -980,17 +993,8 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) /* Initialize the write context for driver callbacks. */ nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 - if (con->write_atomic) { + if (use_atomic) con->write_atomic(con, wctxt); - } else { - /* - * This function should never be called for legacy consoles. - * Handle it as if ownership was lost and try to continue. - */ - WARN_ON_ONCE(1); - nbcon_context_release(ctxt); - return false; - } =20 if (!wctxt->outbuf) { /* @@ -1119,7 +1123,7 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_= context *wctxt) * The higher priority printing context takes over responsibility * to print the pending records. */ - if (!nbcon_emit_next_record(wctxt)) + if (!nbcon_emit_next_record(wctxt, true)) return false; =20 nbcon_context_release(ctxt); @@ -1220,7 +1224,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq, * handed over or taken over. In both cases the context is no * longer valid. */ - if (!nbcon_emit_next_record(&wctxt)) + if (!nbcon_emit_next_record(&wctxt, true)) return -EAGAIN; =20 if (!ctxt->backlog) { @@ -1306,7 +1310,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq= , bool allow_unsafe_takeove if (!(flags & CON_NBCON)) continue; =20 - if (!console_is_usable(con, flags)) + if (!console_is_usable(con, flags, true)) continue; =20 if (nbcon_seq_read(con) >=3D stop_seq) @@ -1491,7 +1495,7 @@ void nbcon_device_release(struct console *con) * the console is usable throughout flushing. */ cookie =3D console_srcu_read_lock(); - if (console_is_usable(con, console_srcu_read_flags(con)) && + if (console_is_usable(con, console_srcu_read_flags(con), true) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { /* * If nbcon_atomic flushing is not available, fallback to diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index c79e962b822a..846306ac2663 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3071,7 +3071,7 @@ static bool console_flush_all(bool do_cond_resched, u= 64 *next_seq, bool *handove u64 printk_seq; bool progress; =20 - if (!console_is_usable(con, flags)) + if (!console_is_usable(con, flags, true)) continue; any_usable =3D true; =20 @@ -3773,7 +3773,7 @@ static int unregister_console_locked(struct console *= console) =20 if (!console_is_registered_locked(console)) res =3D -ENODEV; - else if (console_is_usable(console, console->flags)) + else if (console_is_usable(console, console->flags, true)) __pr_flush(console, 1000, true); =20 /* Disable it unconditionally */ @@ -4043,7 +4043,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre * that they make forward progress, so only increment * @diff for usable consoles. */ - if (!console_is_usable(c, flags)) + if (!console_is_usable(c, flags, true)) continue; =20 if (flags & CON_NBCON) { --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 655561386C6 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; cv=none; b=r5SbQV+rrQfAagsd5RvqIzNDEFBdGQbadsRpJXw+kL9rz2Nppd+KdAa9ozWIBlB8YLCc0NGsuy0GuZ0OQeJhQ1rgPDCBuJZij3VYZyCyJ3OEZnU4fUftq44ERkB9z80JvAsiLp+hMISEuiwC0R3eKuvORzLIfnjQpIKZdBeg7og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; c=relaxed/simple; bh=IEMoa9FR09sPw0lhjIBqmf3Q6HccXUpasdLnSiDglEM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b4wY6LZwHtpWKg8eH11NDhU1oV7eMmwwLdkktURbA4C2yksru7Hj1xiyPMkp7WlbJvtbLmrS3WnJFzYOTzzp9D8rdqa8ON+/s7Ta16N3ugwFDYOHQYJQ0GvF4d1ILR161Y1Kw83njecMSbNr6JnshbnWrHz27xNJwDGJ1mqqaPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UEZHoLBa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Rhgk81Qj; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UEZHoLBa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Rhgk81Qj" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733815; 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=yGwR2dGA2mrkS6D1DBAcrQOGxVjlY0VD3AieVmQrH8c=; b=UEZHoLBaqFjcBJvRRTO1GBUFxtgaPMlUY0cHoVH2dbhUmSHF2or9ffaKX/CgWwXx3BPiQM r612sEGPBS2UJ+g56O94bROiSXgZDZiSQ9wa72fG3zKpIi4K3VTIg/YM5oxOESODT6X3HD tU/DYq5AbKDex2sPdIDTGCgiVXlObZ1K00JS1sjJYdTQ1D1Cn/iAHXZdjzGYe4ggPfQwBN zeN+yaZyIyOATYAjy62+gDk8JcK0jWU9WrIyvjXcMsg6739gHJTecz4Iz2boEI3Cbm9eGs 7riiySNrWvaBfiUmyNBdnRZ6bdIaiZeQxIQNXvqKH5us2C+pJggP/Qsg6gkbAA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733815; 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=yGwR2dGA2mrkS6D1DBAcrQOGxVjlY0VD3AieVmQrH8c=; b=Rhgk81QjX6OXHyrXOFxnRImn864CmhI1vSwE2OwDFPD2e/QK3x+xhMN0TQivVOXVHD9uqp yQyUWBZjWaCnDVBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 05/17] printk: nbcon: Init @nbcon_seq to highest possible Date: Tue, 27 Aug 2024 06:49:21 +0206 Message-Id: <20240827044333.88596-6-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When initializing an nbcon console, have nbcon_alloc() set @nbcon_seq to the highest possible sequence number. For all practical purposes, this will guarantee that the console will have nothing to print until later when @nbcon_seq is set to the proper initial printing value. This will be particularly important once kthread printing is introduced because nbcon_alloc() can create/start the kthread before the desired initial sequence number is known. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 8 +++++++- kernel/printk/printk_ringbuffer.h | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index de6faf34e114..fbf29b3a0811 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1398,7 +1398,13 @@ bool nbcon_alloc(struct console *con) struct nbcon_state state =3D { }; =20 nbcon_state_set(con, &state); - atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), 0); + + /* + * Initialize @nbcon_seq to the highest possible sequence number so + * that practically speaking it will have nothing to print until a + * desired initial sequence number has been set via nbcon_seq_force(). + */ + atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), ULSEQ_MAX(prb)); =20 if (con->flags & CON_BOOT) { /* diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringb= uffer.h index bd2a892deac1..ba778e2ef28b 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -403,10 +403,12 @@ u64 prb_next_reserve_seq(struct printk_ringbuffer *rb= ); =20 #define __u64seq_to_ulseq(u64seq) (u64seq) #define __ulseq_to_u64seq(rb, ulseq) (ulseq) +#define ULSEQ_MAX(rb) (-1) =20 #else /* CONFIG_64BIT */ =20 #define __u64seq_to_ulseq(u64seq) ((u32)u64seq) +#define ULSEQ_MAX(rb) __u64seq_to_ulseq(prb_first_seq(rb) + 0x80000000UL) =20 static inline u64 __ulseq_to_u64seq(struct printk_ringbuffer *rb, u32 ulse= q) { --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66B22139580 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; cv=none; b=mtkkY8rKHZZUDwl3/mYbbyKw3z9Pu8MZtCKJOmYA0WztdjFF7//MmjuCMmWThr66RnJPDQ/j3RBpAIGb8Fctkz/6u6TzKz+B6A1ZEWmmu29UsDi1A+sevWD9FePz8Kk0sAR0+Lc9jqBABBoxhp5OA7sEcqguCuQf8od0qOfZ0i4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; c=relaxed/simple; bh=lSmf7HlBwqhNDKJF962E8MqrPwREsQLpVudaDrsb1ws=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e040p8LuOEc3tlZS3JlbUSHEtCGmtD/v7AH1AqA7Z/uwhfauvz+GK14K192i2gX4hxtBA/GoHGl4WChUws2S6n57Jrluhh3j93KtOGIeZa1YSlkyGTHUObRqoQvzyZJAKe2Sn0h2TxVWR1WzhLlYdHj+V/JRVw1BJzwmR9fQ1B0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zLXyTjQZ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RSHI+PIN; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zLXyTjQZ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RSHI+PIN" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733816; 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=p8elstSOXE4eJN9uZcXVexW0WVe9AVPI9Y3yXNIuZBQ=; b=zLXyTjQZP/SoM4s2cBidEqfFueq2qR1gkent1Jp2EFKzKgthy/ljFbo6lUFVhi3EvAefBJ iWmGx3yy46azjB6X8EfAF54L4iyl7YhiOi5h1bPn1SwgQgK/mTd28IL7HSzedgu4FGAYpr JG87PK/KmrR3U7/WUi1dMqihUGvw4C4KjZGzD+kyuP6KjcF9fWzYZVkovR6D1F3f9ZUAkM HW6hvbnQyijIHvPN8/RwMOuDaLLGyVINwYaxuDvdZkU92txVurGG0cltUxYPO7WlYRCDGG gVAqMFU8p7cJSaA0ZvaVUrs0+mIytxIzELxj2hNWHO4SECr7x0ldbyB/YSQagg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733816; 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=p8elstSOXE4eJN9uZcXVexW0WVe9AVPI9Y3yXNIuZBQ=; b=RSHI+PIN7qDgpt6uvW6zb5DLusU7yNnL9jtkGvpf8Khgwef9SspkTUxJQBwJvvKiBdgZcA UXYpDuYp8dL4T9DQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 06/17] printk: nbcon: Introduce printer kthreads Date: Tue, 27 Aug 2024 06:49:22 +0206 Message-Id: <20240827044333.88596-7-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Provide the main implementation for running a printer kthread per nbcon console that is takeover/handover aware. This includes: - new mandatory write_thread() callback - kthread creation - kthread main printing loop - kthread wakeup mechanism - kthread shutdown kthread creation is a bit tricky because consoles may register before kthreads can be created. In such cases, registration will succeed, even though no kthread exists. Once kthreads can be created, an early_initcall will set @printk_kthreads_ready. If there are no registered boot consoles, the early_initcall creates the kthreads for all registered nbcon consoles. If kthread creation fails, the related console is unregistered. If there are registered boot consoles when @printk_kthreads_ready is set, no kthreads are created until the final boot console unregisters. Once kthread creation finally occurs, @printk_kthreads_running is set so that the system knows kthreads are available for all registered nbcon consoles. If @printk_kthreads_running is already set when the console is registering, the kthread is created during registration. If kthread creation fails, the registration will fail. Until @printk_kthreads_running is set, console printing occurs directly via the console_lock. kthread shutdown on system shutdown/reboot is necessary to ensure the printer kthreads finish their printing so that the system can cleanly transition back to direct printing via the console_lock in order to reliably push out the final shutdown/reboot messages. @printk_kthreads_running is cleared before shutting down the individual kthreads. The kthread uses a new mandatory write_thread() callback that is called with both device_lock() and the console context acquired. The console ownership handling is necessary for synchronization against write_atomic() which is synchronized only via the console context ownership. The device_lock() serializes acquiring the console context with NBCON_PRIO_NORMAL. It is needed in case the device_lock() does not disable preemption. It prevents the following race: CPU0 CPU1 [ task A ] nbcon_context_try_acquire() # success with NORMAL prio # .unsafe =3D=3D false; // safe for takeover [ schedule: task A -> B ] WARN_ON() nbcon_atomic_flush_pending() nbcon_context_try_acquire() # success with EMERGENCY prio # flushing nbcon_context_release() # HERE: con->nbcon_state is free # to take by anyone !!! nbcon_context_try_acquire() # success with NORMAL prio [ task B ] [ schedule: task B -> A ] nbcon_enter_unsafe() nbcon_context_can_proceed() BUG: nbcon_context_can_proceed() returns "true" because the console is owned by a context on CPU0 with NBCON_PRIO_NORMAL. But it should return "false". The console is owned by a context from task B and we do the check in a context from task A. Note that with these changes, the printer kthreads do not yet take over full responsibility for nbcon printing during normal operation. These changes only focus on the lifecycle of the kthreads. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- include/linux/console.h | 40 +++++++ kernel/printk/internal.h | 27 +++++ kernel/printk/nbcon.c | 235 +++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 108 ++++++++++++++++++ 4 files changed, 410 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index 88050d30a9cc..788ce9c829f6 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -16,7 +16,9 @@ =20 #include #include +#include #include +#include #include #include =20 @@ -324,6 +326,9 @@ struct nbcon_write_context { * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations * @pbufs: Pointer to nbcon private buffer + * @kthread: Printer kthread for this console + * @rcuwait: RCU-safe wait object for @kthread waking + * @irq_work: Defer @kthread waking to IRQ work context */ struct console { char name[16]; @@ -377,6 +382,37 @@ struct console { */ void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); =20 + /** + * @write_thread: + * + * NBCON callback to write out text in task context. + * + * This callback must be called only in task context with both + * device_lock() and the nbcon console acquired with + * NBCON_PRIO_NORMAL. + * + * The same rules for console ownership verification and unsafe + * sections handling applies as with write_atomic(). + * + * The console ownership handling is necessary for synchronization + * against write_atomic() which is synchronized only via the context. + * + * The device_lock() provides the primary serialization for operations + * on the device. It might be as relaxed (mutex)[*] or as tight + * (disabled preemption and interrupts) as needed. It allows + * the kthread to operate in the least restrictive mode[**]. + * + * [*] Standalone nbcon_context_try_acquire() is not safe with + * the preemption enabled, see nbcon_owner_matches(). But it + * can be safe when always called in the preemptive context + * under the device_lock(). + * + * [**] The device_lock() makes sure that nbcon_context_try_acquire() + * would never need to spin which is important especially with + * PREEMPT_RT. + */ + void (*write_thread)(struct console *con, struct nbcon_write_context *wct= xt); + /** * @device_lock: * @@ -423,7 +459,11 @@ struct console { atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct nbcon_context __private nbcon_device_ctxt; + struct printk_buffers *pbufs; + struct task_struct *kthread; + struct rcuwait rcuwait; + struct irq_work irq_work; }; =20 #ifdef CONFIG_LOCKDEP diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index ad631824e7e5..14f7fc71e20d 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -48,6 +48,7 @@ struct printk_ringbuffer; struct dev_printk_info; =20 extern struct printk_ringbuffer *prb; +extern bool printk_kthreads_running; =20 __printf(4, 0) int vprintk_store(int facility, int level, @@ -90,6 +91,9 @@ enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(void); bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, int cookie); +bool nbcon_kthread_create(struct console *con); +void nbcon_kthread_stop(struct console *con); +void nbcon_kthreads_wake(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -125,12 +129,34 @@ static inline bool console_is_usable(struct console *= con, short flags, bool use_ return true; } =20 +/** + * nbcon_kthread_wake - Wake up a console printing thread + * @con: Console to operate on + */ +static inline void nbcon_kthread_wake(struct console *con) +{ + /* + * Guarantee any new records can be seen by tasks preparing to wait + * before this context checks if the rcuwait is empty. + * + * The full memory barrier in rcuwait_wake_up() pairs with the full + * memory barrier within set_current_state() of + * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait() + * adds the waiter but before it has checked the wait condition. + * + * This pairs with nbcon_kthread_func:A. + */ + rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */ +} + #else =20 #define PRINTK_PREFIX_MAX 0 #define PRINTK_MESSAGE_MAX 0 #define PRINTKRB_RECORD_MAX 0 =20 +#define printk_kthreads_running (false) + /* * In !PRINTK builds we still export console_sem * semaphore and some of console functions (console_unlock()/etc.), so @@ -149,6 +175,7 @@ static inline enum nbcon_prio nbcon_get_default_prio(vo= id) { return NBCON_PRIO_N static inline void nbcon_atomic_flush_pending(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, int cookie) { return false; } +static inline void nbcon_kthread_wake(struct console *con) { } =20 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index fbf29b3a0811..64ddb038f205 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -952,6 +953,9 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt, bool use_a * implemented the necessary callback for writing: i.e. legacy * consoles and, when atomic, nbcon consoles with no write_atomic(). * Handle it as if ownership was lost and try to continue. + * + * Note that for nbcon consoles the write_thread() callback is + * mandatory and was already checked in nbcon_alloc(). */ if (WARN_ON_ONCE((use_atomic && !con->write_atomic) || !(console_srcu_read_flags(con) & CON_NBCON))) { @@ -995,6 +999,8 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt, bool use_a =20 if (use_atomic) con->write_atomic(con, wctxt); + else + con->write_thread(con, wctxt); =20 if (!wctxt->outbuf) { /* @@ -1036,6 +1042,219 @@ static bool nbcon_emit_next_record(struct nbcon_wri= te_context *wctxt, bool use_a return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_kthread_should_wakeup - Check whether a printer thread should wak= eup + * @con: Console to operate on + * @ctxt: The nbcon context from nbcon_context_try_acquire() + * + * Return: True if the thread should shutdown or if the console is + * allowed to print and a record is available. False otherwise. + * + * After the thread wakes up, it must first check if it should shutdown be= fore + * attempting any printing. + */ +static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_= context *ctxt) +{ + bool ret =3D false; + short flags; + int cookie; + + if (kthread_should_stop()) + return true; + + cookie =3D console_srcu_read_lock(); + + flags =3D console_srcu_read_flags(con); + if (console_is_usable(con, flags, false)) { + /* Bring the sequence in @ctxt up to date */ + ctxt->seq =3D nbcon_seq_read(con); + + ret =3D prb_read_valid(prb, ctxt->seq, NULL); + } + + console_srcu_read_unlock(cookie); + return ret; +} + +/** + * nbcon_kthread_func - The printer thread function + * @__console: Console to operate on + * + * Return: 0 + */ +static int nbcon_kthread_func(void *__console) +{ + struct console *con =3D __console; + struct nbcon_write_context wctxt =3D { + .ctxt.console =3D con, + .ctxt.prio =3D NBCON_PRIO_NORMAL, + }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + short con_flags; + bool backlog; + int cookie; + +wait_for_event: + /* + * Guarantee this task is visible on the rcuwait before + * checking the wake condition. + * + * The full memory barrier within set_current_state() of + * ___rcuwait_wait_event() pairs with the full memory + * barrier within rcuwait_has_sleeper(). + * + * This pairs with rcuwait_has_sleeper:A and nbcon_kthread_wake:A. + */ + rcuwait_wait_event(&con->rcuwait, + nbcon_kthread_should_wakeup(con, ctxt), + TASK_INTERRUPTIBLE); /* LMM(nbcon_kthread_func:A) */ + + do { + if (kthread_should_stop()) + return 0; + + backlog =3D false; + + /* + * Keep the srcu read lock around the entire operation so that + * synchronize_srcu() can guarantee that the kthread stopped + * or suspended printing. + */ + cookie =3D console_srcu_read_lock(); + + con_flags =3D console_srcu_read_flags(con); + + if (console_is_usable(con, con_flags, false)) { + unsigned long lock_flags; + + con->device_lock(con, &lock_flags); + + /* + * Ensure this stays on the CPU to make handover and + * takeover possible. + */ + cant_migrate(); + + if (nbcon_context_try_acquire(ctxt)) { + /* + * If the emit fails, this context is no + * longer the owner. + */ + if (nbcon_emit_next_record(&wctxt, false)) { + nbcon_context_release(ctxt); + backlog =3D ctxt->backlog; + } + } + + con->device_unlock(con, lock_flags); + } + + console_srcu_read_unlock(cookie); + + cond_resched(); + + } while (backlog); + + goto wait_for_event; +} + +/** + * nbcon_irq_work - irq work to wake console printer thread + * @irq_work: The irq work to operate on + */ +static void nbcon_irq_work(struct irq_work *irq_work) +{ + struct console *con =3D container_of(irq_work, struct console, irq_work); + + nbcon_kthread_wake(con); +} + +static inline bool rcuwait_has_sleeper(struct rcuwait *w) +{ + /* + * Guarantee any new records can be seen by tasks preparing to wait + * before this context checks if the rcuwait is empty. + * + * This full memory barrier pairs with the full memory barrier within + * set_current_state() of ___rcuwait_wait_event(), which is called + * after prepare_to_rcuwait() adds the waiter but before it has + * checked the wait condition. + * + * This pairs with nbcon_kthread_func:A. + */ + smp_mb(); /* LMM(rcuwait_has_sleeper:A) */ + return rcuwait_active(w); +} + +/** + * nbcon_kthreads_wake - Wake up printing threads using irq_work + */ +void nbcon_kthreads_wake(void) +{ + struct console *con; + int cookie; + + if (!printk_kthreads_running) + return; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + /* + * Only schedule irq_work if the printing thread is + * actively waiting. If not waiting, the thread will + * notice by itself that it has work to do. + */ + if (rcuwait_has_sleeper(&con->rcuwait)) + irq_work_queue(&con->irq_work); + } + console_srcu_read_unlock(cookie); +} + +/* + * nbcon_kthread_stop - Stop a console printer thread + * @con: Console to operate on + */ +void nbcon_kthread_stop(struct console *con) +{ + lockdep_assert_console_list_lock_held(); + + if (!con->kthread) + return; + + kthread_stop(con->kthread); + con->kthread =3D NULL; +} + +/** + * nbcon_kthread_create - Create a console printer thread + * @con: Console to operate on + * + * Return: True if the kthread was started or already exists. + * Otherwise false and @con must not be registered. + * + * If @con was already registered, it must be unregistered before + * the global state variable @printk_kthreads_running can be set. + */ +bool nbcon_kthread_create(struct console *con) +{ + struct task_struct *kt; + + lockdep_assert_console_list_lock_held(); + + if (con->kthread) + return true; + + kt =3D kthread_run(nbcon_kthread_func, con, "pr/%s%d", con->name, con->in= dex); + if (WARN_ON(IS_ERR(kt))) { + con_printk(KERN_ERR, con, "failed to start printing thread\n"); + return false; + } + + con->kthread =3D kt; + + return true; +} + /* Track the nbcon emergency nesting per CPU. */ static DEFINE_PER_CPU(unsigned int, nbcon_pcpu_emergency_nesting); static unsigned int early_nbcon_pcpu_emergency_nesting __initdata; @@ -1397,6 +1616,12 @@ bool nbcon_alloc(struct console *con) { struct nbcon_state state =3D { }; =20 + /* The write_thread() callback is mandatory. */ + if (WARN_ON(!con->write_thread)) + return false; + + rcuwait_init(&con->rcuwait); + init_irq_work(&con->irq_work, nbcon_irq_work); nbcon_state_set(con, &state); =20 /* @@ -1419,6 +1644,13 @@ bool nbcon_alloc(struct console *con) con_printk(KERN_ERR, con, "failed to allocate printing buffer\n"); return false; } + + if (printk_kthreads_running) { + if (!nbcon_kthread_create(con)) { + kfree(con->pbufs); + return false; + } + } } =20 return true; @@ -1432,6 +1664,9 @@ void nbcon_free(struct console *con) { struct nbcon_state state =3D { }; =20 + if (printk_kthreads_running) + nbcon_kthread_stop(con); + nbcon_state_set(con, &state); =20 /* Boot consoles share global printk buffers. */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 846306ac2663..f27c76c3b5cf 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -496,6 +497,9 @@ static u64 syslog_seq; static size_t syslog_partial; static bool syslog_time; =20 +/* True when _all_ printer threads are available for printing. */ +bool printk_kthreads_running; + struct latched_seq { seqcount_latch_t latch; u64 val[2]; @@ -3027,6 +3031,8 @@ static bool console_emit_next_record(struct console *= con, bool *handover, int co return false; } =20 +static inline void printk_kthreads_check_locked(void) { } + #endif /* CONFIG_PRINTK */ =20 /* @@ -3387,6 +3393,102 @@ void console_start(struct console *console) } EXPORT_SYMBOL(console_start); =20 +#ifdef CONFIG_PRINTK +static int unregister_console_locked(struct console *console); + +/* True when system boot is far enough to create printer threads. */ +static bool printk_kthreads_ready __ro_after_init; + +/** + * printk_kthreads_shutdown - shutdown all threaded printers + * + * On system shutdown all threaded printers are stopped. This allows printk + * to transition back to atomic printing, thus providing a robust mechanism + * for the final shutdown/reboot messages to be output. + */ +static void printk_kthreads_shutdown(void) +{ + struct console *con; + + console_list_lock(); + if (printk_kthreads_running) { + printk_kthreads_running =3D false; + + for_each_console(con) { + if (con->flags & CON_NBCON) + nbcon_kthread_stop(con); + } + + /* + * The threads may have been stopped while printing a + * backlog. Flush any records left over. + */ + nbcon_atomic_flush_pending(); + } + console_list_unlock(); +} + +static struct syscore_ops printk_syscore_ops =3D { + .shutdown =3D printk_kthreads_shutdown, +}; + +/* + * If appropriate, start nbcon kthreads and set @printk_kthreads_running. + * If any kthreads fail to start, those consoles are unregistered. + * + * Must be called under console_list_lock(). + */ +static void printk_kthreads_check_locked(void) +{ + struct hlist_node *tmp; + struct console *con; + + lockdep_assert_console_list_lock_held(); + + if (!printk_kthreads_ready) + return; + + /* + * Printer threads cannot be started as long as any boot console is + * registered because there is no way to synchronize the hardware + * registers between boot console code and regular console code. + * It can only be known that there will be no new boot consoles when + * an nbcon console is registered. + */ + if (have_boot_console || !have_nbcon_console) { + /* Clear flag in case all nbcon consoles unregistered. */ + printk_kthreads_running =3D false; + return; + } + + if (printk_kthreads_running) + return; + + hlist_for_each_entry_safe(con, tmp, &console_list, node) { + if (!(con->flags & CON_NBCON)) + continue; + + if (!nbcon_kthread_create(con)) + unregister_console_locked(con); + } + + printk_kthreads_running =3D true; +} + +static int __init printk_set_kthreads_ready(void) +{ + register_syscore_ops(&printk_syscore_ops); + + console_list_lock(); + printk_kthreads_ready =3D true; + printk_kthreads_check_locked(); + console_list_unlock(); + + return 0; +} +early_initcall(printk_set_kthreads_ready); +#endif /* CONFIG_PRINTK */ + static int __read_mostly keep_bootcon; =20 static int __init keep_bootcon_setup(char *str) @@ -3745,6 +3847,9 @@ void register_console(struct console *newcon) unregister_console_locked(con); } } + + /* Changed console list, may require printer threads to start/stop. */ + printk_kthreads_check_locked(); unlock: console_list_unlock(); } @@ -3841,6 +3946,9 @@ static int unregister_console_locked(struct console *= console) if (!found_nbcon_con) have_nbcon_console =3D found_nbcon_con; =20 + /* Changed console list, may require printer threads to start/stop. */ + printk_kthreads_check_locked(); + return res; } =20 --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6286513774A for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; cv=none; b=AwaSCTPc6TfRpr6szGYOw712SQeVLi90Xafw6e3wWY6IsTzE6spvjQHdWqjMmnmC8Yq/7MEzRy+NhN5nUqffSMrXr0ujod2Bon6ELfCf/LiWKCcW1NEdBVfYqeGbwjkZ5hUJK1HOthIjZe1f7ctpQ6mh+wQMiYfnYXii9EQB4mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; c=relaxed/simple; bh=7eegQmt3hEyn9H+Al+bhIAM7Wh9qu2cgC0jacH9a938=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=doD9KUmAcgahq7Fuf9F/pTqOZ7qESkNMma2+bsyngLa//aKioETZwLcCSLLj0esbskz/PDkzR6rJDf8nRiK2HlmwXTyKtJyPsUK9Dn5OSNYvpfvdZcP8Lsq479ySQxgwGReIp2CwS6DWFTdppQG2id/dYtj+6s4SkIkEgniOUHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zJDbFaZJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=XsWMBLoe; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zJDbFaZJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="XsWMBLoe" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733816; 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=bBCZPlALTUwtzeChOhHUyuQr3VmHSlElSg8MX8Xo0ts=; b=zJDbFaZJ3dor3p75i3vrjy8vn53rVPJvK711P1koDURg4YtUzdlx6iI5EnkRDXGukdRUXZ KjiOCvirDiIvBHJZa3k+guxbqvTFeJbT0TL+elKNwtpMfY6Yq7UXsnDcy/Ept/JEfcVi2a DBetpfMmfJLr85Nh2h8nH243MUUtowPSnrnf5x9sVocoxNf/I1ixotK+YqVXp/H0k9PTWb glmid337CfeXwOgq6d/hcDilql0DPvi9G9o5EpSDP3u1yMeRFp7Fb40wd4KJN3jHcH1TgP mdI3tMtbFqVg3B6shZJeiYmk01Pzra82Gy0rkMx9yuM781fPT+fZFsa4MVz1aQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733816; 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=bBCZPlALTUwtzeChOhHUyuQr3VmHSlElSg8MX8Xo0ts=; b=XsWMBLoeQUxGzuv/QtRub+Re0AAfP0QV2Ek9wUlbhYm5ehv0Gl0M0E2gD6Bi0kcY/4hZY1 5ByFdUKUilsRXYAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 07/17] printk: nbcon: Relocate nbcon_atomic_emit_one() Date: Tue, 27 Aug 2024 06:49:23 +0206 Message-Id: <20240827044333.88596-8-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move nbcon_atomic_emit_one() so that it can be used by nbcon_kthread_func() in a follow-up commit. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 78 +++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 64ddb038f205..16e7d992b054 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1042,6 +1042,45 @@ static bool nbcon_emit_next_record(struct nbcon_writ= e_context *wctxt, bool use_a return nbcon_context_exit_unsafe(ctxt); } =20 +/* + * nbcon_atomic_emit_one - Print one record for an nbcon console using the + * write_atomic() callback + * @wctxt: An initialized write context struct to use for this context + * + * Return: True, when a record has been printed and there are still + * pending records. The caller might want to continue flushing. + * + * False, when there is no pending record, or when the console + * context cannot be acquired, or the ownership has been lost. + * The caller should give up. Either the job is done, cannot be + * done, or will be handled by the owning context. + * + * 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 =3D &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. + * + * The higher priority printing context takes over responsibility + * to print the pending records. + */ + if (!nbcon_emit_next_record(wctxt, true)) + return false; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + /** * nbcon_kthread_should_wakeup - Check whether a printer thread should wak= eup * @con: Console to operate on @@ -1311,45 +1350,6 @@ enum nbcon_prio nbcon_get_default_prio(void) return NBCON_PRIO_NORMAL; } =20 -/* - * nbcon_atomic_emit_one - Print one record for an nbcon console using the - * write_atomic() callback - * @wctxt: An initialized write context struct to use for this context - * - * Return: True, when a record has been printed and there are still - * pending records. The caller might want to continue flushing. - * - * False, when there is no pending record, or when the console - * context cannot be acquired, or the ownership has been lost. - * The caller should give up. Either the job is done, cannot be - * done, or will be handled by the owning context. - * - * 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 =3D &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. - * - * The higher priority printing context takes over responsibility - * to print the pending records. - */ - if (!nbcon_emit_next_record(wctxt, true)) - return false; - - nbcon_context_release(ctxt); - - return ctxt->backlog; -} - /** * nbcon_legacy_emit_next_record - Print one record for an nbcon console * in legacy contexts --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4467512FF72 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; cv=none; b=bXhnVIzpcIMTAyAHp0mJAglaVYZFf/soZIrbVuaRozDLjfAo9IXuCOjB0sVFChqzqIdYaAGaqbVMjhEzhUXrGBr1srkqh1UkWhyWJRJ7DY2FcVeaEoF+STS3w+KA3K48aJOX/zmjUWNNLKstVajTVWZox1/I7kT/iARu6mFBnTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; c=relaxed/simple; bh=A3ulJTzJPBBb+Lxeblzm4LUl+9jEPD6h4aksrrpF0Ag=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kWEi66u41WYCa/ccNo/SYqWJvobnYimZ2zNqpg9PxvfkDgeK3wQm8hANI4owX9lit+SWwNf3QHSTlhSAfQ2EFAyQXjvCH4W5cufI5hE5nZJC1GJuOyGzsl0MvdcBuEVw1Zl2cPbCNMMR956Cti/ICll/GgqKLKIpgqzH8x4dQpw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=yWxEOqW2; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CHGaeZxT; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="yWxEOqW2"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CHGaeZxT" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733816; 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=caNqIaS0JgAsWn+W8Lb/kvwJaj7rAwQYoZMr2ZxDmqY=; b=yWxEOqW2Y/4wg7Jhf+c7/E6O7r7Wo23z+IfDJJPKgWmJYVuwkIPOjY5ukT9pIlZrPHAV0t x/qNNIJ1vZHgM3xWhl2silxZKe2jBMqYD3OWeBTu1aj0BL62zdcjYmkW0K5+OnmI1L15Ap onKY5okckfkPrSVPGFh++5w7krH4Y1pWEhmUaNVMhmiAWsiVPTrlY/XZxNx6onLUmkajiI By3+69BLPniCJ2hCxBBJmNLZDlIin15GVyShHvEDBIYYU/noLcJnMio2LyGCQ2tFPElmYN eMqYFzUamlHwuRCwpCIFpt46UDJcPd7Zd1Elc0XbhC1+cYRRQYDKtNTiyDYX3w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733816; 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=caNqIaS0JgAsWn+W8Lb/kvwJaj7rAwQYoZMr2ZxDmqY=; b=CHGaeZxTbdFvSmwPIcxSJl/ffSWAZofH91uSkjcXsI/HEvpp5Am/a1LXIsKx9TFbXm/VWX EhTW99/sm97kLkAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 08/17] printk: nbcon: Use thread callback if in task context for legacy Date: Tue, 27 Aug 2024 06:49:24 +0206 Message-Id: <20240827044333.88596-9-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When printing via console_lock, the write_atomic() callback is used for nbcon consoles. However, if it is known that the current context is a task context, the write_thread() callback can be used instead. Using write_thread() instead of write_atomic() helps to reduce large disabled preemption regions when the device_lock does not disable preemption. This is mainly a preparatory change to allow avoiding write_atomic() completely during normal operation if boot consoles are registered. As a side-effect, it also allows consolidating the printing code for legacy printing and the kthread printer. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 4 +- kernel/printk/nbcon.c | 95 +++++++++++++++++++++++----------------- kernel/printk/printk.c | 5 ++- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 14f7fc71e20d..a96d4114a1db 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -90,7 +90,7 @@ void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(void); bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, - int cookie); + int cookie, bool use_atomic); bool nbcon_kthread_create(struct console *con); void nbcon_kthread_stop(struct console *con); void nbcon_kthreads_wake(void); @@ -174,7 +174,7 @@ static inline void nbcon_free(struct console *con) { } static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } static inline void nbcon_atomic_flush_pending(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, - int cookie) { return false; } + int cookie, bool use_atomic) { return false; } static inline void nbcon_kthread_wake(struct console *con) { } =20 static inline bool console_is_usable(struct console *con, short flags, diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 16e7d992b054..623f3a29bfc6 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1043,9 +1043,10 @@ static bool nbcon_emit_next_record(struct nbcon_writ= e_context *wctxt, bool use_a } =20 /* - * nbcon_atomic_emit_one - Print one record for an nbcon console using the - * write_atomic() callback + * nbcon_emit_one - Print one record for an nbcon console using the + * specified callback * @wctxt: An initialized write context struct to use for this context + * @use_atomic: True if the write_atomic() callback is to be used * * Return: True, when a record has been printed and there are still * pending records. The caller might want to continue flushing. @@ -1058,12 +1059,25 @@ static bool nbcon_emit_next_record(struct nbcon_wri= te_context *wctxt, bool use_a * 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) +static bool nbcon_emit_one(struct nbcon_write_context *wctxt, bool use_ato= mic) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con =3D ctxt->console; + unsigned long flags; + bool ret =3D false; + + if (!use_atomic) { + con->device_lock(con, &flags); + + /* + * Ensure this stays on the CPU to make handover and + * takeover possible. + */ + cant_migrate(); + } =20 if (!nbcon_context_try_acquire(ctxt)) - return false; + goto out; =20 /* * nbcon_emit_next_record() returns false when the console was @@ -1073,12 +1087,16 @@ static bool nbcon_atomic_emit_one(struct nbcon_writ= e_context *wctxt) * The higher priority printing context takes over responsibility * to print the pending records. */ - if (!nbcon_emit_next_record(wctxt, true)) - return false; + if (!nbcon_emit_next_record(wctxt, use_atomic)) + goto out; =20 nbcon_context_release(ctxt); =20 - return ctxt->backlog; + ret =3D ctxt->backlog; +out: + if (!use_atomic) + con->device_unlock(con, flags); + return ret; } =20 /** @@ -1163,30 +1181,8 @@ static int nbcon_kthread_func(void *__console) =20 con_flags =3D console_srcu_read_flags(con); =20 - if (console_is_usable(con, con_flags, false)) { - unsigned long lock_flags; - - con->device_lock(con, &lock_flags); - - /* - * Ensure this stays on the CPU to make handover and - * takeover possible. - */ - cant_migrate(); - - if (nbcon_context_try_acquire(ctxt)) { - /* - * If the emit fails, this context is no - * longer the owner. - */ - if (nbcon_emit_next_record(&wctxt, false)) { - nbcon_context_release(ctxt); - backlog =3D ctxt->backlog; - } - } - - con->device_unlock(con, lock_flags); - } + if (console_is_usable(con, con_flags, false)) + backlog =3D nbcon_emit_one(&wctxt, false); =20 console_srcu_read_unlock(cookie); =20 @@ -1359,6 +1355,13 @@ enum nbcon_prio nbcon_get_default_prio(void) * both the console_lock and the SRCU read lock. Otherwise it * is set to false. * @cookie: The cookie from the SRCU read lock. + * @use_atomic: Set true when called in an atomic or unknown context. + * It affects which nbcon callback will be used: write_atomic() + * or write_thread(). + * + * When false, the write_thread() callback is used and would be + * called in a preemtible context unless disabled by the + * device_lock. The legacy handover is not allowed in this mode. * * Context: Any context except NMI. * Return: True, when a record has been printed and there are still @@ -1374,26 +1377,36 @@ enum nbcon_prio nbcon_get_default_prio(void) * Essentially it is the nbcon version of console_emit_next_record(). */ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, - int cookie) + int cookie, bool use_atomic) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); unsigned long flags; bool progress; =20 - /* Use the same procedure as console_emit_next_record(). */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); - stop_critical_timings(); - ctxt->console =3D con; ctxt->prio =3D nbcon_get_default_prio(); =20 - progress =3D nbcon_atomic_emit_one(&wctxt); + if (use_atomic) { + /* + * In an atomic or unknown context, use the same procedure as + * in console_emit_next_record(). It allows to handover. + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + } =20 - start_critical_timings(); - *handover =3D console_lock_spinning_disable_and_check(cookie); - printk_safe_exit_irqrestore(flags); + progress =3D nbcon_emit_one(&wctxt, use_atomic); + + if (use_atomic) { + start_critical_timings(); + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + } else { + /* Non-atomic does not perform legacy spinning handovers. */ + *handover =3D false; + } =20 return progress; } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f27c76c3b5cf..55d75db00042 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3077,12 +3077,13 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove u64 printk_seq; bool progress; =20 - if (!console_is_usable(con, flags, true)) + if (!console_is_usable(con, flags, !do_cond_resched)) continue; any_usable =3D true; =20 if (flags & CON_NBCON) { - progress =3D nbcon_legacy_emit_next_record(con, handover, cookie); + progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, + !do_cond_resched); printk_seq =3D nbcon_seq_read(con); } else { progress =3D console_emit_next_record(con, handover, cookie); --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8558613B2A2 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; cv=none; b=leGy+IVsSz1wOdY9H/XSJFjkYnP6ZXZdl/fp/jUAx9fdnVZ38WYIb63EsJLlmsFm6oQTo/Ns3YTOf4zw3YIbKE0PJYi5o1PCxoRXPxfUA1o2lt4FquLkIS+Nky4BvNb5KLSu8eBiM2TAYsueXeSFRelTCr4KJngfOBGqbnLyKz4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; c=relaxed/simple; bh=ElmPOUXsvgc0G41nMuenMQpbF283d7xvqQqcq7eHSTo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kHhgKmvODCXjvpsvysSf/r/tqrQgyU/IoasxCYg8A3I6lbodzl2R7FMjufGFd96LO9v96htzcwV46PzwuwURIesWx0Rj5HsdaS7m3JUAWlBoN610Xoz49lJK+uLGwuGoK8XkrLNqj5VYFZs5Yr3+lP4OT781zGBA8W+bTvcIntc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bZk6nBLq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0BlMdx+m; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bZk6nBLq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0BlMdx+m" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733816; 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=Oe55abLAVC1kEjhyyY1g5mgfxv/PHIPAi3hXGOp+MPk=; b=bZk6nBLqiQt2J+j1WYMyHD4jlzJeYFnMl9CFYggbR0Q4g8Dwmdx3vfzxDZnHxZyqMNm924 x6CQ3n9LOab/HePY0lqmNO7hudX+QOopTaCd7C31xm8pB6DgoyjAohYCW9lMrK9HUIEyQ4 qPvB2OCHXgUvsQxs1zNdgyK+XXygGv9JnZGLuDEN+tqwNcHTVvoz/MaonosdkJg0WuTab3 IJCbuN3TW5IqcgdfhdgAzc3EnzycyeTRgHaMlvNpqUsPUFPHh1a3uIdjcFjDfZfeluZqaW Jx+tYiuTD07jwDUp+ytdVFrOk1CTZN2SDDfbSisQpS5Wast5pYULZXH1FC4qRg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733816; 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=Oe55abLAVC1kEjhyyY1g5mgfxv/PHIPAi3hXGOp+MPk=; b=0BlMdx+mpBH11Fq7EZiDoMnZZBGJaAtSeqodo83IyZrH3XCW0tuBNwcaf3qdHL5SwoPGRk tLMHTXf1KnUK/sBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 09/17] printk: nbcon: Rely on kthreads for normal operation Date: Tue, 27 Aug 2024 06:49:25 +0206 Message-Id: <20240827044333.88596-10-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Once the kthread is running and available (i.e. @printk_kthreads_running is set), the kthread becomes responsible for flushing any pending messages which are added in NBCON_PRIO_NORMAL context. Namely the legacy console_flush_all() and device_release() no longer flush the console. And nbcon_atomic_flush_pending() used by nbcon_cpu_emergency_exit() no longer flushes messages added after the emergency messages. The console context is safe when used by the kthread only when one of the following conditions are true: 1. Other caller acquires the console context with NBCON_PRIO_NORMAL with preemption disabled. It will release the context before rescheduling. 2. Other caller acquires the console context with NBCON_PRIO_NORMAL under the device_lock. 3. The kthread is the only context which acquires the console with NBCON_PRIO_NORMAL. This is satisfied for all atomic printing call sites: nbcon_legacy_emit_next_record() (#1) nbcon_atomic_flush_pending_con() (#1) nbcon_device_release() (#2) It is even double guaranteed when @printk_kthreads_running is set because then _only_ the kthread will print for NBCON_PRIO_NORMAL. (#3) Signed-off-by: John Ogness --- kernel/printk/internal.h | 26 ++++++++++++++++++++++++ kernel/printk/nbcon.c | 17 ++++++++++------ kernel/printk/printk.c | 43 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 7 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index a96d4114a1db..8166e24f8780 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -113,6 +113,13 @@ static inline bool console_is_usable(struct console *c= on, short flags, bool use_ /* The write_atomic() callback is optional. */ if (use_atomic && !con->write_atomic) return false; + + /* + * For the !use_atomic case, @printk_kthreads_running is not + * checked because the write_thread() callback is also used + * via the legacy loop when the printer threads are not + * available. + */ } else { if (!con->write) return false; @@ -176,6 +183,7 @@ static inline void nbcon_atomic_flush_pending(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, int cookie, bool use_atomic) { return false; } static inline void nbcon_kthread_wake(struct console *con) { } +static inline void nbcon_kthreads_wake(void) { } =20 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic) { return false; } @@ -190,6 +198,7 @@ extern bool legacy_allow_panic_sync; /** * struct console_flush_type - Define available console flush methods * @nbcon_atomic: Flush directly using nbcon_atomic() callback + * @nbcon_offload: Offload flush to printer thread * @legacy_direct: Call the legacy loop in this context * @legacy_offload: Offload the legacy loop into IRQ * @@ -197,6 +206,7 @@ extern bool legacy_allow_panic_sync; */ struct console_flush_type { bool nbcon_atomic; + bool nbcon_offload; bool legacy_direct; bool legacy_offload; }; @@ -211,6 +221,22 @@ static inline void printk_get_console_flush_type(struc= t console_flush_type *ft) =20 switch (nbcon_get_default_prio()) { case NBCON_PRIO_NORMAL: + if (have_nbcon_console && !have_boot_console) { + if (printk_kthreads_running) + ft->nbcon_offload =3D true; + else + ft->nbcon_atomic =3D true; + } + + /* Legacy consoles are flushed directly when possible. */ + if (have_legacy_console || have_boot_console) { + if (!is_printk_legacy_deferred()) + ft->legacy_direct =3D true; + else + ft->legacy_offload =3D true; + } + break; + case NBCON_PRIO_EMERGENCY: if (have_nbcon_console && !have_boot_console) ft->nbcon_atomic =3D true; diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 623f3a29bfc6..c36473fbbf89 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1486,6 +1486,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq, static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq, bool allow_unsafe_takeover) { + struct console_flush_type ft; unsigned long flags; int err; =20 @@ -1515,10 +1516,12 @@ static void nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq, =20 /* * If flushing was successful but more records are available, this - * context must flush those remaining records because there is no - * other context that will do it. + * context must flush those remaining records if the printer thread + * is not available do it. */ - if (prb_read_valid(prb, nbcon_seq_read(con), NULL)) { + printk_get_console_flush_type(&ft); + if (!ft.nbcon_offload && + prb_read_valid(prb, nbcon_seq_read(con), NULL)) { stop_seq =3D prb_next_reserve_seq(prb); goto again; } @@ -1745,17 +1748,19 @@ void nbcon_device_release(struct console *con) =20 /* * This context must flush any new records added while the console - * was locked. The console_srcu_read_lock must be taken to ensure - * the console is usable throughout flushing. + * was locked if the printer thread is not available to do it. The + * console_srcu_read_lock must be taken to ensure the console is + * usable throughout flushing. */ cookie =3D console_srcu_read_lock(); + printk_get_console_flush_type(&ft); if (console_is_usable(con, console_srcu_read_flags(con), true) && + !ft.nbcon_offload && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { /* * If nbcon_atomic flushing is not available, fallback to * using the legacy loop. */ - printk_get_console_flush_type(&ft); if (ft.nbcon_atomic) { __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); } else if (ft.legacy_direct) { diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 55d75db00042..0d45d82a7c79 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2384,6 +2384,9 @@ asmlinkage int vprintk_emit(int facility, int level, if (ft.nbcon_atomic) nbcon_atomic_flush_pending(); =20 + if (ft.nbcon_offload) + nbcon_kthreads_wake(); + if (ft.legacy_direct) { /* * The caller may be holding system-critical or @@ -2732,6 +2735,7 @@ void suspend_console(void) =20 void resume_console(void) { + struct console_flush_type ft; struct console *con; =20 if (!console_suspend_enabled) @@ -2749,6 +2753,10 @@ void resume_console(void) */ synchronize_srcu(&console_srcu); =20 + printk_get_console_flush_type(&ft); + if (ft.nbcon_offload) + nbcon_kthreads_wake(); + pr_flush(1000, true); } =20 @@ -3060,6 +3068,7 @@ static inline void printk_kthreads_check_locked(void)= { } */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { + struct console_flush_type ft; bool any_usable =3D false; struct console *con; bool any_progress; @@ -3071,12 +3080,21 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove do { any_progress =3D false; =20 + printk_get_console_flush_type(&ft); + cookie =3D console_srcu_read_lock(); for_each_console_srcu(con) { short flags =3D console_srcu_read_flags(con); u64 printk_seq; bool progress; =20 + /* + * console_flush_all() is only for legacy consoles when + * the nbcon consoles have their printer threads. + */ + if ((flags & CON_NBCON) && ft.nbcon_offload) + continue; + if (!console_is_usable(con, flags, !do_cond_resched)) continue; any_usable =3D true; @@ -3387,9 +3405,25 @@ EXPORT_SYMBOL(console_stop); =20 void console_start(struct console *console) { + struct console_flush_type ft; + bool is_nbcon; + console_list_lock(); console_srcu_write_flags(console, console->flags | CON_ENABLED); + is_nbcon =3D console->flags & CON_NBCON; console_list_unlock(); + + /* + * Ensure that all SRCU list walks have completed. The related + * printing context must be able to see it is enabled so that + * it is guaranteed to wake up and resume printing. + */ + synchronize_srcu(&console_srcu); + + printk_get_console_flush_type(&ft); + if (is_nbcon && ft.nbcon_offload) + nbcon_kthread_wake(console); + __pr_flush(console, 1000, true); } EXPORT_SYMBOL(console_start); @@ -4152,8 +4186,10 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre * that they make forward progress, so only increment * @diff for usable consoles. */ - if (!console_is_usable(c, flags, true)) + if (!console_is_usable(c, flags, true) && + !console_is_usable(c, flags, false)) { continue; + } =20 if (flags & CON_NBCON) { printk_seq =3D nbcon_seq_read(c); @@ -4629,8 +4665,13 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind); */ void console_try_replay_all(void) { + struct console_flush_type ft; + + printk_get_console_flush_type(&ft); if (console_trylock()) { __console_rewind_all(); + if (ft.nbcon_offload) + nbcon_kthreads_wake(); /* Consoles are flushed as part of console_unlock(). */ console_unlock(); } --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63EF3138490 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; cv=none; b=ey8TdH9JP4yxZY2j0RP5OgPdeJaSoGDvd8PsFHyCK24JWc1U9b/uwlmLBqZ+32+rz4qiwjVjESpQuyISUOnRf9N9k2m3JltTaf0PPLpbmSaY/osfwlni+W+2APjhwIJuXKQvzOI5Y6BQDGVqeTLkSkEqER1O9v7t8Rzyokpr5jM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; c=relaxed/simple; bh=77nORTXpI1q4KDBhB66CLINRXF3REoaPCkl5EJvjm9I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ER1nMunP7A2ZbEkLOCWoEIVozXpmEOTZA41Ozjs1DZAP7k7wTijZ6R8EDwzky4/d8xNHUXKFQwddNXHIuQhssRjX4Ml0q/jRoRnTiAZNU5Ed5aVdYCIEDSYcFq05o3tiAllDpvVo+hBWcnJJEfJyIe0Wj1gJErUupZw+Xt5JmF8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=EpUQzoTF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1jj/IdKz; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="EpUQzoTF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1jj/IdKz" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733817; 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=hrB6oXwNT8+Bqp3Goa78TH7hK15/gLFUczJKrHMn7MM=; b=EpUQzoTFCPot6l9kNIc61iMjdwyxymI1459e5YFxtgN73jhndCnok8QtDYBIlJF8rA1jNm qK5u9pBAHqBK0NWxkhJc9xtTtP1k6QuDgx27nI0EUiQEDq5LIid2ZUHJOlKrEe6w9RKvrh LVjoKOBelxaiDNUT8RUiQx05CYKLDbZ9eTeh02Zk/uIbifvkQzvLU8EC0vLnoLgiYM44Br G02R/IbEBCsqY9KOOf+HzXoLZiry7n/H2+pkeavnltFmL6YglhCkp38cFBVDDI7rSDoEri 83f+LLHQSO1XjuJm0rT8EmGUUje8+oyf61thBMhCEz7WhD7AHPgSqnCRlXKPEw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733817; 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=hrB6oXwNT8+Bqp3Goa78TH7hK15/gLFUczJKrHMn7MM=; b=1jj/IdKzMTaJKs7gRMGq3gFIHMY0GHPR4hVy8NYyTU4BPPMtpf1c0zWRjw35rWnYKpc74e 1ylfRVNhit+sulAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 10/17] printk: Provide helper for message prepending Date: Tue, 27 Aug 2024 06:49:26 +0206 Message-Id: <20240827044333.88596-11-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to support prepending different texts to printk messages, split out the prepending code into a helper function. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 0d45d82a7c79..fba923730af9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2843,30 +2843,31 @@ static void __console_unlock(void) #ifdef CONFIG_PRINTK =20 /* - * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". T= his - * is achieved by shifting the existing message over and inserting the dro= pped - * message. + * Prepend the message in @pmsg->pbufs->outbuf. This is achieved by shifti= ng + * the existing message over and inserting the scratchbuf message. * - * @pmsg is the printk message to prepend. - * - * @dropped is the dropped count to report in the dropped message. + * @pmsg is the original printk message. + * @fmt is the printf format of the message which will prepend the existin= g one. * - * If the message text in @pmsg->pbufs->outbuf does not have enough space = for - * the dropped message, the message text will be sufficiently truncated. + * If there is not enough space in @pmsg->pbufs->outbuf, the existing + * message text will be sufficiently truncated. * * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated. */ -void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped) +__printf(2, 3) +static void console_prepend_message(struct printk_message *pmsg, const cha= r *fmt, ...) { struct printk_buffers *pbufs =3D pmsg->pbufs; const size_t scratchbuf_sz =3D sizeof(pbufs->scratchbuf); const size_t outbuf_sz =3D sizeof(pbufs->outbuf); char *scratchbuf =3D &pbufs->scratchbuf[0]; char *outbuf =3D &pbufs->outbuf[0]; + va_list args; size_t len; =20 - len =3D scnprintf(scratchbuf, scratchbuf_sz, - "** %lu printk messages dropped **\n", dropped); + va_start(args, fmt); + len =3D vscnprintf(scratchbuf, scratchbuf_sz, fmt, args); + va_end(args); =20 /* * Make sure outbuf is sufficiently large before prepending. @@ -2888,6 +2889,19 @@ void console_prepend_dropped(struct printk_message *= pmsg, unsigned long dropped) pmsg->outbuf_len +=3D len; } =20 +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". + * @pmsg->outbuf_len is updated appropriately. + * + * @pmsg is the printk message to prepend. + * + * @dropped is the dropped count to report in the dropped message. + */ +void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped) +{ + console_prepend_message(pmsg, "** %lu printk messages dropped **\n", drop= ped); +} + /* * Read and format the specified record (or a later record if the specified * record is not available). --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F30F313D638 for ; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733823; cv=none; b=YlxY0FR7X+SA1NPfwW8GfZuZ0xNFlasEXyiOpPO+6LkdGzF9gQykhtfL8/ZybD2y9zhXluMpDdbn1ZBWaDdgMiFQrFBv6YDku2DxL4rO85stx25y/csxzaH/w7lofYWnPKdPpi3Iy9zOSIMZf7hAnuToV570zPTvHA+Vkp0buR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733823; c=relaxed/simple; bh=17qf7HNpQC42DYqY50wFkJfCpMR9/cCGFUPFO6ROXBw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TweCxVSUjKRd829szGQTA9mA7hsO3Qfxr3ZDszGgdHGWmydrki2+P+N4CcBKrLpWzUdRHbDwmfj7txx3mkxUfjUhPBmgi1mOyyredpD4jBxuPzMQJIPvq6nrg49BWrDA2F0mtKSQySPdxYvZKQEkoyw8hzsTyIz3qFOdiDdanKU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bKKjCMJK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=T9r7gHJs; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bKKjCMJK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="T9r7gHJs" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733817; 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=OP6737NuSJ97dO1aEzW4dBtfZQ43WSf6UBGSkHNiaME=; b=bKKjCMJKca0R+Q/1XLr3f5YyfXkFmDMmiJKhN2VcArH8h3B0Pgtbjbh1H3ytj8pXhjgmeF 7rOedV1GJHcfPRIpFA425XJs/R+aaKz6gpmAo9cX/Q3QEzon9f+mEx/7gXop9PwT2DtA3Q pLerB6o/KG8IXMVL+aEWGs+Sh4sRT/KBB22EQ9NsnX2zquhb5KG4dKtDcHBSLUwuFyJwHG +wYQx5M6ptwXvYtSPhU9pbfac0rsyCgYE81k3iJdeWUfo/M+cUsWfoME2wE03m4AVXF4W2 5kzbR7yxFXlPFLL/QTC47SKwPG8Wsqf4dVjDrrpRpNL4LhvqBmxCox8V1xk/MQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733817; 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=OP6737NuSJ97dO1aEzW4dBtfZQ43WSf6UBGSkHNiaME=; b=T9r7gHJsO9RdYph8mdP5rNulqoq1oL+ZUFpIWGvVVt7nKsPd9VHlRCg7tpitAm1/jTWy0t 9/i0ra7lVGp20uBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 11/17] printk: nbcon: Show replay message on takeover Date: Tue, 27 Aug 2024 06:49:27 +0206 Message-Id: <20240827044333.88596-12-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" An emergency or panic context can takeover console ownership while the current owner was printing a printk message. The atomic printer will re-print the message that the previous owner was printing. However, this can look confusing to the user and may even seem as though a message was lost. [3430014.1 [3430014.181123] usb 1-2: Product: USB Audio Add a new field @nbcon_prev_seq to struct console to track the sequence number to print that was assigned to the previous console owner. If this matches the sequence number to print that the current owner is assigned, then a takeover must have occurred. In this case, print an additional message to inform the user that the previous message is being printed again. [3430014.1 ** replaying previous printk message ** [3430014.181123] usb 1-2: Product: USB Audio Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 ++ kernel/printk/internal.h | 1 + kernel/printk/nbcon.c | 26 ++++++++++++++++++++++++++ kernel/printk/printk.c | 11 +++++++++++ 4 files changed, 40 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index 788ce9c829f6..eba367bf605d 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -325,6 +325,7 @@ struct nbcon_write_context { * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations + * @nbcon_prev_seq: Seq num the previous nbcon owner was assigned to print * @pbufs: Pointer to nbcon private buffer * @kthread: Printer kthread for this console * @rcuwait: RCU-safe wait object for @kthread waking @@ -459,6 +460,7 @@ struct console { atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct nbcon_context __private nbcon_device_ctxt; + atomic_long_t __private nbcon_prev_seq; =20 struct printk_buffers *pbufs; struct task_struct *kthread; diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 8166e24f8780..c365d25b13c7 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -319,4 +319,5 @@ bool printk_get_next_message(struct printk_message *pms= g, u64 seq, =20 #ifdef CONFIG_PRINTK void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped); +void console_prepend_replay(struct printk_message *pmsg); #endif diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index c36473fbbf89..d3a018919046 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -946,7 +946,9 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt, bool use_a .pbufs =3D ctxt->pbufs, }; unsigned long con_dropped; + struct nbcon_state cur; unsigned long dropped; + unsigned long ulseq; =20 /* * This function should never be called for consoles that have not @@ -987,6 +989,29 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt, bool use_a if (dropped && !is_extended) console_prepend_dropped(&pmsg, dropped); =20 + /* + * If the previous owner was assigned the same record, this context + * has taken over ownership and is replaying the record. Prepend a + * message to let the user know the record is replayed. + */ + ulseq =3D atomic_long_read(&ACCESS_PRIVATE(con, nbcon_prev_seq)); + if (__ulseq_to_u64seq(prb, ulseq) =3D=3D pmsg.seq) { + console_prepend_replay(&pmsg); + } else { + /* + * Ensure this context is still the owner before trying to + * update @nbcon_prev_seq. Otherwise the value in @ulseq may + * not be from the previous owner and instead be some later + * value from the context that took over ownership. + */ + nbcon_state_read(con, &cur); + if (!nbcon_context_can_proceed(ctxt, &cur)) + return false; + + atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_prev_seq), &ulseq, + __u64seq_to_ulseq(pmsg.seq)); + } + if (!nbcon_context_exit_unsafe(ctxt)) return false; =20 @@ -1638,6 +1663,7 @@ bool nbcon_alloc(struct console *con) =20 rcuwait_init(&con->rcuwait); init_irq_work(&con->irq_work, nbcon_irq_work); + atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL); nbcon_state_set(con, &state); =20 /* diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fba923730af9..8863519438f9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2902,6 +2902,17 @@ void console_prepend_dropped(struct printk_message *= pmsg, unsigned long dropped) console_prepend_message(pmsg, "** %lu printk messages dropped **\n", drop= ped); } =20 +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "replay message". + * @pmsg->outbuf_len is updated appropriately. + * + * @pmsg is the printk message to prepend. + */ +void console_prepend_replay(struct printk_message *pmsg) +{ + console_prepend_message(pmsg, "** replaying previous printk message **\n"= ); +} + /* * Read and format the specified record (or a later record if the specified * record is not available). --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00C9F13D8A2; Tue, 27 Aug 2024 04:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; cv=none; b=TOubZwGYsW3W2HqLwFFU4PmAiily8Nk5Jw/ArY2v5EXC7jHhZW7L6lbZ9l0EDbm6AsPm/O/E5HbtA4ys+ahRuTkw1V3K88nnLI+gS+gEY4kIdddtlkaVYWvx3Xn3Mu7Pp7DWca8MeP940XuBnSRri+nEUAFdnf2PCXL6MlSpMOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733821; c=relaxed/simple; bh=z7t14psmClgJPnrTS3FePajFlnKKHKwtWJNjr7MCP1s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CxmqMbn5FitQQBs73deh1JoH+n4LdHEZNOAlhbo+VH5EeVCvb4k/o9ehNL5uFWsDtrsicbtp77x2nIe/czmNQNjNBOtHnoywR6IO/8PAsYwTjR4WDf7QbYXhcYvICHLQ2WyPv6hUOmMYbnYgamcNQZjUtQ4QJqRjpnLUgC6Lt4M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BHB3UogT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YfJpvM+q; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BHB3UogT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YfJpvM+q" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733818; 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=u1Oi5xduOOsbRA26g1GCX0jTw6STXCrfhNtn4TB0hdM=; b=BHB3UogTvftKlqiCKPrWz/uqzB8i501qPGPrOUCO/KK9FZzSXn/MNpZkRKD3iyRrM7xHvM 2KTB/PzwAA2Ktocwj11i56wcFFmuaL470kaaPZQO/b7So+4Pr8KzUekWlpR0a7P61j8BjQ crhRE9uCrApEPAc0ckbZXQX+2sU5CTcWe1nk2aWVgO44MaXDE67cIPiP8Pp8O2jKZLpiWn diu+ITjipAImhAMxdSweN6JSx74njGfNUKm/wKKfia2OS0tzG3S3ioxUBW2ylLUVNZ5H3i yDPpdb+cIULtgL83vWDhleU++I6kODJAoR2lfR9sQacT+2GEYx2gSBrbTf1D7g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733818; 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=u1Oi5xduOOsbRA26g1GCX0jTw6STXCrfhNtn4TB0hdM=; b=YfJpvM+qlaS5nH8RYoGUUKn/HJ9rbTrZQ/42yE25b1FZhcdB9bTplV23zkuZEkrtTdApRY rriFHnYCuilDAiBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH printk v4 12/17] proc: consoles: Add notation to c_start/c_stop Date: Tue, 27 Aug 2024 06:49:28 +0206 Message-Id: <20240827044333.88596-13-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" fs/proc/consoles.c:78:13: warning: context imbalance in 'c_start' - wrong count at exit fs/proc/consoles.c:104:13: warning: context imbalance in 'c_stop' - unexpected unlock Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- fs/proc/consoles.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/proc/consoles.c b/fs/proc/consoles.c index e0758fe7936d..7036fdfa0bec 100644 --- a/fs/proc/consoles.c +++ b/fs/proc/consoles.c @@ -68,6 +68,7 @@ static int show_console_dev(struct seq_file *m, void *v) } =20 static void *c_start(struct seq_file *m, loff_t *pos) + __acquires(&console_mutex) { struct console *con; loff_t off =3D 0; @@ -94,6 +95,7 @@ static void *c_next(struct seq_file *m, void *v, loff_t *= pos) } =20 static void c_stop(struct seq_file *m, void *v) + __releases(&console_mutex) { console_list_unlock(); } --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68E58143C72; Tue, 27 Aug 2024 04:43:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; cv=none; b=EarNkumpgv746qe34sKlZeNZzXA0hrdPcdbvbUuLYRtg+GviNkd1qXucoaDToQ4371E6/l9wJSpERdyZzONYldV2G3sQ+MPZu7KyGxN7ntfDBdo2cWnhPYoH502TXypE0KkxWdLLyRWOUoL+qUEQmfa/3H6cxeA0icCuTS0ocmw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; c=relaxed/simple; bh=RuhFMyH0buaWaNTtg4qur9A9+lQbBJQXU2dz3dvH8mI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EKNSz2O/WnAg8wnrZ9pmsUQpfLDipoPty3YVU5CBIetr5ZuSP4xjIIlS5xGNgsH7gONJOS7gEAkr8rMx1SwYgsU2mEJB4/HTCWfBoUaz72zkK09EhfFIMXcpAjrkkAbu7Dhm7FPIkSkrVT93nT1BibujSNctiTzaLSq6iGvGND4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Q8APdDqs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fj9J9HDK; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Q8APdDqs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fj9J9HDK" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733818; 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=SlCaONAJqxgKpAXbE82io/gLxoDWz0lN5rgg6CE37cM=; b=Q8APdDqsYwyyTz3NGIPG4KO34QZmED7z2WtSy+9wdgKqZcRaEL4lOdDyBiyovwWorP/oGu jbUMTFYG4l5OysJNscTNUpd+qzDbvOlRzCAZmoSSKJdwP8byvEooYsSVIxoa6XDyy29LeE CwW4Fw3b+0XqhhD5XSTeuojk6fs8+V+KD1qW1eP/FZEwKGTYpqjISR81Q5Mp4fQH8yT9zF eSsDYT65ZDqvsqBoBOOV0yEhT9b8Qva9/rR4YUTZzPj9RASzUrymejfY+09ZUlAu1nkzJn E0umIgohdNmhURsB4OCGduMDIsIsItvhZMzGp/Xj349DzMK5DuKTPuJ3jylnow== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733818; 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=SlCaONAJqxgKpAXbE82io/gLxoDWz0lN5rgg6CE37cM=; b=fj9J9HDKzzFIOutqa1XueBnfkqeAm/yuaALXi0VXaMdysgQ5p6yeczyohat6aVkMn3PvVO uAe07MtNGKTE3mAw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH printk v4 13/17] proc: Add nbcon support for /proc/consoles Date: Tue, 27 Aug 2024 06:49:29 +0206 Message-Id: <20240827044333.88596-14-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update /proc/consoles output to show 'W' if an nbcon console is registered. Since the write_thread() callback is mandatory, it enough just to check if it is an nbcon console. Also update /proc/consoles output to show 'N' if it is an nbcon console. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- fs/proc/consoles.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/proc/consoles.c b/fs/proc/consoles.c index 7036fdfa0bec..b7cab1ad990d 100644 --- a/fs/proc/consoles.c +++ b/fs/proc/consoles.c @@ -21,6 +21,7 @@ static int show_console_dev(struct seq_file *m, void *v) { CON_ENABLED, 'E' }, { CON_CONSDEV, 'C' }, { CON_BOOT, 'B' }, + { CON_NBCON, 'N' }, { CON_PRINTBUFFER, 'p' }, { CON_BRL, 'b' }, { CON_ANYTIME, 'a' }, @@ -58,8 +59,8 @@ static int show_console_dev(struct seq_file *m, void *v) seq_printf(m, "%s%d", con->name, con->index); seq_pad(m, ' '); seq_printf(m, "%c%c%c (%s)", con->read ? 'R' : '-', - con->write ? 'W' : '-', con->unblank ? 'U' : '-', - flags); + ((con->flags & CON_NBCON) || con->write) ? 'W' : '-', + con->unblank ? 'U' : '-', flags); if (dev) seq_printf(m, " %4d:%d", MAJOR(dev), MINOR(dev)); =20 --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 776D0143C7E; Tue, 27 Aug 2024 04:43:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; cv=none; b=jLrSRpzAMxuhUBMc3r4yrlFkV32V/TgebuR7Nl5bYnS1Uc9ZnDuiwCSuaYPpqYVH+goIJ+qSdAaqNbus2hYkrFYTxLvYWDg2nJskLapwthwhPzVh1NXqVUkR+5dDJOLrXaBMGZr/ua69glZJKcphBS6cxV0e5GBIa3O3d/sKPB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733822; c=relaxed/simple; bh=dp39GBW9ZErJu1DqQO7+qMNtwGg+EqlSaBhaAISNtsU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DpIwD8LJkCcFzryVjjDEx90qwPT32d9t6nxj+9Ub6Fl0w0XmewnMArZtuaOW9KUUnx+ZI3GEsypUj0st4pl/LPhBhJ2Zn7KHIgXtgHyviRdxRAOWpR/PaulRDvBK7ze7xPzcLQxcwtGVGscitbazRRe8LfAkuZrvAF1ea3FsIHc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RSkprxwC; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GALVJHOI; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RSkprxwC"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GALVJHOI" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733818; 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=151ULJoLjYfhpZeJDTwn5PbZoaNXd1Ky9qGsR418PG8=; b=RSkprxwCF0twnNHyQ9hVvJ7T7xIZwIwpDJTIF6GBTaOUD3SeKjd/vJxbp41kGPDp85wrAL LYuKAisWM8yBo5LPvfN4l+fSbAgJWlsh71IQUfXytP63T0bKDrZkj/YoiXU+QSil5oeHve +0/3aZ7ZrOssb58uw8GpnMa4emsDqZDXorQ33gkaA+FFvIX4vgAM8rCFsh4ysgH6tqa+TS 9vRpBJ6IwoR8pSiQFKfI/47L3YbSHlsXJZZlBbncuR5rYiX5weFCYGyDI5dHywcXsC/Q0M I9Lzg2G1SLB3LTLTNPWzD9ZRVzsbPJHIsdRDZmILfKOfOUvrwYWzs/AlwLw6SQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733818; 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=151ULJoLjYfhpZeJDTwn5PbZoaNXd1Ky9qGsR418PG8=; b=GALVJHOI/1iE+zQFS+NOeX5O+/5dLCNOl1iPLgBHy1J2Ov2jwGuDD1bXPfwlCPZ2pltHmr F+MGMTvOudQHNyAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org Subject: [PATCH printk v4 14/17] tty: sysfs: Add nbcon support for 'active' Date: Tue, 27 Aug 2024 06:49:30 +0206 Message-Id: <20240827044333.88596-15-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow the 'active' attribute to list nbcon consoles. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- drivers/tty/tty_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 407b0d87b7c1..9140825e810f 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -3567,7 +3567,7 @@ static ssize_t show_cons_active(struct device *dev, for_each_console(c) { if (!c->device) continue; - if (!c->write) + if (!(c->flags & CON_NBCON) && !c->write) continue; if ((c->flags & CON_ENABLED) =3D=3D 0) continue; --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 87863153BF6 for ; Tue, 27 Aug 2024 04:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; cv=none; b=dJDo7G0CzPyLUUwEDrmDgauQ2+lKgDaTrtZKbc3/j5TnI5BN3PBNHHG+FWMG1zFaCasvmhpNwkAqRdooc3H8C+TBIyKdfXzGLDKmo0K5X56kQMp1bAAm7YOeIMpHFdDr7/OhIdTswV9tM5PoubMufPXowK2KgFQmuhI+xM52D98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; c=relaxed/simple; bh=wiWhgZ4qdA6kZFNrmxyOxUP0rslSP+DF1bHLzzKaIBc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZMdGnURf1FVdr03WrVOijCCjlz3+bWgfaLiFavoMxQQw66SKbeIb/GemSgFBy21XflmMJa6dyxvBwCh7dJQPj5UfgNudVcYu+Aftf5aL28FW0PCfjQDe/8rYeHRNi4RPXjbArD7ZxCQ9hXmeVuDQj/ozIcCekEc3/2ve3csJcE0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ME++pZQd; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=+QTeLVrG; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ME++pZQd"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="+QTeLVrG" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733819; 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=ZmkCNVznyI/FEQ01aseNGP6nsv5eVM+9zeQn4iQiGas=; b=ME++pZQd0yvQ8nvmpfV/OR5lC90Syy7MlB8aEEw7NcMVMguU6h7Bl45otmij0TU8+PT3qw b/hwiUl4uzEVYdfE1cAA+6+LnGDQbsBcc8p8WqF20R1+KzTX6hk5T861OoIJPV6tdDLJoa qgmMAjzmbqKzNhTUp/8qhRnk5R15oWRMJ1ePtSIPf0FoC5U8svEJftvMt/e4hYbKBIoB9u NIIaCRkjRM2f+rzCzFXlA8172J+j1XRO3z2MyGJatAE6IwpRtjQKTHtEP0vl9VfGiZXe4P TUo62eW6XWvxBrYQIEUXoo60iruVAn5IY3hAsXZTuqlrxki28f45L+lRS4v4AA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733819; 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=ZmkCNVznyI/FEQ01aseNGP6nsv5eVM+9zeQn4iQiGas=; b=+QTeLVrGLTMm0DvmeKktG99ezqQSDcLlztINYtLhMLgBbyAxg7Y8x7eTVC0hY+RxrP24kA 70gwPc6C/JgQyyAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 15/17] printk: Implement legacy printer kthread for PREEMPT_RT Date: Tue, 27 Aug 2024 06:49:31 +0206 Message-Id: <20240827044333.88596-16-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The write() callback of legacy consoles usually makes use of spinlocks. This is not permitted with PREEMPT_RT in atomic contexts. For PREEMPT_RT, create a new kthread to handle printing of all the legacy consoles (and nbcon consoles if boot consoles are registered). This allows legacy consoles to work on PREEMPT_RT without requiring modification. (However they will not have the reliability properties guaranteed by nbcon atomic consoles.) Use the existing printk_kthreads_check_locked() to start/stop the legacy kthread as needed. Introduce the macro force_legacy_kthread() to query if the forced threading of legacy consoles is in effect. Although currently only enabled for PREEMPT_RT, this acts as a simple mechanism for the future to allow other preemption models to easily take advantage of the non-interference property provided by the legacy kthread. When force_legacy_kthread() is true, the legacy kthread fulfills the role of the console_flush_type @legacy_offload by waking the legacy kthread instead of printing via the console_lock in the irq_work. If the legacy kthread is not yet available, no legacy printing takes place (unless in panic). If for some reason the legacy kthread fails to create, any legacy consoles are unregistered. With force_legacy_kthread(), the legacy kthread is a critical component for legacy consoles. These changes only affect CONFIG_PREEMPT_RT. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 16 +++- kernel/printk/printk.c | 162 ++++++++++++++++++++++++++++++++---- kernel/printk/printk_safe.c | 4 +- 3 files changed, 164 insertions(+), 18 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index c365d25b13c7..3fcb48502adb 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -21,6 +21,19 @@ int devkmsg_sysctl_set_loglvl(const struct ctl_table *ta= ble, int write, (con->flags & CON_BOOT) ? "boot" : "", \ con->name, con->index, ##__VA_ARGS__) =20 +/* + * Identify if legacy printing is forced in a dedicated kthread. If + * true, all printing via console lock occurs within a dedicated + * legacy printer thread. The only exception is on panic, after the + * nbcon consoles have had their chance to print the panic messages + * first. + */ +#ifdef CONFIG_PREEMPT_RT +# define force_legacy_kthread() (true) +#else +# define force_legacy_kthread() (false) +#endif + #ifdef CONFIG_PRINTK =20 #ifdef CONFIG_PRINTK_CALLER @@ -173,6 +186,7 @@ static inline void nbcon_kthread_wake(struct console *c= on) #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags) =20 static inline bool printk_percpu_data_ready(void) { return false; } +static inline void defer_console_output(void) { } static inline bool is_printk_legacy_deferred(void) { return false; } static inline u64 nbcon_seq_read(struct console *con) { return 0; } static inline void nbcon_seq_force(struct console *con, u64 seq) { } @@ -200,7 +214,7 @@ extern bool legacy_allow_panic_sync; * @nbcon_atomic: Flush directly using nbcon_atomic() callback * @nbcon_offload: Offload flush to printer thread * @legacy_direct: Call the legacy loop in this context - * @legacy_offload: Offload the legacy loop into IRQ + * @legacy_offload: Offload the legacy loop into IRQ or legacy thread * * Note that the legacy loop also flushes the nbcon consoles. */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 8863519438f9..4220abe86296 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -491,6 +491,7 @@ bool legacy_allow_panic_sync; =20 #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); +static DECLARE_WAIT_QUEUE_HEAD(legacy_wait); /* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ static u64 syslog_seq; @@ -2450,6 +2451,7 @@ static u64 syslog_seq; =20 static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true= ; } static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_= progress) { return true; } +static inline void legacy_kthread_wake(void) { } =20 #endif /* CONFIG_PRINTK */ =20 @@ -2756,6 +2758,8 @@ void resume_console(void) printk_get_console_flush_type(&ft); if (ft.nbcon_offload) nbcon_kthreads_wake(); + if (ft.legacy_offload) + defer_console_output(); =20 pr_flush(1000, true); } @@ -3165,19 +3169,7 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove return false; } =20 -/** - * console_unlock - unblock the console subsystem from printing - * - * Releases the console_lock which the caller holds to block printing of - * the console subsystem. - * - * While the console_lock was held, console output may have been buffered - * by printk(). If this is the case, console_unlock(); emits - * the output prior to releasing the lock. - * - * console_unlock(); may be called from any context. - */ -void console_unlock(void) +static void __console_flush_and_unlock(void) { bool do_cond_resched; bool handover; @@ -3221,6 +3213,29 @@ void console_unlock(void) */ } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); } + +/** + * console_unlock - unblock the legacy console subsystem from printing + * + * Releases the console_lock which the caller holds to block printing of + * the legacy console subsystem. + * + * While the console_lock was held, console output may have been buffered + * by printk(). If this is the case, console_unlock() emits the output on + * legacy consoles prior to releasing the lock. + * + * console_unlock(); may be called from any context. + */ +void console_unlock(void) +{ + struct console_flush_type ft; + + printk_get_console_flush_type(&ft); + if (ft.legacy_direct) + __console_flush_and_unlock(); + else + __console_unlock(); +} EXPORT_SYMBOL(console_unlock); =20 /** @@ -3448,6 +3463,8 @@ void console_start(struct console *console) printk_get_console_flush_type(&ft); if (is_nbcon && ft.nbcon_offload) nbcon_kthread_wake(console); + else if (!is_nbcon && ft.legacy_offload) + defer_console_output(); =20 __pr_flush(console, 1000, true); } @@ -3459,6 +3476,87 @@ static int unregister_console_locked(struct console = *console); /* True when system boot is far enough to create printer threads. */ static bool printk_kthreads_ready __ro_after_init; =20 +static struct task_struct *printk_legacy_kthread; + +static bool legacy_kthread_should_wakeup(void) +{ + struct console_flush_type ft; + struct console *con; + bool ret =3D false; + int cookie; + + if (kthread_should_stop()) + return true; + + printk_get_console_flush_type(&ft); + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + u64 printk_seq; + + /* + * The legacy printer thread is only for legacy consoles when + * the nbcon consoles have their printer threads. + */ + if ((flags & CON_NBCON) && ft.nbcon_offload) + continue; + + if (!console_is_usable(con, flags, false)) + continue; + + if (flags & CON_NBCON) { + printk_seq =3D nbcon_seq_read(con); + } else { + /* + * It is safe to read @seq because only this + * thread context updates @seq. + */ + printk_seq =3D con->seq; + } + + if (prb_read_valid(prb, printk_seq, NULL)) { + ret =3D true; + break; + } + } + console_srcu_read_unlock(cookie); + + return ret; +} + +static int legacy_kthread_func(void *unused) +{ + for (;;) { + wait_event_interruptible(legacy_wait, legacy_kthread_should_wakeup()); + + if (kthread_should_stop()) + break; + + console_lock(); + __console_flush_and_unlock(); + } + + return 0; +} + +static bool legacy_kthread_create(void) +{ + struct task_struct *kt; + + lockdep_assert_console_list_lock_held(); + + kt =3D kthread_run(legacy_kthread_func, NULL, "pr/legacy"); + if (WARN_ON(IS_ERR(kt))) { + pr_err("failed to start legacy printing thread\n"); + return false; + } + + printk_legacy_kthread =3D kt; + + return true; +} + /** * printk_kthreads_shutdown - shutdown all threaded printers * @@ -3508,6 +3606,27 @@ static void printk_kthreads_check_locked(void) if (!printk_kthreads_ready) return; =20 + if (have_legacy_console || have_boot_console) { + if (!printk_legacy_kthread && + force_legacy_kthread() && + !legacy_kthread_create()) { + /* + * All legacy consoles must be unregistered. If there + * are any nbcon consoles, they will set up their own + * kthread. + */ + hlist_for_each_entry_safe(con, tmp, &console_list, node) { + if (con->flags & CON_NBCON) + continue; + + unregister_console_locked(con); + } + } + } else if (printk_legacy_kthread) { + kthread_stop(printk_legacy_kthread); + printk_legacy_kthread =3D NULL; + } + /* * Printer threads cannot be started as long as any boot console is * registered because there is no way to synchronize the hardware @@ -4277,14 +4396,23 @@ static bool pr_flush(int timeout_ms, bool reset_on_= progress) =20 static DEFINE_PER_CPU(int, printk_pending); =20 +static void legacy_kthread_wake(void) +{ + if (printk_legacy_kthread) + wake_up_interruptible(&legacy_wait); +} + static void wake_up_klogd_work_func(struct irq_work *irq_work) { int pending =3D this_cpu_xchg(printk_pending, 0); =20 if (pending & PRINTK_PENDING_OUTPUT) { - /* If trylock fails, someone else is doing the printing */ - if (console_trylock()) - console_unlock(); + if (force_legacy_kthread()) { + legacy_kthread_wake(); + } else { + if (console_trylock()) + console_unlock(); + } } =20 if (pending & PRINTK_PENDING_WAKEUP) @@ -4697,6 +4825,8 @@ void console_try_replay_all(void) __console_rewind_all(); if (ft.nbcon_offload) nbcon_kthreads_wake(); + if (ft.legacy_offload) + defer_console_output(); /* Consoles are flushed as part of console_unlock(). */ console_unlock(); } diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c index 86439fd20aab..2b35a9d3919d 100644 --- a/kernel/printk/printk_safe.c +++ b/kernel/printk/printk_safe.c @@ -44,7 +44,9 @@ bool is_printk_legacy_deferred(void) * The per-CPU variable @printk_context can be read safely in any * context. CPU migration is always disabled when set. */ - return (this_cpu_read(printk_context) || in_nmi()); + return (force_legacy_kthread() || + this_cpu_read(printk_context) || + in_nmi()); } =20 asmlinkage int vprintk(const char *fmt, va_list args) --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89B9D154423 for ; Tue, 27 Aug 2024 04:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; cv=none; b=g7mt0Zy37C5anE+zjalQXypSUGe/thL+QfGBzcTZfPQgHL63QQUTDNFgd8JE9qDhBtEfRZPIWb45uDtM6eaqed+ZGFBJarUEhFYfJ5AjtNQ8EUI05Fi1xA1zXInJ/pxeXmvrrtsb9zX4up1ZitUlflgL4/MH8oJQRXdAQtE/n3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; c=relaxed/simple; bh=tAYGQiWA14h9cQbPx+3XGy/Lgh/XuW9AdX1hiU1vzXs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V32LuavnkK05eC0O9LKGNbZm8+tOBufWQp7WXqF0xWrX5iYChXAiQqna5CyGM6dJ+cbolpjTrsAcqBYWcuLFO4r28MZDhrW173uUZgZhWCLeyIFIqXKsr+e58DOw4tyuF8TaMa7ak220tskvyZwE3WBUtaYmdRmGpyPDXTIxnGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YSLQze48; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cWUHrWtD; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YSLQze48"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cWUHrWtD" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733819; 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=LI8DUg3IwQtyCHgTvsHuVrVO9wJnv7kQ0WO+Ps9C198=; b=YSLQze48OWAKZyUOYLP5NPUNVJAQH7vn+g4T7upbeBN+Wwb9lDFy1klvTex8LNF5XGjDsB vSATRHF5bmgscGn7heaAbeWBPlqWBiXbwGkNRg3Ps6wQMxcV5Weg3tBl7MKDzDqYSdMreF /m2S2AeN9EzBGWUC3mIzbYhFn9ry5WIGTR8Tv1c5PUBvvWi0J834fQqIm//5jw+UxkL8Ch BVq2uhct2vJTZpjK5lxzn0hAno2VDL+ktaKlERjy/XMJVrjEYjkDLIJL/EasoNwojU5x9R gLI9EBnjRY99qxso2gfXmc79baHxQhE/ADePXqB3NGatG7ko933C2XOlOY/JIg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733819; 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=LI8DUg3IwQtyCHgTvsHuVrVO9wJnv7kQ0WO+Ps9C198=; b=cWUHrWtDTr/j4Ag0O4DFdHmtHKJwMN8J8NR/TgpX+qWPaNjXe5GMmYXv/oCPsAy9bVuSz2 QSgjTQ786sXk8mBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 16/17] printk: nbcon: Assign nice -20 for printing threads Date: Tue, 27 Aug 2024 06:49:32 +0206 Message-Id: <20240827044333.88596-17-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It is important that console printing threads are scheduled shortly after a printk call and with generous runtime budgets. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 6 ++++++ kernel/printk/printk.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index d3a018919046..32a383c4f62e 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1312,6 +1312,12 @@ bool nbcon_kthread_create(struct console *con) =20 con->kthread =3D kt; =20 + /* + * It is important that console printing threads are scheduled + * shortly after a printk call and with generous runtime budgets. + */ + sched_set_normal(con->kthread, -20); + return true; } =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 4220abe86296..ed4a3cb00095 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3554,6 +3554,12 @@ static bool legacy_kthread_create(void) =20 printk_legacy_kthread =3D kt; =20 + /* + * It is important that console printing threads are scheduled + * shortly after a printk call and with generous runtime budgets. + */ + sched_set_normal(printk_legacy_kthread, -20); + return true; } =20 --=20 2.39.2 From nobody Fri Dec 19 13:50:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9778415444E for ; Tue, 27 Aug 2024 04:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; cv=none; b=bZ5x9+BjYMW5fYvj+fdBpgcHhNPaOnfl1B/g1ysjJhcH+O+Pm1DsX3ylBFz1MG7FJO/Srlbtc8DWE60wQEFUh4oH0Y5MmupgIuPI7LP3oF6B76yEY1b+NJbe0sRW6ARlUGZ3xbFb7r4NXPEkqipqU+Yy17tgEM5MH8SdZEWJM2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724733824; c=relaxed/simple; bh=oE1v1DaJeW6+p8FukWR9NR+vLSM0VFF2u5IAJp1dFrA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IrURToXQ+5F120MikeyZy5l/J1d9Uz74D+jq31zLY6p9beIUYp45ykcJJdaFKMI3I+dmFmRxu+ik4GjSCHuKuhQWb1wB3DuNr6otV23mGHaHSpz00bnqnfxKF4BXabmveKoK2RTJ8aX1No2BQbzSYnkBVyDucH/4RXAGAC60e1k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GrCmUzjn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=CLxlcd/u; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GrCmUzjn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="CLxlcd/u" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1724733819; 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=i+fUqbH7N6o416MlatQIV7ilZsDNv7ET4pJ564xt/kA=; b=GrCmUzjnodUbtrPWy878U9T6adp6DQ56O9eGQNbh13JtQXBYW5KZ2OKpISEYNbl7xS7rEj 5dDd1qZHN94Y4TUe3LZTyPQ1pmFcjllafwjFKM5VYmjf9b7/muO/Cj2ExIJrgMVBdS2Qtz jeLtgFXsArUb2Ogleq8ZilEvVbwQrbfK2zisK2tTm0Klb/zpfnLnWKtS8sdjdDY4bw3EX7 bW7kpV7Ya/cAiwCjAgT50f0bkt0M3er/qGgoBlAaZUWPaDTrZ3t6Yo7NSrZmcUIH2gPwUx NJiKX40yLYOycyV2LXEA57Nhh9U3goIxXrwSNDwfLnQDF1Mo0/TDVzMCQmbdHg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1724733819; 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=i+fUqbH7N6o416MlatQIV7ilZsDNv7ET4pJ564xt/kA=; b=CLxlcd/uKvGfDWFILqnXG8F+M28oW/JGfszhsVriTLtnBuOpZuFBp5kl4YEVOFE4jy/69s WSlR0RNfKEO2L7BQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 17/17] printk: Avoid false positive lockdep report for legacy printing Date: Tue, 27 Aug 2024 06:49:33 +0206 Message-Id: <20240827044333.88596-18-john.ogness@linutronix.de> In-Reply-To: <20240827044333.88596-1-john.ogness@linutronix.de> References: <20240827044333.88596-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Legacy console printing from printk() caller context may invoke the console driver from atomic context. This leads to a lockdep splat because the console driver will acquire a sleeping lock and the caller may already hold a spinning lock. This is noticed by lockdep on !PREEMPT_RT configurations because it will lead to a problem on PREEMPT_RT. However, on PREEMPT_RT the printing path from atomic context is always avoided and the console driver is always invoked from a dedicated thread. Thus the lockdep splat on !PREEMPT_RT is a false positive. For !PREEMPT_RT override the lock-context before invoking the console driver to avoid the false positive. Do not override the lock-context for PREEMPT_RT in order to allow lockdep to catch any real locking context issues related to the write callback usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index ed4a3cb00095..8458f4a22419 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2982,6 +2982,34 @@ bool printk_get_next_message(struct printk_message *= pmsg, u64 seq, return true; } =20 +/* + * Legacy console printing from printk() caller context does not respect + * raw_spinlock/spinlock nesting. For !PREEMPT_RT the lockdep warning is a + * false positive. For PREEMPT_RT the false positive condition does not + * occur. + * + * This map is used to temporarily establish LD_WAIT_SLEEP context for the + * console write() callback when legacy printing to avoid false positive + * lockdep complaints, thus allowing lockdep to continue to function for + * real issues. + */ +#ifdef CONFIG_PREEMPT_RT +static inline void printk_legacy_allow_spinlock_enter(void) { } +static inline void printk_legacy_allow_spinlock_exit(void) { } +#else +static DEFINE_WAIT_OVERRIDE_MAP(printk_legacy_map, LD_WAIT_SLEEP); + +static inline void printk_legacy_allow_spinlock_enter(void) +{ + lock_map_acquire_try(&printk_legacy_map); +} + +static inline void printk_legacy_allow_spinlock_exit(void) +{ + lock_map_release(&printk_legacy_map); +} +#endif /* CONFIG_PREEMPT_RT */ + /* * Used as the printk buffers for non-panic, serialized console printing. * This is for legacy (!CON_NBCON) as well as all boot (CON_BOOT) consoles. @@ -3031,31 +3059,46 @@ static bool console_emit_next_record(struct console= *con, bool *handover, int co con->dropped =3D 0; } =20 - /* - * While actively printing out messages, if another printk() - * were to occur on another CPU, it may wait for this one to - * finish. This task can not be preempted if there is a - * waiter waiting to take over. - * - * Interrupts are disabled because the hand over to a waiter - * must not be interrupted until the hand over is completed - * (@console_waiter is cleared). - */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); + /* Write everything out to the hardware. */ =20 - /* Do not trace print latency. */ - stop_critical_timings(); + if (force_legacy_kthread() && !panic_in_progress()) { + /* + * With forced threading this function is in a task context + * (either legacy kthread or get_init_console_seq()). There + * is no need for concern about printk reentrance, handovers, + * or lockdep complaints. + */ =20 - /* Write everything out to the hardware. */ - con->write(con, outbuf, pmsg.outbuf_len); + con->write(con, outbuf, pmsg.outbuf_len); + con->seq =3D pmsg.seq + 1; + } else { + /* + * While actively printing out messages, if another printk() + * were to occur on another CPU, it may wait for this one to + * finish. This task can not be preempted if there is a + * waiter waiting to take over. + * + * Interrupts are disabled because the hand over to a waiter + * must not be interrupted until the hand over is completed + * (@console_waiter is cleared). + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); =20 - start_critical_timings(); + /* Do not trace print latency. */ + stop_critical_timings(); =20 - con->seq =3D pmsg.seq + 1; + printk_legacy_allow_spinlock_enter(); + con->write(con, outbuf, pmsg.outbuf_len); + printk_legacy_allow_spinlock_exit(); =20 - *handover =3D console_lock_spinning_disable_and_check(cookie); - printk_safe_exit_irqrestore(flags); + start_critical_timings(); + + con->seq =3D pmsg.seq + 1; + + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + } skip: return true; } --=20 2.39.2