[PATCH 03/19] printk: Drop flags argument from console_is_usable

Marcos Paulo de Souza
2025-12-27T22:38:41Z

The flags argument was also used to check if CON_NBCON was set, but their
usage was fixed in the last commit. All current users are reading the
variable just to call console_is_usable.

By calling console_srcu_read_flags inside console_is_usable makes the
code cleaner and removes one argument from the function.

Along with it, create a variant called __console_is_usable that can be
used under console_list_lock(), like unregister_console_locked.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
---
arch/um/kernel/kmsg_dump.c | 3 +--
include/linux/console.h | 25 +++++++++++++++++--------
kernel/debug/kdb/kdb_io.c | 4 +---
kernel/printk/nbcon.c | 15 ++++-----------
kernel/printk/printk.c | 20 +++++++-------------
5 files changed, 30 insertions(+), 37 deletions(-)

diff --git a/arch/um/kernel/kmsg_dump.c b/arch/um/kernel/kmsg_dump.c
index 8ae38308b67c..ca80232cfa2a 100644
--- a/arch/um/kernel/kmsg_dump.c
+++ b/arch/um/kernel/kmsg_dump.c
@@ -31,8 +31,7 @@ static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,
* expected to output the crash information.
*/
if (strcmp(con->name, "ttynull") != 0 &&
- console_is_usable(con, console_srcu_read_flags(con),
- NBCON_USE_ATOMIC)) {
+ console_is_usable(con, NBCON_USE_ATOMIC)) {
break;
}
}
diff --git a/include/linux/console.h b/include/linux/console.h
index dd4ec7a5bff9..648cf10e3f93 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -656,13 +656,8 @@ extern bool nbcon_kdb_try_acquire(struct console *con,
struct nbcon_write_context *wctxt);
extern void nbcon_kdb_release(struct nbcon_write_context *wctxt);

-/*
- * Check if the given console is currently capable and allowed to print
- * records. Note that this function does not consider the current context,
- * 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,
+/* Variant of console_is_usable() when the console_list_lock is held. */
+static inline bool __console_is_usable(struct console *con, short flags,
enum nbcon_write_cb nwc)
{
if (!(flags & CON_ENABLED))
@@ -707,6 +702,18 @@ static inline bool console_is_usable(struct console *con, short flags,
return true;
}

+/*
+ * Check if the given console is currently capable and allowed to print
+ * records. Note that this function does not consider the current context,
+ * 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,
+ enum nbcon_write_cb nwc)
+{
+ return __console_is_usable(con, console_srcu_read_flags(con), nwc);
+}
+
#else
static inline void nbcon_cpu_emergency_enter(void) { }
static inline void nbcon_cpu_emergency_exit(void) { }
@@ -719,7 +726,9 @@ static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) { }
static inline bool nbcon_kdb_try_acquire(struct console *con,
struct nbcon_write_context *wctxt) { return false; }
static inline void nbcon_kdb_release(struct nbcon_write_context *wctxt) { }
-static inline bool console_is_usable(struct console *con, short flags,
+static inline bool __console_is_usable(struct console *con, short flags,
+ enum nbcon_write_cb nwc) { return false; }
+static inline bool console_is_usable(struct console *con,
enum nbcon_write_cb nwc) { return false; }
#endif

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index d6de512b433a..642eab746577 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -589,9 +589,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
*/
cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
- short flags = console_srcu_read_flags(c);
-
- if (!console_is_usable(c, flags, NBCON_USE_ATOMIC))
+ if (!console_is_usable(c, NBCON_USE_ATOMIC))
continue;
if (c == dbg_io_ops->cons)
continue;
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index f0f42e212caa..f0659c1e50ed 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1164,7 +1164,6 @@ static bool nbcon_emit_one(struct nbcon_write_context *wctxt, bool use_atomic)
static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_context *ctxt)
{
bool ret = false;
- short flags;
int cookie;

if (kthread_should_stop())
@@ -1183,8 +1182,7 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex

cookie = console_srcu_read_lock();

- flags = console_srcu_read_flags(con);
- if (console_is_usable(con, flags, NBCON_USE_THREAD)) {
+ if (console_is_usable(con, NBCON_USE_THREAD)) {
/* Bring the sequence in @ctxt up to date */
ctxt->seq = nbcon_seq_read(con);

@@ -1209,7 +1207,6 @@ static int nbcon_kthread_func(void *__console)
.ctxt.prio = NBCON_PRIO_NORMAL,
};
struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
- short con_flags;
bool backlog;
int cookie;

@@ -1249,9 +1246,7 @@ static int nbcon_kthread_func(void *__console)
*/
cookie = console_srcu_read_lock();

- con_flags = console_srcu_read_flags(con);
-
- if (console_is_usable(con, con_flags, NBCON_USE_THREAD))
+ if (console_is_usable(con, NBCON_USE_THREAD))
backlog = nbcon_emit_one(&wctxt, false);

console_srcu_read_unlock(cookie);
@@ -1645,12 +1640,10 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)

cookie = console_srcu_read_lock();
for_each_console_srcu(con) {
- short flags = console_srcu_read_flags(con);
-
if (!console_is_nbcon(con))
continue;

- if (!console_is_usable(con, flags, NBCON_USE_ATOMIC))
+ if (!console_is_usable(con, NBCON_USE_ATOMIC))
continue;

if (nbcon_seq_read(con) >= stop_seq)
@@ -1904,7 +1897,7 @@ void nbcon_device_release(struct console *con)
*/
cookie = console_srcu_read_lock();
printk_get_console_flush_type(&ft);
- if (console_is_usable(con, console_srcu_read_flags(con), NBCON_USE_ATOMIC) &&
+ if (console_is_usable(con, NBCON_USE_ATOMIC) &&
!ft.nbcon_offload &&
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
/*
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index bd0d574be3cf..b03ffc23c27c 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3191,7 +3191,6 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *

cookie = console_srcu_read_lock();
for_each_console_srcu(con) {
- short flags = console_srcu_read_flags(con);
u64 printk_seq;
bool progress;

@@ -3203,9 +3202,8 @@ static bool console_flush_one_record(bool do_cond_resched, u64 *next_seq, bool *
if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload))
continue;

- if (!console_is_usable(con, flags,
- do_cond_resched ? NBCON_USE_THREAD
- : NBCON_USE_ATOMIC))
+ if (!console_is_usable(con, do_cond_resched ? NBCON_USE_THREAD
+ : NBCON_USE_ATOMIC))
continue;
any_usable = true;

@@ -3394,7 +3392,7 @@ void console_unblank(void)
*/
cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
- if (!console_is_usable(c, console_srcu_read_flags(c), NBCON_USE_ATOMIC))
+ if (!console_is_usable(c, NBCON_USE_ATOMIC))
continue;

if (c->unblank) {
@@ -3434,7 +3432,7 @@ void console_unblank(void)

cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
- if (!console_is_usable(c, console_srcu_read_flags(c), NBCON_USE_ATOMIC))
+ if (!console_is_usable(c, NBCON_USE_ATOMIC))
continue;

if (c->unblank)
@@ -3621,7 +3619,6 @@ static bool legacy_kthread_should_wakeup(void)

cookie = console_srcu_read_lock();
for_each_console_srcu(con) {
- short flags = console_srcu_read_flags(con);
u64 printk_seq;

/*
@@ -3632,7 +3629,7 @@ static bool legacy_kthread_should_wakeup(void)
if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload))
continue;

- if (!console_is_usable(con, flags, NBCON_USE_THREAD))
+ if (!console_is_usable(con, NBCON_USE_THREAD))
continue;

if (console_is_nbcon(con)) {
@@ -4203,7 +4200,7 @@ static int unregister_console_locked(struct console *console)

if (!console_is_registered_locked(console))
res = -ENODEV;
- else if (console_is_usable(console, console->flags, NBCON_USE_ATOMIC))
+ else if (__console_is_usable(console, console->flags, NBCON_USE_ATOMIC))
__pr_flush(console, 1000, true);

/* Disable it unconditionally */
@@ -4430,7 +4427,6 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
struct console *c;
u64 last_diff = 0;
u64 printk_seq;
- short flags;
int cookie;
u64 diff;
u64 seq;
@@ -4477,14 +4473,12 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
if (con && con != c)
continue;

- flags = console_srcu_read_flags(c);
-
/*
* If consoles are not usable, it cannot be expected
* that they make forward progress, so only increment
* @diff for usable consoles.
*/
- if (!console_is_usable(c, flags, NBCON_USE_ANY))
+ if (!console_is_usable(c, NBCON_USE_ANY))
continue;

if (console_is_nbcon(c)) {

--
2.52.0