mm/damon/core: iterate over installed probes instead of DAMON_MAX_PROBES
Subsystems (2)
SJ Park
maintainerv5 cover mm/damon/core: iterate over installed probes instead of DAMON_MAX_PROBES
From: Suhaas Joshi <[email protected]>
There were two "todo: " items of the same nature in mm/damon/core.c. Both of these suggested that we should iterate over only installed probes while updating (last_)probe_hits[] during merging/splitting regions, instead of iterating over the entire array, which may have empty/unused indices.
So this patch series does exactly that. It replaces the for-loop over [0, DAMON_MAX_PROBES) with a damon_for_each_probe() over the probe list of the context.
Reviewer note.
These patches are based off mm-new. They pass all KUnit tests related to damon. I have boot tested (but nothing beyond) on qemu-x86_64. Please do let me know if I should do any specific tests and link the logs here.
The series has been tested with mm-new fetched on Oct 14, 2026.
Changes from v4
- v4: https://lore.kernel.org/[email protected]
- Collect R-b: from SJ.
- Remove/separate messages in cover letter that not suitable for commit message.
- Wrap cover letter for 75 columns length.
- Rebase to latest mm-new.
Changes v3 -> v4:
1. In some places, kunit_skip() was being called before freeing
resources. This is correctly ordered now.
2. In some places, regions were not being freed. In v4, they are being freed.
Link to v3: https://lore.kernel.org/damon/[email protected]/T/#t
Changes v2 -> v3:
1. In test functions, new probe and context allocations happen after target, region etc have been allocated. Further, target is added to the context. These 2 changes help in reducing allocation code and improving simplicity and readability of the patches. This change is true for both patches. (on SJ suggestion)
2. Earlier, if a function's argument list required multiple lines, I was aligning these lines to the opening paranthesis of the function. In v3, these begin after 2 tab spaces. (On SJ suggestion)
3. damon_test_last_region_quota_reset() uses the older version of damos_skip_charged_region(). Make it use the new version.
Link to v2: https://lore.kernel.org/damon/[email protected]/
Changes v1 -> v2:
1. simplified commit message of patch 1 (on Liew's suggestion)
2. wrapped all lines at 80 characters (on SJ's suggestion)
3. added comments for new argument in function (on SJ's suggestion)
4. added damon_ctx_destroy() in r2 failure case in damon_test_merge_two() (on SJ/Sashiko/Liew suggestion)
Link to v1: https://lore.kernel.org/damon/[email protected]/
SJ Park (1):
mm/damon/core: copy only installed probe in damon_split_region_at()
Suhaas Joshi (1):
mm/damon/core: use only installed probe in damon_merge_two_regions()
mm/damon/core.c | 57 +++++++++++++++---------- mm/damon/tests/core-kunit.h | 84 +++++++++++++++++++++++++++++++------ 2 files changed, 106 insertions(+), 35 deletions(-)
base-commit: 79e2e91bd5a4b213cf40819c2711a9364b2c0f86
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH v5 1/2 mm/damon/core: use only installed probe in damon_merge_two_regions()
From: Suhaas Joshi <[email protected]>
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the DAMON_MAX_PROBES macro. However, it is possible that we have fewer probes installed than DAMON_MAX_PROBES. In such cases, we end up making redundant iterations. Therefore, to remedy this, iterate over the list of installed probes instead of iterating over the entire array. For doing this, start accepting a struct damon_ctx in damon_merge_two_regions(), and update calling functions to pass this argument.
Update the damon_test_merge_two() test to use this new signature for damon_merge_two_regions() as well.
--- mm/damon/core.c | 15 ++++++++++----- mm/damon/tests/core-kunit.h | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-)
mm/damon/core.c +10 -5
@@ -3549,20 +3549,25 @@ static void damon_verify_merge_two_regions(/* * Merge two adjacent regions into one region */ -static void damon_merge_two_regions (struct damon_target *t, -struct damon_region *l,struct damon_region *r) +static void damon_merge_two_regions (struct damon_ctx *ctx, +struct damon_target *t,struct damon_region *l, +struct damon_region *r) {unsigned long sz_l =damon_sz_region (l), sz_r =damon_sz_region (r);int i; +struct damon_probe *p; l->nr_accesses = (l->nr_accesses * sz_l + r->nr_accesses * sz_r) / (sz_l + sz_r); l->age = (l->age * sz_l + r->age * sz_r) / (sz_l + sz_r); l->ar.end = r->ar.end; -/* todo: do this for only installed probes */ -for (i =0 ; i <DAMON_MAX_PROBES ; i++) + + i =0 ; +damon_for_each_probe (p, ctx) { l->probe_hits[i] = (l->probe_hits[i] * sz_l + r->probe_hits[i] * sz_r) / (sz_l + sz_r); + ++i; + }damon_verify_merge_two_regions (l, r);damon_destroy_region (r, t); }
@@ -3615,7 +3620,7 @@ static void damon_merge_regions_of(struct damon_target *t, unsigned int thres, goto set_prev_continue;if (damon_sz_region (prev) +damon_sz_region (r) > sz_limit) goto set_prev_continue; -damon_merge_two_regions (t, prev, r); +damon_merge_two_regions (ctx, t, prev, r);continue ; set_prev_continue: prev = r;
mm/damon/tests/core-kunit.h +18 -2
@@ -182,6 +182,8 @@ static void damon_test_merge_two(struct kunit *test) {struct damon_target *t;struct damon_region *r, *r2, *r3; +struct damon_probe *p; +struct damon_ctx *ctx;int i; t =damon_new_target ();
@@ -206,7 +208,21 @@ static void damon_test_merge_two(struct kunit *test) r2->age =21 ;damon_add_region (r2, t); -damon_merge_two_regions (t, r, r2); + p =damon_new_probe (); +if (!p) { +damon_free_target (t); +kunit_skip (test,"probe alloc fail" ); + } + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +damon_free_target (t); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); +damon_add_target (ctx, t); + +damon_merge_two_regions (ctx, t, r, r2);KUNIT_EXPECT_EQ (test, r->ar.start,0ul );KUNIT_EXPECT_EQ (test, r->ar.end,300ul );KUNIT_EXPECT_EQ (test, r->nr_accesses,16u );
@@ -220,7 +236,7 @@ static void damon_test_merge_two(struct kunit *test) }KUNIT_EXPECT_EQ (test, i,1 ); -damon_free_target (t); +damon_destroy_ctx (ctx); }static struct damon_region *__nth_region_of (struct damon_target *t,int idx)
Signature · 1 line
2.47.3
SJ Park
maintainerPATCH v5 2/2 mm/damon/core: copy only installed probe in damon_split_region_at()
In reply to SJ ParkIn damon_split_region_at(), we copy the entire probe_hits[] and last_probe_hits[] arrays of DAMON_MAX_PROBES size. However it is possible that the number of probes installed is less than DAMON_MAX_PROBES, in which case one or more iterations is/are redundant. To remedy this, iterate over only as many times as the number of installed probes. To do so, accept a struct damon_ctx pointer in damon_split_region_at(), and update all invocations of the function to pass the new argument.
Also update damon_test_split_at() and damos_test_filter_out() in core-kunit.h to supply a context to function paths that call damon_split_region_at().
--- mm/damon/core.c | 42 +++++++++++++----------- mm/damon/tests/core-kunit.h | 64 +++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 28 deletions(-)
mm/damon/core.c +24 -18
@@ -2046,8 +2046,8 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)return sz; } -static int damon_split_region_at (struct damon_target *t, -struct damon_region *r,unsigned long sz_r); +static int damon_split_region_at (struct damon_ctx *ctx,struct damon_target *t, +struct damon_region *r,unsigned long sz_r);/* * damon_apply_min_nr_regions() - Make effect of min_nr_regions parameter.
@@ -2072,7 +2072,7 @@ static unsigned long damon_apply_min_nr_regions(struct damon_ctx *ctx)damon_for_each_target (t, ctx) {damon_for_each_region_safe (r, next, t) {while (damon_sz_region (r) > max_region_sz) { -if (damon_split_region_at (t, r, max_region_sz)) +if (damon_split_region_at (ctx, t, r, max_region_sz)) goto out; r =damon_next_region (r); }
@@ -2469,6 +2469,7 @@ static bool damos_valid_target(struct damon_ctx *c, struct damon_region *r,/* * damos_skip_charged_region() - Check if the given region or starting part of * it is already charged for the DAMOS quota. +* @ctx: The pointer to the context. * @t: The target of the region. * @rp: The pointer to the region. * @s: The scheme to be applied.
@@ -2491,8 +2492,8 @@ static bool damos_valid_target(struct damon_ctx *c, struct damon_region *r,* * Return: true if the region should be skipped, false otherwise. */ -static bool damos_skip_charged_region (struct damon_target *t, -struct damon_region *r,struct damos *s, +static bool damos_skip_charged_region (struct damon_ctx *ctx, +struct damon_target *t,struct damon_region *r,struct damos *s,unsigned long min_region_sz) {struct damos_quota *quota = &s->quota;
@@ -2520,7 +2521,7 @@ static bool damos_skip_charged_region(struct damon_target *t, } sz_to_skip = min_region_sz; } -damon_split_region_at (t, r, sz_to_skip); +damon_split_region_at (ctx, t, r, sz_to_skip); skip = true; } }
@@ -2579,13 +2580,13 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t, }/* start before the range and overlap */ if (r->ar.start < start) { - *err =damon_split_region_at (t, r, + *err =damon_split_region_at (ctx, t, r, start - r->ar.start); matched = false;break ; }/* start inside the range */ - *err =damon_split_region_at (t, r, end - r->ar.start); + *err =damon_split_region_at (ctx, t, r, end - r->ar.start); matched = true;break ;case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM :
@@ -2784,7 +2785,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, c->min_region_sz);if (!sz) goto update_stat; -if (damon_split_region_at (t, r, sz)) +if (damon_split_region_at (c, t, r, sz)) goto update_stat; }if (damos_core_filter_out (c, t, r, s))
@@ -2831,7 +2832,7 @@ static void damon_do_apply_schemes(struct damon_ctx *c,if (damos_quota_is_full (quota, c->min_region_sz))continue ; -if (damos_skip_charged_region (t, r, s, c->min_region_sz)) +if (damos_skip_charged_region (c, t, r, s, c->min_region_sz))continue ; if (s->max_nr_snapshots &&
@@ -3690,15 +3691,18 @@ static void damon_verify_split_region_at(struct damon_region *r,/* * Split a region in two * +* ctx pointer to the region's context * r the region to be split * sz_r size of the first sub-region that will be made * * Return: 0 on success, negative error code otherwise. */ -static int damon_split_region_at (struct damon_target *t, -struct damon_region *r,unsigned long sz_r) +static int damon_split_region_at (struct damon_ctx *ctx,struct damon_target *t, +struct damon_region *r,unsigned long sz_r) {struct damon_region *new; +struct damon_probe *p; +int i;damon_verify_split_region_at (r, sz_r); new =damon_new_region (r->ar.start + sz_r, r->ar.end);
@@ -3710,11 +3714,13 @@ static int damon_split_region_at(struct damon_target *t, new->age = r->age; new->last_nr_accesses = r->last_nr_accesses; new->nr_accesses = r->nr_accesses; -/* todo: do this for only installed probes */ -memcpy (new->probe_hits, r->probe_hits,sizeof (r->probe_hits)); -memcpy (new->last_probe_hits, r->last_probe_hits, -sizeof (r->last_probe_hits)); + i =0 ; +damon_for_each_probe (p, ctx) { + new->probe_hits[i] = r->probe_hits[i]; + new->last_probe_hits[i] = r->last_probe_hits[i]; + ++i; + }damon_insert_region (new, r,damon_next_region (r), t);return 0 ; }
@@ -3743,7 +3749,7 @@ static void damon_split_regions_of(struct damon_ctx *ctx,if (sz_sub ==0 || sz_sub >= sz_region)continue ; -damon_split_region_at (t, r, sz_sub); +damon_split_region_at (ctx, t, r, sz_sub); sz_region = sz_sub; } }
@@ -3776,7 +3782,7 @@ static void damon_split_some_regions(struct damon_ctx *ctx,if (sz_sub ==0 || sz_sub >= sz_region)continue ; -damon_split_region_at (t, r, sz_sub); +damon_split_region_at (ctx, t, r, sz_sub); } } }
mm/damon/tests/core-kunit.h +54 -10
148 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH v5 1/2] mm/damon/core: use only installed probe in damon_merge_two_regions()
In reply to SJ ParkSashiko has reviewed this patch and found no issues.
Quoted 11 lines
From: Suhaas Joshi <[email protected]>
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the
DAMON_MAX_PROBESmacro. However, it is possible that we have fewer probes installed thanDAMON_MAX_PROBES. In such cases, we end up making redundant iterations. Therefore, to remedy this, iterate over the list of installed probes instead of iterating over the entire array. For doing this, start accepting a structdamon_ctxindamon_merge_two_regions(), and update calling functions to pass this argument.
Quoted 6 lines
Update the
damon_test_merge_two()test to use this new signature fordamon_merge_two_regions()as well.Signed-off-by Suhaas Joshi <[email protected]>Reviewed-by SJ Park <[email protected]>Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v5 2/2] mm/damon/core: copy only installed probe in damon_split_region_at()
In reply to SJ ParkSashiko has reviewed this patch and found no issues.
Quoted 8 lines
In
damon_split_region_at(), we copy the entire probe_hits[] and last_probe_hits[] arrays ofDAMON_MAX_PROBESsize. However it is possible that the number of probes installed is less thanDAMON_MAX_PROBES, in which case one or more iterations is/are redundant. To remedy this, iterate over only as many times as the number of installed probes. To do so, accept a structdamon_ctxpointer indamon_split_region_at(), and update all invocations of the function to pass the new argument.
Quoted 7 lines
Also update
damon_test_split_at()anddamos_test_filter_out()incore-kunit.hto supply a context to function paths that calldamon_split_region_at().Signed-off-by Suhaas Joshi <[email protected]>Reviewed-by SJ Park <[email protected]>Signed-off-by SJ Park <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2