implement threaded console printing
Subsystems (6)
John Ogness
maintainerv2 cover implement threaded console printing
This is v2 of a series to implement a kthread for each registered console. v1 is here [0]. 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:
variables:
@console_lock_count --> @console_kthreads_active @consoles_paused --> @console_kthreads_blocked @kthreads_started --> @printk_kthreads_available @printk_direct --> @printk_prefer_direct
functions/macros:
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()
console flags:
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 console_unlock().
- 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() callback.
- Keep threaded printers asleep if preferred direct printing is active.
- 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 locked.
- 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 printing.
- 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 direct printing.
- Improve commit message to explain the motivation for expanding the console lock and how it works.
John Ogness
[0] https://lore.kernel.org/all/[email protected]
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(-)
base-commit: 3ef4ea3d84ca568dcd57816b9521e82e3bd94f08
Signature · 1 line
2.30.2
John Ogness
maintainerPATCH v2 1/12 printk: rename cpulock functions
Since the printk cpulock is CPU-reentrant and since it is used in all contexts, its usage must be carefully considered and most likely will require programming locklessly. To avoid mistaking the printk cpulock as a typical lock, rename it to cpu_sync. The main functions then become:
printk_cpu_sync_get_irqsave(flags);
printk_cpu_sync_put_irqrestore(flags);Add extra notes of caution in the function description to help developers understand the requirements for correct usage.
--- include/linux/printk.h | 54 +++++++++++++++++++------------- kernel/printk/printk.c | 71 +++++++++++++++++++++--------------------- lib/dump_stack.c | 4 +-- lib/nmi_backtrace.c | 4 +-- 4 files changed, 73 insertions(+), 60 deletions(-)
include/linux/printk.h +33 -21
@@ -277,43 +277,55 @@ static inline void printk_trigger_flush(void)#endif #ifdef CONFIG_SMP -extern int __printk_cpu_trylock (void ); -extern void __printk_wait_on_cpu_lock (void ); -extern void __printk_cpu_unlock (void ); +extern int __printk_cpu_sync_try_get (void ); +extern void __printk_cpu_sync_wait (void ); +extern void __printk_cpu_sync_put (void );/** -* printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning -* lock and disable interrupts. +* printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinning +* lock and disable interrupts. * @flags: Stack-allocated storage for saving local interrupt state, -* to be passed to printk_cpu_unlock_irqrestore(). +* to be passed to printk_cpu_sync_put_irqrestore(). * * If the lock is owned by another CPU, spin until it becomes available. * Interrupts are restored while spinning. +* +* CAUTION: This function must be used carefully. It does not behave like a +* typical lock. Here are important things to watch out for... +* +* * This function is reentrant on the same CPU. Therefore the calling +* code must not assume exclusive access to data if code accessing the +* data can run reentrant or within NMI context on the same CPU. +* +* * If there exists usage of this function from NMI context, it becomes +* unsafe to perform any type of locking or spinning to wait for other +* CPUs after calling this function from any context. This includes +* using spinlocks or any other busy-waiting synchronization methods. */ -#define printk_cpu_lock_irqsave (flags) \ - for (;;) { \ - local_irq_save(flags); \ - if (__printk_cpu_trylock()) \ - break; \ - local_irq_restore(flags); \ - __printk_wait_on_cpu_lock(); \ +#define printk_cpu_sync_get_irqsave (flags) \ + for (;;) { \ + local_irq_save(flags); \ + if (__printk_cpu_sync_try_get()) \ + break; \ + local_irq_restore(flags); \ + __printk_cpu_sync_wait(); \ }/** -* printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinning -* lock and restore interrupts. -* @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave(). +* printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spinning +* lock and restore interrupts. +* @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsave(). */ -#define printk_cpu_unlock_irqrestore (flags) \ +#define printk_cpu_sync_put_irqrestore (flags) \ do { \ - __printk_cpu_unlock(); \ + __printk_cpu_sync_put(); \ local_irq_restore(flags); \ - } while (0) \ + } while (0)#else -#define printk_cpu_lock_irqsave (flags) ((void)flags) -#define printk_cpu_unlock_irqrestore (flags) ((void)flags) +#define printk_cpu_sync_get_irqsave (flags) ((void)flags) +#define printk_cpu_sync_put_irqrestore (flags) ((void)flags)#endif /* CONFIG_SMP */
kernel/printk/printk.c +36 -35
148 lines not on this page · raw message
lib/dump_stack.c +2 -2
@@ -102,9 +102,9 @@ asmlinkage __visible void dump_stack_lvl(const char *log_lvl)* Permit this cpu to perform nested stack dumps while serialising * against other CPUs */ -printk_cpu_lock_irqsave (flags); +printk_cpu_sync_get_irqsave (flags);__dump_stack (log_lvl); -printk_cpu_unlock_irqrestore (flags); +printk_cpu_sync_put_irqrestore (flags); }EXPORT_SYMBOL (dump_stack_lvl);
lib/nmi_backtrace.c +2 -2
@@ -99,7 +99,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)* Allow nested NMI backtraces while serializing * against other CPUs. */ -printk_cpu_lock_irqsave (flags); +printk_cpu_sync_get_irqsave (flags);if (!READ_ONCE (backtrace_idle) && regs &&cpu_in_idle (instruction_pointer (regs))) {pr_warn ("NMI backtrace for cpu %d skipped: idling at %pS\n" , cpu, (void *)instruction_pointer (regs));
@@ -110,7 +110,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)else dump_stack (); } -printk_cpu_unlock_irqrestore (flags); +printk_cpu_sync_put_irqrestore (flags);cpumask_clear_cpu (cpu,to_cpumask (backtrace_mask));return true; }
Signature · 1 line
2.30.2
Re: [PATCH printk v2 01/12] printk: rename cpulock functions
Quoted 6 lines · John Ogness
Since the printk cpulock is CPU-reentrant and since it is used in all contexts, its usage must be carefully considered and most likely will require programming locklessly. To avoid mistaking the printk cpulock as a typical lock, rename it to
cpu_sync. The main functions then become:
Quoted 7 lines
printk_cpu_sync_get_irqsave(flags); printk_cpu_sync_put_irqrestore(flags);Add extra notes of caution in the function description to help developers understand the requirements for correct usage.
Signed-off-by John Ogness <[email protected]>
Looks good the me.
Best Regards,
Petr
John Ogness
maintainerPATCH v2 2/12 printk: cpu sync always disable interrupts
In reply to John OgnessThe CPU sync functions are a NOP for !CONFIG_SMP. But for !CONFIG_SMP they still need to disable interrupts in order to preserve context within the CPU sync sections.
--- include/linux/printk.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/printk.h +9 -9
@@ -281,9 +281,16 @@ extern int __printk_cpu_sync_try_get(void);extern void __printk_cpu_sync_wait (void );extern void __printk_cpu_sync_put (void ); +#else + +#define __printk_cpu_sync_try_get () true +#define __printk_cpu_sync_wait () +#define __printk_cpu_sync_put () +#endif /* CONFIG_SMP */ +/** -* printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinning -* lock and disable interrupts. +* printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the printk +* cpu-reentrant spinning lock. * @flags: Stack-allocated storage for saving local interrupt state, * to be passed to printk_cpu_sync_put_irqrestore(). *
@@ -322,13 +329,6 @@ extern void __printk_cpu_sync_put(void);local_irq_restore (flags); \ }while (0 ) -#else - -#define printk_cpu_sync_get_irqsave (flags) ((void)flags) -#define printk_cpu_sync_put_irqrestore (flags) ((void)flags) - -#endif /* CONFIG_SMP */ - extern int kptr_restrict;/**
Signature · 1 line
2.30.2
John Ogness
maintainerPATCH v2 3/12 printk: get caller_id/timestamp after migration disable
In reply to John OgnessCurrently the local CPU timestamp and caller_id for the record are collected while migration is enabled. Since this information is CPU-specific, it should be collected with migration disabled.
Migration is disabled immediately after collecting this information anyway, so just move the information collection to after the migration disabling.
--- kernel/printk/printk.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
kernel/printk/printk.c +6 -4
@@ -2041,7 +2041,7 @@ static inline void printk_delay(void)static inline u32 printk_caller_id (void ) {return in_task () ?task_pid_nr (current) : -0x80000000 +raw_smp_processor_id (); +0x80000000 +smp_processor_id (); }/**
@@ -2123,7 +2123,6 @@ int vprintk_store(int facility, int level,const struct dev_printk_info *dev_info, const char *fmt, va_list args) { - const u32 caller_id = printk_caller_id(); struct prb_reserved_entry e;enum printk_info_flags flags =0 ;struct printk_record r;
@@ -2133,10 +2132,14 @@ int vprintk_store(int facility, int level,u8 *recursion_ptr;u16 reserve_size;va_list args2; +u32 caller_id;u16 text_len;int ret =0 ;u64 ts_nsec; +if (!printk_enter_irqsave (recursion_ptr, irqflags)) +return 0 ; +/* * Since the duration of printk() can vary depending on the message * and state of the ringbuffer, grab the timestamp now so that it is
@@ -2145,8 +2148,7 @@ int vprintk_store(int facility, int level,*/ ts_nsec =local_clock (); -if (!printk_enter_irqsave (recursion_ptr, irqflags)) -return 0 ; + caller_id =printk_caller_id ();/* * The sprintf needs to come first since the syslog prefix might be
Signature · 1 line
2.30.2
Quoted 23 lines · John Ogness
This is v2 of a series to implement a kthread for each registered console. v1 is here [0]. 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 functionsprintk_prefer_direct_enter()andprintk_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(): amight_sleep()function that will block until all active printing threads have caught up to the latest record at the time of thepr_flush()call. This function is useful, for example, to wait until pending records are flushed to consoles before suspending.
Quoted 5 lines
Note that this series does *not* increase the reliability of console printing. Rather it focuses on the non-interference aspect of
printk()by decouplingprintk()callers from printing (during normal operation). Nonetheless, the reliability aspect should not worsen due to this series.
From the list of patches and the only one I am Cc'ed to I can't find the answer to my question, i.e. does it take care about console_unregister() cases at run time? (We have some drivers to call ->exit() for console in that case, perhaps it should do something before calling it.)
Would be nice to see some pointers where I can find the answer and maybe even describing in the cover-letter/commit message/documentation.
Signature · 2 lines
With Best Regards, Andy Shevchenko
John Ogness
maintainerQuoted 2 lines · Andy Shevchenko
From the list of patches and the only one I am Cc'ed to I can't find the answer to my question,
It seems lore is having trouble picking up the series. Until then, I've pushed the series to github.com so you can see the code.
Quoted 3 lines
i.e. does it take care about
console_unregister()cases at run time? (We have some drivers to call ->exit() for console in that case, perhaps it should do something before calling it.)
Yes. In console_unregister(), the printing thread is shutdown before the actual console unregistration takes place. The thread is shutdown here:
The console's exit() callback is below that:
Quoted 3 lines
Would be nice to see some pointers where I can find the answer and maybe even describing in the cover-letter/commit message/documentation.
I suppose for the console thread patch [0] I could add a paragraph to the commit message mentioning kernel thread shutdown for console unregistration.
[0] https://github.com/Linutronix/linux/commit/e26ae677da2e339dd268c1f871b81e61e782393f
John
John Ogness
maintainerPATCH v2 4/12 printk: call boot_delay_msec() in printk_delay()
In reply to John Ognessboot_delay_msec() is always called immediately before printk_delay() so just call it from within printk_delay().
--- kernel/printk/printk.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/printk/printk.c +4 -3
@@ -2026,8 +2026,10 @@ static u8 *__printk_recursion_counter(void)int printk_delay_msec__read_mostly ; -static inline void printk_delay (void ) +static inline void printk_delay (int level) { +boot_delay_msec (level); +if (unlikely (printk_delay_msec)) {int m = printk_delay_msec;
@@ -2252,8 +2254,7 @@ asmlinkage int vprintk_emit(int facility, int level, in_sched = true; } -boot_delay_msec (level); -printk_delay (); +printk_delay (level); printed_len =vprintk_store (facility, level, dev_info, fmt, args);
Signature · 1 line
2.30.2
John Ogness
maintainerPATCH v2 5/12 printk: add macro for console detail messages
In reply to John OgnessIt is useful to generate log messages that include details about the related console. Rather than duplicate the logic to assemble the details, put that logic into a macro printk_console_msg().
Once console printers become threaded, this macro will find many users.
--- kernel/printk/printk.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
kernel/printk/printk.c +6 -6
@@ -2993,6 +2993,10 @@ static void try_enable_default_console(struct console *newcon) newcon->flags |=CON_CONSDEV ; } +#define printk_console_msg (c, lvl, msg) \ + printk(lvl pr_fmt("%sconsole [%s%d] " msg "\n"), \ + (c->flags & CON_BOOT) ? "boot" : "", c->name, c->index) +/* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to
@@ -3131,9 +3135,7 @@ void register_console(struct console *newcon)* users know there might be something in the kernel's log buffer that * went to the bootconsole (that they do not see on the real console) */ -pr_info ("%sconsole [%s%d] enabled\n" , - (newcon->flags &CON_BOOT ) ?"boot" :"" , - newcon->name, newcon->index); +printk_console_msg (newcon,KERN_INFO ,"enabled" );if (bootcon_enabled && ((newcon->flags & (CON_CONSDEV |CON_BOOT )) ==CON_CONSDEV ) && !keep_bootcon) {
@@ -3152,9 +3154,7 @@ int unregister_console(struct console *console)struct console *con;int res; -pr_info ("%sconsole [%s%d] disabled\n" , - (console->flags &CON_BOOT ) ?"boot" :"" , - console->name, console->index); +printk_console_msg (console,KERN_INFO ,"disabled" ); res =_braille_unregister_console (console);if (res <0 )
Signature · 1 line
2.30.2
Petr Mladek
maintainerRe: [PATCH printk v2 05/12] printk: add macro for console detail messages
Quoted 6 lines · John Ogness
It is useful to generate log messages that include details about the related console. Rather than duplicate the logic to assemble the details, put that logic into a macro
printk_console_msg().Once console printers become threaded, this macro will find many users.
I really like this idea.
Quoted 10 lines
Signed-off-by John Ogness <[email protected]>--- kernel/printk/printk.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)kernel/printk/printk.c +0
-0
kernel/printk/printk.c static void try_enable_default_console(struct console *newcon) · line 2998
Quoted 6 lines
kernel/printk/printk.c +3
-0@@ -2993,6 +2993,10 @@ static void try_enable_default_console(struct console *newcon) newcon->flags |=CON_CONSDEV ; } +#define printk_console_msg (c, lvl, msg) \ + printk(lvl pr_fmt("%sconsole [%s%d] " msg "\n"), \ + (c->flags & CON_BOOT) ? "boot" : "", c->name, c->index)
This might be dangerous because "msg" is handled as printk format. Any potential %<modifier> might cause unexpected memory access.
Honestly, I am not good at writing macros. I played with it and the following worked for me:
#define printk_console_msg(c, lvl, fmt, ...) \
printk(lvl pr_fmt("%sconsole [%s%d] " fmt), \
(c->flags & CON_BOOT) ? "boot" : "", \
c->name, c->index, \
##__VA_ARGS__)Note that it requires adding "\n" back to the callers.
Also, please, call it "console_printk()" or so. It needs extra arguments and this name will help to distinguish it from the plain printk() wrappers. From my POV, it is something like, for example, dev_printk(), snd_printk(), or xfs_printk_ratelimited().
Best Regards,
Petr
John Ogness
maintainerPATCH v2 6/12 printk: refactor and rework printing logic
In reply to John OgnessRefactor/rework printing logic in order to prepare for moving to threaded console printing.
- Move @console_seq into struct console so that the current "position" of each console can be tracked individually.
- Move @console_dropped into struct console so that the current drop count of each console can be tracked individually.
- Modify printing logic so that each console independently loads, prepares, and prints its next record.
- Remove exclusive_console logic. Since console positions are handled independently, replaying past records occurs naturally.
- Update the comments explaining why preemption is disabled while printing from printk() context.
With these changes, there is a change in behavior: the console replaying the log (formerly exclusive console) will no longer block other consoles. New messages appear on the other consoles while the newly added console is still replaying.
--- include/linux/console.h | 2 + kernel/printk/printk.c | 429 ++++++++++++++++++++-------------------- 2 files changed, 220 insertions(+), 211 deletions(-)
include/linux/console.h +2 -0
@@ -151,6 +151,8 @@ struct console {int cflag;uint ispeed;uint ospeed; +u64 seq; +unsigned long dropped;void *data;struct console *next; };
kernel/printk/printk.c +218 -211
540 lines not on this page · raw message
Signature · 1 line
2.30.2
Petr Mladek
maintainerRe: [PATCH printk v2 06/12] printk: refactor and rework printing logic
Quoted 25 lines · John Ogness
Refactor/rework printing logic in order to prepare for moving to threaded console printing.
- Move @console_seq into struct console so that the current "position" of each console can be tracked individually.
- Move @console_dropped into struct console so that the current drop count of each console can be tracked individually.
- Modify printing logic so that each console independently loads, prepares, and prints its next record.
- Remove
exclusive_consolelogic. Since console positions are handled independently, replaying past records occurs naturally.- Update the comments explaining why preemption is disabled while printing from
printk()context.With these changes, there is a change in behavior: the console replaying the log (formerly exclusive console) will no longer block other consoles. New messages appear on the other consoles while the newly added console is still replaying.
kernel/printk/printk.c static bool abandon_console_lock_in_panic(void) · line 2705
Quoted 4 lines
kernel/printk/printk.c +4
-0+/* +* Print out all remaining records to all consoles. +* +* Requires the console_lock.
It would be nice to document @do_cond_resched, something like:
* @do_cond_resched is set by the caller. It can be true only in schedulable context.
Quoted 2 lines
+ *
+ * Returns true if a console was available for flushing, otherwise false.
It is not completely true after adding abandon_console_lock_in_panic(). But it is actually a chance to slightly change the meaning. I suggest to define it as:
* Returns true when there was at least one usable console and all messages were flushed to all usable consoles.
It will help to better define @next_seq meaning. And it is exactly the meaning that we need in console_unlock(), see below.
Quoted 3 lines
+ *
+ * @next_seq is set to the highest sequence number of all of the consoles that
+ * were flushed.
The value is meaningful only when all consoles were flushed. It would be nice to make it more clear. If we changed the return value meaning than we could say:
* @next_seq is the sequence number after the last available record.
* The value is valid only when this function returns true. It means
* that usable consoles are completely flushed.
Quoted 17 lines
+ *
+ * @handover will be set to true if a printk waiter has taken over the
+ *console_lock, in which case the caller is no longer holding the
+ *console_lock. Otherwise it is set to false.
+ */kernel/printk/printk.c +12
-0+static bool console_flush_all (bool do_cond_resched,u64 *next_seq,bool *handover) +{ +bool any_usable = false; +struct console *con; +bool any_progress; + + *next_seq =0 ; + *handover = false; + + do { + any_progress = false; +
kernel/printk/printk.c static bool abandon_console_lock_in_panic(void) · line 2737
Quoted 10 lines
kernel/printk/printk.c +10
-0+for_each_console (con) { +bool progress; + +if (!console_is_usable (con)) +continue ; + any_usable = true; + + progress =console_emit_next_record (con, handover); +if (*handover) +return true;
The return value is not used in this case. I would return "false" here and update the meaning as suggested above.
Quoted 96 lines
kernel/printk/printk.c +25
-57+/* Track the highest seq flushed. */ +if (con->seq > *next_seq) + *next_seq = con->seq; + +if (!progress) +continue ; + any_progress = true; + +/* Allow panic_cpu to take over the consoles safely */ +if (abandon_console_lock_in_panic ()) +return false; + +if (do_cond_resched) +cond_resched (); + } + }while (any_progress); + +return any_usable; }/** @@ -2686,125 +2790,33 @@ void console_unlock(void)* between lines if allowable. Not doing so can cause a very long * scheduling stall on a slow console leading to RCU stall and * softlockup warnings which exacerbate the issue with more -* messages practically incapacitating the system. -* -* console_trylock() is not able to detect the preemptive -* context reliably. Therefore the value must be stored before -* and cleared after the "again" goto label. +* messages practically incapacitating the system. Therefore, create +* a local to use for the printing loop. */ do_cond_resched = console_may_schedule; -again: - console_may_schedule =0 ; - -/* -* We released the console_sem lock, so we need to recheck if -* cpu is online and (if not) is there at least one CON_ANYTIME -* console. -*/ -if (!can_use_console ()) { - console_locked =0 ; -up_console_sem (); -return ; - } -for (;;) { -size_t ext_len =0 ; -int handover; -size_t len; + do { + console_may_schedule = 0; -skip: -if (!prb_read_valid (prb, console_seq, &r)) + flushed = console_flush_all(do_cond_resched, &next_seq, &handover); + if (handover) break; -if (console_seq != r.info->seq) { - console_dropped += r.info->seq - console_seq; - console_seq = r.info->seq; -if (panic_in_progress () && panic_console_dropped++ >10 ) { - suppress_panic_printk =1 ; -pr_warn_once ("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n" ); - } - } - -if (suppress_message_printing (r.info->level)) { -/* -* Skip record we have buffered and already printed -* directly to the console when we received it, and -* record that has level above the console loglevel. -*/ - console_seq++; - goto skip; - } - -/* Output to all consoles once old messages replayed. */ -if (unlikely (exclusive_console && - console_seq >= exclusive_console_stop_seq)) { - exclusive_console =NULL ; - } + __console_unlock(); -/* -* Handle extended console text first because later -* record_print_text() will modify the record buffer in-place. -*/ -if (nr_ext_console_drivers) { - ext_len =info_print_ext_header (ext_text, -sizeof (ext_text), - r.info); - ext_len += msg_print_ext_body(ext_text + ext_len,
kernel/printk/printk.c void console_unlock(void) · line 2807
Quoted 10 lines
kernel/printk/printk.c +1
-9-sizeof (ext_text) - ext_len, - &r.text_buf[0 ], - r.info->text_len, - &r.info->dev_info); - } - len =record_print_text (&r, - console_msg_format &MSG_FORMAT_SYSLOG , - printk_time); - console_seq++; +/* Were there any consoles available for flushing? */
The value is false also when abandon_console_lock_in_panic() returned true. We basically want to break here when we could not do the job. I would write something like:
/* * Do not try again when couldn't do the job. It avoids * an infinite loop. */
Quoted 44 lines
kernel/printk/printk.c +6
-35+if (!flushed) +break ;/* -* While actively printing out messages, if another printk() -* were to occur on another CPU, it may wait for this one to -* finish. This task can not be preempted if there is a -* waiter waiting to take over. -* -* Interrupts are disabled because the hand over to a waiter -* must not be interrupted until the hand over is completed -* (@console_waiter is cleared). +* Some context may have added new records after +* console_flush_all() but before unlocking the console. +* Re-check if there is a new record to flush. If the trylock +* fails, another context is already handling the printing. */ -printk_safe_enter_irqsave (flags); -console_lock_spinning_enable (); - -stop_critical_timings ();/* don't trace print latency */ -call_console_drivers (ext_text, ext_len, text, len); -start_critical_timings (); - - handover =console_lock_spinning_disable_and_check (); -printk_safe_exit_irqrestore (flags); -if (handover) -return ; - -/* Allow panic_cpu to take over the consoles safely */ -if (abandon_console_lock_in_panic ()) -break ; - -if (do_cond_resched) -cond_resched (); - } - -/* Get consistent value of the next-to-be-used sequence number. */ - next_seq = console_seq; - - console_locked =0 ; -up_console_sem (); - -/*
kernel/printk/printk.c void console_unlock(void) · line 2818
Quoted 10 lines
kernel/printk/printk.c +2
-8-* Someone could have filled up the buffer again, so re-check if there's -* something to flush. In case we cannot trylock the console_sem again, -* there's a new owner and the console_unlock() from them will do the -* flush, no worries. -*/ - retry =prb_read_valid (prb, next_seq,NULL ); -if (retry && !abandon_console_lock_in_panic () &&console_trylock ()) - goto again; + } while (prb_read_valid(prb, next_seq, NULL) && + !abandon_console_lock_in_panic() &&
This is not longer needed. Either all messages are proceed or we are not here because console_flush_all() returned false.
Another motivation to remove it that it is bit racy here. The state could change only under the console lock.
It is not a big deal. But I would prefer to remove it because it makes false assumptions about the logic.
kernel/printk/printk.c void console_unlock(void) · line 2821
Quoted 3 lines
kernel/printk/printk.c +1
-0+ console_trylock()); }EXPORT_SYMBOL (console_unlock);
Summary: All the proposed changes are cosmetic. And I like the new code.
Best Regards,
Petr
John Ogness
maintainerPATCH v2 7/12 printk: move buffer definitions into console_emit_next_record() caller
In reply to John OgnessExtended consoles print extended messages and do not print messages about dropped records.
Non-extended consoles print "normal" messages as well as extra messages about dropped records.
Currently the buffers for these various message types are defined within the functions that might use them and their usage is based upon the CON_EXTENDED flag. This will be a problem when moving to kthread printers because each printer must be able to provide its own buffers.
Move all the message buffer definitions outside of console_emit_next_record(). The caller knows if extended or dropped messages should be printed and can specify the appropriate buffers to use. The console_emit_next_record() and call_console_driver() functions can know what to print based on whether specified buffers are non-NULL.
With this change, buffer definition/allocation/specification is separated from the code that does the various types of string printing.
--- kernel/printk/printk.c | 60 ++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 17 deletions(-)
kernel/printk/printk.c +43 -17
@@ -394,6 +394,9 @@ static struct latched_seq clear_seq = {/* the maximum size of a formatted record (i.e. with prefix added per line) */ #define CONSOLE_LOG_MAX 1024 +/* the maximum size for a dropped text message */ +#define DROPPED_TEXT_MAX 64 +/* the maximum size allowed to be reserved for a record */ #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX)
@@ -1901,18 +1904,18 @@ static int console_trylock_spinning(void)/* * Call the specified console driver, asking it to write out the specified -* text and length. For non-extended consoles, if any records have been +* text and length. If @dropped_text is non-NULL and any records have been * dropped, a dropped message will be written out first. */ -static void call_console_driver (struct console *con,const char *text,size_t len) +static void call_console_driver (struct console *con,const char *text,size_t len, +char *dropped_text) { -static char dropped_text[64 ];size_t dropped_len;trace_console_rcuidle (text, len); -if (con->dropped && !(con->flags &CON_EXTENDED )) { - dropped_len =snprintf (dropped_text,sizeof (dropped_text), +if (con->dropped && dropped_text) { + dropped_len =snprintf (dropped_text,DROPPED_TEXT_MAX ,"** %lu printk messages dropped **\n" , con->dropped); con->dropped =0 ;
@@ -2274,6 +2277,7 @@ EXPORT_SYMBOL(_printk);#else /* CONFIG_PRINTK */ #define CONSOLE_LOG_MAX 0 +#define DROPPED_TEXT_MAX 0#define printk_time false#define prb_read_valid (rb, seq, r) false
@@ -2297,7 +2301,10 @@ static ssize_t msg_print_ext_body(char *buf, size_t size, struct dev_printk_info *dev_info) { return 0; }static void console_lock_spinning_enable (void ) { }static int console_lock_spinning_disable_and_check (void ) {return 0 ; } -static void call_console_driver (struct console *con,const char *text,size_t len) { } +static void call_console_driver (struct console *con,const char *text,size_t len, +char *dropped_text) +{ +}static bool suppress_message_printing (int level) {return false; }#endif /* CONFIG_PRINTK */
@@ -2622,6 +2629,14 @@ static void __console_unlock(void)* Print one record for the given console. The record printed is whatever * record is the next available record for the given console. * +* @text is a buffer of size CONSOLE_LOG_MAX. +* +* If extended messages should be printed, @ext_text is a buffer of size +* CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL. +* +* If dropped messages should be printed, @dropped_text is a buffer of size +* DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL. +* * Requires the console_lock. * * Returns false if the given console has no next record to print, otherwise
@@ -2631,10 +2646,9 @@ static void __console_unlock(void)* console_lock, in which case the caller is no longer holding the * console_lock. Otherwise it is set to false. */ -static bool console_emit_next_record (struct console *con,bool *handover) +static bool console_emit_next_record (struct console *con,char *text,char *ext_text, +char *dropped_text,bool *handover) { -static char ext_text[CONSOLE_EXT_LOG_MAX ]; -static char text[CONSOLE_LOG_MAX ];static int panic_console_dropped;struct printk_info info;struct printk_record r;
@@ -2642,7 +2656,7 @@ static bool console_emit_next_record(struct console *con, bool *handover)char *write_text;size_t len; -prb_rec_init_rd (&r, &info, text,sizeof (text)); +prb_rec_init_rd (&r, &info, text,CONSOLE_LOG_MAX ); *handover = false;
@@ -2664,13 +2678,13 @@ static bool console_emit_next_record(struct console *con, bool *handover) goto skip; } -if (con->flags &CON_EXTENDED ) { - write_text = &ext_text[0 ]; - len =info_print_ext_header (ext_text,sizeof (ext_text), r.info); - len +=msg_print_ext_body (ext_text + len,sizeof (ext_text) - len, +if (ext_text) { + write_text = ext_text; + len =info_print_ext_header (ext_text,CONSOLE_EXT_LOG_MAX , r.info); + len +=msg_print_ext_body (ext_text + len,CONSOLE_EXT_LOG_MAX - len, &r.text_buf[0 ], r.info->text_len, &r.info->dev_info); }else { - write_text = &text[0 ]; + write_text = text; len =record_print_text (&r, console_msg_format &MSG_FORMAT_SYSLOG , printk_time); }
@@ -2688,7 +2702,7 @@ static bool console_emit_next_record(struct console *con, bool *handover)console_lock_spinning_enable ();stop_critical_timings ();/* don't trace print latency */ -call_console_driver (con, write_text, len); +call_console_driver (con, write_text, len, dropped_text);start_critical_timings (); con->seq++;
@@ -2715,6 +2729,9 @@ static bool console_emit_next_record(struct console *con, bool *handover)*/ static bool console_flush_all (bool do_cond_resched,u64 *next_seq,bool *handover) { +static char dropped_text[DROPPED_TEXT_MAX ]; +static char ext_text[CONSOLE_EXT_LOG_MAX ]; +static char text[CONSOLE_LOG_MAX ];bool any_usable = false;struct console *con;bool any_progress;
@@ -2732,7 +2749,16 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handovecontinue ; any_usable = true; - progress =console_emit_next_record (con, handover); +if (con->flags &CON_EXTENDED ) { +/* Extended consoles do not print "dropped messages". */ + progress =console_emit_next_record (con, &text[0 ], + &ext_text[0 ],NULL , + handover); + }else { + progress =console_emit_next_record (con, &text[0 ], +NULL , &dropped_text[0 ], + handover); + }if (*handover)return true;
Signature · 1 line
2.30.2
Re: [PATCH printk v2 07/12] printk: move buffer definitions into console_emit_next_record() caller
Quoted 17 lines · John Ogness
Extended consoles print extended messages and do not print messages about dropped records.
Non-extended consoles print "normal" messages as well as extra messages about dropped records.
Currently the buffers for these various message types are defined within the functions that might use them and their usage is based upon the
CON_EXTENDEDflag. This will be a problem when moving to kthread printers because each printer must be able to provide its own buffers.Move all the message buffer definitions outside of
console_emit_next_record(). The caller knows if extended or dropped messages should be printed and can specify the appropriate buffers to use. Theconsole_emit_next_record()andcall_console_driver()functions can know what to print based on whether specified buffers are non-NULL.
Quoted 4 lines
With this change, buffer definition/allocation/specification is separated from the code that does the various types of string printing.
Signed-off-by John Ogness <[email protected]>
I feel slightly sad that we replace the safe sizeof(buffer) with a hardcoded size. But the change makes sense. And I think that it is not worth passing the size as parameters.
Best Regards,
Petr
John Ogness
maintainerPATCH v2 8/12 printk: add pr_flush()
In reply to John OgnessProvide a might-sleep function to allow waiting for console printers to catch up to the latest logged message.
Use pr_flush() whenever it is desirable to get buffered messages printed before continuing: suspend_console(), resume_console(), console_stop(), console_start(), console_unblank().
--- include/linux/printk.h | 7 ++++ kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+)
include/linux/printk.h +7 -0
@@ -170,6 +170,8 @@ extern void __printk_safe_exit(void);#define printk_deferred_enter __printk_safe_enter#define printk_deferred_exit __printk_safe_exit +extern bool pr_flush (int timeout_ms,bool reset_on_progress); +/* * Please don't use printk_ratelimit(), because it shares ratelimiting state * with all other unrelated printk_ratelimit() callsites. Instead use
@@ -220,6 +222,11 @@ static inline void printk_deferred_exit(void) { } +static inline bool pr_flush (int timeout_ms,bool reset_on_progress) +{ +return true; +} +static inline int printk_ratelimit (void ) {return 0 ;
kernel/printk/printk.c +83 -0
131 lines not on this page · raw message
Signature · 1 line
2.30.2
Petr Mladek
maintainerRe: [PATCH printk v2 08/12] printk: add pr_flush()
Quoted 18 lines · John Ogness
Provide a might-sleep function to allow waiting for console printers to catch up to the latest logged message.
Use
pr_flush()whenever it is desirable to get buffered messages printed before continuing:suspend_console(),resume_console(),console_stop(),console_start(),console_unblank().kernel/printk/printk.c +3
-0@@ -3321,6 +3331,79 @@ static int __init printk_late_init(void)late_initcall (printk_late_init);#if definedCONFIG_PRINTK +/* If @con is specified, only wait for that console. Otherwise wait for all. */ +static bool __pr_flush (struct console *con,int timeout_ms,bool reset_on_progress) +{
kernel/printk/printk.c static int __init printk_late_init(void) · line 3346
Quoted 10 lines
kernel/printk/printk.c +10
-0+int remaining = timeout_ms; +struct console *c; +u64 last_diff =0 ; +u64 printk_seq; +u64 diff; +u64 seq; + +might_sleep (); + + seq =prb_next_seq (prb);
I auggest to add here:
/* * Try to flush the messages when kthreads are not available * and there is not other console lock owner. */ if (console_trylock()) console_unlock()
Quoted 5 lines
kernel/printk/printk.c +5
-0+ +for (;;) { + diff =0 ; + +console_lock ();
kernel/printk/printk.c static int __init printk_late_init(void) · line 3361
Quoted 10 lines
kernel/printk/printk.c +10
-0+for_each_console (c) { +if (con && con != c) +continue ; +if (!console_is_usable (c)) +continue ; + printk_seq = c->seq; +if (printk_seq < seq) + diff += seq - printk_seq; + } +console_unlock ();
This is a bit sub-optimal when the kthreads are not available or are disabled. In this case, the messages are flushed [*] by the console_unlock() and the diff is outdated.
As a result, the code would non-necessarily sleep 100ms before it realizes that the job is done.
One might argue that the problem will be only when there are non-handled messages and in situations when the delay probably is not critical.
Well, it is ugly to keep it this way. I suggest to add the extra console_trylock()/unlock() at the beginning.
Quoted 13 lines
kernel/printk/printk.c +13
-0+ +if (diff != last_diff && reset_on_progress) + remaining = timeout_ms; + +if (diff ==0 || remaining ==0 ) +break ; + +if (remaining <0 ) { +/* no timeout limit */ +msleep (100 ); + }else if (remaining <100 ) { +msleep (remaining); + remaining =0 ;
kernel/printk/printk.c static int __init printk_late_init(void) · line 3384
Quoted 10 lines
kernel/printk/printk.c +10
-0+ }else { +msleep (100 ); + remaining -=100 ; + } + + last_diff = diff; + } + +return (diff ==0 ); +}
Otherwise, it looks good to me. It is an interesting function. I wonder if it gets popular also outside printk code. ;-)
Best Regards,
Petr
John Ogness
maintainerRe: [PATCH printk v2 08/12] printk: add pr_flush()
Quoted 20 lines · Petr Mladek
Quoted 7 lines
kernel/printk/printk.c +3
-0@@ -3321,6 +3331,79 @@ static int __init printk_late_init(void)late_initcall (printk_late_init);#if definedCONFIG_PRINTK +/* If @con is specified, only wait for that console. Otherwise wait for all. */ +static bool __pr_flush (struct console *con,int timeout_ms,bool reset_on_progress) +{kernel/printk/printk.c static int __init printk_late_init(void) · line 3346
Quoted 10 lines
kernel/printk/printk.c +10
-0+int remaining = timeout_ms; +struct console *c; +u64 last_diff =0 ; +u64 printk_seq; +u64 diff; +u64 seq; + +might_sleep (); + + seq =prb_next_seq (prb);I suggest to add here:
Quoted 6 lines
/* * Try to flush the messages when kthreads are not available * and there is not other console lock owner. */ if (console_trylock()) console_unlock()
At this point in the series, we do not have kthreads yet. ;-)
And besides, you are implying that there are messages in the ringbuffer, but _nobody_ is printing them. That would be a bug in our implementation.
Quoted 11 lines
Quoted 11 lines
kernel/printk/printk.c +11
-0+ +for (;;) { + diff =0 ; + +console_lock (); +for_each_console (c) { +if (con && con != c) +continue ; +if (!console_is_usable (c)) +continue ; + printk_seq = c->seq;
Quoted 8 lines
kernel/printk/printk.c static int __init printk_late_init(void) · line 3361
Quoted 4 lines
kernel/printk/printk.c +4
-0+if (printk_seq < seq) + diff += seq - printk_seq; + } +console_unlock ();This is a bit sub-optimal when the kthreads are not available or are disabled. In this case, the messages are flushed [*] by the
console_unlock()and the diff is outdated.
The messages must be flushed by another context. It is wrong to rely on this context to do the flushing. In direct mode, those messages should have been flushed before the above console_lock() succeeded. Otherwise it is a bug. (For threaded mode, console_unlock() does not flush anyway.)
Quoted 1 line
Well, it is ugly to keep it this way.
If you don't mind, I will not change this function for v3. I do not want to start sprinkling console_trylock()/console_unlock() calls all over the code to cover up possible bugs and races in the implementation.
John
John Ogness
maintainerPATCH v2 9/12 printk: add functions to prefer direct printing
In reply to John OgnessOnce kthread printing is available, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using pr_flush() to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case.
Introduce printk_prefer_direct_enter() and printk_prefer_direct_exit() functions to explicitly (and globally) activate/deactivate preferred direct console printing. The term "direct console printing" refers to printing to all enabled consoles from the context of the printk caller. The term "prefer" is used because this type of printing is only best effort. If the console is currently locked or other printers are already actively printing, the printk caller will need to rely on the other contexts to handle the printing.
This preferred direct printing is how all printing is currently handled (unless explicitly deferred).
When kthread printing is introduced, there may be some unanticipated problems due to kthreads being unable to flush important messages. In order to minimize such risks, preferred direct printing is activated for the primary important messages when the system experiences general types of major errors. These are:
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrq
Note that since kthread printing does not yet exist, no behavior changes result from this commit. This is only implementing the counter and marking the various places where preferred direct printing is active.
--- drivers/tty/sysrq.c | 2 ++ include/linux/printk.h | 11 +++++++++++ kernel/hung_task.c | 11 ++++++++++- kernel/panic.c | 4 ++++ kernel/printk/printk.c | 28 ++++++++++++++++++++++++++++ kernel/rcu/tree_stall.h | 2 ++ kernel/reboot.c | 14 +++++++++++++- kernel/watchdog.c | 4 ++++ kernel/watchdog_hld.c | 4 ++++ 9 files changed, 78 insertions(+), 2 deletions(-)
drivers/tty/sysrq.c +2 -0
@@ -578,6 +578,7 @@ void __handle_sysrq(int key, bool check_mask)rcu_sysrq_start ();rcu_read_lock (); +printk_prefer_direct_enter ();/* * Raise the apparent loglevel to maximum so that the sysrq header * is shown to provide the user with positive feedback. We do not
@@ -619,6 +620,7 @@ void __handle_sysrq(int key, bool check_mask)pr_cont ("\n" ); console_loglevel = orig_log_level; } +printk_prefer_direct_exit ();rcu_read_unlock ();rcu_sysrq_end ();
include/linux/printk.h +11 -0
@@ -170,6 +170,9 @@ extern void __printk_safe_exit(void);#define printk_deferred_enter __printk_safe_enter#define printk_deferred_exit __printk_safe_exit +extern void printk_prefer_direct_enter (void ); +extern void printk_prefer_direct_exit (void ); +extern bool pr_flush (int timeout_ms,bool reset_on_progress);/*
@@ -222,6 +225,14 @@ static inline void printk_deferred_exit(void) { } +static inline void printk_prefer_direct_enter (void ) +{ +} + +static inline void printk_prefer_direct_exit (void ) +{ +} +static inline bool pr_flush (int timeout_ms,bool reset_on_progress) {return true;
kernel/hung_task.c +10 -1
@@ -127,6 +127,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)* complain: */ if (sysctl_hung_task_warnings) { +printk_prefer_direct_enter (); +if (sysctl_hung_task_warnings >0 ) sysctl_hung_task_warnings--; pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
@@ -142,6 +144,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)if (sysctl_hung_task_all_cpu_backtrace) hung_task_show_all_bt = true; + +printk_prefer_direct_exit (); }touch_nmi_watchdog ();
@@ -204,12 +208,17 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) } unlock:rcu_read_unlock (); -if (hung_task_show_lock) +if (hung_task_show_lock) { +printk_prefer_direct_enter ();debug_show_all_locks (); +printk_prefer_direct_exit (); + }if (hung_task_show_all_bt) { hung_task_show_all_bt = false; +printk_prefer_direct_enter ();trigger_all_cpu_backtrace (); +printk_prefer_direct_exit (); }if (hung_task_call_panic)
kernel/panic.c +4 -0
@@ -560,6 +560,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint, {disable_trace_on_warning (); +printk_prefer_direct_enter (); +if (file) pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", raw_smp_processor_id(), current->pid, file, line,
@@ -597,6 +599,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 ); + +printk_prefer_direct_exit (); }#ifndef __WARN_FLAGS
kernel/printk/printk.c +28 -0
@@ -362,6 +362,34 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;static DEFINE_MUTEX (syslog_lock);#ifdef CONFIG_PRINTK +static atomic_t printk_prefer_direct =ATOMIC_INIT (0 ); + +/** +* printk_prefer_direct_enter - cause printk() calls to attempt direct +* printing to all enabled consoles +* +* Since it is not possible to call into the console printing code from any +* context, there is no guarantee that direct printing will occur. +* +* This globally effects all printk() callers. +* +* Context: Any context. +*/ +void printk_prefer_direct_enter (void ) +{ +atomic_inc (&printk_prefer_direct); +} + +/** +* printk_prefer_direct_exit - restore printk() behavior +* +* Context: Any context. +*/ +void printk_prefer_direct_exit (void ) +{ +WARN_ON (atomic_dec_if_positive (&printk_prefer_direct) <0 ); +} +DECLARE_WAIT_QUEUE_HEAD (log_wait);/* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */
kernel/rcu/tree_stall.h +2 -0
@@ -619,6 +619,7 @@ static void print_cpu_stall(unsigned long gps)* See Documentation/RCU/stallwarn.rst for info on how to debug * RCU CPU stall warnings. */ +printk_prefer_direct_enter ();trace_rcu_stall_warning (rcu_state.name,TPS ("SelfDetected" ));pr_err ("INFO: %s self-detected stall on CPU\n" , rcu_state.name);raw_spin_lock_irqsave_rcu_node (rdp->mynode, flags);
@@ -656,6 +657,7 @@ static void print_cpu_stall(unsigned long gps)*/ set_tsk_need_resched (current);set_preempt_need_resched (); +printk_prefer_direct_exit (); }static void check_cpu_stall (struct rcu_data *rdp)
kernel/reboot.c +13 -1
@@ -447,9 +447,11 @@ static int __orderly_reboot(void) ret =run_cmd (reboot_cmd);if (ret) { +printk_prefer_direct_enter ();pr_warn ("Failed to start orderly reboot: forcing the issue\n" );emergency_sync ();kernel_restart (NULL ); +printk_prefer_direct_exit (); }return ret;
@@ -462,6 +464,7 @@ static int __orderly_poweroff(bool force) ret =run_cmd (poweroff_cmd);if (ret && force) { +printk_prefer_direct_enter ();pr_warn ("Failed to start orderly shutdown: forcing the issue\n" );/*
@@ -471,6 +474,7 @@ static int __orderly_poweroff(bool force)*/ emergency_sync ();kernel_power_off (); +printk_prefer_direct_exit (); }return ret;
@@ -528,6 +532,8 @@ EXPORT_SYMBOL_GPL(orderly_reboot);*/ static void hw_failure_emergency_poweroff_func (struct work_struct *work) { +printk_prefer_direct_enter (); +/* * We have reached here after the emergency shutdown waiting period has * expired. This means orderly_poweroff has not been able to shut off
@@ -544,6 +550,8 @@ static void hw_failure_emergency_poweroff_func(struct work_struct *work)*/ pr_emerg ("Hardware protection shutdown failed. Trying emergency restart\n" );emergency_restart (); + +printk_prefer_direct_exit (); } static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,
@@ -582,11 +590,13 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced) {static atomic_t allow_proceed =ATOMIC_INIT (1 ); +printk_prefer_direct_enter (); +pr_emerg ("HARDWARE PROTECTION shutdown (%s)\n" , reason);/* Shutdown should be initiated only once. */ if (!atomic_dec_and_test (&allow_proceed)) -return ; + goto out;/* * Queue a backup emergency shutdown in the event of
@@ -594,6 +604,8 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced)*/ hw_failure_emergency_poweroff (ms_until_forced);orderly_poweroff (true); +out: +printk_prefer_direct_exit (); }EXPORT_SYMBOL_GPL (hw_protection_shutdown);
kernel/watchdog.c +4 -0
@@ -424,6 +424,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)/* Start period for the next softlockup warning. */ update_report_ts (); +printk_prefer_direct_enter (); +pr_emerg ("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n" ,smp_processor_id (), duration, current->comm,task_pid_nr (current));
@@ -442,6 +444,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)add_taint (TAINT_SOFTLOCKUP ,LOCKDEP_STILL_OK );if (softlockup_panic)panic ("softlockup: hung tasks" ); + +printk_prefer_direct_exit (); }return HRTIMER_RESTART ;
kernel/watchdog_hld.c +4 -0
@@ -135,6 +135,8 @@ static void watchdog_overflow_callback(struct perf_event *event,if (__this_cpu_read (hard_watchdog_warn) == true)return ; +printk_prefer_direct_enter (); +pr_emerg ("Watchdog detected hard LOCKUP on cpu %d\n" , this_cpu);print_modules ();
@@ -155,6 +157,8 @@ static void watchdog_overflow_callback(struct perf_event *event,if (hardlockup_panic)nmi_panic (regs,"Hard LOCKUP" ); +printk_prefer_direct_exit (); +__this_cpu_write (hard_watchdog_warn, true);return ; }
Signature · 1 line
2.30.2
Re: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 32 lines · John Ogness
Once kthread printing is available, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using
pr_flush()to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case.Introduce
printk_prefer_direct_enter()andprintk_prefer_direct_exit()functions to explicitly (and globally) activate/deactivate preferred direct console printing. The term "direct console printing" refers to printing to all enabled consoles from the context of the printk caller. The term "prefer" is used because this type of printing is only best effort. If the console is currently locked or other printers are already actively printing, the printk caller will need to rely on the other contexts to handle the printing.This preferred direct printing is how all printing is currently handled (unless explicitly deferred).
When kthread printing is introduced, there may be some unanticipated problems due to kthreads being unable to flush important messages. In order to minimize such risks, preferred direct printing is activated for the primary important messages when the system experiences general types of major errors. These are:
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrq
Quoted 6 lines
Note that since kthread printing does not yet exist, no behavior changes result from this commit. This is only implementing the counter and marking the various places where preferred direct printing is active.
Signed-off-by John Ogness <[email protected]>
Looks good to me. Let's see how it works in practice. It is possible that we will need to add it on more locations. But it is also possible that we will be able to remove it somewhere.
Best Regards,
Petr
Re: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 26 lines · Petr Mladek
Quoted 25 lines · John Ogness
Once kthread printing is available, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using
pr_flush()to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case.Introduce
printk_prefer_direct_enter()andprintk_prefer_direct_exit()functions to explicitly (and globally) activate/deactivate preferred direct console printing. The term "direct console printing" refers to printing to all enabled consoles from the context of the printk caller. The term "prefer" is used because this type of printing is only best effort. If the console is currently locked or other printers are already actively printing, the printk caller will need to rely on the other contexts to handle the printing.This preferred direct printing is how all printing is currently handled (unless explicitly deferred).
When kthread printing is introduced, there may be some unanticipated problems due to kthreads being unable to flush important messages. In order to minimize such risks, preferred direct printing is activated for the primary important messages when the system experiences general types of major errors. These are:
Quoted 6 lines
Quoted 6 lines
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrq
I agree that those needs direct printing.
Did you maybe checked how much (e.g. in percentage) of a normal log is then still left to be printed threadened? Just in case it's not much, is there still a benefit to introduce threadened printing?
Or another example, e.g. when running on a slow console (the best testcase I know of is a 9600 baud serial port), where some printks are threadened. Then a warn printk() has to be shown, doesn't it mean all threadened printks have to be flushed (waited for) until
the warning can be shown. Will there then still be a measureable time benefit?
Just wondering...
Helge
John Ogness
maintainerRe: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 11 lines · Helge Deller
Quoted 6 lines
Quoted 6 lines
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrqI agree that those needs direct printing.
Did you maybe checked how much (e.g. in percentage) of a normal log is then still left to be printed threadened? Just in case it's not much, is there still a benefit to introduce threadened printing?
A "normal" log can still contain many many messages. There have been reports [0] where the system died because _printk_ killed it. This was not any of the scenarios that we want direct printing for. It was just a lot of messages that some driver wanted to output.
Until now the response was always to reduce printk usage or avoid it altogether. printk has a bad reputation because it is so unpredictable and expensive. So we are fixing printk. With threaded printers, we make a dramatic change. printk becomes 100% lockless and consistently quite fast. You can use it to debug or log messages within any code in the kernel and not need to worry about horrible latencies being introduced.
For drivers that start dumping tons of messages, there is no concern that the system will die because of the messages.
Quoted 5 lines
Or another example, e.g. when running on a slow console (the best testcase I know of is a 9600 baud serial port), where some printks are threadened. Then a warn
printk()has to be shown, doesn't it mean all threadened printks have to be flushed (waited for) until the warning can be shown. Will there then still be a measureable time benefit?
Now you are talking about something else, unrelated to the threaded printing. A warn is considered a serious non-normal event. In this case, printk temporarily falls back to the direct, slow, unpredictable, printk-caller-context behavior we know today.
If we see that direct printing is being used during normal operation (i.e. developers are performing warns just for fun), then we should not mark warns for direct printing. We will need to get some real-world experience here, but the goal is that systems do not use direct printing unless a real serious emergency is going on. Otherwise, the system risks dying because of the messages rather than the problem being reported.
John
Re: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Hi John,
Quoted 25 lines · John Ogness
Quoted 11 lines · Helge Deller
Quoted 6 lines
Quoted 6 lines
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrqI agree that those needs direct printing.
Did you maybe checked how much (e.g. in percentage) of a normal log is then still left to be printed threadened? Just in case it's not much, is there still a benefit to introduce threadened printing?A "normal" log can still contain many many messages. There have been reports [0] where the system died because _printk_ killed it. This was not any of the scenarios that we want direct printing for. It was just a lot of messages that some driver wanted to output.
Until now the response was always to reduce printk usage or avoid it altogether. printk has a bad reputation because it is so unpredictable and expensive. So we are fixing printk. With threaded printers, we make a dramatic change. printk becomes 100% lockless and consistently quite fast. You can use it to debug or log messages within any code in the kernel and not need to worry about horrible latencies being introduced.
Quoted 2 lines
For drivers that start dumping tons of messages, there is no concern that the system will die because of the messages.
Thank you for this good explanation!
In my case - while I debug low-level kernel code - I then just need to use pr_warn() or pr_emerg() and get it printed non-threadened. That's sufficient for me.
Quoted 10 lines
Quoted 5 lines
Or another example, e.g. when running on a slow console (the best testcase I know of is a 9600 baud serial port), where some printks are threadened. Then a warn
printk()has to be shown, doesn't it mean all threadened printks have to be flushed (waited for) until the warning can be shown. Will there then still be a measureable time benefit?Now you are talking about something else, unrelated to the threaded printing. A warn is considered a serious non-normal event. In this case, printk temporarily falls back to the direct, slow, unpredictable, printk-caller-context behavior we know today.
Good.
Quoted 6 lines
If we see that direct printing is being used during normal operation (i.e. developers are performing warns just for fun), then we should not mark warns for direct printing. We will need to get some real-world experience here, but the goal is that systems do not use direct printing unless a real serious emergency is going on. Otherwise, the system risks dying because of the messages rather than the problem being reported.
I agree, it's good if this gets fixed at some point.
Thanks!
Helge
Quoted 3 lines
John
John Ogness
maintainerRe: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 3 lines · Helge Deller
In my case - while I debug low-level kernel code - I then just need to use
pr_warn()orpr_emerg()and get it printed non-threadened. That's sufficient for me.
Actually, no. The loglevel does not determine if a message is direct printed or not. By "warn" I was referring to WARN_ON(condition).
If you are debugging low-level kernel code, you usually will _want_ threaded printing. The timestamps match the printk() call, so you will get accurate logs. And the runtime performance of your low-level kernel code will not be significantly affected by the printk() call.
If for some reason you really want non-threaded printing, the patch we are discussing creates new functions to manually activate it:
printk_prefer_direct_enter();pr_info("debugging\n");printk_prefer_direct_exit();
But then your pr_info() will cause significant latencies. The timestamp would be the same, with or without direct printing.
John
Re: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 7 lines · John Ogness
Quoted 3 lines · Helge Deller
In my case - while I debug low-level kernel code - I then just need to use
pr_warn()orpr_emerg()and get it printed non-threadened. That's sufficient for me.Actually, no. The loglevel does not determine if a message is direct printed or not. By "warn" I was referring to
WARN_ON(condition).
Oh, then there was a misunderstanding on my side.
Quoted 4 lines
If you are debugging low-level kernel code, you usually will _want_ threaded printing. The timestamps match the
printk()call, so you will get accurate logs. And the runtime performance of your low-level kernel code will not be significantly affected by theprintk()call.
That really much depends on what you debug.
Currently I'm debugging some CPU hotplug stuff, and I really want my debug info printed immediately, otherwise it's too late to analyze what's going wrong. I can imaginge other use cases like trying to find memory leaks are similiar.
Quoted 6 lines
If for some reason you really want non-threaded printing, the patch we are discussing creates new functions to manually activate it:
printk_prefer_direct_enter();pr_info("debugging\n");printk_prefer_direct_exit();
That's quite unhandy. At least I would have thought that pr_emerg() would do that call sequence.
Wouldn't it make sense to make pr_emerg() work unthreadened, as it's used quite seldom and only in special situations...
Quoted 2 lines
But then your
pr_info()will cause significant latencies. The timestamp would be the same, with or without direct printing.
Yes, but that would be Ok in my case.
Helge
Petr Mladek
maintainerRe: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
Quoted 16 lines · Helge Deller
Quoted 7 lines · John Ogness
Quoted 3 lines · Helge Deller
In my case - while I debug low-level kernel code - I then just need to use
pr_warn()orpr_emerg()and get it printed non-threadened. That's sufficient for me.Actually, no. The loglevel does not determine if a message is direct printed or not. By "warn" I was referring to
WARN_ON(condition).Oh, then there was a misunderstanding on my side.
Quoted 4 lines
If you are debugging low-level kernel code, you usually will _want_ threaded printing. The timestamps match the
printk()call, so you will get accurate logs. And the runtime performance of your low-level kernel code will not be significantly affected by theprintk()call.
Quoted 5 lines
That really much depends on what you debug.
Currently I'm debugging some CPU hotplug stuff, and I really want my debug info printed immediately, otherwise it's too late to analyze what's going wrong. I can imaginge other use cases like trying to find memory leaks are similiar.
So, it is not about severity of the messages but about the context, in this case the suspend. We try to address this. 10th patch from this patchset adds:
static inline bool allow_direct_printing(void)
{
return (!printk_kthreads_available ||
system_state > SYSTEM_RUNNING ||
oops_in_progress ||
atomic_read(&printk_prefer_direct));
}It means that printk() inside the SYSTEM_SUSPEND state will automatically try to handle the console directly.
I intentionally mention "try" because printk() uses console_trylock(). It fails when anyone else already does the printing, including a kthread. trylock() is needed because printk() must be usable also in atomic context. It has worked this way for more than two decades. It came with SMP support.
Quoted 11 lines
Quoted 6 lines
If for some reason you really want non-threaded printing, the patch we are discussing creates new functions to manually activate it:
printk_prefer_direct_enter();pr_info("debugging\n");printk_prefer_direct_exit();That's quite unhandy. At least I would have thought that
pr_emerg()would do that call sequence.
Wouldn't it make sense to makepr_emerg()work unthreadened, as it's used quite seldom and only in special situations...
It is true that pr_emerg() is relatively special.
But I would really like to avoid a situation where developers use non-appropriate printk level just to get the message directly.
Instead, we should allow using the direct context in some situation easily. We could also add a command line parameter to disable the kthreads completely.
Best Regards,
Petr
Re: [PATCH printk v2 09/12] printk: add functions to prefer direct printing
In reply to John OgnessQuoted 32 lines · John Ogness
Once kthread printing is available, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using
pr_flush()to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case.Introduce
printk_prefer_direct_enter()andprintk_prefer_direct_exit()functions to explicitly (and globally) activate/deactivate preferred direct console printing. The term "direct console printing" refers to printing to all enabled consoles from the context of the printk caller. The term "prefer" is used because this type of printing is only best effort. If the console is currently locked or other printers are already actively printing, the printk caller will need to rely on the other contexts to handle the printing.This preferred direct printing is how all printing is currently handled (unless explicitly deferred).
When kthread printing is introduced, there may be some unanticipated problems due to kthreads being unable to flush important messages. In order to minimize such risks, preferred direct printing is activated for the primary important messages when the system experiences general types of major errors. These are:
- emergency reboot/shutdown
- cpu and rcu stalls
- hard and soft lockups
- hung tasks
- warn
- sysrq
Quoted 6 lines
Note that since kthread printing does not yet exist, no behavior changes result from this commit. This is only implementing the counter and marking the various places where preferred direct printing is active.
Signed-off-by John Ogness <[email protected]>
From an RCU perspective:
Quoted 304 lines
--- drivers/tty/sysrq.c | 2 ++ include/linux/printk.h | 11 +++++++++++ kernel/hung_task.c | 11 ++++++++++- kernel/panic.c | 4 ++++ kernel/printk/printk.c | 28 ++++++++++++++++++++++++++++ kernel/rcu/tree_stall.h | 2 ++ kernel/reboot.c | 14 +++++++++++++- kernel/watchdog.c | 4 ++++ kernel/watchdog_hld.c | 4 ++++ 9 files changed, 78 insertions(+), 2 deletions(-)drivers/tty/sysrq.c +2
-0@@ -578,6 +578,7 @@ void __handle_sysrq(int key, bool check_mask)rcu_sysrq_start ();rcu_read_lock (); +printk_prefer_direct_enter ();/* * Raise the apparent loglevel to maximum so that the sysrq header * is shown to provide the user with positive feedback. We do not @@ -619,6 +620,7 @@ void __handle_sysrq(int key, bool check_mask)pr_cont ("\n" ); console_loglevel = orig_log_level; } +printk_prefer_direct_exit ();rcu_read_unlock ();rcu_sysrq_end ();include/linux/printk.h +11
-0@@ -170,6 +170,9 @@ extern void __printk_safe_exit(void);#define printk_deferred_enter __printk_safe_enter#define printk_deferred_exit __printk_safe_exit +extern void printk_prefer_direct_enter (void ); +extern void printk_prefer_direct_exit (void ); +extern bool pr_flush (int timeout_ms,bool reset_on_progress);/* @@ -222,6 +225,14 @@ static inline void printk_deferred_exit(void) { } +static inline void printk_prefer_direct_enter (void ) +{ +} + +static inline void printk_prefer_direct_exit (void ) +{ +} +static inline bool pr_flush (int timeout_ms,bool reset_on_progress) {return true;kernel/hung_task.c +10
-1@@ -127,6 +127,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)* complain: */ if (sysctl_hung_task_warnings) { +printk_prefer_direct_enter (); +if (sysctl_hung_task_warnings >0 ) sysctl_hung_task_warnings--; pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",@@ -142,6 +144,8 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)if (sysctl_hung_task_all_cpu_backtrace) hung_task_show_all_bt = true; + +printk_prefer_direct_exit (); }touch_nmi_watchdog ();@@ -204,12 +208,17 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) } unlock:rcu_read_unlock (); -if (hung_task_show_lock) +if (hung_task_show_lock) { +printk_prefer_direct_enter ();debug_show_all_locks (); +printk_prefer_direct_exit (); + }if (hung_task_show_all_bt) { hung_task_show_all_bt = false; +printk_prefer_direct_enter ();trigger_all_cpu_backtrace (); +printk_prefer_direct_exit (); }if (hung_task_call_panic)kernel/panic.c +4
-0@@ -560,6 +560,8 @@ void __warn(const char *file, int line, void *caller, unsigned taint, {disable_trace_on_warning (); +printk_prefer_direct_enter (); +if (file) pr_warn("WARNING: CPU: %d PID: %d at %s:%d %pS\n", raw_smp_processor_id(), current->pid, file, line,@@ -597,6 +599,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 ); + +printk_prefer_direct_exit (); }#ifndef __WARN_FLAGSkernel/printk/printk.c +28
-0@@ -362,6 +362,34 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;static DEFINE_MUTEX (syslog_lock);#ifdef CONFIG_PRINTK +static atomic_t printk_prefer_direct =ATOMIC_INIT (0 ); + +/** +* printk_prefer_direct_enter - cause printk() calls to attempt direct +* printing to all enabled consoles +* +* Since it is not possible to call into the console printing code from any +* context, there is no guarantee that direct printing will occur. +* +* This globally effects all printk() callers. +* +* Context: Any context. +*/ +void printk_prefer_direct_enter (void ) +{ +atomic_inc (&printk_prefer_direct); +} + +/** +* printk_prefer_direct_exit - restore printk() behavior +* +* Context: Any context. +*/ +void printk_prefer_direct_exit (void ) +{ +WARN_ON (atomic_dec_if_positive (&printk_prefer_direct) <0 ); +} +DECLARE_WAIT_QUEUE_HEAD (log_wait);/* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ kernel/rcu/tree_stall.h +2
-0@@ -619,6 +619,7 @@ static void print_cpu_stall(unsigned long gps)* See Documentation/RCU/stallwarn.rst for info on how to debug * RCU CPU stall warnings. */ +printk_prefer_direct_enter ();trace_rcu_stall_warning (rcu_state.name,TPS ("SelfDetected" ));pr_err ("INFO: %s self-detected stall on CPU\n" , rcu_state.name);raw_spin_lock_irqsave_rcu_node (rdp->mynode, flags);@@ -656,6 +657,7 @@ static void print_cpu_stall(unsigned long gps)*/ set_tsk_need_resched (current);set_preempt_need_resched (); +printk_prefer_direct_exit (); }static void check_cpu_stall (struct rcu_data *rdp)kernel/reboot.c +13
-1@@ -447,9 +447,11 @@ static int __orderly_reboot(void) ret =run_cmd (reboot_cmd);if (ret) { +printk_prefer_direct_enter ();pr_warn ("Failed to start orderly reboot: forcing the issue\n" );emergency_sync ();kernel_restart (NULL ); +printk_prefer_direct_exit (); }return ret;@@ -462,6 +464,7 @@ static int __orderly_poweroff(bool force) ret =run_cmd (poweroff_cmd);if (ret && force) { +printk_prefer_direct_enter ();pr_warn ("Failed to start orderly shutdown: forcing the issue\n" );/* @@ -471,6 +474,7 @@ static int __orderly_poweroff(bool force)*/ emergency_sync ();kernel_power_off (); +printk_prefer_direct_exit (); }return ret;@@ -528,6 +532,8 @@ EXPORT_SYMBOL_GPL(orderly_reboot);*/ static void hw_failure_emergency_poweroff_func (struct work_struct *work) { +printk_prefer_direct_enter (); +/* * We have reached here after the emergency shutdown waiting period has * expired. This means orderly_poweroff has not been able to shut off @@ -544,6 +550,8 @@ static void hw_failure_emergency_poweroff_func(struct work_struct *work)*/ pr_emerg ("Hardware protection shutdown failed. Trying emergency restart\n" );emergency_restart (); + +printk_prefer_direct_exit (); } static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work,@@ -582,11 +590,13 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced) {static atomic_t allow_proceed =ATOMIC_INIT (1 ); +printk_prefer_direct_enter (); +pr_emerg ("HARDWARE PROTECTION shutdown (%s)\n" , reason);/* Shutdown should be initiated only once. */ if (!atomic_dec_and_test (&allow_proceed)) -return ; + goto out;/* * Queue a backup emergency shutdown in the event of @@ -594,6 +604,8 @@ void hw_protection_shutdown(const char *reason, int ms_until_forced)*/ hw_failure_emergency_poweroff (ms_until_forced);orderly_poweroff (true); +out: +printk_prefer_direct_exit (); }EXPORT_SYMBOL_GPL (hw_protection_shutdown);kernel/watchdog.c +4
-0@@ -424,6 +424,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)/* Start period for the next softlockup warning. */ update_report_ts (); +printk_prefer_direct_enter (); +pr_emerg ("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n" ,smp_processor_id (), duration, current->comm,task_pid_nr (current));@@ -442,6 +444,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)add_taint (TAINT_SOFTLOCKUP ,LOCKDEP_STILL_OK );if (softlockup_panic)panic ("softlockup: hung tasks" ); + +printk_prefer_direct_exit (); }return HRTIMER_RESTART ;kernel/watchdog_hld.c +4
-0@@ -135,6 +135,8 @@ static void watchdog_overflow_callback(struct perf_event *event,if (__this_cpu_read (hard_watchdog_warn) == true)return ; +printk_prefer_direct_enter (); +pr_emerg ("Watchdog detected hard LOCKUP on cpu %d\n" , this_cpu);print_modules ();@@ -155,6 +157,8 @@ static void watchdog_overflow_callback(struct perf_event *event,if (hardlockup_panic)nmi_panic (regs,"Hard LOCKUP" ); +printk_prefer_direct_exit (); +__this_cpu_write (hard_watchdog_warn, true);return ; }Signature · 1 line
2.30.2
John Ogness
maintainerPATCH v2 10/12 printk: add kthread console printers
In reply to John OgnessCreate a kthread for each console to perform console printing. During normal operation (@system_state == SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.
During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irq work triggered by the printk caller.
Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.
--- include/linux/console.h | 2 + kernel/printk/printk.c | 262 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 252 insertions(+), 12 deletions(-)
include/linux/console.h +2 -0
@@ -153,6 +153,8 @@ struct console {uint ospeed;u64 seq;unsigned long dropped; +struct task_struct *thread; +void *data;struct console *next; };
kernel/printk/printk.c +250 -12
378 lines not on this page · raw message
Signature · 1 line
2.30.2
Petr Mladek
maintainerstart/stop: was: Re: [PATCH printk v2 10/12] printk: add kthread console printers
Quoted 17 lines · John Ogness
Create a kthread for each console to perform console printing. During normal operation (@system_state ==
SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irq work triggered by the printk caller.Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.
kernel/printk/printk.c +0
-0
kernel/printk/printk.c void console_start(struct console *console) · line 3033
Quoted 6 lines
kernel/printk/printk.c +3
-0@@ -2988,6 +3028,10 @@ void console_start(struct console *console)console_lock (); console->flags |=CON_ENABLED ;console_unlock (); + +/* Wake the newly enabled kthread printer. */ +wake_up_klogd ();
wake_up_klogd() will wake the kthread only when PRINTK_PENDING_OUTPUT is set. But this particular console might be far behind the other kthreads.
I think that we always want to wake up the kthread. In the worst case, it will just realize that there is nothing to do and will go back to sleep.
kernel/printk/printk.c void console_start(struct console *console) · line 3037
Quoted 4 lines
kernel/printk/printk.c +1
-0+__pr_flush (console,1000 , true); }EXPORT_SYMBOL (console_start);
@@ -3196,6 +3240,8 @@ void register_console(struct console *newcon)
Quoted 17 lines
kernel/printk/printk.c +3
-0/* Begin with next message. */ newcon->seq =prb_next_seq (prb); } +if (printk_kthreads_available) +printk_start_kthread (newcon);console_unlock ();console_sysfs_notify ();@@ -3222,6 +3268,7 @@ EXPORT_SYMBOL(register_console);int unregister_console (struct console *console) { +struct task_struct *thd = console->thread;struct console *con;int res;
kernel/printk/printk.c int unregister_console(struct console *console) · line 3285
Quoted 6 lines
kernel/printk/printk.c +3
-0@@ -3233,6 +3280,11 @@ int unregister_console(struct console *console)if (res >0 )return 0 ; +if (thd) { + console->thread =NULL ; +kthread_stop (thd);
This feels racy against parallel register_console() and unregister_console() calls. I guess that it is not much realistic in the real life but ...
Anyway, printk_start_kthread(newcon) is called under console_lock(). It would be great to stop it under the lock as well so that we do not need to worry about races.
Hmm, it might cause deadlock at this stage because the kthread take console_lock() in the main loop.
It should work after introducing the per-console mutexes. They might be taken when console_lock() is taken. But the kthread will not do the printing when the console_lock() is taken by anyone else.
A solution might be to add a comment that there is a non-realistic race that will be removed by introducing per-console mutexes. And fix it later.
Nit: Also I would prefer to create printk_stop_kthread()
so that it is easy to find the counterparts.
Quoted 5 lines
kernel/printk/printk.c +2
-0+ } + res = -ENODEV ;console_lock ();if (console_drivers == console) {
[...]
Quoted 42 lines
kernel/printk/printk.c +42
-0+static int printk_kthread_func (void *data) +{ +struct console *con = data; +char *dropped_text =NULL ; +char *ext_text =NULL ; +bool handover; +u64 seq =0 ; +char *text; +int error; + + text =kmalloc (CONSOLE_LOG_MAX ,GFP_KERNEL ); +if (!text) { +printk_console_msg (con,KERN_ERR ,"failed to allocate text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + +if (con->flags &CON_EXTENDED ) { + ext_text =kmalloc (CONSOLE_EXT_LOG_MAX ,GFP_KERNEL ); +if (!ext_text) { +printk_console_msg (con,KERN_ERR ,"failed to allocate ext_text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + }else { + dropped_text =kmalloc (DROPPED_TEXT_MAX ,GFP_KERNEL ); +if (!dropped_text) { +printk_console_msg (con,KERN_ERR , +"failed to allocate dropped_text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + } + +printk_console_msg (con,KERN_INFO ,"printing thread started" ); + +for (;;) { +/* +* Guarantee this task is visible on the waitqueue before +* checking the wake condition. +* +* The full memory barrier within set_current_state() of
kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3593
Quoted 10 lines
kernel/printk/printk.c +10
-0+* prepare_to_wait_event() pairs with the full memory barrier +* within wq_has_sleeper(). +* +* See __wake_up_klogd:A for the pairing memory barrier. +*/ + error =wait_event_interruptible (log_wait, +printer_should_wake (con, seq));/* LMM(printk_kthread_func:A) */ + +if (kthread_should_stop () || !printk_kthreads_available) +break ;
printk_fallback_preferred_direct() is an interesting trick. But I am not sure if it is a good idea. This approach leaves con->kthread pointing to exited kthreads.
The following looks cleaner and should work:
void printk_stop_kthread(struct console *con)
{
struct task_struct *thread = con->thread;
if (!thread)
return;
console->thread = NULL;
kthread_stop(thread);
}void printk_deactivate_kthreas()
{
struct console *con;
console_lock();
printk_kthreads_available = false;
console_unlock();
for_each_console(con)
printk_stop_kthread(con);
}And we could move printk_stop_kthread() under console lock after introducing the per-console mutex.
Best Regards,
Petr
Petr Mladek
maintainerconsole_is_usable() check: was: Re: [PATCH printk v2 10/12] printk: add kthread console printers
In reply to John OgnessQuoted 16 lines · John Ogness
Create a kthread for each console to perform console printing. During normal operation (@system_state ==
SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irq work triggered by the printk caller.Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.
kernel/printk/printk.c +2
-0+static bool printer_should_wake (struct console *con,u64 seq) +{
kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3524
Quoted 10 lines
kernel/printk/printk.c +10
-0+short flags; + +if (kthread_should_stop () || !printk_kthreads_available) +return true; + +if (console_suspended) +return false; + +if (!con->write) +return false;
Hmm, the kthread for such consoles will never wake up. It probably does not make sense to create it at all.
On the other hand, it is not a big deal. And we have "bigger" problem how to make these checks in sync with console_is_usable(), see below.
Quoted 73 lines
kernel/printk/printk.c +73
-0+/* +* This is an unsafe read to con->flags, but a false positive is not +* a problem. Worst case it would allow the printer to wake up even +* when it is disabled. But the printer will notice that itself when +* attempting to print and instead go back to sleep. +*/ + flags =data_race (READ_ONCE (con->flags)); +if (!(flags &CON_ENABLED )) +return false; + +if (atomic_read (&printk_prefer_direct)) +return false; + +return prb_read_valid (prb, seq,NULL ); +} + +static int printk_kthread_func (void *data) +{ +struct console *con = data; +char *dropped_text =NULL ; +char *ext_text =NULL ; +bool handover; +u64 seq =0 ; +char *text; +int error; + + text =kmalloc (CONSOLE_LOG_MAX ,GFP_KERNEL ); +if (!text) { +printk_console_msg (con,KERN_ERR ,"failed to allocate text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + +if (con->flags &CON_EXTENDED ) { + ext_text =kmalloc (CONSOLE_EXT_LOG_MAX ,GFP_KERNEL ); +if (!ext_text) { +printk_console_msg (con,KERN_ERR ,"failed to allocate ext_text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + }else { + dropped_text =kmalloc (DROPPED_TEXT_MAX ,GFP_KERNEL ); +if (!dropped_text) { +printk_console_msg (con,KERN_ERR , +"failed to allocate dropped_text buffer" ); +printk_fallback_preferred_direct (); + goto out; + } + } + +printk_console_msg (con,KERN_INFO ,"printing thread started" ); + +for (;;) { +/* +* Guarantee this task is visible on the waitqueue before +* checking the wake condition. +* +* The full memory barrier within set_current_state() of +* prepare_to_wait_event() pairs with the full memory barrier +* within wq_has_sleeper(). +* +* See __wake_up_klogd:A for the pairing memory barrier. +*/ + error =wait_event_interruptible (log_wait, +printer_should_wake (con, seq));/* LMM(printk_kthread_func:A) */ + +if (kthread_should_stop () || !printk_kthreads_available) +break ; + +if (error) +continue ; + +console_lock ();
kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3608
Quoted 10 lines
kernel/printk/printk.c +10
-0+ +if (console_suspended) { +__console_unlock (); +continue ; + } + +if (!console_is_usable (con)) { +__console_unlock (); +continue ; + }
This smells with a busy loop. We should make sure that the same condition will make printk_kthread_func() return false. The current approach is hard to maintain.
Hmm, it is not easy because console_is_usable(con) is supposed to be called under console_lock().
I do not have a good solution for this. But the current approach looks error prone. What about the following?
static inline bool __console_is_usable(struct console *con)
{
short flags;
if (!con->write)
return false;
/* Make flags checks consistent when called without console_lock. */
flags = READ_ONCE(con->flags);
if (!(con->flags & CON_ENABLED))
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()) &&
!(con->flags & CON_ANYTIME))
return false;
return true;
}static inline bool console_is_usable(struct console *con)
{
WARN_ON_ONCE(!lockdep_assert_held(&console_sem));
__console_is_usable();
}Note that we could not use lockdep_assert_held() because we will later need to check both console_sem and con->mutex. Either of them will be enough.
Quoted 16 lines
kernel/printk/printk.c +16
-0+ +/* +* Even though the printk kthread is always preemptible, it is +* still not allowed to call cond_resched() from within +* console drivers. The task may become non-preemptible in the +* console driver call chain. For example, vt_console_print() +* takes a spinlock and then can call into fbcon_redraw(), +* which can conditionally invoke cond_resched(). +*/ + console_may_schedule =0 ; +console_emit_next_record (con, text, ext_text, dropped_text, &handover); +if (handover) +continue ; + + seq = con->seq; +
kernel/printk/printk.c bool pr_flush(int timeout_ms, bool reset_on_progress) · line 3634
Quoted 10 lines
kernel/printk/printk.c +10
-0+__console_unlock (); + } + +printk_console_msg (con,KERN_INFO ,"printing thread stopped" ); +out: +kfree (dropped_text); +kfree (ext_text); +kfree (text); +return 0 ; +}
Best Regards,
Petr
Petr Mladek
maintainerwake_up_klogd(): was: Re: [PATCH printk v2 10/12] printk: add kthread console printers
In reply to John OgnessQuoted 15 lines · John Ogness
Create a kthread for each console to perform console printing. During normal operation (@system_state ==
SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irq work triggered by the printk caller.Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3662
Quoted 4 lines
kernel/printk/printk.c +1
-0@@ -3445,40 +3659,64 @@ static void wake_up_klogd_work_func(struct irq_work *irq_work)int pending =this_cpu_xchg (printk_pending,0 );if (pending &PRINTK_PENDING_OUTPUT ) { +printk_prefer_direct_enter ();
Please, rename PRINTK_PENDING_OUTPUT to PRINTK_PENDING_DIRECT_OUTPUT.
The change confused me a lot. I wrote many lines about why it is (not) needed. Then I continued the review and found that it started making sense after the change in defer_console_output(). ;-)
This patch changes the meaning of the flag. It would deserve renaming.
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3672
Quoted 11 lines
kernel/printk/printk.c +4
-1+/* If trylock fails, someone else is doing the printing */ if (console_trylock ())console_unlock (); + +printk_prefer_direct_exit (); }if (pending &PRINTK_PENDING_WAKEUP ) -wake_up_interruptible (&log_wait); +wake_up_interruptible_all (&log_wait);
This would deserve some explanation in the commit message. I think that this actually was needed even before. IMHO, more pending waiters had to wait for more new messages. It was not guaranteed the the woken waiter woke another one.
It would be nice to put it into separate patch and do it first.
Quoted 15 lines
kernel/printk/printk.c +2
-3} static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =IRQ_WORK_INIT_LAZY (wake_up_klogd_work_func); -void wake_up_klogd (void ) +static void __wake_up_klogd (int val) {if (!printk_percpu_data_ready ())return ;preempt_disable (); -if (waitqueue_active (&log_wait)) { -this_cpu_or (printk_pending,PRINTK_PENDING_WAKEUP ); +/*
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3694
Quoted 10 lines
kernel/printk/printk.c +10
-0+* Guarantee any new records can be seen by printing threads before +* checking if the wait queue is empty. +* +* The full memory barrier within wq_has_sleeper() pairs with the full +* memory barrier within set_current_state() of +* prepare_to_wait_event(), which is called after ___wait_event() adds +* the waiter but before it has checked the wait condition. +* +* See printk_kthread_func:A for the pairing memory barrier. +*/
I guess that this problem was there even before but it was less visible. Do I get it correctly, please?
Quoted 7 lines
kernel/printk/printk.c +3
-0+if (wq_has_sleeper (&log_wait) ||/* LMM(__wake_up_klogd:A) */ + (val &PRINTK_PENDING_OUTPUT )) { +this_cpu_or (printk_pending, val);irq_work_queue (this_cpu_ptr (&wake_up_klogd_work)); }preempt_enable (); }
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3476
Quoted 10 lines
kernel/printk/printk.c +5
-2+void wake_up_klogd (void ) +{ +__wake_up_klogd (PRINTK_PENDING_WAKEUP ); +} +void defer_console_output (void ) { -if (!printk_percpu_data_ready ()) -return ;
/* Always wakeup waiters because there are not only printk kthreads. */
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3710
Quoted 1 line
kernel/printk/printk.c +1
-0+int val =PRINTK_PENDING_WAKEUP ;
IMHO, this was actually needed even before. Otherwise, nobody woken log waiters for deferred messages.
IMHO, it is a regression caused by moving wake_up_klogd() from console_unlock() to vprintk_emit(). It was the commit 43a17111c2553925f6 ("printk: wake up klogd in vprintk_emit").
If I am right then we should fix it separately.
kernel/printk/printk.c static void wake_up_klogd_work_func(struct irq_work *irq_work) · line 3720
Quoted 13 lines
kernel/printk/printk.c +8
-4-preempt_disable (); -this_cpu_or (printk_pending,PRINTK_PENDING_OUTPUT ); -irq_work_queue (this_cpu_ptr (&wake_up_klogd_work)); -preempt_enable (); +/* +* If console deferring was called with preferred direct printing, +* make the irqwork perform the direct printing. +*/ +if (atomic_read (&printk_prefer_direct)) + val |=PRINTK_PENDING_OUTPUT ; + +__wake_up_klogd (val); }
And we should call defer_console_output() also in printk_prefer_direct_enter() to make sure that a potential pending messages are printed.
My understanding is that the kthreads stop processing the messages when the direct output is preferred. I think that this was not the case in v1 and I asked you do it this way so that kthreads do not block entering the direct mode.
Best Regards,
Petr
Petr Mladek
maintainerconsole_flush_all(): was: : was Re: [PATCH printk v2 10/12] printk: add kthread console printers
In reply to John OgnessQuoted 23 lines · John Ogness
Create a kthread for each console to perform console printing. During normal operation (@system_state ==
SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles.During non-normal operation, console printing is done as it has been:
within the context of the printk caller or within irq work triggered by the printk caller.Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed.
Signed-off-by John Ogness <[email protected]>--- include/linux/console.h | 2 + kernel/printk/printk.c | 262 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 252 insertions(+), 12 deletions(-)include/linux/console.h +0
-0
include/linux/console.h static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove · line 2814
Quoted 6 lines
include/linux/console.h +3
-0@@ -2773,6 +2809,10 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove *handover = false; do { + /* Let the kthread printers do the work if they can. */ + if (!allow_direct_printing()) + break;
We should return either "true" or "false" here to have predictable result.
I suggest to return "false". I would mean that the function did not finish the job as I suggested for the 6th patch.
include/linux/console.h static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove · line 2818
Quoted 4 lines
include/linux/console.h +1
-0+ any_progress = false;for_each_console (con) {
Best Regards,
Petr
John Ogness
maintainerPATCH v2 11/12 printk: extend console_lock for proper kthread support
In reply to John OgnessCurrently threaded console printers synchronize against each other using console_lock(). However, different console drivers are unrelated and do not require any synchronization between each other. Removing the synchronization between the threaded console printers will allow each console to print at its own speed.
But the threaded consoles printers do still need to synchronize against console_lock() callers. Introduce a per-console mutex and a new console flag CON_THD_BLOCKED to provide this synchronization.
console_lock() is modified so that it must acquire the mutex of each console in order to set the CON_THD_BLOCKED flag. Console printing threads will acquire their mutex while printing a record. If CON_THD_BLOCKED was set, the thread will go back to sleep instead of printing.
The reason for the CON_THD_BLOCKED flag is so that console_lock() callers do not need to acquire multiple console mutexes simultaneously, which would introduce unnecessary complexity due to nested mutex locking.
However, threaded console printers also need to synchronize against console_trylock() callers. Since console_trylock() may be called from any context, the per-console mutex cannot be used for this synchronization. (mutex_trylock() cannot be called from atomic contexts.) Introduce a global atomic counter to identify if any threaded printers are active. The threaded printers will also check the atomic counter to identify if the console has been locked by another task via console_trylock().
Note that @console_sem is still used to provide synchronization between console_lock() and console_trylock() callers.
A locking overview for console_lock(), console_trylock(), and the threaded printers is as follows (pseudo code):
console_lock()
{
down(&console_sem);
for_each_console(con) {
mutex_lock(&con->lock);
con->flags |= CON_THD_BLOCKED;
mutex_unlock(&con->lock);
}
/* console lock acquired */
}console_trylock()
{
if (down_trylock(&console_sem) == 0) {
if (atomic_cmpxchg(&console_kthreads_active, 0, -1) == 0) {
/* console lock acquired */
}
}
}threaded_printer()
{
mutex_lock(&con->lock);
if (!(con->flags & CON_THD_BLOCKED)) {
/* console_lock() callers blocked */
if (atomic_inc_unless_negative(&console_kthreads_active)) {
/* console_trylock() callers blocked */
con->write();
atomic_dec(&console_lock_count);
}
}
mutex_unlock(&con->lock);
}The console owner and waiter logic now only applies between contexts that have taken the console lock. Since threaded printers never take the console lock, they do not have a console lock to handover to other contexts.
@panic_console_dropped must change to atomic_t since it is no longer protected exclusively by the console lock.
Since threaded printers remain asleep if they see that the console is locked, they now must be explicitly woken in __console_unlock(). This means wake_up_klogd() calls following a console_unlock() are no longer necessary and are removed.
--- include/linux/console.h | 15 +++ kernel/printk/printk.c | 243 ++++++++++++++++++++++++++++++---------- 2 files changed, 199 insertions(+), 59 deletions(-)
include/linux/console.h +15 -0
@@ -16,6 +16,7 @@#include <linux/atomic.h> #include <linux/types.h> +#include <linux/mutex.h> struct vc_data ;struct console_font_op ;
@@ -136,6 +137,7 @@ static inline int con_debug_leave(void)#define CON_ANYTIME (16)/* Safe to call when cpu is offline */ #define CON_BRL (32)/* Used for a braille device */ #define CON_EXTENDED (64)/* Use the extended output format a la /dev/kmsg */ +#define CON_THD_BLOCKED (128)/* Thread blocked because console is locked */ struct console {char name[16 ];
@@ -155,6 +157,19 @@ struct console {unsigned long dropped;struct task_struct *thread; +/* +* The per-console lock is used by printing kthreads to synchronize +* this console with callers of console_lock(). This is necessary in +* order to allow printing kthreads to run in parallel to each other, +* while each safely accessing their own @flags and synchronizing +* against direct printing via console_lock/console_unlock. +* +* Note: For synchronizing against direct printing via +* console_trylock/console_unlock, see the static global +* variable @console_kthreads_active. +*/ +struct mutex lock; +void *data;struct console *next; };
kernel/printk/printk.c +184 -59
401 lines not on this page · raw message
Signature · 1 line
2.30.2
Petr Mladek
maintainerguarantee forward progress: was: Re: [PATCH printk v2 11/12] printk: extend console_lock for proper kthread support
Quoted 23 lines · John Ogness
Currently threaded console printers synchronize against each other using
console_lock(). However, different console drivers are unrelated and do not require any synchronization between each other. Removing the synchronization between the threaded console printers will allow each console to print at its own speed.But the threaded consoles printers do still need to synchronize against
console_lock()callers. Introduce a per-console mutex and a new console flagCON_THD_BLOCKEDto provide this synchronization.kernel/printk/printk.c +5
-0@@ -2691,6 +2760,22 @@ static inline bool console_is_usable(struct console *con)static void __console_unlock (void ) { console_locked =0 ; + +/* +* Depending on whether console_lock() or console_trylock() was used, +* appropriately allow the kthread printers to continue. +*/
kernel/printk/printk.c static inline bool console_is_usable(struct console *con) · line 2777
Quoted 10 lines
kernel/printk/printk.c +10
-0+if (console_kthreads_blocked) +console_kthreads_unblock (); +else +console_kthreads_atomic_unblock (); + +/* +* New records may have arrived while the console was locked. +* Wake the kthread printers to print them. +*/ +wake_up_klogd ();
Thinking loudly:
Strictly speaking, this is needed only when kthreads are allowed. It might make sense to do:
if (!allow_direct_printing()) wake_up_klogd();
On the other hand, the check is racy. So it might be better to always call wake_up_klogd() and do not worry about it. The optimization probably is not worth it.
But this brings the question. How do we actually ensure that either kthreads or direct printk will handle pending messages.
vprintk_emit() or defer_console_output() make sure that either direct printkg is used or kthreads are waken. It makes the decision via the current state of allow_direct_printing(). But it might change in the meantime.
allow_direct_printing() might change in both directions:
1. printk trigger direct -> kthreads enabled when in direct mode.
This switch might be handled by the above wake_up_klogd().
If it is non-conditional, it might handle also racy mutiple direct -> kthread -> direct mode swtiches.
2. printk wakes kthreads -> direct mode allowed when there are still pending messages.
I am afraid that we do not have this path well covered.
It probably worked "better" in v1 where the kthreads continued handling the messages until all were flushed. But I guess that there were still races when new messages appeared after some kthreads finished the job.
Hmm, it actually is not enough to call defer_console_output() in printk_prefer_direct_enter(). It is because wake_up_klogd_work_func() does console_trylock(). It might still be blocked by an active kthread. But the other kthreads might already sleep.
I think that we actually need to call defer_console_output() after kthread() unblocks console_trylock() and
the direct mode is preferred(). I mean something like:
static int printk_kthread_func(void *data)
{
[...]
for (;;) {
[...]
console_emit_next_record(con, text, ext_text, dropped_text);
seq = con->seq;
console_kthread_printing_exit();+ /*
+ * This thread does not longer block console_trylock(). Make
+ * sure that someone will try to flush pending
+ * messages in the direct mode when it has got preferred
+ * in the meantime.
+ */
+ if (allow_direct_printing())
+ deffer_console_output();
+
mutex_unlock(&con->lock); }
[...]
}
Hmm, it is still not enough. We should do it also in printer_should_wake(). It will be needed when the original printk() woke kthreads and the direct mode got preferred before the kthread started printing. In this case, the kthreads will do nothing and nobody will handle the pending messages.
I played a lot with it and it is really hard because:
+ new messages can appear anytime
+ direct mode might get requested anytime
+ only the direct mode knows whether all messages were flushed on all consoles
IMHO, ideal logic would be:
+ new messages will always trigger direct printing or wake kthreads, newer both
+ direct mode will wake kthreads when kthreads are allowed and there are penging messages.
+ (the last) kthread will wake_up direct mode when it is requested and there are pending messages.
We could use the trick that the direct mode never gives up when there are pending messages.
It requires moving printk kthreads into a separate waitqueue:
DECLARE_WAIT_QUEUE_HEAD(printk_kthread_wait);
I suggest to rename the flags in the wake_up_klogd() API
/*
* Delayed printk version, for scheduler-internal messages:
*/
#define PRINTK_WAKE_UP_LOG 0x01
#define PRINTK_WAKE_UP_KTHREADS 0x02
#define PRINTK_PENDING_DIRECT_OUTPUT 0x04
static DEFINE_PER_CPU(int, printk_pending);
static void wake_up_klogd_work_func(struct irq_work *irq_work)
{
int pending = this_cpu_xchg(printk_pending, 0);
if (pending & PRINTK_PENDING_DIRECT_OUTPUT) {
printk_prefer_direct_enter();
/* If trylock fails, someone else is doing the printing */
if (console_trylock())
console_unlock();
printk_prefer_direct_exit();
}
if (pending & PRINTK_WAKEUP_LOG)
wake_up_interruptible_all(&log_wait);
if (pending & PRINTK_WAKEUP_KTHREADS)
wake_up_interruptible_all(&printk_kthread_wait);
}static DEFINE_PER_CPU(struct irq_work, printk_wake_up_work) =
IRQ_WORK_INIT_LAZY(wake_up_klogd_work_func);
static void __printk_wake_up(int val)
{
if (!printk_percpu_data_ready())
return;
preempt_disable();
/*
* Guarantee any new records can be seen by printing threads before
* checking if the wait queue is empty.
*
* The full memory barrier within wq_has_sleeper() pairs with the full
* memory barrier within set_current_state() of
* prepare_to_wait_event(), which is called after ___wait_event() adds
* the waiter but before it has checked the wait condition.
*
* See printk_kthread_func:A for the pairing memory barrier.
*/
if (wq_has_sleeper(&log_wait) || /* LMM(__printk_wake_up:A) */
wq_has_sleeper(&printk_kthread_wait) || /* LMM(__printk_wake_up:B) */
(val & PRINTK_PENDING_DIRECT_OUTPUT)) {
this_cpu_or(printk_pending, val);
irq_work_queue(this_cpu_ptr(&printk_wake_up_work));
}
preempt_enable();
}void printk_wake_up_klogd(void)
{
__wake_up_klogd(PRINTK_WAKE_UP_LOG);
}void printk_wake_up_all(void)
{
__wake_up_klogd(PRINTK_WAKE_UP_LOG || PRITNK_WAKE_UP_KTHREADS);
}void defer_console_output(void)
{
int val = PRINTK_WAKE_UP_LOG;
/*
* If console deferring was called with preferred direct printing,
* make the irqwork perform the direct printing.
*/
if (atomic_read(&printk_prefer_direct))
val |= PRINTK_PENDING_DIRECT_OUTPUT;
else
val |= PRINTK_WAKE_UP_KTHREADS;
__wake_up_klogd(val);
}And do
asmlinkage int vprintk_emit(int facility, int level,
const struct dev_printk_info *dev_info, const char *fmt, va_list args)
{
[...]
/* If called from the scheduler, we can not call up(). */
if (!in_sched && allow_direct_printing()) {
preempt_disable();
if (console_trylock_spinning())
console_unlock();
preempt_enable();
printk_wake_up_klogd();
} else {
printk_wake_up_all();
}
return printed_len;
}static bool printer_should_wake(struct console *con, u64 seq) {
[...]
if (__allow_direct_printing()) {
/* Make sure that someone does the direct printing. */
if (rb_read_valid(prb, next_seq, NULL))
defer_console_output();
return false;
}[...]
}
void console_unlock(void)
{
bool do_cond_resched;
bool handover;
bool flushed;
u64 next_seq;
if (console_suspended) {
up_console_sem();
return;
}
/*
* Console drivers are called with interrupts disabled, so
* @console_may_schedule should be cleared before; however, we may
* end up dumping a lot of lines, for example, if called from
* console registration path, and should invoke cond_resched()
* between lines if allowable. Not doing so can cause a very long
* scheduling stall on a slow console leading to RCU stall and
* softlockup warnings which exacerbate the issue with more
* messages practically incapacitating the system. Therefore, create
* a local to use for the printing loop.
*/
do_cond_resched = console_may_schedule;
do {
console_may_schedule = 0;
if (!any_usable_console()) {
__console_unlock();
break;
}
flushed = console_flush_all(do_cond_resched, &next_seq, &handover);
if (handover)
return;
__console_unlock();
/* Any pending messages? */
if (!prb_read_valid(prb, next_seq, NULL))
break ;
if (!allow_direct_printing()) {
/*
* Make sure that kthreads take over handling
* the pending messages.
*/
printk_wake_up_kthreads();
break;
}
/* Only panic CPU should handle consoles. */
if (abandon_console_lock_in_panic())
break;
WARN_ONCE(!flushed,
"The consoles were not flused in the direct more from unknown reasons\n");
/*
* Try to continue with direct printing. If the trylock fails,
* another context is already handling the printing. It is
* responsible for flushing everything.
*/
} while console_trylock();
}Note that I have added back any_usable_console(). console_flush_all() might fail from different reasons. Each reason needs its own handling. And we need to know that we handled all and use WARN_ONCE().
Uff, I have spent few hours thinking about it and reworked it several times. The above is the best solution that I was able to come up with.
I send what I have now because I feel tired and do not want to sit on it. But it is possible that I just have got lost and will see it on Monday.
kernel/printk/printk.c static inline bool console_is_usable(struct console *con) · line 2781
Quoted 3 lines
kernel/printk/printk.c +0
-0up_console_sem (); }
Alternative solution would be to go back to v1 and allow kthreads handling the messages even when direct more is preferred. The direct mode would be used only when console_trylock() succeeds or when some code calls console_lock().
Best Regards,
Petr
John Ogness
maintainerRe: guarantee forward progress: was: Re: [PATCH printk v2 11/12] printk: extend console_lock for proper kthread support
Quoted 6 lines · Petr Mladek
I played a lot with it and it is really hard because:
+ new messages can appear anytime
+ direct mode might get requested anytime
+ only the direct mode knows whether all messages were flushed on all consoles
Yes, and this is why v1 dramatically simplified the picture by making kthreads not care about direct mode. In v1 the kthread logic is very
simple: If there are messages to print, try to print them no matter what. We didn't need to worry if someone was printing, because we knew that at least the kthread was always printing.
This meant that there would be times when direct mode is active but the kthreads are doing the printing. But in my experimenting, that tends to be the case anyway, even with this more complex v2 approach. The reason is that if some code does:
printk_prefer_direct_enter();
(100 lines of printk calls)printk_prefer_direct_exit();
And directly before that printk_prefer_direct_enter() _any_ kthread was already inside call_console_driver(), then _all_ the console_trylock() calls of the above 100 printk's will fail. Inserting messages into the ringbuffer is fast and any active printer will not have finished printing its message before the above code snippet is done.
In fact, the above snippet will only do direct printing if there were previously no unflushed messages. That is true for v1 (by design) and v2 (by misfortune, because ringbuffer insertion is much faster than a single call_console_driver() call).
This new idea (v2) of trying to stop kthreads in order to "step aside" for direct printing is really just adding a lot of complexity, a lot of irqwork calls, and a lot of races. And with my experimenting I am not seeing any gain, except for new risks of nobody printing.
I understand that when we say printk_prefer_direct_enter() that we _really_ want to do direct printing. But we cannot force it if any printer is already inside call_console_driver(). In that case, direct printing simply will not and cannot happen.
For v3 I recommend going back to the v1 model, where kthreads do not care if direct mode is preferred. I claim that v2 does yield any more actual direct printing than v1 did.
However, I would keep the v2 change that kthreads go into their wait_event check after every message. That at least provides earlier responses for kthreads to stop themselves if they are disabled.
Once we have atomic consoles, things will look different. Then we perform true synchronous direct printing. But without them, the "prefer" in printk_prefer_direct_enter() is only a preference that can only be satisfied under ideal situations (i.e. no kthread is inside call_console_driver()).
John
Petr Mladek
maintainerRe: guarantee forward progress: was: Re: [PATCH printk v2 11/12] printk: extend console_lock for proper kthread support
Quoted 23 lines · John Ogness
Quoted 6 lines · Petr Mladek
I played a lot with it and it is really hard because:
+ new messages can appear anytime
+ direct mode might get requested anytime
+ only the direct mode knows whether all messages were flushed on all consolesYes, and this is why v1 dramatically simplified the picture by making kthreads not care about direct mode. In v1 the kthread logic is very
simple: If there are messages to print, try to print them no matter what. We didn't need to worry if someone was printing, because we knew that at least the kthread was always printing.This meant that there would be times when direct mode is active but the kthreads are doing the printing. But in my experimenting, that tends to be the case anyway, even with this more complex v2 approach. The reason is that if some code does:
printk_prefer_direct_enter();
(100 lines of printk calls)printk_prefer_direct_exit();
Quoted 5 lines
And directly before that
printk_prefer_direct_enter()_any_ kthread was already insidecall_console_driver(), then _all_ theconsole_trylock()calls of the above 100 printk's will fail. Inserting messages into the ringbuffer is fast and any active printer will not have finished printing its message before the above code snippet is done.
Good to know.
Quoted 4 lines
In fact, the above snippet will only do direct printing if there were previously no unflushed messages. That is true for v1 (by design) and v2 (by misfortune, because ringbuffer insertion is much faster than a single
call_console_driver()call).
Yup.
Quoted 9 lines
This new idea (v2) of trying to stop kthreads in order to "step aside" for direct printing is really just adding a lot of complexity, a lot of irqwork calls, and a lot of races. And with my experimenting I am not seeing any gain, except for new risks of nobody printing.
I understand that when we say
printk_prefer_direct_enter()that we _really_ want to do direct printing. But we cannot force it if any printer is already insidecall_console_driver(). In that case, direct printing simply will not and cannot happen.
I think that we should split it into situations when we need the direct printk and where we prefer the direct printing.
1. The direct printing is needed in situations when the kthreads can't work by design, e.g. early boot, panic, suspend, reboot.
This was the reason why I wanted to increase the chance that kthreads would not block it. But as you write above, it does not help much. The consoles are so slow that the direct mode is not used.
It is clear that my proposal does not work. And it is not reliable anyway. This patchset actually takes care of some situations much better way, by calling pr_flush() in console_stop() or suspend_console().
2. The direct printing is only preferred in some situations where the system is in troubles, for example, stall reports.
The direct mode is preferred because we think that it will be more reliable. It is a conservative thinking. But in fact, the kthreads might provide better results in many situations.
These stall reports often print a lot of debugging information. The direct mode might cause soft-lockups and stalls on its own. The kthreads allow showing the messages faster on fast consoles.
I could imagine that we actually remove the direct mode for these stall reports in the future.
Anyway, it is perfectly fine to print them using kthreads when the kthreads are working.
Quoted 3 lines
For v3 I recommend going back to the v1 model, where kthreads do not care if direct mode is preferred. I claim that v2 does yield any more actual direct printing than v1 did.
I agree.
Quoted 3 lines
However, I would keep the v2 change that kthreads go into their
wait_eventcheck after every message. That at least provides earlier responses for kthreads to stop themselves if they are disabled.
I am not sure what you mean. They seems to be completely disabled only when newly registered console is not able to create the kthread or when the kthread is not able to allocate buffers.
I think that there is no hurry to stop the other kthreads in this case.
Or do you mean panic?
Quoted 5 lines
Once we have atomic consoles, things will look different. Then we perform true synchronous direct printing. But without them, the "prefer" in
printk_prefer_direct_enter()is only a preference that can only be satisfied under ideal situations (i.e. no kthread is insidecall_console_driver()).
OK.
Best Regards,
Petr
Petr Mladek
maintainerregister_console: was: Re: [PATCH printk v2 11/12] printk: extend console_lock for proper kthread support
In reply to John OgnessQuoted 80 lines · John Ogness
Currently threaded console printers synchronize against each other using
console_lock(). However, different console drivers are unrelated and do not require any synchronization between each other. Removing the synchronization between the threaded console printers will allow each console to print at its own speed.But the threaded consoles printers do still need to synchronize against
console_lock()callers. Introduce a per-console mutex and a new console flagCON_THD_BLOCKEDto provide this synchronization.
console_lock()is modified so that it must acquire the mutex of each console in order to set theCON_THD_BLOCKEDflag. Console printing threads will acquire their mutex while printing a record. IfCON_THD_BLOCKEDwas set, the thread will go back to sleep instead of printing.The reason for the
CON_THD_BLOCKEDflag is so thatconsole_lock()callers do not need to acquire multiple console mutexes simultaneously, which would introduce unnecessary complexity due to nested mutex locking.However, threaded console printers also need to synchronize against
console_trylock()callers. Sinceconsole_trylock()may be called from any context, the per-console mutex cannot be used for this synchronization. (mutex_trylock()cannot be called from atomic contexts.) Introduce a global atomic counter to identify if any threaded printers are active. The threaded printers will also check the atomic counter to identify if the console has been locked by another task viaconsole_trylock().Note that @console_sem is still used to provide synchronization between
console_lock()andconsole_trylock()callers.A locking overview for
console_lock(),console_trylock(), and the threaded printers is as follows (pseudo code):
console_lock(){ down(&console_sem); for_each_console(con) { mutex_lock(&con->lock); con->flags |= CON_THD_BLOCKED; mutex_unlock(&con->lock); } /* console lock acquired */ }
console_trylock(){ if (down_trylock(&console_sem) == 0) { if (atomic_cmpxchg(&console_kthreads_active, 0, -1) == 0) { /* console lock acquired */ } } }
threaded_printer(){ mutex_lock(&con->lock); if (!(con->flags & CON_THD_BLOCKED)) { /* console_lock() callers blocked */ if (atomic_inc_unless_negative(&console_kthreads_active)) { /* console_trylock() callers blocked */ con->write(); atomic_dec(&console_lock_count); } } mutex_unlock(&con->lock); }The console owner and waiter logic now only applies between contexts that have taken the console lock. Since threaded printers never take the console lock, they do not have a console lock to handover to other contexts.
Quoted 7 lines
@panic_console_dropped must change to
atomic_tsince it is no longer protected exclusively by the console lock.Since threaded printers remain asleep if they see that the console is locked, they now must be explicitly woken in
__console_unlock(). This meanswake_up_klogd()calls following aconsole_unlock()are no longer necessary and are removed.
BTW: I really like the commit message.
kernel/printk/printk.c void register_console(struct console *newcon) · line 3338
Quoted 7 lines
kernel/printk/printk.c +1
-0@@ -3230,7 +3335,11 @@ void register_console(struct console *newcon)if (newcon->flags &CON_EXTENDED ) nr_ext_console_drivers++; +if (console_kthreads_blocked)
I think that this is actully always true because it is called under console_lock().
Quoted 8 lines
kernel/printk/printk.c +3
-0+ newcon->flags |=CON_THD_BLOCKED ; + newcon->dropped =0 ; +mutex_init (&newcon->lock);if (newcon->flags &CON_PRINTBUFFER ) {/* Get a consistent copy of @syslog_seq. */
mutex_lock(&syslog_lock);
Best Regards,
Petr
John Ogness
maintainerPATCH v2 12/12 printk: remove @console_locked
In reply to John OgnessThe static global variable @console_locked is used to help debug VT code to make sure that certain code paths are running with the console_lock held. However, this information is also available with the static global variable @console_kthreads_blocked (for locking via console_lock()), and the static global variable @console_kthreads_active (for locking via console_trylock()).
Remove @console_locked and update is_console_locked() to use the alternative variables.
--- kernel/printk/printk.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
kernel/printk/printk.c +14 -15
@@ -340,15 +340,7 @@ static void console_kthreads_unblock(void) console_kthreads_blocked = false; } -/* -* This is used for debugging the mess that is the VT code by -* keeping track if we have the console semaphore held. It's -* definitely not the perfect debug tool (we don't know if _WE_ -* hold it and are racing, but it helps tracking those weird code -* paths in the console code where we end up in places I want -* locked without the console semaphore held). -*/ -static int console_locked, console_suspended; +static int console_suspended;/* * Array of consoles built from command line options (console=)
@@ -2675,7 +2667,6 @@ void console_lock(void)if (console_suspended)return ;console_kthreads_block (); - console_locked =1 ; console_may_schedule =1 ; }EXPORT_SYMBOL (console_lock);
@@ -2700,15 +2691,26 @@ int console_trylock(void)up_console_sem ();return 0 ; } - console_locked =1 ; console_may_schedule =0 ;return 1 ; }EXPORT_SYMBOL (console_trylock); +/* +* This is used to help to make sure that certain paths within the VT code are +* running with the console lock held. It is definitely not the perfect debug +* tool (it is not known if the VT code is the task holding the console lock), +* but it helps tracking those weird code paths in the console code such as +* when the console is suspended: where the console is not locked but no +* console printing may occur. +* +* Note: This returns true when the console is suspended but is not locked. +* This is intentional because the VT code must consider that situation +* the same as if the console was locked. +*/ int is_console_locked (void ) { -return (console_locked ||atomic_read (&console_kthreads_active)); +return (console_kthreads_blocked ||atomic_read (&console_kthreads_active)); }EXPORT_SYMBOL (is_console_locked);
@@ -2759,8 +2761,6 @@ static inline bool console_is_usable(struct console *con)static void __console_unlock (void ) { - console_locked =0 ; -/* * Depending on whether console_lock() or console_trylock() was used, * appropriately allow the kthread printers to continue.
@@ -3058,7 +3058,6 @@ void console_unblank(void) }else console_lock (); - console_locked =1 ; console_may_schedule =0 ;for_each_console (c)if ((c->flags &CON_ENABLED ) && c->unblank)
Signature · 1 line
2.30.2