xref: /openbmc/linux/arch/x86/kernel/nmi.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 1991, 1992  Linus Torvalds
4  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
5  *  Copyright (C) 2011	Don Zickus Red Hat, Inc.
6  *
7  *  Pentium III FXSR, SSE support
8  *	Gareth Hughes <gareth@valinux.com>, May 2000
9  */
10 
11 /*
12  * Handle hardware traps and faults.
13  */
14 #include <linux/spinlock.h>
15 #include <linux/kprobes.h>
16 #include <linux/kdebug.h>
17 #include <linux/sched/debug.h>
18 #include <linux/nmi.h>
19 #include <linux/debugfs.h>
20 #include <linux/delay.h>
21 #include <linux/hardirq.h>
22 #include <linux/ratelimit.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <linux/atomic.h>
26 #include <linux/sched/clock.h>
27 
28 #include <asm/cpu_entry_area.h>
29 #include <asm/traps.h>
30 #include <asm/mach_traps.h>
31 #include <asm/nmi.h>
32 #include <asm/x86_init.h>
33 #include <asm/reboot.h>
34 #include <asm/cache.h>
35 #include <asm/nospec-branch.h>
36 #include <asm/sev.h>
37 
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/nmi.h>
40 
41 struct nmi_desc {
42 	raw_spinlock_t lock;
43 	struct list_head head;
44 };
45 
46 static struct nmi_desc nmi_desc[NMI_MAX] =
47 {
48 	{
49 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[0].lock),
50 		.head = LIST_HEAD_INIT(nmi_desc[0].head),
51 	},
52 	{
53 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[1].lock),
54 		.head = LIST_HEAD_INIT(nmi_desc[1].head),
55 	},
56 	{
57 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[2].lock),
58 		.head = LIST_HEAD_INIT(nmi_desc[2].head),
59 	},
60 	{
61 		.lock = __RAW_SPIN_LOCK_UNLOCKED(&nmi_desc[3].lock),
62 		.head = LIST_HEAD_INIT(nmi_desc[3].head),
63 	},
64 
65 };
66 
67 struct nmi_stats {
68 	unsigned int normal;
69 	unsigned int unknown;
70 	unsigned int external;
71 	unsigned int swallow;
72 	unsigned long recv_jiffies;
73 	unsigned long idt_seq;
74 	unsigned long idt_nmi_seq;
75 	unsigned long idt_ignored;
76 	atomic_long_t idt_calls;
77 	unsigned long idt_seq_snap;
78 	unsigned long idt_nmi_seq_snap;
79 	unsigned long idt_ignored_snap;
80 	long idt_calls_snap;
81 };
82 
83 static DEFINE_PER_CPU(struct nmi_stats, nmi_stats);
84 
85 static int ignore_nmis __read_mostly;
86 
87 int unknown_nmi_panic;
88 /*
89  * Prevent NMI reason port (0x61) being accessed simultaneously, can
90  * only be used in NMI handler.
91  */
92 static DEFINE_RAW_SPINLOCK(nmi_reason_lock);
93 
94 static int __init setup_unknown_nmi_panic(char *str)
95 {
96 	unknown_nmi_panic = 1;
97 	return 1;
98 }
99 __setup("unknown_nmi_panic", setup_unknown_nmi_panic);
100 
101 #define nmi_to_desc(type) (&nmi_desc[type])
102 
103 static u64 nmi_longest_ns = 1 * NSEC_PER_MSEC;
104 
105 static int __init nmi_warning_debugfs(void)
106 {
107 	debugfs_create_u64("nmi_longest_ns", 0644,
108 			arch_debugfs_dir, &nmi_longest_ns);
109 	return 0;
110 }
111 fs_initcall(nmi_warning_debugfs);
112 
113 static void nmi_check_duration(struct nmiaction *action, u64 duration)
114 {
115 	int remainder_ns, decimal_msecs;
116 
117 	if (duration < nmi_longest_ns || duration < action->max_duration)
118 		return;
119 
120 	action->max_duration = duration;
121 
122 	remainder_ns = do_div(duration, (1000 * 1000));
123 	decimal_msecs = remainder_ns / 1000;
124 
125 	printk_ratelimited(KERN_INFO
126 		"INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
127 		action->handler, duration, decimal_msecs);
128 }
129 
130 static int nmi_handle(unsigned int type, struct pt_regs *regs)
131 {
132 	struct nmi_desc *desc = nmi_to_desc(type);
133 	struct nmiaction *a;
134 	int handled=0;
135 
136 	rcu_read_lock();
137 
138 	/*
139 	 * NMIs are edge-triggered, which means if you have enough
140 	 * of them concurrently, you can lose some because only one
141 	 * can be latched at any given time.  Walk the whole list
142 	 * to handle those situations.
143 	 */
144 	list_for_each_entry_rcu(a, &desc->head, list) {
145 		int thishandled;
146 		u64 delta;
147 
148 		delta = sched_clock();
149 		thishandled = a->handler(type, regs);
150 		handled += thishandled;
151 		delta = sched_clock() - delta;
152 		trace_nmi_handler(a->handler, (int)delta, thishandled);
153 
154 		nmi_check_duration(a, delta);
155 	}
156 
157 	rcu_read_unlock();
158 
159 	/* return total number of NMI events handled */
160 	return handled;
161 }
162 NOKPROBE_SYMBOL(nmi_handle);
163 
164 int __register_nmi_handler(unsigned int type, struct nmiaction *action)
165 {
166 	struct nmi_desc *desc = nmi_to_desc(type);
167 	unsigned long flags;
168 
169 	if (WARN_ON_ONCE(!action->handler || !list_empty(&action->list)))
170 		return -EINVAL;
171 
172 	raw_spin_lock_irqsave(&desc->lock, flags);
173 
174 	/*
175 	 * Indicate if there are multiple registrations on the
176 	 * internal NMI handler call chains (SERR and IO_CHECK).
177 	 */
178 	WARN_ON_ONCE(type == NMI_SERR && !list_empty(&desc->head));
179 	WARN_ON_ONCE(type == NMI_IO_CHECK && !list_empty(&desc->head));
180 
181 	/*
182 	 * some handlers need to be executed first otherwise a fake
183 	 * event confuses some handlers (kdump uses this flag)
184 	 */
185 	if (action->flags & NMI_FLAG_FIRST)
186 		list_add_rcu(&action->list, &desc->head);
187 	else
188 		list_add_tail_rcu(&action->list, &desc->head);
189 
190 	raw_spin_unlock_irqrestore(&desc->lock, flags);
191 	return 0;
192 }
193 EXPORT_SYMBOL(__register_nmi_handler);
194 
195 void unregister_nmi_handler(unsigned int type, const char *name)
196 {
197 	struct nmi_desc *desc = nmi_to_desc(type);
198 	struct nmiaction *n, *found = NULL;
199 	unsigned long flags;
200 
201 	raw_spin_lock_irqsave(&desc->lock, flags);
202 
203 	list_for_each_entry_rcu(n, &desc->head, list) {
204 		/*
205 		 * the name passed in to describe the nmi handler
206 		 * is used as the lookup key
207 		 */
208 		if (!strcmp(n->name, name)) {
209 			WARN(in_nmi(),
210 				"Trying to free NMI (%s) from NMI context!\n", n->name);
211 			list_del_rcu(&n->list);
212 			found = n;
213 			break;
214 		}
215 	}
216 
217 	raw_spin_unlock_irqrestore(&desc->lock, flags);
218 	if (found) {
219 		synchronize_rcu();
220 		INIT_LIST_HEAD(&found->list);
221 	}
222 }
223 EXPORT_SYMBOL_GPL(unregister_nmi_handler);
224 
225 static void
226 pci_serr_error(unsigned char reason, struct pt_regs *regs)
227 {
228 	/* check to see if anyone registered against these types of errors */
229 	if (nmi_handle(NMI_SERR, regs))
230 		return;
231 
232 	pr_emerg("NMI: PCI system error (SERR) for reason %02x on CPU %d.\n",
233 		 reason, smp_processor_id());
234 
235 	if (panic_on_unrecovered_nmi)
236 		nmi_panic(regs, "NMI: Not continuing");
237 
238 	pr_emerg("Dazed and confused, but trying to continue\n");
239 
240 	/* Clear and disable the PCI SERR error line. */
241 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_SERR;
242 	outb(reason, NMI_REASON_PORT);
243 }
244 NOKPROBE_SYMBOL(pci_serr_error);
245 
246 static void
247 io_check_error(unsigned char reason, struct pt_regs *regs)
248 {
249 	unsigned long i;
250 
251 	/* check to see if anyone registered against these types of errors */
252 	if (nmi_handle(NMI_IO_CHECK, regs))
253 		return;
254 
255 	pr_emerg(
256 	"NMI: IOCK error (debug interrupt?) for reason %02x on CPU %d.\n",
257 		 reason, smp_processor_id());
258 	show_regs(regs);
259 
260 	if (panic_on_io_nmi) {
261 		nmi_panic(regs, "NMI IOCK error: Not continuing");
262 
263 		/*
264 		 * If we end up here, it means we have received an NMI while
265 		 * processing panic(). Simply return without delaying and
266 		 * re-enabling NMIs.
267 		 */
268 		return;
269 	}
270 
271 	/* Re-enable the IOCK line, wait for a few seconds */
272 	reason = (reason & NMI_REASON_CLEAR_MASK) | NMI_REASON_CLEAR_IOCHK;
273 	outb(reason, NMI_REASON_PORT);
274 
275 	i = 20000;
276 	while (--i) {
277 		touch_nmi_watchdog();
278 		udelay(100);
279 	}
280 
281 	reason &= ~NMI_REASON_CLEAR_IOCHK;
282 	outb(reason, NMI_REASON_PORT);
283 }
284 NOKPROBE_SYMBOL(io_check_error);
285 
286 static void
287 unknown_nmi_error(unsigned char reason, struct pt_regs *regs)
288 {
289 	int handled;
290 
291 	/*
292 	 * Use 'false' as back-to-back NMIs are dealt with one level up.
293 	 * Of course this makes having multiple 'unknown' handlers useless
294 	 * as only the first one is ever run (unless it can actually determine
295 	 * if it caused the NMI)
296 	 */
297 	handled = nmi_handle(NMI_UNKNOWN, regs);
298 	if (handled) {
299 		__this_cpu_add(nmi_stats.unknown, handled);
300 		return;
301 	}
302 
303 	__this_cpu_add(nmi_stats.unknown, 1);
304 
305 	pr_emerg("Uhhuh. NMI received for unknown reason %02x on CPU %d.\n",
306 		 reason, smp_processor_id());
307 
308 	if (unknown_nmi_panic || panic_on_unrecovered_nmi)
309 		nmi_panic(regs, "NMI: Not continuing");
310 
311 	pr_emerg("Dazed and confused, but trying to continue\n");
312 }
313 NOKPROBE_SYMBOL(unknown_nmi_error);
314 
315 static DEFINE_PER_CPU(bool, swallow_nmi);
316 static DEFINE_PER_CPU(unsigned long, last_nmi_rip);
317 
318 static noinstr void default_do_nmi(struct pt_regs *regs)
319 {
320 	unsigned char reason = 0;
321 	int handled;
322 	bool b2b = false;
323 
324 	/*
325 	 * CPU-specific NMI must be processed before non-CPU-specific
326 	 * NMI, otherwise we may lose it, because the CPU-specific
327 	 * NMI can not be detected/processed on other CPUs.
328 	 */
329 
330 	/*
331 	 * Back-to-back NMIs are interesting because they can either
332 	 * be two NMI or more than two NMIs (any thing over two is dropped
333 	 * due to NMI being edge-triggered).  If this is the second half
334 	 * of the back-to-back NMI, assume we dropped things and process
335 	 * more handlers.  Otherwise reset the 'swallow' NMI behaviour
336 	 */
337 	if (regs->ip == __this_cpu_read(last_nmi_rip))
338 		b2b = true;
339 	else
340 		__this_cpu_write(swallow_nmi, false);
341 
342 	__this_cpu_write(last_nmi_rip, regs->ip);
343 
344 	instrumentation_begin();
345 
346 	handled = nmi_handle(NMI_LOCAL, regs);
347 	__this_cpu_add(nmi_stats.normal, handled);
348 	if (handled) {
349 		/*
350 		 * There are cases when a NMI handler handles multiple
351 		 * events in the current NMI.  One of these events may
352 		 * be queued for in the next NMI.  Because the event is
353 		 * already handled, the next NMI will result in an unknown
354 		 * NMI.  Instead lets flag this for a potential NMI to
355 		 * swallow.
356 		 */
357 		if (handled > 1)
358 			__this_cpu_write(swallow_nmi, true);
359 		goto out;
360 	}
361 
362 	/*
363 	 * Non-CPU-specific NMI: NMI sources can be processed on any CPU.
364 	 *
365 	 * Another CPU may be processing panic routines while holding
366 	 * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping,
367 	 * and if so, call its callback directly.  If there is no CPU preparing
368 	 * crash dump, we simply loop here.
369 	 */
370 	while (!raw_spin_trylock(&nmi_reason_lock)) {
371 		run_crash_ipi_callback(regs);
372 		cpu_relax();
373 	}
374 
375 	reason = x86_platform.get_nmi_reason();
376 
377 	if (reason & NMI_REASON_MASK) {
378 		if (reason & NMI_REASON_SERR)
379 			pci_serr_error(reason, regs);
380 		else if (reason & NMI_REASON_IOCHK)
381 			io_check_error(reason, regs);
382 #ifdef CONFIG_X86_32
383 		/*
384 		 * Reassert NMI in case it became active
385 		 * meanwhile as it's edge-triggered:
386 		 */
387 		reassert_nmi();
388 #endif
389 		__this_cpu_add(nmi_stats.external, 1);
390 		raw_spin_unlock(&nmi_reason_lock);
391 		goto out;
392 	}
393 	raw_spin_unlock(&nmi_reason_lock);
394 
395 	/*
396 	 * Only one NMI can be latched at a time.  To handle
397 	 * this we may process multiple nmi handlers at once to
398 	 * cover the case where an NMI is dropped.  The downside
399 	 * to this approach is we may process an NMI prematurely,
400 	 * while its real NMI is sitting latched.  This will cause
401 	 * an unknown NMI on the next run of the NMI processing.
402 	 *
403 	 * We tried to flag that condition above, by setting the
404 	 * swallow_nmi flag when we process more than one event.
405 	 * This condition is also only present on the second half
406 	 * of a back-to-back NMI, so we flag that condition too.
407 	 *
408 	 * If both are true, we assume we already processed this
409 	 * NMI previously and we swallow it.  Otherwise we reset
410 	 * the logic.
411 	 *
412 	 * There are scenarios where we may accidentally swallow
413 	 * a 'real' unknown NMI.  For example, while processing
414 	 * a perf NMI another perf NMI comes in along with a
415 	 * 'real' unknown NMI.  These two NMIs get combined into
416 	 * one (as described above).  When the next NMI gets
417 	 * processed, it will be flagged by perf as handled, but
418 	 * no one will know that there was a 'real' unknown NMI sent
419 	 * also.  As a result it gets swallowed.  Or if the first
420 	 * perf NMI returns two events handled then the second
421 	 * NMI will get eaten by the logic below, again losing a
422 	 * 'real' unknown NMI.  But this is the best we can do
423 	 * for now.
424 	 */
425 	if (b2b && __this_cpu_read(swallow_nmi))
426 		__this_cpu_add(nmi_stats.swallow, 1);
427 	else
428 		unknown_nmi_error(reason, regs);
429 
430 out:
431 	instrumentation_end();
432 }
433 
434 /*
435  * NMIs can page fault or hit breakpoints which will cause it to lose
436  * its NMI context with the CPU when the breakpoint or page fault does an IRET.
437  *
438  * As a result, NMIs can nest if NMIs get unmasked due an IRET during
439  * NMI processing.  On x86_64, the asm glue protects us from nested NMIs
440  * if the outer NMI came from kernel mode, but we can still nest if the
441  * outer NMI came from user mode.
442  *
443  * To handle these nested NMIs, we have three states:
444  *
445  *  1) not running
446  *  2) executing
447  *  3) latched
448  *
449  * When no NMI is in progress, it is in the "not running" state.
450  * When an NMI comes in, it goes into the "executing" state.
451  * Normally, if another NMI is triggered, it does not interrupt
452  * the running NMI and the HW will simply latch it so that when
453  * the first NMI finishes, it will restart the second NMI.
454  * (Note, the latch is binary, thus multiple NMIs triggering,
455  *  when one is running, are ignored. Only one NMI is restarted.)
456  *
457  * If an NMI executes an iret, another NMI can preempt it. We do not
458  * want to allow this new NMI to run, but we want to execute it when the
459  * first one finishes.  We set the state to "latched", and the exit of
460  * the first NMI will perform a dec_return, if the result is zero
461  * (NOT_RUNNING), then it will simply exit the NMI handler. If not, the
462  * dec_return would have set the state to NMI_EXECUTING (what we want it
463  * to be when we are running). In this case, we simply jump back to
464  * rerun the NMI handler again, and restart the 'latched' NMI.
465  *
466  * No trap (breakpoint or page fault) should be hit before nmi_restart,
467  * thus there is no race between the first check of state for NOT_RUNNING
468  * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs
469  * at this point.
470  *
471  * In case the NMI takes a page fault, we need to save off the CR2
472  * because the NMI could have preempted another page fault and corrupt
473  * the CR2 that is about to be read. As nested NMIs must be restarted
474  * and they can not take breakpoints or page faults, the update of the
475  * CR2 must be done before converting the nmi state back to NOT_RUNNING.
476  * Otherwise, there would be a race of another nested NMI coming in
477  * after setting state to NOT_RUNNING but before updating the nmi_cr2.
478  */
479 enum nmi_states {
480 	NMI_NOT_RUNNING = 0,
481 	NMI_EXECUTING,
482 	NMI_LATCHED,
483 };
484 static DEFINE_PER_CPU(enum nmi_states, nmi_state);
485 static DEFINE_PER_CPU(unsigned long, nmi_cr2);
486 static DEFINE_PER_CPU(unsigned long, nmi_dr7);
487 
488 DEFINE_IDTENTRY_RAW(exc_nmi)
489 {
490 	irqentry_state_t irq_state;
491 	struct nmi_stats *nsp = this_cpu_ptr(&nmi_stats);
492 
493 	/*
494 	 * Re-enable NMIs right here when running as an SEV-ES guest. This might
495 	 * cause nested NMIs, but those can be handled safely.
496 	 */
497 	sev_es_nmi_complete();
498 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU))
499 		raw_atomic_long_inc(&nsp->idt_calls);
500 
501 	if (IS_ENABLED(CONFIG_SMP) && arch_cpu_is_offline(smp_processor_id()))
502 		return;
503 
504 	if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) {
505 		this_cpu_write(nmi_state, NMI_LATCHED);
506 		return;
507 	}
508 	this_cpu_write(nmi_state, NMI_EXECUTING);
509 	this_cpu_write(nmi_cr2, read_cr2());
510 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
511 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
512 		WARN_ON_ONCE(!(nsp->idt_seq & 0x1));
513 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
514 	}
515 nmi_restart:
516 
517 	/*
518 	 * Needs to happen before DR7 is accessed, because the hypervisor can
519 	 * intercept DR7 reads/writes, turning those into #VC exceptions.
520 	 */
521 	sev_es_ist_enter(regs);
522 
523 	this_cpu_write(nmi_dr7, local_db_save());
524 
525 	irq_state = irqentry_nmi_enter(regs);
526 
527 	inc_irq_stat(__nmi_count);
528 
529 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU) && ignore_nmis) {
530 		WRITE_ONCE(nsp->idt_ignored, nsp->idt_ignored + 1);
531 	} else if (!ignore_nmis) {
532 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
533 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
534 			WARN_ON_ONCE(!(nsp->idt_nmi_seq & 0x1));
535 		}
536 		default_do_nmi(regs);
537 		if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
538 			WRITE_ONCE(nsp->idt_nmi_seq, nsp->idt_nmi_seq + 1);
539 			WARN_ON_ONCE(nsp->idt_nmi_seq & 0x1);
540 		}
541 	}
542 
543 	irqentry_nmi_exit(regs, irq_state);
544 
545 	local_db_restore(this_cpu_read(nmi_dr7));
546 
547 	sev_es_ist_exit();
548 
549 	if (unlikely(this_cpu_read(nmi_cr2) != read_cr2()))
550 		write_cr2(this_cpu_read(nmi_cr2));
551 	if (this_cpu_dec_return(nmi_state))
552 		goto nmi_restart;
553 
554 	if (user_mode(regs))
555 		mds_user_clear_cpu_buffers();
556 	if (IS_ENABLED(CONFIG_NMI_CHECK_CPU)) {
557 		WRITE_ONCE(nsp->idt_seq, nsp->idt_seq + 1);
558 		WARN_ON_ONCE(nsp->idt_seq & 0x1);
559 		WRITE_ONCE(nsp->recv_jiffies, jiffies);
560 	}
561 }
562 
563 #if IS_ENABLED(CONFIG_KVM_INTEL)
564 DEFINE_IDTENTRY_RAW(exc_nmi_kvm_vmx)
565 {
566 	exc_nmi(regs);
567 }
568 #if IS_MODULE(CONFIG_KVM_INTEL)
569 EXPORT_SYMBOL_GPL(asm_exc_nmi_kvm_vmx);
570 #endif
571 #endif
572 
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 
646 void stop_nmi(void)
647 {
648 	ignore_nmis++;
649 }
650 
651 void restart_nmi(void)
652 {
653 	ignore_nmis--;
654 }
655 
656 /* reset the back-to-back NMI logic */
657 void local_touch_nmi(void)
658 {
659 	__this_cpu_write(last_nmi_rip, 0);
660 }
661 EXPORT_SYMBOL_GPL(local_touch_nmi);
662