| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- From f978416e1df8d655e6ac7ae848928441cf33d598 Mon Sep 17 00:00:00 2001
- From: Dave Hansen <[email protected]>
- Date: Mon, 4 Dec 2017 15:07:54 +0100
- Subject: [PATCH 179/242] x86/mm: Move the CR3 construction functions to
- tlbflush.h
- MIME-Version: 1.0
- Content-Type: text/plain; charset=UTF-8
- Content-Transfer-Encoding: 8bit
- CVE-2017-5754
- For flushing the TLB, the ASID which has been programmed into the hardware
- must be known. That differs from what is in 'cpu_tlbstate'.
- Add functions to transform the 'cpu_tlbstate' values into to the one
- programmed into the hardware (CR3).
- It's not easy to include mmu_context.h into tlbflush.h, so just move the
- CR3 building over to tlbflush.h.
- Signed-off-by: Dave Hansen <[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: 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]
- Cc: [email protected]
- Signed-off-by: Ingo Molnar <[email protected]>
- (cherry picked from commit 50fb83a62cf472dc53ba23bd3f7bd6c1b2b3b53e)
- Signed-off-by: Andy Whitcroft <[email protected]>
- Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
- (cherry picked from commit f741923acf51c1061c11b45a168f8864d37dc5cd)
- Signed-off-by: Fabian Grünbichler <[email protected]>
- ---
- arch/x86/include/asm/mmu_context.h | 29 +----------------------------
- arch/x86/include/asm/tlbflush.h | 26 ++++++++++++++++++++++++++
- arch/x86/mm/tlb.c | 8 ++++----
- 3 files changed, 31 insertions(+), 32 deletions(-)
- diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
- index 47ec51a821e8..89a01ad7e370 100644
- --- a/arch/x86/include/asm/mmu_context.h
- +++ b/arch/x86/include/asm/mmu_context.h
- @@ -289,33 +289,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
- return __pkru_allows_pkey(vma_pkey(vma), write);
- }
-
- -/*
- - * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID
- - * bits. This serves two purposes. It prevents a nasty situation in
- - * which PCID-unaware code saves CR3, loads some other value (with PCID
- - * == 0), and then restores CR3, thus corrupting the TLB for ASID 0 if
- - * the saved ASID was nonzero. It also means that any bugs involving
- - * loading a PCID-enabled CR3 with CR4.PCIDE off will trigger
- - * deterministically.
- - */
- -
- -static inline unsigned long build_cr3(struct mm_struct *mm, u16 asid)
- -{
- - if (static_cpu_has(X86_FEATURE_PCID)) {
- - VM_WARN_ON_ONCE(asid > 4094);
- - return __sme_pa(mm->pgd) | (asid + 1);
- - } else {
- - VM_WARN_ON_ONCE(asid != 0);
- - return __sme_pa(mm->pgd);
- - }
- -}
- -
- -static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
- -{
- - VM_WARN_ON_ONCE(asid > 4094);
- - return __sme_pa(mm->pgd) | (asid + 1) | CR3_NOFLUSH;
- -}
- -
- /*
- * This can be used from process context to figure out what the value of
- * CR3 is without needing to do a (slow) __read_cr3().
- @@ -325,7 +298,7 @@ static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
- */
- static inline unsigned long __get_current_cr3_fast(void)
- {
- - unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm),
- + unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd,
- this_cpu_read(cpu_tlbstate.loaded_mm_asid));
-
- /* For now, be very restrictive about when this can be called. */
- diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
- index ed5d483c4a1b..3a421b164868 100644
- --- a/arch/x86/include/asm/tlbflush.h
- +++ b/arch/x86/include/asm/tlbflush.h
- @@ -68,6 +68,32 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
- return atomic64_inc_return(&mm->context.tlb_gen);
- }
-
- +/*
- + * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID bits.
- + * This serves two purposes. It prevents a nasty situation in which
- + * PCID-unaware code saves CR3, loads some other value (with PCID == 0),
- + * and then restores CR3, thus corrupting the TLB for ASID 0 if the saved
- + * ASID was nonzero. It also means that any bugs involving loading a
- + * PCID-enabled CR3 with CR4.PCIDE off will trigger deterministically.
- + */
- +struct pgd_t;
- +static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
- +{
- + if (static_cpu_has(X86_FEATURE_PCID)) {
- + VM_WARN_ON_ONCE(asid > 4094);
- + return __sme_pa(pgd) | (asid + 1);
- + } else {
- + VM_WARN_ON_ONCE(asid != 0);
- + return __sme_pa(pgd);
- + }
- +}
- +
- +static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
- +{
- + VM_WARN_ON_ONCE(asid > 4094);
- + return __sme_pa(pgd) | (asid + 1) | CR3_NOFLUSH;
- +}
- +
- #ifdef CONFIG_PARAVIRT
- #include <asm/paravirt.h>
- #else
- diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
- index 5b4342c5039c..87d4f961bcb4 100644
- --- a/arch/x86/mm/tlb.c
- +++ b/arch/x86/mm/tlb.c
- @@ -126,7 +126,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
- * does something like write_cr3(read_cr3_pa()).
- */
- #ifdef CONFIG_DEBUG_VM
- - if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev, prev_asid))) {
- + if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
- /*
- * If we were to BUG here, we'd be very likely to kill
- * the system so hard that we don't see the call trace.
- @@ -193,7 +193,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
- if (need_flush) {
- this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
- this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
- - write_cr3(build_cr3(next, new_asid));
- + write_cr3(build_cr3(next->pgd, new_asid));
-
- /*
- * NB: This gets called via leave_mm() in the idle path
- @@ -206,7 +206,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
- trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
- } else {
- /* The new ASID is already up to date. */
- - write_cr3(build_cr3_noflush(next, new_asid));
- + write_cr3(build_cr3_noflush(next->pgd, new_asid));
-
- /* See above wrt _rcuidle. */
- trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
- @@ -283,7 +283,7 @@ void initialize_tlbstate_and_flush(void)
- !(cr4_read_shadow() & X86_CR4_PCIDE));
-
- /* Force ASID 0 and force a TLB flush. */
- - write_cr3(build_cr3(mm, 0));
- + write_cr3(build_cr3(mm->pgd, 0));
-
- /* Reinitialize tlbstate. */
- this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
- --
- 2.14.2
|