printk: Avoid livelock with heavy printk during panic
authorStephen Brennan <stephen.s.brennan@oracle.com>
Wed, 2 Feb 2022 17:18:20 +0000 (09:18 -0800)
committerPetr Mladek <pmladek@suse.com>
Mon, 14 Feb 2022 12:39:20 +0000 (13:39 +0100)
During panic(), if another CPU is writing heavily the kernel log (e.g.
via /dev/kmsg), then the panic CPU may livelock writing out its messages
to the console. Note when too many messages are dropped during panic and
suppress further printk, except from the panic CPU. This could result in
some important messages being dropped. However, messages are already
being dropped, so this approach at least prevents a livelock.

Reviewed-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220202171821.179394-4-stephen.s.brennan@oracle.com
kernel/printk/printk.c

index e83c127701043b49dd233c21efb67f1109272698..2ec6b547cda699cf9f5ea70ba4212b92b63cda96 100644 (file)
@@ -93,6 +93,12 @@ EXPORT_SYMBOL_GPL(console_drivers);
  */
 int __read_mostly suppress_printk;
 
+/*
+ * During panic, heavy printk by other CPUs can delay the
+ * panic and risk deadlock on console resources.
+ */
+int __read_mostly suppress_panic_printk;
+
 #ifdef CONFIG_LOCKDEP
 static struct lockdep_map console_lock_dep_map = {
        .name = "console_lock"
@@ -2232,6 +2238,10 @@ asmlinkage int vprintk_emit(int facility, int level,
        if (unlikely(suppress_printk))
                return 0;
 
+       if (unlikely(suppress_panic_printk) &&
+           atomic_read(&panic_cpu) != raw_smp_processor_id())
+               return 0;
+
        if (level == LOGLEVEL_SCHED) {
                level = LOGLEVEL_DEFAULT;
                in_sched = true;
@@ -2617,6 +2627,7 @@ void console_unlock(void)
 {
        static char ext_text[CONSOLE_EXT_LOG_MAX];
        static char text[CONSOLE_LOG_MAX];
+       static int panic_console_dropped;
        unsigned long flags;
        bool do_cond_resched, retry;
        struct printk_info info;
@@ -2671,6 +2682,10 @@ skip:
                if (console_seq != r.info->seq) {
                        console_dropped += r.info->seq - console_seq;
                        console_seq = r.info->seq;
+                       if (panic_in_progress() && panic_console_dropped++ > 10) {
+                               suppress_panic_printk = 1;
+                               pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n");
+                       }
                }
 
                if (suppress_message_printing(r.info->level)) {