* [PATCH] watchdog/softlockup: Fix CPU utilization across counter wraparound
@ 2026-12-03 6:51 Avi Weiss
2026-12-03 17:04 ` Bradley Morgan
0 siblings, 1 reply; 2+ messages in thread
From: Avi Weiss @ 2026-12-03 6:51 UTC (permalink / raw)
To: akpm, pmladek, dianders
Cc: tglx, yaoma, yaozhenguo1, mrungta, liusong, linux-kernel
update_cpustat() keeps the low 16 bits of each quantized CPU time
counter. Subtracting two u16 values promotes them to int, so a counter
that wraps gives a negative delta.
Store the difference in a u16 to recover the modular delta before
scaling it. Keep the percentage in an unsigned int until after applying
the 100% cap.
Fixes: d7037381d002 ("watchdog/softlockup: Low-overhead detection of interrupt storm")
Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
---
kernel/watchdog.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/kernel/watchdog.c b/kernel/watchdog.c
index 64da2e500144..b75564260562 100644
--- a/kernel/watchdog.c
+++ b/kernel/watchdog.c
@@ -489,8 +489,8 @@ static u16 get_16bit_precision(u64 data_ns)
static void update_cpustat(void)
{
int i;
- u8 util;
- u16 old_stat, new_stat;
+ unsigned int util;
+ u16 old_stat, new_stat, delta;
struct kernel_cpustat kcpustat;
u64 *cpustat = kcpustat.cpustat;
u8 tail = __this_cpu_read(cpustat_tail);
@@ -501,7 +501,9 @@ static void update_cpustat(void)
for (i = 0; i < NUM_STATS_PER_GROUP; i++) {
old_stat = __this_cpu_read(cpustat_old[i]);
new_stat = get_16bit_precision(cpustat[tracked_stats[i]]);
- util = DIV_ROUND_UP(100 * (new_stat - old_stat), sample_period_16);
+ /* Take the delta modulo 2^16 before scaling it. */
+ delta = new_stat - old_stat;
+ util = DIV_ROUND_UP(100U * delta, sample_period_16);
/*
* Since we use 16-bit precision, the raw data will undergo
* integer division, which may sometimes result in data loss,
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] watchdog/softlockup: Fix CPU utilization across counter wraparound
2026-12-03 6:51 [PATCH] watchdog/softlockup: Fix CPU utilization across counter wraparound Avi Weiss
@ 2026-12-03 17:04 ` Bradley Morgan
0 siblings, 0 replies; 2+ messages in thread
From: Bradley Morgan @ 2026-12-03 17:04 UTC (permalink / raw)
To: thnkslprpt
Cc: akpm, dianders, linux-kernel, liusong, mrungta, pmladek, tglx,
yaoma, yaozhenguo1
On 3 December 2026 06:51:13 GMT, Avi Weiss <thnkslprpt@gmail.com> wrote:
>update_cpustat() keeps the low 16 bits of each quantized CPU time
>counter. Subtracting two u16 values promotes them to int, so a counter
>that wraps gives a negative delta.
>
>Store the difference in a u16 to recover the modular delta before
>scaling it. Keep the percentage in an unsigned int until after applying
>the 100% cap.
ehh?
The negative result goes into a u8, so the softlockup report prints a crap
percentage. With the default 4s sample,
old=65500 and new=100 should report 58%, but the current code turns the
negative quotient into 171 and then caps it to 100%. The cap only sometimes
catches the conversion, so the wrong value can look "ok".
please add to changelog
For one sample after the counter wraps, the report prints a bogus
utilization, e.g. 100% instead of 58%.
btw, if you want stable.
Cc: stable@vger.kernel.org # v6.18+
>
>Fixes: d7037381d002 ("watchdog/softlockup: Low-overhead detection of interrupt storm")
>Signed-off-by: Avi Weiss <thnkslprpt@gmail.com>
>---
> kernel/watchdog.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
>diff --git a/kernel/watchdog.c b/kernel/watchdog.c
>index 64da2e500144..b75564260562 100644
>--- a/kernel/watchdog.c
>+++ b/kernel/watchdog.c
>@@ -489,8 +489,8 @@ static u16 get_16bit_precision(u64 data_ns)
> static void update_cpustat(void)
> {
> int i;
>- u8 util;
>- u16 old_stat, new_stat;
>+ unsigned int util;
>+ u16 old_stat, new_stat, delta;
> struct kernel_cpustat kcpustat;
> u64 *cpustat = kcpustat.cpustat;
> u8 tail = __this_cpu_read(cpustat_tail);
>@@ -501,7 +501,9 @@ static void update_cpustat(void)
> for (i = 0; i < NUM_STATS_PER_GROUP; i++) {
> old_stat = __this_cpu_read(cpustat_old[i]);
> new_stat = get_16bit_precision(cpustat[tracked_stats[i]]);
>- util = DIV_ROUND_UP(100 * (new_stat - old_stat), sample_period_16);
>+ /* Take the delta modulo 2^16 before scaling it. */
>+ delta = new_stat - old_stat;
>+ util = DIV_ROUND_UP(100U * delta, sample_period_16);
> /*
> * Since we use 16-bit precision, the raw data will undergo
> * integer division, which may sometimes result in data loss,
>
>base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
>
--- Thanks!
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 17:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 6:51 [PATCH] watchdog/softlockup: Fix CPU utilization across counter wraparound Avi Weiss
2026-12-03 17:04 ` Bradley Morgan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®