Skip to contentSkip to conversation

mm/damon/tests/core-kunit: add test for unconditionally skipping the last region

PATCH RESEND Superseded Liew Rui Yan 3 participants 6 messages Sep 19 – Sep 24

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 mm/damon/tests/core-kunit: add test for unconditionally skipping the last region

Commit 50585192bc2e ("mm/damon/schemes: skip already charged targets and regions") introduced a minor issue that causes the last region to be skipped before it is processed.

Example:

1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.

Result: R2 is permanently skipped even though it has never been processed.

Add a test to prevent this regression in the future.

---

Changes from v1:
- v1: https://lore.kernel.org/damon/[email protected]
- Resend since the dependent patch is merged into mm-new, that would help us getting AI review help.

---
 mm/damon/tests/core-kunit.h | 43 +++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
mm/damon/tests/core-kunit.h +43 -0
index 4a536d41cdb2..90b0fda34fba 100644
@@ -1632,6 +1632,48 @@ static void damon_test_rand(struct kunit *test)
 	}
 }
 
+static void damon_test_last_region_quota_reset(struct kunit *test)
+{
+	struct damos s = {0};
+	struct damon_target *t;
+	struct damon_region *r1, *r2;
+	unsigned long min_region_sz = 10;
+	bool skipped;
+
+	t = damon_new_target();
+	if (!t) {
+		kunit_skip(test, "target alloc fail");
+	}
+
+	r1 = damon_new_region(0, 100);
+	if (!r1) {
+		damon_free_target(t);
+		kunit_skip(test, "region 1 alloc fail");
+	}
+	damon_add_region(r1, t);
+
+	r2 = damon_new_region(100, 200);
+	if (!r2) {
+		damon_free_target(t);
+		kunit_skip(test, "region 2 alloc fail");
+	}
+	damon_add_region(r2, t);
+
+	s.quota.charge_target_from = t;
+	s.quota.charge_addr_from = r1->ar.end;
+
+	skipped = damos_skip_charged_region(t, r2, &s, min_region_sz);
+
+	/* 'r2' is not processed, it should not skip */
+	KUNIT_EXPECT_EQ(test, skipped, false);
+
+	/* 'r2' is last region, it should reset charge_{target,addr}_from */
+	KUNIT_EXPECT_PTR_EQ(test, s.quota.charge_target_from, NULL);
+	KUNIT_EXPECT_EQ(test, s.quota.charge_addr_from, 0);
+
+	damon_free_target(t);
+}
+
 static struct kunit_case damon_test_cases[] = {
 	KUNIT_CASE(damon_test_target),
 	KUNIT_CASE(damon_test_regions),
@@ -1664,6 +1706,7 @@ static struct kunit_case damon_test_cases[] = {
 	KUNIT_CASE(damon_test_is_last_region),
 	KUNIT_CASE(damon_test_walk_control_obsolete),
 	KUNIT_CASE(damon_test_rand),
+	KUNIT_CASE(damon_test_last_region_quota_reset),
 	{},
 };
 
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] mm: invalid non-power-of-two alignment in damon test --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] mm: invalid non-power-of-two alignment in damon test
--

commit a99d6a5362b525870f71ce4be82705b420aea7ef
Author: Liew Rui Yan <[email protected]>

mm/damon/tests/core-kunit: add test for unconditionally skipping the last region

This commit adds a kunit test to ensure the last region is not skipped before it is processed.

Quoted 4 lines
mm/damon/tests/core-kunit.h +0 -0
index 5ff0436c58441..84e50f08f417d 100644

[ ... ]

mm/damon/tests/core-kunit.h static void damon_test_rand(struct kunit *test) · line 1937

Quoted 10 lines
mm/damon/tests/core-kunit.h +6 -0
@@ -1929,6 +1929,48 @@ static void damon_test_rand(struct kunit *test)
 	}
 }
 
+static void damon_test_last_region_quota_reset(struct kunit *test)
+{
+	struct damos s = {0};
+	struct damon_target *t;
+	struct damon_region *r1, *r2;
+	unsigned long min_region_sz = 10;

[Severity: Low]
Is it safe to initialize min_region_sz to a non-power-of-two value?

The ALIGN_DOWN() macro inside damos_skip_charged_region() relies on bitwise masking and mathematically guarantees correct alignment arithmetic only when the alignment boundary is a strict power of two. Passing 10 yields incorrect values (e.g., ALIGN_DOWN(120, 10) incorrectly evaluates to 112).

Quoted 3 lines
mm/damon/tests/core-kunit.h +3 -0
+	bool skipped;
+
+	t = damon_new_target();

[ ... ]

mm/damon/tests/core-kunit.h static void damon_test_rand(struct kunit *test) · line 1665

Quoted 4 lines
mm/damon/tests/core-kunit.h +4 -0
+	s.quota.charge_target_from = t;
+	s.quota.charge_addr_from = r1->ar.end;
+
+	skipped = damos_skip_charged_region(t, r2, &s, min_region_sz);

[Severity: Low]
Can this call silently corrupt the region skip calculation in the future?

While the current test parameters bypass the ALIGN_DOWN() logic inside damos_skip_charged_region(), any future expansion or variation of the test that hits the branch will encounter the incorrect alignment arithmetic caused by min_region_sz being set to 10.

Quoted 3 lines
mm/damon/tests/core-kunit.h +3 -0
+
+	/* 'r2' is not processed, it should not skip */
+	KUNIT_EXPECT_EQ(test, skipped, false);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Liew Rui Yan
Quoted 14 lines · On Sat, 19 Sep 2026 16:01:58 +0800 Liew Rui Yan <[email protected]>…

Commit 50585192bc2e ("mm/damon/schemes: skip already charged targets and regions") introduced a minor issue that causes the last region to be skipped before it is processed.

Example:

1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.

Quoted 6 lines

Result: R2 is permanently skipped even though it has never been processed.

Add a test to prevent this regression in the future.

Other than a trivial style that I commented below,

[...]

Quoted 2 lines
mm/damon/tests/core-kunit.h +2 -0
+static void damon_test_last_region_quota_reset(struct kunit *test)
+{

[...]

mm/damon/tests/core-kunit.h static void damon_test_rand(struct kunit *test) · line 1646

Quoted 4 lines
mm/damon/tests/core-kunit.h +4 -0
+	t = damon_new_target();
+	if (!t) {
+		kunit_skip(test, "target alloc fail");
+	}

Let's drop braces for single line, as suggested [1] in coding-style.

[1] https://docs.kernel.org/process/coding-style.html#placing-braces-and-spaces

Thanks,
SJ

#3 lore raw
Quoted 31 lines · On Sat, 19 Sep 2026 09:07:25 -0700 SJ Park <[email protected]> wrote:
Quoted 14 lines · On Sat, 19 Sep 2026 16:01:58 +0800 Liew Rui Yan <[email protected]>…

Commit 50585192bc2e ("mm/damon/schemes: skip already charged targets and regions") introduced a minor issue that causes the last region to be skipped before it is processed.

Example:

1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.

Quoted 6 lines

Result: R2 is permanently skipped even though it has never been processed.

Add a test to prevent this regression in the future.

Other than a trivial style that I commented below,

[...]

Quoted 2 lines
mm/damon/tests/core-kunit.h +2 -0
+static void damon_test_last_region_quota_reset(struct kunit *test)
+{

[...]

Quoted 6 lines

mm/damon/tests/core-kunit.h static void damon_test_rand(struct kunit *test) · line 1646

Quoted 4 lines
mm/damon/tests/core-kunit.h +4 -0
+	t = damon_new_target();
+	if (!t) {
+		kunit_skip(test, "target alloc fail");
+	}

Let's drop braces for single line, as suggested [1] in coding-style.

I will change this in next revision.

By the way, Sashiko reported an issue [1]: min_region_sz is not power-of-2. While this do not cause any real issue in this test, non-power-of-2's min_region_sz is not legal. Should min_region_sz initial value set to power-of-2 value (e.g., PAGE_SIZE) in the next revision?

Quoted 3 lines

[1] https://docs.kernel.org/process/coding-style.html#placing-braces-and-spaces

[1] https://lore.kernel.org/damon/[email protected]

Best regards,
Rui Yan

#4 lore raw

Hi SJ,

Just a reminder, I think you might have missed the question I asked.

Quoted 48 lines · On Sun, 20 Sep 2026 03:09:10 +0800 Liew Rui Yan <[email protected]>…
Quoted 31 lines · On Sat, 19 Sep 2026 09:07:25 -0700 SJ Park <[email protected]> wrote:
Quoted 14 lines · On Sat, 19 Sep 2026 16:01:58 +0800 Liew Rui Yan <[email protected]>…

Commit 50585192bc2e ("mm/damon/schemes: skip already charged targets and regions") introduced a minor issue that causes the last region to be skipped before it is processed.

Example:

1. Target has 2 regions: R1 (0-100 bytes) and R2 (100-200 bytes).
2. Quota is configured to process only 100 bytes per window.
3. Window 1: Processes R1 (0-100). Quota is full. charge_{target, addr}_from is saved at (Target, 100).
4. Window 2: The loop reaches R2. Because R2 is damon_last_region(t), the old code unconditionally returns true, skipping R2 entirely and resetting the charge_{target,addr}_from.

Quoted 6 lines

Result: R2 is permanently skipped even though it has never been processed.

Add a test to prevent this regression in the future.

Other than a trivial style that I commented below,

[...]

Quoted 2 lines
mm/damon/tests/core-kunit.h +2 -0
+static void damon_test_last_region_quota_reset(struct kunit *test)
+{

[...]

Quoted 6 lines

mm/damon/tests/core-kunit.h static void damon_test_rand(struct kunit *test) · line 1646

Quoted 4 lines
mm/damon/tests/core-kunit.h +4 -0
+	t = damon_new_target();
+	if (!t) {
+		kunit_skip(test, "target alloc fail");
+	}

Let's drop braces for single line, as suggested [1] in coding-style.

I will change this in next revision.

By the way, Sashiko reported an issue [1]: min_region_sz is not power-of-2. While this do not cause any real issue in this test, non-power-of-2's min_region_sz is not legal. Should min_region_sz initial value set to power-of-2 value (e.g., PAGE_SIZE) in the next revision?

[...]

Quoted 1 line

[1] https://lore.kernel.org/damon/[email protected]

[...]

Best regards,
Rui Yan

maintainer
#5 lore raw
Quoted 3 lines · On Thu, 24 Sep 2026 15:57:42 +0800 Liew Rui Yan <[email protected]>…

Hi SJ,

Just a reminder, I think you might have missed the question I asked.

Sorry, I dropped the ball. Thank you for reminding, Liew!

Quoted 2 lines · On Sun, 20 Sep 2026 03:09:10 +0800 Liew Rui Yan <[email protected]>…

[...]

Quoted 5 lines
Quoted 5 lines

By the way, Sashiko reported an issue [1]: min_region_sz is not power-of-2. While this do not cause any real issue in this test, non-power-of-2's min_region_sz is not legal. Should min_region_sz initial value set to power-of-2 value (e.g., PAGE_SIZE) in the next revision?

Yes, let's use a power-of-2 value. '1' should also be suffice.

Thanks,
SJ

[...]