From nobody Thu Apr 2 11:01:50 2026 Received: from CWXP265CU010.outbound.protection.outlook.com (mail-ukwestazon11022119.outbound.protection.outlook.com [52.101.101.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12A03384233 for ; Tue, 3 Mar 2026 20:30:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.101.119 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772569850; cv=fail; b=WEkCpenejJGgtxZ7ypNpA8jr/sqDvu6XhYCXr5COPbsRabXsdBFHKV3kVqQ8onvUBDWzWQDdPSgk8+nEE0FzNqHaWuYK2XoAd6W9SxdwCAYc0pkMi48/5ind/+rHS/lj94sEUJEieCr4EPkdLy+/jToAxLOaD8Bse4X30JdZv5E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772569850; c=relaxed/simple; bh=J6jWHI/9+XgAadxJoni96kiJn65lwebFLHruP0fjwo8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=WRQtMPYKIYZIOIMxWLzYOhhz0/0FE5V2Phwc5WRJLI4XUwB7OQYLKdsk1jaJHCwqdWaTBvHLCabNdxaTNEnuTxRjVBtMxlbcsMDk7mfe1dL9HpTsug82Ff1Vqv1Ivep38AOYUXWOXM3zaQQzpxrnxNFNknR/bq4eAS2jGJ816Gc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atomlin.com; spf=pass smtp.mailfrom=atomlin.com; arc=fail smtp.client-ip=52.101.101.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atomlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atomlin.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DSrjbkZ8MXe3+4snTZZlJ5LpxlLJ9JezvoPVGgp/Av65MVYmjM+E3RFR1I/pCjNXwk/KUnf+LASqcv2ymUnS34SoU2JIHoSekDdF4ol0BN4nBT0GFfP7kFImT6lg8/mdzcR+E9Q9np9AcZtaTQXxN/ZMUVYD3eOVDHTLfOuQXRPJYDJhv9W9qxdd5754q8B472iCm5dV7WfiDNVTiSZwqLpuJImZGoLVioGNSqQLbI0K5PPClUCMPnCMvtr0dEscsOZAcOOTsixdgXQ2nWI3YvwFgdHoRQWLTxpws2fIYZP+baNh1UXno6MAn1Gfu7xD4BT1dRDmRr4WjhNxkDQPgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=X9xd7iDHC8KIxlnA2Bxo3wL1i+zbuVU6vNlxIDZuE08=; b=HdpqrYDjJDfpfGqbqunmJQsLeuiIRH9vI81sA4gUocx380I2eXWR7tUfK64wdY1vrqFw9Ct+IhLlojs5TUPKwHhHy+nYLQxL5GsWBWBV8HqODhLMj7mwOoz5M1pdtZ9UmZtccYv3v9sONZ+5LP91L3KQ3UpLuAOe71jr2YWgvikcQuWPqxAoh+oJQdyqofAO2sM5WXWZjJTUDfChE7clCfrzk+p8Gpg1uKlgZ+QM8utAplQRNgK78DDWBhwBAK92gGoDq+ytfp+O744IYsUjgAgKnMJC5LJZDoxBkLilP5L1N5zi63CTRqaGxzsevm5+EObLwf6kVoxhbj3bxkVxlw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=atomlin.com; dmarc=pass action=none header.from=atomlin.com; dkim=pass header.d=atomlin.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=atomlin.com; Received: from CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM (2603:10a6:400:70::10) by LO0P123MB6636.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:24e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9654.22; Tue, 3 Mar 2026 20:30:38 +0000 Received: from CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM ([fe80::de8e:2e4f:6c6:f3bf]) by CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM ([fe80::de8e:2e4f:6c6:f3bf%2]) with mapi id 15.20.9654.022; Tue, 3 Mar 2026 20:30:38 +0000 From: Aaron Tomlin To: akpm@linux-foundation.org, lance.yang@linux.dev, mhiramat@kernel.org, gregkh@linuxfoundation.org, pmladek@suse.com Cc: neelx@suse.com, sean@ashe.io, mproche@gmail.com, chjohnst@gmail.com, nick.lange@gmail.com, da.anzani@gmail.com, linux-kernel@vger.kernel.org Subject: [PATCH v9 1/3] hung_task: Refactor detection logic and atomicise detection count Date: Tue, 3 Mar 2026 15:30:29 -0500 Message-ID: <20260303203031.4097316-2-atomlin@atomlin.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260303203031.4097316-1-atomlin@atomlin.com> References: <20260303203031.4097316-1-atomlin@atomlin.com> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: BL1PR13CA0363.namprd13.prod.outlook.com (2603:10b6:208:2c0::8) To CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM (2603:10a6:400:70::10) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CWLP123MB3523:EE_|LO0P123MB6636:EE_ X-MS-Office365-Filtering-Correlation-Id: 06d8be1d-bfbe-4e35-e265-08de7963bb11 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|7416014|376014; X-Microsoft-Antispam-Message-Info: Z2eFmdF/ubx7byhpZintqfMaN/wm/RwLoX+RQwMwiNncf4c0b7PCHMfSCN0sVQrhgR5i4YUQoVj/XnRytDrXeJXp9VUSLsul10Lmu6DFhM0Lo1ZVLp8vmb9Xmz41anmLva1SzMs32nw4vFndRPpS6CLYVcJT5YAtP75W3CYPhd8HibXvQUP8efO0auAoLwG2tz65tZCY4FCM4HFKACdDVQ6cmhRugfBhKiQNARJ/Oac/uZhXCkt9G/1uv3X/2wXfyg/WhjRRUzpd9OcrVWbfpX3t5M0IkiLwJ5QY3E+IBip+S7PUzv2Q2W3THWM34zvb1bon5yZJTkCHrg2xvA3Fy4FZYJIXM13m3j/192cJozR+lgRjgj83lutgn5O+IHXdd+PmoHD6pwRscHtuawdscQBotE8zouU3k8lYyzzhlclNNVdkdmPq2g1NhqSgewsiYiAtNZV4KFCNSbuJPOQo9ETsxIhQgycSfp2gqRrE1iSzJHBoSBDVzTx4cOf6tIEBL2zzn6svSkYOCeq3LTsml43pvKbbFke9iK2ZGSBC/9ANk2U0ui3PZ4eqf/Y+YQjjHHjeFY8DtwUTefmkZDYDvHEVuep/VuXg4nAbzrYkgrU6fSSruCwqtUtJTe1mJz8Zo+FVVvcyc2pe4wSLw2wjlQ51gAd1By4kGBufou7a120n/tys8+jJ7cpp/UWme7rGODRMAGe+jAOT+shEYqEce9/9HKk6B7oL5eq9ZZ4uVFA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(7416014)(376014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ax1iF2jHBAqwPGKLX9fz1WsLyz2j32N0sWK0I4oqJi41ZrCjCj3cogaqRx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Exchange-CrossTenant-Network-Message-Id: 06d8be1d-bfbe-4e35-e265-08de7963bb11 X-MS-Exchange-CrossTenant-AuthSource: CWLP123MB3523.GBRP123.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Mar 2026 20:30:38.2003 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: e6a32402-7d7b-4830-9a2b-76945bbbcb57 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yppY3APKZcnY98lcCW7sK5opX85RZ++MsR6ImA6fzirxAaHubE2TDbnksHe4s3nXqq1MUoWTw032drL9CN7kwA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO0P123MB6636 Content-Type: text/plain; charset="utf-8" The check_hung_task() function currently conflates two distinct responsibilities: validating whether a task is hung and handling the subsequent reporting (printing warnings, triggering panics, or tracepoints). This patch refactors the logic by introducing hung_task_info(), a function dedicated solely to reporting. The actual detection check, task_is_hung(), is hoisted into the primary loop within check_hung_uninterruptible_tasks(). This separation clearly decouples the mechanism of detection from the policy of reporting. Furthermore, to facilitate future support for concurrent hung task detection, the global sysctl_hung_task_detect_count variable is converted from unsigned long to atomic_long_t. Consequently, the counting logic is updated to accumulate the number of hung tasks locally (this_round_count) during the iteration. The global counter is then updated atomically via atomic_long_cmpxchg_relaxed() once the loop concludes, rather than incrementally during the scan. These changes are strictly preparatory and introduce no functional change to the system's runtime behaviour. Reviewed-by: Masami Hiramatsu (Google) Reviewed-by: Petr Mladek Signed-off-by: Aaron Tomlin --- kernel/hung_task.c | 58 ++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/kernel/hung_task.c b/kernel/hung_task.c index d2254c91450b..df10830ed9ef 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -36,7 +36,7 @@ static int __read_mostly sysctl_hung_task_check_count =3D= 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 =3D ATOMIC_LONG_INIT(0); =20 /* * Limit number of tasks checked in a batch. @@ -223,31 +223,29 @@ static inline void debug_show_blocker(struct task_str= uct *task, unsigned long ti } #endif =20 -static void check_hung_task(struct task_struct *t, unsigned long timeout, - unsigned long prev_detect_count) +/** + * hung_task_info - Print diagnostic details for a hung task + * @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 + * + * 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 total_hung_task; - - if (!task_is_hung(t, timeout)) - return; - - /* - * This counter tracks the total number of tasks detected as hung - * since boot. - */ - sysctl_hung_task_detect_count++; - - total_hung_task =3D sysctl_hung_task_detect_count - prev_detect_count; trace_sched_process_hang(t); =20 - if (sysctl_hung_task_panic && total_hung_task >=3D sysctl_hung_task_panic= ) { + if (sysctl_hung_task_panic && this_round_count >=3D sysctl_hung_task_pani= c) { console_verbose(); hung_task_call_panic =3D true; } =20 /* - * Ok, the task did not get scheduled for more than 2 minutes, - * complain: + * The given task did not get scheduled for more than + * CONFIG_DEFAULT_HUNG_TASK_TIMEOUT. Therefore, complain + * accordingly */ if (sysctl_hung_task_warnings || hung_task_call_panic) { if (sysctl_hung_task_warnings > 0) @@ -297,18 +295,18 @@ static bool rcu_lock_break(struct task_struct *g, str= uct task_struct *t) =20 /* * Check whether a TASK_UNINTERRUPTIBLE does not get woken up for - * a really long time (120 seconds). If that happens, print out - * a warning. + * a really long time. If that happens, print out a warning. */ static void check_hung_uninterruptible_tasks(unsigned long timeout) { int max_count =3D sysctl_hung_task_check_count; unsigned long last_break =3D jiffies; struct task_struct *g, *t; - unsigned long prev_detect_count =3D sysctl_hung_task_detect_count; + unsigned long total_count, this_round_count; int need_warning =3D sysctl_hung_task_warnings; unsigned long si_mask =3D hung_task_si_mask; =20 + total_count =3D atomic_long_read(&sysctl_hung_task_detect_count); /* * If the system crashed already then all bets are off, * do not report extra hung tasks: @@ -316,10 +314,9 @@ static void check_hung_uninterruptible_tasks(unsigned = long timeout) if (test_taint(TAINT_DIE) || did_panic) return; =20 - + this_round_count =3D 0; rcu_read_lock(); for_each_process_thread(g, t) { - if (!max_count--) goto unlock; if (time_after(jiffies, last_break + HUNG_TASK_LOCK_BREAK)) { @@ -328,14 +325,25 @@ static void check_hung_uninterruptible_tasks(unsigned= long timeout) last_break =3D jiffies; } =20 - check_hung_task(t, timeout, prev_detect_count); + if (task_is_hung(t, timeout)) { + this_round_count++; + hung_task_info(t, timeout, this_round_count); + } } unlock: rcu_read_unlock(); =20 - if (!(sysctl_hung_task_detect_count - prev_detect_count)) + if (!this_round_count) return; =20 + /* + * This counter tracks the total number of tasks detected as hung + * since boot. + */ + atomic_long_cmpxchg_relaxed(&sysctl_hung_task_detect_count, + total_count, total_count + + this_round_count); + if (need_warning || hung_task_call_panic) { si_mask |=3D SYS_INFO_LOCKS; =20 --=20 2.51.0