[PATCH v1 3/4] locking/mutex: Add mutex_nest_1() scoped guard

Mickaël Salaün mic at digikod.net
Mon Jan 13 16:11:11 UTC 2025


Several places use a mutex with SINGLE_DEPTH_NESTING, but there is no
proper way to use a scoped guard with that.

Add a mutex_nest_1() scoped guard that call metex_lock() with
SINGLE_DEPTH_NESTING (defined in lockdep.h).

This will initially by used by Landlock in a following commit.

Cc: Boqun Feng <boqun.feng at gmail.com>
Cc: Günther Noack <gnoack at google.com>
Cc: Ingo Molnar <mingo at redhat.com>
Cc: Peter Zijlstra <peterz at infradead.org>
Cc: Waiman Long <longman at redhat.com>
Cc: Will Deacon <will at kernel.org>
Signed-off-by: Mickaël Salaün <mic at digikod.net>
Link: https://lore.kernel.org/r/20250113161112.452505-4-mic@digikod.net
---
 include/linux/mutex.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 2bf91b57591b..dfd128a3f365 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -199,6 +199,8 @@ extern void mutex_unlock(struct mutex *lock);
 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
 
 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
+DEFINE_GUARD(mutex_nest_1, struct mutex *,
+	     mutex_lock_nested(_T, SINGLE_DEPTH_NESTING), mutex_unlock(_T))
 DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
 DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0)
 
-- 
2.47.1




More information about the Linux-security-module-archive mailing list