~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/watchdog_perf.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/watchdog_perf.c (Version linux-6.11.5) and /kernel/watchdog_perf.c (Version linux-6.7.12)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * Detect hard lockups on a system using perf       3  * Detect hard lockups on a system using perf
  4  *                                                  4  *
  5  * started by Don Zickus, Copyright (C) 2010 R      5  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
  6  *                                                  6  *
  7  * Note: Most of this code is borrowed heavily      7  * Note: Most of this code is borrowed heavily from the original softlockup
  8  * detector, so thanks to Ingo for the initial      8  * detector, so thanks to Ingo for the initial implementation.
  9  * Some chunks also taken from the old x86-spe      9  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
 10  * to those contributors as well.                  10  * to those contributors as well.
 11  */                                                11  */
 12                                                    12 
 13 #define pr_fmt(fmt) "NMI watchdog: " fmt           13 #define pr_fmt(fmt) "NMI watchdog: " fmt
 14                                                    14 
 15 #include <linux/nmi.h>                             15 #include <linux/nmi.h>
 16 #include <linux/atomic.h>                          16 #include <linux/atomic.h>
 17 #include <linux/module.h>                          17 #include <linux/module.h>
 18 #include <linux/sched/debug.h>                     18 #include <linux/sched/debug.h>
 19                                                    19 
 20 #include <asm/irq_regs.h>                          20 #include <asm/irq_regs.h>
 21 #include <linux/perf_event.h>                      21 #include <linux/perf_event.h>
 22                                                    22 
 23 static DEFINE_PER_CPU(struct perf_event *, wat     23 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
 24 static DEFINE_PER_CPU(struct perf_event *, dea     24 static DEFINE_PER_CPU(struct perf_event *, dead_event);
 25 static struct cpumask dead_events_mask;            25 static struct cpumask dead_events_mask;
 26                                                    26 
 27 static atomic_t watchdog_cpus = ATOMIC_INIT(0)     27 static atomic_t watchdog_cpus = ATOMIC_INIT(0);
 28                                                    28 
 29 #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP           29 #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
 30 static DEFINE_PER_CPU(ktime_t, last_timestamp)     30 static DEFINE_PER_CPU(ktime_t, last_timestamp);
 31 static DEFINE_PER_CPU(unsigned int, nmi_rearme     31 static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
 32 static ktime_t watchdog_hrtimer_sample_thresho     32 static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
 33                                                    33 
 34 void watchdog_update_hrtimer_threshold(u64 per     34 void watchdog_update_hrtimer_threshold(u64 period)
 35 {                                                  35 {
 36         /*                                         36         /*
 37          * The hrtimer runs with a period of (     37          * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
 38          *                                         38          *
 39          * So it runs effectively with 2.5 tim     39          * So it runs effectively with 2.5 times the rate of the NMI
 40          * watchdog. That means the hrtimer sh     40          * watchdog. That means the hrtimer should fire 2-3 times before
 41          * the NMI watchdog expires. The NMI w     41          * the NMI watchdog expires. The NMI watchdog on x86 is based on
 42          * unhalted CPU cycles, so if Turbo-Mo     42          * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles
 43          * might run way faster than expected      43          * might run way faster than expected and the NMI fires in a
 44          * smaller period than the one deduced     44          * smaller period than the one deduced from the nominal CPU
 45          * frequency. Depending on the Turbo-M     45          * frequency. Depending on the Turbo-Mode factor this might be fast
 46          * enough to get the NMI period smalle     46          * enough to get the NMI period smaller than the hrtimer watchdog
 47          * period and trigger false positives.     47          * period and trigger false positives.
 48          *                                         48          *
 49          * The sample threshold is used to che     49          * The sample threshold is used to check in the NMI handler whether
 50          * the minimum time between two NMI sa     50          * the minimum time between two NMI samples has elapsed. That
 51          * prevents false positives.               51          * prevents false positives.
 52          *                                         52          *
 53          * Set this to 4/5 of the actual watch     53          * Set this to 4/5 of the actual watchdog threshold period so the
 54          * hrtimer is guaranteed to fire at le     54          * hrtimer is guaranteed to fire at least once within the real
 55          * watchdog threshold.                     55          * watchdog threshold.
 56          */                                        56          */
 57         watchdog_hrtimer_sample_threshold = pe     57         watchdog_hrtimer_sample_threshold = period * 2;
 58 }                                                  58 }
 59                                                    59 
 60 static bool watchdog_check_timestamp(void)         60 static bool watchdog_check_timestamp(void)
 61 {                                                  61 {
 62         ktime_t delta, now = ktime_get_mono_fa     62         ktime_t delta, now = ktime_get_mono_fast_ns();
 63                                                    63 
 64         delta = now - __this_cpu_read(last_tim     64         delta = now - __this_cpu_read(last_timestamp);
 65         if (delta < watchdog_hrtimer_sample_th     65         if (delta < watchdog_hrtimer_sample_threshold) {
 66                 /*                                 66                 /*
 67                  * If ktime is jiffies based,      67                  * If ktime is jiffies based, a stalled timer would prevent
 68                  * jiffies from being incremen     68                  * jiffies from being incremented and the filter would look
 69                  * at a stale timestamp and ne     69                  * at a stale timestamp and never trigger.
 70                  */                                70                  */
 71                 if (__this_cpu_inc_return(nmi_     71                 if (__this_cpu_inc_return(nmi_rearmed) < 10)
 72                         return false;              72                         return false;
 73         }                                          73         }
 74         __this_cpu_write(nmi_rearmed, 0);          74         __this_cpu_write(nmi_rearmed, 0);
 75         __this_cpu_write(last_timestamp, now);     75         __this_cpu_write(last_timestamp, now);
 76         return true;                               76         return true;
 77 }                                                  77 }
 78                                                !!  78 #else
 79 static void watchdog_init_timestamp(void)      !!  79 static inline bool watchdog_check_timestamp(void)
 80 {                                                  80 {
 81         __this_cpu_write(nmi_rearmed, 0);      !!  81         return true;
 82         __this_cpu_write(last_timestamp, ktime << 
 83 }                                                  82 }
 84 #else                                          << 
 85 static inline bool watchdog_check_timestamp(vo << 
 86 static inline void watchdog_init_timestamp(voi << 
 87 #endif                                             83 #endif
 88                                                    84 
 89 static struct perf_event_attr wd_hw_attr = {       85 static struct perf_event_attr wd_hw_attr = {
 90         .type           = PERF_TYPE_HARDWARE,      86         .type           = PERF_TYPE_HARDWARE,
 91         .config         = PERF_COUNT_HW_CPU_CY     87         .config         = PERF_COUNT_HW_CPU_CYCLES,
 92         .size           = sizeof(struct perf_e     88         .size           = sizeof(struct perf_event_attr),
 93         .pinned         = 1,                       89         .pinned         = 1,
 94         .disabled       = 1,                       90         .disabled       = 1,
 95 };                                                 91 };
 96                                                    92 
 97 static struct perf_event_attr fallback_wd_hw_a << 
 98         .type           = PERF_TYPE_HARDWARE,  << 
 99         .config         = PERF_COUNT_HW_CPU_CY << 
100         .size           = sizeof(struct perf_e << 
101         .pinned         = 1,                   << 
102         .disabled       = 1,                   << 
103 };                                             << 
104                                                << 
105 /* Callback function for perf event subsystem      93 /* Callback function for perf event subsystem */
106 static void watchdog_overflow_callback(struct      94 static void watchdog_overflow_callback(struct perf_event *event,
107                                        struct      95                                        struct perf_sample_data *data,
108                                        struct      96                                        struct pt_regs *regs)
109 {                                                  97 {
110         /* Ensure the watchdog never gets thro     98         /* Ensure the watchdog never gets throttled */
111         event->hw.interrupts = 0;                  99         event->hw.interrupts = 0;
112                                                   100 
113         if (!watchdog_check_timestamp())          101         if (!watchdog_check_timestamp())
114                 return;                           102                 return;
115                                                   103 
116         watchdog_hardlockup_check(smp_processo    104         watchdog_hardlockup_check(smp_processor_id(), regs);
117 }                                                 105 }
118                                                   106 
119 static int hardlockup_detector_event_create(vo    107 static int hardlockup_detector_event_create(void)
120 {                                                 108 {
121         unsigned int cpu;                         109         unsigned int cpu;
122         struct perf_event_attr *wd_attr;          110         struct perf_event_attr *wd_attr;
123         struct perf_event *evt;                   111         struct perf_event *evt;
124                                                   112 
125         /*                                        113         /*
126          * Preemption is not disabled because     114          * Preemption is not disabled because memory will be allocated.
127          * Ensure CPU-locality by calling this    115          * Ensure CPU-locality by calling this in per-CPU kthread.
128          */                                       116          */
129         WARN_ON(!is_percpu_thread());             117         WARN_ON(!is_percpu_thread());
130         cpu = raw_smp_processor_id();             118         cpu = raw_smp_processor_id();
131         wd_attr = &wd_hw_attr;                    119         wd_attr = &wd_hw_attr;
132         wd_attr->sample_period = hw_nmi_get_sa    120         wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
133                                                   121 
134         /* Try to register using hardware perf    122         /* Try to register using hardware perf events */
135         evt = perf_event_create_kernel_counter    123         evt = perf_event_create_kernel_counter(wd_attr, cpu, NULL,
136                                                   124                                                watchdog_overflow_callback, NULL);
137         if (IS_ERR(evt)) {                        125         if (IS_ERR(evt)) {
138                 wd_attr = &fallback_wd_hw_attr << 
139                 wd_attr->sample_period = hw_nm << 
140                 evt = perf_event_create_kernel << 
141                                                << 
142         }                                      << 
143                                                << 
144         if (IS_ERR(evt)) {                     << 
145                 pr_debug("Perf event create on    126                 pr_debug("Perf event create on CPU %d failed with %ld\n", cpu,
146                          PTR_ERR(evt));           127                          PTR_ERR(evt));
147                 return PTR_ERR(evt);              128                 return PTR_ERR(evt);
148         }                                         129         }
149         this_cpu_write(watchdog_ev, evt);         130         this_cpu_write(watchdog_ev, evt);
150         return 0;                                 131         return 0;
151 }                                                 132 }
152                                                   133 
153 /**                                               134 /**
154  * watchdog_hardlockup_enable - Enable the loc    135  * watchdog_hardlockup_enable - Enable the local event
                                                   >> 136  *
155  * @cpu: The CPU to enable hard lockup on.        137  * @cpu: The CPU to enable hard lockup on.
156  */                                               138  */
157 void watchdog_hardlockup_enable(unsigned int c    139 void watchdog_hardlockup_enable(unsigned int cpu)
158 {                                                 140 {
159         WARN_ON_ONCE(cpu != smp_processor_id()    141         WARN_ON_ONCE(cpu != smp_processor_id());
160                                                   142 
161         if (hardlockup_detector_event_create()    143         if (hardlockup_detector_event_create())
162                 return;                           144                 return;
163                                                   145 
164         /* use original value for check */        146         /* use original value for check */
165         if (!atomic_fetch_inc(&watchdog_cpus))    147         if (!atomic_fetch_inc(&watchdog_cpus))
166                 pr_info("Enabled. Permanently     148                 pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
167                                                   149 
168         watchdog_init_timestamp();             << 
169         perf_event_enable(this_cpu_read(watchd    150         perf_event_enable(this_cpu_read(watchdog_ev));
170 }                                                 151 }
171                                                   152 
172 /**                                               153 /**
173  * watchdog_hardlockup_disable - Disable the l    154  * watchdog_hardlockup_disable - Disable the local event
                                                   >> 155  *
174  * @cpu: The CPU to enable hard lockup on.        156  * @cpu: The CPU to enable hard lockup on.
175  */                                               157  */
176 void watchdog_hardlockup_disable(unsigned int     158 void watchdog_hardlockup_disable(unsigned int cpu)
177 {                                                 159 {
178         struct perf_event *event = this_cpu_re    160         struct perf_event *event = this_cpu_read(watchdog_ev);
179                                                   161 
180         WARN_ON_ONCE(cpu != smp_processor_id()    162         WARN_ON_ONCE(cpu != smp_processor_id());
181                                                   163 
182         if (event) {                              164         if (event) {
183                 perf_event_disable(event);        165                 perf_event_disable(event);
184                 this_cpu_write(watchdog_ev, NU    166                 this_cpu_write(watchdog_ev, NULL);
185                 this_cpu_write(dead_event, eve    167                 this_cpu_write(dead_event, event);
186                 cpumask_set_cpu(smp_processor_    168                 cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
187                 atomic_dec(&watchdog_cpus);       169                 atomic_dec(&watchdog_cpus);
188         }                                         170         }
189 }                                                 171 }
190                                                   172 
191 /**                                               173 /**
192  * hardlockup_detector_perf_cleanup - Cleanup     174  * hardlockup_detector_perf_cleanup - Cleanup disabled events and destroy them
193  *                                                175  *
194  * Called from lockup_detector_cleanup(). Seri    176  * Called from lockup_detector_cleanup(). Serialized by the caller.
195  */                                               177  */
196 void hardlockup_detector_perf_cleanup(void)       178 void hardlockup_detector_perf_cleanup(void)
197 {                                                 179 {
198         int cpu;                                  180         int cpu;
199                                                   181 
200         for_each_cpu(cpu, &dead_events_mask) {    182         for_each_cpu(cpu, &dead_events_mask) {
201                 struct perf_event *event = per    183                 struct perf_event *event = per_cpu(dead_event, cpu);
202                                                   184 
203                 /*                                185                 /*
204                  * Required because for_each_c    186                  * Required because for_each_cpu() reports  unconditionally
205                  * CPU0 as set on UP kernels.     187                  * CPU0 as set on UP kernels. Sigh.
206                  */                               188                  */
207                 if (event)                        189                 if (event)
208                         perf_event_release_ker    190                         perf_event_release_kernel(event);
209                 per_cpu(dead_event, cpu) = NUL    191                 per_cpu(dead_event, cpu) = NULL;
210         }                                         192         }
211         cpumask_clear(&dead_events_mask);         193         cpumask_clear(&dead_events_mask);
212 }                                                 194 }
213                                                   195 
214 /**                                               196 /**
215  * hardlockup_detector_perf_stop - Globally st    197  * hardlockup_detector_perf_stop - Globally stop watchdog events
216  *                                                198  *
217  * Special interface for x86 to handle the per    199  * Special interface for x86 to handle the perf HT bug.
218  */                                               200  */
219 void __init hardlockup_detector_perf_stop(void    201 void __init hardlockup_detector_perf_stop(void)
220 {                                                 202 {
221         int cpu;                                  203         int cpu;
222                                                   204 
223         lockdep_assert_cpus_held();               205         lockdep_assert_cpus_held();
224                                                   206 
225         for_each_online_cpu(cpu) {                207         for_each_online_cpu(cpu) {
226                 struct perf_event *event = per    208                 struct perf_event *event = per_cpu(watchdog_ev, cpu);
227                                                   209 
228                 if (event)                        210                 if (event)
229                         perf_event_disable(eve    211                         perf_event_disable(event);
230         }                                         212         }
231 }                                                 213 }
232                                                   214 
233 /**                                               215 /**
234  * hardlockup_detector_perf_restart - Globally    216  * hardlockup_detector_perf_restart - Globally restart watchdog events
235  *                                                217  *
236  * Special interface for x86 to handle the per    218  * Special interface for x86 to handle the perf HT bug.
237  */                                               219  */
238 void __init hardlockup_detector_perf_restart(v    220 void __init hardlockup_detector_perf_restart(void)
239 {                                                 221 {
240         int cpu;                                  222         int cpu;
241                                                   223 
242         lockdep_assert_cpus_held();               224         lockdep_assert_cpus_held();
243                                                   225 
244         if (!(watchdog_enabled & WATCHDOG_HARD    226         if (!(watchdog_enabled & WATCHDOG_HARDLOCKUP_ENABLED))
245                 return;                           227                 return;
246                                                   228 
247         for_each_online_cpu(cpu) {                229         for_each_online_cpu(cpu) {
248                 struct perf_event *event = per    230                 struct perf_event *event = per_cpu(watchdog_ev, cpu);
249                                                   231 
250                 if (event)                        232                 if (event)
251                         perf_event_enable(even    233                         perf_event_enable(event);
252         }                                         234         }
253 }                                                 235 }
254                                                   236 
255 bool __weak __init arch_perf_nmi_is_available(    237 bool __weak __init arch_perf_nmi_is_available(void)
256 {                                                 238 {
257         return true;                              239         return true;
258 }                                                 240 }
259                                                   241 
260 /**                                               242 /**
261  * watchdog_hardlockup_probe - Probe whether N    243  * watchdog_hardlockup_probe - Probe whether NMI event is available at all
262  */                                               244  */
263 int __init watchdog_hardlockup_probe(void)        245 int __init watchdog_hardlockup_probe(void)
264 {                                                 246 {
265         int ret;                                  247         int ret;
266                                                   248 
267         if (!arch_perf_nmi_is_available())        249         if (!arch_perf_nmi_is_available())
268                 return -ENODEV;                   250                 return -ENODEV;
269                                                   251 
270         ret = hardlockup_detector_event_create    252         ret = hardlockup_detector_event_create();
271                                                   253 
272         if (ret) {                                254         if (ret) {
273                 pr_info("Perf NMI watchdog per    255                 pr_info("Perf NMI watchdog permanently disabled\n");
274         } else {                                  256         } else {
275                 perf_event_release_kernel(this    257                 perf_event_release_kernel(this_cpu_read(watchdog_ev));
276                 this_cpu_write(watchdog_ev, NU    258                 this_cpu_write(watchdog_ev, NULL);
277         }                                         259         }
278         return ret;                               260         return ret;
279 }                                              << 
280                                                << 
281 /**                                            << 
282  * hardlockup_config_perf_event - Overwrite co << 
283  * @str: number which identifies the raw perf  << 
284  */                                            << 
285 void __init hardlockup_config_perf_event(const << 
286 {                                              << 
287         u64 config;                            << 
288         char buf[24];                          << 
289         char *comma = strchr(str, ',');        << 
290                                                << 
291         if (!comma) {                          << 
292                 if (kstrtoull(str, 16, &config << 
293                         return;                << 
294         } else {                               << 
295                 unsigned int len = comma - str << 
296                                                << 
297                 if (len >= sizeof(buf))        << 
298                         return;                << 
299                                                << 
300                 if (strscpy(buf, str, sizeof(b << 
301                         return;                << 
302                 buf[len] = 0;                  << 
303                 if (kstrtoull(buf, 16, &config << 
304                         return;                << 
305         }                                      << 
306                                                << 
307         wd_hw_attr.type = PERF_TYPE_RAW;       << 
308         wd_hw_attr.config = config;            << 
309 }                                                 261 }
310                                                   262 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php