aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Hansen <dave.hansen@linux.intel.com>2017-12-04 09:07:54 -0500
committerIngo Molnar <mingo@kernel.org>2017-12-22 14:13:03 -0500
commit50fb83a62cf472dc53ba23bd3f7bd6c1b2b3b53e (patch)
treea22a6fc2e7adf068e1795fe673a6f5b49d0d9435
parent3f67af51e56f291d7417d77c4f67cd774633c5e1 (diff)
x86/mm: Move the CR3 construction functions to tlbflush.h
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 <dave.hansen@linux.intel.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Andy Lutomirski <luto@kernel.org> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: David Laight <David.Laight@aculab.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Eduardo Valentin <eduval@amazon.com> Cc: Greg KH <gregkh@linuxfoundation.org> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Juergen Gross <jgross@suse.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Will Deacon <will.deacon@arm.com> Cc: aliguori@amazon.com Cc: daniel.gruss@iaik.tugraz.at Cc: hughd@google.com Cc: keescook@google.com Cc: linux-mm@kvack.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--arch/x86/include/asm/mmu_context.h29
-rw-r--r--arch/x86/include/asm/tlbflush.h26
-rw-r--r--arch/x86/mm/tlb.c8
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 5e25423bf9bb..5ede7cae1d67 100644
--- a/arch/x86/include/asm/mmu_context.h
+++ b/arch/x86/include/asm/mmu_context.h
@@ -291,33 +291,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
291} 291}
292 292
293/* 293/*
294 * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID
295 * bits. This serves two purposes. It prevents a nasty situation in
296 * which PCID-unaware code saves CR3, loads some other value (with PCID
297 * == 0), and then restores CR3, thus corrupting the TLB for ASID 0 if
298 * the saved ASID was nonzero. It also means that any bugs involving
299 * loading a PCID-enabled CR3 with CR4.PCIDE off will trigger
300 * deterministically.
301 */
302
303static inline unsigned long build_cr3(struct mm_struct *mm, u16 asid)
304{
305 if (static_cpu_has(X86_FEATURE_PCID)) {
306 VM_WARN_ON_ONCE(asid > 4094);
307 return __sme_pa(mm->pgd) | (asid + 1);
308 } else {
309 VM_WARN_ON_ONCE(asid != 0);
310 return __sme_pa(mm->pgd);
311 }
312}
313
314static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
315{
316 VM_WARN_ON_ONCE(asid > 4094);
317 return __sme_pa(mm->pgd) | (asid + 1) | CR3_NOFLUSH;
318}
319
320/*
321 * This can be used from process context to figure out what the value of 294 * This can be used from process context to figure out what the value of
322 * CR3 is without needing to do a (slow) __read_cr3(). 295 * CR3 is without needing to do a (slow) __read_cr3().
323 * 296 *
@@ -326,7 +299,7 @@ static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
326 */ 299 */
327static inline unsigned long __get_current_cr3_fast(void) 300static inline unsigned long __get_current_cr3_fast(void)
328{ 301{
329 unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm), 302 unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd,
330 this_cpu_read(cpu_tlbstate.loaded_mm_asid)); 303 this_cpu_read(cpu_tlbstate.loaded_mm_asid));
331 304
332 /* For now, be very restrictive about when this can be called. */ 305 /* 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 552d581c8f9f..ee7925adfb57 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -69,6 +69,32 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
69 return atomic64_inc_return(&mm->context.tlb_gen); 69 return atomic64_inc_return(&mm->context.tlb_gen);
70} 70}
71 71
72/*
73 * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID bits.
74 * This serves two purposes. It prevents a nasty situation in which
75 * PCID-unaware code saves CR3, loads some other value (with PCID == 0),
76 * and then restores CR3, thus corrupting the TLB for ASID 0 if the saved
77 * ASID was nonzero. It also means that any bugs involving loading a
78 * PCID-enabled CR3 with CR4.PCIDE off will trigger deterministically.
79 */
80struct pgd_t;
81static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
82{
83 if (static_cpu_has(X86_FEATURE_PCID)) {
84 VM_WARN_ON_ONCE(asid > 4094);
85 return __sme_pa(pgd) | (asid + 1);
86 } else {
87 VM_WARN_ON_ONCE(asid != 0);
88 return __sme_pa(pgd);
89 }
90}
91
92static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
93{
94 VM_WARN_ON_ONCE(asid > 4094);
95 return __sme_pa(pgd) | (asid + 1) | CR3_NOFLUSH;
96}
97
72#ifdef CONFIG_PARAVIRT 98#ifdef CONFIG_PARAVIRT
73#include <asm/paravirt.h> 99#include <asm/paravirt.h>
74#else 100#else
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 0569987f6da6..0a1be3adc97e 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -128,7 +128,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
128 * isn't free. 128 * isn't free.
129 */ 129 */
130#ifdef CONFIG_DEBUG_VM 130#ifdef CONFIG_DEBUG_VM
131 if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev, prev_asid))) { 131 if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
132 /* 132 /*
133 * If we were to BUG here, we'd be very likely to kill 133 * If we were to BUG here, we'd be very likely to kill
134 * the system so hard that we don't see the call trace. 134 * the system so hard that we don't see the call trace.
@@ -195,7 +195,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
195 if (need_flush) { 195 if (need_flush) {
196 this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); 196 this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
197 this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); 197 this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
198 write_cr3(build_cr3(next, new_asid)); 198 write_cr3(build_cr3(next->pgd, new_asid));
199 199
200 /* 200 /*
201 * NB: This gets called via leave_mm() in the idle path 201 * NB: This gets called via leave_mm() in the idle path
@@ -208,7 +208,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
208 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); 208 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
209 } else { 209 } else {
210 /* The new ASID is already up to date. */ 210 /* The new ASID is already up to date. */
211 write_cr3(build_cr3_noflush(next, new_asid)); 211 write_cr3(build_cr3_noflush(next->pgd, new_asid));
212 212
213 /* See above wrt _rcuidle. */ 213 /* See above wrt _rcuidle. */
214 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0); 214 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
@@ -288,7 +288,7 @@ void initialize_tlbstate_and_flush(void)
288 !(cr4_read_shadow() & X86_CR4_PCIDE)); 288 !(cr4_read_shadow() & X86_CR4_PCIDE));
289 289
290 /* Force ASID 0 and force a TLB flush. */ 290 /* Force ASID 0 and force a TLB flush. */
291 write_cr3(build_cr3(mm, 0)); 291 write_cr3(build_cr3(mm->pgd, 0));
292 292
293 /* Reinitialize tlbstate. */ 293 /* Reinitialize tlbstate. */
294 this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0); 294 this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);