Introduce basic compatibility with cleanup.h infrastructure: introduce
DECLARE_LOCK_GUARD_*_ATTRS() helpers to add attributes to constructors
and destructors respectively.

Note: Due to the scoped cleanup helpers used for lock guards wrapping
acquire and release around their own constructors/destructors that store
pointers to the passed locks in a separate struct, we currently cannot
accurately annotate *destructors* which lock was released. While it's
possible to annotate the constructor to say which lock was acquired,
that alone would result in false positives claiming the lock was not
released on function return.

Instead, to avoid false positives, we can claim that the constructor
"assumes" that the taken lock is held via __assumes_cap().

This will ensure we can still benefit from the analysis where scoped
guards are used to protect access to guarded variables, while avoiding
false positives. The only downside are false negatives where we might
accidentally lock the same lock again:

        raw_spin_lock(&my_lock);
        ...
        guard(raw_spinlock)(&my_lock);  // no warning

Arguably, lockdep will immediately catch issues like this.

While Clang's analysis supports scoped guards in C++ [1], there's no way
to apply this to C right now. Better support for Linux's scoped guard
design could be added in future if deemed critical.

[1] https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#scoped-capability

Signed-off-by: Marco Elver <el...@google.com>
---
v3:
* Add *_ATTRS helpers instead of implicit __assumes_cap (suggested by Peter)
* __assert -> __assume rename
---
 include/linux/cleanup.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 2573585b7f06..54fc70d8da27 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -274,16 +274,21 @@ const volatile void * __must_check_fn(const volatile void 
*val)
 
 #define DEFINE_CLASS(_name, _type, _exit, _init, _init_args...)                
\
 typedef _type class_##_name##_t;                                       \
+typedef _type lock_##_name##_t;                                                
\
 static inline void class_##_name##_destructor(_type *p)                        
\
+       __no_capability_analysis                                        \
 { _type _T = *p; _exit; }                                              \
 static inline _type class_##_name##_constructor(_init_args)            \
+       __no_capability_analysis                                        \
 { _type t = _init; return t; }
 
 #define EXTEND_CLASS(_name, ext, _init, _init_args...)                 \
+typedef lock_##_name##_t lock_##_name##ext##_t;                                
\
 typedef class_##_name##_t class_##_name##ext##_t;                      \
 static inline void class_##_name##ext##_destructor(class_##_name##_t *p)\
 { class_##_name##_destructor(p); }                                     \
 static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
+       __no_capability_analysis \
 { class_##_name##_t t = _init; return t; }
 
 #define CLASS(_name, var)                                              \
@@ -461,12 +466,14 @@ _label:                                                   
                \
  */
 
 #define __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, ...)              \
+typedef _type lock_##_name##_t;                                                
\
 typedef struct {                                                       \
        _type *lock;                                                    \
        __VA_ARGS__;                                                    \
 } class_##_name##_t;                                                   \
                                                                        \
 static inline void class_##_name##_destructor(class_##_name##_t *_T)   \
+       __no_capability_analysis                                        \
 {                                                                      \
        if (!__GUARD_IS_ERR(_T->lock)) { _unlock; }                     \
 }                                                                      \
@@ -475,6 +482,7 @@ __DEFINE_GUARD_LOCK_PTR(_name, &_T->lock)
 
 #define __DEFINE_LOCK_GUARD_1(_name, _type, _lock)                     \
 static inline class_##_name##_t class_##_name##_constructor(_type *l)  \
+       __no_capability_analysis                                        \
 {                                                                      \
        class_##_name##_t _t = { .lock = l }, *_T = &_t;                \
        _lock;                                                          \
@@ -483,6 +491,7 @@ static inline class_##_name##_t 
class_##_name##_constructor(_type *l)       \
 
 #define __DEFINE_LOCK_GUARD_0(_name, _lock)                            \
 static inline class_##_name##_t class_##_name##_constructor(void)      \
+       __no_capability_analysis                                        \
 {                                                                      \
        class_##_name##_t _t = { .lock = (void*)1 },                    \
                         *_T __maybe_unused = &_t;                      \
@@ -490,6 +499,14 @@ static inline class_##_name##_t 
class_##_name##_constructor(void)  \
        return _t;                                                      \
 }
 
+#define DECLARE_LOCK_GUARD_0_ATTRS(_name, _lock, _unlock)              \
+static inline class_##_name##_t class_##_name##_constructor(void) _lock;\
+static inline void class_##_name##_destructor(class_##_name##_t *_T) _unlock;
+
+#define DECLARE_LOCK_GUARD_1_ATTRS(_name, _lock, _unlock)              \
+static inline class_##_name##_t class_##_name##_constructor(lock_##_name##_t 
*_T) _lock;\
+static inline void class_##_name##_destructor(class_##_name##_t *_T) _unlock;
+
 #define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...)         \
 __DEFINE_CLASS_IS_CONDITIONAL(_name, false);                           \
 __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, __VA_ARGS__)              \
-- 
2.51.0.384.g4c02a37b29-goog


Reply via email to