[patch RFC 10/29] kgbd: Pretend that console list walk is safe

From: Thomas Gleixner
Date: Sat Sep 10 2022 - 18:28:43 EST


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>
---
drivers/tty/serial/kgdboc.c | 5 ++++-
include/linux/console.h | 12 +++++++++++-
kernel/debug/kdb/kdb_io.c | 7 ++++++-
3 files changed, 21 insertions(+), 3 deletions(-)

--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -462,10 +462,13 @@ static void kgdboc_earlycon_pre_exp_hand
* 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");
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -184,7 +184,17 @@ extern void console_list_unlock(void);
* Requires console_lock to be held which guarantees that the
* list is immutable.
*/
-#define for_each_console(con) \
+#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;
--- 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 *ms
cp++;
}

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