public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jan Kara <jack@suse.cz>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] printk/nmi: avoid direct printk()-s from __printk_nmi_flush()
Date: Tue, 30 Aug 2016 11:04:36 +0200	[thread overview]
Message-ID: <20160830090436.GO4866@pathway.suse.cz> (raw)
In-Reply-To: <20160830075834.GA494@swordfish>

On Tue 2016-08-30 16:58:34, Sergey Senozhatsky wrote:
> 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.


> shouldn't we do something like this then? /* not tested */
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> 
> ---
>  kernel/printk/internal.h | 2 ++
>  kernel/printk/nmi.c      | 9 ++++++++-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
> index 7fd2838..5b7508f 100644
> --- a/kernel/printk/internal.h
> +++ b/kernel/printk/internal.h
> @@ -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);
> diff --git a/kernel/printk/nmi.c b/kernel/printk/nmi.c
> index 16bab47..9d83929 100644
> --- a/kernel/printk/nmi.c
> +++ b/kernel/printk/nmi.c
> @@ -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);

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

  reply	other threads:[~2016-08-30  9:04 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-29 12:32 [PATCH] printk/nmi: avoid direct printk()-s from __printk_nmi_flush() Sergey Senozhatsky
2016-08-29 15:16 ` Petr Mladek
2016-08-30  1:07   ` Sergey Senozhatsky
2016-08-30  7:58   ` Sergey Senozhatsky
2016-08-30  9:04     ` Petr Mladek [this message]
2016-08-30  9:39       ` Sergey Senozhatsky
2016-08-30 11:19         ` Petr Mladek
2016-08-31  4:00           ` Sergey Senozhatsky
2016-09-01  7:55           ` Sergey Senozhatsky
2016-09-01  8:17             ` Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160830090436.GO4866@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=sergey.senozhatsky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox