]> git.proxmox.com Git - pve-kernel.git/blame - patches/kernel/0179-x86-mm-Move-the-CR3-construction-functions-to-tlbflu.patch
update ABI file
[pve-kernel.git] / patches / kernel / 0179-x86-mm-Move-the-CR3-construction-functions-to-tlbflu.patch
CommitLineData
59d5af67 1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
321d628a
FG
2From: Dave Hansen <dave.hansen@linux.intel.com>
3Date: Mon, 4 Dec 2017 15:07:54 +0100
59d5af67 4Subject: [PATCH] x86/mm: Move the CR3 construction functions to tlbflush.h
321d628a
FG
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9CVE-2017-5754
10
11For flushing the TLB, the ASID which has been programmed into the hardware
12must be known. That differs from what is in 'cpu_tlbstate'.
13
14Add functions to transform the 'cpu_tlbstate' values into to the one
15programmed into the hardware (CR3).
16
17It's not easy to include mmu_context.h into tlbflush.h, so just move the
18CR3 building over to tlbflush.h.
19
20Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
21Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
22Cc: Andy Lutomirski <luto@kernel.org>
23Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
24Cc: Borislav Petkov <bp@alien8.de>
25Cc: Brian Gerst <brgerst@gmail.com>
26Cc: David Laight <David.Laight@aculab.com>
27Cc: Denys Vlasenko <dvlasenk@redhat.com>
28Cc: Eduardo Valentin <eduval@amazon.com>
29Cc: Greg KH <gregkh@linuxfoundation.org>
30Cc: H. Peter Anvin <hpa@zytor.com>
31Cc: Josh Poimboeuf <jpoimboe@redhat.com>
32Cc: Juergen Gross <jgross@suse.com>
33Cc: Linus Torvalds <torvalds@linux-foundation.org>
34Cc: Peter Zijlstra <peterz@infradead.org>
35Cc: Will Deacon <will.deacon@arm.com>
36Cc: aliguori@amazon.com
37Cc: daniel.gruss@iaik.tugraz.at
38Cc: hughd@google.com
39Cc: keescook@google.com
40Cc: linux-mm@kvack.org
41Signed-off-by: Ingo Molnar <mingo@kernel.org>
42(cherry picked from commit 50fb83a62cf472dc53ba23bd3f7bd6c1b2b3b53e)
43Signed-off-by: Andy Whitcroft <apw@canonical.com>
44Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
45(cherry picked from commit f741923acf51c1061c11b45a168f8864d37dc5cd)
46Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
47---
48 arch/x86/include/asm/mmu_context.h | 29 +----------------------------
49 arch/x86/include/asm/tlbflush.h | 26 ++++++++++++++++++++++++++
50 arch/x86/mm/tlb.c | 8 ++++----
51 3 files changed, 31 insertions(+), 32 deletions(-)
52
53diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
54index 47ec51a821e8..89a01ad7e370 100644
55--- a/arch/x86/include/asm/mmu_context.h
56+++ b/arch/x86/include/asm/mmu_context.h
57@@ -289,33 +289,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
58 return __pkru_allows_pkey(vma_pkey(vma), write);
59 }
60
61-/*
62- * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID
63- * bits. This serves two purposes. It prevents a nasty situation in
64- * which PCID-unaware code saves CR3, loads some other value (with PCID
65- * == 0), and then restores CR3, thus corrupting the TLB for ASID 0 if
66- * the saved ASID was nonzero. It also means that any bugs involving
67- * loading a PCID-enabled CR3 with CR4.PCIDE off will trigger
68- * deterministically.
69- */
70-
71-static inline unsigned long build_cr3(struct mm_struct *mm, u16 asid)
72-{
73- if (static_cpu_has(X86_FEATURE_PCID)) {
74- VM_WARN_ON_ONCE(asid > 4094);
75- return __sme_pa(mm->pgd) | (asid + 1);
76- } else {
77- VM_WARN_ON_ONCE(asid != 0);
78- return __sme_pa(mm->pgd);
79- }
80-}
81-
82-static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
83-{
84- VM_WARN_ON_ONCE(asid > 4094);
85- return __sme_pa(mm->pgd) | (asid + 1) | CR3_NOFLUSH;
86-}
87-
88 /*
89 * This can be used from process context to figure out what the value of
90 * CR3 is without needing to do a (slow) __read_cr3().
91@@ -325,7 +298,7 @@ static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
92 */
93 static inline unsigned long __get_current_cr3_fast(void)
94 {
95- unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm),
96+ unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd,
97 this_cpu_read(cpu_tlbstate.loaded_mm_asid));
98
99 /* For now, be very restrictive about when this can be called. */
100diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
101index ed5d483c4a1b..3a421b164868 100644
102--- a/arch/x86/include/asm/tlbflush.h
103+++ b/arch/x86/include/asm/tlbflush.h
104@@ -68,6 +68,32 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
105 return atomic64_inc_return(&mm->context.tlb_gen);
106 }
107
108+/*
109+ * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID bits.
110+ * This serves two purposes. It prevents a nasty situation in which
111+ * PCID-unaware code saves CR3, loads some other value (with PCID == 0),
112+ * and then restores CR3, thus corrupting the TLB for ASID 0 if the saved
113+ * ASID was nonzero. It also means that any bugs involving loading a
114+ * PCID-enabled CR3 with CR4.PCIDE off will trigger deterministically.
115+ */
116+struct pgd_t;
117+static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
118+{
119+ if (static_cpu_has(X86_FEATURE_PCID)) {
120+ VM_WARN_ON_ONCE(asid > 4094);
121+ return __sme_pa(pgd) | (asid + 1);
122+ } else {
123+ VM_WARN_ON_ONCE(asid != 0);
124+ return __sme_pa(pgd);
125+ }
126+}
127+
128+static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
129+{
130+ VM_WARN_ON_ONCE(asid > 4094);
131+ return __sme_pa(pgd) | (asid + 1) | CR3_NOFLUSH;
132+}
133+
134 #ifdef CONFIG_PARAVIRT
135 #include <asm/paravirt.h>
136 #else
137diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
138index 5b4342c5039c..87d4f961bcb4 100644
139--- a/arch/x86/mm/tlb.c
140+++ b/arch/x86/mm/tlb.c
141@@ -126,7 +126,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
142 * does something like write_cr3(read_cr3_pa()).
143 */
144 #ifdef CONFIG_DEBUG_VM
145- if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev, prev_asid))) {
146+ if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
147 /*
148 * If we were to BUG here, we'd be very likely to kill
149 * the system so hard that we don't see the call trace.
150@@ -193,7 +193,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
151 if (need_flush) {
152 this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
153 this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
154- write_cr3(build_cr3(next, new_asid));
155+ write_cr3(build_cr3(next->pgd, new_asid));
156
157 /*
158 * NB: This gets called via leave_mm() in the idle path
159@@ -206,7 +206,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
160 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
161 } else {
162 /* The new ASID is already up to date. */
163- write_cr3(build_cr3_noflush(next, new_asid));
164+ write_cr3(build_cr3_noflush(next->pgd, new_asid));
165
166 /* See above wrt _rcuidle. */
167 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
168@@ -283,7 +283,7 @@ void initialize_tlbstate_and_flush(void)
169 !(cr4_read_shadow() & X86_CR4_PCIDE));
170
171 /* Force ASID 0 and force a TLB flush. */
172- write_cr3(build_cr3(mm, 0));
173+ write_cr3(build_cr3(mm->pgd, 0));
174
175 /* Reinitialize tlbstate. */
176 this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
177--
1782.14.2
179