printk: Cleanups and softlockup avoidance
Subsystems (3)
v3 cover printk: Cleanups and softlockup avoidance
Hello,
this is another revision of the printk softlockup series. Since previous
version I have fixed up some small problems pointed out by Andrew, added possibility to configure out the printk offloading logic (for small systems), and offload kthreads are now started only once printk.offload_chars is set to value > 0.
Intro for the newcomers to the series below.
---
Currently, console_unlock() prints messages from kernel printk buffer to console while the buffer is non-empty. When serial console is attached, printing is slow and thus other CPUs in the system have plenty of time to append new messages to the buffer while one CPU is printing. Thus the CPU can spend unbounded amount of time doing printing in console_unlock(). This is especially serious since vprintk_emit() calls console_unlock() with interrupts disabled.
In practice users have observed a CPU can spend tens of seconds printing in console_unlock() (usually during boot when hundreds of SCSI devices are discovered) resulting in RCU stalls (CPU doing printing doesn't reach quiescent state for a long time), softlockup reports (IPIs for the printing CPU don't get served and thus other CPUs are spinning waiting for the printing CPU to process IPIs), and eventually a machine death (as messages from stalls and lockups append to printk buffer faster than we are able to print). So these machines are unable to boot with serial console attached. Also during artificial stress testing SATA disk disappears from the system because its interrupts aren't served for too long.
This is a revised series using my new approach to the problem which doesn't let CPU out of console_unlock() until there's someone else to take over the printing. The main difference since the last version is that instead of passing printing duty to different CPUs via IPIs we use dedicated kthreads. This method is somewhat less reliable (in a sense that there are more situations in which handover needn't work at all - e.g. when the currently printing CPU holds a spinlock and the CPU where kthread is scheduled to run is spinning on this spinlock) but the code is much simpler and in my practical testing kthread approach was good enough to avoid any problems (with one exception - see patch 8/8).
Honza
PATCH v3 1/8 printk: Remove outdated comment
Comment about interesting interlocking between lockbuf_lock and console_sem is outdated. It was added in 2002 by commit a880f45a48be2956d2c78a839c472287d54435c1 during conversion of console_lock to console_sem + lockbuf_lock. At that time release_console_sem() (today's equivalent is console_unlock()) was indeed using lockbuf_lock to avoid races between trylock on console_sem in printk() and unlock of console_sem. However these days the interlocking is gone and the races are avoided by rechecking logbuf state after releasing console_sem.
--- kernel/printk/printk.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/printk/printk.c +1 -2
@@ -206,8 +206,7 @@ struct printk_log { };/* -* The logbuf_lock protects kmsg buffer, indices, counters. It is also -* used in interesting ways to provide interlocking in console_unlock(); +* The logbuf_lock protects kmsg buffer, indices, counters. */ static DEFINE_RAW_SPINLOCK (logbuf_lock);
Signature · 1 line
1.8.1.4
PATCH v3 2/8 printk: Release lockbuf_lock before calling console_trylock_for_printk()
In reply to Jan KaraThere's no reason to hold lockbuf_lock when entering console_trylock_for_printk(). The first thing this function does is calling down_trylock(console_sem) and if that fails it immediately unlocks lockbuf_lock. So lockbuf_lock isn't needed for that branch. When down_trylock() succeeds, the rest of console_trylock() is OK without lockbuf_lock (it is called without it from other places), and the only remaining thing in console_trylock_for_printk() is can_use_console() call. For that call console_sem is enough (it iterates all consoles and checks CON_ANYTIME flag).
So we drop logbuf_lock before entering console_trylock_for_printk() which simplifies the code.
--- kernel/printk/printk.c | 49 +++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 32 deletions(-)
kernel/printk/printk.c +17 -32
@@ -249,9 +249,6 @@ static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);static char *log_buf = __log_buf;static u32 log_buf_len = __LOG_BUF_LEN; -/* cpu currently holding logbuf_lock */ -static volatile unsigned int logbuf_cpu =UINT_MAX ; -/* human readable text of the record */ static char *log_text (const struct printk_log *msg) {
@@ -1332,36 +1329,22 @@ static inline int can_use_console(unsigned int cpu)* messages from a 'printk'. Return true (and with the * console_lock held, and 'console_locked' set) if it * is successful, false otherwise. -* -* This gets called with the 'logbuf_lock' spinlock held and -* interrupts disabled. It should return with 'lockbuf_lock' -* released but interrupts still disabled. */ static int console_trylock_for_printk (unsigned int cpu) - __releases(&logbuf_lock) { -int retval =0 , wake =0 ; - -if (console_trylock ()) { - retval =1 ; - -/* -* If we can't use the console, we need to release -* the console semaphore by hand to avoid flushing -* the buffer. We need to hold the console semaphore -* in order to do this test safely. -*/ -if (!can_use_console (cpu)) { - console_locked =0 ; - wake =1 ; - retval =0 ; - } - } - logbuf_cpu =UINT_MAX ; -raw_spin_unlock (&logbuf_lock); -if (wake) +if (!console_trylock ()) +return 0 ; +/* +* If we can't use the console, we need to release the console +* semaphore by hand to avoid flushing the buffer. We need to hold the +* console semaphore in order to do this test safely. +*/ +if (!can_use_console (cpu)) { + console_locked =0 ;up (&console_sem); -return retval; +return 0 ; + } +return 1 ; }int printk_delay_msec__read_mostly ;
@@ -1494,6 +1477,9 @@ asmlinkage int vprintk_emit(int facility, int level,unsigned long flags;int this_cpu;int printed_len =0 ; +/* cpu currently holding logbuf_lock in this function */ +static volatile unsigned int logbuf_cpu =UINT_MAX ; +boot_delay_msec (level);printk_delay ();
@@ -1609,13 +1595,12 @@ asmlinkage int vprintk_emit(int facility, int level, } printed_len += text_len; + logbuf_cpu =UINT_MAX ; +raw_spin_unlock (&logbuf_lock);/* * Try to acquire and then immediately release the console semaphore. * The release will print out buffers and wake up /dev/kmsg and syslog() * users. -* -* The console_trylock_for_printk() function will release 'logbuf_lock' -* regardless of whether it actually gets the console semaphore or not. */ if (console_trylock_for_printk (this_cpu))console_unlock ();
Signature · 1 line
1.8.1.4
PATCH v3 3/8 printk: Enable interrupts before calling console_trylock_for_printk()
In reply to Jan KaraWe need interrupts disabled when calling console_trylock_for_printk() only so that cpu id we pass to can_use_console() remains valid (for other things console_sem provides all the exclusion we need and deadlocks on console_sem due to interrupts are impossible because we use down_trylock()). However if we are rescheduled, we are guaranteed to run on an online cpu so we can easily just get the cpu id in can_use_console().
We can loose a bit of performance when we enable interrupts in vprintk_emit() and then disable them again in console_unlock() but OTOH it can somewhat reduce interrupt latency caused by console_unlock() especially since later in the patch series we will want to spin on console_sem in console_trylock_for_printk().
--- kernel/printk/printk.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-)
kernel/printk/printk.c +18 -11
@@ -1314,10 +1314,9 @@ static int have_callable_console(void)/* * Can we actually use the console at this time on this cpu? * -* 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. +* 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. */ static inline int can_use_console (unsigned int cpu) {
@@ -1330,8 +1329,10 @@ static inline int can_use_console(unsigned int cpu)* console_lock held, and 'console_locked' set) if it * is successful, false otherwise. */ -static int console_trylock_for_printk (unsigned int cpu) +static int console_trylock_for_printk (void ) { +unsigned int cpu =smp_processor_id (); +if (!console_trylock ())return 0 ;/*
@@ -1501,7 +1502,8 @@ asmlinkage int vprintk_emit(int facility, int level,*/ if (!oops_in_progress && !lockdep_recursing (current)) { recursion_bug =1 ; - goto out_restore_irqs; +local_irq_restore (flags); +return 0 ; }zap_locks (); }
@@ -1597,17 +1599,22 @@ asmlinkage int vprintk_emit(int facility, int level, logbuf_cpu =UINT_MAX ;raw_spin_unlock (&logbuf_lock); +lockdep_on (); +local_irq_restore (flags); + +/* +* Disable preemption to avoid being preempted while holding +* console_sem which would prevent anyone from printing to console +*/ +preempt_disable ();/* * Try to acquire and then immediately release the console semaphore. * The release will print out buffers and wake up /dev/kmsg and syslog() * users. */ -if (console_trylock_for_printk (this_cpu)) +if (console_trylock_for_printk ())console_unlock (); - -lockdep_on (); -out_restore_irqs: -local_irq_restore (flags); +preempt_enable ();return printed_len; }
Signature · 1 line
1.8.1.4
PATCH v3 4/8 printk: Remove separate printk_sched buffers and use printk buf instead
In reply to Jan KaraFrom: Steven Rostedt <[email protected]>
To prevent deadlocks with doing a printk inside the scheduler, printk_sched() was created. The issue is that printk has a console_sem that it can grab and release. The release does a wake up if there's a task pending on the sem, and this wake up grabs the rq locks that is held in the scheduler. This leads to a possible deadlock if the wake up uses the same rq as the one with the rq lock held already.
What printk_sched() does is to save the printk write in a per cpu buffer and sets the PRINTK_PENDING_SCHED flag. On a timer tick, if this flag is set, the printk() is done against the buffer.
There's a couple of issues with this approach.
1) If two printk_sched()s are called before the tick, the second one will overwrite the first one.
2) The temporary buffer is 512 bytes and is per cpu. This is a quite a bit of space wasted for something that is seldom used.
In order to remove this, the printk_sched() can use the printk buffer instead, and delay the console_trylock()/console_unlock() to the queued work.
Because printk_sched() would then be taking the logbuf_lock, the logbuf_lock must not be held while doing anything that may call into the scheduler functions, which includes wake ups. Unfortunately, printk() also has a console_sem that it uses, and on release, the up(&console_sem) may do a wake up of any pending waiters. This must be avoided while holding the logbuf_lock.
--- kernel/printk/printk.c | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-)
kernel/printk/printk.c +29 -18
@@ -68,6 +68,9 @@ int console_printk[4] = { DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */ }; +/* Deferred messaged from sched code are marked by this special level */ +#define SCHED_MESSAGE_LOGLEVEL -2 +/* * Low level drivers may need that to know if they can schedule in * their unblank() callback or not. So let's export it.
@@ -206,7 +209,9 @@ struct printk_log { };/* -* The logbuf_lock protects kmsg buffer, indices, counters. +* The logbuf_lock protects kmsg buffer, indices, counters. This can be taken +* within the scheduler's rq lock. It must be released before calling +* console_unlock() or anything else that might wake up a process. */ static DEFINE_RAW_SPINLOCK (logbuf_lock);
@@ -1473,14 +1478,19 @@ asmlinkage int vprintk_emit(int facility, int level,static int recursion_bug;static char textbuf[LOG_LINE_MAX ];char *text = textbuf; -size_t text_len; +size_t text_len =0 ;enum log_flags lflags =0 ;unsigned long flags;int this_cpu;int printed_len =0 ; +bool in_sched = false;/* cpu currently holding logbuf_lock in this function */ static volatile unsigned int logbuf_cpu =UINT_MAX ; +if (level ==SCHED_MESSAGE_LOGLEVEL ) { + level =-1 ; + in_sched = true; + }boot_delay_msec (level);printk_delay ();
@@ -1527,7 +1537,12 @@ asmlinkage int vprintk_emit(int facility, int level,* The printf needs to come first; we need the syslog * prefix which might be passed-in as a parameter. */ - text_len =vscnprintf (text,sizeof (textbuf), fmt, args); +if (in_sched) + text_len =scnprintf (text,sizeof (textbuf), +KERN_WARNING "[sched_delayed] " ); + + text_len +=vscnprintf (text + text_len, +sizeof (textbuf) - text_len, fmt, args);/* mark and strip a trailing newline */ if (text_len && text[text_len-1 ] =='\n' ) {
@@ -1602,6 +1617,10 @@ asmlinkage int vprintk_emit(int facility, int level,lockdep_on ();local_irq_restore (flags); +/* If called from the scheduler, we can not call up(). */ +if (in_sched) +return printed_len; +/* * Disable preemption to avoid being preempted while holding * console_sem which would prevent anyone from printing to console
@@ -2423,21 +2442,19 @@ late_initcall(printk_late_init);/* * Delayed printk version, for scheduler-internal messages: */ -#define PRINTK_BUF_SIZE 512 -#define PRINTK_PENDING_WAKEUP 0x01 -#define PRINTK_PENDING_SCHED 0x02 +#define PRINTK_PENDING_OUTPUT 0x02 static DEFINE_PER_CPU(int, printk_pending); -static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);static void wake_up_klogd_work_func (struct irq_work *irq_work) {int pending =__this_cpu_xchg (printk_pending,0 ); -if (pending &PRINTK_PENDING_SCHED ) { -char *buf =__get_cpu_var (printk_sched_buf); -pr_warn ("[sched_delayed] %s" , buf); +if (pending &PRINTK_PENDING_OUTPUT ) { +/* If trylock fails, someone else is doing the printing */ +if (console_trylock ()) +console_unlock (); }if (pending &PRINTK_PENDING_WAKEUP )
@@ -2461,21 +2478,15 @@ void wake_up_klogd(void)int printk_sched (const char *fmt, ...) { -unsigned long flags;va_list args; -char *buf;int r; -local_irq_save (flags); - buf =__get_cpu_var (printk_sched_buf); -va_start (args, fmt); - r =vsnprintf (buf,PRINTK_BUF_SIZE , fmt, args); + r =vprintk_emit (0 ,SCHED_MESSAGE_LOGLEVEL ,NULL ,0 , fmt, args);va_end (args); -__this_cpu_or (printk_pending,PRINTK_PENDING_SCHED ); +__this_cpu_or (printk_pending,PRINTK_PENDING_OUTPUT );irq_work_queue (&__get_cpu_var (wake_up_klogd_work)); -local_irq_restore (flags);return r; }
Signature · 1 line
1.8.1.4
PATCH v3 5/8 printk: Hand over printing to console if printing too long
In reply to Jan KaraCurrently, console_unlock() prints messages from kernel printk buffer to console while the buffer is non-empty. When serial console is attached, printing is slow and thus other CPUs in the system have plenty of time to append new messages to the buffer while one CPU is printing. Thus the CPU can spend unbounded amount of time doing printing in console_unlock(). This is especially serious problem if the printk() calling console_unlock() was called with interrupts disabled.
In practice users have observed a CPU can spend tens of seconds printing in console_unlock() (usually during boot when hundreds of SCSI devices are discovered) resulting in RCU stalls (CPU doing printing doesn't reach quiescent state for a long time), softlockup reports (IPIs for the printing CPU don't get served and thus other CPUs are spinning waiting for the printing CPU to process IPIs), and eventually a machine death (as messages from stalls and lockups append to printk buffer faster than we are able to print). So these machines are unable to boot with serial console attached. Also during artificial stress testing SATA disk disappears from the system because its interrupts aren't served for too long.
This patch implements a mechanism where after printing specified number of characters (tunable as a kernel parameter printk.offload_chars), CPU doing printing asks for help by setting PRINTK_HANDOVER_B bit in printk_handover_state variable and wakes up one of dedicated kthreads. As soon as the printing CPU notices kthread got scheduled and is spinning on console_sem, it drops console_sem and exits console_unlock(). kthread then takes over printing instead. This way no CPU should spend printing too long even if there is heavy printk traffic.
--- Documentation/kernel-parameters.txt | 15 +++ kernel/printk/printk.c | 196 ++++++++++++++++++++++++++++++++---- 2 files changed, 194 insertions(+), 17 deletions(-)
Documentation/kernel-parameters.txt +15 -0
@@ -2621,6 +2621,21 @@ bytes respectively. Such letter suffixes can also be entirely omitted. Format: <bool> (1/Y/y=enable, 0/N/n=disable) default: disabled + printk.offload_chars= + Printing to console can be relatively slow especially + in case of serial console. When there is intensive + printing happening from several cpus (as is the case + during boot), a cpu can be spending significant time + (seconds or more) doing printing. To avoid softlockups, + lost interrupts, and similar problems other cpus + will take over printing after the currently printing + cpu has printed 'printk.offload_chars' characters. + Higher value means possibly longer interrupt and other + latencies but lower overhead of printing due to handing + over of printing. + Format: <number> (0 = disabled) + default: 1000 + printk.time= Show timing data prefixed to each printk message line Format: <bool> (1/Y/y=enable, 0/N/n=disable)
kernel/printk/printk.c +179 -17
302 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH v3 6/8 printk: Start printing handover kthreads on demand
In reply to Jan KaraStart kthreads for handing over printing only when printk.offload_chars is set to value > 0 (i.e., when print offloading gets enabled).
--- kernel/printk/printk.c | 64 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 14 deletions(-)
kernel/printk/printk.c +50 -14
@@ -109,6 +109,10 @@ static long printk_handover_state;* CPUs. */ #define PRINTING_TASKS 2 +/* Pointers to printing kthreads */ +static struct task_struct *printing_kthread[PRINTING_TASKS ]; +/* Serialization of changes to printk_offload_chars and kthread creation */ +static DEFINE_MUTEX (printk_kthread_mutex);/* Wait queue printing kthreads sleep on when idle */ static DECLARE_WAIT_QUEUE_HEAD (print_queue);
@@ -280,6 +284,12 @@ static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);static char *log_buf = __log_buf;static u32 log_buf_len = __LOG_BUF_LEN; +static int offload_chars_set (const char *val,const struct kernel_param *kp); +static struct kernel_param_ops offload_chars_ops = { + .set = offload_chars_set, + .get = param_get_uint, +}; +/* * How many characters can we print in one call of printk before asking * other cpus to continue printing. 0 means infinity. Tunable via
@@ -288,7 +298,7 @@ static u32 log_buf_len = __LOG_BUF_LEN;*/ static unsigned int __read_mostly printk_offload_chars =1000 ; -module_param_named (offload_chars, printk_offload_chars, uint, +module_param_cb (offload_chars, &offload_chars_ops, &printk_offload_chars,S_IRUGO |S_IWUSR );MODULE_PARM_DESC (offload_chars,"offload printing to console to a different" " cpu after this number of characters" );
@@ -2571,30 +2581,56 @@ static int printing_task(void *arg)return 0 ; } -static int __init printk_late_init (void ) +static void printk_start_offload_kthreads (void ) { -struct console *con;int i;struct task_struct *task; -for_each_console (con) { -if (!keep_bootcon && con->flags &CON_BOOT ) { -unregister_console (con); - } - } -hotcpu_notifier (console_cpu_notify,0 ); - -/* Does any handover of printing have any sence? */ -if (num_possible_cpus () <=1 ) -return 0 ; - +/* Does handover of printing make any sense? */ +if (printk_offload_chars ==0 ||num_possible_cpus () <=1 ) +return ;for (i =0 ; i <PRINTING_TASKS ; i++) { +if (printing_kthread[i]) +continue ; task =kthread_run (printing_task,NULL ,"print/%d" , i);if (IS_ERR (task)) {pr_err ("printk: Cannot create printing thread: %ld\n" ,PTR_ERR (task)); } + printing_kthread[i] = task; } +} + +static int offload_chars_set (const char *val,const struct kernel_param *kp) +{ +int ret; + +/* Protect against parallel change of printk_offload_chars */ +mutex_lock (&printk_kthread_mutex); + ret =param_set_uint (val, kp); +if (ret) { +mutex_unlock (&printk_kthread_mutex); +return ret; + } +printk_start_offload_kthreads (); +mutex_unlock (&printk_kthread_mutex); +return 0 ; +} + +static int __init printk_late_init (void ) +{ +struct console *con; + +for_each_console (con) { +if (!keep_bootcon && con->flags &CON_BOOT ) { +unregister_console (con); + } + } +hotcpu_notifier (console_cpu_notify,0 ); + +mutex_lock (&printk_kthread_mutex); +printk_start_offload_kthreads (); +mutex_unlock (&printk_kthread_mutex);return 0 ; }
Signature · 1 line
1.8.1.4
PATCH v3 7/8 kernel: Avoid softlockups in stop_machine() during heavy printing
In reply to Jan KaraWhen there are lots of messages accumulated in printk buffer, printing them (especially over serial console) can take a long time (tens of seconds). stop_machine() will effectively make all cpus spin in multi_cpu_stop() waiting for the CPU doing printing to print all the messages which triggers NMI softlockup watchdog and RCU stall detector which add even more to the messages to print. Since machine doesn't do anything (except serving interrupts) during this time, also network connections are dropped and other disturbances may happen.
Paper over the problem by waiting for printk buffer to be empty before starting to stop CPUs. In theory a burst of new messages can be appended to the printk buffer before CPUs enter multi_cpu_stop() so this isn't a 100% solution but it works OK in practice and I'm not aware of a reasonably simple better solution.
--- include/linux/console.h | 1 + kernel/printk/printk.c | 22 ++++++++++++++++++++++ kernel/stop_machine.c | 9 +++++++++ 3 files changed, 32 insertions(+)
include/linux/console.h +1 -0
@@ -150,6 +150,7 @@ extern int console_trylock(void);extern void console_unlock (void );extern void console_conditional_schedule (void );extern void console_unblank (void ); +extern void console_flush (void );extern struct tty_driver *console_device (int *);extern void console_stop (struct console *);extern void console_start (struct console *);
kernel/printk/printk.c +22 -0
@@ -2306,6 +2306,28 @@ struct tty_driver *console_device(int *index) }/* +* Wait until all messages accumulated in the printk buffer are printed to +* console. Note that as soon as this function returns, new messages may be +* added to the printk buffer by other CPUs. +*/ +void console_flush (void ) +{ +bool retry; +unsigned long flags; + +while (1 ) { +raw_spin_lock_irqsave (&logbuf_lock, flags); + retry = console_seq != log_next_seq; +raw_spin_unlock_irqrestore (&logbuf_lock, flags); +if (!retry) +break ; +/* Cycle console_sem to wait for outstanding printing */ +console_lock (); +console_unlock (); + } +} + +/* * Prevent further output on the passed console device so that (for example) * serial drivers can disable console output before suspending a port, and can * re-enable output afterwards.
kernel/stop_machine.c +9 -0
@@ -21,6 +21,7 @@#include <linux/smpboot.h> #include <linux/atomic.h> #include <linux/lglock.h> +#include <linux/console.h> /* * Structure to determine completion condition and record errors. May
@@ -574,6 +575,14 @@ int __stop_machine(int (*fn)(void *), void *data, const struct cpumask *cpus)return ret; } +/* +* If there are lots of outstanding messages, printing them can take a +* long time and all cpus would be spinning waiting for the printing to +* finish thus triggering NMI watchdog, RCU lockups etc. Wait for the +* printing here to avoid these. +*/ +console_flush (); +/* Set the initial state and stop all online cpus. */ set_state (&msdata,MULTI_STOP_PREPARE );return stop_cpus (cpu_online_mask, multi_cpu_stop, &msdata);
Signature · 1 line
1.8.1.4
PATCH v3 8/8 printk: Add config option for disabling printk offloading
In reply to Jan KaraNecessity for offloading of printing was observed only for large systems. So add a config option (disabled by default) which removes most of the overhead added by this functionality.
--- Documentation/kernel-parameters.txt | 13 +++++++------ init/Kconfig | 14 ++++++++++++++ kernel/printk/printk.c | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-)
Documentation/kernel-parameters.txt +7 -6
@@ -2621,18 +2621,19 @@ bytes respectively. Such letter suffixes can also be entirely omitted. Format: <bool> (1/Y/y=enable, 0/N/n=disable) default: disabled - printk.offload_chars= + printk.offload_chars= [KNL] Printing to console can be relatively slow especially in case of serial console. When there is intensive printing happening from several cpus (as is the case during boot), a cpu can be spending significant time (seconds or more) doing printing. To avoid softlockups, lost interrupts, and similar problems other cpus - will take over printing after the currently printing - cpu has printed 'printk.offload_chars' characters. - Higher value means possibly longer interrupt and other - latencies but lower overhead of printing due to handing - over of printing. + will take over printing (if CONFIG_PRINTK_OFFLOAD=y) + after the currently printing cpu has printed + 'printk.offload_chars' characters. Higher value means + possibly longer interrupt and other latencies but + lower overhead of printing due to handing over of + printing. Format: <number> (0 = disabled) default: 1000
init/Kconfig +14 -0
@@ -1343,6 +1343,20 @@ config PRINTK very difficult to diagnose system problems, saying N here is strongly discouraged. +config PRINTK_OFFLOAD + default y + bool "Enable support for offloading printing to different CPU" + depends on PRINTK + help + Printing to console can be relatively slow especially in case of + serial console. On large machines when there is intensive printing + happening from several cpus (as is the case during boot), a cpu can + be spending significant time (seconds or more) doing printing. To + avoid softlockups, lost interrupts, and similar problems other cpus + will take over printing after the currently printing cpu has printed + certain number of characters (tunable via 'printk.offload_chars' + kernel parameter). + config BUG bool "BUG() support" if EXPERT default y
kernel/printk/printk.c +14 -0
@@ -103,6 +103,7 @@ enum { };static long printk_handover_state; +#ifdef CONFIG_PRINTK_OFFLOAD /* * Number of kernel threads for offloading printing. We need at least two so * that they can hand over printing from one to another one and thus switch
@@ -116,6 +117,7 @@ static DEFINE_MUTEX(printk_kthread_mutex);/* Wait queue printing kthreads sleep on when idle */ static DECLARE_WAIT_QUEUE_HEAD (print_queue); +#endif /* CONFIG_PRINTK_OFFLOAD */ #ifdef CONFIG_LOCKDEP static struct lockdep_map console_lock_dep_map = {
@@ -284,6 +286,7 @@ static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);static char *log_buf = __log_buf;static u32 log_buf_len = __LOG_BUF_LEN; +#ifdef CONFIG_PRINTK_OFFLOAD static int offload_chars_set (const char *val,const struct kernel_param *kp);static struct kernel_param_ops offload_chars_ops = { .set = offload_chars_set,
@@ -302,6 +305,7 @@ module_param_cb(offload_chars, &offload_chars_ops, &printk_offload_chars, S_IRUGO | S_IWUSR);MODULE_PARM_DESC (offload_chars,"offload printing to console to a different" " cpu after this number of characters" ); +#endif /* human readable text of the record */ static char *log_text (const struct printk_log *msg)
@@ -2021,6 +2025,7 @@ int console_trylock(void) }EXPORT_SYMBOL (console_trylock); +#ifdef CONFIG_PRINTK_OFFLOAD /* * This is a version of console_lock() which spins to acquire console_sem. * It is only for use by threads that take care of flushing printk buffer so
@@ -2052,6 +2057,7 @@ static int console_lock_try_spin(void)mutex_acquire (&console_lock_dep_map,0 ,0 , _RET_IP_); return 1; } +#endif int is_console_locked (void ) {
@@ -2087,6 +2093,7 @@ out:raw_spin_unlock_irqrestore (&logbuf_lock, flags); } +#ifdef CONFIG_PRINTK_OFFLOAD /* * Returns true iff there is other cpu waiting to take over printing. This * function also takes are of setting PRINTK_HANDOVER_B if we want to hand over
@@ -2113,6 +2120,7 @@ static bool cpu_stop_printing(int printed_chars) }return false; } +#endif /** * console_unlock - unlock the console system
@@ -2155,10 +2163,12 @@ again:size_t len;int level; +#ifdef CONFIG_PRINTK_OFFLOAD if (cpu_stop_printing (printed_chars)) { hand_over = true;break ; } +#endif raw_spin_lock_irqsave (&logbuf_lock, flags);if (seen_seq != log_next_seq) {
@@ -2576,6 +2586,7 @@ int unregister_console(struct console *console) }EXPORT_SYMBOL (unregister_console); +#ifdef CONFIG_PRINTK_OFFLOAD /* Kthread which takes over printing from a CPU which asks for help */ static int printing_task (void *arg) {
@@ -2638,6 +2649,7 @@ static int offload_chars_set(const char *val, const struct kernel_param *kp)mutex_unlock (&printk_kthread_mutex); return 0; } +#endif /* CONFIG_PRINTK_OFFLOAD */ static int __init printk_late_init (void ) {
@@ -2650,9 +2662,11 @@ static int __init printk_late_init(void) }hotcpu_notifier (console_cpu_notify,0 ); +#ifdef CONFIG_PRINTK_OFFLOAD mutex_lock (&printk_kthread_mutex);printk_start_offload_kthreads ();mutex_unlock (&printk_kthread_mutex); +#endif return 0 ; }
Signature · 1 line
1.8.1.4