Re: [RFC] Bridging the gap between the Linux Kernel Memory Consistency Model (LKMM) and C11/C++11 atomics

From: Paul E. McKenney
Date: Tue Jul 04 2023 - 17:25:15 EST


On Tue, Jul 04, 2023 at 04:25:45PM -0400, Alan Stern wrote:
> On Tue, Jul 04, 2023 at 01:19:23PM -0400, Olivier Dion wrote:

[ . . . ]

> > I am puzzled by this. Initialization of a shared variable does not need
> > to be atomic until its publication. Could you expand on this?
>
> In the kernel, I believe it sometimes happens that an atomic variable
> may be published before it is initialized. (If that's wrong, Paul or
> Peter can correct me.) But since this doesn't apply to the situations
> you're concerned with, you can forget I mentioned it.

Both use cases exist.

A global atomic is implicitly published at compile time. If the desired
initial value is not known until multiple threads are running, then it
is necessary to be careful. Hence double-check locking and its various
replacements. (Clearly, if you can determine the initial value before
going multithreaded, life is simpler.)

And dynamically allocated or on-stack storage is the other case, where
there is a point in time when the storage is private even after multiple
threads are running.

Or am I missing the point?

Thanx, Paul