hung_task: Deduplicate identical hang reports using explicit blocker tracking
Subsystems (4)
PATCH v5 1/1 hung_task: Deduplicate identical hang reports using explicit blocker tracking
Currently, during severe lock contention, multiple tasks can hang while waiting on the exact same resource. The khungtaskd kthread indiscriminately reports every single instance with a stack trace. This can roll the kernel ring buffer and prematurely exhaust the kernel.hung_task_warnings budget. Consequently, the kernel is left entirely blind to subsequent, unrelated deadlocks.
To preserve the warning budget and ring buffer without sacrificing observability, this patch introduces a two-tier deduplication mechanism:
1. Introduces a hung_task_reported in task_struct. If a task remains hung across multiple check intervals, khungtaskd suppresses redundant stack traces for that specific task until it makes progress (verified via context switch counters). Furthermore, it is packed into an existing compiler alignment hole, consuming zero additional memory
2. For tasks blocked on a lock, we leverage the CONFIG_DETECT_HUNG_TASK_BLOCKER infrastructure. By tracking the exact memory address of the blocker lock in a persistent array of size 32, we deduplicate warning reports for tasks waiting on the same resource across check intervals. If the blocker is already tracked, the warning is suppressed and the warning budget is preserved
3. For duplicate tasks, we still print the single-line "INFO: task ..." message and trigger tracepoint trace_sched_process_hang(). It merely skips calling sched_show_task() and debug_show_blocker(), printing a concise suppression notice instead
4. Once the hang resolves (i.e. no hung tasks are detected in a check round), the budget is restored to the value captured at the start of the hang, and the blocker array is cleared, accompanied by a recovery message in the ring buffer
---
Changes since v4:
- Replaced the stack-local hashmap implementation with a persistent array (Petr Mladek)
- Persistently track blocker addresses across scan intervals. Suppress warning reports and keep the sysctl_hung_task_warnings budget intact if the blocker is already tracked in the array (Petr Mladek)
- Reset the warnings budget and clear the blocker array when the hang resolves (Petr Mladek)
- Output a recovery message to the kernel ring buffer upon hang resolution
- Linked to v4: https://lore.kernel.org/lkml/[email protected]/
Changes since v3:
- Deduct from the global budget if printing a full stack trace
- Pivoted from heuristic Wait Channel hashing to deterministic blocker address hashing via CONFIG_DETECT_HUNG_TASK_BLOCKER
- Replaced the hung_task_reported bit-field with a standalone u8 byte. Move hung_task_reported into an existing structural alignment hole within task_struct following blocked_lock, resulting in zero overall memory footprint increase and optimal cacheline grouping
- Linked to v3: https://lore.kernel.org/lkml/[email protected]/
Changes since v2:
- Replaced the per-round cache flush with a task_struct bit-field for persistent cross-scan tracking, mitigating delayed budget exhaustion
- Abandoned exact-stack hashing in favour of Wait Channel hashing
- Transitioned from jhash() to hash_long() to optimise single-pointer hashing, and relocated the hash map to the local stack
- Linked to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Preserve "INFO:" headers for all hung tasks; suppress only the stack dumps for duplicates (Masami Hiramatsu)
- Print a clear notification when a trace is explicitly suppressed
- Add #ifdef CONFIG_STACKTRACE guards to prevent Kconfig build errors
- Optimise overhead by unwinding the stack only if a warning is actually going to be printed
- Linked to v1: https://lore.kernel.org/lkml/[email protected]/
--- include/linux/hung_task.h | 1 + include/linux/sched.h | 3 + kernel/fork.c | 1 + kernel/hung_task.c | 119 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 118 insertions(+), 6 deletions(-)
include/linux/hung_task.h +1 -0
@@ -37,6 +37,7 @@#define BLOCKER_TYPE_MASK 0x03UL +#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER static inline void hung_task_set_blocker (void *lock,unsigned long type) {
include/linux/sched.h +3 -0
@@ -1252,6 +1252,9 @@ struct task_struct {struct mutex *blocked_on;/* lock we're blocked on */ raw_spinlock_t blocked_lock; +#ifdef CONFIG_DETECT_HUNG_TASK +u8 hung_task_reported; +#endif /* * The task that is boosting this task; a back link for the current * donor stack. Set in schedule() -> find_proxy_task() and only stable
kernel/fork.c +1 -0
@@ -1569,6 +1569,7 @@ static int copy_mm(u64 clone_flags, struct task_struct *tsk)#ifdef CONFIG_DETECT_HUNG_TASK tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; tsk->last_switch_time =0 ; + tsk->hung_task_reported =0 ;#endif tsk->mm =NULL ;
kernel/hung_task.c +113 -6
185 lines not on this page · raw message
Signature · 1 line
2.54.0
Quoted 47 lines · Aaron Tomlin
Currently, during severe lock contention, multiple tasks can hang while waiting on the exact same resource. The khungtaskd kthread indiscriminately reports every single instance with a stack trace. This can roll the kernel ring buffer and prematurely exhaust the kernel.hung_task_warnings budget. Consequently, the kernel is left entirely blind to subsequent, unrelated deadlocks.
To preserve the warning budget and ring buffer without sacrificing observability, this patch introduces a two-tier deduplication mechanism:
1. Introduces a
hung_task_reportedintask_struct. If a task remains hung across multiple check intervals, khungtaskd suppresses redundant stack traces for that specific task until it makes progress (verified via context switch counters). Furthermore, it is packed into an existing compiler alignment hole, consuming zero additional memory2. For tasks blocked on a lock, we leverage the
CONFIG_DETECT_HUNG_TASK_BLOCKERinfrastructure. By tracking the exact memory address of the blocker lock in a persistent array of size 32, we deduplicate warning reports for tasks waiting on the same resource across check intervals. If the blocker is already tracked, the warning is suppressed and the warning budget is preserved3. For duplicate tasks, we still print the single-line "INFO: task ..." message and trigger tracepoint
trace_sched_process_hang(). It merely skips callingsched_show_task()anddebug_show_blocker(), printing a concise suppression notice instead4. Once the hang resolves (i.e. no hung tasks are detected in a check round), the budget is restored to the value captured at the start of the hang, and the blocker array is cleared, accompanied by a recovery message in the ring buffer
Signed-off-by Aaron Tomlin <[email protected]>---
Changes since v4:- Replaced the stack-local hashmap implementation with a persistent array (Petr Mladek)
- Persistently track blocker addresses across scan intervals. Suppress warning reports and keep the
sysctl_hung_task_warningsbudget intact if the blocker is already tracked in the array (Petr Mladek)
Quoted 7 lines
- Reset the warnings budget and clear the blocker array when the hang resolves (Petr Mladek)
- Output a recovery message to the kernel ring buffer upon hang resolution
- Linked to v4: https://lore.kernel.org/lkml/[email protected]/
Hi Petr, Lance, Masami,
Preserving the warning budget for duplicate tasks cause continuous log spam every sysctl_hung_task_timeout_secs. This was also reported by Sashiko [1]. The following now guards sys_info() execution using a new did_report local variable:
[1]: https://sashiko.dev/#/patchset/20260712202100.123934-1-atomlin%40atomlin.com
kernel/hung_task.c +9 -4
@@ -335,6 +335,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout)unsigned long si_mask = hung_task_si_mask;unsigned int skip_show_task;bool scan_completed = false; +bool did_report = false;#ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER unsigned long blocker;static int warnings_reset_value;
@@ -406,9 +407,13 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) }#endif -hung_task_info (t, timeout, this_round_count, - skip_show_task); - t->hung_task_reported =1 ; +if (!t->hung_task_reported) { +hung_task_info (t, timeout, this_round_count, + skip_show_task); + t->hung_task_reported =1 ; +if (!skip_show_task) + did_report = true; + } } } scan_completed = true;
@@ -450,7 +455,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) }#endif -if (need_warning || hung_task_call_panic) { +if ((need_warning && did_report) || hung_task_call_panic) { si_mask |=SYS_INFO_LOCKS ;if (sysctl_hung_task_all_cpu_backtrace)
Signature · 1 line
Aaron Tomlin
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Aaron,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on tip/sched/core linus/master kees/for-next/execve v7.2-rc6 next-20260804] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Aaron-Tomlin/hung_task-Deduplicate-identical-hang-reports-using-explicit-blocker-tracking/20260804-194519 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20260712202100.123934-1-atomlin%40atomlin.com patch subject: [PATCH v5] hung_task: Deduplicate identical hang reports using explicit blocker tracking config: openrisc-randconfig-r071-20260805 (https://download.01.org/0day-ci/archive/20260805/[email protected]/config) compiler: or1k-linux-gcc (GCC) 12.5.0 smatch: v0.5.0-9187-g5189e3fb reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260805/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
kernel/hung_task.c: In function 'check_hung_uninterruptible_tasks':
>> kernel/hung_task.c:337:14: warning: variable 'scan_completed' set but not used [-Wunused-but-set-variable]
337 | bool scan_completed = false;
| ^~~~~~~~~~~~~~vim +/scan_completed +337 kernel/hung_task.c
323
324 /*
325 * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for
326 * a really long time. If that happens, print out a warning.
327 */
328 static void check_hung_uninterruptible_tasks(unsigned long timeout)
329 {
330 int max_count = sysctl_hung_task_check_count;
331 unsigned long last_break = jiffies;
332 struct task_struct *g, *t;
333 unsigned long this_round_count;
334 int need_warning = sysctl_hung_task_warnings;
335 unsigned long si_mask = hung_task_si_mask;
336 unsigned int skip_show_task;
> 337 bool scan_completed = false;
338 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER
339 unsigned long blocker;
340 static int warnings_reset_value;
341 unsigned long seen_blockers_mask = 0;
342 #endif
343
344 /*
345 * If the system crashed already then all bets are off,
346 * do not report extra hung tasks:
347 */
348 if (test_taint(TAINT_DIE) || did_panic)
349 return;
350
351 this_round_count = 0;
352 rcu_read_lock();
353 for_each_process_thread(g, t) {
354 if (!max_count--)
355 goto unlock;
356 if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) {
357 if (!rcu_lock_break(g, t))
358 goto unlock;
359 last_break = jiffies;
360 }
361
362 if (task_is_hung(t, timeout)) {
363 skip_show_task = t->hung_task_reported;
364 /*
365 * Increment the global counter so that userspace could
366 * start migrating tasks ASAP. But count the current
367 * round separately because userspace could reset
368 * the global counter at any time.
369 */
370 atomic_long_inc(&sysctl_hung_task_detect_count);
371 this_round_count++;
372
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki