mm/damon/core: Iterate over installed probes instead of DAMON_MAX_PROBES
Subsystems (2)
cover mm/damon/core: Iterate over installed probes instead of DAMON_MAX_PROBES
Hello,
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.
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.
Regards
Suhaas Joshi
Suhaas Joshi (2):
mm/damon/core: Use only installed probe in damon_merge_two_regions() mm/damon/core: Copy only installed probe in damon_split_region_at()
mm/damon/core.c | 56 ++++++++++++++++++------------- mm/damon/tests/core-kunit.h | 67 +++++++++++++++++++++++++++++++------ 2 files changed, 89 insertions(+), 34 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/2 mm/damon/core: Use only installed probe in damon_merge_two_regions()
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the DAMON_MAX_PROBES macro. It is possible, however, 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 | 14 +++++++++----- mm/damon/tests/core-kunit.h | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-)
mm/damon/core.c +9 -5
@@ -3499,20 +3499,24 @@ 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); }
@@ -3565,7 +3569,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 +17 -2
@@ -182,13 +182,27 @@ 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; + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); t =damon_new_target (); -if (!t) +if (!t) { +damon_destroy_ctx (ctx);kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 );if (!r) { +damon_destroy_ctx (ctx);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }
@@ -206,7 +220,7 @@ static void damon_test_merge_two(struct kunit *test) r2->age =21 ;damon_add_region (r2, t); -damon_merge_two_regions (t, r, r2); +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,6 +234,7 @@ static void damon_test_merge_two(struct kunit *test) }KUNIT_EXPECT_EQ (test, i,1 ); +damon_destroy_ctx (ctx);damon_free_target (t); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 1/2] mm/damon/core: Use only installed probe in damon_merge_two_regions()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mm: memory leak of ctx on r2 allocation failure in damon_test_merge_two() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mm: memory leak of ctx on r2 allocation failure in damon_test_merge_two() --
commit ade902b9c0636f895031124ead4ff579818b8169
Author: Suhaas Joshi <[email protected]>
mm/damon/core: Use only installed probe in damon_merge_two_regions()
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the DAMON_MAX_PROBES macro. It is possible, however, 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.
Quoted 23 lines
mm/damon/tests/core-kunit.h +11
-1@@ -182,13 +182,27 @@ 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; + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); t =damon_new_target (); -if (!t)
mm/damon/tests/core-kunit.h static void damon_test_merge_two(struct kunit *test) · line 208
Quoted 10 lines
mm/damon/tests/core-kunit.h +4
-0+if (!t) { +damon_destroy_ctx (ctx);kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 );if (!r) { +damon_destroy_ctx (ctx);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }
[Severity: Medium]
Is there a missing call to damon_destroy_ctx(ctx) if the allocation for the second region (r2) fails?
Looking at damon_test_merge_two(), if r2 fails to allocate:
r2 = damon_new_region(100, 300);
if (!r2) {
damon_free_target(t);
kunit_skip(test, "second region alloc fail");
}Does this code leak the newly allocated ctx and its contained probe on this error path?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 1/2 mm/damon/core: Use only installed probe in damon_merge_two_regions()
In reply to Suhaas JoshiHi Suhaas,
Thank you for your patches series!
Quoted 3 lines · On Wed, 07 Oct 2026 21:18:45 +0530 Suhaas Joshi <[email protected]> wrote:
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the
DAMON_MAX_PROBESmacro. It is possible, however, that we have fewer probes installed thanDAMON_MAX_PROBES. In such
I find this a bit hard to read. Could we change it to this?
"However, it is possible that we have fewer probes installed than DAMON_MAX_PROBES."
Quoted 109 lines
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_ctxindamon_merge_two_regions(), and update calling functions to pass this argument.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]>--- mm/damon/core.c | 14 +++++++++----- mm/damon/tests/core-kunit.h | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-)mm/damon/core.c +9
-5@@ -3499,20 +3499,24 @@ 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); }@@ -3565,7 +3569,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 +17
-2@@ -182,13 +182,27 @@ 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; + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); t =damon_new_target (); -if (!t) +if (!t) { +damon_destroy_ctx (ctx);kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 );if (!r) { +damon_destroy_ctx (ctx);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }@@ -206,7 +220,7 @@ static void damon_test_merge_two(struct kunit *test) r2->age =21 ;damon_add_region (r2, t); -damon_merge_two_regions (t, r, r2); +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,6 +234,7 @@ static void damon_test_merge_two(struct kunit *test) }KUNIT_EXPECT_EQ (test, i,1 ); +damon_destroy_ctx (ctx);damon_free_target (t); }Signature · 1 line
2.55.0
Other than the commit message I just mentioned, and the issue reported
[1] by Sashiko, LGTM.
[1] https://lore.kernel.org/damon/[email protected]
Best regards,
Rui Yan
SJ Park
maintainerRe: [PATCH 1/2] mm/damon/core: Use only installed probe in damon_merge_two_regions()
In reply to Suhaas JoshiHello Suhaas,
Quoted 11 lines · On Wed, 7 Oct 2026 21:18:45 +0530 Suhaas Joshi <[email protected]> wrote:
While merging 2 regions, we iterate over the entire probe_hits[] array, whose size is determined by the
DAMON_MAX_PROBESmacro. It is possible, however, 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.Update the
damon_test_merge_two()test to use this new signature fordamon_merge_two_regions()as well.
Thank you for this patch.
[...]
Quoted 32 lines
mm/damon/tests/core-kunit.h +15
-1@@ -182,13 +182,27 @@ 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; + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); t =damon_new_target (); -if (!t) +if (!t) { +damon_destroy_ctx (ctx);kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 );if (!r) { +damon_destroy_ctx (ctx);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }
mm/damon/tests/core-kunit.h static void damon_test_merge_two(struct kunit *test) · line 221
Quoted 2 lines
mm/damon/tests/core-kunit.h +0
-0@@ -206,7 +220,7 @@ static void damon_test_merge_two(struct kunit *test) r2->age =21 ;damon_add_region (r2, t);
As also found [1] by Sashiko, seems this patch mistakenly not calling damon_destroy_ctx() for r2 allocation failure. Could you please add that?
[1] https://lore.kernel.org/[email protected]
Thanks,
SJ
PATCH 2/2 mm/damon/core: Copy only installed probe in damon_split_region_at()
In reply to Suhaas JoshiIn 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 | 48 ++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 27 deletions(-)
mm/damon/core.c +23 -19
@@ -2049,8 +2049,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.
@@ -2075,7 +2075,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); }
@@ -2494,9 +2494,9 @@ 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, -unsigned long min_region_sz) +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;unsigned long sz_to_skip;
@@ -2523,7 +2523,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; } }
@@ -2581,12 +2581,12 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t, }/* start before the range and overlap */ if (r->ar.start < start) { -damon_split_region_at (t, r, start - r->ar.start); +damon_split_region_at (ctx, t, r, start - r->ar.start); matched = false;break ; }/* start inside the range */ -damon_split_region_at (t, r, end - r->ar.start); +damon_split_region_at (ctx, t, r, end - r->ar.start); matched = true;break ;case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM :
@@ -2779,7 +2779,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))
@@ -2826,7 +2826,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 &&
@@ -3643,10 +3643,12 @@ static void damon_verify_split_region_at(struct damon_region *r,* * 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);
@@ -3658,11 +3660,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 ; }
@@ -3691,7 +3695,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; } }
@@ -3723,7 +3727,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 +40 -8
124 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/2] mm/damon/core: Copy only installed probe in damon_split_region_at()
Sashiko has reviewed this patch and found no issues. It looks great!
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 5 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]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 2/2 mm/damon/core: Copy only installed probe in damon_split_region_at()
In reply to Suhaas JoshiQuoted 285 lines · On Wed, 07 Oct 2026 21:18:46 +0530 Suhaas Joshi <[email protected]> wrote:
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.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]>--- mm/damon/core.c | 42 +++++++++++++++++--------------- mm/damon/tests/core-kunit.h | 48 ++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 27 deletions(-)mm/damon/core.c +23
-19@@ -2049,8 +2049,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. @@ -2075,7 +2075,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); }@@ -2494,9 +2494,9 @@ 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, -unsigned long min_region_sz) +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;unsigned long sz_to_skip;@@ -2523,7 +2523,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; } }@@ -2581,12 +2581,12 @@ static bool damos_filter_match(struct damon_ctx *ctx, struct damon_target *t, }/* start before the range and overlap */ if (r->ar.start < start) { -damon_split_region_at (t, r, start - r->ar.start); +damon_split_region_at (ctx, t, r, start - r->ar.start); matched = false;break ; }/* start inside the range */ -damon_split_region_at (t, r, end - r->ar.start); +damon_split_region_at (ctx, t, r, end - r->ar.start); matched = true;break ;case DAMOS_FILTER_TYPE_PROBE_HITS_WSUM :@@ -2779,7 +2779,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))@@ -2826,7 +2826,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 &&@@ -3643,10 +3643,12 @@ static void damon_verify_split_region_at(struct damon_region *r,* * 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);@@ -3658,11 +3660,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 ; }@@ -3691,7 +3695,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; } }@@ -3723,7 +3727,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 +40
-8@@ -141,12 +141,26 @@ static void damon_test_split_at(struct kunit *test) {struct damon_target *t;struct damon_region *r, *r_new; +struct damon_ctx *ctx; +struct damon_probe *p; + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); t =damon_new_target (); -if (!t) +if (!t) { +damon_destroy_ctx (ctx);kunit_skip (test,"target alloc fail" ); + } r =damon_new_region (0 ,100 );if (!r) { +damon_destroy_ctx (ctx);damon_free_target (t);kunit_skip (test,"region alloc fail" ); }@@ -156,7 +170,7 @@ static void damon_test_split_at(struct kunit *test) r->last_probe_hits[0 ] =3 ; r->age =10 ;damon_add_region (r, t); -damon_split_region_at (t, r,25 ); +damon_split_region_at (ctx, t, r,25 );KUNIT_EXPECT_EQ (test,damon_nr_regions (t),2 );if (damon_nr_regions (t) !=2 ) goto out;@@ -176,6 +190,7 @@ static void damon_test_split_at(struct kunit *test) out:damon_free_target (t); +damon_destroy_ctx (ctx); }static void damon_test_merge_two (struct kunit *test)@@ -1648,19 +1663,35 @@ static void damos_test_filter_out(struct kunit *test)struct damon_target *t;struct damon_region *r, *r2;struct damos_filter *f; +struct damon_ctx *ctx; +struct damon_probe *p; + + p =damon_new_probe (); +if (!p) +kunit_skip (test,"probe alloc fail" ); + ctx =damon_new_ctx (); +if (!ctx) { +damon_destroy_probe (p); +kunit_skip (test,"context alloc fail" ); + } +damon_add_probe (ctx, p); f =damos_new_filter (DAMOS_FILTER_TYPE_ADDR , true, false); -if (!f) +if (!f) { +damon_destroy_ctx (ctx);kunit_skip (test,"filter alloc fail" ); + } f->addr_range = (struct damon_addr_range ){.start =2 , .end =6 }; t =damon_new_target ();if (!t) { +damon_destroy_ctx (ctx);damos_destroy_filter (f);kunit_skip (test,"target alloc fail" ); } r =damon_new_region (3 ,5 );if (!r) { +damon_destroy_ctx (ctx);damos_destroy_filter (f);damon_free_target (t);kunit_skip (test,"region alloc fail" );@@ -1668,27 +1699,27 @@ static void damos_test_filter_out(struct kunit *test)damon_add_region (r, t);/* region in the range */ -KUNIT_EXPECT_TRUE (test,damos_filter_match (NULL , t, r, f,1 )); +KUNIT_EXPECT_TRUE (test,damos_filter_match (ctx, t, r, f,1 ));KUNIT_EXPECT_EQ (test,damon_nr_regions (t),1 );/* region before the range */ r->ar.start =1 ; r->ar.end =2 ;KUNIT_EXPECT_FALSE (test, -damos_filter_match (NULL , t, r, f,1 )); +damos_filter_match (ctx, t, r, f,1 ));KUNIT_EXPECT_EQ (test,damon_nr_regions (t),1 );/* region after the range */ r->ar.start =6 ; r->ar.end =8 ;KUNIT_EXPECT_FALSE (test, -damos_filter_match (NULL , t, r, f,1 )); +damos_filter_match (ctx, t, r, f,1 ));KUNIT_EXPECT_EQ (test,damon_nr_regions (t),1 );/* region started before the range */ r->ar.start =1 ; r->ar.end =4 ; -KUNIT_EXPECT_FALSE (test,damos_filter_match (NULL , t, r, f,1 )); +KUNIT_EXPECT_FALSE (test,damos_filter_match (ctx, t, r, f,1 ));/* filter should have split the region */ KUNIT_EXPECT_EQ (test, r->ar.start,1 );KUNIT_EXPECT_EQ (test, r->ar.end,2 );@@ -1704,7 +1735,7 @@ static void damos_test_filter_out(struct kunit *test) r->ar.start =2 ; r->ar.end =8 ;KUNIT_EXPECT_TRUE (test, -damos_filter_match (NULL , t, r, f,1 )); +damos_filter_match (ctx, t, r, f,1 ));/* filter should have split the region */ KUNIT_EXPECT_EQ (test, r->ar.start,2 );KUNIT_EXPECT_EQ (test, r->ar.end,6 );@@ -1717,6 +1748,7 @@ static void damos_test_filter_out(struct kunit *test)damon_destroy_region (r2, t); out: +damon_destroy_ctx (ctx);damon_free_target (t);damos_free_filter (f); }Signature · 1 line
2.55.0
Thank you for your patches! LGTM.
Best regards,
Rui Yan
SJ Park
maintainerRe: [PATCH 2/2] mm/damon/core: Copy only installed probe in damon_split_region_at()
In reply to Suhaas JoshiHello Suhaas,
From the next time, please send a patch series as a single thread, with a cover letter.
Quoted 23 lines · On Wed, 7 Oct 2026 21:18:46 +0530 Suhaas Joshi <[email protected]> wrote:
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.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]>--- mm/damon/core.c | 42 +++++++++++++++++--------------- mm/damon/tests/core-kunit.h | 48 ++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 27 deletions(-)mm/damon/core.c +0
-0
mm/damon/core.c static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) · line 2053
Quoted 7 lines
mm/damon/core.c +2
-2@@ -2049,8 +2049,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);
Please wrap lines for 80 columns [1] limit per line. Same for below.
[...]
mm/damon/core.c static bool damos_valid_target(struct damon_ctx *c, struct damon_region *r, · line 2499
Quoted 10 lines
mm/damon/core.c +3
-3@@ -2494,9 +2494,9 @@ 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, -unsigned long min_region_sz) +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)
Please update the comment above to document the new parameter.
[...]
mm/damon/core.c static void damon_verify_split_region_at(struct damon_region *r, · line 3647
Quoted 8 lines
mm/damon/core.c +2
-2@@ -3643,10 +3643,12 @@ static void damon_verify_split_region_at(struct damon_region *r,* * 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)
Ditto.
[...]
[1] https://docs.kernel.org/process/coding-style.html#breaking-long-lines-and-strings
Thanks,
SJ