1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/init.h>
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/mm.h>
5*4882a593Smuzhiyun #include <linux/spinlock.h>
6*4882a593Smuzhiyun #include <linux/smp.h>
7*4882a593Smuzhiyun #include <linux/interrupt.h>
8*4882a593Smuzhiyun #include <linux/export.h>
9*4882a593Smuzhiyun #include <linux/cpu.h>
10*4882a593Smuzhiyun #include <linux/debugfs.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <asm/tlbflush.h>
13*4882a593Smuzhiyun #include <asm/mmu_context.h>
14*4882a593Smuzhiyun #include <asm/nospec-branch.h>
15*4882a593Smuzhiyun #include <asm/cache.h>
16*4882a593Smuzhiyun #include <asm/apic.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "mm_internal.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #ifdef CONFIG_PARAVIRT
21*4882a593Smuzhiyun # define STATIC_NOPV
22*4882a593Smuzhiyun #else
23*4882a593Smuzhiyun # define STATIC_NOPV static
24*4882a593Smuzhiyun # define __flush_tlb_local native_flush_tlb_local
25*4882a593Smuzhiyun # define __flush_tlb_global native_flush_tlb_global
26*4882a593Smuzhiyun # define __flush_tlb_one_user(addr) native_flush_tlb_one_user(addr)
27*4882a593Smuzhiyun # define __flush_tlb_others(msk, info) native_flush_tlb_others(msk, info)
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * TLB flushing, formerly SMP-only
32*4882a593Smuzhiyun * c/o Linus Torvalds.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * These mean you can really definitely utterly forget about
35*4882a593Smuzhiyun * writing to user space from interrupts. (Its not allowed anyway).
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * Optimizations Manfred Spraul <manfred@colorfullife.com>
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * More scalable flush, from Andi Kleen
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * Implement flush IPI by CALL_FUNCTION_VECTOR, Alex Shi
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun * Use bit 0 to mangle the TIF_SPEC_IB state into the mm pointer which is
46*4882a593Smuzhiyun * stored in cpu_tlb_state.last_user_mm_ibpb.
47*4882a593Smuzhiyun */
48*4882a593Smuzhiyun #define LAST_USER_MM_IBPB 0x1UL
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * The x86 feature is called PCID (Process Context IDentifier). It is similar
52*4882a593Smuzhiyun * to what is traditionally called ASID on the RISC processors.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * We don't use the traditional ASID implementation, where each process/mm gets
55*4882a593Smuzhiyun * its own ASID and flush/restart when we run out of ASID space.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * Instead we have a small per-cpu array of ASIDs and cache the last few mm's
58*4882a593Smuzhiyun * that came by on this CPU, allowing cheaper switch_mm between processes on
59*4882a593Smuzhiyun * this CPU.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * We end up with different spaces for different things. To avoid confusion we
62*4882a593Smuzhiyun * use different names for each of them:
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * ASID - [0, TLB_NR_DYN_ASIDS-1]
65*4882a593Smuzhiyun * the canonical identifier for an mm
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * kPCID - [1, TLB_NR_DYN_ASIDS]
68*4882a593Smuzhiyun * the value we write into the PCID part of CR3; corresponds to the
69*4882a593Smuzhiyun * ASID+1, because PCID 0 is special.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * uPCID - [2048 + 1, 2048 + TLB_NR_DYN_ASIDS]
72*4882a593Smuzhiyun * for KPTI each mm has two address spaces and thus needs two
73*4882a593Smuzhiyun * PCID values, but we can still do with a single ASID denomination
74*4882a593Smuzhiyun * for each mm. Corresponds to kPCID + 2048.
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* There are 12 bits of space for ASIDS in CR3 */
79*4882a593Smuzhiyun #define CR3_HW_ASID_BITS 12
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * When enabled, PAGE_TABLE_ISOLATION consumes a single bit for
83*4882a593Smuzhiyun * user/kernel switches
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun #ifdef CONFIG_PAGE_TABLE_ISOLATION
86*4882a593Smuzhiyun # define PTI_CONSUMED_PCID_BITS 1
87*4882a593Smuzhiyun #else
88*4882a593Smuzhiyun # define PTI_CONSUMED_PCID_BITS 0
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun #define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS)
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * ASIDs are zero-based: 0->MAX_AVAIL_ASID are valid. -1 below to account
95*4882a593Smuzhiyun * for them being zero-based. Another -1 is because PCID 0 is reserved for
96*4882a593Smuzhiyun * use by non-PCID-aware users.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun #define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2)
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Given @asid, compute kPCID
102*4882a593Smuzhiyun */
kern_pcid(u16 asid)103*4882a593Smuzhiyun static inline u16 kern_pcid(u16 asid)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun #ifdef CONFIG_PAGE_TABLE_ISOLATION
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * Make sure that the dynamic ASID space does not confict with the
110*4882a593Smuzhiyun * bit we are using to switch between user and kernel ASIDs.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_PCID_USER_BIT));
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * The ASID being passed in here should have respected the
116*4882a593Smuzhiyun * MAX_ASID_AVAILABLE and thus never have the switch bit set.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_PCID_USER_BIT));
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * The dynamically-assigned ASIDs that get passed in are small
122*4882a593Smuzhiyun * (<TLB_NR_DYN_ASIDS). They never have the high switch bit set,
123*4882a593Smuzhiyun * so do not bother to clear it.
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * If PCID is on, ASID-aware code paths put the ASID+1 into the
126*4882a593Smuzhiyun * PCID bits. This serves two purposes. It prevents a nasty
127*4882a593Smuzhiyun * situation in which PCID-unaware code saves CR3, loads some other
128*4882a593Smuzhiyun * value (with PCID == 0), and then restores CR3, thus corrupting
129*4882a593Smuzhiyun * the TLB for ASID 0 if the saved ASID was nonzero. It also means
130*4882a593Smuzhiyun * that any bugs involving loading a PCID-enabled CR3 with
131*4882a593Smuzhiyun * CR4.PCIDE off will trigger deterministically.
132*4882a593Smuzhiyun */
133*4882a593Smuzhiyun return asid + 1;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * Given @asid, compute uPCID
138*4882a593Smuzhiyun */
user_pcid(u16 asid)139*4882a593Smuzhiyun static inline u16 user_pcid(u16 asid)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun u16 ret = kern_pcid(asid);
142*4882a593Smuzhiyun #ifdef CONFIG_PAGE_TABLE_ISOLATION
143*4882a593Smuzhiyun ret |= 1 << X86_CR3_PTI_PCID_USER_BIT;
144*4882a593Smuzhiyun #endif
145*4882a593Smuzhiyun return ret;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
build_cr3(pgd_t * pgd,u16 asid)148*4882a593Smuzhiyun static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun if (static_cpu_has(X86_FEATURE_PCID)) {
151*4882a593Smuzhiyun return __sme_pa(pgd) | kern_pcid(asid);
152*4882a593Smuzhiyun } else {
153*4882a593Smuzhiyun VM_WARN_ON_ONCE(asid != 0);
154*4882a593Smuzhiyun return __sme_pa(pgd);
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
build_cr3_noflush(pgd_t * pgd,u16 asid)158*4882a593Smuzhiyun static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Use boot_cpu_has() instead of this_cpu_has() as this function
163*4882a593Smuzhiyun * might be called during early boot. This should work even after
164*4882a593Smuzhiyun * boot because all CPU's the have same capabilities:
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun VM_WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_PCID));
167*4882a593Smuzhiyun return __sme_pa(pgd) | kern_pcid(asid) | CR3_NOFLUSH;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * We get here when we do something requiring a TLB invalidation
172*4882a593Smuzhiyun * but could not go invalidate all of the contexts. We do the
173*4882a593Smuzhiyun * necessary invalidation by clearing out the 'ctx_id' which
174*4882a593Smuzhiyun * forces a TLB flush when the context is loaded.
175*4882a593Smuzhiyun */
clear_asid_other(void)176*4882a593Smuzhiyun static void clear_asid_other(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun u16 asid;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * This is only expected to be set if we have disabled
182*4882a593Smuzhiyun * kernel _PAGE_GLOBAL pages.
183*4882a593Smuzhiyun */
184*4882a593Smuzhiyun if (!static_cpu_has(X86_FEATURE_PTI)) {
185*4882a593Smuzhiyun WARN_ON_ONCE(1);
186*4882a593Smuzhiyun return;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun for (asid = 0; asid < TLB_NR_DYN_ASIDS; asid++) {
190*4882a593Smuzhiyun /* Do not need to flush the current asid */
191*4882a593Smuzhiyun if (asid == this_cpu_read(cpu_tlbstate.loaded_mm_asid))
192*4882a593Smuzhiyun continue;
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * Make sure the next time we go to switch to
195*4882a593Smuzhiyun * this asid, we do a flush:
196*4882a593Smuzhiyun */
197*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[asid].ctx_id, 0);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.invalidate_other, false);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun
choose_new_asid(struct mm_struct * next,u64 next_tlb_gen,u16 * new_asid,bool * need_flush)205*4882a593Smuzhiyun static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen,
206*4882a593Smuzhiyun u16 *new_asid, bool *need_flush)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun u16 asid;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (!static_cpu_has(X86_FEATURE_PCID)) {
211*4882a593Smuzhiyun *new_asid = 0;
212*4882a593Smuzhiyun *need_flush = true;
213*4882a593Smuzhiyun return;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.invalidate_other))
217*4882a593Smuzhiyun clear_asid_other();
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun for (asid = 0; asid < TLB_NR_DYN_ASIDS; asid++) {
220*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.ctxs[asid].ctx_id) !=
221*4882a593Smuzhiyun next->context.ctx_id)
222*4882a593Smuzhiyun continue;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun *new_asid = asid;
225*4882a593Smuzhiyun *need_flush = (this_cpu_read(cpu_tlbstate.ctxs[asid].tlb_gen) <
226*4882a593Smuzhiyun next_tlb_gen);
227*4882a593Smuzhiyun return;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * We don't currently own an ASID slot on this CPU.
232*4882a593Smuzhiyun * Allocate a slot.
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun *new_asid = this_cpu_add_return(cpu_tlbstate.next_asid, 1) - 1;
235*4882a593Smuzhiyun if (*new_asid >= TLB_NR_DYN_ASIDS) {
236*4882a593Smuzhiyun *new_asid = 0;
237*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.next_asid, 1);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun *need_flush = true;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /*
243*4882a593Smuzhiyun * Given an ASID, flush the corresponding user ASID. We can delay this
244*4882a593Smuzhiyun * until the next time we switch to it.
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * See SWITCH_TO_USER_CR3.
247*4882a593Smuzhiyun */
invalidate_user_asid(u16 asid)248*4882a593Smuzhiyun static inline void invalidate_user_asid(u16 asid)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun /* There is no user ASID if address space separation is off */
251*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
252*4882a593Smuzhiyun return;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun * We only have a single ASID if PCID is off and the CR3
256*4882a593Smuzhiyun * write will have flushed it.
257*4882a593Smuzhiyun */
258*4882a593Smuzhiyun if (!cpu_feature_enabled(X86_FEATURE_PCID))
259*4882a593Smuzhiyun return;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (!static_cpu_has(X86_FEATURE_PTI))
262*4882a593Smuzhiyun return;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun __set_bit(kern_pcid(asid),
265*4882a593Smuzhiyun (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask));
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
load_new_mm_cr3(pgd_t * pgdir,u16 new_asid,bool need_flush)268*4882a593Smuzhiyun static void load_new_mm_cr3(pgd_t *pgdir, u16 new_asid, bool need_flush)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun unsigned long new_mm_cr3;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if (need_flush) {
273*4882a593Smuzhiyun invalidate_user_asid(new_asid);
274*4882a593Smuzhiyun new_mm_cr3 = build_cr3(pgdir, new_asid);
275*4882a593Smuzhiyun } else {
276*4882a593Smuzhiyun new_mm_cr3 = build_cr3_noflush(pgdir, new_asid);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * Caution: many callers of this function expect
281*4882a593Smuzhiyun * that load_cr3() is serializing and orders TLB
282*4882a593Smuzhiyun * fills with respect to the mm_cpumask writes.
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun write_cr3(new_mm_cr3);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
leave_mm(int cpu)287*4882a593Smuzhiyun void leave_mm(int cpu)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * It's plausible that we're in lazy TLB mode while our mm is init_mm.
293*4882a593Smuzhiyun * If so, our callers still expect us to flush the TLB, but there
294*4882a593Smuzhiyun * aren't any user TLB entries in init_mm to worry about.
295*4882a593Smuzhiyun *
296*4882a593Smuzhiyun * This needs to happen before any other sanity checks due to
297*4882a593Smuzhiyun * intel_idle's shenanigans.
298*4882a593Smuzhiyun */
299*4882a593Smuzhiyun if (loaded_mm == &init_mm)
300*4882a593Smuzhiyun return;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* Warn if we're not lazy. */
303*4882a593Smuzhiyun WARN_ON(!this_cpu_read(cpu_tlbstate.is_lazy));
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun switch_mm(NULL, &init_mm, NULL);
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(leave_mm);
308*4882a593Smuzhiyun
switch_mm(struct mm_struct * prev,struct mm_struct * next,struct task_struct * tsk)309*4882a593Smuzhiyun void switch_mm(struct mm_struct *prev, struct mm_struct *next,
310*4882a593Smuzhiyun struct task_struct *tsk)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun unsigned long flags;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun local_irq_save(flags);
315*4882a593Smuzhiyun switch_mm_irqs_off(prev, next, tsk);
316*4882a593Smuzhiyun local_irq_restore(flags);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
mm_mangle_tif_spec_ib(struct task_struct * next)319*4882a593Smuzhiyun static inline unsigned long mm_mangle_tif_spec_ib(struct task_struct *next)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun unsigned long next_tif = task_thread_info(next)->flags;
322*4882a593Smuzhiyun unsigned long ibpb = (next_tif >> TIF_SPEC_IB) & LAST_USER_MM_IBPB;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun return (unsigned long)next->mm | ibpb;
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
cond_ibpb(struct task_struct * next)327*4882a593Smuzhiyun static void cond_ibpb(struct task_struct *next)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun if (!next || !next->mm)
330*4882a593Smuzhiyun return;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /*
333*4882a593Smuzhiyun * Both, the conditional and the always IBPB mode use the mm
334*4882a593Smuzhiyun * pointer to avoid the IBPB when switching between tasks of the
335*4882a593Smuzhiyun * same process. Using the mm pointer instead of mm->context.ctx_id
336*4882a593Smuzhiyun * opens a hypothetical hole vs. mm_struct reuse, which is more or
337*4882a593Smuzhiyun * less impossible to control by an attacker. Aside of that it
338*4882a593Smuzhiyun * would only affect the first schedule so the theoretically
339*4882a593Smuzhiyun * exposed data is not really interesting.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun if (static_branch_likely(&switch_mm_cond_ibpb)) {
342*4882a593Smuzhiyun unsigned long prev_mm, next_mm;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /*
345*4882a593Smuzhiyun * This is a bit more complex than the always mode because
346*4882a593Smuzhiyun * it has to handle two cases:
347*4882a593Smuzhiyun *
348*4882a593Smuzhiyun * 1) Switch from a user space task (potential attacker)
349*4882a593Smuzhiyun * which has TIF_SPEC_IB set to a user space task
350*4882a593Smuzhiyun * (potential victim) which has TIF_SPEC_IB not set.
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * 2) Switch from a user space task (potential attacker)
353*4882a593Smuzhiyun * which has TIF_SPEC_IB not set to a user space task
354*4882a593Smuzhiyun * (potential victim) which has TIF_SPEC_IB set.
355*4882a593Smuzhiyun *
356*4882a593Smuzhiyun * This could be done by unconditionally issuing IBPB when
357*4882a593Smuzhiyun * a task which has TIF_SPEC_IB set is either scheduled in
358*4882a593Smuzhiyun * or out. Though that results in two flushes when:
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * - the same user space task is scheduled out and later
361*4882a593Smuzhiyun * scheduled in again and only a kernel thread ran in
362*4882a593Smuzhiyun * between.
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * - a user space task belonging to the same process is
365*4882a593Smuzhiyun * scheduled in after a kernel thread ran in between
366*4882a593Smuzhiyun *
367*4882a593Smuzhiyun * - a user space task belonging to the same process is
368*4882a593Smuzhiyun * scheduled in immediately.
369*4882a593Smuzhiyun *
370*4882a593Smuzhiyun * Optimize this with reasonably small overhead for the
371*4882a593Smuzhiyun * above cases. Mangle the TIF_SPEC_IB bit into the mm
372*4882a593Smuzhiyun * pointer of the incoming task which is stored in
373*4882a593Smuzhiyun * cpu_tlbstate.last_user_mm_ibpb for comparison.
374*4882a593Smuzhiyun */
375*4882a593Smuzhiyun next_mm = mm_mangle_tif_spec_ib(next);
376*4882a593Smuzhiyun prev_mm = this_cpu_read(cpu_tlbstate.last_user_mm_ibpb);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /*
379*4882a593Smuzhiyun * Issue IBPB only if the mm's are different and one or
380*4882a593Smuzhiyun * both have the IBPB bit set.
381*4882a593Smuzhiyun */
382*4882a593Smuzhiyun if (next_mm != prev_mm &&
383*4882a593Smuzhiyun (next_mm | prev_mm) & LAST_USER_MM_IBPB)
384*4882a593Smuzhiyun indirect_branch_prediction_barrier();
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.last_user_mm_ibpb, next_mm);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun if (static_branch_unlikely(&switch_mm_always_ibpb)) {
390*4882a593Smuzhiyun /*
391*4882a593Smuzhiyun * Only flush when switching to a user space task with a
392*4882a593Smuzhiyun * different context than the user space task which ran
393*4882a593Smuzhiyun * last on this CPU.
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.last_user_mm) != next->mm) {
396*4882a593Smuzhiyun indirect_branch_prediction_barrier();
397*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.last_user_mm, next->mm);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun #ifdef CONFIG_PERF_EVENTS
cr4_update_pce_mm(struct mm_struct * mm)403*4882a593Smuzhiyun static inline void cr4_update_pce_mm(struct mm_struct *mm)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun if (static_branch_unlikely(&rdpmc_always_available_key) ||
406*4882a593Smuzhiyun (!static_branch_unlikely(&rdpmc_never_available_key) &&
407*4882a593Smuzhiyun atomic_read(&mm->context.perf_rdpmc_allowed)))
408*4882a593Smuzhiyun cr4_set_bits_irqsoff(X86_CR4_PCE);
409*4882a593Smuzhiyun else
410*4882a593Smuzhiyun cr4_clear_bits_irqsoff(X86_CR4_PCE);
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
cr4_update_pce(void * ignored)413*4882a593Smuzhiyun void cr4_update_pce(void *ignored)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun cr4_update_pce_mm(this_cpu_read(cpu_tlbstate.loaded_mm));
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun #else
cr4_update_pce_mm(struct mm_struct * mm)419*4882a593Smuzhiyun static inline void cr4_update_pce_mm(struct mm_struct *mm) { }
420*4882a593Smuzhiyun #endif
421*4882a593Smuzhiyun
switch_mm_irqs_off(struct mm_struct * prev,struct mm_struct * next,struct task_struct * tsk)422*4882a593Smuzhiyun void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
423*4882a593Smuzhiyun struct task_struct *tsk)
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm);
426*4882a593Smuzhiyun u16 prev_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
427*4882a593Smuzhiyun bool was_lazy = this_cpu_read(cpu_tlbstate.is_lazy);
428*4882a593Smuzhiyun unsigned cpu = smp_processor_id();
429*4882a593Smuzhiyun u64 next_tlb_gen;
430*4882a593Smuzhiyun bool need_flush;
431*4882a593Smuzhiyun u16 new_asid;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * NB: The scheduler will call us with prev == next when switching
435*4882a593Smuzhiyun * from lazy TLB mode to normal mode if active_mm isn't changing.
436*4882a593Smuzhiyun * When this happens, we don't assume that CR3 (and hence
437*4882a593Smuzhiyun * cpu_tlbstate.loaded_mm) matches next.
438*4882a593Smuzhiyun *
439*4882a593Smuzhiyun * NB: leave_mm() calls us with prev == NULL and tsk == NULL.
440*4882a593Smuzhiyun */
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun /* We don't want flush_tlb_func_* to run concurrently with us. */
443*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_PROVE_LOCKING))
444*4882a593Smuzhiyun WARN_ON_ONCE(!irqs_disabled());
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun * Verify that CR3 is what we think it is. This will catch
448*4882a593Smuzhiyun * hypothetical buggy code that directly switches to swapper_pg_dir
449*4882a593Smuzhiyun * without going through leave_mm() / switch_mm_irqs_off() or that
450*4882a593Smuzhiyun * does something like write_cr3(read_cr3_pa()).
451*4882a593Smuzhiyun *
452*4882a593Smuzhiyun * Only do this check if CONFIG_DEBUG_VM=y because __read_cr3()
453*4882a593Smuzhiyun * isn't free.
454*4882a593Smuzhiyun */
455*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_VM
456*4882a593Smuzhiyun if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
457*4882a593Smuzhiyun /*
458*4882a593Smuzhiyun * If we were to BUG here, we'd be very likely to kill
459*4882a593Smuzhiyun * the system so hard that we don't see the call trace.
460*4882a593Smuzhiyun * Try to recover instead by ignoring the error and doing
461*4882a593Smuzhiyun * a global flush to minimize the chance of corruption.
462*4882a593Smuzhiyun *
463*4882a593Smuzhiyun * (This is far from being a fully correct recovery.
464*4882a593Smuzhiyun * Architecturally, the CPU could prefetch something
465*4882a593Smuzhiyun * back into an incorrect ASID slot and leave it there
466*4882a593Smuzhiyun * to cause trouble down the road. It's better than
467*4882a593Smuzhiyun * nothing, though.)
468*4882a593Smuzhiyun */
469*4882a593Smuzhiyun __flush_tlb_all();
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun #endif
472*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.is_lazy, false);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun * The membarrier system call requires a full memory barrier and
476*4882a593Smuzhiyun * core serialization before returning to user-space, after
477*4882a593Smuzhiyun * storing to rq->curr, when changing mm. This is because
478*4882a593Smuzhiyun * membarrier() sends IPIs to all CPUs that are in the target mm
479*4882a593Smuzhiyun * to make them issue memory barriers. However, if another CPU
480*4882a593Smuzhiyun * switches to/from the target mm concurrently with
481*4882a593Smuzhiyun * membarrier(), it can cause that CPU not to receive an IPI
482*4882a593Smuzhiyun * when it really should issue a memory barrier. Writing to CR3
483*4882a593Smuzhiyun * provides that full memory barrier and core serializing
484*4882a593Smuzhiyun * instruction.
485*4882a593Smuzhiyun */
486*4882a593Smuzhiyun if (real_prev == next) {
487*4882a593Smuzhiyun VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) !=
488*4882a593Smuzhiyun next->context.ctx_id);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * Even in lazy TLB mode, the CPU should stay set in the
492*4882a593Smuzhiyun * mm_cpumask. The TLB shootdown code can figure out from
493*4882a593Smuzhiyun * from cpu_tlbstate.is_lazy whether or not to send an IPI.
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun if (WARN_ON_ONCE(real_prev != &init_mm &&
496*4882a593Smuzhiyun !cpumask_test_cpu(cpu, mm_cpumask(next))))
497*4882a593Smuzhiyun cpumask_set_cpu(cpu, mm_cpumask(next));
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /*
500*4882a593Smuzhiyun * If the CPU is not in lazy TLB mode, we are just switching
501*4882a593Smuzhiyun * from one thread in a process to another thread in the same
502*4882a593Smuzhiyun * process. No TLB flush required.
503*4882a593Smuzhiyun */
504*4882a593Smuzhiyun if (!was_lazy)
505*4882a593Smuzhiyun return;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /*
508*4882a593Smuzhiyun * Read the tlb_gen to check whether a flush is needed.
509*4882a593Smuzhiyun * If the TLB is up to date, just use it.
510*4882a593Smuzhiyun * The barrier synchronizes with the tlb_gen increment in
511*4882a593Smuzhiyun * the TLB shootdown code.
512*4882a593Smuzhiyun */
513*4882a593Smuzhiyun smp_mb();
514*4882a593Smuzhiyun next_tlb_gen = atomic64_read(&next->context.tlb_gen);
515*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.ctxs[prev_asid].tlb_gen) ==
516*4882a593Smuzhiyun next_tlb_gen)
517*4882a593Smuzhiyun return;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun * TLB contents went out of date while we were in lazy
521*4882a593Smuzhiyun * mode. Fall through to the TLB switching code below.
522*4882a593Smuzhiyun */
523*4882a593Smuzhiyun new_asid = prev_asid;
524*4882a593Smuzhiyun need_flush = true;
525*4882a593Smuzhiyun } else {
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * Avoid user/user BTB poisoning by flushing the branch
528*4882a593Smuzhiyun * predictor when switching between processes. This stops
529*4882a593Smuzhiyun * one process from doing Spectre-v2 attacks on another.
530*4882a593Smuzhiyun */
531*4882a593Smuzhiyun cond_ibpb(tsk);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /*
534*4882a593Smuzhiyun * Stop remote flushes for the previous mm.
535*4882a593Smuzhiyun * Skip kernel threads; we never send init_mm TLB flushing IPIs,
536*4882a593Smuzhiyun * but the bitmap manipulation can cause cache line contention.
537*4882a593Smuzhiyun */
538*4882a593Smuzhiyun if (real_prev != &init_mm) {
539*4882a593Smuzhiyun VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu,
540*4882a593Smuzhiyun mm_cpumask(real_prev)));
541*4882a593Smuzhiyun cpumask_clear_cpu(cpu, mm_cpumask(real_prev));
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /*
545*4882a593Smuzhiyun * Start remote flushes and then read tlb_gen.
546*4882a593Smuzhiyun */
547*4882a593Smuzhiyun if (next != &init_mm)
548*4882a593Smuzhiyun cpumask_set_cpu(cpu, mm_cpumask(next));
549*4882a593Smuzhiyun next_tlb_gen = atomic64_read(&next->context.tlb_gen);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun /* Let nmi_uaccess_okay() know that we're changing CR3. */
554*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING);
555*4882a593Smuzhiyun barrier();
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun if (need_flush) {
559*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
560*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
561*4882a593Smuzhiyun load_new_mm_cr3(next->pgd, new_asid, true);
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
564*4882a593Smuzhiyun } else {
565*4882a593Smuzhiyun /* The new ASID is already up to date. */
566*4882a593Smuzhiyun load_new_mm_cr3(next->pgd, new_asid, false);
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, 0);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /* Make sure we write CR3 before loaded_mm. */
572*4882a593Smuzhiyun barrier();
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.loaded_mm, next);
575*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.loaded_mm_asid, new_asid);
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun if (next != real_prev) {
578*4882a593Smuzhiyun cr4_update_pce_mm(next);
579*4882a593Smuzhiyun switch_ldt(real_prev, next);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /*
584*4882a593Smuzhiyun * Please ignore the name of this function. It should be called
585*4882a593Smuzhiyun * switch_to_kernel_thread().
586*4882a593Smuzhiyun *
587*4882a593Smuzhiyun * enter_lazy_tlb() is a hint from the scheduler that we are entering a
588*4882a593Smuzhiyun * kernel thread or other context without an mm. Acceptable implementations
589*4882a593Smuzhiyun * include doing nothing whatsoever, switching to init_mm, or various clever
590*4882a593Smuzhiyun * lazy tricks to try to minimize TLB flushes.
591*4882a593Smuzhiyun *
592*4882a593Smuzhiyun * The scheduler reserves the right to call enter_lazy_tlb() several times
593*4882a593Smuzhiyun * in a row. It will notify us that we're going back to a real mm by
594*4882a593Smuzhiyun * calling switch_mm_irqs_off().
595*4882a593Smuzhiyun */
enter_lazy_tlb(struct mm_struct * mm,struct task_struct * tsk)596*4882a593Smuzhiyun void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm)
599*4882a593Smuzhiyun return;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.is_lazy, true);
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun /*
605*4882a593Smuzhiyun * Call this when reinitializing a CPU. It fixes the following potential
606*4882a593Smuzhiyun * problems:
607*4882a593Smuzhiyun *
608*4882a593Smuzhiyun * - The ASID changed from what cpu_tlbstate thinks it is (most likely
609*4882a593Smuzhiyun * because the CPU was taken down and came back up with CR3's PCID
610*4882a593Smuzhiyun * bits clear. CPU hotplug can do this.
611*4882a593Smuzhiyun *
612*4882a593Smuzhiyun * - The TLB contains junk in slots corresponding to inactive ASIDs.
613*4882a593Smuzhiyun *
614*4882a593Smuzhiyun * - The CPU went so far out to lunch that it may have missed a TLB
615*4882a593Smuzhiyun * flush.
616*4882a593Smuzhiyun */
initialize_tlbstate_and_flush(void)617*4882a593Smuzhiyun void initialize_tlbstate_and_flush(void)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun int i;
620*4882a593Smuzhiyun struct mm_struct *mm = this_cpu_read(cpu_tlbstate.loaded_mm);
621*4882a593Smuzhiyun u64 tlb_gen = atomic64_read(&init_mm.context.tlb_gen);
622*4882a593Smuzhiyun unsigned long cr3 = __read_cr3();
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun /* Assert that CR3 already references the right mm. */
625*4882a593Smuzhiyun WARN_ON((cr3 & CR3_ADDR_MASK) != __pa(mm->pgd));
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /*
628*4882a593Smuzhiyun * Assert that CR4.PCIDE is set if needed. (CR4.PCIDE initialization
629*4882a593Smuzhiyun * doesn't work like other CR4 bits because it can only be set from
630*4882a593Smuzhiyun * long mode.)
631*4882a593Smuzhiyun */
632*4882a593Smuzhiyun WARN_ON(boot_cpu_has(X86_FEATURE_PCID) &&
633*4882a593Smuzhiyun !(cr4_read_shadow() & X86_CR4_PCIDE));
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun /* Force ASID 0 and force a TLB flush. */
636*4882a593Smuzhiyun write_cr3(build_cr3(mm->pgd, 0));
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /* Reinitialize tlbstate. */
639*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.last_user_mm_ibpb, LAST_USER_MM_IBPB);
640*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
641*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.next_asid, 1);
642*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, mm->context.ctx_id);
643*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, tlb_gen);
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun for (i = 1; i < TLB_NR_DYN_ASIDS; i++)
646*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[i].ctx_id, 0);
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /*
650*4882a593Smuzhiyun * flush_tlb_func_common()'s memory ordering requirement is that any
651*4882a593Smuzhiyun * TLB fills that happen after we flush the TLB are ordered after we
652*4882a593Smuzhiyun * read active_mm's tlb_gen. We don't need any explicit barriers
653*4882a593Smuzhiyun * because all x86 flush operations are serializing and the
654*4882a593Smuzhiyun * atomic64_read operation won't be reordered by the compiler.
655*4882a593Smuzhiyun */
flush_tlb_func_common(const struct flush_tlb_info * f,bool local,enum tlb_flush_reason reason)656*4882a593Smuzhiyun static void flush_tlb_func_common(const struct flush_tlb_info *f,
657*4882a593Smuzhiyun bool local, enum tlb_flush_reason reason)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun /*
660*4882a593Smuzhiyun * We have three different tlb_gen values in here. They are:
661*4882a593Smuzhiyun *
662*4882a593Smuzhiyun * - mm_tlb_gen: the latest generation.
663*4882a593Smuzhiyun * - local_tlb_gen: the generation that this CPU has already caught
664*4882a593Smuzhiyun * up to.
665*4882a593Smuzhiyun * - f->new_tlb_gen: the generation that the requester of the flush
666*4882a593Smuzhiyun * wants us to catch up to.
667*4882a593Smuzhiyun */
668*4882a593Smuzhiyun struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
669*4882a593Smuzhiyun u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
670*4882a593Smuzhiyun u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
671*4882a593Smuzhiyun u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun /* This code cannot presently handle being reentered. */
674*4882a593Smuzhiyun VM_WARN_ON(!irqs_disabled());
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun if (unlikely(loaded_mm == &init_mm))
677*4882a593Smuzhiyun return;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) !=
680*4882a593Smuzhiyun loaded_mm->context.ctx_id);
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun if (this_cpu_read(cpu_tlbstate.is_lazy)) {
683*4882a593Smuzhiyun /*
684*4882a593Smuzhiyun * We're in lazy mode. We need to at least flush our
685*4882a593Smuzhiyun * paging-structure cache to avoid speculatively reading
686*4882a593Smuzhiyun * garbage into our TLB. Since switching to init_mm is barely
687*4882a593Smuzhiyun * slower than a minimal flush, just switch to init_mm.
688*4882a593Smuzhiyun *
689*4882a593Smuzhiyun * This should be rare, with native_flush_tlb_others skipping
690*4882a593Smuzhiyun * IPIs to lazy TLB mode CPUs.
691*4882a593Smuzhiyun */
692*4882a593Smuzhiyun switch_mm_irqs_off(NULL, &init_mm, NULL);
693*4882a593Smuzhiyun return;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun if (unlikely(local_tlb_gen == mm_tlb_gen)) {
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun * There's nothing to do: we're already up to date. This can
699*4882a593Smuzhiyun * happen if two concurrent flushes happen -- the first flush to
700*4882a593Smuzhiyun * be handled can catch us all the way up, leaving no work for
701*4882a593Smuzhiyun * the second flush.
702*4882a593Smuzhiyun */
703*4882a593Smuzhiyun trace_tlb_flush(reason, 0);
704*4882a593Smuzhiyun return;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen);
708*4882a593Smuzhiyun WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen);
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * If we get to this point, we know that our TLB is out of date.
712*4882a593Smuzhiyun * This does not strictly imply that we need to flush (it's
713*4882a593Smuzhiyun * possible that f->new_tlb_gen <= local_tlb_gen), but we're
714*4882a593Smuzhiyun * going to need to flush in the very near future, so we might
715*4882a593Smuzhiyun * as well get it over with.
716*4882a593Smuzhiyun *
717*4882a593Smuzhiyun * The only question is whether to do a full or partial flush.
718*4882a593Smuzhiyun *
719*4882a593Smuzhiyun * We do a partial flush if requested and two extra conditions
720*4882a593Smuzhiyun * are met:
721*4882a593Smuzhiyun *
722*4882a593Smuzhiyun * 1. f->new_tlb_gen == local_tlb_gen + 1. We have an invariant that
723*4882a593Smuzhiyun * we've always done all needed flushes to catch up to
724*4882a593Smuzhiyun * local_tlb_gen. If, for example, local_tlb_gen == 2 and
725*4882a593Smuzhiyun * f->new_tlb_gen == 3, then we know that the flush needed to bring
726*4882a593Smuzhiyun * us up to date for tlb_gen 3 is the partial flush we're
727*4882a593Smuzhiyun * processing.
728*4882a593Smuzhiyun *
729*4882a593Smuzhiyun * As an example of why this check is needed, suppose that there
730*4882a593Smuzhiyun * are two concurrent flushes. The first is a full flush that
731*4882a593Smuzhiyun * changes context.tlb_gen from 1 to 2. The second is a partial
732*4882a593Smuzhiyun * flush that changes context.tlb_gen from 2 to 3. If they get
733*4882a593Smuzhiyun * processed on this CPU in reverse order, we'll see
734*4882a593Smuzhiyun * local_tlb_gen == 1, mm_tlb_gen == 3, and end != TLB_FLUSH_ALL.
735*4882a593Smuzhiyun * If we were to use __flush_tlb_one_user() and set local_tlb_gen to
736*4882a593Smuzhiyun * 3, we'd be break the invariant: we'd update local_tlb_gen above
737*4882a593Smuzhiyun * 1 without the full flush that's needed for tlb_gen 2.
738*4882a593Smuzhiyun *
739*4882a593Smuzhiyun * 2. f->new_tlb_gen == mm_tlb_gen. This is purely an optimiation.
740*4882a593Smuzhiyun * Partial TLB flushes are not all that much cheaper than full TLB
741*4882a593Smuzhiyun * flushes, so it seems unlikely that it would be a performance win
742*4882a593Smuzhiyun * to do a partial flush if that won't bring our TLB fully up to
743*4882a593Smuzhiyun * date. By doing a full flush instead, we can increase
744*4882a593Smuzhiyun * local_tlb_gen all the way to mm_tlb_gen and we can probably
745*4882a593Smuzhiyun * avoid another flush in the very near future.
746*4882a593Smuzhiyun */
747*4882a593Smuzhiyun if (f->end != TLB_FLUSH_ALL &&
748*4882a593Smuzhiyun f->new_tlb_gen == local_tlb_gen + 1 &&
749*4882a593Smuzhiyun f->new_tlb_gen == mm_tlb_gen) {
750*4882a593Smuzhiyun /* Partial flush */
751*4882a593Smuzhiyun unsigned long nr_invalidate = (f->end - f->start) >> f->stride_shift;
752*4882a593Smuzhiyun unsigned long addr = f->start;
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun while (addr < f->end) {
755*4882a593Smuzhiyun flush_tlb_one_user(addr);
756*4882a593Smuzhiyun addr += 1UL << f->stride_shift;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun if (local)
759*4882a593Smuzhiyun count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_invalidate);
760*4882a593Smuzhiyun trace_tlb_flush(reason, nr_invalidate);
761*4882a593Smuzhiyun } else {
762*4882a593Smuzhiyun /* Full flush. */
763*4882a593Smuzhiyun flush_tlb_local();
764*4882a593Smuzhiyun if (local)
765*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
766*4882a593Smuzhiyun trace_tlb_flush(reason, TLB_FLUSH_ALL);
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /* Both paths above update our state to mm_tlb_gen. */
770*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
flush_tlb_func_local(const void * info,enum tlb_flush_reason reason)773*4882a593Smuzhiyun static void flush_tlb_func_local(const void *info, enum tlb_flush_reason reason)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun const struct flush_tlb_info *f = info;
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun flush_tlb_func_common(f, true, reason);
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
flush_tlb_func_remote(void * info)780*4882a593Smuzhiyun static void flush_tlb_func_remote(void *info)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun const struct flush_tlb_info *f = info;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun inc_irq_stat(irq_tlb_count);
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.loaded_mm))
787*4882a593Smuzhiyun return;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
790*4882a593Smuzhiyun flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN);
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
tlb_is_not_lazy(int cpu,void * data)793*4882a593Smuzhiyun static bool tlb_is_not_lazy(int cpu, void *data)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun return !per_cpu(cpu_tlbstate.is_lazy, cpu);
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
native_flush_tlb_others(const struct cpumask * cpumask,const struct flush_tlb_info * info)798*4882a593Smuzhiyun STATIC_NOPV void native_flush_tlb_others(const struct cpumask *cpumask,
799*4882a593Smuzhiyun const struct flush_tlb_info *info)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
802*4882a593Smuzhiyun if (info->end == TLB_FLUSH_ALL)
803*4882a593Smuzhiyun trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
804*4882a593Smuzhiyun else
805*4882a593Smuzhiyun trace_tlb_flush(TLB_REMOTE_SEND_IPI,
806*4882a593Smuzhiyun (info->end - info->start) >> PAGE_SHIFT);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun /*
809*4882a593Smuzhiyun * If no page tables were freed, we can skip sending IPIs to
810*4882a593Smuzhiyun * CPUs in lazy TLB mode. They will flush the CPU themselves
811*4882a593Smuzhiyun * at the next context switch.
812*4882a593Smuzhiyun *
813*4882a593Smuzhiyun * However, if page tables are getting freed, we need to send the
814*4882a593Smuzhiyun * IPI everywhere, to prevent CPUs in lazy TLB mode from tripping
815*4882a593Smuzhiyun * up on the new contents of what used to be page tables, while
816*4882a593Smuzhiyun * doing a speculative memory access.
817*4882a593Smuzhiyun */
818*4882a593Smuzhiyun if (info->freed_tables)
819*4882a593Smuzhiyun smp_call_function_many(cpumask, flush_tlb_func_remote,
820*4882a593Smuzhiyun (void *)info, 1);
821*4882a593Smuzhiyun else
822*4882a593Smuzhiyun on_each_cpu_cond_mask(tlb_is_not_lazy, flush_tlb_func_remote,
823*4882a593Smuzhiyun (void *)info, 1, cpumask);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
flush_tlb_others(const struct cpumask * cpumask,const struct flush_tlb_info * info)826*4882a593Smuzhiyun void flush_tlb_others(const struct cpumask *cpumask,
827*4882a593Smuzhiyun const struct flush_tlb_info *info)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun __flush_tlb_others(cpumask, info);
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /*
833*4882a593Smuzhiyun * See Documentation/x86/tlb.rst for details. We choose 33
834*4882a593Smuzhiyun * because it is large enough to cover the vast majority (at
835*4882a593Smuzhiyun * least 95%) of allocations, and is small enough that we are
836*4882a593Smuzhiyun * confident it will not cause too much overhead. Each single
837*4882a593Smuzhiyun * flush is about 100 ns, so this caps the maximum overhead at
838*4882a593Smuzhiyun * _about_ 3,000 ns.
839*4882a593Smuzhiyun *
840*4882a593Smuzhiyun * This is in units of pages.
841*4882a593Smuzhiyun */
842*4882a593Smuzhiyun unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun static DEFINE_PER_CPU_SHARED_ALIGNED(struct flush_tlb_info, flush_tlb_info);
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_VM
847*4882a593Smuzhiyun static DEFINE_PER_CPU(unsigned int, flush_tlb_info_idx);
848*4882a593Smuzhiyun #endif
849*4882a593Smuzhiyun
get_flush_tlb_info(struct mm_struct * mm,unsigned long start,unsigned long end,unsigned int stride_shift,bool freed_tables,u64 new_tlb_gen)850*4882a593Smuzhiyun static inline struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm,
851*4882a593Smuzhiyun unsigned long start, unsigned long end,
852*4882a593Smuzhiyun unsigned int stride_shift, bool freed_tables,
853*4882a593Smuzhiyun u64 new_tlb_gen)
854*4882a593Smuzhiyun {
855*4882a593Smuzhiyun struct flush_tlb_info *info = this_cpu_ptr(&flush_tlb_info);
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_VM
858*4882a593Smuzhiyun /*
859*4882a593Smuzhiyun * Ensure that the following code is non-reentrant and flush_tlb_info
860*4882a593Smuzhiyun * is not overwritten. This means no TLB flushing is initiated by
861*4882a593Smuzhiyun * interrupt handlers and machine-check exception handlers.
862*4882a593Smuzhiyun */
863*4882a593Smuzhiyun BUG_ON(this_cpu_inc_return(flush_tlb_info_idx) != 1);
864*4882a593Smuzhiyun #endif
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun info->start = start;
867*4882a593Smuzhiyun info->end = end;
868*4882a593Smuzhiyun info->mm = mm;
869*4882a593Smuzhiyun info->stride_shift = stride_shift;
870*4882a593Smuzhiyun info->freed_tables = freed_tables;
871*4882a593Smuzhiyun info->new_tlb_gen = new_tlb_gen;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun return info;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun
put_flush_tlb_info(void)876*4882a593Smuzhiyun static inline void put_flush_tlb_info(void)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_VM
879*4882a593Smuzhiyun /* Complete reentrency prevention checks */
880*4882a593Smuzhiyun barrier();
881*4882a593Smuzhiyun this_cpu_dec(flush_tlb_info_idx);
882*4882a593Smuzhiyun #endif
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
flush_tlb_mm_range(struct mm_struct * mm,unsigned long start,unsigned long end,unsigned int stride_shift,bool freed_tables)885*4882a593Smuzhiyun void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
886*4882a593Smuzhiyun unsigned long end, unsigned int stride_shift,
887*4882a593Smuzhiyun bool freed_tables)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun struct flush_tlb_info *info;
890*4882a593Smuzhiyun u64 new_tlb_gen;
891*4882a593Smuzhiyun int cpu;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun cpu = get_cpu();
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /* Should we flush just the requested range? */
896*4882a593Smuzhiyun if ((end == TLB_FLUSH_ALL) ||
897*4882a593Smuzhiyun ((end - start) >> stride_shift) > tlb_single_page_flush_ceiling) {
898*4882a593Smuzhiyun start = 0;
899*4882a593Smuzhiyun end = TLB_FLUSH_ALL;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /* This is also a barrier that synchronizes with switch_mm(). */
903*4882a593Smuzhiyun new_tlb_gen = inc_mm_tlb_gen(mm);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun info = get_flush_tlb_info(mm, start, end, stride_shift, freed_tables,
906*4882a593Smuzhiyun new_tlb_gen);
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) {
909*4882a593Smuzhiyun lockdep_assert_irqs_enabled();
910*4882a593Smuzhiyun local_irq_disable();
911*4882a593Smuzhiyun flush_tlb_func_local(info, TLB_LOCAL_MM_SHOOTDOWN);
912*4882a593Smuzhiyun local_irq_enable();
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids)
916*4882a593Smuzhiyun flush_tlb_others(mm_cpumask(mm), info);
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun put_flush_tlb_info();
919*4882a593Smuzhiyun put_cpu();
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun
do_flush_tlb_all(void * info)923*4882a593Smuzhiyun static void do_flush_tlb_all(void *info)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
926*4882a593Smuzhiyun __flush_tlb_all();
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun
flush_tlb_all(void)929*4882a593Smuzhiyun void flush_tlb_all(void)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
932*4882a593Smuzhiyun on_each_cpu(do_flush_tlb_all, NULL, 1);
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
do_kernel_range_flush(void * info)935*4882a593Smuzhiyun static void do_kernel_range_flush(void *info)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun struct flush_tlb_info *f = info;
938*4882a593Smuzhiyun unsigned long addr;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun /* flush range by one by one 'invlpg' */
941*4882a593Smuzhiyun for (addr = f->start; addr < f->end; addr += PAGE_SIZE)
942*4882a593Smuzhiyun flush_tlb_one_kernel(addr);
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
flush_tlb_kernel_range(unsigned long start,unsigned long end)945*4882a593Smuzhiyun void flush_tlb_kernel_range(unsigned long start, unsigned long end)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun /* Balance as user space task's flush, a bit conservative */
948*4882a593Smuzhiyun if (end == TLB_FLUSH_ALL ||
949*4882a593Smuzhiyun (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
950*4882a593Smuzhiyun on_each_cpu(do_flush_tlb_all, NULL, 1);
951*4882a593Smuzhiyun } else {
952*4882a593Smuzhiyun struct flush_tlb_info *info;
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun preempt_disable();
955*4882a593Smuzhiyun info = get_flush_tlb_info(NULL, start, end, 0, false, 0);
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun on_each_cpu(do_kernel_range_flush, info, 1);
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun put_flush_tlb_info();
960*4882a593Smuzhiyun preempt_enable();
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun /*
965*4882a593Smuzhiyun * This can be used from process context to figure out what the value of
966*4882a593Smuzhiyun * CR3 is without needing to do a (slow) __read_cr3().
967*4882a593Smuzhiyun *
968*4882a593Smuzhiyun * It's intended to be used for code like KVM that sneakily changes CR3
969*4882a593Smuzhiyun * and needs to restore it. It needs to be used very carefully.
970*4882a593Smuzhiyun */
__get_current_cr3_fast(void)971*4882a593Smuzhiyun unsigned long __get_current_cr3_fast(void)
972*4882a593Smuzhiyun {
973*4882a593Smuzhiyun unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd,
974*4882a593Smuzhiyun this_cpu_read(cpu_tlbstate.loaded_mm_asid));
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun /* For now, be very restrictive about when this can be called. */
977*4882a593Smuzhiyun VM_WARN_ON(in_nmi() || preemptible());
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun VM_BUG_ON(cr3 != __read_cr3());
980*4882a593Smuzhiyun return cr3;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__get_current_cr3_fast);
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun /*
985*4882a593Smuzhiyun * Flush one page in the kernel mapping
986*4882a593Smuzhiyun */
flush_tlb_one_kernel(unsigned long addr)987*4882a593Smuzhiyun void flush_tlb_one_kernel(unsigned long addr)
988*4882a593Smuzhiyun {
989*4882a593Smuzhiyun count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun /*
992*4882a593Smuzhiyun * If PTI is off, then __flush_tlb_one_user() is just INVLPG or its
993*4882a593Smuzhiyun * paravirt equivalent. Even with PCID, this is sufficient: we only
994*4882a593Smuzhiyun * use PCID if we also use global PTEs for the kernel mapping, and
995*4882a593Smuzhiyun * INVLPG flushes global translations across all address spaces.
996*4882a593Smuzhiyun *
997*4882a593Smuzhiyun * If PTI is on, then the kernel is mapped with non-global PTEs, and
998*4882a593Smuzhiyun * __flush_tlb_one_user() will flush the given address for the current
999*4882a593Smuzhiyun * kernel address space and for its usermode counterpart, but it does
1000*4882a593Smuzhiyun * not flush it for other address spaces.
1001*4882a593Smuzhiyun */
1002*4882a593Smuzhiyun flush_tlb_one_user(addr);
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun if (!static_cpu_has(X86_FEATURE_PTI))
1005*4882a593Smuzhiyun return;
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun /*
1008*4882a593Smuzhiyun * See above. We need to propagate the flush to all other address
1009*4882a593Smuzhiyun * spaces. In principle, we only need to propagate it to kernelmode
1010*4882a593Smuzhiyun * address spaces, but the extra bookkeeping we would need is not
1011*4882a593Smuzhiyun * worth it.
1012*4882a593Smuzhiyun */
1013*4882a593Smuzhiyun this_cpu_write(cpu_tlbstate.invalidate_other, true);
1014*4882a593Smuzhiyun }
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun /*
1017*4882a593Smuzhiyun * Flush one page in the user mapping
1018*4882a593Smuzhiyun */
native_flush_tlb_one_user(unsigned long addr)1019*4882a593Smuzhiyun STATIC_NOPV void native_flush_tlb_one_user(unsigned long addr)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun if (!static_cpu_has(X86_FEATURE_PTI))
1026*4882a593Smuzhiyun return;
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun /*
1029*4882a593Smuzhiyun * Some platforms #GP if we call invpcid(type=1/2) before CR4.PCIDE=1.
1030*4882a593Smuzhiyun * Just use invalidate_user_asid() in case we are called early.
1031*4882a593Smuzhiyun */
1032*4882a593Smuzhiyun if (!this_cpu_has(X86_FEATURE_INVPCID_SINGLE))
1033*4882a593Smuzhiyun invalidate_user_asid(loaded_mm_asid);
1034*4882a593Smuzhiyun else
1035*4882a593Smuzhiyun invpcid_flush_one(user_pcid(loaded_mm_asid), addr);
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun
flush_tlb_one_user(unsigned long addr)1038*4882a593Smuzhiyun void flush_tlb_one_user(unsigned long addr)
1039*4882a593Smuzhiyun {
1040*4882a593Smuzhiyun __flush_tlb_one_user(addr);
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun /*
1044*4882a593Smuzhiyun * Flush everything
1045*4882a593Smuzhiyun */
native_flush_tlb_global(void)1046*4882a593Smuzhiyun STATIC_NOPV void native_flush_tlb_global(void)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun unsigned long cr4, flags;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun if (static_cpu_has(X86_FEATURE_INVPCID)) {
1051*4882a593Smuzhiyun /*
1052*4882a593Smuzhiyun * Using INVPCID is considerably faster than a pair of writes
1053*4882a593Smuzhiyun * to CR4 sandwiched inside an IRQ flag save/restore.
1054*4882a593Smuzhiyun *
1055*4882a593Smuzhiyun * Note, this works with CR4.PCIDE=0 or 1.
1056*4882a593Smuzhiyun */
1057*4882a593Smuzhiyun invpcid_flush_all();
1058*4882a593Smuzhiyun return;
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun /*
1062*4882a593Smuzhiyun * Read-modify-write to CR4 - protect it from preemption and
1063*4882a593Smuzhiyun * from interrupts. (Use the raw variant because this code can
1064*4882a593Smuzhiyun * be called from deep inside debugging code.)
1065*4882a593Smuzhiyun */
1066*4882a593Smuzhiyun raw_local_irq_save(flags);
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun cr4 = this_cpu_read(cpu_tlbstate.cr4);
1069*4882a593Smuzhiyun /* toggle PGE */
1070*4882a593Smuzhiyun native_write_cr4(cr4 ^ X86_CR4_PGE);
1071*4882a593Smuzhiyun /* write old PGE again and flush TLBs */
1072*4882a593Smuzhiyun native_write_cr4(cr4);
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun raw_local_irq_restore(flags);
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun /*
1078*4882a593Smuzhiyun * Flush the entire current user mapping
1079*4882a593Smuzhiyun */
native_flush_tlb_local(void)1080*4882a593Smuzhiyun STATIC_NOPV void native_flush_tlb_local(void)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun /*
1083*4882a593Smuzhiyun * Preemption or interrupts must be disabled to protect the access
1084*4882a593Smuzhiyun * to the per CPU variable and to prevent being preempted between
1085*4882a593Smuzhiyun * read_cr3() and write_cr3().
1086*4882a593Smuzhiyun */
1087*4882a593Smuzhiyun WARN_ON_ONCE(preemptible());
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun invalidate_user_asid(this_cpu_read(cpu_tlbstate.loaded_mm_asid));
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun /* If current->mm == NULL then the read_cr3() "borrows" an mm */
1092*4882a593Smuzhiyun native_write_cr3(__native_read_cr3());
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun
flush_tlb_local(void)1095*4882a593Smuzhiyun void flush_tlb_local(void)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun __flush_tlb_local();
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun /*
1101*4882a593Smuzhiyun * Flush everything
1102*4882a593Smuzhiyun */
__flush_tlb_all(void)1103*4882a593Smuzhiyun void __flush_tlb_all(void)
1104*4882a593Smuzhiyun {
1105*4882a593Smuzhiyun /*
1106*4882a593Smuzhiyun * This is to catch users with enabled preemption and the PGE feature
1107*4882a593Smuzhiyun * and don't trigger the warning in __native_flush_tlb().
1108*4882a593Smuzhiyun */
1109*4882a593Smuzhiyun VM_WARN_ON_ONCE(preemptible());
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun if (boot_cpu_has(X86_FEATURE_PGE)) {
1112*4882a593Smuzhiyun __flush_tlb_global();
1113*4882a593Smuzhiyun } else {
1114*4882a593Smuzhiyun /*
1115*4882a593Smuzhiyun * !PGE -> !PCID (setup_pcid()), thus every flush is total.
1116*4882a593Smuzhiyun */
1117*4882a593Smuzhiyun flush_tlb_local();
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__flush_tlb_all);
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun /*
1123*4882a593Smuzhiyun * arch_tlbbatch_flush() performs a full TLB flush regardless of the active mm.
1124*4882a593Smuzhiyun * This means that the 'struct flush_tlb_info' that describes which mappings to
1125*4882a593Smuzhiyun * flush is actually fixed. We therefore set a single fixed struct and use it in
1126*4882a593Smuzhiyun * arch_tlbbatch_flush().
1127*4882a593Smuzhiyun */
1128*4882a593Smuzhiyun static const struct flush_tlb_info full_flush_tlb_info = {
1129*4882a593Smuzhiyun .mm = NULL,
1130*4882a593Smuzhiyun .start = 0,
1131*4882a593Smuzhiyun .end = TLB_FLUSH_ALL,
1132*4882a593Smuzhiyun };
1133*4882a593Smuzhiyun
arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch * batch)1134*4882a593Smuzhiyun void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
1135*4882a593Smuzhiyun {
1136*4882a593Smuzhiyun int cpu = get_cpu();
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun if (cpumask_test_cpu(cpu, &batch->cpumask)) {
1139*4882a593Smuzhiyun lockdep_assert_irqs_enabled();
1140*4882a593Smuzhiyun local_irq_disable();
1141*4882a593Smuzhiyun flush_tlb_func_local(&full_flush_tlb_info, TLB_LOCAL_SHOOTDOWN);
1142*4882a593Smuzhiyun local_irq_enable();
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
1146*4882a593Smuzhiyun flush_tlb_others(&batch->cpumask, &full_flush_tlb_info);
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun cpumask_clear(&batch->cpumask);
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun put_cpu();
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun /*
1154*4882a593Smuzhiyun * Blindly accessing user memory from NMI context can be dangerous
1155*4882a593Smuzhiyun * if we're in the middle of switching the current user task or
1156*4882a593Smuzhiyun * switching the loaded mm. It can also be dangerous if we
1157*4882a593Smuzhiyun * interrupted some kernel code that was temporarily using a
1158*4882a593Smuzhiyun * different mm.
1159*4882a593Smuzhiyun */
nmi_uaccess_okay(void)1160*4882a593Smuzhiyun bool nmi_uaccess_okay(void)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
1163*4882a593Smuzhiyun struct mm_struct *current_mm = current->mm;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun VM_WARN_ON_ONCE(!loaded_mm);
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun /*
1168*4882a593Smuzhiyun * The condition we want to check is
1169*4882a593Smuzhiyun * current_mm->pgd == __va(read_cr3_pa()). This may be slow, though,
1170*4882a593Smuzhiyun * if we're running in a VM with shadow paging, and nmi_uaccess_okay()
1171*4882a593Smuzhiyun * is supposed to be reasonably fast.
1172*4882a593Smuzhiyun *
1173*4882a593Smuzhiyun * Instead, we check the almost equivalent but somewhat conservative
1174*4882a593Smuzhiyun * condition below, and we rely on the fact that switch_mm_irqs_off()
1175*4882a593Smuzhiyun * sets loaded_mm to LOADED_MM_SWITCHING before writing to CR3.
1176*4882a593Smuzhiyun */
1177*4882a593Smuzhiyun if (loaded_mm != current_mm)
1178*4882a593Smuzhiyun return false;
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun VM_WARN_ON_ONCE(current_mm->pgd != __va(read_cr3_pa()));
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun return true;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
tlbflush_read_file(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1185*4882a593Smuzhiyun static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
1186*4882a593Smuzhiyun size_t count, loff_t *ppos)
1187*4882a593Smuzhiyun {
1188*4882a593Smuzhiyun char buf[32];
1189*4882a593Smuzhiyun unsigned int len;
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
1192*4882a593Smuzhiyun return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun
tlbflush_write_file(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1195*4882a593Smuzhiyun static ssize_t tlbflush_write_file(struct file *file,
1196*4882a593Smuzhiyun const char __user *user_buf, size_t count, loff_t *ppos)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun char buf[32];
1199*4882a593Smuzhiyun ssize_t len;
1200*4882a593Smuzhiyun int ceiling;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun len = min(count, sizeof(buf) - 1);
1203*4882a593Smuzhiyun if (copy_from_user(buf, user_buf, len))
1204*4882a593Smuzhiyun return -EFAULT;
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun buf[len] = '\0';
1207*4882a593Smuzhiyun if (kstrtoint(buf, 0, &ceiling))
1208*4882a593Smuzhiyun return -EINVAL;
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun if (ceiling < 0)
1211*4882a593Smuzhiyun return -EINVAL;
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun tlb_single_page_flush_ceiling = ceiling;
1214*4882a593Smuzhiyun return count;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun static const struct file_operations fops_tlbflush = {
1218*4882a593Smuzhiyun .read = tlbflush_read_file,
1219*4882a593Smuzhiyun .write = tlbflush_write_file,
1220*4882a593Smuzhiyun .llseek = default_llseek,
1221*4882a593Smuzhiyun };
1222*4882a593Smuzhiyun
create_tlb_single_page_flush_ceiling(void)1223*4882a593Smuzhiyun static int __init create_tlb_single_page_flush_ceiling(void)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
1226*4882a593Smuzhiyun arch_debugfs_dir, NULL, &fops_tlbflush);
1227*4882a593Smuzhiyun return 0;
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun late_initcall(create_tlb_single_page_flush_ceiling);
1230