[tip: sched/rt] printk: Add is_printk_legacy_deferred()

From: tip-bot2 for John Ogness
Date: Mon Sep 09 2024 - 13:33:27 EST


The following commit has been merged into the sched/rt branch of tip:

Commit-ID: 70411bf8d22ab3d2f794f199d81d70b62d3a85fa
Gitweb: https://git.kernel.org/tip/70411bf8d22ab3d2f794f199d81d70b62d3a85fa
Author: John Ogness <john.ogness@xxxxxxxxxxxxx>
AuthorDate: Tue, 20 Aug 2024 08:35:49 +02:06
Committer: Petr Mladek <pmladek@xxxxxxxx>
CommitterDate: Wed, 21 Aug 2024 14:56:24 +02:00

printk: Add is_printk_legacy_deferred()

If printk has been explicitly deferred or is called from NMI
context, legacy console printing must be deferred to an irq_work
context. Introduce a helper function is_printk_legacy_deferred()
for a CPU to query if it must defer legacy console printing.

In follow-up commits this helper will be needed at other call
sites as well.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>
Link: https://lore.kernel.org/r/20240820063001.36405-24-john.ogness@xxxxxxxxxxxxx
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/internal.h | 2 ++
kernel/printk/printk_safe.c | 11 ++++++++++-
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 44468f3..84706c1 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -75,6 +75,7 @@ bool printk_percpu_data_ready(void);
} while (0)

void defer_console_output(void);
+bool is_printk_legacy_deferred(void);

u16 printk_parse_prefix(const char *text, int *level,
enum printk_info_flags *flags);
@@ -138,6 +139,7 @@ static inline bool console_is_usable(struct console *con, short flags)
#define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)

static inline bool printk_percpu_data_ready(void) { return false; }
+static inline bool is_printk_legacy_deferred(void) { return false; }
static inline u64 nbcon_seq_read(struct console *con) { return 0; }
static inline void nbcon_seq_force(struct console *con, u64 seq) { }
static inline bool nbcon_alloc(struct console *con) { return false; }
diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
index 4421cca..86439fd 100644
--- a/kernel/printk/printk_safe.c
+++ b/kernel/printk/printk_safe.c
@@ -38,6 +38,15 @@ void __printk_deferred_exit(void)
__printk_safe_exit();
}

+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.
+ */
+ return (this_cpu_read(printk_context) || in_nmi());
+}
+
asmlinkage int vprintk(const char *fmt, va_list args)
{
#ifdef CONFIG_KGDB_KDB
@@ -50,7 +59,7 @@ asmlinkage int vprintk(const char *fmt, va_list args)
* Use the main logbuf even in NMI. But avoid calling console
* drivers that might have their own locks.
*/
- if (this_cpu_read(printk_context) || in_nmi())
+ if (is_printk_legacy_deferred())
return vprintk_deferred(fmt, args);

/* No obstacles. */