watchdog/hardlockup/perf: Use atomics to track in-use cpu counter
[sfrench/cifs-2.6.git] / kernel / watchdog_hld.c
index 71a62ceacdc883aa77a711e4b1bfba8c3185dceb..a84b205fac9a0be08c6e96570857260efe5b3f41 100644 (file)
@@ -12,6 +12,7 @@
 #define pr_fmt(fmt) "NMI watchdog: " fmt
 
 #include <linux/nmi.h>
+#include <linux/atomic.h>
 #include <linux/module.h>
 #include <linux/sched/debug.h>
 
 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
+static DEFINE_PER_CPU(struct perf_event *, dead_event);
 static struct cpumask dead_events_mask;
 
 static unsigned long hardlockup_allcpu_dumped;
-static unsigned int watchdog_cpus;
+static atomic_t watchdog_cpus = ATOMIC_INIT(0);
 
 void arch_touch_nmi_watchdog(void)
 {
@@ -188,7 +190,8 @@ void hardlockup_detector_perf_enable(void)
        if (hardlockup_detector_event_create())
                return;
 
-       if (!watchdog_cpus++)
+       /* use original value for check */
+       if (!atomic_fetch_inc(&watchdog_cpus))
                pr_info("Enabled. Permanently consumes one hw-PMU counter.\n");
 
        perf_event_enable(this_cpu_read(watchdog_ev));
@@ -203,8 +206,10 @@ void hardlockup_detector_perf_disable(void)
 
        if (event) {
                perf_event_disable(event);
+               this_cpu_write(watchdog_ev, NULL);
+               this_cpu_write(dead_event, event);
                cpumask_set_cpu(smp_processor_id(), &dead_events_mask);
-               watchdog_cpus--;
+               atomic_dec(&watchdog_cpus);
        }
 }
 
@@ -218,7 +223,7 @@ void hardlockup_detector_perf_cleanup(void)
        int cpu;
 
        for_each_cpu(cpu, &dead_events_mask) {
-               struct perf_event *event = per_cpu(watchdog_ev, cpu);
+               struct perf_event *event = per_cpu(dead_event, cpu);
 
                /*
                 * Required because for_each_cpu() reports  unconditionally
@@ -226,7 +231,7 @@ void hardlockup_detector_perf_cleanup(void)
                 */
                if (event)
                        perf_event_release_kernel(event);
-               per_cpu(watchdog_ev, cpu) = NULL;
+               per_cpu(dead_event, cpu) = NULL;
        }
        cpumask_clear(&dead_events_mask);
 }