From: Jiayuan Chen kdamond_split_regions() returns early when nr_regions is above max_nr_regions / 2, leaving internal access variation inside a large region undetected. Such a layout is common with damon-paddr on hugepage workloads or damon-vaddr on processes with a large anonymous mmap. For example, with max_nr_regions == 1500, a target may end up with 799 small alternating-temperature regions plus one large region that absorbed a uniformly-accessed range during an earlier merge: H:hot C:cold r1 r2 r3 r800 HHHHHH|CCCCCC|HHHHHH|...|HHHHHH..........................| nr_regions = 800 > max_nr_regions / 2 = 750 If a cold subarea later emerges inside r800: r1 r2 r3 r800 HHHHHH|CCCCCC|HHHHHH|...|HHHHHH........CCCCCC.............| The small regions cannot merge with each other (different access counts), so the budget stays full. r800 cannot be split because nr_regions > max_nr_regions / 2 causes an early return. The cold subarea is never discovered. Split regions whose access pattern has just changed (age == 0) on this path, up to the remaining budget against max_nr_regions. An unnecessary split is reverted by the next kdamond_merge_regions(). Cc: Jiayuan Chen Signed-off-by: Jiayuan Chen --- mm/damon/core.c | 68 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 6b8af7f956b7..442a6c323aeb 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -3452,37 +3452,81 @@ static void damon_split_regions_of(struct damon_ctx *ctx, } /* - * Split every target region into randomly-sized small regions + * Split each region whose access pattern has just changed (age == 0) + * into two, until @budget new regions have been produced or no eligible + * region remains. + */ +static void damon_split_zero_age_regions(struct damon_ctx *ctx, + unsigned long budget) +{ + struct damon_target *t; + struct damon_region *r, *next; + + damon_for_each_target(t, ctx) { + damon_for_each_region_safe(r, next, t) { + unsigned long sz_region, sz_sub; + + if (!budget) + return; + if (r->age != 0) + continue; + sz_region = damon_sz_region(r); + if (sz_region < 2 * ctx->min_region_sz) + continue; + + sz_sub = ALIGN_DOWN(damon_rand(ctx, 1, 10) * + sz_region / 10, ctx->min_region_sz); + /* Do not allow blank region */ + if (sz_sub == 0 || sz_sub >= sz_region) + continue; + + damon_split_region_at(t, r, sz_sub); + budget--; + } + } +} + +/* + * Split target regions to refine the monitoring resolution under + * dynamically changing access patterns. * - * This function splits every target region into random-sized small regions if - * current total number of the regions is equal or smaller than half of the - * user-specified maximum number of regions. This is for maximizing the - * monitoring accuracy under the dynamically changeable access patterns. If a - * split was unnecessarily made, later 'kdamond_merge_regions()' will revert - * it. + * When the total region count leaves room for a blanket doubling + * (nr_regions <= max_nr_regions / 2), every region is randomly split. + * Otherwise, only regions whose access pattern has just changed + * (age == 0) are split, up to the remaining budget against + * max_nr_regions. + * + * Unnecessary splits are reverted by a later kdamond_merge_regions(). */ static void kdamond_split_regions(struct damon_ctx *ctx) { struct damon_target *t; - unsigned int nr_regions = 0; - static unsigned int last_nr_regions; + unsigned long nr_regions = 0; + unsigned long max_nr_regions = ctx->attrs.max_nr_regions; + static unsigned long last_nr_regions; int nr_subregions = 2; damon_for_each_target(t, ctx) nr_regions += damon_nr_regions(t); - if (nr_regions > ctx->attrs.max_nr_regions / 2) - return; + if (nr_regions >= max_nr_regions) + goto done; + + if (nr_regions > max_nr_regions / 2) { + damon_split_zero_age_regions(ctx, max_nr_regions - nr_regions); + goto done; + } /* Maybe the middle of the region has different access frequency */ if (last_nr_regions == nr_regions && - nr_regions < ctx->attrs.max_nr_regions / 3) + nr_regions < max_nr_regions / 3) nr_subregions = 3; damon_for_each_target(t, ctx) damon_split_regions_of(ctx, t, nr_subregions, ctx->min_region_sz); +done: last_nr_regions = nr_regions; } -- 2.43.0 From: Jiayuan Chen Add a test that exercises kdamond_split_regions() when the total region count is already above max_nr_regions / 2, asserting that the function can still produce new regions and does not overshoot the limit. All tests pass: damon: pass:29 fail:0 skip:0 total:29 Totals: pass:29 fail:0 skip:0 total:29 Cc: Jiayuan Chen Signed-off-by: Jiayuan Chen --- mm/damon/tests/core-kunit.h | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h index 1cfb8c176b87..6b2439670049 100644 --- a/mm/damon/tests/core-kunit.h +++ b/mm/damon/tests/core-kunit.h @@ -339,6 +339,75 @@ static void damon_test_split_regions_of(struct kunit *test) damon_destroy_ctx(c); } +/* + * kdamond_split_regions() must still be able to make progress when the + * total region count is above max_nr_regions / 2, as long as there is + * unused budget and at least one region whose access pattern has just + * changed. + */ +static void damon_test_split_above_half_progresses(struct kunit *test) +{ + struct damon_ctx *c; + struct damon_target *t; + struct damon_region *r, *big; + unsigned long start; + unsigned int nr_before, nr_after, i; + const unsigned int nr_small = 799; + const unsigned long small_sz = 10; + const unsigned long big_sz = 1000000; + + c = damon_new_ctx(); + if (!c) + kunit_skip(test, "ctx alloc fail"); + + c->attrs.min_nr_regions = 10; + c->attrs.max_nr_regions = 1500; + + t = damon_new_target(); + if (!t) { + damon_destroy_ctx(c); + kunit_skip(test, "target alloc fail"); + } + + for (i = 0; i < nr_small; i++) { + start = i * small_sz; + r = damon_new_region(start, start + small_sz); + if (!r) { + damon_free_target(t); + damon_destroy_ctx(c); + kunit_skip(test, "region alloc fail"); + } + r->nr_accesses = (i & 1) ? 0 : 100; + r->age = 5; + damon_add_region(r, t); + } + + start = nr_small * small_sz; + big = damon_new_region(start, start + big_sz); + if (!big) { + damon_free_target(t); + damon_destroy_ctx(c); + kunit_skip(test, "big region alloc fail"); + } + big->nr_accesses = 50; + damon_add_region(big, t); + + damon_add_target(c, t); + + nr_before = damon_nr_regions(t); + KUNIT_EXPECT_GT(test, (unsigned long)nr_before, + c->attrs.max_nr_regions / 2); + + kdamond_split_regions(c); + + nr_after = damon_nr_regions(t); + KUNIT_EXPECT_GT(test, nr_after, nr_before); + KUNIT_EXPECT_LE(test, (unsigned long)nr_after, + c->attrs.max_nr_regions); + + damon_destroy_ctx(c); +} + static void damon_test_ops_registration(struct kunit *test) { struct damon_ctx *c = damon_new_ctx(); @@ -1468,6 +1537,7 @@ static struct kunit_case damon_test_cases[] = { KUNIT_CASE(damon_test_merge_two), KUNIT_CASE(damon_test_merge_regions_of), KUNIT_CASE(damon_test_split_regions_of), + KUNIT_CASE(damon_test_split_above_half_progresses), KUNIT_CASE(damon_test_ops_registration), KUNIT_CASE(damon_test_set_regions), KUNIT_CASE(damon_test_nr_accesses_to_accesses_bp), -- 2.43.0