All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
From: Peter Zijlstra <peterz@infradead.org>
To: David Woodhouse <dwmw2@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Josh Poimboeuf <jpoimboe@redhat.com>
Cc: linux-kernel@vger.kernel.org, Dave Hansen <dave.hansen@intel.com>,
	Ashok Raj <ashok.raj@intel.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Andy Lutomirski <luto@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Greg KH <gregkh@linuxfoundation.org>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Andi Kleen <ak@linux.intel.com>,
	Arjan Van De Ven <arjan.van.de.ven@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Jun Nakajima <jun.nakajima@intel.com>,
	Asit Mallick <asit.k.mallick@intel.com>,
	Jason Baron <jbaron@akamai.com>,
	Peter Zijlstra <peterz@infradead.org>,
	David Woodhouse <dwmw@amazon.co.uk>
Subject: [PATCH 27/35] x86/enter: Use IBRS on syscall and interrupts
Date: Thu, 18 Jan 2018 14:48:27 +0100	[thread overview]
Message-ID: <20180118140153.076225139@infradead.org> (raw)
In-Reply-To: 20180118134800.711245485@infradead.org

[-- Attachment #1: x86-enter--Use_IBRS_on_syscall_and_interrupts.patch --]
[-- Type: text/plain, Size: 7948 bytes --]

From: Tim Chen <tim.c.chen@linux.intel.com>

Stop Indirect Branch Speculation on every user space to kernel space
transition and reenable it when returning to user space.

The NMI interrupt save/restore of IBRS state was based on Andrea
Arcangeli's implementation.  Here's an explanation by Dave Hansen on why we
save IBRS state for NMI.

The normal interrupt code uses the 'error_entry' path which uses the
Code Segment (CS) of the instruction that was interrupted to tell
whether it interrupted the kernel or userspace and thus has to switch
IBRS, or leave it alone.

The NMI code is different.  It uses 'paranoid_entry' because it can
interrupt the kernel while it is running with a userspace IBRS (and %GS
and CR3) value, but has a kernel CS.  If we used the same approach as
the normal interrupt code, we might do the following;

	SYSENTER_entry
<-------------- NMI HERE
	IBRS=1
		do_something()
	IBRS=0
	SYSRET

The NMI code might notice that we are running in the kernel and decide
that it is OK to skip the IBRS=1.  This would leave it running
unprotected with IBRS=0, which is bad.

However, if we unconditionally set IBRS=1, in the NMI, we might get the
following case:

	SYSENTER_entry
	IBRS=1
		do_something()
	IBRS=0
<-------------- NMI HERE (set IBRS=1)
	SYSRET

and we would return to userspace with IBRS=1.  Userspace would run
slowly until we entered and exited the kernel again.

Instead of those two approaches, we chose a third one where we simply
save the IBRS value in a scratch register (%r13) and then restore that
value, verbatim.

Co-developed-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Greg KH <gregkh@linuxfoundation.org>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Arjan Van De Ven <arjan.van.de.ven@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Cc: Ashok Raj <ashok.raj@intel.com>
---
 arch/x86/entry/entry_64.S        |   35 ++++++++++++++++++++++++++++++++++-
 arch/x86/entry/entry_64_compat.S |   21 +++++++++++++++++++--
 2 files changed, 53 insertions(+), 3 deletions(-)

--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -171,6 +171,8 @@ ENTRY(entry_SYSCALL_64_trampoline)
 
 	/* Load the top of the task stack into RSP */
 	movq	CPU_ENTRY_AREA_tss + TSS_sp1 + CPU_ENTRY_AREA, %rsp
+	/* Stop indirect branch speculation */
+	STOP_IB_SPEC
 
 	/* Start building the simulated IRET frame. */
 	pushq	$__USER_DS			/* pt_regs->ss */
@@ -214,6 +216,8 @@ ENTRY(entry_SYSCALL_64)
 	 */
 	movq	%rsp, PER_CPU_VAR(rsp_scratch)
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC
 
 	TRACE_IRQS_OFF
 
@@ -409,6 +413,8 @@ GLOBAL(entry_SYSCALL_64_after_hwframe)
 	pushq	RSP-RDI(%rdi)	/* RSP */
 	pushq	(%rdi)		/* RDI */
 
+	/* Restart Indirect Branch Speculation */
+	RESTART_IB_SPEC
 	/*
 	 * We are on the trampoline stack.  All regs except RDI are live.
 	 * We can do future final exit work right here.
@@ -757,11 +763,12 @@ GLOBAL(swapgs_restore_regs_and_return_to
 	/* Push user RDI on the trampoline stack. */
 	pushq	(%rdi)
 
+	/* Restart Indirect Branch Speculation */
+	RESTART_IB_SPEC
 	/*
 	 * We are on the trampoline stack.  All regs except RDI are live.
 	 * We can do future final exit work right here.
 	 */
-
 	SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
 
 	/* Restore RDI. */
@@ -849,6 +856,13 @@ ENTRY(native_iret)
 	SWAPGS					/* to kernel GS */
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi	/* to kernel CR3 */
 
+	/*
+	 * There is no point in disabling Indirect Branch Speculation
+	 * here as this is going to return to user space immediately
+	 * after fixing ESPFIX stack.  There is no vulnerable code
+	 * to protect so spare two MSR writes.
+	 */
+
 	movq	PER_CPU_VAR(espfix_waddr), %rdi
 	movq	%rax, (0*8)(%rdi)		/* user RAX */
 	movq	(1*8)(%rsp), %rax		/* user RIP */
@@ -982,6 +996,8 @@ ENTRY(switch_to_thread_stack)
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdi
 	movq	%rsp, %rdi
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC
 	UNWIND_HINT sp_offset=16 sp_reg=ORC_REG_DI
 
 	pushq	7*8(%rdi)		/* regs->ss */
@@ -1282,6 +1298,8 @@ ENTRY(paranoid_entry)
 
 1:
 	SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
+	/* Stop Indirect Branch speculation */
+	STOP_IB_SPEC_SAVE_AND_CLOBBER save_reg=%r13d
 
 	ret
 END(paranoid_entry)
@@ -1305,6 +1323,8 @@ ENTRY(paranoid_exit)
 	testl	%ebx, %ebx			/* swapgs needed? */
 	jnz	.Lparanoid_exit_no_swapgs
 	TRACE_IRQS_IRETQ
+	/* Restore Indirect Branch Speculation to the previous state */
+	RESTORE_IB_SPEC_CLOBBER save_reg=%r13d
 	RESTORE_CR3	scratch_reg=%rbx save_reg=%r14
 	SWAPGS_UNSAFE_STACK
 	jmp	.Lparanoid_exit_restore
@@ -1335,6 +1355,8 @@ ENTRY(error_entry)
 	SWAPGS
 	/* We have user CR3.  Change to kernel CR3. */
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC_CLOBBER
 
 .Lerror_entry_from_usermode_after_swapgs:
 	/* Put us onto the real thread stack. */
@@ -1382,6 +1404,8 @@ ENTRY(error_entry)
 	 */
 	SWAPGS
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC_CLOBBER
 	jmp .Lerror_entry_done
 
 .Lbstep_iret:
@@ -1396,6 +1420,8 @@ ENTRY(error_entry)
 	 */
 	SWAPGS
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC
 
 	/*
 	 * Pretend that the exception came from user mode: set up pt_regs
@@ -1497,6 +1523,10 @@ ENTRY(nmi)
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
 	movq	%rsp, %rdx
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
+
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC
+
 	UNWIND_HINT_IRET_REGS base=%rdx offset=8
 	pushq	5*8(%rdx)	/* pt_regs->ss */
 	pushq	4*8(%rdx)	/* pt_regs->rsp */
@@ -1747,6 +1777,9 @@ ENTRY(nmi)
 	movq	$-1, %rsi
 	call	do_nmi
 
+	/* Restore Indirect Branch speculation to the previous state */
+	RESTORE_IB_SPEC_CLOBBER save_reg=%r13d
+
 	RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
 
 	testl	%ebx, %ebx			/* swapgs needed? */
--- a/arch/x86/entry/entry_64_compat.S
+++ b/arch/x86/entry/entry_64_compat.S
@@ -54,6 +54,8 @@ ENTRY(entry_SYSENTER_compat)
 	SWITCH_TO_KERNEL_CR3 scratch_reg=%rsp
 
 	movq	PER_CPU_VAR(cpu_current_top_of_stack), %rsp
+	/* Stop Indirect Branch Speculation */
+	STOP_IB_SPEC
 
 	/*
 	 * User tracing code (ptrace or signal handlers) might assume that
@@ -224,12 +226,18 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram
 	pushq   $0			/* pt_regs->r14 = 0 */
 	pushq   $0			/* pt_regs->r15 = 0 */
 
-	/*
-	 * User mode is traced as though IRQs are on, and SYSENTER
+	/* Stop Indirect Branch Speculation. All registers are saved already */
+	STOP_IB_SPEC_CLOBBER
+
+	/* User mode is traced as though IRQs are on, and SYSENTER
 	 * turned them off.
 	 */
 	TRACE_IRQS_OFF
 
+	/*
+	 * We just saved %rdi so it is safe to clobber.  It is not
+	 * preserved during the C calls inside TRACE_IRQS_OFF anyway.
+	 */
 	movq	%rsp, %rdi
 	call	do_fast_syscall_32
 	/* XEN PV guests always use IRET path */
@@ -239,6 +247,15 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram
 	/* Opportunistic SYSRET */
 sysret32_from_system_call:
 	TRACE_IRQS_ON			/* User mode traces as IRQs on. */
+
+	/*
+	 * Restart Indirect Branch Speculation. This is safe to do here
+	 * because there are no indirect branches between here and the
+	 * return to userspace (sysretl).
+	 * Clobber of %rax, %rcx, %rdx is OK before register restoring.
+	 */
+	RESTART_IB_SPEC_CLOBBER
+
 	movq	RBX(%rsp), %rbx		/* pt_regs->rbx */
 	movq	RBP(%rsp), %rbp		/* pt_regs->rbp */
 	movq	EFLAGS(%rsp), %r11	/* pt_regs->flags (in r11) */

  parent reply	other threads:[~2018-01-18 15:10 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-18 13:48 [PATCH 00/35] jump_label, objtool, IBRS and IBPB Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 01/35] jump_label: Add branch hints to static_branch_{un,}likely() Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 02/35] sched: Optimize ttwu_stat() Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 03/35] x86: Reindent _static_cpu_has Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 04/35] x86: Update _static_cpu_has to use all named variables Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 05/35] x86: Add a type field to alt_instr Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 06/35] objtool: Implement base jump_assert support Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 07/35] x86: Annotate static_cpu_has alternative Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 08/35] objtool: Implement jump_assert for _static_cpu_has() Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 09/35] objtool: Introduce special_type Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 10/35] x86/jump_label: Implement arch_static_assert() Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 11/35] objtool: Add retpoline validation Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 12/35] x86/paravirt: Annotate indirect calls Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 13/35] x86,nospec: Annotate indirect calls/jumps Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 14/35] x86: Annotate indirect jump in head_64.S Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 15/35] objtool: More complex static jump implementation Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 16/35] objtool: Use existing global variables for options Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 17/35] objtool: Even more complex static block checks Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 18/35] objtool: Another static block fail Peter Zijlstra, Peter Zijlstra
2018-01-19 16:42   ` Peter Zijlstra
2018-01-29 18:01     ` Josh Poimboeuf
2018-01-29 18:24       ` Peter Zijlstra
2018-01-18 13:48 ` [PATCH 19/35] objtool: Skip static assert when KCOV/KASAN Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 20/35] x86: Force asm-goto Peter Zijlstra, Peter Zijlstra
2018-01-18 16:25   ` David Woodhouse
2018-01-18 13:48 ` [PATCH 21/35] x86: Remove FAST_FEATURE_TESTS Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 22/35] x86/cpufeatures: Detect Speculation control feature Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 23/35] x86/speculation: Add basic speculation control code Peter Zijlstra, Peter Zijlstra
2018-01-18 16:37   ` Josh Poimboeuf
2018-01-18 17:08     ` Dave Hansen
2018-01-18 17:12       ` Paolo Bonzini
2018-01-18 18:24         ` Josh Poimboeuf
2018-01-18 19:08           ` Andrea Arcangeli
2018-01-18 23:25             ` Andy Lutomirski
2018-01-18 23:35               ` Andrew Cooper
2018-01-19  1:41               ` Andrea Arcangeli
2018-01-19  4:10                 ` Andy Lutomirski
2018-01-19  4:15                   ` Van De Ven, Arjan
2018-01-19 15:47                     ` Andrea Arcangeli
2018-01-18 13:48 ` [PATCH 24/35] x86/msr: Move native_*msr macros out of microcode.h Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 25/35] x86/speculation: Add inlines to control Indirect Branch Speculation Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 26/35] x86/enter: Create macros to stop/restart " Peter Zijlstra, Peter Zijlstra
2018-01-18 19:44   ` Tim Chen
2018-01-18 13:48 ` Peter Zijlstra, Peter Zijlstra [this message]
2018-01-18 13:48 ` [PATCH 28/35] x86/idle: Control Indirect Branch Speculation in idle Peter Zijlstra, Peter Zijlstra
2018-01-18 19:52   ` Andrew Cooper
2018-01-18 13:48 ` [PATCH 29/35] x86/speculation: Add IPBP support Peter Zijlstra, Peter Zijlstra
2018-01-18 16:22   ` Josh Poimboeuf
2018-01-18 18:31   ` Borislav Petkov
2018-01-18 18:35     ` Josh Poimboeuf
2018-01-18 18:46       ` Borislav Petkov
2018-01-18 13:48 ` [PATCH 30/35] x86/speculation: Use Indirect Branch Prediction Barrier in context switch Peter Zijlstra, Peter Zijlstra
2018-01-19  0:38   ` Tim Chen
2018-01-19  4:03     ` Kevin Easton
2018-01-19 20:26       ` Tim Chen
2018-01-18 13:48 ` [PATCH 31/35] x86/ibrs: Add new helper macros to save/restore MSR_IA32_SPEC_CTRL Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 32/35] x86/vmx: Direct access to MSR_IA32_SPEC_CTRL Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 33/35] x86/svm: " Peter Zijlstra, Peter Zijlstra
2018-01-18 13:48 ` [PATCH 34/35] x86/kvm: Add IBPB support Peter Zijlstra, Peter Zijlstra
2018-01-18 15:32   ` Paolo Bonzini
2018-01-19 15:25     ` Paolo Bonzini
2018-01-19 16:08       ` David Woodhouse
2018-01-19 16:27         ` Andy Lutomirski
2018-01-19 16:48         ` Paolo Bonzini
2018-01-18 13:48 ` [PATCH 35/35] x86/nospec: Add static assertions Peter Zijlstra, Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180118140153.076225139@infradead.org \
    --to=peterz@infradead.org \
    --cc=aarcange@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=arjan.van.de.ven@intel.com \
    --cc=ashok.raj@intel.com \
    --cc=asit.k.mallick@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=dwmw2@infradead.org \
    --cc=dwmw@amazon.co.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=jbaron@akamai.com \
    --cc=jpoimboe@redhat.com \
    --cc=jun.nakajima@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.