mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: SJ Park <sj@kernel.org>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Aveline Noir <jm5905938@gmail.com>, SJ Park <sj@kernel.org>,
	damon@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Subject: [PATCH 4/9] mm/damon/stat: avoid 32-bit overflow in idle percentiles
Date: Mon,  2 Nov 2026 07:07:51 -0800	[thread overview]
Message-ID: <20261102150757.93058-5-sj@kernel.org> (raw)
In-Reply-To: <20261102150757.93058-1-sj@kernel.org>

From: Aveline Noir <jm5905938@gmail.com>

On 32-bit, accounted_bytes * 100 can wrap before division by total_sz.
Some entries in memory_idle_ms_percentiles then keep their old values
instead of being updated.

For example, a 256 MiB i386 QEMU guest reported p0=19200 ms, but p50 and
p100 were both zero.  The issue was also shared with another mail [1].

Cast accounted_bytes to u64 before multiplying and use div64_ul() for
the division. Compute the percentage once per region.

Link: https://lore.kernel.org/r/CANOyQmHad1vamnZT12M5yPrJ69==Pt1U0+MGw6u0MuNtr-DRCw@mail.gmail.com [1]
Link: https://lore.kernel.org/179207592479.3.11595453180868898703@gmail.com
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Aveline Noir <jm5905938@gmail.com>
Reviewed-by: SJ Park <sj@kernel.org>
Signed-off-by: SJ Park <sj@kernel.org>
---
Changes from v1
- v1: https://lore.kernel.org/179207592479.3.11595453180868898703@gmail.com
- Collect R-b: from SJ.
- Rebase to latest mm-new.

 mm/damon/stat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mm/damon/stat.c b/mm/damon/stat.c
index b05b68f73e10d..eccd372810d04 100644
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -114,7 +114,7 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)
 {
 	struct damon_region **sorted_regions, *region;
 	int nr_regions;
-	unsigned long total_sz, accounted_bytes = 0;
+	unsigned long total_sz, accounted_bytes = 0, percentile;
 	int err, i, next_percentile = 0;
 
 	err = damon_stat_sort_regions(c, &sorted_regions, &nr_regions,
@@ -124,7 +124,8 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c)
 	for (i = 0; i < nr_regions; i++) {
 		region = sorted_regions[i];
 		accounted_bytes += region->ar.end - region->ar.start;
-		while (next_percentile <= accounted_bytes * 100 / total_sz)
+		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;
-- 
2.47.3

  parent reply	other threads:[~2026-11-02 15:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 15:07 [PATCH 0/9] mm/damon: various fixes and cleanup of code, test, and docs SJ Park
2026-11-02 15:07 ` [PATCH 1/9] mm/damon/core: remove unnecessary zero-initialization in damon_new_ctx() SJ Park
2026-11-02 15:07 ` [PATCH 2/9] mm/damon/ops-common: align MMU notifier range when clearing PTE young bit SJ Park
2026-11-02 15:07 ` [PATCH 3/9] mm/damon/ops-common: align MMU notifier range when clearing PMD " SJ Park
2026-11-02 15:07 ` SJ Park [this message]
2026-11-02 15:07 ` [PATCH 5/9] mm/damon/{p,v}addr: skip folios already on the migration target node SJ Park
2026-11-02 15:07 ` [PATCH 6/9] selftests/damon/sysfs.py: stop obsolete_target shells hanging the test SJ Park
2026-11-02 15:07 ` [PATCH 7/9] mm/damon/reclaim: fix typo and grammar SJ Park
2026-11-02 15:07 ` [PATCH 8/9] Docs/admin-guide/mm/damon/lru_sort: fix grammar and stlye SJ Park
2026-11-02 15:07 ` [PATCH 9/9] Docs/admin-guide/mm/damon/reclaim: document how to disable watermarks SJ Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261102150757.93058-5-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=damon@lists.linux.dev \
    --cc=jm5905938@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®