public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH printk v7 24/35] printk: nbcon: Flush new records on device_release()
Date: Wed, 7 Aug 2024 14:48:09 +0200	[thread overview]
Message-ID: <ZrNtCWvRK2ASWovm@pathway.suse.cz> (raw)
In-Reply-To: <875xsdf7zm.fsf@jogness.linutronix.de>

On Wed 2024-08-07 03:21:57, John Ogness wrote:
> On 2024-08-05, Petr Mladek <pmladek@suse.com> wrote:
> >> +	/*
> >> +	 * This context must flush any new records added while the console
> >> +	 * was locked. The console_srcu_read_lock must be taken to ensure
> >> +	 * the console is usable throughout flushing.
> >> +	 */
> >> +	cookie = console_srcu_read_lock();
> >> +	if (console_is_usable(con, console_srcu_read_flags(con)) &&
> >> +	    prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
> >> +		if (!have_boot_console) {
> >> +			__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb));
> >> +		} else if (!is_printk_legacy_deferred()) {
> >> +			if (console_trylock())
> >> +				console_unlock();
> >
> > nbcon_device_release() is going to be called in uart_port_unlock*()
> > still under the port->lock.
> >
> > => It smells with a potential deadlock. The console_flush_all() in
> >    console_unlock() might want to flush this console under the
> >    port->lock as well.
> >
> >    And it almost happens because nbcon_legacy_emit_next_record()
> >    might eventually take con->device_lock() when called in
> >    a task context.
> >
> >    It will not happen here because this code uses console_trylock()
> >    which would set @console_may_schedule to false.
> 
> Exactly. That is an important point. We must never try to invoke the
> write_thread() callback while holding a spinlock.
> 
> > Anyway, it would look more safe when the flush was done after releasing
> > the port->lock.
> 
> Even then we could never invoke the write_thread() callback because the
> caller may be holding other spinlocks. If we cannot safely call
> console_lock(), we cannot take the device lock. The atomic callback must
> be used and that means the port lock cannot be involved in the
> console_trylock().

It makes sense. But it is not obvious.

I actually thought about using con->device_lock() lock around
nbcon_legacy_emit_next_record(). It would help to synchronize
the legacy loop against nbcon_device_lock()/nbcon_device_release().
It won't be needed to call the legacy loop in
nbcon_device_release().

But it was bad idea. There are more reasons to avoid taking
con->device_lock() after console_trylock():

  1. con->device_lock() might be a sleeping lock in the future. [*]
     It actually already is a sleeping lock in RT. And
     console_trylock() might be used in printk() in any context.

  2. The less locks we take the more safe printk() will be in
     various contexts and situations, especially in panic().


[*] I still have to wire this into my mental model. I keep forgetting it.

    It would be nice to have a documentation summarizing the main
    ideas and describing the printk() design. I hope that we will
    do it one day.

Best Regards,
Petr

  reply	other threads:[~2024-08-07 12:48 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-04  0:51 [PATCH printk v7 00/35] wire up write_atomic() printing John Ogness
2024-08-04  0:51 ` [PATCH printk v7 01/35] printk: Add notation to console_srcu locking John Ogness
2024-08-04  0:51 ` [PATCH printk v7 02/35] printk: nbcon: Consolidate alloc() and init() John Ogness
2024-08-05 14:39   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 03/35] printk: Properly deal with nbcon consoles on seq init John Ogness
2024-08-04  0:51 ` [PATCH printk v7 04/35] printk: Check printk_deferred_enter()/_exit() usage John Ogness
2024-08-04  0:51 ` [PATCH printk v7 05/35] printk: nbcon: Clarify rules of the owner/waiter matching John Ogness
2024-08-04  0:51 ` [PATCH printk v7 06/35] printk: nbcon: Remove return value for write_atomic() John Ogness
2024-08-04  0:51 ` [PATCH printk v7 07/35] printk: nbcon: Add detailed doc " John Ogness
2024-08-04  0:51 ` [PATCH printk v7 08/35] printk: nbcon: Add callbacks to synchronize with driver John Ogness
2024-08-04  0:51 ` [PATCH printk v7 09/35] printk: nbcon: Use driver synchronization while (un)registering John Ogness
2024-08-04  0:51 ` [PATCH printk v7 10/35] serial: core: Provide low-level functions to lock port John Ogness
2024-08-04  0:51 ` [PATCH printk v7 11/35] serial: core: Introduce wrapper to set @uart_port->cons John Ogness
2024-08-04  0:51 ` [PATCH printk v7 12/35] console: Improve console_srcu_read_flags() comments John Ogness
2024-08-04  0:51 ` [PATCH printk v7 13/35] nbcon: Add API to acquire context for non-printing operations John Ogness
2024-08-04  0:51 ` [PATCH printk v7 14/35] serial: core: Acquire nbcon context in port->lock wrapper John Ogness
2024-08-04  0:51 ` [PATCH printk v7 15/35] printk: nbcon: Do not rely on proxy headers John Ogness
2024-08-04  0:51 ` [PATCH printk v7 16/35] printk: Make console_is_usable() available to nbcon.c John Ogness
2024-08-04  0:51 ` [PATCH printk v7 17/35] printk: Let console_is_usable() handle nbcon John Ogness
2024-08-04  0:51 ` [PATCH printk v7 18/35] printk: Add @flags argument for console_is_usable() John Ogness
2024-08-04  0:51 ` [PATCH printk v7 19/35] printk: nbcon: Add helper to assign priority based on CPU state John Ogness
2024-08-04  0:51 ` [PATCH printk v7 20/35] printk: nbcon: Provide function to flush using write_atomic() John Ogness
2024-08-05 14:59   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 21/35] printk: Track registered boot consoles John Ogness
2024-08-04  0:51 ` [PATCH printk v7 22/35] printk: nbcon: Use nbcon consoles in console_flush_all() John Ogness
2024-08-04  0:51 ` [PATCH printk v7 23/35] printk: Add is_printk_legacy_deferred() John Ogness
2024-08-05 15:03   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 24/35] printk: nbcon: Flush new records on device_release() John Ogness
2024-08-05 15:31   ` Petr Mladek
2024-08-07  1:15     ` John Ogness
2024-08-07 12:48       ` Petr Mladek [this message]
2024-08-04  0:51 ` [PATCH printk v7 25/35] printk: Flush nbcon consoles first on panic John Ogness
2024-08-06  8:31   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 26/35] printk: nbcon: Add unsafe flushing " John Ogness
2024-08-04  0:51 ` [PATCH printk v7 27/35] printk: Avoid console_lock dance if no legacy or boot consoles John Ogness
2024-08-06  8:40   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 28/35] printk: Track nbcon consoles John Ogness
2024-08-04  0:51 ` [PATCH printk v7 29/35] printk: Coordinate direct printing in panic John Ogness
2024-08-06  9:59   ` Petr Mladek
2024-08-06 17:29     ` Linus Torvalds
2024-08-06 12:17   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 30/35] printk: Add helper for flush type logic John Ogness
2024-08-07 11:39   ` Petr Mladek
2024-08-07 14:11     ` John Ogness
2024-08-08  9:03       ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 31/35] printk: nbcon: Implement emergency sections John Ogness
2024-08-07 12:52   ` Petr Mladek
2024-08-04  0:51 ` [PATCH printk v7 32/35] panic: Mark emergency section in warn John Ogness
2024-08-04  0:51 ` [PATCH printk v7 33/35] panic: Mark emergency section in oops John Ogness
2024-08-04  0:51 ` [PATCH printk v7 34/35] rcu: Mark emergency sections in rcu stalls John Ogness
2024-08-04  0:51 ` [PATCH printk v7 35/35] lockdep: Mark emergency sections in lockdep splats John Ogness

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=ZrNtCWvRK2ASWovm@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=tglx@linutronix.de \
    /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