Re: [PATCH 02/19] printk: Introduce console_is_nbcon
From: Petr Mladek <pmladek@suse.com>
Date: 2026-01-13 17:37:21
Also in:
linux-fsdevel, linux-hardening, linux-m68k, linux-serial, linux-um, linuxppc-dev, lkml, netdev, sparclinux
On Sat 2025-12-27 09:16:09, Marcos Paulo de Souza wrote:
quoted hunk ↗ jump to hunk
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@suse.com> Signed-off-by: Marcos Paulo de Souza <redacted> --- 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
There is _srcu in the function name, see below.
quoted hunk ↗ jump to hunk
+ * @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)
And here it is without _srcu. I would prefer the variant with _srcu to make it clear that it can be called only under _srcu. Similar to console_srcu_read_flags(con).
quoted hunk ↗ jump to hunk
+{ + 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) {
Otherwise, it looks good to me. With a consistent name, feel free to use: Reviewed-by: Petr Mladek <pmladek@suse.com> Best Regards, Petr