* [PATCH 6.18.y] mm/damon/core: skip aging from repeated aggressive merging
[not found] <2026090840-shifter-galvanize-450f@gregkh>
@ 2026-09-09 4:40 ` SJ Park
2026-09-09 4:51 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: SJ Park @ 2026-09-09 4:40 UTC (permalink / raw)
To: stable; +Cc: damon, SJ Park, Andrew Morton
The number of DAMON regions could temporarily exceed the user-defined
maximum number of regions limit for corner cases. For example, users
could lower the limit via runtime parameters update. For such a case,
kdamond_merge_regions() repeats merging regions in the case doubling the
merge threshold. The repeated merge operation could update the age of
regions multiple times. This corrupts the monitoring results. Fix the
issue by asking the merge operation to skip aging for the corner case.
The user impact is degradation of the monitoring quality. The impact
should be mild, since the degradation is only temporal, and it is not
common to happen in realistic setups.
The issue was discovered [1,2] by Sashiko.
Link: https://lore.kernel.org/20260712165432.87609-1-sj@kernel.org
Link: https://lore.kernel.org/20260621203548.10718-1-sj@kernel.org [1]
Link: https://lore.kernel.org/20260709145425.96247-1-sj@kernel.org [2]
Fixes: 310d6c15e910 ("mm/damon/core: merge regions aggressively when max_nr_regions is unmet")
Signed-off-by: SJ Park <sj@kernel.org>
Cc: <stable@vger.kernel.org> # 6.10
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 0250dbe08c730d003ef9f484da56ae09a1ea0c4c)
Signed-off-by: SJ Park <sj@kernel.org>
---
mm/damon/core.c | 21 +++++++++++++--------
mm/damon/tests/core-kunit.h | 2 +-
2 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 70ac1f08753d1..c98539efd2a56 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2364,17 +2364,19 @@ static void damon_merge_two_regions(struct damon_target *t,
* sz_limit size upper limit of each region
*/
static void damon_merge_regions_of(struct damon_target *t, unsigned int thres,
- unsigned long sz_limit)
+ unsigned long sz_limit, bool count_age)
{
struct damon_region *r, *prev = NULL, *next;
damon_for_each_region_safe(r, next, t) {
- if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
- r->age = 0;
- else if ((r->nr_accesses == 0) != (r->last_nr_accesses == 0))
- r->age = 0;
- else
- r->age++;
+ if (count_age) {
+ if (abs(r->nr_accesses - r->last_nr_accesses) > thres)
+ r->age = 0;
+ else if ((r->nr_accesses == 0) != (r->last_nr_accesses == 0))
+ r->age = 0;
+ else
+ r->age++;
+ }
if (prev && prev->ar.end == r->ar.start &&
abs(prev->nr_accesses - r->nr_accesses) <= thres &&
@@ -2408,15 +2410,18 @@ static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
struct damon_target *t;
unsigned int nr_regions;
unsigned int max_thres;
+ bool count_age = true;
max_thres = c->attrs.aggr_interval /
(c->attrs.sample_interval ? c->attrs.sample_interval : 1);
do {
nr_regions = 0;
damon_for_each_target(t, c) {
- damon_merge_regions_of(t, threshold, sz_limit);
+ damon_merge_regions_of(t, threshold, sz_limit,
+ count_age);
nr_regions += damon_nr_regions(t);
}
+ count_age = false;
threshold = max(1, threshold * 2);
} while (nr_regions > c->attrs.max_nr_regions &&
threshold / 2 < max_thres);
diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 6687778752728..80dcf1d641e07 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -261,7 +261,7 @@ static void damon_test_merge_regions_of(struct kunit *test)
damon_add_region(r, t);
}
- damon_merge_regions_of(t, 9, 9999);
+ damon_merge_regions_of(t, 9, 9999, true);
/* 0-112, 114-130, 130-156, 156-170 */
KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 5u);
for (i = 0; i < 5; i++) {
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH 6.18.y] mm/damon/core: skip aging from repeated aggressive merging
2026-09-09 4:40 ` [PATCH 6.18.y] mm/damon/core: skip aging from repeated aggressive merging SJ Park
@ 2026-09-09 4:51 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-09-09 4:51 UTC (permalink / raw)
To: SJ Park; +Cc: damon
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Infinite loop in kdamond_merge_regions due to threshold integer overflow
--
commit af1455c64e5ba1df658a88dc7d8a3cbfe32c0c5b
Author: SJ Park <sj@kernel.org>
mm/damon/core: skip aging from repeated aggressive merging
The number of DAMON regions could temporarily exceed the user-defined
maximum number of regions limit. This patch modifies kdamond_merge_regions()
to skip aging when the merge operation is repeated to meet the max regions
limit, preventing corruption of the monitoring results.
Link: https://lore.kernel.org/20260712165432.87609-1-sj@kernel.org
Link: https://lore.kernel.org/20260621203548.10718-1-sj@kernel.org [1]
Link: https://lore.kernel.org/20260709145425.96247-1-sj@kernel.org [2]
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 70ac1f08753d1..c98539efd2a56 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
[ ... ]
> @@ -2408,15 +2410,18 @@ static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
> struct damon_target *t;
> unsigned int nr_regions;
> unsigned int max_thres;
> + bool count_age = true;
>
> max_thres = c->attrs.aggr_interval /
> (c->attrs.sample_interval ? c->attrs.sample_interval : 1);
> do {
> nr_regions = 0;
> damon_for_each_target(t, c) {
> - damon_merge_regions_of(t, threshold, sz_limit);
> + damon_merge_regions_of(t, threshold, sz_limit,
> + count_age);
> nr_regions += damon_nr_regions(t);
> }
> + count_age = false;
> threshold = max(1, threshold * 2);
> } while (nr_regions > c->attrs.max_nr_regions &&
> threshold / 2 < max_thres);
[Severity: High]
This is a pre-existing issue, but can this loop run infinitely due to an
integer overflow?
If a user updates DAMON parameters while it is running, setting a large
aggr_interval and a small sample_interval (for example, aggr_us=0x80000000,
sample_us=1), max_thres will be greater than or equal to 0x80000000.
Concurrently, if max_nr_regions is dramatically lowered,
kdamond_merge_regions() could be called with a stale, small sz_limit. This
small sz_limit would prevent regions from merging enough to meet the new
max_nr_regions.
Inside the loop, threshold (a 32-bit unsigned int) doubles on each
iteration. If nr_regions > c->attrs.max_nr_regions remains true, threshold
will eventually overflow (for example, 0x80000000 * 2 = 0) and reset back
to 1 via max(1, threshold * 2).
Because threshold / 2 never exceeds 0x7FFFFFFF, the condition
threshold / 2 < max_thres will always evaluate to true when
max_thres >= 0x80000000, permanently hanging the kdamond thread.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260909044055.213060-1-sj@kernel.org?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-09-09 4:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <2026090840-shifter-galvanize-450f@gregkh>
2026-09-09 4:40 ` [PATCH 6.18.y] mm/damon/core: skip aging from repeated aggressive merging SJ Park
2026-09-09 4:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox