From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.139 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796051570; b=Cdqa1c+TPFL5e9qn+DsbQ0j8/Hs9yGI7AdlX/ACt6WDEkNV2UOeJGvIl3FbA8aqW6lIO L68hSSmwOwf5IFvwPOELFGWFhEAw9lxrUbwqS9r6LaC3TYTtF7z3v0SY3qnLSjtLIJada GH5djkwplee7mpTgGI+AvCVfHEu11hZhTCfM1/32N7ztFKmPTaxAOJLomT3QEFdHv0uLh C7YUANzmCNDGJ/3UI6Y/X86UoVykqAaKa5z4t7LvTlQYBi7neHwW5V4E51y167fbXyETh v4AKrs8M9rHrA/1RWHDm+WTNv8ITwLFgYjNkTOSJ/xOqTzXJrsXQegsOvOqP/1gXavg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796051570; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=6b7Jomgzev6ztqWpM1kY86NrmEbKYeCgSxcJLRKVHiY=; b=YH8SXmbd84aMCZ/5LDX6SkYwhUkcws9iIxhnAqQxVGSGawso5WJBTcM8EbtK4R6pYW1O OFbdW7gKxDkri91rL+mRnBFRYI1/Z0rlX12KNhLs4ulMcrMVFwNMZ7CvOfAXl9U09PZSp j5bIMAwN7Hv2wTrP2OA+y/S+NfkFFazQYS/z3jhVR9nYw+UHPXl4mJfVPHWazW/i3aRHS GZeNQC6XfVthkCAH+jnYSq2A8VkikYAWBDJMgS1k0d3UrcLPyD9ZsANNF7wSsAGeqj3ve Q0uj3Jt97aWR5Yd0UX8hjB/Vs0dE6FXpgsB3lazIxigvsaRCgKcKBvvRw0BtpO/axIA== 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=Uea9Oorb; arc=none smtp.remote-ip=95.215.58.139 Received: from mta1.migadu.com (out-139.mta1.migadu.com [95.215.58.139]) (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 31E83606A32A for ; Mon, 30 Nov 2026 15:12:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 31E83606A32A 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=Uea9Oorb X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=R4q7vklY/wuarUvz403InDmWCjbdvtO0WxwUPL6fkgM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796051568; v=1; x=1796656368; b=Uea9OorbxdeTGJ2TveEgTDuapX292SA3sVc/mihSGk4yb+fHRpRTN5PWwQ1ZWDf3g6EDXvLA 4ngBxgmBgzd1pbFljvjHmJT1yT7VeboDtcRXkSrcWEsGZjdf6AJ5afGsD4944Jves7p2rf3K9Dy zblRSRvgelubwkQciFGNLI6c= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id fb4375b3790a3ba2; Mon, 30 Nov 2026 15:12:48 +0000 X-Mizu-Trace-ID: fb4375b3790a3ba2 X-Migadu-Flow: FLOW_OUT From: Leon Hwang To: Andrew Morton , Lance Yang , Masami Hiramatsu , Petr Mladek Cc: 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, Leon Hwang Subject: [PATCH] hung_task: avoid duplicate stack dumps in sys_info() Date: Mon, 30 Nov 2026 23:11:56 +0800 Message-ID: <20261130151156.22234-1-leon.hwang@linux.dev> X-Mailer: git-send-email 2.55.0 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 Content-Transfer-Encoding: 8bit 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. 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 +++++ kernel/hung_task.c | 76 ++++++++++++++++++++++++++++++++++++--- kernel/sched/core.c | 4 +++ 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/include/linux/hung_task.h b/include/linux/hung_task.h index c4403eeb7144..d220c4c8d34e 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(const struct task_struct *p); +#else +static inline bool hung_task_check_reported(const struct task_struct *p) +{ + return false; +} +#endif + /* * @blocker: Combines lock address and blocking type. * diff --git a/kernel/hung_task.c b/kernel/hung_task.c index b8554aedc589..02ee3972f4cf 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -75,6 +76,7 @@ static int __read_mostly did_panic; static bool hung_task_call_panic; static struct task_struct *watchdog_task; +static DEFINE_XARRAY(reported_tasks); /* * A bitmask to control what kinds of system info to be printed when @@ -112,9 +114,8 @@ static struct notifier_block panic_block = { .notifier_call = hung_task_panic, }; -static bool task_is_hung(struct task_struct *t, unsigned long timeout) +static bool task_check_state(const struct task_struct *t) { - unsigned long switch_count = t->nvcsw + t->nivcsw; unsigned int state = READ_ONCE(t->__state); /* @@ -126,6 +127,16 @@ static bool task_is_hung(struct task_struct *t, unsigned long timeout) (state & (TASK_WAKEKILL | TASK_NOLOAD | TASK_FROZEN))) return false; + return true; +} + +static bool task_is_hung(struct task_struct *t, unsigned long timeout) +{ + unsigned long switch_count = t->nvcsw + t->nivcsw; + + if (!task_check_state(t)) + return false; + /* * When a freshly created task is scheduled once, changes its state to * TASK_UNINTERRUPTIBLE without having ever been switched out once, it @@ -145,6 +156,60 @@ static bool task_is_hung(struct task_struct *t, unsigned long timeout) return true; } +static bool task_likely_reported(const struct task_struct *t) +{ + unsigned long switch_count; + + if (!task_check_state(t)) + return false; + + switch_count = READ_ONCE(t->nvcsw) + READ_ONCE(t->nivcsw); + return switch_count && switch_count == t->last_switch_count; +} + +bool hung_task_check_reported(const struct task_struct *t) +{ + bool reported; + + if (current != watchdog_task || !in_task()) + return false; + + reported = xa_load(&reported_tasks, (unsigned long)t) && task_likely_reported(t); + if (reported) + pr_info("task %s:%d has been reported by hung task detector.\n", + t->comm, t->pid); + return reported; +} + +static bool sys_info_blocked_tasks(unsigned long mask) +{ + return mask & SYS_INFO_BLOCKED_TASKS; +} + +static void track_task(struct task_struct *t, unsigned long si_mask) +{ + if (!sys_info_blocked_tasks(si_mask)) + return; + + /* Allocation failure leaves this stack eligible for a second dump. */ + if (!xa_insert(&reported_tasks, (unsigned long)t, t, GFP_NOWAIT)) + get_task_struct(t); +} + +static void release_tracked_tasks(unsigned long si_mask) +{ + struct task_struct *t; + unsigned long index; + + if (!sys_info_blocked_tasks(si_mask)) + return; + + xa_for_each(&reported_tasks, index, t) + put_task_struct(t); + + xa_destroy(&reported_tasks); +} + #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER static void debug_show_blocker(struct task_struct *task, unsigned long timeout) { @@ -240,12 +305,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 +341,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); + track_task(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"); @@ -351,7 +418,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: @@ -375,6 +442,7 @@ static void check_hung_uninterruptible_tasks(unsigned long timeout) } sys_info(si_mask); + release_tracked_tasks(si_mask); if (hung_task_call_panic) panic("hung_task: blocked tasks"); 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