From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.79 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796143960; b=RBL3gT/E4wPuzwh5hOZmaqsfdb2GJ3yH6IpqBY/vzet86g3BwzOj4JWOY8at8v+RyjSx TZkg6Q5QubLTiJ6woET05kfVZG0sGCVudNTVxP7kBQFH6vXVIZh8nS+yKOx8i1BtH6X3M sWyfinqyZMQ3LktXRr7c0KVaiRaf3M9sAXg2TAm9bT73xWuCrUSn/uETgS6YbYuxqKQGG SCXI4W8ZwzUPy+GAyqofDdCaznvvxy43oAPdM3VZzYRPU8Aj4k/MGLWi2JAc6et0zS4LJ N6mpf9eO0FNAfR9yaeHf38srAjqsJaJQ+88shVysv4nlDyGlk6FLPuUZAxGYHFsLmAg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796143960; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=50G/wgyZ40KGVwm2vNb19nR+wdkWA8quwSIf500YuoA=; b=utaxqpRRex03PiRHxoU3Glzj3qhF8ez4FibqZPsypDH7mMdBpvwTKYEDuSq9xCEOILQF Hyh7wkU8OFlHKsJ05Hne1oTZzQCh28FFDSLTG7eOEVu+d8a7PBZ/9s9Yd0ruENBtot51R 4IS7KvBVOYqRLv3rEg33DntKpV06t1R3v5cYPE0QShGwKe35XCXEhxlNzkE14SB3avxJu TvwEbc5Exh1iUyhwhFZ97jZRFfz88oh239o0vCTrjwz4Dti5fFbE6Esjd6mlGQ2e7GSxP LmwnbHHjmXpcITdHYJD/bofAKCSGWABUAAq92Q037TKjttE+mSmFb17JqiJR8TnFtIg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=JlR8J+AK; arc=none smtp.remote-ip=95.215.58.79 Received: from mta1.migadu.com (out-79.mta1.migadu.com [95.215.58.79]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0615606A31A for ; Tue, 1 Dec 2026 16:52:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B0615606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=JlR8J+AK X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=EEQQ7x1WvR7H8dXgZCRiNncAvnkj99o0/zJKUoMvjfk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796143956; v=1; x=1796748756; b=JlR8J+AKu4Gjvmpv+hOkGZES1bWGAH0kWGru0GVixAsdOkgcLcB1UJfcVEBk5AMYV9N9wkVg Siqufxc+z4bWz+UHrj+vd+F8VTSSO3tGQBG4hBr8STYienv9KES2oPHRXCWoDRKKh69FbB988Z4 oKprWAsnhEMkqAAm8fAWHUbg= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id cdcc2ef3f9dcbd69; Tue, 01 Dec 2026 16:52:36 +0000 X-Mizu-Trace-ID: cdcc2ef3f9dcbd69 X-Migadu-Flow: FLOW_OUT Message-ID: <0388d8ee-3c7a-4638-a898-19a8c81936d6@linux.dev> Date: Wed, 2 Dec 2026 00:52:29 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] hung_task: avoid duplicate stack dumps in sys_info() To: Andrew Morton Cc: Lance Yang , Masami Hiramatsu , Petr Mladek , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , Feng Tang , linux-kernel@vger.kernel.org References: <20261130151156.22234-1-leon.hwang@linux.dev> <20261130165041.323f372cd6435bcab0f13c0f@linux-foundation.org> Content-Language: en-US From: Leon Hwang In-Reply-To: <20261130165041.323f372cd6435bcab0f13c0f@linux-foundation.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 2026/12/1 08:50, Andrew Morton wrote: > On Mon, 30 Nov 2026 23:11:56 +0800 Leon Hwang wrote: > >> When hung_task_sys_info includes blocked_tasks, khungtaskd dumps each >> reported hung task's stack, then calls sys_info() to dump D-state tasks. >> A task that stays blocked gets its stack printed twice in the same scan. >> >> Track the tasks khungtaskd reports during the scan. If a recorded task >> still appears blocked at the same switch count, print a short message >> in place of the second stack dump. >> >> With this change, the second dump would be replaced with a log. >> >> [ 20.143549] INFO: task cat:607 blocked for more than 5 seconds. >> [ 20.143560] Not tainted 7.4.0-rc4-gfb22a6547753-dirty #12 >> [ 20.143564] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. >> [ 20.143567] task:cat state:D stack:0 pid:607 tgid:607 ppid:603 task_flags:0x400000 flags:0x00080000 >> >> ... >> >> [ 20.144250] task cat:607 has been reported by hung task detector. > > OK. This is a pretty minor bug! And userspace can filter out the > duplicates easily enough (I assume). > >> --- >> include/linux/hung_task.h | 9 +++++ >> kernel/hung_task.c | 76 ++++++++++++++++++++++++++++++++++++--- >> kernel/sched/core.c | 4 +++ >> 3 files changed, 85 insertions(+), 4 deletions(-) > > And that's a lot of churn. Agreed. A minor bug should be fixed with less churn. > > > > task_struct.atomic_flags has plenty of unused bits and using one of > them for this purpose seems appropriate. > > Can we set one of those bits for deduplication and later test-n-clear > it in > > check_hung_uninterruptible_tasks() > ->sys_info(si_mask) > ->show_state_filter(TASK_UNINTERRUPTIBLE)? > > Or something along these lines? Adding PFA_HUNG_TASK_REPORTED to task_struct.atomic_flags works. See below patch. Thanks, Leon --- >From 2057cc9c6193f5f06806f5f94ac2557bb9858152 Mon Sep 17 00:00:00 2001 From: Leon Hwang Date: Mon, 30 Nov 2026 13:31:55 +0800 Subject: [PATCH v2] hung_task: avoid duplicate stack dumps in sys_info() When hung_task_sys_info includes blocked_tasks, khungtaskd dumps each reported hung task's stack, then calls sys_info() to dump D-state tasks. A task that stays blocked gets its stack printed twice in the same scan. Mark the tasks khungtaskd reports as reported during the scan. If a reported task still appears blocked at the same switch count, print a short message in place of the second stack dump. With this change, the second dump would be replaced with a log. [ 20.143549] INFO: task cat:607 blocked for more than 5 seconds. [ 20.143560] Not tainted 7.4.0-rc4-gfb22a6547753-dirty #12 [ 20.143564] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 20.143567] task:cat state:D stack:0 pid:607 tgid:607 ppid:603 task_flags:0x400000 flags:0x00080000 ... [ 20.144250] task cat:607 has been reported by hung task detector. Fixes: 8b2b9b4f6f4f ("hung_task: add hung_task_sys_info sysctl to dump sys info on task-hung") Signed-off-by: Leon Hwang --- include/linux/hung_task.h | 9 +++++++++ include/linux/sched.h | 5 +++++ kernel/fork.c | 1 + kernel/hung_task.c | 38 ++++++++++++++++++++++++++++++++++++-- kernel/sched/core.c | 4 ++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/include/linux/hung_task.h b/include/linux/hung_task.h index c4403eeb7144..d273a8c6b0a0 100644 --- a/include/linux/hung_task.h +++ b/include/linux/hung_task.h @@ -12,6 +12,15 @@ #include #include +#ifdef CONFIG_DETECT_HUNG_TASK +bool hung_task_check_reported(struct task_struct *p); +#else +static inline bool hung_task_check_reported(struct task_struct *p) +{ + return false; +} +#endif + /* * @blocker: Combines lock address and blocking type. * diff --git a/include/linux/sched.h b/include/linux/sched.h index e4f6323d7855..cc1ddb3ce5df 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1882,6 +1882,7 @@ static __always_inline bool is_user_task(struct task_struct *task) #define PFA_SPEC_IB_DISABLE 5 /* Indirect branch speculation restricted */ #define PFA_SPEC_IB_FORCE_DISABLE 6 /* Indirect branch speculation permanently restricted */ #define PFA_SPEC_SSB_NOEXEC 7 /* Speculative Store Bypass clear on execve() */ +#define PFA_HUNG_TASK_REPORTED 8 /* The task has been reported by khungtaskd */ #define TASK_PFA_TEST(name, func) \ static inline bool task_##func(struct task_struct *p) \ @@ -1920,6 +1921,10 @@ TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable) TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) +TASK_PFA_TEST(HUNG_TASK_REPORTED, hung_task_reported) +TASK_PFA_SET(HUNG_TASK_REPORTED, hung_task_reported) +TASK_PFA_CLEAR(HUNG_TASK_REPORTED, hung_task_reported) + static inline void current_restore_flags(unsigned long orig_flags, unsigned long flags) { diff --git a/kernel/fork.c b/kernel/fork.c index d55a7d21a8fb..78f025a07c79 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1581,6 +1581,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; + task_clear_hung_task_reported(tsk); #endif tsk->mm = NULL; diff --git a/kernel/hung_task.c b/kernel/hung_task.c index b8554aedc589..92f33808b950 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -145,6 +145,37 @@ static bool task_is_hung(struct task_struct *t, unsigned long timeout) return true; } +static bool task_clear_reported(struct task_struct *t) +{ + bool reported = task_hung_task_reported(t); + + if (reported) + task_clear_hung_task_reported(t); + return reported; +} + +bool hung_task_check_reported(struct task_struct *t) +{ + if (current != watchdog_task || !in_task()) + return false; + + if (!task_clear_reported(t)) + return false; + + if (READ_ONCE(t->nvcsw) + READ_ONCE(t->nivcsw) == t->last_switch_count) { + pr_info("task %s:%d has been reported by hung task detector.\n", + t->comm, t->pid); + return true; + } + return false; +} + +static void task_set_reported(struct task_struct *t, unsigned long si_mask) +{ + if (si_mask & SYS_INFO_BLOCKED_TASKS) + task_set_hung_task_reported(t); +} + #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER static void debug_show_blocker(struct task_struct *task, unsigned long timeout) { @@ -240,12 +271,13 @@ static inline void debug_show_blocker(struct task_struct *task, unsigned long ti * @t: Pointer to the detected hung task. * @timeout: Timeout threshold for detecting hung tasks * @this_round_count: Count of hung tasks detected in the current iteration + * @si_mask: Sys info mask to track reported tasks * * Print structured information about the specified hung task, if warnings * are enabled or if the panic batch threshold is exceeded. */ static void hung_task_info(struct task_struct *t, unsigned long timeout, - unsigned long this_round_count) + unsigned long this_round_count, unsigned long si_mask) { trace_sched_process_hang(t); @@ -275,6 +307,7 @@ static void hung_task_info(struct task_struct *t, unsigned long timeout, " disables this message.\n"); sched_show_task(t); debug_show_blocker(t, timeout); + task_set_reported(t, si_mask); if (!hung_task_warnings_budget && !hung_task_call_panic) pr_info("hung_task: further per-task details suppressed until warning budget is reset (see sysctl kernel.hung_task_warnings)\n"); @@ -342,6 +375,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) last_break = jiffies; } + task_clear_reported(t); if (task_is_hung(t, timeout)) { /* * Increment the global counter so that userspace could @@ -351,7 +385,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) */ atomic_long_inc(&sysctl_hung_task_detect_count); this_round_count++; - hung_task_info(t, timeout, this_round_count); + hung_task_info(t, timeout, this_round_count, si_mask); } } unlock: diff --git a/kernel/sched/core.c b/kernel/sched/core.c index ec75450c49a8..e54a453fcd42 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -68,6 +68,7 @@ #include #include #include +#include #ifdef CONFIG_PREEMPT_DYNAMIC # ifdef CONFIG_GENERIC_IRQ_ENTRY @@ -8205,6 +8206,9 @@ state_filter_match(unsigned long state_filter, struct task_struct *p) if (state_filter == TASK_UNINTERRUPTIBLE && (state & TASK_NOLOAD)) return false; + if (state_filter == TASK_UNINTERRUPTIBLE && hung_task_check_reported(p)) + return false; + return true; } -- 2.55.0