]> asedeno.scripts.mit.edu Git - linux.git/blob - arch/x86/include/asm/tlbflush.h
x86/mm: Implement PCID based optimization: try to preserve old TLB entries using PCID
[linux.git] / arch / x86 / include / asm / tlbflush.h
1 #ifndef _ASM_X86_TLBFLUSH_H
2 #define _ASM_X86_TLBFLUSH_H
3
4 #include <linux/mm.h>
5 #include <linux/sched.h>
6
7 #include <asm/processor.h>
8 #include <asm/cpufeature.h>
9 #include <asm/special_insns.h>
10 #include <asm/smp.h>
11
12 static inline void __invpcid(unsigned long pcid, unsigned long addr,
13                              unsigned long type)
14 {
15         struct { u64 d[2]; } desc = { { pcid, addr } };
16
17         /*
18          * The memory clobber is because the whole point is to invalidate
19          * stale TLB entries and, especially if we're flushing global
20          * mappings, we don't want the compiler to reorder any subsequent
21          * memory accesses before the TLB flush.
22          *
23          * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
24          * invpcid (%rcx), %rax in long mode.
25          */
26         asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
27                       : : "m" (desc), "a" (type), "c" (&desc) : "memory");
28 }
29
30 #define INVPCID_TYPE_INDIV_ADDR         0
31 #define INVPCID_TYPE_SINGLE_CTXT        1
32 #define INVPCID_TYPE_ALL_INCL_GLOBAL    2
33 #define INVPCID_TYPE_ALL_NON_GLOBAL     3
34
35 /* Flush all mappings for a given pcid and addr, not including globals. */
36 static inline void invpcid_flush_one(unsigned long pcid,
37                                      unsigned long addr)
38 {
39         __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
40 }
41
42 /* Flush all mappings for a given PCID, not including globals. */
43 static inline void invpcid_flush_single_context(unsigned long pcid)
44 {
45         __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
46 }
47
48 /* Flush all mappings, including globals, for all PCIDs. */
49 static inline void invpcid_flush_all(void)
50 {
51         __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
52 }
53
54 /* Flush all mappings for all PCIDs except globals. */
55 static inline void invpcid_flush_all_nonglobals(void)
56 {
57         __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
58 }
59
60 static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
61 {
62         u64 new_tlb_gen;
63
64         /*
65          * Bump the generation count.  This also serves as a full barrier
66          * that synchronizes with switch_mm(): callers are required to order
67          * their read of mm_cpumask after their writes to the paging
68          * structures.
69          */
70         smp_mb__before_atomic();
71         new_tlb_gen = atomic64_inc_return(&mm->context.tlb_gen);
72         smp_mb__after_atomic();
73
74         return new_tlb_gen;
75 }
76
77 #ifdef CONFIG_PARAVIRT
78 #include <asm/paravirt.h>
79 #else
80 #define __flush_tlb() __native_flush_tlb()
81 #define __flush_tlb_global() __native_flush_tlb_global()
82 #define __flush_tlb_single(addr) __native_flush_tlb_single(addr)
83 #endif
84
85 /*
86  * 6 because 6 should be plenty and struct tlb_state will fit in
87  * two cache lines.
88  */
89 #define TLB_NR_DYN_ASIDS 6
90
91 struct tlb_context {
92         u64 ctx_id;
93         u64 tlb_gen;
94 };
95
96 struct tlb_state {
97         /*
98          * cpu_tlbstate.loaded_mm should match CR3 whenever interrupts
99          * are on.  This means that it may not match current->active_mm,
100          * which will contain the previous user mm when we're in lazy TLB
101          * mode even if we've already switched back to swapper_pg_dir.
102          */
103         struct mm_struct *loaded_mm;
104         u16 loaded_mm_asid;
105         u16 next_asid;
106
107         /*
108          * Access to this CR4 shadow and to H/W CR4 is protected by
109          * disabling interrupts when modifying either one.
110          */
111         unsigned long cr4;
112
113         /*
114          * This is a list of all contexts that might exist in the TLB.
115          * There is one per ASID that we use, and the ASID (what the
116          * CPU calls PCID) is the index into ctxts.
117          *
118          * For each context, ctx_id indicates which mm the TLB's user
119          * entries came from.  As an invariant, the TLB will never
120          * contain entries that are out-of-date as when that mm reached
121          * the tlb_gen in the list.
122          *
123          * To be clear, this means that it's legal for the TLB code to
124          * flush the TLB without updating tlb_gen.  This can happen
125          * (for now, at least) due to paravirt remote flushes.
126          *
127          * NB: context 0 is a bit special, since it's also used by
128          * various bits of init code.  This is fine -- code that
129          * isn't aware of PCID will end up harmlessly flushing
130          * context 0.
131          */
132         struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
133 };
134 DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
135
136 /* Initialize cr4 shadow for this CPU. */
137 static inline void cr4_init_shadow(void)
138 {
139         this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
140 }
141
142 /* Set in this cpu's CR4. */
143 static inline void cr4_set_bits(unsigned long mask)
144 {
145         unsigned long cr4;
146
147         cr4 = this_cpu_read(cpu_tlbstate.cr4);
148         if ((cr4 | mask) != cr4) {
149                 cr4 |= mask;
150                 this_cpu_write(cpu_tlbstate.cr4, cr4);
151                 __write_cr4(cr4);
152         }
153 }
154
155 /* Clear in this cpu's CR4. */
156 static inline void cr4_clear_bits(unsigned long mask)
157 {
158         unsigned long cr4;
159
160         cr4 = this_cpu_read(cpu_tlbstate.cr4);
161         if ((cr4 & ~mask) != cr4) {
162                 cr4 &= ~mask;
163                 this_cpu_write(cpu_tlbstate.cr4, cr4);
164                 __write_cr4(cr4);
165         }
166 }
167
168 static inline void cr4_toggle_bits(unsigned long mask)
169 {
170         unsigned long cr4;
171
172         cr4 = this_cpu_read(cpu_tlbstate.cr4);
173         cr4 ^= mask;
174         this_cpu_write(cpu_tlbstate.cr4, cr4);
175         __write_cr4(cr4);
176 }
177
178 /* Read the CR4 shadow. */
179 static inline unsigned long cr4_read_shadow(void)
180 {
181         return this_cpu_read(cpu_tlbstate.cr4);
182 }
183
184 /*
185  * Save some of cr4 feature set we're using (e.g.  Pentium 4MB
186  * enable and PPro Global page enable), so that any CPU's that boot
187  * up after us can get the correct flags.  This should only be used
188  * during boot on the boot cpu.
189  */
190 extern unsigned long mmu_cr4_features;
191 extern u32 *trampoline_cr4_features;
192
193 static inline void cr4_set_bits_and_update_boot(unsigned long mask)
194 {
195         mmu_cr4_features |= mask;
196         if (trampoline_cr4_features)
197                 *trampoline_cr4_features = mmu_cr4_features;
198         cr4_set_bits(mask);
199 }
200
201 static inline void __native_flush_tlb(void)
202 {
203         /*
204          * If current->mm == NULL then we borrow a mm which may change during a
205          * task switch and therefore we must not be preempted while we write CR3
206          * back:
207          */
208         preempt_disable();
209         native_write_cr3(__native_read_cr3());
210         preempt_enable();
211 }
212
213 static inline void __native_flush_tlb_global_irq_disabled(void)
214 {
215         unsigned long cr4;
216
217         cr4 = this_cpu_read(cpu_tlbstate.cr4);
218         /* clear PGE */
219         native_write_cr4(cr4 & ~X86_CR4_PGE);
220         /* write old PGE again and flush TLBs */
221         native_write_cr4(cr4);
222 }
223
224 static inline void __native_flush_tlb_global(void)
225 {
226         unsigned long flags;
227
228         if (static_cpu_has(X86_FEATURE_INVPCID)) {
229                 /*
230                  * Using INVPCID is considerably faster than a pair of writes
231                  * to CR4 sandwiched inside an IRQ flag save/restore.
232                  */
233                 invpcid_flush_all();
234                 return;
235         }
236
237         /*
238          * Read-modify-write to CR4 - protect it from preemption and
239          * from interrupts. (Use the raw variant because this code can
240          * be called from deep inside debugging code.)
241          */
242         raw_local_irq_save(flags);
243
244         __native_flush_tlb_global_irq_disabled();
245
246         raw_local_irq_restore(flags);
247 }
248
249 static inline void __native_flush_tlb_single(unsigned long addr)
250 {
251         asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
252 }
253
254 static inline void __flush_tlb_all(void)
255 {
256         if (boot_cpu_has(X86_FEATURE_PGE))
257                 __flush_tlb_global();
258         else
259                 __flush_tlb();
260
261         /*
262          * Note: if we somehow had PCID but not PGE, then this wouldn't work --
263          * we'd end up flushing kernel translations for the current ASID but
264          * we might fail to flush kernel translations for other cached ASIDs.
265          *
266          * To avoid this issue, we force PCID off if PGE is off.
267          */
268 }
269
270 static inline void __flush_tlb_one(unsigned long addr)
271 {
272         count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
273         __flush_tlb_single(addr);
274 }
275
276 #define TLB_FLUSH_ALL   -1UL
277
278 /*
279  * TLB flushing:
280  *
281  *  - flush_tlb_all() flushes all processes TLBs
282  *  - flush_tlb_mm(mm) flushes the specified mm context TLB's
283  *  - flush_tlb_page(vma, vmaddr) flushes one page
284  *  - flush_tlb_range(vma, start, end) flushes a range of pages
285  *  - flush_tlb_kernel_range(start, end) flushes a range of kernel pages
286  *  - flush_tlb_others(cpumask, info) flushes TLBs on other cpus
287  *
288  * ..but the i386 has somewhat limited tlb flushing capabilities,
289  * and page-granular flushes are available only on i486 and up.
290  */
291 struct flush_tlb_info {
292         /*
293          * We support several kinds of flushes.
294          *
295          * - Fully flush a single mm.  .mm will be set, .end will be
296          *   TLB_FLUSH_ALL, and .new_tlb_gen will be the tlb_gen to
297          *   which the IPI sender is trying to catch us up.
298          *
299          * - Partially flush a single mm.  .mm will be set, .start and
300          *   .end will indicate the range, and .new_tlb_gen will be set
301          *   such that the changes between generation .new_tlb_gen-1 and
302          *   .new_tlb_gen are entirely contained in the indicated range.
303          *
304          * - Fully flush all mms whose tlb_gens have been updated.  .mm
305          *   will be NULL, .end will be TLB_FLUSH_ALL, and .new_tlb_gen
306          *   will be zero.
307          */
308         struct mm_struct        *mm;
309         unsigned long           start;
310         unsigned long           end;
311         u64                     new_tlb_gen;
312 };
313
314 #define local_flush_tlb() __flush_tlb()
315
316 #define flush_tlb_mm(mm)        flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL)
317
318 #define flush_tlb_range(vma, start, end)        \
319                 flush_tlb_mm_range(vma->vm_mm, start, end, vma->vm_flags)
320
321 extern void flush_tlb_all(void);
322 extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
323                                 unsigned long end, unsigned long vmflag);
324 extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
325
326 static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
327 {
328         flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, VM_NONE);
329 }
330
331 void native_flush_tlb_others(const struct cpumask *cpumask,
332                              const struct flush_tlb_info *info);
333
334 static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
335                                         struct mm_struct *mm)
336 {
337         inc_mm_tlb_gen(mm);
338         cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
339 }
340
341 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
342
343 #ifndef CONFIG_PARAVIRT
344 #define flush_tlb_others(mask, info)    \
345         native_flush_tlb_others(mask, info)
346 #endif
347
348 #endif /* _ASM_X86_TLBFLUSH_H */