[PATCH v2 2/2] printk: Add update_preferred_console()

From: Tony Lindgren
Date: Thu Jun 13 2024 - 08:55:46 EST


Let's add update_preferred_console() for driver subsystems to call during
init when the console is ready, and it's character device name is known.
For now, we use it only for the serial layer to allow console=DEVNAME:0.0
style hardware based addressing for consoles.

The earlier attempt on doing this caused a regression with the kernel
command line console order as it added calling __add_preferred_console()
again later on during init. A better approach was suggested by Petr where
we add the deferred console to the console_cmdline[] and update it later
on when the console is ready.

Suggested-by: Petr Mladek <pmladek@xxxxxxxx>
Co-developed-by: Petr Mladek <pmladek@xxxxxxxx>
Signed-off-by: Tony Lindgren <tony.lindgren@xxxxxxxxxxxxxxx>
---
drivers/tty/serial/serial_base_bus.c | 8 ++-
include/linux/printk.h | 2 +
kernel/printk/console_cmdline.h | 1 +
kernel/printk/printk.c | 77 +++++++++++++++++++++++-----
4 files changed, 73 insertions(+), 15 deletions(-)

diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c
index 5ebacb982f9e..a34f55ef6f37 100644
--- a/drivers/tty/serial/serial_base_bus.c
+++ b/drivers/tty/serial/serial_base_bus.c
@@ -210,7 +210,13 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev)
static int serial_base_add_one_prefcon(const char *match, const char *dev_name,
int port_id)
{
- return 0;
+ int ret;
+
+ ret = update_preferred_console(match, dev_name, port_id);
+ if (ret == -ENOENT)
+ return 0;
+
+ return ret;
}

#ifdef __sparc__
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 65c5184470f1..fda935280e33 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -60,6 +60,8 @@ static inline const char *printk_skip_headers(const char *buffer)
#define CONSOLE_LOGLEVEL_DEFAULT CONFIG_CONSOLE_LOGLEVEL_DEFAULT
#define CONSOLE_LOGLEVEL_QUIET CONFIG_CONSOLE_LOGLEVEL_QUIET

+int update_preferred_console(const char *match, const char *name, const short idx);
+
extern int console_printk[];

#define console_loglevel (console_printk[0])
diff --git a/kernel/printk/console_cmdline.h b/kernel/printk/console_cmdline.h
index 3ca74ad391d6..0ab573b6d4dc 100644
--- a/kernel/printk/console_cmdline.h
+++ b/kernel/printk/console_cmdline.h
@@ -6,6 +6,7 @@ struct console_cmdline
{
char name[16]; /* Name of the driver */
int index; /* Minor dev. to use */
+ char devname[32]; /* DEVNAME:0.0 style device name */
bool user_specified; /* Specified by command line vs. platform */
char *options; /* Options for the driver */
#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index dddb15f48d59..0e2e442593bd 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2429,18 +2429,23 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified)
console_set_on_cmdline = 1;
}

-static int __add_preferred_console(const char *name, const short idx, char *options,
+static int __add_preferred_console(const char *name, const short idx,
+ const char *devname, char *options,
char *brl_options, bool user_specified)
{
struct console_cmdline *c;
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 for preferred console.
+ * value is not valid when the console name and index are defined on
+ * the command line.
*/
- if (idx < 0)
+ if (name && idx < 0)
return -EINVAL;

/*
@@ -2448,9 +2453,10 @@ static int __add_preferred_console(const char *name, const short idx, char *opti
* if we have a slot free.
*/
for (i = 0, c = console_cmdline;
- i < MAX_CMDLINECONSOLES && c->name[0];
+ i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
i++, c++) {
- if (strcmp(c->name, name) == 0 && c->index == idx) {
+ if ((name && strcmp(c->name, name) == 0 && c->index == idx) ||
+ (devname && strcmp(c->devname, devname) == 0)) {
if (!brl_options)
preferred_console = i;
set_user_specified(c, user_specified);
@@ -2461,7 +2467,10 @@ static int __add_preferred_console(const char *name, const short idx, char *opti
return -E2BIG;
if (!brl_options)
preferred_console = i;
- strscpy(c->name, name, sizeof(c->name));
+ if (name)
+ strscpy(c->name, name);
+ if (devname)
+ strscpy(c->devname, devname);
c->options = options;
set_user_specified(c, user_specified);
braille_set_options(c, brl_options);
@@ -2486,8 +2495,8 @@ __setup("console_msg_format=", console_msg_format_setup);
*/
static int __init console_setup(char *str)
{
- char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
- char *s, *options, *brl_options = NULL;
+ char buf[sizeof(console_cmdline[0].devname)]; /* name with "ttyS" prefix or devname */
+ char *s, *options, *brl_options = NULL, *chardev = NULL, *devname = NULL;
int idx;

/*
@@ -2496,17 +2505,23 @@ static int __init console_setup(char *str)
* for exactly this purpose.
*/
if (str[0] == 0 || strcmp(str, "null") == 0) {
- __add_preferred_console("ttynull", 0, NULL, NULL, true);
+ __add_preferred_console("ttynull", 0, NULL, NULL, NULL, true);
return 1;
}

if (_braille_console_setup(&str, &brl_options))
return 1;

+ /* For a DEVNAME:0.0 style console the character device is unknown early */
+ if (strchr(str, ':'))
+ devname = buf;
+ else
+ chardev = buf;
+
/*
* Decode str into name, index, options.
*/
- if (isdigit(str[0]))
+ if (chardev && isdigit(str[0]))
scnprintf(buf, sizeof(buf), "ttyS%s", str);
else
strscpy(buf, str);
@@ -2523,12 +2538,12 @@ static int __init console_setup(char *str)
#endif

for (s = buf; *s; s++)
- if (isdigit(*s) || *s == ',')
+ if ((chardev && isdigit(*s)) || *s == ',')
break;
idx = simple_strtoul(s, NULL, 10);
*s = 0;

- __add_preferred_console(buf, idx, options, brl_options, true);
+ __add_preferred_console(chardev, idx, devname, options, brl_options, true);
return 1;
}
__setup("console=", console_setup);
@@ -2548,7 +2563,38 @@ __setup("console=", console_setup);
*/
int add_preferred_console(const char *name, const short idx, char *options)
{
- return __add_preferred_console(name, idx, options, NULL, false);
+ return __add_preferred_console(name, idx, NULL, options, NULL, false);
+}
+
+/**
+ * update_preferred_console - Updates a preferred console if a match is found
+ * @devname: Expected console on kernel command line, such as console=DEVNAME:0.0
+ * @name: Name of the console character device to add such as ttyS
+ * @idx: Index for the console
+ *
+ * Allows driver subsystems to update a console after translating the command
+ * line name to the character device name used for the console.
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+int update_preferred_console(const char *devname, const char *name, const short idx)
+{
+ struct console_cmdline *c = console_cmdline;
+ int i;
+
+ if (!devname || !strlen(devname) || !name || !strlen(name) || idx < 0)
+ return -EINVAL;
+
+ for (i = 0; i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
+ i++, c++) {
+ if (!strcmp(devname, c->devname)) {
+ strscpy(c->name, name);
+ c->index = idx;
+ return 0;
+ }
+ }
+
+ return -ENOENT;
}

bool console_suspend_enabled = true;
@@ -3318,8 +3364,11 @@ static int try_enable_preferred_console(struct console *newcon,
int i, err;

for (i = 0, c = console_cmdline;
- i < MAX_CMDLINECONSOLES && c->name[0];
+ i < MAX_CMDLINECONSOLES && (c->name[0] || c->devname[0]);
i++, c++) {
+ /* Console not yet initialized? */
+ if (!c->name[0])
+ continue;
if (c->user_specified != user_specified)
continue;
if (!newcon->match ||
--
2.45.2