Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Detect Hung Task |
| 3 | * |
| 4 | * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/cpu.h> |
| 10 | #include <linux/nmi.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/freezer.h> |
| 14 | #include <linux/kthread.h> |
| 15 | #include <linux/lockdep.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/sysctl.h> |
| 18 | |
| 19 | /* |
| 20 | * Have a reasonable limit on the number of tasks checked: |
| 21 | */ |
| 22 | unsigned long __read_mostly sysctl_hung_task_check_count = 1024; |
| 23 | |
| 24 | /* |
| 25 | * Zero means infinite timeout - no checking done: |
| 26 | */ |
| 27 | unsigned long __read_mostly sysctl_hung_task_timeout_secs = 120; |
| 28 | static unsigned long __read_mostly hung_task_poll_jiffies; |
| 29 | |
| 30 | unsigned long __read_mostly sysctl_hung_task_warnings = 10; |
| 31 | |
| 32 | static int __read_mostly did_panic; |
| 33 | |
| 34 | static struct task_struct *watchdog_task; |
| 35 | |
| 36 | /* |
| 37 | * Should we panic (and reboot, if panic_timeout= is set) when a |
| 38 | * hung task is detected: |
| 39 | */ |
| 40 | unsigned int __read_mostly sysctl_hung_task_panic = |
| 41 | CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE; |
| 42 | |
| 43 | static int __init hung_task_panic_setup(char *str) |
| 44 | { |
| 45 | sysctl_hung_task_panic = simple_strtoul(str, NULL, 0); |
| 46 | |
| 47 | return 1; |
| 48 | } |
| 49 | __setup("hung_task_panic=", hung_task_panic_setup); |
| 50 | |
| 51 | static int |
| 52 | hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr) |
| 53 | { |
| 54 | did_panic = 1; |
| 55 | |
| 56 | return NOTIFY_DONE; |
| 57 | } |
| 58 | |
| 59 | static struct notifier_block panic_block = { |
| 60 | .notifier_call = hung_task_panic, |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Returns seconds, approximately. We don't need nanosecond |
| 65 | * resolution, and we don't need to waste time with a big divide when |
| 66 | * 2^30ns == 1.074s. |
| 67 | */ |
| 68 | static unsigned long get_timestamp(void) |
| 69 | { |
| 70 | int this_cpu = raw_smp_processor_id(); |
| 71 | |
| 72 | return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */ |
| 73 | } |
| 74 | |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 75 | static void check_hung_task(struct task_struct *t, unsigned long now, |
| 76 | unsigned long timeout) |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 77 | { |
| 78 | unsigned long switch_count = t->nvcsw + t->nivcsw; |
| 79 | |
| 80 | if (t->flags & PF_FROZEN) |
| 81 | return; |
| 82 | |
| 83 | if (switch_count != t->last_switch_count || !t->last_switch_timestamp) { |
| 84 | t->last_switch_count = switch_count; |
| 85 | t->last_switch_timestamp = now; |
| 86 | return; |
| 87 | } |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 88 | if ((long)(now - t->last_switch_timestamp) < timeout) |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 89 | return; |
| 90 | if (!sysctl_hung_task_warnings) |
| 91 | return; |
| 92 | sysctl_hung_task_warnings--; |
| 93 | |
| 94 | /* |
| 95 | * Ok, the task did not get scheduled for more than 2 minutes, |
| 96 | * complain: |
| 97 | */ |
| 98 | printk(KERN_ERR "INFO: task %s:%d blocked for more than " |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 99 | "%ld seconds.\n", t->comm, t->pid, timeout); |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 100 | printk(KERN_ERR "\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\"" |
| 101 | " disables this message.\n"); |
| 102 | sched_show_task(t); |
| 103 | __debug_show_held_locks(t); |
| 104 | |
| 105 | t->last_switch_timestamp = now; |
| 106 | touch_nmi_watchdog(); |
| 107 | |
| 108 | if (sysctl_hung_task_panic) |
| 109 | panic("hung_task: blocked tasks"); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for |
| 114 | * a really long time (120 seconds). If that happens, print out |
| 115 | * a warning. |
| 116 | */ |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 117 | static void check_hung_uninterruptible_tasks(unsigned long timeout) |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 118 | { |
| 119 | int max_count = sysctl_hung_task_check_count; |
| 120 | unsigned long now = get_timestamp(); |
| 121 | struct task_struct *g, *t; |
| 122 | |
| 123 | /* |
| 124 | * If the system crashed already then all bets are off, |
| 125 | * do not report extra hung tasks: |
| 126 | */ |
| 127 | if (test_taint(TAINT_DIE) || did_panic) |
| 128 | return; |
| 129 | |
| 130 | read_lock(&tasklist_lock); |
| 131 | do_each_thread(g, t) { |
| 132 | if (!--max_count) |
| 133 | goto unlock; |
| 134 | /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */ |
| 135 | if (t->state == TASK_UNINTERRUPTIBLE) |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 136 | check_hung_task(t, now, timeout); |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 137 | } while_each_thread(g, t); |
| 138 | unlock: |
| 139 | read_unlock(&tasklist_lock); |
| 140 | } |
| 141 | |
| 142 | static void update_poll_jiffies(void) |
| 143 | { |
| 144 | /* timeout of 0 will disable the watchdog */ |
| 145 | if (sysctl_hung_task_timeout_secs == 0) |
| 146 | hung_task_poll_jiffies = MAX_SCHEDULE_TIMEOUT; |
| 147 | else |
| 148 | hung_task_poll_jiffies = sysctl_hung_task_timeout_secs * HZ / 2; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Process updating of timeout sysctl |
| 153 | */ |
| 154 | int proc_dohung_task_timeout_secs(struct ctl_table *table, int write, |
| 155 | struct file *filp, void __user *buffer, |
| 156 | size_t *lenp, loff_t *ppos) |
| 157 | { |
| 158 | int ret; |
| 159 | |
| 160 | ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos); |
| 161 | |
| 162 | if (ret || !write) |
| 163 | goto out; |
| 164 | |
| 165 | update_poll_jiffies(); |
| 166 | |
| 167 | wake_up_process(watchdog_task); |
| 168 | |
| 169 | out: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * kthread which checks for tasks stuck in D state |
| 175 | */ |
| 176 | static int watchdog(void *dummy) |
| 177 | { |
| 178 | set_user_nice(current, 0); |
| 179 | update_poll_jiffies(); |
| 180 | |
| 181 | for ( ; ; ) { |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 182 | unsigned long timeout; |
| 183 | |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 184 | while (schedule_timeout_interruptible(hung_task_poll_jiffies)); |
Mandeep Singh Baines | 603a148 | 2009-01-17 10:31:48 -0800 | [diff] [blame^] | 185 | |
| 186 | /* |
| 187 | * Need to cache timeout here to avoid timeout being set |
| 188 | * to 0 via sysctl while inside check_hung_*_tasks(). |
| 189 | */ |
| 190 | timeout = sysctl_hung_task_timeout_secs; |
| 191 | if (timeout) |
| 192 | check_hung_uninterruptible_tasks(timeout); |
Mandeep Singh Baines | e162b39 | 2009-01-15 11:08:40 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int __init hung_task_init(void) |
| 199 | { |
| 200 | atomic_notifier_chain_register(&panic_notifier_list, &panic_block); |
| 201 | watchdog_task = kthread_run(watchdog, NULL, "khungtaskd"); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | module_init(hung_task_init); |