Re: [PATCH -tip 00/12] locking/atomics: Add and use inc,dec calls for FETCH-OP flavors

From: Davidlohr Bueso
Date: Fri Jun 24 2016 - 16:36:10 EST


On Fri, 24 Jun 2016, James Bottomley wrote:

On Fri, 2016-06-24 at 10:30 -0700, Davidlohr Bueso wrote:
On Fri, 24 Jun 2016, James Bottomley wrote:

> On Mon, 2016-06-20 at 13:05 -0700, Davidlohr Bueso wrote:
> > Hi,
> >
> > The series is really straightforward and based on Peter's work
> > that introduces[1] the atomic_fetch_$op machinery. Only patch 1
> > implements the actual atomic_fetch_{inc,dec} calls based on
> > atomic_fetch_{add,sub}.
>
> Could I just ask why? atomic_inc_return(x) - 1 seems a reasonable
> thing to do to me.

For one restoring the old state like that can be racy and looses the
notion of atomicity.

I don't understand this argument: any return of an atomic value is
inherently racy because the atomic source may have changed by the time
you use the returned value. It's no more or less racy to my mind to
return the original value and increment than to return the incremented
value and subtract one.

I was looking at the n + xadd() vs xadd(). But yeah, same applies to
checking the return value of any cas operation.

The new family of atomic_fetch_$ops also better express the purpose
of the call imo.

So this is probably the core of my objection: adding APIs simply
because we can. A good reason to add things like this is because it's
a common pattern people get wrong, because we can optimize it nicely on
an architecture, or some other good reason. Absent a good reason it
doesn't seem like a good API addition because trying to keep up with
all the API variants when you want to use atomics adds to the burden of
the programmer.

That all makes sense, but again the 'race' was my main concern. Although
saving the add cycles in the rwsem (loop) path is always welcome. The rest,
and including this patchset, is merely for completeness.
Thanks,
Davidlohr