On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / kernel / softlockup.c
bloba85ace2bc018c1c8b5e764c6c26d2ff067661276
1 /*
2 * Detect Soft Lockups
4 * started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc.
6 * this code detects soft lockups: incidents in where on a CPU
7 * the kernel does not reschedule for 10 seconds or more.
8 */
9 #include <linux/mm.h>
10 #include <linux/cpu.h>
11 #include <linux/nmi.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/freezer.h>
15 #include <linux/kthread.h>
16 #include <linux/lockdep.h>
17 #include <linux/notifier.h>
18 #include <linux/module.h>
19 #include <linux/sysctl.h>
21 #include <asm/irq_regs.h>
23 static DEFINE_SPINLOCK(print_lock);
25 static DEFINE_PER_CPU(unsigned long, touch_timestamp);
26 static DEFINE_PER_CPU(unsigned long, print_timestamp);
27 static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
29 static int __read_mostly did_panic;
30 int __read_mostly softlockup_thresh = 60;
33 * Should we panic (and reboot, if panic_timeout= is set) when a
34 * soft-lockup occurs:
36 unsigned int __read_mostly softlockup_panic =
37 CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
39 static int __init softlockup_panic_setup(char *str)
41 softlockup_panic = simple_strtoul(str, NULL, 0);
43 return 1;
45 __setup("softlockup_panic=", softlockup_panic_setup);
47 static int
48 softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
50 did_panic = 1;
52 return NOTIFY_DONE;
55 static struct notifier_block panic_block = {
56 .notifier_call = softlock_panic,
60 * Returns seconds, approximately. We don't need nanosecond
61 * resolution, and we don't need to waste time with a big divide when
62 * 2^30ns == 1.074s.
64 static unsigned long get_timestamp(int this_cpu)
66 return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
69 static void __touch_softlockup_watchdog(void)
71 int this_cpu = raw_smp_processor_id();
73 __raw_get_cpu_var(touch_timestamp) = get_timestamp(this_cpu);
76 void touch_softlockup_watchdog(void)
78 __raw_get_cpu_var(touch_timestamp) = 0;
80 EXPORT_SYMBOL(touch_softlockup_watchdog);
82 static int softlock_touch_sync[NR_CPUS];
84 void touch_softlockup_watchdog_sync(void)
86 softlock_touch_sync[raw_smp_processor_id()] = 1;
87 __raw_get_cpu_var(touch_timestamp) = 0;
90 void touch_all_softlockup_watchdogs(void)
92 int cpu;
94 /* Cause each CPU to re-update its timestamp rather than complain */
95 for_each_online_cpu(cpu)
96 per_cpu(touch_timestamp, cpu) = 0;
98 EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
100 int proc_dosoftlockup_thresh(struct ctl_table *table, int write,
101 void __user *buffer,
102 size_t *lenp, loff_t *ppos)
104 touch_all_softlockup_watchdogs();
105 return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
109 * This callback runs from the timer interrupt, and checks
110 * whether the watchdog thread has hung or not:
112 void softlockup_tick(void)
114 int this_cpu = smp_processor_id();
115 unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
116 unsigned long print_timestamp;
117 struct pt_regs *regs = get_irq_regs();
118 unsigned long now;
120 /* Is detection switched off? */
121 if (!per_cpu(watchdog_task, this_cpu) || softlockup_thresh <= 0) {
122 /* Be sure we don't false trigger if switched back on */
123 if (touch_timestamp)
124 per_cpu(touch_timestamp, this_cpu) = 0;
125 return;
128 if (touch_timestamp == 0) {
129 if (unlikely(softlock_touch_sync[this_cpu])) {
131 * If the time stamp was touched atomically
132 * make sure the scheduler tick is up to date.
134 softlock_touch_sync[this_cpu] = 0;
135 sched_clock_tick();
137 __touch_softlockup_watchdog();
138 return;
141 print_timestamp = per_cpu(print_timestamp, this_cpu);
143 /* report at most once a second */
144 if (print_timestamp == touch_timestamp || did_panic)
145 return;
147 /* do not print during early bootup: */
148 if (unlikely(system_state != SYSTEM_RUNNING)) {
149 __touch_softlockup_watchdog();
150 return;
153 now = get_timestamp(this_cpu);
156 * Wake up the high-prio watchdog task twice per
157 * threshold timespan.
159 if (now > touch_timestamp + softlockup_thresh/2)
160 wake_up_process(per_cpu(watchdog_task, this_cpu));
162 /* Warn about unreasonable delays: */
163 if (now <= (touch_timestamp + softlockup_thresh))
164 return;
166 per_cpu(print_timestamp, this_cpu) = touch_timestamp;
168 spin_lock(&print_lock);
169 printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n",
170 this_cpu, now - touch_timestamp,
171 current->comm, task_pid_nr(current));
172 print_modules();
173 print_irqtrace_events(current);
174 if (regs)
175 show_regs(regs);
176 else
177 dump_stack();
178 spin_unlock(&print_lock);
180 if (softlockup_panic)
181 panic("softlockup: hung tasks");
185 * The watchdog thread - runs every second and touches the timestamp.
187 static int watchdog(void *__bind_cpu)
189 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
191 sched_setscheduler(current, SCHED_FIFO, &param);
193 /* initialize timestamp */
194 __touch_softlockup_watchdog();
196 set_current_state(TASK_INTERRUPTIBLE);
198 * Run briefly once per second to reset the softlockup timestamp.
199 * If this gets delayed for more than 60 seconds then the
200 * debug-printout triggers in softlockup_tick().
202 while (!kthread_should_stop()) {
203 __touch_softlockup_watchdog();
204 schedule();
206 if (kthread_should_stop())
207 break;
209 set_current_state(TASK_INTERRUPTIBLE);
211 __set_current_state(TASK_RUNNING);
213 return 0;
217 * Create/destroy watchdog threads as CPUs come and go:
219 static int __cpuinit
220 cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
222 int hotcpu = (unsigned long)hcpu;
223 struct task_struct *p;
225 switch (action) {
226 case CPU_UP_PREPARE:
227 case CPU_UP_PREPARE_FROZEN:
228 BUG_ON(per_cpu(watchdog_task, hotcpu));
229 p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
230 if (IS_ERR(p)) {
231 printk(KERN_ERR "watchdog for %i failed\n", hotcpu);
232 return NOTIFY_BAD;
234 per_cpu(touch_timestamp, hotcpu) = 0;
235 per_cpu(watchdog_task, hotcpu) = p;
236 kthread_bind(p, hotcpu);
237 break;
238 case CPU_ONLINE:
239 case CPU_ONLINE_FROZEN:
240 wake_up_process(per_cpu(watchdog_task, hotcpu));
241 break;
242 #ifdef CONFIG_HOTPLUG_CPU
243 case CPU_UP_CANCELED:
244 case CPU_UP_CANCELED_FROZEN:
245 if (!per_cpu(watchdog_task, hotcpu))
246 break;
247 /* Unbind so it can run. Fall thru. */
248 kthread_bind(per_cpu(watchdog_task, hotcpu),
249 cpumask_any(cpu_online_mask));
250 case CPU_DEAD:
251 case CPU_DEAD_FROZEN:
252 p = per_cpu(watchdog_task, hotcpu);
253 per_cpu(watchdog_task, hotcpu) = NULL;
254 kthread_stop(p);
255 break;
256 #endif /* CONFIG_HOTPLUG_CPU */
258 return NOTIFY_OK;
261 static struct notifier_block __cpuinitdata cpu_nfb = {
262 .notifier_call = cpu_callback
265 static int __initdata nosoftlockup;
267 static int __init nosoftlockup_setup(char *str)
269 nosoftlockup = 1;
270 return 1;
272 __setup("nosoftlockup", nosoftlockup_setup);
274 static int __init spawn_softlockup_task(void)
276 void *cpu = (void *)(long)smp_processor_id();
277 int err;
279 if (nosoftlockup)
280 return 0;
282 err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
283 if (err == NOTIFY_BAD) {
284 BUG();
285 return 1;
287 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
288 register_cpu_notifier(&cpu_nfb);
290 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
292 return 0;
294 early_initcall(spawn_softlockup_task);