[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251227-printk-cleanup-part3-v1-2-21a291bcf197@suse.com>
Date: Sat, 27 Dec 2025 09:16:09 -0300
From: Marcos Paulo de Souza <mpdesouza@...e.com>
To: Richard Weinberger <richard@....at>,
Anton Ivanov <anton.ivanov@...bridgegreys.com>,
Johannes Berg <johannes@...solutions.net>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Jason Wessel <jason.wessel@...driver.com>,
Daniel Thompson <danielt@...nel.org>,
Douglas Anderson <dianders@...omium.org>, Petr Mladek <pmladek@...e.com>,
Steven Rostedt <rostedt@...dmis.org>,
John Ogness <john.ogness@...utronix.de>,
Sergey Senozhatsky <senozhatsky@...omium.org>,
Jiri Slaby <jirislaby@...nel.org>, Breno Leitao <leitao@...ian.org>,
Andrew Lunn <andrew+netdev@...n.ch>,
"David S. Miller" <davem@...emloft.net>, Eric Dumazet <edumazet@...gle.com>,
Jakub Kicinski <kuba@...nel.org>, Paolo Abeni <pabeni@...hat.com>,
Geert Uytterhoeven <geert@...ux-m68k.org>, Kees Cook <kees@...nel.org>,
Tony Luck <tony.luck@...el.com>,
"Guilherme G. Piccoli" <gpiccoli@...lia.com>,
Madhavan Srinivasan <maddy@...ux.ibm.com>,
Michael Ellerman <mpe@...erman.id.au>, Nicholas Piggin <npiggin@...il.com>,
Christophe Leroy <christophe.leroy@...roup.eu>,
Andreas Larsson <andreas@...sler.com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Maxime Coquelin <mcoquelin.stm32@...il.com>,
Alexandre Torgue <alexandre.torgue@...s.st.com>,
Jacky Huang <ychuang3@...oton.com>, Shan-Chun Hung <schung@...oton.com>,
Laurentiu Tudor <laurentiu.tudor@....com>
Cc: linux-um@...ts.infradead.org, linux-kernel@...r.kernel.org,
kgdb-bugreport@...ts.sourceforge.net, linux-serial@...r.kernel.org,
netdev@...r.kernel.org, linux-m68k@...ts.linux-m68k.org,
linux-hardening@...r.kernel.org, linuxppc-dev@...ts.ozlabs.org,
sparclinux@...r.kernel.org, linux-stm32@...md-mailman.stormreply.com,
linux-arm-kernel@...ts.infradead.org, linux-fsdevel@...r.kernel.org,
Marcos Paulo de Souza <mpdesouza@...e.com>
Subject: [PATCH 02/19] printk: Introduce console_is_nbcon
Besides checking if the current console is NBCON or not, console->flags
is also being read in order to serve as argument of the console_is_usable
function.
But CON_NBCON flag is unique: it's set just once in the console
registration and never cleared. In this case it can be possible to read
the flag when console_srcu_lock is held (which is the case when using
for_each_console).
This change makes possible to remove the flags argument from
console_is_usable in the next patches.
Signed-off-by: Petr Mladek <pmladek@...e.com>
Signed-off-by: Marcos Paulo de Souza <mpdesouza@...e.com>
---
include/linux/console.h | 27 +++++++++++++++++++++++++++
kernel/debug/kdb/kdb_io.c | 2 +-
kernel/printk/nbcon.c | 2 +-
kernel/printk/printk.c | 15 ++++++---------
4 files changed, 35 insertions(+), 11 deletions(-)
diff --git a/include/linux/console.h b/include/linux/console.h
index 35c03fc4ed51..dd4ec7a5bff9 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -561,6 +561,33 @@ static inline void console_srcu_write_flags(struct console *con, short flags)
WRITE_ONCE(con->flags, flags);
}
+/**
+ * console_srcu_is_nbcon - Locklessly check whether the console is nbcon
+ * @con: struct console pointer of console to check
+ *
+ * Requires console_srcu_read_lock to be held, which implies that @con might
+ * be a registered console. The purpose of holding console_srcu_read_lock is
+ * to guarantee that no exit/cleanup routines will run if the console
+ * is currently undergoing unregistration.
+ *
+ * If the caller is holding the console_list_lock or it is _certain_ that
+ * @con is not and will not become registered, the caller may read
+ * @con->flags directly instead.
+ *
+ * Context: Any context.
+ * Return: True when CON_NBCON flag is set.
+ */
+static inline bool console_is_nbcon(const struct console *con)
+{
+ WARN_ON_ONCE(!console_srcu_read_lock_is_held());
+
+ /*
+ * The CON_NBCON flag is statically initialized and is never
+ * set or cleared at runtime.
+ */
+ return data_race(con->flags & CON_NBCON);
+}
+
/* Variant of console_is_registered() when the console_list_lock is held. */
static inline bool console_is_registered_locked(const struct console *con)
{
diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index 6ffb962392a4..d6de512b433a 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -596,7 +596,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
if (c == dbg_io_ops->cons)
continue;
- if (flags & CON_NBCON) {
+ if (console_is_nbcon(c)) {
struct nbcon_write_context wctxt = { };
/*
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 13865ef85990..f0f42e212caa 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1647,7 +1647,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
for_each_console_srcu(con) {
short flags = console_srcu_read_flags(con);
- if (!(flags & CON_NBCON))
+ if (!console_is_nbcon(con))
continue;
if (!console_is_usable(con, flags, NBCON_USE_ATOMIC))
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 5f4b84f9562e..bd0d574be3cf 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3200,7 +3200,7 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
* nbcon consoles when the nbcon consoles cannot print via
* their atomic or threaded flushing.
*/
- if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
+ if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload))
continue;
if (!console_is_usable(con, flags,
@@ -3209,7 +3209,7 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
continue;
any_usable = true;
- if (flags & CON_NBCON) {
+ if (console_is_nbcon(con)) {
progress = nbcon_legacy_emit_next_record(con, handover, cookie,
!do_cond_resched);
printk_seq = nbcon_seq_read(con);
@@ -3458,7 +3458,6 @@ void console_unblank(void)
static void __console_rewind_all(void)
{
struct console *c;
- short flags;
int cookie;
u64 seq;
@@ -3466,9 +3465,7 @@ static void __console_rewind_all(void)
cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
- flags = console_srcu_read_flags(c);
-
- if (flags & CON_NBCON) {
+ if (console_is_nbcon(c)) {
nbcon_seq_force(c, seq);
} else {
/*
@@ -3632,13 +3629,13 @@ static bool legacy_kthread_should_wakeup(void)
* consoles when the nbcon consoles cannot print via their
* atomic or threaded flushing.
*/
- if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
+ if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload))
continue;
if (!console_is_usable(con, flags, NBCON_USE_THREAD))
continue;
- if (flags & CON_NBCON) {
+ if (console_is_nbcon(con)) {
printk_seq = nbcon_seq_read(con);
} else {
/*
@@ -4490,7 +4487,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
if (!console_is_usable(c, flags, NBCON_USE_ANY))
continue;
- if (flags & CON_NBCON) {
+ if (console_is_nbcon(c)) {
printk_seq = nbcon_seq_read(c);
} else {
printk_seq = c->seq;
--
2.52.0
Powered by blists - more mailing lists