From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-188.mta1.migadu.com (out-188.mta1.migadu.com [95.215.58.188]) (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 D55C51DA62E for ; Mon, 22 Dec 2025 02:21:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766370109; cv=none; b=TlGIh4Fwr7Dey6tDFm5nIsG3sK5EnyNPrI22fGPGvBzhdq5zlo+ckrGFk8hmf/1EpbqO9OyeMMhbPBeCZZnFNeomWPAYNCaSq7WPTLSiSAgm7MXhabkQEyYKVLIn+Ajj+wGwyENigVLAB+4Kwd5IRFswBDEmUAkoTel6MchWZcU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766370109; c=relaxed/simple; bh=LQhLHmG0vRTb0RQJ1sKppQa0PxIJzydUnT5jTUp9DaE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=p7Izh2x4OJX+AdV1BEi1xjkucacR6LvcS6L4qWHcD0AzLLOw1SKFdn2IXOwgzvNkhfUHp8AQI4y+gFahd9DzG4n5upK0h+aUoY27QZj44wCfoO+m5F0NZHIyUesuOgqllxO+EdN+unSyiVSp9EBJArEHOPaRiwJlEz2asyzED3k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=KgSe5rJR; arc=none smtp.client-ip=95.215.58.188 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) header.d=linux.dev header.i=@linux.dev header.b="KgSe5rJR" Message-ID: <77749a90-9209-4495-8995-9e4535483e07@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1766370104; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=77KBH99ImGgaZmZJegup8+djz2ZCIuOuziTuT5Ewrlo=; b=KgSe5rJRroOgnvdRZrnqN/Y6Jn3dh61L/sQx2UzP5UlBwN1biWnD6jvg1xKmxMQOKnhs/l JsdeUvJ0yHOjL47reXS8HT3yo8JbiFvdhGZndeH8EoQu9WgQMeeQ93lXYRGRuhvkFkJHxa tIR8GIB5KvuFtLyl1bfA3t8rzNVzvBQ= Date: Mon, 22 Dec 2025 10:21:31 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [v4 PATCH 2/2] hung_task: Enable runtime reset of hung_task_detect_count To: Aaron Tomlin Cc: sean@ashe.io, linux-kernel@vger.kernel.org, pmladek@suse.com, joel.granados@kernel.org, gregkh@linuxfoundation.org, akpm@linux-foundation.org, mhiramat@kernel.org References: <20251222014210.2032214-1-atomlin@atomlin.com> <20251222014210.2032214-3-atomlin@atomlin.com> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: <20251222014210.2032214-3-atomlin@atomlin.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2025/12/22 09:42, Aaron Tomlin wrote: > 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. > > Signed-off-by: Aaron Tomlin > --- > Documentation/admin-guide/sysctl/kernel.rst | 3 +- > kernel/hung_task.c | 76 ++++++++++++++++++--- > 2 files changed, 67 insertions(+), 12 deletions(-) > > diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/admin-guide/sysctl/kernel.rst > index 239da22c4e28..68da4235225a 100644 > --- a/Documentation/admin-guide/sysctl/kernel.rst > +++ b/Documentation/admin-guide/sysctl/kernel.rst > @@ -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. > > diff --git a/kernel/hung_task.c b/kernel/hung_task.c > index 00c3296fd692..70b3db047f5d 100644 > --- a/kernel/hung_task.c > +++ b/kernel/hung_task.c > @@ -17,6 +17,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -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; > > @@ -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