blob: 4e5a5ddb9e4d0630d473446c5b9acdd731c70bf3 [file] [log] [blame]
Glauber Costac048fdf2008-03-03 14:12:54 -03001#include <linux/init.h>
2
3#include <linux/mm.h>
Glauber Costac048fdf2008-03-03 14:12:54 -03004#include <linux/spinlock.h>
5#include <linux/smp.h>
Glauber Costac048fdf2008-03-03 14:12:54 -03006#include <linux/interrupt.h>
Paul Gortmaker4b599fe2016-07-13 20:18:55 -04007#include <linux/export.h>
Shaohua Li93296722010-10-20 11:07:03 +08008#include <linux/cpu.h>
Glauber Costac048fdf2008-03-03 14:12:54 -03009
Glauber Costac048fdf2008-03-03 14:12:54 -030010#include <asm/tlbflush.h>
Glauber Costac048fdf2008-03-03 14:12:54 -030011#include <asm/mmu_context.h>
Jan Beulich350f8f52009-11-13 11:54:40 +000012#include <asm/cache.h>
Tejun Heo6dd01be2009-01-21 17:26:06 +090013#include <asm/apic.h>
Tejun Heobdbcdd42009-01-21 17:26:06 +090014#include <asm/uv/uv.h>
Alex Shi3df32122012-06-28 09:02:20 +080015#include <linux/debugfs.h>
Glauber Costa5af55732008-03-25 13:28:56 -030016
Glauber Costac048fdf2008-03-03 14:12:54 -030017/*
Andy Lutomirskice4a4e562017-05-28 10:00:14 -070018 * TLB flushing, formerly SMP-only
Glauber Costac048fdf2008-03-03 14:12:54 -030019 * c/o Linus Torvalds.
20 *
21 * These mean you can really definitely utterly forget about
22 * writing to user space from interrupts. (Its not allowed anyway).
23 *
24 * Optimizations Manfred Spraul <manfred@colorfullife.com>
25 *
26 * More scalable flush, from Andi Kleen
27 *
Alex Shi52aec332012-06-28 09:02:23 +080028 * Implement flush IPI by CALL_FUNCTION_VECTOR, Alex Shi
Glauber Costac048fdf2008-03-03 14:12:54 -030029 */
30
Andy Lutomirskif39681e2017-06-29 08:53:15 -070031atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1);
32
Glauber Costac048fdf2008-03-03 14:12:54 -030033void leave_mm(int cpu)
34{
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070035 struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
36
37 /*
38 * It's plausible that we're in lazy TLB mode while our mm is init_mm.
39 * If so, our callers still expect us to flush the TLB, but there
40 * aren't any user TLB entries in init_mm to worry about.
41 *
42 * This needs to happen before any other sanity checks due to
43 * intel_idle's shenanigans.
44 */
45 if (loaded_mm == &init_mm)
46 return;
47
Alex Shic6ae41e2012-05-11 15:35:27 +080048 if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
Glauber Costac048fdf2008-03-03 14:12:54 -030049 BUG();
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070050
51 switch_mm(NULL, &init_mm, NULL);
Glauber Costac048fdf2008-03-03 14:12:54 -030052}
53EXPORT_SYMBOL_GPL(leave_mm);
54
Andy Lutomirski69c03192016-04-26 09:39:08 -070055void switch_mm(struct mm_struct *prev, struct mm_struct *next,
56 struct task_struct *tsk)
57{
Andy Lutomirski078194f2016-04-26 09:39:09 -070058 unsigned long flags;
59
60 local_irq_save(flags);
61 switch_mm_irqs_off(prev, next, tsk);
62 local_irq_restore(flags);
63}
64
65void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
66 struct task_struct *tsk)
67{
Andy Lutomirski69c03192016-04-26 09:39:08 -070068 unsigned cpu = smp_processor_id();
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070069 struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm);
Andy Lutomirski69c03192016-04-26 09:39:08 -070070
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070071 /*
72 * NB: The scheduler will call us with prev == next when
73 * switching from lazy TLB mode to normal mode if active_mm
74 * isn't changing. When this happens, there is no guarantee
75 * that CR3 (and hence cpu_tlbstate.loaded_mm) matches next.
76 *
77 * NB: leave_mm() calls us with prev == NULL and tsk == NULL.
78 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -070079
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070080 this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -070081
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070082 if (real_prev == next) {
Andy Lutomirski69c03192016-04-26 09:39:08 -070083 /*
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070084 * There's nothing to do: we always keep the per-mm control
85 * regs in sync with cpu_tlbstate.loaded_mm. Just
86 * sanity-check mm_cpumask.
Andy Lutomirski69c03192016-04-26 09:39:08 -070087 */
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070088 if (WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(next))))
89 cpumask_set_cpu(cpu, mm_cpumask(next));
90 return;
91 }
Andy Lutomirski69c03192016-04-26 09:39:08 -070092
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070093 if (IS_ENABLED(CONFIG_VMAP_STACK)) {
94 /*
95 * If our current stack is in vmalloc space and isn't
96 * mapped in the new pgd, we'll double-fault. Forcibly
97 * map it.
98 */
99 unsigned int stack_pgd_index = pgd_index(current_stack_pointer());
Andy Lutomirski69c03192016-04-26 09:39:08 -0700100
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700101 pgd_t *pgd = next->pgd + stack_pgd_index;
Andy Lutomirski69c03192016-04-26 09:39:08 -0700102
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700103 if (unlikely(pgd_none(*pgd)))
104 set_pgd(pgd, init_mm.pgd[stack_pgd_index]);
105 }
106
107 this_cpu_write(cpu_tlbstate.loaded_mm, next);
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700108 this_cpu_write(cpu_tlbstate.ctxs[0].ctx_id, next->context.ctx_id);
109 this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, atomic64_read(&next->context.tlb_gen));
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700110
111 WARN_ON_ONCE(cpumask_test_cpu(cpu, mm_cpumask(next)));
112 cpumask_set_cpu(cpu, mm_cpumask(next));
113
114 /*
115 * Re-load page tables.
116 *
117 * This logic has an ordering constraint:
118 *
119 * CPU 0: Write to a PTE for 'next'
120 * CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI.
121 * CPU 1: set bit 1 in next's mm_cpumask
122 * CPU 1: load from the PTE that CPU 0 writes (implicit)
123 *
124 * We need to prevent an outcome in which CPU 1 observes
125 * the new PTE value and CPU 0 observes bit 1 clear in
126 * mm_cpumask. (If that occurs, then the IPI will never
127 * be sent, and CPU 0's TLB will contain a stale entry.)
128 *
129 * The bad outcome can occur if either CPU's load is
130 * reordered before that CPU's store, so both CPUs must
131 * execute full barriers to prevent this from happening.
132 *
133 * Thus, switch_mm needs a full barrier between the
134 * store to mm_cpumask and any operation that could load
135 * from next->pgd. TLB fills are special and can happen
136 * due to instruction fetches or for no reason at all,
137 * and neither LOCK nor MFENCE orders them.
138 * Fortunately, load_cr3() is serializing and gives the
139 * ordering guarantee we need.
140 */
141 load_cr3(next->pgd);
142
143 /*
144 * This gets called via leave_mm() in the idle path where RCU
145 * functions differently. Tracing normally uses RCU, so we have to
146 * call the tracepoint specially here.
147 */
148 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
149
150 /* Stop flush ipis for the previous mm */
151 WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(real_prev)) &&
152 real_prev != &init_mm);
153 cpumask_clear_cpu(cpu, mm_cpumask(real_prev));
154
Andy Lutomirski73534252017-06-20 22:22:08 -0700155 /* Load per-mm CR4 and LDTR state */
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700156 load_mm_cr4(next);
Andy Lutomirski73534252017-06-20 22:22:08 -0700157 switch_ldt(real_prev, next);
Andy Lutomirski69c03192016-04-26 09:39:08 -0700158}
159
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700160/*
161 * flush_tlb_func_common()'s memory ordering requirement is that any
162 * TLB fills that happen after we flush the TLB are ordered after we
163 * read active_mm's tlb_gen. We don't need any explicit barriers
164 * because all x86 flush operations are serializing and the
165 * atomic64_read operation won't be reordered by the compiler.
166 */
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700167static void flush_tlb_func_common(const struct flush_tlb_info *f,
168 bool local, enum tlb_flush_reason reason)
Glauber Costac048fdf2008-03-03 14:12:54 -0300169{
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700170 /*
171 * We have three different tlb_gen values in here. They are:
172 *
173 * - mm_tlb_gen: the latest generation.
174 * - local_tlb_gen: the generation that this CPU has already caught
175 * up to.
176 * - f->new_tlb_gen: the generation that the requester of the flush
177 * wants us to catch up to.
178 */
179 struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
180 u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
181 u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[0].tlb_gen);
182
Andy Lutomirskibc0d5a82017-06-29 08:53:13 -0700183 /* This code cannot presently handle being reentered. */
184 VM_WARN_ON(!irqs_disabled());
185
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700186 VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[0].ctx_id) !=
187 loaded_mm->context.ctx_id);
188
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700189 if (this_cpu_read(cpu_tlbstate.state) != TLBSTATE_OK) {
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700190 /*
191 * leave_mm() is adequate to handle any type of flush, and
192 * we would prefer not to receive further IPIs. leave_mm()
193 * clears this CPU's bit in mm_cpumask().
194 */
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700195 leave_mm(smp_processor_id());
196 return;
197 }
198
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700199 if (unlikely(local_tlb_gen == mm_tlb_gen)) {
200 /*
201 * There's nothing to do: we're already up to date. This can
202 * happen if two concurrent flushes happen -- the first flush to
203 * be handled can catch us all the way up, leaving no work for
204 * the second flush.
205 */
206 return;
207 }
208
209 WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen);
210 WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen);
211
212 /*
213 * If we get to this point, we know that our TLB is out of date.
214 * This does not strictly imply that we need to flush (it's
215 * possible that f->new_tlb_gen <= local_tlb_gen), but we're
216 * going to need to flush in the very near future, so we might
217 * as well get it over with.
218 *
219 * The only question is whether to do a full or partial flush.
220 *
221 * We do a partial flush if requested and two extra conditions
222 * are met:
223 *
224 * 1. f->new_tlb_gen == local_tlb_gen + 1. We have an invariant that
225 * we've always done all needed flushes to catch up to
226 * local_tlb_gen. If, for example, local_tlb_gen == 2 and
227 * f->new_tlb_gen == 3, then we know that the flush needed to bring
228 * us up to date for tlb_gen 3 is the partial flush we're
229 * processing.
230 *
231 * As an example of why this check is needed, suppose that there
232 * are two concurrent flushes. The first is a full flush that
233 * changes context.tlb_gen from 1 to 2. The second is a partial
234 * flush that changes context.tlb_gen from 2 to 3. If they get
235 * processed on this CPU in reverse order, we'll see
236 * local_tlb_gen == 1, mm_tlb_gen == 3, and end != TLB_FLUSH_ALL.
237 * If we were to use __flush_tlb_single() and set local_tlb_gen to
238 * 3, we'd be break the invariant: we'd update local_tlb_gen above
239 * 1 without the full flush that's needed for tlb_gen 2.
240 *
241 * 2. f->new_tlb_gen == mm_tlb_gen. This is purely an optimiation.
242 * Partial TLB flushes are not all that much cheaper than full TLB
243 * flushes, so it seems unlikely that it would be a performance win
244 * to do a partial flush if that won't bring our TLB fully up to
245 * date. By doing a full flush instead, we can increase
246 * local_tlb_gen all the way to mm_tlb_gen and we can probably
247 * avoid another flush in the very near future.
248 */
249 if (f->end != TLB_FLUSH_ALL &&
250 f->new_tlb_gen == local_tlb_gen + 1 &&
251 f->new_tlb_gen == mm_tlb_gen) {
252 /* Partial flush */
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700253 unsigned long addr;
Andy Lutomirskibe4ffc02017-05-28 10:00:16 -0700254 unsigned long nr_pages = (f->end - f->start) >> PAGE_SHIFT;
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700255
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700256 addr = f->start;
257 while (addr < f->end) {
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700258 __flush_tlb_single(addr);
259 addr += PAGE_SIZE;
260 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700261 if (local)
262 count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_pages);
263 trace_tlb_flush(reason, nr_pages);
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700264 } else {
265 /* Full flush. */
266 local_flush_tlb();
267 if (local)
268 count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
269 trace_tlb_flush(reason, TLB_FLUSH_ALL);
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700270 }
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700271
272 /* Both paths above update our state to mm_tlb_gen. */
273 this_cpu_write(cpu_tlbstate.ctxs[0].tlb_gen, mm_tlb_gen);
Glauber Costac048fdf2008-03-03 14:12:54 -0300274}
275
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700276static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
277{
278 const struct flush_tlb_info *f = info;
279
280 flush_tlb_func_common(f, true, reason);
281}
282
283static void flush_tlb_func_remote(void *info)
284{
285 const struct flush_tlb_info *f = info;
286
287 inc_irq_stat(irq_tlb_count);
288
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700289 if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.loaded_mm))
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700290 return;
291
292 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
293 flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN);
294}
295
Rusty Russell4595f962009-01-10 21:58:09 -0800296void native_flush_tlb_others(const struct cpumask *cpumask,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700297 const struct flush_tlb_info *info)
Rusty Russell4595f962009-01-10 21:58:09 -0800298{
Mel Gormanec659932014-01-21 14:33:16 -0800299 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700300 if (info->end == TLB_FLUSH_ALL)
Nadav Amit18c98242016-04-01 14:31:23 -0700301 trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
302 else
303 trace_tlb_flush(TLB_REMOTE_SEND_IPI,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700304 (info->end - info->start) >> PAGE_SHIFT);
Nadav Amit18c98242016-04-01 14:31:23 -0700305
Rusty Russell4595f962009-01-10 21:58:09 -0800306 if (is_uv_system()) {
Tejun Heobdbcdd42009-01-21 17:26:06 +0900307 unsigned int cpu;
Rusty Russell4595f962009-01-10 21:58:09 -0800308
Xiao Guangrong25542c62011-03-15 09:57:37 +0800309 cpu = smp_processor_id();
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700310 cpumask = uv_flush_tlb_others(cpumask, info);
Tejun Heobdbcdd42009-01-21 17:26:06 +0900311 if (cpumask)
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700312 smp_call_function_many(cpumask, flush_tlb_func_remote,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700313 (void *)info, 1);
Mike Travis0e219902009-01-10 21:58:10 -0800314 return;
Rusty Russell4595f962009-01-10 21:58:09 -0800315 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700316 smp_call_function_many(cpumask, flush_tlb_func_remote,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700317 (void *)info, 1);
Rusty Russell4595f962009-01-10 21:58:09 -0800318}
319
Dave Hansena5102472014-07-31 08:41:03 -0700320/*
321 * See Documentation/x86/tlb.txt for details. We choose 33
322 * because it is large enough to cover the vast majority (at
323 * least 95%) of allocations, and is small enough that we are
324 * confident it will not cause too much overhead. Each single
325 * flush is about 100 ns, so this caps the maximum overhead at
326 * _about_ 3,000 ns.
327 *
328 * This is in units of pages.
329 */
Jeremiah Mahler86426852014-08-09 00:38:33 -0700330static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700331
Alex Shi611ae8e2012-06-28 09:02:22 +0800332void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
333 unsigned long end, unsigned long vmflag)
334{
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700335 int cpu;
Alex Shi611ae8e2012-06-28 09:02:22 +0800336
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700337 struct flush_tlb_info info = {
338 .mm = mm,
339 };
Andy Lutomirskice273742017-04-22 00:01:21 -0700340
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700341 cpu = get_cpu();
Andy Lutomirskice273742017-04-22 00:01:21 -0700342
Andy Lutomirskif39681e2017-06-29 08:53:15 -0700343 /* This is also a barrier that synchronizes with switch_mm(). */
Andy Lutomirskib0579ad2017-06-29 08:53:16 -0700344 info.new_tlb_gen = inc_mm_tlb_gen(mm);
Andy Lutomirski71b3c122016-01-06 12:21:01 -0800345
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700346 /* Should we flush just the requested range? */
347 if ((end != TLB_FLUSH_ALL) &&
348 !(vmflag & VM_HUGETLB) &&
349 ((end - start) >> PAGE_SHIFT) <= tlb_single_page_flush_ceiling) {
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700350 info.start = start;
351 info.end = end;
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700352 } else {
353 info.start = 0UL;
354 info.end = TLB_FLUSH_ALL;
Dave Hansen4995ab92014-07-31 08:40:54 -0700355 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700356
Andy Lutomirskibc0d5a82017-06-29 08:53:13 -0700357 if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) {
358 VM_WARN_ON(irqs_disabled());
359 local_irq_disable();
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700360 flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN);
Andy Lutomirskibc0d5a82017-06-29 08:53:13 -0700361 local_irq_enable();
362 }
363
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700364 if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids)
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700365 flush_tlb_others(mm_cpumask(mm), &info);
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700366 put_cpu();
Alex Shie7b52ff2012-06-28 09:02:17 +0800367}
368
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700369
Glauber Costac048fdf2008-03-03 14:12:54 -0300370static void do_flush_tlb_all(void *info)
371{
Mel Gormanec659932014-01-21 14:33:16 -0800372 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
Glauber Costac048fdf2008-03-03 14:12:54 -0300373 __flush_tlb_all();
Alex Shic6ae41e2012-05-11 15:35:27 +0800374 if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
Borislav Petkov3f8afb72010-07-21 14:47:05 +0200375 leave_mm(smp_processor_id());
Glauber Costac048fdf2008-03-03 14:12:54 -0300376}
377
378void flush_tlb_all(void)
379{
Mel Gormanec659932014-01-21 14:33:16 -0800380 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200381 on_each_cpu(do_flush_tlb_all, NULL, 1);
Glauber Costac048fdf2008-03-03 14:12:54 -0300382}
Alex Shi3df32122012-06-28 09:02:20 +0800383
Alex Shieffee4b2012-06-28 09:02:24 +0800384static void do_kernel_range_flush(void *info)
385{
386 struct flush_tlb_info *f = info;
387 unsigned long addr;
388
389 /* flush range by one by one 'invlpg' */
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700390 for (addr = f->start; addr < f->end; addr += PAGE_SIZE)
Alex Shieffee4b2012-06-28 09:02:24 +0800391 __flush_tlb_single(addr);
392}
393
394void flush_tlb_kernel_range(unsigned long start, unsigned long end)
395{
Alex Shieffee4b2012-06-28 09:02:24 +0800396
397 /* Balance as user space task's flush, a bit conservative */
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700398 if (end == TLB_FLUSH_ALL ||
Andy Lutomirskibe4ffc02017-05-28 10:00:16 -0700399 (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
Alex Shieffee4b2012-06-28 09:02:24 +0800400 on_each_cpu(do_flush_tlb_all, NULL, 1);
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700401 } else {
402 struct flush_tlb_info info;
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700403 info.start = start;
404 info.end = end;
Alex Shieffee4b2012-06-28 09:02:24 +0800405 on_each_cpu(do_kernel_range_flush, &info, 1);
406 }
407}
Dave Hansen2d040a12014-07-31 08:41:01 -0700408
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700409void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
410{
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700411 struct flush_tlb_info info = {
412 .mm = NULL,
413 .start = 0UL,
414 .end = TLB_FLUSH_ALL,
415 };
416
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700417 int cpu = get_cpu();
418
Andy Lutomirskibc0d5a82017-06-29 08:53:13 -0700419 if (cpumask_test_cpu(cpu, &batch->cpumask)) {
420 VM_WARN_ON(irqs_disabled());
421 local_irq_disable();
Andy Lutomirski3f79e4c2017-05-28 10:00:13 -0700422 flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN);
Andy Lutomirskibc0d5a82017-06-29 08:53:13 -0700423 local_irq_enable();
424 }
425
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700426 if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700427 flush_tlb_others(&batch->cpumask, &info);
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700428 cpumask_clear(&batch->cpumask);
429
430 put_cpu();
431}
432
Dave Hansen2d040a12014-07-31 08:41:01 -0700433static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
434 size_t count, loff_t *ppos)
435{
436 char buf[32];
437 unsigned int len;
438
439 len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
440 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
441}
442
443static ssize_t tlbflush_write_file(struct file *file,
444 const char __user *user_buf, size_t count, loff_t *ppos)
445{
446 char buf[32];
447 ssize_t len;
448 int ceiling;
449
450 len = min(count, sizeof(buf) - 1);
451 if (copy_from_user(buf, user_buf, len))
452 return -EFAULT;
453
454 buf[len] = '\0';
455 if (kstrtoint(buf, 0, &ceiling))
456 return -EINVAL;
457
458 if (ceiling < 0)
459 return -EINVAL;
460
461 tlb_single_page_flush_ceiling = ceiling;
462 return count;
463}
464
465static const struct file_operations fops_tlbflush = {
466 .read = tlbflush_read_file,
467 .write = tlbflush_write_file,
468 .llseek = default_llseek,
469};
470
471static int __init create_tlb_single_page_flush_ceiling(void)
472{
473 debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
474 arch_debugfs_dir, NULL, &fops_tlbflush);
475 return 0;
476}
477late_initcall(create_tlb_single_page_flush_ceiling);