* [PATCH printk v3 00/40] reduce console_lock scope
@ 2022-11-07 14:15 John Ogness
2022-11-07 14:16 ` [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function John Ogness
` (11 more replies)
0 siblings, 12 replies; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:15 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Josh Triplett, Mathieu Desnoyers, Lai Jiangshan, Joel Fernandes,
rcu, Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial,
linux-fsdevel, Miguel Ojeda, Richard Weinberger, Anton Ivanov,
Johannes Berg, linux-um, Luis Chamberlain, Aaron Tomlin,
Andy Shevchenko, Ilpo Järvinen, Geert Uytterhoeven,
Tony Lindgren, Lukas Wunner, Geert Uytterhoeven, linux-m68k,
Ard Biesheuvel, linux-efi, linuxppc-dev, Krzysztof Kozlowski,
Alim Akhtar, linux-arm-kernel, linux-samsung-soc, Michal Simek,
Peter Zijlstra, Mathias Nyman, linux-usb, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, Helge Deller,
Javier Martinez Canillas, Thomas Zimmermann, Juergen Gross,
Boris Ostrovsky, Tom Rix, linux-fbdev, dri-devel
This is v3 of a series to prepare for threaded/atomic
printing. v2 is here [0]. This series focuses on reducing the
scope of the BKL console_lock. It achieves this by switching to
SRCU and a dedicated mutex for console list iteration and
modification, respectively. The console_lock will no longer
offer this protection and is completely removed from
(un)register_console() and console_stop/start() code.
Also, during the review of v2 it came to our attention that
many console drivers are checking CON_ENABLED to see if they
are registered. Because this flag can change without
unregistering and because this flag does not represent an
atomic point when an (un)registration process is complete,
a new console_is_registered() function is introduced. This
function uses the console_list_lock to synchronize with the
(un)registration process to provide a reliable status.
All users of the console_lock for list iteration have been
modified. For the call sites where the console_lock is still
needed (because of other reasons), comments are added to
explain exactly why the console_lock was needed.
All users of CON_ENABLED for registration status have been
modified to use console_is_registered(). Note that there are
still users of CON_ENABLED, but this is for legitimate purposes
about a registered console being able to print.
The base commit for this series is from Paul McKenney's RCU tree
and provides an NMI-safe SRCU implementation [1]. Without the
NMI-safe SRCU implementation, this series is not less safe than
mainline. But we will need the NMI-safe SRCU implementation for
atomic consoles anyway, so we might as well get it in
now. Especially since it _does_ increase the reliability for
mainline in the panic path.
Changes since v3:
general:
- introduce a synchronized console_is_registered() to query if
a console is registered, meant to replace CON_ENABLED
(mis)use for this purpose
- directly read console->flags for registered consoles if it is
race-free (and document that it is so)
- replace uart_console_enabled() with a new
uart_console_registered() based on console_is_registered()
- change comments about why console_lock is used to synchronize
console->device() by providing an example
registration check fixups:
- the following drivers were modified to use the new
console_is_registered() instead of CON_ENABLED checks
- arch/m68k/emu/nfcon.c
- drivers/firmware/efi/earlycon.c
- drivers/net/netconsole.c
- drivers/tty/hvc/hvc_console.c
- drivers/tty/serial/8250/8250_core.c
- drivers/tty/serial/earlycon.c
- drivers/tty/serial/pic32_uart.c
- drivers/tty/serial/samsung_tty.c
- drivers/tty/serial/serial_core.c
- drivers/tty/serial/xilinx_uartps.c
- drivers/usb/early/xhci-dbc.c
um: kmsg_dumper:
- change stdout dump criteria to match original intention
kgdb/kdb:
- in configure_kgdboc(), take console_list_lock to synchronize
tty_find_polling_driver() against register_console()
- add comments explaining why calling console->write() without
locking might work
tty: sh-sci:
- use a setup() callback to setup the early console
fbdev: xen:
- implement a cleaner approach for
console_force_preferred_locked()
rcu:
- implement debug_lockdep_rcu_enabled() for
!CONFIG_DEBUG_LOCK_ALLOC
printk:
- check CONFIG_DEBUG_LOCK_ALLOC for srcu_read_lock_held()
availability
- for console_lock/_trylock/_unlock, replace "lock the console
system" language with "block the console subsystem from
printing"
- use WRITE_ONCE() for updating console->flags of registered
consoles
- expand comments of synchronize_srcu() calls to explain why
they are needed, and also expand comments to explain when it
is not needed
- change CON_BOOT consoles to always begin at earliest message
- for non-BOOT/non-PRINTBUFFER consoles, initialize @seq to the
minimal @seq of any of the enabled boot consoles
- add comments and lockdep assertion to
unregister_console_locked() because it is not clear from the
name which lock is implied
- dropped patches that caused unnecessary churn in the series
John Ogness
[0] https://lore.kernel.org/lkml/20221019145600.1282823-1-john.ogness@linutronix.de
[1] https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git/log/?h=srcunmisafe.2022.10.21a
John Ogness (38):
rcu: implement lockdep_rcu_enabled for !CONFIG_DEBUG_LOCK_ALLOC
printk: Prepare for SRCU console list protection
printk: fix setting first seq for consoles
um: kmsg_dump: only dump when no output console available
console: introduce console_is_enabled() wrapper
printk: use console_is_enabled()
um: kmsg_dump: use console_is_enabled()
kdb: kdb_io: use console_is_enabled()
um: kmsg_dumper: use srcu console list iterator
tty: serial: kgdboc: document console_lock usage
tty: tty_io: document console_lock usage
proc: consoles: document console_lock usage
kdb: use srcu console list iterator
printk: console_flush_all: use srcu console list iterator
printk: console_unblank: use srcu console list iterator
printk: console_flush_on_panic: use srcu console list iterator
printk: console_device: use srcu console list iterator
printk: __pr_flush: use srcu console list iterator
printk: introduce console_list_lock
console: introduce console_is_registered()
serial_core: replace uart_console_enabled() with
uart_console_registered()
tty: nfcon: use console_is_registered()
efi: earlycon: use console_is_registered()
tty: hvc: use console_is_registered()
tty: serial: earlycon: use console_is_registered()
tty: serial: pic32_uart: use console_is_registered()
tty: serial: samsung_tty: use console_is_registered()
tty: serial: xilinx_uartps: use console_is_registered()
usb: early: xhci-dbc: use console_is_registered()
netconsole: avoid CON_ENABLED misuse to track registration
printk, xen: fbfront: create/use safe function for forcing preferred
tty: tty_io: use console_list_lock for list synchronization
proc: consoles: use console_list_lock for list iteration
tty: serial: kgdboc: use console_list_lock for list traversal
tty: serial: kgdboc: synchronize tty_find_polling_driver() and
register_console()
tty: serial: kgdboc: use console_list_lock to trap exit
printk: relieve console_lock of list synchronization duties
tty: serial: sh-sci: use setup() callback for early console
Thomas Gleixner (2):
serial: kgdboc: Lock console list in probe function
printk: Convert console_drivers list to hlist
.clang-format | 1 +
arch/m68k/emu/nfcon.c | 10 +-
arch/um/kernel/kmsg_dump.c | 24 +-
drivers/firmware/efi/earlycon.c | 8 +-
drivers/net/netconsole.c | 21 +-
drivers/tty/hvc/hvc_console.c | 4 +-
drivers/tty/serial/8250/8250_core.c | 2 +-
drivers/tty/serial/earlycon.c | 4 +-
drivers/tty/serial/kgdboc.c | 46 ++-
drivers/tty/serial/pic32_uart.c | 4 +-
drivers/tty/serial/samsung_tty.c | 2 +-
drivers/tty/serial/serial_core.c | 14 +-
drivers/tty/serial/sh-sci.c | 17 +-
drivers/tty/serial/xilinx_uartps.c | 2 +-
drivers/tty/tty_io.c | 18 +-
drivers/usb/early/xhci-dbc.c | 2 +-
drivers/video/fbdev/xen-fbfront.c | 12 +-
fs/proc/consoles.c | 21 +-
include/linux/console.h | 111 +++++++-
include/linux/rcupdate.h | 5 +
include/linux/serial_core.h | 15 +-
kernel/debug/kdb/kdb_io.c | 14 +-
kernel/printk/printk.c | 424 +++++++++++++++++++++-------
23 files changed, 605 insertions(+), 176 deletions(-)
base-commit: e29a4915db1480f96e0bc2e928699d086a71f43c
--
2.30.2
^ permalink raw reply [flat|nested] 32+ messages in thread
* [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-09 8:20 ` Daniel Thompson
2022-11-07 14:16 ` [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage John Ogness
` (10 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
From: Thomas Gleixner <tglx@linutronix.de>
Unprotected list walks are not necessarily safe.
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
drivers/tty/serial/kgdboc.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index 7aa37be3216a..e76f0186c335 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -193,6 +193,7 @@ static int configure_kgdboc(void)
if (!p)
goto noconfig;
+ console_lock();
for_each_console(cons) {
int idx;
if (cons->device && cons->device(cons, &idx) == p &&
@@ -201,6 +202,7 @@ static int configure_kgdboc(void)
break;
}
}
+ console_unlock();
kgdb_tty_driver = p;
kgdb_tty_line = tty_line;
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
2022-11-07 14:16 ` [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-09 8:23 ` Daniel Thompson
2022-11-09 15:19 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered() John Ogness
` (9 subsequent siblings)
11 siblings, 2 replies; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
are unregistered until the kgdboc_earlycon is setup. This is necessary
because the trapping of the exit() callback assumes that the exit()
callback is not called before the trap is setup.
Explicitly document this non-typical console_lock usage.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
---
drivers/tty/serial/kgdboc.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index e76f0186c335..5be381003e58 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -530,6 +530,14 @@ static int __init kgdboc_earlycon_init(char *opt)
* Look for a matching console, or if the name was left blank just
* pick the first one we find.
*/
+
+ /*
+ * Hold the console_lock to guarantee that no consoles are
+ * unregistered until the kgdboc_earlycon setup is complete.
+ * Trapping the exit() callback relies on exit() not being
+ * called until the trap is setup. This also allows safe
+ * traversal of the console list and race-free reading of @flags.
+ */
console_lock();
for_each_console(con) {
if (con->write && con->read &&
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
2022-11-07 14:16 ` [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function John Ogness
2022-11-07 14:16 ` [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-08 8:46 ` Geert Uytterhoeven
2022-11-07 14:16 ` [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered() John Ogness
` (8 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, Andy Shevchenko,
Ilpo Järvinen, Geert Uytterhoeven, Tony Lindgren,
Lukas Wunner, linux-serial
All users of uart_console_enabled() really want to know if a console
is registered. It is not reliable to check for CON_ENABLED in order
to identify if a console is registered. Use console_is_registered()
instead.
A _locked() variant is provided because uart_set_options() is always
called with the console_list_lock held and must check if a console
is registered in order to synchronize with kgdboc.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/8250/8250_core.c | 2 +-
drivers/tty/serial/pic32_uart.c | 2 +-
drivers/tty/serial/serial_core.c | 14 +++++++-------
include/linux/serial_core.h | 15 +++++++++++++--
4 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c
index 94fbf0add2ce..74568292186f 100644
--- a/drivers/tty/serial/8250/8250_core.c
+++ b/drivers/tty/serial/8250/8250_core.c
@@ -565,7 +565,7 @@ serial8250_register_ports(struct uart_driver *drv, struct device *dev)
up->port.dev = dev;
- if (uart_console_enabled(&up->port))
+ if (uart_console_registered(&up->port))
pm_runtime_get_sync(up->port.dev);
serial8250_apply_quirks(up);
diff --git a/drivers/tty/serial/pic32_uart.c b/drivers/tty/serial/pic32_uart.c
index 2beada66c824..1183b2a26539 100644
--- a/drivers/tty/serial/pic32_uart.c
+++ b/drivers/tty/serial/pic32_uart.c
@@ -919,7 +919,7 @@ static int pic32_uart_probe(struct platform_device *pdev)
}
#ifdef CONFIG_SERIAL_PIC32_CONSOLE
- if (uart_console_enabled(port)) {
+ if (uart_console_registered(port)) {
/* The peripheral clock has been enabled by console_setup,
* so disable it till the port is used.
*/
diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index 179ee199df34..b9fbbee598b8 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -2223,11 +2223,11 @@ uart_set_options(struct uart_port *port, struct console *co,
/*
* Ensure that the serial-console lock is initialised early.
*
- * Note that the console-enabled check is needed because of kgdboc,
- * which can end up calling uart_set_options() for an already enabled
+ * Note that the console-registered check is needed because
+ * kgdboc can call uart_set_options() for an already registered
* console via tty_find_polling_driver() and uart_poll_init().
*/
- if (!uart_console_enabled(port) && !port->console_reinit)
+ if (!uart_console_registered_locked(port) && !port->console_reinit)
uart_port_spin_lock_init(port);
memset(&termios, 0, sizeof(struct ktermios));
@@ -2573,7 +2573,7 @@ uart_configure_port(struct uart_driver *drv, struct uart_state *state,
* successfully registered yet, try to re-register it.
* It may be that the port was not available.
*/
- if (port->cons && !(port->cons->flags & CON_ENABLED))
+ if (port->cons && !console_is_registered(port->cons))
register_console(port->cons);
/*
@@ -2956,7 +2956,7 @@ static ssize_t console_show(struct device *dev,
mutex_lock(&port->mutex);
uport = uart_port_check(state);
if (uport)
- console = uart_console_enabled(uport);
+ console = uart_console_registered(uport);
mutex_unlock(&port->mutex);
return sprintf(buf, "%c\n", console ? 'Y' : 'N');
@@ -2978,7 +2978,7 @@ static ssize_t console_store(struct device *dev,
mutex_lock(&port->mutex);
uport = uart_port_check(state);
if (uport) {
- oldconsole = uart_console_enabled(uport);
+ oldconsole = uart_console_registered(uport);
if (oldconsole && !newconsole) {
ret = unregister_console(uport->cons);
} else if (!oldconsole && newconsole) {
@@ -3086,7 +3086,7 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
* If this port is in use as a console then the spinlock is already
* initialised.
*/
- if (!uart_console_enabled(uport))
+ if (!uart_console_registered(uport))
uart_port_spin_lock_init(uport);
if (uport->cons && uport->dev)
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index d657f2a42a7b..2f910f2bbe53 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -743,9 +743,20 @@ static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
static inline int setup_earlycon(char *buf) { return 0; }
#endif
-static inline bool uart_console_enabled(struct uart_port *port)
+/* Variant of uart_console_registered() when the console_list_lock is held. */
+static inline bool uart_console_registered_locked(struct uart_port *port)
{
- return uart_console(port) && (port->cons->flags & CON_ENABLED);
+ return uart_console(port) && console_is_registered_locked(port->cons);
+}
+
+static inline bool uart_console_registered(struct uart_port *port)
+{
+ bool ret;
+
+ console_list_lock();
+ ret = uart_console_registered_locked(port);
+ console_list_unlock();
+ return ret;
}
struct uart_port *uart_get_console(struct uart_port *ports, int nr,
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (2 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered() John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:00 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 28/40] tty: serial: pic32_uart: " John Ogness
` (7 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial
It is not reliable to check for CON_ENABLED in order to identify if a
console is registered. Use console_is_registered() instead.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/earlycon.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/tty/serial/earlycon.c b/drivers/tty/serial/earlycon.c
index a5f380584cda..4f6e9bf57169 100644
--- a/drivers/tty/serial/earlycon.c
+++ b/drivers/tty/serial/earlycon.c
@@ -181,7 +181,7 @@ int __init setup_earlycon(char *buf)
if (!buf || !buf[0])
return -EINVAL;
- if (early_con.flags & CON_ENABLED)
+ if (console_is_registered(&early_con))
return -EALREADY;
again:
@@ -253,7 +253,7 @@ int __init of_setup_earlycon(const struct earlycon_id *match,
bool big_endian;
u64 addr;
- if (early_con.flags & CON_ENABLED)
+ if (console_is_registered(&early_con))
return -EALREADY;
spin_lock_init(&port->lock);
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 28/40] tty: serial: pic32_uart: use console_is_registered()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (3 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered() John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:01 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 29/40] tty: serial: samsung_tty: " John Ogness
` (6 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial
It is not reliable to check for CON_ENABLED in order to identify if a
console is registered. Use console_is_registered() instead.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/pic32_uart.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/tty/serial/pic32_uart.c b/drivers/tty/serial/pic32_uart.c
index 1183b2a26539..c38754d593ca 100644
--- a/drivers/tty/serial/pic32_uart.c
+++ b/drivers/tty/serial/pic32_uart.c
@@ -843,7 +843,7 @@ console_initcall(pic32_console_init);
*/
static int __init pic32_late_console_init(void)
{
- if (!(pic32_console.flags & CON_ENABLED))
+ if (!console_is_registered(&pic32_console))
register_console(&pic32_console);
return 0;
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 29/40] tty: serial: samsung_tty: use console_is_registered()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (4 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 28/40] tty: serial: pic32_uart: " John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:01 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 30/40] tty: serial: xilinx_uartps: " John Ogness
` (5 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby,
linux-arm-kernel, linux-samsung-soc, linux-serial
It is not reliable to check for CON_ENABLED in order to identify if a
console is registered. Use console_is_registered() instead.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/samsung_tty.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/tty/serial/samsung_tty.c b/drivers/tty/serial/samsung_tty.c
index 77d1363029f5..9c252c9ca95a 100644
--- a/drivers/tty/serial/samsung_tty.c
+++ b/drivers/tty/serial/samsung_tty.c
@@ -1732,7 +1732,7 @@ static void __init s3c24xx_serial_register_console(void)
static void s3c24xx_serial_unregister_console(void)
{
- if (s3c24xx_serial_console.flags & CON_ENABLED)
+ if (console_is_registered(&s3c24xx_serial_console))
unregister_console(&s3c24xx_serial_console);
}
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 30/40] tty: serial: xilinx_uartps: use console_is_registered()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (5 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 29/40] tty: serial: samsung_tty: " John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:04 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal John Ogness
` (4 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, Michal Simek, linux-serial,
linux-arm-kernel
It is not reliable to check for CON_ENABLED in order to identify if a
console is registered. Use console_is_registered() instead.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/xilinx_uartps.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/tty/serial/xilinx_uartps.c b/drivers/tty/serial/xilinx_uartps.c
index 2eff7cff57c4..0cbd1892c53b 100644
--- a/drivers/tty/serial/xilinx_uartps.c
+++ b/drivers/tty/serial/xilinx_uartps.c
@@ -1631,7 +1631,7 @@ static int cdns_uart_probe(struct platform_device *pdev)
#ifdef CONFIG_SERIAL_XILINX_PS_UART_CONSOLE
/* This is not port which is used for console that's why clean it up */
if (console_port == port &&
- !(cdns_uart_uart_driver.cons->flags & CON_ENABLED)) {
+ !console_is_registered(cdns_uart_uart_driver.cons)) {
console_port = NULL;
cdns_uart_console.index = -1;
}
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (6 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 30/40] tty: serial: xilinx_uartps: " John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-09 9:06 ` Daniel Thompson
2022-11-07 14:16 ` [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console() John Ogness
` (3 subsequent siblings)
11 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
configure_kgdboc() uses the console_lock for console list iteration. Use
the console_list_lock instead because list synchronization responsibility
will be removed from the console_lock in a later change.
The SRCU iterator could have been used here, but a later change will
relocate the locking of the console_list_lock to also provide
synchronization against register_console().
Note, the console_lock is still needed to serialize the device()
callback with other console operations.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/kgdboc.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index 5be381003e58..82b4b4d67823 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -193,7 +193,16 @@ static int configure_kgdboc(void)
if (!p)
goto noconfig;
+ /* For safe traversal of the console list. */
+ console_list_lock();
+
+ /*
+ * Take console_lock to serialize device() callback with
+ * other console operations. For example, fg_console is
+ * modified under console_lock when switching vt.
+ */
console_lock();
+
for_each_console(cons) {
int idx;
if (cons->device && cons->device(cons, &idx) == p &&
@@ -202,8 +211,11 @@ static int configure_kgdboc(void)
break;
}
}
+
console_unlock();
+ console_list_unlock();
+
kgdb_tty_driver = p;
kgdb_tty_line = tty_line;
@@ -451,6 +463,7 @@ static void kgdboc_earlycon_pre_exp_handler(void)
{
struct console *con;
static bool already_warned;
+ int cookie;
if (already_warned)
return;
@@ -463,9 +476,14 @@ static void kgdboc_earlycon_pre_exp_handler(void)
* serial drivers might be OK with this, print a warning once per
* boot if we detect this case.
*/
- for_each_console(con)
+ cookie = console_srcu_read_lock();
+ for_each_console_srcu(con) {
if (con == kgdboc_earlycon_io_ops.cons)
- return;
+ break;
+ }
+ console_srcu_read_unlock(cookie);
+ if (con)
+ return;
already_warned = true;
pr_warn("kgdboc_earlycon is still using bootconsole\n");
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console()
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (7 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:13 ` Daniel Thompson
2022-11-10 18:03 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit John Ogness
` (2 subsequent siblings)
11 siblings, 2 replies; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
Calling tty_find_polling_driver() can lead to uart_set_options() being
called (via the poll_init() callback of tty_operations) to configure the
uart. But uart_set_options() can also be called by register_console()
(via the setup() callback of console).
Take the console_list_lock to synchronize against register_console() and
also use it for console list traversal. This also ensures the console list
cannot change until the polling console has been chosen.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/kgdboc.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index 82b4b4d67823..8c2b7ccdfebf 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -189,12 +189,20 @@ static int configure_kgdboc(void)
if (kgdboc_register_kbd(&cptr))
goto do_register;
+ /*
+ * tty_find_polling_driver() can call uart_set_options()
+ * (via poll_init) to configure the uart. Take the console_list_lock
+ * in order to synchronize against register_console(), which can also
+ * configure the uart via uart_set_options(). This also allows safe
+ * traversal of the console list.
+ */
+ console_list_lock();
+
p = tty_find_polling_driver(cptr, &tty_line);
- if (!p)
+ if (!p) {
+ console_list_unlock();
goto noconfig;
-
- /* For safe traversal of the console list. */
- console_list_lock();
+ }
/*
* Take console_lock to serialize device() callback with
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (8 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console() John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-10 15:18 ` Daniel Thompson
2022-11-11 9:59 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console John Ogness
2022-11-11 14:43 ` [PATCH printk v3 00/40] reduce console_lock scope Mathieu Desnoyers
11 siblings, 2 replies; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
are unregistered until the kgdboc_earlycon is setup. The console_list_lock
should be used instead because list synchronization responsibility will
be removed from the console_lock in a later change.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/kgdboc.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
index 8c2b7ccdfebf..a3ed9b34e2ab 100644
--- a/drivers/tty/serial/kgdboc.c
+++ b/drivers/tty/serial/kgdboc.c
@@ -558,13 +558,13 @@ static int __init kgdboc_earlycon_init(char *opt)
*/
/*
- * Hold the console_lock to guarantee that no consoles are
+ * Hold the console_list_lock to guarantee that no consoles are
* unregistered until the kgdboc_earlycon setup is complete.
* Trapping the exit() callback relies on exit() not being
* called until the trap is setup. This also allows safe
* traversal of the console list and race-free reading of @flags.
*/
- console_lock();
+ console_list_lock();
for_each_console(con) {
if (con->write && con->read &&
(con->flags & (CON_BOOT | CON_ENABLED)) &&
@@ -606,7 +606,7 @@ static int __init kgdboc_earlycon_init(char *opt)
}
unlock:
- console_unlock();
+ console_list_unlock();
/* Non-zero means malformed option so we always return zero */
return 0;
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (9 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit John Ogness
@ 2022-11-07 14:16 ` John Ogness
2022-11-08 8:53 ` Geert Uytterhoeven
2022-11-11 17:15 ` Petr Mladek
2022-11-11 14:43 ` [PATCH printk v3 00/40] reduce console_lock scope Mathieu Desnoyers
11 siblings, 2 replies; 32+ messages in thread
From: John Ogness @ 2022-11-07 14:16 UTC (permalink / raw)
To: Petr Mladek
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial
When setting up the early console, the setup() callback of the
regular console is used. It is called manually before registering
the early console instead of providing a setup() callback for the
early console. This is probably because the early setup needs a
different @options during the early stage.
The issue here is that the setup() callback is called without the
console_list_lock held and functions such as uart_set_options()
expect that.
Rather than manually calling the setup() function before registering,
provide an early console setup() callback that will use the different
early options. This ensures that the error checking, ordering, and
locking context when setting up the early console are correct.
Note that technically the current implementation works because it is
only used in early boot. And since the early console setup is
performed before registering, it cannot race with anything and thus
does not need any locking. However, longterm maintenance is easier
when drivers rely on the subsystem API rather than manually
implementing steps that could cause breakage in the future.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
drivers/tty/serial/sh-sci.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index 62f773286d44..f3a1cfec757a 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -3054,15 +3054,26 @@ static struct console serial_console = {
};
#ifdef CONFIG_SUPERH
+static char early_serial_buf[32];
+
+static int early_serial_console_setup(struct console *co, char *options)
+{
+ WARN_ON(options);
+ /*
+ * Use @early_serial_buf because @options will always be
+ * NULL at this early stage.
+ */
+ return serial_console_setup(co, early_serial_buf);
+}
+
static struct console early_serial_console = {
.name = "early_ttySC",
.write = serial_console_write,
+ .setup = early_serial_console_setup,
.flags = CON_PRINTBUFFER,
.index = -1,
};
-static char early_serial_buf[32];
-
static int sci_probe_earlyprintk(struct platform_device *pdev)
{
const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
@@ -3074,8 +3085,6 @@ static int sci_probe_earlyprintk(struct platform_device *pdev)
sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
- serial_console_setup(&early_serial_console, early_serial_buf);
-
if (!strstr(early_serial_buf, "keep"))
early_serial_console.flags |= CON_BOOT;
--
2.30.2
^ permalink raw reply related [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered()
2022-11-07 14:16 ` [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered() John Ogness
@ 2022-11-08 8:46 ` Geert Uytterhoeven
2022-11-10 13:24 ` Petr Mladek
0 siblings, 1 reply; 32+ messages in thread
From: Geert Uytterhoeven @ 2022-11-08 8:46 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Greg Kroah-Hartman, Jiri Slaby, Andy Shevchenko,
Ilpo Järvinen, Geert Uytterhoeven, Tony Lindgren,
Lukas Wunner, linux-serial
Hi John,
On Mon, Nov 7, 2022 at 3:16 PM John Ogness <john.ogness@linutronix.de> wrote:
> All users of uart_console_enabled() really want to know if a console
> is registered. It is not reliable to check for CON_ENABLED in order
> to identify if a console is registered. Use console_is_registered()
> instead.
>
> A _locked() variant is provided because uart_set_options() is always
> called with the console_list_lock held and must check if a console
> is registered in order to synchronize with kgdboc.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> --- a/include/linux/serial_core.h
> +++ b/include/linux/serial_core.h
> @@ -743,9 +743,20 @@ static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
> static inline int setup_earlycon(char *buf) { return 0; }
> #endif
>
> -static inline bool uart_console_enabled(struct uart_port *port)
> +/* Variant of uart_console_registered() when the console_list_lock is held. */
> +static inline bool uart_console_registered_locked(struct uart_port *port)
> {
> - return uart_console(port) && (port->cons->flags & CON_ENABLED);
> + return uart_console(port) && console_is_registered_locked(port->cons);
> +}
> +
> +static inline bool uart_console_registered(struct uart_port *port)
> +{
> + bool ret;
> +
> + console_list_lock();
> + ret = uart_console_registered_locked(port);
> + console_list_unlock();
> + return ret;
Perhaps
return uart_console(port) && console_is_registered();
to avoid locking the list when the first condition is not true?
> }
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console
2022-11-07 14:16 ` [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console John Ogness
@ 2022-11-08 8:53 ` Geert Uytterhoeven
2022-11-11 17:15 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Geert Uytterhoeven @ 2022-11-08 8:53 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Greg Kroah-Hartman, Jiri Slaby, linux-serial,
Linux-sh list, Linux-Renesas
Hi John,
CC linux-sh (SH-specific code)
CC linux-renesas-soc (JFYI)
On Mon, Nov 7, 2022 at 3:20 PM John Ogness <john.ogness@linutronix.de> wrote:
> When setting up the early console, the setup() callback of the
> regular console is used. It is called manually before registering
> the early console instead of providing a setup() callback for the
> early console. This is probably because the early setup needs a
> different @options during the early stage.
>
> The issue here is that the setup() callback is called without the
> console_list_lock held and functions such as uart_set_options()
> expect that.
>
> Rather than manually calling the setup() function before registering,
> provide an early console setup() callback that will use the different
> early options. This ensures that the error checking, ordering, and
> locking context when setting up the early console are correct.
>
> Note that technically the current implementation works because it is
> only used in early boot. And since the early console setup is
> performed before registering, it cannot race with anything and thus
> does not need any locking. However, longterm maintenance is easier
> when drivers rely on the subsystem API rather than manually
> implementing steps that could cause breakage in the future.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Thanks for your patch!
> --- a/drivers/tty/serial/sh-sci.c
> +++ b/drivers/tty/serial/sh-sci.c
> @@ -3054,15 +3054,26 @@ static struct console serial_console = {
> };
>
> #ifdef CONFIG_SUPERH
> +static char early_serial_buf[32];
> +
> +static int early_serial_console_setup(struct console *co, char *options)
> +{
> + WARN_ON(options);
> + /*
> + * Use @early_serial_buf because @options will always be
> + * NULL at this early stage.
> + */
> + return serial_console_setup(co, early_serial_buf);
> +}
> +
> static struct console early_serial_console = {
> .name = "early_ttySC",
> .write = serial_console_write,
> + .setup = early_serial_console_setup,
> .flags = CON_PRINTBUFFER,
> .index = -1,
> };
>
> -static char early_serial_buf[32];
> -
> static int sci_probe_earlyprintk(struct platform_device *pdev)
> {
> const struct plat_sci_port *cfg = dev_get_platdata(&pdev->dev);
> @@ -3074,8 +3085,6 @@ static int sci_probe_earlyprintk(struct platform_device *pdev)
>
> sci_init_single(pdev, &sci_ports[pdev->id], pdev->id, cfg, true);
>
> - serial_console_setup(&early_serial_console, early_serial_buf);
> -
> if (!strstr(early_serial_buf, "keep"))
> early_serial_console.flags |= CON_BOOT;
>
> --
> 2.30.2
LGTM, so
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function
2022-11-07 14:16 ` [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function John Ogness
@ 2022-11-09 8:20 ` Daniel Thompson
0 siblings, 0 replies; 32+ messages in thread
From: Daniel Thompson @ 2022-11-09 8:20 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On Mon, Nov 07, 2022 at 03:22:00PM +0106, John Ogness wrote:
> From: Thomas Gleixner <tglx@linutronix.de>
>
> Unprotected list walks are not necessarily safe.
>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Douglas Anderson <dianders@chromium.org>
> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Reviewed-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
Daniel.
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage
2022-11-07 14:16 ` [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage John Ogness
@ 2022-11-09 8:23 ` Daniel Thompson
2022-11-09 15:19 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Daniel Thompson @ 2022-11-09 8:23 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On Mon, Nov 07, 2022 at 03:22:10PM +0106, John Ogness wrote:
> kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
> are unregistered until the kgdboc_earlycon is setup. This is necessary
> because the trapping of the exit() callback assumes that the exit()
> callback is not called before the trap is setup.
>
> Explicitly document this non-typical console_lock usage.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
Daniel.
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal
2022-11-07 14:16 ` [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal John Ogness
@ 2022-11-09 9:06 ` Daniel Thompson
2022-11-09 9:44 ` John Ogness
0 siblings, 1 reply; 32+ messages in thread
From: Daniel Thompson @ 2022-11-09 9:06 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On Mon, Nov 07, 2022 at 03:22:34PM +0106, John Ogness wrote:
> configure_kgdboc() uses the console_lock for console list iteration. Use
> the console_list_lock instead because list synchronization responsibility
> will be removed from the console_lock in a later change.
>
> The SRCU iterator could have been used here, but a later change will
> relocate the locking of the console_list_lock to also provide
> synchronization against register_console().
>
> Note, the console_lock is still needed to serialize the device()
> callback with other console operations.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
> drivers/tty/serial/kgdboc.c | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
> index 5be381003e58..82b4b4d67823 100644
> --- a/drivers/tty/serial/kgdboc.c
> +++ b/drivers/tty/serial/kgdboc.c
> @@ -451,6 +463,7 @@ static void kgdboc_earlycon_pre_exp_handler(void)
> {
> struct console *con;
> static bool already_warned;
> + int cookie;
>
> if (already_warned)
> return;
> @@ -463,9 +476,14 @@ static void kgdboc_earlycon_pre_exp_handler(void)
> * serial drivers might be OK with this, print a warning once per
> * boot if we detect this case.
> */
> - for_each_console(con)
> + cookie = console_srcu_read_lock();
> + for_each_console_srcu(con) {
> if (con == kgdboc_earlycon_io_ops.cons)
> - return;
> + break;
> + }
> + console_srcu_read_unlock(cookie);
> + if (con)
> + return;
This change isn't mentioned in the patch description.
Daniel.
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal
2022-11-09 9:06 ` Daniel Thompson
@ 2022-11-09 9:44 ` John Ogness
2022-11-10 18:00 ` Petr Mladek
0 siblings, 1 reply; 32+ messages in thread
From: John Ogness @ 2022-11-09 9:44 UTC (permalink / raw)
To: Daniel Thompson
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On 2022-11-09, Daniel Thompson <daniel.thompson@linaro.org> wrote:
>> @@ -463,9 +476,14 @@ static void kgdboc_earlycon_pre_exp_handler(void)
>> * serial drivers might be OK with this, print a warning once per
>> * boot if we detect this case.
>> */
>> - for_each_console(con)
>> + cookie = console_srcu_read_lock();
>> + for_each_console_srcu(con) {
>> if (con == kgdboc_earlycon_io_ops.cons)
>> - return;
>> + break;
>> + }
>> + console_srcu_read_unlock(cookie);
>> + if (con)
>> + return;
>
> This change isn't mentioned in the patch description.
I will move this change into its own separate patch.
tty: serial: kgdboc: use srcu console list iterator
Use srcu console list iteration for safe console list traversal.
Thanks.
John
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage
2022-11-07 14:16 ` [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage John Ogness
2022-11-09 8:23 ` Daniel Thompson
@ 2022-11-09 15:19 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-09 15:19 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
On Mon 2022-11-07 15:22:10, John Ogness wrote:
> kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
> are unregistered until the kgdboc_earlycon is setup. This is necessary
> because the trapping of the exit() callback assumes that the exit()
> callback is not called before the trap is setup.
>
> Explicitly document this non-typical console_lock usage.
It is great to have the description.
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered()
2022-11-08 8:46 ` Geert Uytterhoeven
@ 2022-11-10 13:24 ` Petr Mladek
2022-11-10 13:46 ` John Ogness
0 siblings, 1 reply; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 13:24 UTC (permalink / raw)
To: Geert Uytterhoeven
Cc: John Ogness, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Greg Kroah-Hartman, Jiri Slaby, Andy Shevchenko,
Ilpo Järvinen, Geert Uytterhoeven, Tony Lindgren,
Lukas Wunner, linux-serial
On Tue 2022-11-08 09:46:20, Geert Uytterhoeven wrote:
> Hi John,
>
> On Mon, Nov 7, 2022 at 3:16 PM John Ogness <john.ogness@linutronix.de> wrote:
> > All users of uart_console_enabled() really want to know if a console
> > is registered. It is not reliable to check for CON_ENABLED in order
> > to identify if a console is registered. Use console_is_registered()
> > instead.
> >
> > A _locked() variant is provided because uart_set_options() is always
> > called with the console_list_lock held and must check if a console
> > is registered in order to synchronize with kgdboc.
> >
> > Signed-off-by: John Ogness <john.ogness@linutronix.de>
>
> > --- a/include/linux/serial_core.h
> > +++ b/include/linux/serial_core.h
> > @@ -743,9 +743,20 @@ static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
> > static inline int setup_earlycon(char *buf) { return 0; }
> > #endif
> >
> > -static inline bool uart_console_enabled(struct uart_port *port)
> > +/* Variant of uart_console_registered() when the console_list_lock is held. */
> > +static inline bool uart_console_registered_locked(struct uart_port *port)
> > {
> > - return uart_console(port) && (port->cons->flags & CON_ENABLED);
> > + return uart_console(port) && console_is_registered_locked(port->cons);
> > +}
> > +
> > +static inline bool uart_console_registered(struct uart_port *port)
> > +{
> > + bool ret;
> > +
> > + console_list_lock();
> > + ret = uart_console_registered_locked(port);
> > + console_list_unlock();
> > + return ret;
>
> Perhaps
>
> return uart_console(port) && console_is_registered();
>
> to avoid locking the list when the first condition is not true?
I do not have strong opinion on this. It is true that the code
duplication is trivial but it is a code duplication. Either
way would work for me.
The reset of the code looks good. Feel free to use:
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered()
2022-11-10 13:24 ` Petr Mladek
@ 2022-11-10 13:46 ` John Ogness
0 siblings, 0 replies; 32+ messages in thread
From: John Ogness @ 2022-11-10 13:46 UTC (permalink / raw)
To: Petr Mladek, Geert Uytterhoeven
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, Andy Shevchenko,
Ilpo Järvinen, Geert Uytterhoeven, Tony Lindgren,
Lukas Wunner, linux-serial
On 2022-11-10, Petr Mladek <pmladek@suse.com> wrote:
>>> -static inline bool uart_console_enabled(struct uart_port *port)
>>> +/* Variant of uart_console_registered() when the console_list_lock is held. */
>>> +static inline bool uart_console_registered_locked(struct uart_port *port)
>>> {
>>> - return uart_console(port) && (port->cons->flags & CON_ENABLED);
>>> + return uart_console(port) && console_is_registered_locked(port->cons);
>>> +}
>>> +
>>> +static inline bool uart_console_registered(struct uart_port *port)
>>> +{
>>> + bool ret;
>>> +
>>> + console_list_lock();
>>> + ret = uart_console_registered_locked(port);
>>> + console_list_unlock();
>>> + return ret;
>>
>> Perhaps
>>
>> return uart_console(port) && console_is_registered();
>>
>> to avoid locking the list when the first condition is not true?
>
> I do not have strong opinion on this. It is true that the code
> duplication is trivial but it is a code duplication. Either
> way would work for me.
I will go with Geert's suggestion for v4. It is important that we reduce
lock contention for non-console ports.
> The reset of the code looks good. Feel free to use:
>
> Reviewed-by: Petr Mladek <pmladek@suse.com>
Thanks.
John
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered()
2022-11-07 14:16 ` [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered() John Ogness
@ 2022-11-10 15:00 ` Petr Mladek
0 siblings, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 15:00 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial
On Mon 2022-11-07 15:22:25, John Ogness wrote:
> It is not reliable to check for CON_ENABLED in order to identify if a
> console is registered. Use console_is_registered() instead.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 28/40] tty: serial: pic32_uart: use console_is_registered()
2022-11-07 14:16 ` [PATCH printk v3 28/40] tty: serial: pic32_uart: " John Ogness
@ 2022-11-10 15:01 ` Petr Mladek
0 siblings, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 15:01 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial
On Mon 2022-11-07 15:22:26, John Ogness wrote:
> It is not reliable to check for CON_ENABLED in order to identify if a
> console is registered. Use console_is_registered() instead.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 29/40] tty: serial: samsung_tty: use console_is_registered()
2022-11-07 14:16 ` [PATCH printk v3 29/40] tty: serial: samsung_tty: " John Ogness
@ 2022-11-10 15:01 ` Petr Mladek
0 siblings, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 15:01 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Krzysztof Kozlowski, Alim Akhtar, Greg Kroah-Hartman, Jiri Slaby,
linux-arm-kernel, linux-samsung-soc, linux-serial
On Mon 2022-11-07 15:22:27, John Ogness wrote:
> It is not reliable to check for CON_ENABLED in order to identify if a
> console is registered. Use console_is_registered() instead.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 30/40] tty: serial: xilinx_uartps: use console_is_registered()
2022-11-07 14:16 ` [PATCH printk v3 30/40] tty: serial: xilinx_uartps: " John Ogness
@ 2022-11-10 15:04 ` Petr Mladek
0 siblings, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 15:04 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, Michal Simek, linux-serial,
linux-arm-kernel
On Mon 2022-11-07 15:22:28, John Ogness wrote:
> It is not reliable to check for CON_ENABLED in order to identify if a
> console is registered. Use console_is_registered() instead.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console()
2022-11-07 14:16 ` [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console() John Ogness
@ 2022-11-10 15:13 ` Daniel Thompson
2022-11-10 18:03 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Daniel Thompson @ 2022-11-10 15:13 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On Mon, Nov 07, 2022 at 03:22:35PM +0106, John Ogness wrote:
> Calling tty_find_polling_driver() can lead to uart_set_options() being
> called (via the poll_init() callback of tty_operations) to configure the
> uart. But uart_set_options() can also be called by register_console()
> (via the setup() callback of console).
>
> Take the console_list_lock to synchronize against register_console() and
> also use it for console list traversal. This also ensures the console list
> cannot change until the polling console has been chosen.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
Daniel.
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit
2022-11-07 14:16 ` [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit John Ogness
@ 2022-11-10 15:18 ` Daniel Thompson
2022-11-11 9:59 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Daniel Thompson @ 2022-11-10 15:18 UTC (permalink / raw)
To: John Ogness
Cc: Petr Mladek, Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner,
linux-kernel, Jason Wessel, Douglas Anderson, Greg Kroah-Hartman,
Jiri Slaby, kgdb-bugreport, linux-serial
On Mon, Nov 07, 2022 at 03:22:36PM +0106, John Ogness wrote:
> kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
> are unregistered until the kgdboc_earlycon is setup. The console_list_lock
> should be used instead because list synchronization responsibility will
> be removed from the console_lock in a later change.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
I've not looked at the other patches in the series to understand the
future tense here (e.g. why we need intermediate patches like this one).
However I've no objections to the change so:
Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org>
Daniel.
> ---
> drivers/tty/serial/kgdboc.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c
> index 8c2b7ccdfebf..a3ed9b34e2ab 100644
> --- a/drivers/tty/serial/kgdboc.c
> +++ b/drivers/tty/serial/kgdboc.c
> @@ -558,13 +558,13 @@ static int __init kgdboc_earlycon_init(char *opt)
> */
>
> /*
> - * Hold the console_lock to guarantee that no consoles are
> + * Hold the console_list_lock to guarantee that no consoles are
> * unregistered until the kgdboc_earlycon setup is complete.
> * Trapping the exit() callback relies on exit() not being
> * called until the trap is setup. This also allows safe
> * traversal of the console list and race-free reading of @flags.
> */
> - console_lock();
> + console_list_lock();
> for_each_console(con) {
> if (con->write && con->read &&
> (con->flags & (CON_BOOT | CON_ENABLED)) &&
> @@ -606,7 +606,7 @@ static int __init kgdboc_earlycon_init(char *opt)
> }
>
> unlock:
> - console_unlock();
> + console_list_unlock();
>
> /* Non-zero means malformed option so we always return zero */
> return 0;
> --
> 2.30.2
>
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal
2022-11-09 9:44 ` John Ogness
@ 2022-11-10 18:00 ` Petr Mladek
0 siblings, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 18:00 UTC (permalink / raw)
To: John Ogness
Cc: Daniel Thompson, Sergey Senozhatsky, Steven Rostedt,
Thomas Gleixner, linux-kernel, Jason Wessel, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
On Wed 2022-11-09 10:50:43, John Ogness wrote:
> On 2022-11-09, Daniel Thompson <daniel.thompson@linaro.org> wrote:
> >> @@ -463,9 +476,14 @@ static void kgdboc_earlycon_pre_exp_handler(void)
> >> * serial drivers might be OK with this, print a warning once per
> >> * boot if we detect this case.
> >> */
> >> - for_each_console(con)
> >> + cookie = console_srcu_read_lock();
> >> + for_each_console_srcu(con) {
> >> if (con == kgdboc_earlycon_io_ops.cons)
> >> - return;
> >> + break;
> >> + }
> >> + console_srcu_read_unlock(cookie);
> >> + if (con)
> >> + return;
> >
> > This change isn't mentioned in the patch description.
>
> I will move this change into its own separate patch.
>
> tty: serial: kgdboc: use srcu console list iterator
>
> Use srcu console list iteration for safe console list traversal.
Yes, split it please :-) Anyway, both changes look good to me.
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console()
2022-11-07 14:16 ` [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console() John Ogness
2022-11-10 15:13 ` Daniel Thompson
@ 2022-11-10 18:03 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-10 18:03 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
On Mon 2022-11-07 15:22:35, John Ogness wrote:
> Calling tty_find_polling_driver() can lead to uart_set_options() being
> called (via the poll_init() callback of tty_operations) to configure the
> uart. But uart_set_options() can also be called by register_console()
> (via the setup() callback of console).
>
> Take the console_list_lock to synchronize against register_console() and
> also use it for console list traversal. This also ensures the console list
> cannot change until the polling console has been chosen.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Huh, this is a maze of related calls. At least for me. But the change
seems to be correct.
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit
2022-11-07 14:16 ` [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit John Ogness
2022-11-10 15:18 ` Daniel Thompson
@ 2022-11-11 9:59 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-11 9:59 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial
On Mon 2022-11-07 15:22:36, John Ogness wrote:
> kgdboc_earlycon_init() uses the console_lock to ensure that no consoles
> are unregistered until the kgdboc_earlycon is setup. The console_list_lock
> should be used instead because list synchronization responsibility will
> be removed from the console_lock in a later change.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 00/40] reduce console_lock scope
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
` (10 preceding siblings ...)
2022-11-07 14:16 ` [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console John Ogness
@ 2022-11-11 14:43 ` Mathieu Desnoyers
11 siblings, 0 replies; 32+ messages in thread
From: Mathieu Desnoyers @ 2022-11-11 14:43 UTC (permalink / raw)
To: John Ogness, Petr Mladek, Paul E. McKenney, Frederic Weisbecker
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Neeraj Upadhyay, Josh Triplett, Lai Jiangshan, Joel Fernandes,
rcu, Jason Wessel, Daniel Thompson, Douglas Anderson,
Greg Kroah-Hartman, Jiri Slaby, kgdb-bugreport, linux-serial,
linux-fsdevel, Miguel Ojeda, Richard Weinberger, Anton Ivanov,
Johannes Berg, linux-um, Luis Chamberlain, Aaron Tomlin,
Andy Shevchenko, Ilpo Järvinen, Geert Uytterhoeven,
Tony Lindgren, Lukas Wunner, Geert Uytterhoeven, linux-m68k,
Ard Biesheuvel, linux-efi, linuxppc-dev, Krzysztof Kozlowski,
Alim Akhtar, linux-arm-kernel, linux-samsung-soc, Michal Simek,
Peter Zijlstra, Mathias Nyman, linux-usb, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, Helge Deller,
Javier Martinez Canillas, Thomas Zimmermann, Juergen Gross,
Boris Ostrovsky, Tom Rix, linux-fbdev, dri-devel
On 2022-11-07 09:15, John Ogness wrote:
[...]
>
> The base commit for this series is from Paul McKenney's RCU tree
> and provides an NMI-safe SRCU implementation [1]. Without the
> NMI-safe SRCU implementation, this series is not less safe than
> mainline. But we will need the NMI-safe SRCU implementation for
> atomic consoles anyway, so we might as well get it in
> now. Especially since it _does_ increase the reliability for
> mainline in the panic path.
So, your email got me to review the SRCU nmi-safe series:
[1] https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git/log/?h=srcunmisafe.2022.10.21a
Especially this commit:
https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git/commit/?h=srcunmisafe.2022.10.21a&id=5d0f5953b60f5f7a278085b55ddc73e2932f4c33
I disagree with the overall approach taken there, which is to create
yet another SRCU flavor, this time with explicit "nmi-safe" read-locks.
This adds complexity to the kernel APIs and I think we can be clever
about this and make SRCU nmi-safe without requiring a whole new incompatible
API.
You can find the basic idea needed to achieve this in the libside RCU
user-space implementation. I needed to introduce a split-counter concept
to support rseq vs atomics to keep track of per-cpu grace period counters.
The "rseq" counter is the fast-path, but if rseq fails, the abort handler
uses the atomic counter instead.
https://github.com/compudj/side/blob/main/src/rcu.h#L23
struct side_rcu_percpu_count {
uintptr_t begin;
uintptr_t rseq_begin;
uintptr_t end;
uintptr_t rseq_end;
} __attribute__((__aligned__(SIDE_CACHE_LINE_SIZE)));
The idea is to "split" each percpu counter into two counters, one for rseq,
and the other for atomics. When a grace period wants to observe the value of
a percpu counter, it simply sums the two counters:
https://github.com/compudj/side/blob/main/src/rcu.c#L112
The same idea can be applied to SRCU in the kernel: one counter for percpu ops,
and the other counter for nmi context, so basically:
srcu_read_lock()
if (likely(!in_nmi()))
increment the percpu-ops lock counter
else
increment the atomic lock counter
srcu_read_unlock()
if (likely(!in_nmi()))
increment the percpu-ops unlock counter
else
increment the atomic unlock counter
Then in the grace period sum the percpu-ops and the atomic values whenever
each counter value is read.
This would allow SRCU to be NMI-safe without requiring the callers to
explicitly state whether they need to be nmi-safe or not, and would only
take the overhead of the atomics in the NMI handlers rather than for all
users which happen to use SRCU read locks shared with nmi handlers.
Thoughts ?
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
^ permalink raw reply [flat|nested] 32+ messages in thread
* Re: [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console
2022-11-07 14:16 ` [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console John Ogness
2022-11-08 8:53 ` Geert Uytterhoeven
@ 2022-11-11 17:15 ` Petr Mladek
1 sibling, 0 replies; 32+ messages in thread
From: Petr Mladek @ 2022-11-11 17:15 UTC (permalink / raw)
To: John Ogness
Cc: Sergey Senozhatsky, Steven Rostedt, Thomas Gleixner, linux-kernel,
Greg Kroah-Hartman, Jiri Slaby, linux-serial, Bartosz Golaszewski,
Geert Uytterhoeven
Ccing Bartosz who should be familiar with the early platform code.
On Mon 2022-11-07 15:22:38, John Ogness wrote:
> When setting up the early console, the setup() callback of the
> regular console is used. It is called manually before registering
> the early console instead of providing a setup() callback for the
> early console. This is probably because the early setup needs a
> different @options during the early stage.
This last sentece makes a bit nervous ;-)
I think that I understood it in the end, see below.
> The issue here is that the setup() callback is called without the
> console_list_lock held and functions such as uart_set_options()
> expect that.
>
> Rather than manually calling the setup() function before registering,
> provide an early console setup() callback that will use the different
> early options. This ensures that the error checking, ordering, and
> locking context when setting up the early console are correct.
>
> Note that technically the current implementation works because it is
> only used in early boot. And since the early console setup is
> performed before registering, it cannot race with anything and thus
> does not need any locking. However, longterm maintenance is easier
> when drivers rely on the subsystem API rather than manually
> implementing steps that could cause breakage in the future.
>
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
> drivers/tty/serial/sh-sci.c | 17 +++++++++++++----
> 1 file changed, 13 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
> index 62f773286d44..f3a1cfec757a 100644
> --- a/drivers/tty/serial/sh-sci.c
> +++ b/drivers/tty/serial/sh-sci.c
> @@ -3054,15 +3054,26 @@ static struct console serial_console = {
> };
>
> #ifdef CONFIG_SUPERH
> +static char early_serial_buf[32];
> +
> +static int early_serial_console_setup(struct console *co, char *options)
> +{
> + WARN_ON(options);
> + /*
> + * Use @early_serial_buf because @options will always be
> + * NULL at this early stage.
> + */
The commit message says that we use @early_serial_buf because
the early console probably needs another parameters.
It suggests that @options might be for the later stage and
we need to replace them there. Are we sure that this will always
be NULL?
Background:
The console->setup() is called in two situations:
1. when the console is registered as the default console, see
try_enable_default_console(). In this case, @options
is really NULL.
2. when the console is preferred either via the commnadline,
or device tree, or SPCR, see try_enable_preferred_console().
In this case, some real @options would be passed.
From the code POV, the preferred consoles are added by calling
add_preferred_console().
Now, it means that the WARN_ON() is correct only when this console
is always registered before the preferred consoles are defined.
I think that this is really the case. This console
is actually registered via the "earlyprintk" parameter that
is proceed by the arch-specific code before the preferred
consoles are added the standard way via the kernel commandline.
Note that "earlyprintk" and "earlycon" are two different parameters.
"earlyprintk" normally initializes "early_console" that is
called directly by early_printk(). It is used for super early
debugging. These messages even do not end in the ring buffer.
"earlycon" defines a "normal" console that is used by the standard
printk(). They are later replaced by properly initialized console
drivers that are in sysfs, ...
Note that "earlycon" calls add_preferred_console() so that
the @options are stored and passed from try_enable_preferred_console().
But "earlyprintk" does not call add_preferred_console() so
we need this hack to store and pass the console options
another way.
> + return serial_console_setup(co, early_serial_buf);
> +}
> +
So I would do something like:
static int early_serial_console_setup(struct console *co, char *options)
{
/*
* This early console is registered using earlyprintk= parameter
* that does not call add_preferred_console(). The @options
* are passed using a custom buffer.
*/
WARN_ON(options);
return serial_console_setup(co, early_serial_buf);
}
Also we should explain this in the commit message.
Best Regards,
Petr
^ permalink raw reply [flat|nested] 32+ messages in thread
end of thread, other threads:[~2022-11-11 17:16 UTC | newest]
Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-07 14:15 [PATCH printk v3 00/40] reduce console_lock scope John Ogness
2022-11-07 14:16 ` [PATCH printk v3 02/40] serial: kgdboc: Lock console list in probe function John Ogness
2022-11-09 8:20 ` Daniel Thompson
2022-11-07 14:16 ` [PATCH printk v3 12/40] tty: serial: kgdboc: document console_lock usage John Ogness
2022-11-09 8:23 ` Daniel Thompson
2022-11-09 15:19 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 23/40] serial_core: replace uart_console_enabled() with uart_console_registered() John Ogness
2022-11-08 8:46 ` Geert Uytterhoeven
2022-11-10 13:24 ` Petr Mladek
2022-11-10 13:46 ` John Ogness
2022-11-07 14:16 ` [PATCH printk v3 27/40] tty: serial: earlycon: use console_is_registered() John Ogness
2022-11-10 15:00 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 28/40] tty: serial: pic32_uart: " John Ogness
2022-11-10 15:01 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 29/40] tty: serial: samsung_tty: " John Ogness
2022-11-10 15:01 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 30/40] tty: serial: xilinx_uartps: " John Ogness
2022-11-10 15:04 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 36/40] tty: serial: kgdboc: use console_list_lock for list traversal John Ogness
2022-11-09 9:06 ` Daniel Thompson
2022-11-09 9:44 ` John Ogness
2022-11-10 18:00 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 37/40] tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console() John Ogness
2022-11-10 15:13 ` Daniel Thompson
2022-11-10 18:03 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 38/40] tty: serial: kgdboc: use console_list_lock to trap exit John Ogness
2022-11-10 15:18 ` Daniel Thompson
2022-11-11 9:59 ` Petr Mladek
2022-11-07 14:16 ` [PATCH printk v3 40/40] tty: serial: sh-sci: use setup() callback for early console John Ogness
2022-11-08 8:53 ` Geert Uytterhoeven
2022-11-11 17:15 ` Petr Mladek
2022-11-11 14:43 ` [PATCH printk v3 00/40] reduce console_lock scope Mathieu Desnoyers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).