From: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Petr Mladek <pmladek@suse.com>,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
Peter Zijlstra <peterz@infradead.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [GIT PULL] printk for 4.13
Date: Thu, 6 Jul 2017 09:44:04 +0900 [thread overview]
Message-ID: <20170706004404.GA7195@jagdpanzerIV.localdomain> (raw)
In-Reply-To: <CA+55aFzu1KKJHfKtpeBkK7zSGMrzrUnkJhKEB2FJQ9HMb0VBLg@mail.gmail.com>
Hello,
On (07/05/17 11:23), Linus Torvalds wrote:
> On Mon, Jul 3, 2017 at 7:24 AM, Petr Mladek <pmladek@suse.com> wrote:
> >
> > - Store printk() messages into the main log buffer directly even in NMI
> > when the lock is available. It is the best effort to print even large
> > chunk of text. It is handy, for example, when all ftrace messages
> > are printed during the system panic in NMI.
>
> No, this is entirely buggered.
>
> You can't just do
>
> raw_spin_is_locked(&logbuf_lock)
>
> to test whether you can get the logbuf_lock and then call vprintk_default().
>
> It's not just about deadlock avoidance, the code will call things like
> down_trylock() on the console semaphore, and that operation is
> fundamentally not NMI-safe. The semaphore count is literally protected
> by a irq-safe (BUT NOT NMI-SAFE!) semaphore spinlock.
>
> So now you can instead deadlock just on the internal console semaphore
> spinlock (ie somebody is doing "console_lock()", NMI comes in, *BOOM*.
you are right. but
PRINTK_NMI_DEFERRED_CONTEXT_MASK calls into vprintk_deferred(), which
does vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args). and
vprintk_emit() LOGLEVEL_SCHED is nothing, but printk_deferred(), which
never calls into the scheduler and does not even try to lock console_sem.
the only lock it takes is logbuf_lock, and the idea behind those checks
in printk_nmi_enter() is to make sure that we can safely call
vprintk_deferred() from NMI on that CPU.
printk_safe context is per-CPU, so if we see at NMI entry that this_cpu
has PRINTK_SAFE_CONTEXT_MASK set, then it means that NMI is preempting
printk-safe context on this_cpu, which is already alarming, because we
switch to printk-safe context *mostly* in places where printk recursion
can deadlock us (console_sem up/down/try_down and logbuf_lock lock/unlock).
why *mostly*... because we are also in printk-safe context during the
call_console_drivers() call.
so this is why we also check raw_spin_is_locked(&logbuf_lock). by the
time we do raw_spin_is_locked(&logbuf_lock) we already know that
this_cpu is in printk-safe, so it is _potentially_ unsafe to call
vprintk_emit(), unless we are printk-safe for call_console_drivers(),
which means that logbuf_lock is not lockde by this_cpu.
so what printk_nmi_enter() does
- this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK
if we are NOT in printk-safe then we know that from this CPU we can
call printk_deferred().
otherwise, we need to make sure that it is safe to call printk_deferred().
- raw_spin_is_locked(&logbuf_lock)
if the lock is not locked -- we can printk_deferred() from that CPU.
otherwise, we use printk-nmi buffer and flush the messages to logbuf
later from irq_work.
does this make sense?
-ss
next prev parent reply other threads:[~2017-07-06 0:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-03 14:24 Petr Mladek
2017-07-05 18:23 ` Linus Torvalds
2017-07-06 0:44 ` Sergey Senozhatsky [this message]
2017-07-06 0:53 ` Sergey Senozhatsky
2017-07-06 1:15 ` Linus Torvalds
2017-07-06 1:18 ` Sergey Senozhatsky
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=20170706004404.GA7195@jagdpanzerIV.localdomain \
--to=sergey.senozhatsky.work@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sergey.senozhatsky@gmail.com \
--cc=torvalds@linux-foundation.org \
/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
all inboxes | Powered by JetHome®