printk/nmi: avoid direct printk()-s from __printk_nmi_flush()
PATCH 1/1 printk/nmi: avoid direct printk()-s from __printk_nmi_flush()
From: Sergey Senozhatsky <[email protected]>
__printk_nmi_flush() can be called from nmi_panic(), therefore it has to test whether it's executed in NMI context and thus must route the messages through deferred printk() or via direct printk(). Except for two places where __printk_nmi_flush() does unconditional direct printk() calls:
- pr_err("printk_nmi_flush: internal error ...")
- pr_cont("\n")
Factor out __print_nmi_seq_line(), which takes care of in_nmi(), and use it in __printk_nmi_flush() for printing and error-reporting.
--- kernel/printk/nmi.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
kernel/printk/nmi.c +20 -9
@@ -103,23 +103,32 @@ again:* printk one line from the temporary buffer from @start index until * and including the @end index. */ -static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +static void __print_nmi_seq_line (const char *text,int len) { -const char *buf = s->buffer + start; -/* * The buffers are flushed in NMI only on panic. The messages must * go only into the ring buffer at this stage. Consoles will get * explicitly called later when a crashdump is not generated. */ if (in_nmi ()) -printk_deferred ("%.*s" , (end - start) +1 , buf); +printk_deferred ("%.*s" , len, text);else -printk ("%.*s" , (end - start) +1 , buf); +printk ("%.*s" , len, text); }/* +* printk one line from the temporary buffer from @start index until +* and including the @end index. +*/ +static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +{ +const char *buf = s->buffer + start; + +__print_nmi_seq_line (buf, (end - start) +1 ); +} + +/* * Flush data from the associated per_CPU buffer. The function * can be called either via IRQ work or independently. */
@@ -150,9 +159,11 @@ more:* the buffer an unexpected way. If we printed something then * @len must only increase. */ -if (i && i >= len) -pr_err ("printk_nmi_flush: internal error: i=%d >= len=%zu\n" , - i, len); +if (i && i >= len) { +const char *msg ="printk_nmi_flush: internal error\n" ; + +__print_nmi_seq_line (msg,strlen (msg)); + }if (!len) goto out;/* Someone else has already flushed the buffer. */
@@ -173,7 +184,7 @@ more:/* Check if there was a partial line. */ if (last_i < size) {print_nmi_seq_line (s, last_i, size -1 ); -pr_cont ("\n" ); +__print_nmi_seq_line ("\n" ,strlen ("\n" )); }/*
Signature · 1 line
2.9.3
Quoted 11 lines · Sergey Senozhatsky
From: Sergey Senozhatsky <[email protected]>
__printk_nmi_flush()can be called fromnmi_panic(), therefore it has to test whether it's executed in NMI context and thus must route the messages through deferredprintk()or via directprintk(). Except for two places where__printk_nmi_flush()does unconditional directprintk()calls:
-pr_err("printk_nmi_flush: internal error ...")
-pr_cont("\n")Factor out
__print_nmi_seq_line(), which takes care ofin_nmi(), and use it in__printk_nmi_flush()for printing and error-reporting.
Great catch! The check and eventually printk_deferred() need to be used everywhere in __printk_nmi_flush().
Just some nitpicks below.
kernel/printk/nmi.c again: · line 105
Quoted 13 lines
Signed-off-by Sergey Senozhatsky <[email protected]>--- kernel/printk/nmi.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)kernel/printk/nmi.c +0
-0@@ -103,23 +103,32 @@ again:* printk one line from the temporary buffer from @start index until * and including the @end index. */
The comment above is not longer valid.
kernel/printk/nmi.c again: · line 106
Quoted 2 lines
kernel/printk/nmi.c +1
-1-static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +static void __print_nmi_seq_line (const char *text,int len)
Also the name of the function might be confusing because it is not longer used only for the seq buffer. I would rename it to something like:
printk_nmi_flush_line()
Quoted 13 lines
kernel/printk/nmi.c +1
-4{ -const char *buf = s->buffer + start; -/* * The buffers are flushed in NMI only on panic. The messages must * go only into the ring buffer at this stage. Consoles will get * explicitly called later when a crashdump is not generated. */ if (in_nmi ()) -printk_deferred ("%.*s" , (end - start) +1 , buf); +printk_deferred ("%.*s" , len, text);else -printk ("%.*s" , (end - start) +1 , buf);
kernel/printk/nmi.c again: · line 125
Quoted 10 lines
kernel/printk/nmi.c +6
-0+printk ("%.*s" , len, text); }/* +* printk one line from the temporary buffer from @start index until +* and including the @end index. +*/ +static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +{
Then I would rename also this function to something like:
printk_nmi_flush_seq_line()
kernel/printk/nmi.c again: · line 130
Quoted 5 lines
kernel/printk/nmi.c +5
-0+const char *buf = s->buffer + start; + +__print_nmi_seq_line (buf, (end - start) +1 ); +} +
Othrewise, it looks fine. With the above suggested changes, feel free to add:
Best Regards,
Petr
On (08/29/16 17:16), Petr Mladek wrote:
[..]
Quoted 1 line
The comment above is not longer valid.
oh, yes. it shouldn't even be there.
Quoted 8 lines
kernel/printk/nmi.c again: · line 106
Quoted 2 lines
kernel/printk/nmi.c +1
-1-static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +static void __print_nmi_seq_line (const char *text,int len)Also the name of the function might be confusing because it is not longer used only for the seq buffer. I would rename it to something like:
printk_nmi_flush_line()
sounds good.
Quoted 6 lines
kernel/printk/nmi.c again: · line 125
Quoted 2 lines
kernel/printk/nmi.c +2
-0+static void print_nmi_seq_line (struct nmi_seq_buf *s,int start,int end) +{Then I would rename also this function to something like:
printk_nmi_flush_seq_line()
sounds good.
Quoted 10 lines
kernel/printk/nmi.c again: · line 130
Quoted 5 lines
kernel/printk/nmi.c +5
-0+const char *buf = s->buffer + start; + +__print_nmi_seq_line (buf, (end - start) +1 ); +} +Othrewise, it looks fine. With the above suggested changes, feel free to add:
Reviewed-by Petr Mladek <[email protected]>
thanks, will re-spin today.
-ss
Petr,
one more question. Not related to the patch, but still related to NMI.
can NMI nest?
CPU0
-> NMI#0
printk_nmi_enter() this_cpu_write(printk_func, vprintk_nmi) ... => NMI#1 : printk_nmi_enter() : this_cpu_write(printk_func, vprintk_nmi) : this_cpu_read(printk_func)(fmt, args) : ^^^^^^^^^^^^^ OK, vprintk_nmi() from NMI : printk_nmi_exit() : this_cpu_write(printk_func, vprintk_default) << return NMI#1 this_cpu_read(printk_func)(fmt, args) ^^^^^^^^^^^^^ vprintk_default() from NMI? printk_nmi_exit() this_cpu_write(printk_func, vprintk_default) return NMI#0
can this happen?
shouldn't we do something like this then? /* not tested */
--- kernel/printk/internal.h | 2 ++ kernel/printk/nmi.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
kernel/printk/internal.h +2 -0
@@ -31,6 +31,8 @@ extern raw_spinlock_t logbuf_lock;* via per-CPU variable. */ DECLARE_PER_CPU (printk_func_t, printk_func); +DECLARE_PER_CPU (printk_func_t, printk_func_saved); + static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return this_cpu_read(printk_func)(fmt, args);
kernel/printk/nmi.c +8 -1
@@ -39,6 +39,7 @@* were handled or when IRQs are blocked. */ DEFINE_PER_CPU (printk_func_t, printk_func) = vprintk_default; +DEFINE_PER_CPU (printk_func_t, printk_func_saved);static int printk_nmi_irq_ready;atomic_t nmi_message_lost;
@@ -259,10 +260,16 @@ void __init printk_nmi_init(void)void printk_nmi_enter (void ) { +printk_func_t func =this_cpu_read (printk_func); + +if (func != vprintk_nmi) +this_cpu_write (printk_func_saved, func);this_cpu_write (printk_func, vprintk_nmi); }void printk_nmi_exit (void ) { -this_cpu_write (printk_func, vprintk_default); +printk_func_t func =this_cpu_read (printk_func_saved); + +this_cpu_write (printk_func, func); }
Signature · 1 line
2.9.3
Petr Mladek
maintainerQuoted 4 lines · Sergey Senozhatsky
Petr,
one more question. Not related to the patch, but still related to NMI.can NMI nest?
AFAIK, they cannot. NMIs should be disabled until iret is called. Therefore we should be on the safe side if iret is not called inside the NMI handler. But this should not happen because it would cause other problems, like using wrong return address.
Well, x86 nmi code has some hacks to handle exceptions inside NMI handlers that use iret. But printk_nmi_enter()/printk_nmi_exit() are never nested there. It is prevented by the nmi_state per-CPU variable. See do_nmi() in arch/x86/kernel/nmi.c.
Quoted 41 lines
shouldn't we do something like this then? /* not tested */
Signed-off-by Sergey Senozhatsky <[email protected]>--- kernel/printk/internal.h | 2 ++ kernel/printk/nmi.c | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-)kernel/printk/internal.h +2
-0@@ -31,6 +31,8 @@ extern raw_spinlock_t logbuf_lock;* via per-CPU variable. */ DECLARE_PER_CPU (printk_func_t, printk_func); +DECLARE_PER_CPU (printk_func_t, printk_func_saved); + static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return this_cpu_read(printk_func)(fmt, args);kernel/printk/nmi.c +4
-0@@ -39,6 +39,7 @@* were handled or when IRQs are blocked. */ DEFINE_PER_CPU (printk_func_t, printk_func) = vprintk_default; +DEFINE_PER_CPU (printk_func_t, printk_func_saved);static int printk_nmi_irq_ready;atomic_t nmi_message_lost;@@ -259,10 +260,16 @@ void __init printk_nmi_init(void)void printk_nmi_enter (void ) { +printk_func_t func =this_cpu_read (printk_func); + +if (func != vprintk_nmi)
kernel/printk/nmi.c void __init printk_nmi_init(void) · line 274
Quoted 10 lines
kernel/printk/nmi.c +4
-1+this_cpu_write (printk_func_saved, func);this_cpu_write (printk_func, vprintk_nmi); }void printk_nmi_exit (void ) { -this_cpu_write (printk_func, vprintk_default); +printk_func_t func =this_cpu_read (printk_func_saved); + +this_cpu_write (printk_func, func);
This would handle only one level of nesting. If nesting was possible we would probably need something else. Fortunately, I believe that we do not need this.
Thanks for checking the code.
Best Regards,
Petr
Quoted 11 lines · Petr Mladek
Quoted 4 lines · Sergey Senozhatsky
Petr,
one more question. Not related to the patch, but still related to NMI.can NMI nest?
AFAIK, they cannot. NMIs should be disabled until iret is called. Therefore we should be on the safe side if iret is not called inside the NMI handler. But this should not happen because it would cause other problems, like using wrong return address.
Quoted 4 lines
Well, x86 nmi code has some hacks to handle exceptions inside NMI handlers that use iret. But printk_nmi_enter()/printk_nmi_exit() are never nested there. It is prevented by the
nmi_stateper-CPU variable. Seedo_nmi()inarch/x86/kernel/nmi.c.
yes, x86 has a per-cpu nmi_state to handle the case when NMI is loosing its NMI context. But other arch-s, as far as I can see, don't do that. Does it mean that we are safe only on x86?
this printk_func_saved thing is still will be needed, I think, for alt_printk.
Example:
process abc
printk() alt_printk_enter() this_cpu_write(printk_func, vprintk_alt); -> NMI : printk_nmi_enter() : this_cpu_write(printk_func, vprintk_nmi); : printk_nmi_exit() : this_cpu_write(printk_func, vprintk_default); return NMI printk() <<<< nested printk -> vprintk_default(), set by nmi_exit() alt_printk_exit() ... -ss
Petr Mladek
maintainerQuoted 12 lines · Sergey Senozhatsky
Quoted 11 lines · Petr Mladek
Quoted 4 lines · Sergey Senozhatsky
Petr,
one more question. Not related to the patch, but still related to NMI.can NMI nest?
AFAIK, they cannot. NMIs should be disabled until iret is called. Therefore we should be on the safe side if iret is not called inside the NMI handler. But this should not happen because it would cause other problems, like using wrong return address.
Quoted 8 lines
Quoted 4 lines
Well, x86 nmi code has some hacks to handle exceptions inside NMI handlers that use iret. But printk_nmi_enter()/printk_nmi_exit() are never nested there. It is prevented by the
nmi_stateper-CPU variable. Seedo_nmi()inarch/x86/kernel/nmi.c.yes, x86 has a per-cpu
nmi_stateto handle the case when NMI is loosing its NMI context. But other arch-s, as far as I can see, don't do that. Does it mean that we are safe only on x86?
My understanding is that the kernel would crash on the other architectures if a double iret was called. By other words, they would have bigger problems than the nmi_enter()/nmi_exit() calls. So, we should be on the safe side.
Quoted 6 lines
this
printk_func_savedthing is still will be needed, I think, foralt_printk.Example:
process abc
Quoted 13 lines
printk() alt_printk_enter() this_cpu_write(printk_func, vprintk_alt); -> NMI : printk_nmi_enter() : this_cpu_write(printk_func, vprintk_nmi); : printk_nmi_exit() : this_cpu_write(printk_func, vprintk_default); return NMI printk() <<<< nested printk -> vprintk_default(), set by nmi_exit() alt_printk_exit() ...
I see. But then we will need to be more careful because printk_func and printk_func_saved will be manipulated in different contexts:
normal, irq, nmi. A solution might be using an atomic counter and selecting the right vprintk_func according to the value.
Well, I am still afraid that yet another alt_printk is not the way to go.
Best Regards,
Petr
On (08/30/16 13:19), Petr Mladek wrote:
[..]
Quoted 25 lines
Quoted 3 lines
yes, x86 has a per-cpu
nmi_stateto handle the case when NMI is loosing its NMI context. But other arch-s, as far as I can see, don't do that. Does it mean that we are safe only on x86?My understanding is that the kernel would crash on the other architectures if a double iret was called. By other words, they would have bigger problems than the nmi_enter()/nmi_exit() calls. So, we should be on the safe side.
Quoted 16 lines
this
printk_func_savedthing is still will be needed, I think, foralt_printk.Example:
process abc
printk() alt_printk_enter() this_cpu_write(printk_func, vprintk_alt); -> NMI : printk_nmi_enter() : this_cpu_write(printk_func, vprintk_nmi); : printk_nmi_exit() : this_cpu_write(printk_func, vprintk_default); return NMI
Quoted 8 lines
Quoted 3 lines
printk() <<<< nested printk -> vprintk_default(), set by nmi_exit() alt_printk_exit() ...I see. But then we will need to be more careful because
printk_funcandprintk_func_savedwill be manipulated in different contexts:
normal, irq, nmi. A solution might be using an atomic counter and selecting the rightvprintk_funcaccording to the value.
yes, I thought about something like this.
... or we can use the one and only 'nmi_seq' buffer and share it between NMI and alt_printk, adding a special prefix to every message
if (in_nmi())
sprintf("NMI:%s", message)
else
sprintf("%s", message)so, yes, it can get hairy, but at least it will be grep-able, still better than nothing.
Quoted 2 lines
Well, I am still afraid that yet another
alt_printkis not the way to go.
well, it might be and it might be not.
-ss
Quoted 5 lines · Petr Mladek
I see. But then we will need to be more careful because
printk_funcandprintk_func_savedwill be manipulated in different contexts:
normal, irq, nmi. A solution might be using an atomic counter and selecting the rightvprintk_funcaccording to the value.
alt_printk_enter() must be done with local IRQs disabled. so IRQ cannot race with normal' alt_printk. other IRQs cannot race with the current IRQ, because we have local IRQs disabled. the only thing that can race here is - NMI. both normal' and IRQ alt_printk can use the same per-CPU buffer, they never race. NMI needs to have its own.
-ss
Petr Mladek
maintainerQuoted 7 lines · Sergey Senozhatsky
Quoted 5 lines · Petr Mladek
I see. But then we will need to be more careful because
printk_funcandprintk_func_savedwill be manipulated in different contexts:
normal, irq, nmi. A solution might be using an atomic counter and selecting the rightvprintk_funcaccording to the value.
Quoted 5 lines
alt_printk_enter()must be done with local IRQs disabled. so IRQ cannot race withnormal' alt_printk. other IRQs cannot race with the current IRQ, because we have local IRQs disabled. the only thing that can race here is - NMI. bothnormal' and IRQalt_printkcan use the same per-CPU buffer, they never race. NMI needs to have its own.
Yes. Well, my concern was how to atomically change the printk_func pointer and save the previous value at the same time. You could not use locks because NMIs are involved.
Best Regards,
Petr