Use %a asm operand modifier to obtain %rip-relative addressing

From: Uros Bizjak
Date: Mon Nov 20 2023 - 04:39:25 EST


On Thu, Oct 12, 2023 at 7:10 PM Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Thu, 12 Oct 2023 at 09:55, Uros Bizjak <ubizjak@xxxxxxxxx> wrote:
> >
> > An example:
>
> Oh, I'm convinced.
>
> 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]") \
>
> and it turns out that we have other places where I think we could use that '%a',
>
> For example, we have things like this:
>
> asm ("lea sme_cmdline_arg(%%rip), %0"
> : "=r" (cmdline_arg)
> : "p" (sme_cmdline_arg));
>
> and I think the only reason we do that ridiculous asm is that the code
> in question really does want that (%rip) encoding. It sounds like this
> could just do
>
> asm ("lea %a1, %0"
> : "=r" (cmdline_arg)
> : "p" (sme_cmdline_arg));
>
> instead. Once again, I claim ignorance of the operand modifiers as the
> reason for these kinds of things.

I have looked a bit in the above code. From the compiler PoV, the
above can be written as:

asm ("lea %1, %0"
: "=r" (cmdline_arg)
: "m" (sme_cmdline_arg));

and it will always result in %rip-relative asm address:

#APP
# 585 "arch/x86/mm/mem_encrypt_identity.c" 1
lea sme_cmdline_arg(%rip), %rsi
# 0 "" 2
#NO_APP

Uros.