RE: [PATCH 1/2 v2] cleanup: Add cond_guard() to conditional guards

From: Dan Williams
Date: Mon Feb 05 2024 - 14:08:16 EST


Fabio M. De Francesco wrote:
> Add cond_guard() macro to conditional guards.
>
> cond_guard() is a guard to be used with the conditional variants of locks,
> like down_read_trylock() or mutex_lock_interruptible().
>
> It takes a statement (or more statements in a block) that is passed to its
> second argument. That statement (or block) is executed if waiting for a
> lock is interrupted or if a _trylock() fails in case of contention.
>
> Usage example:
>
> cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore);
>
> Consistently with the other guards, locks are unlocked at the exit of the
> scope where cond_guard() is called.
>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Suggested-by: Dan Williams <dan.j.williams@xxxxxxxxx>
> Suggested-by: Ira Weiny <ira.weiny@xxxxxxxxx>
> Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@xxxxxxxxxxxxxxx>
> ---
> include/linux/cleanup.h | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> index c2d09bc4f976..88af56600325 100644
> --- a/include/linux/cleanup.h
> +++ b/include/linux/cleanup.h
> @@ -134,6 +134,16 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
> * an anonymous instance of the (guard) class, not recommended for
> * conditional locks.
> *
> + * cond_guard(name, fail, args...):
> + * a guard to be used with the conditional variants of locks, like
> + * down_read_trylock() or mutex_lock_interruptible. 'fail' are one or more
> + * statements that are executed when waiting for a lock is interrupted or
> + * when a _trylock() fails in case of contention.
> + *
> + * Example:
> + *
> + * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore);

That _fail argument likely needs to be a statement expression for the
multi-statement case.

> + *
> * scoped_guard (name, args...) { }:
> * similar to CLASS(name, scope)(args), except the variable (with the
> * explicit name 'scope') is declard in a for-loop such that its scope is
> @@ -165,6 +175,10 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
>
> #define __guard_ptr(_name) class_##_name##_lock_ptr
>
> +#define cond_guard(_name, _fail, args...) \
> + CLASS(_name, scope)(args); \
> + if (!__guard_ptr(_name)(&scope)) _fail

No, as I stated before this is broken for usages of:

if () cond_guard() else if ()

The 'else' in the definition is critical, this builds for me (untested):

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 88af56600325..665407498781 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -142,7 +142,7 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
*
* Example:
*
- * cond_guard(rwsem_read_try, { printk(...); return 0; }, &semaphore);
+ * cond_guard(rwsem_read_try, ({ printk(...); return 0; }), &semaphore);
*
* scoped_guard (name, args...) { }:
* similar to CLASS(name, scope)(args), except the variable (with the
@@ -177,7 +177,8 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \

#define cond_guard(_name, _fail, args...) \
CLASS(_name, scope)(args); \
- if (!__guard_ptr(_name)(&scope)) _fail
+ if (!__guard_ptr(_name)(&scope)) _fail; \
+ else

#define scoped_guard(_name, args...) \
for (CLASS(_name, scope)(args), \