Re: [PATCH] SUNRPC: Don't allow compiler optimisation of svc_xprt_release_slot()

Trond Myklebust
2019-01-03T23:41:06Z

On Thu, 2019-01-03 at 17:45 -0500, J Bruce Fields wrote:
> On Thu, Jan 03, 2019 at 09:17:12AM -0500, Trond Myklebust wrote:
> > Use READ_ONCE() to tell the compiler to not optimse away the read
> > of
> > xprt->xpt_flags in svc_xprt_release_slot().
>
> What exactly is the possible race here? And why is a READ_ONCE()
> sufficient, as opposed to some memory barriers?
>
> I may need to shut myself in a room with memory-barriers.txt, I'm
> pretty
> hazy on these things.
>

It's not about fixing any races. It is about ensuring that the compiler
does not optimise away the read if the function is ever called from
inside a loop. Not an important fix, since I'm not aware of any cases
where this has happened. However strictly speaking, we should use
READ_ONCE() here because that variable is volatile; it can be changed
by a background action.

> --b.
>
> > Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
> > ---
> > net/sunrpc/svc_xprt.c | 6 ++++--
> > 1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c
> > index 51d36230b6e3..94d344325e22 100644
> > --- a/net/sunrpc/svc_xprt.c
> > +++ b/net/sunrpc/svc_xprt.c
> > @@ -363,9 +363,11 @@ static void svc_xprt_release_slot(struct
> > svc_rqst *rqstp)
> >
> > static bool svc_xprt_has_something_to_do(struct svc_xprt *xprt)
> > {
> > - if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE)))
> > + unsigned long xpt_flags = READ_ONCE(xprt->xpt_flags);
> > +
> > + if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE)))
> > return true;
> > - if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED))) {
> > + if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) {
> > if (xprt->xpt_ops->xpo_has_wspace(xprt) &&
> > svc_xprt_slots_in_range(xprt))
> > return true;
> > --
> > 2.20.1
--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
trond.myklebust@hammerspace.com