Re: [GIT PULL] tracing/NMI/printk: Use seq_buf for safe printing from NMI context

From: Steven Rostedt
Date: Thu Dec 11 2014 - 06:33:21 EST


On Wed, 10 Dec 2014 21:29:40 -0800
Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Wed, Dec 10, 2014 at 8:20 PM, Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > Would it perhaps be possible/reasonable to also use this to get rid of
> > the horrible "early_printk()" stuff [...]
>
> Another question: the "preempt_disable/enable()" around the use of the
> per-cpu vprintk_func thing seems dubious.
>
> Why do I say that? I think it cannot possibly make sense. Anybody who
> sets that function pointer to any per-cpu value has to disable
> preemption for that to make sense, so doing it inside the printk()
> paths seems dubious at best.
>
> No?

So you are saying that anytime the printk_func is not the default, the
caller had to have disable preemption? Even if the caller changes all
per cpu calls, it is probably still safe as other cpus will be either
using the default or the one that is going to be the "default" (I say
that as being global).

Thus something like this?

-- Steve

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 5af2b8bc88f0..9b896e7a50a9 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1859,10 +1859,16 @@ asmlinkage __visible int printk(const char *fmt, ...)
int r;

va_start(args, fmt);
- preempt_disable();
+
+ /*
+ * If a caller overrides the per_cpu printk_func, then it needs
+ * to disable preemption when calling printk(). Otherwise
+ * the printk_func should be set to the default. No need to
+ * disable preemption here.
+ */
vprintk_func = this_cpu_read(printk_func);
r = vprintk_func(fmt, args);
- preempt_enable();
+
va_end(args);

return r;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/