Skip to content

Commit 4bbbc9c

Browse files
jgross1gregkh
authored andcommitted
x86/pv: Switch SWAPGS to ALTERNATIVE
[ Upstream commit 53c9d92 ] SWAPGS is used only for interrupts coming from user mode or for returning to user mode. So there is no reason to use the PARAVIRT framework, as it can easily be replaced by an ALTERNATIVE depending on X86_FEATURE_XENPV. There are several instances using the PV-aware SWAPGS macro in paths which are never executed in a Xen PV guest. Replace those with the plain swapgs instruction. For SWAPGS_UNSAFE_STACK the same applies. Signed-off-by: Juergen Gross <jgross@suse.com> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Borislav Petkov <bp@suse.de> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Andy Lutomirski <luto@kernel.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20210120135555.32594-5-jgross@suse.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 4d42b7b commit 4bbbc9c

8 files changed

Lines changed: 13 additions & 47 deletions

File tree

‎arch/x86/entry/entry_64.S‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ native_irq_return_ldt:
673673
*/
674674

675675
pushq %rdi /* Stash user RDI */
676-
SWAPGS /* to kernel GS */
676+
swapgs /* to kernel GS */
677677
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi /* to kernel CR3 */
678678

679679
movq PER_CPU_VAR(espfix_waddr), %rdi
@@ -703,7 +703,7 @@ native_irq_return_ldt:
703703
orq PER_CPU_VAR(espfix_stack), %rax
704704

705705
SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
706-
SWAPGS /* to user GS */
706+
swapgs /* to user GS */
707707
popq %rdi /* Restore user RDI */
708708

709709
movq %rax, %rsp
@@ -947,7 +947,7 @@ SYM_CODE_START_LOCAL(paranoid_entry)
947947
ret
948948

949949
.Lparanoid_entry_swapgs:
950-
SWAPGS
950+
swapgs
951951

952952
/*
953953
* The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
@@ -1005,7 +1005,7 @@ SYM_CODE_START_LOCAL(paranoid_exit)
10051005
jnz restore_regs_and_return_to_kernel
10061006

10071007
/* We are returning to a context with user GSBASE */
1008-
SWAPGS_UNSAFE_STACK
1008+
swapgs
10091009
jmp restore_regs_and_return_to_kernel
10101010
SYM_CODE_END(paranoid_exit)
10111011

@@ -1431,7 +1431,7 @@ nmi_no_fsgsbase:
14311431
jnz nmi_restore
14321432

14331433
nmi_swapgs:
1434-
SWAPGS_UNSAFE_STACK
1434+
swapgs
14351435

14361436
nmi_restore:
14371437
POP_REGS

‎arch/x86/include/asm/irqflags.h‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,6 @@ static __always_inline unsigned long arch_local_irq_save(void)
131131
#define SAVE_FLAGS(x) pushfq; popq %rax
132132
#endif
133133

134-
#define SWAPGS swapgs
135-
/*
136-
* Currently paravirt can't handle swapgs nicely when we
137-
* don't have a stack we can rely on (such as a user space
138-
* stack). So we either find a way around these or just fault
139-
* and emulate if a guest tries to call swapgs directly.
140-
*
141-
* Either way, this is a good way to document that we don't
142-
* have a reliable stack. x86_64 only.
143-
*/
144-
#define SWAPGS_UNSAFE_STACK swapgs
145-
146134
#define INTERRUPT_RETURN jmp native_iret
147135
#define USERGS_SYSRET64 \
148136
swapgs; \
@@ -170,6 +158,14 @@ static __always_inline int arch_irqs_disabled(void)
170158

171159
return arch_irqs_disabled_flags(flags);
172160
}
161+
#else
162+
#ifdef CONFIG_X86_64
163+
#ifdef CONFIG_XEN_PV
164+
#define SWAPGS ALTERNATIVE "swapgs", "", X86_FEATURE_XENPV
165+
#else
166+
#define SWAPGS swapgs
167+
#endif
168+
#endif
173169
#endif /* !__ASSEMBLY__ */
174170

175171
#endif

‎arch/x86/include/asm/paravirt.h‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -776,26 +776,6 @@ extern void default_banner(void);
776776

777777
#ifdef CONFIG_X86_64
778778
#ifdef CONFIG_PARAVIRT_XXL
779-
/*
780-
* If swapgs is used while the userspace stack is still current,
781-
* there's no way to call a pvop. The PV replacement *must* be
782-
* inlined, or the swapgs instruction must be trapped and emulated.
783-
*/
784-
#define SWAPGS_UNSAFE_STACK \
785-
PARA_SITE(PARA_PATCH(PV_CPU_swapgs), swapgs)
786-
787-
/*
788-
* Note: swapgs is very special, and in practise is either going to be
789-
* implemented with a single "swapgs" instruction or something very
790-
* special. Either way, we don't need to save any registers for
791-
* it.
792-
*/
793-
#define SWAPGS \
794-
PARA_SITE(PARA_PATCH(PV_CPU_swapgs), \
795-
ANNOTATE_RETPOLINE_SAFE; \
796-
call PARA_INDIRECT(pv_ops+PV_CPU_swapgs); \
797-
)
798-
799779
#define USERGS_SYSRET64 \
800780
PARA_SITE(PARA_PATCH(PV_CPU_usergs_sysret64), \
801781
ANNOTATE_RETPOLINE_SAFE; \

‎arch/x86/include/asm/paravirt_types.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ struct pv_cpu_ops {
169169
frame set up. */
170170
void (*iret)(void);
171171

172-
void (*swapgs)(void);
173-
174172
void (*start_context_switch)(struct task_struct *prev);
175173
void (*end_context_switch)(struct task_struct *next);
176174
#endif

‎arch/x86/kernel/asm-offsets_64.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ int main(void)
1515
#ifdef CONFIG_PARAVIRT_XXL
1616
OFFSET(PV_CPU_usergs_sysret64, paravirt_patch_template,
1717
cpu.usergs_sysret64);
18-
OFFSET(PV_CPU_swapgs, paravirt_patch_template, cpu.swapgs);
1918
#ifdef CONFIG_DEBUG_ENTRY
2019
OFFSET(PV_IRQ_save_fl, paravirt_patch_template, irq.save_fl);
2120
#endif

‎arch/x86/kernel/paravirt.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ struct paravirt_patch_template pv_ops = {
312312

313313
.cpu.usergs_sysret64 = native_usergs_sysret64,
314314
.cpu.iret = native_iret,
315-
.cpu.swapgs = native_swapgs,
316315

317316
#ifdef CONFIG_X86_IOPL_IOPERM
318317
.cpu.invalidate_io_bitmap = native_tss_invalidate_io_bitmap,

‎arch/x86/kernel/paravirt_patch.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct patch_xxl {
2828
const unsigned char irq_restore_fl[2];
2929
const unsigned char cpu_wbinvd[2];
3030
const unsigned char cpu_usergs_sysret64[6];
31-
const unsigned char cpu_swapgs[3];
3231
const unsigned char mov64[3];
3332
};
3433

@@ -43,7 +42,6 @@ static const struct patch_xxl patch_data_xxl = {
4342
.cpu_wbinvd = { 0x0f, 0x09 }, // wbinvd
4443
.cpu_usergs_sysret64 = { 0x0f, 0x01, 0xf8,
4544
0x48, 0x0f, 0x07 }, // swapgs; sysretq
46-
.cpu_swapgs = { 0x0f, 0x01, 0xf8 }, // swapgs
4745
.mov64 = { 0x48, 0x89, 0xf8 }, // mov %rdi, %rax
4846
};
4947

@@ -86,7 +84,6 @@ unsigned int native_patch(u8 type, void *insn_buff, unsigned long addr,
8684
PATCH_CASE(mmu, write_cr3, xxl, insn_buff, len);
8785

8886
PATCH_CASE(cpu, usergs_sysret64, xxl, insn_buff, len);
89-
PATCH_CASE(cpu, swapgs, xxl, insn_buff, len);
9087
PATCH_CASE(cpu, wbinvd, xxl, insn_buff, len);
9188
#endif
9289

‎arch/x86/xen/enlighten_pv.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,6 @@ static const struct pv_cpu_ops xen_cpu_ops __initconst = {
10831083
#endif
10841084
.io_delay = xen_io_delay,
10851085

1086-
/* Xen takes care of %gs when switching to usermode for us */
1087-
.swapgs = paravirt_nop,
1088-
10891086
.start_context_switch = paravirt_start_context_switch,
10901087
.end_context_switch = xen_end_context_switch,
10911088
};

0 commit comments

Comments
 (0)