[PATCH printk v2 31/38] printk: register_console: use srcu console list iterator

From: John Ogness
Date: Wed Oct 19 2022 - 11:07:36 EST


Use srcu console list iteration for console list traversal. Now
the traversal at the beginning of register_console() is safe.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
kernel/printk/printk.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 410ad9d5649c..809c43e596d8 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3188,20 +3188,23 @@ void register_console(struct console *newcon)
struct console *con;
bool bootcon_enabled = false;
bool realcon_enabled = false;
+ int cookie;
int err;

- for_each_console(con) {
+ cookie = console_srcu_read_lock();
+ for_each_console_srcu(con) {
if (WARN(con == newcon, "console '%s%d' already registered\n",
- con->name, con->index))
+ con->name, con->index)) {
+ console_srcu_read_unlock(cookie);
return;
- }
+ }

- for_each_console(con) {
if (con->flags & CON_BOOT)
bootcon_enabled = true;
else
realcon_enabled = true;
}
+ console_srcu_read_unlock(cookie);

/* Do not register boot consoles when there already is a real one. */
if (newcon->flags & CON_BOOT && realcon_enabled) {
--
2.30.2