cleanup: Introduce ACQUIRE(), a guard() for conditional locks
Subsystems (3)
v2 cover cleanup: Introduce ACQUIRE(), a guard() for conditional locks
Changes since v1: [1]
* Peter took one look at v1 and rewrote it into something significantly better. Unlike my attempt that required suffering a new parallel universe of lock guards, the rewrite reuses existing lock guards. ACQUIRE() can be used any place guard() can be used, and adds ACQUIRE_ERR() to pass the result of conditional locks.
[1]: http://lore.kernel.org/[email protected]
Note, all the code in patch1 is Peter's I just wrapped it in a changelog and added some commentary. Peter, forgive me if you were still in the process of circling back to this topic. I marked the patch attributed to you as: "Not-yet-signed-off-by". Otherwise, my motivation for going ahead with a formal submission are the multiple patchsets in my review / development queue where I would like to use ACQUIRE().
The orginal motivation of v1 for this work is that the CXL subsystem adopted scope-based helpers and achieved some decent cleanups. However, that work stalled with conditional locks. It stalled due to the pain points of scoped_cond_guard() detailed in patch1.
This work also allows for replacing open-coded equivalents like rwsem_read_intr_acquire() that went upstream in v6.16:
0c6e6f1357cb cxl/edac: Add CXL memory device patrol scrub control feature
The open question from the discussion [2] was whether it was worth defining a __GUARD_IS_ERR() asm helper. I left that alone.
Lastly, this version of ACQUIRE_ERR() matches Peter's original proposal to have the caller pass the lock scope variable by reference [3]. My change of heart came from looking at the conversion and wanting ACQUIRE_ERR() to be more visually distinct from ACQUIRE() especially because it is accessing lock condition metadata, not the lock itself.
E.g.
ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region);
if ((ret = ACQUIRE_ERR(rwsem_read_intr, &rwsem)))
return ret;Yes, checkpatch disagrees with assignment in if(), but cleanup.h already demands other expections for historical style, and a compact / limited idiom for ACQUIRE_ERR() feels reasonable.
[2]: http://lore.kernel.org/[email protected]
[3]: http://lore.kernel.org/[email protected]
Dan Williams (7):
cxl/mbox: Convert poison list mutex to ACQUIRE() cxl/decoder: Move decoder register programming to a helper cxl/decoder: Drop pointless locking
cxl/region: Split commit_store() into __commit() and queue_reset()
helpers
cxl/region: Move ready-to-probe state check to a helper cxl/region: Introduce CLASS(cxl_decoder_detach...) consolidate
multiple paths
cxl: Convert to ACQUIRE() for conditional rwsem locking
Peter Zijlstra (1):
cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
drivers/cxl/core/cdat.c | 6 +- drivers/cxl/core/core.h | 60 +++++- drivers/cxl/core/edac.c | 44 ++-- drivers/cxl/core/hdm.c | 118 +++++----- drivers/cxl/core/mbox.c | 13 +- drivers/cxl/core/memdev.c | 50 ++--- drivers/cxl/core/port.c | 24 +-- drivers/cxl/core/region.c | 443 +++++++++++++++++++------------------- drivers/cxl/cxl.h | 13 +- drivers/cxl/cxlmem.h | 4 +- include/linux/cleanup.h | 77 +++++-- include/linux/mutex.h | 2 +- include/linux/rwsem.h | 3 +- 13 files changed, 460 insertions(+), 397 deletions(-)
base-commit: e04c78d86a9699d136910cfc0bdcf01087e3267e
Signature · 1 line
2.49.0
PATCH v2 1/8 cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
From: Peter Zijlstra <[email protected]>
scoped_cond_guard(), automatic cleanup for conditional locks, has a couple pain points:
* It causes existing straight-line code to be re-indented into a new bracketed scope. While this can be mitigated by a new helper function to contain the scope, that is not always a comfortable conversion.
* The return code from the conditional lock is tossed in favor of a scheme to pass a 'return err;' statement to the macro.
Other attempts to clean this up, to behave more like guard() [1], got hung up trying to both establish and evaluate the conditional lock in one statement.
ACQUIRE() solves this by reflecting the result of the condition in the automatic variable established by the lock CLASS(). The result is separately retrieved with the ACQUIRE_ERR() helper, effectively a PTR_ERR() operation.
--- include/linux/cleanup.h | 77 ++++++++++++++++++++++++++++++++++------- include/linux/mutex.h | 2 +- include/linux/rwsem.h | 2 +- 3 files changed, 67 insertions(+), 14 deletions(-)
include/linux/cleanup.h +65 -12
@@ -3,6 +3,8 @@#define _LINUX_CLEANUP_H#include <linux/compiler.h> +#include <linux/err.h> +#include <linux/args.h> /** * DOC: scope-based cleanup helpers
@@ -61,9 +63,21 @@* Observe the lock is held for the remainder of the "if ()" block not * the remainder of "func()". * -* Now, when a function uses both __free() and guard(), or multiple -* instances of __free(), the LIFO order of variable definition order -* matters. GCC documentation says: +* The ACQUIRE() macro can be used in all places that guard() can be +* used and additionally support conditional locks +* +* +* DEFINE_GUARD_COND(pci_dev, _try, pci_dev_trylock(_T)) +* ... +* ACQUIRE(pci_dev_try, lock)(dev); +* rc = ACQUIRE_ERR(pci_dev_try, &lock); +* if (rc) +* return rc; +* // @lock is held +* +* Now, when a function uses both __free() and guard()/ACQUIRE(), or +* multiple instances of __free(), the LIFO order of variable definition +* order matters. GCC documentation says: * * "When multiple variables in the same scope have cleanup attributes, * at exit from the scope their associated cleanup functions are run in
@@ -305,14 +319,32 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \* acquire fails. * * Only for conditional locks. +* +* ACQUIRE(name, var): +* a named instance of the (guard) class, suitable for conditional +* locks when paired with ACQUIRE_ERR(). +* +* ACQUIRE_ERR(name, &var): +* a helper that is effectively a PTR_ERR() conversion of the guard +* pointer. Returns 0 when the lock was acquired and a negative +* error code otherwise. */ #define __DEFINE_CLASS_IS_CONDITIONAL (_name, _is_cond) \ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond +#define __GUARD_IS_ERR (_ptr) \ + ({ unsigned long _rc = (__force unsigned long)(_ptr); \ + unlikely((_rc-1) >= -MAX_ERRNO-1); }) +#define __DEFINE_GUARD_LOCK_PTR (_name, _exp) \ static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \ - { return (void *)(__force unsigned long)*(_exp); } + { void *_ptr = (void *)(__force unsigned long)*(_exp); \ + if (IS_ERR(_ptr)) { _ptr = NULL; } return _ptr; } \ + static inline int class_##_name##_lock_err(class_##_name##_t *_T) \ + { long _rc = (__force unsigned long)*(_exp); \ + if (!_rc) { _rc = -EBUSY; } if (!IS_ERR_VALUE(_rc)) { _rc = 0; } \ + return _rc; }#define DEFINE_CLASS_IS_GUARD (_name) \ __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \
@@ -323,23 +355,37 @@ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond __DEFINE_GUARD_LOCK_PTR(_name, _T)#define DEFINE_GUARD (_name, _type, _lock, _unlock) \ - DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \ + DEFINE_CLASS(_name, _type, if (!__GUARD_IS_ERR(_T)) { _unlock; }, ({ _lock; _T; }), _type _T); \ DEFINE_CLASS_IS_GUARD(_name) -#define DEFINE_GUARD_COND (_name, _ext, _condlock) \ +#define DEFINE_GUARD_COND_4 (_name, _ext, _lock, _cond) \ __DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \ EXTEND_CLASS(_name, _ext, \ - ({ void *_t = _T; if (_T && !(_condlock)) _t = NULL; _t; }), \ + ({ void *_t = _T; int _RET = (_lock); if (_T && !(_cond)) _t = ERR_PTR(_RET); _t; }), \ class_##_name##_t _T) \ static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ - { return class_##_name##_lock_ptr(_T); } + { return class_##_name##_lock_ptr(_T); } \ + static inline int class_##_name##_ext##_lock_err(class_##_name##_t *_T) \ + { return class_##_name##_lock_err(_T); } + +/* +* Default binary condition; success on 'true'. +*/ +#define DEFINE_GUARD_COND_3 (_name, _ext, _lock) \ + DEFINE_GUARD_COND_4(_name, _ext, _lock, _RET) + +#define DEFINE_GUARD_COND(X...) CONCATENATE(DEFINE_GUARD_COND_, COUNT_ARGS(X))(X)#define guard (_name) \ CLASS(_name, __UNIQUE_ID(guard))#define __guard_ptr (_name) class_##_name##_lock_ptr +#define __guard_err (_name) class_##_name##_lock_err#define __is_cond_ptr (_name) class_##_name##_is_conditional +#define ACQUIRE (_name, _var) CLASS(_name, _var) +#define ACQUIRE_ERR (_name, _var) __guard_err(_name)(_var) +/* * Helper macro for scoped_guard(). *
@@ -401,7 +447,7 @@ typedef struct { \ \ static inline void class_##_name##_destructor(class_##_name##_t *_T) \ { \ -if (_T->lock) { _unlock; } \ +if (!__GUARD_IS_ERR (_T->lock)) { _unlock; } \ } \ \__DEFINE_GUARD_LOCK_PTR (_name, &_T->lock)
@@ -433,15 +479,22 @@ __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ __DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \ __DEFINE_LOCK_GUARD_0(_name, _lock) -#define DEFINE_LOCK_GUARD_1_COND (_name, _ext, _condlock) \ +#define DEFINE_LOCK_GUARD_1_COND_4 (_name, _ext, _lock, _cond) \ __DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \ EXTEND_CLASS(_name, _ext, \ ({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\ - if (_T->lock && !(_condlock)) _T->lock = NULL; \ + int _RET = (_lock); \ + if (_T->lock && !(_cond)) _T->lock = ERR_PTR(_RET);\ _t; }), \ typeof_member(class_##_name##_t, lock) l) \ static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ - { return class_##_name##_lock_ptr(_T); } + { return class_##_name##_lock_ptr(_T); } \ + static inline int class_##_name##_ext##_lock_err(class_##_name##_t *_T) \ + { return class_##_name##_lock_err(_T); } + +#define DEFINE_LOCK_GUARD_1_COND_3 (_name, _ext, _lock) \ + DEFINE_LOCK_GUARD_1_COND_4(_name, _ext, _lock, _RET) +#define DEFINE_LOCK_GUARD_1_COND(X...) CONCATENATE(DEFINE_LOCK_GUARD_1_COND_, COUNT_ARGS(X))(X)#endif /* _LINUX_CLEANUP_H */
include/linux/mutex.h +1 -1
@@ -224,7 +224,7 @@ extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T)) DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T)) -DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0) +DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T), _RET == 0)extern unsigned long mutex_get_owner (struct mutex *lock);
include/linux/rwsem.h +1 -1
@@ -240,7 +240,7 @@ extern void up_write(struct rw_semaphore *sem); DEFINE_GUARD(rwsem_read, struct rw_semaphore *, down_read(_T), up_read(_T)) DEFINE_GUARD_COND(rwsem_read, _try, down_read_trylock(_T)) -DEFINE_GUARD_COND(rwsem_read, _intr, down_read_interruptible(_T) == 0) +DEFINE_GUARD_COND(rwsem_read, _intr, down_read_interruptible(_T), _RET == 0) DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T))DEFINE_GUARD_COND (rwsem_write, _try,down_write_trylock (_T))
Signature · 1 line
2.49.0
Re: [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
Quoted 21 lines · Dan Williams
From: Peter Zijlstra <[email protected]>
scoped_cond_guard(), automatic cleanup for conditional locks, has a couple pain points:* It causes existing straight-line code to be re-indented into a new bracketed scope. While this can be mitigated by a new helper function to contain the scope, that is not always a comfortable conversion.
* The return code from the conditional lock is tossed in favor of a scheme to pass a 'return err;' statement to the macro.
Other attempts to clean this up, to behave more like
guard()[1], got hung up trying to both establish and evaluate the conditional lock in one statement.
ACQUIRE()solves this by reflecting the result of the condition in the automatic variable established by the lockCLASS(). The result is separately retrieved with theACQUIRE_ERR()helper, effectively aPTR_ERR()operation.
Quoted 11 lines
Cc Ingo Molnar <[email protected]>Cc Linus Torvalds <[email protected]>Cc David Lechner <[email protected]>Cc Fabio M. De Francesco <[email protected]>Not-yet-signed-off-by Peter Zijlstra <[email protected]>[djbw: wrap Peter's proposal with changelog and comments]Co-developed-by Dan Williams <[email protected]>Signed-off-by Dan Williams <[email protected]>
With Peter's sign-off, pushed to for-6.17/cleanup-acquire as a stable commit that others can reference:
https://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl.git/log/?h=for-6.17/cleanup-acquire
Re: [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
In reply to Dan WilliamsQuoted 21 lines · On Wed, 18 Jun 2025 22:04:09 -0700 Dan Williams <[email protected]>…
From: Peter Zijlstra <[email protected]>
scoped_cond_guard(), automatic cleanup for conditional locks, has a couple pain points:* It causes existing straight-line code to be re-indented into a new bracketed scope. While this can be mitigated by a new helper function to contain the scope, that is not always a comfortable conversion.
* The return code from the conditional lock is tossed in favor of a scheme to pass a 'return err;' statement to the macro.
Other attempts to clean this up, to behave more like
guard()[1], got hung up trying to both establish and evaluate the conditional lock in one statement.
ACQUIRE()solves this by reflecting the result of the condition in the automatic variable established by the lockCLASS(). The result is separately retrieved with theACQUIRE_ERR()helper, effectively aPTR_ERR()operation.
Quoted 11 lines
Cc Ingo Molnar <[email protected]>Cc Linus Torvalds <[email protected]>Cc David Lechner <[email protected]>Cc Fabio M. De Francesco <[email protected]>Not-yet-signed-off-by Peter Zijlstra <[email protected]>[djbw: wrap Peter's proposal with changelog and comments]Co-developed-by Dan Williams <[email protected]>Signed-off-by Dan Williams <[email protected]>
This looks like a nice solution. One trivial style thing inline.
include/linux/cleanup.h static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ · line 338
Quoted 13 lines
--- include/linux/cleanup.h | 77 ++++++++++++++++++++++++++++++++++------- include/linux/mutex.h | 2 +- include/linux/rwsem.h | 2 +- 3 files changed, 67 insertions(+), 14 deletions(-)include/linux/cleanup.h +3
-0+#define __GUARD_IS_ERR (_ptr) \ + ({ unsigned long _rc = (__force unsigned long)(_ptr); \ + unlikely((_rc-1) >= -MAX_ERRNO-1); })
Trivial but I'd have added spaces to make this
unlikely((_rc - 1) >= -MAX_ERRNO - 1); })
Quoted 11 lines
include/linux/cleanup.h +7
-1+#define __DEFINE_GUARD_LOCK_PTR (_name, _exp) \ static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \ - { return (void *)(__force unsigned long)*(_exp); } + { void *_ptr = (void *)(__force unsigned long)*(_exp); \ + if (IS_ERR(_ptr)) { _ptr = NULL; } return _ptr; } \ + static inline int class_##_name##_lock_err(class_##_name##_t *_T) \ + { long _rc = (__force unsigned long)*(_exp); \ + if (!_rc) { _rc = -EBUSY; } if (!IS_ERR_VALUE(_rc)) { _rc = 0; } \ + return _rc; }
Re: [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
Quoted 52 lines · Jonathan Cameron
Quoted 21 lines · On Wed, 18 Jun 2025 22:04:09 -0700 Dan Williams <[email protected]>…
From: Peter Zijlstra <[email protected]>
scoped_cond_guard(), automatic cleanup for conditional locks, has a couple pain points:* It causes existing straight-line code to be re-indented into a new bracketed scope. While this can be mitigated by a new helper function to contain the scope, that is not always a comfortable conversion.
* The return code from the conditional lock is tossed in favor of a scheme to pass a 'return err;' statement to the macro.
Other attempts to clean this up, to behave more like
guard()[1], got hung up trying to both establish and evaluate the conditional lock in one statement.
ACQUIRE()solves this by reflecting the result of the condition in the automatic variable established by the lockCLASS(). The result is separately retrieved with theACQUIRE_ERR()helper, effectively aPTR_ERR()operation.Quoted 11 lines
Cc Ingo Molnar <[email protected]>Cc Linus Torvalds <[email protected]>Cc David Lechner <[email protected]>Cc Fabio M. De Francesco <[email protected]>Not-yet-signed-off-by Peter Zijlstra <[email protected]>[djbw: wrap Peter's proposal with changelog and comments]Co-developed-by Dan Williams <[email protected]>Signed-off-by Dan Williams <[email protected]>This looks like a nice solution. One trivial style thing inline.
include/linux/cleanup.h static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ · line 338
Quoted 13 lines
--- include/linux/cleanup.h | 77 ++++++++++++++++++++++++++++++++++------- include/linux/mutex.h | 2 +- include/linux/rwsem.h | 2 +- 3 files changed, 67 insertions(+), 14 deletions(-)include/linux/cleanup.h +3
-0+#define __GUARD_IS_ERR (_ptr) \ + ({ unsigned long _rc = (__force unsigned long)(_ptr); \ + unlikely((_rc-1) >= -MAX_ERRNO-1); })Trivial but I'd have added spaces to make this
Quoted 1 line
unlikely((_rc - 1) >= -MAX_ERRNO - 1); })
Looks like even though I pushed out a stable commit with this nobody has pulled this into linux-next so I can fold in this fixup and send out a new version.
Quoted 11 lines
include/linux/cleanup.h static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ · line 347
Quoted 11 lines
include/linux/cleanup.h +7
-1+#define __DEFINE_GUARD_LOCK_PTR (_name, _exp) \ static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \ - { return (void *)(__force unsigned long)*(_exp); } + { void *_ptr = (void *)(__force unsigned long)*(_exp); \ + if (IS_ERR(_ptr)) { _ptr = NULL; } return _ptr; } \ + static inline int class_##_name##_lock_err(class_##_name##_t *_T) \ + { long _rc = (__force unsigned long)*(_exp); \ + if (!_rc) { _rc = -EBUSY; } if (!IS_ERR_VALUE(_rc)) { _rc = 0; } \ + return _rc; }
I will also take the opportunity to let clang-format add more whitespace to this to make it more readable:
-#define __DEFINE_GUARD_LOCK_PTR(_name, _exp) \
- static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \
- { return (void *)(__force unsigned long)*(_exp); }
+#define __GUARD_IS_ERR(_ptr) \
+ ({ \
+ unsigned long _rc = (__force unsigned long)(_ptr); \
+ unlikely((_rc - 1) >= -MAX_ERRNO - 1); \
+ })
+
+#define __DEFINE_GUARD_LOCK_PTR(_name, _exp) \
+ static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T) \
+ { \
+ void *_ptr = (void *)(__force unsigned long)*(_exp); \
+ if (IS_ERR(_ptr)) { \
+ _ptr = NULL; \
+ } \
+ return _ptr; \
+ } \
+ static inline int class_##_name##_lock_err(class_##_name##_t *_T) \
+ { \
+ long _rc = (__force unsigned long)*(_exp); \
+ if (!_rc) { \
+ _rc = -EBUSY; \
+ } \
+ if (!IS_ERR_VALUE(_rc)) { \
+ _rc = 0; \
+ } \
+ return _rc; \+ }
PATCH v2 2/8 cxl/mbox: Convert poison list mutex to ACQUIRE()
In reply to Dan WilliamsTowards removing all explicit unlock calls in the CXL subsystem, convert the conditional poison list mutex to use a conditional lock guard.
Rename the lock to have the compiler validate that all existing call sites are converted.
--- drivers/cxl/core/mbox.c | 7 +++---- drivers/cxl/cxlmem.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-)
drivers/cxl/core/mbox.c +3 -4
@@ -1401,8 +1401,8 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,int nr_records =0 ;int rc; - rc =mutex_lock_interruptible (&mds->poison.lock); -if (rc) +ACQUIRE (mutex_intr, lock)(&mds->poison.mutex); +if ((rc =ACQUIRE_ERR (mutex_intr, &lock)))return rc; po = mds->poison.list_out;
@@ -1437,7 +1437,6 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, } }while (po->flags &CXL_POISON_FLAG_MORE ); -mutex_unlock (&mds->poison.lock);return rc; }EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" );
@@ -1473,7 +1472,7 @@ int cxl_poison_state_init(struct cxl_memdev_state *mds)return rc; } -mutex_init (&mds->poison.lock); +mutex_init (&mds->poison.mutex);return 0 ; }EXPORT_SYMBOL_NS_GPL (cxl_poison_state_init,"CXL" );
drivers/cxl/cxlmem.h +2 -2
@@ -254,7 +254,7 @@ enum security_cmd_enabled_bits {* @max_errors: Maximum media error records held in device cache * @enabled_cmds: All poison commands enabled in the CEL * @list_out: The poison list payload returned by device -* @lock: Protect reads of the poison list +* @mutex: Protect reads of the poison list * * Reads of the poison list are synchronized to ensure that a reader * does not get an incomplete list because their request overlapped
@@ -265,7 +265,7 @@ struct cxl_poison_state {u32 max_errors;DECLARE_BITMAP (enabled_cmds,CXL_POISON_ENABLED_MAX );struct cxl_mbox_poison_out *list_out; -struct mutex lock;/* Protect reads of poison list */ +struct mutex mutex;/* Protect reads of poison list */ };/*
Signature · 1 line
2.49.0
Re: [PATCH v2 2/8] cxl/mbox: Convert poison list mutex to ACQUIRE()
Quoted 5 lines · Dan Williams
Towards removing all explicit unlock calls in the CXL subsystem, convert the conditional poison list mutex to use a conditional lock guard.
Rename the lock to have the compiler validate that all existing call sites are converted.
Re: [PATCH v2 2/8] cxl/mbox: Convert poison list mutex to ACQUIRE()
In reply to Dan WilliamsQuoted 6 lines · On Wed, 18 Jun 2025 22:04:10 -0700 Dan Williams <[email protected]>…
Towards removing all explicit unlock calls in the CXL subsystem, convert the conditional poison list mutex to use a conditional lock guard.
Rename the lock to have the compiler validate that all existing call sites are converted.
Quoted 9 lines
Cc Linus Torvalds <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
One trivial inline. Either way
Quoted 10 lines
--- drivers/cxl/core/mbox.c | 7 +++---- drivers/cxl/cxlmem.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-)drivers/cxl/core/mbox.c +0
-0
drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1404
Quoted 6 lines
drivers/cxl/core/mbox.c +1
-2@@ -1401,8 +1401,8 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,int nr_records =0 ;int rc; - rc =mutex_lock_interruptible (&mds->poison.lock); -if (rc) +ACQUIRE (mutex_intr, lock)(&mds->poison.mutex);
I'd slightly prefer the 'canonical' style from the cleanup.h docs in previous patch.
rc = ACQUIRE_ERR(mutex_intr, &lock); if (rc) return rc;
Quoted 43 lines
drivers/cxl/core/mbox.c +2
-2+if ((rc =ACQUIRE_ERR (mutex_intr, &lock)))return rc; po = mds->poison.list_out;@@ -1437,7 +1437,6 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, } }while (po->flags &CXL_POISON_FLAG_MORE ); -mutex_unlock (&mds->poison.lock);return rc; }EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" );@@ -1473,7 +1472,7 @@ int cxl_poison_state_init(struct cxl_memdev_state *mds)return rc; } -mutex_init (&mds->poison.lock); +mutex_init (&mds->poison.mutex);return 0 ; }EXPORT_SYMBOL_NS_GPL (cxl_poison_state_init,"CXL" );drivers/cxl/cxlmem.h +2
-2@@ -254,7 +254,7 @@ enum security_cmd_enabled_bits {* @max_errors: Maximum media error records held in device cache * @enabled_cmds: All poison commands enabled in the CEL * @list_out: The poison list payload returned by device -* @lock: Protect reads of the poison list +* @mutex: Protect reads of the poison list * * Reads of the poison list are synchronized to ensure that a reader * does not get an incomplete list because their request overlapped @@ -265,7 +265,7 @@ struct cxl_poison_state {u32 max_errors;DECLARE_BITMAP (enabled_cmds,CXL_POISON_ENABLED_MAX );struct cxl_mbox_poison_out *list_out; -struct mutex lock;/* Protect reads of poison list */ +struct mutex mutex;/* Protect reads of poison list */ };/*
Re: [PATCH v2 2/8] cxl/mbox: Convert poison list mutex to ACQUIRE()
Quoted 42 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 18 Jun 2025 22:04:10 -0700 Dan Williams <[email protected]>…
Towards removing all explicit unlock calls in the CXL subsystem, convert the conditional poison list mutex to use a conditional lock guard.
Rename the lock to have the compiler validate that all existing call sites are converted.
Quoted 9 lines
Cc Linus Torvalds <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>One trivial inline. Either way
Reviewed-by Jonathan Cameron <[email protected]>Quoted 10 lines
--- drivers/cxl/core/mbox.c | 7 +++---- drivers/cxl/cxlmem.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-)drivers/cxl/core/mbox.c +0
-0drivers/cxl/core/mbox.c int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, · line 1404
Quoted 6 lines
drivers/cxl/core/mbox.c +1
-2@@ -1401,8 +1401,8 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,int nr_records =0 ;int rc; - rc =mutex_lock_interruptible (&mds->poison.lock); -if (rc) +ACQUIRE (mutex_intr, lock)(&mds->poison.mutex);I'd slightly prefer the 'canonical' style from the
cleanup.hdocs in previous patch.
Quoted 3 lines
rc = ACQUIRE_ERR(mutex_intr, &lock); if (rc) return rc;
I took a pass at that conversion before sending this out, but came back to the inline assignment approach because the compactness matched the familiar pattern with other error pointer handling.
The comfortable pattern with ERR_PTR() is:
if (IS_ERR()) return PTR_ERR();
The same could be done here with something like:
if (ACQUIRE_ERR(@class, @lock)) return ACQUIRE_ERR(@class, @lock))
...but that also feels like a mouthful especially if there is already a local return code variable that can be used. So,
if ((ret = ACQUIRE_ERR(@class, @lock)) return ret;
...feel like a reasonable compromise for compactness on a common pattern. Especially if cleanup helpers are also generally accepted as a reason to bend the "variables declared at the top of scope" convention.
Re: [PATCH v2 2/8] cxl/mbox: Convert poison list mutex to ACQUIRE()
In reply to Dan WilliamsQuoted 6 lines · Dan Williams
Towards removing all explicit unlock calls in the CXL subsystem, convert the conditional poison list mutex to use a conditional lock guard.
Rename the lock to have the compiler validate that all existing call sites are converted.
Quoted 9 lines
Cc Linus Torvalds <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 59 lines
--- drivers/cxl/core/mbox.c | 7 +++---- drivers/cxl/cxlmem.h | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-)drivers/cxl/core/mbox.c +3
-4@@ -1401,8 +1401,8 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len,int nr_records =0 ;int rc; - rc =mutex_lock_interruptible (&mds->poison.lock); -if (rc) +ACQUIRE (mutex_intr, lock)(&mds->poison.mutex); +if ((rc =ACQUIRE_ERR (mutex_intr, &lock)))return rc; po = mds->poison.list_out;@@ -1437,7 +1437,6 @@ int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, } }while (po->flags &CXL_POISON_FLAG_MORE ); -mutex_unlock (&mds->poison.lock);return rc; }EXPORT_SYMBOL_NS_GPL (cxl_mem_get_poison,"CXL" );@@ -1473,7 +1472,7 @@ int cxl_poison_state_init(struct cxl_memdev_state *mds)return rc; } -mutex_init (&mds->poison.lock); +mutex_init (&mds->poison.mutex);return 0 ; }EXPORT_SYMBOL_NS_GPL (cxl_poison_state_init,"CXL" );drivers/cxl/cxlmem.h +2
-2@@ -254,7 +254,7 @@ enum security_cmd_enabled_bits {* @max_errors: Maximum media error records held in device cache * @enabled_cmds: All poison commands enabled in the CEL * @list_out: The poison list payload returned by device -* @lock: Protect reads of the poison list +* @mutex: Protect reads of the poison list * * Reads of the poison list are synchronized to ensure that a reader * does not get an incomplete list because their request overlapped @@ -265,7 +265,7 @@ struct cxl_poison_state {u32 max_errors;DECLARE_BITMAP (enabled_cmds,CXL_POISON_ENABLED_MAX );struct cxl_mbox_poison_out *list_out; -struct mutex lock;/* Protect reads of poison list */ +struct mutex mutex;/* Protect reads of poison list */ };/*
PATCH v2 3/8 cxl/decoder: Move decoder register programming to a helper
In reply to Dan WilliamsIn preparation for converting to rw_semaphore_acquire semantics move the contents of an open-coded {down,up}_read(&cxl_dpa_rwsem) section to a helper function.
--- drivers/cxl/core/hdm.c | 77 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-)
drivers/cxl/core/hdm.c +42 -35
@@ -764,14 +764,53 @@ static int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; } +static void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) +{ +int id = cxld->id; +u64 base, size; +u32 ctrl; + +/* common decoder settings */ + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); +cxld_set_interleave (cxld, &ctrl); +cxld_set_type (cxld, &ctrl); + base = cxld->hpa_range.start; + size =range_len (&cxld->hpa_range); + +writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); +writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); +writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); +writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); + +if (is_switch_decoder (&cxld->dev)) { +struct cxl_switch_decoder *cxlsd = +to_cxl_switch_decoder (&cxld->dev); +void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); +void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); +u64 targets; + +cxlsd_set_targets (cxlsd, &targets); +writel (upper_32_bits (targets), tl_hi); +writel (lower_32_bits (targets), tl_lo); + }else { +struct cxl_endpoint_decoder *cxled = +to_cxl_endpoint_decoder (&cxld->dev); +void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); +void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); + +writel (upper_32_bits (cxled->skip), sk_hi); +writel (lower_32_bits (cxled->skip), sk_lo); + } + +writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +} +static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev);void __iomem *hdm = cxlhdm->regs.hdm_decoder;int id = cxld->id, rc; -u64 base, size; -u32 ctrl;if (cxld->flags &CXL_DECODER_F_ENABLE )return 0 ;
@@ -804,39 +843,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) }down_read (&cxl_dpa_rwsem); -/* common decoder settings */ - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); -cxld_set_interleave (cxld, &ctrl); -cxld_set_type (cxld, &ctrl); - base = cxld->hpa_range.start; - size =range_len (&cxld->hpa_range); - -writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); -writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); -writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); - -if (is_switch_decoder (&cxld->dev)) { -struct cxl_switch_decoder *cxlsd = -to_cxl_switch_decoder (&cxld->dev); -void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); -void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); -u64 targets; - -cxlsd_set_targets (cxlsd, &targets); -writel (upper_32_bits (targets), tl_hi); -writel (lower_32_bits (targets), tl_lo); - }else { -struct cxl_endpoint_decoder *cxled = -to_cxl_endpoint_decoder (&cxld->dev); -void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); -void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); - -writel (upper_32_bits (cxled->skip), sk_hi); -writel (lower_32_bits (cxled->skip), sk_lo); - } - -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +setup_hw_decoder (cxld, hdm);up_read (&cxl_dpa_rwsem); port->commit_end++;
Signature · 1 line
2.49.0
Re: [PATCH v2 3/8] cxl/decoder: Move decoder register programming to a helper
Quoted 4 lines · Dan Williams
In preparation for converting to
rw_semaphore_acquiresemantics move the contents of an open-coded {down,up}_read(&cxl_dpa_rwsem) section to a helper function.
Re: [PATCH v2 3/8] cxl/decoder: Move decoder register programming to a helper
In reply to Dan WilliamsQuoted 11 lines · On Wed, 18 Jun 2025 22:04:11 -0700 Dan Williams <[email protected]>…
In preparation for converting to
rw_semaphore_acquiresemantics move the contents of an open-coded {down,up}_read(&cxl_dpa_rwsem) section to a helper function.Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Seems reasonable.
Quoted 105 lines
--- drivers/cxl/core/hdm.c | 77 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-)drivers/cxl/core/hdm.c +42
-35@@ -764,14 +764,53 @@ static int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; } +static void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) +{ +int id = cxld->id; +u64 base, size; +u32 ctrl; + +/* common decoder settings */ + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); +cxld_set_interleave (cxld, &ctrl); +cxld_set_type (cxld, &ctrl); + base = cxld->hpa_range.start; + size =range_len (&cxld->hpa_range); + +writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); +writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); +writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); +writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); + +if (is_switch_decoder (&cxld->dev)) { +struct cxl_switch_decoder *cxlsd = +to_cxl_switch_decoder (&cxld->dev); +void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); +void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); +u64 targets; + +cxlsd_set_targets (cxlsd, &targets); +writel (upper_32_bits (targets), tl_hi); +writel (lower_32_bits (targets), tl_lo); + }else { +struct cxl_endpoint_decoder *cxled = +to_cxl_endpoint_decoder (&cxld->dev); +void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); +void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); + +writel (upper_32_bits (cxled->skip), sk_hi); +writel (lower_32_bits (cxled->skip), sk_lo); + } + +writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +} +static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev);void __iomem *hdm = cxlhdm->regs.hdm_decoder;int id = cxld->id, rc; -u64 base, size; -u32 ctrl;if (cxld->flags &CXL_DECODER_F_ENABLE )return 0 ;@@ -804,39 +843,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) }down_read (&cxl_dpa_rwsem); -/* common decoder settings */ - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); -cxld_set_interleave (cxld, &ctrl); -cxld_set_type (cxld, &ctrl); - base = cxld->hpa_range.start; - size =range_len (&cxld->hpa_range); - -writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); -writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); -writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); - -if (is_switch_decoder (&cxld->dev)) { -struct cxl_switch_decoder *cxlsd = -to_cxl_switch_decoder (&cxld->dev); -void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); -void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); -u64 targets; - -cxlsd_set_targets (cxlsd, &targets); -writel (upper_32_bits (targets), tl_hi); -writel (lower_32_bits (targets), tl_lo); - }else { -struct cxl_endpoint_decoder *cxled = -to_cxl_endpoint_decoder (&cxld->dev); -void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); -void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); - -writel (upper_32_bits (cxled->skip), sk_hi); -writel (lower_32_bits (cxled->skip), sk_lo); - } - -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +setup_hw_decoder (cxld, hdm);up_read (&cxl_dpa_rwsem); port->commit_end++;
Re: [PATCH v2 3/8] cxl/decoder: Move decoder register programming to a helper
In reply to Dan WilliamsQuoted 11 lines · Dan Williams
In preparation for converting to
rw_semaphore_acquiresemantics move the contents of an open-coded {down,up}_read(&cxl_dpa_rwsem) section to a helper function.Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 105 lines
--- drivers/cxl/core/hdm.c | 77 +++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-)drivers/cxl/core/hdm.c +42
-35@@ -764,14 +764,53 @@ static int cxld_await_commit(void __iomem *hdm, int id)return -ETIMEDOUT ; } +static void setup_hw_decoder (struct cxl_decoder *cxld,void __iomem *hdm) +{ +int id = cxld->id; +u64 base, size; +u32 ctrl; + +/* common decoder settings */ + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); +cxld_set_interleave (cxld, &ctrl); +cxld_set_type (cxld, &ctrl); + base = cxld->hpa_range.start; + size =range_len (&cxld->hpa_range); + +writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); +writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); +writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); +writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); + +if (is_switch_decoder (&cxld->dev)) { +struct cxl_switch_decoder *cxlsd = +to_cxl_switch_decoder (&cxld->dev); +void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); +void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); +u64 targets; + +cxlsd_set_targets (cxlsd, &targets); +writel (upper_32_bits (targets), tl_hi); +writel (lower_32_bits (targets), tl_lo); + }else { +struct cxl_endpoint_decoder *cxled = +to_cxl_endpoint_decoder (&cxld->dev); +void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); +void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); + +writel (upper_32_bits (cxled->skip), sk_hi); +writel (lower_32_bits (cxled->skip), sk_lo); + } + +writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +} +static int cxl_decoder_commit (struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent);struct cxl_hdm *cxlhdm =dev_get_drvdata (&port->dev);void __iomem *hdm = cxlhdm->regs.hdm_decoder;int id = cxld->id, rc; -u64 base, size; -u32 ctrl;if (cxld->flags &CXL_DECODER_F_ENABLE )return 0 ;@@ -804,39 +843,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) }down_read (&cxl_dpa_rwsem); -/* common decoder settings */ - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); -cxld_set_interleave (cxld, &ctrl); -cxld_set_type (cxld, &ctrl); - base = cxld->hpa_range.start; - size =range_len (&cxld->hpa_range); - -writel (upper_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id)); -writel (lower_32_bits (base), hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -writel (upper_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_HIGH_OFFSET (id)); -writel (lower_32_bits (size), hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id)); - -if (is_switch_decoder (&cxld->dev)) { -struct cxl_switch_decoder *cxlsd = -to_cxl_switch_decoder (&cxld->dev); -void __iomem *tl_hi = hdm +CXL_HDM_DECODER0_TL_HIGH (id); -void __iomem *tl_lo = hdm +CXL_HDM_DECODER0_TL_LOW (id); -u64 targets; - -cxlsd_set_targets (cxlsd, &targets); -writel (upper_32_bits (targets), tl_hi); -writel (lower_32_bits (targets), tl_lo); - }else { -struct cxl_endpoint_decoder *cxled = -to_cxl_endpoint_decoder (&cxld->dev); -void __iomem *sk_hi = hdm +CXL_HDM_DECODER0_SKIP_HIGH (id); -void __iomem *sk_lo = hdm +CXL_HDM_DECODER0_SKIP_LOW (id); - -writel (upper_32_bits (cxled->skip), sk_hi); -writel (lower_32_bits (cxled->skip), sk_lo); - } - -writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); +setup_hw_decoder (cxld, hdm);up_read (&cxl_dpa_rwsem); port->commit_end++;
PATCH v2 4/8 cxl/decoder: Drop pointless locking
In reply to Dan Williamscxl_dpa_rwsem coordinates changes to dpa allocation settings for a given decoder. cxl_decoder_reset() has no need for a consistent snapshot of the dpa settings since it is merely clearing out whatever was there previously.
Otherwise, cxl_region_rwsem protects against 'reset' racing 'setup'.
In preparationg for converting to rw_semaphore_acquire semantics, drop this locking.
--- drivers/cxl/core/hdm.c | 2 -- 1 file changed, 2 deletions(-)
drivers/cxl/core/hdm.c +0 -2
@@ -914,7 +914,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)"%s: out of order reset, expected decoder%d.%d\n" , dev_name(&cxld->dev), port->id, port->commit_end); -down_read (&cxl_dpa_rwsem); ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT ;writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id));
@@ -923,7 +922,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -up_read (&cxl_dpa_rwsem); cxld->flags &= ~CXL_DECODER_F_ENABLE ;
Signature · 1 line
2.49.0
Re: [PATCH v2 4/8] cxl/decoder: Drop pointless locking
Quoted 9 lines · Dan Williams
cxl_dpa_rwsemcoordinates changes to dpa allocation settings for a given decoder.cxl_decoder_reset()has no need for a consistent snapshot of the dpa settings since it is merely clearing out whatever was there previously.Otherwise,
cxl_region_rwsemprotects against 'reset' racing 'setup'.In preparationg for converting to
rw_semaphore_acquiresemantics, drop this locking.
Quoted 7 lines
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Re: [PATCH v2 4/8] cxl/decoder: Drop pointless locking
In reply to Dan WilliamsQuoted 9 lines · Dan Williams
cxl_dpa_rwsemcoordinates changes to dpa allocation settings for a given decoder.cxl_decoder_reset()has no need for a consistent snapshot of the dpa settings since it is merely clearing out whatever was there previously.Otherwise,
cxl_region_rwsemprotects against 'reset' racing 'setup'.In preparationg for converting to
rw_semaphore_acquiresemantics, drop this locking.
Re: [PATCH v2 4/8] cxl/decoder: Drop pointless locking
In reply to Dan WilliamsQuoted 7 lines · On Wed, 18 Jun 2025 22:04:12 -0700 Dan Williams <[email protected]>…
cxl_dpa_rwsemcoordinates changes to dpa allocation settings for a given decoder.cxl_decoder_reset()has no need for a consistent snapshot of the dpa settings since it is merely clearing out whatever was there previously.Otherwise,
cxl_region_rwsemprotects against 'reset' racing 'setup'.In preparationg for converting to
rw_semaphore_acquiresemantics, drop this
typo
Quoted 9 lines
locking.
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 24 lines
--- drivers/cxl/core/hdm.c | 2 -- 1 file changed, 2 deletions(-)drivers/cxl/core/hdm.c +0
-2@@ -914,7 +914,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)"%s: out of order reset, expected decoder%d.%d\n" , dev_name(&cxld->dev), port->id, port->commit_end); -down_read (&cxl_dpa_rwsem); ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT ;writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id));@@ -923,7 +922,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -up_read (&cxl_dpa_rwsem); cxld->flags &= ~CXL_DECODER_F_ENABLE ;
Re: [PATCH v2 4/8] cxl/decoder: Drop pointless locking
In reply to Dan WilliamsQuoted 9 lines · Dan Williams
cxl_dpa_rwsemcoordinates changes to dpa allocation settings for a given decoder.cxl_decoder_reset()has no need for a consistent snapshot of the dpa settings since it is merely clearing out whatever was there previously.Otherwise,
cxl_region_rwsemprotects against 'reset' racing 'setup'.In preparationg for converting to
rw_semaphore_acquiresemantics, drop this locking.
Quoted 7 lines
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 24 lines
--- drivers/cxl/core/hdm.c | 2 -- 1 file changed, 2 deletions(-)drivers/cxl/core/hdm.c +0
-2@@ -914,7 +914,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)"%s: out of order reset, expected decoder%d.%d\n" , dev_name(&cxld->dev), port->id, port->commit_end); -down_read (&cxl_dpa_rwsem); ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id)); ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT ;writel (ctrl, hdm +CXL_HDM_DECODER0_CTRL_OFFSET (id));@@ -923,7 +922,6 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_SIZE_LOW_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_HIGH_OFFSET (id));writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); -up_read (&cxl_dpa_rwsem); cxld->flags &= ~CXL_DECODER_F_ENABLE ;
PATCH v2 5/8 cxl/region: Split commit_store() into __commit() and queue_reset() helpers
In reply to Dan WilliamsThe complexity of dropping the lock is removed in favor of splitting commit operations to a helper, and leaving all the complexities of "decommit" for commit_store() to coordinate the different locking contexts.
The CPU cache-invalidation in the decommit path is solely handled now by cxl_region_decode_reset(). Previously the CPU caches were being needlessly flushed twice in the decommit path where the first flush had no guarantee that the memory would not be immediately re-dirtied.
--- drivers/cxl/core/region.c | 99 +++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 29 deletions(-)
drivers/cxl/core/region.c +70 -29
@@ -349,30 +349,42 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)return rc; } -static ssize_t commit_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t len) +static int queue_reset (struct cxl_region *cxlr) { -struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params; -bool commit; -ssize_t rc; +int rc; - rc =kstrtobool (buf, &commit); + rc =down_write_killable (&cxl_region_rwsem);if (rc)return rc; +/* Already in the requested state? */ +if (p->state <CXL_CONFIG_COMMIT ) + goto out; + + p->state =CXL_CONFIG_RESET_PENDING ; + +out: +up_write (&cxl_region_rwsem); + +return rc; +} + +static int __commit (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + rc =down_write_killable (&cxl_region_rwsem);if (rc)return rc;/* Already in the requested state? */ -if (commit && p->state >=CXL_CONFIG_COMMIT ) - goto out; -if (!commit && p->state <CXL_CONFIG_COMMIT ) +if (p->state >=CXL_CONFIG_COMMIT ) goto out;/* Not ready to commit? */ -if (commit && p->state <CXL_CONFIG_ACTIVE ) { +if (p->state <CXL_CONFIG_ACTIVE ) { rc = -ENXIO ; goto out; }
@@ -385,31 +397,60 @@ static ssize_t commit_store(struct device *dev, struct device_attribute *attr,if (rc) goto out; -if (commit) { - rc =cxl_region_decode_commit (cxlr); -if (rc ==0 ) - p->state =CXL_CONFIG_COMMIT ; - }else { - p->state =CXL_CONFIG_RESET_PENDING ; -up_write (&cxl_region_rwsem); -device_release_driver (&cxlr->dev); -down_write (&cxl_region_rwsem); - -/* -* The lock was dropped, so need to revalidate that the reset is -* still pending. -*/ -if (p->state ==CXL_CONFIG_RESET_PENDING ) { -cxl_region_decode_reset (cxlr, p->interleave_ways); - p->state =CXL_CONFIG_ACTIVE ; - } - } + rc =cxl_region_decode_commit (cxlr); +if (rc ==0 ) + p->state =CXL_CONFIG_COMMIT ; out:up_write (&cxl_region_rwsem); +return rc; +} + +static ssize_t commit_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +struct cxl_region *cxlr =to_cxl_region (dev); +struct cxl_region_params *p = &cxlr->params; +bool commit; +ssize_t rc; + + rc =kstrtobool (buf, &commit);if (rc)return rc; + +if (commit) { + rc =__commit (cxlr); +if (rc) +return rc; +return len; + } + + rc =queue_reset (cxlr); +if (rc) +return rc; + +/* +* Unmap the region and depend the reset-pending state to ensure +* it does not go active again until post reset +*/ +device_release_driver (&cxlr->dev); + +/* +* With the reset pending take cxl_region_rwsem unconditionally +* to ensure the reset gets handled before returning. +*/ +guard (rwsem_write)(&cxl_region_rwsem); + +/* +* Revalidate that the reset is still pending in case another +* thread already handled this reset. +*/ +if (p->state ==CXL_CONFIG_RESET_PENDING ) { +cxl_region_decode_reset (cxlr, p->interleave_ways); + p->state =CXL_CONFIG_ACTIVE ; + } +return len; }
Signature · 1 line
2.49.0
Alison Schofield
maintainerRe: [PATCH v2 5/8] cxl/region: Split commit_store() into __commit() and queue_reset() helpers
Quoted 25 lines · Dan Williams
The complexity of dropping the lock is removed in favor of splitting commit operations to a helper, and leaving all the complexities of "decommit" for
commit_store()to coordinate the different locking contexts.The CPU cache-invalidation in the decommit path is solely handled now by
cxl_region_decode_reset(). Previously the CPU caches were being needlessly flushed twice in the decommit path where the first flush had no guarantee that the memory would not be immediately re-dirtied.Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>--- drivers/cxl/core/region.c | 99 +++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 29 deletions(-)drivers/cxl/core/region.c +0
-0
drivers/cxl/core/region.c static int cxl_region_decode_commit(struct cxl_region *cxlr) · line 352
Quoted 6 lines
drivers/cxl/core/region.c +1
-2@@ -349,30 +349,42 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)return rc; } -static ssize_t commit_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t len) +static int queue_reset (struct cxl_region *cxlr)
How about defining both new helpers to return ssize_t type for consistency with sysfs functions. The local 'int rc' can remain as long as the function return type is ssize_t.
There no possible breakage here based on the actual limited return values. Just a suggestion to follow sysfs convention.
Quoted 23 lines
drivers/cxl/core/region.c +13
-4{ -struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params; -bool commit; -ssize_t rc; +int rc; - rc =kstrtobool (buf, &commit); + rc =down_write_killable (&cxl_region_rwsem);if (rc)return rc; +/* Already in the requested state? */ +if (p->state <CXL_CONFIG_COMMIT ) + goto out; + + p->state =CXL_CONFIG_RESET_PENDING ; + +out: +up_write (&cxl_region_rwsem); + +return rc; +}
snip
Re: [PATCH v2 5/8] cxl/region: Split commit_store() into __commit() and queue_reset() helpers
Quoted 29 lines · Alison Schofield
Quoted 28 lines · Dan Williams
The complexity of dropping the lock is removed in favor of splitting commit operations to a helper, and leaving all the complexities of "decommit" for
commit_store()to coordinate the different locking contexts.The CPU cache-invalidation in the decommit path is solely handled now by
cxl_region_decode_reset(). Previously the CPU caches were being needlessly flushed twice in the decommit path where the first flush had no guarantee that the memory would not be immediately re-dirtied.Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>--- drivers/cxl/core/region.c | 99 +++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 29 deletions(-)drivers/cxl/core/region.c +0
-0@@ -349,30 +349,42 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)return rc; }
Quoted 7 lines
drivers/cxl/core/region.c static int cxl_region_decode_commit(struct cxl_region *cxlr) · line 352
Quoted 3 lines
drivers/cxl/core/region.c +1
-2-static ssize_t commit_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t len) +static int queue_reset (struct cxl_region *cxlr)How about defining both new helpers to return
ssize_ttype for consistency with sysfs functions. The local 'int rc' can remain as long as the function return type isssize_t.
The type change to 'int' for the helpers that are not dealing with input/ouput is deliberate. queue_reset() is never returning a "size", it is always returning 0 or a negative error code. An int fits in ssize_t.
Re: [PATCH v2 5/8] cxl/region: Split commit_store() into __commit() and queue_reset() helpers
In reply to Dan WilliamsQuoted 9 lines · On Wed, 18 Jun 2025 22:04:13 -0700 Dan Williams <[email protected]>…
The complexity of dropping the lock is removed in favor of splitting commit operations to a helper, and leaving all the complexities of "decommit" for
commit_store()to coordinate the different locking contexts.The CPU cache-invalidation in the decommit path is solely handled now by
cxl_region_decode_reset(). Previously the CPU caches were being needlessly flushed twice in the decommit path where the first flush had no guarantee that the memory would not be immediately re-dirtied.
Quoted 7 lines
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Seems fine
Re: [PATCH v2 5/8] cxl/region: Split commit_store() into __commit() and queue_reset() helpers
In reply to Dan WilliamsQuoted 9 lines · Dan Williams
The complexity of dropping the lock is removed in favor of splitting commit operations to a helper, and leaving all the complexities of "decommit" for
commit_store()to coordinate the different locking contexts.The CPU cache-invalidation in the decommit path is solely handled now by
cxl_region_decode_reset(). Previously the CPU caches were being needlessly flushed twice in the decommit path where the first flush had no guarantee that the memory would not be immediately re-dirtied.
Quoted 7 lines
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 141 lines
--- drivers/cxl/core/region.c | 99 +++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 29 deletions(-)drivers/cxl/core/region.c +70
-29@@ -349,30 +349,42 @@ static int cxl_region_decode_commit(struct cxl_region *cxlr)return rc; } -static ssize_t commit_store (struct device *dev,struct device_attribute *attr, -const char *buf,size_t len) +static int queue_reset (struct cxl_region *cxlr) { -struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params; -bool commit; -ssize_t rc; +int rc; - rc =kstrtobool (buf, &commit); + rc =down_write_killable (&cxl_region_rwsem);if (rc)return rc; +/* Already in the requested state? */ +if (p->state <CXL_CONFIG_COMMIT ) + goto out; + + p->state =CXL_CONFIG_RESET_PENDING ; + +out: +up_write (&cxl_region_rwsem); + +return rc; +} + +static int __commit (struct cxl_region *cxlr) +{ +struct cxl_region_params *p = &cxlr->params; +int rc; + rc =down_write_killable (&cxl_region_rwsem);if (rc)return rc;/* Already in the requested state? */ -if (commit && p->state >=CXL_CONFIG_COMMIT ) - goto out; -if (!commit && p->state <CXL_CONFIG_COMMIT ) +if (p->state >=CXL_CONFIG_COMMIT ) goto out;/* Not ready to commit? */ -if (commit && p->state <CXL_CONFIG_ACTIVE ) { +if (p->state <CXL_CONFIG_ACTIVE ) { rc = -ENXIO ; goto out; }@@ -385,31 +397,60 @@ static ssize_t commit_store(struct device *dev, struct device_attribute *attr,if (rc) goto out; -if (commit) { - rc =cxl_region_decode_commit (cxlr); -if (rc ==0 ) - p->state =CXL_CONFIG_COMMIT ; - }else { - p->state =CXL_CONFIG_RESET_PENDING ; -up_write (&cxl_region_rwsem); -device_release_driver (&cxlr->dev); -down_write (&cxl_region_rwsem); - -/* -* The lock was dropped, so need to revalidate that the reset is -* still pending. -*/ -if (p->state ==CXL_CONFIG_RESET_PENDING ) { -cxl_region_decode_reset (cxlr, p->interleave_ways); - p->state =CXL_CONFIG_ACTIVE ; - } - } + rc =cxl_region_decode_commit (cxlr); +if (rc ==0 ) + p->state =CXL_CONFIG_COMMIT ; out:up_write (&cxl_region_rwsem); +return rc; +} + +static ssize_t commit_store (struct device *dev,struct device_attribute *attr, +const char *buf,size_t len) +{ +struct cxl_region *cxlr =to_cxl_region (dev); +struct cxl_region_params *p = &cxlr->params; +bool commit; +ssize_t rc; + + rc =kstrtobool (buf, &commit);if (rc)return rc; + +if (commit) { + rc =__commit (cxlr); +if (rc) +return rc; +return len; + } + + rc =queue_reset (cxlr); +if (rc) +return rc; + +/* +* Unmap the region and depend the reset-pending state to ensure +* it does not go active again until post reset +*/ +device_release_driver (&cxlr->dev); + +/* +* With the reset pending take cxl_region_rwsem unconditionally +* to ensure the reset gets handled before returning. +*/ +guard (rwsem_write)(&cxl_region_rwsem); + +/* +* Revalidate that the reset is still pending in case another +* thread already handled this reset. +*/ +if (p->state ==CXL_CONFIG_RESET_PENDING ) { +cxl_region_decode_reset (cxlr, p->interleave_ways); + p->state =CXL_CONFIG_ACTIVE ; + } +return len; }
PATCH v2 6/8 cxl/region: Move ready-to-probe state check to a helper
In reply to Dan WilliamsRather than unlocking the region rwsem in the middle of cxl_region_probe() create a helper for determining when the region is ready-to-probe.
--- drivers/cxl/core/region.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/cxl/core/region.c +18 -6
@@ -3572,9 +3572,8 @@ static void shutdown_notifiers(void *_cxlr)unregister_mt_adistance_algorithm (&cxlr->adist_notifier); } -static int cxl_region_probe (struct device *dev) +static int cxl_region_can_probe (struct cxl_region *cxlr) { -struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params;int rc;
@@ -3597,15 +3596,28 @@ static int cxl_region_probe(struct device *dev) goto out; } -/* -* From this point on any path that changes the region's state away from -* CXL_CONFIG_COMMIT is also responsible for releasing the driver. -*/ out:up_read (&cxl_region_rwsem);if (rc)return rc; +return 0 ; +} + +static int cxl_region_probe (struct device *dev) +{ +struct cxl_region *cxlr =to_cxl_region (dev); +struct cxl_region_params *p = &cxlr->params; +int rc; + + rc =cxl_region_can_probe (cxlr); +if (rc) +return rc; + +/* +* From this point on any path that changes the region's state away from +* CXL_CONFIG_COMMIT is also responsible for releasing the driver. +*/ cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ;
Signature · 1 line
2.49.0
Re: [PATCH v2 6/8] cxl/region: Move ready-to-probe state check to a helper
Quoted 10 lines · Dan Williams
Rather than unlocking the region rwsem in the middle of
cxl_region_probe()create a helper for determining when the region is ready-to-probe.Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>
Quoted 52 lines
--- drivers/cxl/core/region.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)drivers/cxl/core/region.c +18
-6@@ -3572,9 +3572,8 @@ static void shutdown_notifiers(void *_cxlr)unregister_mt_adistance_algorithm (&cxlr->adist_notifier); } -static int cxl_region_probe (struct device *dev) +static int cxl_region_can_probe (struct cxl_region *cxlr) { -struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params;int rc;@@ -3597,15 +3596,28 @@ static int cxl_region_probe(struct device *dev) goto out; } -/* -* From this point on any path that changes the region's state away from -* CXL_CONFIG_COMMIT is also responsible for releasing the driver. -*/ out:up_read (&cxl_region_rwsem);if (rc)return rc; +return 0 ; +} + +static int cxl_region_probe (struct device *dev) +{ +struct cxl_region *cxlr =to_cxl_region (dev); +struct cxl_region_params *p = &cxlr->params; +int rc; + + rc =cxl_region_can_probe (cxlr); +if (rc) +return rc; + +/* +* From this point on any path that changes the region's state away from +* CXL_CONFIG_COMMIT is also responsible for releasing the driver. +*/ cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ;
PATCH v2 7/8 cxl/region: Introduce CLASS(cxl_decoder_detach...) consolidate multiple paths
In reply to Dan WilliamsBoth detach_target() and cxld_unregister() want to tear down a cxl_region when an endpoint decoder is either detached or destroyed.
When a region is to be destroyed cxl_decoder_detach() releases cxl_region_rwsem unbinds the cxl_region driver and re-acquires the rwsem.
This "reverse" locking pattern is difficult to reason about, not amenable to scope-based cleanup, and the minor differences in the calling convention of cxl_decoder_detach() currently results in the cxl_decoder_kill_region() wrapper.
Introduce CLASS(cxl_decoder_detach...) which creates an object that moves the post-detach cleanup work to a destructor, and consolidates minor preamble differences in the constructor.
--- drivers/cxl/core/core.h | 43 ++++++++++++++++++- drivers/cxl/core/port.c | 6 +-- drivers/cxl/core/region.c | 88 ++++++++++++++++++--------------------- 3 files changed, 83 insertions(+), 54 deletions(-)
drivers/cxl/core/core.h +41 -2
@@ -12,6 +12,11 @@ extern const struct device_type cxl_pmu_type;extern struct attribute_group cxl_base_attribute_group; +enum cxl_detach_mode { +DETACH_ONLY , +DETACH_INVALIDATE , +}; +#ifdef CONFIG_CXL_REGION extern struct device_attribute dev_attr_create_pmem_region;extern struct device_attribute dev_attr_create_ram_region;
@@ -20,7 +25,11 @@ extern struct device_attribute dev_attr_region;extern const struct device_type cxl_pmem_region_type;extern const struct device_type cxl_dax_region_type;extern const struct device_type cxl_region_type; -void cxl_decoder_kill_region (struct cxl_endpoint_decoder *cxled); + +struct cxl_region *cxl_decoder_detach (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +int pos,enum cxl_detach_mode mode); +#define CXL_REGION_ATTR (x) (&dev_attr_##x.attr)#define CXL_REGION_TYPE (x) (&cxl_region_type)#define SET_CXL_REGION_ATTR (x) (&dev_attr_##x.attr),
@@ -48,7 +57,9 @@ static inline int cxl_get_poison_by_endpoint(struct cxl_port *port) {return 0 ; } -static inline void cxl_decoder_kill_region (struct cxl_endpoint_decoder *cxled) +static inline struct cxl_region * +cxl_decoder_detach (struct cxl_region *cxlr,struct cxl_endpoint_decoder *cxled, +int pos,enum cxl_detach_mode mode) { }static inline int cxl_region_init (void )
@@ -99,6 +110,34 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb);extern struct rw_semaphore cxl_dpa_rwsem;extern struct rw_semaphore cxl_region_rwsem; +DEFINE_CLASS ( + cxl_decoder_detach, struct cxl_region *, + if (!IS_ERR_OR_NULL(_T)) { +device_release_driver (&_T->dev); +put_device (&_T->dev); + }, + ({ +int rc =0 ; + +/* when the decoder is being destroyed lock unconditionally */ +if (mode ==DETACH_INVALIDATE ) +down_write (&cxl_region_rwsem); +else + rc =down_write_killable (&cxl_region_rwsem); + +if (rc) + cxlr =ERR_PTR (rc); +else { + cxlr =cxl_decoder_detach (cxlr, cxled, pos, mode); +get_device (&cxlr->dev); + } +up_write (&cxl_region_rwsem); + + cxlr; + }), + struct cxl_region *cxlr, struct cxl_endpoint_decoder *cxled, int pos, + enum cxl_detach_mode mode) +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
drivers/cxl/core/port.c +2 -4
@@ -2001,11 +2001,9 @@ EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL");static void cxld_unregister (void *dev) { -struct cxl_endpoint_decoder *cxled; -if (is_endpoint_decoder (dev)) { - cxled =to_cxl_endpoint_decoder (dev); -cxl_decoder_kill_region (cxled); +CLASS (cxl_decoder_detach, cxlr) + (NULL ,to_cxl_endpoint_decoder (dev),-1 ,DETACH_INVALIDATE ); }device_unregister (dev);
drivers/cxl/core/region.c +40 -48
124 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v2 7/8] cxl/region: Introduce CLASS(cxl_decoder_detach...) consolidate multiple paths
Quoted 6 lines · On Wed, 18 Jun 2025 22:04:15 -0700 Dan Williams <[email protected]>…
Both
detach_target()andcxld_unregister()want to tear down acxl_regionwhen an endpoint decoder is either detached or destroyed.When a region is to be destroyed
cxl_decoder_detach()releasescxl_region_rwsemunbinds thecxl_regiondriver and re-acquires the rwsem.
Quoted 8 lines
This "reverse" locking pattern is difficult to reason about, not amenable to scope-based cleanup, and the minor differences in the calling convention of
cxl_decoder_detach()currently results in thecxl_decoder_kill_region()wrapper.Introduce
CLASS(cxl_decoder_detach...)which creates an object that moves the post-detach cleanup work to a destructor, and consolidates minor preamble differences in the constructor.
I'm struggling somewhat with grasping how the destructor pattern is useful here. In the two cases the scope is tightly defined around the declaration of the class instance. Doesn't that just boil down to automatically calling the destuctor function immediately? If so what is the use of wrapping it up in a destructor?
Quoted 239 lines
Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Signed-off-by Dan Williams <[email protected]>--- drivers/cxl/core/core.h | 43 ++++++++++++++++++- drivers/cxl/core/port.c | 6 +-- drivers/cxl/core/region.c | 88 ++++++++++++++++++--------------------- 3 files changed, 83 insertions(+), 54 deletions(-)drivers/cxl/core/core.h +41
-2@@ -12,6 +12,11 @@ extern const struct device_type cxl_pmu_type;extern struct attribute_group cxl_base_attribute_group; +enum cxl_detach_mode { +DETACH_ONLY , +DETACH_INVALIDATE , +}; +#ifdef CONFIG_CXL_REGION extern struct device_attribute dev_attr_create_pmem_region;extern struct device_attribute dev_attr_create_ram_region;@@ -20,7 +25,11 @@ extern struct device_attribute dev_attr_region;extern const struct device_type cxl_pmem_region_type;extern const struct device_type cxl_dax_region_type;extern const struct device_type cxl_region_type; -void cxl_decoder_kill_region (struct cxl_endpoint_decoder *cxled); + +struct cxl_region *cxl_decoder_detach (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +int pos,enum cxl_detach_mode mode); +#define CXL_REGION_ATTR (x) (&dev_attr_##x.attr)#define CXL_REGION_TYPE (x) (&cxl_region_type)#define SET_CXL_REGION_ATTR (x) (&dev_attr_##x.attr),@@ -48,7 +57,9 @@ static inline int cxl_get_poison_by_endpoint(struct cxl_port *port) {return 0 ; } -static inline void cxl_decoder_kill_region (struct cxl_endpoint_decoder *cxled) +static inline struct cxl_region * +cxl_decoder_detach (struct cxl_region *cxlr,struct cxl_endpoint_decoder *cxled, +int pos,enum cxl_detach_mode mode) { }static inline int cxl_region_init (void )@@ -99,6 +110,34 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb);extern struct rw_semaphore cxl_dpa_rwsem;extern struct rw_semaphore cxl_region_rwsem; +DEFINE_CLASS ( + cxl_decoder_detach, struct cxl_region *, + if (!IS_ERR_OR_NULL(_T)) { +device_release_driver (&_T->dev); +put_device (&_T->dev); + }, + ({ +int rc =0 ; + +/* when the decoder is being destroyed lock unconditionally */ +if (mode ==DETACH_INVALIDATE ) +down_write (&cxl_region_rwsem); +else + rc =down_write_killable (&cxl_region_rwsem); + +if (rc) + cxlr =ERR_PTR (rc); +else { + cxlr =cxl_decoder_detach (cxlr, cxled, pos, mode); +get_device (&cxlr->dev); + } +up_write (&cxl_region_rwsem); + + cxlr; + }), + struct cxl_region *cxlr, struct cxl_endpoint_decoder *cxled, int pos, + enum cxl_detach_mode mode) +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );drivers/cxl/core/port.c +2
-4@@ -2001,11 +2001,9 @@ EXPORT_SYMBOL_NS_GPL(cxl_decoder_add, "CXL");static void cxld_unregister (void *dev) { -struct cxl_endpoint_decoder *cxled; -if (is_endpoint_decoder (dev)) { - cxled =to_cxl_endpoint_decoder (dev); -cxl_decoder_kill_region (cxled); +CLASS (cxl_decoder_detach, cxlr) + (NULL ,to_cxl_endpoint_decoder (dev),-1 ,DETACH_INVALIDATE ); }device_unregister (dev);drivers/cxl/core/region.c +40
-48@@ -2135,27 +2135,52 @@ static int cxl_region_attach(struct cxl_region *cxlr,return 0 ; } -static int cxl_region_detach (struct cxl_endpoint_decoder *cxled) +/* +* Cleanup a decoder's interest in a region. There are 2 cases to +* handle, removing an unknown @cxled from a known position in a region +* (detach_target()) or removing a known @cxled from an unknown @cxlr +* (cxld_unregister()) +* +* When the detachment finds a region, the caller is responsible for +* releasing the region driver. +*/ +struct cxl_region *cxl_decoder_detach (struct cxl_region *cxlr, +struct cxl_endpoint_decoder *cxled, +int pos,enum cxl_detach_mode mode) { -struct cxl_port *iter, *ep_port =cxled_to_port (cxled); -struct cxl_region *cxlr = cxled->cxld.region;struct cxl_region_params *p; -int rc =0 ;lockdep_assert_held_write (&cxl_region_rwsem); -if (!cxlr) -return 0 ; +if (!cxled) { + p = &cxlr->params; - p = &cxlr->params; -get_device (&cxlr->dev); +if (pos >= p->interleave_ways) { +dev_dbg (&cxlr->dev,"position %d out of range %d\n" , + pos, p->interleave_ways); +return ERR_PTR (-ENXIO ); + } + +if (!p->targets[pos]) +return NULL ; + cxled = p->targets[pos]; + }else { + cxlr = cxled->cxld.region; +if (!cxlr) +return NULL ; + p = &cxlr->params; + } + + +if (mode ==DETACH_INVALIDATE ) + cxled->part =-1 ;if (p->state >CXL_CONFIG_ACTIVE ) {cxl_region_decode_reset (cxlr, p->interleave_ways); p->state =CXL_CONFIG_ACTIVE ; } -for (iter = ep_port; !is_cxl_root (iter); +for (struct cxl_port *iter =cxled_to_port (cxled); !is_cxl_root (iter); iter =to_cxl_port (iter->dev.parent))cxl_port_detach_region (iter, cxlr, cxled);@@ -2166,7 +2191,7 @@ static int cxl_region_detach(struct cxl_endpoint_decoder *cxled)dev_WARN_ONCE (&cxlr->dev,1 ,"expected %s:%s at position %d\n" ,dev_name (&cxlmd->dev),dev_name (&cxled->cxld.dev), cxled->pos); - goto out; +return NULL ; }if (p->state ==CXL_CONFIG_ACTIVE ) {@@ -2180,21 +2205,7 @@ static int cxl_region_detach(struct cxl_endpoint_decoder *cxled) .end = -1, }; -/* notify the region driver that one of its targets has departed */ -up_write (&cxl_region_rwsem); -device_release_driver (&cxlr->dev); -down_write (&cxl_region_rwsem); -out: -put_device (&cxlr->dev); -return rc; -} - -void cxl_decoder_kill_region (struct cxl_endpoint_decoder *cxled) -{ -down_write (&cxl_region_rwsem); - cxled->part =-1 ; -cxl_region_detach (cxled); -up_write (&cxl_region_rwsem); +return cxlr; } static int attach_target(struct cxl_region *cxlr,@@ -2225,29 +2236,10 @@ static int attach_target(struct cxl_region *cxlr,static int detach_target (struct cxl_region *cxlr,int pos) { -struct cxl_region_params *p = &cxlr->params; -int rc; - - rc =down_write_killable (&cxl_region_rwsem); -if (rc) -return rc; - -if (pos >= p->interleave_ways) { -dev_dbg (&cxlr->dev,"position %d out of range %d\n" , pos, - p->interleave_ways); - rc = -ENXIO ; - goto out; - } - -if (!p->targets[pos]) { - rc =0 ; - goto out; - } - - rc =cxl_region_detach (p->targets[pos]); -out: -up_write (&cxl_region_rwsem); -return rc; +CLASS (cxl_decoder_detach, ret)(cxlr,NULL , pos,DETACH_ONLY ); +if (IS_ERR (ret)) +return PTR_ERR (ret); +return 0 ; } static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos,
Re: [PATCH v2 7/8] cxl/region: Introduce CLASS(cxl_decoder_detach...) consolidate multiple paths
Quoted 18 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 18 Jun 2025 22:04:15 -0700 Dan Williams <[email protected]>…
Both
detach_target()andcxld_unregister()want to tear down acxl_regionwhen an endpoint decoder is either detached or destroyed.When a region is to be destroyed
cxl_decoder_detach()releasescxl_region_rwsemunbinds thecxl_regiondriver and re-acquires the rwsem.Quoted 8 lines
This "reverse" locking pattern is difficult to reason about, not amenable to scope-based cleanup, and the minor differences in the calling convention of
cxl_decoder_detach()currently results in thecxl_decoder_kill_region()wrapper.Introduce
CLASS(cxl_decoder_detach...)which creates an object that moves the post-detach cleanup work to a destructor, and consolidates minor preamble differences in the constructor.
Quoted 5 lines
I'm struggling somewhat with grasping how the destructor pattern is useful here. In the two cases the scope is tightly defined around the declaration of the class instance. Doesn't that just boil down to automatically calling the destuctor function immediately? If so what is the use of wrapping it up in a destructor?
tl;dr: yes, the CLASS() is not needed.
I came at this from the perspective of removing the mid-function unlock pattern, and it was useful for me to decompose that into a constructor/destructor pattern. However, you're right, with the refactoring in place this further simplifies into a straightforward helper function scheme.
I have __cxl_decoder_detach() which does the conditional cxl_region lookup, and cxl_decoder_detach() which handles the conditional locking and device_release_driver() follow-up.
PATCH v2 8/8 cxl: Convert to ACQUIRE() for conditional rwsem locking
In reply to Dan WilliamsUse ACQUIRE() to cleanup conditional locking paths in the CXL driver The ACQUIRE() macro and its associated ACQUIRE_ERR() helpers, like scoped_cond_guard(), arrange for scoped-based conditional locking. Unlike scoped_cond_guard(), these macros arrange for an ERR_PTR() to be retrieved representing the state of the conditional lock.
The goal of this conversion is to complete the removal of all explicit unlock calls in the subsystem. I.e. the methods to acquire a lock are solely via guard(), scoped_guard() (for limited cases), or ACQUIRE(). All unlock is implicit / scope-based. In order to make sure all lock sites are converted, the existing rwsem's are consolidated and renamed in 'struct cxl_rwsem'. While that makes the patch noisier it gives a clean cut-off between old-world (explicit unlock allowed), and new world (explicit unlock deleted).
--- drivers/cxl/core/cdat.c | 6 +- drivers/cxl/core/core.h | 43 +++--- drivers/cxl/core/edac.c | 44 +++---- drivers/cxl/core/hdm.c | 41 +++--- drivers/cxl/core/mbox.c | 6 +- drivers/cxl/core/memdev.c | 50 +++---- drivers/cxl/core/port.c | 18 +-- drivers/cxl/core/region.c | 266 +++++++++++++++----------------------- drivers/cxl/cxl.h | 13 +- include/linux/rwsem.h | 1 + 10 files changed, 206 insertions(+), 282 deletions(-)
drivers/cxl/core/cdat.c +3 -3
@@ -336,7 +336,7 @@ static int match_cxlrd_hb(struct device *dev, void *data) cxlrd =to_cxl_root_decoder (dev); cxlsd = &cxlrd->cxlsd; -guard (rwsem_read)(&cxl_region_rwsem); +guard (rwsem_read)(&cxl_rwsem.region);for (int i =0 ; i < cxlsd->nr_targets; i++) {if (host_bridge == cxlsd->target[i]->dport_dev)return 1 ;
@@ -987,7 +987,7 @@ void cxl_region_shared_upstream_bandwidth_update(struct cxl_region *cxlr)bool is_root;int rc; -lockdep_assert_held (&cxl_dpa_rwsem); +lockdep_assert_held (&cxl_rwsem.dpa); struct xarray *usp_xa __free(free_perf_xa) =kzalloc (sizeof (*usp_xa),GFP_KERNEL );
@@ -1057,7 +1057,7 @@ void cxl_region_perf_data_calculate(struct cxl_region *cxlr, {struct cxl_dpa_perf *perf; -lockdep_assert_held (&cxl_dpa_rwsem); +lockdep_assert_held (&cxl_rwsem.dpa); perf =cxled_get_dpa_perf (cxled);if (IS_ERR (perf))
drivers/cxl/core/core.h +28 -15
64 lines not on this page · raw message
drivers/cxl/core/edac.c +20 -24
72 lines not on this page · raw message
drivers/cxl/core/hdm.c +21 -20
137 lines not on this page · raw message
drivers/cxl/core/mbox.c +3 -3
@@ -909,8 +909,8 @@ void cxl_event_trace_record(const struct cxl_memdev *cxlmd,* translations. Take topology mutation locks and lookup * { HPA, REGION } from { DPA, MEMDEV } in the event record. */ -guard (rwsem_read)(&cxl_region_rwsem); -guard (rwsem_read)(&cxl_dpa_rwsem); +guard (rwsem_read)(&cxl_rwsem.region); +guard (rwsem_read)(&cxl_rwsem.dpa); dpa =le64_to_cpu (evt->media_hdr.phys_addr) &CXL_DPA_MASK ; cxlr =cxl_dpa_to_region (cxlmd, dpa);
@@ -1265,7 +1265,7 @@ int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd)/* synchronize with cxl_mem_probe() and decoder write operations */ guard (device)(&cxlmd->dev); endpoint = cxlmd->endpoint; -guard (rwsem_read)(&cxl_region_rwsem); +guard (rwsem_read)(&cxl_rwsem.region);/* * Require an endpoint to be safe otherwise the driver can not * be sure that the device is unmapped.
drivers/cxl/core/memdev.c +18 -32
115 lines not on this page · raw message
drivers/cxl/core/port.c +6 -12
@@ -30,18 +30,12 @@* instantiated by the core. */ -/* -* All changes to the interleave configuration occur with this lock held -* for write. -*/ -DECLARE_RWSEM (cxl_region_rwsem); -static DEFINE_IDA (cxl_port_ida);static DEFINE_XARRAY (cxl_root_buses);int cxl_num_decoders_committed (struct cxl_port *port) { -lockdep_assert_held (&cxl_region_rwsem); +lockdep_assert_held (&cxl_rwsem.region);return port->commit_end +1 ; }
@@ -176,7 +170,7 @@ static ssize_t target_list_show(struct device *dev,ssize_t offset;int rc; -guard (rwsem_read)(&cxl_region_rwsem); +guard (rwsem_read)(&cxl_rwsem.region); rc =emit_target_list (cxlsd, buf);if (rc <0 )return rc;
@@ -196,7 +190,7 @@ static ssize_t mode_show(struct device *dev, struct device_attribute *attr,struct cxl_endpoint_decoder *cxled =to_cxl_endpoint_decoder (dev);struct cxl_memdev *cxlmd =cxled_to_memdev (cxled);struct cxl_dev_state *cxlds = cxlmd->cxlds; -/* without @cxl_dpa_rwsem, make sure @part is not reloaded */ +/* without @cxl_rwsem.dpa, make sure @part is not reloaded */ int part =READ_ONCE (cxled->part);const char *desc;
@@ -235,7 +229,7 @@ static ssize_t dpa_resource_show(struct device *dev, struct device_attribute *at {struct cxl_endpoint_decoder *cxled =to_cxl_endpoint_decoder (dev); -guard (rwsem_read)(&cxl_dpa_rwsem); +guard (rwsem_read)(&cxl_rwsem.dpa);return sysfs_emit (buf,"%#llx\n" , (u64 )cxl_dpa_resource_start (cxled)); }static DEVICE_ATTR_RO (dpa_resource);
@@ -560,7 +554,7 @@ static ssize_t decoders_committed_show(struct device *dev, {struct cxl_port *port =to_cxl_port (dev); -guard (rwsem_read)(&cxl_region_rwsem); +guard (rwsem_read)(&cxl_rwsem.region);return sysfs_emit (buf,"%d\n" ,cxl_num_decoders_committed (port)); }
@@ -1722,7 +1716,7 @@ static int decoder_populate_targets(struct cxl_switch_decoder *cxlsd,if (xa_empty (&port->dports))return -EINVAL ; -guard (rwsem_write)(&cxl_region_rwsem); +guard (rwsem_write)(&cxl_rwsem.region);for (i =0 ; i < cxlsd->cxld.interleave_ways; i++) {struct cxl_dport *dport =find_dport (port, target_map[i]);
drivers/cxl/core/region.c +105 -161
559 lines not on this page · raw message
drivers/cxl/cxl.h +1 -12
@@ -469,7 +469,7 @@ enum cxl_config_state {* @nr_targets: number of targets * @cache_size: extended linear cache size if exists, otherwise zero. * -* State transitions are protected by the cxl_region_rwsem +* State transitions are protected by cxl_rwsem.region */ struct cxl_region_params {enum cxl_config_state state;
@@ -912,15 +912,4 @@ bool cxl_endpoint_decoder_reset_detected(struct cxl_port *port);#endif u16 cxl_gpf_get_dvsec (struct device *dev); - -static inline struct rw_semaphore *rwsem_read_intr_acquire (struct rw_semaphore *rwsem) -{ -if (down_read_interruptible (rwsem)) -return NULL ; - -return rwsem; -} - -DEFINE_FREE(rwsem_read_release, struct rw_semaphore *, if (_T) up_read(_T)) -#endif /* __CXL_H__ */
include/linux/rwsem.h +1 -0
@@ -244,6 +244,7 @@ DEFINE_GUARD_COND(rwsem_read, _intr, down_read_interruptible(_T), _RET == 0) DEFINE_GUARD(rwsem_write, struct rw_semaphore *, down_write(_T), up_write(_T)) DEFINE_GUARD_COND(rwsem_write, _try, down_write_trylock(_T)) +DEFINE_GUARD_COND (rwsem_write, _kill,down_write_killable (_T), _RET ==0 )/* * downgrade write lock to read lock
Signature · 1 line
2.49.0
Re: [PATCH v2 8/8] cxl: Convert to ACQUIRE() for conditional rwsem locking
Quoted 15 lines · On Wed, 18 Jun 2025 22:04:16 -0700 Dan Williams <[email protected]>…
Use
ACQUIRE()to cleanup conditional locking paths in the CXL driver TheACQUIRE()macro and its associatedACQUIRE_ERR()helpers, likescoped_cond_guard(), arrange for scoped-based conditional locking. Unlikescoped_cond_guard(), these macros arrange for anERR_PTR()to be retrieved representing the state of the conditional lock.The goal of this conversion is to complete the removal of all explicit unlock calls in the subsystem. I.e. the methods to acquire a lock are solely via
guard(),scoped_guard()(for limited cases), orACQUIRE(). All unlock is implicit / scope-based. In order to make sure all lock sites are converted, the existing rwsem's are consolidated and renamed in 'structcxl_rwsem'. While that makes the patch noisier it gives a clean cut-off between old-world (explicit unlock allowed), and new world (explicit unlock deleted).
Quoted 13 lines
Cc David Lechner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Linus Torvalds <[email protected]>Cc Ingo Molnar <[email protected]>Cc "Fabio M. De Francesco" <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Cc Shiju Jose <[email protected]>Signed-off-by Dan Williams <[email protected]>
A few comments inline.
Quoted 12 lines
index
010964aa5489..a2ba19151d4f100644drivers/cxl/core/region.c +0
-2static DEVICE_ATTR_RW (interleave_ways);@@ -561,15 +537,11 @@ static ssize_t interleave_granularity_show(struct device *dev, {struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params; -ssize_t rc; -
drivers/cxl/core/region.c static ssize_t interleave_granularity_show(struct device *dev, · line 543
Quoted 10 lines
drivers/cxl/core/region.c +3
-6- rc =down_read_interruptible (&cxl_region_rwsem); -if (rc) -return rc; - rc =sysfs_emit (buf,"%d\n" , p->interleave_granularity); -up_read (&cxl_region_rwsem); -return rc; +ACQUIRE (rwsem_read_intr, rwsem)(&cxl_rwsem.region); +if (ACQUIRE_ERR (rwsem_read_intr, &rwsem)) +return ACQUIRE_ERR (rwsem_read_intr, &rwsem);
Local variable?
Quoted 14 lines
drivers/cxl/core/region.c +1
-7+return sysfs_emit (buf,"%d\n" , p->interleave_granularity); }@@ -2212,19 +2167,19 @@ static int attach_target(struct cxl_region *cxlr, struct cxl_endpoint_decoder *cxled, int pos, unsigned int state) { - int rc = 0; - -if (state ==TASK_INTERRUPTIBLE ) - rc =down_write_killable (&cxl_region_rwsem); -else -down_write (&cxl_region_rwsem); -if (rc)
drivers/cxl/core/region.c static int attach_target(struct cxl_region *cxlr, · line 2174
Quoted 10 lines
drivers/cxl/core/region.c +4
-5-return rc; +int rc; -down_read (&cxl_dpa_rwsem); - rc =cxl_region_attach (cxlr, cxled, pos); -up_read (&cxl_dpa_rwsem); -up_write (&cxl_region_rwsem); +if (state ==TASK_INTERRUPTIBLE ) { +ACQUIRE (rwsem_write_kill, rwsem)(&cxl_rwsem.region); +if ((rc =ACQUIRE_ERR (rwsem_write_kill, &rwsem)) ==0 ) {
I'd lift the warning print to a wrapper function so that you can return early in error case and avoid this rather messy line. e.g.
static int do_attach_target(struct cxl_region *cxlr,
struct cxl_endpoint_decoder *cxled, int pos,
unsigned int state)
if (state == TASK_INTERRUPTIBLE) {
ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region);
rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem);
if (rc)
return rc;
guard(rwsem_read)(&cxl_rwsem.dpa);
return cxl_region_attach(cxlr, cxled, pos);
}
guard(rwsem_write)(&cxl_rwsem.region);
guard(rwsem_read)(&cxl_rwsem.dpa);
return cxl_region_attach(cxlr, cxled, pos);
}static int attach_target(struct cxl_region *cxlr,
struct cxl_endpoint_decoder *cxled, int pos, unsigned int state)
{
int rc = do_attach_target(cxlr, cxled, pos, state);
if (rc)
dev_warn();
return rc;
}Quoted 14 lines
drivers/cxl/core/region.c +8
-0+guard (rwsem_read)(&cxl_rwsem.dpa); + rc =cxl_region_attach (cxlr, cxled, pos); + } + }else { +guard (rwsem_write)(&cxl_rwsem.region); +guard (rwsem_read)(&cxl_rwsem.dpa); + rc =cxl_region_attach (cxlr, cxled, pos); + }if (rc) dev_warn(cxled->cxld.dev.parent,
drivers/cxl/core/region.c static int cxl_region_can_probe(struct cxl_region *cxlr) · line 3523
Quoted 6 lines
drivers/cxl/core/region.c +1
-2@@ -3569,30 +3520,23 @@ static int cxl_region_can_probe(struct cxl_region *cxlr)struct cxl_region_params *p = &cxlr->params;int rc; - rc =down_read_interruptible (&cxl_region_rwsem); -if (rc) { +ACQUIRE (rwsem_read_intr, rwsem)(&cxl_rwsem.region);
Similar to earlier - I'd do this next bit in two lines for slightly better readability. Same for the other cases. I don't care that strongly though.
Quoted 4 lines
drivers/cxl/core/region.c +1
-0+if ((rc =ACQUIRE_ERR (rwsem_read_intr, &rwsem))) {dev_dbg (&cxlr->dev,"probe interrupted\n" );return rc; }
Re: [PATCH v2 8/8] cxl: Convert to ACQUIRE() for conditional rwsem locking
Quoted 54 lines · Jonathan Cameron
Quoted 15 lines · On Wed, 18 Jun 2025 22:04:16 -0700 Dan Williams <[email protected]>…
Use
ACQUIRE()to cleanup conditional locking paths in the CXL driver TheACQUIRE()macro and its associatedACQUIRE_ERR()helpers, likescoped_cond_guard(), arrange for scoped-based conditional locking. Unlikescoped_cond_guard(), these macros arrange for anERR_PTR()to be retrieved representing the state of the conditional lock.The goal of this conversion is to complete the removal of all explicit unlock calls in the subsystem. I.e. the methods to acquire a lock are solely via
guard(),scoped_guard()(for limited cases), orACQUIRE(). All unlock is implicit / scope-based. In order to make sure all lock sites are converted, the existing rwsem's are consolidated and renamed in 'structcxl_rwsem'. While that makes the patch noisier it gives a clean cut-off between old-world (explicit unlock allowed), and new world (explicit unlock deleted).Quoted 13 lines
Cc David Lechner <[email protected]>Cc Peter Zijlstra <[email protected]>Cc Linus Torvalds <[email protected]>Cc Ingo Molnar <[email protected]>Cc "Fabio M. De Francesco" <[email protected]>Cc Davidlohr Bueso <[email protected]>Cc Jonathan Cameron <[email protected]>Cc Dave Jiang <[email protected]>Cc Alison Schofield <[email protected]>Cc Vishal Verma <[email protected]>Cc Ira Weiny <[email protected]>Cc Shiju Jose <[email protected]>Signed-off-by Dan Williams <[email protected]>A few comments inline.
Quoted 18 lines
index
010964aa5489..a2ba19151d4f100644drivers/cxl/core/region.c +0
-7static DEVICE_ATTR_RW (interleave_ways);@@ -561,15 +537,11 @@ static ssize_t interleave_granularity_show(struct device *dev, {struct cxl_region *cxlr =to_cxl_region (dev);struct cxl_region_params *p = &cxlr->params; -ssize_t rc; - - rc =down_read_interruptible (&cxl_region_rwsem); -if (rc) -return rc; - rc =sysfs_emit (buf,"%d\n" , p->interleave_granularity); -up_read (&cxl_region_rwsem);
Quoted 6 lines
drivers/cxl/core/region.c static ssize_t interleave_ways_show(struct device *dev, · line 479
Quoted 4 lines
drivers/cxl/core/region.c +3
-1-return rc; +ACQUIRE (rwsem_read_intr, rwsem)(&cxl_rwsem.region); +if (ACQUIRE_ERR (rwsem_read_intr, &rwsem)) +return ACQUIRE_ERR (rwsem_read_intr, &rwsem);Local variable?
Yeah, I think this was leftover from playing with ways to make this more compact, will switch it to:
if ((rc = ACQUIRE_ERR(@class, @lock)) return rc;
Quoted 52 lines
Quoted 15 lines
drivers/cxl/core/region.c +1
-7+return sysfs_emit (buf,"%d\n" , p->interleave_granularity); }@@ -2212,19 +2167,19 @@ static int attach_target(struct cxl_region *cxlr, struct cxl_endpoint_decoder *cxled, int pos, unsigned int state) { - int rc = 0; - -if (state ==TASK_INTERRUPTIBLE ) - rc =down_write_killable (&cxl_region_rwsem); -else -down_write (&cxl_region_rwsem); -if (rc)drivers/cxl/core/region.c static int attach_target(struct cxl_region *cxlr, · line 2174
Quoted 10 lines
drivers/cxl/core/region.c +4
-5-return rc; +int rc; -down_read (&cxl_dpa_rwsem); - rc =cxl_region_attach (cxlr, cxled, pos); -up_read (&cxl_dpa_rwsem); -up_write (&cxl_region_rwsem); +if (state ==TASK_INTERRUPTIBLE ) { +ACQUIRE (rwsem_write_kill, rwsem)(&cxl_rwsem.region); +if ((rc =ACQUIRE_ERR (rwsem_write_kill, &rwsem)) ==0 ) {I'd lift the warning print to a wrapper function so that you can return early in error case and avoid this rather messy line. e.g.
static int do_attach_target(struct
cxl_region*cxlr,struct cxl_endpoint_decoder *cxled, int pos, unsigned int state) if (state == TASK_INTERRUPTIBLE) { ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem); if (rc) return rc; guard(rwsem_read)(&cxl_rwsem.dpa); return cxl_region_attach(cxlr, cxled, pos); } guard(rwsem_write)(&cxl_rwsem.region); guard(rwsem_read)(&cxl_rwsem.dpa); return cxl_region_attach(cxlr, cxled, pos); }static int attach_target(struct
cxl_region*cxlr,
structcxl_endpoint_decoder*cxled, int pos, unsigned int state)
Quoted 8 lines
{
int rc = do_attach_target(cxlr, cxled, pos, state);
if (rc)
dev_warn();
return rc;
}Yes, I like that better.
Quoted 18 lines
Quoted 18 lines
drivers/cxl/core/region.c +8
-0+guard (rwsem_read)(&cxl_rwsem.dpa); + rc =cxl_region_attach (cxlr, cxled, pos); + } + }else { +guard (rwsem_write)(&cxl_rwsem.region); +guard (rwsem_read)(&cxl_rwsem.dpa); + rc =cxl_region_attach (cxlr, cxled, pos); + }if (rc) dev_warn(cxled->cxld.dev.parent,@@ -3569,30 +3520,23 @@ static int cxl_region_can_probe(struct cxl_region *cxlr)struct cxl_region_params *p = &cxlr->params;int rc;
Quoted 7 lines
drivers/cxl/core/region.c static int cxl_region_can_probe(struct cxl_region *cxlr) · line 3523
Quoted 3 lines
drivers/cxl/core/region.c +1
-2- rc =down_read_interruptible (&cxl_region_rwsem); -if (rc) { +ACQUIRE (rwsem_read_intr, rwsem)(&cxl_rwsem.region);Similar to earlier - I'd do this next bit in two lines for slightly better readability. Same for the other cases. I don't care that strongly though.
I want to keep the compactness, if only in the CXL subsystem.
Quoted 6 lines · Dan Williams
Note, all the code in patch1 is Peter's I just wrapped it in a changelog and added some commentary. Peter, forgive me if you were still in the process of circling back to this topic. I marked the patch attributed to you as: "Not-yet-signed-off-by". Otherwise, my motivation for going ahead with a formal submission are the multiple patchsets in my review / development queue where I would like to use
ACQUIRE().
Definitely a case of too many balls in the air. Feel free to make that Signed-off-by, and also:
for the patches that were sent my way. Thanks for the changelogs and pushing this ahead.
Alison Schofield
maintainerQuoted 37 lines · Dan Williams
Changes since v1: [1]
* Peter took one look at v1 and rewrote it into something significantly better. Unlike my attempt that required suffering a new parallel universe of lock guards, the rewrite reuses existing lock guards.ACQUIRE()can be used any placeguard()can be used, and addsACQUIRE_ERR()to pass the result of conditional locks.[1]: http://lore.kernel.org/[email protected]
Note, all the code in patch1 is Peter's I just wrapped it in a changelog and added some commentary. Peter, forgive me if you were still in the process of circling back to this topic. I marked the patch attributed to you as: "Not-yet-signed-off-by". Otherwise, my motivation for going ahead with a formal submission are the multiple patchsets in my review / development queue where I would like to use
ACQUIRE().The orginal motivation of v1 for this work is that the CXL subsystem adopted scope-based helpers and achieved some decent cleanups. However, that work stalled with conditional locks. It stalled due to the pain points of
scoped_cond_guard()detailed in patch1.This work also allows for replacing open-coded equivalents like
rwsem_read_intr_acquire()that went upstream in v6.16:
0c6e6f1357cbcxl/edac: Add CXL memory device patrol scrub control featureThe open question from the discussion [2] was whether it was worth defining a
__GUARD_IS_ERR()asm helper. I left that alone.Lastly, this version of
ACQUIRE_ERR()matches Peter's original proposal to have the caller pass the lock scope variable by reference [3]. My change of heart came from looking at the conversion and wantingACQUIRE_ERR()to be more visually distinct fromACQUIRE()especially because it is accessing lock condition metadata, not the lock itself.E.g.
Quoted 7 lines
ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); if ((ret = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) return ret;Yes, checkpatch disagrees with assignment in
if(), butcleanup.halready demands other expections for historical style, and a compact / limited idiom forACQUIRE_ERR()feels reasonable.
Hi Dan,
I've been building upon this set and applying this diff to squelch those checkpatch ERRORs. Please take a look and consider adding for review in next version.
scripts/checkpatch.pl +8 -1
@@ -5682,7 +5682,14 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0; - if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)\)/s) { + my $expr = $1; + + # Allow ACQUIRE_ERR() special case + if ($expr =~ /\w+\s*=\s*ACQUIRE_ERR\s*\(/) { + next; + } + if (ERROR("ASSIGN_IN_IF", "do not use assignment in if condition\n" . $herecurr) && $fix && $perl_version_ok) {
snip
Quoted 59 lines · Alison Schofield
Quoted 37 lines · Dan Williams
Changes since v1: [1]
* Peter took one look at v1 and rewrote it into something significantly better. Unlike my attempt that required suffering a new parallel universe of lock guards, the rewrite reuses existing lock guards.ACQUIRE()can be used any placeguard()can be used, and addsACQUIRE_ERR()to pass the result of conditional locks.[1]: http://lore.kernel.org/[email protected]
Note, all the code in patch1 is Peter's I just wrapped it in a changelog and added some commentary. Peter, forgive me if you were still in the process of circling back to this topic. I marked the patch attributed to you as: "Not-yet-signed-off-by". Otherwise, my motivation for going ahead with a formal submission are the multiple patchsets in my review / development queue where I would like to use
ACQUIRE().The orginal motivation of v1 for this work is that the CXL subsystem adopted scope-based helpers and achieved some decent cleanups. However, that work stalled with conditional locks. It stalled due to the pain points of
scoped_cond_guard()detailed in patch1.This work also allows for replacing open-coded equivalents like
rwsem_read_intr_acquire()that went upstream in v6.16:
0c6e6f1357cbcxl/edac: Add CXL memory device patrol scrub control featureThe open question from the discussion [2] was whether it was worth defining a
__GUARD_IS_ERR()asm helper. I left that alone.Lastly, this version of
ACQUIRE_ERR()matches Peter's original proposal to have the caller pass the lock scope variable by reference [3]. My change of heart came from looking at the conversion and wantingACQUIRE_ERR()to be more visually distinct fromACQUIRE()especially because it is accessing lock condition metadata, not the lock itself.E.g.
Quoted 7 lines
ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); if ((ret = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) return ret;Yes, checkpatch disagrees with assignment in
if(), butcleanup.halready demands other expections for historical style, and a compact / limited idiom forACQUIRE_ERR()feels reasonable.Hi Dan,
I've been building upon this set and applying this diff to squelch those checkpatch ERRORs. Please take a look and consider adding for review in next version.
scripts/checkpatch.pl +0
-0@@ -5682,7 +5682,14 @@ sub process { my ($s, $c) = ($stat, $cond); my $fixed_assign_in_if = 0;
scripts/checkpatch.pl sub process { · line 5692
Quoted 10 lines
scripts/checkpatch.pl +8
-1- if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { + if ($c =~ /\bif\s*\((.*[^<>!=]=[^=].*)\)/s) { + my $expr = $1; + + # Allow ACQUIRE_ERR() special case + if ($expr =~ /\w+\s*=\s*ACQUIRE_ERR\s*\(/) { + next; + } +
Thanks! This lookls like a good fixup to send after ACQUIRE_ERR() moves upstream. Should probably go with a wider set to update checkpatch's understanding of other scoped-based-macros like DEFINE_{FREE,GUARD}().