Skip to contentSkip to conversation

Optionally allow ttynull to be selected as a default console

PATCH Superseded [email protected] 3 participants 10 messages Feb 24, 2025 – Mar 4, 2025

Subsystems (2)

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v5 cover Optionally allow ttynull to be selected as a default console

From: Adam Simonelli <[email protected]>

When switching to a CONFIG_VT=n world, at least on x86 systems, /dev/console becomes /dev/ttyS0. This can cause some undesired effects. /dev/console's behavior is now tied to the physical /dev/ttyS0, which when disconnected can cause isatty() to fail when /dev/ttyS0 is disconnected, and users who upgrade to a theoretical vt-less kernel from their distribution who have a device such as a science instrument connected to their /dev/ttyS0 port will suddenly see it receive kernel log messages.

When the new CONFIG_NULL_TTY_CONSOLE option is turned on, this will allow the ttynull device to be leveraged as the default console. Distributions that had CONFIG_VT turned on before will be able to leverage this option to where /dev/console is still backed by a psuedo device, avoiding these issues, without needing to enable the entire VT subsystem.

v2:
rebase

v3:
Clarify commit messages.

Guard the all the register_console()s in ttynull to prevent it from being registered twice.

Only change the link order if CONFIG_NULL_TTY_CONSOLE is enabled, otherwise use the existing order for ttynull if only CONFIG_NULL_TTY is enabled.

Document why the link order changes in the drivers/tty/Makefile file.

Replace #ifdefs

v4:
Remember to actually include the changes to v3 in the cover letter.

v5:
Correct code formatting in Makefile comment.

Adam Simonelli (2):
ttynull: Add an option to allow ttynull to be used as a console device
tty: Change order of ttynull to be linked sooner if enabled as a
console.

 drivers/tty/Kconfig   | 15 ++++++++++++++-
 drivers/tty/Makefile  | 10 ++++++++++
 drivers/tty/ttynull.c | 20 +++++++++++++++++++-
 3 files changed, 43 insertions(+), 2 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH v5 1/2 ttynull: Add an option to allow ttynull to be used as a console device

From: Adam Simonelli <[email protected]>

The new config option, CONFIG_NULL_TTY_CONSOLE will allow ttynull to be initialized by console_initcall() and selected as a possible console device.

---
 drivers/tty/Kconfig   | 15 ++++++++++++++-
 drivers/tty/ttynull.c | 20 +++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
drivers/tty/Kconfig +14 -1
index 63a494d36a1f..b4afae8b0e74 100644
@@ -383,7 +383,20 @@ config NULL_TTY
 	  available or desired.
 
 	  In order to use this driver, you should redirect the console to this
-	  TTY, or boot the kernel with console=ttynull.
+	  TTY, boot the kernel with console=ttynull, or enable
+	  CONFIG_NULL_TTY_CONSOLE.
+
+	  If unsure, say N.
+
+config NULL_TTY_CONSOLE
+        bool "Support for console on ttynull"
+        depends on NULL_TTY=y && !VT_CONSOLE
+	help
+	  Say Y here if you want the NULL TTY to be used as a /dev/console
+	  device.
+
+	  This is similar to CONFIG_VT_CONSOLE, but without the dependency on
+	  CONFIG_VT. It uses the ttynull driver as the system console.
 
 	  If unsure, say N.
 
drivers/tty/ttynull.c +19 -1
index 6b2f7208b564..ec3dd3fd41c0 100644
@@ -57,6 +57,13 @@ static struct tty_driver *ttynull_device(struct console *c, int *index)
 static struct console ttynull_console = {
 	.name = "ttynull",
 	.device = ttynull_device,
+
+	/*
+	 * Match the index and flags from other boot consoles when CONFIG_NULL_TTY_CONSOLE is
+	 * enabled, otherwise, use the default values for the index and flags.
+	*/
+	.index = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? -1 : 0,
+	.flags = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? CON_PRINTBUFFER : 0,
 };
 
 static int __init ttynull_init(void)
@@ -90,11 +97,22 @@ static int __init ttynull_init(void)
 	}
 
 	ttynull_driver = driver;
-	register_console(&ttynull_console);
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
 
 	return 0;
 }
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+static int __init ttynull_register(void)
+{
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
+	return 0;
+}
+console_initcall(ttynull_register);
+#endif
+
 static void __exit ttynull_exit(void)
 {
 	unregister_console(&ttynull_console);
Signature · 1 line
2.45.2
maintainer
#2 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Hi Adam,

please add printk maintainers into Cc as already suggested by Andy
at https://lore.kernel.org/r/CAHp75VeBaetiQBykfLk_weBHdzZF1nWp=k8BJu+OKNp6iYRRTg@mail.gmail.com

The motivation is that the console registration code is in kernel/printk/printk.c. It is historically pretty tricky. Some ordering is defined rather by chance than by design. And we should be careful when adding new rules and hacks.

Quoted 17 lines · On Mon 2025-02-24 07:39:14, [email protected] wrote:

From: Adam Simonelli <[email protected]>

The new config option, CONFIG_NULL_TTY_CONSOLE will allow ttynull to be initialized by console_initcall() and selected as a possible console device.

---
 drivers/tty/Kconfig   | 15 ++++++++++++++-
 drivers/tty/ttynull.c | 20 +++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
drivers/tty/Kconfig +0 -0
index 63a494d36a1f..b4afae8b0e74 100644

drivers/tty/Kconfig config NULL_TTY · line 391

Quoted 10 lines
drivers/tty/Kconfig +6 -1
@@ -383,7 +383,20 @@ config NULL_TTY
 	  available or desired.
 
 	  In order to use this driver, you should redirect the console to this
-	  TTY, or boot the kernel with console=ttynull.
+	  TTY, boot the kernel with console=ttynull, or enable
+	  CONFIG_NULL_TTY_CONSOLE.
+
+	  If unsure, say N.
+
+config NULL_TTY_CONSOLE

It makes sense to enable this behavior by a CONFIG_ setting but the name is misleading.

drivers/tty/Kconfig config NULL_TTY · line 399

Quoted 9 lines
drivers/tty/Kconfig +9 -0
+
+        bool "Support for console on ttynull"
+        depends on NULL_TTY=y && !VT_CONSOLE
+	help
+	  Say Y here if you want the NULL TTY to be used as a /dev/console
+	  device.
+
+	  This is similar to CONFIG_VT_CONSOLE, but without the dependency on
+	  CONFIG_VT. It uses the ttynull driver as the system console.

It is true that CONFIG_VT_CONSOLE causes that the virtual terminal will get associated with /dev/console. But it works only "by chance".

It works because "register_console(&vt_console_driver)" in con_init() is the first register_console() call. And it also works only by chance because of the linking order.

Anyway, there are more similar CONFIG_ options, for example, CONFIG_LP_CONSOLE, or CONFIG_VIRTIO_CONSOLE. And they are not default when CONFIG_VT_CONSOLE is enabled. They are registered only when the related console= option is defined on the command line.

I want to say that CONFIG_<BLA>_CONSOLE does not mean that the BLA console will be registered by default. And we should us a better descriptive name, for example,

NULL_TTY_DEFAULT_CONSOLE
NULL_TTY_DEV_CONSOLE

Quoted 7 lines
drivers/tty/Kconfig +0 -0
 	  If unsure, say N.
 
drivers/tty/ttynull.c +0 -0
index 6b2f7208b564..ec3dd3fd41c0 100644

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 65

Quoted 9 lines
drivers/tty/ttynull.c +6 -0
@@ -57,6 +57,13 @@ static struct tty_driver *ttynull_device(struct console *c, int *index)
 static struct console ttynull_console = {
 	.name = "ttynull",
 	.device = ttynull_device,
+
+	/*
+	 * Match the index and flags from other boot consoles when CONFIG_NULL_TTY_CONSOLE is
+	 * enabled, otherwise, use the default values for the index and flags.
+	*/
+	.index = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? -1 : 0,

This should not be needed. "con->index" is always initialized to "0" for the default console, see:

static void try_enable_default_console(struct console *newcon)

{
	if (newcon->index < 0)
		newcon->index = 0;

[...]
}

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 66

Quoted 1 line
drivers/tty/ttynull.c +1 -0
+	.flags = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? CON_PRINTBUFFER : 0,

This does not make much sense to me.

CON_PRINTBUFFER prevents duplicated output when the same device has already been registered as a boot console. But ttynull does not have a boot console variant. Also it is a "null" device. It never prints anything. The output could never be duplicated by definition.

Quoted 13 lines
drivers/tty/ttynull.c +2 -1
 };
 
 static int __init ttynull_init(void)
@@ -90,11 +97,22 @@ static int __init ttynull_init(void)
 	}
 
 	ttynull_driver = driver;
-	register_console(&ttynull_console);
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
 
 	return 0;
 }

drivers/tty/ttynull.c static int __init ttynull_init(void) · line 114

Quoted 10 lines
drivers/tty/ttynull.c +9 -0
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+static int __init ttynull_register(void)
+{
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
+	return 0;
+}
+console_initcall(ttynull_register);
+#endif

This looks strange. I guess that you needed to move this into console_initcall() because it is called earlier together with the other console_initcall() calls for serial ports. Otherwise, the hack with the linking order (2nd patch) did not work.

But you needed to keep it in ttynull_init() so that ttynull did not get registered prematurely when CONFIG_NULL_TTY_CONSOLE was not enabled.

Sigh, it looks like a dirty hack which works rather by chance than by design.

Thinking loudly:

The register_console() code is a historic mess. I dream about having time to clean it up. Anyway, there are basically two modes:

1. try_enable_default_console(newcon) is called only when there is no @preferred_console and there is no registered console with tty binding (valid con->device).

The first register_console() caller wins. The order is defined by the __con_initcall section. Which is defined by the linking order.

IMHO, it is quite fragile and non-intuitive.

2. try_enable_preferred_console() is called when some console is preferred via console_cmdline[]. The entries are added by __add_preferred_console() calls.

This approach was created to handle console= command line parameters. But it was later used to define default consoles also via SPCR and device tree, see add_preferred_console() callers.

It is also a bit tricky because the last added entry is preferred. Plus the .user_specified entries are preferred over the entries added via SPCR or device tree.

Anyway, I think that the preference and ordering defined by console_cmdline[] array is a more intuitive approach.

My proposal is to call:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
	add_preferred_console("ttynull", 0, NULL);

#endif

somewhere in the kernel code. The question is where. I wonder if the following would work:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	add_preferred_console("ttynull", 0, NULL);
	return 0;
}

console_initcall(ttynull_register);
#endif

Best Regards,
Petr

#3 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Quoted 5 lines · EST Petr Mladek

Hi Adam,

please add printk maintainers into Cc as already suggested by Andy
at https://lore.kernel.org/r/CAHp75VeBaetiQBykfLk_weBHdzZF1nWp=k8BJu+OKNp6iYRRTg@mail.gmail.com

OK, I have added the other printk maintainers in this thread, and will update my git send email script.

Quoted 59 lines

The motivation is that the console registration code is in kernel/printk/printk.c. It is historically pretty tricky. Some ordering is defined rather by chance than by design. And we should be careful when adding new rules and hacks.

Quoted 17 lines · On Mon 2025-02-24 07:39:14, [email protected] wrote:

From: Adam Simonelli <[email protected]>

The new config option, CONFIG_NULL_TTY_CONSOLE will allow ttynull to be initialized by console_initcall() and selected as a possible console device.

---
 drivers/tty/Kconfig   | 15 ++++++++++++++-
 drivers/tty/ttynull.c | 20 +++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
drivers/tty/Kconfig +0 -0
index 63a494d36a1f..b4afae8b0e74 100644

drivers/tty/Kconfig config NULL_TTY · line 391

Quoted 10 lines
drivers/tty/Kconfig +6 -1
@@ -383,7 +383,20 @@ config NULL_TTY
 	  available or desired.
 
 	  In order to use this driver, you should redirect the console to this
-	  TTY, or boot the kernel with console=ttynull.
+	  TTY, boot the kernel with console=ttynull, or enable
+	  CONFIG_NULL_TTY_CONSOLE.
+
+	  If unsure, say N.
+
+config NULL_TTY_CONSOLE

It makes sense to enable this behavior by a CONFIG_ setting but the name is misleading.

drivers/tty/Kconfig config NULL_TTY · line 400

Quoted 9 lines
drivers/tty/Kconfig +9 -0
+
+        bool "Support for console on ttynull"
+        depends on NULL_TTY=y && !VT_CONSOLE
+	help
+	  Say Y here if you want the NULL TTY to be used as a /dev/console
+	  device.
+
+	  This is similar to CONFIG_VT_CONSOLE, but without the dependency on
+	  CONFIG_VT. It uses the ttynull driver as the system console.

It is true that CONFIG_VT_CONSOLE causes that the virtual terminal will get associated with /dev/console. But it works only "by chance".

It works because "register_console(&vt_console_driver)" in con_init() is the first register_console() call. And it also works only by chance because of the linking order.

Anyway, there are more similar CONFIG_ options, for example, CONFIG_LP_CONSOLE, or CONFIG_VIRTIO_CONSOLE. And they are not default when CONFIG_VT_CONSOLE is enabled. They are registered only when the related console= option is defined on the command line.

Quoted 7 lines

I want to say that CONFIG_<BLA>_CONSOLE does not mean that the BLA console will be registered by default. And we should us a better descriptive name, for example,

NULL_TTY_DEFAULT_CONSOLE
NULL_TTY_DEV_CONSOLE

OK. I can change the option name.

Quoted 11 lines
Quoted 11 lines
drivers/tty/Kconfig +0 -0
 	  If unsure, say N.
 
drivers/tty/ttynull.c +1 -0
index 6b2f7208b564..ec3dd3fd41c0 100644
@@ -57,6 +57,13 @@ static struct tty_driver *ttynull_device(struct console *c, int *index)
 static struct console ttynull_console = {
 	.name = "ttynull",
 	.device = ttynull_device,
+
Quoted 9 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 65

Quoted 5 lines
drivers/tty/ttynull.c +5 -0
+	/*
+	 * Match the index and flags from other boot consoles when CONFIG_NULL_TTY_CONSOLE is
+	 * enabled, otherwise, use the default values for the index and flags.
+	*/
+	.index = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? -1 : 0,

This should not be needed. "con->index" is always initialized to "0" for the default console, see:

OK, I had this in an #ifdef before, it was the cleanest way to set it to -1 that I could think of, other than the ifdef... If I still need this, I will try to think of something else to set it to -1 when the option is enabled

Quoted 7 lines

static void try_enable_default_console(struct console *newcon)

{
	if (newcon->index < 0)
		newcon->index = 0;

[...]
}

Quoted 9 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 66

Quoted 1 line
drivers/tty/ttynull.c +1 -0
+	.flags = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? CON_PRINTBUFFER : 0,

This does not make much sense to me.

CON_PRINTBUFFER prevents duplicated output when the same device has already been registered as a boot console. But ttynull does not have a boot console variant. Also it is a "null" device. It never prints anything. The output could never be duplicated by definition.

OK, I was duplicating what I saw in other consoles. I can try to remove it

Quoted 88 lines
Quoted 13 lines
drivers/tty/ttynull.c +2 -1
 };
 
 static int __init ttynull_init(void)
@@ -90,11 +97,22 @@ static int __init ttynull_init(void)
 	}
 
 	ttynull_driver = driver;
-	register_console(&ttynull_console);
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
 
 	return 0;
 }

drivers/tty/ttynull.c static int __init ttynull_init(void) · line 114

Quoted 10 lines
drivers/tty/ttynull.c +9 -0
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+static int __init ttynull_register(void)
+{
+	if (!console_is_registered(&ttynull_console))
+		register_console(&ttynull_console);
+	return 0;
+}
+console_initcall(ttynull_register);
+#endif

This looks strange. I guess that you needed to move this into console_initcall() because it is called earlier together with the other console_initcall() calls for serial ports. Otherwise, the hack with the linking order (2nd patch) did not work.

But you needed to keep it in ttynull_init() so that ttynull did not get registered prematurely when CONFIG_NULL_TTY_CONSOLE was not enabled.

Sigh, it looks like a dirty hack which works rather by chance than by design.

Thinking loudly:

The register_console() code is a historic mess. I dream about having time to clean it up. Anyway, there are basically two modes:

1. try_enable_default_console(newcon) is called only when there is no @preferred_console and there is no registered console with tty binding (valid con->device).

The first register_console() caller wins. The order is defined by the __con_initcall section. Which is defined by the linking order.

IMHO, it is quite fragile and non-intuitive.

2. try_enable_preferred_console() is called when some console is preferred via console_cmdline[]. The entries are added by __add_preferred_console() calls.

This approach was created to handle console= command line parameters. But it was later used to define default consoles also via SPCR and device tree, see add_preferred_console() callers.

It is also a bit tricky because the last added entry is preferred. Plus the .user_specified entries are preferred over the entries added via SPCR or device tree.

Anyway, I think that the preference and ordering defined by console_cmdline[] array is a more intuitive approach.

My proposal is to call:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
	add_preferred_console("ttynull", 0, NULL);

#endif

somewhere in the kernel code. The question is where. I wonder if the following would work:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	add_preferred_console("ttynull", 0, NULL);
	return 0;
}
Quoted 6 lines

console_initcall(ttynull_register);
#endif

Best Regards,
Petr

OK, actually in earlier revisions locally, I did actually have

kernel/printk/printk.c +5 -0
index dddb15f48d59..c1554a789de8 100644
@@ -3712,6 +3712,11 @@ void __init console_init(void)
 	initcall_t call;
 	initcall_entry_t *ce;
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+       if (!strstr(boot_command_line, "console="))
+               add_preferred_console("ttynull", 0, NULL);
+#endif
+
 	/* Setup the default TTY line discipline. */
 	n_tty_init();
 

Which worked as far as I could tell, at least on x86. Not sure if that was the right place, and yeah, I was trying to better copy how CONFIG_VT_CONSOLE worked because I thought that was more correct.

#4 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Quoted 1 line · Adam Simonelli

On Tuesday, February 25, 2025 11:19:04 AM EST Petr Mladek wrote:

...

Quoted 26 lines
Quoted 10 lines

My proposal is to call:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
      add_preferred_console("ttynull", 0, NULL);

#endif

somewhere in the kernel code. The question is where. I wonder if the following would work:

Quoted 9 lines

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
      add_preferred_console("ttynull", 0, NULL);
      return 0;
}

console_initcall(ttynull_register);
#endif

OK, actually in earlier revisions locally, I did actually have

kernel/printk/printk.c +0 -0
index dddb15f48d59..c1554a789de8 100644

kernel/printk/printk.c void __init console_init(void) · line 3716

Quoted 5 lines
kernel/printk/printk.c +2 -0
@@ -3712,6 +3712,11 @@ void __init console_init(void)
        initcall_t call;
        initcall_entry_t *ce;
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+       if (!strstr(boot_command_line, "console="))

Just a side note: strstr() is fragile as theoretically "console=" can be part of an argument unrelated to the console, like foo="bar,baz,console=10,key=value". Although I haven't checked if this is allowed by cmdline parser (lib/cmdline.c).

Quoted 11 lines
kernel/printk/printk.c +3 -0
+               add_preferred_console("ttynull", 0, NULL);
+#endif
+
        /* Setup the default TTY line discipline. */
        n_tty_init();
 

Which worked as far as I could tell, at least on x86. Not sure if that was the right place, and yeah, I was trying to better copy how CONFIG_VT_CONSOLE worked because I thought that was more correct.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Quoted 34 lines · EST Andy Shevchenko
Quoted 1 line · Adam Simonelli

On Tuesday, February 25, 2025 11:19:04 AM EST Petr Mladek wrote:

...

Quoted 29 lines
Quoted 10 lines

My proposal is to call:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
      add_preferred_console("ttynull", 0, NULL);

#endif

somewhere in the kernel code. The question is where. I wonder if the following would work:

Quoted 9 lines

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
      add_preferred_console("ttynull", 0, NULL);
      return 0;
}

console_initcall(ttynull_register);
#endif

OK, actually in earlier revisions locally, I did actually have

kernel/printk/printk.c +0 -0
index dddb15f48d59..c1554a789de8 100644
@@ -3712,6 +3712,11 @@ void __init console_init(void)
        initcall_t call;
        initcall_entry_t *ce;
 
Quoted 8 lines

kernel/printk/printk.c void __init console_init(void) · line 3716

Quoted 2 lines
kernel/printk/printk.c +2 -0
+#ifdef CONFIG_NULL_TTY_CONSOLE
+       if (!strstr(boot_command_line, "console="))

Just a side note: strstr() is fragile as theoretically "console=" can be part of an argument unrelated to the console, like foo="bar,baz,console=10,key=value". Although I haven't checked if this is allowed by cmdline parser (lib/cmdline.c).

Dang, good call. As a crude test, console=ttynull= results in a panic, so it does look like it allows ='s in parameter values, as it looks like it is handling the =...

Gotta find a better way to parse it if I'm to do the add_preferred_console route, Maybe I can try get_option...

What do you think of the placement of it too?

Quoted 13 lines
Quoted 11 lines
kernel/printk/printk.c +3 -0
+               add_preferred_console("ttynull", 0, NULL);
+#endif
+
        /* Setup the default TTY line discipline. */
        n_tty_init();
 

Which worked as far as I could tell, at least on x86. Not sure if that was the right place, and yeah, I was trying to better copy how CONFIG_VT_CONSOLE worked because I thought that was more correct.

#6 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Quoted 3 lines · Adam Simonelli
Quoted 1 line · EST Andy Shevchenko

On Wed, Feb 26, 2025 at 3:39 PM Adam Simonelli <[email protected]> wrote:

...

Quoted 11 lines
Quoted 5 lines

Just a side note: strstr() is fragile as theoretically "console=" can be part of an argument unrelated to the console, like foo="bar,baz,console=10,key=value". Although I haven't checked if this is allowed by cmdline parser (lib/cmdline.c).

Dang, good call. As a crude test, console=ttynull= results in a panic, so it does look like it allows ='s in parameter values, as it looks like it is handling the =...

Gotta find a better way to parse it if I'm to do the add_preferred_console route, Maybe I can try get_option...

It's rather next_arg() and its derivatives. I dunno if we have a helper for the "give me an array of this options from the command line", but it might be somewhere lurking as a static function. If that
is the case, one may make it global and visible to other C-files
inside the kernel.

Quoted 1 line

What do you think of the placement of it too?

No particular ideas.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

In reply to Adam Simonelli
Quoted 26 lines · Adam Simonelli
Quoted 16 lines · EST Petr Mladek
Quoted 15 lines · On Mon 2025-02-24 07:39:14, [email protected] wrote:

From: Adam Simonelli <[email protected]>

The new config option, CONFIG_NULL_TTY_CONSOLE will allow ttynull to be initialized by console_initcall() and selected as a possible console device.

drivers/tty/ttynull.c +1 -0
index 6b2f7208b564..ec3dd3fd41c0 100644
@@ -57,6 +57,13 @@ static struct tty_driver *ttynull_device(struct console *c, int *index)
 static struct console ttynull_console = {
 	.name = "ttynull",
 	.device = ttynull_device,
+
Quoted 9 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 65

Quoted 5 lines
drivers/tty/ttynull.c +5 -0
+	/*
+	 * Match the index and flags from other boot consoles when CONFIG_NULL_TTY_CONSOLE is
+	 * enabled, otherwise, use the default values for the index and flags.
+	*/
+	.index = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? -1 : 0,

This should not be needed. "con->index" is always initialized to "0" for the default console, see:

Quoted 3 lines

OK, I had this in an #ifdef before, it was the cleanest way to set it to -1 that I could think of, other than the ifdef... If I still need this, I will try to think of something else to set it to -1 when the option is enabled

Ah, I was not clear enough. It should be perfectly fine to always statically initialize the value to -1. We should not need any #ifdef or IS_ENABLED.

I mean to do:

static struct console ttynull_console = {
	.name = "ttynull",
	.device = ttynull_device,
	.index = -1,
};

We might even do this in a separate patch. IMHO, it should have been done this way since the beginning.

Quoted 11 lines
Quoted 11 lines

static void try_enable_default_console(struct console *newcon)

{
	if (newcon->index < 0)
		newcon->index = 0;

[...]
}

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 66

Quoted 1 line
drivers/tty/ttynull.c +1 -0
+	.flags = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? CON_PRINTBUFFER : 0,

This does not make much sense to me.

Quoted 6 lines
Quoted 5 lines

CON_PRINTBUFFER prevents duplicated output when the same device has already been registered as a boot console. But ttynull does not have a boot console variant. Also it is a "null" device. It never prints anything. The output could never be duplicated by definition.

OK, I was duplicating what I saw in other consoles. I can try to remove it

Again, I was not clear enough. My primary concern was that it did not make much sense to use the IS_ENABLED() check and initialize the value different way.

Anyway, I would omit the flag. It is a NULL device. It does not matter whether it prints existing (old) messages during registration or not.

Quoted 22 lines
Quoted 4 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 68

Quoted 2 lines
drivers/tty/ttynull.c +0 -0
 };
 

My proposal is to call:

Quoted 9 lines

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	add_preferred_console("ttynull", 0, NULL);
	return 0;
}

console_initcall(ttynull_register);
#endif

OK, actually in earlier revisions locally, I did actually have

kernel/printk/printk.c +0 -0
index dddb15f48d59..c1554a789de8 100644

kernel/printk/printk.c void __init console_init(void) · line 3717

Quoted 6 lines
kernel/printk/printk.c +3 -0
@@ -3712,6 +3712,11 @@ void __init console_init(void)
 	initcall_t call;
 	initcall_entry_t *ce;
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+       if (!strstr(boot_command_line, "console="))
+               add_preferred_console("ttynull", 0, NULL);

Good point! We should call add_preferred_console() only when the is no console= command line parameter. Otherwise, it could not get overridden by the command line.

We could check "console_set_on_cmdline", similar to xenfb_make_preferred_console().

Quoted 7 lines
kernel/printk/printk.c +2 -0
+#endif
+
 	/* Setup the default TTY line discipline. */
 	n_tty_init();
 

Which worked as far as I could tell, at least on x86. Not sure if that was the right place,

I would prefer to keep it in drivers/tty/ttynull.c when possible. The following might do the trick:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	if (!console_set_on_cmdline)
		add_preferred_console("ttynull", 0, NULL);

	return 0;
}

console_initcall(ttynull_register);
#endif

Best Regards,
Petr

#8 lore raw

Re: [PATCH v5 1/2] ttynull: Add an option to allow ttynull to be used as a console device

Quoted 37 lines · EST Petr Mladek
Quoted 26 lines · Adam Simonelli
Quoted 16 lines · EST Petr Mladek
Quoted 15 lines · On Mon 2025-02-24 07:39:14, [email protected] wrote:

From: Adam Simonelli <[email protected]>

The new config option, CONFIG_NULL_TTY_CONSOLE will allow ttynull to be initialized by console_initcall() and selected as a possible console device.

drivers/tty/ttynull.c +1 -0
index 6b2f7208b564..ec3dd3fd41c0 100644
@@ -57,6 +57,13 @@ static struct tty_driver *ttynull_device(struct console *c, int *index)
 static struct console ttynull_console = {
 	.name = "ttynull",
 	.device = ttynull_device,
+
Quoted 9 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 65

Quoted 5 lines
drivers/tty/ttynull.c +5 -0
+	/*
+	 * Match the index and flags from other boot consoles when CONFIG_NULL_TTY_CONSOLE is
+	 * enabled, otherwise, use the default values for the index and flags.
+	*/
+	.index = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? -1 : 0,

This should not be needed. "con->index" is always initialized to "0" for the default console, see:

Quoted 3 lines

OK, I had this in an #ifdef before, it was the cleanest way to set it to -1 that I could think of, other than the ifdef... If I still need this, I will try to think of something else to set it to -1 when the option is enabled

Ah, I was not clear enough. It should be perfectly fine to always statically initialize the value to -1. We should not need any #ifdef or IS_ENABLED.

I mean to do:

Quoted 9 lines
static struct console ttynull_console = {
	.name = "ttynull",
	.device = ttynull_device,
	.index = -1,
};

We might even do this in a separate patch. IMHO, it should have been done this way since the beginning.

OK, will do. This makes sense.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

static void try_enable_default_console(struct console *newcon)

{
	if (newcon->index < 0)
		newcon->index = 0;

[...]
}

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 66

Quoted 1 line
drivers/tty/ttynull.c +1 -0
+	.flags = IS_ENABLED(CONFIG_NULL_TTY_CONSOLE) ? CON_PRINTBUFFER : 0,

This does not make much sense to me.

CON_PRINTBUFFER prevents duplicated output when the same device has already been registered as a boot console. But ttynull does not have a boot console variant. Also it is a "null" device. It never prints anything. The output could never be duplicated by definition.

Quoted 9 lines
Quoted 1 line

OK, I was duplicating what I saw in other consoles. I can try to remove it

Again, I was not clear enough. My primary concern was that it did not make much sense to use the IS_ENABLED() check and initialize the value different way.

Anyway, I would omit the flag. It is a NULL device. It does not matter whether it prints existing (old) messages during registration or not.

Understood, this makes sense.

Quoted 55 lines
Quoted 22 lines
Quoted 4 lines

drivers/tty/ttynull.c static struct tty_driver *ttynull_device(struct console *c, int *index) · line 68

Quoted 2 lines
drivers/tty/ttynull.c +0 -0
 };
 

My proposal is to call:

Quoted 9 lines

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	add_preferred_console("ttynull", 0, NULL);
	return 0;
}

console_initcall(ttynull_register);
#endif

OK, actually in earlier revisions locally, I did actually have

kernel/printk/printk.c +0 -0
index dddb15f48d59..c1554a789de8 100644

kernel/printk/printk.c void __init console_init(void) · line 3717

Quoted 6 lines
kernel/printk/printk.c +3 -0
@@ -3712,6 +3712,11 @@ void __init console_init(void)
 	initcall_t call;
 	initcall_entry_t *ce;
 
+#ifdef CONFIG_NULL_TTY_CONSOLE
+       if (!strstr(boot_command_line, "console="))
+               add_preferred_console("ttynull", 0, NULL);

Good point! We should call add_preferred_console() only when the is no console= command line parameter. Otherwise, it could not get overridden by the command line.

We could check "console_set_on_cmdline", similar to xenfb_make_preferred_console().

Quoted 7 lines
kernel/printk/printk.c +2 -0
+#endif
+
 	/* Setup the default TTY line discipline. */
 	n_tty_init();
 

Which worked as far as I could tell, at least on x86. Not sure if that was the right place,

I would prefer to keep it in drivers/tty/ttynull.c when possible. The following might do the trick:

#ifdef CONFIG_NULL_TTY_DEFAULT_CONSOLE
static int __init ttynull_default_console(void)

{
	if (!console_set_on_cmdline)
		add_preferred_console("ttynull", 0, NULL);

	return 0;
}
Quoted 6 lines

console_initcall(ttynull_register);
#endif

Best Regards,
Petr

Thanks for that, that works.

#9 lore raw

PATCH v5 2/2 tty: Change order of ttynull to be linked sooner if enabled as a console.

In reply to [email protected]

From: Adam Simonelli <[email protected]>

If CONFIG_NULL_TTY_CONSOLE is enabled, and CONFIG_VT is disabled, ttynull will become the default primary console device, based on the link order.

Many distributions ship with CONFIG_VT enabled. On tested desktop hardware if CONFIG_VT is disabled, the default console device falls back to /dev/ttyS0 instead of /dev/tty.

This could cause issues in user space, and hardware problems:

1. The user space issues include the case where /dev/ttyS0 is disconnected, and the TCGETS ioctl, which some user space libraries use as a probe to determine if a file is a tty, is called on /dev/console and fails. Programs that call isatty() on /dev/console and get an incorrect false value may skip expected logging to /dev/console

2. The hardware issues include the case if a user has a science instrument or other device connected to the /dev/ttyS0 port, and they were to upgrade to a kernel that is disabling the CONFIG_VT option, kernel logs will then be sent to the device connected to /dev/ttyS0 unless they edit their kernel command line manually.

The new CONFIG_NULL_TTY_CONSOLE option will give users and distribution maintainers an option to avoid this. Disabling CONFIG_VT and enabling CONFIG_NULL_TTY_CONSOLE will ensure the default kernel console behavior is not dependant on hardware configuration by default, and avoid unexpected new behavior on devices connected to the /dev/ttyS0 serial port.

---
 drivers/tty/Makefile | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/tty/Makefile +10 -0
index 07aca5184a55..1fb905cfb420 100644
@@ -11,6 +11,10 @@ obj-$(CONFIG_N_HDLC)		+= n_hdlc.o
 obj-$(CONFIG_N_GSM)		+= n_gsm.o
 
 obj-y				+= vt/
+# If ttynull is configured to be a console by default, ensure that it is linked
+# earlier before a real one is selected.
+obj-$(CONFIG_NULL_TTY_CONSOLE)	+= ttynull.o
+
 obj-$(CONFIG_HVC_DRIVER)	+= hvc/
 obj-y				+= serial/
 obj-$(CONFIG_SERIAL_DEV_BUS)	+= serdev/
@@ -20,7 +24,13 @@ obj-$(CONFIG_AMIGA_BUILTIN_SERIAL) += amiserial.o
 obj-$(CONFIG_MOXA_INTELLIO)	+= moxa.o
 obj-$(CONFIG_MOXA_SMARTIO)	+= mxser.o
 obj-$(CONFIG_NOZOMI)		+= nozomi.o
+
+# If ttynull is enabled, but not as a boot console, it is linked and used later
+# after the real ones.
+ifneq ($(CONFIG_NULL_TTY_CONSOLE),y)
 obj-$(CONFIG_NULL_TTY)	        += ttynull.o
+endif
+
 obj-$(CONFIG_SYNCLINK_GT)	+= synclink_gt.o
 obj-$(CONFIG_PPC_EPAPR_HV_BYTECHAN) += ehv_bytechan.o
 obj-$(CONFIG_GOLDFISH_TTY)	+= goldfish.o
Signature · 1 line
2.45.2