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

From: Linus Torvalds
Date: Mon Dec 13 2021 - 13:25:10 EST


On Mon, Dec 13, 2021 at 10:19 AM Marco Elver <elver@xxxxxxxxxx> wrote:
>
> I'm still genuinely worried about this:
>
> > 2. Yet another potentially larger issue is if some code
> > kmalloc()s some structs containing refcount_t, and relies on
> > GFP_ZERO (kzalloc()) to initialize their data assuming that a
> > freshly initialized refcount_t contains 0.
>
> Even with everything properly wrapped up in atomic_ref_t, it's not going
> to prevent mis-initialization via kzalloc() and friends.

I agree that it's an issue, but it's not a new issue. We've had the
exact same thing with a lot of other core data structures.

And a ref-count of zero isn't valid _anyway_. When you allocate a
structure, a zero ref-count by definition is wrong. You need to set
the ref-count to the user that allocated it.

So I don't actually think the "implicit zero" is an issue in practice,
because it would be wrong in the first place. Code that relies on
kzmalloc() to initialize a refcount cannot work right.

(And by "cannot" I obviously mean "can, if you do wrong things" - it's
not like it's *impossible* to do an "atomic_inc_ref()" to change a 0
refcount to a 1, but it's both wrong *AND* actively stupid, since an
allocation does not need to set the refcount atomically).

Linus