Re: [PATCH next v4 1/2] lib/dump_stack: move cpu lock to printk.c

From: Steven Rostedt
Date: Thu Jun 17 2021 - 09:32:49 EST

On Thu, 17 Jun 2021 11:56:50 +0206
John Ogness <john.ogness@xxxxxxxxxxxxx> wrote:

> dump_stack() implements its own cpu-reentrant spinning lock to
> best-effort serialize stack traces in the printk log. However,
> there are other functions (such as show_regs()) that can also
> benefit from this serialization.
> Move the cpu-reentrant spinning lock (cpu lock) into new helper
> functions printk_cpu_lock_irqsave()/printk_cpu_unlock_irqrestore()
> so that it is available for others as well. For !CONFIG_SMP the
> cpu lock is a NOP.
> Note that having multiple cpu locks in the system can easily
> lead to deadlock. Code needing a cpu lock should use the
> printk cpu lock, since the printk cpu lock could be acquired
> from any code and any context.
> Also note that it is not necessary for a cpu lock to disable
> interrupts. However, in upcoming work this cpu lock will be used
> for emergency tasks (for example, atomic consoles during kernel
> crashes) and any interruptions while holding the cpu lock should
> be avoided if possible.
> Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
> ---

Can we add this lock to early_printk() ?

This would make early_printk() so much more readable.

-- Steve

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 421c35571797..2b749c745c1f 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2259,6 +2259,7 @@ struct console *early_console;

asmlinkage __visible void early_printk(const char *fmt, ...)
+ unsigned long flags;
va_list ap;
char buf[512];
int n;
@@ -2270,7 +2271,9 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
n = vscnprintf(buf, sizeof(buf), fmt, ap);

+ printk_cpu_lock_irqsave(flags);
early_console->write(early_console, buf, n);
+ printk_cpu_unlock_irqrestore(flags);