x86/fsgsbase: Some fixes
Subsystems (4)
v2 cover x86/fsgsbase: Some fixes
These apply to tip:x86/fsgsbase.
The first two are trivial selftest fixes. The third one fixes an actual regression. The forth one fixes a crash on boot on Xen PV.
Andy Lutomirski (4):
selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test selftests/x86/fsgsbase: Add a missing memory constraint x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase x86/fsgsbase: Fix Xen PV support
arch/x86/include/asm/fsgsbase.h | 2 + arch/x86/kernel/process_64.c | 24 +- arch/x86/kernel/ptrace.c | 43 ++- tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/fsgsbase.c | 6 +- .../testing/selftests/x86/fsgsbase_restore.c | 245 ++++++++++++++++++ 6 files changed, 298 insertions(+), 24 deletions(-) create mode 100644 tools/testing/selftests/x86/fsgsbase_restore.c
Signature · 1 line
2.25.4
PATCH v2 1/4 selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test
A comment was unclear. Fix it.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -498,7 +498,8 @@ static void test_ptrace_write_gsbase(void)* base would zero the selector. On newer kernels, * this behavior has changed -- poking the base * changes only the base and, if FSGSBASE is not -* available, this may not effect. +* available, this may have no effect once the tracee +* is resumed. */ if (gs ==0 )printf ("\tNote: this is expected behavior on older kernels.\n" );
Signature · 1 line
2.25.4
PATCH v2 4/4 x86/fsgsbase: Fix Xen PV support
In reply to Andy LutomirskiOn Xen PV, SWAPGS doesn't work. Teach __rdfsbase_inactive() and __wrgsbase_inactive() to use rdmsrl()/wrmsrl() on Xen PV. The Xen pvop code will understand this and issue the correct hypercalls.
--- arch/x86/kernel/process_64.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
arch/x86/kernel/process_64.c +14 -6
@@ -163,9 +163,13 @@ static noinstr unsigned long __rdgsbase_inactive(void)lockdep_assert_irqs_disabled (); -native_swapgs (); - gsbase =rdgsbase (); -native_swapgs (); +if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); + gsbase =rdgsbase (); +native_swapgs (); + }else { +rdmsrl (MSR_KERNEL_GS_BASE , gsbase); + }return gsbase; }
@@ -182,9 +186,13 @@ static noinstr void __wrgsbase_inactive(unsigned long gsbase) {lockdep_assert_irqs_disabled (); -native_swapgs (); -wrgsbase (gsbase); -native_swapgs (); +if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); +wrgsbase (gsbase); +native_swapgs (); + }else { +wrmsrl (MSR_KERNEL_GS_BASE , gsbase); + } }/*
Signature · 1 line
2.25.4
PATCH v2 2/4 selftests/x86/fsgsbase: Add a missing memory constraint
In reply to Andy LutomirskiThe manual call to set_thread_area() via int $0x80 was missing any indication that the descriptor was a pointer, causing gcc to occasionally generate wrong code. Add the missing constraint.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -285,7 +285,8 @@ static unsigned short load_gs(void)/* 32-bit set_thread_area */ long ret; asmvolatile ("int $0x80" - :"=a" (ret) :"a" (243 ),"b" (low_desc) + :"=a" (ret),"+m" (*low_desc) + :"a" (243 ),"b" (low_desc) :"r8" ,"r9" ,"r10" ,"r11" );memcpy (&desc, low_desc,sizeof (desc));munmap (low_desc,sizeof (desc));
Signature · 1 line
2.25.4
PATCH v2 3/4 x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
In reply to Andy LutomirskiDebuggers expect that doing PTRACE_GETREGS, then poking at a tracee and maybe letting it run for a while, then doing PTRACE_SETREGS will put the tracee back where it was. In the specific case of a 32-bit tracer and tracee, the PTRACE_GETREGS/SETREGS data structure doesn't have fs_base or gs_base fields, so FSBASE and GSBASE fields are never stored anywhere. Everything used to still work because nonzero FS or GS would result full reloads of the segment registers when the tracee resumes, and the bases associated with FS==0 or GS==0 are irrelevant to 32-bit code.
Adding FSGSBASE support broke this: when FSGSBASE is enabled, FSBASE and GSBASE are now restored independently of FS and GS for all tasks when context-switched in. This means that, if a 32-bit tracer restores a previous state using PTRACE_SETREGS but the tracee's pre-restore and post-restore bases don't match, then the tracee is resumed with the wrong base.
Fix it by explicitly loading the base when a 32-bit tracer pokes FS or GS on a 64-bit kernel.
Also add a test case.
--- arch/x86/include/asm/fsgsbase.h | 2 + arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/ptrace.c | 43 ++- tools/testing/selftests/x86/Makefile | 2 +- .../testing/selftests/x86/fsgsbase_restore.c | 245 ++++++++++++++++++ 5 files changed, 280 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/x86/fsgsbase_restore.c
arch/x86/include/asm/fsgsbase.h +2 -0
@@ -75,6 +75,8 @@ static inline void x86_fsbase_write_cpu(unsigned long fsbase)extern unsigned long x86_gsbase_read_cpu_inactive (void );extern void x86_gsbase_write_cpu_inactive (unsigned long gsbase); +extern unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector);#endif /* CONFIG_X86_64 */
arch/x86/kernel/process_64.c +2 -2
@@ -347,8 +347,8 @@ static __always_inline void x86_fsgsbase_load(struct thread_struct *prev, } } -static unsigned long x86_fsgsbase_read_task (struct task_struct *task, -unsigned short selector) +unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector) {unsigned short idx = selector >>3 ;unsigned long base;
arch/x86/kernel/ptrace.c +30 -13
61 lines not on this page · raw message
tools/testing/selftests/x86/Makefile +1 -1
@@ -13,7 +13,7 @@ CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie) TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \ check_initial_reg_state sigreturn iopl ioperm \ test_vdso test_vsyscall mov_ss_trap \ - syscall_arg_fault + syscall_arg_fault fsgsbase_restore TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/fsgsbase_restore.c new file +245 -0
245 lines not on this page · raw message
Signature · 1 line
2.25.4
Jürgen Groß
maintainerRe: [PATCH fsgsbase v2 4/4] x86/fsgsbase: Fix Xen PV support
In reply to Andy LutomirskiQuoted 42 lines · Andy Lutomirski
On Xen PV, SWAPGS doesn't work. Teach
__rdfsbase_inactive()and__wrgsbase_inactive()to use rdmsrl()/wrmsrl() on Xen PV. The Xen pvop code will understand this and issue the correct hypercalls.Cc Boris Ostrovsky <[email protected]>Cc Juergen Gross <[email protected]>Cc Stefano Stabellini <[email protected]>Signed-off-by Andy Lutomirski <[email protected]>--- arch/x86/kernel/process_64.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)arch/x86/kernel/process_64.c +7
-6@@ -163,9 +163,13 @@ static noinstr unsigned long __rdgsbase_inactive(void)lockdep_assert_irqs_disabled (); -native_swapgs (); - gsbase =rdgsbase (); -native_swapgs (); +if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); + gsbase =rdgsbase (); +native_swapgs (); + }else { +rdmsrl (MSR_KERNEL_GS_BASE , gsbase); + }return gsbase; }@@ -182,9 +186,13 @@ static noinstr void __wrgsbase_inactive(unsigned long gsbase) {lockdep_assert_irqs_disabled (); -native_swapgs (); -wrgsbase (gsbase); -native_swapgs ();
arch/x86/kernel/process_64.c static noinstr void __wrgsbase_inactive(unsigned long gsbase) · line 198
Quoted 10 lines
arch/x86/kernel/process_64.c +7
-0+if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); +wrgsbase (gsbase); +native_swapgs (); + }else { +wrmsrl (MSR_KERNEL_GS_BASE , gsbase); + } }/*
Another possibility would be to just do (I'm fine either way):
arch/x86/xen/enlighten_pv.c +2 -0
@@ -318,6 +318,8 @@ static void __init xen_init_capabilities(void)setup_clear_cpu_cap (X86_FEATURE_XSAVE );setup_clear_cpu_cap (X86_FEATURE_OSXSAVE ); } + +setup_clear_cpu_cap (X86_FEATURE_FSGSBASE ); }static void xen_set_debugreg (int reg,unsigned long val)
Juergen
Re: [PATCH fsgsbase v2 4/4] x86/fsgsbase: Fix Xen PV support
Quoted 58 lines · Jürgen Groß
Quoted 39 lines · Andy Lutomirski
On Xen PV, SWAPGS doesn't work. Teach
__rdfsbase_inactive()and__wrgsbase_inactive()to use rdmsrl()/wrmsrl() on Xen PV. The Xen pvop code will understand this and issue the correct hypercalls.Cc Boris Ostrovsky <[email protected]>Cc Juergen Gross <[email protected]>Cc Stefano Stabellini <[email protected]>Signed-off-by Andy Lutomirski <[email protected]>---
arch/x86/kernel/process_64.c| 20 ++++++++++++++------1 file changed, 14 insertions(+), 6 deletions(-)arch/x86/kernel/process_64.c +7
-3@@ -163,9 +163,13 @@ static noinstr unsigned long__rdgsbase_inactive (void ) lockdep_assert_irqs_disabled(); - native_swapgs(); - gsbase = rdgsbase(); - native_swapgs(); + if (!static_cpu_has(X86_FEATURE_XENPV)) { + native_swapgs(); + gsbase = rdgsbase(); + native_swapgs(); + } else { + rdmsrl(MSR_KERNEL_GS_BASE, gsbase); + } return gsbase; }@@ -182,9 +186,13 @@ static noinstr void __wrgsbase_inactive(unsigned long gsbase) { lockdep_assert_irqs_disabled(); - native_swapgs(); - wrgsbase(gsbase);arch/x86/kernel/process_64.c static noinstr void __wrgsbase_inactive(unsigned · line 198
Quoted 10 lines
arch/x86/kernel/process_64.c +7
-1- native_swapgs(); + if (!static_cpu_has(X86_FEATURE_XENPV)) { + native_swapgs(); + wrgsbase(gsbase); + native_swapgs(); + } else { + wrmsrl(MSR_KERNEL_GS_BASE, gsbase); + } } /*Another possibility would be to just do (I'm fine either way):
arch/x86/xen/enlighten_pv.c +0
-0
arch/x86/xen/enlighten_pv.c static void __init xen_init_capabilities(void) · line 322
Quoted 5 lines
arch/x86/xen/enlighten_pv.c +2
-0@@ -318,6 +318,8 @@ static void __init xen_init_capabilities(void) setup_clear_cpu_cap(X86_FEATURE_XSAVE); setup_clear_cpu_cap(X86_FEATURE_OSXSAVE); } + + setup_clear_cpu_cap(X86_FEATURE_FSGSBASE);
That will stop both userspace and Xen (side effect of the guest kernel's CR4 choice) from using the instructions.
Even when the kernel is using the paravirt fastpath, its still Xen actually taking the hit. MSR_{FS,GS}_BASE/SHADOW are thousands of cycles to access, whereas {RD,WR}{FS,GS}BASE are a handful.
~Andrew
[tip: x86/fsgsbase] x86/fsgsbase: Fix Xen PV support
In reply to Andy LutomirskiThe following commit has been merged into the x86/fsgsbase branch of tip:
The following commit has been merged into the x86/fsgsbase branch of tip:
Commit-ID: d029bff60aa6c7eab281d52602b6a7a971615324 Gitweb: https://git.kernel.org/tip/d029bff60aa6c7eab281d52602b6a7a971615324 Author: Andy Lutomirski <[email protected]> AuthorDate: Fri, 26 Jun 2020 10:24:30 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Jul 2020 15:27:20 +02:00
x86/fsgsbase: Fix Xen PV support
On Xen PV, SWAPGS doesn't work. Teach __rdfsbase_inactive() and __wrgsbase_inactive() to use rdmsrl()/wrmsrl() on Xen PV. The Xen pvop code will understand this and issue the correct hypercalls.
--- arch/x86/kernel/process_64.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
arch/x86/kernel/process_64.c +18 -6
@@ -163,9 +163,15 @@ static noinstr unsigned long __rdgsbase_inactive(void)lockdep_assert_irqs_disabled (); -native_swapgs (); - gsbase =rdgsbase (); -native_swapgs (); +if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); + gsbase =rdgsbase (); +native_swapgs (); + }else { +instrumentation_begin (); +rdmsrl (MSR_KERNEL_GS_BASE , gsbase); +instrumentation_end (); + }return gsbase; }
@@ -182,9 +188,15 @@ static noinstr void __wrgsbase_inactive(unsigned long gsbase) {lockdep_assert_irqs_disabled (); -native_swapgs (); -wrgsbase (gsbase); -native_swapgs (); +if (!static_cpu_has (X86_FEATURE_XENPV )) { +native_swapgs (); +wrgsbase (gsbase); +native_swapgs (); + }else { +instrumentation_begin (); +wrmsrl (MSR_KERNEL_GS_BASE , gsbase); +instrumentation_end (); + } }/*
[tip: x86/fsgsbase] selftests/x86/fsgsbase: Add a missing memory constraint
In reply to Andy LutomirskiThe following commit has been merged into the x86/fsgsbase branch of tip:
The following commit has been merged into the x86/fsgsbase branch of tip:
Commit-ID: 8e259031c67a5ea0666428edb64c89e8c6ebd18e Gitweb: https://git.kernel.org/tip/8e259031c67a5ea0666428edb64c89e8c6ebd18e Author: Andy Lutomirski <[email protected]> AuthorDate: Fri, 26 Jun 2020 10:24:28 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Jul 2020 15:27:20 +02:00
selftests/x86/fsgsbase: Add a missing memory constraint
The manual call to set_thread_area() via int $0x80 was missing any indication that the descriptor was a pointer, causing gcc to occasionally generate wrong code. Add the missing constraint.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -285,7 +285,8 @@ static unsigned short load_gs(void)/* 32-bit set_thread_area */ long ret; asmvolatile ("int $0x80" - :"=a" (ret) :"a" (243 ),"b" (low_desc) + :"=a" (ret),"+m" (*low_desc) + :"a" (243 ),"b" (low_desc) :"r8" ,"r9" ,"r10" ,"r11" );memcpy (&desc, low_desc,sizeof (desc));munmap (low_desc,sizeof (desc));
[tip: x86/fsgsbase] selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test
In reply to Andy LutomirskiThe following commit has been merged into the x86/fsgsbase branch of tip:
The following commit has been merged into the x86/fsgsbase branch of tip:
Commit-ID: 979c2c4247cafd8a91628a7306b6871efbd12fdb Gitweb: https://git.kernel.org/tip/979c2c4247cafd8a91628a7306b6871efbd12fdb Author: Andy Lutomirski <[email protected]> AuthorDate: Fri, 26 Jun 2020 10:24:27 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Jul 2020 15:27:20 +02:00
selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test
A comment was unclear. Fix it.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -498,7 +498,8 @@ static void test_ptrace_write_gsbase(void)* base would zero the selector. On newer kernels, * this behavior has changed -- poking the base * changes only the base and, if FSGSBASE is not -* available, this may not effect. +* available, this may have no effect once the tracee +* is resumed. */ if (gs ==0 )printf ("\tNote: this is expected behavior on older kernels.\n" );
[tip: x86/fsgsbase] x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
In reply to Andy LutomirskiThe following commit has been merged into the x86/fsgsbase branch of tip:
The following commit has been merged into the x86/fsgsbase branch of tip:
Commit-ID: 40c45904f818c1f6555294ca27afc5fda4f09e68 Gitweb: https://git.kernel.org/tip/40c45904f818c1f6555294ca27afc5fda4f09e68 Author: Andy Lutomirski <[email protected]> AuthorDate: Fri, 26 Jun 2020 10:24:29 -07:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Wed, 01 Jul 2020 15:27:20 +02:00
x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
Debuggers expect that doing PTRACE_GETREGS, then poking at a tracee and maybe letting it run for a while, then doing PTRACE_SETREGS will put the tracee back where it was. In the specific case of a 32-bit tracer and tracee, the PTRACE_GETREGS/SETREGS data structure doesn't have fs_base or gs_base fields, so FSBASE and GSBASE fields are never stored anywhere. Everything used to still work because nonzero FS or GS would result full reloads of the segment registers when the tracee resumes, and the bases associated with FS==0 or GS==0 are irrelevant to 32-bit code.
Adding FSGSBASE support broke this: when FSGSBASE is enabled, FSBASE and GSBASE are now restored independently of FS and GS for all tasks when context-switched in. This means that, if a 32-bit tracer restores a previous state using PTRACE_SETREGS but the tracee's pre-restore and post-restore bases don't match, then the tracee is resumed with the wrong base.
Fix it by explicitly loading the base when a 32-bit tracer pokes FS or GS on a 64-bit kernel.
Also add a test case.
--- arch/x86/include/asm/fsgsbase.h | 2 +- arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/ptrace.c | 43 ++- tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/fsgsbase_restore.c | 245 ++++++++++++++++- 5 files changed, 280 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/x86/fsgsbase_restore.c
arch/x86/include/asm/fsgsbase.h +2 -0
@@ -75,6 +75,8 @@ static inline void x86_fsbase_write_cpu(unsigned long fsbase)extern unsigned long x86_gsbase_read_cpu_inactive (void );extern void x86_gsbase_write_cpu_inactive (unsigned long gsbase); +extern unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector);#endif /* CONFIG_X86_64 */
arch/x86/kernel/process_64.c +2 -2
@@ -347,8 +347,8 @@ static __always_inline void x86_fsgsbase_load(struct thread_struct *prev, } } -static unsigned long x86_fsgsbase_read_task (struct task_struct *task, -unsigned short selector) +unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector) {unsigned short idx = selector >>3 ;unsigned long base;
arch/x86/kernel/ptrace.c +30 -13
61 lines not on this page · raw message
tools/testing/selftests/x86/Makefile +1 -1
@@ -13,7 +13,7 @@ CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie) TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \ check_initial_reg_state sigreturn iopl ioperm \ test_vdso test_vsyscall mov_ss_trap \ - syscall_arg_fault + syscall_arg_fault fsgsbase_restore TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/fsgsbase_restore.c new file +245 -0
245 lines not on this page · raw message