[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <07acc636-75d9-4e4b-9e99-9a784d88e188@ralfj.de>
Date: Fri, 28 Feb 2025 08:44:58 +0100
From: Ralf Jung <post@...fj.de>
To: David Laight <david.laight.linux@...il.com>,
Steven Rostedt <rostedt@...dmis.org>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Martin Uecker <uecker@...raz.at>, "Paul E. McKenney" <paulmck@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Ventura Jack <venturajack85@...il.com>,
Kent Overstreet <kent.overstreet@...ux.dev>, Gary Guo <gary@...yguo.net>,
airlied@...il.com, boqun.feng@...il.com, ej@...i.de,
gregkh@...uxfoundation.org, hch@...radead.org, hpa@...or.com,
ksummit@...ts.linux.dev, linux-kernel@...r.kernel.org,
miguel.ojeda.sandonis@...il.com, rust-for-linux@...r.kernel.org
Subject: Re: C aggregate passing (Rust kernel policy)
Hi,
>> I guess you can sum this up to:
>>
>> The compiler should never assume it's safe to read a global more than the
>> code specifies, but if the code reads a global more than once, it's fine
>> to cache the multiple reads.
>>
>> Same for writes, but I find WRITE_ONCE() used less often than READ_ONCE().
>> And when I do use it, it is more to prevent write tearing as you mentioned.
>
> Except that (IIRC) it is actually valid for the compiler to write something
> entirely unrelated to a memory location before writing the expected value.
> (eg use it instead of stack for a register spill+reload.)
> Not gcc doesn't do that - but the standard lets it do it.
Whether the compiler is permitted to do that depends heavily on what exactly the
code looks like, so it's hard to discuss this in the abstract.
If inside some function, *all* writes to a given location are atomic (I think
that's what you call WRITE_ONCE?), then the compiler is *not* allowed to invent
any new writes to that memory. The compiler has to assume that there might be
concurrent reads from other threads, whose behavior could change from the extra
compiler-introduced writes. The spec (in C, C++, and Rust) already works like that.
OTOH, the moment you do a single non-atomic write (i.e., a regular "*ptr = val;"
or memcpy or so), that is a signal to the compiler that there cannot be any
concurrent accesses happening at the moment, and therefore it can (and likely
will) introduce extra writes to that memory.
Kind regards,
Ralf
Powered by blists - more mailing lists