All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	John Ogness <john.ogness@linutronix.de>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Guenter Roeck <linux@roeck-us.net>,
	Shreyas Joshi <shreyas.joshi@biamp.com>,
	shreyasjoshi15@gmail.com,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>,
	linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.com>
Subject: [PATCH 1/2] init/console: Use ttynull as a fallback when there is no console
Date: Wed, 11 Nov 2020 14:54:49 +0100	[thread overview]
Message-ID: <20201111135450.11214-2-pmladek@suse.com> (raw)
In-Reply-To: <20201111135450.11214-1-pmladek@suse.com>

stdin, stdout, and stderr standard I/O stream are created for the init
process. They are not available when there is no console registered
for /dev/console. It might lead to a crash when the init process
tries to use them, see the commit 48021f98130880dd742 ("printk: handle
blank console arguments passed in.").

Normally, ttySX and ttyX consoles are used as a fallback when no consoles
are defined via the command line, device tree, or SPCR. But there
will be no console registered when an invalid console name is configured
or when the configured consoles do not exist on the system.

Users even try to avoid the console intentionally, for example,
by using console="" or console=null. It is used on production
systems where the serial port or terminal are not visible to
users. Pushing messages to these consoles would just unnecessary
slowdown the system.

Make sure that stdin, stdout, stderr, and /dev/console are always
available by a fallback to the existing ttynull driver. It has
been implemented for exactly this purpose but it was used only
when explicitly configured.

Signed-off-by: Petr Mladek <pmladek@suse.com>
---
 drivers/tty/Kconfig     | 14 --------------
 drivers/tty/Makefile    |  3 +--
 drivers/tty/ttynull.c   | 18 ++++++++++++++++++
 include/linux/console.h |  3 +++
 init/main.c             | 10 ++++++++--
 5 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/drivers/tty/Kconfig b/drivers/tty/Kconfig
index 93fd984eb2f5..ca359bbd62f5 100644
--- a/drivers/tty/Kconfig
+++ b/drivers/tty/Kconfig
@@ -428,20 +428,6 @@ config MIPS_EJTAG_FDC_KGDB_CHAN
 	help
 	  FDC channel number to use for KGDB.
 
-config NULL_TTY
-	tristate "NULL TTY driver"
-	help
-	  Say Y here if you want a NULL TTY which simply discards messages.
-
-	  This is useful to allow userspace applications which expect a console
-	  device to work without modifications even when no console is
-	  available or desired.
-
-	  In order to use this driver, you should redirect the console to this
-	  TTY, or boot the kernel with console=ttynull.
-
-	  If unsure, say N.
-
 config TRACE_ROUTER
 	tristate "Trace data router for MIPI P1149.7 cJTAG standard"
 	depends on TRACE_SINK
diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile
index 020b1cd9294f..f6b6bee0422d 100644
--- a/drivers/tty/Makefile
+++ b/drivers/tty/Makefile
@@ -2,7 +2,7 @@
 obj-$(CONFIG_TTY)		+= tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o \
 				   tty_buffer.o tty_port.o tty_mutex.o \
 				   tty_ldsem.o tty_baudrate.o tty_jobctrl.o \
-				   n_null.o
+				   n_null.o ttynull.o
 obj-$(CONFIG_LEGACY_PTYS)	+= pty.o
 obj-$(CONFIG_UNIX98_PTYS)	+= pty.o
 obj-$(CONFIG_AUDIT)		+= tty_audit.o
@@ -25,7 +25,6 @@ obj-$(CONFIG_ISI)		+= isicom.o
 obj-$(CONFIG_MOXA_INTELLIO)	+= moxa.o
 obj-$(CONFIG_MOXA_SMARTIO)	+= mxser.o
 obj-$(CONFIG_NOZOMI)		+= nozomi.o
-obj-$(CONFIG_NULL_TTY)	        += ttynull.o
 obj-$(CONFIG_ROCKETPORT)	+= rocket.o
 obj-$(CONFIG_SYNCLINK_GT)	+= synclink_gt.o
 obj-$(CONFIG_SYNCLINKMP)	+= synclinkmp.o
diff --git a/drivers/tty/ttynull.c b/drivers/tty/ttynull.c
index 17f05b7eb6d3..eced70ec54e1 100644
--- a/drivers/tty/ttynull.c
+++ b/drivers/tty/ttynull.c
@@ -2,6 +2,13 @@
 /*
  * Copyright (C) 2019 Axis Communications AB
  *
+ * The console is useful for userspace applications which expect a console
+ * device to work without modifications even when no console is available
+ * or desired.
+ *
+ * In order to use this driver, you should redirect the console to this
+ * TTY, or boot the kernel with console=ttynull.
+ *
  * Based on ttyprintk.c:
  *  Copyright (C) 2010 Samo Pogacnik
  */
@@ -59,6 +66,17 @@ static struct console ttynull_console = {
 	.device = ttynull_device,
 };
 
+void __init register_ttynull_console(void)
+{
+	if (!ttynull_driver)
+		return;
+
+	if (add_preferred_console(ttynull_console.name, 0, NULL))
+		return;
+
+	register_console(&ttynull_console);
+}
+
 static int __init ttynull_init(void)
 {
 	struct tty_driver *driver;
diff --git a/include/linux/console.h b/include/linux/console.h
index 4b1e26c4cb42..9c662e41cde5 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -187,9 +187,12 @@ extern int braille_register_console(struct console *, int index,
 extern int braille_unregister_console(struct console *);
 #ifdef CONFIG_TTY
 extern void console_sysfs_notify(void);
+extern void register_ttynull_console(void);
 #else
 static inline void console_sysfs_notify(void)
 { }
+static inline void register_ttynull_console(void)
+{ }
 #endif
 extern bool console_suspend_enabled;
 
diff --git a/init/main.c b/init/main.c
index 130376ec10ba..24413c055a85 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1470,8 +1470,14 @@ void __init console_on_rootfs(void)
 	struct file *file = filp_open("/dev/console", O_RDWR, 0);
 
 	if (IS_ERR(file)) {
-		pr_err("Warning: unable to open an initial console.\n");
-		return;
+		pr_err("Warning: unable to open an initial console. Fallback to ttynull.\n");
+		register_ttynull_console();
+
+		file = filp_open("/dev/console", O_RDWR, 0);
+		if (IS_ERR(file)) {
+			pr_err("Warning: Failed to add ttynull console. No stdin, stdout, and stderr for the init process!\n");
+			return;
+		}
 	}
 	init_dup(file);
 	init_dup(file);
-- 
2.26.2


  reply	other threads:[~2020-11-11 13:56 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 13:54 [PATCH 0/2] printk/console: Use ttynull when no console is available or wanted Petr Mladek
2020-11-11 13:54 ` Petr Mladek [this message]
2020-11-11 16:36   ` [PATCH 1/2] init/console: Use ttynull as a fallback when there is no console Greg Kroah-Hartman
2020-11-11 22:52   ` Guenter Roeck
2020-11-11 23:58   ` Sergey Senozhatsky
2020-11-12  0:17   ` Sergey Senozhatsky
2020-11-12  1:45     ` Sergey Senozhatsky
2020-11-16 16:20       ` Petr Mladek
2021-01-06 23:36   ` ARC no console output (was Re: [PATCH 1/2] init/console: Use ttynull as a fallback when there is no console) Vineet Gupta
2021-01-06 23:36     ` Vineet Gupta
2021-01-06 23:36     ` [Buildroot] " Vineet Gupta
2021-01-07  9:02     ` John Ogness
2021-01-07  9:02       ` John Ogness
2021-01-07 13:14       ` Greg Ungerer
2021-01-07 13:14         ` Greg Ungerer
2021-01-07 16:43       ` Vineet Gupta
2021-01-07 16:43         ` Vineet Gupta
2021-01-07 16:43         ` [Buildroot] " Vineet Gupta
2021-01-07 17:04         ` Petr Mladek
2021-01-07 17:04           ` Petr Mladek
2021-01-07 17:58           ` Vineet Gupta
2021-01-07 17:58             ` Vineet Gupta
2021-01-07 17:58             ` [Buildroot] " Vineet Gupta
2021-01-08  3:48             ` Sergey Senozhatsky
2021-01-08  3:48               ` Sergey Senozhatsky
2021-01-08  5:18               ` Vineet Gupta
2021-01-08  5:18                 ` Vineet Gupta
2021-01-08  5:18                 ` [Buildroot] " Vineet Gupta
2021-01-08  9:30                 ` Petr Mladek
2021-01-08  9:30                   ` Petr Mladek
2021-01-13 22:07             ` Peter Korsgaard
2021-01-13 22:07               ` Peter Korsgaard
2021-01-13 22:07               ` [Buildroot] " Peter Korsgaard
2021-01-07  9:09     ` Richard Weinberger
2021-01-07  9:09       ` Richard Weinberger
2021-01-07 12:48     ` Petr Mladek
2021-01-07 12:48       ` Petr Mladek
2021-01-07 15:27     ` [Buildroot] " Yann E. MORIN
2021-01-07 15:27       ` Yann E. MORIN
2021-01-07 15:27       ` Yann E. MORIN
2020-11-11 13:54 ` [PATCH 2/2] printk/console: Allow to disable console output by using console="" or console=null Petr Mladek
2020-11-11 22:52   ` Guenter Roeck
2020-11-11 23:59   ` Sergey Senozhatsky
2020-11-20 14:24 ` [PATCH 0/2] printk/console: Use ttynull when no console is available or wanted Petr Mladek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201111135450.11214-2-pmladek@suse.com \
    --to=pmladek@suse.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=rostedt@goodmis.org \
    --cc=sergey.senozhatsky.work@gmail.com \
    --cc=sergey.senozhatsky@gmail.com \
    --cc=shreyas.joshi@biamp.com \
    --cc=shreyasjoshi15@gmail.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.