Skip to contentSkip to conversation

mm/damon/stat: avoid 32-bit overflow in idle time milliseconds

PATCH In review David Carlier 3 participants 3 messages Nov 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/damon/stat: avoid 32-bit overflow in idle time milliseconds

On 32-bit, the region idle time in microseconds is computed in a long before the division to milliseconds. It wraps once a region has been idle for about 36 minutes regardless of the intervals, so the cold end of memory_idle_ms_percentiles reports wrong values with flipped signs.

Do the multiplication in 64 bits and divide with div_s64() so the sign of recently accessed regions is kept.

Fixes e5d2585d9e85 ("mm/damon/stat: calculate and expose idle time percentiles")

---
Ref: https://lore.kernel.org/[email protected]

 mm/damon/stat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/stat.c +2 -2
index 6e05a2b067f2..f2387aee675d 100644
@@ -130,8 +130,8 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)
 		percentile = div64_ul((u64)accounted_bytes * 100, total_sz);
 		while (next_percentile <= percentile)
 			memory_idle_ms_percentiles[next_percentile++] =
-				damon_stat_idletime(region) *
-				(long)c->attrs.aggr_interval / USEC_PER_MSEC;
+				div_s64((s64)damon_stat_idletime(region) *
+				c->attrs.aggr_interval, USEC_PER_MSEC);
 	}
 	kfree(sorted_regions);
 }
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

On 32-bit, the region idle time in microseconds is computed in a long before the division to milliseconds. It wraps once a region has been idle for about 36 minutes regardless of the intervals, so the cold end of memory_idle_ms_percentiles reports wrong values with flipped signs.

Do the multiplication in 64 bits and divide with div_s64() so the sign of recently accessed regions is kept.

Fixes e5d2585d9e85 ("mm/damon/stat: calculate and expose idle time percentiles")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to David Carlier

Hello David,

Quoted 7 lines · On Tue, 17 Nov 2026 09:43:22 +0000 David Carlier <[email protected]> wrote:

On 32-bit, the region idle time in microseconds is computed in a long before the division to milliseconds. It wraps once a region has been idle for about 36 minutes regardless of the intervals, so the cold end of memory_idle_ms_percentiles reports wrong values with flipped signs.

Do the multiplication in 64 bits and divide with div_s64() so the sign of recently accessed regions is kept.

Thank you for this patch. Looks good to me.

Quoted 4 lines
Fixes e5d2585d9e85 ("mm/damon/stat: calculate and expose idle time percentiles")

This patch is applied to damon/next [1] tree. I will repost it soon (~1 week) as an mm.git merge request. So, no action from your side is needed for now.

[1] https://origin.kernel.org/doc/html/latest/mm/damon/maintainer-profile.html#scm-trees

Thanks,
SJ

[...]