printk: Clean up preferred console handling
Petr Mladek
maintainerv7 cover printk: Clean up preferred console handling
Hi,
this patchset does some clean up of the code for handling preferred consoles in the console registration code.
It is a prerequisite some more patchsets, namely:
+ for Marcos' clean up of CON_ENABLE
flag handling. It should prevent regressions caused by the
hidden effects of try_enable_preferred_console(), for example,
see https://lore.kernel.org/r/[email protected]
+ for Chris' per-console log-level, see
https://lore.kernel.org/r/[email protected]
and maybe even some more.
Changes against [v6]:
+ Hopefully fixed the formatt of the new function documentation (4th patch) [Chris, Sashiko]
+ Switch order for the handling of @name and @devname for new entries to follow the ordering of function parameters (4th patch) [Petr]
+ Add missing SOB (5th patch) [Petr]
+ Do not do the refactoring. Which just complicated the logic later. [Petr]
+ Make the check of @idx for @devname symmetric with the check for @name. Allow any negative value instead of hard-coded -1 (7th patch) [Petr]
+ Keep the check of the new entries separate. It should prevent the problem with the cross-update. (8th patch) [Sashiko]
+ Update Braille options when the same console is preferred more times (new 14th patch) [Sashiko]
Changes against [v5]:
+ Updated comment about preferred_consoles[] (1st patch) [Chris]
+ Removed extra newline (1st patch) [Chris]
+ Added back two comments (4th patch) [John]
+ Fixed kernel-doc formatting (4th patch) [Chris]
+ Added check for empty @name or @devname (5th patch (new)) [Chris]
+ Fixed typos (6th patch) [Chris]
+ Added back "[*]" (7th patch) [John]
+ Removed problematic patch which allowed to override console options via the command line (9th patch in v5)) [Chris]
+ Added comment about that console_call_setup() was made global only for Braille console driver. (11th patch) [Chris]
+ Reworked description of behavior changes when preferring
Braille over non-Braille preferred console entries (13th patch) [Chris]
+ Fixed comment: s/preferred_console/preferred_dev_console/ (13th patch) [Chris]
+ Optimize handling of preferred_dev_console list (14th patch) [Chris]
+ Added Reviewed-by and Acked-by tags.
Changes against [v4]:
+ Removed 1st patch from v4. Already in upstream.
+ Cleanup _braille_(un)register_console() wrappers already in 3rd patch. is_braille_console_preferred() will be needed sooner.
+ Split 4th patch. Start with reshufling the add_preferred_console() code without any functional changes. Add the various checks and behavior changes one by one is a followup patches.
+ Rework the code to make it clear when it is a new entry or an update.
+ Put back braille_set_options() which just updates the optional entry. The logic when the value should get updated is the same as for pc->options.
+ Make sure that options can be overridden by the command line.
+ Add preferred_dev_node to remember the ordering of preferred consoles. Use it when the last one becomes Braille. The previous version tried to remembered just the last preferred console. But the logic because pretty complicated when I tried to make it error proof. AI was able to find many scenarios where it broke. The list is
the ultimate solution and is easier in the end.
+ Use console_call_setup() in braille_register_console() instead of duplicating the code [John]
Changes against [v3]:
+ Move the handling of pre-enabled consoles into register_console() as 1st patch. It prevents a possible invalid access. [Petr]
+ Move the code for handling pre-enabled to console to try_enable_console() in 7th patch. The resulting code is the same as in v3.
This 2nd move was not needed in v3 because the code was moved to this final location later. [Petr]
+ Switch order of the 10th and 11th patch. Aka make try_enable_default_console() return an err before refactoring try_enable_console() call try_enable_preferred_console() only when there is any. [Petr]
+ Initialize err value in try_enable_console() in 11th patch. It prevents using a non-initialized variable. [AI:gemini-3]
+ Make sure that pre-enabled consoles won't get registered when something went wrong in try_enable_console() in 11th patch [Petr]
Changes against [v2]:
+ Move static_assert() after declaration (1st patch) [Steven]
+ Rename update_preferred_console() to add_or_update_preferred_console() [John]
+ WARN_ON() when called without "name" and "devname" [John]
+ print error when reached MAX_PREFERRED_CONSOLES
+ Add comment above code adding new entries [John]
(all in 4th patch)
+ Call console->setup() under console_lock() in braille_register_console() (new patch) [Sashiko]
+ Use "No functional changes." phrase in commit messages. [Steven]
+ Call try_enable_preferred_console() only when there is no preferred console (reworked and split 9th patch) [Petr]
+ Added taken Reviewed-by and Acked-by tags [John, Macros]
Changes against [v1]:
+ Fix stale comment (1st patch, Chris)
+ Add comment descibing update_preferred_console() behavior (3rd patch, John)
+ Better describe behavior changes caused by update_preferred_console() in the commit message (3rd patch, Chris)
+ Use unsigned int for iterating/indexing preferred_consoles[] table (3rd patch, John)
+ Add a check preventing an update via "devname" is already assigned to a real "name". (3rd patch, Petr)
+ Use only Braille entries when trying to enable Braille console (Chris)
+ Split all try_enable_*console() calls into new try_enable_console() and return early when try_enable_braille_console() succeeded. Note that we do not longer need to initialize "err" variable in this case. (new 5th patch, Chris, Petr)
+ Put back the explicit check of CON_BRL flag in register_console() so that it returns prelimitary only when the driver is used in Braille mode. (6th patch, Chris, Petr)
+ Add comments explaining that try_enable_braille_console() might succeeded also when newcon->match() succeeded. In this case, the Braille mode can't be used because the console device was already used as an early console.[*] (6th patch, Chris, Petr)
+ Reset preferred_dev_console_prev when used to revert the value. (7th patch, John)
+ Add Acked-by and Reviewed-by tags (1st, 2nd, 4th patch, Chris, Marcos)
[v1] https://lore.kernel.org/r/[email protected]
[v2] https://lore.kernel.org/r/[email protected]
[v3] https://lore.kernel.org/r/[email protected]
[v4] https://lore.kernel.org/r/[email protected]
[v5] https://lore.kernel.org/r/[email protected]
[v6] https://lore.kernel.org/r/[email protected]
Petr Mladek (17):
printk: Rename struct console_cmdline to preferred_console
printk: Rename preferred_console to preferred_dev_console
printk: Cleanup _braille_(un)register_console() wrappers
printk: Separate code for adding/updating preferred console metadata
printk: Do not accept an empty name or devname as a preferred console
printk: Prefer the console either via name or devname exclusively
printk: Check that index is not defined when a console is preferred
via devname
printk: Ensure consistent name and devname when updating preferred
console
printk: Do not quietly ignore adding entry for a preferred console
when there is no space
printk: Allow to debug misuse of add_or_update_preferred_console() console/braille: Lock console->setup() call during the registration
printk: Separate code for enabling console
printk: Try to register each console as Braille first
printk: Update Braille options when preferred more times
printk: Do not set Braille console as preferred_console
printk: Modify try_enable_default_console() to return error/success
printk: Try enable preferred consoles only when there are any
.../accessibility/braille/braille_console.c | 17 +-
include/linux/console.h | 1 +
kernel/printk/braille.c | 20 +-
kernel/printk/braille.h | 23 +-
.../{console_cmdline.h => console_register.h} | 9 +-
kernel/printk/printk.c | 438 ++++++++++++------
6 files changed, 348 insertions(+), 160 deletions(-)
rename kernel/printk/{console_cmdline.h => console_register.h} (74%)Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 1/17 printk: Rename struct console_cmdline to preferred_console
The structure 'console_cmdline' was originally intended to store details about consoles defined on the kernel command line. However, its usage has since expanded; it now stores information for consoles preferred via SPCR, device tree, or by particular platforms, e.g. XEN.
The current naming is misleading as it implies the configuration only originates from the command line.
Rename the structure and associated artifacts to better reflect their current purpose, for example:
- struct console_cmdline c -> struct preferred_console pc
- console_cmdline[] -> preferred_consoles[]
- console_cmdline.h -> console_register.h
- c -> pc
Additionally, renaming the header file to console_register.h would eventually allow to decouple console registration logic from the monolithic printk.c.
Finally, renaming the local variable from "c" to "pc" helps to distinguish it from struct console variables. Note that "c" is used for struct console in some code, for example see vt_console_device() function definition.
No functional changes.
---
kernel/printk/braille.c | 10 +--
kernel/printk/braille.h | 10 +--
.../{console_cmdline.h => console_register.h} | 6 +-
kernel/printk/printk.c | 89 ++++++++++---------
4 files changed, 58 insertions(+), 57 deletions(-)
rename kernel/printk/{console_cmdline.h => console_register.h} (83%)kernel/printk/braille.c +5 -5
@@ -6,7 +6,7 @@#include <linux/errno.h> #include <linux/string.h> -#include "console_cmdline.h" +#include "console_register.h" #include "braille.h" int _braille_console_setup (char **str,char **brl_options)
@@ -35,14 +35,14 @@ int _braille_console_setup(char **str, char **brl_options) }int -_braille_register_console (struct console *console,struct console_cmdline *c) +_braille_register_console (struct console *console,struct preferred_console *pc) {int rtn =0 ; -if (c->brl_options) { +if (pc->brl_options) { console->flags |=CON_BRL ; - rtn =braille_register_console (console, c->index, c->options, - c->brl_options); + rtn =braille_register_console (console, pc->index, pc->options, + pc->brl_options); }return rtn;
kernel/printk/braille.h +5 -5
@@ -5,9 +5,9 @@#ifdef CONFIG_A11Y_BRAILLE_CONSOLE static inline void -braille_set_options (struct console_cmdline *c,char *brl_options) +braille_set_options (struct preferred_console *pc,char *brl_options) { - c->brl_options = brl_options; + pc->brl_options = brl_options; }/*
@@ -21,7 +21,7 @@ int_braille_console_setup (char **str, char **brl_options);int -_braille_register_console (struct console *console,struct console_cmdline *c); +_braille_register_console (struct console *console,struct preferred_console *pc);int _braille_unregister_console (struct console *console);
@@ -29,7 +29,7 @@ _braille_unregister_console(struct console *console);#else static inline void -braille_set_options (struct console_cmdline *c,char *brl_options) +braille_set_options (struct preferred_console *pc,char *brl_options) { }
@@ -40,7 +40,7 @@ _braille_console_setup(char **str, char **brl_options) }static inline int -_braille_register_console (struct console *console,struct console_cmdline *c) +_braille_register_console (struct console *console,struct preferred_console *pc) {return 0 ; }
kernel/printk/console_register.h renamed from kernel/printk/console_cmdline.h +3 -3
@@ -1,8 +1,8 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _CONSOLE_CMDLINE_H -#define _CONSOLE_CMDLINE_H +#ifndef _CONSOLE_REGISTER_H +#define _CONSOLE_REGISTER_H -struct console_cmdline +struct preferred_console { char name[16]; /* Name of the driver */ int index; /* Minor dev. to use */
kernel/printk/printk.c +45 -44
183 lines not on this page · raw message
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 2/17 printk: Rename preferred_console to preferred_dev_console
In reply to Petr MladekThe preferred_consoles[] array stores information about consoles requested via the command line, SPCR, Device Tree, or platform-specific code. Within this array, the 'preferred_console' variable tracks the specific index that should be associated with /dev/console (typically the last non-braille console defined).
The current name "preferred_console" is ambiguous and leads to confusion. It does not clearly communicate why one console is "more preferred" than others in the array. Furthermore, entries for Braille consoles can exist within the preferred_consoles[] array, yet they are never associated with /dev/console and do not receive standard printk() output. Consequently, the 'preferred_console' index must skip these entries, which is not immediately obvious from the name.
Rename the variable to 'preferred_dev_console' to explicitly clarify its role in identifying which entry is linked to /dev/console.
No behavior change.
--- kernel/printk/printk.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/printk/printk.c +5 -5
@@ -365,7 +365,7 @@ static int console_locked;#define MAX_PREFERRED_CONSOLES 8static struct preferred_console preferred_consoles[MAX_PREFERRED_CONSOLES ]; -static int preferred_console =-1 ; +static int preferred_dev_console =-1 ;int console_set_on_cmdline;EXPORT_SYMBOL (console_set_on_cmdline);
@@ -2586,7 +2586,7 @@ static int __add_preferred_console(const char *name, const short idx,if ((name &&strcmp (pc->name, name) ==0 && pc->index == idx) || (devname &&strcmp (pc->devname, devname) ==0 )) {if (!brl_options) - preferred_console = i; + preferred_dev_console = i;set_user_specified (pc, user_specified);return 0 ; }
@@ -2594,7 +2594,7 @@ static int __add_preferred_console(const char *name, const short idx,if (i ==MAX_PREFERRED_CONSOLES )return -E2BIG ;if (!brl_options) - preferred_console = i; + preferred_dev_console = i;if (name)strscpy (pc->name, name);if (devname)
@@ -3935,7 +3935,7 @@ static int try_enable_preferred_console(struct console *newcon,return err; } newcon->flags |=CON_ENABLED ; -if (i == preferred_console) +if (i == preferred_dev_console) newcon->flags |=CON_CONSDEV ;return 0 ; }
@@ -4108,7 +4108,7 @@ void register_console(struct console *newcon)* Note that a console with tty binding will have CON_CONSDEV * flag set and will be first in the list. */ -if (preferred_console <0 ) { +if (preferred_dev_console <0 ) {if (hlist_empty (&console_list) || !console_first ()->device ||console_first ()->flags &CON_BOOT ) {try_enable_default_console (newcon);
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 3/17 printk: Cleanup _braille_(un)register_console() wrappers
In reply to Petr MladekThe _braille_(un)register_console() wrappers currently attempt to hide implementation details like the CON_BRL flag and pc->brl_options. This forces callers to handle an unconventional tri-state return value (0 for NOP, >0 for success, <0 for error), which makes the control flow harder to follow and non-standard.
Refactor the wrappers to use standard kernel return codes (0 for success, -ERRCODE on failure). Move the responsibility of checking brl_options to the caller to make the logic more explicit.
Additionally, move the assignment of the CON_BRL flag from the internal wrapper to braille_register_console(). This aligns it with how CON_ENABLED is handled. To maintain symmetry and fix a potential bug where flags might persist after removal, explicitly clear both CON_ENABLED and CON_BRL in braille_unregister_console().
No behavior change.
--- drivers/accessibility/braille/braille_console.c | 7 ++++--- kernel/printk/braille.c | 16 +++------------- kernel/printk/braille.h | 12 ++++++++++++ kernel/printk/printk.c | 13 +++++-------- 4 files changed, 24 insertions(+), 24 deletions(-)
drivers/accessibility/braille/braille_console.c +4 -3
@@ -413,12 +413,12 @@ int braille_register_console(struct console *console, int index,if (ret !=0 )return ret; } - console->flags |=CON_ENABLED ; + console->flags |=CON_ENABLED |CON_BRL ; console->index = index; braille_co = console;register_keyboard_notifier (&keyboard_notifier_block);register_vt_notifier (&vt_notifier_block); -return 1 ; +return 0 ; }int braille_unregister_console (struct console *console)
@@ -428,5 +428,6 @@ int braille_unregister_console(struct console *console)unregister_keyboard_notifier (&keyboard_notifier_block);unregister_vt_notifier (&vt_notifier_block); braille_co =NULL ; -return 1 ; + console->flags &= ~(CON_ENABLED |CON_BRL ); +return 0 ; }
kernel/printk/braille.c +3 -13
@@ -37,22 +37,12 @@ int _braille_console_setup(char **str, char **brl_options)int _braille_register_console (struct console *console,struct preferred_console *pc) { -int rtn =0 ; - -if (pc->brl_options) { - console->flags |=CON_BRL ; - rtn =braille_register_console (console, pc->index, pc->options, - pc->brl_options); - } - -return rtn; +return braille_register_console (console, pc->index, pc->options, + pc->brl_options); }int _braille_unregister_console (struct console *console) { -if (console->flags &CON_BRL ) -return braille_unregister_console (console); - -return 0 ; +return braille_unregister_console (console); }
kernel/printk/braille.h +12 -0
@@ -10,6 +10,12 @@ braille_set_options(struct preferred_console *pc, char *brl_options) pc->brl_options = brl_options; } +static inline bool +is_braille_console_preferred (struct preferred_console *pc) +{ +return (!!pc->brl_options); +} +/* * Setup console according to braille options. * Return -EINVAL on syntax error, 0 on success (or no braille option was
@@ -33,6 +39,12 @@ braille_set_options(struct preferred_console *pc, char *brl_options) { } +static inline bool +is_braille_console_preferred (struct preferred_console *pc) +{ +return false; +} +static inline int _braille_console_setup (char **str,char **brl_options) {
kernel/printk/printk.c +5 -8
@@ -3927,8 +3927,8 @@ static int try_enable_preferred_console(struct console *newcon,if (newcon->index <0 ) newcon->index = pc->index; -if (_braille_register_console (newcon, pc)) -return 0 ; +if (is_braille_console_preferred (pc)) +return _braille_register_console (newcon, pc); err =console_call_setup (newcon, pc->options);if (err)
@@ -4240,17 +4240,14 @@ static int unregister_console_locked(struct console *console)bool found_boot_con = false;unsigned long flags;struct console *c; -int res; +int res =0 ;lockdep_assert_console_list_lock_held ();con_printk (KERN_INFO , console,"disabled\n" ); - res =_braille_unregister_console (console); -if (res <0 ) -return res; -if (res >0 ) -return 0 ; +if (console->flags &CON_BRL ) +return _braille_unregister_console (console);if (!console_is_registered_locked (console)) res = -ENODEV ;
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 4/17 printk: Separate code for adding/updating preferred console metadata
In reply to Petr MladekThe logic for adding or updating a preferred console is currently duplicated within __add_preferred_console(), making the code difficult to follow and prone to consistency issues.
Split the code for adding or updating entries to avoid code duplication. Make it clear what operations are done only for new entries.
Use this opportunity and replace an int with an unsigned int for iterating over the preferred_consoles array.
It is a preparation step before adding more consistency checks. No intended functional changes.
--- kernel/printk/printk.c | 114 ++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 40 deletions(-)
kernel/printk/printk.c +74 -40
@@ -366,6 +366,7 @@ static int console_locked;static struct preferred_console preferred_consoles[MAX_PREFERRED_CONSOLES ];static int preferred_dev_console =-1 ; +/* Set when there is at least one console preferred via the command line. */ int console_set_on_cmdline;EXPORT_SYMBOL (console_set_on_cmdline);
@@ -2543,26 +2544,34 @@ asmlinkage __visible void early_printk(const char *fmt, ...) }#endif -static void set_user_specified (struct preferred_console *pc,bool user_specified) -{ -if (!user_specified) -return ; - -/* -* @pc console was defined by the user on the command line. -* Do not clear when added twice also by SPCR or the device tree. -*/ - pc->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 (const char *name,const short idx, -const char *devname,char *options, -char *brl_options,bool user_specified) +/** +* add_or_update_preferred_console - Add or update a given entry in +* the preferred_consoles[] array. +* @i: index of the entry in @preferred_consoles table which should get updated. +* @name: The name of the preferred console driver. +* @idx: Preferred console index, e.g. port number. +* @devname: The name of the preferred physical device. +* @options: Options used when setting up the console driver. +* @brl_options: Options used when setting up the console driver +* as a braille console. +* @user_specified: True if preferred via the kernel command line. +* +* The function adds or updates an entry in preferred_consoles[] array. Also +* it updates some global variables which are used to make the right decisions +* in register_console(). +*/ +static int add_or_update_preferred_console (unsigned int i, +const char *name,const short idx, +const char *devname,char *options, +char *brl_options,bool user_specified) {struct preferred_console *pc; -int i; +bool is_new_entry = false; + +if (i >=MAX_PREFERRED_CONSOLES ) +return -E2BIG ; + + pc = &preferred_consoles[i];if (!name && !devname)return -EINVAL ;
@@ -2576,35 +2585,60 @@ static int __add_preferred_console(const char *name, const short idx,if (name && idx <0 )return -EINVAL ; +if (!pc->name[0 ] && !pc->devname[0 ]) + is_new_entry = true; + +if (is_new_entry) { +if (name) +strscpy (pc->name, name); +if (devname) +strscpy (pc->devname, devname); + pc->index = idx; + } +/* -* See if this tty is not yet registered, and -* if we have a slot free. +* Historically, the preferred options are stored only by the first +* caller who prefers the particular console. */ +if (is_new_entry) { + pc->options = options; +braille_set_options (pc, brl_options); + } + +if (!brl_options) + preferred_dev_console = i; + +if (user_specified) { +/* +* @pc console was defined by the user on the command line. +* Do not clear when added twice also by SPCR or DT. +*/ + pc->user_specified = true; + console_set_on_cmdline =1 ; + } + +return 0 ; +} + +static int __add_preferred_console (const char *name,const short idx, +const char *devname,char *options, +char *brl_options,bool user_specified) +{ +struct preferred_console *pc; +unsigned int i; + +/* See if this console is already preferred. */ for (i =0 , pc = preferred_consoles; i <MAX_PREFERRED_CONSOLES && (pc->name[0 ] || pc->devname[0 ]); i++, pc++) { -if ((name &&strcmp (pc->name, name) ==0 && pc->index == idx) || - (devname &&strcmp (pc->devname, devname) ==0 )) { -if (!brl_options) - preferred_dev_console = i; -set_user_specified (pc, user_specified); -return 0 ; - } +if (name &&strcmp (pc->name, name) ==0 && pc->index == idx) +break ; +if (devname &&strcmp (pc->devname, devname) ==0 ) +break ; } -if (i ==MAX_PREFERRED_CONSOLES ) -return -E2BIG ; -if (!brl_options) - preferred_dev_console = i; -if (name) -strscpy (pc->name, name); -if (devname) -strscpy (pc->devname, devname); - pc->options = options; -set_user_specified (pc, user_specified); -braille_set_options (pc, brl_options); - pc->index = idx; -return 0 ; +return add_or_update_preferred_console (i, name, idx, devname, options, + brl_options, user_specified); }static int __init console_msg_format_setup (char *str)
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 5/17 printk: Do not accept an empty name or devname as a preferred console
In reply to Petr MladekThe current code for storing preferred consoles does not expect the case when an empty string is passed via @name or @devname.
Empty console driver name does not make any sense. Avoid handling such entries completely.
--- kernel/printk/printk.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/printk/printk.c +8 -1
@@ -2558,7 +2558,9 @@ asmlinkage __visible void early_printk(const char *fmt, ...)* * The function adds or updates an entry in preferred_consoles[] array. Also * it updates some global variables which are used to make the right decisions -* in register_console(). +* in register_console(). Finally, it enforces some rules: +* +* 1. Do not accept empty @name or @devname. */ static int add_or_update_preferred_console(unsigned int i, const char *name, const short idx,
@@ -2576,6 +2578,11 @@ static int add_or_update_preferred_console(unsigned int i,if (!name && !devname)return -EINVAL ; +if ((name && !name[0 ]) || (devname && !devname[0 ])) { +pr_err ("Adding a preferred console with an empty name\n" ); +return -EINVAL ; + } +/* * We use a signed short index for struct console for device drivers to * indicate a not yet assigned index or port. However, a negative index
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 6/17 printk: Prefer the console either via name or devname exclusively
In reply to Petr MladekThe commit 7640f1a44eba ("printk: Add match_devname_and_update_preferred_console()") added the possibility to prefer a console driver via device name. The name of the console driver gets assigned when the device gets initialized and it tries whether it is eligible for a console.
The above mentioned commit ensures that __add_preferred_console() is called at least with name or devname value. But it does not check whether both are defined which is a nonsense.
Add the check and do not fail quietly.
--- kernel/printk/printk.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/printk/printk.c +12 -0
@@ -2561,6 +2561,12 @@ asmlinkage __visible void early_printk(const char *fmt, ...)* in register_console(). Finally, it enforces some rules: * * 1. Do not accept empty @name or @devname. +* +* 2. Either @name OR @devname are allowed. [*] +* +* [*] A valid @name and @idx will get assigned for a devname later when +* @devname matches during the device initialization, see +* match_devname_and_update_preferred_console(). */ static int add_or_update_preferred_console(unsigned int i, const char *name, const short idx,
@@ -2583,6 +2589,12 @@ static int add_or_update_preferred_console(unsigned int i,return -EINVAL ; } +if (name && devname) { +pr_err ("Adding a preferred console devname with a hard-coded console name: %s, %s\n" , + devname, name); +return -EINVAL ; + } +/* * We use a signed short index for struct console for device drivers to * indicate a not yet assigned index or port. However, a negative index
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 7/17 printk: Check that index is not defined when a console is preferred via devname
In reply to Petr MladekThe current code makes sure that a valid index is defined when a console is preferred via a name.
On the contrary, the index must not be defined when the console is defined via a devname. The index will be defined later when the device matches and gets registered.
Enforce the 2nd rule. Make the checks symmetric in the code. Do not ignore the errors quietly.
--- kernel/printk/printk.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
kernel/printk/printk.c +22 -12
@@ -2562,7 +2562,7 @@ asmlinkage __visible void early_printk(const char *fmt, ...)* * 1. Do not accept empty @name or @devname. * -* 2. Either @name OR @devname are allowed. [*] +* 2. Either @name and valid @idx OR @devname and @idx=-1 are allowed. [*] * * [*] A valid @name and @idx will get assigned for a devname later when * @devname matches during the device initialization, see
@@ -2595,23 +2595,33 @@ static int add_or_update_preferred_console(unsigned int i,return -EINVAL ; } -/* -* We use a signed short index for struct console for device drivers to -* indicate a not yet assigned index or port. However, a negative index -* value is not valid when the console name and index are defined on -* the command line. -*/ -if (name && idx <0 ) -return -EINVAL ; -if (!pc->name[0 ] && !pc->devname[0 ]) is_new_entry = true;if (is_new_entry) { -if (name) +if (name) { +/* A console name must be defined with a valid index. */ +if (idx <0 ) { +pr_err ("Adding a preferred console with an invalid index: %s, %d\n" , + name, idx); +return -EINVAL ; + }strscpy (pc->name, name); -if (devname) + } + +if (devname) { +/* +* A valid console name and index will get assigned when +* a matching device gets registered. +*/ +if (idx >=0 ) { +pr_err ("Adding a preferred console devname with a hard-coded index: %s, %d\n" , + devname, idx); +return -EINVAL ; + }strscpy (pc->devname, devname); + } + pc->index = idx; }
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 8/17 printk: Ensure consistent name and devname when updating preferred console
In reply to Petr MladekMake sure that add_or_update_preferred_console() updates the right entry.
The current caller does the right thing. The new paranoid checks prevent a potential misuse of the API in the future.
--- kernel/printk/printk.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
kernel/printk/printk.c +15 -0
@@ -2564,6 +2564,8 @@ asmlinkage __visible void early_printk(const char *fmt, ...)* * 2. Either @name and valid @idx OR @devname and @idx=-1 are allowed. [*] * +* 3. Only matching entries can be updated. +* * [*] A valid @name and @idx will get assigned for a devname later when * @devname matches during the device initialization, see * match_devname_and_update_preferred_console().
@@ -2623,6 +2625,19 @@ static int add_or_update_preferred_console(unsigned int i, } pc->index = idx; + }else { +/* Make sure that only matching entries are updated. */ +if (name && (strcmp (pc->name, name) || (pc->index != idx))) { +pr_err ("Updating a preferred console with an invalid name or index: %s%d vs. %s%d\n" , + pc->name, pc->index, name, idx); +return -EINVAL ; + } + +if (devname &&strcmp (pc->devname, devname)) { +pr_err ("Updating a preferred console with an invalid devname: %s vs. %s\n" , + pc->devname, devname); +return -EINVAL ; + } }/*
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 9/17 printk: Do not quietly ignore adding entry for a preferred console when there is no space
In reply to Petr MladekIt is hard to imagine that anyone would want to add more than 8 preferred consoles. But never say never. And do not ignore new entries quietly.
--- kernel/printk/printk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/printk/printk.c +3 -1
@@ -2578,8 +2578,10 @@ static int add_or_update_preferred_console(unsigned int i,struct preferred_console *pc;bool is_new_entry = false; -if (i >=MAX_PREFERRED_CONSOLES ) +if (i >=MAX_PREFERRED_CONSOLES ) { +pr_err_once ("Reached maximal number of preferred consoles.\n" );return -E2BIG ; + } pc = &preferred_consoles[i];
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 10/17 printk: Allow to debug misuse of add_or_update_preferred_console()
In reply to Petr Mladek__add_preferred_console() should always be called only when either @name or @devname is defined. Allow to debug the case when they are misused.
Use WARN_ON() because it is rather an implementation problem, it should never happen. Also the consoles are typically added during boot so this should not panic a running system even when panic_on_warn is enabled.
--- kernel/printk/printk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk.c +1 -1
@@ -2585,7 +2585,7 @@ static int add_or_update_preferred_console(unsigned int i, pc = &preferred_consoles[i]; -if (!name && !devname) +if (WARN_ON (!name && !devname))return -EINVAL ;if ((name && !name[0 ]) || (devname && !devname[0 ])) {
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 11/17 console/braille: Lock console->setup() call during the registration
In reply to Petr MladekIn braille_register_console(), the console->setup() callback must be called while holding the console_lock. This prevents a race condition with a possible active boot console that might be using the same hardware registers.
See also the commit 801410b26a0e ("serial: Lock console when calling into driver before registration").
--- drivers/accessibility/braille/braille_console.c | 10 +++++----- include/linux/console.h | 1 + kernel/printk/printk.c | 8 +++++++- 3 files changed, 13 insertions(+), 6 deletions(-)
drivers/accessibility/braille/braille_console.c +5 -5
@@ -408,11 +408,11 @@ int braille_register_console(struct console *console, int index, console_options ="57600o8" ;if (braille_co)return -ENODEV ; -if (console->setup) { - ret = console->setup (console, console_options); -if (ret !=0 ) -return ret; - } + + ret =console_call_setup (console, console_options); +if (ret !=0 ) +return ret; + console->flags |=CON_ENABLED |CON_BRL ; console->index = index; braille_co = console;
include/linux/console.h +1 -0
@@ -710,6 +710,7 @@ enum con_flush_mode { };extern int add_preferred_console (const char *name,const short idx,char *options); +extern int console_call_setup (struct console *newcon,char *options);extern void console_force_preferred_locked (struct console *con);extern void register_console (struct console *);extern int unregister_console (struct console *);
kernel/printk/printk.c +7 -1
@@ -3960,7 +3960,13 @@ static int __init keep_bootcon_setup(char *str)early_param ("keep_bootcon" , keep_bootcon_setup); -static int console_call_setup (struct console *newcon,char *options) +/* +* The function is intended only for the generic console registration code. +* It has been made global only for the Braille console driver which +* is partly using the generic console registration code but it is +* integrated with VT code instead of printk(). +*/ +int console_call_setup (struct console *newcon,char *options) {int err;
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 12/17 printk: Separate code for enabling console
In reply to Petr MladekThere are several code paths which try to enable a newly registered console. Move the logic into a separate try_enable_console() function.
It simplifies a bit the long register_console() function definition.
Also followup patches are going to add even more code paths. And it will be easier to use "return" when it does not make sense to try other variants.
No behavior change.
--- kernel/printk/printk.c | 84 ++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 36 deletions(-)
kernel/printk/printk.c +48 -36
@@ -4044,6 +4044,53 @@ static void try_enable_default_console(struct console *newcon) newcon->flags |=CON_CONSDEV ; } +#define console_first () \ + hlist_entry(console_list.first, struct console, node) + +static int try_enable_console (struct console *newcon) +{ +int err; + +/* +* See if we want to enable this console driver by default. +* +* Nope when a console is preferred by the command line, device +* tree, or SPCR. +* +* The first real console with tty binding (driver) wins. More +* consoles might get enabled before the right one is found. +* +* Note that a console with tty binding will have CON_CONSDEV +* flag set and will be first in the list. +*/ +if (preferred_dev_console <0 ) { +if (hlist_empty (&console_list) || !console_first ()->device || +console_first ()->flags &CON_BOOT ) { +try_enable_default_console (newcon); + } + } + +/* See if this console matches one we selected on the command line */ + err =try_enable_preferred_console (newcon, true); +if (err != -ENOENT ) +return err; + +/* If not, try to match against the platform default(s) */ + err =try_enable_preferred_console (newcon, false); +if (err != -ENOENT ) +return err; + +/* +* Some consoles, such as pstore and netconsole, can be enabled even +* without matching. Accept them at this stage when they had a chance +* to match() and call setup(). +*/ +if (newcon->flags &CON_ENABLED ) + err =0 ; + +return err; +} +/* Return the starting sequence number for a newly registered console. */ static u64 get_init_console_seq (struct console *newcon,bool bootcon_registered) {
@@ -4118,9 +4165,6 @@ static u64 get_init_console_seq(struct console *newcon, bool bootcon_registered)return init_seq; } -#define console_first () \ - hlist_entry(console_list.first, struct console, node) -static int unregister_console_locked (struct console *console);/*
@@ -4182,39 +4226,7 @@ void register_console(struct console *newcon) goto unlock; } -/* -* See if we want to enable this console driver by default. -* -* Nope when a console is preferred by the command line, device -* tree, or SPCR. -* -* The first real console with tty binding (driver) wins. More -* consoles might get enabled before the right one is found. -* -* Note that a console with tty binding will have CON_CONSDEV -* flag set and will be first in the list. -*/ -if (preferred_dev_console <0 ) { -if (hlist_empty (&console_list) || !console_first ()->device || -console_first ()->flags &CON_BOOT ) { -try_enable_default_console (newcon); - } - } - -/* See if this console matches one we selected on the command line */ - err =try_enable_preferred_console (newcon, true); - -/* If not, try to match against the platform default(s) */ -if (err == -ENOENT ) - err =try_enable_preferred_console (newcon, false); - -/* -* Some consoles, such as pstore and netconsole, can be enabled even -* without matching. Accept them at this stage when they had a chance -* to match() and call setup(). -*/ -if (err == -ENOENT && (newcon->flags &CON_ENABLED )) - err =0 ; + err =try_enable_console (newcon);/* printk() messages are not printed to the Braille console. */ if (err || newcon->flags &CON_BRL ) {
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 13/17 printk: Try to register each console as Braille first
In reply to Petr MladekBraille consoles are unique: they can only be enabled via the command line and use the preferred console framework for initialization, yet they are never added to the console_list or associated with /dev/console. Because of this, the preferred_console variable remains unset when only a Braille console is requested.
Currently, try_enable_preferred_console() must be called even when "preferred_dev_console" variable is not set, just to catch these "hidden" Braille requests.
Refactor the logic by adding a parameter to try_enable_preferred_console() to explicitly handle Braille vs. non-Braille cases. It will eventually allow to skip try_enable_preferred_console() when there are no preferred consoles. This improves code robustness by ensuring the console setup is explicit and only performed once.
Note that _braille_register_console() is skipped when the given console driver has a match() callback and it succeeded. It means that it took over an early console and can't be used as a Braille console.
As a result try_enable_braille_console() might return success even when it did not enabled the console in the Braille mode. It is the reason why register_console() has to explicitly check whether CON_BRL flag was really set and could skip the further steps.
Behavior change:
1. The Braille console driver is newly always preferred even when different serial ports are preferred. For example, let's have the following command lines:
a) console=ttyS1,115200 console=brl,ttyS0,115200
b) console=00:00:0.0,115200 console=brl,ttyS0,115200
c) console=brl,ttyS0,115200 console=ttyS1,115200They all create two entries in preferred_consoles[] because:
a) and c) differ in @index (0 vs. 1).
b) defines either @name or @devname.
But the original code registers:
a) ttyS1 as normal console because it is the 1st entry in preferred_consoles[].
b) ttyS0 are Braille console because the generic "ttyS" driver is registered early in univ8250_console_init(). The @devname entry is resolved too late when serial8250_init() registers all found devices, it assigns "tty0" for the given "00:00:0.0" but it is already in use.
c) ttyS0 as Braille console because it is the 1st entry in preferred_consoles[].
The new code would register ttyS0 as the Braille console in all three variants because try_enable_braille_console() is called 1st.
2. When only the Braille console is defined on the command line, the original code might attempt to enable the same console twice. One as a default and once as a Braille console. This results in calling the setup() callback twice and incorrectly setting the CON_CONSDEV flag.
The 1st change makes the behavior more consistent and understandable. The 2nd change is an obvious bug fix.
--- kernel/printk/printk.c | 58 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 6 deletions(-)
kernel/printk/printk.c +52 -6
@@ -366,6 +366,7 @@ static int console_locked;static struct preferred_console preferred_consoles[MAX_PREFERRED_CONSOLES ];static int preferred_dev_console =-1 ; +static bool want_braille_console;/* Set when there is at least one console preferred via the command line. */ int console_set_on_cmdline;EXPORT_SYMBOL (console_set_on_cmdline);
@@ -2651,7 +2652,9 @@ static int add_or_update_preferred_console(unsigned int i,braille_set_options (pc, brl_options); } -if (!brl_options) +if (brl_options) + want_braille_console = true; +else preferred_dev_console = i;if (user_specified) {
@@ -3987,8 +3990,9 @@ int console_call_setup(struct console *newcon, char *options)* by either the command line or add_preferred_console() and * setup/enable it. */ -static int try_enable_preferred_console (struct console *newcon, -bool user_specified) +static int __try_enable_preferred_console (struct console *newcon, +bool user_specified, +bool try_only_braille) {struct preferred_console *pc;int i, err;
@@ -3999,8 +4003,21 @@ static int try_enable_preferred_console(struct console *newcon,/* Console not yet initialized? */ if (!pc->name[0 ])continue ; -if (pc->user_specified != user_specified) -continue ; + +/* +* @try_only_braille and @user_specified define which +* preferred console entries are handled in this round. +*/ +if (try_only_braille) { +if (!is_braille_console_preferred (pc)) +continue ; + }else { +if (is_braille_console_preferred (pc)) +continue ; +if (pc->user_specified != user_specified) +continue ; + } +if (!newcon->match || newcon->match (newcon, pc->name, pc->index, pc->options) !=0 ) {/* default matching */
@@ -4013,7 +4030,7 @@ static int try_enable_preferred_console(struct console *newcon,if (newcon->index <0 ) newcon->index = pc->index; -if (is_braille_console_preferred (pc)) +if (try_only_braille)return _braille_register_console (newcon, pc); err =console_call_setup (newcon, pc->options);
@@ -4029,6 +4046,17 @@ static int try_enable_preferred_console(struct console *newcon,return -ENOENT ; } +static int try_enable_preferred_console (struct console *newcon, +bool user_specified) +{ +return __try_enable_preferred_console (newcon, user_specified, false); +} + +static int try_enable_braille_console (struct console *newcon) +{ +return __try_enable_preferred_console (newcon, true, true); +} +/* Try to enable the console unconditionally */ static void try_enable_default_console (struct console *newcon) {
@@ -4051,6 +4079,24 @@ static int try_enable_console(struct console *newcon) {int err; +/* +* First, try to enable the console driver as a Braille console. +* It would have metadata in the preferred_consoles[] array. +* But it won't be counted as preferred_dev_console because +* it does not get printk() messages and is not associated +* with /dev/console. +* +* Note that it might succeed also when the driver has a match() +* callback and it took over a boot console. In this case, +* the driver will continue working as a classic non-Braille +* console. +*/ +if (want_braille_console) { + err =try_enable_braille_console (newcon); +if (err != -ENOENT ) +return err; + } +/* * See if we want to enable this console driver by default. *
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 14/17 printk: Update Braille options when preferred more times
In reply to Petr MladekThe current behavior is not consistent when the same console driver is preferred several times as Braille or non-Braille console.
In particular, preferred_dev_console is not updated when @brl_options are passed. But @brl_options are stored only it is a new entry in preferred_consoles[].
It is even more hairy because preferred_console[] entry is considered a Braille entry only when .brl->options are defined.
Make the behavior consistent and always update pc->brl_options when @brl_options are passed and they were not defined yet.
--- kernel/printk/braille.h | 7 ++++--- kernel/printk/printk.c | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-)
kernel/printk/braille.h +4 -3
@@ -5,9 +5,10 @@#ifdef CONFIG_A11Y_BRAILLE_CONSOLE static inline void -braille_set_options (struct preferred_console *pc,char *brl_options) +braille_update_options (struct preferred_console *pc,char *brl_options) { - pc->brl_options = brl_options; +if (!pc->brl_options || !pc->brl_options[0 ]) + pc->brl_options = brl_options; } static inline bool
@@ -35,7 +36,7 @@ _braille_unregister_console(struct console *console);#else static inline void -braille_set_options (struct preferred_console *pc,char *brl_options) +braille_update_options (struct preferred_console *pc,char *brl_options) { }
kernel/printk/printk.c +9 -3
@@ -2647,10 +2647,16 @@ static int add_or_update_preferred_console(unsigned int i,* Historically, the preferred options are stored only by the first * caller who prefers the particular console. */ -if (is_new_entry) { +if (is_new_entry) pc->options = options; -braille_set_options (pc, brl_options); - } + +/* +* Allow to override normal console with the Braille variant, +* for example when ttyS0 is preferred via SPCR brl,ttyS0 +* via the command line. +*/ +if (brl_options) +braille_update_options (pc, brl_options);if (brl_options) want_braille_console = true;
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 15/17 printk: Do not set Braille console as preferred_console
In reply to Petr MladekThe Braille console reuses the framework for handling consoles preferred via the command line. However, it is a special-purpose interface that neither receives standard printk messages nor associates with /dev/console.
Currently, the "preferred_dev_console" variable can point to a Braille console entry in the "preferred_consoles[]" array. This occurs if an entry was first created for a non-Braille console, but a later 'console=' parameter redefined it as a Braille console.
Since a Braille console will only ever be enabled as such, it should not be tracked as the primary system console. Adjust the logic to ensure "preferred_dev_console" continues to point to the previously designated normal console instead.
--- kernel/printk/console_register.h | 3 +++ kernel/printk/printk.c | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-)
kernel/printk/console_register.h +3 -0
@@ -2,6 +2,8 @@ #ifndef _CONSOLE_REGISTER_H #define _CONSOLE_REGISTER_H +#include <linux/list.h> + struct preferred_console { char name[16]; /* Name of the driver */
@@ -12,6 +14,7 @@ struct preferred_console#ifdef CONFIG_A11Y_BRAILLE_CONSOLE char *brl_options;/* Options for braille driver */ #endif + struct list_head preferred_dev_node; };#endif
kernel/printk/printk.c +24 -7
@@ -365,7 +365,7 @@ static int console_locked;#define MAX_PREFERRED_CONSOLES 8static struct preferred_console preferred_consoles[MAX_PREFERRED_CONSOLES ]; -static int preferred_dev_console =-1 ; +static LIST_HEAD (preferred_dev_console);static bool want_braille_console;/* Set when there is at least one console preferred via the command line. */ int console_set_on_cmdline;
@@ -2567,9 +2567,16 @@ asmlinkage __visible void early_printk(const char *fmt, ...)* * 3. Only matching entries can be updated. * +* 4. Braille console is never associated with /dev/console.[**] +* * [*] A valid @name and @idx will get assigned for a devname later when * @devname matches during the device initialization, see * match_devname_and_update_preferred_console(). +* +* [**] Braille console is using the mechanism for registering consoles +* but it is very special. It is primarily used for user interaction +* with the system. It neither gets printk() messages nor is +* associated with /dev/console. */ static int add_or_update_preferred_console(unsigned int i, const char *name, const short idx,
@@ -2658,10 +2665,20 @@ static int add_or_update_preferred_console(unsigned int i,if (brl_options)braille_update_options (pc, brl_options); -if (brl_options) +if (is_new_entry) +INIT_LIST_HEAD (&pc->preferred_dev_node); + +/* +* The last preferred console should get associated with /dev/console. +* Except for the Braille console which can't get associated with +* /dev/console. +*/ +if (brl_options) { want_braille_console = true; -else - preferred_dev_console = i; +list_del_init (&pc->preferred_dev_node); + }else if (!is_braille_console_preferred (pc)) { +list_move (&pc->preferred_dev_node, &preferred_dev_console); + }if (user_specified) {/*
@@ -4044,7 +4061,7 @@ static int __try_enable_preferred_console(struct console *newcon,return err; } newcon->flags |=CON_ENABLED ; -if (i == preferred_dev_console) +if (list_is_first (&pc->preferred_dev_node, &preferred_dev_console)) newcon->flags |=CON_CONSDEV ;return 0 ; }
@@ -4088,7 +4105,7 @@ static int try_enable_console(struct console *newcon)/* * First, try to enable the console driver as a Braille console. * It would have metadata in the preferred_consoles[] array. -* But it won't be counted as preferred_dev_console because +* But it won't be listed as preferred_dev_console because * it does not get printk() messages and is not associated * with /dev/console. *
@@ -4115,7 +4132,7 @@ static int try_enable_console(struct console *newcon)* Note that a console with tty binding will have CON_CONSDEV * flag set and will be first in the list. */ -if (preferred_dev_console <0 ) { +if (list_empty (&preferred_dev_console)) {if (hlist_empty (&console_list) || !console_first ()->device ||console_first ()->flags &CON_BOOT ) {try_enable_default_console (newcon);
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 16/17 printk: Modify try_enable_default_console() to return error/success
In reply to Petr MladekCurrently, try_enable_default_console() has a void return type, and its potential failures (specifically when console_call_setup() fails) are ignored by its caller, try_enable_console().
Modify try_enable_default_console() to return error/success, and update try_enable_console() to capture and propagate this error.
With this change, try_enable_console() will no longer return success for a pre-enabled console if try_enable_default_console() failed (such as when newcon->setup() fails). While this might change existing behavior, it is the correct approach to ensure setup errors are not silently ignored or treated as successful registrations simply because the console was marked pre-enabled.
--- kernel/printk/printk.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
kernel/printk/printk.c +11 -4
@@ -4081,18 +4081,23 @@ static int try_enable_braille_console(struct console *newcon) }/* Try to enable the console unconditionally */ -static void try_enable_default_console (struct console *newcon) +static int try_enable_default_console (struct console *newcon) { +int err; +if (newcon->index <0 ) newcon->index =0 ; -if (console_call_setup (newcon,NULL ) !=0 ) -return ; + err =console_call_setup (newcon,NULL ); +if (err) +return err; newcon->flags |=CON_ENABLED ;if (newcon->device) newcon->flags |=CON_CONSDEV ; + +return 0 ; }#define console_first () \
@@ -4135,7 +4140,9 @@ static int try_enable_console(struct console *newcon)if (list_empty (&preferred_dev_console)) {if (hlist_empty (&console_list) || !console_first ()->device ||console_first ()->flags &CON_BOOT ) { -try_enable_default_console (newcon); + err =try_enable_default_console (newcon); +if (err != -ENOENT ) +return err; } }
Signature · 1 line
2.56.0
Petr Mladek
maintainerPATCH v7 17/17 printk: Try enable preferred consoles only when there are any
In reply to Petr Mladektry_enable_preferred_console() used to be always called because it had several hidden effects, namely:
- returned success when a console was pre-enabled using CON_ENABLED flag.
- enabled Braille consoles which were ignored by "preferred_dev_console" because they were not associated with /dev/console.
- returned success when a console was enabled by default because try_enable_default_console() did not return success.
The first two hidden effects were removed in previous patches. The already fixed handling of pre-enabled consoles actually helps even the 3rd case. try_enable_default_console() sets CON_ENABLED flag on success and is later handled as pre-enabled.
Prevent any future hidden effects and call try_enable_preferred_console() only when some console is preferred.
No behavior change.
--- kernel/printk/printk.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
kernel/printk/printk.c +17 -9
@@ -4105,7 +4105,7 @@ static int try_enable_default_console(struct console *newcon)static int try_enable_console (struct console *newcon) { -int err; +int err = -ENOENT ;/* * First, try to enable the console driver as a Braille console.
@@ -4144,16 +4144,24 @@ static int try_enable_console(struct console *newcon)if (err != -ENOENT )return err; } + }else { +/* See if this console matches one we selected on the command line */ + err =try_enable_preferred_console (newcon, true); +if (err != -ENOENT ) +return err; + +/* If not, try to match against the platform default(s) */ + err =try_enable_preferred_console (newcon, false); +if (err != -ENOENT ) +return err; } -/* See if this console matches one we selected on the command line */ - err =try_enable_preferred_console (newcon, true); -if (err != -ENOENT ) -return err; - -/* If not, try to match against the platform default(s) */ - err =try_enable_preferred_console (newcon, false); -if (err != -ENOENT ) +/* +* Make sure that pre-enabled consoles won't get registered when +* something went wrong. This is just a paranoid check that all +* branches above returned on error. +*/ +if (WARN_ON (err != -ENOENT ))return err;/*
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v7 11/17] console/braille: Lock console->setup() call during the registration
In reply to Petr MladekThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] console/braille: missing Fixes trailer for unlocked setup() call --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] console/braille: missing Fixes trailer for unlocked setup() call --
commit b38dd583153412f19250e27165240271e50c6249
Author: Petr Mladek <[email protected]>
Quoted 7 lines
console/braille: Lock
console->setup()call during the registrationIn
braille_register_console(), theconsole->setup()callback must be called while holding theconsole_lock. This prevents a race condition with a possible active boot console that might be using the same hardware registers.
Quoted 8 lines
See also the commit
801410b26a0e("serial: Lock console when calling into driver before registration").Reported-by Sashiko <[email protected]>Suggested-by John Ogness <[email protected]>Signed-off-by Petr Mladek <[email protected]>Acked-by Chris Down <[email protected]>
[Severity: Low]
This fixes a race condition in braille_register_console() where console->setup() was called without holding console_lock. Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Re: [PATCH v7 04/17] printk: Separate code for adding/updating preferred console metadata
In reply to Petr MladekHi Petr,
Quoted 4 lines · Petr Mladek
+/**
+ *add_or_update_preferred_console- Add or update a given entry in
+ * the preferred_consoles[] array.
+ * @i: index of the entry in @preferred_consoles table which should get updated.
kernel-doc picks the block up now, thanks. scripts/kernel-doc -none -Wall still says "No description found for return value of 'add_or_update_preferred_console'" though, because it wants a "Return:" line. "@preferred_consoles" in the @i line also still reads as a parameter, maybe preferred_consoles[] would be better?
Fine to fix when applying.
Thanks!
Chris
Re: [PATCH v7 05/17] printk: Do not accept an empty name or devname as a preferred console
In reply to Petr MladekHi Petr,
Thanks for splitting this out! I've tested that this works well.
Chris
Re: [PATCH v7 06/17] printk: Prefer the console either via name or devname exclusively
In reply to Petr MladekRe: [PATCH v7 07/17] printk: Check that index is not defined when a console is preferred via devname
In reply to Petr MladekHi Petr,
Quoted 2 lines · Petr Mladek
- * 2. Either @name OR @devname are allowed. [*]
+ * 2. Either @name and valid @idx OR @devname and @idx=-1 are allowed. [*]
The check below now rejects only idx >= 0 for @devname, so this could say "negative @idx" instead of "@idx=-1" to match it.
With that (fine to do when applying):
Chris
Re: [PATCH v7 08/17] printk: Ensure consistent name and devname when updating preferred console
In reply to Petr MladekRe: [PATCH v7 14/17] printk: Update Braille options when preferred more times
In reply to Petr MladekHi Petr,
Quoted 2 lines · Petr Mladek
Make the behavior consistent and always update
pc->brl_optionswhen @brl_options are passed and they were not defined yet.
This changes what users get for something like "console=ttyS0 console=brl,ttyS0". Before, ttyS0 stayed a normal console with CON_CONSDEV and the Braille request was dropped. Now ttyS0 becomes the Braille console and, with 15/17, tty0 gets enabled as the default console instead. That fits with Braille always winning in 13/17, but could you say so here like you did there?
With only this patch applied, that command line also leaves the guest without a console until 15/17. preferred_dev_console still points at the ttyS0 entry after it turns into Braille, so tty0 is not enabled as the default console. ttyS0 itself is only enabled as the Braille console. That leaves only earlyser0 registered and "Warning: unable to open an initial console". Going by the code, the base already ends up there for the reverse order (console=brl,ttyS0 console=ttyS0), and both of these seem to be fixed in 15/17. So maybe squashing the two is the simplest way to keep bisect clean.
kernel/printk/printk.c static int add_or_update_preferred_console(unsigned int i, · line 2659
Quoted 7 lines
kernel/printk/printk.c +7
-0+/* +* Allow to override normal console with the Braille variant, +* for example when ttyS0 is preferred via SPCR brl,ttyS0 +* via the command line. +*/ +if (brl_options) +braille_update_options (pc, brl_options);
When an existing entry turns into Braille here, it keeps pc->options from whoever added it first, and braille_register_console() only falls back to "57600o8" when those are NULL. So the serial options given with the Braille request are dropped and the result depends on the order. The cflag of braille_co ends up as:
console=ttyS1,115200 console=brl,ttyS1,57600 -> B115200
console=brl,ttyS1,57600 console=ttyS1,115200 -> B57600Should the options from the Braille request win when the entry switches over here? Or would you rather leave that for the separate series about overriding options?
Also, SPCR never adds a "ttyS" entry, it uses "uart", "pl011" and friends. For "uart", univ8250_console_match() succeeds when try_enable_braille_console() tries it, so that one ends up as a normal console anyway. Maybe use a second console= on the command line as the example. There's also an "and" missing before "brl,ttyS0".
A few changelog typos while you're there: "stored only it is a new entry" -> "only when it is", "preferred_console[]" -> "preferred_consoles[]" and ".brl->options" -> ".brl_options".
Once those are fixed, please feel free to add:
Chris