Re: C aggregate passing (Rust kernel policy)

From: Steven Rostedt
Date: Wed Feb 26 2025 - 18:27:38 EST


On Wed, 26 Feb 2025 15:18:48 -0800
Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:

> On Wed, 26 Feb 2025 at 14:34, Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
> >
> > Correct, but if the variable had some other protection, like a lock held
> > when this function was called, it is fine to do and the compiler may
> > optimize it or not and still have the same result.
>
> Sure.
>
> But locking isn't always there. And shouldn't always be there. Lots of
> lockless algorithms exist, and some of them are very simple indeed ("I
> set a flag, you read a flag, you get one or the other value")

Yes, for the case of:

r = READ_ONCE(global);
if (r > 1000)
goto out;
x = r;

As I've done that in my code without locks, as I just need a consistent
value not necessarily the "current" value.

I was talking for the case the code has (not the compiler creating):

if (global > 1000)
goto out;
x = global;

Because without a lock or some other protection, that's likely a bug.

My point is that the compiler is free to turn that into:

r = READ_ONCE(global);
if (r > 1000)
goto out;
x = r;

and not change the expected result.

-- Steve