Skip to contentSkip to conversation

hung_task: Provide runtime reset interface for hung task detector

PATCH Superseded Aaron Tomlin 2 participants 5 messages Dec 22, 2025 – Dec 23, 2025

Subsystems (2)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover hung_task: Provide runtime reset interface for hung task detector

Hi Lance, Greg, Petr, Joel,

This series introduces the ability to reset
/proc/sys/kernel/hung_task_detect_count.

Writing a zero value to this file atomically resets the counter of detected hung tasks. This functionality provides system administrators with the means to clear the cumulative diagnostic history following incident resolution, thereby simplifying subsequent monitoring without necessitating a system restart.

The implementation uses atomic acquire/release semantics to ensure that diagnostic metadata published by one CPU is correctly observed by the monitoring thread on another CPU.

Please let me know your thoughts.

Changes since v3 [1]:
- Use atomic operations to ensure cross-CPU visibility and prevent an integer underflow
- Use acquire/release semantics for memory ordering (Petr Mladek)
- Move quoted string to a single line (Petr Mladek)
- Remove variables coredump_msg and disable_msg to simplify code (Petr Mladek)
- Add trailing "\n" to all strings to ensure immediate console flushing (Petr Mladek)
- Improve the hung task counter documentation (Joel Granados)
- Reject non-zero writes with -EINVAL (Joel Granados)
- Translate to the new sysctl API (Petr Mladek)

Changes since v2 [2]:
- Avoided a needless double update to hung_task_detect_count (Lance Yang)
- Restored previous use of pr_err() for each message (Greg KH)
- Provided a complete descriptive comment for the helper

Changes since v1 [3]:

- Removed write-only sysfs attribute (Lance Yang)
- Modified procfs hung_task_detect_count instead (Lance Yang)
- Introduced a custom proc_handler
- Updated documentation (Lance Yang)
- Added 'static inline' as a hint to eliminate any function call overhead
- Removed clutter through encapsulation

[1]: https://lore.kernel.org/all/[email protected]/
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/

Aaron Tomlin (2):
hung_task: Introduce helper for hung task warning
hung_task: Enable runtime reset of hung_task_detect_count

 Documentation/admin-guide/sysctl/kernel.rst |   3 +-
 kernel/hung_task.c                          | 109 ++++++++++++++++----
 2 files changed, 90 insertions(+), 22 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v4 1/2 hung_task: Introduce helper for hung task warning

Consolidate the multi-line console output block for reporting a hung task into a new helper function, hung_task_diagnostics(). This improves readability in the main check_hung_task() loop and makes the diagnostic output structure easier to maintain and update in the future.

---
 kernel/hung_task.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
kernel/hung_task.c +23 -10
index d2254c91450b..00c3296fd692 100644
@@ -223,6 +223,28 @@ static inline void debug_show_blocker(struct task_struct *task, unsigned long ti
 }
 #endif
 
+/**
+ * hung_task_diagnostics - Print structured diagnostic info for a hung task.
+ * @t: Pointer to the detected hung task.
+ *
+ * This function consolidates the printing of core diagnostic information
+ * for a task found to be blocked.
+ */
+static inline void hung_task_diagnostics(struct task_struct *t)
+{
+	unsigned long blocked_secs = (jiffies - t->last_switch_time) / HZ;
+
+	pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
+	       t->comm, t->pid, blocked_secs);
+	pr_err("      %s %s %.*s\n",
+	       print_tainted(), init_utsname()->release,
+	       (int)strcspn(init_utsname()->version, " "),
+	       init_utsname()->version);
+	if (t->flags & PF_POSTCOREDUMP)
+		pr_err("      Blocked by coredump.\n");
+	pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\" disables this message.\n");
+}
+
 static void check_hung_task(struct task_struct *t, unsigned long timeout,
 		unsigned long prev_detect_count)
 {
@@ -252,16 +274,7 @@ static void check_hung_task(struct task_struct *t, unsigned long timeout,
 	if (sysctl_hung_task_warnings || hung_task_call_panic) {
 		if (sysctl_hung_task_warnings > 0)
 			sysctl_hung_task_warnings--;
-		pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n",
-		       t->comm, t->pid, (jiffies - t->last_switch_time) / HZ);
-		pr_err("      %s %s %.*s\n",
-			print_tainted(), init_utsname()->release,
-			(int)strcspn(init_utsname()->version, " "),
-			init_utsname()->version);
-		if (t->flags & PF_POSTCOREDUMP)
-			pr_err("      Blocked by coredump.\n");
-		pr_err("\"echo 0 > /proc/sys/kernel/hung_task_timeout_secs\""
-			" disables this message.\n");
+		hung_task_diagnostics(t);
 		sched_show_task(t);
 		debug_show_blocker(t, timeout);
 
Signature · 1 line
2.51.0
#2 lore raw

PATCH v4 2/2 hung_task: Enable runtime reset of hung_task_detect_count

In reply to Aaron Tomlin

Introduce support for writing to /proc/sys/kernel/hung_task_detect_count.

Writing a value of zero to this file atomically resets the counter of detected hung tasks. This grants system administrators the ability to clear the cumulative diagnostic history after resolving an incident, simplifying monitoring without requiring a system restart.

---
 Documentation/admin-guide/sysctl/kernel.rst |  3 +-
 kernel/hung_task.c                          | 76 ++++++++++++++++++---
 2 files changed, 67 insertions(+), 12 deletions(-)
Documentation/admin-guide/sysctl/kernel.rst +2 -1
index 239da22c4e28..68da4235225a 100644
@@ -418,7 +418,8 @@ hung_task_detect_count
 ======================
 
 Indicates the total number of tasks that have been detected as hung since
-the system boot.
+the system boot or since the counter was reset. The counter is zeroed when
+a value of 0 is written.
 
 This file shows up if ``CONFIG_DETECT_HUNG_TASK`` is enabled.
 
kernel/hung_task.c +65 -11

128 lines not on this page · raw message

Signature · 1 line
2.51.0
maintainer
#3 lore raw

Re: [v4 PATCH 2/2] hung_task: Enable runtime reset of hung_task_detect_count

Quoted 91 lines · Aaron Tomlin

Introduce support for writing to /proc/sys/kernel/hung_task_detect_count.

Writing a value of zero to this file atomically resets the counter of detected hung tasks. This grants system administrators the ability to clear the cumulative diagnostic history after resolving an incident, simplifying monitoring without requiring a system restart.

---
  Documentation/admin-guide/sysctl/kernel.rst |  3 +-
  kernel/hung_task.c                          | 76 ++++++++++++++++++---
  2 files changed, 67 insertions(+), 12 deletions(-)
Documentation/admin-guide/sysctl/kernel.rst +2 -1
index 239da22c4e28..68da4235225a 100644
@@ -418,7 +418,8 @@ hung_task_detect_count
  ======================
 
  Indicates the total number of tasks that have been detected as hung since
-the system boot.
+the system boot or since the counter was reset. The counter is zeroed when
+a value of 0 is written.
 
  This file shows up if ``CONFIG_DETECT_HUNG_TASK`` is enabled.
 
kernel/hung_task.c +15 -7
index 00c3296fd692..70b3db047f5d 100644
@@ -17,6 +17,7 @@
  #include <linux/export.h>
  #include <linux/panic_notifier.h>
  #include <linux/sysctl.h>
+#include <linux/atomic.h>
  #include <linux/suspend.h>
  #include <linux/utsname.h>
  #include <linux/sched/signal.h>
@@ -36,7 +37,7 @@ static int __read_mostly sysctl_hung_task_check_count = PID_MAX_LIMIT;
  /*
   * Total number of tasks detected as hung since boot:
   */
-static unsigned long __read_mostly sysctl_hung_task_detect_count;
+static atomic_long_t sysctl_hung_task_detect_count = ATOMIC_LONG_INIT(0);
 
  /*
   * Limit number of tasks checked in a batch.
@@ -246,20 +247,26 @@ static inline void hung_task_diagnostics(struct task_struct *t)
  }
 
  static void check_hung_task(struct task_struct *t, unsigned long timeout,
-		unsigned long prev_detect_count)
+			    unsigned long prev_detect_count)
  {
-	unsigned long total_hung_task;
+	unsigned long total_hung_task, current_detect;
 
  	if (!task_is_hung(t, timeout))
  		return;
 
  	/*
  	 * This counter tracks the total number of tasks detected as hung
-	 * since boot.
+	 * since boot. If a reset occurred during the scan, we treat the
+	 * current count as the new delta to avoid an underflow error.
+	 * Ensure hang details are globally visible before the counter
+	 * update.
  	 */
-	sysctl_hung_task_detect_count++;
+	current_detect = atomic_long_inc_return_acquire(&sysctl_hung_task_detect_count);
+	if (current_detect >= prev_detect_count)
+		total_hung_task = current_detect - prev_detect_count;
+	else
+		total_hung_task = current_detect;
 
-	total_hung_task = sysctl_hung_task_detect_count - prev_detect_count;
  	trace_sched_process_hang(t);
 
  	if (sysctl_hung_task_panic && total_hung_task >= sysctl_hung_task_panic) {
@@ -318,7 +325,8 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
  	int max_count = sysctl_hung_task_check_count;
  	unsigned long last_break = jiffies;
  	struct task_struct *g, *t;
-	unsigned long prev_detect_count = sysctl_hung_task_detect_count;
+	/* Acquire prevents reordering task checks before this point. */
+	unsigned long prev_detect_count = atomic_long_read_acquire(&sysctl_hung_task_detect_count);
  	int need_warning = sysctl_hung_task_warnings;
  	unsigned long si_mask = hung_task_si_mask;
 

kernel/hung_task.c static void check_hung_uninterruptible_tasks(unsigned long timeout) · line 360

Quoted 8 lines
kernel/hung_task.c +3 -1
@@ -346,7 +354,9 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)
   unlock:
  	rcu_read_unlock();
 
-	if (!(sysctl_hung_task_detect_count - prev_detect_count))
+	/* Ensures we see all hang details recorded during the scan. */
+	if (!(atomic_long_read_acquire(&sysctl_hung_task_detect_count) -
+				       prev_detect_count))
  		return;

Hmm, I think we're missing the same underflow check here ...

If reset happens mid-scan, it can also underflow and cause false positives in the diagnostics :)

we should apply the same "if (current < prev) use current" logic here as Petr metioned before.

[...]

Cheers,
Lance

#4 lore raw

Re: [v4 PATCH 2/2] hung_task: Enable runtime reset of hung_task_detect_count

Quoted 7 lines · Lance Yang

Hmm, I think we're missing the same underflow check here ...

If reset happens mid-scan, it can also underflow and cause false positives in the diagnostics :)

we should apply the same "if (current < prev) use current" logic here as Petr metioned before.

Hi Lance,

You are quite right; that was an oversight on my part. The underflow check was indeed omitted in error.

Kind regards,

Signature · 1 line
Aaron Tomlin