[PATCH] mm/damon/tests: use scoped_guard() for damon_test_ops_registration

Jaeyeon Lee posted 1 patch 1 month, 2 weeks ago
mm/damon/tests/core-kunit.h | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
[PATCH] mm/damon/tests: use scoped_guard() for damon_test_ops_registration
Posted by Jaeyeon Lee 1 month, 2 weeks ago
Replace manual mutex_lock() and mutex_unlock() calls with the
scoped_guard() macro. This simplifies the code, improves readability,
and ensures that the lock is automatically released when the scope
ends, preventing potential lock leaks in the future.

Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>
---
 mm/damon/tests/core-kunit.h | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/mm/damon/tests/core-kunit.h b/mm/damon/tests/core-kunit.h
index 4a536d41cdb2..770927a30021 100644
--- a/mm/damon/tests/core-kunit.h
+++ b/mm/damon/tests/core-kunit.h
@@ -434,17 +434,17 @@ static void damon_test_ops_registration(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, damon_select_ops(c, NR_DAMON_OPS), -EINVAL);
 
 	/* Registration should success after unregistration */
-	mutex_lock(&damon_ops_lock);
-	bak = damon_registered_ops[DAMON_OPS_VADDR];
-	damon_registered_ops[DAMON_OPS_VADDR] = (struct damon_operations){};
-	mutex_unlock(&damon_ops_lock);
+	scoped_guard(mutex, &damon_ops_lock) {
+		bak = damon_registered_ops[DAMON_OPS_VADDR];
+		damon_registered_ops[DAMON_OPS_VADDR] =
+			(struct damon_operations){};
+	}
 
 	ops.id = DAMON_OPS_VADDR;
 	KUNIT_EXPECT_EQ(test, damon_register_ops(&ops), 0);
 
-	mutex_lock(&damon_ops_lock);
-	damon_registered_ops[DAMON_OPS_VADDR] = bak;
-	mutex_unlock(&damon_ops_lock);
+	scoped_guard(mutex, &damon_ops_lock)
+		damon_registered_ops[DAMON_OPS_VADDR] = bak;
 
 	/* Check double-registration failure again */
 	KUNIT_EXPECT_EQ(test, damon_register_ops(&ops), -EINVAL);
@@ -452,10 +452,9 @@ static void damon_test_ops_registration(struct kunit *test)
 	damon_destroy_ctx(c);
 
 	if (need_cleanup) {
-		mutex_lock(&damon_ops_lock);
-		damon_registered_ops[DAMON_OPS_VADDR] =
-			(struct damon_operations){};
-		mutex_unlock(&damon_ops_lock);
+		scoped_guard(mutex, &damon_ops_lock)
+			damon_registered_ops[DAMON_OPS_VADDR] =
+				(struct damon_operations){};
 	}
 }
 
-- 
2.43.0
Re: [PATCH] mm/damon/tests: use scoped_guard() for damon_test_ops_registration
Posted by SJ Park 1 month, 2 weeks ago
Hi Jaeyeon,


Thank you for sending this patch!

On Sat, 15 Aug 2026 16:30:39 +0200 Jaeyeon Lee <jaeyeon.lee.dev@gmail.com> wrote:

> Replace manual mutex_lock() and mutex_unlock() calls with the
> scoped_guard() macro. This simplifies the code, improves readability,
> and ensures that the lock is automatically released when the scope
> ends, preventing potential lock leaks in the future.

I'm concerned if DAMON community is not familiar enough with scoped_guard()
macro to handle it well.  At least I'm not yet familiar with it.  That said, it
seems using it for one function in the unit test could be a safe and good
start.

So I want to have this change.  It is not because I want all DAMON code to use
scoped guard, though.  I want to take this change because I want to see if it
is really safe and easy to maintain.  In other words, let's not adopt scoped
guard to more parts of DAMON code, for now.

Thank you for introducing scoped guard to DAMON!

> 
> Signed-off-by: Jaeyeon Lee <jaeyeon.lee.dev@gmail.com>

Reviewed-by: SJ Park <sj@kernel.org>

I applied  this patch to damon/next [1] tree.  We are now quite close to next
merge window.  We (mm community) want to focus on making mm.git more stabilized
and therefore ready for the next merge window, rather than adding more changes
that are not really urgent.  I understand this series is not really urgent.
Hence, Andrew might not add this patch until next -rc1 release.  In the case, I
will request adding this to mm.git after next -rc1 release.  So, no action from
your side is needed for now.  Let me know if you think this is really  urgent.

[1] https://origin.kernel.org/doc/html/latest/mm/damon/maintainer-profile.html#scm-trees


Thanks,
SJ

[...]