Re: [PATCH v2 -tip] x86/percpu: Use C for arch_raw_cpu_ptr()

From: Uros Bizjak
Date: Thu Oct 12 2023 - 14:02:02 EST


On Thu, Oct 12, 2023 at 7:47 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Thu, 12 Oct 2023 at 10:10, Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > The fix seems to be a simple one-liner, ie just
> >
> > - asm(__pcpu_op2_##size(op, __percpu_arg(P[var]), "%[val]") \
> > + asm(__pcpu_op2_##size(op, __percpu_arg(a[var]), "%[val]") \
>
> Nope. That doesn't work at all.
>
> It turns out that we're not the only ones that didn't know about the
> 'a' modifier.
>
> clang has also never heard of it in this context, and the above
> one-liner results in an endless sea of errors, with
>
> error: invalid operand in inline asm: 'movq %gs:${1:a}, $0'
>
> Looking around, I think it's X86AsmPrinter::PrintAsmOperand() that is
> supposed to handle these things, and while it does have some handling
> for 'a', the comment around it says
>
> case 'a': // This is an address. Currently only 'i' and 'r' are expected.
>
> and I think our use ends up just confusing the heck out of clang. Of
> course, clang also does this:
>
> case 'P': // This is the operand of a call, treat specially.
> PrintPCRelImm(MI, OpNo, O);
> return false;
>
> so clang *already* generates those 'current' accesses as PCrelative, and I see
>
> movq %gs:pcpu_hot(%rip), %r13
>
> in the generated code.
>
> End result: clang actually generates what we want just using 'P', and
> the whole "P vs a" is only a gcc thing.

Ugh, this isn't exactly following Clang's claim that "In general,
Clang is highly compatible with the GCC inline assembly extensions,
allowing the same set of constraints, modifiers and operands as GCC
inline assembly."

[1] https://clang.llvm.org/compatibility.html#inline-asm

> Why *does* gcc do that silly thing of dropping '(%rip)' from the address, btw?

The documentation says:

[p] Print raw symbol name (without syntax-specific prefixes).

[P] If used for a function, print the PLT suf-fix and generate PIC
code. For example, emit foo@PLT instead of ’foo’ for the function
foo(). If used for a constant, drop all syntax-specific prefixes and
issue the bare constant. See p above.

I'd say that "bare constant" is something without (%pic).

Uros.