Re: [PATCH v2 8/9] atomic,x86: Alternative atomic_*_overflow() scheme

From: Linus Torvalds
Date: Mon Dec 13 2021 - 13:11:57 EST


On Mon, Dec 13, 2021 at 8:43 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> So Marco was expressing doubt about this exact interface for the
> atomic_*_overflow() functions, since it's extremely easy to get the
> whole ATOMIC_OVERFLOW_OFFSET thing wrong.

I missed that discussion (maybe it was on irc? Or maybe I just get too
much email).

Anyway, my preferred solution would simply be to make the ref-counting
atomics use a different type.

Voilà, problem solved. You can't really misuse them by mistake,
because you can't access it by mistake.

Sure, it could be a wrapper around 'atomic_t' on architectures that
end up using the generic fallback, so it might be as simple as

typedef atomic_t atomic_ref_t;

in some asm-generic implementation, although I suspect that you'd want
type safety even there, and do

typedef struct { atomic_t atomic_val; } atomic_ref_t;

But then on x86 - and other architectures that might prefer to use
that offset trick because they have flags - I'm not sure it even makes
sense to have anything to do with 'atomic_t' at all, since there would
basically be zero overlap with the regular atomic operations (partly
due to the offset, but partly simply because the 'ref' operations are
simply different).

(Wrt naming: I do think this is more about the "ref" part than the
"overflow" part - thus I'd suggest the "atomic_ref_t" rather than your
ofl naming).

Linus