]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/hung_task.c
softlockup: convert read_lock in hung_task to rcu_read_lock
[mirror_ubuntu-bionic-kernel.git] / kernel / hung_task.c
CommitLineData
e162b39a
MSB
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/*
ce9dbe24 20 * The number of tasks checked:
e162b39a 21 */
ce9dbe24
MSB
22unsigned long __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
23
24/*
25 * Limit number of tasks checked in a batch.
26 *
27 * This value controls the preemptibility of khungtaskd since preemption
28 * is disabled during the critical section. It also controls the size of
29 * the RCU grace period. So it needs to be upper-bound.
30 */
31#define HUNG_TASK_BATCHING 1024
e162b39a
MSB
32
33/*
34 * Zero means infinite timeout - no checking done:
35 */
36unsigned long __read_mostly sysctl_hung_task_timeout_secs = 120;
37static unsigned long __read_mostly hung_task_poll_jiffies;
38
39unsigned long __read_mostly sysctl_hung_task_warnings = 10;
40
41static int __read_mostly did_panic;
42
43static struct task_struct *watchdog_task;
44
45/*
46 * Should we panic (and reboot, if panic_timeout= is set) when a
47 * hung task is detected:
48 */
49unsigned int __read_mostly sysctl_hung_task_panic =
50 CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
51
52static int __init hung_task_panic_setup(char *str)
53{
54 sysctl_hung_task_panic = simple_strtoul(str, NULL, 0);
55
56 return 1;
57}
58__setup("hung_task_panic=", hung_task_panic_setup);
59
60static int
61hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
62{
63 did_panic = 1;
64
65 return NOTIFY_DONE;
66}
67
68static struct notifier_block panic_block = {
69 .notifier_call = hung_task_panic,
70};
71
72/*
73 * Returns seconds, approximately. We don't need nanosecond
74 * resolution, and we don't need to waste time with a big divide when
75 * 2^30ns == 1.074s.
76 */
77static unsigned long get_timestamp(void)
78{
79 int this_cpu = raw_smp_processor_id();
80
81 return cpu_clock(this_cpu) >> 30LL; /* 2^30 ~= 10^9 */
82}
83
603a148f
MSB
84static void check_hung_task(struct task_struct *t, unsigned long now,
85 unsigned long timeout)
e162b39a
MSB
86{
87 unsigned long switch_count = t->nvcsw + t->nivcsw;
88
89 if (t->flags & PF_FROZEN)
90 return;
91
92 if (switch_count != t->last_switch_count || !t->last_switch_timestamp) {
93 t->last_switch_count = switch_count;
94 t->last_switch_timestamp = now;
95 return;
96 }
603a148f 97 if ((long)(now - t->last_switch_timestamp) < timeout)
e162b39a
MSB
98 return;
99 if (!sysctl_hung_task_warnings)
100 return;
101 sysctl_hung_task_warnings--;
102
103 /*
104 * Ok, the task did not get scheduled for more than 2 minutes,
105 * complain:
106 */
107 printk(KERN_ERR "INFO: task %s:%d blocked for more than "
603a148f 108 "%ld seconds.\n", t->comm, t->pid, timeout);
e162b39a
MSB
109 printk(KERN_ERR "\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
110 " disables this message.\n");
111 sched_show_task(t);
112 __debug_show_held_locks(t);
113
114 t->last_switch_timestamp = now;
115 touch_nmi_watchdog();
116
117 if (sysctl_hung_task_panic)
118 panic("hung_task: blocked tasks");
119}
120
ce9dbe24
MSB
121/*
122 * To avoid extending the RCU grace period for an unbounded amount of time,
123 * periodically exit the critical section and enter a new one.
124 *
125 * For preemptible RCU it is sufficient to call rcu_read_unlock in order
126 * exit the grace period. For classic RCU, a reschedule is required.
127 */
128static void rcu_lock_break(struct task_struct *g, struct task_struct *t)
129{
130 get_task_struct(g);
131 get_task_struct(t);
132 rcu_read_unlock();
133 cond_resched();
134 rcu_read_lock();
135 put_task_struct(t);
136 put_task_struct(g);
137}
138
e162b39a
MSB
139/*
140 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
141 * a really long time (120 seconds). If that happens, print out
142 * a warning.
143 */
603a148f 144static void check_hung_uninterruptible_tasks(unsigned long timeout)
e162b39a
MSB
145{
146 int max_count = sysctl_hung_task_check_count;
ce9dbe24 147 int batch_count = HUNG_TASK_BATCHING;
e162b39a
MSB
148 unsigned long now = get_timestamp();
149 struct task_struct *g, *t;
150
151 /*
152 * If the system crashed already then all bets are off,
153 * do not report extra hung tasks:
154 */
155 if (test_taint(TAINT_DIE) || did_panic)
156 return;
157
94be52dc 158 rcu_read_lock();
e162b39a
MSB
159 do_each_thread(g, t) {
160 if (!--max_count)
161 goto unlock;
ce9dbe24
MSB
162 if (!--batch_count) {
163 batch_count = HUNG_TASK_BATCHING;
164 rcu_lock_break(g, t);
165 /* Exit if t or g was unhashed during refresh. */
166 if (t->state == TASK_DEAD || g->state == TASK_DEAD)
167 goto unlock;
168 }
e162b39a
MSB
169 /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
170 if (t->state == TASK_UNINTERRUPTIBLE)
603a148f 171 check_hung_task(t, now, timeout);
e162b39a
MSB
172 } while_each_thread(g, t);
173 unlock:
94be52dc 174 rcu_read_unlock();
e162b39a
MSB
175}
176
177static void update_poll_jiffies(void)
178{
179 /* timeout of 0 will disable the watchdog */
180 if (sysctl_hung_task_timeout_secs == 0)
181 hung_task_poll_jiffies = MAX_SCHEDULE_TIMEOUT;
182 else
183 hung_task_poll_jiffies = sysctl_hung_task_timeout_secs * HZ / 2;
184}
185
186/*
187 * Process updating of timeout sysctl
188 */
189int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
190 struct file *filp, void __user *buffer,
191 size_t *lenp, loff_t *ppos)
192{
193 int ret;
194
195 ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos);
196
197 if (ret || !write)
198 goto out;
199
200 update_poll_jiffies();
201
202 wake_up_process(watchdog_task);
203
204 out:
205 return ret;
206}
207
208/*
209 * kthread which checks for tasks stuck in D state
210 */
211static int watchdog(void *dummy)
212{
213 set_user_nice(current, 0);
214 update_poll_jiffies();
215
216 for ( ; ; ) {
603a148f
MSB
217 unsigned long timeout;
218
e162b39a 219 while (schedule_timeout_interruptible(hung_task_poll_jiffies));
603a148f
MSB
220
221 /*
222 * Need to cache timeout here to avoid timeout being set
223 * to 0 via sysctl while inside check_hung_*_tasks().
224 */
225 timeout = sysctl_hung_task_timeout_secs;
226 if (timeout)
227 check_hung_uninterruptible_tasks(timeout);
e162b39a
MSB
228 }
229
230 return 0;
231}
232
233static int __init hung_task_init(void)
234{
235 atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
236 watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
237
238 return 0;
239}
240
241module_init(hung_task_init);