Re: [PATCH] kgdboc: Be a bit more robust about handling earlycon leaving
From: Daniel Thompson
Date: Fri May 01 2020 - 09:32:13 EST
On Fri, May 01, 2020 at 12:49:43PM +0100, Daniel Thompson wrote:
> On Thu, Apr 30, 2020 at 09:59:09AM -0700, Douglas Anderson wrote:
> > The original implementation of kgdboc_earlycon included a comment
> > about how it was impossible to get notified about the boot console
> > going away without making changes to the Linux core. Since folks
> > often don't want to change the Linux core for kgdb's purposes, the
> > kgdboc_earlycon implementation did a bit of polling to figure out when
> > the boot console went away.
> >
> > It turns out, though, that it is possible to get notified about the
> > boot console going away. The solution is either clever or a hack
> > depending on your viewpoint. ...or, perhaps, a clever hack. All we
> > need to do is head-patch the "exit" routine of the boot console. We
> > know that "struct console" must be writable because it has a "next"
> > pointer in it, so we can just put our own exit routine in, do our
> > stuff, and then call back to the original.
>
> I think I'm in the hack camp on this one!
>
>
> > This works great to get notified about the boot console going away.
> > The (slight) problem is that in the context of the boot console's exit
> > routine we can't call tty_find_polling_driver().
>
> I presume this is something to do with the tty_mutex?
>
>
> > We solve this by
> > kicking off some work on the system_wq when we get notified and this
> > works pretty well.
>
> There are some problems with the workqueue approach.
... so did a couple of experiments to avoid the workqueue.
It occured to me that, since we have interfered with deinit() then the
console hasn't actually been uninitialized meaning we could still use it.
This does exposes us to risks similar to keep_bootcon but in exchange
there is no window where kgdb is broken (and no need to panic).
My prototype is minimal but I did wonder about ripping out all the
code to defend against removal of the earlycon and simply keep the
earlycon around until a new kgdbio handler is installed.
Daniel.
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index 596213272ec3..05d58f9f38b1 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -46,6 +46,7 @@ static struct platform_device *kgdboc_pdev;
#ifdef CONFIG_KGDB_SERIAL_CONSOLE
static struct kgdb_io kgdboc_earlycon_io_ops;
struct console *earlycon;
+static int (*earlycon_orig_exit)(struct console *con);
#else /* ! CONFIG_KGDB_SERIAL_CONSOLE */
#define earlycon NULL
#endif /* ! CONFIG_KGDB_SERIAL_CONSOLE */
@@ -478,25 +479,28 @@ static void kgdboc_earlycon_put_char(u8 chr)
earlycon->write(earlycon, &chr, 1);
}
-static void kgdboc_earlycon_pre_exp_handler(void)
+static int kgdboc_earlycon_deferred_exit(struct console *con)
{
/*
- * We don't get notified when the boot console is unregistered.
- * Double-check when we enter the debugger. Unfortunately we
- * can't really unregister ourselves now, so we panic. We rely
- * on kgdb's ability to detect re-entrancy to make the panic
- * take effect.
- *
- * NOTE: if you're here in the lull when the real console has
- * replaced the boot console but our init hasn't run yet it's
- * possible that the "keep_bootcon" argument may help.
+ * restoring the exit function ensures the earlycon is
+ * deinitialized when/if we find a suitable tty
*/
- if (earlycon && !is_earlycon_still_valid())
- panic("KGDB earlycon vanished and nothing replaced it\n");
+ con->exit = earlycon_orig_exit;
+
+ return 0;
}
static void kgdboc_earlycon_deinit(void)
{
+ if (!earlycon)
+ return;
+
+ if (earlycon->exit == kgdboc_earlycon_deferred_exit) {
+ earlycon->exit = earlycon_orig_exit;
+ } else if (earlycon->exit) {
+ earlycon->exit(earlycon);
+ }
+
earlycon = NULL;
}
@@ -504,7 +508,6 @@ static struct kgdb_io kgdboc_earlycon_io_ops = {
.name = "kgdboc_earlycon",
.read_char = kgdboc_earlycon_get_char,
.write_char = kgdboc_earlycon_put_char,
- .pre_exception = kgdboc_earlycon_pre_exp_handler,
.deinit = kgdboc_earlycon_deinit,
.is_console = true,
};
@@ -562,6 +565,9 @@ static int __init kgdboc_earlycon_init(char *opt)
return 0;
}
+ earlycon_orig_exit = con->exit;
+ con->exit = kgdboc_earlycon_deferred_exit;
+
return 0;
}
early_param("kgdboc_earlycon", kgdboc_earlycon_init);