From: SJ Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SJ Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [RFC PATCH v1.1 6/8] mm/damon: document region size validation in damon_set_regions()
Date: Thu, 2 Jul 2026 14:48:30 -0700 [thread overview]
Message-ID: <20260702214831.97260-1-sj@kernel.org> (raw)
In-Reply-To: <20260702211954.794F81F000E9@smtp.kernel.org>
On Thu, 02 Jul 2026 21:19:54 +0000 sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
>
> Pre-existing issues:
> - [High] `damon_set_regions()` creates duplicate and overlapping regions if input ranges share aligned boundaries, corrupting DAMON's internal state.
> - [Medium] `damon_set_regions()` inconsistently bypasses size validation, silently accepting and re-aligning negative-sized (`start > end`) ranges instead of rejecting them.
> --
>
> commit ded29c13e7c58ad791814e55b8139da57477556b
> Author: SJ Park <sj@kernel.org>
>
> mm/damon: document region size validation in damon_set_regions()
>
> The kernel doc comment of damon_region clearly specifies every region
> should have positive size. But it is unclear who should verify it.
> damon_set_regions() is the recommended DAMON core function for setting
> regions from the callers, and has the verification.
>
> > diff --git a/include/linux/damon.h b/include/linux/damon.h
> > index 805e089ff4f2f..b3c0ac5bf9b88 100644
> > --- a/include/linux/damon.h
> > +++ b/include/linux/damon.h
> > @@ -49,7 +49,8 @@ struct damon_size_range {
> > * @list: List head for siblings.
> > * @age: Age of this region.
> > *
> > - * For any use case, @ar should be non-zero positive size.
> > + * For any use case, @ar should be non-zero positive size. damon_set_regions()
> > + * does the validation.
>
> [Severity: High]
> This is a pre-existing issue, but if callers rely on damon_set_regions() to
> do the validation, can it create duplicate and overlapping regions when input
> ranges share aligned boundaries?
>
> If userspace passes multiple valid non-overlapping ranges (e.g., [0, 100] and
> [100, 200]) that align to the same min_region_sz (e.g., 4096), and the target
> is empty, the loop in mm/damon/core.c:damon_set_regions() blindly adds
> aligned ranges without checking for previous alignments:
>
> mm/damon/core.c:damon_set_regions() {
> ...
> r = damon_new_region(
> ALIGN_DOWN(ranges[i].start,
> min_region_sz),
> ALIGN(ranges[i].end, min_region_sz));
> if (!r)
> return -ENOMEM;
> damon_add_region(r, t);
> ...
> }
>
> When regions exist, the iterator is not reset per input range:
>
> mm/damon/core.c:damon_set_regions() {
> ...
> r = damon_first_region(t);
> /* Add new regions or resize existing regions to fit in the ranges */
> for (i = 0; i < nr_ranges; i++) {
> ...
> /* Get the first/last regions intersecting with the range */
> damon_for_each_region_from(r, t) {
> ...
> }
>
> It advances past the first intersection, fails to find the same region for the
> second range, and creates a duplicate [0, 4096] region, which corrupts
> DAMON's internal lists.
Yes, and we are working for this with a separate hotfix [1].
>
> [Severity: Medium]
> This isn't a bug introduced by this patch, but does damon_set_regions()
> actually fully validate the size?
>
> The validation check in mm/damon/core.c:damon_set_regions() uses aligned
> boundaries:
>
> mm/damon/core.c:damon_set_regions() {
> ...
> if (ALIGN_DOWN(ranges[i].start, min_region_sz) >=
> ALIGN(ranges[i].end, min_region_sz))
> return -EINVAL;
> ...
> }
>
> If a user writes a reversed range (e.g., start=4000, end=1000) via sysfs,
> since 0 >= 4096 evaluates to false, wouldn't the invalid input be silently
> accepted and transformed into [0, 4096], bypassing the intended size
> restrictions?
This is an intended behavior.
Yes, this might be confusing. But why a sane user would do this? Maybe we can
update documentation for reducing the confusion, but not necessarily a blocker
of this patch.
[1] https://lore.kernel.org/20260702170326.87255-1-sj@kernel.org
Thanks,
SJ
[...]
next prev parent reply other threads:[~2026-07-02 21:48 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-02 21:05 [RFC PATCH v1.1 0/8] mm/damon: validate all parameters in the core SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 1/8] mm/damon/core: safely validate src on damon_commit_ctx() SJ Park
2026-07-02 21:21 ` sashiko-bot
2026-07-02 21:35 ` SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 2/8] mm/damon/core: do parameter testing commit on damon_start() SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 3/8] mm/damon/sysfs: remove duplicated commit input validity check SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 4/8] mm/damon/reclaim: remove duplicated min_region_sz power of 2 check SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 5/8] mm/damon/lru_sort: remove duplicated min_region_sz power_of_2() check SJ Park
2026-07-02 21:20 ` sashiko-bot
2026-07-02 21:44 ` SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 6/8] mm/damon: document region size validation in damon_set_regions() SJ Park
2026-07-02 21:19 ` sashiko-bot
2026-07-02 21:48 ` SJ Park [this message]
2026-07-02 21:05 ` [RFC PATCH v1.1 7/8] mm/damon/core: remove start, end check in damon_set_region_system_rams() SJ Park
2026-07-02 21:16 ` sashiko-bot
2026-07-02 21:50 ` SJ Park
2026-07-02 21:05 ` [RFC PATCH v1.1 8/8] mm/damon/sysfs: remove region size validation SJ Park
2026-07-02 21:15 ` sashiko-bot
2026-07-02 21:53 ` 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=20260702214831.97260-1-sj@kernel.org \
--to=sj@kernel.org \
--cc=damon@lists.linux.dev \
--cc=sashiko-bot@kernel.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