From 6d4773a777a5a67e6b532e18a063bdabbe8616c9 Mon Sep 17 00:00:00 2001
From: Thomas Gleixner <tglx@linutronix.de>
Date: Mon, 11 Sep 2023 16:01:49 +0000
Subject: [PATCH 130/184] printk: nbcon: Add nbcon console flushing using
 write_atomic()

Perform nbcon console flushing at key call sites:

console_flush_on_panic() - This function is not only used in
   panic() but also other locations where there may be stored
   messages that need to be flushed. Flush the nbcon consoles
   as well.

printk_trigger_flush() - Used in urgent situations to trigger a
   dump in an irq_work context. For nbcon consoles, flush using
   the write_atomic() callback directly in the calling context
   since an alternative context is not required.

Co-developed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Thomas Gleixner (Intel) <tglx@linutronix.de>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/printk/printk.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 4624c9d92a9d..8060d8a88fed 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3195,6 +3195,9 @@ void console_flush_on_panic(enum con_flush_mode mode)
 		console_srcu_read_unlock(cookie);
 	}
 
+	if (!have_boot_console)
+		nbcon_atomic_flush_all();
+
 	console_flush_all(false, &next_seq, &handover);
 }
 
@@ -3963,8 +3966,22 @@ void defer_console_output(void)
 	__wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT);
 }
 
+/**
+ * printk_trigger_flush() - Make sure that the consoles will get flushed
+ *
+ * Try to flush consoles when possible or queue flushing consoles like
+ * in the deferred printk.
+ *
+ * Context: Can be used in any context
+ */
 void printk_trigger_flush(void)
 {
+	if (!have_boot_console) {
+		preempt_disable();
+		nbcon_atomic_flush_all();
+		preempt_enable();
+	}
+
 	defer_console_output();
 }
 
-- 
2.34.1

