[PATCH] RFC: init/main: wait up to 10 second for a console to show up

Martin Roukala (né Peres) posted 1 patch 3 weeks ago
init/main.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
[PATCH] RFC: init/main: wait up to 10 second for a console to show up
Posted by Martin Roukala (né Peres) 3 weeks ago
There are some devices (such as tablets, handheld gaming devices, ...)
that do not have a readily-available serial port. Our only option for
a tty device is thus to use a USB-based one.

The issue with USB-based tty devices is that device enumeration takes
time, and thus the console may not have been registered by the time
init tries to open `/dev/console`, leading to a kernel panic...

Make init wait a valid console for up to 10s before panicing, giving
more time for the consoles to show up.

Signed-off-by: Martin Roukala (né Peres) <martin.roukala@mupuf.org>
---
What do you guys think of this solution? Is this something we do by
default? Should we make it a config option?
---
 init/main.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/init/main.c b/init/main.c
index 1cb395dd94e4..6ecc2b52545f 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1648,7 +1648,18 @@ static int __ref kernel_init(void *unused)
 /* Open /dev/console, for stdin/stdout/stderr, this should never fail */
 void __init console_on_rootfs(void)
 {
-	struct file *file = filp_open("/dev/console", O_RDWR, 0);
+	struct file *file = NULL;
+	int i = 0;
+
+	/* Wait for up to 10 seconds for a console to appear. This makes it
+	 * possible to reliably use USB-backed TTY consoles as system consoles
+	 * (ie. `console=ttyUSB0,115200`).
+	 */
+	do {
+		file = filp_open("/dev/console", O_RDWR, 0);
+		if (IS_ERR(file))
+			msleep(100);
+	} while (IS_ERR(file) && ++i < 100);
 
 	if (IS_ERR(file)) {
 		pr_err("Warning: unable to open an initial console.\n");

---
base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
change-id: 20260316-init_main_wait_for_console-837b875258da

Best regards,
-- 
Martin Roukala (né Peres) <martin.roukala@mupuf.org>