DAMON development mailing list
 help / color / mirror / Atom feed
* [PATCH 5.15.y] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop
       [not found] <2026090834-entourage-unwound-48d4@gregkh>
@ 2026-09-09  4:24 ` SJ Park
  2026-09-09  4:33   ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: SJ Park @ 2026-09-09  4:24 UTC (permalink / raw)
  To: stable; +Cc: damon, SJ Park, Andrew Morton

Patch series "mm/damon: unurgent fixes for infinite loop, NULL de-ref and
races", v1.1.

Sashiko found a few issues in DAMON that could cause infinite loop, NULL
dereference and monitoring results degradation.  The first two sounds
scary but the infinite loop happens only under unreasonable user setup.
The NULL dereference is only in a unit test.  Monitoring results
degradation is trivial since it is only best-effort, and those happens
from only unlikely races.  Still those are bugs that better to fix if
possible.  Fix those.

This patch (of 6):

Due to online parameter update like events, the number of DAMON regions
could be higher than the user-set upper limit.  kdamond_merge_regions()
repeats merge regions until the number meets the limit, while doubling the
merge threshold up to the theoretical maximum threshold.  It is tried only
up to the theoretical maximum threshold because even the aggressive
merging can fail from reducing the number of regions under the
user-defined upper limit.  For example, there could be many user-defined
non-contiguous regions that cannot be merged.

The threshold based loop break condition is evaluated by comparing the
threshold for the next merging try against the theoretical maximum
threshold.  If max_thres is larger than UINT_MAX / 2, doubling the
threshold could make it overflow, and bypass the loop break condition.  In
the case, if the number of regions cannot be reduced under the upper limit
like explained above, the loop will run infinitely.

Prevent the case by doing the break condition check before doubling the
threshold.  Also, prevent the threshold exceeding the maximum threshold,
as it could overflow and apply the wrong merge threshold.

This issue is unlikely to occur in real world, since having the max_thres
higher than UINT_MAX / 2 require unrealistically large aggregation
intervals compared to the sampling interval.  Also, it requires an
unrealistically large number of uncontiguous regions setup.  Nonetheless,
the consequence is bad and the fix is simple.

The issue was discovered [1] by Sashiko.

Link: https://lore.kernel.org/20260715031002.108504-1-sj@kernel.org
Link: https://lore.kernel.org/20260715031002.108504-2-sj@kernel.org
Link: https://lore.kernel.org/20260709145425.96247-1-sj@kernel.org [1]
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.x
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 123e4619ab6c8ab1c4cb1d7a58311a2af13929cd)
Signed-off-by: SJ Park <sj@kernel.org>
---
 mm/damon/core.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index 4f031412f65cc..3a50ced2efc0f 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -523,15 +523,20 @@ static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
 
 	max_thres = c->aggr_interval /
 		(c->sample_interval ?  c->sample_interval : 1);
-	do {
+	while (true) {
 		nr_regions = 0;
 		damon_for_each_target(t, c) {
 			damon_merge_regions_of(t, threshold, sz_limit);
 			nr_regions += damon_nr_regions(t);
 		}
-		threshold = max(1, threshold * 2);
-	} while (nr_regions > c->max_nr_regions &&
-			threshold / 2 < max_thres);
+		if (nr_regions <= c->max_nr_regions ||
+				max_thres <= threshold)
+			break;
+		if (threshold < max_thres / 2)
+			threshold = max(1, threshold * 2);
+		else
+			threshold = max_thres;
+	}
 }
 
 /*
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 5.15.y] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop
  2026-09-09  4:24 ` [PATCH 5.15.y] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop SJ Park
@ 2026-09-09  4:33   ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-09-09  4:33 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] Calculation of merged region access frequency can overflow unsigned long on 32-bit architectures, leading to corrupted access statistics.
--

commit 49c8c958aefed16883d3ce4d2099a0422bae39ec
Author: SJ Park <sj@kernel.org>

mm/damon/core: avoid infinite kdamond_merge_regions() internal loop

This commit fixes a potential infinite loop in kdamond_merge_regions()
by checking the loop break condition before doubling the threshold. It
also prevents the threshold from exceeding the theoretical maximum.

Link: https://lore.kernel.org/20260715031002.108504-1-sj@kernel.org
Link: https://lore.kernel.org/20260715031002.108504-2-sj@kernel.org
Link: https://lore.kernel.org/20260709145425.96247-1-sj@kernel.org

> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 4f031412f65cc..3a50ced2efc0f 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c

[ ... ]

> @@ -523,15 +523,20 @@ static void kdamond_merge_regions(struct damon_ctx *c, unsigned int threshold,
>  
>  	max_thres = c->aggr_interval /
>  		(c->sample_interval ?  c->sample_interval : 1);
> -	do {
> +	while (true) {
>  		nr_regions = 0;
>  		damon_for_each_target(t, c) {
>  			damon_merge_regions_of(t, threshold, sz_limit);

[Severity: High]
This is a pre-existing issue, but does the access frequency calculation in
damon_merge_two_regions() (called via damon_merge_regions_of()) overflow on
32-bit architectures?

In mm/damon/core.c:damon_merge_two_regions(), the new access count is
calculated as:

    l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) /
                     (sz_l + sz_r);

Since sz_limit allows regions to grow up to 1/10th of the target's address
space, large regions can cause this to silently overflow a 32-bit unsigned
long. For example, merging a 250 MB region with a typical access frequency
of 20 results in 20 * 262,144,000 = 5,242,880,000, which exceeds the 32-bit
maximum of 4,294,967,295.

Can this lead to severely undercounted access statistics, potentially causing
DAMON_RECLAIM to aggressively swap out falsely "cold" regions and induce
system thrashing?

>  			nr_regions += damon_nr_regions(t);
>  		}
> -		threshold = max(1, threshold * 2);
> -	} while (nr_regions > c->max_nr_regions &&
> -			threshold / 2 < max_thres);
> +		if (nr_regions <= c->max_nr_regions ||
> +				max_thres <= threshold)
> +			break;

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260909042418.198028-1-sj@kernel.org?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-09  4:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <2026090834-entourage-unwound-48d4@gregkh>
2026-09-09  4:24 ` [PATCH 5.15.y] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop SJ Park
2026-09-09  4:33   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox