To cover scenarios where the scope of the guard differs from the scope
of its activation, introduce DEFINE_INACTIVE_GUARD() and activate_guard().

Here is an example use for a conditionally activated guard variable:

void func(bool a)
{
        DEFINE_INACTIVE_GUARD(preempt_notrace, myguard);

        [...]
        if (a) {
                might_sleep();
                activate_guard(preempt_notrace, myguard)();
        }
        [ protected code ]
}

Signed-off-by: Mathieu Desnoyers <[email protected]>
Cc: Peter Zijlstra (Intel) <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Greg KH <[email protected]>
Cc: Sean Christopherson <[email protected]>
---
 include/linux/cleanup.h | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 1247e67a6161..5f031cce97ca 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -149,12 +149,20 @@ static inline class_##_name##_t 
class_##_name##ext##_constructor(_init_args) \
  *      similar to scoped_guard(), except it does fail when the lock
  *      acquire fails.
  *
+ * DEFINE_INACTIVE_GUARD(name, var):
+ *      define an inactive guard variable in a given scope, initialized to 
NULL.
+ *
+ * activate_guard(name, var)(args...):
+ *      activate a guard variable with its constructor, if it is not already
+ *      activated.
  */
 
 #define DECLARE_GUARD(_name, _type, _lock, _unlock) \
        DECLARE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), 
_type _T); \
        static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \
-       { return *_T; }
+       { return *_T; } \
+       static inline class_##_name##_t class_##_name##_null(void) \
+       { return NULL; }
 
 #define DECLARE_GUARD_COND(_name, _ext, _condlock) \
        EXTEND_CLASS(_name, _ext, \
@@ -178,6 +186,14 @@ static inline class_##_name##_t 
class_##_name##ext##_constructor(_init_args) \
                if (!__guard_ptr(_name)(&scope)) _fail; \
                else
 
+#define DEFINE_INACTIVE_GUARD(_name, _var) \
+       class_##_name##_t _var __cleanup(class_##_name##_destructor) = \
+               class_##_name##_null()
+
+#define activate_guard(_name, _var) \
+       if (!class_##_name##_lock_ptr(&(_var))) \
+               _var = class_##_name##_constructor
+
 /*
  * Additional helper macros for generating lock guards with types, either for
  * locks that don't have a native type (eg. RCU, preempt) or those that need a
@@ -212,6 +228,11 @@ static inline void 
class_##_name##_destructor(class_##_name##_t *_T)       \
 static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T)    \
 {                                                                      \
        return _T->lock;                                                \
+}                                                                      \
+static inline class_##_name##_t class_##_name##_null(void)             \
+{                                                                      \
+       class_##_name##_t _t = { .lock = NULL };                        \
+       return _t;                                                      \
 }
 
 
-- 
2.39.2


Reply via email to