* [PATCH printk 0/3] Introduce sync mode @ 2026-07-10 14:45 John Ogness 2026-07-10 14:45 ` [PATCH printk 3/3] printk: Support setting console sync mode via console= John Ogness 2026-07-10 21:05 ` [PATCH printk 0/3] Introduce sync mode John Ogness 0 siblings, 2 replies; 4+ messages in thread From: John Ogness @ 2026-07-10 14:45 UTC (permalink / raw) To: Petr Mladek Cc: Sergey Senozhatsky, Steven Rostedt, Andrew Murray, Chris Down, linux-kernel, Greg Kroah-Hartman, linux-fsdevel, Jonathan Corbet, Shuah Khan, linux-doc Hi, As proposed in an LKML thread [0], here is a series to introduce a new console feature to use synchronous printing. The feature is activated using the keyword "sync" in the console= command line argument. For example: console=ttyS0,115200,sync Sync mode is only available for nbcon consoles that provide a safe write_atomic() callback. Specifying it for other consoles will have no effect other than a log entry that sync mode is not supported. Patch 3/3 shamelessly copied an implementation from Chris Down to parse and update the console options. (Chris's version of find_and_remove_console_option() is here [1]). I slightly extended the function to support valueless-options. At some point we may want to add a sysfs interface to toggle sync mode. John Ogness [0] https://lore.kernel.org/lkml/87v7aruub1.fsf@jogness.linutronix.de [1] https://lore.kernel.org/lkml/77aa59337507e067f3a4ad7e15375893612bcfa3.1763492585.git.chris@chrisdown.name John Ogness (3): printk: Introduce console sync mode proc: Add console sync support for /proc/consoles printk: Support setting console sync mode via console= Documentation/admin-guide/serial-console.rst | 8 ++ fs/proc/consoles.c | 1 + include/linux/console.h | 2 + kernel/printk/console_cmdline.h | 1 + kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 42 ++++++-- kernel/printk/printk.c | 104 +++++++++++++++++++ 7 files changed, 154 insertions(+), 6 deletions(-) base-commit: 080d60fffa8e0d285871cde8395438006a9b5b0c -- 2.47.3 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH printk 3/3] printk: Support setting console sync mode via console= 2026-07-10 14:45 [PATCH printk 0/3] Introduce sync mode John Ogness @ 2026-07-10 14:45 ` John Ogness 2026-07-15 12:23 ` Petr Mladek 2026-07-10 21:05 ` [PATCH printk 0/3] Introduce sync mode John Ogness 1 sibling, 1 reply; 4+ messages in thread From: John Ogness @ 2026-07-10 14:45 UTC (permalink / raw) To: Petr Mladek Cc: Sergey Senozhatsky, Steven Rostedt, Andrew Murray, Chris Down, linux-kernel, Jonathan Corbet, Shuah Khan, linux-doc Extend the console= kernel command line argument to support specifying sync mode at boot time. This is achieved by introducing a new "sync" option that can be passed within a console= cmdline argument. For example, assuming the first serial device should be a console using sync mode: console=ttyS0,115200,sync Signed-off-by: John Ogness <john.ogness@linutronix.de> Co-authored-by: Chris Down <chris@chrisdown.name> --- Documentation/admin-guide/serial-console.rst | 8 ++ kernel/printk/console_cmdline.h | 1 + kernel/printk/printk.c | 79 ++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/Documentation/admin-guide/serial-console.rst b/Documentation/admin-guide/serial-console.rst index 1609e7479249f..37cbd9581c53a 100644 --- a/Documentation/admin-guide/serial-console.rst +++ b/Documentation/admin-guide/serial-console.rst @@ -32,6 +32,14 @@ The format of this option is:: and F is flow control ('r' for RTS). Default is 9600n8. The maximum baudrate is 115200. + One can also specify extra options (comma-separated) + to modify console printing behavior. The "sync" extra + option will cause console printing on that console to + be synchronous (printed in the context of the printk() + caller). This could cause adverse timing side-effects + for the system, but can be useful in certain debugging + scenarios. + You can specify multiple console= options on the kernel command line. The behavior is well defined when each device type is mentioned only once. diff --git a/kernel/printk/console_cmdline.h b/kernel/printk/console_cmdline.h index 0ab573b6d4dc2..ce4709f7a1b0c 100644 --- a/kernel/printk/console_cmdline.h +++ b/kernel/printk/console_cmdline.h @@ -8,6 +8,7 @@ struct console_cmdline int index; /* Minor dev. to use */ char devname[32]; /* DEVNAME:0.0 style device name */ bool user_specified; /* Specified by command line vs. platform */ + bool sync; /* Print in sync mode */ char *options; /* Options for the driver */ #ifdef CONFIG_A11Y_BRAILLE_CONSOLE char *brl_options; /* Options for braille driver */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e82e864a4d672..e53ac5554e5eb 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2567,6 +2567,81 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) console_set_on_cmdline = 1; } +/** + * find_and_remove_console_option - Find and remove a named option from console options string + * @options: The console options string (will be modified in-place) + * @key: The option name to find (e.g., "loglevel") + * @val_buf: Buffer to store the option value (if present) + * @val_buf_size: Size of @val_buf + * + * This function searches for a named option in a comma-separated options string + * (e.g., "9600n8,loglevel:3,sync"). If found, it extracts the value + * (the part after ':') and removes the entire option from the string. + * + * If an option does not support values, @val_buf should be NULL, in which + * case no value part is extracted and any user provided ':' is considered part + * of the option key. + * + * The function modifies @options in-place by: + * 1. Temporarily null-terminating option names and values during parsing + * 2. Restoring separators if the option isn't found + * 3. Removing the found option by shifting the remaining string + * + * Return: true if the option was found and removed, false otherwise + */ +static bool find_and_remove_console_option(char *options, const char *key, + char *val_buf, size_t val_buf_size) +{ + bool found = false, first = true; + char *option, *next = options; + + while ((option = strsep(&next, ","))) { + char *value = NULL; + + if (val_buf) { + value = strchr(option, ':'); + if (value) + *(value++) = '\0'; + } + + if (strcmp(option, key) == 0) { + found = true; + if (value) { + if (strlen(value) >= val_buf_size) { + pr_warn("Cannot copy console option value for %s:%s: not enough space (%zu)\n", + option, value, val_buf_size); + found = false; + } else { + strscpy(val_buf, value, val_buf_size); + } + } else if (val_buf) { + *val_buf = '\0'; + } + } + + if (found) + break; + + if (next) + *(next - 1) = ','; + if (value) + *(value - 1) = ':'; + + first = false; + } + + if (found) { + if (next) + memmove(option, next, strlen(next) + 1); + else if (first) + *option = '\0'; + else + *--option = '\0'; + } + + return found; +} + static int __add_preferred_console(const char *name, const short idx, const char *devname, char *options, char *brl_options, bool user_specified) @@ -2609,6 +2684,7 @@ static int __add_preferred_console(const char *name, const short idx, strscpy(c->name, name); if (devname) strscpy(c->devname, devname); + c->sync = find_and_remove_console_option(options, "sync", NULL, 0); c->options = options; set_user_specified(c, user_specified); braille_set_options(c, brl_options); @@ -3932,6 +4008,9 @@ static int try_enable_preferred_console(struct console *newcon, if (_braille_register_console(newcon, c)) return 0; + if (c->sync) + newcon->flags |= CON_SYNC; + err = console_call_setup(newcon, c->options); if (err) return err; -- 2.47.3 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH printk 3/3] printk: Support setting console sync mode via console= 2026-07-10 14:45 ` [PATCH printk 3/3] printk: Support setting console sync mode via console= John Ogness @ 2026-07-15 12:23 ` Petr Mladek 0 siblings, 0 replies; 4+ messages in thread From: Petr Mladek @ 2026-07-15 12:23 UTC (permalink / raw) To: John Ogness Cc: Sergey Senozhatsky, Steven Rostedt, Andrew Murray, Chris Down, linux-kernel, Jonathan Corbet, Shuah Khan, linux-doc On Fri 2026-07-10 16:51:53, John Ogness wrote: > Extend the console= kernel command line argument to support specifying > sync mode at boot time. This is achieved by introducing a new "sync" > option that can be passed within a console= cmdline argument. > > For example, assuming the first serial device should be a console using > sync mode: > > console=ttyS0,115200,sync > > Signed-off-by: John Ogness <john.ogness@linutronix.de> > Co-authored-by: Chris Down <chris@chrisdown.name> Just for record, I guess that Chris Down is mentioned as a Co-author because of the function find_and_remove_console_option() which seems to be taken from https://lore.kernel.org/all/d1cae00c839a3681759061e646f21a35b9b66613.1764272407.git.chris@chrisdown.name/ > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -2567,6 +2567,81 @@ static void set_user_specified(struct console_cmdline *c, bool user_specified) > console_set_on_cmdline = 1; > } > > +/** > + * find_and_remove_console_option - Find and remove a named option from console options string > + * @options: The console options string (will be modified in-place) > + * @key: The option name to find (e.g., "loglevel") > + * @val_buf: Buffer to store the option value (if present) > + * @val_buf_size: Size of @val_buf > + * > + * This function searches for a named option in a comma-separated options string > + * (e.g., "9600n8,loglevel:3,sync"). If found, it extracts the value > + * (the part after ':') and removes the entire option from the string. > + * > + * If an option does not support values, @val_buf should be NULL, in which > + * case no value part is extracted and any user provided ':' is considered part > + * of the option key. > + * > + * The function modifies @options in-place by: > + * 1. Temporarily null-terminating option names and values during parsing > + * 2. Restoring separators if the option isn't found > + * 3. Removing the found option by shifting the remaining string > + * > + * Return: true if the option was found and removed, false otherwise > + */ > +static bool find_and_remove_console_option(char *options, const char *key, > + char *val_buf, size_t val_buf_size) > +{ > + bool found = false, first = true; > + char *option, *next = options; > + > + while ((option = strsep(&next, ","))) { Sashiko AI pointed out that add_preferred_console() is called also by of_console_check() where the @options parameter is casted from "const char *", see https://sashiko.dev/#/patchset/20260710144609.194487-1-john.ogness%40linutronix.de I believe that we should be on the safe side. Honestly, I did not analyze it to the bottom. But the string "of_stdout_options": + must be '\0' terminated. So that it can't be part of a full memory dump of the original device tree file. It must be a dedicated memory entity which is accessed separately in some node tree, see of_find_node_opts_by_path(). + is used only by of_console_check(). So there should not be problem with parallel access [*] [*] of_console_check() is actually called from two locations: serial_core_add_one_port() and sprd_uart_is_console(). So there is some risk of parallelism. On the other hand, it makes sense to add the console from the device tree only once. And we would have bigger problems when add_preferred_console() is called by mode CPUs in parallel. Sigh, I think that we could keep it because it should be good enough. But I do not have 100% good feeling about it. Second sigh, proper fix would need to either allocate a copy or create some static buffer for options in struct console_cmdline or so. Or handle this a different way. > + char *value = NULL; > + > + if (val_buf) { > + value = strchr(option, ':'); > + if (value) > + *(value++) = '\0'; > + } > + > + if (strcmp(option, key) == 0) { > + found = true; > + if (value) { > + if (strlen(value) >= val_buf_size) { > + pr_warn("Cannot copy console option value for %s:%s: not enough space (%zu)\n", > + option, value, val_buf_size); > + found = false; > + } else { > + strscpy(val_buf, value, val_buf_size); > + } > + } else if (val_buf) { > + *val_buf = '\0'; > + } > + } > + > + if (found) > + break; > + > + if (next) > + *(next - 1) = ','; > + if (value) > + *(value - 1) = ':'; > + > + first = false; > + } > + > + if (found) { > + if (next) > + memmove(option, next, strlen(next) + 1); > + else if (first) > + *option = '\0'; Sashiko AI also pointed out that this would create an empty string. For example, serial8250_console_setup() checks only whether it is NULL and tries to read the baudate, ... Sigh, the handling of the console parameters is yet another historic mess which would deserve a clean up. I'll add this to my TODO in the console registration clean up. But I think that it is beyond the scope of this patch set. Well, we should stay conservative and set options to NULL in this case. > + else > + *--option = '\0'; > + } > + > + return found; > +} > + > static int __add_preferred_console(const char *name, const short idx, > const char *devname, char *options, > char *brl_options, bool user_specified) Otherwise, it looks good to me. Best Regards, Petr ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH printk 0/3] Introduce sync mode 2026-07-10 14:45 [PATCH printk 0/3] Introduce sync mode John Ogness 2026-07-10 14:45 ` [PATCH printk 3/3] printk: Support setting console sync mode via console= John Ogness @ 2026-07-10 21:05 ` John Ogness 1 sibling, 0 replies; 4+ messages in thread From: John Ogness @ 2026-07-10 21:05 UTC (permalink / raw) To: Petr Mladek Cc: Sergey Senozhatsky, Steven Rostedt, Andrew Murray, Chris Down, linux-kernel, Greg Kroah-Hartman, linux-fsdevel, Jonathan Corbet, Shuah Khan, linux-doc On 2026-07-10, John Ogness <john.ogness@linutronix.de> wrote: > As proposed in an LKML thread [0], here is a series to introduce a > new console feature to use synchronous printing. So the Sashiko review [0] found all kinds of issues and every one of them are legitimate. In particular, I totally forgot about: - the console_lock synchronization when boot consoles exist - the various sites that _rely_ on kthreads printing when printk_get_console_flush_type() reports ft.nbcon_offload I will consider each of the points mentioned by Sashiko and include my proposed solution in my response. I will need a few days to go through all of it. John [0] https://sashiko.dev/#/patchset/20260710144609.194487-1-john.ogness%40linutronix.de ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-15 12:23 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-07-10 14:45 [PATCH printk 0/3] Introduce sync mode John Ogness 2026-07-10 14:45 ` [PATCH printk 3/3] printk: Support setting console sync mode via console= John Ogness 2026-07-15 12:23 ` Petr Mladek 2026-07-10 21:05 ` [PATCH printk 0/3] Introduce sync mode John Ogness
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox