^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Detect Hung Task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/nmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/sched/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <trace/events/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <trace/hooks/hung_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * The number of tasks checked:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Limit number of tasks checked in a batch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * This value controls the preemptibility of khungtaskd since preemption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * is disabled during the critical section. It also controls the size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * the RCU grace period. So it needs to be upper-bound.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define HUNG_TASK_LOCK_BREAK (HZ / 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Zero means infinite timeout - no checking done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Zero (default value) means use sysctl_hung_task_timeout_secs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int __read_mostly sysctl_hung_task_warnings = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int __read_mostly did_panic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static bool hung_task_show_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static bool hung_task_call_panic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static bool hung_task_show_all_bt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct task_struct *watchdog_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * Should we dump all CPUs backtraces in a hung task event?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Defaults to 0, can be changed via sysctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #endif /* CONFIG_SMP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Should we panic (and reboot, if panic_timeout= is set) when a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * hung task is detected:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned int __read_mostly sysctl_hung_task_panic =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) CONFIG_BOOTPARAM_HUNG_TASK_PANIC_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) did_panic = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct notifier_block panic_block = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .notifier_call = hung_task_panic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void check_hung_task(struct task_struct *t, unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned long switch_count = t->nvcsw + t->nivcsw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * Ensure the task is not frozen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Also, skip vfork and any other user process that freezer should skip.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (unlikely(frozen_or_skipped(t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * When a freshly created task is scheduled once, changes its state to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * musn't be checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (unlikely(!switch_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (switch_count != t->last_switch_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) t->last_switch_count = switch_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) t->last_switch_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) trace_sched_process_hang(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (sysctl_hung_task_panic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) console_verbose();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) hung_task_show_lock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) hung_task_call_panic = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Ok, the task did not get scheduled for more than 2 minutes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * complain:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (sysctl_hung_task_warnings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (sysctl_hung_task_warnings > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sysctl_hung_task_warnings--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) pr_err(" %s %s %.*s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) print_tainted(), init_utsname()->release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) (int)strcspn(init_utsname()->version, " "),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) init_utsname()->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) " disables this message.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) sched_show_task(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) hung_task_show_lock = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (sysctl_hung_task_all_cpu_backtrace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) hung_task_show_all_bt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) touch_nmi_watchdog();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * To avoid extending the RCU grace period for an unbounded amount of time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * periodically exit the critical section and enter a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * For preemptible RCU it is sufficient to call rcu_read_unlock in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * to exit the grace period. For classic RCU, a reschedule is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bool can_cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) get_task_struct(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) get_task_struct(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) can_cont = pid_alive(g) && pid_alive(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) put_task_struct(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) put_task_struct(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return can_cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * a really long time (120 seconds). If that happens, print out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * a warning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void check_hung_uninterruptible_tasks(unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int max_count = sysctl_hung_task_check_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) unsigned long last_break = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct task_struct *g, *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) bool need_check = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * If the system crashed already then all bets are off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * do not report extra hung tasks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (test_taint(TAINT_DIE) || did_panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) hung_task_show_lock = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) for_each_process_thread(g, t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!max_count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!rcu_lock_break(g, t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) last_break = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) trace_android_vh_check_uninterruptible_tasks(t, timeout, &need_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (need_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* use "==" to skip the TASK_KILLABLE tasks waiting on NFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (t->state == TASK_UNINTERRUPTIBLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) check_hung_task(t, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) trace_android_vh_check_uninterruptible_tasks_dn(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (hung_task_show_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) debug_show_all_locks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (hung_task_show_all_bt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) hung_task_show_all_bt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) trigger_all_cpu_backtrace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (hung_task_call_panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) panic("hung_task: blocked tasks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static long hung_timeout_jiffies(unsigned long last_checked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) unsigned long timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* timeout of 0 will disable the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return timeout ? last_checked - jiffies + timeout * HZ :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) MAX_SCHEDULE_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * Process updating of timeout sysctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) void *buffer, size_t *lenp, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ret || !write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) wake_up_process(watchdog_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static atomic_t reset_hung_task = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) void reset_hung_task_detector(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) atomic_set(&reset_hung_task, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) EXPORT_SYMBOL_GPL(reset_hung_task_detector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static bool hung_detector_suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int hungtask_pm_notify(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) unsigned long action, void *hcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) case PM_SUSPEND_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case PM_HIBERNATION_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) case PM_RESTORE_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) hung_detector_suspended = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case PM_POST_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case PM_POST_HIBERNATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case PM_POST_RESTORE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) hung_detector_suspended = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * kthread which checks for tasks stuck in D state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int watchdog(void *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned long hung_last_checked = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) set_user_nice(current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) for ( ; ; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) unsigned long timeout = sysctl_hung_task_timeout_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned long interval = sysctl_hung_task_check_interval_secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) long t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (interval == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) interval = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) interval = min_t(unsigned long, interval, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) t = hung_timeout_jiffies(hung_last_checked, interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (t <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (!atomic_xchg(&reset_hung_task, 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) !hung_detector_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) check_hung_uninterruptible_tasks(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) hung_last_checked = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) schedule_timeout_interruptible(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int __init hung_task_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Disable hung task detector on suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) pm_notifier(hungtask_pm_notify, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) subsys_initcall(hung_task_init);