]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
printk: Defer legacy printing when holding printk_cpu_sync
authorJohn Ogness <john.ogness@linutronix.de>
Mon, 9 Dec 2024 11:17:46 +0000 (12:23 +0106)
committerPetr Mladek <pmladek@suse.com>
Mon, 16 Dec 2024 12:26:31 +0000 (13:26 +0100)
The documentation of printk_cpu_sync_get() clearly states
that the owner must never perform any activities where it waits
for a CPU. For legacy printing there can be spinning on the
console_lock and on the port lock. Therefore legacy printing
must be deferred when holding the printk_cpu_sync.

Note that in the case of emergency states, atomic consoles
are not prevented from printing when printk is deferred. This
is appropriate because they do not spin-wait indefinitely for
other CPUs.

Reported-by: Rik van Riel <riel@surriel.com>
Closes: https://lore.kernel.org/r/20240715232052.73eb7fb1@imladris.surriel.com
Signed-off-by: John Ogness <john.ogness@linutronix.de>
Fixes: 55d6af1d6688 ("lib/nmi_backtrace: explicitly serialize banner and regs")
Reviewed-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20241209111746.192559-3-john.ogness@linutronix.de
Signed-off-by: Petr Mladek <pmladek@suse.com>
kernel/printk/internal.h
kernel/printk/printk.c
kernel/printk/printk_safe.c

index c6bb47666aef67ef4d5281eaa0b865d150fb455e..a91bdf802967169db21d8c8ef176ee63bbd77705 100644 (file)
@@ -338,3 +338,9 @@ bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
 void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
 void console_prepend_replay(struct printk_message *pmsg);
 #endif
+
+#ifdef CONFIG_SMP
+bool is_printk_cpu_sync_owner(void);
+#else
+static inline bool is_printk_cpu_sync_owner(void) { return false; }
+#endif
index 80910bc3470c234cf695eecf4a2963f5fa51d664..f446a06b4da8ca364fddfaec2d70315031016cd3 100644 (file)
@@ -4922,6 +4922,11 @@ void console_try_replay_all(void)
 static atomic_t printk_cpu_sync_owner = ATOMIC_INIT(-1);
 static atomic_t printk_cpu_sync_nested = ATOMIC_INIT(0);
 
+bool is_printk_cpu_sync_owner(void)
+{
+       return (atomic_read(&printk_cpu_sync_owner) == raw_smp_processor_id());
+}
+
 /**
  * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant
  *                            spinning lock is not owned by any CPU.
index 6283bc0b55e6dbdc79a214e7c3eefa4346fe9410..32a28f563b1378931a89cf2bc8bdd78c455ea8df 100644 (file)
@@ -61,10 +61,15 @@ bool is_printk_legacy_deferred(void)
        /*
         * The per-CPU variable @printk_context can be read safely in any
         * context. CPU migration is always disabled when set.
+        *
+        * A context holding the printk_cpu_sync must not spin waiting for
+        * another CPU. For legacy printing, it could be the console_lock
+        * or the port lock.
         */
        return (force_legacy_kthread() ||
                this_cpu_read(printk_context) ||
-               in_nmi());
+               in_nmi() ||
+               is_printk_cpu_sync_owner());
 }
 
 asmlinkage int vprintk(const char *fmt, va_list args)