[PATCH printk v2 00/12] implement threaded console printing
From: John Ogness
Date: Tue Apr 05 2022 - 21:10:27 EST
This is v2 of a series to implement a kthread for each registered
console. v1 is here . The kthreads locklessly retrieve the
records from the printk ringbuffer and also do not cause any lock
contention between each other. This allows consoles to run at full
speed. For example, a netconsole is able to dump records much
faster than a serial or vt console. Also, during normal operation,
it completely decouples printk() callers from console printing.
There are situations where kthread printing is not sufficient. For
example, during panic situations, where the kthreads may not get a
chance to schedule. In such cases, the current method of attempting
to print directly within the printk() caller context is used. New
functions printk_prefer_direct_enter() and
printk_prefer_direct_exit() are made available to mark areas of the
kernel where direct printing is preferred. (These should only be
areas that do not occur during normal operation.)
This series also introduces pr_flush(): a might_sleep() function
that will block until all active printing threads have caught up
to the latest record at the time of the pr_flush() call. This
function is useful, for example, to wait until pending records
are flushed to consoles before suspending.
Note that this series does *not* increase the reliability of console
printing. Rather it focuses on the non-interference aspect of
printk() by decoupling printk() callers from printing (during normal
operation). Nonetheless, the reliability aspect should not worsen
due to this series.
Changes since v1:
- Rename nearly all new variables, functions, macros, and flags:
@console_lock_count --> @console_kthreads_active
@consoles_paused --> @console_kthreads_blocked
@kthreads_started --> @printk_kthreads_available
@printk_direct --> @printk_prefer_direct
console_excl_trylock() --> console_kthreads_atomic_tryblock()
console_excl_unlock() --> console_kthreads_atomic_unblock()
console_printer_tryenter() --> console_kthread_printing_tryenter()
console_printer_exit() --> console_kthread_printing_exit()
pause_all_consoles() --> console_kthreads_block()
unpause_all_consoles() --> console_kthreads_unblock()
printk_direct_enter() --> printk_prefer_direct_enter()
printk_direct_exit() --> printk_prefer_direct_exit()
start_printk_kthread() --> printk_start_kthread()
CON_PAUSED --> CON_THD_BLOCKED
- Drop the patch to avoid VT printing if in an atomic context. (This
can only be done after the Linux BSoD feature is implemented.)
- Keep printk_delay() in the printk() context. (We may want to move
this into printer contexts later.)
- Keep console_trylock() usage to reacquire the console lock in
- Keep cpu_online(raw_smp_processor_id()) in the check if a console
is usable and remove the printk_percpu_data_ready() check (percpu
data is always ready for consoles).
- Start console printer threads as an early_initcall. This means
threads are active before multiple CPUs come online.
- Remove "system_state < SYSTEM_RUNNING" as a reason to allow direct
printing. @kthreads_started is used for the early boot decision.
- Add console_kthreads_atomically_blocked() macro.
- Add printk_console_msg() macro to printk messages with console
details. The macro allows print indexing.
- Add __pr_flush() to allow waiting for only a single console.
- Add a full memory barrier in wake_up_klogd() to ensure new records
are visible before checking if the waitqueue is empty.
- Wake kthreads when exiting preferred direct mode.
- Wake kthreads when a CPU comes online.
- In unregister_console(), stop the printer thread before acquiring
the console lock.
- Print "thread started" message after all allocations successful.
- Permanently enable preferred direct printing if there are any
errors setting up any of the threaded console printers.
- Keep threaded printers asleep if there is no con->write()
- Keep threaded printers asleep if preferred direct printing is
- Always allow handovers if the console is locked.
- Only stop irqsoff tracing if handovers are allowed.
- console_emit_next_record() is for when the console is not locked,
console_emit_next_record_transferable() is for when the console is
- Wait for console printers to catch up after unblanking the screen.
- In console_stop(), only wait for the stopping console.
- In console_start(), only wait for the starting console.
- Print only 1 record per wait cycle to increase chances of direct
printing taking over.
- Repurpose the irqwork flag PRINTK_PENDING_OUTPUT for direct
- Add caution notice and tips to description of
printk_cpu_sync_get_irqsave() on how to use it correctly.
- Improve the comments explaining why preemption is disabled during
- Improve commit message to explain the motivation for expanding the
console lock and how it works.
John Ogness (12):
printk: rename cpulock functions
printk: cpu sync always disable interrupts
printk: get caller_id/timestamp after migration disable
printk: call boot_delay_msec() in printk_delay()
printk: add macro for console detail messages
printk: refactor and rework printing logic
printk: move buffer definitions into console_emit_next_record() caller
printk: add pr_flush()
printk: add functions to prefer direct printing
printk: add kthread console printers
printk: extend console_lock for proper kthread support
printk: remove @console_locked
drivers/tty/sysrq.c | 2 +
include/linux/console.h | 19 +
include/linux/printk.h | 82 ++-
kernel/hung_task.c | 11 +-
kernel/panic.c | 4 +
kernel/printk/printk.c | 1070 +++++++++++++++++++++++++++++----------
kernel/rcu/tree_stall.h | 2 +
kernel/reboot.c | 14 +-
kernel/watchdog.c | 4 +
kernel/watchdog_hld.c | 4 +
lib/dump_stack.c | 4 +-
lib/nmi_backtrace.c | 4 +-
12 files changed, 908 insertions(+), 312 deletions(-)