All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kernel/printk: prevent deadlock at unexpected call kmsg_dump in NMI context
@ 2019-07-15  8:04 Konstantin Khlebnikov
  2019-07-16  7:41 ` Petr Mladek
  0 siblings, 1 reply; 3+ messages in thread
From: Konstantin Khlebnikov @ 2019-07-15  8:04 UTC (permalink / raw)
  To: Petr Mladek, linux-kernel, Steven Rostedt, Sergey Senozhatsky

Kernel message dumper - function kmsg_dump() is called on various oops or
panic paths which could happen in unpredictable context including NMI.

Panic in NMI is handled especially by stopping all other cpus with
smp_send_stop() and busting locks in printk_safe_flush_on_panic().

Other less-fatal cases shouldn't happen in NMI and cannot be handled.
But this might happen for example on oops in nmi context. In this case
dumper could deadlock on lockbuf_lock or break internal structures.

This patch catches kmsg_dump() called in NMI context except panic and
prints warning once.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Link: https://lore.kernel.org/lkml/156294329676.1745.2620297516210526183.stgit@buzz/ (v1)
---
 kernel/printk/printk.c |    7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 1888f6a3b694..e711f64a1843 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3104,6 +3104,13 @@ void kmsg_dump(enum kmsg_dump_reason reason)
 	struct kmsg_dumper *dumper;
 	unsigned long flags;
 
+	/*
+	 * In NMI context only panic could be handled safely:
+	 * it stops other cpus and busts logbuf lock.
+	 */
+	if (WARN_ON_ONCE(reason != KMSG_DUMP_PANIC && in_nmi()))
+		return;
+
 	if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
 		return;
 


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] kernel/printk: prevent deadlock at unexpected call kmsg_dump in NMI context
  2019-07-15  8:04 [PATCH v2] kernel/printk: prevent deadlock at unexpected call kmsg_dump in NMI context Konstantin Khlebnikov
@ 2019-07-16  7:41 ` Petr Mladek
  2019-07-16  7:55   ` Konstantin Khlebnikov
  0 siblings, 1 reply; 3+ messages in thread
From: Petr Mladek @ 2019-07-16  7:41 UTC (permalink / raw)
  To: Konstantin Khlebnikov; +Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel

On Mon 2019-07-15 11:04:55, Konstantin Khlebnikov wrote:
> Kernel message dumper - function kmsg_dump() is called on various oops or
> panic paths which could happen in unpredictable context including NMI.
> 
> Panic in NMI is handled especially by stopping all other cpus with
> smp_send_stop() and busting locks in printk_safe_flush_on_panic().
> 
> Other less-fatal cases shouldn't happen in NMI and cannot be handled.
> But this might happen for example on oops in nmi context. In this case
> dumper could deadlock on lockbuf_lock or break internal structures.

If I get it correctly than this patch could really prevent a deadlock
in at least:

  + oops_end()
    + oops_exit()
      + kmsg_dump(KMSG_DUMP_OOPS)

If it is called in NMI, it should end up with panic(). Then the dump
will be called later after stopping CPUs...

Or am I wrong?

Otherwise, the patch looks good to me. I would just mention
the above scenario if it is correct.

Best Regards,
Petr

> This patch catches kmsg_dump() called in NMI context except panic and
> prints warning once.
> 
> Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
> Link: https://lore.kernel.org/lkml/156294329676.1745.2620297516210526183.stgit@buzz/ (v1)
> ---
>  kernel/printk/printk.c |    7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 1888f6a3b694..e711f64a1843 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -3104,6 +3104,13 @@ void kmsg_dump(enum kmsg_dump_reason reason)
>  	struct kmsg_dumper *dumper;
>  	unsigned long flags;
>  
> +	/*
> +	 * In NMI context only panic could be handled safely:
> +	 * it stops other cpus and busts logbuf lock.
> +	 */
> +	if (WARN_ON_ONCE(reason != KMSG_DUMP_PANIC && in_nmi()))
> +		return;
> +
>  	if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
>  		return;
>  
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] kernel/printk: prevent deadlock at unexpected call kmsg_dump in NMI context
  2019-07-16  7:41 ` Petr Mladek
@ 2019-07-16  7:55   ` Konstantin Khlebnikov
  0 siblings, 0 replies; 3+ messages in thread
From: Konstantin Khlebnikov @ 2019-07-16  7:55 UTC (permalink / raw)
  To: Petr Mladek; +Cc: Sergey Senozhatsky, Steven Rostedt, linux-kernel

On 16.07.2019 10:41, Petr Mladek wrote:
> On Mon 2019-07-15 11:04:55, Konstantin Khlebnikov wrote:
>> Kernel message dumper - function kmsg_dump() is called on various oops or
>> panic paths which could happen in unpredictable context including NMI.
>>
>> Panic in NMI is handled especially by stopping all other cpus with
>> smp_send_stop() and busting locks in printk_safe_flush_on_panic().
>>
>> Other less-fatal cases shouldn't happen in NMI and cannot be handled.
>> But this might happen for example on oops in nmi context. In this case
>> dumper could deadlock on lockbuf_lock or break internal structures.
> 
> If I get it correctly than this patch could really prevent a deadlock
> in at least:
> 
>    + oops_end()
>      + oops_exit()
>        + kmsg_dump(KMSG_DUMP_OOPS)
> 
> If it is called in NMI, it should end up with panic(). Then the dump
> will be called later after stopping CPUs...
> 
> Or am I wrong?

Yep. Under 'oops in nmi context' I mean exactly that case.

> 
> Otherwise, the patch looks good to me. I would just mention
> the above scenario if it is correct.
> 
> Best Regards,
> Petr
> 
>> This patch catches kmsg_dump() called in NMI context except panic and
>> prints warning once.
>>
>> Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
>> Link: https://lore.kernel.org/lkml/156294329676.1745.2620297516210526183.stgit@buzz/ (v1)
>> ---
>>   kernel/printk/printk.c |    7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
>> index 1888f6a3b694..e711f64a1843 100644
>> --- a/kernel/printk/printk.c
>> +++ b/kernel/printk/printk.c
>> @@ -3104,6 +3104,13 @@ void kmsg_dump(enum kmsg_dump_reason reason)
>>   	struct kmsg_dumper *dumper;
>>   	unsigned long flags;
>>   
>> +	/*
>> +	 * In NMI context only panic could be handled safely:
>> +	 * it stops other cpus and busts logbuf lock.
>> +	 */
>> +	if (WARN_ON_ONCE(reason != KMSG_DUMP_PANIC && in_nmi()))
>> +		return;
>> +
>>   	if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
>>   		return;
>>   
>>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-16  7:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-15  8:04 [PATCH v2] kernel/printk: prevent deadlock at unexpected call kmsg_dump in NMI context Konstantin Khlebnikov
2019-07-16  7:41 ` Petr Mladek
2019-07-16  7:55   ` Konstantin Khlebnikov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.