lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <687042b7ef13f_588c10013@dwillia2-xfh.jf.intel.com.notmuch>
Date: Thu, 10 Jul 2025 15:46:15 -0700
From: <dan.j.williams@...el.com>
To: Jonathan Cameron <Jonathan.Cameron@...wei.com>, Dan Williams
	<dan.j.williams@...el.com>
CC: <linux-cxl@...r.kernel.org>, <linux-kernel@...r.kernel.org>, "Peter
 Zijlstra" <peterz@...radead.org>, Ingo Molnar <mingo@...nel.org>, "Linus
 Torvalds" <torvalds@...ux-foundation.org>, David Lechner
	<dlechner@...libre.com>, "Fabio M. De Francesco"
	<fabio.m.de.francesco@...ux.intel.com>
Subject: Re: [PATCH v2 1/8] cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for
 conditional locks

Jonathan Cameron wrote:
> On Wed, 18 Jun 2025 22:04:09 -0700
> Dan Williams <dan.j.williams@...el.com> wrote:
> 
> > From: Peter Zijlstra <peterz@...radead.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@...nel.org>
> > Cc: Linus Torvalds <torvalds@...ux-foundation.org>
> > Cc: David Lechner <dlechner@...libre.com>
> > Cc: Fabio M. De Francesco <fabio.m.de.francesco@...ux.intel.com>
> > Not-yet-signed-off-by: Peter Zijlstra <peterz@...radead.org>
> > [djbw: wrap Peter's proposal with changelog and comments]
> > Co-developed-by: Dan Williams <dan.j.williams@...el.com>
> > Signed-off-by: Dan Williams <dan.j.williams@...el.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;                                                 \
+       }

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ