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

Trond Myklebust
2019-01-08T16:22:25Z

On Tue, 2019-01-08 at 10:01 -0500, bfields@fieldses.org wrote:
> On Mon, Jan 07, 2019 at 10:06:19PM +0000, Trond Myklebust wrote:
> > On Mon, 2019-01-07 at 16:32 -0500, bfields@fieldses.org wrote:
> > > So maybe we actually need
> > >
> > > static bool (struct svc_xprt *xprt)
> > > {
> > > + mb();
> >
> > You would at best need a 'smp_rmb()'. There is nothing to gain from
> > adding a write barrier here,
>
> That's not my understanding.
>
> What we have is basically:
>
> 1 2
> ---- ----
> WRITE to A WRITE to B
>
> READ from A and B READ from A and B
>
> and we want to guarantee that at least one of those two reads will
> see
> both of the writes.
>
> A read barrier only orders reads with respect to the barrier, it
> doesn't
> do anything about writes, so doesn't guarantee anything here.

In this context 'WRITE to A' and/or 'WRITE to B' are presumably the
operations of setting the flag bits in xprt->xpt_flags, no? That's not
occurring here, it is occurring elsewhere.

The test_and_set_bit(XPT_DATA, &xprt->xpt_flags) in svc_data_ready()
performs an explicit barrier, so we shouldn't really care. The other
cases where we do set_bit(XPT_DATA) don't matter since the socket has
its own locking, and so the XPT_DATA is really just a test for whether
or not we need to enqueue the svc_xprt.

In the only place where XPT_DEFERRED is set, you have an implicit write
barrier (due to a spin_unlock) between the call to set_bit() and the
call to svc_xprt_enqueue(), so all data writes are guaranteed to be
complete before any attempt to enqueue the socket.

I can't see that you really care for the case of XPT_CONN, since the
just-created socket isn't going to be visible to other cpus until
you've added it to &pool->sp_sockets (which also has implicit write
barriers due to spin locks).

I don't think you really care for the case of XPT_CLOSE either since
svc_delete_xprt() doesn't depend on any other data writes that aren't
already protected by spinlocks.

So the conclusion would be to add smp_rmb() in
svc_xprt_has_something_to_do(). No extra write barriers are needed
AFAICS.
You may still need the READ_ONCE() in order to add a data dependency
barrier (i.e. to ensure that alpha processors don't reorder reads of
the xpt_flags with other speculative reads). That should reduce to a
standard read on all non-alpha architectures.

>
> --b.
>
>
>
> > and you don't even need a read barrier in
> > the non-smp case.
> >
> > > if (xprt->xpt_flags & ((1<<XPT_CONN)|(1<<XPT_CLOSE)))
> > > return true;
> > > if (xprt->xpt_flags & ((1<<XPT_DATA)|(1<<XPT_DEFERRED))) {
> > >
> > > Then whichever memory barrier executes second guarantees that the
> > > following check sees the result of both the XPT_DATA and
> > > xpt_nr_rqsts
> > > changes. I think....
> >
--
Trond Myklebust
Linux NFS client maintainer, Hammerspace
trond.myklebust@hammerspace.com