Re: [PATCH v3 00/11] Introduce Simple atomic counters

From: Shuah Khan
Date: Wed Oct 14 2020 - 05:25:16 EST


On 10/10/20 5:09 AM, Peter Zijlstra wrote:
On Fri, Oct 09, 2020 at 01:45:43PM -0700, Kees Cook wrote:
On Fri, Oct 09, 2020 at 09:37:46PM +0200, Peter Zijlstra wrote:
On Fri, Oct 09, 2020 at 09:55:55AM -0600, Shuah Khan wrote:
Simple atomic counters api provides interfaces for simple atomic counters
that just count, and don't guard resource lifetimes. The interfaces are
built on top of atomic_t api, providing a smaller subset of atomic_t
interfaces necessary to support simple counters.

To what actual purpose?!? AFACIT its pointless wrappery, it gets us
nothing.

It's not pointless. There is value is separating types for behavioral
constraint to avoid flaws. atomic_t provides a native operation. We gained
refcount_t for the "must not wrap" type, and this gets us the other side
of that behavioral type, which is "wrapping is expected". Separating the
atomic_t uses allows for a clearer path to being able to reason about
code flow, whether it be a human or a static analyzer.

refcount_t got us actual rutime exceptions that atomic_t doesn't. This
propsal gets us nothing.

atomic_t is very much expected to wrap.

The counter wrappers add nothing to the image size, and only serve to
confine the API to one that cannot be used for lifetime management.

It doesn't add anything period. It doesn't get us new behaviour, it
splits a 'can wrap' use-case from a 'can wrap' type. That's sodding
pointless.


They don't add any new behavior, As Kees mentioned they do give us a
way to clearly differentiate atomic usages that can wrap.

Let's discuss the problem at hand before dismissing it as pointless.

Worse, it mixes 2 unrelated cases into one type, which just makes a
mockery of things (all the inc_return users are not statistics, some
might even mis-behave if they wrap).


You are right that all inc_return usages aren't statistics. There are
3 distinct usages:

1. Stats
2. Cases where wrapping is fine
3. Cases where wrapping could be a problem. In which case, this API
shouldn't be used.

There is no need to keep inc_return in this API as such. I included it
so it can be used for above cases 1 and 2, so the users don't have to
call inc() followed by read(). It can be left out of the API.

The atomic_t usages in the kernel fall into the following categories:

1. Stats (tolerance for accuracy determines whether they need to be
atomic or not). RFC version included non-atomic API for cases
when lossiness is acceptable. All these cases use/need just init
and inc. There are two variations in this case:

a. No checks for wrapping. Use signed value.
b. No checks for wrapping, but return unsigned.

2. Reference counters that release resource and rapping could result
in use-after-free type problems. There are two variations in this
case:

a. Increments and decrements aren't bounded.
b. Increments and decrements are bounded.

Currently tools that flag unsafe atomic_t usages that are candidates
for refcount_t conversions don't make a distinction between the two.

The second case, since increments and decrements are bounded, it is
safe to continue to use it. At the moment there is no good way to
tell them apart other than looking at each of these cases.

3. Reference counters that manage/control states. Wrapping is a problem
in this case, as it could lead to undefined behavior. These cases
don't use test and free, use inc/dec. At the moment there is no good
way to tell them apart other than looking at each of these cases.
This is addressed by REFCOUNT_SATURATED case.

This API addresses 1a. Stats. No checks for wrapping. Use signed value
at the moment with plan to add support for unsigned for cases where
unsigned is being used.

It is possible to cover 2b in this API, so it becomes easier to make a
clear distinction the two cases and we can focus on only the atomic_t
cases that need to converted to refcount_t. This is easy to do by
allowing max. threshold for the variable and checking against that
and not letting it go above it.

There are several atomic_t usages that use just:

-- init or set and inc
-- init or set and inc/dec (including the ones that manage state)
-- Increments and decrements are bounded

Creating a sub-set of atomic_t api would help us with differentiate
these cases and make it easy for us identify and fix cases where
refcount_t should be used.

Would you be open to considering a subset if it addresses 2b and
unsigned returns for stats?

thanks,
-- Shuah