All of lore.kernel.org
 help / color / mirror / Atom feed
From: John Ogness <john.ogness@linutronix.de>
To: Petr Mladek <pmladek@suse.com>
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: Fri, 22 Apr 2022 16:20:52 +0206	[thread overview]
Message-ID: <87o80tp5lv.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <YmKnp3Ccu7laW3E4@alley>

On 2022-04-22, Petr Mladek <pmladek@suse.com> wrote:
> IMHO, it is actually a generic problem of the complex locking scheme
> when there are too many combinations of the protected data.

Sure. We are in a delicate situation of continuing to support the old
locking scheme while transitioning to a new one.

> In the current state, the problem seems to be only with CON_ENABLED
> flag but there might be other hidden races in the future.
>
> IMHO, it would be much easier when there are the following rules:
>
>    + console_lock() blocks taking con->lock
>    + con->lock blocks taking console_lock()
>    + Different con->lock might be taken in parallel
>
> The result would be:
>
>    + global variables need to be guarded by the big console_lock()
>    + con->lock should be enough to guard per-console variables
>    + the big console_lock() would serialize also access to
>      per-console variables.

It looks like you are talking about nested locking. This was my original
idea but I had problems relating to kthread stopping. However, the code
has changed a lot since then and now when I look at it, it does not look
like it would be a problem. Getting rid of CON_THD_BLOCKED would greatly
simplify the relationship between console_lock and kthreads.

For this we would need the console list to become a list_head so that it
is doubly linked (in order to unlock in reverse order). That probably
would be a good idea anyway. It is a bit bizarre that printk implements
its own linked list.

> Of course, it is not that simple. I am not 100% that we could
> even achieve this.

It just might be that simple. I will explore it again.

> Anyway, I think about the following wrapper:
>
> void single_console_lock(struct console *con)
> {
> 	for (;;) {
> 		error = wait_event_interruptible(log_wait,
> 					con->flags & CON_THB_BLOCKED);
>
> 		if (error)
> 			continue;
>
> 		mutex_lock(&con->lock);
>
> 		if (!con->flags & CON_THB_BLOCKED)
> 			break;
>
> 		mutex_unlock(&con->lock);
> 	}
> }
>
> void single_console_unlock(struct console *con)
> {
> 	mutex_unlock(&con->lock);
> }
>
> We should use it everywhere instead of the simple mutex_lock(con->lock)
> and mutex_lock(con->lock). And we could remove mutex_lock()/unlock()
> from code called under the big console_lock().

Hmmm. Waiting on @log_wait is not correct. A @log_wait wakeup with the
kthread already in the blocked state is unusual. There would need to be
a per-console waitqueue for when the kthread unlocks its mutex.

Maybe something like:

void single_console_lock(struct console *con)
{
	for (;;) {
		error = wait_event_interruptible(con->lock_wait,
				!(con->flags & CON_THB_BLOCKED));
		if (error)
			continue;

		mutex_lock(&con->lock);

		if (!(con->flags & CON_THB_BLOCKED))
			break;

		mutex_unlock(&con->lock);
	}
}

And in printk_kthread_func(), after the kthread unlocks its con->lock,
it calls:

if (wq_has_sleeper(&con->lock_wait))
	wake_up_interruptible_all(&con->lock_wait);

But single_console_lock() would not be allowed to be called under
console_lock(), so I don't see how it is useful. con->flags is always
modified under @console_sem to make sure the console does not disappear.

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.

John

  reply	other threads:[~2022-04-22 14:15 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 [this message]
2022-04-22 15:15           ` Petr Mladek
2022-04-22 21:25             ` John Ogness
2022-04-25 15:18               ` Petr Mladek
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=87o80tp5lv.fsf@jogness.linutronix.de \
    --to=john.ogness@linutronix.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmladek@suse.com \
    --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.