[PATCH v5 06/16] printk: Check that index is not defined when a console is preferred via devname
From: Petr Mladek
Date: Tue Sep 29 2026 - 08:10:20 EST
The 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.
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 0199c918ef03..b8c348e305eb 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2559,7 +2559,7 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
* it updates some global variables which are used to make the right decisions
* in register_console(). Finally, it enforces some rules:
*
- * 1. Either @name OR @devname are allowed. [*]
+ * 1. 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
@@ -2581,15 +2581,6 @@ static int add_or_update_preferred_console(unsigned int i,
if (!name && !devname)
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;
@@ -2603,12 +2594,24 @@ static int add_or_update_preferred_console(unsigned int i,
devname, name);
return -EINVAL;
}
+ if (idx != -1) {
+ 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;
}
if (is_new_entry && 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);
pc->index = idx;
}
--
2.55.0