Re: [PATCH] locking/atomic: atomic: Use arch_atomic_{read,set} in generic atomic ops

From: Boqun Feng
Date: Fri Jan 27 2023 - 17:10:39 EST


On Fri, Jan 27, 2023 at 03:34:33PM +0100, Peter Zijlstra wrote:
> > I also noticed that GCC has some builtin/extension to do such things,
> > __atomic_OP_fetch and __atomic_fetch_OP, but I do not know if this
> > can be used in the kernel.
>
> On a per-architecture basis only, the C/C++ memory model does not match
> the Linux Kernel memory model so using the compiler to generate the
> atomic ops is somewhat tricky and needs architecture audits.

Hijack this thread a little bit, but while we are at it, do you think it
makes sense that we have a config option that allows archs to
implement LKMM atomics via C11 (volatile) atomics? I know there are gaps
between two memory models, but the option is only for fallback/generic
implementation so we can put extra barriers/orderings to make things
guaranteed to work.

It'll be a code version of this document:

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0124r7.html

(although I realise there may be a few mistakes in that doc since I
wasn't familiar with C11 memory model when I wrote part of the doc, but
these can be fixed)

Another reason I ask is that since Rust is coming, we need to provide
our LKMM atomics in Rust so that C code and Rust code can talk via same
atomic variables, since both sides need to use the same memory model.
My choices are:

1. Using FFI to call Linux atomic APIs: not inline therefore not
efficient.

2. Implementing Rust LKMM atomics in asm: much more work although
I'm OK if we have to do it.

3. Implementing Rust LKMM atomics with standard atomics (i.e. C/C++
atomics):

* Requires Rust has "volatile" atomics, which is WIP but
looks promising

* Less efficient compared to choice #2 but more efficient
compared to choice #1

Ideally, choice #2 is the best option for all architectures, however, if
we have the generic implementation based on choice #3, for some archs it
may be good enough.

Thoughts?

[Cc LKMM and Rust people]

Regards,
Boqun