[printk,v3,19/40] printk: console_device: use srcu console list iterator

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

Commit Message

John Ogness Nov. 7, 2022, 2:16 p.m. UTC
  Use srcu console list iteration for console list traversal.

Document why the console_lock is still necessary. Note that this
is a preparatory change for when console_lock no longer provides
synchronization for the console list.

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

Comments

Petr Mladek Nov. 9, 2022, 3:58 p.m. UTC | #1
On Mon 2022-11-07 15:22:17, John Ogness wrote:
> Use srcu console list iteration for console list traversal.

I wonder if it would make sense to describe why this is acceptable.
But I am not sure how to explain this a reasonable way. I am afraid
that it might cause more confusion.

I would write something like:

<proposal>
It is acceptable because the consoles might come and go at any time.
Strict synchronizing with console registration code would not bring
any advantage over RCU.
</proposal>

> Document why the console_lock is still necessary. Note that this
> is a preparatory change for when console_lock no longer provides
> synchronization for the console list.
> 
> Signed-off-by: John Ogness <john.ogness@linutronix.de>

As I said, I do not have strong opinion if the extra explanation
would help. So, with or without it:

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

Best Regards,
Petr
  

Patch

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index de86a502b50b..ec50777d0301 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3029,15 +3029,25 @@  struct tty_driver *console_device(int *index)
 {
 	struct console *c;
 	struct tty_driver *driver = NULL;
+	int cookie;
 
+	/*
+	 * Take console_lock to serialize device() callback with
+	 * other console operations. For example, fg_console is
+	 * modified under console_lock when switching vt.
+	 */
 	console_lock();
-	for_each_console(c) {
+
+	cookie = console_srcu_read_lock();
+	for_each_console_srcu(c) {
 		if (!c->device)
 			continue;
 		driver = c->device(c, index);
 		if (driver)
 			break;
 	}
+	console_srcu_read_unlock(cookie);
+
 	console_unlock();
 	return driver;
 }