diff mbox series

[v5,3/8] cleanup.h: Introduce DEFINE_INACTIVE_GUARD and activate_guard

Message ID 20240627152340.82413-4-mathieu.desnoyers@efficios.com (mailing list archive)
State Superseded
Headers show
Series Faultable Tracepoints | expand

Commit Message

Mathieu Desnoyers June 27, 2024, 3:23 p.m. UTC
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 <mathieu.desnoyers@efficios.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Kees Cook <keescook@chromium.org>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Sean Christopherson <seanjc@google.com>
---
 include/linux/cleanup.h | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

Comments

Steven Rostedt Aug. 19, 2024, 11 p.m. UTC | #1
On Thu, 27 Jun 2024 11:23:35 -0400
Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:

> 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 ]
> }
> 

Hi Mathieu,

The three cleanup patches fail to apply (I believe one has already been
fixed by Ingo too). Could you have the clean up patches be a separate
series that is likely to get in, especially since it's more of a moving
target.

Thanks,

-- Steve
Mathieu Desnoyers Aug. 28, 2024, 1:58 p.m. UTC | #2
On 2024-08-20 01:00, Steven Rostedt wrote:
> On Thu, 27 Jun 2024 11:23:35 -0400
> Mathieu Desnoyers <mathieu.desnoyers@efficios.com> wrote:
> 
>> 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 ]
>> }
>>
> 
> Hi Mathieu,
> 
> The three cleanup patches fail to apply (I believe one has already been
> fixed by Ingo too). Could you have the clean up patches be a separate
> series that is likely to get in, especially since it's more of a moving
> target.

Then it would make sense to split this into two series: one for the
guard stuff, and one for tracing which depends on the first one.

I'll do that.

Mathieu

> 
> Thanks,
> 
> -- Steve
diff mbox series

Patch

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 04f03ad5f25d..d6a3d8099d77 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -146,12 +146,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, \
@@ -175,6 +183,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
@@ -209,6 +225,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;							\
 }