From nobody Fri Dec 19 07:24:15 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 7EC621B5811 for ; Fri, 30 Aug 2024 15:29:24 +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=1725031766; cv=none; b=kHeFLllKh71/+P3qGh/tpNwlBfXQrt3lK4zGuLF0HuFt92FP36IhbMGjQGSHM6NyIVWy6DpQxVR2NoO9SKV1V0emjMAtSounuQgO4WS8vwpS305WJv5Lsn2wxPmSL6p1OUI8V9jAXrdV8XqPKBAoL+BykVg0xtSfuUymyej6Vpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031766; c=relaxed/simple; bh=Z2FzLOhrYPb4SwVt3LRiu+LCw82+Xjl4A5u2HlGOY7g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a5BbUeAjg/WwOr4/JahD2pwtQUtWUufVinTGzEkPz+W9Kqk2z+wpkwIUwtkkClEchZjPzYIcYFuJIxzWNWjfSJBQAbSzGP3Fc8awdJEmVIcXladUHa9/MxEvjVih76XGLjrAJCcI7gU9x5RO1bj3wUkSKVpqEoq/qYlXg0VKl+4= 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=Gp37x8Nm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WO1vmUGW; 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="Gp37x8Nm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WO1vmUGW" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031757; 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=Qs4boc16pkWZxtexxvPfNeVBSJ9Cag6SQbs3NPVnzWA=; b=Gp37x8NmtpOR5JxSiHY2bVA2DuFda/MLUxkVy5yGX+pcAd1vcPpv8pL6m8FEj3b0QptkhE LV1h3auXVnm3OsB33EluXT9qws6xwZWViZSGehEo9dpUB7ritEthveqt2Vx8mqHDC2IcF2 +XAMFKAnpGfCde3I+3YhKne71Ql3yCdl0QFjefKqRIyatjJkIMjvxxMroaSe06Q97K3Ind o8xhs/SIeaJNEtSOqSbstobMwSrGqcu1UuiCeCWGaXLUW4P5t5Mwze519eyGB4Xtpr0M0z Ib5PFHDtIS3gcmeduUuQYcE5us+eMYg4LjwZyJ9S+pIiFqJ/4Fpk6IDESm7FWQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031757; 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=Qs4boc16pkWZxtexxvPfNeVBSJ9Cag6SQbs3NPVnzWA=; b=WO1vmUGW2q6ICk0vvOsZ96YSVscllFHlVGDAUoGFkFHgF2cHHyjKDLYokQX1deze+03Rr2 q1sJ8M3UtRFJiYDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 01/17] printk: nbcon: Add function for printers to reacquire ownership Date: Fri, 30 Aug 2024 17:35:00 +0206 Message-Id: <20240830152916.10136-2-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 7EC0F1B5332 for ; Fri, 30 Aug 2024 15:29:24 +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=1725031766; cv=none; b=S1Dnfjo3GhWx0onz3dVcwh6k0H78Qc6w9+jL2bnDdqKdnxKco+Zz/3JiCy5J0BC2lXSp+mLmR+HHgO2Tndq501Opz0Pb4+Ym1ThWa+al+O3WOmICOCaRI9vl44q65pNRbtUFxmo2BtCkxOJWKeAeDLVoMnvjOKGjuW6UhIe+AQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031766; c=relaxed/simple; bh=0PgZmc33n0ImzzEdrZmJFd94ixrtSDUtzS9LhgYwm5I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Eko5e6ZA+QDcJJCzKBf9GhrjmI4tiwBcmsc/huC0e61iHNFAlEPrcxtACeOVJMPEh6MCFpCd0aROupoDXP4oTc1W0zjZ+kDqlwIgETDleFi605JH9HSc572IHF71125yyFuoBjGZ62jexd9XVjyqJb5EXUQGi+sq4dFsEDpf/yE= 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=MzkyKVuQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=vi7kaS5h; 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="MzkyKVuQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="vi7kaS5h" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031757; 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=ADYc8AoJaMh9wjYacblmb7q1jgHolA4/JLzRo7pYcCE=; b=MzkyKVuQ/84+Emgq/8H7/q3w6GVbKm+/bIuoWTuBc+3ZOWz/LAR0dlrAkMsXlKQyerA+fr 6633Z94Kg+4Ed+efnnuCxoOl41n2fMQM5HslXbm/u+4YbFixEhLFnVUR/pWKa4zY/tEkcf HJb1NJR3qQqcR0h2Kq2v9byQVOtlihUNJAdCgUQR9YUivlYDbMA9qIwQuva6wJy5CazyV/ o2TWUdo6YqnDZy7vnU3J5IFe/EIAFBHla07eMstW8REOyqOEQYhIxGjX7SE50O7wZkOrTj 2YL+za8RCFp9WIglKadC040BzXzLVLbPuQm6X2tt9uJyxSRlM4hWdbgHV8/lGA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031757; 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=ADYc8AoJaMh9wjYacblmb7q1jgHolA4/JLzRo7pYcCE=; b=vi7kaS5h11YwwO7gWRhK2hOswQrLfVfNqfjvde2EXCIW3Te1/+a5SS1OcF/GrYZJDTpgnh Pqc8ofbebarAoqAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 02/17] printk: Fail pr_flush() if before SYSTEM_SCHEDULING Date: Fri, 30 Aug 2024 17:35:01 +0206 Message-Id: <20240830152916.10136-3-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 4465E1B530E for ; Fri, 30 Aug 2024 15:29:25 +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=1725031766; cv=none; b=G2sM4Ngr9HxGuPwmAxTxCxsqhn0imrod29FtA03SW3+jZ7z099dHZQnBNpfZ5jdhV3Ls3O26lIeM545+prVKpa1kO0ZXiQI37DL1SeSQus0KYMEZJ5H0qJvusbISNYpKJE7h/l+eQJ8cd6TQmXvghabc6Hpd9BqnmFzstYMqvUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031766; c=relaxed/simple; bh=hO2oomLoxf/bUX1WgRwfUv7yEovKt+lixG0W+vCa7/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fsiw+zrXUi7N7mIkF8y+u1UrKJMLHVOJgeox5XyoWbd3ae2aZ3FW995qdUI+IdtXI/std05wcfTUvE4vROPKBIr1e2TbA7z7mekMoBjT+rnXtmo0K52fqaD29/eJIFuiHFqLukIfjiep0+EWx5piiPUalOqNXRJ2ciHK5IXvHTk= 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=hKDbQ038; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Fnvtt2md; 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="hKDbQ038"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Fnvtt2md" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031757; 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=hKDbQ038rtQMA1MXO8/bSU5ehFwp36irJdrpevbNNMnlzO/oJH4XS7P4wEHVbUXPdQRjgx Cc0J0QTz1YRcqpg1XR7v+7QWIr3zUsvZPs8p6VZ3LEdjmP4bjgoLBCGxhtZcPHXWciAwjD Y81T2jnMxNVkJQuE9VNCxFUBqAtwUlStHpZGRgVdGsE/uCAXpQKzgsStqrZFQSsXLEb29k iQaxMgwnOlLjh4fE4+/4y2eGLvJN1zonA/SKKUBz+HQ6BYI31ikMX374sp3u723bWhvV// ni/heZrk3bZzF7azpIML2jjGpvTNHmk8I69dYPJT3dCH9OrA+MNnjU1EwcKDUQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031757; 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=Fnvtt2mdCesnnxOjBO5o3L/b/geUE3V/4c0FjtkM9AXX/EfftTA4rzsGzpHOexfE/maPu4 xpVZdGzhzK0YqbAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 03/17] printk: Flush console on unregister_console() Date: Fri, 30 Aug 2024 17:35:02 +0206 Message-Id: <20240830152916.10136-4-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 446D11B5813 for ; Fri, 30 Aug 2024 15:29:25 +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=1725031767; cv=none; b=JXkYt7JQX0KS+qfNoQ1mZcZRXy/rqF647s4ZltlNx5hsTroY/weW7bOsN/3afUcKOhnTrXLZcIoROXdT1Q+4AhPyMVjIegTIgE77OntnOaFMWb/bWkViNgdRutz6LD3uMqVrvInmzojIoYHYvA6V4i/+6lCXmk+4B4ZxzORneEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031767; c=relaxed/simple; bh=UJgrLHlycgkl+eOz4V6dwXdgZCADGYZk9llQVSW42sA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n5F7ZxFRaVytmDa+LCtTb3WX6GP6K+EJnxd5ZqCJsF3UXVVy4cLRLunAZ8oRRhFq7Rzs1LhJOGgFDfSujerDpd1YEQGB3Ps5vVxCdfk8EtgxIxYpPT0lTs/qO/bEiE8b8usoQb1kqHztdvmkFEkShsexSPsxEo1H7w6naaqv2YM= 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=CQLMCHLJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=4RhvYWLm; 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="CQLMCHLJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4RhvYWLm" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031758; 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=Rw16BInzN1vDxjLbXWE8q8eJXoJehJwW3paqHgsbGDc=; b=CQLMCHLJ5RESnKpoBWZo/0KSXpV4bubK9o5HmhwNxS+PscQHnFrUUmuKWKS3zzomikaiBu 3QTIXR9dxh5tE1OUh1wLMfvIsfCbnjGM+PZvfIwZok15EW1zREuIw9/rOGVBxXty3sKDJA 3AAeu/k2+vEg29GWpSnaXG9TdBk9cdZFPDWQGPyqGGoAZou32Ivw4eLYU/EkoHHTh/XDmX 5ynJwXcTjcsppHs4sFlYqpGqhJjTtd1rL+LJ3bg7ZWfTxOM+qDygFhc3YZ1l/VzCIJPiDq cn70Z5pi9/NIHrIe+58bCo3wEDtXZEG4MRVv6vXfL8mw6La9EYXYJrnsdaVfcQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031758; 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=Rw16BInzN1vDxjLbXWE8q8eJXoJehJwW3paqHgsbGDc=; b=4RhvYWLmRakILdav0i5/PD5NKSzVMisrJYPz/oIIerC8JapPMjo/XRcg9a/BTqe4qGxyA2 sGaalTPPA/SrwzAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 04/17] printk: nbcon: Add context to usable() and emit() Date: Fri, 30 Aug 2024 17:35:03 +0206 Message-Id: <20240830152916.10136-5-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 DEE911B81A9 for ; Fri, 30 Aug 2024 15:29:27 +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=1725031769; cv=none; b=MwlM/KBTFsW3iYQ6CdUMwMIFqLTQvfVCkGNKQNHJIKLXfCXzZnv12pwCXc2DVIZXVXBlJADTp1alNrH6vv8XrZ7Lu4/bCvWSb2zbssRcDxqefAqH0rBs7MoZrqohY9WEHyROKY0ymvbjYig5fOntBWzzvAgGafA07WTPQi73JC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031769; c=relaxed/simple; bh=ruzArZlZHwTfGS9fsg/jLKUMRDaHRxe43i2xOb5V7ZU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KEz6uTkjyC2KjtuFX0TH2kjadGZSdyeme3lqfhP9dhrc06T4bv/etSx/t1spIHdRT6492gdZ5snxsN02tiW55iKGDT5JOzJQ6cuaFrNKhSlO+rh/UnXFdwr74QNfsAfdEd4VQ1CsRZsdNuQSLFXEb/kPbr3TZFuTHpaZIh9pFpE= 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=NRXfbyMF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W8DlDLpq; 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="NRXfbyMF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W8DlDLpq" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031758; 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=Zqmn2pFt1/sOwz2b2uJyqHM05n1+rI1nWkzMoev/7nk=; b=NRXfbyMFcAHLAsk/Crwn9iT4+ZuOFuAEAdNYZG6hRGI9Ejm3P0W3UhUL4HsBNWPZsbP/Kx U0GMMlTy1wFHg+nezMhwOSVuhxH6Un1oMMG3YU5R2K2CAOPyqygvkEIQezstCdcW51WJlD +Gv4R3YKWwwATQqbXuHSuMc2T/yHAwrazPNMNbQsM48qBmet2QSSUzBuOJHedE2Xi80Ylf vgbechKS+mvq1HLzlun2QZG/PBCFMJ/OM4CfL0qr3xop9QJiAhW5RIPVlpNoQ51C0vqj54 SpRHVv3k/c8wM1ZGSkNJIlrEOr9rZI9SrCYfRUkB2gImZGN4r8WOrfGSkiAwTw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031758; 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=Zqmn2pFt1/sOwz2b2uJyqHM05n1+rI1nWkzMoev/7nk=; b=W8DlDLpqgqVtlOwtMVz1BIYuO+CC2yGAQeRthBuT0BXACZgrUjBWEj1DVEckjldDx20G0e VcsTS6CwxOqK8TCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 05/17] printk: nbcon: Init @nbcon_seq to highest possible Date: Fri, 30 Aug 2024 17:35:04 +0206 Message-Id: <20240830152916.10136-6-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 DEEFF1B81AB for ; Fri, 30 Aug 2024 15:29:27 +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=1725031772; cv=none; b=WepRPd+TvF/1x3N4FSaO8dYc4QDgHdDImYcvEupBlGy01sVwaw1/Gwx+engiZNFUWRhCaPX9XN5KKsWd2og7KhNzXHpYVR+jj9Hhsv06rTEWFfZD212UNteHe3ukgiLkBNn1qer73G2GDC5FGJlwykS1LxcBQaRLKbKckEFaBRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031772; c=relaxed/simple; bh=MUXXHqeSw/if3YGOnUF7LDHURP8E8HZif8/G1eTpcDQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WIQtZIy0apX5/Mr4FL2O5/zSDtFAcbna5ZkniDgH7TTKU86wN0UGrgxgfn2fCLmuiUwDm00gjdYb9gYxrv/COYjJc5VGC7NDYSHylJ5zLDyUFlKYWo1bR013iPYmQYqGevCEsYCLlsI6Xa+senrz+McFetzqMq9nB0BFiSdnipU= 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=1CAlUXso; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=n43H3apb; 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="1CAlUXso"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="n43H3apb" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031758; 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=vt0/m9hdus7DkUDvCVbUgmALBo9TahEOOxTknb6Xm5I=; b=1CAlUXsoSbyvM9BngiW1+lv9/gm8Um+gdHD1OxPS6Fo1v2HRUwsyFZt8SB5eoXfLLB630B njV2Vg1MEBpWXo0IrPFUNf3T/UBSJXkljbxMpI40NUEzBn7vKKbUkkgv24iDFwW0FBdm37 fs3XFy7vhcIqZKSPPsjXhQVKLloJYBDeEZq5RinrA+rYdMsix6a1om/XhXstt9ZxK5WDvZ Z55BDoHOfZTdsAxCX+8Mgs31Z4smPCObNpoEtZSlUzWBDf/7VmxfGAyG+hBZbJHNjnZyS+ ulGKrIQ1VYmyjMAm3ngr1vidrZIxCo29G4mgi5uajVR8m3bcjq/ePjWsED7p9w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031758; 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=vt0/m9hdus7DkUDvCVbUgmALBo9TahEOOxTknb6Xm5I=; b=n43H3apbHgkB4ppHpki4bHqbvgUvPG/RftYrFRQRUSBGCOF1h0cbcXqymjpQibFwM4geyp +ya2jqlRFErPbuBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 06/17] printk: nbcon: Introduce printer kthreads Date: Fri, 30 Aug 2024 17:35:05 +0206 Message-Id: <20240830152916.10136-7-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 | 242 +++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 108 +++++++++++++++++ 4 files changed, 417 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..40d00e92494c 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,225 @@ 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. + * + * This function is called when it will be expected that nbcon consoles are + * flushed using the kthread. The messages printed with NBCON_PRIO_NORMAL + * will be no longer flushed by the legacy loop. This is why failure must + * be fatal for console registration. + * + * If @con was already registered and this function fails, @con 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 +1622,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 +1650,14 @@ 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); + con->pbufs =3D NULL; + return false; + } + } } =20 return true; @@ -1432,6 +1671,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 07:24:15 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 9C1511B790D for ; Fri, 30 Aug 2024 15:29:27 +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=1725031769; cv=none; b=quXb6+y/9zmv5QE/3Oh/Ucl4VDRGoZwDVd4CfEZxsI71ln0MPJJyz1kI6byNiP9MdaSavdJsSX/CkSVQL3GBdO/tvOIljy/4MSQ+NUJpf108zFVoBeJwSjKm2nMH0ghxSPC+wZbK8yadOmkI/OXWQ4axm7yaeli3Z3f/VlONcoQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031769; c=relaxed/simple; bh=febIYAM5vIRVW3oul4SfxzgPr5QxGQCshZCHHjI8lGU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S7CN6bdQeVVHxUZIwaFTl7FbwXCY+2K1Te4tFCGxIDZUOCu5pKJ6QkRkihHmuBSLLNPcrkTfzAESMQMV0Y+4wvVcs7V0WgLvTp+5LeNflk/oO0gsyf72diAXdgroZLlO3zpu640vyKqiEhbRB6ePTZGDSgXG8TBK3aOW/xv/WL0= 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=m+tKLmij; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nVf41HDp; 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="m+tKLmij"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nVf41HDp" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031759; 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=SW2mDi81ohzukBzY1YP+ImgLBDt2qqho9NcptWHgjCk=; b=m+tKLmijpoktM0RPwF4ktmdHfIFsCG9q7GypDT/l10CVOUzzyz734YYF4RzpseaHYsG5Xy sCgVU5BoR/N+6BDm5Iu9kZlDNFHHJiqCfXL3KKZF3fPlq8aVs8IwaNcTQiMsuJJpu0ucAr OCm4mkMFMVBRenb+aqwOwTHlo/h5+ziyQFl/4dIQvhOshdoxRpHgC0hzm68/c8OCFucCmS vezQUMBU1G5ZgMzKoMRn+ha90xPcp2SOZfbPdQgsyK9kofLNrfrHJYUBcv8uI71j4oiKOa Q0328h7qyOGFU0ZSu+W+6NW1ZZUdoP/SEoxmJRd1WqbZoZwzYSw0HIeJX84Wvg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031759; 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=SW2mDi81ohzukBzY1YP+ImgLBDt2qqho9NcptWHgjCk=; b=nVf41HDpfytlSC6TPLBwHTAs7gQtjf9Q3/+CGypsqZQ9gGt4mVxMv8vAXvrM0nd+rGGz2w YnPsoqJuwJev7zCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 07/17] printk: nbcon: Relocate nbcon_atomic_emit_one() Date: Fri, 30 Aug 2024 17:35:06 +0206 Message-Id: <20240830152916.10136-8-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 40d00e92494c..807ed6221022 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 @@ -1317,45 +1356,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 07:24:15 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 9C1C71B790F for ; Fri, 30 Aug 2024 15:29:27 +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=1725031770; cv=none; b=sKbS1p29/MMiRQ6HoPwBJyzI7kccUbeCUHPx7N/WeM+EJ/zIb5grIPkKM3KQDtLDa06lljf/9mgKoNCVjW4X7dKh1EsjLQNpxgU5nmPfcaSIZSgTEcQCXhIIcdAZb25yghgJPpsGQVA/hfn14f6sb8BeIn7Rra/m9Qo2luTGP1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031770; c=relaxed/simple; bh=5OmesKfkjUh9igNDm9EIfjCoKBBQ3g+OXl7GNRY3SqE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Z6g5jahaxWNw0Y9xTuG8RlHjon2smmRRMQ3TY4B0FSOWjkl7U7AVPsagzfB6bHNJVPFCNotRsGrX3hnajIQR1dQ3FXgQo6uGreVGw1KvU9ABAmOO3GNC+LD1i4FLHEldLVs3K670hjDDfUlSMqGb9QFMtBnxTmMxy1IU4wpce58= 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=t/6F82Ji; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=t9tdAdg7; 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="t/6F82Ji"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="t9tdAdg7" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031759; 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=7dVJ7Wn1ziU2Nl1CWxDAIRRz+s2H1HE/aDeJc7wSUhI=; b=t/6F82JiAHVCjncl1Sn/rsQ6RM7ZiAfisXyjn85GRTr23+Snxp8A0q+eyT1PLFUPUu12mZ LfuxlfKihksnzyHcyvMI6LLF9lXsVf+vo9Q9dsd7O/C2nUrKqGOxo4KJSxRIAsYzQKQlYj o54L9IeEWdzX+pR2B2GskCxFjBt2OLMLx1W0FvRzt25aGYIwOZbdMLcgYWMuUJT03Y6x7f uQpZ63xBJZ+ZfUUzlkyMHWVA2NYWfQwzmf55XO1PeY0DxwxMYJqIdlqCiSDgmKvqdAvwoO KtnqeTj1wTSyDFJsf1rqt/Mbkad/XeX7EPZ1mi7qcqvRdP3+A2bZAclE8OZfMw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031759; 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=7dVJ7Wn1ziU2Nl1CWxDAIRRz+s2H1HE/aDeJc7wSUhI=; b=t9tdAdg70m73d2noh145REfM3/m2HFOSjuHU1UQJMMYB3m68D82e7eLqf2X9XakflrGX3+ Z6nC5lo8MXlqv8BA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 08/17] printk: nbcon: Use thread callback if in task context for legacy Date: Fri, 30 Aug 2024 17:35:07 +0206 Message-Id: <20240830152916.10136-9-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 807ed6221022..8745fffbfbb0 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 @@ -1365,6 +1361,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 @@ -1380,26 +1383,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 07:24:15 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 23DA71B5EB9 for ; Fri, 30 Aug 2024 15:29:27 +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=1725031770; cv=none; b=EPNFDF0ulcCoGIGGKTeDTa3ECYBoSwV6nwT2lMCXYy0s5T28qe952cpOuLB68oI6C439g5nGFfaGfLuCiWL3jVGDkZOhPJfhNgEEm+3e7QSrvlfkzo98YFhKgB9tJN6//38s8xv1e52MNTQLzd3J5SHJy72CL1thxadFei3Khb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031770; c=relaxed/simple; bh=SDTh/5f6K/T1YNyTMfofCY2NZ0cpcZYH4JqrtxcaTjk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=C+YcnYTGaQy0RpZ0t9228w5WBSMDm+3/1b8vgqecNtF+/KUbl5WpLykUnfaY/Cu6MkmvBvt+XWZW/GmKk4owyhBWQ0DtUODwHZuk3vpeSyPLF3KZY+z/PGKSKW7EMB2RM5wjmdm3VVXG6c3LAmCHD3NNssZEelcCB0aCNFod0nY= 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=D/0ApaWg; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2SK9c0J6; 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="D/0ApaWg"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2SK9c0J6" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031760; 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=tnnub2JBKw4ncZI/uPE2HgPxoXFMsSDgUiwh6Mxy6Fk=; b=D/0ApaWgcRObZKgDFGIesxgWTSVsRfo+2mUkqiDWsoeMLWQiNjpgPQsXPNUhzZzr3u6JgL LY1uAfVnZyvOFAoSi2hCzcj5dDu0KxCQ57yIns20A3VJVhs7WLVoeDpYcRQoeRMSgWCqXZ jei2dQYhMEPK4gTgbhcCsXT064ZYpqYEV68vZKU1SEhjiOPDTh2HDKQ2jzqvazbnSR/sP0 7FV9Sjd/TKppqJsXXD3xftGO83TgbSsVmQuO0nDs2nOdYobLoSqWxyqUFZrS+gWoJydJa/ odDFYQNEZO3YS9RA7a06LFA8PBwEqTZFAoU++N1jrR/U1RFB1/ZG/LBH8W2a9g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031760; 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=tnnub2JBKw4ncZI/uPE2HgPxoXFMsSDgUiwh6Mxy6Fk=; b=2SK9c0J6hM5l8hKBtY6wdCCGDK51nDE6isNbsRXZ1b/a4nsQYQPKlYaRuGSkjebAzfjBJ1 cQ4YtIXxPTAfgUCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 09/17] printk: nbcon: Rely on kthreads for normal operation Date: Fri, 30 Aug 2024 17:35:08 +0206 Message-Id: <20240830152916.10136-10-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 | 47 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 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 8745fffbfbb0..cebdb9936609 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1492,6 +1492,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 @@ -1521,10 +1522,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; } @@ -1752,17 +1755,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..b9378636188e 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); @@ -4115,6 +4149,8 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre =20 /* Flush the consoles so that records up to @seq are printed. */ printk_get_console_flush_type(&ft); + if (ft.nbcon_atomic) + nbcon_atomic_flush_pending(); if (ft.legacy_direct) { console_lock(); console_unlock(); @@ -4152,8 +4188,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 +4667,15 @@ 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_atomic) + nbcon_atomic_flush_pending(); + 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 07:24:15 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 C12001B5EB4 for ; Fri, 30 Aug 2024 15:29:27 +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=1725031769; cv=none; b=LWUR1sD0WfVdlRc1FBBtXl7d9lyfOMmxCVBf8iyjCMUgQiaS1sxjs5SkQSBetc0y+woU/FjdiAMGVRRjB4N2dl/s2lSj02HAspznOghbvGxK5+h64ua8g3UNq/SS76RI/jjQiyBeb3Ort6NCFHouSZQ/gTjge0gfHP4fIGrfIUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031769; c=relaxed/simple; bh=uxefz0lFBZn8bfR+v64SgJv36gK2bXfpv4eZUpX70h4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fRUszXQo/BjNN2wK1FzcBTNToTMobOKAYAGGeFiDjM4vO+u+qUtgTsw9UEnGr8WJe5vE1hZ+KNHu8y8065QXnQKAp3PUtdbj+WPFm9ZAw8cvwAVYuU3/h+DIGEaBXqNGXcv88TAi/a4NgGgxym9EBrKu7qORw3kDaKQyjMN5nsI= 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=kMCW+jqk; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=m1FVKtSJ; 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="kMCW+jqk"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="m1FVKtSJ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031760; 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=Aar3kyXyxhY3P8EcLLnuSG7SHnPmwjk2CsjtEu1FQHs=; b=kMCW+jqk+Bfu7HGOjkapur36cVg7OB24kdbkJyOSAwPuNQqYKBTZHP9t40T5yGZZR2qLr4 oTv+k+Euw+5aZUjQJiKz2QoXIcplxaT63BoCb7C1D4fyEnIrljRAgaAcDADJNvdudcSezy 6hss659Xd/eW4xnUDx4o6JiNP0fP/XvD/WWDGuP0Ch1+/QIURSpkNz4AlqnRrLx+KQnZp2 pdZoG6SF6nki5EfHnCo6+SO1xoKMeDtOj54p8xvKmfNKMMMsxDpGL7XZxmK3+INQnhSjmp LGLSefp5c+7X81ZKNb5YH1CGHGyGy7ipLWBt0TLcNNgc3zZL+6aHmVsOX+wQDQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031760; 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=Aar3kyXyxhY3P8EcLLnuSG7SHnPmwjk2CsjtEu1FQHs=; b=m1FVKtSJ3LhJrdxBrxa2ksM+7pUlpqxyrQMIJKTMRkRl5LoHC7yyV4KrKKS/ss+TQJpFC8 c3plcFpycAoMdMCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 10/17] printk: Provide helper for message prepending Date: Fri, 30 Aug 2024 17:35:09 +0206 Message-Id: <20240830152916.10136-11-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 b9378636188e..ca1dcf892891 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 07:24:15 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 7F54B1B81BB for ; Fri, 30 Aug 2024 15:29:28 +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=1725031770; cv=none; b=FB6BmOb+NeLymYzoGDZqV3PfAPETi2V8V7mBtVbVtMh/rGt3LY96m3J68OPBo4hIwoaAOFw1abKxmfIMMNFgbB2AuLeaxveRObDfFnVzWDEb6bffQAijCbaUH3H6/jEFw/vVCK+Rp8OeUxyPdHQddk4G8+PNr8HF9G5+dxg7KvE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031770; c=relaxed/simple; bh=WWLl7W7RJS5PD0n5RzquHX5iRUNP1C1q2six4zy5MMo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bj8tEu32VrFJ3Y1IWDBLokNcqmL0m9D0xYdzgX0bmtKR3JjgmKl4QdHYUDrSAWvwCfhnygisDWqdV/kkcEyQZQ6ir7FDH/qCoqTJ3RlDncFvyGWaEVlXBDTzzkK/+hmy6hYVJ+LVK3M6gY7e3hx5fNH67qgAngCcFLBxFNncxQE= 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=qvznOjzH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=y+j+zUiR; 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="qvznOjzH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="y+j+zUiR" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031760; 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=TkX0ba6Yp2s5JTrjuw05KSAjqziIKTQfvmdp4nIcFVY=; b=qvznOjzH1l9WkY0IJgE49N8EPVfpnzuAq0BstJsGw+6h6Iu+5BZTWcuinxaIMljCuIoJDu 6irBGRhRKb8byJwdAn0sxFD6E+tV7TcO6XJ4EX2oxjTC/zPFL0hS1YbWNaKdpsXPY7RvTs z9CE4/XCRyQmxEIzWCFDUE67B0exaR4EniJc+sJ0ZVL0KvLwr9yWwlY3seYFiHzrlGXPRu AIdWRNVDPsmTMSD1IXdT8wWvWdkaVBLad2CwMFOMwwDnbQKsfKesG8re4XmwF5dl2np8q1 qwj+xZYdxohQ/kQ77goiiOMMm3jlrw1xsDBgvexkm9UqggWANH/MvMevlYhn2g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031760; 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=TkX0ba6Yp2s5JTrjuw05KSAjqziIKTQfvmdp4nIcFVY=; b=y+j+zUiRRuGUfgY8BmS55Mdl7KSQIDHoN+OHikpOhmdPPUi9+SF9A8xURp7PUDOChnhAGD 4/iSBWnXTSJsooCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 11/17] printk: nbcon: Show replay message on takeover Date: Fri, 30 Aug 2024 17:35:10 +0206 Message-Id: <20240830152916.10136-12-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 cebdb9936609..1559b5dc3075 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 @@ -1644,6 +1669,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 ca1dcf892891..f43cb87a7fb1 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 07:24:15 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 90A061B81C8; Fri, 30 Aug 2024 15:29:28 +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=1725031770; cv=none; b=G1fyfgKS8E3LE410sKqoSz5qnfoJJUV5mmDimR+TO7bKtgwsWVOUDI7t43w2WEyFfi4HYebgi+7UU7/i7hi2ixH8x8exI2uYK7fepmwT7NUiN5UJ8RERozd1+rGZAx268erXQqHZy+5+gXZX5HiSw0Of8qaGLm5r3ATIx8pCx4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031770; c=relaxed/simple; bh=z7t14psmClgJPnrTS3FePajFlnKKHKwtWJNjr7MCP1s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=beLn/asMseaeZYeWuM1aCSFaDSWE5e2n81nZC+j8OXiwcIqDPm+5/OGWngaf6hHH9Y5rrMOlnSl3DK+trMTeboOvId51KOBCwfQmdfFa1sIVMfkXpzlkn1rOexmcyL2kozTfepW/7qtAofFHs+cLTtxZW7J8drzugyKu0W8teEs= 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=AhPQ4/Yb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bXjWNwX2; 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="AhPQ4/Yb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bXjWNwX2" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031761; 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=AhPQ4/YbnRSYX4pxChB3fh7UI8eXL78WxqmtKljMc+kv4/bbX33gtmG2vIasuPf8+x/PGl Cb+9D2pG809ZcAuSxEhm4tM90KlakS+IQ5DDBXRBEUJPP83CExlUctayfMxjibudw/aRoy V+oVEVfZRex1E4M3GmjsyLnbKKQNykyIfAGdCE7yXdZorQ0QbEY4f0/7xmp8adEjkYxntN hrH29QXqTmLaJOyTvhOIKeeLjO1mAn2Tem1nPXYdwYqzR90doRDfs8PXoqPOvqm8tW85Tz PHPdATj4tSKNUycP9jgzfP9NhQ7ANLH85UzIfqYHBBgTmg5VQ8ONLwUCJFIbSg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031761; 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=bXjWNwX2LfGnK3EeHs2nLrCjHY04PI6gjMaRBS/hleAuB6BPV//wGEMBaqQAgd2a+pez0O GzI546dbUlG/TmAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH printk v5 12/17] proc: consoles: Add notation to c_start/c_stop Date: Fri, 30 Aug 2024 17:35:11 +0206 Message-Id: <20240830152916.10136-13-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 B84611B86D9; Fri, 30 Aug 2024 15:29:28 +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=1725031770; cv=none; b=Fcl5BAjO1vVhrp57PADeTx+5EhAer6uaiL9w/WEyTykLlRLFeherc9Sc2uIvtT9Uih4b+9dbHQyMMwSvqvVDmgFwMrgFhxBgqWhJU17BzZnz4B9fTpveevZg0PGurWcelwSzzjizevliaKpGDwMwOKm22xtDybrgbKtpoUybNcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031770; c=relaxed/simple; bh=RuhFMyH0buaWaNTtg4qur9A9+lQbBJQXU2dz3dvH8mI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JMJ9nlvs+emYtyL0dzWzPd8xhWfFOi03xSmE0NGY06IWgVHg7s3u0c1YGkUpOKByP9jGx5oFxtO83E2kUDl+n9l0ItvIQOU1ViOwei6PPc0mCcu6ZLHuB7hqDJeWzWYUcd1aIfZvToEsxGSbHdSmdC1OAfDQRxi+IIA2xkF4Xis= 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=FchdMTmy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=c2WSGed7; 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="FchdMTmy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="c2WSGed7" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031761; 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=FchdMTmyntoPE7akPOtsgrfdHuBqrDJVN0oI+pRsJok/wa/pBzXOwGm3B+PC/OACeYjw+U f4NBDeH3pXYkZCNjDJWnU1VImFI4OjMrtEE8aMF62I1DeNgt6WtwWiZj3XOt7oJalrLVRM UWu9BbWLKeFuL28bjtemoRZlChrDFsHM3UfrQ0HPJ1m+V3IGLKPOYXnXfD+CYcUkzyUJn7 VJuaegyjVRwobtkyou4Zuy568u+GwLDaQNRrKoEY1eGXiWDl4BXBekDgw2R+8OB3ezQSDt 0OD3uUq/NlViMbkwe4X5mWyrYgcVeI8yrbom01ZhJ5zy3OcehjhkPkfCyrrY0Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031761; 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=c2WSGed7ScXWBb2EWRODhDJNAc7E/zEgqm/ph1ozdffPcqNpXrD+CovpdM3jjaZWTKTRnN NE7961cuiQrnveBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH printk v5 13/17] proc: Add nbcon support for /proc/consoles Date: Fri, 30 Aug 2024 17:35:12 +0206 Message-Id: <20240830152916.10136-14-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 BA1FA1B86DB; Fri, 30 Aug 2024 15:29:28 +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=1725031771; cv=none; b=hfHcjr4GtHw+L4WGJqUjNYmA0PnCeF2U4aZdqDx0EzrqSMMm5vWCIUgkT36iqkvdpDt03u0YA6XU39NG8FXd5cfEfJRmr6U7oFuxSS2n2sJosNbd24xKr30E53l8LkbpU/gUmKl7T1UUSzseusjmajhTPosNF69aaRKTotneP+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031771; c=relaxed/simple; bh=dp39GBW9ZErJu1DqQO7+qMNtwGg+EqlSaBhaAISNtsU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ub/yCdqzxQMwgxseTzOZXAd3oLvm1kASUQAzf1SjX/JcN7hDEEcW16ondbeoOvvimTxbGu/bSgjwyVuJ4ramCfCd+gPs+szuWQ+7XNILp2zhoKf0Rcv0iqR1Lj4lwnIbhhSx9NrDet2aQY4Xk3QDGZtYV9/nwLf+z2QAb/QvP4s= 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=C9dZHlPx; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=sVXpqmL5; 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="C9dZHlPx"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sVXpqmL5" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031761; 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=C9dZHlPxu+QbqZzAnS8/GuRaberIOWWy9iyajTeFJLV0bfCu7abkbUNVjCckG3j/YSdvKS uSUj+93it8AuVOnEWaSCSnDrQBptEI30YKkKbvGkJQ4bEqMqzdQ4XCnINKnOljQumsruLk qvBmmv47GkvJ5UsPtph6uyO4lTRODgp1LTC2lJtbXYehQ1+Z4taDCgfZb5bm7KrUKYCyEg XVeDqXsJAB1O6dNC3Wr1PhdQBFX479mxIUf1B1Fk9jSB+J5WcRt/zCPV7BxW/Sr2wttLGM /GiEBXOqCe3aQVglB8dBnFdlQabNkRZrUoYfUL/WICBiOCqTDKg44qryFdJzvg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031761; 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=sVXpqmL5PqNY5f9ydlM2jLIUMe5u5oZ8FgudnXjLT6khdLsgLq/Bc7Ep7Jwe0kzhySqpbG +BjuZDHgmsLtymCQ== 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 v5 14/17] tty: sysfs: Add nbcon support for 'active' Date: Fri, 30 Aug 2024 17:35:13 +0206 Message-Id: <20240830152916.10136-15-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 07:24:15 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 5EEC61BAEE9 for ; Fri, 30 Aug 2024 15:29:31 +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=1725031773; cv=none; b=g0faUCuPFD0bxapWnSmc5m1RxxWUNwY32HAURuRpsm5YSCZP6wiR4yF6hQ/iwJG81/MYwdk5nz2rb6SQVaUHiNOm3TgE8s5k6zyhBvPaL+ResbZx2x8WEB22dpWOj8/2DJ1I1/+bENpp8SndTbkhBIKte7Nbih0F2mqp8kTYkYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031773; c=relaxed/simple; bh=GJwpqL8oZySmLddjaRONC8PnggaG2Q1DpU4VpaoVps4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UFfU1TfISTHmWsNVfxKPr8ywpurBezrGTL8LcHXCn+KgYABqTCYAHQXMKBC4YxtvSIw5NXcTaDhaAWFnSA1T2ePt07fszucAVA/J1Hlviphsy2rbS/y4SAfo/OE6kwhC5fYEWenMQK1J7k/YFjW0GqhRnItCI3uM9QM29+oZflg= 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=jr3oDtX6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AzhGqqUp; 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="jr3oDtX6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AzhGqqUp" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031762; 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=w7LMz46ZyOLjNjPEBevrUlbTcLThpW9R4IOrEJIp2lg=; b=jr3oDtX6hyG2LyVmhYKdSZND5c+HmsMwRzEsfWXbQV7R5VG4/eU26Yv19Tj5xyOiwifOyb YkoRhqgDcL4Wn2ZzL7ZqcZlnqReyP2PnKcZpOH3jwu0tqSjqPjQEQbIDPwgs7P2mVCmPcL NFsK2nfEbT10C6w5d7MxJb16KhbDTrQ2nHm92BGtMp+32Wr0lUUVQhJR7gwd9PBbXbR8jX z4YpuyAwiUI+nVwO6w9i/0BqKMdV6Ifb1+qQ3ppxRGBob2HXOpmzErCKjJkPimv+TYHLiS lS97ryVkvcqTBNX16KG+LRVxdTqaXk8R4cmdOyK5NWToi/TkVD2mWPrUBW71Rw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031762; 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=w7LMz46ZyOLjNjPEBevrUlbTcLThpW9R4IOrEJIp2lg=; b=AzhGqqUpGFwzhqAC18jau+mQygptq4hhdbaxjJ9gTDGQioEVUGEom/3lu747sCmB6tc3Za j1FQehsW+bP8/bAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 15/17] printk: Implement legacy printer kthread for PREEMPT_RT Date: Fri, 30 Aug 2024 17:35:14 +0206 Message-Id: <20240830152916.10136-16-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 --- kernel/printk/internal.h | 16 +++- kernel/printk/printk.c | 156 ++++++++++++++++++++++++++++++++---- kernel/printk/printk_safe.c | 4 +- 3 files changed, 158 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 f43cb87a7fb1..1c6afb0ed4d2 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; @@ -2756,6 +2757,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 +3168,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 +3212,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 +3462,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 (ft.legacy_offload) + defer_console_output(); =20 __pr_flush(console, 1000, true); } @@ -3459,6 +3475,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 +3605,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 @@ -4284,9 +4402,13 @@ 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()) { + if (printk_legacy_kthread) + wake_up_interruptible(&legacy_wait); + } else { + if (console_trylock()) + console_unlock(); + } } =20 if (pending & PRINTK_PENDING_WAKEUP) @@ -4701,6 +4823,8 @@ void console_try_replay_all(void) nbcon_atomic_flush_pending(); 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 07:24:15 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 5BBDF1BAEE8 for ; Fri, 30 Aug 2024 15:29:31 +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=1725031773; cv=none; b=AUUafnBOrzo0J3h3W4AoefLi9hrgAEgSueLXrme8HAb62k3SfMj+7aGeqAfYTkpwByT+n1UVHpDmqBibSX4yyA7ypZF447YIyYfJ4VU2Q1F7W7X8Wgqjm0VOoecOQzuU+5UJtBg1iiERfaie3pyufnspPaETCs2B6nqGShgBMKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031773; c=relaxed/simple; bh=dMJ3G22DaEAmSBEb+VUjJdPyBO6c5j3VTVlMc7Ixp+8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OLgmD+sgLMAmFe7CnpLUGl46YjtCQciTSBO3XBNB4EcS60LKeA7HVyND+4n9idVnjGNVHT8mrlNxaA/IA1XaPuxQTXlVOI2Uaydj7NneHgZuXRFNTEwbaGNMSR1Ojtyj4W/b8Moy9dH/PihTp0cSrDm1eoy2hoUe8m/aL92YY/4= 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=OXiPA8zH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bumET0S1; 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="OXiPA8zH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bumET0S1" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031762; 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=XfMaXEid6qzuyKyUebh6BXsmnCqdBipNozQCXlzM1e0=; b=OXiPA8zHAiiaMmzcbFIUyl4xvYf9wprbYJ6KqANKSIurPTOg3lQFqVwoOO0nrJGoSBV6JW isyrFLGqCOKYJZWfQNJJSLC51X4Q15hccDW18Zb9ExKrOsLV0VB3clJFLrEnzAMGwsNvrB IQ6n+bOiTW6G8wd1Bg/IoNwNGO0oo/LnHHcR+Z20IvI8lYa7+5eUcWIbafENeTXwxEORqV CoBcv0QL6msLJ7SNxHjMlrc3xvBrqGY3Q08UHJngYalC429eMdxamm3ll8TTvfBA5dLgQO rLojJ+uEeQSRTUXQ1asMwPZj7Qu4sQ94mvHXXDkTISih5sELwc1wRAz5+AuDWw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031762; 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=XfMaXEid6qzuyKyUebh6BXsmnCqdBipNozQCXlzM1e0=; b=bumET0S1tur+oSlfT6oxF6LA+rIkmBB/S9+vIlP6uAEidXrw1YYNWxSGWMH2MiezuevctR e4EEUlFEYo8/LmAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 16/17] printk: nbcon: Assign nice -20 for printing threads Date: Fri, 30 Aug 2024 17:35:15 +0206 Message-Id: <20240830152916.10136-17-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 1559b5dc3075..384bda9b1019 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1318,6 +1318,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 1c6afb0ed4d2..c4a6aaddf8e6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3553,6 +3553,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 07:24:15 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 5F0401BAEEA for ; Fri, 30 Aug 2024 15:29:31 +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=1725031773; cv=none; b=AHDqbRAJjaEFXFnZ1aaHpGp9owca7mB/Y/+SJ9utsO+V7Iy3IuEoNNuRLQT+Iaq2r6Kbo+o/lJPxOL2mNv0jE1NC3DMRXyOb01B/2ru2/240idtGav56utptZxyVm1tQG6aPK8RavnWO/xJ5J8E3XOkaPqTC7YmYluN9I7BL3ws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725031773; c=relaxed/simple; bh=9IGRyHvIOyO4h4UgC+kRSg00xKhgkenj+kdyGqiNM1o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KAykUb5ICnBNe6rg/sNM4woRuSp3VNrnteyxAG2uiyUQx3LGvxjYLaGwbdEUNQb9x3Ml/3McJxwB2cIIafxsmXN00mhUr2JUdtzlLrq/0jxxceu9CvaVeXKse9NIiQC8Uav14KHjtvB/iiv8dy7GzsD2FS0z9rF+5dIDJC1pdIE= 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=nqEWWsAG; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=A6ZbNNvm; 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="nqEWWsAG"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="A6ZbNNvm" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1725031762; 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=DFlbLUwcAn0jTkWxIFR5wHqelEelckpUmnq7kM1wCmY=; b=nqEWWsAGukMmhlJFxLIOHOoIX5Z5X7P743hH9AJqJBw3kzzqgp4ulillUCMxyXTCkXYW/7 02rk+OifIatwUcNwfJdfT3cAXW8zW1lyg5iZE7ZV1VmvjIdYfa6z5k8tNXQ9zRTCr13cPA k80aIqiU18RAriNEy6uZZGj47dc3qq5d6uuYJjlFTAfAjP3VYwwIFrwhBbJfSNgP3o8SQG o8Pp89jE8ETIOrsoTMjge18GLzKuj0nOBvI0RVuc94pA11CmGDTsvUJ3jrMB++TZuZn5zt u4eSN0lYsEySCkpo1Gaw0HQ/aUBFmemCgEgGNU1H8t16TfgbXdcDNv1b2qOWCQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1725031762; 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=DFlbLUwcAn0jTkWxIFR5wHqelEelckpUmnq7kM1wCmY=; b=A6ZbNNvmOBTPYK0TaT5D5wU/8l8dEj8GxJcKdwl545K2Oe26MRcPR98ODaUUCnqmJ8WIND urUpb57y/dgLSEBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 17/17] printk: Avoid false positive lockdep report for legacy printing Date: Fri, 30 Aug 2024 17:35:16 +0206 Message-Id: <20240830152916.10136-18-john.ogness@linutronix.de> In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de> References: <20240830152916.10136-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 c4a6aaddf8e6..19e998c4985b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2981,6 +2981,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. @@ -3030,31 +3058,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