Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

printk: Consolidate console deferred printing

Printing to consoles can be deferred for several reasons:

- explicitly with printk_deferred()
- printk() in NMI context
- recursive printk() calls

The current implementation is not consistent. For printk_deferred(),
irq work is scheduled twice. For NMI und recursive, panic CPU
suppression and caller delays are not properly enforced.

Correct these inconsistencies by consolidating the deferred printing
code so that vprintk_deferred() is the top-level function for
deferred printing and vprintk_emit() will perform whichever irq_work
queueing is appropriate.

Also add kerneldoc for wake_up_klogd() and defer_console_output() to
clarify their differences and appropriate usage.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20230717194607.145135-6-john.ogness@linutronix.de

authored by

John Ogness and committed by
Petr Mladek
696ffaf5 eacb04ff

+30 -14
+28 -7
kernel/printk/printk.c
··· 2306 2306 preempt_enable(); 2307 2307 } 2308 2308 2309 - wake_up_klogd(); 2309 + if (in_sched) 2310 + defer_console_output(); 2311 + else 2312 + wake_up_klogd(); 2313 + 2310 2314 return printed_len; 2311 2315 } 2312 2316 EXPORT_SYMBOL(vprintk_emit); ··· 3845 3841 preempt_enable(); 3846 3842 } 3847 3843 3844 + /** 3845 + * wake_up_klogd - Wake kernel logging daemon 3846 + * 3847 + * Use this function when new records have been added to the ringbuffer 3848 + * and the console printing of those records has already occurred or is 3849 + * known to be handled by some other context. This function will only 3850 + * wake the logging daemon. 3851 + * 3852 + * Context: Any context. 3853 + */ 3848 3854 void wake_up_klogd(void) 3849 3855 { 3850 3856 __wake_up_klogd(PRINTK_PENDING_WAKEUP); 3851 3857 } 3852 3858 3859 + /** 3860 + * defer_console_output - Wake kernel logging daemon and trigger 3861 + * console printing in a deferred context 3862 + * 3863 + * Use this function when new records have been added to the ringbuffer, 3864 + * this context is responsible for console printing those records, but 3865 + * the current context is not allowed to perform the console printing. 3866 + * Trigger an irq_work context to perform the console printing. This 3867 + * function also wakes the logging daemon. 3868 + * 3869 + * Context: Any context. 3870 + */ 3853 3871 void defer_console_output(void) 3854 3872 { 3855 3873 /* ··· 3888 3862 3889 3863 int vprintk_deferred(const char *fmt, va_list args) 3890 3864 { 3891 - int r; 3892 - 3893 - r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, fmt, args); 3894 - defer_console_output(); 3895 - 3896 - return r; 3865 + return vprintk_emit(0, LOGLEVEL_SCHED, NULL, fmt, args); 3897 3866 } 3898 3867 3899 3868 int _printk_deferred(const char *fmt, ...)
+2 -7
kernel/printk/printk_safe.c
··· 38 38 * Use the main logbuf even in NMI. But avoid calling console 39 39 * drivers that might have their own locks. 40 40 */ 41 - if (this_cpu_read(printk_context) || in_nmi()) { 42 - int len; 43 - 44 - len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args); 45 - defer_console_output(); 46 - return len; 47 - } 41 + if (this_cpu_read(printk_context) || in_nmi()) 42 + return vprintk_deferred(fmt, args); 48 43 49 44 /* No obstacles. */ 50 45 return vprintk_default(fmt, args);