From nobody Sun May 24 21:37:01 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6452A20125F for ; Thu, 21 May 2026 04:53:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779339183; cv=none; b=U/E79y+AduJ9fuWArmQVKQ9qlcxHO4NsCtAutY1e4PGEubZ8ziEPSkkkbMT5leUSfO7CdglGRX2SlO0UJMDFl4Xw7/eqknZZA8T432TuQ2RdghpBcEP8pshXEvTatJC5BzQa3ssLNn18CCO/pgf0tDT1yARrdZ7pMAm8bf1q/9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779339183; c=relaxed/simple; bh=BBIRbu7OFErK/KKz4QUAx+oIApZCly2X+pCMd+7ysNU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EEQd0xgqfQuXILoWqPa9dcugYeQmKj8f4HnAe9pp05h4ZVZ91YDQHcyr5ZmoMm1qnPsWDTuVXU2kv32gx4mBAKYNc5nPx7pG3TUTLgFDQObT+GrX9lFnWRpizAk7DQvkxm5DdU4t/0jMPSegDOFLpqzZeHKipa5efU7hXRLEMu8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=nYn1BTHX; arc=none smtp.client-ip=91.218.175.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="nYn1BTHX" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779339179; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ihrvNaIXgM84SacKpF3s4jv+T6hZY9k+429hTF/wmxo=; b=nYn1BTHXkkqEjDgxljj9bDe0WsfSvO90O7O5eLgE5ADHDmQIk0G1lF9ndyAgb4SBD+BF/0 AWZSrY5kzkRHp1hpEAMdArAoK4YUnIbPi6iUYhVBqHh8mhGwflzQ3Ef9TwvzZumdHX62aM ZKTUcyi7PoolOGHSSVAvyCCdP+DFgtY= From: Jiayuan Chen To: damon@lists.linux.dev Cc: Jiayuan Chen , Jiayuan Chen , SeongJae Park , Andrew Morton , Shu Anzai , Quanmin Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] mm/damon/core: split age==0 regions when nr_regions exceeds max/2 Date: Thu, 21 May 2026 12:52:23 +0800 Message-ID: <20260521045236.115749-2-jiayuan.chen@linux.dev> In-Reply-To: <20260521045236.115749-1-jiayuan.chen@linux.dev> References: <20260521045236.115749-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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 =3D=3D 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 =3D 800 > max_nr_regions / 2 =3D 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 =3D=3D 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, } =20 /* - * Split every target region into randomly-sized small regions + * Split each region whose access pattern has just changed (age =3D=3D 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 !=3D 0) + continue; + sz_region =3D damon_sz_region(r); + if (sz_region < 2 * ctx->min_region_sz) + continue; + + sz_sub =3D ALIGN_DOWN(damon_rand(ctx, 1, 10) * + sz_region / 10, ctx->min_region_sz); + /* Do not allow blank region */ + if (sz_sub =3D=3D 0 || sz_sub >=3D 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 region= s 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 reve= rt - * it. + * When the total region count leaves room for a blanket doubling + * (nr_regions <=3D max_nr_regions / 2), every region is randomly split. + * Otherwise, only regions whose access pattern has just changed + * (age =3D=3D 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 =3D 0; - static unsigned int last_nr_regions; + unsigned long nr_regions =3D 0; + unsigned long max_nr_regions =3D ctx->attrs.max_nr_regions; + static unsigned long last_nr_regions; int nr_subregions =3D 2; =20 damon_for_each_target(t, ctx) nr_regions +=3D damon_nr_regions(t); =20 - if (nr_regions > ctx->attrs.max_nr_regions / 2) - return; + if (nr_regions >=3D 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; + } =20 /* Maybe the middle of the region has different access frequency */ if (last_nr_regions =3D=3D nr_regions && - nr_regions < ctx->attrs.max_nr_regions / 3) + nr_regions < max_nr_regions / 3) nr_subregions =3D 3; =20 damon_for_each_target(t, ctx) damon_split_regions_of(ctx, t, nr_subregions, ctx->min_region_sz); =20 +done: last_nr_regions =3D nr_regions; } =20 --=20 2.43.0 From nobody Sun May 24 21:37:01 2026 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD4843630A3 for ; Thu, 21 May 2026 04:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779339189; cv=none; b=jnrcqtIg2+2dTVGy5haZATEr/gPZLhDLjesgfUSSGj5lNsAPnDifvcYmogKd+39gvq3lOwZxC9zip5todnkZkSciHQkfeYkzKHt2CjkW0bEBWYzQQiH2j6SUUqHWS6QTyco2Z3rXCcspWpWgLpzCE3zNyYj01PuX27rlMgC7cLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779339189; c=relaxed/simple; bh=w+qUK/n2Ff+mKSdAKzNJS9/pHdTTj2lLuwe8izNLXCs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pg+4rMht9IquGdVHDO2BIvIgqQhKaPg07kRgM9DqGLr18MJjYRMxwLadOZsGTYHM59uk+Nz/KuyRNV3b2ig50RM6SZSZnnSfmSXJyjB12V2RX960WqtPO2icTkoxvHe/I4T0Wy8jE1cXl61KLfMHYum3SPlA2X7BiMON5hU2TDs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=JdSL8oC2; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="JdSL8oC2" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779339185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+TMZKInof45y0i4jsA/uEMDRZe595ToXMa24DqFXerQ=; b=JdSL8oC26JhfCkCCLDzXIJ2jl/dNNqteRDB5gXeL3AWCvNAMIEyK/G1dyix+KjY0Lg1CkF ki0FsJpjVjXfF4ojbNeIALSxNdTytyEasbe+8MA7g0EQi/1cOh8UnhYqFvs/UM0qlMOIRE EmxDuyGyWPLTGvcj30/Y9Wk6pOiUgOo= From: Jiayuan Chen To: damon@lists.linux.dev Cc: Jiayuan Chen , Jiayuan Chen , SeongJae Park , Andrew Morton , Shu Anzai , Quanmin Yan , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] mm/damon/tests/core-kunit: test split above max_nr_regions/2 Date: Thu, 21 May 2026 12:52:24 +0800 Message-ID: <20260521045236.115749-3-jiayuan.chen@linux.dev> In-Reply-To: <20260521045236.115749-1-jiayuan.chen@linux.dev> References: <20260521045236.115749-1-jiayuan.chen@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" 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); } =20 +/* + * 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 =3D 799; + const unsigned long small_sz =3D 10; + const unsigned long big_sz =3D 1000000; + + c =3D damon_new_ctx(); + if (!c) + kunit_skip(test, "ctx alloc fail"); + + c->attrs.min_nr_regions =3D 10; + c->attrs.max_nr_regions =3D 1500; + + t =3D damon_new_target(); + if (!t) { + damon_destroy_ctx(c); + kunit_skip(test, "target alloc fail"); + } + + for (i =3D 0; i < nr_small; i++) { + start =3D i * small_sz; + r =3D 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 =3D (i & 1) ? 0 : 100; + r->age =3D 5; + damon_add_region(r, t); + } + + start =3D nr_small * small_sz; + big =3D 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 =3D 50; + damon_add_region(big, t); + + damon_add_target(c, t); + + nr_before =3D damon_nr_regions(t); + KUNIT_EXPECT_GT(test, (unsigned long)nr_before, + c->attrs.max_nr_regions / 2); + + kdamond_split_regions(c); + + nr_after =3D 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 =3D damon_new_ctx(); @@ -1468,6 +1537,7 @@ static struct kunit_case damon_test_cases[] =3D { 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), --=20 2.43.0