From nobody Sat Jul 25 21:19:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 470ED437127; Mon, 13 Jul 2026 13:58:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951127; cv=none; b=njeQp3vYkYs2ioCL5g30YwRkda99aigHXQd7n0w2R/EMObqDKF4aKWFLBXME43C7NoDYvEjjRoJspGhLe/yypp2RIEVzN2IQUIbbd/3ILExFtDBLXapFO1hG0q03Bi48Q2MFdQLZZW0zkF84HCnwwj/or/MI8XhGFK3tdBCXiQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951127; c=relaxed/simple; bh=UvG3w3Q38JCvdgaSsgN+1fOmYYd1A7xQdvoEpH7rRxM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qut64Mb5T7Qsd0qdqtNPAn1NGC/pV85pQqjSoV2T2s9P2g3BqeKljlL/tU5sHBChgex+hcWfDQApVzO+outjPGSu4azzAHOqJopeuYaS6iX+YKavk5MmQTUfz1s36cIFTcoS4pQGlmROlz2RK66DnniiDC/6/ki4hCnxTdSEJ4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MEkO4cOG; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MEkO4cOG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00C461F00A3E; Mon, 13 Jul 2026 13:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783951126; bh=yFIQucAr4vFmOrW82XY64T561EcF6nkSnWV7DSiSLJg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MEkO4cOG07NJ7HKPJXu62PXT/o98eeK7TbKkTVT+LMqWbUKy8aZygB2ygoNst2sRj gzXjIFlJyFrWDM6dButP2m/w78beObSMeuDCcC6XBQN+wMUof0NDeVOkmTzOIjVsvX vda83iP5GNBiHr2EWkKqMacRAYB3/h7Skld5ia18TmOZJHI90UFAaZW8mFaQN1STnB Oh6nZjIWOrghkf2//6kGQBmZUUIjmUhpBCDv8RxOqziLQlsy0jcZN+1q2uTQorM0z1 hBb6oEjPFxvqOmnAsUZrNGVkxxl9jp56aoWwMgfZnCkNkSye/bJUUzrFwapcTh/0w+ /p3IkKRDDSeyA== From: SJ Park To: Cc: SJ Park , stable@vger.kernel.org, Andrew Morton , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1.2 1/5] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop Date: Mon, 13 Jul 2026 06:58:32 -0700 Message-ID: <20260713135838.32730-2-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713135838.32730-1-sj@kernel.org> References: <20260713135838.32730-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Due to online parameter update like events, the number of DAMON regions could be higher than the user-set upper limit. kdamond_merge_regions() repeats merge regions until the number meets the limit, while doubling the merge threshold up to the theoretical maximum threshold. It is tried only up to the theoretical maximum threshold because even the aggressive merging can fail from reducing the number of regions under the user-defined upper limit. For example, there could be many user-defined non-contiguous regions that cannot be merged. The threshold based loop break condition is evaluated by comparing the threshold for the next merging try against the theoretical maximum threshold. If max_thres is larger than UINT_MAX / 2, doubling the threshold could make it overflow, and bypass the loop break condition. In the case, if the number of regions cannot be reduced under the upper limit like explained above, the loop will run infinitely. Prevent the case by doing the break condition check before doubling the threshold. Also, prevent the threshold exceeding the maximum threshold, as it could overflow and apply the wrong merge threshold. This issue is unlikely to occur in real world, since having the max_thres higher than UINT_MAX / 2 require unrealistically large aggregation intervals compared to the sampling interval. Also, it requires an unrealistically large number of uncontiguous regions setup. Nonetheless, the consequence is bad and the fix is simple. The issue was discovered [1] by Sashiko. [1] https://lore.kernel.org/20260709145425.96247-1-sj@kernel.org Fixes: 310d6c15e910 ("mm/damon/core: merge regions aggressively when max_nr= _regions is unmet") Cc: # 6.10.x Signed-off-by: SJ Park --- mm/damon/core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 806a67d02a6e9..f3b6a46fdaabd 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -3369,15 +3369,20 @@ static void kdamond_merge_regions(struct damon_ctx = *c, unsigned int threshold, =20 max_thres =3D c->attrs.aggr_interval / (c->attrs.sample_interval ? c->attrs.sample_interval : 1); - do { + while (true) { nr_regions =3D 0; damon_for_each_target(t, c) { damon_merge_regions_of(t, threshold, sz_limit, c); nr_regions +=3D damon_nr_regions(t); } - threshold =3D max(1, threshold * 2); - } while (nr_regions > c->attrs.max_nr_regions && - threshold / 2 < max_thres); + if (nr_regions <=3D c->attrs.max_nr_regions || + max_thres <=3D threshold) + break; + if (threshold < max_thres / 2) + threshold =3D max(1, threshold * 2); + else + threshold =3D max_thres; + } } =20 #ifdef CONFIG_DAMON_DEBUG_SANITY --=20 2.47.3 From nobody Sat Jul 25 21:19:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 C5DC3437107; Mon, 13 Jul 2026 13:58:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951127; cv=none; b=OkPsyd3WeZpeD4V8CZAPuhuMSTu1sWYwLoURgYJqnQ15hqHIMJNDO4D8SMfevsN6l2Pw9EDP5Idip0IQ86knA/26WlYHokMUBCMoJ5VxcYEmOSNqqFSRI9SeGCcxxne+Tf9xWaklISRJzN/cqaBK1I3Nfs+e8mIF5O9EEYD+Iiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951127; c=relaxed/simple; bh=2YTUi5fssLwguc0tdmj+L1Qs0tTe4BqhfdbB6oOiClA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q3022A0Kp4m9MlG3xSV+V9jEgNRESRjYVlj4mJ0w7zJ8De+xyQfUKFl/szlQfaKBwC5P6s0PfPusX/pgMuXFbMO4RNrnJN03pxJ6qv8VZeBZCginIJZJ6qu5Lo7Q7C+tuIXCq8C095ccBL782nDas+b3f5Q7I6Au5KCxCbdv538= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LQ/zMYiy; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LQ/zMYiy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5565E1F00A3D; Mon, 13 Jul 2026 13:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783951126; bh=SKLq7ZuoHq2BFm176pu4DfNEYfPU8F0EbpgzHa8ZXlA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LQ/zMYiyDc9DBS+2PK4RjZYTpHzO2uSG2unKv5GwCgnDUqdMiXhJTqI0CAFZAa/3P 1VHFTEGSTxLynIIT943yFiCNPlZs2qYLlkPj5Lrfw3eveLRGKadq+v8/xT17JP2OpH L82xYFFVjkghxdfAMdHlpQzTJEh9RWCz2fVa3BBJsjN/WjDZR49JoYSQ71YyYsSRaF RDGRPLh6A1tTvWXaOQvf8hecqWIQ21JwhI3GXDDS4vJnPKRN/RLhDiRbTNbz5gq3Eq nd1bC5m5KLvePmsUtrEd19vJxmscIWClZ9+2ZPN9UuOnMDRbSCD/38I76b4JqnZrzo bKvk5Ooi9Yn2A== From: SJ Park To: Cc: SJ Park , stable@vger.kernel.org, Andrew Morton , Brendan Higgins , David Gow , SeongJae Park , damon@lists.linux.dev, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1.2 2/5] mm/damon/tests/core-kunit: catch test failure in test_merge_regions_of() Date: Mon, 13 Jul 2026 06:58:33 -0700 Message-ID: <20260713135838.32730-3-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713135838.32730-1-sj@kernel.org> References: <20260713135838.32730-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" KUNIT_EXPECT_EQ() does not abort the execution of test code when the expectation is not met. But damon_test_merge_regions_of() code after its initial KUNIT_EXPECT_EQ() call assumes the expectation is met. It does a per-region test with a hard-coded number of regions that is correct only if the expectation was met. As a result, __nth_region_of() could return NULL, and the test code can dereference NULL pointers. Fix the issue by catching the expectation failure and skip the per-region tests. The user impact on realistic setups should be negligible, as it is a unit test. The issue was discovered [1] by Sashiko. [1] https://lore.kernel.org/20260710144937.26981-1-sj@kernel.org Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests") Cc: # 5.15.x Signed-off-by: SJ Park --- mm/damon/tests/core-kunit.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h index 6ad73559dd8ea..a99363720e677 100644 --- a/mm/damon/tests/core-kunit.h +++ b/mm/damon/tests/core-kunit.h @@ -260,11 +260,14 @@ static void damon_test_merge_regions_of(struct kunit = *test) damon_merge_regions_of(t, 9, 9999, ctx); /* 0-112, 114-130, 130-156, 156-170, 170-230, 230-10170 */ KUNIT_EXPECT_EQ(test, damon_nr_regions(t), 6u); + if (damon_nr_regions(t) !=3D 6) + goto out; for (i =3D 0; i < 6; i++) { r =3D __nth_region_of(t, i); KUNIT_EXPECT_EQ(test, r->ar.start, saddrs[i]); KUNIT_EXPECT_EQ(test, r->ar.end, eaddrs[i]); } +out: damon_free_target(t); damon_destroy_ctx(ctx); } --=20 2.47.3 From nobody Sat Jul 25 21:19:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 50596437469; Mon, 13 Jul 2026 13:58:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951128; cv=none; b=WPVvQg1txlUyt2U3l6vn9eFXbrbvqko6wakl/V0axcT6zZe2Jv6LW00CdG0iqfK4sDQMhjfrt1751jpmzBe/Cr5i85qPlMnfOt99lTWDm7u6vzPRDjy1fZ5ZGMSYNP7wD/5gUX54WNxbcyi6dpo9Blh7k1Xz2KpouHEOLxUaaLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951128; c=relaxed/simple; bh=vZYyX+a12usp29MAJtKmwcAKj0175PGkEUqyahpcjCo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=anhkqwa4RR3Qtp2sU3ubwjBVsOKs6WYA0Qlg+N+ru/Xdc9VbJHCX52JcM5nzR8OMj2prJKtAzWUd4j/vCjOQT5O48aYcpcRvIFLdoahy/M37jOs/WxjpiJNuoV40TEdb/ZWS/jMnKTo/gQ+oq6gcIJX5itn9a8lYiIghmr9w+2U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hQhsi6oB; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="hQhsi6oB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D36F11F00A3F; Mon, 13 Jul 2026 13:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783951127; bh=dZ7Hztpv9AG3ym6rD1iyRftJCr5BqjDTKMYF5vaHn/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hQhsi6oB6Dqy2UhzZgl0R7wMwFYoC01nOavo2Gn4P+0kq0kD3Hm545yLLe0+Uqr9+ +YgeckSbvOCmw+JICQvQotz/sNpn3oMsz1QqQSCtrX3MUr4nqLFGA82HkQuhIg6iLl zdC1W79HSej4vTNr4wI6ayYd886K+14GcnmxMxQmJBBHYK55SJ1laUK6bUiIDtrJjP HAL8ysVALSqofM0x8r2eZmtB/JTUnV+A0ELxHYbOuLUwMWL6Oc7teoFydLg13VhiOX STDoK6mftNt7g6Rvf/LlFXfMwuSZNbNbNfVVm/Y2pUnvc1poIMN62fESq3zzZl+Qzt SCgZE6BrNge/A== From: SJ Park To: Cc: SJ Park , stable@vger.kernel.org, Andrew Morton , Fernand Sieber , Leonard Foerster , SeongJae Park , Shakeel Butt , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1.2 3/5] mm/damon/vaddr: drop last same folio access check optimization Date: Mon, 13 Jul 2026 06:58:34 -0700 Message-ID: <20260713135838.32730-4-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713135838.32730-1-sj@kernel.org> References: <20260713135838.32730-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" The optimization can race when multiple kdamonds are running. Meanwhile, the impact of the optimization is quite doubtful. Just remove it. The user impact of the issue should be quite trivial. After all, the race can happen only when the user intentionally setup DAMON in the way. Even if it happens, it would be rare and only degrade the best-effort monitoring results. No critical consequences like kernel panic or memory corruption happen. The race possibility was discovered [1] by Sashiko. [1] https://lore.kernel.org/20260621204050.10993-1-sj@kernel.org Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory= address spaces") Cc: # 5.15.x Signed-off-by: SJ Park --- mm/damon/vaddr.c | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c index d10b8042adb5b..d487b7a4a1042 100644 --- a/mm/damon/vaddr.c +++ b/mm/damon/vaddr.c @@ -383,8 +383,6 @@ static void damon_va_prepare_access_checks(struct damon= _ctx *ctx) } =20 struct damon_young_walk_private { - /* size of the folio for the access checked virtual memory address */ - unsigned long *folio_sz; bool young; }; =20 @@ -411,7 +409,6 @@ static int damon_young_pmd_entry(pmd_t *pmd, unsigned l= ong addr, mmu_notifier_test_young(walk->mm, addr)) priv->young =3D true; - *priv->folio_sz =3D HPAGE_PMD_SIZE; huge_out: spin_unlock(ptl); return 0; @@ -430,7 +427,6 @@ static int damon_young_pmd_entry(pmd_t *pmd, unsigned l= ong addr, if (pte_young(ptent) || !folio_test_idle(folio) || mmu_notifier_test_young(walk->mm, addr)) priv->young =3D true; - *priv->folio_sz =3D folio_size(folio); out: pte_unmap_unlock(pte, ptl); return 0; @@ -458,7 +454,6 @@ static int damon_young_hugetlb_entry(pte_t *pte, unsign= ed long hmask, if (pte_young(entry) || !folio_test_idle(folio) || mmu_notifier_test_young(walk->mm, addr)) priv->young =3D true; - *priv->folio_sz =3D huge_page_size(h); =20 folio_put(folio); =20 @@ -470,11 +465,9 @@ static int damon_young_hugetlb_entry(pte_t *pte, unsig= ned long hmask, #define damon_young_hugetlb_entry NULL #endif /* CONFIG_HUGETLB_PAGE */ =20 -static bool damon_va_young(struct mm_struct *mm, unsigned long addr, - unsigned long *folio_sz) +static bool damon_va_young(struct mm_struct *mm, unsigned long addr) { struct damon_young_walk_private arg =3D { - .folio_sz =3D folio_sz, .young =3D false, }; =20 @@ -494,28 +487,17 @@ static bool damon_va_young(struct mm_struct *mm, unsi= gned long addr, * r the region to be checked */ static void __damon_va_check_access(struct mm_struct *mm, - struct damon_region *r, bool same_target) + struct damon_region *r) { - static unsigned long last_addr; - static unsigned long last_folio_sz =3D PAGE_SIZE; - static bool last_accessed; + bool accessed; =20 if (!mm) { damon_update_region_access_rate(r, false); return; } =20 - /* If the region is in the last checked page, reuse the result */ - if (same_target && (ALIGN_DOWN(last_addr, last_folio_sz) =3D=3D - ALIGN_DOWN(r->sampling_addr, last_folio_sz))) { - damon_update_region_access_rate(r, last_accessed); - return; - } - - last_accessed =3D damon_va_young(mm, r->sampling_addr, &last_folio_sz); - damon_update_region_access_rate(r, last_accessed); - - last_addr =3D r->sampling_addr; + accessed =3D damon_va_young(mm, r->sampling_addr); + damon_update_region_access_rate(r, accessed); } =20 static unsigned int damon_va_check_accesses(struct damon_ctx *ctx) @@ -524,15 +506,12 @@ static unsigned int damon_va_check_accesses(struct da= mon_ctx *ctx) struct mm_struct *mm; struct damon_region *r; unsigned int max_nr_accesses =3D 0; - bool same_target; =20 damon_for_each_target(t, ctx) { mm =3D damon_get_mm(t); - same_target =3D false; damon_for_each_region(r, t) { - __damon_va_check_access(mm, r, same_target); + __damon_va_check_access(mm, r); max_nr_accesses =3D max(r->nr_accesses, max_nr_accesses); - same_target =3D true; } if (mm) mmput(mm); --=20 2.47.3 From nobody Sat Jul 25 21:19:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 9E720438007; Mon, 13 Jul 2026 13:58:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951129; cv=none; b=E6DXqK+D6L1W86Vak/JeFR8lQq07QMH0Hco5uW/Xwzcz/2Zm5pVCFKMyCbbmUsOyymiW45cTfPVD3wPywqG5+yD6UoyBowuMhz/WKqahK063OJxU+eLCfQKux3/WXf39RCcWOg8Yt7axN9ouEgOe5M4eTC3zver4aSPUJuz0PDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951129; c=relaxed/simple; bh=5ScejZ7WR20mP7M32rQjyj8bSoc3hvLkckL0qcqy7ww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rj2uGeA0S8ZDCbZPwbhraYqmxKnJK34LbJSdOPIZrt8Nc8kVZ6lXssvu4aNHRps7IAwS3SFFBVwNYHj0YRGDia2qVR3YShLEfzm5wLfVh6AHise+WyNGCWxg0+7zbAcETBmTSjShM1S+ClAsUFN/i20cnDMdR3BwsqGWii8KEw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K198PuNm; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K198PuNm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 513471F000E9; Mon, 13 Jul 2026 13:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783951127; bh=MTFa+hpzXfcfyF1zP3rvNz2Fhu2Cffg36gE6sioOaYQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=K198PuNm525azO9JOEGv+kYtKZf9GL5saEoMnfwLfy+QoVvf3M61SFFj9Ca/kvdOi VNY6nNIHsGF9lg6m6lqNuukfbHXvnSjaHO9YU6+vcHqg9ZdMcecBXajrDGxgE/s2g4 l0Wt/AKMPXEfe93V9qTx55JGD7KTaF8fpcH+sjIBHom4GIVw4m0pnTJMdiz9Tu3W3n gTdjeVBMURLeaBsAeoG7+hfOfpXTA/PkLpa40PYH/h1vpDWwT4A2sPxsSgny4eCYQE iR6FlfA4UqD/vJnw7VcrrLV9Dr82Gl6cOq7kSKZwgg8bJwCf+62WBbXPZgrqMDDbW1 rPBXT5uQWgmYA== From: SJ Park To: Cc: SJ Park , stable@vger.kernel.org, Andrew Morton , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1.2 4/5] mm/damon/paddr: drop last same folio access check reuse optimization Date: Mon, 13 Jul 2026 06:58:35 -0700 Message-ID: <20260713135838.32730-5-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713135838.32730-1-sj@kernel.org> References: <20260713135838.32730-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" It can race when multiple kdamonds are being used. The problem from the race is doubtful, but the gain from the optimization is also doubtful. Simply drop the optimization in favor of code simplicity. The user impact is doubtfully trivial. After all, this kind of interference can happen only by intentional user setup. Even if it happens, it will be rare, and the consequence is degradation of the best-effort monitoring results. No critical consequences like kernel panic or memory corruption happen. The race was discovered [1] by Sashiko. [1] https://lore.kernel.org/20260621204050.10993-1-sj@kernel.org Fixes: a28397beb55b ("mm/damon: implement primitives for physical address s= pace monitoring") Cc: # 5.16.x Signed-off-by: SJ Park --- mm/damon/paddr.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c index b85f88a7a38f4..e4f98d67461f5 100644 --- a/mm/damon/paddr.c +++ b/mm/damon/paddr.c @@ -65,7 +65,7 @@ static void damon_pa_prepare_access_checks(struct damon_c= tx *ctx) } } =20 -static bool damon_pa_young(phys_addr_t paddr, unsigned long *folio_sz) +static bool damon_pa_young(phys_addr_t paddr) { struct folio *folio =3D damon_get_folio(PHYS_PFN(paddr)); bool accessed; @@ -74,7 +74,6 @@ static bool damon_pa_young(phys_addr_t paddr, unsigned lo= ng *folio_sz) return false; =20 accessed =3D damon_folio_young(folio); - *folio_sz =3D folio_size(folio); folio_put(folio); return accessed; } @@ -82,23 +81,12 @@ static bool damon_pa_young(phys_addr_t paddr, unsigned = long *folio_sz) static void __damon_pa_check_access(struct damon_region *r, unsigned long addr_unit) { - static phys_addr_t last_addr; - static unsigned long last_folio_sz =3D PAGE_SIZE; - static bool last_accessed; + bool accessed; phys_addr_t sampling_addr =3D damon_pa_phys_addr( r->sampling_addr, addr_unit); =20 - /* If the region is in the last checked page, reuse the result */ - if (ALIGN_DOWN(last_addr, last_folio_sz) =3D=3D - ALIGN_DOWN(sampling_addr, last_folio_sz)) { - damon_update_region_access_rate(r, last_accessed); - return; - } - - last_accessed =3D damon_pa_young(sampling_addr, &last_folio_sz); - damon_update_region_access_rate(r, last_accessed); - - last_addr =3D sampling_addr; + accessed =3D damon_pa_young(sampling_addr); + damon_update_region_access_rate(r, accessed); } =20 static unsigned int damon_pa_check_accesses(struct damon_ctx *ctx) --=20 2.47.3 From nobody Sat Jul 25 21:19:02 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 1B1EB431E5F; Mon, 13 Jul 2026 13:58:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951129; cv=none; b=KhyznhK7UxDEch8eISFZEjFqGwGRxNRk1qKntwzco13w92FBTYCLvj+kOTgQBH3VUnPNmJSfbMKguFKLzgGhE8Uv8NTJdR7O7mfBO4i2DVX0RW3g1UXnsBbJmYkAJzudSJJ/3SqEpvuWqGRAEKoK/tDOTs2266/lCbuHYLiDN1o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783951129; c=relaxed/simple; bh=nt1m4yOHpUkfhmuPI5sfQNBML1ejUt2LDcM7VHyWuzk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WISEKQAFmxbI1oJiX4jTDQ1fwLkmWcZOw1uD0PiaLut1dMZWRjCdif4RvXC2IEpztZcEVKpcdCwHtqCEQwO2alp2JKyyU1nXIr6Vqqlf5Yc6PdOAiUHUNIP2nkPu0s8K5ZxdbifU5CuLbGozMe9uOHyQfiZwu7URIVw+9yBeMqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=euZHtD/a; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="euZHtD/a" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A566E1F00AC4; Mon, 13 Jul 2026 13:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783951127; bh=hhmDCI+ot7IL3VsT3pLzYBJmbk4njLZd345nGTc/fNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=euZHtD/aDvIZBDQjpyKLp0lEEVw3YHagyScpG6RM09WNsMcQ/ypT/C9GcVouODud+ 8p5nPr2a84cQB1FrIzkdjnfj1SNgF6FfBFCRN/6/DUUCJcZqg06eeKx0pgLwhVFsAr dXkjFMa3AYDXtLwtk0Ryh6Dvfw1KtBQcIzXUiEedRoAInwPVDYv+U+bsyRJUbX2rzc t4aIw4thGprKvXHV/Wz+NL9338m7P/vHz7Q9nFG2eVUctNjPMVqjMkKvAbsp8aPn1a 93oyYPQkCN2QrwjSBR6/heoQ+mS1s0LprpOUeKW4L/tqjCvl731WSr0tEXZHCww9ju METmJcFrUwk5g== From: SJ Park To: Cc: SJ Park , Andrew Morton , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH v1.2 5/5] mm/damon/sysfs: read ops_id only once Date: Mon, 13 Jul 2026 06:58:36 -0700 Message-ID: <20260713135838.32730-6-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713135838.32730-1-sj@kernel.org> References: <20260713135838.32730-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" damon_sysfs_apply_inputs() reads ops_id twice. It could race with ops_id_store(). As a result, the min_region_sz could wrongly be set up. Read it once. The user impact is trivial. Sane users ain't update the parameter in parallel. Even if it happens, only monitoring itself runs differently than expected. No critical consequences like kernel panic or memory corruption happen. The issue was discovered [1] by Sashiko. [1] https://lore.kernel.org/20260703172417.95426-1-sj@kernel.org Signed-off-by: SJ Park --- mm/damon/sysfs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index b5fe036f78015..60a1a9e4ada34 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -2094,14 +2094,16 @@ static inline bool damon_sysfs_kdamond_running( static int damon_sysfs_apply_inputs(struct damon_ctx *ctx, struct damon_sysfs_context *sys_ctx) { + enum damon_ops_id ops_id; int err; =20 - err =3D damon_select_ops(ctx, sys_ctx->ops_id); + ops_id =3D READ_ONCE(sys_ctx->ops_id); + err =3D damon_select_ops(ctx, ops_id); if (err) return err; ctx->addr_unit =3D sys_ctx->addr_unit; /* addr_unit is respected by only DAMON_OPS_PADDR */ - if (sys_ctx->ops_id =3D=3D DAMON_OPS_PADDR) + if (ops_id =3D=3D DAMON_OPS_PADDR) ctx->min_region_sz =3D max( DAMON_MIN_REGION_SZ / sys_ctx->addr_unit, 1); ctx->pause =3D sys_ctx->pause; --=20 2.47.3