Skip to contentSkip to conversation

printk: Set console_set_on_cmdline=1 when using console="" or console=null

PATCH In review Andre Kalb 3 participants 9 messages Jan 27, 2022 – Feb 21, 2022 Printk

lore
Share & tools
lore

Andre Kalb

author
#0 lore raw

PATCH 1/1 printk: Set console_set_on_cmdline=1 when using console="" or console=null

In case of using console="" or console=null set console_set_on_cmdline=1 to disable chosen{ "stdout-path" } node from devicetree.

To jump out from drivers/of/base.c, line 2087 of_console_check function with false.

---
 kernel/printk/printk.c | 1 +
 1 file changed, 1 insertion(+)
kernel/printk/printk.c +1 -0
index 82abfaf3c2aa..df5ab35b8af2 100644
@@ -2385,6 +2385,7 @@ static int __init console_setup(char *str)
 	 */
 	if (str[0] == 0 || strcmp(str, "null") == 0) {
 		__add_preferred_console("ttynull", 0, NULL, NULL, true);
+		console_set_on_cmdline = 1;
 		return 1;
 	}
 
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 5 lines · Andre Kalb

In case of using console="" or console=null set console_set_on_cmdline=1 to disable chosen{ "stdout-path" } node from devicetree.

To jump out from drivers/of/base.c, line 2087 of_console_check function with false.

It makes sense. I would just fix it slightly different way, see below.

Quoted 10 lines
---
 kernel/printk/printk.c | 1 +
 1 file changed, 1 insertion(+)
kernel/printk/printk.c +0 -0
index 82abfaf3c2aa..df5ab35b8af2 100644

kernel/printk/printk.c static int __init console_setup(char *str) · line 2389

Quoted 5 lines
kernel/printk/printk.c +1 -0
@@ -2385,6 +2385,7 @@ static int __init console_setup(char *str)
 	 */
 	if (str[0] == 0 || strcmp(str, "null") == 0) {
 		__add_preferred_console("ttynull", 0, NULL, NULL,
 true); 
+		console_set_on_cmdline = 1;

We basically always need to set it when __add_preferred_console() is called with user_specified == true.

Therefore, we should move the assigment into __add_preferred_console(). We should do something like:

kernel/printk/printk.c +11 -4
index 82abfaf3c2aa..3654695ca5d2 100644
@@ -2324,6 +2324,15 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 }
 #endif
 
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
+	c->user_specified = true;
+	console_set_on_cmdline = 1;
+}
+
 static int __add_preferred_console(char *name, int idx, char *options,
 				   char *brl_options, bool user_specified)
 {
@@ -2340,8 +2349,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		if (strcmp(c->name, name) == 0 && c->index == idx) {
 			if (!brl_options)
 				preferred_console = i;
-			if (user_specified)
-				c->user_specified = true;
+			set_user_specified(c, user_specified);
 			return 0;
 		}
 	}
@@ -2351,7 +2359,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		preferred_console = i;
 	strlcpy(c->name, name, sizeof(c->name));
 	c->options = options;
-	c->user_specified = user_specified;
+	set_user_specified(c, user_specified);
 	braille_set_options(c, brl_options);
 
 	c->index = idx;
@@ -2417,7 +2425,6 @@ static int __init console_setup(char *str)
 	*s = 0;
 
 	__add_preferred_console(buf, idx, options, brl_options, true);
-	console_set_on_cmdline = 1;
 	return 1;
 }
 __setup("console=", console_setup);

Best Regards,
Petr

author
#2 lore raw

PATCH v2 1/1 printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

From: Andre Kalb <[email protected]>

In case of using console="" or console=null
set console_set_on_cmdline=1 to disable "stdout-path" node from DT.

We basically need to set it every time when __add_preferred_console() is called with parameter 'user_specified' set.
Therefore we can move setting it into a helper function that is called from __add_preferred_console().

---
Changelog v1 to v2:
Move console_set_on_cmdling into separate function set_user_specified(), which is called from __add_preferred_console().

The old patch v1 could be used to backport to stable 5.4 and lower.

---
 kernel/printk/printk.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
kernel/printk/printk.c +11 -4
index 82abfaf3c2aa..3654695ca5d2 100644
@@ -2324,6 +2324,15 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 }
 #endif
 
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
+	c->user_specified = true;
+	console_set_on_cmdline = 1;
+}
+
 static int __add_preferred_console(char *name, int idx, char *options,
 				   char *brl_options, bool user_specified)
 {
@@ -2340,8 +2349,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		if (strcmp(c->name, name) == 0 && c->index == idx) {
 			if (!brl_options)
 				preferred_console = i;
-			if (user_specified)
-				c->user_specified = true;
+			set_user_specified(c, user_specified);
 			return 0;
 		}
 	}
@@ -2351,7 +2359,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		preferred_console = i;
 	strlcpy(c->name, name, sizeof(c->name));
 	c->options = options;
-	c->user_specified = user_specified;
+	set_user_specified(c, user_specified);
 	braille_set_options(c, brl_options);
 
 	c->index = idx;
@@ -2417,7 +2425,6 @@ static int __init console_setup(char *str)
 	*s = 0;
 
 	__add_preferred_console(buf, idx, options, brl_options, true);
-	console_set_on_cmdline = 1;
 	return 1;
 }
 __setup("console=", console_setup);
Signature · 1 line
2.31.1
#3 lore raw

Re: [PATCH v2] printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2334

Quoted 8 lines · Andre Kalb
kernel/printk/printk.c +8 -0
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
+	c->user_specified = true;
+	console_set_on_cmdline = 1;
+}

In original code we always set c->user_specified. Is it guaranteed that ->user_specified is properly initialized to 0? Maybe can do something like:

static void set_user_specified(struct console_cmdline *c, bool user_specified)

{
	c->user_specified = user_specified;

	if (!user_specified)
		return;

	console_set_on_cmdline = 1;
}
maintainer
#4 lore raw

Re: [PATCH v2] printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

Quoted 12 lines · Sergey Senozhatsky

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2334

Quoted 8 lines · Andre Kalb
kernel/printk/printk.c +8 -0
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
+	c->user_specified = true;
+	console_set_on_cmdline = 1;
+}

In original code we always set c->user_specified. Is it guaranteed that ->user_specified is properly initialized to 0? Maybe can do something like:

It is guaranteed. console_cmdline is a static array initialized with zeroes. The 2nd set_user_specified() call is done for a not-yet-used slot in the array, so it must be zero.

Quoted 3 lines

static void set_user_specified(struct console_cmdline *c, bool user_specified)

{
	c->user_specified = user_specified;

This will change the behavior for the 1st set_user_specified() call. It happens when the same console is added more times by device tree, SPCR, and/or command line. c->user_specified must stay "true" when at least one __add_preferred_console() call added it from the command line.

Quoted 5 lines
	if (!user_specified)
		return;

	console_set_on_cmdline = 1;
}

I agree that it is not obvious. It would make sense to add a comment into the code. I am going to propose something in a reply to the original post.

Best Regards,
Petr

maintainer Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH v2] printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

In reply to Andre Kalb
Quoted 27 lines · Andre Kalb

From: Andre Kalb <[email protected]>

In case of using console="" or console=null
set console_set_on_cmdline=1 to disable "stdout-path" node from DT.

We basically need to set it every time when __add_preferred_console() is called with parameter 'user_specified' set.
Therefore we can move setting it into a helper function that is called from __add_preferred_console().

---
Changelog v1 to v2:
Move console_set_on_cmdling into separate function set_user_specified(), which is called from __add_preferred_console().

The old patch v1 could be used to backport to stable 5.4 and lower.

---
 kernel/printk/printk.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
kernel/printk/printk.c +0 -0
index 82abfaf3c2aa..3654695ca5d2 100644

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2326

Quoted 3 lines
kernel/printk/printk.c +0 -0
@@ -2324,6 +2324,15 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 }
 #endif
 

We should add a comment explaining the less obvious behavior as discussed in the thread. Something like:

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2331

Quoted 5 lines
kernel/printk/printk.c +5 -0
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
	/*
	 * @c console was defined by the user on the command line.
	 * Do not clear when added twice also by SPCR or the device tree.
	 */

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2332

Quoted 1 line
kernel/printk/printk.c +1 -0
+	c->user_specified = true;
	/* At least one console defined by the user on the command line. */

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2338

Quoted 6 lines
kernel/printk/printk.c +3 -0
+	console_set_on_cmdline = 1;
+}
+
 static int __add_preferred_console(char *name, int idx, char *options,
 				   char *brl_options, bool user_specified)
 {

With the above comments:

Sergey, is it enough from your POV, please?

Best Regards,
Petr

reviewer Reviewed-by
#6 lore raw

Re: [PATCH v2] printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

On (22/02/15 19:10), Petr Mladek wrote:
[..]

Quoted 23 lines

We should add a comment explaining the less obvious behavior as discussed in the thread. Something like:

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2331

Quoted 5 lines
kernel/printk/printk.c +5 -0
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
	/*
	 * @c console was defined by the user on the command line.
	 * Do not clear when added twice also by SPCR or the device tree.
	 */

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2332

Quoted 1 line
kernel/printk/printk.c +1 -0
+	c->user_specified = true;
	/* At least one console defined by the user on the command line. */

kernel/printk/printk.c asmlinkage __visible void early_printk(const char *fmt, ...) · line 2338

Quoted 6 lines
kernel/printk/printk.c +3 -0
+	console_set_on_cmdline = 1;
+}
+
 static int __add_preferred_console(char *name, int idx, char *options,
 				   char *brl_options, bool user_specified)
 {

With the above comments:

Quoted 3 lines

Sergey, is it enough from your POV, please?

Yes, Petr. Thank you.

author
#7 lore raw

PATCH v3 1/1 printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

From: Andre Kalb <[email protected]>

In case of using console="" or console=null
set console_set_on_cmdline=1 to disable "stdout-path" node from DT.

We basically need to set it every time when __add_preferred_console() is called with parameter 'user_specified' set.
Therefore we can move setting it into a helper function that is called from __add_preferred_console().

---
Changelog v1 to v2:
Move console_set_on_cmdling into separate function set_user_specified(), which is called from __add_preferred_console().

The old patch v1 could be used to backport to stable 5.4 and lower.

Changelog v2 to v3:
Add comment to explain the less obvious behaviour.

---
 kernel/printk/printk.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/printk/printk.c +16 -4
index 82abfaf3c2aa..e4e1a5266108 100644
@@ -2324,6 +2324,20 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
 }
 #endif
 
+static void set_user_specified(struct console_cmdline *c, bool user_specified)
+{
+	if (!user_specified)
+		return;
+
+	/*
+	 * @c console was defined by the user on the command line.
+	 * Do not clear when added twice also by SPCR or the device tree.
+	 */
+	c->user_specified = true;
+	/* At least one console defined by the user on the command line. */
+	console_set_on_cmdline = 1;
+}
+
 static int __add_preferred_console(char *name, int idx, char *options,
 				   char *brl_options, bool user_specified)
 {
@@ -2340,8 +2354,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		if (strcmp(c->name, name) == 0 && c->index == idx) {
 			if (!brl_options)
 				preferred_console = i;
-			if (user_specified)
-				c->user_specified = true;
+			set_user_specified(c, user_specified);
 			return 0;
 		}
 	}
@@ -2351,7 +2364,7 @@ static int __add_preferred_console(char *name, int idx, char *options,
 		preferred_console = i;
 	strlcpy(c->name, name, sizeof(c->name));
 	c->options = options;
-	c->user_specified = user_specified;
+	set_user_specified(c, user_specified);
 	braille_set_options(c, brl_options);
 
 	c->index = idx;
@@ -2417,7 +2430,6 @@ static int __init console_setup(char *str)
 	*s = 0;
 
 	__add_preferred_console(buf, idx, options, brl_options, true);
-	console_set_on_cmdline = 1;
 	return 1;
 }
 __setup("console=", console_setup);
Signature · 1 line
2.31.1
maintainer
#8 lore raw

Re: [PATCH v3] printk: Set console_set_on_cmdline=1 when __add_preferred_console() is called with user_specified == true

Quoted 10 lines · Andre Kalb

From: Andre Kalb <[email protected]>

In case of using console="" or console=null
set console_set_on_cmdline=1 to disable "stdout-path" node from DT.

We basically need to set it every time when __add_preferred_console() is called with parameter 'user_specified' set.
Therefore we can move setting it into a helper function that is called from __add_preferred_console().

Quoted 4 lines

The patch is comitted in printk/linux.git, branch for-5.18.

Best Regards,
Petr