Skip to contentSkip to conversation

lockdep: introduce lockdep_assert_not_held()

PATCH In review David Sterba 2 participants 2 messages Jun 13, 2019 – Jun 28, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lockdep: introduce lockdep_assert_not_held()

Add an assertion that a lock is not held, suitable for the following (simplified) usecase in filesystems:

- filesystem write
- lock(&big_filesystem_lock)
- kmalloc(GFP_KERNEL)
- trigger dirty data write to get more memory
- find dirty pages
- call filesystem write
- lock(&big_filesystem_lock)
deadlock

The cause here is the use of GFP_KERNEL that does not exclude poking filesystems to allow freeing some memory. Such scenario is a bug, so the use of GFP_NOFS is the right flag.

The annotation can help catch such bugs during development because the actual deadlock could be hard to hit in practice.

---
 Documentation/locking/lockdep-design.txt | 5 +++++
 include/linux/lockdep.h                  | 5 +++++
 2 files changed, 10 insertions(+)
Documentation/locking/lockdep-design.txt +5 -0
index 39fae143c9cb..8b3013aaf518 100644
@@ -211,6 +211,11 @@ that nobody tampered with the lock, e.g. kernel/sched/sched.h
 	lockdep_unpin_lock(&rq->lock, rf->cookie);
   }
 
+In some contexts it is useful to assert that a given lock is not held.  A
+typical example are filesystems that must avoid recursion to their code when
+a memory allocation triggers write of dirty data. When the allocation is done
+with a lock taken, re-entering the code would cause a deadlock.
+
 While comments about locking requirements might provide useful information,
 the runtime checks performed by annotations are invaluable when debugging
 locking problems and they carry the same level of details when inspecting
include/linux/lockdep.h +5 -0
index 6e2377e6c1d6..a6682104bd95 100644
@@ -397,6 +397,10 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
 		WARN_ON_ONCE(debug_locks && !lockdep_is_held(l));	\
 	} while (0)
 
+#define lockdep_assert_not_held(l)	do {			\
+		WARN_ON(debug_locks && lockdep_is_held(l));	\
+	} while (0)
+
 #define lockdep_recursing(tsk)	((tsk)->lockdep_recursion)
 
 #define lockdep_pin_lock(l)	lock_pin_lock(&(l)->dep_map)
@@ -469,6 +473,7 @@ struct lockdep_map { };
 #define lockdep_assert_held_exclusive(l)	do { (void)(l); } while (0)
 #define lockdep_assert_held_read(l)		do { (void)(l); } while (0)
 #define lockdep_assert_held_once(l)		do { (void)(l); } while (0)
+#define lockdep_assert_not_held(l)		do { (void)(l); } while (0)
 
 #define lockdep_recursing(tsk)			(0)
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 12 lines · David Sterba

Add an assertion that a lock is not held, suitable for the following (simplified) usecase in filesystems:

- filesystem write
- lock(&big_filesystem_lock)
- kmalloc(GFP_KERNEL)
- trigger dirty data write to get more memory
- find dirty pages
- call filesystem write
- lock(&big_filesystem_lock)
deadlock

Quoted 8 lines

The cause here is the use of GFP_KERNEL that does not exclude poking filesystems to allow freeing some memory. Such scenario is a bug, so the use of GFP_NOFS is the right flag.

The annotation can help catch such bugs during development because the actual deadlock could be hard to hit in practice.

Any comments on that? I just found another case with convoluted callstacks where the lockdep assertion would catch the potential lock up earlier than under the testing load.