* [PATCH 1/3 v5] cleanup: Add cond_guard() to conditional guards
2024-02-17 10:59 [PATCH 0/3 v5] Add cond_guard() to conditional guards Fabio M. De Francesco
@ 2024-02-17 10:59 ` Fabio M. De Francesco
2024-02-19 11:51 ` Jonathan Cameron
2024-02-22 18:27 ` Dave Jiang
2024-02-17 10:59 ` [PATCH 2/3 v5] cxl/region: Use cond_guard() in show_targetN() Fabio M. De Francesco
2024-02-17 10:59 ` [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison() Fabio M. De Francesco
2 siblings, 2 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2024-02-17 10:59 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, linux-kernel
Cc: linux-cxl, Ingo Molnar, Dave Jiang, Jonathan Cameron, Ira Weiny,
Fabio M. De Francesco
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 statement-expression) that is passed as its
second argument. That statement (or statement-expression) is executed if
waiting for a lock is interrupted or if a _trylock() fails in case of
contention.
Usage example:
cond_guard(mutex_intr, return -EINTR, &mutex);
Consistent with other usage of _guard(), locks are unlocked at the exit of
the scope where cond_guard() is called. This macro can be called multiple
times in the same scope.
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Suggested-by: Dan Williams <dan.j.williams@intel.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Suggested-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
---
include/linux/cleanup.h | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index c2d09bc4f976..602afb85da34 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -134,6 +134,19 @@ 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' is a
+ * statement or statement-expression that is executed if waiting for a
+ * lock is interrupted or if a _trylock() fails in case of contention.
+ *
+ * Example:
+ *
+ * cond_guard(mutex_intr, return -EINTR, &mutex);
+ *
+ * This macro can be called multiple times in the same scope, for it
+ * declares unique instances of type 'name'.
+ *
* 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 +178,13 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
#define __guard_ptr(_name) class_##_name##_lock_ptr
+#define __cond_guard(__unique, _name, _fail, args...) \
+ CLASS(_name, __unique)(args); \
+ if (!__guard_ptr(_name)(&__unique)) _fail; \
+ else { }
+#define cond_guard(_name, _fail, args...) \
+ __cond_guard(__UNIQUE_ID(scope), _name, _fail, args)
+
#define scoped_guard(_name, args...) \
for (CLASS(_name, scope)(args), \
*done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
--
2.43.2
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 1/3 v5] cleanup: Add cond_guard() to conditional guards
2024-02-17 10:59 ` [PATCH 1/3 v5] cleanup: " Fabio M. De Francesco
@ 2024-02-19 11:51 ` Jonathan Cameron
2024-02-22 18:27 ` Dave Jiang
1 sibling, 0 replies; 8+ messages in thread
From: Jonathan Cameron @ 2024-02-19 11:51 UTC (permalink / raw)
To: Fabio M. De Francesco
Cc: Peter Zijlstra, Dan Williams, linux-kernel, linux-cxl,
Ingo Molnar, Dave Jiang, Ira Weiny
On Sat, 17 Feb 2024 11:59:02 +0100
"Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> 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 statement-expression) that is passed as its
> second argument. That statement (or statement-expression) is executed if
> waiting for a lock is interrupted or if a _trylock() fails in case of
> contention.
>
> Usage example:
>
> cond_guard(mutex_intr, return -EINTR, &mutex);
>
> Consistent with other usage of _guard(), locks are unlocked at the exit of
> the scope where cond_guard() is called. This macro can be called multiple
> times in the same scope.
>
> Cc: Dave Jiang <dave.jiang@intel.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Suggested-by: Dan Williams <dan.j.williams@intel.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Suggested-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
Looks good.
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 1/3 v5] cleanup: Add cond_guard() to conditional guards
2024-02-17 10:59 ` [PATCH 1/3 v5] cleanup: " Fabio M. De Francesco
2024-02-19 11:51 ` Jonathan Cameron
@ 2024-02-22 18:27 ` Dave Jiang
1 sibling, 0 replies; 8+ messages in thread
From: Dave Jiang @ 2024-02-22 18:27 UTC (permalink / raw)
To: Fabio M. De Francesco, Peter Zijlstra, Dan Williams, linux-kernel
Cc: linux-cxl, Ingo Molnar, Jonathan Cameron, Ira Weiny
On 2/17/24 3:59 AM, 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 statement-expression) that is passed as its
> second argument. That statement (or statement-expression) is executed if
> waiting for a lock is interrupted or if a _trylock() fails in case of
> contention.
>
> Usage example:
>
> cond_guard(mutex_intr, return -EINTR, &mutex);
>
> Consistent with other usage of _guard(), locks are unlocked at the exit of
> the scope where cond_guard() is called. This macro can be called multiple
> times in the same scope.
>
> Cc: Dave Jiang <dave.jiang@intel.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Suggested-by: Dan Williams <dan.j.williams@intel.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Suggested-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> include/linux/cleanup.h | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> index c2d09bc4f976..602afb85da34 100644
> --- a/include/linux/cleanup.h
> +++ b/include/linux/cleanup.h
> @@ -134,6 +134,19 @@ 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' is a
> + * statement or statement-expression that is executed if waiting for a
> + * lock is interrupted or if a _trylock() fails in case of contention.
> + *
> + * Example:
> + *
> + * cond_guard(mutex_intr, return -EINTR, &mutex);
> + *
> + * This macro can be called multiple times in the same scope, for it
> + * declares unique instances of type 'name'.
> + *
> * 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 +178,13 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
>
> #define __guard_ptr(_name) class_##_name##_lock_ptr
>
> +#define __cond_guard(__unique, _name, _fail, args...) \
> + CLASS(_name, __unique)(args); \
> + if (!__guard_ptr(_name)(&__unique)) _fail; \
> + else { }
> +#define cond_guard(_name, _fail, args...) \
> + __cond_guard(__UNIQUE_ID(scope), _name, _fail, args)
> +
> #define scoped_guard(_name, args...) \
> for (CLASS(_name, scope)(args), \
> *done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/3 v5] cxl/region: Use cond_guard() in show_targetN()
2024-02-17 10:59 [PATCH 0/3 v5] Add cond_guard() to conditional guards Fabio M. De Francesco
2024-02-17 10:59 ` [PATCH 1/3 v5] cleanup: " Fabio M. De Francesco
@ 2024-02-17 10:59 ` Fabio M. De Francesco
2024-02-17 10:59 ` [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison() Fabio M. De Francesco
2 siblings, 0 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2024-02-17 10:59 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, linux-kernel
Cc: linux-cxl, Ingo Molnar, Dave Jiang, Jonathan Cameron, Ira Weiny,
Fabio M. De Francesco
Use cond_guard() in show_target() to not open code an up_read() in an 'out'
block. If the down_read_interruptible() fails, the statement passed to the
second argument of cond_guard() returns -EINTR.
Cc: Peter Zijlstra <peterz@infradead.org>
Suggested-by: Dan Williams <dan.j.williams@intel.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Ira Weiny <ira.weiny@intel.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
---
drivers/cxl/core/region.c | 16 ++++------------
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index ce0e2d82bb2b..704102f75c14 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -666,28 +666,20 @@ static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos)
{
struct cxl_region_params *p = &cxlr->params;
struct cxl_endpoint_decoder *cxled;
- int rc;
- rc = down_read_interruptible(&cxl_region_rwsem);
- if (rc)
- return rc;
+ cond_guard(rwsem_read_intr, return -EINTR, &cxl_region_rwsem);
if (pos >= p->interleave_ways) {
dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos,
p->interleave_ways);
- rc = -ENXIO;
- goto out;
+ return -ENXIO;
}
cxled = p->targets[pos];
if (!cxled)
- rc = sysfs_emit(buf, "\n");
- else
- rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
-out:
- up_read(&cxl_region_rwsem);
+ return sysfs_emit(buf, "\n");
- return rc;
+ return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev));
}
static int match_free_decoder(struct device *dev, void *data)
--
2.43.2
^ permalink raw reply related [flat|nested] 8+ messages in thread* [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison()
2024-02-17 10:59 [PATCH 0/3 v5] Add cond_guard() to conditional guards Fabio M. De Francesco
2024-02-17 10:59 ` [PATCH 1/3 v5] cleanup: " Fabio M. De Francesco
2024-02-17 10:59 ` [PATCH 2/3 v5] cxl/region: Use cond_guard() in show_targetN() Fabio M. De Francesco
@ 2024-02-17 10:59 ` Fabio M. De Francesco
2024-02-19 11:52 ` Jonathan Cameron
2024-02-22 18:27 ` Dave Jiang
2 siblings, 2 replies; 8+ messages in thread
From: Fabio M. De Francesco @ 2024-02-17 10:59 UTC (permalink / raw)
To: Peter Zijlstra, Dan Williams, linux-kernel
Cc: linux-cxl, Ingo Molnar, Dave Jiang, Jonathan Cameron, Ira Weiny,
Fabio M. De Francesco
Use cond_guard() in cxl_inject_poison() to not open code two up_write()
in an 'out' block. If the down_read_interruptible() fail, the statements
passed as the second argument of cond_guard() return -EINTR.
Cc: Dave Jiang <dave.jiang@intel.com>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Suggested-by: Dan Williams <dan.j.williams@intel.com>
Suggested-by: Ira Weiny <ira.weiny@intel.com>
Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
---
drivers/cxl/core/memdev.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index dae8802ecdb0..bd97eea65bb0 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -331,19 +331,13 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
if (!IS_ENABLED(CONFIG_DEBUG_FS))
return 0;
- rc = down_read_interruptible(&cxl_region_rwsem);
- if (rc)
- return rc;
+ cond_guard(rwsem_read_intr, return -EINTR, &cxl_region_rwsem);
- rc = down_read_interruptible(&cxl_dpa_rwsem);
- if (rc) {
- up_read(&cxl_region_rwsem);
- return rc;
- }
+ cond_guard(rwsem_read_intr, return -EINTR, &cxl_dpa_rwsem);
rc = cxl_validate_poison_dpa(cxlmd, dpa);
if (rc)
- goto out;
+ return rc;
inject.address = cpu_to_le64(dpa);
mbox_cmd = (struct cxl_mbox_cmd) {
@@ -353,7 +347,7 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
};
rc = cxl_internal_send_cmd(mds, &mbox_cmd);
if (rc)
- goto out;
+ return rc;
cxlr = cxl_dpa_to_region(cxlmd, dpa);
if (cxlr)
@@ -366,11 +360,8 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
.length = cpu_to_le32(1),
};
trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_INJECT);
-out:
- up_read(&cxl_dpa_rwsem);
- up_read(&cxl_region_rwsem);
- return rc;
+ return 0;
}
EXPORT_SYMBOL_NS_GPL(cxl_inject_poison, CXL);
--
2.43.2
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison()
2024-02-17 10:59 ` [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison() Fabio M. De Francesco
@ 2024-02-19 11:52 ` Jonathan Cameron
2024-02-22 18:27 ` Dave Jiang
1 sibling, 0 replies; 8+ messages in thread
From: Jonathan Cameron @ 2024-02-19 11:52 UTC (permalink / raw)
To: Fabio M. De Francesco
Cc: Peter Zijlstra, Dan Williams, linux-kernel, linux-cxl,
Ingo Molnar, Dave Jiang, Ira Weiny
On Sat, 17 Feb 2024 11:59:04 +0100
"Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote:
> Use cond_guard() in cxl_inject_poison() to not open code two up_write()
> in an 'out' block. If the down_read_interruptible() fail, the statements
> passed as the second argument of cond_guard() return -EINTR.
>
> Cc: Dave Jiang <dave.jiang@intel.com>
> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Suggested-by: Dan Williams <dan.j.williams@intel.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison()
2024-02-17 10:59 ` [PATCH 3/3 v5] cxl/memdev: Use cond_guard() in cxl_inject_poison() Fabio M. De Francesco
2024-02-19 11:52 ` Jonathan Cameron
@ 2024-02-22 18:27 ` Dave Jiang
1 sibling, 0 replies; 8+ messages in thread
From: Dave Jiang @ 2024-02-22 18:27 UTC (permalink / raw)
To: Fabio M. De Francesco, Peter Zijlstra, Dan Williams, linux-kernel
Cc: linux-cxl, Ingo Molnar, Jonathan Cameron, Ira Weiny
On 2/17/24 3:59 AM, Fabio M. De Francesco wrote:
> Use cond_guard() in cxl_inject_poison() to not open code two up_write()
> in an 'out' block. If the down_read_interruptible() fail, the statements
> passed as the second argument of cond_guard() return -EINTR.
>
> Cc: Dave Jiang <dave.jiang@intel.com>
> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Suggested-by: Dan Williams <dan.j.williams@intel.com>
> Suggested-by: Ira Weiny <ira.weiny@intel.com>
> Signed-off-by: Fabio M. De Francesco <fabio.maria.de.francesco@linux.intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> drivers/cxl/core/memdev.c | 19 +++++--------------
> 1 file changed, 5 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
> index dae8802ecdb0..bd97eea65bb0 100644
> --- a/drivers/cxl/core/memdev.c
> +++ b/drivers/cxl/core/memdev.c
> @@ -331,19 +331,13 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
> if (!IS_ENABLED(CONFIG_DEBUG_FS))
> return 0;
>
> - rc = down_read_interruptible(&cxl_region_rwsem);
> - if (rc)
> - return rc;
> + cond_guard(rwsem_read_intr, return -EINTR, &cxl_region_rwsem);
>
> - rc = down_read_interruptible(&cxl_dpa_rwsem);
> - if (rc) {
> - up_read(&cxl_region_rwsem);
> - return rc;
> - }
> + cond_guard(rwsem_read_intr, return -EINTR, &cxl_dpa_rwsem);
>
> rc = cxl_validate_poison_dpa(cxlmd, dpa);
> if (rc)
> - goto out;
> + return rc;
>
> inject.address = cpu_to_le64(dpa);
> mbox_cmd = (struct cxl_mbox_cmd) {
> @@ -353,7 +347,7 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
> };
> rc = cxl_internal_send_cmd(mds, &mbox_cmd);
> if (rc)
> - goto out;
> + return rc;
>
> cxlr = cxl_dpa_to_region(cxlmd, dpa);
> if (cxlr)
> @@ -366,11 +360,8 @@ int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa)
> .length = cpu_to_le32(1),
> };
> trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_INJECT);
> -out:
> - up_read(&cxl_dpa_rwsem);
> - up_read(&cxl_region_rwsem);
>
> - return rc;
> + return 0;
> }
> EXPORT_SYMBOL_NS_GPL(cxl_inject_poison, CXL);
>
^ permalink raw reply [flat|nested] 8+ messages in thread