| 1 | /* |
| 2 | * Detect Soft Lockups |
| 3 | * |
| 4 | * started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc. |
| 5 | * |
| 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/init.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/freezer.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/module.h> |
| 17 | |
| 18 | #include <asm/irq_regs.h> |
| 19 | |
| 20 | static DEFINE_SPINLOCK(print_lock); |
| 21 | |
| 22 | static DEFINE_PER_CPU(unsigned long, touch_timestamp); |
| 23 | static DEFINE_PER_CPU(unsigned long, print_timestamp); |
| 24 | static DEFINE_PER_CPU(struct task_struct *, watchdog_task); |
| 25 | |
| 26 | static int did_panic; |
| 27 | int softlockup_thresh = 10; |
| 28 | |
| 29 | static int |
| 30 | softlock_panic(struct notifier_block *this, unsigned long event, void *ptr) |
| 31 | { |
| 32 | did_panic = 1; |
| 33 | |
| 34 | return NOTIFY_DONE; |
| 35 | } |
| 36 | |
| 37 | static struct notifier_block panic_block = { |
| 38 | .notifier_call = softlock_panic, |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Returns seconds, approximately. We don't need nanosecond |
| 43 | * resolution, and we don't need to waste time with a big divide when |
| 44 | * 2^30ns == 1.074s. |
| 45 | */ |
| 46 | static unsigned long get_timestamp(int this_cpu) |
| 47 | { |
| 48 | return cpu_clock(this_cpu) >> 30; /* 2^30 ~= 10^9 */ |
| 49 | } |
| 50 | |
| 51 | void touch_softlockup_watchdog(void) |
| 52 | { |
| 53 | int this_cpu = raw_smp_processor_id(); |
| 54 | |
| 55 | __raw_get_cpu_var(touch_timestamp) = get_timestamp(this_cpu); |
| 56 | } |
| 57 | EXPORT_SYMBOL(touch_softlockup_watchdog); |
| 58 | |
| 59 | void touch_all_softlockup_watchdogs(void) |
| 60 | { |
| 61 | int cpu; |
| 62 | |
| 63 | /* Cause each CPU to re-update its timestamp rather than complain */ |
| 64 | for_each_online_cpu(cpu) |
| 65 | per_cpu(touch_timestamp, cpu) = 0; |
| 66 | } |
| 67 | EXPORT_SYMBOL(touch_all_softlockup_watchdogs); |
| 68 | |
| 69 | /* |
| 70 | * This callback runs from the timer interrupt, and checks |
| 71 | * whether the watchdog thread has hung or not: |
| 72 | */ |
| 73 | void softlockup_tick(void) |
| 74 | { |
| 75 | int this_cpu = smp_processor_id(); |
| 76 | unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu); |
| 77 | unsigned long print_timestamp; |
| 78 | struct pt_regs *regs = get_irq_regs(); |
| 79 | unsigned long now; |
| 80 | |
| 81 | if (touch_timestamp == 0) { |
| 82 | touch_softlockup_watchdog(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | print_timestamp = per_cpu(print_timestamp, this_cpu); |
| 87 | |
| 88 | /* report at most once a second */ |
| 89 | if ((print_timestamp >= touch_timestamp && |
| 90 | print_timestamp < (touch_timestamp + 1)) || |
| 91 | did_panic || !per_cpu(watchdog_task, this_cpu)) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | /* do not print during early bootup: */ |
| 96 | if (unlikely(system_state != SYSTEM_RUNNING)) { |
| 97 | touch_softlockup_watchdog(); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | now = get_timestamp(this_cpu); |
| 102 | |
| 103 | /* Wake up the high-prio watchdog task every second: */ |
| 104 | if (now > (touch_timestamp + 1)) |
| 105 | wake_up_process(per_cpu(watchdog_task, this_cpu)); |
| 106 | |
| 107 | /* Warn about unreasonable 10+ seconds delays: */ |
| 108 | if (now <= (touch_timestamp + softlockup_thresh)) |
| 109 | return; |
| 110 | |
| 111 | per_cpu(print_timestamp, this_cpu) = touch_timestamp; |
| 112 | |
| 113 | spin_lock(&print_lock); |
| 114 | printk(KERN_ERR "BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n", |
| 115 | this_cpu, now - touch_timestamp, |
| 116 | current->comm, current->pid); |
| 117 | if (regs) |
| 118 | show_regs(regs); |
| 119 | else |
| 120 | dump_stack(); |
| 121 | spin_unlock(&print_lock); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * The watchdog thread - runs every second and touches the timestamp. |
| 126 | */ |
| 127 | static int watchdog(void *__bind_cpu) |
| 128 | { |
| 129 | struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; |
| 130 | |
| 131 | sched_setscheduler(current, SCHED_FIFO, ¶m); |
| 132 | |
| 133 | /* initialize timestamp */ |
| 134 | touch_softlockup_watchdog(); |
| 135 | |
| 136 | /* |
| 137 | * Run briefly once per second to reset the softlockup timestamp. |
| 138 | * If this gets delayed for more than 10 seconds then the |
| 139 | * debug-printout triggers in softlockup_tick(). |
| 140 | */ |
| 141 | while (!kthread_should_stop()) { |
| 142 | set_current_state(TASK_INTERRUPTIBLE); |
| 143 | touch_softlockup_watchdog(); |
| 144 | schedule(); |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Create/destroy watchdog threads as CPUs come and go: |
| 152 | */ |
| 153 | static int __cpuinit |
| 154 | cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| 155 | { |
| 156 | int hotcpu = (unsigned long)hcpu; |
| 157 | struct task_struct *p; |
| 158 | |
| 159 | switch (action) { |
| 160 | case CPU_UP_PREPARE: |
| 161 | case CPU_UP_PREPARE_FROZEN: |
| 162 | BUG_ON(per_cpu(watchdog_task, hotcpu)); |
| 163 | p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu); |
| 164 | if (IS_ERR(p)) { |
| 165 | printk(KERN_ERR "watchdog for %i failed\n", hotcpu); |
| 166 | return NOTIFY_BAD; |
| 167 | } |
| 168 | per_cpu(touch_timestamp, hotcpu) = 0; |
| 169 | per_cpu(watchdog_task, hotcpu) = p; |
| 170 | kthread_bind(p, hotcpu); |
| 171 | break; |
| 172 | case CPU_ONLINE: |
| 173 | case CPU_ONLINE_FROZEN: |
| 174 | wake_up_process(per_cpu(watchdog_task, hotcpu)); |
| 175 | break; |
| 176 | #ifdef CONFIG_HOTPLUG_CPU |
| 177 | case CPU_UP_CANCELED: |
| 178 | case CPU_UP_CANCELED_FROZEN: |
| 179 | if (!per_cpu(watchdog_task, hotcpu)) |
| 180 | break; |
| 181 | /* Unbind so it can run. Fall thru. */ |
| 182 | kthread_bind(per_cpu(watchdog_task, hotcpu), |
| 183 | any_online_cpu(cpu_online_map)); |
| 184 | case CPU_DEAD: |
| 185 | case CPU_DEAD_FROZEN: |
| 186 | p = per_cpu(watchdog_task, hotcpu); |
| 187 | per_cpu(watchdog_task, hotcpu) = NULL; |
| 188 | kthread_stop(p); |
| 189 | break; |
| 190 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 191 | } |
| 192 | return NOTIFY_OK; |
| 193 | } |
| 194 | |
| 195 | static struct notifier_block __cpuinitdata cpu_nfb = { |
| 196 | .notifier_call = cpu_callback |
| 197 | }; |
| 198 | |
| 199 | __init void spawn_softlockup_task(void) |
| 200 | { |
| 201 | void *cpu = (void *)(long)smp_processor_id(); |
| 202 | int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu); |
| 203 | |
| 204 | BUG_ON(err == NOTIFY_BAD); |
| 205 | cpu_callback(&cpu_nfb, CPU_ONLINE, cpu); |
| 206 | register_cpu_notifier(&cpu_nfb); |
| 207 | |
| 208 | atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
| 209 | } |