[Devel] [PATCH rh7 2/4] ms/lockdep: Add lockdep lock state defines

Konstantin Khorenko khorenko at virtuozzo.com
Mon Nov 28 19:16:17 MSK 2022


From: Shuah Khan <skhan at linuxfoundation.org>

Adds defines for lock state returns from lock_is_held_type() based on
Johannes Berg's suggestions as it make it easier to read and maintain
the lock states. These are defines and a enum to avoid changes to
lock_is_held_type() and lockdep_is_held() return types.

Updates to lock_is_held_type() and  __lock_is_held() to use the new
defines.

Signed-off-by: Shuah Khan <skhan at linuxfoundation.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz at infradead.org>
Signed-off-by: Ingo Molnar <mingo at kernel.org>
Link: https://lore.kernel.org/linux-wireless/871rdmu9z9.fsf@codeaurora.org/

(cherry picked from ms commit f8cfa46608f8aa5ca5421ce281ab314129c15411)
Signed-off-by: Konstantin Khorenko <khorenko at virtuozzo.com>
---
 include/linux/lockdep.h | 11 +++++++++--
 kernel/lockdep.c        | 11 ++++++-----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 1aff47ff71fd..208a9a11de90 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -336,6 +336,11 @@ extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
 extern void lock_release(struct lockdep_map *lock, int nested,
 			 unsigned long ip);
 
+/* lock_is_held_type() returns */
+#define LOCK_STATE_UNKNOWN	-1
+#define LOCK_STATE_NOT_HELD	0
+#define LOCK_STATE_HELD		1
+
 /*
  * Same "read" as for lock_acquire(), except -1 means any.
  */
@@ -368,11 +373,13 @@ extern void lockdep_trace_alloc(gfp_t mask);
 #define lockdep_depth(tsk)	(debug_locks ? (tsk)->lockdep_depth : 0)
 
 #define lockdep_assert_held(l)	do {					\
-		WARN_ON(debug_locks && lockdep_is_held(l) == 0);	\
+		WARN_ON(debug_locks &&					\
+			lockdep_is_held(l) == LOCK_STATE_NOT_HELD);	\
 	} while (0)
 
 #define lockdep_assert_not_held(l)	do {				\
-		WARN_ON(debug_locks && lockdep_is_held(l) == 1);	\
+		WARN_ON(debug_locks &&					\
+			lockdep_is_held(l) == LOCK_STATE_HELD);		\
 	} while (0)
 
 #define lockdep_assert_held_exclusive(l)	do {			\
diff --git a/kernel/lockdep.c b/kernel/lockdep.c
index cdb82f1ce49a..195248405dcb 100644
--- a/kernel/lockdep.c
+++ b/kernel/lockdep.c
@@ -45,6 +45,7 @@
 #include <linux/bitops.h>
 #include <linux/gfp.h>
 #include <linux/kmemcheck.h>
+#include <linux/lockdep.h>
 
 #include <asm/sections.h>
 
@@ -3541,13 +3542,13 @@ static int __lock_is_held(struct lockdep_map *lock, int read)
 
 		if (match_held_lock(hlock, lock)) {
 			if (read == -1 || hlock->read == read)
-				return 1;
+				return LOCK_STATE_HELD;
 
-			return 0;
+			return LOCK_STATE_NOT_HELD;
 		}
 	}
 
-	return 0;
+	return LOCK_STATE_NOT_HELD;
 }
 
 /*
@@ -3655,14 +3656,14 @@ EXPORT_SYMBOL_GPL(lock_release);
 int lock_is_held_type(struct lockdep_map *lock, int read)
 {
 	unsigned long flags;
-	int ret = 0;
+	int ret = LOCK_STATE_NOT_HELD;
 
 	/*
 	 * Avoid false negative lockdep_assert_held() and
 	 * lockdep_assert_not_held().
 	 */
 	if (unlikely(current->lockdep_recursion))
-		return -1;
+		return LOCK_STATE_UNKNOWN;
 
 	raw_local_irq_save(flags);
 	check_flags(flags);
-- 
2.24.3



More information about the Devel mailing list