From 120c1f7de5b7e050d67cb8caa678e0290b609458 Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Thu, 14 Dec 2023 14:38:42 +0000
Subject: [PATCH 134/204] printk: nbcon: Assign priority based on CPU state

Use the current state of the CPU to determine which priority to
assign to the printing context.

Note: The uart_port wrapper, which is responsible for non-console-
      printing activities, will always use NORMAL priority.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/printk/internal.h |    2 ++
 kernel/printk/nbcon.c    |   30 ++++++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 2 deletions(-)

Index: linux-6.6.58-rt45/kernel/printk/internal.h
===================================================================
@ linux-6.6.58-rt45/kernel/printk/internal.h:82 @ void nbcon_seq_force(struct console *con
 bool nbcon_alloc(struct console *con);
 void nbcon_init(struct console *con);
 void nbcon_free(struct console *con);
+enum nbcon_prio nbcon_get_default_prio(void);
 void nbcon_atomic_flush_all(void);
 bool nbcon_atomic_emit_next_record(struct console *con, bool *handover, int cookie);
 
@ linux-6.6.58-rt45/kernel/printk/internal.h:139 @ static inline void nbcon_seq_force(struc
 static inline bool nbcon_alloc(struct console *con) { return false; }
 static inline void nbcon_init(struct console *con) { }
 static inline void nbcon_free(struct console *con) { }
+static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
 static inline void nbcon_atomic_flush_all(void) { }
 static inline bool nbcon_atomic_emit_next_record(struct console *con, bool *handover,
 						 int cookie) { return false; }
Index: linux-6.6.58-rt45/kernel/printk/nbcon.c
===================================================================
--- linux-6.6.58-rt45.orig/kernel/printk/nbcon.c
+++ linux-6.6.58-rt45/kernel/printk/nbcon.c
@ linux-6.6.58-rt45/kernel/printk/internal.h:965 @ static bool nbcon_atomic_emit_one(struct
 }
 
 /**
+ * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon
+ *				printing on the current CPU
+ *
+ * Context:	Any context which could not be migrated to another CPU.
+ * Return:	The nbcon_prio to use for acquiring an nbcon console in this
+ *		context for printing.
+ */
+enum nbcon_prio nbcon_get_default_prio(void)
+{
+	if (this_cpu_in_panic())
+		return NBCON_PRIO_PANIC;
+
+	return NBCON_PRIO_NORMAL;
+}
+
+/**
  * nbcon_atomic_emit_next_record - Print one record for an nbcon console
  *					using the write_atomic() callback
  * @con:	The console to print on
@ linux-6.6.58-rt45/kernel/printk/internal.h:1016 @ bool nbcon_atomic_emit_next_record(struc
 	cant_migrate();
 
 	ctxt->console	= con;
-	ctxt->prio	= NBCON_PRIO_NORMAL;
+	ctxt->prio	= nbcon_get_default_prio();
 
 	progress = nbcon_atomic_emit_one(&wctxt);
 
@ linux-6.6.58-rt45/kernel/printk/internal.h:1062 @ static void __nbcon_atomic_flush_all(u64
 			memset(ctxt, 0, sizeof(*ctxt));
 			ctxt->console			= con;
 			ctxt->spinwait_max_us		= 2000;
-			ctxt->prio			= NBCON_PRIO_NORMAL;
 
 			/*
 			 * Atomic flushing does not use console driver
@ linux-6.6.58-rt45/kernel/printk/internal.h:1070 @ static void __nbcon_atomic_flush_all(u64
 			 * disabled to avoid being interrupted and then
 			 * calling into a driver that will deadlock trying
 			 * acquire console ownership.
+			 *
+			 * This also disables migration in order to get the
+			 * current CPU priority.
 			 */
 			local_irq_save(irq_flags);
 
+			ctxt->prio = nbcon_get_default_prio();
+
 			any_progress |= nbcon_atomic_emit_one(&wctxt);
 
 			local_irq_restore(irq_flags);
@ linux-6.6.58-rt45/kernel/printk/internal.h:1189 @ static inline bool uart_is_nbcon(struct
  *
  * If @up is an nbcon console, this console will be acquired and marked as
  * unsafe. Otherwise this function does nothing.
+ *
+ * nbcon consoles acquired via the port lock wrapper always use priority
+ * NBCON_PRIO_NORMAL.
  */
 void nbcon_acquire(struct uart_port *up)
 {
@ linux-6.6.58-rt45/kernel/printk/internal.h:1226 @ EXPORT_SYMBOL_GPL(nbcon_acquire);
  *
  * If @up is an nbcon console, the console will be marked as safe and
  * released. Otherwise this function does nothing.
+ *
+ * nbcon consoles acquired via the port lock wrapper always use priority
+ * NBCON_PRIO_NORMAL.
  */
 void nbcon_release(struct uart_port *up)
 {