Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Revert "init/console: Use ttynull as a fallback when there is no console"

This reverts commit 757055ae8dedf5333af17b3b5b4b70ba9bc9da4e.

The commit caused that ttynull was used as the default console
on several systems[1][2][3]. As a result, the console was
blank even when a better alternative existed.

It happened when there was no console configured
on the command line and ttynull_init() was the first initcall
calling register_console().

Or it happened when /dev/ did not exist when console_on_rootfs()
was called. It was not able to open /dev/console even though
a console driver was registered. It tried to add ttynull console
but it obviously did not help. But ttynull became the preferred
console and was used by /dev/console when it was available later.

The commit tried to fix a historical problem that have been there
for ages. The primary motivation was the commit 3cffa06aeef7ece30f6
("printk/console: Allow to disable console output by using console=""
or console=null"). It provided a clean solution for a workaround
that was widely used and worked only by chance.

This revert causes that the console="" or console=null command line
options will again work only by chance. These options will cause that
a particular console will be preferred and the default (tty) ones
will not get enabled. There will be no console registered at
all. As a result there won't be stdin, stdout, and stderr for
the init process. But it worked exactly this way even before.

The proper solution has to fulfill many conditions:

+ Register ttynull only when explicitly required or as
the ultimate fallback.

+ ttynull should get associated with /dev/console but it must
not become preferred console when used as a fallback.
Especially, it must still be possible to replace it
by a better console later.

Such a change requires clean up of the register_console() code.
Otherwise, it would be even harder to follow. Especially, the use
of has_preferred_console and CON_CONSDEV flag is tricky. The clean
up is risky. The ordering of consoles is not well defined. And
any changes tend to break existing user settings.

Do the revert at the least risky solution for now.

[1] https://lore.kernel.org/linux-kselftest/20201221144302.GR4077@smile.fi.intel.com/
[2] https://lore.kernel.org/lkml/d2a3b3c0-e548-7dd1-730f-59bc5c04e191@synopsys.com/
[3] https://patchwork.ozlabs.org/project/linux-um/patch/20210105120128.10854-1-thomas@m3y3r.de/

Reported-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reported-by: Vineet Gupta <vgupta@synopsys.com>
Reported-by: Thomas Meyer <thomas@m3y3r.de>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Petr Mladek and committed by
Linus Torvalds
a91bd622 c4cc3b1d

+18 -30
+14
drivers/tty/Kconfig
··· 401 401 help 402 402 FDC channel number to use for KGDB. 403 403 404 + config NULL_TTY 405 + tristate "NULL TTY driver" 406 + help 407 + Say Y here if you want a NULL TTY which simply discards messages. 408 + 409 + This is useful to allow userspace applications which expect a console 410 + device to work without modifications even when no console is 411 + available or desired. 412 + 413 + In order to use this driver, you should redirect the console to this 414 + TTY, or boot the kernel with console=ttynull. 415 + 416 + If unsure, say N. 417 + 404 418 config TRACE_ROUTER 405 419 tristate "Trace data router for MIPI P1149.7 cJTAG standard" 406 420 depends on TRACE_SINK
+2 -1
drivers/tty/Makefile
··· 2 2 obj-$(CONFIG_TTY) += tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o \ 3 3 tty_buffer.o tty_port.o tty_mutex.o \ 4 4 tty_ldsem.o tty_baudrate.o tty_jobctrl.o \ 5 - n_null.o ttynull.o 5 + n_null.o 6 6 obj-$(CONFIG_LEGACY_PTYS) += pty.o 7 7 obj-$(CONFIG_UNIX98_PTYS) += pty.o 8 8 obj-$(CONFIG_AUDIT) += tty_audit.o ··· 25 25 obj-$(CONFIG_MOXA_INTELLIO) += moxa.o 26 26 obj-$(CONFIG_MOXA_SMARTIO) += mxser.o 27 27 obj-$(CONFIG_NOZOMI) += nozomi.o 28 + obj-$(CONFIG_NULL_TTY) += ttynull.o 28 29 obj-$(CONFIG_ROCKETPORT) += rocket.o 29 30 obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o 30 31 obj-$(CONFIG_PPC_EPAPR_HV_BYTECHAN) += ehv_bytechan.o
-18
drivers/tty/ttynull.c
··· 2 2 /* 3 3 * Copyright (C) 2019 Axis Communications AB 4 4 * 5 - * The console is useful for userspace applications which expect a console 6 - * device to work without modifications even when no console is available 7 - * or desired. 8 - * 9 - * In order to use this driver, you should redirect the console to this 10 - * TTY, or boot the kernel with console=ttynull. 11 - * 12 5 * Based on ttyprintk.c: 13 6 * Copyright (C) 2010 Samo Pogacnik 14 7 */ ··· 58 65 .name = "ttynull", 59 66 .device = ttynull_device, 60 67 }; 61 - 62 - void __init register_ttynull_console(void) 63 - { 64 - if (!ttynull_driver) 65 - return; 66 - 67 - if (add_preferred_console(ttynull_console.name, 0, NULL)) 68 - return; 69 - 70 - register_console(&ttynull_console); 71 - } 72 68 73 69 static int __init ttynull_init(void) 74 70 {
-3
include/linux/console.h
··· 186 186 extern int braille_unregister_console(struct console *); 187 187 #ifdef CONFIG_TTY 188 188 extern void console_sysfs_notify(void); 189 - extern void register_ttynull_console(void); 190 189 #else 191 190 static inline void console_sysfs_notify(void) 192 - { } 193 - static inline void register_ttynull_console(void) 194 191 { } 195 192 #endif 196 193 extern bool console_suspend_enabled;
+2 -8
init/main.c
··· 1480 1480 struct file *file = filp_open("/dev/console", O_RDWR, 0); 1481 1481 1482 1482 if (IS_ERR(file)) { 1483 - pr_err("Warning: unable to open an initial console. Fallback to ttynull.\n"); 1484 - register_ttynull_console(); 1485 - 1486 - file = filp_open("/dev/console", O_RDWR, 0); 1487 - if (IS_ERR(file)) { 1488 - pr_err("Warning: Failed to add ttynull console. No stdin, stdout, and stderr for the init process!\n"); 1489 - return; 1490 - } 1483 + pr_err("Warning: unable to open an initial console.\n"); 1484 + return; 1491 1485 } 1492 1486 init_dup(file); 1493 1487 init_dup(file);