Re: [PATCH v2 3/9] atomic: Introduce atomic_{inc,dec,dec_and_test}_overflow()

From: Peter Zijlstra
Date: Mon Dec 13 2021 - 05:59:57 EST


On Mon, Dec 13, 2021 at 10:06:01AM +0000, Mark Rutland wrote:
> On Fri, Dec 10, 2021 at 05:16:21PM +0100, Peter Zijlstra wrote:

> > +#ifndef arch_atomic_inc_overflow
> > +#define arch_atomic_inc_overflow(_v, _label) \
> > +do { \
> > + int __old = arch_atomic_fetch_inc(_v); \
> > + if (unlikely(__old <= 0)) \
> > + goto _label; \
> > +} while (0)
> > +#endif
> > +
> > +#ifndef arch_atomic_dec_overflow
> > +#define arch_atomic_dec_overflow(_v, _label) \
> > +do { \
> > + int __new = arch_atomic_dec_return(_v); \
> > + if (unlikely(__new <= 0)) \
> > + goto _label; \
> > +} while (0)
> > +#endif
> > +
> > +#ifndef arch_atomic_dec_and_test_overflow
> > +#define arch_atomic_dec_and_test_overflow(_v, _label) \
> > +({ \
> > + bool __ret = false; \
> > + int __new = arch_atomic_dec_return(_v); \
> > + if (unlikely(__new < 0)) \
> > + goto _label; \
> > + if (unlikely(__new == 0)) \
> > + __ret = true; \
> > + __ret; \
> > +})
> > +#endif
>
> I had wanted to move at least part of this to a function to ensure
> single-evaluation and avoid accidental symbol aliasing, but (as we discussed
> over IRC) I couldn't find any good way to do so, and given this is sufficiently
> specialise I think we should be ok with this as-is. It's certainly no worse
> than the existing stuff for xchg/cmpxchg.

Right, as you know I tried the same :-) Anyway, the above macros should
be free of multi-evaluation issues, both _v and _label are only used the
once. Aliassing is always a possibility but minimized by __ prefixing
the local variables.