Back to home page

LXR

 
 

    


0001 /*
0002  * Detect Hung Task
0003  *
0004  * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
0005  *
0006  */
0007 
0008 #include <linux/mm.h>
0009 #include <linux/cpu.h>
0010 #include <linux/nmi.h>
0011 #include <linux/init.h>
0012 #include <linux/delay.h>
0013 #include <linux/freezer.h>
0014 #include <linux/kthread.h>
0015 #include <linux/lockdep.h>
0016 #include <linux/export.h>
0017 #include <linux/sysctl.h>
0018 #include <linux/utsname.h>
0019 #include <trace/events/sched.h>
0020 
0021 /*
0022  * The number of tasks checked:
0023  */
0024 int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
0025 
0026 /*
0027  * Limit number of tasks checked in a batch.
0028  *
0029  * This value controls the preemptibility of khungtaskd since preemption
0030  * is disabled during the critical section. It also controls the size of
0031  * the RCU grace period. So it needs to be upper-bound.
0032  */
0033 #define HUNG_TASK_BATCHING 1024
0034 
0035 /*
0036  * Zero means infinite timeout - no checking done:
0037  */
0038 unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
0039 
0040 int __read_mostly sysctl_hung_task_warnings = 10;
0041 
0042 static int __read_mostly did_panic;
0043 
0044 static struct task_struct *watchdog_task;
0045 
0046 /*
0047  * Should we panic (and reboot, if panic_timeout= is set) when a
0048  * hung task is detected:
0049  */
0050 unsigned int __read_mostly sysctl_hung_task_panic =
0051                 CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
0052 
0053 static int __init hung_task_panic_setup(char *str)
0054 {
0055     int rc = kstrtouint(str, 0, &sysctl_hung_task_panic);
0056 
0057     if (rc)
0058         return rc;
0059     return 1;
0060 }
0061 __setup("hung_task_panic=", hung_task_panic_setup);
0062 
0063 static int
0064 hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
0065 {
0066     did_panic = 1;
0067 
0068     return NOTIFY_DONE;
0069 }
0070 
0071 static struct notifier_block panic_block = {
0072     .notifier_call = hung_task_panic,
0073 };
0074 
0075 static void check_hung_task(struct task_struct *t, unsigned long timeout)
0076 {
0077     unsigned long switch_count = t->nvcsw + t->nivcsw;
0078 
0079     /*
0080      * Ensure the task is not frozen.
0081      * Also, skip vfork and any other user process that freezer should skip.
0082      */
0083     if (unlikely(t->flags & (PF_FROZEN | PF_FREEZER_SKIP)))
0084         return;
0085 
0086     /*
0087      * When a freshly created task is scheduled once, changes its state to
0088      * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
0089      * musn't be checked.
0090      */
0091     if (unlikely(!switch_count))
0092         return;
0093 
0094     if (switch_count != t->last_switch_count) {
0095         t->last_switch_count = switch_count;
0096         return;
0097     }
0098 
0099     trace_sched_process_hang(t);
0100 
0101     if (!sysctl_hung_task_warnings && !sysctl_hung_task_panic)
0102         return;
0103 
0104     /*
0105      * Ok, the task did not get scheduled for more than 2 minutes,
0106      * complain:
0107      */
0108     if (sysctl_hung_task_warnings) {
0109         if (sysctl_hung_task_warnings > 0)
0110             sysctl_hung_task_warnings--;
0111         pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
0112             t->comm, t->pid, timeout);
0113         pr_err("      %s %s %.*s\n",
0114             print_tainted(), init_utsname()->release,
0115             (int)strcspn(init_utsname()->version, " "),
0116             init_utsname()->version);
0117         pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
0118             " disables this message.\n");
0119         sched_show_task(t);
0120         debug_show_all_locks();
0121     }
0122 
0123     touch_nmi_watchdog();
0124 
0125     if (sysctl_hung_task_panic) {
0126         trigger_all_cpu_backtrace();
0127         panic("hung_task: blocked tasks");
0128     }
0129 }
0130 
0131 /*
0132  * To avoid extending the RCU grace period for an unbounded amount of time,
0133  * periodically exit the critical section and enter a new one.
0134  *
0135  * For preemptible RCU it is sufficient to call rcu_read_unlock in order
0136  * to exit the grace period. For classic RCU, a reschedule is required.
0137  */
0138 static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
0139 {
0140     bool can_cont;
0141 
0142     get_task_struct(g);
0143     get_task_struct(t);
0144     rcu_read_unlock();
0145     cond_resched();
0146     rcu_read_lock();
0147     can_cont = pid_alive(g) && pid_alive(t);
0148     put_task_struct(t);
0149     put_task_struct(g);
0150 
0151     return can_cont;
0152 }
0153 
0154 /*
0155  * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
0156  * a really long time (120 seconds). If that happens, print out
0157  * a warning.
0158  */
0159 static void check_hung_uninterruptible_tasks(unsigned long timeout)
0160 {
0161     int max_count = sysctl_hung_task_check_count;
0162     int batch_count = HUNG_TASK_BATCHING;
0163     struct task_struct *g, *t;
0164 
0165     /*
0166      * If the system crashed already then all bets are off,
0167      * do not report extra hung tasks:
0168      */
0169     if (test_taint(TAINT_DIE) || did_panic)
0170         return;
0171 
0172     rcu_read_lock();
0173     for_each_process_thread(g, t) {
0174         if (!max_count--)
0175             goto unlock;
0176         if (!--batch_count) {
0177             batch_count = HUNG_TASK_BATCHING;
0178             if (!rcu_lock_break(g, t))
0179                 goto unlock;
0180         }
0181         /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
0182         if (t->state == TASK_UNINTERRUPTIBLE)
0183             check_hung_task(t, timeout);
0184     }
0185  unlock:
0186     rcu_read_unlock();
0187 }
0188 
0189 static long hung_timeout_jiffies(unsigned long last_checked,
0190                  unsigned long timeout)
0191 {
0192     /* timeout of 0 will disable the watchdog */
0193     return timeout ? last_checked - jiffies + timeout * HZ :
0194         MAX_SCHEDULE_TIMEOUT;
0195 }
0196 
0197 /*
0198  * Process updating of timeout sysctl
0199  */
0200 int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
0201                   void __user *buffer,
0202                   size_t *lenp, loff_t *ppos)
0203 {
0204     int ret;
0205 
0206     ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
0207 
0208     if (ret || !write)
0209         goto out;
0210 
0211     wake_up_process(watchdog_task);
0212 
0213  out:
0214     return ret;
0215 }
0216 
0217 static atomic_t reset_hung_task = ATOMIC_INIT(0);
0218 
0219 void reset_hung_task_detector(void)
0220 {
0221     atomic_set(&reset_hung_task, 1);
0222 }
0223 EXPORT_SYMBOL_GPL(reset_hung_task_detector);
0224 
0225 /*
0226  * kthread which checks for tasks stuck in D state
0227  */
0228 static int watchdog(void *dummy)
0229 {
0230     unsigned long hung_last_checked = jiffies;
0231 
0232     set_user_nice(current, 0);
0233 
0234     for ( ; ; ) {
0235         unsigned long timeout = sysctl_hung_task_timeout_secs;
0236         long t = hung_timeout_jiffies(hung_last_checked, timeout);
0237 
0238         if (t <= 0) {
0239             if (!atomic_xchg(&reset_hung_task, 0))
0240                 check_hung_uninterruptible_tasks(timeout);
0241             hung_last_checked = jiffies;
0242             continue;
0243         }
0244         schedule_timeout_interruptible(t);
0245     }
0246 
0247     return 0;
0248 }
0249 
0250 static int __init hung_task_init(void)
0251 {
0252     atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
0253     watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
0254 
0255     return 0;
0256 }
0257 subsys_initcall(hung_task_init);