2023-11-17 14:56:28

by tip-bot2 for Jacob Pan

[permalink] [raw]
Subject: [tip: x86/mm] x86/mm: Use %RIP-relative address in untagged_addr()

The following commit has been merged into the x86/mm branch of tip:

Commit-ID: bc5607d7777423b742f5b0f7a760d074154c613f
Gitweb: https://git.kernel.org/tip/bc5607d7777423b742f5b0f7a760d074154c613f
Author: Uros Bizjak <[email protected]>
AuthorDate: Thu, 16 Nov 2023 20:10:59 +01:00
Committer: Dave Hansen <[email protected]>
CommitterDate: Fri, 17 Nov 2023 06:27:29 -08:00

x86/mm: Use %RIP-relative address in untagged_addr()

%RIP-relative addresses are nowadays correctly handled in alternative
instructions, so remove misleading comment and improve assembly to
use %RIP-relative address.

Also, explicitly using %gs: prefix will segfault for non-SMP builds.
Use macros from percpu.h which will DTRT with segment prefix register
as far as SMP/non-SMP builds are concerned.

Signed-off-by: Uros Bizjak <[email protected]>
Signed-off-by: Dave Hansen <[email protected]>
Acked-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Kirill A. Shutemov <[email protected]>
Link: https://lore.kernel.org/all/20231116191127.3446476-1-ubizjak%40gmail.com
---
arch/x86/include/asm/uaccess_64.h | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h
index f2c02e4..01455c0 100644
--- a/arch/x86/include/asm/uaccess_64.h
+++ b/arch/x86/include/asm/uaccess_64.h
@@ -11,6 +11,7 @@
#include <asm/alternative.h>
#include <asm/cpufeatures.h>
#include <asm/page.h>
+#include <asm/percpu.h>

#ifdef CONFIG_ADDRESS_MASKING
/*
@@ -18,14 +19,10 @@
*/
static inline unsigned long __untagged_addr(unsigned long addr)
{
- /*
- * Refer tlbstate_untag_mask directly to avoid RIP-relative relocation
- * in alternative instructions. The relocation gets wrong when gets
- * copied to the target place.
- */
asm (ALTERNATIVE("",
- "and %%gs:tlbstate_untag_mask, %[addr]\n\t", X86_FEATURE_LAM)
- : [addr] "+r" (addr) : "m" (tlbstate_untag_mask));
+ "and " __percpu_arg([mask]) ", %[addr]", X86_FEATURE_LAM)
+ : [addr] "+r" (addr)
+ : [mask] "m" (__my_cpu_var(tlbstate_untag_mask)));

return addr;
}


2023-11-17 16:53:18

by Uros Bizjak

[permalink] [raw]
Subject: Re: [tip: x86/mm] x86/mm: Use %RIP-relative address in untagged_addr()

On Fri, Nov 17, 2023 at 3:56 PM tip-bot2 for Uros Bizjak
<[email protected]> wrote:
>
> The following commit has been merged into the x86/mm branch of tip:
>
> Commit-ID: bc5607d7777423b742f5b0f7a760d074154c613f
> Gitweb: https://git.kernel.org/tip/bc5607d7777423b742f5b0f7a760d074154c613f
> Author: Uros Bizjak <[email protected]>
> AuthorDate: Thu, 16 Nov 2023 20:10:59 +01:00
> Committer: Dave Hansen <[email protected]>
> CommitterDate: Fri, 17 Nov 2023 06:27:29 -08:00
>
> x86/mm: Use %RIP-relative address in untagged_addr()
>
> %RIP-relative addresses are nowadays correctly handled in alternative
> instructions, so remove misleading comment and improve assembly to
> use %RIP-relative address.
>
> Also, explicitly using %gs: prefix will segfault for non-SMP builds.
> Use macros from percpu.h which will DTRT with segment prefix register
> as far as SMP/non-SMP builds are concerned.
>
> Signed-off-by: Uros Bizjak <[email protected]>
> Signed-off-by: Dave Hansen <[email protected]>
> Acked-by: Peter Zijlstra (Intel) <[email protected]>
> Acked-by: Kirill A. Shutemov <[email protected]>
> Link: https://lore.kernel.org/all/20231116191127.3446476-1-ubizjak%40gmail.com

I would just like to point out that this change depends on x86/percpu
branch in tip. So, the x86/mm has to come after x86/percpu.

Uros.