blob: 5ea631742dbcf246e7dbd772e33c798c74ee6625 [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>
13#include <linux/kthread.h>
14#include <linux/notifier.h>
15#include <linux/module.h>
16
17static DEFINE_SPINLOCK(print_lock);
18
Ingo Molnar6687a972006-03-24 03:18:41 -080019static DEFINE_PER_CPU(unsigned long, touch_timestamp);
20static DEFINE_PER_CPU(unsigned long, print_timestamp);
Ingo Molnar8446f1d2005-09-06 15:16:27 -070021static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
22
23static int did_panic = 0;
Ingo Molnar6687a972006-03-24 03:18:41 -080024
25static int
26softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
Ingo Molnar8446f1d2005-09-06 15:16:27 -070027{
28 did_panic = 1;
29
30 return NOTIFY_DONE;
31}
32
33static struct notifier_block panic_block = {
34 .notifier_call = softlock_panic,
35};
36
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070037/*
38 * Returns seconds, approximately. We don't need nanosecond
39 * resolution, and we don't need to waste time with a big divide when
40 * 2^30ns == 1.074s.
41 */
42static unsigned long get_timestamp(void)
43{
44 return sched_clock() >> 30; /* 2^30 ~= 10^9 */
45}
46
Ingo Molnar8446f1d2005-09-06 15:16:27 -070047void touch_softlockup_watchdog(void)
48{
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070049 __raw_get_cpu_var(touch_timestamp) = get_timestamp();
Ingo Molnar8446f1d2005-09-06 15:16:27 -070050}
51EXPORT_SYMBOL(touch_softlockup_watchdog);
52
53/*
54 * This callback runs from the timer interrupt, and checks
55 * whether the watchdog thread has hung or not:
56 */
Ingo Molnar6687a972006-03-24 03:18:41 -080057void softlockup_tick(void)
Ingo Molnar8446f1d2005-09-06 15:16:27 -070058{
59 int this_cpu = smp_processor_id();
Ingo Molnar6687a972006-03-24 03:18:41 -080060 unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070061 unsigned long print_timestamp;
62 unsigned long now;
Ingo Molnar8446f1d2005-09-06 15:16:27 -070063
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070064 /* watchdog task hasn't updated timestamp yet */
65 if (touch_timestamp == 0)
66 return;
67
68 print_timestamp = per_cpu(print_timestamp, this_cpu);
69
70 /* report at most once a second */
71 if (print_timestamp < (touch_timestamp + 1) ||
Ingo Molnar6687a972006-03-24 03:18:41 -080072 did_panic ||
73 !per_cpu(watchdog_task, this_cpu))
Ingo Molnar8446f1d2005-09-06 15:16:27 -070074 return;
75
Ingo Molnar6687a972006-03-24 03:18:41 -080076 /* do not print during early bootup: */
77 if (unlikely(system_state != SYSTEM_RUNNING)) {
78 touch_softlockup_watchdog();
Ingo Molnar8446f1d2005-09-06 15:16:27 -070079 return;
Ingo Molnar6687a972006-03-24 03:18:41 -080080 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -070081
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070082 now = get_timestamp();
83
Ingo Molnar6687a972006-03-24 03:18:41 -080084 /* Wake up the high-prio watchdog task every second: */
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070085 if (now > (touch_timestamp + 1))
Ingo Molnar6687a972006-03-24 03:18:41 -080086 wake_up_process(per_cpu(watchdog_task, this_cpu));
87
88 /* Warn about unreasonable 10+ seconds delays: */
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -070089 if (now > (touch_timestamp + 10)) {
Ingo Molnar6687a972006-03-24 03:18:41 -080090 per_cpu(print_timestamp, this_cpu) = touch_timestamp;
Ingo Molnar8446f1d2005-09-06 15:16:27 -070091
92 spin_lock(&print_lock);
93 printk(KERN_ERR "BUG: soft lockup detected on CPU#%d!\n",
94 this_cpu);
Ingo Molnar6687a972006-03-24 03:18:41 -080095 dump_stack();
Ingo Molnar8446f1d2005-09-06 15:16:27 -070096 spin_unlock(&print_lock);
97 }
98}
99
100/*
101 * The watchdog thread - runs every second and touches the timestamp.
102 */
103static int watchdog(void * __bind_cpu)
104{
Oleg Nesterov02fb6142007-05-08 00:24:03 -0700105 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700106
107 sched_setscheduler(current, SCHED_FIFO, &param);
108 current->flags |= PF_NOFREEZE;
109
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -0700110 /* initialize timestamp */
111 touch_softlockup_watchdog();
112
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700113 /*
Ingo Molnar6687a972006-03-24 03:18:41 -0800114 * Run briefly once per second to reset the softlockup timestamp.
115 * If this gets delayed for more than 10 seconds then the
116 * debug-printout triggers in softlockup_tick().
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700117 */
118 while (!kthread_should_stop()) {
Ingo Molnar6687a972006-03-24 03:18:41 -0800119 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700120 touch_softlockup_watchdog();
Ingo Molnar6687a972006-03-24 03:18:41 -0800121 schedule();
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700122 }
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700123
124 return 0;
125}
126
127/*
128 * Create/destroy watchdog threads as CPUs come and go:
129 */
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700130static int __cpuinit
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700131cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
132{
133 int hotcpu = (unsigned long)hcpu;
134 struct task_struct *p;
135
136 switch (action) {
137 case CPU_UP_PREPARE:
138 BUG_ON(per_cpu(watchdog_task, hotcpu));
139 p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
140 if (IS_ERR(p)) {
141 printk("watchdog for %i failed\n", hotcpu);
142 return NOTIFY_BAD;
143 }
Jeremy Fitzhardinge966812d2007-05-08 00:28:02 -0700144 per_cpu(touch_timestamp, hotcpu) = 0;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700145 per_cpu(watchdog_task, hotcpu) = p;
146 kthread_bind(p, hotcpu);
147 break;
148 case CPU_ONLINE:
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700149 wake_up_process(per_cpu(watchdog_task, hotcpu));
150 break;
151#ifdef CONFIG_HOTPLUG_CPU
152 case CPU_UP_CANCELED:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -0700153 if (!per_cpu(watchdog_task, hotcpu))
154 break;
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700155 /* Unbind so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -0800156 kthread_bind(per_cpu(watchdog_task, hotcpu),
157 any_online_cpu(cpu_online_map));
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700158 case CPU_DEAD:
159 p = per_cpu(watchdog_task, hotcpu);
160 per_cpu(watchdog_task, hotcpu) = NULL;
161 kthread_stop(p);
162 break;
163#endif /* CONFIG_HOTPLUG_CPU */
164 }
165 return NOTIFY_OK;
166}
167
Chandra Seetharaman8c78f302006-07-30 03:03:35 -0700168static struct notifier_block __cpuinitdata cpu_nfb = {
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700169 .notifier_call = cpu_callback
170};
171
172__init void spawn_softlockup_task(void)
173{
174 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -0700175 int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700176
Akinobu Mita07dccf32006-09-29 02:00:22 -0700177 BUG_ON(err == NOTIFY_BAD);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700178 cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
179 register_cpu_notifier(&cpu_nfb);
180
Alan Sterne041c682006-03-27 01:16:30 -0800181 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
Ingo Molnar8446f1d2005-09-06 15:16:27 -0700182}