Re: C aggregate passing (Rust kernel policy)

Steven Rostedt
2025-02-26T22:12:42Z

On Wed, 26 Feb 2025 16:56:19 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:

> r = *p;
> if (r > 1000)
> goto out;
> x = r;
>
> to:
>
> if (*p > 1000)
> goto out;
> x = *p;

And you could replace *p with any variable that is visible outside the
function. As that's where I have to remember to use READ_ONCE() all the
time. When I need to access a variable that may change, but the old value
may still be fine to use as long as it is consistent.

I take this is what you meant by following what the code does.

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

Is the code saying to read "global" once. But today the compiler may not do
that and we have to use READ_ONCE() to prevent it.

But if I used:

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

Then the code itself is saying it is fine to re-read global or not, and the
compiler is fine with converting that to:

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

I guess this is where you say "volatile" is too strong, as this isn't an
issue and is an optimization the compiler can do. Where as the former
(reading global twice) is a bug because the code did not explicitly state
to do that.

-- Steve