xref: /linux-6.15/include/linux/nmi.h (revision 930d8f8d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  linux/include/linux/nmi.h
4  */
5 #ifndef LINUX_NMI_H
6 #define LINUX_NMI_H
7 
8 #include <linux/sched.h>
9 #include <asm/irq.h>
10 #if defined(CONFIG_HAVE_NMI_WATCHDOG)
11 #include <asm/nmi.h>
12 #endif
13 
14 #ifdef CONFIG_LOCKUP_DETECTOR
15 void lockup_detector_init(void);
16 void lockup_detector_retry_init(void);
17 void lockup_detector_soft_poweroff(void);
18 void lockup_detector_cleanup(void);
19 
20 extern int watchdog_user_enabled;
21 extern int watchdog_thresh;
22 extern unsigned long watchdog_enabled;
23 
24 extern struct cpumask watchdog_cpumask;
25 extern unsigned long *watchdog_cpumask_bits;
26 #ifdef CONFIG_SMP
27 extern int sysctl_softlockup_all_cpu_backtrace;
28 extern int sysctl_hardlockup_all_cpu_backtrace;
29 #else
30 #define sysctl_softlockup_all_cpu_backtrace 0
31 #define sysctl_hardlockup_all_cpu_backtrace 0
32 #endif /* !CONFIG_SMP */
33 
34 #else /* CONFIG_LOCKUP_DETECTOR */
35 static inline void lockup_detector_init(void) { }
36 static inline void lockup_detector_retry_init(void) { }
37 static inline void lockup_detector_soft_poweroff(void) { }
38 static inline void lockup_detector_cleanup(void) { }
39 #endif /* !CONFIG_LOCKUP_DETECTOR */
40 
41 #ifdef CONFIG_SOFTLOCKUP_DETECTOR
42 extern void touch_softlockup_watchdog_sched(void);
43 extern void touch_softlockup_watchdog(void);
44 extern void touch_softlockup_watchdog_sync(void);
45 extern void touch_all_softlockup_watchdogs(void);
46 extern unsigned int  softlockup_panic;
47 
48 extern int lockup_detector_online_cpu(unsigned int cpu);
49 extern int lockup_detector_offline_cpu(unsigned int cpu);
50 #else /* CONFIG_SOFTLOCKUP_DETECTOR */
51 static inline void touch_softlockup_watchdog_sched(void) { }
52 static inline void touch_softlockup_watchdog(void) { }
53 static inline void touch_softlockup_watchdog_sync(void) { }
54 static inline void touch_all_softlockup_watchdogs(void) { }
55 
56 #define lockup_detector_online_cpu	NULL
57 #define lockup_detector_offline_cpu	NULL
58 #endif /* CONFIG_SOFTLOCKUP_DETECTOR */
59 
60 #ifdef CONFIG_DETECT_HUNG_TASK
61 void reset_hung_task_detector(void);
62 #else
63 static inline void reset_hung_task_detector(void) { }
64 #endif
65 
66 /*
67  * The run state of the lockup detectors is controlled by the content of the
68  * 'watchdog_enabled' variable. Each lockup detector has its dedicated bit -
69  * bit 0 for the hard lockup detector and bit 1 for the soft lockup detector.
70  *
71  * 'watchdog_user_enabled', 'watchdog_hardlockup_user_enabled' and
72  * 'watchdog_softlockup_user_enabled' are variables that are only used as an
73  * 'interface' between the parameters in /proc/sys/kernel and the internal
74  * state bits in 'watchdog_enabled'. The 'watchdog_thresh' variable is
75  * handled differently because its value is not boolean, and the lockup
76  * detectors are 'suspended' while 'watchdog_thresh' is equal zero.
77  */
78 #define WATCHDOG_HARDLOCKUP_ENABLED_BIT  0
79 #define WATCHDOG_SOFTOCKUP_ENABLED_BIT   1
80 #define WATCHDOG_HARDLOCKUP_ENABLED     (1 << WATCHDOG_HARDLOCKUP_ENABLED_BIT)
81 #define WATCHDOG_SOFTOCKUP_ENABLED      (1 << WATCHDOG_SOFTOCKUP_ENABLED_BIT)
82 
83 #if defined(CONFIG_HARDLOCKUP_DETECTOR)
84 extern void hardlockup_detector_disable(void);
85 extern unsigned int hardlockup_panic;
86 #else
87 static inline void hardlockup_detector_disable(void) {}
88 #endif
89 
90 #if defined(CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER)
91 void arch_touch_nmi_watchdog(void);
92 void watchdog_hardlockup_touch_cpu(unsigned int cpu);
93 void watchdog_hardlockup_check(unsigned int cpu, struct pt_regs *regs);
94 #elif !defined(CONFIG_HAVE_NMI_WATCHDOG)
95 static inline void arch_touch_nmi_watchdog(void) { }
96 #endif
97 
98 #if defined(CONFIG_HAVE_NMI_WATCHDOG) || defined(CONFIG_HARDLOCKUP_DETECTOR)
99 # define NMI_WATCHDOG_SYSCTL_PERM	0644
100 #else
101 # define NMI_WATCHDOG_SYSCTL_PERM	0444
102 #endif
103 
104 #if defined(CONFIG_HARDLOCKUP_DETECTOR_PERF)
105 extern void hardlockup_detector_perf_stop(void);
106 extern void hardlockup_detector_perf_restart(void);
107 extern void hardlockup_detector_perf_cleanup(void);
108 #else
109 static inline void hardlockup_detector_perf_stop(void) { }
110 static inline void hardlockup_detector_perf_restart(void) { }
111 static inline void hardlockup_detector_perf_cleanup(void) { }
112 #endif
113 
114 void watchdog_hardlockup_stop(void);
115 void watchdog_hardlockup_start(void);
116 int watchdog_hardlockup_probe(void);
117 void watchdog_hardlockup_enable(unsigned int cpu);
118 void watchdog_hardlockup_disable(unsigned int cpu);
119 
120 void lockup_detector_reconfigure(void);
121 
122 #ifdef CONFIG_HARDLOCKUP_DETECTOR_BUDDY
123 void watchdog_buddy_check_hardlockup(unsigned long hrtimer_interrupts);
124 #else
125 static inline void watchdog_buddy_check_hardlockup(unsigned long hrtimer_interrupts) {}
126 #endif
127 
128 /**
129  * touch_nmi_watchdog - manually reset the hardlockup watchdog timeout.
130  *
131  * If we support detecting hardlockups, touch_nmi_watchdog() may be
132  * used to pet the watchdog (reset the timeout) - for code which
133  * intentionally disables interrupts for a long time. This call is stateless.
134  *
135  * Though this function has "nmi" in the name, the hardlockup watchdog might
136  * not be backed by NMIs. This function will likely be renamed to
137  * touch_hardlockup_watchdog() in the future.
138  */
139 static inline void touch_nmi_watchdog(void)
140 {
141 	/*
142 	 * Pass on to the hardlockup detector selected via CONFIG_. Note that
143 	 * the hardlockup detector may not be arch-specific nor using NMIs
144 	 * and the arch_touch_nmi_watchdog() function will likely be renamed
145 	 * in the future.
146 	 */
147 	arch_touch_nmi_watchdog();
148 
149 	/*
150 	 * Touching the hardlock detector implicitly resets the
151 	 * softlockup detector too
152 	 */
153 	touch_softlockup_watchdog();
154 }
155 
156 /*
157  * Create trigger_all_cpu_backtrace() out of the arch-provided
158  * base function. Return whether such support was available,
159  * to allow calling code to fall back to some other mechanism:
160  */
161 #ifdef arch_trigger_cpumask_backtrace
162 static inline bool trigger_all_cpu_backtrace(void)
163 {
164 	arch_trigger_cpumask_backtrace(cpu_online_mask, false);
165 	return true;
166 }
167 
168 static inline bool trigger_allbutself_cpu_backtrace(void)
169 {
170 	arch_trigger_cpumask_backtrace(cpu_online_mask, true);
171 	return true;
172 }
173 
174 static inline bool trigger_cpumask_backtrace(struct cpumask *mask)
175 {
176 	arch_trigger_cpumask_backtrace(mask, false);
177 	return true;
178 }
179 
180 static inline bool trigger_single_cpu_backtrace(int cpu)
181 {
182 	arch_trigger_cpumask_backtrace(cpumask_of(cpu), false);
183 	return true;
184 }
185 
186 /* generic implementation */
187 void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
188 				   bool exclude_self,
189 				   void (*raise)(cpumask_t *mask));
190 bool nmi_cpu_backtrace(struct pt_regs *regs);
191 
192 #else
193 static inline bool trigger_all_cpu_backtrace(void)
194 {
195 	return false;
196 }
197 static inline bool trigger_allbutself_cpu_backtrace(void)
198 {
199 	return false;
200 }
201 static inline bool trigger_cpumask_backtrace(struct cpumask *mask)
202 {
203 	return false;
204 }
205 static inline bool trigger_single_cpu_backtrace(int cpu)
206 {
207 	return false;
208 }
209 #endif
210 
211 #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
212 u64 hw_nmi_get_sample_period(int watchdog_thresh);
213 bool arch_perf_nmi_is_available(void);
214 #endif
215 
216 #if defined(CONFIG_HARDLOCKUP_CHECK_TIMESTAMP) && \
217     defined(CONFIG_HARDLOCKUP_DETECTOR_PERF)
218 void watchdog_update_hrtimer_threshold(u64 period);
219 #else
220 static inline void watchdog_update_hrtimer_threshold(u64 period) { }
221 #endif
222 
223 struct ctl_table;
224 int proc_watchdog(struct ctl_table *, int, void *, size_t *, loff_t *);
225 int proc_nmi_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *);
226 int proc_soft_watchdog(struct ctl_table *, int , void *, size_t *, loff_t *);
227 int proc_watchdog_thresh(struct ctl_table *, int , void *, size_t *, loff_t *);
228 int proc_watchdog_cpumask(struct ctl_table *, int, void *, size_t *, loff_t *);
229 
230 #ifdef CONFIG_HAVE_ACPI_APEI_NMI
231 #include <asm/nmi.h>
232 #endif
233 
234 #ifdef CONFIG_NMI_CHECK_CPU
235 void nmi_backtrace_stall_snap(const struct cpumask *btp);
236 void nmi_backtrace_stall_check(const struct cpumask *btp);
237 #else
238 static inline void nmi_backtrace_stall_snap(const struct cpumask *btp) {}
239 static inline void nmi_backtrace_stall_check(const struct cpumask *btp) {}
240 #endif
241 
242 #endif
243