ANDROID: binder: don't queue async transactions to thread.
[linux/fpc-iii.git] / kernel / watchdog_hld.c
blob3a09ea1b1d3d5e6e284d058052403ac1396804ca
1 /*
2 * Detect hard lockups on a system
4 * started by Don Zickus, Copyright (C) 2010 Red Hat, Inc.
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.
12 #define pr_fmt(fmt) "NMI watchdog: " fmt
14 #include <linux/nmi.h>
15 #include <linux/module.h>
16 #include <linux/sched/debug.h>
18 #include <asm/irq_regs.h>
19 #include <linux/perf_event.h>
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);
25 static unsigned long hardlockup_allcpu_dumped;
27 void arch_touch_nmi_watchdog(void)
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.
36 raw_cpu_write(watchdog_nmi_touch, true);
38 EXPORT_SYMBOL(arch_touch_nmi_watchdog);
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;
45 void watchdog_update_hrtimer_threshold(u64 period)
48 * The hrtimer runs with a period of (watchdog_threshold * 2) / 5
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.
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.
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.
68 watchdog_hrtimer_sample_threshold = period * 2;
71 static bool watchdog_check_timestamp(void)
73 ktime_t delta, now = ktime_get_mono_fast_ns();
75 delta = now - __this_cpu_read(last_timestamp);
76 if (delta < watchdog_hrtimer_sample_threshold) {
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.
82 if (__this_cpu_inc_return(nmi_rearmed) < 10)
83 return false;
85 __this_cpu_write(nmi_rearmed, 0);
86 __this_cpu_write(last_timestamp, now);
87 return true;
89 #else
90 static inline bool watchdog_check_timestamp(void)
92 return true;
94 #endif
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,
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)
109 /* Ensure the watchdog never gets throttled */
110 event->hw.interrupts = 0;
112 if (atomic_read(&watchdog_park_in_progress) != 0)
113 return;
115 if (__this_cpu_read(watchdog_nmi_touch) == true) {
116 __this_cpu_write(watchdog_nmi_touch, false);
117 return;
120 if (!watchdog_check_timestamp())
121 return;
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
129 if (is_hardlockup()) {
130 int this_cpu = smp_processor_id();
132 /* only print hardlockups once */
133 if (__this_cpu_read(hard_watchdog_warn) == true)
134 return;
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();
145 * Perform all-CPU dump only once to avoid multiple hardlockups
146 * generating interleaving traces
148 if (sysctl_hardlockup_all_cpu_backtrace &&
149 !test_and_set_bit(0, &hardlockup_allcpu_dumped))
150 trigger_allbutself_cpu_backtrace();
152 if (hardlockup_panic)
153 nmi_panic(regs, "Hard LOCKUP");
155 __this_cpu_write(hard_watchdog_warn, true);
156 return;
159 __this_cpu_write(hard_watchdog_warn, false);
160 return;
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.
168 static unsigned long firstcpu_err;
169 static atomic_t watchdog_cpus;
171 int watchdog_nmi_enable(unsigned int cpu)
173 struct perf_event_attr *wd_attr;
174 struct perf_event *event = per_cpu(watchdog_ev, cpu);
175 int firstcpu = 0;
177 /* nothing to do if the hard lockup detector is disabled */
178 if (!(watchdog_enabled & NMI_WATCHDOG_ENABLED))
179 goto out;
181 /* is it already setup and enabled? */
182 if (event && event->state > PERF_EVENT_STATE_OFF)
183 goto out;
185 /* it is setup but not enabled */
186 if (event != NULL)
187 goto out_enable;
189 if (atomic_inc_return(&watchdog_cpus) == 1)
190 firstcpu = 1;
192 wd_attr = &wd_hw_attr;
193 wd_attr->sample_period = hw_nmi_get_sample_period(watchdog_thresh);
195 /* Try to register using hardware perf events */
196 event = perf_event_create_kernel_counter(wd_attr, cpu, NULL, watchdog_overflow_callback, NULL);
198 /* save the first cpu's error for future comparision */
199 if (firstcpu && IS_ERR(event))
200 firstcpu_err = PTR_ERR(event);
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;
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.
214 * The barriers are for syncing up watchdog_enabled across all the
215 * cpus, as clear_bit() does not use barriers.
217 smp_mb__before_atomic();
218 clear_bit(NMI_WATCHDOG_ENABLED_BIT, &watchdog_enabled);
219 smp_mb__after_atomic();
221 /* skip displaying the same error again */
222 if (!firstcpu && (PTR_ERR(event) == firstcpu_err))
223 return PTR_ERR(event);
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));
235 pr_info("Shutting down hard lockup detector on all cpus\n");
237 return PTR_ERR(event);
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;
248 void watchdog_nmi_disable(unsigned int cpu)
250 struct perf_event *event = per_cpu(watchdog_ev, cpu);
252 if (event) {
253 perf_event_disable(event);
254 per_cpu(watchdog_ev, cpu) = NULL;
256 /* should be in cleanup, but blocks oprofile */
257 perf_event_release_kernel(event);
259 /* watchdog_nmi_enable() expects this to be zero initially. */
260 if (atomic_dec_and_test(&watchdog_cpus))
261 firstcpu_err = 0;