| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- From eb4a670cb54266bfab7bb4d9fd9e5da7b296ecdf Mon Sep 17 00:00:00 2001
- From: Peter Zijlstra <[email protected]>
- Date: Mon, 4 Dec 2017 15:08:00 +0100
- Subject: [PATCH 210/242] x86/mm: Optimize RESTORE_CR3
- MIME-Version: 1.0
- Content-Type: text/plain; charset=UTF-8
- Content-Transfer-Encoding: 8bit
- CVE-2017-5754
- Most NMI/paranoid exceptions will not in fact change pagetables and would
- thus not require TLB flushing, however RESTORE_CR3 uses flushing CR3
- writes.
- Restores to kernel PCIDs can be NOFLUSH, because we explicitly flush the
- kernel mappings and now that we track which user PCIDs need flushing we can
- avoid those too when possible.
- This does mean RESTORE_CR3 needs an additional scratch_reg, luckily both
- sites have plenty available.
- Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
- Signed-off-by: Thomas Gleixner <[email protected]>
- Cc: Andy Lutomirski <[email protected]>
- Cc: Boris Ostrovsky <[email protected]>
- Cc: Borislav Petkov <[email protected]>
- Cc: Brian Gerst <[email protected]>
- Cc: Dave Hansen <[email protected]>
- Cc: David Laight <[email protected]>
- Cc: Denys Vlasenko <[email protected]>
- Cc: Eduardo Valentin <[email protected]>
- Cc: Greg KH <[email protected]>
- Cc: H. Peter Anvin <[email protected]>
- Cc: Josh Poimboeuf <[email protected]>
- Cc: Juergen Gross <[email protected]>
- Cc: Linus Torvalds <[email protected]>
- Cc: Peter Zijlstra <[email protected]>
- Cc: Will Deacon <[email protected]>
- Cc: [email protected]
- Cc: [email protected]
- Cc: [email protected]
- Cc: [email protected]
- Signed-off-by: Ingo Molnar <[email protected]>
- (cherry picked from commit 21e94459110252d41b45c0c8ba50fd72a664d50c)
- Signed-off-by: Andy Whitcroft <[email protected]>
- Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
- (cherry picked from commit 6ebe6e2896841282357d43c09394b0ca47c41e4a)
- Signed-off-by: Fabian Grünbichler <[email protected]>
- ---
- arch/x86/entry/calling.h | 30 ++++++++++++++++++++++++++++--
- arch/x86/entry/entry_64.S | 4 ++--
- 2 files changed, 30 insertions(+), 4 deletions(-)
- diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
- index ce5fb309926d..015e0a84bb99 100644
- --- a/arch/x86/entry/calling.h
- +++ b/arch/x86/entry/calling.h
- @@ -280,8 +280,34 @@ For 32-bit we have the following conventions - kernel is built with
- .Ldone_\@:
- .endm
-
- -.macro RESTORE_CR3 save_reg:req
- +.macro RESTORE_CR3 scratch_reg:req save_reg:req
- ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
- +
- + ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
- +
- + /*
- + * KERNEL pages can always resume with NOFLUSH as we do
- + * explicit flushes.
- + */
- + bt $X86_CR3_PTI_SWITCH_BIT, \save_reg
- + jnc .Lnoflush_\@
- +
- + /*
- + * Check if there's a pending flush for the user ASID we're
- + * about to set.
- + */
- + movq \save_reg, \scratch_reg
- + andq $(0x7FF), \scratch_reg
- + bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
- + jnc .Lnoflush_\@
- +
- + btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
- + jmp .Lwrcr3_\@
- +
- +.Lnoflush_\@:
- + SET_NOFLUSH_BIT \save_reg
- +
- +.Lwrcr3_\@:
- /*
- * The CR3 write could be avoided when not changing its value,
- * but would require a CR3 read *and* a scratch register.
- @@ -300,7 +326,7 @@ For 32-bit we have the following conventions - kernel is built with
- .endm
- .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
- .endm
- -.macro RESTORE_CR3 save_reg:req
- +.macro RESTORE_CR3 scratch_reg:req save_reg:req
- .endm
-
- #endif
- diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
- index fb43f14ed299..b48f2c78a9bf 100644
- --- a/arch/x86/entry/entry_64.S
- +++ b/arch/x86/entry/entry_64.S
- @@ -1300,7 +1300,7 @@ ENTRY(paranoid_exit)
- testl %ebx, %ebx /* swapgs needed? */
- jnz .Lparanoid_exit_no_swapgs
- TRACE_IRQS_IRETQ
- - RESTORE_CR3 save_reg=%r14
- + RESTORE_CR3 scratch_reg=%rbx save_reg=%r14
- SWAPGS_UNSAFE_STACK
- jmp .Lparanoid_exit_restore
- .Lparanoid_exit_no_swapgs:
- @@ -1742,7 +1742,7 @@ end_repeat_nmi:
- movq $-1, %rsi
- call do_nmi
-
- - RESTORE_CR3 save_reg=%r14
- + RESTORE_CR3 scratch_reg=%r15 save_reg=%r14
-
- testl %ebx, %ebx /* swapgs needed? */
- jnz nmi_restore
- --
- 2.14.2
|