Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr()
From: Linus Torvalds
Date: Wed Oct 18 2023 - 17:13:02 EST
On Wed, 18 Oct 2023 at 13:52, Uros Bizjak <ubizjak@xxxxxxxxx> wrote:
>
> FYI: This is the reason RMW instructions in percpu.h are not (blindly)
> converted to C ops. They will remain in their (volatile or not) asm
> form because of the above reason, and due to the fact that they don't
> combine with anything.
Well, also because converting them to C would be HORRIBYL BUGGY.
They absolutely have to be a single instruction. We have architectures
that can't do rmw instructions, and then they have to do lots of extra
horrid crud (disable interrupts or whatever) to make a percpu 'add' be
a valid thing.
> Actually, RMW insns are better written in asm, while simple "mov"
> should be converted to (volatile or not) memory access.
No.
This remat issue has convinced me that the *only* thing that should be
converted to a memory access is the "stable" case (which in practice
is mainly just 'current').
Because if you make them 'volatile' memory instructions, then the
simple "mov" inline asm is simply better. It still allows CSE on the
asm (in the "raw" form).
And if you make them memory instructions _without_ the 'volatile', the
memory access is simply buggy until we have some 'nomaterialize'
model.
So the *only* situation where a memory access is better is that
'stable' case. In all other cases they are the same or strictly worse
than 'asm'.
Linus