[PATCH printk 10/18] kgbd: Pretend that console list walk is safe

From: John Ogness
Date: Fri Sep 23 2022 - 20:06:05 EST


From: Thomas Gleixner <tglx@xxxxxxxxxxxxx>

Provide a special list iterator macro for KGDB to allow unprotected list
walks and add a few comments to explain the hope based approach.

Preperatory change for changing the console list to hlist and adding
lockdep asserts to regular list walks.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
Reviewed-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
drivers/tty/serial/kgdboc.c | 5 ++++-
include/linux/console.h | 10 ++++++++++
kernel/debug/kdb/kdb_io.c | 7 ++++++-
3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index af2aa76bae15..57a5fd27dffe 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -462,10 +462,13 @@ static void kgdboc_earlycon_pre_exp_handler(void)
* we have no other choice so we keep using it. Since not all
* serial drivers might be OK with this, print a warning once per
* boot if we detect this case.
+ *
+ * Pretend that walking the console list is safe...
*/
- for_each_console(con)
+ for_each_console_kgdb(con) {
if (con == kgdboc_earlycon_io_ops.cons)
return;
+ }

already_warned = true;
pr_warn("kgdboc_earlycon is still using bootconsole\n");
diff --git a/include/linux/console.h b/include/linux/console.h
index 24344f9b0bc1..86a6125512b9 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -187,6 +187,16 @@ extern void console_list_unlock(void) __releases(console_mutex);
#define for_each_console(con) \
for (con = console_drivers; con != NULL; con = con->next)

+/**
+ * for_each_console_kgdb() - Iterator over registered consoles for KGDB
+ * @con: struct console pointer used as loop cursor
+ *
+ * Has no serialization requirements and KGDB pretends that this is safe.
+ * Don't use outside of the KGDB fairy tale land!
+ */
+#define for_each_console_kgdb(con) \
+ for (con = console_drivers; con != NULL; con = con->next)
+
extern int console_set_on_cmdline;
extern struct console *early_console;

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index 67d3c48a1522..fb3775e61a3b 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -558,7 +558,12 @@ static void kdb_msg_write(const char *msg, int msg_len)
cp++;
}

- for_each_console(c) {
+ /*
+ * This is a completely unprotected list walk designed by the
+ * wishful thinking department. See the oops_in_progress comment
+ * below - especially the encourage section...
+ */
+ for_each_console_kgdb(c) {
if (!(c->flags & CON_ENABLED))
continue;
if (c == dbg_io_ops->cons)
--
2.30.2