Re: [RFC][PATCH] locking/refcount: Provide __refcount API to obtain the old value

From: Will Deacon
Date: Mon Sep 21 2020 - 07:22:27 EST


On Wed, Jul 29, 2020 at 01:11:20PM +0200, peterz@xxxxxxxxxxxxx wrote:
> On Tue, Jul 28, 2020 at 11:56:38AM +0100, David Howells wrote:
> > Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> >
> > > > Please do not _undo_ the changes; just add the API you need.
> > >
> > > add_return and sub_return are horrible interface for refcount, which is
> > > the problem.
> > >
> > > If you meant: refcount_dec(), but want the old value for tracing, you
> > > want a different ordering than if you wanted to do
> > > refcount_dec_and_test(); dec_return can't know this.
> > >
> > > David, would something like a __refcount_*() API work where there is a
> > > 3rd argument (int *), which, if !NULL, will be assigned the old value?
> >
> > That would be fine, though the number needs to be something I can interpret
> > easily when looking through the traces. It would also be okay if there was an
> > interpretation function that I could use in the trace point when setting the
> > variable.
>
> I'm not entirely sure what you mean with interpret, provided you don't
> trigger a refcount fail, the number will be just what you expect and
> would get from refcount_read(). If you do trigger a fail, you'll get a
> negative value.
>
> How's the below? I didn't provide __refcount versions for the external
> functions, I suppose that can be done too, but wondered if you really
> needed those.

It looks like this didn't go anywhere, but I'm supportive of the general
idea if it's useful to somebody. The only part I find a bit grotty is that
we end up introducing a bunch of redundancy in some of the new functions,
e.g.:


> @@ -219,9 +235,14 @@ static inline void refcount_add(int i, r
> *
> * Return: true if the increment was successful, false otherwise
> */
> +static inline __must_check bool __refcount_inc_not_zero(refcount_t *r, int *oldp)
> +{
> + return __refcount_add_not_zero(1, r, oldp);
> +}

Where returning both a bool to indicate whether the old value was zero
and also the old value itself is a bit OTT.

Will