wire up write_atomic() printing
Subsystems (6)
John Ogness
maintainerv6 cover wire up write_atomic() printing
Hi,
This is v6 of a series to wire up the nbcon consoles so that they actually perform printing using their write_atomic() callback. v5 is here [0]. For information about the motivation of the atomic consoles, please read the cover letter of v1 [1].
The main focus of this series:
- For nbcon consoles, always call write_atomic() directly from printk() caller context for the panic CPU.
- For nbcon consoles, call write_atomic() when unlocking the console lock.
- Only perform the console lock/unlock dance if legacy or boot consoles are registered.
- For legacy consoles, if nbcon consoles are registered, do not attempt to print from printk() caller context for the panic CPU until nbcon consoles have had a chance to print the most significant messages.
- Mark emergency sections. In these sections printk() calls will only store the messages. Upon exiting the emergency section, nbcon consoles are flushed directly. If legacy consoles cannot be flushed safely, an irq_work is triggered to do the legacy console flushing.
This series does _not_ include threaded printing or nbcon drivers. Those features will be added in separate follow-up series.
Note: With this series, a system with _only_ nbcon consoles
registered will not perform console printing unless the console lock or nbcon port lock are used or on panic. This is on purpose. When nbcon kthreads are introduced, they will fill the gaps.
The changes since v5:
- In struct console, rename @nbcon_driver_ctxt to
@nbcon_device_ctxt.
- Rename nbcon_driver_try_acquire() to
nbcon_device_try_acquire().
- Rename nbcon_driver_release() to nbcon_device_release().
- Implement a helper function is_printk_deferred() for use in nbcon_cpu_emergency_exit(), nbcon_cpu_emergency_flush(), vprintk().
- In nbcon_cpu_emergency_exit(), for legacy consoles, try to flush directly if safe. If legacy flushing directly was successful, do not trigger the irq_work.
- In nbcon_cpu_emergency_exit(), do not decrement @cpu_emergency_nesting if it is zero (and WARN_ON_ONCE in this case).
- For register_console() and unregister_console_locked(), use a local variable @use_device_lock to track if locking is used so that the compiler knows that the lock and unlock match.
- For synchronize_rcu_expedited_wait(), move the nbcon_cpu_emergency_flush() after dump_cpu_task().
- Refactor nbcon_atomic_flush_pending_con() as suggested by pmladek. No functional change.
- Update various comments as suggested by pmladek.
- In nbcon_device_try_acquire(), add missing kerneldoc for the return value.
John Ogness
[0] https://lore.kernel.org/lkml/[email protected]
[1] https://lore.kernel.org/lkml/[email protected]
John Ogness (25):
printk: Add notation to console_srcu locking
printk: nbcon: Remove return value for write_atomic()
printk: nbcon: Add detailed doc for write_atomic()
printk: nbcon: Add callbacks to synchronize with driver
printk: nbcon: Use driver synchronization while (un)registering
serial: core: Provide low-level functions to lock port
serial: core: Introduce wrapper to set @uart_port->cons
console: Improve console_srcu_read_flags() comments
nbcon: Add API to acquire context for non-printing operations
serial: core: Implement processing in port->lock wrapper
printk: nbcon: Do not rely on proxy headers
printk: nbcon: Fix kerneldoc for enums
printk: Make console_is_usable() available to nbcon
printk: Let console_is_usable() handle nbcon
printk: Add @flags argument for console_is_usable()
printk: nbcon: Add helper to assign priority based on CPU state
printk: Track registered boot consoles
printk: nbcon: Use nbcon consoles in console_flush_all()
printk: nbcon: Add unsafe flushing on panic
printk: Avoid console_lock dance if no legacy or boot consoles
printk: Track nbcon consoles
printk: Coordinate direct printing in panic
panic: Mark emergency section in oops
rcu: Mark emergency sections in rcu stalls
lockdep: Mark emergency sections in lockdep splats
Petr Mladek (1):
printk: Properly deal with nbcon consoles on seq init
Sebastian Andrzej Siewior (1):
printk: Check printk_deferred_enter()/_exit() usage
Thomas Gleixner (3):
printk: nbcon: Provide function to flush using write_atomic()
printk: nbcon: Implement emergency sections
panic: Mark emergency section in warn
drivers/tty/serial/8250/8250_core.c | 6 +- drivers/tty/serial/amba-pl011.c | 2 +- drivers/tty/serial/serial_core.c | 16 +- include/linux/console.h | 116 ++++++- include/linux/printk.h | 33 +- include/linux/serial_core.h | 117 ++++++- kernel/locking/lockdep.c | 84 ++++- kernel/panic.c | 9 + kernel/printk/internal.h | 73 +++- kernel/printk/nbcon.c | 506 +++++++++++++++++++++++++++- kernel/printk/printk.c | 307 +++++++++++++---- kernel/printk/printk_ringbuffer.h | 2 + kernel/printk/printk_safe.c | 23 +- kernel/rcu/tree_exp.h | 9 + kernel/rcu/tree_stall.h | 11 + 15 files changed, 1181 insertions(+), 133 deletions(-)
base-commit: 596ffa476e201ecbf7ea024f1b59d4f28e91060c
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 1/30 printk: Add notation to console_srcu locking
kernel/printk/printk.c:284:5: sparse: sparse: context imbalance in 'console_srcu_read_lock' - wrong count at exit include/linux/srcu.h:301:9: sparse: sparse: context imbalance in 'console_srcu_read_unlock' - unexpected unlock
--- kernel/printk/printk.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/printk/printk.c +2 -0
@@ -282,6 +282,7 @@ EXPORT_SYMBOL(console_list_unlock);* Return: A cookie to pass to console_srcu_read_unlock(). */ int console_srcu_read_lock (void ) + __acquires(&console_srcu) {return srcu_read_lock_nmisafe (&console_srcu); }
@@ -295,6 +296,7 @@ EXPORT_SYMBOL(console_srcu_read_lock);* Counterpart to console_srcu_read_lock() */ void console_srcu_read_unlock (int cookie) + __releases(&console_srcu) {srcu_read_unlock_nmisafe (&console_srcu, cookie); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 2/30 printk: Properly deal with nbcon consoles on seq init
In reply to John OgnessFrom: Petr Mladek <[email protected]>
If a non-boot console is registering and boot consoles exist, the consoles are flushed before being unregistered. This allows the non-boot console to continue where the boot console left off.
If for whatever reason flushing fails, the lowest seq found from any of the enabled boot consoles is used. Until now con->seq was checked. However, if it is an nbcon boot console, the function nbcon_seq_read() must be used to read seq because con->seq is not updated for nbcon consoles.
Check if it is an nbcon boot console and if so call nbcon_seq_read() to read seq.
Also, avoid usage of con->seq as temporary storage of the starting record. Instead, rename console_init_seq() to get_init_console_seq() and just return the value. For nbcon consoles set the sequence via nbcon_init(), for legacy consoles set con->seq.
The cleaned design should make sure that the value stays and is set before the printing kthread is created.
--- kernel/printk/internal.h | 4 ++-- kernel/printk/nbcon.c | 10 +++------- kernel/printk/printk.c | 41 ++++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 21 deletions(-)
kernel/printk/internal.h +2 -2
@@ -75,7 +75,7 @@ u16 printk_parse_prefix(const char *text, int *level,u64 nbcon_seq_read (struct console *con);void nbcon_seq_force (struct console *con,u64 seq);bool nbcon_alloc (struct console *con); -void nbcon_init (struct console *con); +void nbcon_init (struct console *con,u64 init_seq);void nbcon_free (struct console *con);#else
@@ -96,7 +96,7 @@ static inline bool printk_percpu_data_ready(void) { return false; }static inline u64 nbcon_seq_read (struct console *con) {return 0 ; }static inline void nbcon_seq_force (struct console *con,u64 seq) { }static inline bool nbcon_alloc (struct console *con) {return false; } -static inline void nbcon_init (struct console *con) { } +static inline void nbcon_init (struct console *con,u64 init_seq) { }static inline void nbcon_free (struct console *con) { }#endif /* CONFIG_PRINTK */
kernel/printk/nbcon.c +3 -7
@@ -172,9 +172,6 @@ void nbcon_seq_force(struct console *con, u64 seq)u64 valid_seq =max_t (u64, seq,prb_first_valid_seq (prb));atomic_long_set (&ACCESS_PRIVATE (con, nbcon_seq),__u64seq_to_ulseq (valid_seq)); - -/* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */ - con->seq =0 ; }/**
@@ -961,20 +958,19 @@ bool nbcon_alloc(struct console *con)/** * nbcon_init - Initialize the nbcon console specific data * @con: Console to initialize +* @init_seq: Sequence number of the first record to be emitted * * nbcon_alloc() *must* be called and succeed before this function * is called. -* -* This function expects that the legacy @con->seq has been set. */ -void nbcon_init (struct console *con) +void nbcon_init (struct console *con,u64 init_seq) {struct nbcon_state state = { };/* nbcon_alloc() must have been called and successful! */ BUG_ON (!con->pbufs); -nbcon_seq_force (con, con->seq); +nbcon_seq_force (con, init_seq);nbcon_state_set (con, &state); }
kernel/printk/printk.c +29 -12
90 lines not on this page · raw message
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 3/30 printk: nbcon: Remove return value for write_atomic()
In reply to John OgnessThe return value of write_atomic() does not provide any useful information. On the contrary, it makes things more complicated for the caller to appropriately deal with the information.
Change write_atomic() to not have a return value. If the message did not get printed due to loss of ownership, the caller will notice this on its own. If ownership was not lost, it will be assumed that the driver successfully printed the message and the sequence number for that console will be incremented.
--- include/linux/console.h | 2 +- kernel/printk/nbcon.c | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-)
include/linux/console.h +1 -1
@@ -327,7 +327,7 @@ struct console {struct hlist_node node;/* nbcon console specific members */ -bool (*write_atomic)(struct console *con, +void (*write_atomic)(struct console *con,struct nbcon_write_context *wctxt);atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;
kernel/printk/nbcon.c +7 -8
@@ -849,7 +849,6 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)unsigned long con_dropped;struct nbcon_state cur;unsigned long dropped; -bool done;/* * The printk buffers are filled within an unsafe section. This
@@ -889,16 +888,16 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) wctxt->unsafe_takeover = cur.unsafe_takeover;if (con->write_atomic) { - done = con->write_atomic (con, wctxt); + con->write_atomic (con, wctxt); }else { -nbcon_context_release (ctxt); +/* +* This function should never be called for legacy consoles. +* Handle it as if ownership was lost and try to continue. +*/ WARN_ON_ONCE (1 ); - done = false; - } - -/* If not done, the emit was aborted. */ -if (!done) +nbcon_context_release (ctxt);return false; + }/* * Since any dropped message was successfully output, reset the
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 4/30 printk: Check printk_deferred_enter()/_exit() usage
In reply to John OgnessFrom: Sebastian Andrzej Siewior <[email protected]>
Add validation that printk_deferred_enter()/_exit() are called in non-migration contexts.
--- include/linux/printk.h | 9 +++++---- kernel/printk/internal.h | 3 +++ kernel/printk/printk_safe.c | 12 ++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-)
include/linux/printk.h +5 -4
@@ -157,15 +157,16 @@ int _printk(const char *fmt, ...);*/ __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...); -extern void __printk_safe_enter (void ); -extern void __printk_safe_exit (void ); +extern void __printk_deferred_enter (void ); +extern void __printk_deferred_exit (void ); +/* * The printk_deferred_enter/exit macros are available only as a hack for * some code paths that need to defer all printk console printing. Interrupts * must be disabled for the deferred duration. */ -#define printk_deferred_enter __printk_safe_enter -#define printk_deferred_exit __printk_safe_exit +#define printk_deferred_enter () __printk_deferred_enter() +#define printk_deferred_exit () __printk_deferred_exit()/* * Please don't use printk_ratelimit(), because it shares ratelimiting state
kernel/printk/internal.h +3 -0
@@ -53,6 +53,9 @@ int vprintk_store(int facility, int level, __printf(1, 0) int vprintk_default(const char *fmt, va_list args); __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args); +void __printk_safe_enter (void ); +void __printk_safe_exit (void ); +bool printk_percpu_data_ready (void );#define printk_safe_enter_irqsave (flags) \
kernel/printk/printk_safe.c +12 -0
@@ -26,6 +26,18 @@ void __printk_safe_exit(void)this_cpu_dec (printk_context); } +void __printk_deferred_enter (void ) +{ +cant_migrate (); +__printk_safe_enter (); +} + +void __printk_deferred_exit (void ) +{ +cant_migrate (); +__printk_safe_exit (); +} + asmlinkage int vprintk(const char *fmt, va_list args) {#ifdef CONFIG_KGDB_KDB
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 5/30 printk: nbcon: Add detailed doc for write_atomic()
In reply to John OgnessThe write_atomic() callback has special requirements and is allowed to use special helper functions. Provide detailed documentation of the callback so that a developer has a chance of implementing it correctly.
--- include/linux/console.h | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
include/linux/console.h +29 -4
@@ -285,7 +285,7 @@ struct nbcon_write_context {/** * struct console - The console descriptor structure * @name: The name of the console driver -* @write: Write callback to output messages (Optional) +* @write: Legacy write callback to output messages (Optional) * @read: Read callback for console input (Optional) * @device: The underlying TTY device driver (Optional) * @unblank: Callback to unblank the console (Optional)
@@ -302,7 +302,6 @@ struct nbcon_write_context {* @data: Driver private data * @node: hlist node for the console list * -* @write_atomic: Write callback for atomic context * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @pbufs: Pointer to nbcon private buffer
@@ -327,8 +326,34 @@ struct console {struct hlist_node node;/* nbcon console specific members */ -void (*write_atomic)(struct console *con, -struct nbcon_write_context *wctxt); + +/** +* @write_atomic: +* +* NBCON callback to write out text in any context. +* +* This callback is called with the console already acquired. However, +* a higher priority context is allowed to take it over by default. +* +* The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe() +* around any code where the takeover is not safe, for example, when +* manipulating the serial port registers. +* +* nbcon_enter_unsafe() will fail if the context has lost the console +* ownership in the meantime. In this case, the callback is no longer +* allowed to go forward. It must back out immediately and carefully. +* The buffer content is also no longer trusted since it no longer +* belongs to the context. +* +* The callback should allow the takeover whenever it is safe. It +* increases the chance to see messages when the system is in trouble. +* +* The callback can be called from any context (including NMI). +* Therefore it must avoid usage of any locking and instead rely +* on the console ownership for synchronization. +*/ +void (*write_atomic)(struct console *con,struct nbcon_write_context *wctxt); +atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;struct printk_buffers *pbufs;
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 6/30 printk: nbcon: Add callbacks to synchronize with driver
In reply to John OgnessConsole drivers typically must deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. To provide the necessary synchronization, usually some driver-specific locking mechanism is used (for example, the port spinlock for uart serial consoles).
Until now, usage of this driver-specific locking has been hidden from the printk-subsystem and implemented within the various console callbacks. However, nbcon consoles would need to use it even in the generic code.
Add device_lock() and device_unlock() callback which will need to get implemented by nbcon consoles.
The callbacks will use whatever synchronization mechanism the driver is using for itself. The minimum requirement is to prevent CPU migration. It would allow a context friendly acquiring of nbcon console ownership in non-emergency and non-panic context.
--- include/linux/console.h | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
include/linux/console.h +43 -0
@@ -354,6 +354,49 @@ struct console {*/ void (*write_atomic)(struct console *con,struct nbcon_write_context *wctxt); +/** +* @device_lock: +* +* NBCON callback to begin synchronization with driver code. +* +* Console drivers typically must deal with access to the hardware +* via user input/output (such as an interactive login shell) and +* output of kernel messages via printk() calls. This callback is +* called by the printk-subsystem whenever it needs to synchronize +* with hardware access by the driver. It should be implemented to +* use whatever synchronization mechanism the driver is using for +* itself (for example, the port lock for uart serial consoles). +* +* The callback is always called from task context. It may use any +* synchronization method required by the driver. +* +* IMPORTANT: The callback MUST disable migration. The console driver +* may be using a synchronization mechanism that already takes +* care of this (such as spinlocks). Otherwise this function must +* explicitly call migrate_disable(). +* +* The flags argument is provided as a convenience to the driver. It +* will be passed again to device_unlock(). It can be ignored if the +* driver does not need it. +*/ +void (*device_lock)(struct console *con,unsigned long *flags); + +/** +* @device_unlock: +* +* NBCON callback to finish synchronization with driver code. +* +* It is the counterpart to device_lock(). +* +* This callback is always called from task context. It must +* appropriately re-enable migration (depending on how device_lock() +* disabled migration). +* +* The flags argument is the value of the same variable that was +* passed to device_lock(). +*/ +void (*device_unlock)(struct console *con,unsigned long flags); +atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq;struct printk_buffers *pbufs;
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 7/30 printk: nbcon: Use driver synchronization while (un)registering
In reply to John OgnessConsole drivers typically have to deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls.
They use some classic driver-specific locking mechanism in most situations. But console->write_atomic() callbacks, used by nbcon consoles, are synchronized only by acquiring the console context.
The synchronization via the console context ownership is possible only when the console driver is registered. It is when a particular device driver is connected with a particular console driver.
The two synchronization mechanisms must be synchronized between each other. It is tricky because the console context ownership is quite special. It might be taken over by a higher priority context. Also CPU migration must be disabled. The most tricky part is to (dis)connect these two mechanisms during the console (un)registration.
Use the driver-specific locking callbacks: device_lock(), device_unlock(). They allow taking the device-specific lock while the device is being (un)registered by the related console driver.
For example, these callbacks lock/unlock the port lock for serial port drivers.
--- kernel/printk/printk.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-)
kernel/printk/printk.c +32 -1
@@ -3448,9 +3448,11 @@ static int unregister_console_locked(struct console *console);*/ void register_console (struct console *newcon) { -struct console *con; +bool use_device_lock = (newcon->flags &CON_NBCON ) && newcon->write_atomic;bool bootcon_registered = false;bool realcon_registered = false; +struct console *con; +unsigned long flags;u64 init_seq;int err;
@@ -3537,6 +3539,19 @@ void register_console(struct console *newcon) newcon->seq = init_seq; } +/* +* If another context is actively using the hardware of this new +* console, it will not be aware of the nbcon synchronization. This +* is a risk that two contexts could access the hardware +* simultaneously if this new console is used for atomic printing +* and the other context is still using the hardware. +* +* Use the driver synchronization to ensure that the hardware is not +* in use while this new console transitions to being registered. +*/ +if (use_device_lock) + newcon->device_lock (newcon, &flags); +/* * Put this console in the list - keep the * preferred driver at the head of the list.
@@ -3561,6 +3576,10 @@ void register_console(struct console *newcon)* register_console() completes. */ +/* This new console is now registered. */ +if (use_device_lock) + newcon->device_unlock (newcon, flags); +console_sysfs_notify ();/*
@@ -3589,6 +3608,8 @@ EXPORT_SYMBOL(register_console);/* Must be called under console_list_lock(). */ static int unregister_console_locked (struct console *console) { +bool use_device_lock = (console->flags &CON_NBCON ) && console->write_atomic; +unsigned long flags;int res;lockdep_assert_console_list_lock_held ();
@@ -3607,8 +3628,18 @@ static int unregister_console_locked(struct console *console)if (!console_is_registered_locked (console))return -ENODEV ; +/* +* Use the driver synchronization to ensure that the hardware is not +* in use while this console transitions to being unregistered. +*/ +if (use_device_lock) + console->device_lock (console, &flags); +hlist_del_init_rcu (&console->node); +if (use_device_lock) + console->device_unlock (console, flags); +/* * <HISTORICAL> * If this isn't the last console and it has CON_CONSDEV set, we
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 8/30 serial: core: Provide low-level functions to lock port
In reply to John OgnessIt will be necessary at times for the uart nbcon console drivers to acquire the port lock directly (without the additional nbcon functionality of the port lock wrappers). These are special cases such as the implementation of the device_lock()/device_unlock() callbacks or for internal port lock wrapper synchronization.
Provide low-level variants __uart_port_lock_irqsave() and __uart_port_unlock_irqrestore() for this purpose.
--- include/linux/serial_core.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
include/linux/serial_core.h +18 -0
@@ -588,6 +588,24 @@ struct uart_port {void *private_data;/* generic platform data pointer */ }; +/* +* Only for console->device_lock()/_unlock() callbacks and internal +* port lock wrapper synchronization. +*/ +static inline void __uart_port_lock_irqsave (struct uart_port *up,unsigned long *flags) +{ +spin_lock_irqsave (&up->lock, *flags); +} + +/* +* Only for console->device_lock()/_unlock() callbacks and internal +* port lock wrapper synchronization. +*/ +static inline void __uart_port_unlock_irqrestore (struct uart_port *up,unsigned long flags) +{ +spin_unlock_irqrestore (&up->lock, flags); +} +/** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 9/30 serial: core: Introduce wrapper to set @uart_port->cons
In reply to John OgnessIntroduce uart_port_set_cons() as a wrapper to set @cons of a uart_port. The wrapper sets @cons under the port lock in order to prevent @cons from disappearing while another context is holding the port lock. This is necessary for a follow-up commit relating to the port lock wrappers, which rely on @cons not changing between lock and unlock.
--- drivers/tty/serial/8250/8250_core.c | 6 +++--- drivers/tty/serial/amba-pl011.c | 2 +- drivers/tty/serial/serial_core.c | 16 ++++++++-------- include/linux/serial_core.h | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-)
drivers/tty/serial/8250/8250_core.c +3 -3
@@ -627,11 +627,11 @@ static int univ8250_console_setup(struct console *co, char *options) port = &serial8250_ports[co->index].port;/* link port to console */ - port->cons = co; +uart_port_set_cons (port, co); retval =serial8250_console_setup (port, options, false);if (retval !=0 ) - port->cons =NULL ; +uart_port_set_cons (port,NULL );return retval; }
@@ -689,7 +689,7 @@ static int univ8250_console_match(struct console *co, char *name, int idx,continue ; co->index = i; - port->cons = co; +uart_port_set_cons (port, co);return serial8250_console_setup (port, options, true); }
drivers/tty/serial/amba-pl011.c +1 -1
@@ -2496,7 +2496,7 @@ static int pl011_console_match(struct console *co, char *name, int idx,continue ; co->index = i; - port->cons = co; +uart_port_set_cons (port, co);return pl011_console_setup (co, options); }
drivers/tty/serial/serial_core.c +8 -8
@@ -3145,8 +3145,15 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u state->uart_port = uport; uport->state = state; +/* +* If this port is in use as a console then the spinlock is already +* initialised. +*/ +if (!uart_console_registered (uport)) +uart_port_spin_lock_init (uport); + state->pm_state =UART_PM_STATE_UNDEFINED ; - uport->cons = drv->cons; +uart_port_set_cons (uport, drv->cons); uport->minor = drv->tty_driver->minor_start + uport->line; uport->name =kasprintf (GFP_KERNEL ,"%s%d" , drv->dev_name, drv->tty_driver->name_base + uport->line);
@@ -3155,13 +3162,6 @@ static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *u goto out; } -/* -* If this port is in use as a console then the spinlock is already -* initialised. -*/ -if (!uart_console_registered (uport)) -uart_port_spin_lock_init (uport); -if (uport->cons && uport->dev)of_console_check (uport->dev->of_node, uport->cons->name, uport->line);
include/linux/serial_core.h +17 -0
@@ -606,6 +606,23 @@ static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsignedspin_unlock_irqrestore (&up->lock, flags); } +/** +* uart_port_set_cons - Safely set the @cons field for a uart +* @up: The uart port to set +* @con: The new console to set to +* +* This function must be used to set @up->cons. It uses the port lock to +* synchronize with the port lock wrappers in order to ensure that the console +* cannot change or disappear while another context is holding the port lock. +*/ +static inline void uart_port_set_cons (struct uart_port *up,struct console *con) +{ +unsigned long flags; + +__uart_port_lock_irqsave (up, &flags); + up->cons = con; +__uart_port_unlock_irqrestore (up, flags); +}/** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 10/30 console: Improve console_srcu_read_flags() comments
In reply to John OgnessIt was not clear when exactly console_srcu_read_flags() must be used vs. directly reading @console->flags.
Refactor and clarify that console_srcu_read_flags() is only needed if the console is registered or the caller is in a context where the registration status of the console may change (due to another context).
The function requires the caller holds @console_srcu, which will ensure that the caller sees an appropriate @flags value for the registered console and that exit/cleanup routines will not run if the console is in the process of unregistration.
--- include/linux/console.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
include/linux/console.h +17 -11
@@ -428,28 +428,34 @@ extern void console_list_unlock(void) __releases(console_mutex);extern struct hlist_head console_list;/** -* console_srcu_read_flags - Locklessly read the console flags +* console_srcu_read_flags - Locklessly read flags of a possibly registered +* console * @con: struct console pointer of console to read flags from * -* This function provides the necessary READ_ONCE() and data_race() -* notation for locklessly reading the console flags. The READ_ONCE() -* in this function matches the WRITE_ONCE() when @flags are modified -* for registered consoles with console_srcu_write_flags(). +* Locklessly reading @con->flags provides a consistent read value because +* there is at most one CPU modifying @con->flags and that CPU is using only +* read-modify-write operations to do so. * -* Only use this function to read console flags when locklessly -* iterating the console list via srcu. +* Requires console_srcu_read_lock to be held, which implies that @con might +* be a registered console. The purpose of holding console_srcu_read_lock is +* to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED) +* and that no exit/cleanup routines will run if the console is currently +* undergoing unregistration. +* +* If the caller is holding the console_list_lock or it is _certain_ that +* @con is not and will not become registered, the caller may read +* @con->flags directly instead. * * Context: Any context. +* Return: The current value of the @con->flags field. */ static inline short console_srcu_read_flags (const struct console *con) {WARN_ON_ONCE (!console_srcu_read_lock_is_held ());/* -* Locklessly reading console->flags provides a consistent -* read value because there is at most one CPU modifying -* console->flags and that CPU is using only read-modify-write -* operations to do so. +* The READ_ONCE() matches the WRITE_ONCE() when @flags are modified +* for registered consoles with console_srcu_write_flags(). */ return data_race (READ_ONCE (con->flags)); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 11/30 nbcon: Add API to acquire context for non-printing operations
In reply to John OgnessProvide functions nbcon_device_try_acquire() and nbcon_device_release() which will try to acquire the nbcon console ownership with NBCON_PRIO_NORMAL and mark it unsafe for handover/takeover.
These functions are to be used together with the device-specific locking when performing non-printing activities on the console device. They will allow synchronization against the atomic_write() callback which will be serialized, for higher priority contexts, only by acquiring the console context ownership.
Pitfalls:
The API requires to be called in a context with migration disabled because it uses per-CPU variables internally.
The context is set unsafe for a takeover all the time. It guarantees full serialization against any atomic_write() caller except for the final flush in panic() which might try an unsafe takeover.
--- include/linux/console.h | 2 ++ include/linux/printk.h | 14 +++++++++++ kernel/printk/nbcon.c | 56 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-)
include/linux/console.h +2 -0
@@ -304,6 +304,7 @@ struct nbcon_write_context {* * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print +* @nbcon_device_ctxt: Context available for non-printing operations * @pbufs: Pointer to nbcon private buffer */ struct console {
@@ -399,6 +400,7 @@ struct console {atomic_t __private nbcon_state;atomic_long_t __private nbcon_seq; +struct nbcon_context __private nbcon_device_ctxt;struct printk_buffers *pbufs; };
include/linux/printk.h +14 -0
@@ -9,6 +9,8 @@#include <linux/ratelimit_types.h> #include <linux/once_lite.h> +struct console ; +extern const char linux_banner[];extern const char linux_proc_banner[];
@@ -193,6 +195,8 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold;void printk_trigger_flush (void ); +extern bool nbcon_device_try_acquire (struct console *con); +extern void nbcon_device_release (struct console *con);#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)
@@ -272,6 +276,16 @@ static inline void dump_stack(void)static inline void printk_trigger_flush (void ) { } + +static inline bool nbcon_device_try_acquire (struct console *con) +{ +return false; +} + +static inline void nbcon_device_release (struct console *con) +{ +} +#endif bool this_cpu_in_panic (void );
kernel/printk/nbcon.c +55 -1
78 lines not on this page · raw message
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 12/30 serial: core: Implement processing in port->lock wrapper
In reply to John OgnessCurrently the port->lock wrappers uart_port_lock(), uart_port_unlock() (and their variants) only lock/unlock the spin_lock.
If the port is an nbcon console, the wrappers must also acquire/release the console and mark the region as unsafe. This allows general port->lock synchronization to be synchronized with the nbcon console ownership.
Note that __uart_port_using_nbcon() relies on the port->lock being held while a console is added and removed from the console list (i.e. all uart nbcon drivers *must* take the port->lock in their device_lock() callbacks).
--- include/linux/serial_core.h | 82 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-)
include/linux/serial_core.h +80 -2
@@ -12,6 +12,8 @@#include <linux/console.h> #include <linux/interrupt.h> #include <linux/circ_buf.h> +#include <linux/lockdep.h> +#include <linux/printk.h> #include <linux/spinlock.h> #include <linux/sched.h> #include <linux/tty.h>
@@ -623,6 +625,60 @@ static inline void uart_port_set_cons(struct uart_port *up, struct console *con) up->cons = con;__uart_port_unlock_irqrestore (up, flags); } + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_using_nbcon (struct uart_port *up) +{ +lockdep_assert_held_once (&up->lock); + +if (likely (!uart_console (up))) +return false; + +/* +* @up->cons is only modified under the port lock. Therefore it is +* certain that it cannot disappear here. +* +* @up->cons->node is added/removed from the console list under the +* port lock. Therefore it is certain that the registration status +* cannot change here, thus @up->cons->flags can be read directly. +*/ +if (hlist_unhashed_lockless (&up->cons->node) || + !(up->cons->flags &CON_NBCON ) || + !up->cons->write_atomic) { +return false; + } + +return true; +} + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_nbcon_try_acquire (struct uart_port *up) +{ +if (!__uart_port_using_nbcon (up)) +return true; + +return nbcon_device_try_acquire (up->cons); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_acquire (struct uart_port *up) +{ +if (!__uart_port_using_nbcon (up)) +return ; + +while (!nbcon_device_try_acquire (up->cons)) +cpu_relax (); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_release (struct uart_port *up) +{ +if (!__uart_port_using_nbcon (up)) +return ; + +nbcon_device_release (up->cons); +} +/** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure
@@ -630,6 +686,7 @@ static inline void uart_port_set_cons(struct uart_port *up, struct console *con)static inline void uart_port_lock (struct uart_port *up) {spin_lock (&up->lock); +__uart_port_nbcon_acquire (up); }/**
@@ -639,6 +696,7 @@ static inline void uart_port_lock(struct uart_port *up)static inline void uart_port_lock_irq (struct uart_port *up) {spin_lock_irq (&up->lock); +__uart_port_nbcon_acquire (up); }/**
@@ -649,6 +707,7 @@ static inline void uart_port_lock_irq(struct uart_port *up)static inline void uart_port_lock_irqsave (struct uart_port *up,unsigned long *flags) {spin_lock_irqsave (&up->lock, *flags); +__uart_port_nbcon_acquire (up); }/**
@@ -659,7 +718,15 @@ static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *f*/ static inline bool uart_port_trylock (struct uart_port *up) { -return spin_trylock (&up->lock); +if (!spin_trylock (&up->lock)) +return false; + +if (!__uart_port_nbcon_try_acquire (up)) { +spin_unlock (&up->lock); +return false; + } + +return true; }/**
@@ -671,7 +738,15 @@ static inline bool uart_port_trylock(struct uart_port *up)*/ static inline bool uart_port_trylock_irqsave (struct uart_port *up,unsigned long *flags) { -return spin_trylock_irqsave (&up->lock, *flags); +if (!spin_trylock_irqsave (&up->lock, *flags)) +return false; + +if (!__uart_port_nbcon_try_acquire (up)) { +spin_unlock_irqrestore (&up->lock, *flags); +return false; + } + +return true; }/**
@@ -680,6 +755,7 @@ static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long*/ static inline void uart_port_unlock (struct uart_port *up) { +__uart_port_nbcon_release (up);spin_unlock (&up->lock); }
@@ -689,6 +765,7 @@ static inline void uart_port_unlock(struct uart_port *up)*/ static inline void uart_port_unlock_irq (struct uart_port *up) { +__uart_port_nbcon_release (up);spin_unlock_irq (&up->lock); }
@@ -699,6 +776,7 @@ static inline void uart_port_unlock_irq(struct uart_port *up)*/ static inline void uart_port_unlock_irqrestore (struct uart_port *up,unsigned long flags) { +__uart_port_nbcon_release (up);spin_unlock_irqrestore (&up->lock, flags); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 13/30 printk: nbcon: Do not rely on proxy headers
In reply to John OgnessThe headers kernel.h, serial_core.h, and console.h allow for the definitions of many types and functions from other headers. Rather than relying on these as proxy headers, explicitly include all headers providing needed definitions. Also sort the list alphabetically to be able to easily detect duplicates.
--- kernel/printk/internal.h | 8 ++++++-- kernel/printk/nbcon.c | 13 ++++++++++++- kernel/printk/printk_ringbuffer.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-)
kernel/printk/internal.h +6 -2
@@ -2,11 +2,12 @@/* * internal.h - printk internal definitions */ -#include <linux/percpu.h> #include <linux/console.h> -#include "printk_ringbuffer.h" +#include <linux/percpu.h> +#include <linux/types.h> #if defined(CONFIG_PRINTK ) && defined(CONFIG_SYSCTL ) +struct ctl_table ;void __init printk_sysctl_init (void );int devkmsg_sysctl_set_loglvl (struct ctl_table *table,int write,void *buffer,size_t *lenp,loff_t *ppos);
@@ -43,6 +44,9 @@ enum printk_info_flags { LOG_CONT = 8, /* text is a fragment of a continuation line */ }; +struct printk_ringbuffer ; +struct dev_printk_info ; +extern struct printk_ringbuffer *prb;__printf (4 ,0 )
kernel/printk/nbcon.c +12 -1
@@ -2,13 +2,24 @@// Copyright (C) 2022 Linutronix GmbH, John Ogness // Copyright (C) 2022 Intel, Thomas Gleixner -#include <linux/kernel.h> +#include <linux/atomic.h> +#include <linux/bug.h> #include <linux/console.h> #include <linux/delay.h> +#include <linux/errno.h> #include <linux/export.h> +#include <linux/init.h> +#include <linux/irqflags.h> +#include <linux/minmax.h> +#include <linux/percpu.h> +#include <linux/preempt.h> #include <linux/slab.h> +#include <linux/smp.h> +#include <linux/stddef.h> #include <linux/string.h> +#include <linux/types.h> #include "internal.h" +#include "printk_ringbuffer.h" /* * Printk console printing implementation for consoles which does not depend * on the legacy style console_lock mechanism.
kernel/printk/printk_ringbuffer.h +2 -0
@@ -5,6 +5,8 @@#include <linux/atomic.h> #include <linux/dev_printk.h> +#include <linux/stddef.h> +#include <linux/types.h> /* * Meta information about each stored message.
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 14/30 printk: nbcon: Fix kerneldoc for enums
In reply to John OgnessKerneldoc requires enums to be specified as such. Otherwise it is interpreted as function documentation.
--- include/linux/console.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/console.h +2 -2
@@ -137,7 +137,7 @@ static inline int con_debug_leave(void)*/ /** -* cons_flags - General console flags +* enum cons_flags - General console flags * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate * output of messages that were already shown by boot * consoles or read by userspace via syslog() syscall.
@@ -218,7 +218,7 @@ struct nbcon_state {static_assert (sizeof (struct nbcon_state ) <=sizeof (int ));/** -* nbcon_prio - console owner priority for nbcon consoles +* enum nbcon_prio - console owner priority for nbcon consoles * @NBCON_PRIO_NONE: Unused * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...)
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 15/30 printk: Make console_is_usable() available to nbcon
In reply to John OgnessMove console_is_usable() as-is into internal.h so that it can be used by nbcon printing functions as well.
--- kernel/printk/internal.h | 32 ++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 30 ------------------------------ 2 files changed, 32 insertions(+), 30 deletions(-)
kernel/printk/internal.h +32 -0
@@ -85,6 +85,36 @@ bool nbcon_alloc(struct console *con);void nbcon_init (struct console *con,u64 init_seq);void nbcon_free (struct console *con); +/* +* Check if the given console is currently capable and allowed to print +* records. +* +* Requires the console_srcu_read_lock. +*/ +static inline bool console_is_usable (struct console *con) +{ +short flags =console_srcu_read_flags (con); + +if (!(flags &CON_ENABLED )) +return false; + +if ((flags &CON_SUSPENDED )) +return false; + +if (!con->write) +return false; + +/* +* Console drivers may assume that per-cpu resources have been +* allocated. So unless they're explicitly marked as being able to +* cope (CON_ANYTIME) don't call them until this CPU is officially up. +*/ +if (!cpu_online (raw_smp_processor_id ()) && !(flags &CON_ANYTIME )) +return false; + +return true; +} +#else #define PRINTK_PREFIX_MAX 0
@@ -106,6 +136,8 @@ static inline bool nbcon_alloc(struct console *con) { return false; }static inline void nbcon_init (struct console *con,u64 init_seq) { }static inline void nbcon_free (struct console *con) { } +static inline bool console_is_usable (struct console *con) {return false; } +#endif /* CONFIG_PRINTK */ extern struct printk_buffers printk_shared_pbufs;
kernel/printk/printk.c +0 -30
@@ -2697,36 +2697,6 @@ int is_console_locked(void) }EXPORT_SYMBOL (is_console_locked); -/* -* Check if the given console is currently capable and allowed to print -* records. -* -* Requires the console_srcu_read_lock. -*/ -static inline bool console_is_usable (struct console *con) -{ -short flags =console_srcu_read_flags (con); - -if (!(flags &CON_ENABLED )) -return false; - -if ((flags &CON_SUSPENDED )) -return false; - -if (!con->write) -return false; - -/* -* Console drivers may assume that per-cpu resources have been -* allocated. So unless they're explicitly marked as being able to -* cope (CON_ANYTIME) don't call them until this CPU is officially up. -*/ -if (!cpu_online (raw_smp_processor_id ()) && !(flags &CON_ANYTIME )) -return false; - -return true; -} -static void __console_unlock (void ) { console_locked =0 ;
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 16/30 printk: Let console_is_usable() handle nbcon
In reply to John OgnessThe nbcon consoles use a different printing callback. For nbcon consoles, check for the write_atomic() callback instead of write().
--- kernel/printk/internal.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/printk/internal.h +9 -2
@@ -87,6 +87,8 @@ void nbcon_free(struct console *con);/* * Check if the given console is currently capable and allowed to print +* records. Note that this function does not consider the current context, +* which can also play a role in deciding if @con can be used to print * records. * * Requires the console_srcu_read_lock.
@@ -101,8 +103,13 @@ static inline bool console_is_usable(struct console *con)if ((flags &CON_SUSPENDED ))return false; -if (!con->write) -return false; +if (flags &CON_NBCON ) { +if (!con->write_atomic) +return false; + }else { +if (!con->write) +return false; + }/* * Console drivers may assume that per-cpu resources have been
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 17/30 printk: Add @flags argument for console_is_usable()
In reply to John OgnessThe caller of console_is_usable() usually needs @console->flags for its own checks. Rather than having console_is_usable() read its own copy, make the caller pass in the @flags. This also ensures that the caller saw the same @flags value.
--- kernel/printk/internal.h | 8 ++------ kernel/printk/printk.c | 5 +++-- 2 files changed, 5 insertions(+), 8 deletions(-)
kernel/printk/internal.h +2 -6
@@ -90,13 +90,9 @@ void nbcon_free(struct console *con);* records. Note that this function does not consider the current context, * which can also play a role in deciding if @con can be used to print * records. -* -* Requires the console_srcu_read_lock. */ -static inline bool console_is_usable (struct console *con) +static inline bool console_is_usable (struct console *con,short flags) { -short flags =console_srcu_read_flags (con); -if (!(flags &CON_ENABLED ))return false;
@@ -143,7 +139,7 @@ static inline bool nbcon_alloc(struct console *con) { return false; }static inline void nbcon_init (struct console *con,u64 init_seq) { }static inline void nbcon_free (struct console *con) { } -static inline bool console_is_usable (struct console *con) {return false; } +static inline bool console_is_usable (struct console *con,short flags) {return false; }#endif /* CONFIG_PRINTK */
kernel/printk/printk.c +3 -2
@@ -2942,9 +2942,10 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove cookie =console_srcu_read_lock ();for_each_console_srcu (con) { +short flags =console_srcu_read_flags (con);bool progress; -if (!console_is_usable (con)) +if (!console_is_usable (con, flags))continue ; any_usable = true;
@@ -3825,7 +3826,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre* that they make forward progress, so only increment * @diff for usable consoles. */ -if (!console_is_usable (c)) +if (!console_is_usable (c, flags))continue ;if (flags &CON_NBCON ) {
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 18/30 printk: nbcon: Add helper to assign priority based on CPU state
In reply to John OgnessAdd a helper function to use the current state of the CPU to determine which priority to assign to the printing context.
The EMERGENCY priority handling is added in a follow-up commit. It will use a per-CPU variable.
Note: nbcon_device_try_acquire(), which is used by console
drivers to acquire the nbcon console for non-printing activities, will always use NORMAL priority.
--- kernel/printk/internal.h | 2 ++ kernel/printk/nbcon.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+)
kernel/printk/internal.h +2 -0
@@ -84,6 +84,7 @@ void nbcon_seq_force(struct console *con, u64 seq);bool nbcon_alloc (struct console *con);void nbcon_init (struct console *con,u64 init_seq);void nbcon_free (struct console *con); +enum nbcon_prio nbcon_get_default_prio (void );/* * Check if the given console is currently capable and allowed to print
@@ -138,6 +139,7 @@ static inline void nbcon_seq_force(struct console *con, u64 seq) { }static inline bool nbcon_alloc (struct console *con) {return false; }static inline void nbcon_init (struct console *con,u64 init_seq) { }static inline void nbcon_free (struct console *con) { } +static inline enum nbcon_prio nbcon_get_default_prio (void ) {return NBCON_PRIO_NONE ; }static inline bool console_is_usable (struct console *con,short flags) {return false; }
kernel/printk/nbcon.c +16 -0
@@ -937,6 +937,22 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)return nbcon_context_exit_unsafe (ctxt); } +/** +* nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon +* printing on the current CPU +* +* Context: Any context which could not be migrated to another CPU. +* Return: The nbcon_prio to use for acquiring an nbcon console in this +* context for printing. +*/ +enum nbcon_prio nbcon_get_default_prio (void ) +{ +if (this_cpu_in_panic ()) +return NBCON_PRIO_PANIC ; + +return NBCON_PRIO_NORMAL ; +} +/** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 19/30 printk: nbcon: Provide function to flush using write_atomic()
In reply to John OgnessFrom: Thomas Gleixner <[email protected]>
Provide nbcon_atomic_flush_pending() to perform flushing of all registered nbcon consoles using their write_atomic() callback.
Unlike console_flush_all(), nbcon_atomic_flush_pending() will only flush up through the newest record at the time of the call. This prevents a CPU from printing unbounded when other CPUs are adding records. If new records are added while flushing, it is expected that the dedicated printer threads will print those records. If the printer thread is not available (which is always the case at this point in the rework), nbcon_atomic_flush_pending() _will_ flush all records in the ringbuffer.
Unlike console_flush_all(), nbcon_atomic_flush_pending() will fully flush one console before flushing the next. This helps to guarantee that a block of pending records (such as a stack trace in an emergency situation) can be printed atomically at once before releasing console ownership.
nbcon_atomic_flush_pending() is safe in any context because it uses write_atomic() and acquires with unsafe_takeover disabled.
Use it in console_flush_on_panic() before flushing legacy consoles. The legacy write() callbacks are not fully safe when oops_in_progress is set.
Also use it in nbcon_device_release() to flush records added while the driver had the console locked to perform non-printing operations.
--- kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 169 ++++++++++++++++++++++++++++++++++++++- kernel/printk/printk.c | 2 + 3 files changed, 170 insertions(+), 3 deletions(-)
kernel/printk/internal.h +2 -0
@@ -85,6 +85,7 @@ bool nbcon_alloc(struct console *con);void nbcon_init (struct console *con,u64 init_seq);void nbcon_free (struct console *con);enum nbcon_prio nbcon_get_default_prio (void ); +void nbcon_atomic_flush_pending (void );/* * Check if the given console is currently capable and allowed to print
@@ -140,6 +141,7 @@ static inline bool nbcon_alloc(struct console *con) { return false; }static inline void nbcon_init (struct console *con,u64 init_seq) { }static inline void nbcon_free (struct console *con) { }static inline enum nbcon_prio nbcon_get_default_prio (void ) {return NBCON_PRIO_NONE ; } +static inline void nbcon_atomic_flush_pending (void ) { }static inline bool console_is_usable (struct console *con,short flags) {return false; }
kernel/printk/nbcon.c +166 -3
187 lines not on this page · raw message
kernel/printk/printk.c +2 -0
@@ -3172,6 +3172,8 @@ void console_flush_on_panic(enum con_flush_mode mode)console_srcu_read_unlock (cookie); } +nbcon_atomic_flush_pending (); +console_flush_all (false, &next_seq, &handover); }
Signature · 1 line
2.39.2
Re: [PATCH printk v6 19/30] printk: nbcon: Provide function to flush using write_atomic()
Quoted 28 lines · John Ogness
From: Thomas Gleixner <[email protected]>
Provide
nbcon_atomic_flush_pending()to perform flushing of all registered nbcon consoles using theirwrite_atomic()callback.Unlike
console_flush_all(),nbcon_atomic_flush_pending()will only flush up through the newest record at the time of the call. This prevents a CPU from printing unbounded when other CPUs are adding records. If new records are added while flushing, it is expected that the dedicated printer threads will print those records. If the printer thread is not available (which is always the case at this point in the rework),nbcon_atomic_flush_pending()_will_ flush all records in the ringbuffer.Unlike
console_flush_all(),nbcon_atomic_flush_pending()will fully flush one console before flushing the next. This helps to guarantee that a block of pending records (such as a stack trace in an emergency situation) can be printed atomically at once before releasing console ownership.
nbcon_atomic_flush_pending()is safe in any context because it useswrite_atomic()and acquires withunsafe_takeoverdisabled.Use it in
console_flush_on_panic()before flushing legacy consoles. The legacywrite()callbacks are not fully safe whenoops_in_progressis set.
Quoted 7 lines
Also use it in
nbcon_device_release()to flush records added while the driver had the console locked to perform non-printing operations.Co-developed-by John Ogness <[email protected]>Signed-off-by John Ogness <[email protected]>Signed-off-by Thomas Gleixner (Intel) <[email protected]>
Best Regards,
Petr
John Ogness
maintainerPATCH v6 20/30 printk: Track registered boot consoles
In reply to John OgnessUnfortunately it is not known if a boot console and a regular (legacy or nbcon) console use the same hardware. For this reason they must not be allowed to print simultaneously.
For legacy consoles this is not an issue because they are already synchronized with the boot consoles using the console lock. However nbcon consoles can be triggered separately.
Add a global flag @have_boot_console to identify if any boot consoles are registered. This will be used in follow-up commits to ensure that boot consoles and nbcon consoles cannot print simultaneously.
--- kernel/printk/printk.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
kernel/printk/printk.c +24 -0
@@ -463,6 +463,14 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;/* syslog_lock protects syslog_* variables and write access to clear_seq. */ static DEFINE_MUTEX (syslog_lock); +/* +* Specifies if a boot console is registered. If boot consoles are present, +* nbcon consoles cannot print simultaneously and must be synchronized by +* the console lock. This is because boot consoles and nbcon consoles may +* have mapped the same hardware. +*/ +static bool have_boot_console; +#ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD (log_wait);/* All 3 protected by @syslog_lock. */
@@ -3512,6 +3520,9 @@ void register_console(struct console *newcon) newcon->seq = init_seq; } +if (newcon->flags &CON_BOOT ) + have_boot_console = true; +/* * If another context is actively using the hardware of this new * console, it will not be aware of the nbcon synchronization. This
@@ -3582,7 +3593,9 @@ EXPORT_SYMBOL(register_console);static int unregister_console_locked (struct console *console) {bool use_device_lock = (console->flags &CON_NBCON ) && console->write_atomic; +bool found_boot_con = false;unsigned long flags; +struct console *c;int res;lockdep_assert_console_list_lock_held ();
@@ -3640,6 +3653,17 @@ static int unregister_console_locked(struct console *console)if (console->exit) res = console->exit (console); +/* +* With this console gone, the global flags tracking registered +* console types may have changed. Update them. +*/ +for_each_console (c) { +if (c->flags &CON_BOOT ) + found_boot_con = true; + } +if (!found_boot_con) + have_boot_console = found_boot_con; +return res; }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 21/30 printk: nbcon: Use nbcon consoles in console_flush_all()
In reply to John OgnessAllow nbcon consoles to print messages in the legacy printk() caller context (printing via unlock) by integrating them into console_flush_all(). The write_atomic() callback is used for printing.
Provide nbcon_legacy_emit_next_record(), which acts as the nbcon variant of console_emit_next_record(). Call this variant within console_flush_all() for nbcon consoles. Since nbcon consoles use their own @nbcon_seq variable to track the next record to print, this also must be appropriately handled in console_flush_all().
Note that the legacy printing logic uses @handover to detect handovers for printing all consoles. For nbcon consoles, handovers/takeovers occur on a per-console basis and thus do not cause the console_flush_all() loop to abort.
--- kernel/printk/internal.h | 6 +++ kernel/printk/nbcon.c | 87 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 17 +++++--- 3 files changed, 105 insertions(+), 5 deletions(-)
kernel/printk/internal.h +6 -0
@@ -78,6 +78,8 @@ void defer_console_output(void);u16 printk_parse_prefix (const char *text,int *level,enum printk_info_flags *flags); +void console_lock_spinning_enable (void ); +int console_lock_spinning_disable_and_check (int cookie);u64 nbcon_seq_read (struct console *con);void nbcon_seq_force (struct console *con,u64 seq);
@@ -86,6 +88,8 @@ void nbcon_init(struct console *con, u64 init_seq);void nbcon_free (struct console *con);enum nbcon_prio nbcon_get_default_prio (void );void nbcon_atomic_flush_pending (void ); +bool nbcon_legacy_emit_next_record (struct console *con,bool *handover, +int cookie);/* * Check if the given console is currently capable and allowed to print
@@ -142,6 +146,8 @@ static inline void nbcon_init(struct console *con, u64 init_seq) { }static inline void nbcon_free (struct console *con) { }static inline enum nbcon_prio nbcon_get_default_prio (void ) {return NBCON_PRIO_NONE ; }static inline void nbcon_atomic_flush_pending (void ) { } +static inline bool nbcon_legacy_emit_next_record (struct console *con,bool *handover, +int cookie) {return false; }static inline bool console_is_usable (struct console *con,short flags) {return false; }
kernel/printk/nbcon.c +87 -0
93 lines not on this page · raw message
kernel/printk/printk.c +12 -5
@@ -1860,7 +1860,7 @@ static bool console_waiter;* there may be a waiter spinning (like a spinlock). Also it must be * ready to hand over the lock at the end of the section. */ -static void console_lock_spinning_enable (void ) +void console_lock_spinning_enable (void ) {/* * Do not use spinning in panic(). The panic CPU wants to keep the lock.
@@ -1899,7 +1899,7 @@ static void console_lock_spinning_enable(void)* * Return: 1 if the lock rights were passed, 0 otherwise. */ -static int console_lock_spinning_disable_and_check (int cookie) +int console_lock_spinning_disable_and_check (int cookie) {int waiter;
@@ -2951,13 +2951,20 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove cookie =console_srcu_read_lock ();for_each_console_srcu (con) {short flags =console_srcu_read_flags (con); +u64 printk_seq;bool progress;if (!console_is_usable (con, flags))continue ; any_usable = true; - progress =console_emit_next_record (con, handover, cookie); +if (flags &CON_NBCON ) { + progress =nbcon_legacy_emit_next_record (con, handover, cookie); + printk_seq =nbcon_seq_read (con); + }else { + progress =console_emit_next_record (con, handover, cookie); + printk_seq = con->seq; + }/* * If a handover has occurred, the SRCU read lock
@@ -2967,8 +2974,8 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handovereturn false;/* Track the next of the highest seq flushed. */ -if (con->seq > *next_seq) - *next_seq = con->seq; +if (printk_seq > *next_seq) + *next_seq = printk_seq;if (!progress)continue ;
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 22/30 printk: nbcon: Add unsafe flushing on panic
In reply to John OgnessAdd nbcon_atomic_flush_unsafe() to flush all nbcon consoles using the write_atomic() callback and allowing unsafe hostile takeovers. Call this at the end of panic() as a final attempt to flush any pending messages.
Note that legacy consoles use unsafe methods for flushing from the beginning of panic (see bust_spinlocks()). Therefore, systems using both legacy and nbcon consoles may still fail to see panic messages due to unsafe legacy console usage.
--- include/linux/printk.h | 5 +++++ kernel/panic.c | 1 + kernel/printk/nbcon.c | 32 +++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 7 deletions(-)
include/linux/printk.h +5 -0
@@ -197,6 +197,7 @@ extern asmlinkage void dump_stack(void) __cold;void printk_trigger_flush (void );extern bool nbcon_device_try_acquire (struct console *con);extern void nbcon_device_release (struct console *con); +void nbcon_atomic_flush_unsafe (void );#else static inline __printf(1, 0) int vprintk(const char *s, va_list args)
@@ -286,6 +287,10 @@ static inline void nbcon_device_release(struct console *con) { } +static inline void nbcon_atomic_flush_unsafe (void ) +{ +} +#endif bool this_cpu_in_panic (void );
kernel/panic.c +1 -0
@@ -453,6 +453,7 @@ void panic(const char *fmt, ...)* Explicitly flush the kernel log buffer one last time. */ console_flush_on_panic (CONSOLE_FLUSH_PENDING ); +nbcon_atomic_flush_unsafe ();local_irq_enable ();for (i =0 ; ; i +=PANIC_TIMER_STEP ) {
kernel/printk/nbcon.c +25 -7
@@ -1044,6 +1044,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,* write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record +* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on * failure.
@@ -1062,7 +1063,8 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,* returned, it cannot be expected that the unfinalized record will become * available. */ -static int __nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq) +static int __nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq, +bool allow_unsafe_takeover) {struct nbcon_write_context wctxt = { };struct nbcon_context *ctxt = &ACCESS_PRIVATE (&wctxt, ctxt);
@@ -1071,6 +1073,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq) ctxt->console = con; ctxt->spinwait_max_us =2000 ; ctxt->prio =nbcon_get_default_prio (); + ctxt->allow_unsafe_takeover = allow_unsafe_takeover;if (!nbcon_context_try_acquire (ctxt))return -EPERM ;
@@ -1101,13 +1104,15 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)* write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record +* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * This will stop flushing before @stop_seq if another context has ownership. * That context is then responsible for the flushing. Likewise, if new records * are added while this context was flushing and there is no other context * to handle the printing, this context must also flush those records. */ -static void nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq) +static void nbcon_atomic_flush_pending_con (struct console *con,u64 stop_seq, +bool allow_unsafe_takeover) {unsigned long flags;int err;
@@ -1121,7 +1126,7 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)*/ local_irq_save (flags); - err =__nbcon_atomic_flush_pending_con (con, stop_seq); + err =__nbcon_atomic_flush_pending_con (con, stop_seq, allow_unsafe_takeover);local_irq_restore (flags);
@@ -1151,8 +1156,9 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)* __nbcon_atomic_flush_pending - Flush all nbcon consoles using their * write_atomic() callback * @stop_seq: Flush up until this record +* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers */ -static void __nbcon_atomic_flush_pending (u64 stop_seq) +static void __nbcon_atomic_flush_pending (u64 stop_seq,bool allow_unsafe_takeover) {struct console *con;int cookie;
@@ -1170,7 +1176,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)if (nbcon_seq_read (con) >= stop_seq)continue ; -nbcon_atomic_flush_pending_con (con, stop_seq); +nbcon_atomic_flush_pending_con (con, stop_seq, allow_unsafe_takeover); }console_srcu_read_unlock (cookie); }
@@ -1185,7 +1191,19 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)*/ void nbcon_atomic_flush_pending (void ) { -__nbcon_atomic_flush_pending (prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending (prb_next_reserve_seq (prb), false); +} + +/** +* nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their +* write_atomic() callback and allowing unsafe hostile takeovers +* +* Flush the backlog up through the currently newest record. Unsafe hostile +* takeovers will be performed, if necessary. +*/ +void nbcon_atomic_flush_unsafe (void ) +{ +__nbcon_atomic_flush_pending (prb_next_reserve_seq (prb), true); }/**
@@ -1315,7 +1333,7 @@ void nbcon_device_release(struct console *con) cookie =console_srcu_read_lock ();if (console_is_usable (con,console_srcu_read_flags (con)) &&prb_read_valid (prb,nbcon_seq_read (con),NULL )) { -__nbcon_atomic_flush_pending_con (con,prb_next_reserve_seq (prb)); +__nbcon_atomic_flush_pending_con (con,prb_next_reserve_seq (prb), false); }console_srcu_read_unlock (cookie); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 23/30 printk: Avoid console_lock dance if no legacy or boot consoles
In reply to John OgnessCurrently the console lock is used to attempt legacy-type printing even if there are no legacy or boot consoles registered. If no such consoles are registered, the console lock does not need to be taken.
Add tracking of legacy console registration and use it with boot console tracking to avoid unnecessary code paths, i.e. do not use the console lock if there are no boot consoles and no legacy consoles.
--- kernel/printk/printk.c | 78 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 15 deletions(-)
kernel/printk/printk.c +63 -15
@@ -463,6 +463,13 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;/* syslog_lock protects syslog_* variables and write access to clear_seq. */ static DEFINE_MUTEX (syslog_lock); +/* +* Specifies if a legacy console is registered. If legacy consoles are +* present, it is necessary to perform the console lock/unlock dance +* whenever console flushing should occur. +*/ +static bool have_legacy_console; +/* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by
@@ -471,6 +478,14 @@ static DEFINE_MUTEX(syslog_lock);*/ static bool have_boot_console; +/* +* Specifies if the console lock/unlock dance is needed for console +* printing. If @have_boot_console is true, the nbcon consoles will +* be printed serially along with the legacy consoles because nbcon +* consoles cannot print simultaneously with boot consoles. +*/ +#define printing_via_unlock (have_legacy_console || have_boot_console) +#ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD (log_wait);/* All 3 protected by @syslog_lock. */
@@ -2339,7 +2354,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len =vprintk_store (facility, level, dev_info, fmt, args);/* If called from the scheduler, we can not call up(). */ -if (!in_sched) { +if (!in_sched && printing_via_unlock) {/* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during
@@ -2648,7 +2663,7 @@ void resume_console(void)*/ static int console_cpu_notify (unsigned int cpu) { -if (!cpuhp_tasks_frozen) { +if (!cpuhp_tasks_frozen && printing_via_unlock) {/* If trylock fails, someone else is doing the printing */ if (console_trylock ())console_unlock ();
@@ -3189,7 +3204,8 @@ void console_flush_on_panic(enum con_flush_mode mode)nbcon_atomic_flush_pending (); -console_flush_all (false, &next_seq, &handover); +if (printing_via_unlock) +console_flush_all (false, &next_seq, &handover); }/*
@@ -3524,6 +3540,7 @@ void register_console(struct console *newcon)if (newcon->flags &CON_NBCON ) {nbcon_init (newcon, init_seq); }else { + have_legacy_console = true; newcon->seq = init_seq; }
@@ -3600,6 +3617,7 @@ EXPORT_SYMBOL(register_console);static int unregister_console_locked (struct console *console) {bool use_device_lock = (console->flags &CON_NBCON ) && console->write_atomic; +bool found_legacy_con = false;bool found_boot_con = false;unsigned long flags;struct console *c;
@@ -3667,9 +3685,13 @@ static int unregister_console_locked(struct console *console)for_each_console (c) {if (c->flags &CON_BOOT ) found_boot_con = true; +if (!(c->flags &CON_NBCON )) + found_legacy_con = true; }if (!found_boot_con) have_boot_console = found_boot_con; +if (!found_legacy_con) + have_legacy_console = found_legacy_con;return res; }
@@ -3830,22 +3852,34 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre seq =prb_next_reserve_seq (prb);/* Flush the consoles so that records up to @seq are printed. */ -console_lock (); -console_unlock (); +if (printing_via_unlock) { +console_lock (); +console_unlock (); + }for (;;) {unsigned long begin_jiffies;unsigned long slept_jiffies; - - diff =0 ; +bool use_console_lock = printing_via_unlock;/* -* Hold the console_lock to guarantee safe access to -* console->seq. Releasing console_lock flushes more -* records in case @seq is still not printed on all -* usable consoles. +* Ensure the compiler does not optimize @use_console_lock to +* be @printing_via_unlock since the latter can change at any +* time. */ -console_lock (); +barrier (); + + diff =0 ; + +if (use_console_lock) { +/* +* Hold the console_lock to guarantee safe access to +* console->seq. Releasing console_lock flushes more +* records in case @seq is still not printed on all +* usable consoles. +*/ +console_lock (); + } cookie =console_srcu_read_lock ();for_each_console_srcu (c) {
@@ -3865,6 +3899,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progreif (flags &CON_NBCON ) { printk_seq =nbcon_seq_read (c); }else { +WARN_ON_ONCE (!use_console_lock); printk_seq = c->seq; }
@@ -3876,7 +3911,8 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progreif (diff != last_diff && reset_on_progress) remaining_jiffies = timeout_jiffies; -console_unlock (); +if (use_console_lock) +console_unlock ();/* Note: @diff is 0 if there are no usable consoles. */ if (diff ==0 || remaining_jiffies ==0 )
@@ -3946,6 +3982,7 @@ static void __wake_up_klogd(int val)return ;preempt_disable (); +/* * Guarantee any new records can be seen by tasks preparing to wait * before this context checks if the wait queue is empty.
@@ -3957,11 +3994,22 @@ static void __wake_up_klogd(int val)* * This pairs with devkmsg_read:A and syslog_print:A. */ -if (wq_has_sleeper (&log_wait) ||/* LMM(__wake_up_klogd:A) */ - (val &PRINTK_PENDING_OUTPUT )) { +if (!wq_has_sleeper (&log_wait))/* LMM(__wake_up_klogd:A) */ + val &= ~PRINTK_PENDING_WAKEUP ; + +/* +* Simple read is safe. register_console() would flush a newly +* registered legacy console when writing the message about it +* being enabled. +*/ +if (!printing_via_unlock) + val &= ~PRINTK_PENDING_OUTPUT ; + +if (val) {this_cpu_or (printk_pending, val);irq_work_queue (this_cpu_ptr (&wake_up_klogd_work)); } +preempt_enable (); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 24/30 printk: Track nbcon consoles
In reply to John OgnessAdd a global flag @have_nbcon_console to identify if any nbcon consoles are registered. This will be used in follow-up commits to preserve legacy behavior when no nbcon consoles are registered.
--- kernel/printk/printk.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/printk/printk.c +13 -1
@@ -470,6 +470,11 @@ static DEFINE_MUTEX(syslog_lock);*/ static bool have_legacy_console; +/* +* Specifies if an nbcon console is registered. +*/ +static bool have_nbcon_console; +/* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by
@@ -3538,6 +3543,7 @@ void register_console(struct console *newcon) init_seq =get_init_console_seq (newcon, bootcon_registered);if (newcon->flags &CON_NBCON ) { + have_nbcon_console = true;nbcon_init (newcon, init_seq); }else { have_legacy_console = true;
@@ -3618,6 +3624,7 @@ static int unregister_console_locked(struct console *console) {bool use_device_lock = (console->flags &CON_NBCON ) && console->write_atomic;bool found_legacy_con = false; +bool found_nbcon_con = false;bool found_boot_con = false;unsigned long flags;struct console *c;
@@ -3685,13 +3692,18 @@ static int unregister_console_locked(struct console *console)for_each_console (c) {if (c->flags &CON_BOOT ) found_boot_con = true; -if (!(c->flags &CON_NBCON )) + +if (c->flags &CON_NBCON ) + found_nbcon_con = true; +else found_legacy_con = true; }if (!found_boot_con) have_boot_console = found_boot_con;if (!found_legacy_con) have_legacy_console = found_legacy_con; +if (!found_nbcon_con) + have_nbcon_console = found_nbcon_con;return res; }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 25/30 printk: Coordinate direct printing in panic
In reply to John OgnessPerform printing by nbcon consoles on the panic CPU from the printk() caller context in order to get panic messages printed as soon as possible.
If legacy and nbcon consoles are registered, the legacy consoles will no longer perform direct printing on the panic CPU until after the backtrace has been stored. This will give the safe nbcon consoles a chance to print the panic messages before allowing the unsafe legacy consoles to print.
If no nbcon consoles are registered, there is no change in behavior (i.e. legacy consoles will always attempt to print from the printk() caller context).
--- include/linux/printk.h | 5 ++++ kernel/panic.c | 2 ++ kernel/printk/printk.c | 62 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 61 insertions(+), 8 deletions(-)
include/linux/printk.h +5 -0
@@ -195,6 +195,7 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold;void printk_trigger_flush (void ); +void printk_legacy_allow_panic_sync (void );extern bool nbcon_device_try_acquire (struct console *con);extern void nbcon_device_release (struct console *con);void nbcon_atomic_flush_unsafe (void );
@@ -278,6 +279,10 @@ static inline void printk_trigger_flush(void) { } +static inline void printk_legacy_allow_panic_sync (void ) +{ +} +static inline bool nbcon_device_try_acquire (struct console *con) {return false;
kernel/panic.c +2 -0
@@ -364,6 +364,8 @@ void panic(const char *fmt, ...)panic_other_cpus_shutdown (_crash_kexec_post_notifiers); +printk_legacy_allow_panic_sync (); +/* * Run any panic handlers, including those that might need to * add information to the kmsg dump output.
kernel/printk/printk.c +54 -8
98 lines not on this page · raw message
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 26/30 printk: nbcon: Implement emergency sections
In reply to John OgnessFrom: Thomas Gleixner <[email protected]>
In emergency situations (something has gone wrong but the system continues to operate), usually important information (such as a backtrace) is generated via printk(). Each individual printk record has little meaning. It is the collection of printk messages that is most often needed by developers and users.
In order to help ensure that the collection of printk messages in an emergency situation are all stored to the ringbuffer as quickly as possible, disable console output for that CPU while it is in the emergency situation. The consoles need to be flushed when exiting the emergency situation.
Add per-CPU emergency nesting tracking because an emergency can arise while in an emergency situation.
Add functions to mark the beginning and end of emergency sections where the urgent messages are generated.
Do not print if the current CPU is in an emergency state.
When exiting all emergency nesting, flush nbcon consoles directly using their atomic callback. Legacy consoles are flushed directly if safe, otherwise they are triggered for flushing via irq_work.
Note that the emergency state is not system-wide. While one CPU is in an emergency state, another CPU may continue to print console messages.
--- include/linux/console.h | 6 ++ kernel/printk/internal.h | 13 ++++ kernel/printk/nbcon.c | 126 ++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 25 +++---- kernel/printk/printk_safe.c | 11 +++- 5 files changed, 168 insertions(+), 13 deletions(-)
include/linux/console.h +6 -0
@@ -535,10 +535,16 @@ static inline bool console_is_registered(const struct console *con) hlist_for_each_entry(con, &console_list, node)#ifdef CONFIG_PRINTK +extern void nbcon_cpu_emergency_enter (void ); +extern void nbcon_cpu_emergency_exit (void ); +extern void nbcon_cpu_emergency_flush (void );extern bool nbcon_can_proceed (struct nbcon_write_context *wctxt);extern bool nbcon_enter_unsafe (struct nbcon_write_context *wctxt);extern bool nbcon_exit_unsafe (struct nbcon_write_context *wctxt);#else +static inline void nbcon_cpu_emergency_enter (void ) { } +static inline void nbcon_cpu_emergency_exit (void ) { } +static inline void nbcon_cpu_emergency_flush (void ) { }static inline bool nbcon_can_proceed (struct nbcon_write_context *wctxt) {return false; }static inline bool nbcon_enter_unsafe (struct nbcon_write_context *wctxt) {return false; }static inline bool nbcon_exit_unsafe (struct nbcon_write_context *wctxt) {return false; }
kernel/printk/internal.h +13 -0
@@ -76,6 +76,8 @@ bool printk_percpu_data_ready(void);void defer_console_output (void ); +bool is_printk_deferred (void ); +u16 printk_parse_prefix (const char *text,int *level,enum printk_info_flags *flags);void console_lock_spinning_enable (void );
@@ -153,6 +155,17 @@ static inline bool console_is_usable(struct console *con, short flags) { return#endif /* CONFIG_PRINTK */ +extern bool have_boot_console; +extern bool have_legacy_console; + +/* +* Specifies if the console lock/unlock dance is needed for console +* printing. If @have_boot_console is true, the nbcon consoles will +* be printed serially along with the legacy consoles because nbcon +* consoles cannot print simultaneously with boot consoles. +*/ +#define printing_via_unlock (have_legacy_console || have_boot_console) +extern struct printk_buffers printk_shared_pbufs;/**
kernel/printk/nbcon.c +126 -0
147 lines not on this page · raw message
kernel/printk/printk.c +13 -12
@@ -468,7 +468,7 @@ static DEFINE_MUTEX(syslog_lock);* present, it is necessary to perform the console lock/unlock dance * whenever console flushing should occur. */ -static bool have_legacy_console; +bool have_legacy_console;/* * Specifies if an nbcon console is registered. If nbcon consoles are present,
@@ -483,15 +483,7 @@ static bool have_nbcon_console;* the console lock. This is because boot consoles and nbcon consoles may * have mapped the same hardware. */ -static bool have_boot_console; - -/* -* Specifies if the console lock/unlock dance is needed for console -* printing. If @have_boot_console is true, the nbcon consoles will -* be printed serially along with the legacy consoles because nbcon -* consoles cannot print simultaneously with boot consoles. -*/ -#define printing_via_unlock (have_legacy_console || have_boot_console) +bool have_boot_console;#ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD (log_wait);
@@ -2412,16 +2404,25 @@ asmlinkage int vprintk_emit(int facility, int level,* printing of all remaining records to all consoles so that * this context can return as soon as possible. Hopefully * another printk() caller will take over the printing. +* +* Also, nbcon_get_default_prio() requires migration disabled. */ preempt_disable (); +/* * Try to acquire and then immediately release the console * semaphore. The release will print out buffers. With the * spinning variant, this context tries to take over the * printing from another printing context. +* +* Skip it in EMERGENCY priority. The console will be +* explicitly flushed when exiting the emergency section. */ -if (console_trylock_spinning ()) -console_unlock (); +if (nbcon_get_default_prio () !=NBCON_PRIO_EMERGENCY ) { +if (console_trylock_spinning ()) +console_unlock (); + } +preempt_enable (); }
kernel/printk/printk_safe.c +10 -1
@@ -38,6 +38,15 @@ void __printk_deferred_exit(void)__printk_safe_exit (); } +bool is_printk_deferred (void ) +{ +/* +* The per-CPU variable @printk_context can be read safely in any +* context. The CPU migration always disabled when set. +*/ +return (this_cpu_read (printk_context) ||in_nmi ()); +} + asmlinkage int vprintk(const char *fmt, va_list args) {#ifdef CONFIG_KGDB_KDB
@@ -50,7 +59,7 @@ asmlinkage int vprintk(const char *fmt, va_list args)* Use the main logbuf even in NMI. But avoid calling console * drivers that might have their own locks. */ -if (this_cpu_read (printk_context) ||in_nmi ()) +if (is_printk_deferred ())return vprintk_deferred (fmt, args);/* No obstacles. */
Signature · 1 line
2.39.2
Re: [PATCH printk v6 26/30] printk: nbcon: Implement emergency sections
Quoted 28 lines · John Ogness
From: Thomas Gleixner <[email protected]>
In emergency situations (something has gone wrong but the system continues to operate), usually important information (such as a backtrace) is generated via
printk(). Each individual printk record has little meaning. It is the collection of printk messages that is most often needed by developers and users.In order to help ensure that the collection of printk messages in an emergency situation are all stored to the ringbuffer as quickly as possible, disable console output for that CPU while it is in the emergency situation. The consoles need to be flushed when exiting the emergency situation.
Add per-CPU emergency nesting tracking because an emergency can arise while in an emergency situation.
Add functions to mark the beginning and end of emergency sections where the urgent messages are generated.
Do not print if the current CPU is in an emergency state.
When exiting all emergency nesting, flush nbcon consoles directly using their atomic callback. Legacy consoles are flushed directly if safe, otherwise they are triggered for flushing via
irq_work.
Quoted 7 lines
Note that the emergency state is not system-wide. While one CPU is in an emergency state, another CPU may continue to print console messages.
Co-developed-by John Ogness <[email protected]>Signed-off-by John Ogness <[email protected]>Signed-off-by Thomas Gleixner (Intel) <[email protected]>
Best Regards,
Petr
John Ogness
maintainerPATCH v6 27/30 panic: Mark emergency section in warn
In reply to John OgnessFrom: Thomas Gleixner <[email protected]>
Mark the full contents of __warn() as an emergency section. In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section.
--- kernel/panic.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/panic.c +4 -0
@@ -667,6 +667,8 @@ struct warn_args {void __warn (const char *file,int line,void *caller,unsigned taint,struct pt_regs *regs,struct warn_args *args) { +nbcon_cpu_emergency_enter (); +disable_trace_on_warning ();if (file)
@@ -697,6 +699,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint,/* Just a warning, don't kill lockdep. */ add_taint (taint,LOCKDEP_STILL_OK ); + +nbcon_cpu_emergency_exit (); }#ifdef CONFIG_BUG
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 28/30 panic: Mark emergency section in oops
In reply to John OgnessMark an emergency section beginning with oops_enter() until the end of oops_exit(). In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section.
The very end of oops_exit() performs a kmsg_dump(). This is not included in the emergency section because it is another flushing mechanism that should occur after the consoles have been triggered to flush.
--- kernel/panic.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/panic.c +2 -0
@@ -634,6 +634,7 @@ bool oops_may_print(void)*/ void oops_enter (void ) { +nbcon_cpu_emergency_enter ();tracing_off ();/* can't trust the integrity of the kernel anymore: */ debug_locks_off ();
@@ -656,6 +657,7 @@ void oops_exit(void) {do_oops_enter_exit ();print_oops_end_marker (); +nbcon_cpu_emergency_exit ();kmsg_dump (KMSG_DUMP_OOPS ); }
Signature · 1 line
2.39.2
John Ogness
maintainerPATCH v6 29/30 rcu: Mark emergency sections in rcu stalls
In reply to John OgnessMark emergency sections wherever multiple lines of rcu stall information are generated. In an emergency section the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. This allows the full message block to be stored as quickly as possible in the ringbuffer.
--- kernel/rcu/tree_exp.h | 9 +++++++++ kernel/rcu/tree_stall.h | 11 +++++++++++ 2 files changed, 20 insertions(+)
kernel/rcu/tree_exp.h +9 -0
@@ -7,6 +7,7 @@* Authors: Paul E. McKenney <[email protected]> */ +#include <linux/console.h> #include <linux/lockdep.h> static void rcu_exp_handler (void *unused);
@@ -571,6 +572,9 @@ static void synchronize_rcu_expedited_wait(void)return ;if (rcu_stall_is_suppressed ())continue ; + +nbcon_cpu_emergency_enter (); + j = jiffies;rcu_stall_notifier_call_chain (RCU_STALL_NOTIFY_EXP , (void *)(j - jiffies_start));trace_rcu_stall_warning (rcu_state.name,TPS ("ExpeditedStall" ));
@@ -620,10 +624,14 @@ static void synchronize_rcu_expedited_wait(void)preempt_disable ();// For smp_processor_id() in dump_cpu_task(). dump_cpu_task (cpu);preempt_enable (); +nbcon_cpu_emergency_flush (); }rcu_exp_print_detail_task_stall_rnp (rnp); } jiffies_stall =3 *rcu_exp_jiffies_till_stall_check () +3 ; + +nbcon_cpu_emergency_exit (); +panic_on_rcu_stall (); } }
@@ -792,6 +800,7 @@ static void rcu_exp_print_detail_task_stall_rnp(struct rcu_node *rnp)*/ touch_nmi_watchdog ();sched_show_task (t); +nbcon_cpu_emergency_flush (); }raw_spin_unlock_irqrestore_rcu_node (rnp, flags); }
kernel/rcu/tree_stall.h +11 -0
@@ -7,6 +7,7 @@* Author: Paul E. McKenney <[email protected]> */ +#include <linux/console.h> #include <linux/kvm_para.h> #include <linux/rcu_notifier.h>
@@ -260,6 +261,7 @@ static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)*/ touch_nmi_watchdog ();sched_show_task (t); +nbcon_cpu_emergency_flush (); }raw_spin_unlock_irqrestore_rcu_node (rnp, flags); }
@@ -522,6 +524,7 @@ static void print_cpu_stall_info(int cpu) falsepositive ? " (false positive?)" : "");print_cpu_stat_info (cpu); +nbcon_cpu_emergency_flush (); }/* Complain about starvation of grace-period kthread. */
@@ -604,6 +607,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)if (rcu_stall_is_suppressed ())return ; +nbcon_cpu_emergency_enter (); +/* * OK, time to rat on our buddy... * See Documentation/RCU/stallwarn.rst for info on how to debug
@@ -655,6 +660,8 @@ static void print_other_cpu_stall(unsigned long gp_seq, unsigned long gps)rcu_check_gp_kthread_expired_fqs_timer ();rcu_check_gp_kthread_starvation (); +nbcon_cpu_emergency_exit (); +panic_on_rcu_stall ();rcu_force_quiescent_state ();/* Kick them all. */
@@ -675,6 +682,8 @@ static void print_cpu_stall(unsigned long gps)if (rcu_stall_is_suppressed ())return ; +nbcon_cpu_emergency_enter (); +/* * OK, time to rat on ourselves... * See Documentation/RCU/stallwarn.rst for info on how to debug
@@ -703,6 +712,8 @@ static void print_cpu_stall(unsigned long gps) jiffies + 3 * rcu_jiffies_till_stall_check() + 3);raw_spin_unlock_irqrestore_rcu_node (rnp, flags); +nbcon_cpu_emergency_exit (); +panic_on_rcu_stall ();/*
Signature · 1 line
2.39.2
Re: [PATCH printk v6 29/30] rcu: Mark emergency sections in rcu stalls
Quoted 9 lines · John Ogness
Mark emergency sections wherever multiple lines of rcu stall information are generated. In an emergency section the CPU will not perform console output for the
printk()calls. Instead, a flushing of the console output is triggered when exiting the emergency section. This allows the full message block to be stored as quickly as possible in the ringbuffer.Signed-off-by John Ogness <[email protected]>
Best Regards,
Petr
John Ogness
maintainerPATCH v6 30/30 lockdep: Mark emergency sections in lockdep splats
In reply to John OgnessMark emergency sections wherever multiple lines of lock debugging output are generated. In an emergency section the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. This allows the full message block to be stored as quickly as possible in the ringbuffer.
Note that debug_show_all_locks() and lockdep_print_held_locks() rely on their callers to enter the emergency section. This is because these functions can also be called in non-emergency situations (such as sysrq).
--- kernel/locking/lockdep.c | 84 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-)
kernel/locking/lockdep.c +82 -2
@@ -56,6 +56,7 @@#include <linux/kprobes.h> #include <linux/lockdep.h> #include <linux/context_tracking.h> +#include <linux/console.h> #include <asm/sections.h>
@@ -574,8 +575,10 @@ static struct lock_trace *save_trace(void)if (!debug_locks_off_graph_unlock ())return NULL ; +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_STACK_TRACE_ENTRIES too low!" );dump_stack (); +nbcon_cpu_emergency_exit ();return NULL ; }
@@ -888,11 +891,13 @@ look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)if (unlikely (subclass >=MAX_LOCKDEP_SUBCLASSES )) {instrumentation_begin ();debug_locks_off (); +nbcon_cpu_emergency_enter ();printk (KERN_ERR "BUG: looking up invalid subclass: %u\n" , subclass);printk (KERN_ERR "turning off the locking correctness validator.\n" );dump_stack (); +nbcon_cpu_emergency_exit ();instrumentation_end ();return NULL ; }
@@ -969,11 +974,13 @@ static bool assign_lock_key(struct lockdep_map *lock)else {/* Debug-check: all keys must be persistent! */ debug_locks_off (); +nbcon_cpu_emergency_enter ();pr_err ("INFO: trying to register non-static key.\n" );pr_err ("The code is fine but needs lockdep annotation, or maybe\n" );pr_err ("you didn't initialize this object before use?\n" );pr_err ("turning off the locking correctness validator.\n" );dump_stack (); +nbcon_cpu_emergency_exit ();return false; }
@@ -1317,8 +1324,10 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)return NULL ; } +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_LOCKDEP_KEYS too low!" );dump_stack (); +nbcon_cpu_emergency_exit ();return NULL ; } nr_lock_classes++;
@@ -1350,11 +1359,13 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)if (verbose (class)) {graph_unlock (); +nbcon_cpu_emergency_enter ();printk ("\nnew class %px: %s" , class->key, class->name);if (class->name_version >1 )printk (KERN_CONT "#%d" , class->name_version);printk (KERN_CONT "\n" );dump_stack (); +nbcon_cpu_emergency_exit ();if (!graph_lock ()) {return NULL ;
@@ -1393,8 +1404,10 @@ static struct lock_list *alloc_list_entry(void)if (!debug_locks_off_graph_unlock ())return NULL ; +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_LOCKDEP_ENTRIES too low!" );dump_stack (); +nbcon_cpu_emergency_exit ();return NULL ; } nr_list_entries++;
@@ -2040,6 +2053,8 @@ static noinline void print_circular_bug(struct lock_list *this, depth =get_lock_depth (target); +nbcon_cpu_emergency_enter (); +print_circular_bug_header (target, depth, check_src, check_tgt); parent =get_lock_parent (target);
@@ -2058,6 +2073,8 @@ static noinline void print_circular_bug(struct lock_list *this,printk ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); } static noinline void print_bfs_bug(int ret)
@@ -2570,6 +2587,8 @@ print_bad_irq_dependency(struct task_struct *curr,if (!debug_locks_off_graph_unlock () || debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("=====================================================\n" ); pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
@@ -2619,11 +2638,13 @@ print_bad_irq_dependency(struct task_struct *curr,pr_warn (" and %s-irq-unsafe lock:\n" , irqclass); next_root->trace =save_trace ();if (!next_root->trace) -return ; + goto out;print_shortest_lock_dependencies (forwards_entry, next_root);pr_warn ("\nstack backtrace:\n" );dump_stack (); +out: +nbcon_cpu_emergency_exit (); }static const char *state_names[] = {
@@ -2988,6 +3009,8 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,if (!debug_locks_off_graph_unlock () || debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("============================================\n" );pr_warn ("WARNING: possible recursive locking detected\n" );
@@ -3010,6 +3033,8 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); }/*
@@ -3607,6 +3632,8 @@ static void print_collision(struct task_struct *curr,struct held_lock *hlock_next, struct lock_chain *chain) { +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("============================\n" );pr_warn ("WARNING: chain_key collision\n" );
@@ -3623,6 +3650,8 @@ static void print_collision(struct task_struct *curr,pr_warn ("\nstack backtrace:\n" );dump_stack (); + + nbcon_cpu_emergency_exit(); }#endif
@@ -3713,8 +3742,10 @@ static inline int add_chain_cache(struct task_struct *curr,if (!debug_locks_off_graph_unlock ())return 0 ; +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_LOCKDEP_CHAINS too low!" );dump_stack (); +nbcon_cpu_emergency_exit ();return 0 ; } chain->chain_key = chain_key;
@@ -3731,8 +3762,10 @@ static inline int add_chain_cache(struct task_struct *curr,if (!debug_locks_off_graph_unlock ())return 0 ; +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!" );dump_stack (); +nbcon_cpu_emergency_exit ();return 0 ; }
@@ -3971,6 +4004,8 @@ print_usage_bug(struct task_struct *curr, struct held_lock *this,if (!debug_locks_off () || debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("================================\n" );pr_warn ("WARNING: inconsistent lock state\n" );
@@ -3999,6 +4034,8 @@ print_usage_bug(struct task_struct *curr, struct held_lock *this,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); }/*
@@ -4033,6 +4070,8 @@ print_irq_inversion_bug(struct task_struct *curr,if (!debug_locks_off_graph_unlock () || debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("========================================================\n" );pr_warn ("WARNING: possible irq lock inversion dependency detected\n" );
@@ -4073,11 +4112,13 @@ print_irq_inversion_bug(struct task_struct *curr,pr_warn ("\nthe shortest dependencies between 2nd lock and 1st lock:\n" ); root->trace =save_trace ();if (!root->trace) -return ; + goto out;print_shortest_lock_dependencies (other, root);pr_warn ("\nstack backtrace:\n" );dump_stack (); +out: +nbcon_cpu_emergency_exit (); }/*
@@ -4154,6 +4195,8 @@ void print_irqtrace_events(struct task_struct *curr) {const struct irqtrace_events *trace = &curr->irqtrace; +nbcon_cpu_emergency_enter (); +printk ("irq event stamp: %u\n" , trace->irq_events); printk("hardirqs last enabled at (%u): [<%px>] %pS\n", trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
@@ -4167,6 +4210,8 @@ void print_irqtrace_events(struct task_struct *curr)printk ("softirqs last disabled at (%u): [<%px>] %pS\n" , trace->softirq_disable_event, (void *)trace->softirq_disable_ip, (void *)trace->softirq_disable_ip); + +nbcon_cpu_emergency_exit (); }static int HARDIRQ_verbose (struct lock_class *class)
@@ -4687,10 +4732,12 @@ static int mark_lock(struct task_struct *curr, struct held_lock *this,* We must printk outside of the graph_lock: */ if (ret ==2 ) { +nbcon_cpu_emergency_enter ();printk ("\nmarked lock as {%s}:\n" , usage_str[new_bit]);print_lock (this);print_irqtrace_events (curr);dump_stack (); +nbcon_cpu_emergency_exit (); }return ret;
@@ -4731,6 +4778,8 @@ print_lock_invalid_wait_context(struct task_struct *curr,if (debug_locks_silent)return 0 ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("=============================\n" );pr_warn ("[ BUG: Invalid wait context ]\n" );
@@ -4750,6 +4799,8 @@ print_lock_invalid_wait_context(struct task_struct *curr,pr_warn ("stack backtrace:\n" );dump_stack (); +nbcon_cpu_emergency_exit (); +return 0 ; }
@@ -4954,6 +5005,8 @@ print_lock_nested_lock_not_held(struct task_struct *curr,if (debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("==================================\n" );pr_warn ("WARNING: Nested lock was not taken\n" );
@@ -4974,6 +5027,8 @@ print_lock_nested_lock_not_held(struct task_struct *curr,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); }static int __lock_is_held (const struct lockdep_map *lock,int read);
@@ -5019,11 +5074,13 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,debug_class_ops_inc (class);if (very_verbose (class)) { +nbcon_cpu_emergency_enter ();printk ("\nacquire class [%px] %s" , class->key, class->name);if (class->name_version >1 )printk (KERN_CONT "#%d" , class->name_version);printk (KERN_CONT "\n" );dump_stack (); +nbcon_cpu_emergency_exit (); }/*
@@ -5150,6 +5207,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,#endif if (unlikely (curr->lockdep_depth >=MAX_LOCK_DEPTH )) {debug_locks_off (); +nbcon_cpu_emergency_enter ();print_lockdep_off ("BUG: MAX_LOCK_DEPTH too low!" );printk (KERN_DEBUG "depth: %i max: %lu!\n" , curr->lockdep_depth,MAX_LOCK_DEPTH );
@@ -5157,6 +5215,7 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,lockdep_print_held_locks (current);debug_show_all_locks ();dump_stack (); +nbcon_cpu_emergency_exit ();return 0 ; }
@@ -5176,6 +5235,8 @@ static void print_unlock_imbalance_bug(struct task_struct *curr,if (debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("=====================================\n" );pr_warn ("WARNING: bad unlock balance detected!\n" );
@@ -5192,6 +5253,8 @@ static void print_unlock_imbalance_bug(struct task_struct *curr,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); } static noinstr int match_held_lock(const struct held_lock *hlock,
@@ -5895,6 +5958,8 @@ static void print_lock_contention_bug(struct task_struct *curr,if (debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("=================================\n" );pr_warn ("WARNING: bad contention detected!\n" );
@@ -5911,6 +5976,8 @@ static void print_lock_contention_bug(struct task_struct *curr,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); } static void
@@ -6524,6 +6591,8 @@ print_freed_lock_bug(struct task_struct *curr, const void *mem_from,if (debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("=========================\n" );pr_warn ("WARNING: held lock freed!\n" );
@@ -6536,6 +6605,8 @@ print_freed_lock_bug(struct task_struct *curr, const void *mem_from,pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); } static inline int not_in_range(const void* mem_from, unsigned long mem_len,
@@ -6582,6 +6653,8 @@ static void print_held_locks_bug(void)if (debug_locks_silent)return ; +nbcon_cpu_emergency_enter (); +pr_warn ("\n" );pr_warn ("====================================\n" ); pr_warn("WARNING: %s/%d still has locks held!\n",
@@ -6591,6 +6664,8 @@ static void print_held_locks_bug(void)lockdep_print_held_locks (current);pr_warn ("\nstack backtrace:\n" );dump_stack (); + +nbcon_cpu_emergency_exit (); }void debug_check_no_locks_held (void )
@@ -6616,6 +6691,7 @@ void debug_show_all_locks(void)if (!p->lockdep_depth)continue ;lockdep_print_held_locks (p); +nbcon_cpu_emergency_flush ();touch_nmi_watchdog ();touch_all_softlockup_watchdogs (); }
@@ -6648,6 +6724,7 @@ asmlinkage __visible void lockdep_sys_exit(void)if (unlikely (curr->lockdep_depth)) {if (!debug_locks_off ())return ; +nbcon_cpu_emergency_enter ();pr_warn ("\n" );pr_warn ("================================================\n" );pr_warn ("WARNING: lock held when returning to user space!\n" );
@@ -6656,6 +6733,7 @@ asmlinkage __visible void lockdep_sys_exit(void)pr_warn ("%s/%d is leaving the kernel with locks still held!\n" , curr->comm, curr->pid);lockdep_print_held_locks (curr); +nbcon_cpu_emergency_exit (); }/*
@@ -6672,6 +6750,7 @@ void lockdep_rcu_suspicious(const char *file, const int line, const char *s)bool rcu =warn_rcu_enter ();/* Note: the following can be executed concurrently, so be careful. */ +nbcon_cpu_emergency_enter ();pr_warn ("\n" );pr_warn ("=============================\n" );pr_warn ("WARNING: suspicious RCU usage\n" );
@@ -6710,6 +6789,7 @@ void lockdep_rcu_suspicious(const char *file, const int line, const char *s)lockdep_print_held_locks (curr);pr_warn ("\nstack backtrace:\n" );dump_stack (); +nbcon_cpu_emergency_exit ();warn_rcu_exit (rcu); }EXPORT_SYMBOL_GPL (lockdep_rcu_suspicious);
Signature · 1 line
2.39.2
Petr Mladek
maintainerQuoted 33 lines · John Ogness
Hi,
This is v6 of a series to wire up the nbcon consoles so that they actually perform printing using their
write_atomic()callback. v5 is here [0]. For information about the motivation of the atomic consoles, please read the cover letter of v1 [1].The main focus of this series:
- For nbcon consoles, always call
write_atomic()directly fromprintk()caller context for the panic CPU.- For nbcon consoles, call
write_atomic()when unlocking the console lock.- Only perform the console lock/unlock dance if legacy or boot consoles are registered.
- For legacy consoles, if nbcon consoles are registered, do not attempt to print from
printk()caller context for the panic CPU until nbcon consoles have had a chance to print the most significant messages.- Mark emergency sections. In these sections
printk()calls will only store the messages. Upon exiting the emergency section, nbcon consoles are flushed directly. If legacy consoles cannot be flushed safely, anirq_workis triggered to do the legacy console flushing.This series does _not_ include threaded printing or nbcon drivers. Those features will be added in separate follow-up series.
Quoted 5 lines
Note: With this series, a system with _only_ nbcon consoles
registered will not perform console printing unless the console lock or nbcon port lock are used or on panic. This is on purpose. When nbcon kthreads are introduced, they will fill the gaps.
The series seems to be ready for linux-next from my POV.
I am going to push it there so that we get as much testing as possible before the next merge window.
Best Regards,
Petr
Petr Mladek
maintainerQuoted 40 lines · Petr Mladek
Quoted 33 lines · John Ogness
Hi,
This is v6 of a series to wire up the nbcon consoles so that they actually perform printing using their
write_atomic()callback. v5 is here [0]. For information about the motivation of the atomic consoles, please read the cover letter of v1 [1].The main focus of this series:
- For nbcon consoles, always call
write_atomic()directly fromprintk()caller context for the panic CPU.- For nbcon consoles, call
write_atomic()when unlocking the console lock.- Only perform the console lock/unlock dance if legacy or boot consoles are registered.
- For legacy consoles, if nbcon consoles are registered, do not attempt to print from
printk()caller context for the panic CPU until nbcon consoles have had a chance to print the most significant messages.- Mark emergency sections. In these sections
printk()calls will only store the messages. Upon exiting the emergency section, nbcon consoles are flushed directly. If legacy consoles cannot be flushed safely, anirq_workis triggered to do the legacy console flushing.This series does _not_ include threaded printing or nbcon drivers. Those features will be added in separate follow-up series.
Quoted 5 lines
Note: With this series, a system with _only_ nbcon consoles
registered will not perform console printing unless the console lock or nbcon port lock are used or on panic. This is on purpose. When nbcon kthreads are introduced, they will fill the gaps.
Quoted 4 lines
The series seems to be ready for linux-next from my POV.
I am going to push it there so that we get as much testing as possible before the next merge window.
JFYI, the patchset has been committed into printk/linux.git, branch rework/write-atomic.
Best Regards,
Petr