mmc: sdhci-xenon: Fix clock resource by adding an optional bus clock
[platform/kernel/linux-exynos.git] / kernel / watchdog_hld.c
1 /*
2  * Detect hard lockups on a system
3  *
4  * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
5  *
6  * Note: Most of this code is borrowed heavily from the original softlockup
7  * detector, so thanks to Ingo for the initial implementation.
8  * Some chunks also taken from the old x86-specific nmi watchdog code, thanks
9  * to those contributors as well.
10  */
11
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
13
14 #include <linux/nmi.h>
15 #include <linux/module.h>
16 #include <linux/sched/debug.h>
17
18 #include <asm/irq_regs.h>
19 #include <linux/perf_event.h>
20
21 static DEFINE_PER_CPU(bool, hard_watchdog_warn);
22 static DEFINE_PER_CPU(bool, watchdog_nmi_touch);
23 static DEFINE_PER_CPU(struct perf_event *, watchdog_ev);
24
25 static unsigned long hardlockup_allcpu_dumped;
26
27 void arch_touch_nmi_watchdog(void)
28 {
29         /*
30          * Using __raw here because some code paths have
31          * preemption enabled.  If preemption is enabled
32          * then interrupts should be enabled too, in which
33          * case we shouldn't have to worry about the watchdog
34          * going off.
35          */
36         raw_cpu_write(watchdog_nmi_touch, true);
37 }
38 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
39
40 #ifdef CONFIG_HARDLOCKUP_CHECK_TIMESTAMP
41 static DEFINE_PER_CPU(ktime_t, last_timestamp);
42 static DEFINE_PER_CPU(unsigned int, nmi_rearmed);
43 static ktime_t watchdog_hrtimer_sample_threshold __read_mostly;
44
45 void watchdog_update_hrtimer_threshold(u64 period)
46 {
47         /*
48          * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
49          *
50          * So it runs effectively with 2.5 times the rate of the NMI
51          * watchdog. That means the hrtimer should fire 2-3 times before
52          * the NMI watchdog expires. The NMI watchdog on x86 is based on
53          * unhalted CPU cycles, so if Turbo-Mode is enabled the CPU cycles
54          * might run way faster than expected and the NMI fires in a
55          * smaller period than the one deduced from the nominal CPU
56          * frequency. Depending on the Turbo-Mode factor this might be fast
57          * enough to get the NMI period smaller than the hrtimer watchdog
58          * period and trigger false positives.
59          *
60          * The sample threshold is used to check in the NMI handler whether
61          * the minimum time between two NMI samples has elapsed. That
62          * prevents false positives.
63          *
64          * Set this to 4/5 of the actual watchdog threshold period so the
65          * hrtimer is guaranteed to fire at least once within the real
66          * watchdog threshold.
67          */
68         watchdog_hrtimer_sample_threshold = period * 2;
69 }
70
71 static bool watchdog_check_timestamp(void)
72 {
73         ktime_t delta, now = ktime_get_mono_fast_ns();
74
75         delta = now - __this_cpu_read(last_timestamp);
76         if (delta < watchdog_hrtimer_sample_threshold) {
77                 /*
78                  * If ktime is jiffies based, a stalled timer would prevent
79                  * jiffies from being incremented and the filter would look
80                  * at a stale timestamp and never trigger.
81                  */
82                 if (__this_cpu_inc_return(nmi_rearmed) < 10)
83                         return false;
84         }
85         __this_cpu_write(nmi_rearmed, 0);
86         __this_cpu_write(last_timestamp, now);
87         return true;
88 }
89 #else
90 static inline bool watchdog_check_timestamp(void)
91 {
92         return true;
93 }
94 #endif
95
96 static struct perf_event_attr wd_hw_attr = {
97         .type           = PERF_TYPE_HARDWARE,
98         .config         = PERF_COUNT_HW_CPU_CYCLES,
99         .size           = sizeof(struct perf_event_attr),
100         .pinned         = 1,
101         .disabled       = 1,
102 };
103
104 /* Callback function for perf event subsystem */
105 static void watchdog_overflow_callback(struct perf_event *event,
106                  struct perf_sample_data *data,
107                  struct pt_regs *regs)
108 {
109         /* Ensure the watchdog never gets throttled */
110         event->hw.interrupts = 0;
111
112         if (atomic_read(&watchdog_park_in_progress) != 0)
113                 return;
114
115         if (__this_cpu_read(watchdog_nmi_touch) == true) {
116                 __this_cpu_write(watchdog_nmi_touch, false);
117                 return;
118         }
119
120         if (!watchdog_check_timestamp())
121                 return;
122
123         /* check for a hardlockup
124          * This is done by making sure our timer interrupt
125          * is incrementing.  The timer interrupt should have
126          * fired multiple times before we overflow'd.  If it hasn't
127          * then this is a good indication the cpu is stuck
128          */
129         if (is_hardlockup()) {
130                 int this_cpu = smp_processor_id();
131
132                 /* only print hardlockups once */
133                 if (__this_cpu_read(hard_watchdog_warn) == true)
134                         return;
135
136                 pr_emerg("Watchdog detected hard LOCKUP on cpu %d", this_cpu);
137                 print_modules();
138                 print_irqtrace_events(current);
139                 if (regs)
140                         show_regs(regs);
141                 else
142                         dump_stack();
143
144                 /*
145                  * Perform all-CPU dump only once to avoid multiple hardlockups
146                  * generating interleaving traces
147                  */
148                 if (sysctl_hardlockup_all_cpu_backtrace &&
149                                 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
150                         trigger_allbutself_cpu_backtrace();
151
152                 if (hardlockup_panic)
153                         nmi_panic(regs, "Hard LOCKUP");
154
155                 __this_cpu_write(hard_watchdog_warn, true);
156                 return;
157         }
158
159         __this_cpu_write(hard_watchdog_warn, false);
160         return;
161 }
162
163 /*
164  * People like the simple clean cpu node info on boot.
165  * Reduce the watchdog noise by only printing messages
166  * that are different from what cpu0 displayed.
167  */
168 static unsigned long firstcpu_err;
169 static atomic_t watchdog_cpus;
170
171 int watchdog_nmi_enable(unsigned int cpu)
172 {
173         struct perf_event_attr *wd_attr;
174         struct perf_event *event = per_cpu(watchdog_ev, cpu);
175         int firstcpu = 0;
176
177         /* nothing to do if the hard lockup detector is disabled */
178         if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
179                 goto out;
180
181         /* is it already setup and enabled? */
182         if (event && event->state > PERF_EVENT_STATE_OFF)
183                 goto out;
184
185         /* it is setup but not enabled */
186         if (event != NULL)
187                 goto out_enable;
188
189         if (atomic_inc_return(&watchdog_cpus) == 1)
190                 firstcpu = 1;
191
192         wd_attr = &wd_hw_attr;
193         wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
194
195         /* Try to register using hardware perf events */
196         event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
197
198         /* save the first cpu's error for future comparision */
199         if (firstcpu && IS_ERR(event))
200                 firstcpu_err = PTR_ERR(event);
201
202         if (!IS_ERR(event)) {
203                 /* only print for the first cpu initialized */
204                 if (firstcpu || firstcpu_err)
205                         pr_info("enabled on all CPUs, permanently consumes one hw-PMU counter.\n");
206                 goto out_save;
207         }
208
209         /*
210          * Disable the hard lockup detector if _any_ CPU fails to set up
211          * set up the hardware perf event. The watchdog() function checks
212          * the NMI_WATCHDOG_ENABLED bit periodically.
213          *
214          * The barriers are for syncing up watchdog_enabled across all the
215          * cpus, as clear_bit() does not use barriers.
216          */
217         smp_mb__before_atomic();
218         clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
219         smp_mb__after_atomic();
220
221         /* skip displaying the same error again */
222         if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
223                 return PTR_ERR(event);
224
225         /* vary the KERN level based on the returned errno */
226         if (PTR_ERR(event) == -EOPNOTSUPP)
227                 pr_info("disabled (cpu%i): not supported (no LAPIC?)\n", cpu);
228         else if (PTR_ERR(event) == -ENOENT)
229                 pr_warn("disabled (cpu%i): hardware events not enabled\n",
230                          cpu);
231         else
232                 pr_err("disabled (cpu%i): unable to create perf event: %ld\n",
233                         cpu, PTR_ERR(event));
234
235         pr_info("Shutting down hard lockup detector on all cpus\n");
236
237         return PTR_ERR(event);
238
239         /* success path */
240 out_save:
241         per_cpu(watchdog_ev, cpu) = event;
242 out_enable:
243         perf_event_enable(per_cpu(watchdog_ev, cpu));
244 out:
245         return 0;
246 }
247
248 void watchdog_nmi_disable(unsigned int cpu)
249 {
250         struct perf_event *event = per_cpu(watchdog_ev, cpu);
251
252         if (event) {
253                 perf_event_disable(event);
254                 per_cpu(watchdog_ev, cpu) = NULL;
255
256                 /* should be in cleanup, but blocks oprofile */
257                 perf_event_release_kernel(event);
258
259                 /* watchdog_nmi_enable() expects this to be zero initially. */
260                 if (atomic_dec_and_test(&watchdog_cpus))
261                         firstcpu_err = 0;
262         }
263 }