[RFC PATCH v5 02/11] mm/damon/core: merge quota-sliced regions back

SeongJae Park
2026-04-10T14:20:52Z

damos_apply_scheme() could split the given region if applying the
scheme's action to the entire region can result in violating the
quota-set upper limit. Keeping regions that are created by such split
operations is unnecessary overhead.

The overhead would be negligible in the common case because such split
operations could happen only up to the number of installed schemes per
scheme apply interval. The following commit could make the impact
larger, though. The following commit will allow the action-failed
region to be charged in a different ratio. If both the ratio and the
remaining quota is quite small, and the action is nearly always failing,
a high number of split operations could be made.

Remove the unnecessary overhead by merging the sliced regions back, as
soon as the schemes handling for the slices are done, and there is no
reason to keep the sliced regions.

The sliced regions may need to be kept if no scheme action is applied to
the slice due to the quota being fully charged. That is, the 'age'
field of a region is reset when a DAMOS action is applied to the region.
If no DAMOS action is applied to the sliced region, the region keeps the
age information. If it is merged into the original region, the age
information is lost.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/core.c | 39 +++++++++++++++++++++++++++++++++++----
1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/mm/damon/core.c b/mm/damon/core.c
index c7d05d2385fe8..b0f673efc53c8 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2159,6 +2159,40 @@ static void damon_do_apply_schemes(struct damon_ctx *c,
}
}

+static void damos_apply_target(struct damon_ctx *c, struct damon_target *t)
+{
+ struct damon_region *r, *orig_region = NULL;
+ unsigned long orig_end_addr;
+
+ damon_for_each_region(r, t) {
+ /*
+ * damon_do_apply_schemes() could split the region for the
+ * quota. Keeping the new slices is an overhead. Merge back
+ * the slices into the original region if there is no reason to
+ * keep those.
+ */
+ if (!orig_region || orig_end_addr <= r->ar.start) {
+ orig_region = r;
+ orig_end_addr = r->ar.end;
+ }
+ damon_do_apply_schemes(c, t, r);
+ if (r == orig_region)
+ continue;
+ /*
+ * If no scheme was applied to the sliced region, the age of
+ * the slice ain't be reset. Don't merge that back.
+ * Otherwise, the monitored information of the region is lost.
+ */
+ if (r->age) {
+ orig_region = NULL;
+ continue;
+ }
+ orig_region->ar.end = r->ar.end;
+ damon_destroy_region(r, t);
+ r = orig_region;
+ }
+}
+
/*
* damon_feed_loop_next_input() - get next input to achieve a target score.
* @last_input The last input.
@@ -2528,7 +2562,6 @@ static void damos_trace_stat(struct damon_ctx *c, struct damos *s)
static void kdamond_apply_schemes(struct damon_ctx *c)
{
struct damon_target *t;
- struct damon_region *r;
struct damos *s;
bool has_schemes_to_apply = false;

@@ -2551,9 +2584,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c)
damon_for_each_target(t, c) {
if (c->ops.target_valid && c->ops.target_valid(t) == false)
continue;
-
- damon_for_each_region(r, t)
- damon_do_apply_schemes(c, t, r);
+ damos_apply_target(c, t);
}

damon_for_each_scheme(s, c) {
--
2.47.3