Re: C aggregate passing (Rust kernel policy)
From: comex
Date: Wed Feb 26 2025 - 19:26:35 EST
> On Feb 26, 2025, at 3:16 PM, Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Wed, 26 Feb 2025 at 14:27, Kent Overstreet <kent.overstreet@xxxxxxxxx> wrote:
>>
>> This is another one that's entirely eliminated due to W^X references.
>
> Are you saying rust cannot have global flags?
Believe it or not, no, it cannot.
All global variables must be either immutable, atomic, or protected with some sort of lock.
You can bypass this with unsafe code (UnsafeCell), but then you need to ensure no concurrent mutations for yourself, or else you get UB.
For a simple flag, you would probably use an atomic type with relaxed loads/stores. So you get the same load/store instructions as non-atomic accesses, but zero optimizations. And uglier syntax.
Personally I wish Rust had a weaker atomic ordering that did allow some optimizations, along with more syntax sugar for atomics. But in practice it’s really not a big deal, since use of mutable globals is discouraged in the first place.