Linux kernel -stable discussions
 help / color / mirror / Atom feed
* + mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch added to mm-hotfixes-unstable branch
@ 2026-11-10  1:02 Andrew Morton
  0 siblings, 0 replies; only message in thread
From: Andrew Morton @ 2026-11-10  1:02 UTC (permalink / raw)
  To: mm-commits, stable, sj, gkwang, akpm


The patch titled
     Subject: mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation
has been added to the -mm mm-hotfixes-unstable branch.  Its filename is
     mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch

This patch will later appear in the mm-hotfixes-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Wang Guangke <gkwang@linx-info.com>
Subject: mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation
Date: Mon,  9 Nov 2026 16:48:14 -0800

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.

Link: https://lore.kernel.org/20261110004816.100066-1-sj@kernel.org
Fixes: fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth")
Signed-off-by: Wang Guangke <gkwang@linx-info.com>
Signed-off-by: SJ Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: SJ Park <sj@kernel.org>
Cc: <stable@vger.kernel.org>
---

 mm/damon/stat.c |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

--- a/mm/damon/stat.c~mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation
+++ a/mm/damon/stat.c
@@ -32,8 +32,8 @@ static bool enabled __read_mostly = IS_E
 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_mem
 {
 	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)
_

Patches currently in -mm which might be from gkwang@linx-info.com are

mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-10  1:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-10  1:02 + mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch added to mm-hotfixes-unstable branch Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox