blob: 708d4882c0c391bffa47a24ae0153bccabd0a1c7 [file] [log] [blame]
Ingo Molnar8446f1d2005-09-06 15:16:27 -07001/*
2 * Detect Soft Lockups
3 *
Ingo Molnar6687a972006-03-24 03:18:41 -08004 * started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc.
Ingo Molnar8446f1d2005-09-06 15:16:27 -07005 *
6 * this code detects soft lockups: incidents in where on a CPU
7 * the kernel does not reschedule for 10 seconds or more.
8 */
Ingo Molnar8446f1d2005-09-06 15:16:27 -07009#include <linux/mm.h>
10#include <linux/cpu.h>
11#include <linux/init.h>
12#include <linux/delay.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070013#include <linux/freezer.h>
Ingo Molnar8446f1d2005-09-06 15:16:27 -070014#include <linux/kthread.h>
15#include <linux/notifier.h>
16#include <linux/module.h>
17
18static DEFINE_SPINLOCK(print_lock);
19
Ingo Molnar6687a972006-03-24 03:18:41 -080020static DEFINE_PER_CPU(unsigned long, touch_timestamp);
21static DEFINE_PER_CPU(unsigned long, print_timestamp);
Ingo Molnar8446f1d2005-09-06 15:16:27 -070022static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
23
24static int did_panic = 0;
Ingo Molnar6687a972006-03-24 03:18:41 -080025
26static int
27softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
Ingo Molnar8446f1d2005-09-06 15:16:27 -070028{
29 did_panic = 1;
30
31 return NOTIFY_DONE;
32}
33
34static struct notifier_block panic_block = {
35 .notifier_call = softlock_panic,
36};
37
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070038/*
39 * Returns seconds, approximately. We don't need nanosecond
40 * resolution, and we don't need to waste time with a big divide when
41 * 2^30ns == 1.074s.
42 */
43static unsigned long get_timestamp(void)
44{
45 return sched_clock() >> 30; /* 2^30 ~= 10^9 */
46}
47
Ingo Molnar8446f1d2005-09-06 15:16:27 -070048void touch_softlockup_watchdog(void)
49{
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070050 __raw_get_cpu_var(touch_timestamp) = get_timestamp();
Ingo Molnar8446f1d2005-09-06 15:16:27 -070051}
52EXPORT_SYMBOL(touch_softlockup_watchdog);
53
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -070054void touch_all_softlockup_watchdogs(void)
55{
56 int cpu;
57
58 /* Cause each CPU to re-update its timestamp rather than complain */
59 for_each_online_cpu(cpu)
60 per_cpu(touch_timestamp, cpu) = 0;
61}
62EXPORT_SYMBOL(touch_all_softlockup_watchdogs);
63
Ingo Molnar8446f1d2005-09-06 15:16:27 -070064/*
65 * This callback runs from the timer interrupt, and checks
66 * whether the watchdog thread has hung or not:
67 */
Ingo Molnar6687a972006-03-24 03:18:41 -080068void softlockup_tick(void)
Ingo Molnar8446f1d2005-09-06 15:16:27 -070069{
70 int this_cpu = smp_processor_id();
Ingo Molnar6687a972006-03-24 03:18:41 -080071 unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070072 unsigned long print_timestamp;
73 unsigned long now;
Ingo Molnar8446f1d2005-09-06 15:16:27 -070074
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -070075 if (touch_timestamp == 0) {
76 touch_softlockup_watchdog();
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070077 return;
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -070078 }
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070079
80 print_timestamp = per_cpu(print_timestamp, this_cpu);
81
82 /* report at most once a second */
83 if (print_timestamp < (touch_timestamp + 1) ||
Ingo Molnar6687a972006-03-24 03:18:41 -080084 did_panic ||
85 !per_cpu(watchdog_task, this_cpu))
Ingo Molnar8446f1d2005-09-06 15:16:27 -070086 return;
87
Ingo Molnar6687a972006-03-24 03:18:41 -080088 /* do not print during early bootup: */
89 if (unlikely(system_state != SYSTEM_RUNNING)) {
90 touch_softlockup_watchdog();
Ingo Molnar8446f1d2005-09-06 15:16:27 -070091 return;
Ingo Molnar6687a972006-03-24 03:18:41 -080092 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -070093
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070094 now = get_timestamp();
95
Ingo Molnar6687a972006-03-24 03:18:41 -080096 /* Wake up the high-prio watchdog task every second: */
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070097 if (now > (touch_timestamp + 1))
Ingo Molnar6687a972006-03-24 03:18:41 -080098 wake_up_process(per_cpu(watchdog_task, this_cpu));
99
100 /* Warn about unreasonable 10+ seconds delays: */
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -0700101 if (now > (touch_timestamp + 10)) {
Ingo Molnar6687a972006-03-24 03:18:41 -0800102 per_cpu(print_timestamp, this_cpu) = touch_timestamp;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700103
104 spin_lock(&print_lock);
105 printk(KERN_ERR "BUG: soft lockup detected on CPU#%d!\n",
106 this_cpu);
Ingo Molnar6687a972006-03-24 03:18:41 -0800107 dump_stack();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700108 spin_unlock(&print_lock);
109 }
110}
111
112/*
113 * The watchdog thread - runs every second and touches the timestamp.
114 */
115static int watchdog(void * __bind_cpu)
116{
Oleg Nesterov02fb6142007-05-08 00:24:03 -0700117 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700118
119 sched_setscheduler(current, SCHED_FIFO, &param);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700120
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -0700121 /* initialize timestamp */
122 touch_softlockup_watchdog();
123
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700124 /*
Ingo Molnar6687a972006-03-24 03:18:41 -0800125 * Run briefly once per second to reset the softlockup timestamp.
126 * If this gets delayed for more than 10 seconds then the
127 * debug-printout triggers in softlockup_tick().
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700128 */
129 while (!kthread_should_stop()) {
Ingo Molnar6687a972006-03-24 03:18:41 -0800130 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700131 touch_softlockup_watchdog();
Ingo Molnar6687a972006-03-24 03:18:41 -0800132 schedule();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700133 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700134
135 return 0;
136}
137
138/*
139 * Create/destroy watchdog threads as CPUs come and go:
140 */
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700141static int __cpuinit
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700142cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
143{
144 int hotcpu = (unsigned long)hcpu;
145 struct task_struct *p;
146
147 switch (action) {
148 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700149 case CPU_UP_PREPARE_FROZEN:
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700150 BUG_ON(per_cpu(watchdog_task, hotcpu));
151 p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
152 if (IS_ERR(p)) {
153 printk("watchdog for %i failed\n", hotcpu);
154 return NOTIFY_BAD;
155 }
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -0700156 per_cpu(touch_timestamp, hotcpu) = 0;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700157 per_cpu(watchdog_task, hotcpu) = p;
158 kthread_bind(p, hotcpu);
159 break;
160 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700161 case CPU_ONLINE_FROZEN:
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700162 wake_up_process(per_cpu(watchdog_task, hotcpu));
163 break;
164#ifdef CONFIG_HOTPLUG_CPU
165 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700166 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -0700167 if (!per_cpu(watchdog_task, hotcpu))
168 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700169 /* Unbind so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -0800170 kthread_bind(per_cpu(watchdog_task, hotcpu),
171 any_online_cpu(cpu_online_map));
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700172 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700173 case CPU_DEAD_FROZEN:
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700174 p = per_cpu(watchdog_task, hotcpu);
175 per_cpu(watchdog_task, hotcpu) = NULL;
176 kthread_stop(p);
177 break;
178#endif /* CONFIG_HOTPLUG_CPU */
179 }
180 return NOTIFY_OK;
181}
182
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700183static struct notifier_block __cpuinitdata cpu_nfb = {
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700184 .notifier_call = cpu_callback
185};
186
187__init void spawn_softlockup_task(void)
188{
189 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -0700190 int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700191
Akinobu Mita07dccf32006-09-29 02:00:22 -0700192 BUG_ON(err == NOTIFY_BAD);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700193 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
194 register_cpu_notifier(&cpu_nfb);
195
Alan Sterne041c682006-03-27 01:16:30 -0800196 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700197}