Re: C aggregate passing (Rust kernel policy)

comex
2025-02-27T00:26:15Z

> On Feb 26, 2025, at 3:16 PM, Linus Torvalds <torvalds@linux-foundation.org> wrote:
>
> On Wed, 26 Feb 2025 at 14:27, Kent Overstreet <kent.overstreet@linux.dev> 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.