linux-kernel.vger.kernel.org archive mirror
 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
Subject: [PATCH printk v2 27/38] printk: console_flush_all: use srcu console list iterator
Date: Wed, 19 Oct 2022 17:01:49 +0206	[thread overview]
Message-ID: <20221019145600.1282823-28-john.ogness@linutronix.de> (raw)
In-Reply-To: <20221019145600.1282823-1-john.ogness@linutronix.de>

Guarantee safe iteration of the console list by using SRCU.

Note that in the case of a handover, the SRCU read lock is also
released. This is documented in the function description and as
comments in the code. It is a bit tricky, but this preserves the
lockdep lock ordering for the context handing over the
console_lock:

  console_lock()
  | mutex_acquire(&console_lock_dep_map)       <-- console lock
  |
  console_unlock()
  | console_flush_all()
  | | srcu_read_lock(&console_srcu)            <-- srcu lock
  | | console_emit_next_record()
  | | | console_lock_spinning_disable_and_check()
  | | | | srcu_read_unlock(&console_srcu)      <-- srcu unlock
  | | | | mutex_release(&console_lock_dep_map) <-- console unlock

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 kernel/printk/printk.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 7ff2fc75fc3b..c4d5d58b5977 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -1851,7 +1851,7 @@ static void console_lock_spinning_enable(void)
  *
  * Return: 1 if the lock rights were passed, 0 otherwise.
  */
-static int console_lock_spinning_disable_and_check(void)
+static int console_lock_spinning_disable_and_check(int cookie)
 {
 	int waiter;
 
@@ -1870,6 +1870,12 @@ static int console_lock_spinning_disable_and_check(void)
 
 	spin_release(&console_owner_dep_map, _THIS_IP_);
 
+	/*
+	 * Preserve lockdep lock ordering. Release the SRCU read lock before
+	 * releasing the console_lock.
+	 */
+	console_srcu_read_unlock(cookie);
+
 	/*
 	 * Hand off console_lock to waiter. The waiter will perform
 	 * the up(). After this, the waiter is the console_lock owner.
@@ -2353,7 +2359,7 @@ static ssize_t msg_print_ext_body(char *buf, size_t size,
 				  char *text, size_t text_len,
 				  struct dev_printk_info *dev_info) { return 0; }
 static void console_lock_spinning_enable(void) { }
-static int console_lock_spinning_disable_and_check(void) { return 0; }
+static int console_lock_spinning_disable_and_check(int cookie) { return 0; }
 static void call_console_driver(struct console *con, const char *text, size_t len,
 				char *dropped_text)
 {
@@ -2695,8 +2701,8 @@ static void __console_unlock(void)
  * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL.
  *
  * @handover will be set to true if a printk waiter has taken over the
- * console_lock, in which case the caller is no longer holding the
- * console_lock. Otherwise it is set to false.
+ * console_lock, in which case the caller is no longer holding both the
+ * console_lock and the SRCU read lock. Otherwise it is set to false.
  *
  * Returns false if the given console has no next record to print, otherwise
  * true.
@@ -2704,7 +2710,7 @@ static void __console_unlock(void)
  * Requires the console_lock.
  */
 static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
-				     char *dropped_text, bool *handover)
+				     char *dropped_text, bool *handover, int cookie)
 {
 	static int panic_console_dropped;
 	struct printk_info info;
@@ -2764,7 +2770,7 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_
 
 	con->seq++;
 
-	*handover = console_lock_spinning_disable_and_check();
+	*handover = console_lock_spinning_disable_and_check(cookie);
 	printk_safe_exit_irqrestore(flags);
 skip:
 	return true;
@@ -2801,6 +2807,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
 	bool any_usable = false;
 	struct console *con;
 	bool any_progress;
+	int cookie;
 
 	*next_seq = 0;
 	*handover = false;
@@ -2808,7 +2815,8 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
 	do {
 		any_progress = false;
 
-		for_each_console(con) {
+		cookie = console_srcu_read_lock();
+		for_each_console_srcu(con) {
 			bool progress;
 
 			if (!console_is_usable(con))
@@ -2819,12 +2827,17 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
 				/* Extended consoles do not print "dropped messages". */
 				progress = console_emit_next_record(con, &text[0],
 								    &ext_text[0], NULL,
-								    handover);
+								    handover, cookie);
 			} else {
 				progress = console_emit_next_record(con, &text[0],
 								    NULL, &dropped_text[0],
-								    handover);
+								    handover, cookie);
 			}
+
+			/*
+			 * If a handover has occurred, the SRCU read lock
+			 * is already released.
+			 */
 			if (*handover)
 				return false;
 
@@ -2838,14 +2851,19 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
 
 			/* Allow panic_cpu to take over the consoles safely. */
 			if (abandon_console_lock_in_panic())
-				return false;
+				goto abandon;
 
 			if (do_cond_resched)
 				cond_resched();
 		}
+		console_srcu_read_unlock(cookie);
 	} while (any_progress);
 
 	return any_usable;
+
+abandon:
+	console_srcu_read_unlock(cookie);
+	return false;
 }
 
 /**
-- 
2.30.2


  parent reply	other threads:[~2022-10-19 15:07 UTC|newest]

Thread overview: 129+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-19 14:55 [PATCH printk v2 00/38] reduce console_lock scope John Ogness
2022-10-19 14:55 ` [PATCH printk v2 01/38] serial: kgdboc: Lock console list in probe function John Ogness
2022-10-19 15:41   ` Greg Kroah-Hartman
2022-10-24  5:22   ` Sergey Senozhatsky
2022-10-25  0:34   ` Doug Anderson
2022-10-19 14:55 ` [PATCH printk v2 02/38] printk: Convert console_drivers list to hlist John Ogness
2022-10-19 15:44   ` Greg Kroah-Hartman
2022-10-19 21:46     ` John Ogness
2022-10-20  7:43       ` Greg Kroah-Hartman
2022-10-20 12:36   ` Petr Mladek
2022-10-24  5:23   ` Sergey Senozhatsky
2022-10-19 14:55 ` [PATCH printk v2 03/38] printk: Prepare for SRCU console list protection John Ogness
2022-10-19 15:16   ` Miguel Ojeda
2022-10-19 17:12   ` Paul E. McKenney
2022-10-21  8:34   ` Petr Mladek
2022-10-31 13:06     ` John Ogness
2022-10-31 14:09       ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 04/38] printk: introduce console_is_enabled() wrapper John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21  8:57   ` Petr Mladek
2022-10-21  9:37     ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 05/38] printk: use console_is_enabled() John Ogness
2022-10-21  9:25   ` Petr Mladek
2022-10-31 15:39     ` John Ogness
2022-10-19 14:55 ` [PATCH printk v2 06/38] tty: nfcon: " John Ogness
2022-10-21  9:55   ` Petr Mladek
2022-10-31 15:59     ` John Ogness
2022-11-01  8:57       ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 07/38] um: kmsg_dump: " John Ogness
2022-10-21 12:46   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 08/38] efi: earlycon: " John Ogness
2022-10-19 15:32   ` Ard Biesheuvel
2022-10-21 12:53   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 09/38] netconsole: " John Ogness
2022-10-21 13:14   ` Petr Mladek
2022-11-04 15:12     ` John Ogness
2022-10-19 14:55 ` [PATCH printk v2 10/38] tty: hvc: " John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21 13:22   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 11/38] tty: serial: earlycon: " John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21 13:51   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 12/38] tty: serial: kgdboc: " John Ogness
2022-10-19 16:00   ` Greg Kroah-Hartman
2022-10-21 14:10   ` Petr Mladek
2022-10-24 22:46   ` Doug Anderson
2022-10-25  0:49     ` Doug Anderson
2022-10-25 11:28       ` John Ogness
2022-11-04 16:23         ` John Ogness
2022-11-07  8:47           ` Petr Mladek
2022-11-07  9:45             ` John Ogness
2022-10-19 14:55 ` [PATCH printk v2 13/38] tty: serial: pic32_uart: " John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21 14:11   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 14/38] tty: serial: samsung_tty: " John Ogness
2022-10-19 16:00   ` Greg Kroah-Hartman
2022-10-21 14:14   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 15/38] tty: serial: serial_core: " John Ogness
2022-10-19 16:00   ` Greg Kroah-Hartman
2022-10-21 14:14   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 16/38] tty: serial: xilinx_uartps: " John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21 14:23   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 17/38] tty: tty_io: " John Ogness
2022-10-19 16:00   ` Greg Kroah-Hartman
2022-10-21 14:24   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 18/38] usb: early: xhci-dbc: " John Ogness
2022-10-19 16:01   ` Greg Kroah-Hartman
2022-10-21 14:27   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 19/38] kdb: kdb_io: " John Ogness
2022-10-21 14:28   ` Petr Mladek
2022-10-25  0:34   ` Doug Anderson
2022-10-19 14:55 ` [PATCH printk v2 20/38] um: kmsg_dumper: use srcu console list iterator John Ogness
2022-10-21 14:56   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 21/38] serial: kgdboc: " John Ogness
2022-10-19 16:02   ` Greg Kroah-Hartman
2022-10-21 15:09   ` Petr Mladek
2022-10-25  0:33     ` Doug Anderson
2022-10-19 14:55 ` [PATCH printk v2 22/38] serial: kgdboc: document console_lock usage John Ogness
2022-10-20  7:42   ` Greg Kroah-Hartman
2022-10-25  0:36   ` Doug Anderson
2022-10-25 10:09   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 23/38] tty: tty_io: " John Ogness
2022-10-20  7:43   ` Greg Kroah-Hartman
2022-10-25 13:31   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 24/38] xen: fbfront: use srcu console list iterator John Ogness
2022-10-25 13:39   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 25/38] proc: consoles: document console_lock usage John Ogness
2022-10-25 14:40   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 26/38] kdb: use srcu console list iterator John Ogness
2022-10-25  0:47   ` Doug Anderson
2022-10-25 14:59     ` Petr Mladek
2022-10-19 14:55 ` John Ogness [this message]
2022-10-25 15:17   ` [PATCH printk v2 27/38] printk: console_flush_all: " Petr Mladek
2022-11-07  0:00     ` John Ogness
2022-11-07 13:03       ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 28/38] printk: console_unblank: " John Ogness
2022-10-25 15:28   ` Petr Mladek
2022-10-25 15:31   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 29/38] printk: console_flush_on_panic: " John Ogness
2022-10-25 15:32   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 30/38] printk: console_device: " John Ogness
2022-10-25 15:35   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 31/38] printk: register_console: " John Ogness
2022-10-26  8:20   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 32/38] printk: __pr_flush: " John Ogness
2022-10-26  8:33   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 33/38] printk: introduce console_list_lock John Ogness
2022-10-20  7:53   ` Greg Kroah-Hartman
2022-10-27 10:09   ` Petr Mladek
2022-10-27 18:50     ` Paul E. McKenney
2022-10-28 18:09       ` Boqun Feng
2022-10-28 18:42         ` Paul E. McKenney
2022-11-07 10:10       ` John Ogness
2022-11-07 16:16         ` Paul E. McKenney
2022-10-19 14:55 ` [PATCH printk v2 34/38] serial: kgdboc: use console_list_lock instead of console_lock John Ogness
2022-10-20  7:52   ` Greg Kroah-Hartman
2022-10-27 10:13   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 35/38] tty: tty_io: use console_list_lock for list synchronization John Ogness
2022-10-20  7:43   ` Greg Kroah-Hartman
2022-10-27 10:17   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 36/38] proc: consoles: use console_list_lock for list iteration John Ogness
2022-10-27 12:02   ` Petr Mladek
2022-10-19 14:55 ` [PATCH printk v2 37/38] printk: relieve console_lock of list synchronization duties John Ogness
2022-10-27 12:40   ` Petr Mladek
2022-10-19 14:56 ` [PATCH printk v2 38/38] printk, xen: fbfront: create/use safe function for forcing preferred John Ogness
2022-10-27 13:18   ` Petr Mladek
2022-10-27 13:35     ` John Ogness
2022-10-27 14:27       ` Petr Mladek

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=20221019145600.1282823-28-john.ogness@linutronix.de \
    --to=john.ogness@linutronix.de \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).