]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
printk: nbcon: Allow KDB to acquire the NBCON context
authorMarcos Paulo de Souza <mpdesouza@suse.com>
Thu, 16 Oct 2025 14:47:56 +0000 (11:47 -0300)
committerPetr Mladek <pmladek@suse.com>
Fri, 24 Oct 2025 10:55:10 +0000 (12:55 +0200)
KDB can interrupt any console to execute the "mirrored printing" at any
time, so add an exception to nbcon_context_try_acquire_direct to allow
to get the context if the current CPU is the same as kdb_printf_cpu.

This change will be necessary for the next patch, which fixes
kdb_msg_write to work with NBCON consoles by calling ->write_atomic on
such consoles. But to print it first needs to acquire the ownership of
the console, so nbcon_context_try_acquire_direct is fixed here.

Reviewed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Link: https://patch.msgid.link/20251016-nbcon-kgdboc-v6-3-866aac60a80e@suse.com
[pmladek@suse.com: Fix compilation with !CONFIG_KGDB_KDB.]
Signed-off-by: Petr Mladek <pmladek@suse.com>
include/linux/kdb.h
kernel/printk/nbcon.c

index ecbf819deeca118f27e98bf71bb37dd27a257ebb..741c58e864314656b050991b1996ebcf06b7c639 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 #include <linux/list.h>
+#include <linux/smp.h>
 
 /* Shifted versions of the command enable bits are be used if the command
  * has no arguments (see kdb_check_flags). This allows commands, such as
@@ -207,11 +208,26 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)
 /* Dynamic kdb shell command registration */
 extern int kdb_register(kdbtab_t *cmd);
 extern void kdb_unregister(kdbtab_t *cmd);
+
+/* Return true when KDB as locked for printing a message on this CPU. */
+static inline
+bool kdb_printf_on_this_cpu(void)
+{
+       /*
+        * We can use raw_smp_processor_id() here because the task could
+        * not get migrated when KDB has locked for printing on this CPU.
+        */
+       return unlikely(READ_ONCE(kdb_printf_cpu) == raw_smp_processor_id());
+}
+
 #else /* ! CONFIG_KGDB_KDB */
 static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
 static inline void kdb_init(int level) {}
 static inline int kdb_register(kdbtab_t *cmd) { return 0; }
 static inline void kdb_unregister(kdbtab_t *cmd) {}
+
+static inline bool kdb_printf_on_this_cpu(void) { return false; }
+
 #endif /* CONFIG_KGDB_KDB */
 enum {
        KDB_NOT_INITIALIZED,
index e1bf5409cb6bd39c5af6774569efa79bffe262df..5be0184939098494bf61830a3de5f93ea856bcd7 100644 (file)
@@ -10,6 +10,7 @@
 #include <linux/export.h>
 #include <linux/init.h>
 #include <linux/irqflags.h>
+#include <linux/kdb.h>
 #include <linux/kthread.h>
 #include <linux/minmax.h>
 #include <linux/panic.h>
@@ -249,13 +250,16 @@ static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,
                 * since all non-panic CPUs are stopped during panic(), it
                 * is safer to have them avoid gaining console ownership.
                 *
-                * If this acquire is a reacquire (and an unsafe takeover
+                * One exception is when kdb has locked for printing on this CPU.
+                *
+                * Second exception is a reacquire (and an unsafe takeover
                 * has not previously occurred) then it is allowed to attempt
                 * a direct acquire in panic. This gives console drivers an
                 * opportunity to perform any necessary cleanup if they were
                 * interrupted by the panic CPU while printing.
                 */
                if (panic_on_other_cpu() &&
+                   !kdb_printf_on_this_cpu() &&
                    (!is_reacquire || cur->unsafe_takeover)) {
                        return -EPERM;
                }