summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@linux.dev>2023-11-22 18:44:38 -0500
committerKent Overstreet <kent.overstreet@linux.dev>2024-01-16 16:48:39 -0500
commitdb93a4abdb10afa33a4d208cc71640e134310f4a (patch)
tree4d9e56059e527014b6a99cbac9a98ee260ff8b1e
parentf92bd4b00692241b278008de1f75e5185b50383d (diff)
locking/lockdep: lock_class_is_held()
This patch adds lock_class_is_held(), which can be used to assert that a particular type of lock is not held. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Will Deacon <will@kernel.org> Cc: Waiman Long <longman@redhat.com> Cc: Boqun Feng <boqun.feng@gmail.com>
-rw-r--r--include/linux/lockdep.h4
-rw-r--r--kernel/locking/lockdep.c20
2 files changed, 24 insertions, 0 deletions
diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 3b4a94f8f8ad..6d71214d38ad 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -347,6 +347,8 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
#define lockdep_repin_lock(l,c) lock_repin_lock(&(l)->dep_map, (c))
#define lockdep_unpin_lock(l,c) lock_unpin_lock(&(l)->dep_map, (c))
+int lock_class_is_held(struct lock_class_key *key);
+
/*
* Must use lock_map_aquire_try() with override maps to avoid
* lockdep thinking they participate in the block chain.
@@ -446,6 +448,8 @@ extern int lockdep_is_held(const void *);
#define lockdep_repin_lock(l, c) do { (void)(l); (void)(c); } while (0)
#define lockdep_unpin_lock(l, c) do { (void)(l); (void)(c); } while (0)
+static inline int lock_class_is_held(struct lock_class_key *key) { return 0; }
+
#define DEFINE_WAIT_OVERRIDE_MAP(_name, _wait_type) \
struct lockdep_map __maybe_unused _name = {}
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index b6bb9fcd992a..8b32744e26cd 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -6607,6 +6607,26 @@ void debug_check_no_locks_held(void)
}
EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
+#ifdef CONFIG_LOCKDEP
+int lock_class_is_held(struct lock_class_key *key)
+{
+ struct task_struct *curr = current;
+ struct held_lock *hlock;
+
+ if (unlikely(!debug_locks))
+ return 0;
+
+ for (hlock = curr->held_locks;
+ hlock < curr->held_locks + curr->lockdep_depth;
+ hlock++)
+ if (hlock->instance->key == key)
+ return 1;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(lock_class_is_held);
+#endif
+
#ifdef __KERNEL__
void debug_show_all_locks(void)
{