From nobody Sat Jul 25 19:28:29 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 D2AB2478E4A; Tue, 14 Jul 2026 13:52:40 +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=1784037161; cv=none; b=j1gRcPa6gm5pJVcgmdAcc8uVqCmE/ht/xomczE9k6tuEw+1NpT2oInGyjTSbgBhQS4DmvzKVuXRjczNI+LqLxt65qBSHwGgvbNA+2JnW7Rdo6Y338A+UDGUEUft9hrChkAJNZ3lal5YJlqnGreo3YHG+mmHTFkWpWAwXlbCzVsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784037161; c=relaxed/simple; bh=uommVYjcgv1yUQymXHLWitvXUy8HlVyIq9wOy8obzmw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C3r/yuTEZp/rKnyQZpPnr09uDO203IDKMpSK65V7/5DQqMvud6Cm3rNdtjcc7tiY9VsfS2iyb0FEQoBvWukHtWc4KqKxh2WfVoGV/HLZtr1Zox522nbkDenaWd7/T3m+cV2u5q4fBS7ygnqOlSe0ShIh5s2/r/CI5o+ocKhRJHk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EJRSBpUX; 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="EJRSBpUX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B7F81F00A3A; Tue, 14 Jul 2026 13:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784037160; bh=Q9KjTQrAluw9j+QP6j3O8xbCeroVnCVq06v65dUktLw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=EJRSBpUX3PedikG4AVOL6Hd0tQpz05Ds29mSlPWNdjvO/63ajYDckPbJAg5+6rAn0 5nhX5gcm92FNBCgte+EIYl70ZREsaJcu/cWHc4Qk2oy3Vzlijl9mDlWvoEFk5G/6HD zF9cYE6xI0F5EiPxJczCgS4A9S+9jkncDjzqxik864KDlBA2d0WhN4C8XKA23f0hSS 9l1FM4ArevpTjdOKCaHRFytdXzeo8Y6A+XPQmdwwfFKT53f66ejq7Qwl4PIDxEiNO3 dgjm8Bsdt9lacS9uisrPwpbkxjdse4Thb7+tftSQj+bH9ELKQXULeidbIREkJ8siZW thGBU7ty7t77Q== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/5] mm/damon/core: avoid infinite kdamond_merge_regions() internal loop Date: Tue, 14 Jul 2026 06:52:29 -0700 Message-ID: <20260714135236.92699-2-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260714135236.92699-1-sj@kernel.org> References: <20260714135236.92699-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 6c4215cc809ec..603b102ff80f9 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -3372,7 +3372,7 @@ 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, @@ -3380,9 +3380,14 @@ static void kdamond_merge_regions(struct damon_ctx *= c, unsigned int threshold, nr_regions +=3D damon_nr_regions(t); } count_age =3D false; - 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 19:28:29 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 85880478E50; Tue, 14 Jul 2026 13:52:41 +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=1784037162; cv=none; b=fra0qhzt0dAYhIVEr/W7bAx4ulFyO9hgmYov2ua6XOFdEEkOsG/Rz+eCXt4ZQJXCOetZiouZUxSQAITnaYecjDhZERf9Nsmt0y9gKEsGgiMZLztj20+S5VJ1hQibOKck+K9YJYa8ScH/iavi9tIHGlCbsp9HJZ7hA73b/d4NyqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784037162; c=relaxed/simple; bh=oa/Api2m6C6paRLqdcrPW1anhL7pYe2ZAUU7W+EEjsg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c3O20P+XdOoDfYlr29TFrQSEdA88/wwHmTMuJ0U+rUs6cA5BvlwTmoQwoNB7ZkafmgPpm98p5OG5Ess5yLzlxrsTGBhfjj9659YEfnLNYqk+/2imF9o0FjqhAVyldKYuCWJL0miI4AG1O6sCjLKAoiEkwI9QF7k1z52jHsh075E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=o2Mu32fe; 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="o2Mu32fe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E051D1F00A3D; Tue, 14 Jul 2026 13:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784037161; bh=Csk2Bs95SoFAJpGitaPFL2lXi5OcS5tAvUkrHnWNWRA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=o2Mu32feVKP83fD7JNCE+FPeaLoTggU2hLWV+t91Oq6Xo/b7T0n91PDrKGBicdpIT tpx7pkI6XwSagk62CiGVmhne6IeOKo8WbVk+n3mogaHWHsVIDCTFl2ar8xwch0Kc1Q 4DxxOJ66fjwEMNPC8Gx/UtamMjq5vnz2drmQ2Ms2q/x0xLhSQSZYEl8WQ+zvrcQCz2 4n/elR7mws1pVx6Rfn7PvRVZYtRNH1JfJnMdtcSCP5/9dTyxKcWpQimVMabUYxJNKM /2kZJa3E83DkUVcmNqKZbkYKgnyEdquT4xc4eSqF1wqV77KN8xdgkWTJsDDc5HNOCO xnu1NV3GuDpfg== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, 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: [PATCH 2/5] mm/damon/tests/core-kunit: catch test failure in test_merge_regions_of() Date: Tue, 14 Jul 2026 06:52:30 -0700 Message-ID: <20260714135236.92699-3-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260714135236.92699-1-sj@kernel.org> References: <20260714135236.92699-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 485472ddebd19..eba643762132f 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, true); /* 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 19:28:29 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 E3DEB47A0B4; Tue, 14 Jul 2026 13:52:41 +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=1784037163; cv=none; b=dI55WIL7pKczEmk8RpnS/L1SEBWcnZSnNqAdtlZG+if8LHLs9UBIfKV8BkvEr69o0O1OsgyoP+s4dbTvyrK6TQEocROagLKh0fEOZmXZcAtyCwN7Tko3YpaXuZRipHOnJeKJRzEj76MCatHTXjqrqyt1CmdwcvzHRC4r5pUHXkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784037163; c=relaxed/simple; bh=vZYyX+a12usp29MAJtKmwcAKj0175PGkEUqyahpcjCo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s1ojKjAkS1sufAxPjBWAMF/308Gw/GhWtCp1F6fFbGRF7MWtbN/N3bC6aSSuDy4dXUiMM5mgALJYTHZpWSyFEaCbBvzJA/hCA91zJGIDjw6yktJo5GlLXYK0gLnrOpU6wOUrwr77pGPhjZphwyKveoYrIubyKo5tekbq1Qzs4QM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mg/1cImU; 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="Mg/1cImU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 648221F00A3E; Tue, 14 Jul 2026 13:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784037161; bh=dZ7Hztpv9AG3ym6rD1iyRftJCr5BqjDTKMYF5vaHn/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Mg/1cImUvg1xFHeuB7HlVPy4iR52T3765lzbUe744vSr6w8vgzvRsPXCeM8TIAKBH EfnFC2sunhWRbpKAwMxprwe8uusmv3x7oT+iInwMgbk2UajQHiCGqsJ66jZOAR/6Iu fY4i7nwBAdYIct4z3u9mmAaTUP/NTEDWXwGh253f0vC8iKCEdZUlG76KCmzLYkFc7D Qy4xGvWQLRjSHnhnNVI+qu+FkVvFef8x9bKu/aZWQOVkLqNwCznx/iwmLd/7hFn2Ws XRdj4VpzQVT2MvoyUgAJ2ZXS14418PyBe3FDRUN2PGTngkDFRYd+nbRQyqL+Po2Sqc DHRb07n+BCDNQ== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, Fernand Sieber , Leonard Foerster , SeongJae Park , Shakeel Butt , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 3/5] mm/damon/vaddr: drop last same folio access check optimization Date: Tue, 14 Jul 2026 06:52:31 -0700 Message-ID: <20260714135236.92699-4-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260714135236.92699-1-sj@kernel.org> References: <20260714135236.92699-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 19:28:29 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 5C60E47AF5F; Tue, 14 Jul 2026 13:52:42 +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=1784037163; cv=none; b=eig9MHfTYsIipRmOND07OlG7zy8LY+EJikI7OeWdueAmqN7WXtEeLbbXQ5EOa7w/nbaf+BJ8V2RQuyolUGI4d0f38lR6rA6Eg+g9F4Sb2OTq2QT7p1jnf6inq3ci3EhzU/t5D5a7znCLbWd9LwBmrbaKCAduDqW64+5iu8IeFUA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784037163; c=relaxed/simple; bh=5ScejZ7WR20mP7M32rQjyj8bSoc3hvLkckL0qcqy7ww=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ao+/C/hIFdwL82R38WuMhLWedhJoNlVBiRG4fIf9WRLP1YXZnB+362Wx22VHtg1kB8snRVuKStfE6XLkyc76u6PFV2PeuKTXW2GvBTL+0yspVpE2yLHkmr4SCBRuj3sj85Md9DTh3Bpx30abWLRgD1AkJM1OmwXuhoTejIx+/yA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TXSHgfay; 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="TXSHgfay" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4B211F000E9; Tue, 14 Jul 2026 13:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784037162; bh=MTFa+hpzXfcfyF1zP3rvNz2Fhu2Cffg36gE6sioOaYQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TXSHgfayZDyKOjxmFxTfQTlWYACYLOF1UShP6zz/S0vV6siNuJJPHalozH+s0r8YB gwGVBU9sm99r4zVvNBWuJM1x8YMeYOjk52ctqyyS/FKv4mZVpB9grooZW1flRLiEvy mZ462KChCsmiktOfOujR+u2hYkPpRwQrtBUN6GnKJ7PnCC6pzIJdQ4ATSo/4G82BVL dhQehNgCxHx7kY4C9tgNQ7ADb73tY2p9BPVlwJWlzJh0yUwccX4uaq4PkveFw5beMy cR0YFSEFjgDehYed2pJ1V7dCiy3CLqHNr/aldqnn1cGafOo/FkG2vJWlG/P1US5xCR bXuLRqUFzMaJw== From: SJ Park To: Andrew Morton Cc: SJ Park , stable@vger.kernel.org, damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 4/5] mm/damon/paddr: drop last same folio access check reuse optimization Date: Tue, 14 Jul 2026 06:52:32 -0700 Message-ID: <20260714135236.92699-5-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260714135236.92699-1-sj@kernel.org> References: <20260714135236.92699-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 19:28:29 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 7637347AF65; Tue, 14 Jul 2026 13:52:42 +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=1784037163; cv=none; b=JBHb7+2smFW53LxwW0Wl/+EY3boIiHA7zIL22dYsy1iENVaYUlP2uOiWP/jy1KpQkmtyEwSmrVk7UR7j3T5Jd0jr/e577ffKWTUnqz/4hEy7A3+HmC+VkATtU/gVNeuiESu3AnSM2v1LIgwFhdtPf2kETTsG1JASQHoMj4pjfZA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784037163; c=relaxed/simple; bh=nt1m4yOHpUkfhmuPI5sfQNBML1ejUt2LDcM7VHyWuzk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qISd9r+J7m2jx+cWyx95SVgNnxeFErvMreP/8QKee1n1UG4h+1oIFHxl3ruV8zcJZwklZKRiYP2IuUlnMvTeMdqJCBC6+ISzdRziFyeuIQtb19GfqI0DD+OnmWHLtIphzOsDeBJNTNlciX3d1wu6BbGW1tugesgwaDRXTxKRtCY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d0KLQDgD; 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="d0KLQDgD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 374B31F00A3F; Tue, 14 Jul 2026 13:52:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1784037162; bh=hhmDCI+ot7IL3VsT3pLzYBJmbk4njLZd345nGTc/fNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=d0KLQDgDpLeerstVYPZQr9U6UvUxQHOZeA3gGp96M0sHUHYIQvTLsOQu8j9ZkZjOD Lovnt/dNpVbutyl7O51WD5VJYBEOgKrYFJjtMEoncELZe33ehRqbD+sSZiWgKYb4m2 wP5jZhnfLWN9umI+1wMHvD58d0IzCHJfg9r+nPLg2wTwZrhkecb7VwubPiq22IzErr YAjwXNS1SmHclM456MNnuIKt8whSfSxja5+wfsMKqTzdQ6vqfb+pxXQ7/MXaNzlIOT MUZUt7TAAI3Yos/2cya6zodYFjT0hf0O8Hr8wPKBDsmv9Q6RL6Q1YygDFB+cld3LSr XAASuxfuqLPhg== From: SJ Park To: Andrew Morton Cc: SJ Park , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 5/5] mm/damon/sysfs: read ops_id only once Date: Tue, 14 Jul 2026 06:52:33 -0700 Message-ID: <20260714135236.92699-6-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260714135236.92699-1-sj@kernel.org> References: <20260714135236.92699-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