Skip to content

Commit 344da54

Browse files
committed
x86/nmi: Print reasons why backtrace NMIs are ignored
Instrument nmi_trigger_cpumask_backtrace() to dump out diagnostics based on evidence accumulated by exc_nmi(). These diagnostics are dumped for CPUs that ignored an NMI backtrace request for more than 10 seconds. [ paulmck: Apply Ingo Molnar feedback. ] Signed-off-by: Paul E. McKenney <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Dave Hansen <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: <[email protected]> Reviewed-by: Ingo Molnar <[email protected]>
1 parent 1a3ea61 commit 344da54

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

arch/x86/kernel/nmi.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,79 @@ DEFINE_IDTENTRY_RAW(exc_nmi_noist)
570570
EXPORT_SYMBOL_GPL(asm_exc_nmi_noist);
571571
#endif
572572

573+
#ifdef CONFIG_NMI_CHECK_CPU
574+
575+
static char *nmi_check_stall_msg[] = {
576+
/* */
577+
/* +--------- nsp->idt_seq_snap & 0x1: CPU is in NMI handler. */
578+
/* | +------ cpu_is_offline(cpu) */
579+
/* | | +--- nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls): */
580+
/* | | | NMI handler has been invoked. */
581+
/* | | | */
582+
/* V V V */
583+
/* 0 0 0 */ "NMIs are not reaching exc_nmi() handler",
584+
/* 0 0 1 */ "exc_nmi() handler is ignoring NMIs",
585+
/* 0 1 0 */ "CPU is offline and NMIs are not reaching exc_nmi() handler",
586+
/* 0 1 1 */ "CPU is offline and exc_nmi() handler is legitimately ignoring NMIs",
587+
/* 1 0 0 */ "CPU is in exc_nmi() handler and no further NMIs are reaching handler",
588+
/* 1 0 1 */ "CPU is in exc_nmi() handler which is legitimately ignoring NMIs",
589+
/* 1 1 0 */ "CPU is offline in exc_nmi() handler and no more NMIs are reaching exc_nmi() handler",
590+
/* 1 1 1 */ "CPU is offline in exc_nmi() handler which is legitimately ignoring NMIs",
591+
};
592+
593+
void nmi_backtrace_stall_snap(const struct cpumask *btp)
594+
{
595+
int cpu;
596+
struct nmi_stats *nsp;
597+
598+
for_each_cpu(cpu, btp) {
599+
nsp = per_cpu_ptr(&nmi_stats, cpu);
600+
nsp->idt_seq_snap = READ_ONCE(nsp->idt_seq);
601+
nsp->idt_nmi_seq_snap = READ_ONCE(nsp->idt_nmi_seq);
602+
nsp->idt_ignored_snap = READ_ONCE(nsp->idt_ignored);
603+
nsp->idt_calls_snap = atomic_long_read(&nsp->idt_calls);
604+
}
605+
}
606+
607+
void nmi_backtrace_stall_check(const struct cpumask *btp)
608+
{
609+
int cpu;
610+
int idx;
611+
unsigned long nmi_seq;
612+
unsigned long j = jiffies;
613+
char *modp;
614+
char *msgp;
615+
char *msghp;
616+
struct nmi_stats *nsp;
617+
618+
for_each_cpu(cpu, btp) {
619+
nsp = per_cpu_ptr(&nmi_stats, cpu);
620+
modp = "";
621+
msghp = "";
622+
nmi_seq = READ_ONCE(nsp->idt_nmi_seq);
623+
if (nsp->idt_nmi_seq_snap + 1 == nmi_seq && (nmi_seq & 0x1)) {
624+
msgp = "CPU entered NMI handler function, but has not exited";
625+
} else if ((nsp->idt_nmi_seq_snap & 0x1) != (nmi_seq & 0x1)) {
626+
msgp = "CPU is handling NMIs";
627+
} else {
628+
idx = ((nsp->idt_seq_snap & 0x1) << 2) |
629+
(cpu_is_offline(cpu) << 1) |
630+
(nsp->idt_calls_snap != atomic_long_read(&nsp->idt_calls));
631+
msgp = nmi_check_stall_msg[idx];
632+
if (nsp->idt_ignored_snap != READ_ONCE(nsp->idt_ignored) && (idx & 0x1))
633+
modp = ", but OK because ignore_nmis was set";
634+
if (nmi_seq & ~0x1)
635+
msghp = " (CPU currently in NMI handler function)";
636+
else if (nsp->idt_nmi_seq_snap + 1 == nmi_seq)
637+
msghp = " (CPU exited one NMI handler function)";
638+
}
639+
pr_alert("%s: CPU %d: %s%s%s, last activity: %lu jiffies ago.\n",
640+
__func__, cpu, msgp, modp, msghp, j - READ_ONCE(nsp->recv_jiffies));
641+
}
642+
}
643+
644+
#endif
645+
573646
void stop_nmi(void)
574647
{
575648
ignore_nmis++;

include/linux/nmi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,12 @@ int proc_watchdog_cpumask(struct ctl_table *, int, void *, size_t *, loff_t *);
214214
#include <asm/nmi.h>
215215
#endif
216216

217+
#ifdef CONFIG_NMI_CHECK_CPU
218+
void nmi_backtrace_stall_snap(const struct cpumask *btp);
219+
void nmi_backtrace_stall_check(const struct cpumask *btp);
220+
#else
221+
static inline void nmi_backtrace_stall_snap(const struct cpumask *btp) {}
222+
static inline void nmi_backtrace_stall_check(const struct cpumask *btp) {}
223+
#endif
224+
217225
#endif

lib/nmi_backtrace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
6464
if (!cpumask_empty(to_cpumask(backtrace_mask))) {
6565
pr_info("Sending NMI from CPU %d to CPUs %*pbl:\n",
6666
this_cpu, nr_cpumask_bits, to_cpumask(backtrace_mask));
67+
nmi_backtrace_stall_snap(to_cpumask(backtrace_mask));
6768
raise(to_cpumask(backtrace_mask));
6869
}
6970

@@ -74,6 +75,7 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
7475
mdelay(1);
7576
touch_softlockup_watchdog();
7677
}
78+
nmi_backtrace_stall_check(to_cpumask(backtrace_mask));
7779

7880
/*
7981
* Force flush any remote buffers that might be stuck in IRQ context

0 commit comments

Comments
 (0)