File Index
Files and directories
Sign in
FILE

hung_task.c

File preview with syntax highlighting.

linux-6.19.14/kernel/hung_task.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Detect Hung Task
  4 *
  5 * kernel/hung_task.c - kernel thread for detecting tasks stuck in D state
  6 *
  7 */
  8
  9#include <linux/mm.h>
 10#include <linux/cpu.h>
 11#include <linux/nmi.h>
 12#include <linux/init.h>
 13#include <linux/delay.h>
 14#include <linux/freezer.h>
 15#include <linux/kthread.h>
 16#include <linux/lockdep.h>
 17#include <linux/export.h>
 18#include <linux/panic_notifier.h>
 19#include <linux/sysctl.h>
 20#include <linux/suspend.h>
 21#include <linux/utsname.h>
 22#include <linux/sched/signal.h>
 23#include <linux/sched/debug.h>
 24#include <linux/sched/sysctl.h>
 25#include <linux/hung_task.h>
 26#include <linux/rwsem.h>
 27#include <linux/sys_info.h>
 28
 29#include <trace/events/sched.h>
 30
 31/*
 32 * The number of tasks checked:
 33 */
 34static int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
 35
 36/*
 37 * Total number of tasks detected as hung since boot:
 38 */
 39static unsigned long __read_mostly sysctl_hung_task_detect_count;
 40
 41/*
 42 * Limit number of tasks checked in a batch.
 43 *
 44 * This value controls the preemptibility of khungtaskd since preemption
 45 * is disabled during the critical section. It also controls the size of
 46 * the RCU grace period. So it needs to be upper-bound.
 47 */
 48#define HUNG_TASK_LOCK_BREAK (HZ / 10)
 49
 50/*
 51 * Zero means infinite timeout - no checking done:
 52 */
 53unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_TASK_TIMEOUT;
 54
 55/*
 56 * Zero (default value) means use sysctl_hung_task_timeout_secs:
 57 */
 58static unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
 59
 60static int __read_mostly sysctl_hung_task_warnings = 10;
 61
 62static int __read_mostly did_panic;
 63static bool hung_task_call_panic;
 64
 65static struct task_struct *watchdog_task;
 66
 67/*
 68 * A bitmask to control what kinds of system info to be printed when
 69 * a hung task is detected, it could be task, memory, lock etc. Refer
 70 * include/linux/sys_info.h for detailed bit definition.
 71 */
 72static unsigned long hung_task_si_mask;
 73
 74#ifdef CONFIG_SMP
 75/*
 76 * Should we dump all CPUs backtraces in a hung task event?
 77 * Defaults to 0, can be changed via sysctl.
 78 */
 79static unsigned int __read_mostly sysctl_hung_task_all_cpu_backtrace;
 80#else
 81#define sysctl_hung_task_all_cpu_backtrace 0
 82#endif /* CONFIG_SMP */
 83
 84/*
 85 * Should we panic (and reboot, if panic_timeout= is set) when a
 86 * hung task is detected:
 87 */
 88static unsigned int __read_mostly sysctl_hung_task_panic =
 89	CONFIG_BOOTPARAM_HUNG_TASK_PANIC;
 90
 91static int
 92hung_task_panic(struct notifier_block *this, unsigned long event, void *ptr)
 93{
 94	did_panic = 1;
 95
 96	return NOTIFY_DONE;
 97}
 98
 99static struct notifier_block panic_block = {
100	.notifier_call = hung_task_panic,
101};
102
103static bool task_is_hung(struct task_struct *t, unsigned long timeout)
104{
105	unsigned long switch_count = t->nvcsw + t->nivcsw;
106	unsigned int state = READ_ONCE(t->__state);
107
108	/*
109	 * skip the TASK_KILLABLE tasks -- these can be killed
110	 * skip the TASK_IDLE tasks -- those are genuinely idle
111	 * skip the TASK_FROZEN task -- it reasonably stops scheduling by freezer
112	 */
113	if (!(state & TASK_UNINTERRUPTIBLE) ||
114	    (state & (TASK_WAKEKILL | TASK_NOLOAD | TASK_FROZEN)))
115		return false;
116
117	/*
118	 * When a freshly created task is scheduled once, changes its state to
119	 * TASK_UNINTERRUPTIBLE without having ever been switched out once, it
120	 * musn't be checked.
121	 */
122	if (unlikely(!switch_count))
123		return false;
124
125	if (switch_count != t->last_switch_count) {
126		t->last_switch_count = switch_count;
127		t->last_switch_time = jiffies;
128		return false;
129	}
130	if (time_is_after_jiffies(t->last_switch_time + timeout * HZ))
131		return false;
132
133	return true;
134}
135
136#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
137static void debug_show_blocker(struct task_struct *task, unsigned long timeout)
138{
139	struct task_struct *g, *t;
140	unsigned long owner, blocker, blocker_type;
141	const char *rwsem_blocked_by, *rwsem_blocked_as;
142
143	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "No rcu lock held");
144
145	blocker = READ_ONCE(task->blocker);
146	if (!blocker)
147		return;
148
149	blocker_type = hung_task_get_blocker_type(blocker);
150
151	switch (blocker_type) {
152	case BLOCKER_TYPE_MUTEX:
153		owner = mutex_get_owner(hung_task_blocker_to_lock(blocker));
154		break;
155	case BLOCKER_TYPE_SEM:
156		owner = sem_last_holder(hung_task_blocker_to_lock(blocker));
157		break;
158	case BLOCKER_TYPE_RWSEM_READER:
159	case BLOCKER_TYPE_RWSEM_WRITER:
160		owner = (unsigned long)rwsem_owner(
161					hung_task_blocker_to_lock(blocker));
162		rwsem_blocked_as = (blocker_type == BLOCKER_TYPE_RWSEM_READER) ?
163					"reader" : "writer";
164		rwsem_blocked_by = is_rwsem_reader_owned(
165					hung_task_blocker_to_lock(blocker)) ?
166					"reader" : "writer";
167		break;
168	default:
169		WARN_ON_ONCE(1);
170		return;
171	}
172
173
174	if (unlikely(!owner)) {
175		switch (blocker_type) {
176		case BLOCKER_TYPE_MUTEX:
177			pr_err("INFO: task %s:%d is blocked on a mutex, but the owner is not found.\n",
178			       task->comm, task->pid);
179			break;
180		case BLOCKER_TYPE_SEM:
181			pr_err("INFO: task %s:%d is blocked on a semaphore, but the last holder is not found.\n",
182			       task->comm, task->pid);
183			break;
184		case BLOCKER_TYPE_RWSEM_READER:
185		case BLOCKER_TYPE_RWSEM_WRITER:
186			pr_err("INFO: task %s:%d is blocked on an rw-semaphore, but the owner is not found.\n",
187			       task->comm, task->pid);
188			break;
189		}
190		return;
191	}
192
193	/* Ensure the owner information is correct. */
194	for_each_process_thread(g, t) {
195		if ((unsigned long)t != owner)
196			continue;
197
198		switch (blocker_type) {
199		case BLOCKER_TYPE_MUTEX:
200			pr_err("INFO: task %s:%d is blocked on a mutex likely owned by task %s:%d.\n",
201			       task->comm, task->pid, t->comm, t->pid);
202			break;
203		case BLOCKER_TYPE_SEM:
204			pr_err("INFO: task %s:%d blocked on a semaphore likely last held by task %s:%d\n",
205			       task->comm, task->pid, t->comm, t->pid);
206			break;
207		case BLOCKER_TYPE_RWSEM_READER:
208		case BLOCKER_TYPE_RWSEM_WRITER:
209			pr_err("INFO: task %s:%d <%s> blocked on an rw-semaphore likely owned by task %s:%d <%s>\n",
210			       task->comm, task->pid, rwsem_blocked_as, t->comm,
211			       t->pid, rwsem_blocked_by);
212			break;
213		}
214		/* Avoid duplicated task dump, skip if the task is also hung. */
215		if (!task_is_hung(t, timeout))
216			sched_show_task(t);
217		return;
218	}
219}
220#else
221static inline void debug_show_blocker(struct task_struct *task, unsigned long timeout)
222{
223}
224#endif
225
226static void check_hung_task(struct task_struct *t, unsigned long timeout,
227		unsigned long prev_detect_count)
228{
229	unsigned long total_hung_task;
230
231	if (!task_is_hung(t, timeout))
232		return;
233
234	/*
235	 * This counter tracks the total number of tasks detected as hung
236	 * since boot.
237	 */
238	sysctl_hung_task_detect_count++;
239
240	total_hung_task = sysctl_hung_task_detect_count - prev_detect_count;
241	trace_sched_process_hang(t);
242
243	if (sysctl_hung_task_panic && total_hung_task >= sysctl_hung_task_panic) {
244		console_verbose();
245		hung_task_call_panic = true;
246	}
247
248	/*
249	 * Ok, the task did not get scheduled for more than 2 minutes,
250	 * complain:
251	 */
252	if (sysctl_hung_task_warnings || hung_task_call_panic) {
253		if (sysctl_hung_task_warnings > 0)
254			sysctl_hung_task_warnings--;
255		pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
256		       t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
257		pr_err("      %s %s %.*s\n",
258			print_tainted(), init_utsname()->release,
259			(int)strcspn(init_utsname()->version, " "),
260			init_utsname()->version);
261		if (t->flags & PF_POSTCOREDUMP)
262			pr_err("      Blocked by coredump.\n");
263		pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
264			" disables this message.\n");
265		sched_show_task(t);
266		debug_show_blocker(t, timeout);
267
268		if (!sysctl_hung_task_warnings)
269			pr_info("Future hung task reports are suppressed, see sysctl kernel.hung_task_warnings\n");
270	}
271
272	touch_nmi_watchdog();
273}
274
275/*
276 * To avoid extending the RCU grace period for an unbounded amount of time,
277 * periodically exit the critical section and enter a new one.
278 *
279 * For preemptible RCU it is sufficient to call rcu_read_unlock in order
280 * to exit the grace period. For classic RCU, a reschedule is required.
281 */
282static bool rcu_lock_break(struct task_struct *g, struct task_struct *t)
283{
284	bool can_cont;
285
286	get_task_struct(g);
287	get_task_struct(t);
288	rcu_read_unlock();
289	cond_resched();
290	rcu_read_lock();
291	can_cont = pid_alive(g) && pid_alive(t);
292	put_task_struct(t);
293	put_task_struct(g);
294
295	return can_cont;
296}
297
298/*
299 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
300 * a really long time (120 seconds). If that happens, print out
301 * a warning.
302 */
303static void check_hung_uninterruptible_tasks(unsigned long timeout)
304{
305	int max_count = sysctl_hung_task_check_count;
306	unsigned long last_break = jiffies;
307	struct task_struct *g, *t;
308	unsigned long prev_detect_count = sysctl_hung_task_detect_count;
309	int need_warning = sysctl_hung_task_warnings;
310	unsigned long si_mask = hung_task_si_mask;
311
312	/*
313	 * If the system crashed already then all bets are off,
314	 * do not report extra hung tasks:
315	 */
316	if (test_taint(TAINT_DIE) || did_panic)
317		return;
318
319
320	rcu_read_lock();
321	for_each_process_thread(g, t) {
322
323		if (!max_count--)
324			goto unlock;
325		if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
326			if (!rcu_lock_break(g, t))
327				goto unlock;
328			last_break = jiffies;
329		}
330
331		check_hung_task(t, timeout, prev_detect_count);
332	}
333 unlock:
334	rcu_read_unlock();
335
336	if (!(sysctl_hung_task_detect_count - prev_detect_count))
337		return;
338
339	if (need_warning || hung_task_call_panic) {
340		si_mask |= SYS_INFO_LOCKS;
341
342		if (sysctl_hung_task_all_cpu_backtrace)
343			si_mask |= SYS_INFO_ALL_BT;
344	}
345
346	sys_info(si_mask);
347
348	if (hung_task_call_panic)
349		panic("hung_task: blocked tasks");
350}
351
352static long hung_timeout_jiffies(unsigned long last_checked,
353				 unsigned long timeout)
354{
355	/* timeout of 0 will disable the watchdog */
356	return timeout ? last_checked - jiffies + timeout * HZ :
357		MAX_SCHEDULE_TIMEOUT;
358}
359
360#ifdef CONFIG_SYSCTL
361/*
362 * Process updating of timeout sysctl
363 */
364static int proc_dohung_task_timeout_secs(const struct ctl_table *table, int write,
365				  void *buffer,
366				  size_t *lenp, loff_t *ppos)
367{
368	int ret;
369
370	ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
371
372	if (ret || !write)
373		goto out;
374
375	wake_up_process(watchdog_task);
376
377 out:
378	return ret;
379}
380
381/*
382 * This is needed for proc_doulongvec_minmax of sysctl_hung_task_timeout_secs
383 * and hung_task_check_interval_secs
384 */
385static const unsigned long hung_task_timeout_max = (LONG_MAX / HZ);
386static const struct ctl_table hung_task_sysctls[] = {
387#ifdef CONFIG_SMP
388	{
389		.procname	= "hung_task_all_cpu_backtrace",
390		.data		= &sysctl_hung_task_all_cpu_backtrace,
391		.maxlen		= sizeof(int),
392		.mode		= 0644,
393		.proc_handler	= proc_dointvec_minmax,
394		.extra1		= SYSCTL_ZERO,
395		.extra2		= SYSCTL_ONE,
396	},
397#endif /* CONFIG_SMP */
398	{
399		.procname	= "hung_task_panic",
400		.data		= &sysctl_hung_task_panic,
401		.maxlen		= sizeof(int),
402		.mode		= 0644,
403		.proc_handler	= proc_dointvec_minmax,
404		.extra1		= SYSCTL_ZERO,
405		.extra2		= SYSCTL_INT_MAX,
406	},
407	{
408		.procname	= "hung_task_check_count",
409		.data		= &sysctl_hung_task_check_count,
410		.maxlen		= sizeof(int),
411		.mode		= 0644,
412		.proc_handler	= proc_dointvec_minmax,
413		.extra1		= SYSCTL_ZERO,
414	},
415	{
416		.procname	= "hung_task_timeout_secs",
417		.data		= &sysctl_hung_task_timeout_secs,
418		.maxlen		= sizeof(unsigned long),
419		.mode		= 0644,
420		.proc_handler	= proc_dohung_task_timeout_secs,
421		.extra2		= (void *)&hung_task_timeout_max,
422	},
423	{
424		.procname	= "hung_task_check_interval_secs",
425		.data		= &sysctl_hung_task_check_interval_secs,
426		.maxlen		= sizeof(unsigned long),
427		.mode		= 0644,
428		.proc_handler	= proc_dohung_task_timeout_secs,
429		.extra2		= (void *)&hung_task_timeout_max,
430	},
431	{
432		.procname	= "hung_task_warnings",
433		.data		= &sysctl_hung_task_warnings,
434		.maxlen		= sizeof(int),
435		.mode		= 0644,
436		.proc_handler	= proc_dointvec_minmax,
437		.extra1		= SYSCTL_NEG_ONE,
438	},
439	{
440		.procname	= "hung_task_detect_count",
441		.data		= &sysctl_hung_task_detect_count,
442		.maxlen		= sizeof(unsigned long),
443		.mode		= 0444,
444		.proc_handler	= proc_doulongvec_minmax,
445	},
446	{
447		.procname	= "hung_task_sys_info",
448		.data		= &hung_task_si_mask,
449		.maxlen         = sizeof(hung_task_si_mask),
450		.mode		= 0644,
451		.proc_handler	= sysctl_sys_info_handler,
452	},
453};
454
455static void __init hung_task_sysctl_init(void)
456{
457	register_sysctl_init("kernel", hung_task_sysctls);
458}
459#else
460#define hung_task_sysctl_init() do { } while (0)
461#endif /* CONFIG_SYSCTL */
462
463
464static atomic_t reset_hung_task = ATOMIC_INIT(0);
465
466void reset_hung_task_detector(void)
467{
468	atomic_set(&reset_hung_task, 1);
469}
470EXPORT_SYMBOL_GPL(reset_hung_task_detector);
471
472static bool hung_detector_suspended;
473
474static int hungtask_pm_notify(struct notifier_block *self,
475			      unsigned long action, void *hcpu)
476{
477	switch (action) {
478	case PM_SUSPEND_PREPARE:
479	case PM_HIBERNATION_PREPARE:
480	case PM_RESTORE_PREPARE:
481		hung_detector_suspended = true;
482		break;
483	case PM_POST_SUSPEND:
484	case PM_POST_HIBERNATION:
485	case PM_POST_RESTORE:
486		hung_detector_suspended = false;
487		break;
488	default:
489		break;
490	}
491	return NOTIFY_OK;
492}
493
494/*
495 * kthread which checks for tasks stuck in D state
496 */
497static int watchdog(void *dummy)
498{
499	unsigned long hung_last_checked = jiffies;
500
501	set_user_nice(current, 0);
502
503	for ( ; ; ) {
504		unsigned long timeout = sysctl_hung_task_timeout_secs;
505		unsigned long interval = sysctl_hung_task_check_interval_secs;
506		long t;
507
508		if (interval == 0)
509			interval = timeout;
510		interval = min_t(unsigned long, interval, timeout);
511		t = hung_timeout_jiffies(hung_last_checked, interval);
512		if (t <= 0) {
513			if (!atomic_xchg(&reset_hung_task, 0) &&
514			    !hung_detector_suspended)
515				check_hung_uninterruptible_tasks(timeout);
516			hung_last_checked = jiffies;
517			continue;
518		}
519		schedule_timeout_interruptible(t);
520	}
521
522	return 0;
523}
524
525static int __init hung_task_init(void)
526{
527	atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
528
529	/* Disable hung task detector on suspend */
530	pm_notifier(hungtask_pm_notify, 0);
531
532	watchdog_task = kthread_run(watchdog, NULL, "khungtaskd");
533	hung_task_sysctl_init();
534
535	return 0;
536}
537subsys_initcall(hung_task_init);
~/ Read-only file preview.