Skip to contentSkip to conversation

hungtask: add filter kthread/check comm

PATCH In review chenguanyou 2 participants 2 messages May 21, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hungtask: add filter kthread/check comm

Some kernel threads are always in D state, when we enable hung_task, it will misjudge, we should skip these to narrow the scope.

exp mtk mobilephone:

root            420   420      2       0      0 kwdt_thread         0 D wdtk-0
root            421   421      2       0      0 kwdt_thread         0 D wdtk-1
root            422   422      2       0      0 kwdt_thread         0 D wdtk-2
root            423   423      2       0      0 kwdt_thread         0 D wdtk-3
root            424   424      2       0      0 kwdt_thread         0 D wdtk-4
root            425   425      2       0      0 kwdt_thread         0 D wdtk-5
root            426   426      2       0      0 kwdt_thread         0 D wdtk-6
root            427   427      2       0      0 kwdt_thread         0 D wdtk-7
root            435   435      2       0      0 mtk_lpm_monitor_thread 0 D LPM-0
root            436   436      2       0      0 mtk_lpm_monitor_thread 0 D LPM-1
root            437   437      2       0      0 mtk_lpm_monitor_thread 0 D LPM-2
root            438   438      2       0      0 mtk_lpm_monitor_thread 0 D LPM-3
root            439   439      2       0      0 mtk_lpm_monitor_thread 0 D LPM-4
root            440   440      2       0      0 mtk_lpm_monitor_thread 0 D LPM-5
root            441   441      2       0      0 mtk_lpm_monitor_thread 0 D LPM-6
root            442   442      2       0      0 mtk_lpm_monitor_thread 0 D LPM-7
---
 include/linux/sched/sysctl.h |  4 ++++
 kernel/hung_task.c           | 17 +++++++++++++++++
 kernel/sysctl.c              | 16 ++++++++++++++++
 lib/Kconfig.debug            | 24 ++++++++++++++++++++++++
 4 files changed, 61 insertions(+)
include/linux/sched/sysctl.h +4 -0
index 3c31ba88aca5..e8a9a28215bf 100644
@@ -14,11 +14,15 @@ extern unsigned int sysctl_hung_task_all_cpu_backtrace;
 #define sysctl_hung_task_all_cpu_backtrace 0
 #endif /* CONFIG_SMP */
 
+#define TASK_COMM_LEN 16
+
 extern int	     sysctl_hung_task_check_count;
 extern unsigned int  sysctl_hung_task_panic;
 extern unsigned long sysctl_hung_task_timeout_secs;
 extern unsigned long sysctl_hung_task_check_interval_secs;
 extern int sysctl_hung_task_warnings;
+extern unsigned int sysctl_hung_task_filter_kthread;
+extern char sysctl_hung_task_check_comm[TASK_COMM_LEN];
 int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
 		void *buffer, size_t *lenp, loff_t *ppos);
 #else
kernel/hung_task.c +17 -0
index 396ebaebea3f..e018563d4882 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/sysctl.h>
 
 #include <trace/events/sched.h>
+#include <linux/string.h>
 
 /*
  * The number of tasks checked:
@@ -48,6 +49,16 @@ unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_
  */
 unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
 
+/*
+ * Non zero means no checking kthread
+ */
+unsigned int __read_mostly sysctl_hung_task_filter_kthread = CONFIG_DEFAULT_HUNG_TASK_FILTER_KTHREAD;
+
+/*
+ * Only one
+ */
+char __read_mostly sysctl_hung_task_check_comm[TASK_COMM_LEN] = CONFIG_DEFAULT_HUNG_TASK_CHECK_COMM;
+
 int __read_mostly sysctl_hung_task_warnings = 10;
 
 static int __read_mostly did_panic;
@@ -88,6 +99,12 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)
 {
 	unsigned long switch_count = t->nvcsw + t->nivcsw;
 
+	if (unlikely(strlen(sysctl_hung_task_check_comm) && !strstr(t->comm, sysctl_hung_task_check_comm)))
+		return;
+
+	if (unlikely(sysctl_hung_task_filter_kthread && t->flags & PF_KTHREAD))
+		return;
+
 	/*
 	 * Ensure the task is not frozen.
 	 * Also, skip vfork and any other user process that freezer should skip.
kernel/sysctl.c +16 -0
index 62fbd09b5dc1..1daede87c88d 100644
@@ -2519,6 +2519,22 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &neg_one,
 	},
+	{
+		.procname	= "hung_task_filter_kthread",
+		.data		= &sysctl_hung_task_filter_kthread,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler   = proc_dointvec_minmax,
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_ONE,
+	},
+	{
+		.procname	= "hung_task_check_comm",
+		.data		= &sysctl_hung_task_check_comm,
+		.maxlen		= TASK_COMM_LEN,
+		.mode		= 0644,
+		.proc_handler	= proc_dostring,
+	},
 #endif
 #ifdef CONFIG_RT_MUTEXES
 	{
lib/Kconfig.debug +24 -0
index 2c7f46b366f1..6eab8cf0c37f 100644
@@ -1101,6 +1101,30 @@ config DEFAULT_HUNG_TASK_TIMEOUT
 	  A timeout of 0 disables the check.  The default is two minutes.
 	  Keeping the default should be fine in most cases.
 
+config DEFAULT_HUNG_TASK_FILTER_KTHREAD
+	int "Default filter kthread for hung task"
+	depends on DETECT_HUNG_TASK
+	range 0 1
+	default 0
+	help
+	  This option controls filter kthread used to determine when
+	  a kernel task has become "state=TASK_UNINTERRUPTIBLE" and should be skip.
+
+	  It can be adjusted at runtime via the kernel.hung_task_filter_kthread
+	  sysctl or by writing a value to
+	  /proc/sys/kernel/hung_task_filter_kthread.
+
+	  A filter of 1 disables the check
+
+config DEFAULT_HUNG_TASK_CHECK_COMM
+	string "Default check only one comm"
+	depends on DETECT_HUNG_TASK
+	default ""
+	help
+	  It can be adjusted at runtime via the kernel.hung_task_check_comm
+	  sysctl or by writing a value to
+	  /proc/sys/kernel/hung_task_check_comm.
+
 config BOOTPARAM_HUNG_TASK_PANIC
 	bool "Panic (Reboot) On Hung Tasks"
 	depends on DETECT_HUNG_TASK
Signature · 1 line
2.17.1
#1 lore raw
Quoted 62 lines · chenguanyou

Some kernel threads are always in D state, when we enable hung_task, it will misjudge, we should skip these to narrow the scope.

exp mtk mobilephone:

root            420   420      2       0      0 kwdt_thread         0 D wdtk-0
root            421   421      2       0      0 kwdt_thread         0 D wdtk-1
root            422   422      2       0      0 kwdt_thread         0 D wdtk-2
root            423   423      2       0      0 kwdt_thread         0 D wdtk-3
root            424   424      2       0      0 kwdt_thread         0 D wdtk-4
root            425   425      2       0      0 kwdt_thread         0 D wdtk-5
root            426   426      2       0      0 kwdt_thread         0 D wdtk-6
root            427   427      2       0      0 kwdt_thread         0 D wdtk-7
root            435   435      2       0      0 mtk_lpm_monitor_thread 0 D LPM-0
root            436   436      2       0      0 mtk_lpm_monitor_thread 0 D LPM-1
root            437   437      2       0      0 mtk_lpm_monitor_thread 0 D LPM-2
root            438   438      2       0      0 mtk_lpm_monitor_thread 0 D LPM-3
root            439   439      2       0      0 mtk_lpm_monitor_thread 0 D LPM-4
root            440   440      2       0      0 mtk_lpm_monitor_thread 0 D LPM-5
root            441   441      2       0      0 mtk_lpm_monitor_thread 0 D LPM-6
root            442   442      2       0      0 mtk_lpm_monitor_thread 0 D LPM-7
---
 include/linux/sched/sysctl.h |  4 ++++
 kernel/hung_task.c           | 17 +++++++++++++++++
 kernel/sysctl.c              | 16 ++++++++++++++++
 lib/Kconfig.debug            | 24 ++++++++++++++++++++++++
 4 files changed, 61 insertions(+)
include/linux/sched/sysctl.h +4 -0
index 3c31ba88aca5..e8a9a28215bf 100644
@@ -14,11 +14,15 @@ extern unsigned int sysctl_hung_task_all_cpu_backtrace;
 #define sysctl_hung_task_all_cpu_backtrace 0
 #endif /* CONFIG_SMP */
 
+#define TASK_COMM_LEN 16
+
 extern int	     sysctl_hung_task_check_count;
 extern unsigned int  sysctl_hung_task_panic;
 extern unsigned long sysctl_hung_task_timeout_secs;
 extern unsigned long sysctl_hung_task_check_interval_secs;
 extern int sysctl_hung_task_warnings;
+extern unsigned int sysctl_hung_task_filter_kthread;
+extern char sysctl_hung_task_check_comm[TASK_COMM_LEN];
 int proc_dohung_task_timeout_secs(struct ctl_table *table, int write,
 		void *buffer, size_t *lenp, loff_t *ppos);
 #else
kernel/hung_task.c +1 -0
index 396ebaebea3f..e018563d4882 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/sysctl.h>
 
 #include <trace/events/sched.h>
+#include <linux/string.h>
 
 /*
  * The number of tasks checked:

kernel/hung_task.c unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_ · line 53

Quoted 5 lines
kernel/hung_task.c +2 -0
@@ -48,6 +49,16 @@ unsigned long __read_mostly sysctl_hung_task_timeout_secs = CONFIG_DEFAULT_HUNG_
  */
 unsigned long __read_mostly sysctl_hung_task_check_interval_secs;
 
+/*
+ * Non zero means no checking kthread

Non-zero

(also mentioned in v1 review, whereas this is v2, I guess, although it doesn't say that it is v2, but it should say that.)

Quoted 38 lines
kernel/hung_task.c +14 -0
+ */
+unsigned int __read_mostly sysctl_hung_task_filter_kthread = CONFIG_DEFAULT_HUNG_TASK_FILTER_KTHREAD;
+
+/*
+ * Only one
+ */
+char __read_mostly sysctl_hung_task_check_comm[TASK_COMM_LEN] = CONFIG_DEFAULT_HUNG_TASK_CHECK_COMM;
+
 int __read_mostly sysctl_hung_task_warnings = 10;
 
 static int __read_mostly did_panic;
@@ -88,6 +99,12 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout)
 {
 	unsigned long switch_count = t->nvcsw + t->nivcsw;
 
+	if (unlikely(strlen(sysctl_hung_task_check_comm) && !strstr(t->comm, sysctl_hung_task_check_comm)))
+		return;
+
+	if (unlikely(sysctl_hung_task_filter_kthread && t->flags & PF_KTHREAD))
+		return;
+
 	/*
 	 * Ensure the task is not frozen.
 	 * Also, skip vfork and any other user process that freezer should skip.
kernel/sysctl.c +6 -0
index 62fbd09b5dc1..1daede87c88d 100644
@@ -2519,6 +2519,22 @@ static struct ctl_table kern_table[] = {
 		.proc_handler	= proc_dointvec_minmax,
 		.extra1		= &neg_one,
 	},
+	{
+		.procname	= "hung_task_filter_kthread",
+		.data		= &sysctl_hung_task_filter_kthread,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler   = proc_dointvec_minmax,

kernel/sysctl.c static struct ctl_table kern_table[] = { · line 2537

Quoted 10 lines
kernel/sysctl.c +10 -0
+		.extra1		= SYSCTL_ZERO,
+		.extra2		= SYSCTL_ONE,
+	},
+	{
+		.procname	= "hung_task_check_comm",
+		.data		= &sysctl_hung_task_check_comm,
+		.maxlen		= TASK_COMM_LEN,
+		.mode		= 0644,
+		.proc_handler	= proc_dostring,
+	},

(copy-paste from v1 review:)

These new sysctls should be documented in Documentation/admin-guide/sysctl/kernel.rst.

Quoted 8 lines
kernel/sysctl.c +0 -0
 #endif
 #ifdef CONFIG_RT_MUTEXES
 	{
lib/Kconfig.debug +0 -0
index 2c7f46b366f1..6eab8cf0c37f 100644

lib/Kconfig.debug config DEFAULT_HUNG_TASK_TIMEOUT · line 1110

Quoted 10 lines
lib/Kconfig.debug +7 -0
@@ -1101,6 +1101,30 @@ config DEFAULT_HUNG_TASK_TIMEOUT
 	  A timeout of 0 disables the check.  The default is two minutes.
 	  Keeping the default should be fine in most cases.
 
+config DEFAULT_HUNG_TASK_FILTER_KTHREAD
+	int "Default filter kthread for hung task"
+	depends on DETECT_HUNG_TASK
+	range 0 1
+	default 0
+	help
+	  This option controls filter kthread used to determine when

(again:)
uses

lib/Kconfig.debug config DEFAULT_HUNG_TASK_TIMEOUT · line 1111

Quoted 1 line
lib/Kconfig.debug +1 -0
+	  a kernel task has become "state=TASK_UNINTERRUPTIBLE" and should be skip.

(again:)
skipped.

lib/Kconfig.debug config DEFAULT_HUNG_TASK_TIMEOUT · line 1117

Quoted 6 lines
lib/Kconfig.debug +6 -0
+
+	  It can be adjusted at runtime via the kernel.hung_task_filter_kthread
+	  sysctl or by writing a value to
+	  /proc/sys/kernel/hung_task_filter_kthread.
+
+	  A filter of 1 disables the check

(again:)
check.

lib/Kconfig.debug config DEFAULT_HUNG_TASK_TIMEOUT · line 1127

Quoted 10 lines
lib/Kconfig.debug +10 -0
+
+config DEFAULT_HUNG_TASK_CHECK_COMM
+	string "Default check only one comm"
+	depends on DETECT_HUNG_TASK
+	default ""
+	help
+	  It can be adjusted at runtime via the kernel.hung_task_check_comm
+	  sysctl or by writing a value to
+	  /proc/sys/kernel/hung_task_check_comm.
+

(again:)
That help text doesn't tell how the Kconfig symbol is used.

Quoted 4 lines
lib/Kconfig.debug +0 -0
 config BOOTPARAM_HUNG_TASK_PANIC
 	bool "Panic (Reboot) On Hung Tasks"
 	depends on DETECT_HUNG_TASK
Signature · 1 line
~Randy