From: <dan.j.williams@intel.com>
To: Jonathan Cameron <Jonathan.Cameron@huawei.com>,
Dan Williams <dan.j.williams@intel.com>
Cc: <linux-cxl@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
"Linus Torvalds" <torvalds@linux-foundation.org>,
David Lechner <dlechner@baylibre.com>,
"Fabio M. De Francesco" <fabio.m.de.francesco@linux.intel.com>
Subject: Re: [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks
Date: Thu, 10 Jul 2025 15:46:15 -0700 [thread overview]
Message-ID: <687042b7ef13f_588c10013@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <20250623110544.0000753d@huawei.com>
Jonathan Cameron wrote:
> On Wed, 18 Jun 2025 22:04:09 -0700
> Dan Williams <dan.j.williams@intel.com> wrote:
>
> > From: Peter Zijlstra <peterz@infradead.org>
> >
> > 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.
> >
> > Link: http://lore.kernel.org/all/Z1LBnX9TpZLR5Dkf@gmail.com [1]
> > Link: http://patch.msgid.link/20250512105026.GP4439@noisy.programming.kicks-ass.net
> > Link: http://patch.msgid.link/20250512185817.GA1808@noisy.programming.kicks-ass.net
> > Cc: Ingo Molnar <mingo@kernel.org>
> > Cc: Linus Torvalds <torvalds@linux-foundation.org>
> > Cc: David Lechner <dlechner@baylibre.com>
> > Cc: Fabio M. De Francesco <fabio.m.de.francesco@linux.intel.com>
> > Not-yet-signed-off-by: Peter Zijlstra <peterz@infradead.org>
> > [djbw: wrap Peter's proposal with changelog and comments]
> > Co-developed-by: Dan Williams <dan.j.williams@intel.com>
> > Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> This looks like a nice solution. One trivial style thing inline.
>
> > ---
> > include/linux/cleanup.h | 77 ++++++++++++++++++++++++++++++++++-------
> > include/linux/mutex.h | 2 +-
> > include/linux/rwsem.h | 2 +-
> > 3 files changed, 67 insertions(+), 14 deletions(-)
> >
> > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> > index 7093e1d08af0..1e1eb35cc225 100644
> > --- a/include/linux/cleanup.h
> > +++ b/include/linux/cleanup.h
> > +#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); })
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.
>
> > +
> > #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; \
+ }
next prev parent reply other threads:[~2025-07-10 22:47 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-19 5:04 [PATCH v2 0/8] cleanup: Introduce ACQUIRE(), a guard() for conditional locks Dan Williams
2025-06-19 5:04 ` [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() " Dan Williams
2025-06-19 21:17 ` Dan Williams
2025-06-23 10:05 ` Jonathan Cameron
2025-07-10 22:46 ` dan.j.williams [this message]
2025-06-19 5:04 ` [PATCH v2 2/8] cxl/mbox: Convert poison list mutex to ACQUIRE() Dan Williams
2025-06-20 20:43 ` Alison Schofield
2025-06-23 10:08 ` Jonathan Cameron
2025-07-10 22:25 ` dan.j.williams
2025-06-23 14:49 ` Dave Jiang
2025-06-19 5:04 ` [PATCH v2 3/8] cxl/decoder: Move decoder register programming to a helper Dan Williams
2025-06-20 21:00 ` Alison Schofield
2025-06-23 10:51 ` Jonathan Cameron
2025-06-23 14:50 ` Dave Jiang
2025-06-19 5:04 ` [PATCH v2 4/8] cxl/decoder: Drop pointless locking Dan Williams
2025-06-19 23:40 ` Davidlohr Bueso
2025-06-20 21:02 ` Alison Schofield
2025-06-23 10:53 ` Jonathan Cameron
2025-06-23 14:51 ` Dave Jiang
2025-06-19 5:04 ` [PATCH v2 5/8] cxl/region: Split commit_store() into __commit() and queue_reset() helpers Dan Williams
2025-06-20 21:32 ` Alison Schofield
2025-06-21 4:51 ` dan.j.williams
2025-06-23 10:59 ` Jonathan Cameron
2025-06-23 14:59 ` Dave Jiang
2025-06-19 5:04 ` [PATCH v2 6/8] cxl/region: Move ready-to-probe state check to a helper Dan Williams
2025-06-23 15:01 ` Dave Jiang
2025-06-19 5:04 ` [PATCH v2 7/8] cxl/region: Introduce CLASS(cxl_decoder_detach...) consolidate multiple paths Dan Williams
2025-06-23 10:49 ` Jonathan Cameron
2025-07-11 4:12 ` dan.j.williams
2025-06-19 5:04 ` [PATCH v2 8/8] cxl: Convert to ACQUIRE() for conditional rwsem locking Dan Williams
2025-06-23 10:32 ` Jonathan Cameron
2025-07-11 3:21 ` dan.j.williams
2025-06-19 11:13 ` [PATCH v2 0/8] cleanup: Introduce ACQUIRE(), a guard() for conditional locks Peter Zijlstra
2025-07-02 23:39 ` Alison Schofield
2025-07-11 4:28 ` dan.j.williams
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=687042b7ef13f_588c10013@dwillia2-xfh.jf.intel.com.notmuch \
--to=dan.j.williams@intel.com \
--cc=Jonathan.Cameron@huawei.com \
--cc=dlechner@baylibre.com \
--cc=fabio.m.de.francesco@linux.intel.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).