* [PATCH v2] mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation
@ 2026-11-03 3:40 Wang Guangke
2026-11-03 14:34 ` SJ Park
0 siblings, 1 reply; 2+ messages in thread
From: Wang Guangke @ 2026-11-03 3:40 UTC (permalink / raw)
To: sj, akpm; +Cc: damon, linux-mm, linux-kernel, Wang Guangke, stable
estimated_memory_bandwidth is calculated by access_bytes *
USEC_PER_MSEC * MSEC_PER_SEC (= USEC_PER_SEC) / aggr_interval,
and users read it via
/sys/module/damon_stat/parameters/estimated_memory_bandwidth.
On 32-bit systems, access_bytes * USEC_PER_SEC overflows unsigned
long once access_bytes exceeds about 4294 (U32_MAX / USEC_PER_SEC),
so users get a wrong value.
Fix it by doing the calculation in 64-bit and splitting the division
into quotient and remainder.
The issue was reproduced on a 32-bit system (qemu-system-i386): the
parameter estimated_memory_bandwidth reported 362 instead of the
expected 1926150; with the fix, the value matches the expectation.
Fixes: fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth")
Cc: <stable@vger.kernel.org>
Signed-off-by: Wang Guangke <gkwang@linx-info.com>
---
Changes in v2:
- v1: https://lore.kernel.org/all/20261101180001.167198-1-gkwang@linx-info.com
- Clarify the issue and its impact on users.
- Add Cc: stable@.
- Rename variable bytes_per_interval to bytes_per_us.
- Use USEC_PER_SEC instead of 'USEC_PER_MSEC * MSEC_PER_SEC'.
---
mm/damon/stat.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/mm/damon/stat.c b/mm/damon/stat.c
index b05b68f73e10..e44f5bf3645c 100644
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -32,8 +32,8 @@ static bool enabled __read_mostly = IS_ENABLED(
module_param_cb(enabled, &enabled_param_ops, NULL, 0600);
MODULE_PARM_DESC(enabled, "Enable of disable DAMON_STAT");
-static unsigned long estimated_memory_bandwidth __read_mostly;
-module_param(estimated_memory_bandwidth, ulong, 0400);
+static u64 estimated_memory_bandwidth __read_mostly;
+module_param(estimated_memory_bandwidth, ullong, 0400);
MODULE_PARM_DESC(estimated_memory_bandwidth,
"Estimated memory bandwidth usage in bytes per second");
@@ -55,15 +55,18 @@ static void damon_stat_set_estimated_memory_bandwidth(struct damon_ctx *c)
{
struct damon_target *t;
struct damon_region *r;
- unsigned long access_bytes = 0;
+ u64 access_bytes = 0;
+ u64 remainder, bytes_per_us;
damon_for_each_target(t, c) {
damon_for_each_region(r, t)
- access_bytes += (r->ar.end - r->ar.start) *
+ access_bytes += (u64)(r->ar.end - r->ar.start) *
r->nr_accesses;
}
- estimated_memory_bandwidth = access_bytes * USEC_PER_MSEC *
- MSEC_PER_SEC / c->attrs.aggr_interval;
+ bytes_per_us = div64_u64_rem(access_bytes,
+ c->attrs.aggr_interval, &remainder);
+ estimated_memory_bandwidth = bytes_per_us * USEC_PER_SEC +
+ div64_u64(remainder * USEC_PER_SEC, c->attrs.aggr_interval);
}
static int damon_stat_idletime(const struct damon_region *r)
--
2.47.3
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH v2] mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation
2026-11-03 3:40 [PATCH v2] mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation Wang Guangke
@ 2026-11-03 14:34 ` SJ Park
0 siblings, 0 replies; 2+ messages in thread
From: SJ Park @ 2026-11-03 14:34 UTC (permalink / raw)
To: Wang Guangke; +Cc: SJ Park, akpm, damon, linux-mm, linux-kernel, stable
Hi Wang,
On Tue, 3 Nov 2026 11:40:34 +0800 Wang Guangke <gkwang@linx-info.com> wrote:
> estimated_memory_bandwidth is calculated by access_bytes *
> USEC_PER_MSEC * MSEC_PER_SEC (= USEC_PER_SEC) / aggr_interval,
> and users read it via
> /sys/module/damon_stat/parameters/estimated_memory_bandwidth.
>
> On 32-bit systems, access_bytes * USEC_PER_SEC overflows unsigned
> long once access_bytes exceeds about 4294 (U32_MAX / USEC_PER_SEC),
> so users get a wrong value.
>
> Fix it by doing the calculation in 64-bit and splitting the division
> into quotient and remainder.
>
> The issue was reproduced on a 32-bit system (qemu-system-i386): the
> parameter estimated_memory_bandwidth reported 362 instead of the
> expected 1926150; with the fix, the value matches the expectation.
Looks good to me.
>
> Fixes: fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth")
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Wang Guangke <gkwang@linx-info.com>
Reviewed-by: SJ Park <sj@kernel.org>
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
[...]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-03 14:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03 3:40 [PATCH v2] mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation Wang Guangke
2026-11-03 14:34 ` SJ Park
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®