]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: assert debug mutex lock is not held if !recursive
authorPatrick Donnelly <pdonnell@redhat.com>
Sat, 28 Sep 2024 01:21:34 +0000 (21:21 -0400)
committerPatrick Donnelly <pdonnell@redhat.com>
Wed, 2 Oct 2024 14:44:52 +0000 (10:44 -0400)
There's appropriate checks for unlock and post-lock but nothing to stop the
undefined behavior of a double-lock on a non-recursive mutex.

Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
src/common/mutex_debug.h

index c1a4ff2a435015e659815ced6a4b69f24bb55c6b..d56d0ebee9987ef38a69e36fdcb80048bc49c264 100644 (file)
@@ -169,20 +169,16 @@ public:
   }
 
   bool try_lock(bool no_lockdep = false) {
-    bool locked = try_lock_impl();
-    if (locked) {
-      if (enable_lockdep(no_lockdep))
-       _locked();
-      _post_lock();
-    }
-    return locked;
+    ceph_assert(recursive || !is_locked_by_me());
+    return _try_lock(no_lockdep);
   }
 
   void lock(bool no_lockdep = false) {
+    ceph_assert(recursive || !is_locked_by_me());
     if (enable_lockdep(no_lockdep))
       _will_lock(recursive);
 
-    if (try_lock(no_lockdep))
+    if (_try_lock(no_lockdep))
       return;
 
     lock_impl();
@@ -198,6 +194,16 @@ public:
     unlock_impl();
   }
 
+private:
+  bool _try_lock(bool no_lockdep) {
+    bool locked = try_lock_impl();
+    if (locked) {
+      if (enable_lockdep(no_lockdep))
+       _locked();
+      _post_lock();
+    }
+    return locked;
+  }
 };