All of lore.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,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Subject: Re: [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support
Date: Mon, 25 Apr 2022 17:18:47 +0200	[thread overview]
Message-ID: <Yma71x2p10d6yOLU@alley> (raw)
In-Reply-To: <877d7gu7yg.fsf@jogness.linutronix.de>

On Fri 2022-04-22 23:31:11, John Ogness wrote:
> On 2022-04-22, Petr Mladek <pmladek@suse.com> wrote:
> > Another problem is that the ordering is not stable. The console
> > might come and go.
> 
> The console list is protected by @console_sem, so it wouldn't be an
> actual problem. The real issue is that lockdep would not like it. A new
> lockdep class would need to be setup for each register_console().

Yeah. I did not mention it explicitely but I meant it as a problem
with lockdep.

> >> Anyway, I will first look into the nested locking solution. That
> >> seems more promising to me and it would go a long way to simplify the
> >> locking hierarchy.
> >
> > Please, do not spend too much time on this. The solution must be
> > simple in principle. If it gets complicated than it will likely
> > be worse than the current code.
> 
> Sure. The goal is to simplify. The only complexity will be doing in a
> way that allow lockdep to understand it.

I am not sure how to distinguish intentional and non-intentional
ordering change.


> > Alternative solution would be to reduce the number of variables
> > affected by the race. I mean:
> >
> >    + replace CON_THB_BLOCKED flag with con->blocked to avoid
> >      the needed of READ_ONCE()/WRITE_ONCE().
> >
> >    + check con->blocked right after taking con->lock in
> >      printk_kthread_func() so that all the other accesses are
> >      safe.
> 
> Honestly, I would prefer this to what v4 is doing. The only reason
> CON_THD_BLOCKED is a flag is to save space. But we are only talking
> about a few bytes being saved. There aren't that many consoles.
> 
> It would be a very simple change. Literally just replacing the 3 lines
> that set/clear CON_THD_BLOCKED and replacing/reordering the 2 lines that
> check the flag. Then all the READ_ONCE/WRITE_ONCE to @flags could be
> removed.

I agree that it sounds like the easiest solution for now. If you
prepare v5 with this change then I push it into linux-next instead
of v4.

Well, I think that we need to make con->lock safe to use in the long
term. The above workaround in printk_kthread_func() is good enough
for now because this is the only location where con->lock is taken without
console_sem. But I am sure that we/people will want to do more
console-specific operations without console_sem in the future.

IMHO, the only sane approach is to follow the proposed rules:

    + console_lock() will synchronize both global and per-console
      stuff.

    + con->lock will synchronize per-console stuff.

    + con->lock could not be taken alone when the big console_lock()
      is taken.


I currently know only about two solutions:

    1. The nested locking. console_lock() will take console_sem
       and all con->lock's and will keep them locked.

       It is rather trivial in principle. The problem is lockdep
       and possible ABBA deadlocks caused by unstable ordering.


    2. Create the wrappers around con->lock that will check
       whether console_sem is taken (con->locked flag).

       It will require additional per-console waitqueue. But all
       the magic will be hidden in the wrappers.


I personally prefer 2nd approach for the long term solution. It might
look more complicated but it will not break lockdep.

Best Regards,
Petr

  reply	other threads:[~2022-04-25 15:18 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-19 23:46 [PATCH printk v3 00/15] printk/for-next John Ogness
2022-04-19 23:46 ` [PATCH printk v3 01/15] printk: rename cpulock functions John Ogness
2022-04-19 23:46 ` [PATCH printk v3 02/15] printk: cpu sync always disable interrupts John Ogness
2022-04-19 23:46 ` [PATCH printk v3 03/15] printk: add missing memory barrier to wake_up_klogd() John Ogness
2022-04-20 12:34   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 04/15] printk: wake up all waiters John Ogness
2022-04-20 12:36   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 05/15] printk: wake waiters for safe and NMI contexts John Ogness
2022-04-20 13:55   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 06/15] printk: get caller_id/timestamp after migration disable John Ogness
2022-04-19 23:46 ` [PATCH printk v3 07/15] printk: call boot_delay_msec() in printk_delay() John Ogness
2022-04-19 23:46 ` [PATCH printk v3 08/15] printk: add con_printk() macro for console details John Ogness
2022-04-20 14:01   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 09/15] printk: refactor and rework printing logic John Ogness
2022-04-20 14:55   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 10/15] printk: move buffer definitions into console_emit_next_record() caller John Ogness
2022-04-19 23:46 ` [PATCH printk v3 11/15] printk: add pr_flush() John Ogness
2022-04-20 15:10   ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 12/15] printk: add functions to prefer direct printing John Ogness
2022-04-19 23:46 ` [PATCH printk v3 13/15] printk: add kthread console printers John Ogness
2022-04-20 17:53   ` Petr Mladek
2022-04-20 20:02     ` John Ogness
2022-04-21 14:25       ` Petr Mladek
2022-04-19 23:46 ` [PATCH printk v3 14/15] printk: extend console_lock for proper kthread support John Ogness
2022-04-20  2:13   ` kernel test robot
2022-04-20 13:32     ` John Ogness
2022-04-20 13:32       ` John Ogness
2022-04-20  4:04   ` kernel test robot
2022-04-21 12:41   ` Petr Mladek
2022-04-21 14:30     ` John Ogness
2022-04-22 13:03       ` Petr Mladek
2022-04-22 14:14         ` John Ogness
2022-04-22 15:15           ` Petr Mladek
2022-04-22 21:25             ` John Ogness
2022-04-25 15:18               ` Petr Mladek [this message]
2022-04-25 19:10                 ` John Ogness
2022-04-19 23:46 ` [PATCH printk v3 15/15] printk: remove @console_locked John Ogness
2022-04-21 12:46   ` Petr Mladek
2022-04-21 14:40 ` [PATCH printk v3 00/15] printk/for-next Petr Mladek
2022-04-21 15:02   ` 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=Yma71x2p10d6yOLU@alley \
    --to=pmladek@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --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 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.