Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * printk_safe.c - Safe printk for printk-deadlock-prone contexts
0004  */
0005 
0006 #include <linux/preempt.h>
0007 #include <linux/kdb.h>
0008 #include <linux/smp.h>
0009 #include <linux/cpumask.h>
0010 #include <linux/printk.h>
0011 #include <linux/kprobes.h>
0012 
0013 #include "internal.h"
0014 
0015 static DEFINE_PER_CPU(int, printk_context);
0016 
0017 /* Can be preempted by NMI. */
0018 void __printk_safe_enter(void)
0019 {
0020     this_cpu_inc(printk_context);
0021 }
0022 
0023 /* Can be preempted by NMI. */
0024 void __printk_safe_exit(void)
0025 {
0026     this_cpu_dec(printk_context);
0027 }
0028 
0029 asmlinkage int vprintk(const char *fmt, va_list args)
0030 {
0031 #ifdef CONFIG_KGDB_KDB
0032     /* Allow to pass printk() to kdb but avoid a recursion. */
0033     if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0))
0034         return vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
0035 #endif
0036 
0037     /*
0038      * Use the main logbuf even in NMI. But avoid calling console
0039      * drivers that might have their own locks.
0040      */
0041     if (this_cpu_read(printk_context) || in_nmi()) {
0042         int len;
0043 
0044         len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
0045         defer_console_output();
0046         return len;
0047     }
0048 
0049     /* No obstacles. */
0050     return vprintk_default(fmt, args);
0051 }
0052 EXPORT_SYMBOL(vprintk);