mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for John Ogness" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: John Ogness <john.ogness@linutronix.de>,
	Petr Mladek <pmladek@suse.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: sched/rt] printk: nbcon: Clarify rules of the owner/waiter matching
Date: Mon, 09 Sep 2024 17:27:58 -0000	[thread overview]
Message-ID: <172590287874.2215.1571832259655722622.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20240820063001.36405-6-john.ogness@linutronix.de>

The following commit has been merged into the sched/rt branch of tip:

Commit-ID:     8c9dab2c55ad74680728c72949971b20d70b56ca
Gitweb:        https://git.kernel.org/tip/8c9dab2c55ad74680728c72949971b20d70b56ca
Author:        John Ogness <john.ogness@linutronix.de>
AuthorDate:    Tue, 20 Aug 2024 08:35:31 +02:06
Committer:     Petr Mladek <pmladek@suse.com>
CommitterDate: Wed, 21 Aug 2024 14:56:22 +02:00

printk: nbcon: Clarify rules of the owner/waiter matching

The functions nbcon_owner_matches() and nbcon_waiter_matches()
use a minimal set of data to determine if a context matches.
The existing kerneldoc and comments were not clear enough and
caused the printk folks to re-prove that the functions are
indeed reliable in all cases.

Update and expand the explanations so that it is clear that the
implementations are sufficient for all cases.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20240820063001.36405-6-john.ogness@linutronix.de
Signed-off-by: Petr Mladek <pmladek@suse.com>
---
 kernel/printk/nbcon.c | 56 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 776746d..931b8f0 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -228,6 +228,13 @@ static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,
 	struct nbcon_state new;
 
 	do {
+		/*
+		 * Panic does not imply that the console is owned. However, it
+		 * is critical that non-panic CPUs during panic are unable to
+		 * acquire ownership in order to satisfy the assumptions of
+		 * nbcon_waiter_matches(). In particular, the assumption that
+		 * lower priorities are ignored during panic.
+		 */
 		if (other_cpu_in_panic())
 			return -EPERM;
 
@@ -259,18 +266,29 @@ static bool nbcon_waiter_matches(struct nbcon_state *cur, int expected_prio)
 	/*
 	 * The request context is well defined by the @req_prio because:
 	 *
-	 * - Only a context with a higher priority can take over the request.
+	 * - Only a context with a priority higher than the owner can become
+	 *   a waiter.
+	 * - Only a context with a priority higher than the waiter can
+	 *   directly take over the request.
 	 * - There are only three priorities.
 	 * - Only one CPU is allowed to request PANIC priority.
 	 * - Lower priorities are ignored during panic() until reboot.
 	 *
 	 * As a result, the following scenario is *not* possible:
 	 *
-	 * 1. Another context with a higher priority directly takes ownership.
-	 * 2. The higher priority context releases the ownership.
-	 * 3. A lower priority context takes the ownership.
-	 * 4. Another context with the same priority as this context
+	 * 1. This context is currently a waiter.
+	 * 2. Another context with a higher priority than this context
+	 *    directly takes ownership.
+	 * 3. The higher priority context releases the ownership.
+	 * 4. Another lower priority context takes the ownership.
+	 * 5. Another context with the same priority as this context
 	 *    creates a request and starts waiting.
+	 *
+	 * Event #1 implies this context is EMERGENCY.
+	 * Event #2 implies the new context is PANIC.
+	 * Event #3 occurs when panic() has flushed the console.
+	 * Events #4 and #5 are not possible due to the other_cpu_in_panic()
+	 * check in nbcon_context_try_acquire_direct().
 	 */
 
 	return (cur->req_prio == expected_prio);
@@ -578,11 +596,29 @@ static bool nbcon_owner_matches(struct nbcon_state *cur, int expected_cpu,
 				int expected_prio)
 {
 	/*
-	 * Since consoles can only be acquired by higher priorities,
-	 * owning contexts are uniquely identified by @prio. However,
-	 * since contexts can unexpectedly lose ownership, it is
-	 * possible that later another owner appears with the same
-	 * priority. For this reason @cpu is also needed.
+	 * A similar function, nbcon_waiter_matches(), only deals with
+	 * EMERGENCY and PANIC priorities. However, this function must also
+	 * deal with the NORMAL priority, which requires additional checks
+	 * and constraints.
+	 *
+	 * For the case where preemption and interrupts are disabled, it is
+	 * enough to also verify that the owning CPU has not changed.
+	 *
+	 * For the case where preemption or interrupts are enabled, an
+	 * external synchronization method *must* be used. In particular,
+	 * the driver-specific locking mechanism used in device_lock()
+	 * (including disabling migration) should be used. It prevents
+	 * scenarios such as:
+	 *
+	 * 1. [Task A] owns a context with NBCON_PRIO_NORMAL on [CPU X] and
+	 *    is scheduled out.
+	 * 2. Another context takes over the lock with NBCON_PRIO_EMERGENCY
+	 *    and releases it.
+	 * 3. [Task B] acquires a context with NBCON_PRIO_NORMAL on [CPU X]
+	 *    and is scheduled out.
+	 * 4. [Task A] gets running on [CPU X] and sees that the console is
+	 *    still owned by a task on [CPU X] with NBON_PRIO_NORMAL. Thus
+	 *    [Task A] thinks it is the owner when it is not.
 	 */
 
 	if (cur->prio != expected_prio)

  reply	other threads:[~2024-09-09 17:28 UTC|newest]

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

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=172590287874.2215.1571832259655722622.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=x86@kernel.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®