From: SeongJae Park <sj@kernel.org>
To: SeongJae Park <sj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
damon@lists.linux.dev, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH 2/5] mm/damon/core: support addr_unit on damon_find_biggest_system_ram()
Date: Tue, 17 Mar 2026 07:47:24 -0700 [thread overview]
Message-ID: <20260317144725.88524-1-sj@kernel.org> (raw)
In-Reply-To: <20260311052927.93921-3-sj@kernel.org>
On Tue, 10 Mar 2026 22:29:23 -0700 SeongJae Park <sj@kernel.org> wrote:
> damon_find_biggest_system_ram() sets an 'unsigned long' variable with
> 'resource_size_t' value. This is fundamentally wrong. On environments
> such as ARM 32 bit machines having LPAE (Large Physical Address
> Extensions), which DAMON supports, the size of 'unsigned long' may be
> smaller than that of 'resource_size_t'. It is safe, though, since we
> restrict the walk to be done only up to ULONG_MAX.
>
> DAMON supports the address size gap using 'addr_unit'. We didn't add
> the support to the function, just to make the initial support change
> small. Now the support is reasonably settled. This kind of gap is only
> making the code inconsistent and easy to be confused. Add the support
> of 'addr_unit' to the function, by letting callers pass the 'addr_unit'
> and handling it in the function. All callers are passing 'addr_unit' 1,
> though, to keep the old behavior.
>
> Signed-off-by: SeongJae Park <sj@kernel.org>
> ---
> mm/damon/core.c | 33 +++++++++++++++++++++++----------
> 1 file changed, 23 insertions(+), 10 deletions(-)
>
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 3925720a172a6..aee61bf991baa 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -3056,31 +3056,44 @@ static int kdamond_fn(void *data)
>
> static int walk_system_ram(struct resource *res, void *arg)
> {
> - struct damon_addr_range *a = arg;
> + struct resource *a = arg;
>
> - if (a->end - a->start < resource_size(res)) {
> + if (resource_size(a) < resource_size(res)) {
> a->start = res->start;
> - a->end = res->end + 1;
> + a->end = res->end;
> }
> return 0;
> }
>
> +static unsigned long damon_res_to_core_addr(resource_size_t ra,
> + unsigned long addr_unit)
> +{
> + /*
> + * Use div_u64() for avoiding linking errors related with __udivdi3,
> + * __aeabi_uldivmod, or similar problems. This should also improve the
> + * performance optimization (read div_u64() comment for the detail).
> + */
> + if (sizeof(ra) == 8 && sizeof(addr_unit) == 4)
> + return div_u64(ra, addr_unit);
> + return ra / addr_unit;
> +}
> +
> /*
> * Find biggest 'System RAM' resource and store its start and end address in
> * @start and @end, respectively. If no System RAM is found, returns false.
> */
> static bool damon_find_biggest_system_ram(unsigned long *start,
> - unsigned long *end)
> + unsigned long *end, unsigned long addr_unit)
>
> {
> - struct damon_addr_range arg = {};
> + struct resource res = {};
>
> - walk_system_ram_res(0, ULONG_MAX, &arg, walk_system_ram);
> - if (arg.end <= arg.start)
> + walk_system_ram_res(0, -1, &res, walk_system_ram);
> + if (res.end < res.start)
> return false;
>
> - *start = arg.start;
> - *end = arg.end;
> + *start = damon_res_to_core_addr(res.start, addr_unit);
> + *end = damon_res_to_core_addr(res.end + 1, addr_unit);
> return true;
On 32 bit systems having PAE (>4 GiB physical memory address space), above
start and end address could be overflowed, resulting in making an invalid
address range (end <= start). The range validity should be tested here, like
below attaching fixup patch.
Andrew, could you please add the fixup patch?
Thanks,
SJ
[...]
=== >8 ===
From d5654a6cce8a21ae100625ed54c0885556f89645 Mon Sep 17 00:00:00 2001
From: SeongJae Park <sj@kernel.org>
Date: Mon, 16 Mar 2026 23:32:48 -0700
Subject: [PATCH] mm/damon/core: verify found biggest system ram
On 32 bit systems having PAE (>4 GiB physical memory address sapce), the
final start and end address could overflow, resulting in returning an
invalid address range. Verify the returning region. Also remove the
resource validation after walk_system_ram_res(), since the validation
means not a lot.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/core.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/mm/damon/core.c b/mm/damon/core.c
index f9854aedc42d1..339325e1096bc 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -3089,11 +3089,10 @@ static bool damon_find_biggest_system_ram(unsigned long *start,
struct resource res = {};
walk_system_ram_res(0, -1, &res, walk_system_ram);
- if (res.end < res.start)
- return false;
-
*start = damon_res_to_core_addr(res.start, addr_unit);
*end = damon_res_to_core_addr(res.end + 1, addr_unit);
+ if (*end <= *start)
+ return false;
return true;
}
--
2.47.3
next prev parent reply other threads:[~2026-03-17 14:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-11 5:29 [PATCH 0/5] mm/damon: support addr_unit on default monitoring targets for modules SeongJae Park
2026-03-11 5:29 ` [PATCH 1/5] mm/damon/core: fix wrong end address assignment on walk_system_ram() SeongJae Park
2026-03-11 5:29 ` [PATCH 2/5] mm/damon/core: support addr_unit on damon_find_biggest_system_ram() SeongJae Park
2026-03-17 14:47 ` SeongJae Park [this message]
2026-03-11 5:29 ` [PATCH 3/5] mm/damon/core: receive addr_unit on damon_set_region_biggest_system_ram_default() SeongJae Park
2026-03-14 0:18 ` SeongJae Park
2026-03-11 5:29 ` [PATCH 4/5] mm/damon/reclaim: respect addr_unit on default monitoring region setup SeongJae Park
2026-03-11 5:29 ` [PATCH 5/5] mm/damon/lru_sort: " SeongJae 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=20260317144725.88524-1-sj@kernel.org \
--to=sj@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=damon@lists.linux.dev \
--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