percpu: Add a DEFINE_PER_CPU_2PAGE_ALIGNED
Subsystems (4)
PATCH 1/8 percpu: Add a DEFINE_PER_CPU_2PAGE_ALIGNED
From: Andi Kleen <[email protected]>
Needed in a followon patch which needs to naturally align a 8K stack. I just put it into the page aligned section. This may cause an extra 4K hole if we're unlucky.
--- include/linux/percpu-defs.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/percpu-defs.h +4 -0
@@ -163,6 +163,10 @@ DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \ __aligned(PAGE_SIZE) +#define DEFINE_PER_CPU_2PAGE_ALIGNED (type, name) \ + DEFINE_PER_CPU_SECTION(type, name, "..page_aligned") \ + __aligned(2*PAGE_SIZE) +/* * Declaration/definition used for per-CPU variables that must be read mostly. */
Signature · 1 line
1.9.3
PATCH 2/8 x86: Naturally align the debug IST stack
From: Andi Kleen <[email protected]>
The followon patch for RD*BASE requires the IST stacks to be naturally aligned because it uses and to access the bottom of the stack. This was true for everyone except for the debug ist stack. Make the debug ist stack naturally aligned too.
--- arch/x86/include/asm/page_64_types.h | 4 ++-- arch/x86/kernel/cpu/common.c | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-)
arch/x86/include/asm/page_64_types.h +2 -2
@@ -17,8 +17,8 @@#define STACKFAULT_STACK 1#define DOUBLEFAULT_STACK 2#define NMI_STACK 3 -#define DEBUG_STACK 4 -#define MCE_STACK 5 +#define MCE_STACK 4 +#define DEBUG_STACK 5/* Must be always last */ #define N_EXCEPTION_STACKS 5/* hw limit: 7 */ #define PUD_PAGE_SIZE (_AC(1, UL) << PUD_SHIFT)
arch/x86/kernel/cpu/common.c +7 -1
@@ -1162,8 +1162,12 @@ static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { [DEBUG_STACK -1 ] =DEBUG_STKSZ }; +/* The IST stacks must be naturally aligned */ + static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks - [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ]); + [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ]); +static DEFINE_PER_CPU_2PAGE_ALIGNED(char, debug_stack + [DEBUG_STKSZ]);/* May not be marked __init: used by software suspend */ void syscall_init (void )
@@ -1336,6 +1340,8 @@ void cpu_init(void)char *estacks =per_cpu (exception_stacks, cpu);for (v =0 ; v <N_EXCEPTION_STACKS ; v++) { +if (v ==DEBUG_STACK -1 ) + estacks =per_cpu (debug_stack, cpu); estacks += exception_stack_sizes[v]; oist->ist[v] = t->x86_tss.ist[v] = (unsigned long )estacks;
Signature · 1 line
1.9.3
PATCH 3/8 x86: Add intrinsics/macros for new rd/wr fs/gs base instructions
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Add C intrinsics and assembler macros for the new rd/wr fs/gs base instructions and for swapgs.
Very straight forward. Used in followon patch.
For assembler only a few standard registers used by entry_64.S are defined.
--- arch/x86/include/asm/fsgs.h | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 arch/x86/include/asm/fsgs.h
arch/x86/include/asm/fsgs.h new file +54 -0
@@ -0,0 +1,54 @@ +#ifndef _ASM_FSGS_H +#define _ASM_FSGS_H 1 + +#ifndef __ASSEMBLY__ + +static inline void swapgs (void ) +{ + asmvolatile ("swapgs" :::"memory" ); +} + +/* Must be protected by X86_FEATURE_FSGSBASE check. */ + +static inline unsigned long rdgsbase (void ) +{ +unsigned long gs; + asmvolatile (".byte 0xf3,0x48,0x0f,0xae,0xc8 # rdgsbaseq %%rax" + :"=a" (gs) + ::"memory" ); +return gs; +} + +static inline unsigned long rdfsbase (void ) +{ +unsigned long fs; + asmvolatile (".byte 0xf3,0x48,0x0f,0xae,0xc0 # rdfsbaseq %%rax" + :"=a" (fs) + ::"memory" ); +return fs; +} + +static inline void wrgsbase (unsigned long gs) +{ + asmvolatile (".byte 0xf3,0x48,0x0f,0xae,0xd8 # wrgsbaseq %%rax" + ::"a" (gs) + :"memory" ); +} + +static inline void wrfsbase (unsigned long fs) +{ + asmvolatile (".byte 0xf3,0x48,0x0f,0xae,0xd0 # wrfsbaseq %%rax" + ::"a" (fs) + :"memory" ); +} + +#else + +/* Handle old assemblers. */ +#define RDGSBASE_R15 .byte 0xf3,0x49,0x0f,0xae,0xcf +#define WRGSBASE_RDI .byte 0xf3,0x48,0x0f,0xae,0xdf +#define WRGSBASE_R15 .byte 0xf3,0x49,0x0f,0xae,0xdf + +#endif /* __ASSEMBLY__ */ + +#endif
Signature · 1 line
1.9.3
PATCH 4/8 x86: Add support for rd/wr fs/gs base
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Introduction:
IvyBridge added four new instructions to directly write the fs and gs 64bit base registers. Previously this had to be done with a system call to write to MSRs. The main use case is fast user space threading and switching the fs/gs registers quickly there. Another use case is having (relatively) cheap access to a new address register per thread.
The instructions are opt-in and have to be explicitely enabled by the OS.
For more details on how to use the instructions see Documentation/x86/fsgs.txt added in a followon patch.
Paranoid exception path changes:
===============================
The paranoid entry/exit code is used for any NMI like exception.
Previously Linux couldn't support the new instructions because the paranoid entry code relied on the gs base never being negative outside the kernel to decide when to use swaps. It would check the gs MSR value and assume it was already running in kernel if negative.
To get rid of this assumption we have to revamp the paranoid exception path to not rely on this. We can use the new instructions to get (relatively) quick access to the GS value, and use it directly.
This is also significantly faster than a MSR read, so will speed NMIs (critical for profiling)
The kernel gs for the paranoid path is now stored at the bottom of the IST stack (so that it can be derived from RSP). For this we need to know the size of the IST stack (4K or 8K), which is now passed in as a stack parameter to save_paranoid.
The original patch compared the gs with the kernel gs and assumed that if it was identical, swapgs was not needed (and no user space processing was needed). This was nice and simple and didn't need a lot of changes.
But this had the side effect that if a user process set its GS to the same as the kernel it may lose rescheduling checks (so a racing reschedule IPI would have been only acted upon the next non paranoid interrupt)
This version now switches to full save/restore of the GS. This requires quite some changes in the paranoid path. Unfortunately I didn't come up with a simpler scheme.
Previously we had a flag in EBX that indicated whether SWAPGS needs to be called later or not. In the new scheme this turns into a tristate, with a new "restore from R15" mode that is used when the FSGSBASE instructions are available. In this case the GS base is saved and restored.
The exit paths are all adjusted to handle this correctly.
There is one complication: to allow debuggers (especially from the int3 or debug vectors) access to the user GS we need to save it in the task struct. Normally the next context switch would overwrite it with the wrong value from kernel_gs, so we set new flag also in task_struct that prevents it. The flag is cleared on context switch.
[Even with the additional flag the new FS/GS context switch is vastly faster than the old MSR based version for bases >4GB]
To prevent recursive interrupts clobbering this state in the task_struct this is only done for interrupts coming directly from ring 3.
After a reschedule comes back we check if the flag is still set. If it wasn't set the GS is back in the (swapped) kernel gs so we revert to the SWAPGS mode, instead of restoring GS.
So the three possible states for the paranoid exit path are:
- Do nothing (pre FSGSBASE), if we interrupted the kernel as determined by the existing negative GS
- Do swapgs, if we interrupted user space with positive GS (pre FSGSBASE), or we saved gs, but it was overwritten later by a context switch (with FSGSBASE)
- Restore from R15 (with FSGSBASE), if the gs base was saved in R15, and not overwritten by a context switch.
Context switch changes:
======================
Then after these changes we need to also use the new instructions to save/restore fs and gs, so that the new values set by the users won't disappear. This is also significantly faster for the case when the 64bit base has to be switched (that is when GS is larger than 4GB), as we can replace the slow MSR write with a faster wr[fg]sbase execution.
The instructions do not context switch
the segment index, so the old invariant that fs or gs index have to be 0 for a different 64bit value to stick is still true. Previously it was enforced by arch_prctl, now the user program has to make sure it keeps the segment indexes zero. If it doesn't the changes may not stick.
[Yes, this implies that programs can find out when they got context switched. However they could already do that before by checking the timing.]
This is in term enables fast switching when there are enough threads that their TLS segment does not fit below 4GB, or alternatively programs that use fs as an additional base register will not get a sigificant context switch penalty.
It is all done in a single patch because there was no simple way to do it in pieces without having crash holes inbetween.
v2: Change to save/restore GS instead of using swapgs based on the value. Large scale changes.
v3: Fix wrong flag initialization in fallback path. Thanks 0day!
--- arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets_64.c | 4 + arch/x86/kernel/cpu/common.c | 6 ++ arch/x86/kernel/entry_64.S | 163 +++++++++++++++++++++++++++++++-------- arch/x86/kernel/process_64.c | 31 +++++++- 5 files changed, 170 insertions(+), 38 deletions(-)
arch/x86/include/asm/processor.h +4 -0
@@ -522,6 +522,10 @@ struct thread_struct {* a short time */ unsigned char fpu_counter; +/* +* Set to one when gs is already saved. +*/ +unsigned char gs_saved; };/*
arch/x86/kernel/asm-offsets_64.c +4 -0
@@ -85,6 +85,10 @@ int main(void)DEFINE (__NR_ia32_syscall_max,sizeof (syscalls_ia32) -1 );DEFINE (IA32_NR_syscalls,sizeof (syscalls_ia32)); +BLANK (); + +OFFSET (task_thread_gs, task_struct, thread.gs); +OFFSET (task_thread_gs_saved, task_struct, thread.gs_saved);return 0 ; }
arch/x86/kernel/cpu/common.c +6 -0
@@ -953,6 +953,9 @@ static void identify_cpu(struct cpuinfo_x86 *c)#ifdef CONFIG_NUMA numa_add_cpu (smp_processor_id ());#endif + +if (cpu_has (c,X86_FEATURE_FSGSBASE )) +set_in_cr4 (X86_CR4_FSGSBASE ); }#ifdef CONFIG_X86_64
@@ -1338,10 +1341,13 @@ void cpu_init(void)*/ if (!oist->ist[0 ]) {char *estacks =per_cpu (exception_stacks, cpu); +void *gs =per_cpu (irq_stack_union.gs_base, cpu);for (v =0 ; v <N_EXCEPTION_STACKS ; v++) {if (v ==DEBUG_STACK -1 ) estacks =per_cpu (debug_stack, cpu); +/* Store GS at bottom of stack for bootstrap access */ + *(void **)estacks = gs; estacks += exception_stack_sizes[v]; oist->ist[v] = t->x86_tss.ist[v] = (unsigned long )estacks;
arch/x86/kernel/entry_64.S +129 -34
268 lines not on this page · raw message
arch/x86/kernel/process_64.c +27 -4
@@ -49,6 +49,7 @@#include <asm/syscalls.h> #include <asm/debugreg.h> #include <asm/switch_to.h> +#include <asm/fsgs.h> asmlinkage extern void ret_from_fork(void);
@@ -311,6 +312,16 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)*/ savesegment (fs, fsindex);savesegment (gs, gsindex); +if (static_cpu_has (X86_FEATURE_FSGSBASE )) { + prev->fs =rdfsbase (); +/* Interrupts are disabled here. */ +if (!prev->gs_saved) { +swapgs (); + prev->gs =rdgsbase (); +swapgs (); + } + prev->gs_saved =0 ; + }load_TLS (next, cpu);
@@ -341,8 +352,12 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p) prev->fs =0 ; }/* when next process has a 64bit base use it */ -if (next->fs) -wrmsrl (MSR_FS_BASE , next->fs); +if (next->fs) { +if (static_cpu_has (X86_FEATURE_FSGSBASE )) +wrfsbase (next->fs); +else +wrmsrl (MSR_FS_BASE , next->fs); + } prev->fsindex = fsindex;if (unlikely (gsindex | next->gsindex | prev->gs)) {
@@ -350,8 +365,16 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)if (gsindex) prev->gs =0 ; } -if (next->gs) -wrmsrl (MSR_KERNEL_GS_BASE , next->gs); +if (next->gs) { +if (static_cpu_has (X86_FEATURE_FSGSBASE )) { +/* Interrupts are disabled here. */ +swapgs (); +wrgsbase (next->gs); +swapgs (); + }else { +wrmsrl (MSR_KERNEL_GS_BASE , next->gs); + } + } prev->gsindex = gsindex;switch_fpu_finish (next_p, fpu);
Signature · 1 line
1.9.3
PATCH 5/8 x86: Make old K8 swapgs workaround conditional
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Every gs selector/index reload always paid an extra MFENCE between the two SWAPGS. This was to work around an old bug in early K8 steppings. All other CPUs don't need the extra mfence. Patch the extra MFENCE only in for K8.
--- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/kernel/cpu/amd.c | 3 +++ arch/x86/kernel/entry_64.S | 10 +++++++++- 3 files changed, 13 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -102,6 +102,7 @@#define X86_FEATURE_APERFMPERF ( 3*32+28)/* APERFMPERF */ #define X86_FEATURE_EAGER_FPU ( 3*32+29)/* "eagerfpu" Non lazy FPU restore */ #define X86_FEATURE_NONSTOP_TSC_S3 ( 3*32+30)/* TSC doesn't stop in S3 state */ +#define X86_FEATURE_SWAPGS_MFENCE (3*32+31)/* SWAPGS may need MFENCE */ /* Intel-defined CPU features, CPUID level 0x00000001 (ecx), word 4 */ #define X86_FEATURE_XMM3 ( 4*32+ 0)/* "pni" SSE-3 */
arch/x86/kernel/cpu/amd.c +3 -0
@@ -544,6 +544,9 @@ static void init_amd_k8(struct cpuinfo_x86 *c)if ((level >=0x0f48 && level <0x0f50 ) || level >=0x0f58 )set_cpu_cap (c,X86_FEATURE_REP_GOOD ); +/* Early steppings needed a mfence on swapgs. */ +set_cpu_cap (c,X86_FEATURE_SWAPGS_MFENCE ); +/* * Some BIOSes incorrectly force this feature, but only K8 revision D * (model = 0x14) and later actually support it.
arch/x86/kernel/entry_64.S +9 -1
@@ -1216,13 +1216,21 @@ ENTRY(native_load_gs_index) SWAPGS gs_change: movl %edi,%gs -2: mfence /* workaround */ +2: ASM_NOP3 /* may be replaced with mfence */ SWAPGS popfq_cfi ret CFI_ENDPROC END(native_load_gs_index) + /* Early K8 systems needed an mfence after swapgs to workaround a bug */ + .section .altinstr_replacement,"ax" +3: mfence + .previous + .section .altinstructions,"a" + altinstruction_entry 2b,3b,X86_FEATURE_SWAPGS_MFENCE,3,3 + .previous + _ASM_EXTABLE(gs_change,bad_gs) .section .fixup,"ax" /* running with kernelgs */
Signature · 1 line
1.9.3
PATCH 6/8 x86: Enumerate kernel FSGS capability in AT_HWCAP2
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
The kernel needs to explicitely enable RD/WRFSBASE to handle context switch correctly. So the application needs to know if it can safely use these instruction. Just looking at the CPUID bit is not enough because it may be running in a kernel that does not enable the instructions.
One way for the application would be to just try and catch the SIGILL. But that is difficult to do in libraries which may not want to overwrite the signal handlers of the main application.
So we need to provide a way for the application to discover the kernel capability.
I used AT_HWCAP2 in the ELF aux vector which is already used by PPC for similar things. We define a new Linux defined bitmap returned in AT_HWCAP. Currently it has only one bit set, for kernel is FSGSBASE capable.
The application can then access it manually or using the getauxval() function in newer glibc.
--- arch/x86/include/asm/elf.h | 7 +++++++ arch/x86/include/uapi/asm/hwcap.h | 7 +++++++ arch/x86/kernel/cpu/common.c | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/uapi/asm/hwcap.h
arch/x86/include/asm/elf.h +7 -0
@@ -256,6 +256,13 @@ extern int force_personality32;#define ELF_HWCAP (boot_cpu_data.x86_capability[0]) +extern unsigned kernel_enabled_features; + +/* HWCAP2 supplies kernel enabled CPU feature, so that the application +can know that it can safely use them. The bits are defined in +uapi/asm/hwcap.h. */ +#define ELF_HWCAP2 kernel_enabled_features; +/* This yields a string that ld.so will use to load implementation specific libraries for optimization. This is more specific in intent than poking at uname or /proc/cpuinfo.
arch/x86/include/uapi/asm/hwcap.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef _ASM_HWCAP_H +#define _ASM_HWCAP_H 1 + +#define HWCAP2_FSGSBASE (1 << 0)/* Kernel enabled RD/WR FS/GS BASE */ +/* upto bit 31 free */ + +#endif
arch/x86/kernel/cpu/common.c +6 -1
@@ -33,6 +33,7 @@#include <asm/i387.h> #include <asm/fpu-internal.h> #include <asm/mtrr.h> +#include <asm/hwcap.h> #include <linux/numa.h> #include <asm/asm.h> #include <asm/cpu.h>
@@ -48,6 +49,8 @@#include "cpu.h" +unsigned kernel_enabled_features__read_mostly ; +/* all of these masks are initialized in setup_cpu_local_masks() */ cpumask_var_t cpu_initialized_mask;cpumask_var_t cpu_callout_mask;
@@ -954,8 +957,10 @@ static void identify_cpu(struct cpuinfo_x86 *c)numa_add_cpu (smp_processor_id ());#endif -if (cpu_has (c,X86_FEATURE_FSGSBASE )) +if (cpu_has (c,X86_FEATURE_FSGSBASE )) { + kernel_enabled_features |=HWCAP2_FSGSBASE ;set_in_cr4 (X86_CR4_FSGSBASE ); + } }#ifdef CONFIG_X86_64
Signature · 1 line
1.9.3
PATCH 7/8 x86: Add documentation for rd/wr fs/gs base
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
--- Documentation/x86/fsgs.txt | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Documentation/x86/fsgs.txt
Documentation/x86/fsgs.txt new file +73 -0
@@ -0,0 +1,73 @@ + +Using FS and GS prefixes on x86_64-linux + +The x86 architecture supports segment prefixes per instruction to add an +offset to an address. On 64bit x86, these are mostly nops, except for FS +and GS. + +This offers an efficient way to reference a global pointer. + +The compiler has to generate special code to use these base registers, +or they can be accessed with inline assembler. + + mov %gs:offset,%reg + mov %fs:offset,%reg + +FS is used to address the thread local segment (TLS), declared using +__thread. The compiler the automatically generates the correct prefixes and +relocations to access these values. + +FS is normally managed by the runtime code or the threading library. + +GS is freely available, but may need special (compiler or inline assembler) +code to use. + +Traditionally 64bit FS and GS could be set by the arch_prctl system call + + arch_prctl(ARCH_SET_GS, value) + arch_prctl(ARCH_SET_FS, value) + +[There was also an older method using modify_ldt(), inherited from 32bit, +but this is not discussed here.] + +However using a syscall is problematic for user space threading libraries +that want to context switch in user space. + +Newer Intel CPUs (Ivy Bridge and later) added new instructions to directly +access these registers quickly from user context + + RDFSBASE %reg read the FS base (or _readfsbase_u64) + RDGSBASE %reg read the GS base (or _readgsbase_u64) + + WRFSBASE %reg write the FS base (or _writefsbase_u64) + WRGSBASE %reg write the GS base (or _writegsbase_u64) + +The instructions are supported by the CPU when the "fsgsbase" string is shown in +/proc/cpuinfo (or directly retrieved through the CPUID instruction). +The instructions are only available to 64bit binaries. + +However the kernel needs to explicitly enable these instructions, as it +may otherwise not correctly context switch the state. Newer Linux +kernels enable this. When the kernel did not enable the instruction +they will fault. + +An FSGSBASE enabled kernel can be detected by checking the AT_HWCAP2 +bitmask in the aux vector. When the HWCAP2_FSGSBASE bit is set the +kernel supports RDFSGSBASE. + + #include <sys/auxv.h> + #include <elf.h> + + /* Will be eventually in asm/hwcap.h */ + #define HWCAP2_FSGSBASE (1 << 0) + + unsigned val = getauxval(AT_HWCAP2); + if (val & HWCAP2_FSGSBASE) { + asm("wrgsbase %0" :: "r" (ptr)); + } + +Another requirement is that the FS or GS selector has to be zero +(is normally true unless changed explicitly) + + +Andi Kleen
Signature · 1 line
1.9.3
Re: [PATCH 7/8] x86: Add documentation for rd/wr fs/gs base
Quoted 21 lines · Andi Kleen
From: Andi Kleen <[email protected]>
Signed-off-by Andi Kleen <[email protected]>--- Documentation/x86/fsgs.txt | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Documentation/x86/fsgs.txtDocumentation/x86/fsgs.txt new file +7
-0@@ -0,0 +1,73 @@ + +Using FS and GS prefixes on x86_64-linux + +The x86 architecture supports segment prefixes per instruction to add an +offset to an address. On 64bit x86, these are mostly nops, except for FS +and GS. +
Documentation/x86/fsgs.txt line 17
Quoted 10 lines
Documentation/x86/fsgs.txt new file +10
-0+This offers an efficient way to reference a global pointer. + +The compiler has to generate special code to use these base registers, +or they can be accessed with inline assembler. + + mov %gs:offset,%reg + mov %fs:offset,%reg + +FS is used to address the thread local segment (TLS), declared using +__thread. The compiler the automatically generates the correct prefixes and
s/the/then
Quoted 7 lines
Documentation/x86/fsgs.txt new file +7
-0+relocations to access these values. + +FS is normally managed by the runtime code or the threading library. + +GS is freely available, but may need special (compiler or inline assembler) +code to use. +
Documentation/x86/fsgs.txt line 34
Quoted 10 lines
Documentation/x86/fsgs.txt new file +10
-0+Traditionally 64bit FS and GS could be set by the arch_prctl system call + + arch_prctl(ARCH_SET_GS, value) + arch_prctl(ARCH_SET_FS, value) + +[There was also an older method using modify_ldt(), inherited from 32bit, +but this is not discussed here.] + +However using a syscall is problematic for user space threading libraries +that want to context switch in user space.
Could you explain that a bit more?
Quoted 8 lines
Documentation/x86/fsgs.txt new file +8
-0+ +Newer Intel CPUs (Ivy Bridge and later) added new instructions to directly +access these registers quickly from user context + + RDFSBASE %reg read the FS base (or _readfsbase_u64) + RDGSBASE %reg read the GS base (or _readgsbase_u64) + + WRFSBASE %reg write the FS base (or _writefsbase_u64)
Documentation/x86/fsgs.txt line 52
Quoted 10 lines
Documentation/x86/fsgs.txt new file +10
-0+ WRGSBASE %reg write the GS base (or _writegsbase_u64) + +The instructions are supported by the CPU when the "fsgsbase" string is shown in +/proc/cpuinfo (or directly retrieved through the CPUID instruction). +The instructions are only available to 64bit binaries. + +However the kernel needs to explicitly enable these instructions, as it +may otherwise not correctly context switch the state. Newer Linux +kernels enable this. When the kernel did not enable the instruction +they will fault.
#GP? or #UD?
Quoted 29 lines
Documentation/x86/fsgs.txt new file +21
-0+ +An FSGSBASE enabled kernel can be detected by checking the AT_HWCAP2 +bitmask in the aux vector. When the HWCAP2_FSGSBASE bit is set the +kernel supports RDFSGSBASE. + + #include <sys/auxv.h> + #include <elf.h> + + /* Will be eventually in asm/hwcap.h */ + #define HWCAP2_FSGSBASE (1 << 0) + + unsigned val = getauxval(AT_HWCAP2); + if (val & HWCAP2_FSGSBASE) { + asm("wrgsbase %0" :: "r" (ptr)); + } + +Another requirement is that the FS or GS selector has to be zero +(is normally true unless changed explicitly) + + +Andi KleenSignature · 7 lines
1.9.3 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
PATCH 8/8 x86: Use rd/wr fs/gs base in arch_prctl
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Convert arch_prctl to use the new instructions to change fs/gs if available, instead of using MSRs.
This is merely a small performance optimization, no new functionality.
With the new instructions the syscall is really obsolete, as everything can be set directly in ring 3. But the syscall is widely used by existing software, so we still support it.
The syscall still enforces that the addresses are not in kernel space, even though that is not needed more. This is mainly so that the programs written for new CPUs do not suddenly fail on old CPUs.
With the new instructions available it prefers to use them in the context switch, instead of using the old "use GDT segment rewrite" trick.
--- arch/x86/kernel/process_64.c | 45 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-)
arch/x86/kernel/process_64.c +37 -8
@@ -486,15 +486,23 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)int ret =0 ;int doit = task == current;int cpu; +int fast_seg =boot_cpu_has (X86_FEATURE_FSGSBASE );switch (code) {case ARCH_SET_GS : +/* +* With fast_seg we don't need that check anymore, +* but keep it so that programs do not suddenly +* start failing when run on older CPUs. +* If you really want to set a address in kernel space +* use WRGSBASE directly. +*/ if (addr >=TASK_SIZE_OF (task))return -EPERM ; cpu =get_cpu ();/* handle small bases via the GDT because that's faster to switch. */ -if (addr <=0xffffffff ) { +if (addr <=0xffffffff && !fast_seg) {set_32bit_tls (task,GS_TLS , addr);if (doit) {load_TLS (&task->thread, cpu);
@@ -506,8 +514,17 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr) task->thread.gsindex =0 ; task->thread.gs = addr;if (doit) { -load_gs_index (0 ); - ret =wrmsrl_safe (MSR_KERNEL_GS_BASE , addr); +if (fast_seg) { +local_irq_disable (); +swapgs (); +loadsegment (gs,0 ); +wrgsbase (addr); +swapgs (); +local_irq_enable (); + }else { +load_gs_index (0 ); + ret =wrmsrl_safe (MSR_KERNEL_GS_BASE , addr); + } } }put_cpu ();
@@ -520,7 +537,7 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr) cpu =get_cpu ();/* handle small bases via the GDT because that's faster to switch. */ -if (addr <=0xffffffff ) { +if (addr <=0xffffffff && !fast_seg) {set_32bit_tls (task,FS_TLS , addr);if (doit) {load_TLS (&task->thread, cpu);
@@ -535,7 +552,10 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)/* set the selector to 0 to not confuse __switch_to */ loadsegment (fs,0 ); - ret =wrmsrl_safe (MSR_FS_BASE , addr); +if (fast_seg) +wrfsbase (addr); +else + ret =wrmsrl_safe (MSR_FS_BASE , addr); } }put_cpu ();
@@ -544,6 +564,8 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)unsigned long base;if (task->thread.fsindex ==FS_TLS_SEL ) base =read_32bit_tls (task,FS_TLS ); +else if (doit && fast_seg) + base =rdfsbase ();else if (doit)rdmsrl (MSR_FS_BASE , base);else
@@ -558,9 +580,16 @@ long do_arch_prctl(struct task_struct *task, int code, unsigned long addr) base =read_32bit_tls (task,GS_TLS ); else if (doit) {savesegment (gs, gsindex); -if (gsindex) -rdmsrl (MSR_KERNEL_GS_BASE , base); -else +if (gsindex) { +if (fast_seg) { +local_irq_disable (); +swapgs (); + base =rdgsbase (); +swapgs (); +local_irq_enable (); + }else +rdmsrl (MSR_KERNEL_GS_BASE , base); + }else base = task->thread.gs; } else base = task->thread.gs;
Signature · 1 line
1.9.3
Quoted 8 lines · Andi Kleen
From: Andi Kleen <[email protected]>
Needed in a followon patch which needs to naturally align a 8K stack. I just put it into the page aligned section. This may cause an extra 4K hole if we're unlucky.
Signed-off-by Andi Kleen <[email protected]>
Looks fine to me. Please feel free to add
and route any way you see fit.
Thanks.
Signature · 1 line
tejun