From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
To: "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>, <dan.j.williams@intel.com>,
<linux-kernel@vger.kernel.org>, Ingo Molnar <mingo@kernel.org>,
<linux-cxl@vger.kernel.org>, Ira Weiny <ira.weiny@intel.com>
Subject: Re: [RFC PATCH v3] cleanup: Add cond_guard() to conditional guards
Date: Thu, 1 Feb 2024 16:05:05 +0000 [thread overview]
Message-ID: <20240201160505.00007151@Huawei.com> (raw)
In-Reply-To: <3280120.44csPzL39Z@fdefranc-mobl3>
On Thu, 01 Feb 2024 16:32:25 +0100
"Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote:
> On Thursday, 1 February 2024 16:13:34 CET Fabio M. De Francesco wrote:
> > On Thursday, 1 February 2024 12:36:12 CET Jonathan Cameron wrote:
> > > On Thu, 01 Feb 2024 09:16:59 +0100
> > >
> > > "Fabio M. De Francesco" <fabio.maria.de.francesco@linux.intel.com> wrote:
> > > > [snip]
> > > >
> > > > Actually, I'm doing this:
> > > > cond_guard(..., rc, 0, -EINTR, ...);
> > >
> > > Can we not works some magic to do.
> > >
> > > cond_guard(..., return -EINTR, ...)
> > >
> > > and not have an rc at all if we don't want to.
> > >
> > > Something like
> > >
> > > #define cond_guard(_name, _fail, args...) \
> > >
> > > CLASS(_name, scope)(args); \
> > > if (!__guard_ptr(_name)(&scope)) _fail
> > >
> > > Completely untested so I'm probably missing some subtleties.
> > >
> > > Jonathan
> >
> > Jonathan,
> >
> > Can you please comment on the v5 of this RFC?
Would lose context of this discussion.
> > It is at
> > https://lore.kernel.org/all/20240201131033.9850-1-fabio.maria.de.francesco@
> > linux.intel.com/
> >
> > The macro introduced in v5 has the following, more general, use case:
> >
> > * * int ret;
> > + * // down_read_trylock() returns 1 on success, 0 on contention
> > + * cond_guard(rwsem_read_try, ret, 1, 0, &sem);
> > + * if (!ret) {
> > + * dev_dbg("down_read_trylock() failed to down 'sem')\n");
> > + * return ret;
> > + * }
> >
> > The text above has been copy-pasted from the RFC Patch v5.
> >
> > Please notice that we need to provide both the success and the failure code
> > to make it work also with the _trylock() variants (more details in the
> > patch).
>
> The next three lines have been messed up by a copy-paste.
> They are:
>
> If we simply do something like:
>
> cond_guard(..., ret = 0, ...)
>
> We won't store the success (that is 1) in ret and it would still contain 0,
> that is the code of the contended case.
If there are cases that need to do different things in the two paths the
define full conditions for success and failure.
#define cond_guard(_name, _fail, _success, args...) \
CLASS(_name, scope)(args); \
if (!__guard_ptr(_name)(&scope)) _fail; \
else _success
However I'm not sure that additional complexity is worth while.
Maybe just handling failure is all we need.
This should allow
cond_guard(rwsem_read_try, return -EINVAL, , lock); or
cond_guard(rwsem_read_try, rc = 1, rc = 0, lock);
So similar to scoped_cond_guard() there is no need to
have a local variable if all you want to do is return on
failure.
>
> > If we simply do something like:
> >
> > cond_guard(..., ret = 0, ...)
> >
> > to be able store in 'ret' the code of the contended case, that is 0.
> >
> > Since down_read_trylock() returns 1 on down semaphore, when we later check
> > 'ret' with "if (!ret) <failure path>;" we always enter in that failure path
> > even if the semaphore is down because we didn't store the success code in
> > ret (and ret is still probably 0).
> >
> > This is why, I think, we need a five arguments cond_guard(). This can manage
> > also the _interruptible() and _killable() cases as:
> >
> > cond_guard(..., ret, 0, -EINTR, ...)
> >
> > In this case we don't need 5 arguments, but we have a general use case, one
> > only macro, that can work with all the three variants of locks.
> >
> > Fabio
>
>
>
>
prev parent reply other threads:[~2024-02-01 16:05 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-31 13:37 [RFC PATCH v3] cleanup: Add cond_guard() to conditional guards Fabio M. De Francesco
2024-02-01 1:08 ` Fabio M. De Francesco
2024-02-01 1:12 ` Dan Williams
2024-02-01 1:25 ` Fabio M. De Francesco
2024-02-01 8:16 ` Fabio M. De Francesco
2024-02-01 11:36 ` Jonathan Cameron
2024-02-01 15:13 ` Fabio M. De Francesco
2024-02-01 15:32 ` Fabio M. De Francesco
2024-02-01 16:05 ` Jonathan Cameron [this message]
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=20240201160505.00007151@Huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=dan.j.williams@intel.com \
--cc=fabio.maria.de.francesco@linux.intel.com \
--cc=ira.weiny@intel.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.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