[printk,v2,32/38] printk: __pr_flush: use srcu console list iterator

Message ID 20221019145600.1282823-33-john.ogness@linutronix.de
State New
Headers
Series reduce console_lock scope |

Commit Message

John Ogness Oct. 19, 2022, 2:55 p.m. UTC
  Use srcu console list iteration for console list traversal.

Document why the console_lock is still necessary.

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

Comments

Petr Mladek Oct. 26, 2022, 8:33 a.m. UTC | #1
On Wed 2022-10-19 17:01:54, John Ogness wrote:
> Use srcu console list iteration for console list traversal.
>
> Document why the console_lock is still necessary.

When thinking about it. It might be worth explaining why the srcu
traversal is added even when console_lock is still needed.
It would make the life of code archaeologists easier.

I mean to add a note that it will keep the traversal safe
even after console_drivers list manipulation will not longer be
serialized using console_lock(). Or something like this.

It would be nice to add this to all other patches the introduced
srcu and kept console_lock.

> Signed-off-by: John Ogness <john.ogness@linutronix.de>

Anyway, the change looks good. Feel free to use:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
  

Patch

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 809c43e596d8..2faa6e3e2fb8 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3454,6 +3454,7 @@  static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
 	struct console *c;
 	u64 last_diff = 0;
 	u64 printk_seq;
+	int cookie;
 	u64 diff;
 	u64 seq;
 
@@ -3464,9 +3465,15 @@  static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
 	for (;;) {
 		diff = 0;
 
+		/*
+		 * Hold the console_lock to guarantee safe access to
+		 * console->seq and to prevent changes to @console_suspended
+		 * until all consoles have been processed.
+		 */
 		console_lock();
 
-		for_each_console(c) {
+		cookie = console_srcu_read_lock();
+		for_each_console_srcu(c) {
 			if (con && con != c)
 				continue;
 			if (!console_is_usable(c))
@@ -3475,6 +3482,7 @@  static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
 			if (printk_seq < seq)
 				diff += seq - printk_seq;
 		}
+		console_srcu_read_unlock(cookie);
 
 		/*
 		 * If consoles are suspended, it cannot be expected that they