Blame view
kernel/softlockup.c
7.06 KB
8446f1d39
|
1 2 3 |
/* * Detect Soft Lockups * |
6687a97d4
|
4 |
* started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc. |
8446f1d39
|
5 6 7 8 |
* * this code detects soft lockups: incidents in where on a CPU * the kernel does not reschedule for 10 seconds or more. */ |
8446f1d39
|
9 10 |
#include <linux/mm.h> #include <linux/cpu.h> |
82a1fcb90
|
11 |
#include <linux/nmi.h> |
8446f1d39
|
12 13 |
#include <linux/init.h> #include <linux/delay.h> |
831441862
|
14 |
#include <linux/freezer.h> |
8446f1d39
|
15 |
#include <linux/kthread.h> |
8d5be7f4e
|
16 |
#include <linux/lockdep.h> |
8446f1d39
|
17 18 |
#include <linux/notifier.h> #include <linux/module.h> |
baf48f657
|
19 |
#include <linux/sysctl.h> |
8446f1d39
|
20 |
|
43581a100
|
21 |
#include <asm/irq_regs.h> |
8446f1d39
|
22 |
static DEFINE_SPINLOCK(print_lock); |
1871e52c7
|
23 24 25 |
static DEFINE_PER_CPU(unsigned long, softlockup_touch_ts); /* touch timestamp */ static DEFINE_PER_CPU(unsigned long, softlockup_print_ts); /* print timestamp */ static DEFINE_PER_CPU(struct task_struct *, softlockup_watchdog); |
d6ad3e286
|
26 |
static DEFINE_PER_CPU(bool, softlock_touch_sync); |
8446f1d39
|
27 |
|
90739081e
|
28 |
static int __read_mostly did_panic; |
9383d9679
|
29 |
int __read_mostly softlockup_thresh = 60; |
6687a97d4
|
30 |
|
9c44bc03f
|
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* * Should we panic (and reboot, if panic_timeout= is set) when a * soft-lockup occurs: */ unsigned int __read_mostly softlockup_panic = CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE; static int __init softlockup_panic_setup(char *str) { softlockup_panic = simple_strtoul(str, NULL, 0); return 1; } __setup("softlockup_panic=", softlockup_panic_setup); |
6687a97d4
|
45 46 |
static int softlock_panic(struct notifier_block *this, unsigned long event, void *ptr) |
8446f1d39
|
47 48 49 50 51 52 53 54 55 |
{ did_panic = 1; return NOTIFY_DONE; } static struct notifier_block panic_block = { .notifier_call = softlock_panic, }; |
966812dc9
|
56 57 58 59 60 |
/* * Returns seconds, approximately. We don't need nanosecond * resolution, and we don't need to waste time with a big divide when * 2^30ns == 1.074s. */ |
a3b13c23f
|
61 |
static unsigned long get_timestamp(int this_cpu) |
966812dc9
|
62 |
{ |
82a1fcb90
|
63 |
return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */ |
966812dc9
|
64 |
} |
8c2238eaa
|
65 |
static void __touch_softlockup_watchdog(void) |
8446f1d39
|
66 |
{ |
a3b13c23f
|
67 |
int this_cpu = raw_smp_processor_id(); |
1871e52c7
|
68 |
__raw_get_cpu_var(softlockup_touch_ts) = get_timestamp(this_cpu); |
8446f1d39
|
69 |
} |
8c2238eaa
|
70 71 72 |
void touch_softlockup_watchdog(void) { |
1871e52c7
|
73 |
__raw_get_cpu_var(softlockup_touch_ts) = 0; |
8c2238eaa
|
74 |
} |
8446f1d39
|
75 |
EXPORT_SYMBOL(touch_softlockup_watchdog); |
d6ad3e286
|
76 77 78 79 80 |
void touch_softlockup_watchdog_sync(void) { __raw_get_cpu_var(softlock_touch_sync) = true; __raw_get_cpu_var(softlockup_touch_ts) = 0; } |
04c9167f9
|
81 82 83 84 85 86 |
void touch_all_softlockup_watchdogs(void) { int cpu; /* Cause each CPU to re-update its timestamp rather than complain */ for_each_online_cpu(cpu) |
1871e52c7
|
87 |
per_cpu(softlockup_touch_ts, cpu) = 0; |
04c9167f9
|
88 89 |
} EXPORT_SYMBOL(touch_all_softlockup_watchdogs); |
baf48f657
|
90 |
int proc_dosoftlockup_thresh(struct ctl_table *table, int write, |
8d65af789
|
91 |
void __user *buffer, |
baf48f657
|
92 93 94 |
size_t *lenp, loff_t *ppos) { touch_all_softlockup_watchdogs(); |
8d65af789
|
95 |
return proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
baf48f657
|
96 |
} |
8446f1d39
|
97 98 99 100 |
/* * This callback runs from the timer interrupt, and checks * whether the watchdog thread has hung or not: */ |
6687a97d4
|
101 |
void softlockup_tick(void) |
8446f1d39
|
102 103 |
{ int this_cpu = smp_processor_id(); |
1871e52c7
|
104 105 |
unsigned long touch_ts = per_cpu(softlockup_touch_ts, this_cpu); unsigned long print_ts; |
43581a100
|
106 |
struct pt_regs *regs = get_irq_regs(); |
966812dc9
|
107 |
unsigned long now; |
8446f1d39
|
108 |
|
9383d9679
|
109 |
/* Is detection switched off? */ |
1871e52c7
|
110 |
if (!per_cpu(softlockup_watchdog, this_cpu) || softlockup_thresh <= 0) { |
9383d9679
|
111 |
/* Be sure we don't false trigger if switched back on */ |
1871e52c7
|
112 113 |
if (touch_ts) per_cpu(softlockup_touch_ts, this_cpu) = 0; |
9383d9679
|
114 115 |
return; } |
1871e52c7
|
116 |
if (touch_ts == 0) { |
d6ad3e286
|
117 118 119 120 121 122 123 124 |
if (unlikely(per_cpu(softlock_touch_sync, this_cpu))) { /* * If the time stamp was touched atomically * make sure the scheduler tick is up to date. */ per_cpu(softlock_touch_sync, this_cpu) = false; sched_clock_tick(); } |
8c2238eaa
|
125 |
__touch_softlockup_watchdog(); |
966812dc9
|
126 |
return; |
04c9167f9
|
127 |
} |
966812dc9
|
128 |
|
1871e52c7
|
129 |
print_ts = per_cpu(softlockup_print_ts, this_cpu); |
966812dc9
|
130 131 |
/* report at most once a second */ |
1871e52c7
|
132 |
if (print_ts == touch_ts || did_panic) |
8446f1d39
|
133 |
return; |
6687a97d4
|
134 135 |
/* do not print during early bootup: */ if (unlikely(system_state != SYSTEM_RUNNING)) { |
8c2238eaa
|
136 |
__touch_softlockup_watchdog(); |
8446f1d39
|
137 |
return; |
6687a97d4
|
138 |
} |
8446f1d39
|
139 |
|
a3b13c23f
|
140 |
now = get_timestamp(this_cpu); |
966812dc9
|
141 |
|
dd7a1e561
|
142 143 144 145 |
/* * Wake up the high-prio watchdog task twice per * threshold timespan. */ |
8c2eb4805
|
146 |
if (time_after(now - softlockup_thresh/2, touch_ts)) |
1871e52c7
|
147 |
wake_up_process(per_cpu(softlockup_watchdog, this_cpu)); |
ed50d6cbc
|
148 |
|
82a1fcb90
|
149 |
/* Warn about unreasonable delays: */ |
8c2eb4805
|
150 |
if (time_before_eq(now - softlockup_thresh, touch_ts)) |
43581a100
|
151 |
return; |
8446f1d39
|
152 |
|
1871e52c7
|
153 |
per_cpu(softlockup_print_ts, this_cpu) = touch_ts; |
43581a100
|
154 155 |
spin_lock(&print_lock); |
c4f3b63fe
|
156 157 |
printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d] ", |
1871e52c7
|
158 |
this_cpu, now - touch_ts, |
ba25f9dcc
|
159 |
current->comm, task_pid_nr(current)); |
688c91755
|
160 |
print_modules(); |
8d5be7f4e
|
161 |
print_irqtrace_events(current); |
43581a100
|
162 163 164 |
if (regs) show_regs(regs); else |
6687a97d4
|
165 |
dump_stack(); |
43581a100
|
166 |
spin_unlock(&print_lock); |
9c44bc03f
|
167 168 169 |
if (softlockup_panic) panic("softlockup: hung tasks"); |
8446f1d39
|
170 171 172 173 174 |
} /* * The watchdog thread - runs every second and touches the timestamp. */ |
a5f2ce3c6
|
175 |
static int watchdog(void *__bind_cpu) |
8446f1d39
|
176 |
{ |
02fb6149f
|
177 |
struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
8446f1d39
|
178 179 |
sched_setscheduler(current, SCHED_FIFO, ¶m); |
8446f1d39
|
180 |
|
966812dc9
|
181 |
/* initialize timestamp */ |
8c2238eaa
|
182 |
__touch_softlockup_watchdog(); |
966812dc9
|
183 |
|
7be2a03e3
|
184 |
set_current_state(TASK_INTERRUPTIBLE); |
8446f1d39
|
185 |
/* |
6687a97d4
|
186 |
* Run briefly once per second to reset the softlockup timestamp. |
82a1fcb90
|
187 |
* If this gets delayed for more than 60 seconds then the |
6687a97d4
|
188 |
* debug-printout triggers in softlockup_tick(). |
8446f1d39
|
189 190 |
*/ while (!kthread_should_stop()) { |
8c2238eaa
|
191 |
__touch_softlockup_watchdog(); |
ed50d6cbc
|
192 193 194 195 |
schedule(); if (kthread_should_stop()) break; |
82a1fcb90
|
196 |
|
7be2a03e3
|
197 |
set_current_state(TASK_INTERRUPTIBLE); |
8446f1d39
|
198 |
} |
7be2a03e3
|
199 |
__set_current_state(TASK_RUNNING); |
8446f1d39
|
200 201 202 203 204 205 206 |
return 0; } /* * Create/destroy watchdog threads as CPUs come and go: */ |
8c78f3075
|
207 |
static int __cpuinit |
8446f1d39
|
208 209 210 211 212 213 214 |
cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) { int hotcpu = (unsigned long)hcpu; struct task_struct *p; switch (action) { case CPU_UP_PREPARE: |
8bb784428
|
215 |
case CPU_UP_PREPARE_FROZEN: |
1871e52c7
|
216 |
BUG_ON(per_cpu(softlockup_watchdog, hotcpu)); |
8446f1d39
|
217 218 |
p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu); if (IS_ERR(p)) { |
a5f2ce3c6
|
219 220 |
printk(KERN_ERR "watchdog for %i failed ", hotcpu); |
8446f1d39
|
221 222 |
return NOTIFY_BAD; } |
1871e52c7
|
223 224 |
per_cpu(softlockup_touch_ts, hotcpu) = 0; per_cpu(softlockup_watchdog, hotcpu) = p; |
8446f1d39
|
225 |
kthread_bind(p, hotcpu); |
a5f2ce3c6
|
226 |
break; |
8446f1d39
|
227 |
case CPU_ONLINE: |
8bb784428
|
228 |
case CPU_ONLINE_FROZEN: |
1871e52c7
|
229 |
wake_up_process(per_cpu(softlockup_watchdog, hotcpu)); |
8446f1d39
|
230 231 |
break; #ifdef CONFIG_HOTPLUG_CPU |
ed50d6cbc
|
232 233 |
case CPU_UP_CANCELED: case CPU_UP_CANCELED_FROZEN: |
1871e52c7
|
234 |
if (!per_cpu(softlockup_watchdog, hotcpu)) |
ed50d6cbc
|
235 236 |
break; /* Unbind so it can run. Fall thru. */ |
1871e52c7
|
237 |
kthread_bind(per_cpu(softlockup_watchdog, hotcpu), |
f1fc057c7
|
238 |
cpumask_any(cpu_online_mask)); |
8446f1d39
|
239 |
case CPU_DEAD: |
8bb784428
|
240 |
case CPU_DEAD_FROZEN: |
1871e52c7
|
241 242 |
p = per_cpu(softlockup_watchdog, hotcpu); per_cpu(softlockup_watchdog, hotcpu) = NULL; |
8446f1d39
|
243 244 245 |
kthread_stop(p); break; #endif /* CONFIG_HOTPLUG_CPU */ |
a5f2ce3c6
|
246 |
} |
8446f1d39
|
247 248 |
return NOTIFY_OK; } |
8c78f3075
|
249 |
static struct notifier_block __cpuinitdata cpu_nfb = { |
8446f1d39
|
250 251 |
.notifier_call = cpu_callback }; |
7babe8db9
|
252 253 254 255 256 257 258 259 260 261 |
static int __initdata nosoftlockup; static int __init nosoftlockup_setup(char *str) { nosoftlockup = 1; return 1; } __setup("nosoftlockup", nosoftlockup_setup); static int __init spawn_softlockup_task(void) |
8446f1d39
|
262 263 |
{ void *cpu = (void *)(long)smp_processor_id(); |
7babe8db9
|
264 |
int err; |
8446f1d39
|
265 |
|
7babe8db9
|
266 267 268 269 270 271 272 273 |
if (nosoftlockup) return 0; err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu); if (err == NOTIFY_BAD) { BUG(); return 1; } |
8446f1d39
|
274 275 |
cpu_callback(&cpu_nfb, CPU_ONLINE, cpu); register_cpu_notifier(&cpu_nfb); |
e041c6834
|
276 |
atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
7babe8db9
|
277 278 |
return 0; |
8446f1d39
|
279 |
} |
7babe8db9
|
280 |
early_initcall(spawn_softlockup_task); |