printk: Remove console options before decoding the name
PATCH 1/1 printk: Remove console options before decoding the name
This fixes a regression when a console option includes ':'. Commit 7640f1a44eba ("printk: Add match_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.
Fix by handling the options first which will add a NULL terminator to the string.
--- kernel/printk/printk.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
kernel/printk/printk.c +5 -7
@@ -2646,24 +2646,22 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* Decode str into name, index, options */ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str); - options =strchr (str,',' ); -if (options) - *(options++) =0 ; -#ifdef __sparc__if (!strcmp (str,"ttya" ))strscpy (buf,"ttyS0" );
Signature · 1 line
2.53.0
kernel/printk/printk.c static int __init console_setup(char *str) · line 2653
Quoted 11 lines · David Engraf
kernel/printk/printk.c +5
-0@@ -2646,24 +2646,22 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* Decode str into name, index, options */ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +
How about update the comment for why it needs to be first?
Maybe something like:
Decode str into options first. The options may contain a ':' used also for DEVNAME.
On 16.09.26 08:48 wrote Tony Lindgren:
Quoted 15 lines · David Engraf
kernel/printk/printk.c static int __init console_setup(char *str) · line 2653
Quoted 11 lines
kernel/printk/printk.c +5
-0@@ -2646,24 +2646,22 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* Decode str into name, index, options */ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +How about update the comment for why it needs to be first?
Quoted 4 lines
Maybe something like:
Decode str into options first. The options may contain a ':' used also for DEVNAME.
Okay I can update the comment if there are no other objections.
Best regards
- David
PATCH v2 1/1 printk: Remove console options before decoding the name
This fixes a regression when a console option includes ':'. Commit 7640f1a44eba ("printk: Add match_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.
Fix by handling the options first which will add a NULL terminator to the string.
--- kernel/printk/printk.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/printk/printk.c +8 -7
@@ -2646,24 +2646,25 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* +* Decode str into name, index and options. Start with options, since +* it might also contain a ':' used for DEVNAME. +*/ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str); - options =strchr (str,',' ); -if (options) - *(options++) =0 ; -#ifdef __sparc__if (!strcmp (str,"ttya" ))strscpy (buf,"ttyS0" );
Signature · 1 line
2.53.0
Quoted 5 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.
Just curious, which console did you hit this issue with?
In any case, thanks for updating the comments:
On 17.09.26 17:03 wrote Tony Lindgren:
Quoted 8 lines · David Engraf
Quoted 5 lines
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Just curious, which console did you hit this issue with?
It's a self-developed console driver to access our hypervisor.
Quoted 3 lines
In any case, thanks for updating the comments:
Reviewed-by Tony Lindgren <[email protected]>
Thanks
- David
Quoted 11 lines · David Engraf
On 17.09.26 17:03 wrote Tony Lindgren:
Quoted 8 lines · David Engraf
Quoted 5 lines
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Just curious, which console did you hit this issue with?
It's a self-developed console driver to access our hypervisor.
OK thanks. From a "fix or feature" point of view, I wonder if this issue can happen with some of the current Linux console drivers too?
On 21.09.26 08:17 wrote Tony Lindgren:
Quoted 9 lines · David Engraf
Quoted 8 lines
On 17.09.26 17:03 wrote Tony Lindgren:
Quoted 7 lines · David Engraf
Quoted 5 lines
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Just curious, which console did you hit this issue with?
It's a self-developed console driver to access our hypervisor.
OK thanks. From a "fix or feature" point of view, I wonder if this issue can happen with some of the current Linux console drivers too?
AFAIK there is no restriction using ':' in the console options even if I don't know any in-kernel driver using it. That's why I would say it's a bug fix.
Best regards
- David
Petr Mladek
maintainerQuoted 33 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string.
Signed-off-by David Engraf <[email protected]>--- kernel/printk/printk.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)kernel/printk/printk.c +8
-0@@ -2646,24 +2646,25 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* +* Decode str into name, index and options. Start with options, since +* it might also contain a ':' used for DEVNAME. +*/ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;
kernel/printk/printk.c static int __init console_setup(char *str) · line 2666
Quoted 10 lines
kernel/printk/printk.c +0
-3else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str);
Sashiko AI has the following comment:
| Does moving the options parsing and null-termination earlier in this function | leave the loop below with an unreachable condition? | | Since str is now truncated at the first comma before being copied into buf, | buf will never contain a comma. This means the comma check inside the loop | over buf appears to be structurally impossible to satisfy: | | for (s = buf; *s; s++) | if ((ttyname && isdigit(*s)) || *s == ',') | break; | | Can the comma check be safely removed from the loop condition?
And it is right. The original code copied the original string into "buf". The new does not copy the options any longer.
It would deserve some refactoring to make the code cleaner. Something like:
kernel/printk/printk.c +11 -10
@@ -2672,17 +2672,18 @@ static int __init console_setup(char *str)strscpy (buf,"ttyS1" );#endif -for (s = buf; *s; s++) -if ((ttyname &&isdigit (*s)) || *s ==',' ) -break ; - -/* @idx will get defined when devname matches. */ -if (devname) - idx =-1 ; -else +if (ttyname) { +/* Detect @idx in ttyname and remove it. */ +for (s = buf; *s; s++) { +if (isdigit (*s)) +break ; + } idx =simple_strtoul (s,NULL ,10 ); - - *s =0 ; + *s =0 ; + }else { +/* @idx will get defined when devname matches. */ + idx =-1 ; + }__add_preferred_console (ttyname, idx, devname, options, brl_options, true);return 1 ;
I see two possibilities. We could either merge this cleanup into the original patch and send v3. Or we could add it on top of the original patch.
I would slightly prefer v3 and have both changes in a single patch.
Best Regards,
Petr
Quoted 60 lines · Petr Mladek
Quoted 33 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string.
Signed-off-by David Engraf <[email protected]>--- kernel/printk/printk.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)kernel/printk/printk.c +8
-0@@ -2646,24 +2646,25 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* +* Decode str into name, index and options. Start with options, since +* it might also contain a ':' used for DEVNAME. +*/ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;kernel/printk/printk.c static int __init console_setup(char *str) · line 2666
Quoted 10 lines
kernel/printk/printk.c +0
-3else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str);Sashiko AI has the following comment:
| Does moving the options parsing and null-termination earlier in this function | leave the loop below with an unreachable condition? | | Since str is now truncated at the first comma before being copied into buf, | buf will never contain a comma. This means the comma check inside the loop | over buf appears to be structurally impossible to satisfy: | | for (s = buf; *s; s++) | if ((ttyname && isdigit(*s)) || *s == ',') | break; | | Can the comma check be safely removed from the loop condition?
Quoted 2 lines
And it is right. The original code copied the original string into "buf". The new does not copy the options any longer.
OK
Quoted 26 lines
It would deserve some refactoring to make the code cleaner. Something like:
kernel/printk/printk.c +6
-8@@ -2672,17 +2672,18 @@ static int __init console_setup(char *str)strscpy (buf,"ttyS1" );#endif -for (s = buf; *s; s++) -if ((ttyname &&isdigit (*s)) || *s ==',' ) -break ; - -/* @idx will get defined when devname matches. */ -if (devname) - idx =-1 ; -else +if (ttyname) { +/* Detect @idx in ttyname and remove it. */ +for (s = buf; *s; s++) { +if (isdigit (*s)) +break ; + } idx =simple_strtoul (s,NULL ,10 );
kernel/printk/printk.c static int __init console_setup(char *str) · line 2689
Quoted 10 lines
kernel/printk/printk.c +5
-2- - *s =0 ; + *s =0 ; + }else { +/* @idx will get defined when devname matches. */ + idx =-1 ; + }__add_preferred_console (ttyname, idx, devname, options, brl_options, true);return 1 ;
Nice, you could now initialize idx = -1 to start with to leave out the else for setting devname idx?
Quoted 5 lines
I see two possibilities. We could either merge this cleanup into the original patch and send v3. Or we could add it on top of the original patch.
I would slightly prefer v3 and have both changes in a single patch.
Having a v3 sounds good to me.
Petr Mladek
maintainerQuoted 93 lines · Tony Lindgren
Quoted 53 lines · Petr Mladek
Quoted 26 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string.
kernel/printk/printk.c +8
-0@@ -2646,24 +2646,25 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* +* Decode str into name, index and options. Start with options, since +* it might also contain a ':' used for DEVNAME. +*/ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;kernel/printk/printk.c static int __init console_setup(char *str) · line 2666
Quoted 10 lines
kernel/printk/printk.c +0
-3else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str);Sashiko AI has the following comment:
| Does moving the options parsing and null-termination earlier in this function | leave the loop below with an unreachable condition? | | Since str is now truncated at the first comma before being copied into buf, | buf will never contain a comma. This means the comma check inside the loop | over buf appears to be structurally impossible to satisfy: | | for (s = buf; *s; s++) | if ((ttyname && isdigit(*s)) || *s == ',') | break; | | Can the comma check be safely removed from the loop condition?Quoted 2 lines
And it is right. The original code copied the original string into "buf". The new does not copy the options any longer.
OK
Quoted 34 lines
It would deserve some refactoring to make the code cleaner. Something like:
kernel/printk/printk.c +11
-10@@ -2672,17 +2672,18 @@ static int __init console_setup(char *str)strscpy (buf,"ttyS1" );#endif -for (s = buf; *s; s++) -if ((ttyname &&isdigit (*s)) || *s ==',' ) -break ; - -/* @idx will get defined when devname matches. */ -if (devname) - idx =-1 ; -else +if (ttyname) { +/* Detect @idx in ttyname and remove it. */ +for (s = buf; *s; s++) { +if (isdigit (*s)) +break ; + } idx =simple_strtoul (s,NULL ,10 ); - - *s =0 ; + *s =0 ; + }else { +/* @idx will get defined when devname matches. */ + idx =-1 ; + }
Quoted 5 lines
kernel/printk/printk.c static int __init console_setup(char *str) · line 2689
Quoted 2 lines
kernel/printk/printk.c +0
-0__add_preferred_console (ttyname, idx, devname, options, brl_options, true);return 1 ;Nice, you could now initialize idx = -1 to start with to leave out the else for setting devname idx?
I would personally prefer to keep the else part because it makes it clear how the "devname" variant is handled. But I could live without it as well.
Quoted 7 lines
Quoted 5 lines
I see two possibilities. We could either merge this cleanup into the original patch and send v3. Or we could add it on top of the original patch.
I would slightly prefer v3 and have both changes in a single patch.
Having a v3 sounds good to me.
Great.
Best Regards,
Petr
Quoted 7 lines · Petr Mladek
Quoted 2 lines · Tony Lindgren
Nice, you could now initialize idx = -1 to start with to leave out the else for setting devname idx?
I would personally prefer to keep the else part because it makes it clear how the "devname" variant is handled. But I could live without it as well.
OK thanks, that works just fine for me.
On 23.09.26 15:14 wrote Tony Lindgren:
Quoted 10 lines · Petr Mladek
Quoted 7 lines · Tony Lindgren
Quoted 2 lines
Nice, you could now initialize idx = -1 to start with to leave out the else for setting devname idx?
I would personally prefer to keep the else part because it makes it clear how the "devname" variant is handled. But I could live without it as well.
OK thanks, that works just fine for me.
Thanks for the review. I'm going to prepare v3 including Petr's changes.
Best regards
- David
PATCH v3 1/1 printk: Remove console options before decoding the name
This fixes a regression when a console option includes ':'. Commit 7640f1a44eba ("printk: Add match_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.
Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
--- kernel/printk/printk.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-)
kernel/printk/printk.c +19 -17
@@ -2646,24 +2646,25 @@ static int __init console_setup(char *str)if (_braille_console_setup (&str, &brl_options))return 1 ; +/* +* Decode str into name, index and options. Start with options, since +* it might also contain a ':' used for DEVNAME. +*/ + options =strchr (str,',' ); +if (options) + *(options++) =0 ; +/* For a DEVNAME:0.0 style console the character device is unknown early */ if (strchr (str,':' )) devname = buf;else ttyname = buf; -/* -* Decode str into name, index, options. -*/ if (ttyname &&isdigit (str[0 ]))scnprintf (buf,sizeof (buf),"ttyS%s" , str);else strscpy (buf, str); - options =strchr (str,',' ); -if (options) - *(options++) =0 ; -#ifdef __sparc__if (!strcmp (str,"ttya" ))strscpy (buf,"ttyS0" );
@@ -2671,17 +2672,18 @@ static int __init console_setup(char *str)strscpy (buf,"ttyS1" );#endif -for (s = buf; *s; s++) -if ((ttyname &&isdigit (*s)) || *s ==',' ) -break ; - -/* @idx will get defined when devname matches. */ -if (devname) - idx =-1 ; -else +if (ttyname) { +/* Detect @idx in ttyname and remove it. */ +for (s = ttyname; *s; s++) { +if (isdigit (*s)) +break ; + } idx =simple_strtoul (s,NULL ,10 ); - - *s =0 ; + *s =0 ; + }else { +/* @idx will get defined when devname matches. */ + idx =-1 ; + }__add_preferred_console (ttyname, idx, devname, options, brl_options, true);return 1 ;
Signature · 1 line
2.53.0
Quoted 9 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
Maybe clarify the above a bit to make it clear that the refactoring is needed for the fix. Something like:
Fix by handling the options first which will add a NULL terminator to the string. Note that parsing the options first means that also idx parsing needs changing. Handle the idx parsing by refactoring the code (thanks to Petr Mladek).
Other than that:
On 24.09.26 10:42 wrote Tony Lindgren:
Quoted 14 lines · David Engraf
Quoted 9 lines
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
Maybe clarify the above a bit to make it clear that the refactoring is needed for the fix. Something like:
Quoted 8 lines
Fix by handling the options first which will add a NULL terminator to the string. Note that parsing the options first means that also idx parsing needs changing. Handle the idx parsing by refactoring the code (thanks to Petr Mladek).
Other than that:
Reviewed-by Tony Lindgren <[email protected]>
Sure I can update the description. Should I create a v4 with the updated description and your review tag or how to proceed?
Best regards
- David
Quoted 11 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
Signed-off-by David Engraf <[email protected]>
Looks good to me:
There is no need to send v4 just to update the commit message. I could do so when committing the change.
I am going to wait one or two more days for more potential feedback. Then I'll push it with the updated commit message unless anyone complains in the meantime.
Best Regards,
Petr
Quoted 11 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
Signed-off-by David Engraf <[email protected]>
Petr Mladek
maintainerQuoted 11 lines · David Engraf
This fixes a regression when a console option includes ':'. Commit
7640f1a44eba("printk: Addmatch_devname_and_update_preferred_console()") introduced console=DEVNAME:0.0 hardware style addressing by looking for a colon. If the colon is part of an option the name is handled as devname instead of ttyname.Fix by handling the options first which will add a NULL terminator to the string and refactor idx handling to clean up the code (thanks to Petr Mladek).
Signed-off-by David Engraf <[email protected]>
JFYI, the patch has been comitted into printk/linux.git, branch for-7.4.
Note that I have updated the commit message as suggested by Tony, see
https://git.kernel.org/pub/scm/linux/kernel/git/printk/linux.git/commit/?id=b80fc54890d44cd35360ca122b72224fe78061bf
Best Regards,
Petr