Skip to contentSkip to conversation

mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation

PATCH Applied SJ Park 3 participants 3 messages Nov 10 – Nov 23

Subsystems (3)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

PATCH v3 1/1 mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation

From: Wang Guangke <[email protected]>

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")

---
Changes from v2
- v2: https://lore.kernel.org/[email protected]
- Collect R-b: from SJ.
- Rebase to the latest mm-new.
Changes from v1:
- v1: https://lore.kernel.org/all/[email protected]
- 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(-)
mm/damon/stat.c +9 -6
index b05b68f73e10d..e44f5bf3645cc 100644
@@ -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)

base-commit: 09f140d2cb078edfcd08a40a23ead0bdceea30a4

Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines

From: Wang Guangke <[email protected]>

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.

Quoted 5 lines
Fixes fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer tree notice Applied
#2 lore raw

[merged mm-hotfixes-stable] mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch removed from -mm tree

In reply to SJ Park
The quilt patch titled Subject: mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation has been removed from the -mm tree. Its filename was…

The quilt patch titled
Subject: mm/damon/stat: avoid overflow in estimated_memory_bandwidth calculation
has been removed from the -mm tree. Its filename was
mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation.patch

This patch was dropped because it was merged into the mm-hotfixes-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Wang Guangke <[email protected]>
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.

Fixes fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth")
 mm/damon/stat.c |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
mm/damon/stat.c renamed from mm/damon/stat.c~mm-damon-stat-avoid-overflow-in-estimated_memory_bandwidth-calculation +9 -6
@@ -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 [email protected] are