printk: remove NMI tracking
authorJohn Ogness <john.ogness@linutronix.de>
Thu, 15 Jul 2021 19:33:57 +0000 (21:39 +0206)
committerPetr Mladek <pmladek@suse.com>
Mon, 26 Jul 2021 13:09:44 +0000 (15:09 +0200)
All NMI contexts are handled the same as the safe context: store the
message and defer printing. There is no need to have special NMI
context tracking for this. Using in_nmi() is enough.

There are several parts of the kernel that are manually calling into
the printk NMI context tracking in order to cause general printk
deferred printing:

    arch/arm/kernel/smp.c
    arch/powerpc/kexec/crash.c
    kernel/trace/trace.c

For arm/kernel/smp.c and powerpc/kexec/crash.c, provide a new
function pair printk_deferred_enter/exit that explicitly achieves the
same objective.

For ftrace, remove the printk context manipulation completely. It was
added in commit 03fc7f9c99c1 ("printk/nmi: Prevent deadlock when
accessing the main log buffer in NMI"). The purpose was to enforce
storing messages directly into the ring buffer even in NMI context.
It really should have only modified the behavior in NMI context.
There is no need for a special behavior any longer. All messages are
always stored directly now. The console deferring is handled
transparently in vprintk().

Signed-off-by: John Ogness <john.ogness@linutronix.de>
[pmladek@suse.com: Remove special handling in ftrace.c completely.
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20210715193359.25946-5-john.ogness@linutronix.de
arch/arm/kernel/smp.c
arch/powerpc/kexec/crash.c
include/linux/hardirq.h
include/linux/printk.h
init/Kconfig
kernel/printk/internal.h
kernel/printk/printk_safe.c
kernel/trace/trace.c

index c7bb168..842427f 100644 (file)
@@ -667,9 +667,9 @@ static void do_handle_IPI(int ipinr)
                break;
 
        case IPI_CPU_BACKTRACE:
-               printk_nmi_enter();
+               printk_deferred_enter();
                nmi_cpu_backtrace(get_irq_regs());
-               printk_nmi_exit();
+               printk_deferred_exit();
                break;
 
        default:
index 0196d0c..1070378 100644 (file)
@@ -313,7 +313,7 @@ void default_machine_crash_shutdown(struct pt_regs *regs)
        int (*old_handler)(struct pt_regs *regs);
 
        /* Avoid hardlocking with irresponsive CPU holding logbuf_lock */
-       printk_nmi_enter();
+       printk_deferred_enter();
 
        /*
         * This function is only called after the system
index 69bc86e..76878b3 100644 (file)
@@ -116,7 +116,6 @@ extern void rcu_nmi_exit(void);
        do {                                                    \
                lockdep_off();                                  \
                arch_nmi_enter();                               \
-               printk_nmi_enter();                             \
                BUG_ON(in_nmi() == NMI_MASK);                   \
                __preempt_count_add(NMI_OFFSET + HARDIRQ_OFFSET);       \
        } while (0)
@@ -135,7 +134,6 @@ extern void rcu_nmi_exit(void);
        do {                                                    \
                BUG_ON(!in_nmi());                              \
                __preempt_count_sub(NMI_OFFSET + HARDIRQ_OFFSET);       \
-               printk_nmi_exit();                              \
                arch_nmi_exit();                                \
                lockdep_on();                                   \
        } while (0)
index 719d919..a1379df 100644 (file)
@@ -149,18 +149,6 @@ static inline __printf(1, 2) __cold
 void early_printk(const char *s, ...) { }
 #endif
 
-#ifdef CONFIG_PRINTK_NMI
-extern void printk_nmi_enter(void);
-extern void printk_nmi_exit(void);
-extern void printk_nmi_direct_enter(void);
-extern void printk_nmi_direct_exit(void);
-#else
-static inline void printk_nmi_enter(void) { }
-static inline void printk_nmi_exit(void) { }
-static inline void printk_nmi_direct_enter(void) { }
-static inline void printk_nmi_direct_exit(void) { }
-#endif /* PRINTK_NMI */
-
 struct dev_printk_info;
 
 #ifdef CONFIG_PRINTK
@@ -180,6 +168,16 @@ int printk(const char *fmt, ...);
  */
 __printf(1, 2) __cold int printk_deferred(const char *fmt, ...);
 
+extern void __printk_safe_enter(void);
+extern void __printk_safe_exit(void);
+/*
+ * The printk_deferred_enter/exit macros are available only as a hack for
+ * some code paths that need to defer all printk console printing. Interrupts
+ * must be disabled for the deferred duration.
+ */
+#define printk_deferred_enter __printk_safe_enter
+#define printk_deferred_exit __printk_safe_exit
+
 /*
  * Please don't use printk_ratelimit(), because it shares ratelimiting state
  * with all other unrelated printk_ratelimit() callsites.  Instead use
@@ -224,6 +222,15 @@ int printk_deferred(const char *s, ...)
 {
        return 0;
 }
+
+static inline void printk_deferred_enter(void)
+{
+}
+
+static inline void printk_deferred_exit(void)
+{
+}
+
 static inline int printk_ratelimit(void)
 {
        return 0;
index a61c920..9c05106 100644 (file)
@@ -1506,11 +1506,6 @@ config PRINTK
          very difficult to diagnose system problems, saying N here is
          strongly discouraged.
 
-config PRINTK_NMI
-       def_bool y
-       depends on PRINTK
-       depends on HAVE_NMI
-
 config BUG
        bool "BUG() support" if EXPERT
        default y
index 6cc35c5..b6d310c 100644 (file)
@@ -6,12 +6,6 @@
 
 #ifdef CONFIG_PRINTK
 
-#define PRINTK_SAFE_CONTEXT_MASK       0x007ffffff
-#define PRINTK_NMI_DIRECT_CONTEXT_MASK 0x008000000
-#define PRINTK_NMI_CONTEXT_MASK                0xff0000000
-
-#define PRINTK_NMI_CONTEXT_OFFSET      0x010000000
-
 __printf(4, 0)
 int vprintk_store(int facility, int level,
                  const struct dev_printk_info *dev_info,
@@ -19,8 +13,6 @@ int vprintk_store(int facility, int level,
 
 __printf(1, 0) int vprintk_default(const char *fmt, va_list args);
 __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
-void __printk_safe_enter(void);
-void __printk_safe_exit(void);
 
 bool printk_percpu_data_ready(void);
 
index 29c580d..ef0f9a2 100644 (file)
@@ -4,12 +4,9 @@
  */
 
 #include <linux/preempt.h>
-#include <linux/spinlock.h>
-#include <linux/debug_locks.h>
 #include <linux/kdb.h>
 #include <linux/smp.h>
 #include <linux/cpumask.h>
-#include <linux/irq_work.h>
 #include <linux/printk.h>
 #include <linux/kprobes.h>
 
 
 static DEFINE_PER_CPU(int, printk_context);
 
-#ifdef CONFIG_PRINTK_NMI
-void noinstr printk_nmi_enter(void)
-{
-       this_cpu_add(printk_context, PRINTK_NMI_CONTEXT_OFFSET);
-}
-
-void noinstr printk_nmi_exit(void)
-{
-       this_cpu_sub(printk_context, PRINTK_NMI_CONTEXT_OFFSET);
-}
-
-/*
- * Marks a code that might produce many messages in NMI context
- * and the risk of losing them is more critical than eventual
- * reordering.
- */
-void printk_nmi_direct_enter(void)
-{
-       if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
-               this_cpu_or(printk_context, PRINTK_NMI_DIRECT_CONTEXT_MASK);
-}
-
-void printk_nmi_direct_exit(void)
-{
-       this_cpu_and(printk_context, ~PRINTK_NMI_DIRECT_CONTEXT_MASK);
-}
-
-#endif /* CONFIG_PRINTK_NMI */
-
 /* Can be preempted by NMI. */
 void __printk_safe_enter(void)
 {
@@ -70,10 +38,7 @@ asmlinkage int vprintk(const char *fmt, va_list args)
         * Use the main logbuf even in NMI. But avoid calling console
         * drivers that might have their own locks.
         */
-       if (this_cpu_read(printk_context) &
-           (PRINTK_NMI_DIRECT_CONTEXT_MASK |
-            PRINTK_NMI_CONTEXT_MASK |
-            PRINTK_SAFE_CONTEXT_MASK)) {
+       if (this_cpu_read(printk_context) || in_nmi()) {
                int len;
 
                len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
index d23a09d..2f41311 100644 (file)
@@ -9647,7 +9647,6 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
        tracing_off();
 
        local_irq_save(flags);
-       printk_nmi_direct_enter();
 
        /* Simulate the iterator */
        trace_init_global_iter(&iter);
@@ -9729,7 +9728,6 @@ void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
                atomic_dec(&per_cpu_ptr(iter.array_buffer->data, cpu)->disabled);
        }
        atomic_dec(&dump_running);
-       printk_nmi_direct_exit();
        local_irq_restore(flags);
 }
 EXPORT_SYMBOL_GPL(ftrace_dump);