x86/cpu: Use SERIALIZE in sync_core()
Subsystems (5)
cover x86/cpu: Use SERIALIZE in sync_core()
A recent submission to LKML introduced a CPU feature flag for a new Intel architecture Serializing Instruction, SERIALIZE [1]. Unlike the existing Serializing Instructions, this new instruction does not have side effects such as clobbering registers or exiting to a hypervisor.
As stated in the Intel "extensions" (ISE) manual [2], this instruction will appear first in Sapphire Rapids and Alder Lake.
Andy Lutomirski suggested to use this instruction in sync_core() as it serves the very purpose of this function [3].
For completeness, I picked patch #3 from Cathy's series (and has become patch #1 here) [1]. Her series depends on such patch to build correctly. Maybe it can be merged independently while the discussion continues?
Thanks and BR,
Ricardo
[1]. https://lore.kernel.org/kvm/[email protected]/
[2]. https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf
[3]. https://lore.kernel.org/kvm/CALCETrWudiF8G8r57r5i4JefuP5biG1kHg==0O8YXb-bYS-0BA@mail.gmail.com/
Ricardo Neri (4):
x86/cpufeatures: Add enumeration for SERIALIZE instruction x86/cpu: Relocate sync_core() to sync_core.h x86/cpu: Refactor sync_core() for readability x86/cpu: Use SERIALIZE in sync_core() when available
arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/processor.h | 64 ---------------------- arch/x86/include/asm/special_insns.h | 4 ++ arch/x86/include/asm/sync_core.h | 80 ++++++++++++++++++++++++++++ arch/x86/kernel/alternative.c | 1 + arch/x86/kernel/cpu/mce/core.c | 1 + drivers/misc/sgi-gru/grufault.c | 1 + drivers/misc/sgi-gru/gruhandles.c | 1 + drivers/misc/sgi-gru/grukservices.c | 1 + 9 files changed, 90 insertions(+), 64 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/4 x86/cpufeatures: Add enumeration for SERIALIZE instruction
The Intel architecture defines a set of Serializing Instructions (a detailed definition can be found in Vol.3 Section 8.3 of the Intel "main" manual, SDM). However, these instructions do more than what is required, have side effects and/or may be rather invasive. Furthermore, some of these instructions are only available in kernel mode or may cause VMExits. Thus, software using these instructions only to serialize execution (as defined in the manual) must handle the undesired side effects.
As indicated in the name, SERIALIZE is a new Intel architecture Serializing Instruction. Crucially, it does not have any of the mentioned side effects. Also, it does not cause VMExit and can be used in user mode.
This new instruction is currently documented in the latest "extensions" manual (ISE). It will appear in the "main" manual in the future.
---
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -367,6 +367,7 @@#define X86_FEATURE_SRBDS_CTRL (18*32+ 9)/* "" SRBDS mitigation MSR available */ #define X86_FEATURE_MD_CLEAR (18*32+10)/* VERW clears CPU buffers */ #define X86_FEATURE_TSX_FORCE_ABORT (18*32+13)/* "" TSX_FORCE_ABORT */ +#define X86_FEATURE_SERIALIZE (18*32+14)/* SERIALIZE instruction */ #define X86_FEATURE_PCONFIG (18*32+18)/* Intel PCONFIG */ #define X86_FEATURE_ARCH_LBR (18*32+19)/* Intel ARCH LBR */ #define X86_FEATURE_SPEC_CTRL (18*32+26)/* "" Speculation Control (IBRS + IBPB) */
Signature · 1 line
2.17.1
PATCH 3/4 x86/cpu: Refactor sync_core() for readability
In reply to Ricardo NeriInstead of having #ifdef/#endif blocks inside sync_core() for X86_64 and X86_32, implement the new function iret_to_self() with two versions.
In this manner, avoid having to use even more more #ifdef/#endif blocks when adding support for SERIALIZE in sync_core().
---
--- arch/x86/include/asm/special_insns.h | 1 - arch/x86/include/asm/sync_core.h | 56 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 25 deletions(-)
arch/x86/include/asm/special_insns.h +0 -1
@@ -234,7 +234,6 @@ static inline void clwb(volatile void *__p)#define nop () asm volatile ("nop") -#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
arch/x86/include/asm/sync_core.h +32 -24
68 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 4/4 x86/cpu: Use SERIALIZE in sync_core() when available
In reply to Ricardo NeriThe SERIALIZE instruction gives software a way to force the processor to complete all modifications to flags, registers and memory from previous instructions and drain all buffered writes to memory before the next instruction is fetched and executed. Thus, it serves the purpose of sync_core(). Use it when available.
Use boot_cpu_has() and not static_cpu_has(); the most critical paths (returning to user mode and from interrupt and NMI) will not reach sync_core().
---
--- arch/x86/include/asm/special_insns.h | 5 +++++ arch/x86/include/asm/sync_core.h | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/include/asm/special_insns.h +5 -0
@@ -234,6 +234,11 @@ static inline void clwb(volatile void *__p)#define nop () asm volatile ("nop") +static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
arch/x86/include/asm/sync_core.h +9 -1
@@ -5,6 +5,7 @@#include <linux/preempt.h> #include <asm/processor.h> #include <asm/cpufeature.h> +#include <asm/special_insns.h> #ifdef CONFIG_X86_32 static inline void iret_to_self (void )
@@ -54,7 +55,8 @@ static inline void iret_to_self(void)static inline void sync_core (void ) {/* -* There are quite a few ways to do this. IRET-to-self is nice +* Hardware can do this for us if SERIALIZE is available. Otherwise, +* there are quite a few ways to do this. IRET-to-self is nice * because it works on every CPU, at any CPL (so it's compatible * with paravirtualization), and it never exits to a hypervisor. * The only down sides are that it's a bit slow (it seems to be
@@ -75,6 +77,12 @@ static inline void sync_core(void)* Like all of Linux's memory ordering operations, this is a * compiler barrier as well. */ + +if (boot_cpu_has (X86_FEATURE_SERIALIZE )) { +serialize (); +return ; + } +iret_to_self (); }
Signature · 1 line
2.17.1
PATCH 2/4 x86/cpu: Relocate sync_core() to sync_core.h
In reply to Ricardo NeriHaving sync_core() in processor.h is problematic since it is not possible to check for hardware capabilities via the *cpu_has() family of macros. The latter needs the definitions in processor.h.
It also looks more intuitive to relocate the function to sync_core.h.
This changeset does not make changes in functionality.
---
--- arch/x86/include/asm/processor.h | 64 ----------------------------- arch/x86/include/asm/sync_core.h | 64 +++++++++++++++++++++++++++++ arch/x86/kernel/alternative.c | 1 + arch/x86/kernel/cpu/mce/core.c | 1 + drivers/misc/sgi-gru/grufault.c | 1 + drivers/misc/sgi-gru/gruhandles.c | 1 + drivers/misc/sgi-gru/grukservices.c | 1 + 7 files changed, 69 insertions(+), 64 deletions(-)
arch/x86/include/asm/processor.h +0 -64
@@ -676,70 +676,6 @@ static inline unsigned int cpuid_edx(unsigned int op)return edx; } -/* -* This function forces the icache and prefetched instruction stream to -* catch up with reality in two very specific cases: -* -* a) Text was modified using one virtual address and is about to be executed -* from the same physical page at a different virtual address. -* -* b) Text was modified on a different CPU, may subsequently be -* executed on this CPU, and you want to make sure the new version -* gets executed. This generally means you're calling this in a IPI. -* -* If you're calling this for a different reason, you're probably doing -* it wrong. -*/ -static inline void sync_core (void ) -{ -/* -* There are quite a few ways to do this. IRET-to-self is nice -* because it works on every CPU, at any CPL (so it's compatible -* with paravirtualization), and it never exits to a hypervisor. -* The only down sides are that it's a bit slow (it seems to be -* a bit more than 2x slower than the fastest options) and that -* it unmasks NMIs. The "push %cs" is needed because, in -* paravirtual environments, __KERNEL_CS may not be a valid CS -* value when we do IRET directly. -* -* In case NMI unmasking or performance ever becomes a problem, -* the next best option appears to be MOV-to-CR2 and an -* unconditional jump. That sequence also works on all CPUs, -* but it will fault at CPL3 (i.e. Xen PV). -* -* CPUID is the conventional way, but it's nasty: it doesn't -* exist on some 486-like CPUs, and it usually exits to a -* hypervisor. -* -* Like all of Linux's memory ordering operations, this is a -* compiler barrier as well. -*/ -#ifdef CONFIG_X86_32 - asmvolatile ( -"pushfl\n\t" -"pushl %%cs\n\t" -"pushl $1f\n\t" -"iret\n\t" -"1:" - : ASM_CALL_CONSTRAINT : : "memory"); -#else -unsigned int tmp; - - asmvolatile ( -"mov %%ss, %0\n\t" -"pushq %q0\n\t" -"pushq %%rsp\n\t" -"addq $8, (%%rsp)\n\t" -"pushfq\n\t" -"mov %%cs, %0\n\t" -"pushq %q0\n\t" -"pushq $1f\n\t" -"iretq\n\t" -"1:" - : "=&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory"); -#endif -} -extern void select_idle_routine (const struct cpuinfo_x86 *c);extern void amd_e400_c1e_apic_setup (void );
arch/x86/include/asm/sync_core.h +64 -0
70 lines not on this page · raw message
arch/x86/kernel/alternative.c +1 -0
@@ -16,6 +16,7 @@#include <linux/kprobes.h> #include <linux/mmu_context.h> #include <linux/bsearch.h> +#include <linux/sync_core.h> #include <asm/text-patching.h> #include <asm/alternative.h> #include <asm/sections.h>
arch/x86/kernel/cpu/mce/core.c +1 -0
@@ -42,6 +42,7 @@#include <linux/export.h> #include <linux/jump_label.h> #include <linux/set_memory.h> +#include <linux/sync_core.h> #include <linux/task_work.h> #include <linux/hardirq.h>
drivers/misc/sgi-gru/grufault.c +1 -0
@@ -20,6 +20,7 @@#include <linux/io.h> #include <linux/uaccess.h> #include <linux/security.h> +#include <linux/sync_core.h> #include <linux/prefetch.h> #include "gru.h" #include "grutables.h"
drivers/misc/sgi-gru/gruhandles.c +1 -0
@@ -16,6 +16,7 @@#define GRU_OPERATION_TIMEOUT (((cycles_t) local_cpu_data->itc_freq)*10)#define CLKS2NSEC (c) ((c) *1000000000 / local_cpu_data->itc_freq)#else +#include <linux/sync_core.h> #include <asm/tsc.h> #define GRU_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)#define CLKS2NSEC (c) ((c) * 1000000 / tsc_khz)
drivers/misc/sgi-gru/grukservices.c +1 -0
@@ -16,6 +16,7 @@#include <linux/miscdevice.h> #include <linux/proc_fs.h> #include <linux/interrupt.h> +#include <linux/sync_core.h> #include <linux/uaccess.h> #include <linux/delay.h> #include <linux/export.h>
Signature · 1 line
2.17.1
[email protected]
reviewerRe: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to Ricardo NeriQuoted 74 lines · Ricardo Neri
The SERIALIZE instruction gives software a way to force the processor to
complete all modifications to flags, registers and memory from previous instructions and drain all buffered writes to memory before the next instruction is fetched and executed. Thus, it serves the purpose ofsync_core(). Use it when available.Use
boot_cpu_has()and notstatic_cpu_has(); the most critical paths (returning to user mode and from interrupt and NMI) will not reachsync_core().Cc Andy Lutomirski <[email protected]>Cc Cathy Zhang <[email protected]>Cc Dave Hansen <[email protected]>Cc Fenghua Yu <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Kyung Min Park <[email protected]>Cc Peter Zijlstra <[email protected]>Cc "Ravi V. Shankar" <[email protected]>Cc Sean Christopherson <[email protected]>Reviwed-by Tony Luck <[email protected]>Suggested-by Andy Lutomirski <[email protected]>Signed-off-by Ricardo Neri <[email protected]>---
---arch/x86/include/asm/special_insns.h| 5 +++++arch/x86/include/asm/sync_core.h| 10 +++++++++-2 files changed, 14 insertions(+), 1 deletion(-)arch/x86/include/asm/special_insns.h +5
-0@@ -234,6 +234,11 @@ static inline void clwb(volatile void *__p)#define nop () asm volatile ("nop") +static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */ arch/x86/include/asm/sync_core.h +3
-1@@ -5,6 +5,7 @@#include <linux/preempt.h> #include <asm/processor.h> #include <asm/cpufeature.h> +#include <asm/special_insns.h> #ifdef CONFIG_X86_32 static inline void iret_to_self (void )@@ -54,7 +55,8 @@ static inline void iret_to_self(void)static inline void sync_core (void ) {/* -* There are quite a few ways to do this. IRET-to-self is nice +* Hardware can do this for us if SERIALIZE is available. Otherwise, +* there are quite a few ways to do this. IRET-to-self is nice * because it works on every CPU, at any CPL (so it's compatible * with paravirtualization), and it never exits to a hypervisor. * The only down sides are that it's a bit slow (it seems to be @@ -75,6 +77,12 @@ static inline void sync_core(void)* Like all of Linux's memory ordering operations, this is a * compiler barrier as well.
arch/x86/include/asm/sync_core.h static inline void sync_core(void) · line 88
Quoted 10 lines
arch/x86/include/asm/sync_core.h +6
-0*/ + +if (boot_cpu_has (X86_FEATURE_SERIALIZE )) { +serialize (); +return ; + } +iret_to_self (); }
Any reason to not make sync_core() an inline with alternatives?
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte 0xf, 0x1, 0xe8; 2:"
_ASM_EXTABLE(1b,2b));
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
[email protected]
reviewerRe: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
Quoted 90 lines · On July 26, 2020 10:55:15 PM PDT, [email protected] wrote:
Quoted 3 lines · Ricardo Neri
The SERIALIZE instruction gives software a way to force the processor to
complete all modifications to flags, registers and memory fromprevious
Quoted 71 lines
instructions and drain all buffered writes to memory before the next instruction is fetched and executed. Thus, it serves the purpose of
sync_core(). Use it when available.Use
boot_cpu_has()and notstatic_cpu_has(); the most critical paths (returning to user mode and from interrupt and NMI) will not reachsync_core().Cc Andy Lutomirski <[email protected]>Cc Cathy Zhang <[email protected]>Cc Dave Hansen <[email protected]>Cc Fenghua Yu <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Kyung Min Park <[email protected]>Cc Peter Zijlstra <[email protected]>Cc "Ravi V. Shankar" <[email protected]>Cc Sean Christopherson <[email protected]>Reviwed-by Tony Luck <[email protected]>Suggested-by Andy Lutomirski <[email protected]>Signed-off-by Ricardo Neri <[email protected]>---
---arch/x86/include/asm/special_insns.h| 5 +++++arch/x86/include/asm/sync_core.h| 10 +++++++++-2 files changed, 14 insertions(+), 1 deletion(-)arch/x86/include/asm/special_insns.h +5
-0@@ -234,6 +234,11 @@ static inline void clwb(volatile void *__p)#define nop () asm volatile ("nop") +static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +} +#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */ arch/x86/include/asm/sync_core.h +3
-1@@ -5,6 +5,7 @@#include <linux/preempt.h> #include <asm/processor.h> #include <asm/cpufeature.h> +#include <asm/special_insns.h> #ifdef CONFIG_X86_32 static inline void iret_to_self (void )@@ -54,7 +55,8 @@ static inline void iret_to_self(void)static inline void sync_core (void ) {/* -* There are quite a few ways to do this. IRET-to-self is nice +* Hardware can do this for us if SERIALIZE is available. Otherwise, +* there are quite a few ways to do this. IRET-to-self is nice * because it works on every CPU, at any CPL (so it's compatible * with paravirtualization), and it never exits to a hypervisor. * The only down sides are that it's a bit slow (it seems to be @@ -75,6 +77,12 @@ static inline void sync_core(void)* Like all of Linux's memory ordering operations, this is a * compiler barrier as well. arch/x86/include/asm/sync_core.h static inline void sync_core(void) · line 88
Quoted 10 lines
arch/x86/include/asm/sync_core.h +6
-0*/ + +if (boot_cpu_has (X86_FEATURE_SERIALIZE )) { +serialize (); +return ; + } +iret_to_self (); }Any reason to not make
sync_core()an inline with alternatives?
Quoted 5 lines
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte
0xf,0x1,0xe8; 2:"_ASM_EXTABLE(1b,2b));
(and : : : "memory" of course.)
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to Ricardo Neriarch/x86/include/asm/special_insns.h static inline void clwb(volatile void *__p) · line 240
Quoted 4 lines · Ricardo Neri
arch/x86/include/asm/special_insns.h +4
-0+static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +}
Can we pretty please have a comment with the binutils version that has the mnomic? Such that when we increase the required binutils version we can grep for this.
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to Ricardo Neriarch/x86/include/asm/sync_core.h static inline void sync_core(void) · line 86
Quoted 11 lines · Ricardo Neri
arch/x86/include/asm/sync_core.h +6
-0@@ -75,6 +77,12 @@ static inline void sync_core(void)* Like all of Linux's memory ordering operations, this is a * compiler barrier as well. */ + +if (boot_cpu_has (X86_FEATURE_SERIALIZE )) { +serialize (); +return ; + } +iret_to_self ();
I was sorta expecting something like:
alternative(IRET_TO_SELF, SERIALIZE, X86_FEATURE_SERIALIZE);
But instead you used boot_cpu_has() which is a dynamic test.
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 5 lines · On Sun, Jul 26, 2020 at 10:55:15PM -0700, [email protected] wrote:
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte
0xf,0x1,0xe8; 2:"_ASM_EXTABLE(1b,2b));
Ha! cute, you take an #UD ?
We could optimize the #UD exception handler for this I suppose, but that makes it an even worse hack. The simple alternative() seems like a much simpler approach.
Quoted 22 lines · Ricardo Neri
A recent submission to LKML introduced a CPU feature flag for a new Intel architecture Serializing Instruction, SERIALIZE [1]. Unlike the existing Serializing Instructions, this new instruction does not have side effects such as clobbering registers or exiting to a hypervisor.
As stated in the Intel "extensions" (ISE) manual [2], this instruction will appear first in Sapphire Rapids and Alder Lake.
Andy Lutomirski suggested to use this instruction in
sync_core()as it serves the very purpose of this function [3].For completeness, I picked patch #3 from Cathy's series (and has become patch #1 here) [1]. Her series depends on such patch to build correctly. Maybe it can be merged independently while the discussion continues?
Thanks and BR,
Ricardo[1]. https://lore.kernel.org/kvm/[email protected]/
[2]. https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf
[3]. https://lore.kernel.org/kvm/CALCETrWudiF8G8r57r5i4JefuP5biG1kHg==0O8YXb-bYS-0BA@mail.gmail.com/
Quoted 5 lines
Ricardo Neri (4):
x86/cpufeatures: Add enumeration for SERIALIZE instruction x86/cpu: Relocatesync_core()tosync_core.hx86/cpu: Refactorsync_core()for readability x86/cpu: Use SERIALIZE insync_core()when available
I've picked up the first 3 preparatory patches into tip:x86/cpu, as they are valid improvements even without the 4th patch. The actual functionality in the 4th patch still needs work.
Thannks,
Ingo
[tip: x86/cpu] x86/cpu: Relocate sync_core() to sync_core.h
In reply to Ricardo NeriThe following commit has been merged into the x86/cpu branch of tip:
The following commit has been merged into the x86/cpu branch of tip:
Commit-ID: 9998a9832c4027e907353e5e05fde730cf624b77 Gitweb: https://git.kernel.org/tip/9998a9832c4027e907353e5e05fde730cf624b77 Author: Ricardo Neri <[email protected]> AuthorDate: Sun, 26 Jul 2020 21:31:30 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 27 Jul 2020 12:42:06 +02:00
x86/cpu: Relocate sync_core() to sync_core.h
Having sync_core() in processor.h is problematic since it is not possible to check for hardware capabilities via the *cpu_has() family of macros. The latter needs the definitions in processor.h.
It also looks more intuitive to relocate the function to sync_core.h.
This changeset does not make changes in functionality.
--- arch/x86/include/asm/processor.h | 64 +---------------------------- arch/x86/include/asm/sync_core.h | 64 ++++++++++++++++++++++++++++- arch/x86/kernel/alternative.c | 1 +- arch/x86/kernel/cpu/mce/core.c | 1 +- drivers/misc/sgi-gru/grufault.c | 1 +- drivers/misc/sgi-gru/gruhandles.c | 1 +- drivers/misc/sgi-gru/grukservices.c | 1 +- 7 files changed, 69 insertions(+), 64 deletions(-)
arch/x86/include/asm/processor.h +0 -64
@@ -678,70 +678,6 @@ static inline unsigned int cpuid_edx(unsigned int op)return edx; } -/* -* This function forces the icache and prefetched instruction stream to -* catch up with reality in two very specific cases: -* -* a) Text was modified using one virtual address and is about to be executed -* from the same physical page at a different virtual address. -* -* b) Text was modified on a different CPU, may subsequently be -* executed on this CPU, and you want to make sure the new version -* gets executed. This generally means you're calling this in a IPI. -* -* If you're calling this for a different reason, you're probably doing -* it wrong. -*/ -static inline void sync_core (void ) -{ -/* -* There are quite a few ways to do this. IRET-to-self is nice -* because it works on every CPU, at any CPL (so it's compatible -* with paravirtualization), and it never exits to a hypervisor. -* The only down sides are that it's a bit slow (it seems to be -* a bit more than 2x slower than the fastest options) and that -* it unmasks NMIs. The "push %cs" is needed because, in -* paravirtual environments, __KERNEL_CS may not be a valid CS -* value when we do IRET directly. -* -* In case NMI unmasking or performance ever becomes a problem, -* the next best option appears to be MOV-to-CR2 and an -* unconditional jump. That sequence also works on all CPUs, -* but it will fault at CPL3 (i.e. Xen PV). -* -* CPUID is the conventional way, but it's nasty: it doesn't -* exist on some 486-like CPUs, and it usually exits to a -* hypervisor. -* -* Like all of Linux's memory ordering operations, this is a -* compiler barrier as well. -*/ -#ifdef CONFIG_X86_32 - asmvolatile ( -"pushfl\n\t" -"pushl %%cs\n\t" -"pushl $1f\n\t" -"iret\n\t" -"1:" - : ASM_CALL_CONSTRAINT : : "memory"); -#else -unsigned int tmp; - - asmvolatile ( -"mov %%ss, %0\n\t" -"pushq %q0\n\t" -"pushq %%rsp\n\t" -"addq $8, (%%rsp)\n\t" -"pushfq\n\t" -"mov %%cs, %0\n\t" -"pushq %q0\n\t" -"pushq $1f\n\t" -"iretq\n\t" -"1:" - : "=&r" (tmp), ASM_CALL_CONSTRAINT : : "cc", "memory"); -#endif -} -extern void select_idle_routine (const struct cpuinfo_x86 *c);extern void amd_e400_c1e_apic_setup (void );
arch/x86/include/asm/sync_core.h +64 -0
70 lines not on this page · raw message
arch/x86/kernel/alternative.c +1 -0
@@ -15,6 +15,7 @@#include <linux/kprobes.h> #include <linux/mmu_context.h> #include <linux/bsearch.h> +#include <linux/sync_core.h> #include <asm/text-patching.h> #include <asm/alternative.h> #include <asm/sections.h>
arch/x86/kernel/cpu/mce/core.c +1 -0
@@ -42,6 +42,7 @@#include <linux/export.h> #include <linux/jump_label.h> #include <linux/set_memory.h> +#include <linux/sync_core.h> #include <linux/task_work.h> #include <linux/hardirq.h>
drivers/misc/sgi-gru/grufault.c +1 -0
@@ -20,6 +20,7 @@#include <linux/io.h> #include <linux/uaccess.h> #include <linux/security.h> +#include <linux/sync_core.h> #include <linux/prefetch.h> #include "gru.h" #include "grutables.h"
drivers/misc/sgi-gru/gruhandles.c +1 -0
@@ -16,6 +16,7 @@#define GRU_OPERATION_TIMEOUT (((cycles_t) local_cpu_data->itc_freq)*10)#define CLKS2NSEC (c) ((c) *1000000000 / local_cpu_data->itc_freq)#else +#include <linux/sync_core.h> #include <asm/tsc.h> #define GRU_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)#define CLKS2NSEC (c) ((c) * 1000000 / tsc_khz)
drivers/misc/sgi-gru/grukservices.c +1 -0
@@ -16,6 +16,7 @@#include <linux/miscdevice.h> #include <linux/proc_fs.h> #include <linux/interrupt.h> +#include <linux/sync_core.h> #include <linux/uaccess.h> #include <linux/delay.h> #include <linux/export.h>
[tip: x86/cpu] x86/cpufeatures: Add enumeration for SERIALIZE instruction
In reply to Ricardo NeriThe following commit has been merged into the x86/cpu branch of tip:
The following commit has been merged into the x86/cpu branch of tip:
Commit-ID: 85b23fbc7d88f8c6e3951721802d7845bc39663d Gitweb: https://git.kernel.org/tip/85b23fbc7d88f8c6e3951721802d7845bc39663d Author: Ricardo Neri <[email protected]> AuthorDate: Sun, 26 Jul 2020 21:31:29 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 27 Jul 2020 12:42:06 +02:00
x86/cpufeatures: Add enumeration for SERIALIZE instruction
The Intel architecture defines a set of Serializing Instructions (a detailed definition can be found in Vol.3 Section 8.3 of the Intel "main" manual, SDM). However, these instructions do more than what is required, have side effects and/or may be rather invasive. Furthermore, some of these instructions are only available in kernel mode or may cause VMExits. Thus, software using these instructions only to serialize execution (as defined in the manual) must handle the undesired side effects.
As indicated in the name, SERIALIZE is a new Intel architecture Serializing Instruction. Crucially, it does not have any of the mentioned side effects. Also, it does not cause VMExit and can be used in user mode.
This new instruction is currently documented in the latest "extensions" manual (ISE). It will appear in the "main" manual in the future.
--- arch/x86/include/asm/cpufeatures.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -365,6 +365,7 @@#define X86_FEATURE_SRBDS_CTRL (18*32+ 9)/* "" SRBDS mitigation MSR available */ #define X86_FEATURE_MD_CLEAR (18*32+10)/* VERW clears CPU buffers */ #define X86_FEATURE_TSX_FORCE_ABORT (18*32+13)/* "" TSX_FORCE_ABORT */ +#define X86_FEATURE_SERIALIZE (18*32+14)/* SERIALIZE instruction */ #define X86_FEATURE_PCONFIG (18*32+18)/* Intel PCONFIG */ #define X86_FEATURE_SPEC_CTRL (18*32+26)/* "" Speculation Control (IBRS + IBPB) */ #define X86_FEATURE_INTEL_STIBP (18*32+27)/* "" Single Thread Indirect Branch Predictors */
[tip: x86/cpu] x86/cpu: Refactor sync_core() for readability
In reply to Ricardo NeriThe following commit has been merged into the x86/cpu branch of tip:
The following commit has been merged into the x86/cpu branch of tip:
Commit-ID: f69ca629d89d65737537e05308ac531f7bb07d5c Gitweb: https://git.kernel.org/tip/f69ca629d89d65737537e05308ac531f7bb07d5c Author: Ricardo Neri <[email protected]> AuthorDate: Sun, 26 Jul 2020 21:31:31 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Mon, 27 Jul 2020 12:42:06 +02:00
x86/cpu: Refactor sync_core() for readability
Instead of having #ifdef/#endif blocks inside sync_core() for X86_64 and X86_32, implement the new function iret_to_self() with two versions.
In this manner, avoid having to use even more more #ifdef/#endif blocks when adding support for SERIALIZE in sync_core().
--- arch/x86/include/asm/special_insns.h | 1 +- arch/x86/include/asm/sync_core.h | 56 +++++++++++++++------------ 2 files changed, 32 insertions(+), 25 deletions(-)
arch/x86/include/asm/special_insns.h +0 -1
@@ -234,7 +234,6 @@ static inline void clwb(volatile void *__p)#define nop () asm volatile ("nop") -#endif /* __KERNEL__ */ #endif /* _ASM_X86_SPECIAL_INSNS_H */
arch/x86/include/asm/sync_core.h +32 -24
68 lines not on this page · raw message
[email protected]
reviewerRe: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 9 lines · On July 27, 2020 1:20:03 AM PDT, [email protected] wrote:
arch/x86/include/asm/special_insns.h static inline void clwb(volatile void *__p) · line 240
Quoted 4 lines · Ricardo Neri
arch/x86/include/asm/special_insns.h +4
-0+static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +}Can we pretty please have a comment with the binutils version that has the mnomic? Such that when we increase the required binutils version we can grep for this.
It also needs : : : "memory" to be a barrier.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
[email protected]
reviewerRe: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 7 lines · On July 27, 2020 1:36:19 AM PDT, [email protected] wrote:
Quoted 5 lines · On Sun, Jul 26, 2020 at 10:55:15PM -0700, [email protected] wrote:
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte
0xf,0x1,0xe8; 2:"_ASM_EXTABLE(1b,2b));
Quoted 6 lines
Ha! cute, you take an #UD ?
We could optimize the #UD exception handler for this I suppose, but that
makes it an even worse hack. The simplealternative()seems like a much simpler approach.
If this is in any way performance critical, then no :) Taking the #UD has the cute property that we end up IRET on the way back, so we don't even need a fix-up path.
Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
Quoted 8 lines · On Mon, Jul 27, 2020 at 05:49:28AM -0700, [email protected] wrote:
Quoted 7 lines · On July 27, 2020 1:36:19 AM PDT, [email protected] wrote:
Quoted 5 lines · On Sun, Jul 26, 2020 at 10:55:15PM -0700, [email protected] wrote:
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte
0xf,0x1,0xe8; 2:"_ASM_EXTABLE(1b,2b));
Quoted 7 lines
Quoted 5 lines
Ha! cute, you take an #UD ?
We could optimize the #UD exception handler for this I suppose, but that makes it an even worse hack. The simple
alternative()seems like a much simpler approach.If this is in any way performance critical, then no :)
Yeah, I'm not sure.. the 'funny' thing is that typically call sync_core() from an IPI anyway. And the synchronous broadcast IPI is by far the most expensive part of that.
Something like this...
arch/x86/kernel/alternative.c +2 -1
@@ -989,12 +989,13 @@ void *text_poke_kgdb(void *addr, const void *opcode, size_t len)static void do_sync_core (void *info) { -sync_core (); +/* IRET implies sync_core() */ }void text_poke_sync (void ) {on_each_cpu (do_sync_core,NULL ,1 ); +sync_core (); }struct text_poke_loc {
Quoted 3 lines
Taking the #UD
has the cute property that we end up IRET on the way back, so we don't even need a fix-up path.
I got that, what I had in mind was making sure #UD avoids the overhead of doing exception entry/exit by adding an early exit.
Something like so:
arch/x86/kernel/traps.c +17 -2
@@ -96,6 +96,16 @@ __always_inline int is_valid_bugaddr(unsigned long addr)return *(unsigned short *)addr ==INSN_UD2 ; } +__always_inline int handle_serialize(struct pt_regs *regs) +{ +const char serialize[3 ] = {0x0f ,0xe8 ,0x02 }; + +if (regs->ip <TASK_SIZE_MAX ) +return 0 ; + +return !memcmp ((const void *)regs->ip, serialize,3 ); +} + static nokprobe_inline intdo_trap_no_signal (struct task_struct *tsk,int trapnr,const char *str,struct pt_regs *regs,long error_code)
@@ -252,8 +262,13 @@ DEFINE_IDTENTRY_RAW(exc_invalid_op)* handle it before exception entry to avoid recursive WARN * in case exception entry is the one triggering WARNs. */ -if (!user_mode (regs) &&handle_bug (regs)) -return ; +if (!user_mode (regs)) { +if (handle_bug (regs)) +return ; + +if (handle_serialize (regs)) +return ; + } state =idtentry_enter (regs);instrumentation_begin ();
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
Quoted 15 lines · On Mon, Jul 27, 2020 at 03:05:36PM +0200, [email protected] wrote:
Yeah, I'm not sure.. the 'funny' thing is that typically call
sync_core()from an IPI anyway. And the synchronous broadcast IPI is by far the most expensive part of that.Something like this...
arch/x86/kernel/alternative.c +0
-1@@ -989,12 +989,13 @@ void *text_poke_kgdb(void *addr, const void *opcode, size_t len)static void do_sync_core (void *info) { -sync_core ();
arch/x86/kernel/alternative.c void *text_poke_kgdb(void *addr, const void *opcode, size_t len) · line 1001
Quoted 10 lines
arch/x86/kernel/alternative.c +2
-0+/* IRET implies sync_core() */ }void text_poke_sync (void ) {on_each_cpu (do_sync_core,NULL ,1 ); +sync_core (); }struct text_poke_loc {
So 'people' have wanted to optimize this for NOHZ_FULL and I suppose virt as well.
IFF VMENTER is serializing, I suppose we can simply do something like:
bool text_poke_cond(int cpu, void *info)
{
/*
* If we observe the vCPU is preempted, it will do VMENTER
* no point in sending an IPI to SERIALIZE.
*/
return !vcpu_is_preempted(cpu);
}void text_poke_sync(void)
{
smp_call_function_many_cond(cpu_possible_mask,
do_sync_core, NULL, 1, text_poke_cond);
sync_core();
}The 'same' for NOHZ_FULL, except we need to cmpxchg a value such that if the cmpxchg() succeeds we know the CPU is in userspace and will SERIALIZE on the next entry. Much like kvm_flush_tlb_others().
Anyway, that's all hand-wavey.. I'll let someone that cares about those things write actual patches :-)
Luck, Tony
maintainerRE: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 4 lines
For a really overenginered solution, but which might perform unnecessary poorly on existing hardware:
asm volatile("1: .byte
0xf,0x1,0xe8; 2:"_ASM_EXTABLE(1b,2b));
You win the prize for the smallest code. Might need (the already large) comment to double in size to explain the subtleties!
-Tony
Quoted 31 lines · Ingo Molnar
Quoted 22 lines · Ricardo Neri
A recent submission to LKML introduced a CPU feature flag for a new Intel architecture Serializing Instruction, SERIALIZE [1]. Unlike the existing Serializing Instructions, this new instruction does not have side effects such as clobbering registers or exiting to a hypervisor.
As stated in the Intel "extensions" (ISE) manual [2], this instruction will appear first in Sapphire Rapids and Alder Lake.
Andy Lutomirski suggested to use this instruction in
sync_core()as it serves the very purpose of this function [3].For completeness, I picked patch #3 from Cathy's series (and has become patch #1 here) [1]. Her series depends on such patch to build correctly. Maybe it can be merged independently while the discussion continues?
Thanks and BR,
Ricardo[1]. https://lore.kernel.org/kvm/[email protected]/
[2]. https://software.intel.com/sites/default/files/managed/c5/15/architecture-instruction-set-extensions-programming-reference.pdf
[3]. https://lore.kernel.org/kvm/CALCETrWudiF8G8r57r5i4JefuP5biG1kHg==0O8YXb-bYS-0BA@mail.gmail.com/Quoted 5 lines
Ricardo Neri (4):
x86/cpufeatures: Add enumeration for SERIALIZE instruction x86/cpu: Relocatesync_core()tosync_core.hx86/cpu: Refactorsync_core()for readability x86/cpu: Use SERIALIZE insync_core()when available
Quoted 3 lines
I've picked up the first 3 preparatory patches into tip:x86/cpu, as they are valid improvements even without the 4th patch. The actual functionality in the 4th patch still needs work.
Thank you Ingo! I'll work on the fourth patch.
BR,
Ricardo
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 9 lines · On Mon, Jul 27, 2020 at 10:20:03AM +0200, [email protected] wrote:
arch/x86/include/asm/special_insns.h static inline void clwb(volatile void *__p) · line 240
Quoted 4 lines · Ricardo Neri
arch/x86/include/asm/special_insns.h +4
-0+static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +}Can we pretty please have a comment with the binutils version that has the mnomic? Such that when we increase the required binutils version we can grep for this.
This is supported since binutils 2.35[1]. I'll add a comment with the clarification.
Thanks and BR,
Ricardo
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 11 lines · On Mon, Jul 27, 2020 at 05:47:32AM -0700, [email protected] wrote:
Quoted 9 lines · On July 27, 2020 1:20:03 AM PDT, [email protected] wrote:
arch/x86/include/asm/special_insns.h static inline void clwb(volatile void *__p) · line 240
Quoted 4 lines · Ricardo Neri
arch/x86/include/asm/special_insns.h +4
-0+static inline void serialize (void ) +{ + asmvolatile (".byte 0xf, 0x1, 0xe8" ); +}Can we pretty please have a comment with the binutils version that has the mnomic? Such that when we increase the required binutils version we can grep for this.
Quoted 1 line
It also needs : : : "memory" to be a barrier.
Sure Peter, I will make this change.
Thanks and BR,
Ricardo
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
In reply to [email protected]Quoted 48 lines · On Mon, Jul 27, 2020 at 03:30:20PM +0200, [email protected] wrote:
Quoted 15 lines · On Mon, Jul 27, 2020 at 03:05:36PM +0200, [email protected] wrote:
Yeah, I'm not sure.. the 'funny' thing is that typically call
sync_core()from an IPI anyway. And the synchronous broadcast IPI is by far the most expensive part of that.Something like this...
arch/x86/kernel/alternative.c +0
-1@@ -989,12 +989,13 @@ void *text_poke_kgdb(void *addr, const void *opcode, size_t len)static void do_sync_core (void *info) { -sync_core ();arch/x86/kernel/alternative.c void *text_poke_kgdb(void *addr, const void *opcode, size_t len) · line 1001
Quoted 10 lines
arch/x86/kernel/alternative.c +2
-0+/* IRET implies sync_core() */ }void text_poke_sync (void ) {on_each_cpu (do_sync_core,NULL ,1 ); +sync_core (); }struct text_poke_loc {So 'people' have wanted to optimize this for
NOHZ_FULLand I suppose virt as well.IFF VMENTER is serializing, I suppose we can simply do something like:
bool
text_poke_cond(int cpu, void *info){ /* * If we observe the vCPU is preempted, it will do VMENTER * no point in sending an IPI to SERIALIZE. */ return !vcpu_is_preempted(cpu); }void
text_poke_sync(void){ smp_call_function_many_cond(cpu_possible_mask, do_sync_core, NULL, 1, text_poke_cond); sync_core(); }
Quoted 7 lines
The 'same' for
NOHZ_FULL, except we need to cmpxchg a value such that if thecmpxchg()succeeds we know the CPU is in userspace and will SERIALIZE on the next entry. Much likekvm_flush_tlb_others().Anyway, that's all hand-wavey.. I'll let someone that cares about those things write actual patches :-)
I think I got a little lost here. If I understand correctly, there are two alternatives to implement support for serialize better:
a) alternative(IRET_TO_SELF, SERIALIZE, X86_FEATURE_SERIALIZE); or
b) asm volatile("1:.byte 0xf, 0x1, 0xe8;2:" _ASM_EXTABLE(1b:2b)
a) would be the traditional and simpler solution. b) would rely on causing an #UD and getting an IRET on existing hardware b) would need some more optimization work when handling the exception and a few reworks on the poke patching code.
Which option should I focus on? Which option would be more desirable/better?
Thanks and BR,
Ricardo
Re: [PATCH 4/4] x86/cpu: Use SERIALIZE in sync_core() when available
Quoted 1 line · Ricardo Neri
I think I got a little lost here.
Hehe, sorry. I got carried away, it's just that recently people expressed interest in 'fixing' some of the text_poke_sync() issues again.
Quoted 6 lines
If I understand correctly, there are
two alternatives to implement support for serialize better:a)
alternative(IRET_TO_SELF, SERIALIZE, X86_FEATURE_SERIALIZE); or
b) asm volatile("1:.byte0xf,0x1, 0xe8;2:"_ASM_EXTABLE(1b:2b)
Quoted 6 lines
a) would be the traditional and simpler solution. b) would rely on causing an #UD and getting an IRET on existing hardware b) would need some more optimization work when handling the exception and a few reworks on the poke patching code.
Which option should I focus on? Which option would be more desirable/better?
I'd say go with (a) for now. We can always go overboard later ;-)