[PATCH] printk: nbcon_atomic_flush_pending() is safe only when there is no boot console

From: Petr Mladek
Date: Thu Jun 13 2024 - 08:52:53 EST


Boot consoles are not serialized with the nbcon consoles via the nbcon
console context or con->device_lock(). The serialization is possible only
via the legacy console_lock().

The decision whether nbcon_atomic_flush_pending() should and can be
called safely is similar and closely related to the decision
whether the legacy loop should be used.

Define printing_via_context_safe symmetrically with printing_via_unlock.
Allow to call nbcon_atomic_flush_pending() only when it is needed and safe.

Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/internal.h | 10 ++++++++++
kernel/printk/nbcon.c | 9 +++++++--
kernel/printk/printk.c | 7 ++++---
3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 38680c6b2b39..bafec0a27da3 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -182,6 +182,7 @@ static inline bool console_is_usable(struct console *con, short flags) { return
#endif /* CONFIG_PRINTK */

extern bool have_boot_console;
+extern bool have_nbcon_console;
extern bool have_legacy_console;

/*
@@ -192,6 +193,15 @@ extern bool have_legacy_console;
*/
#define printing_via_unlock (have_legacy_console || have_boot_console)

+/*
+ * Specifies if printing on nbcon consoles is needed and also safe
+ * when serialized only by the nbcon context. If @have_boot_console
+ * is true, the nbcon consoles must be serialized with the boot
+ * consoles using the legacy console_lock().
+ */
+#define printing_via_context_safe (have_nbcon_console || !have_boot_console)
+
+
extern struct printk_buffers printk_shared_pbufs;

/**
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 89b340ca303c..9b2df848718c 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1404,7 +1404,8 @@ void nbcon_cpu_emergency_exit(void)
* for the emergency messages is NBCON_PRIO_EMERGENCY.
*/
if (*cpu_emergency_nesting == 1) {
- nbcon_atomic_flush_pending();
+ if (printing_via_context_safe)
+ nbcon_atomic_flush_pending();

/*
* Safely attempt to flush the legacy consoles in this
@@ -1446,7 +1447,8 @@ void nbcon_cpu_emergency_flush(void)
if (*(nbcon_get_cpu_emergency_nesting()) == 0)
return;

- nbcon_atomic_flush_pending();
+ if (printing_via_context_safe)
+ nbcon_atomic_flush_pending();

if (printing_via_unlock && !is_printk_deferred()) {
if (console_trylock())
@@ -1637,6 +1639,9 @@ void nbcon_device_release(struct console *con)
* was locked. The console_srcu_read_lock must be taken to ensure
* the console is usable throughout flushing.
*/
+ if (!printing_via_context_safe)
+ return;
+
cookie = console_srcu_read_lock();
if (console_is_usable(con, console_srcu_read_flags(con)) &&
!con->kthread &&
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 137bd9a721c4..3183db5b4180 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -475,7 +475,7 @@ bool have_legacy_console;
* synchronous printing of legacy consoles will not occur during panic until
* the backtrace has been stored to the ringbuffer.
*/
-static bool have_nbcon_console;
+bool have_nbcon_console;

/*
* Specifies if a boot console is registered. If boot consoles are present,
@@ -2370,7 +2370,7 @@ asmlinkage int vprintk_emit(int facility, int level,

printed_len = vprintk_store(facility, level, dev_info, fmt, args);

- if (have_nbcon_console && !have_boot_console) {
+ if (printing_via_context_safe) {
bool is_panic_context = this_cpu_in_panic();

/*
@@ -3283,7 +3283,8 @@ void console_flush_on_panic(enum con_flush_mode mode)
if (mode == CONSOLE_REPLAY_ALL)
__console_rewind_all();

- nbcon_atomic_flush_pending();
+ if (printing_via_context_safe)
+ nbcon_atomic_flush_pending();

if (printing_via_unlock)
console_flush_all(false, &next_seq, &handover);
--
2.45.2