blob: 2a5e851f203569f7344d890c9766adc4635736b6 [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
Glauber Costac048fdf2008-03-03 14:12:54 -030031void leave_mm(int cpu)
32{
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070033 struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
34
35 /*
36 * It's plausible that we're in lazy TLB mode while our mm is init_mm.
37 * If so, our callers still expect us to flush the TLB, but there
38 * aren't any user TLB entries in init_mm to worry about.
39 *
40 * This needs to happen before any other sanity checks due to
41 * intel_idle's shenanigans.
42 */
43 if (loaded_mm == &init_mm)
44 return;
45
Alex Shic6ae41e2012-05-11 15:35:27 +080046 if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
Glauber Costac048fdf2008-03-03 14:12:54 -030047 BUG();
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070048
49 switch_mm(NULL, &init_mm, NULL);
Glauber Costac048fdf2008-03-03 14:12:54 -030050}
51EXPORT_SYMBOL_GPL(leave_mm);
52
Andy Lutomirski69c03192016-04-26 09:39:08 -070053void switch_mm(struct mm_struct *prev, struct mm_struct *next,
54 struct task_struct *tsk)
55{
Andy Lutomirski078194f2016-04-26 09:39:09 -070056 unsigned long flags;
57
58 local_irq_save(flags);
59 switch_mm_irqs_off(prev, next, tsk);
60 local_irq_restore(flags);
61}
62
63void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
64 struct task_struct *tsk)
65{
Andy Lutomirski69c03192016-04-26 09:39:08 -070066 unsigned cpu = smp_processor_id();
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070067 struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm);
Andy Lutomirski69c03192016-04-26 09:39:08 -070068
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070069 /*
70 * NB: The scheduler will call us with prev == next when
71 * switching from lazy TLB mode to normal mode if active_mm
72 * isn't changing. When this happens, there is no guarantee
73 * that CR3 (and hence cpu_tlbstate.loaded_mm) matches next.
74 *
75 * NB: leave_mm() calls us with prev == NULL and tsk == NULL.
76 */
Andy Lutomirskie37e43a2016-08-11 02:35:23 -070077
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070078 this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
Andy Lutomirskie37e43a2016-08-11 02:35:23 -070079
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070080 if (real_prev == next) {
Andy Lutomirski69c03192016-04-26 09:39:08 -070081 /*
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070082 * There's nothing to do: we always keep the per-mm control
83 * regs in sync with cpu_tlbstate.loaded_mm. Just
84 * sanity-check mm_cpumask.
Andy Lutomirski69c03192016-04-26 09:39:08 -070085 */
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070086 if (WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(next))))
87 cpumask_set_cpu(cpu, mm_cpumask(next));
88 return;
89 }
Andy Lutomirski69c03192016-04-26 09:39:08 -070090
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070091 if (IS_ENABLED(CONFIG_VMAP_STACK)) {
92 /*
93 * If our current stack is in vmalloc space and isn't
94 * mapped in the new pgd, we'll double-fault. Forcibly
95 * map it.
96 */
97 unsigned int stack_pgd_index = pgd_index(current_stack_pointer());
Andy Lutomirski69c03192016-04-26 09:39:08 -070098
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -070099 pgd_t *pgd = next->pgd + stack_pgd_index;
Andy Lutomirski69c03192016-04-26 09:39:08 -0700100
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700101 if (unlikely(pgd_none(*pgd)))
102 set_pgd(pgd, init_mm.pgd[stack_pgd_index]);
103 }
104
105 this_cpu_write(cpu_tlbstate.loaded_mm, next);
106
107 WARN_ON_ONCE(cpumask_test_cpu(cpu, mm_cpumask(next)));
108 cpumask_set_cpu(cpu, mm_cpumask(next));
109
110 /*
111 * Re-load page tables.
112 *
113 * This logic has an ordering constraint:
114 *
115 * CPU 0: Write to a PTE for 'next'
116 * CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI.
117 * CPU 1: set bit 1 in next's mm_cpumask
118 * CPU 1: load from the PTE that CPU 0 writes (implicit)
119 *
120 * We need to prevent an outcome in which CPU 1 observes
121 * the new PTE value and CPU 0 observes bit 1 clear in
122 * mm_cpumask. (If that occurs, then the IPI will never
123 * be sent, and CPU 0's TLB will contain a stale entry.)
124 *
125 * The bad outcome can occur if either CPU's load is
126 * reordered before that CPU's store, so both CPUs must
127 * execute full barriers to prevent this from happening.
128 *
129 * Thus, switch_mm needs a full barrier between the
130 * store to mm_cpumask and any operation that could load
131 * from next->pgd. TLB fills are special and can happen
132 * due to instruction fetches or for no reason at all,
133 * and neither LOCK nor MFENCE orders them.
134 * Fortunately, load_cr3() is serializing and gives the
135 * ordering guarantee we need.
136 */
137 load_cr3(next->pgd);
138
139 /*
140 * This gets called via leave_mm() in the idle path where RCU
141 * functions differently. Tracing normally uses RCU, so we have to
142 * call the tracepoint specially here.
143 */
144 trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
145
146 /* Stop flush ipis for the previous mm */
147 WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(real_prev)) &&
148 real_prev != &init_mm);
149 cpumask_clear_cpu(cpu, mm_cpumask(real_prev));
150
151 /* Load per-mm CR4 state */
152 load_mm_cr4(next);
Andy Lutomirski69c03192016-04-26 09:39:08 -0700153
154#ifdef CONFIG_MODIFY_LDT_SYSCALL
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700155 /*
156 * Load the LDT, if the LDT is different.
157 *
158 * It's possible that prev->context.ldt doesn't match
159 * the LDT register. This can happen if leave_mm(prev)
160 * was called and then modify_ldt changed
161 * prev->context.ldt but suppressed an IPI to this CPU.
162 * In this case, prev->context.ldt != NULL, because we
163 * never set context.ldt to NULL while the mm still
164 * exists. That means that next->context.ldt !=
165 * prev->context.ldt, because mms never share an LDT.
166 */
167 if (unlikely(real_prev->context.ldt != next->context.ldt))
168 load_mm_ldt(next);
Andy Lutomirski69c03192016-04-26 09:39:08 -0700169#endif
Andy Lutomirski69c03192016-04-26 09:39:08 -0700170}
171
Glauber Costac048fdf2008-03-03 14:12:54 -0300172/*
Glauber Costac048fdf2008-03-03 14:12:54 -0300173 * The flush IPI assumes that a thread switch happens in this order:
174 * [cpu0: the cpu that switches]
175 * 1) switch_mm() either 1a) or 1b)
176 * 1a) thread switch to a different mm
Alex Shi52aec332012-06-28 09:02:23 +0800177 * 1a1) set cpu_tlbstate to TLBSTATE_OK
178 * Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
179 * if cpu0 was in lazy tlb mode.
180 * 1a2) update cpu active_mm
Glauber Costac048fdf2008-03-03 14:12:54 -0300181 * Now cpu0 accepts tlb flushes for the new mm.
Alex Shi52aec332012-06-28 09:02:23 +0800182 * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
Glauber Costac048fdf2008-03-03 14:12:54 -0300183 * Now the other cpus will send tlb flush ipis.
184 * 1a4) change cr3.
Alex Shi52aec332012-06-28 09:02:23 +0800185 * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
186 * Stop ipi delivery for the old mm. This is not synchronized with
187 * the other cpus, but flush_tlb_func ignore flush ipis for the wrong
188 * mm, and in the worst case we perform a superfluous tlb flush.
Glauber Costac048fdf2008-03-03 14:12:54 -0300189 * 1b) thread switch without mm change
Alex Shi52aec332012-06-28 09:02:23 +0800190 * cpu active_mm is correct, cpu0 already handles flush ipis.
191 * 1b1) set cpu_tlbstate to TLBSTATE_OK
Glauber Costac048fdf2008-03-03 14:12:54 -0300192 * 1b2) test_and_set the cpu bit in cpu_vm_mask.
193 * Atomically set the bit [other cpus will start sending flush ipis],
194 * and test the bit.
195 * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
196 * 2) switch %%esp, ie current
197 *
198 * The interrupt must handle 2 special cases:
199 * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
200 * - the cpu performs speculative tlb reads, i.e. even if the cpu only
201 * runs in kernel space, the cpu could load tlb entries for user space
202 * pages.
203 *
Alex Shi52aec332012-06-28 09:02:23 +0800204 * The good news is that cpu_tlbstate is local to each cpu, no
Glauber Costac048fdf2008-03-03 14:12:54 -0300205 * write/read ordering problems.
206 */
207
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700208static void flush_tlb_func_common(const struct flush_tlb_info *f,
209 bool local, enum tlb_flush_reason reason)
Glauber Costac048fdf2008-03-03 14:12:54 -0300210{
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700211 if (this_cpu_read(cpu_tlbstate.state) != TLBSTATE_OK) {
212 leave_mm(smp_processor_id());
213 return;
214 }
215
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700216 if (f->end == TLB_FLUSH_ALL) {
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700217 local_flush_tlb();
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700218 if (local)
219 count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
220 trace_tlb_flush(reason, TLB_FLUSH_ALL);
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700221 } else {
222 unsigned long addr;
Andy Lutomirskibe4ffc02017-05-28 10:00:16 -0700223 unsigned long nr_pages = (f->end - f->start) >> PAGE_SHIFT;
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700224 addr = f->start;
225 while (addr < f->end) {
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700226 __flush_tlb_single(addr);
227 addr += PAGE_SIZE;
228 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700229 if (local)
230 count_vm_tlb_events(NR_TLB_LOCAL_FLUSH_ONE, nr_pages);
231 trace_tlb_flush(reason, nr_pages);
Andy Lutomirskib3b90e52017-05-22 15:30:02 -0700232 }
Glauber Costac048fdf2008-03-03 14:12:54 -0300233}
234
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700235static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
236{
237 const struct flush_tlb_info *f = info;
238
239 flush_tlb_func_common(f, true, reason);
240}
241
242static void flush_tlb_func_remote(void *info)
243{
244 const struct flush_tlb_info *f = info;
245
246 inc_irq_stat(irq_tlb_count);
247
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700248 if (f->mm && f->mm != this_cpu_read(cpu_tlbstate.loaded_mm))
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700249 return;
250
251 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
252 flush_tlb_func_common(f, false, TLB_REMOTE_SHOOTDOWN);
253}
254
Rusty Russell4595f962009-01-10 21:58:09 -0800255void native_flush_tlb_others(const struct cpumask *cpumask,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700256 const struct flush_tlb_info *info)
Rusty Russell4595f962009-01-10 21:58:09 -0800257{
Mel Gormanec659932014-01-21 14:33:16 -0800258 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700259 if (info->end == TLB_FLUSH_ALL)
Nadav Amit18c98242016-04-01 14:31:23 -0700260 trace_tlb_flush(TLB_REMOTE_SEND_IPI, TLB_FLUSH_ALL);
261 else
262 trace_tlb_flush(TLB_REMOTE_SEND_IPI,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700263 (info->end - info->start) >> PAGE_SHIFT);
Nadav Amit18c98242016-04-01 14:31:23 -0700264
Rusty Russell4595f962009-01-10 21:58:09 -0800265 if (is_uv_system()) {
Tejun Heobdbcdd42009-01-21 17:26:06 +0900266 unsigned int cpu;
Rusty Russell4595f962009-01-10 21:58:09 -0800267
Xiao Guangrong25542c62011-03-15 09:57:37 +0800268 cpu = smp_processor_id();
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700269 cpumask = uv_flush_tlb_others(cpumask, info);
Tejun Heobdbcdd42009-01-21 17:26:06 +0900270 if (cpumask)
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700271 smp_call_function_many(cpumask, flush_tlb_func_remote,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700272 (void *)info, 1);
Mike Travis0e219902009-01-10 21:58:10 -0800273 return;
Rusty Russell4595f962009-01-10 21:58:09 -0800274 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700275 smp_call_function_many(cpumask, flush_tlb_func_remote,
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700276 (void *)info, 1);
Rusty Russell4595f962009-01-10 21:58:09 -0800277}
278
Dave Hansena5102472014-07-31 08:41:03 -0700279/*
280 * See Documentation/x86/tlb.txt for details. We choose 33
281 * because it is large enough to cover the vast majority (at
282 * least 95%) of allocations, and is small enough that we are
283 * confident it will not cause too much overhead. Each single
284 * flush is about 100 ns, so this caps the maximum overhead at
285 * _about_ 3,000 ns.
286 *
287 * This is in units of pages.
288 */
Jeremiah Mahler86426852014-08-09 00:38:33 -0700289static unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700290
Alex Shi611ae8e2012-06-28 09:02:22 +0800291void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
292 unsigned long end, unsigned long vmflag)
293{
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700294 int cpu;
Alex Shi611ae8e2012-06-28 09:02:22 +0800295
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700296 struct flush_tlb_info info = {
297 .mm = mm,
298 };
Andy Lutomirskice273742017-04-22 00:01:21 -0700299
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700300 cpu = get_cpu();
Andy Lutomirskice273742017-04-22 00:01:21 -0700301
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700302 /* Synchronize with switch_mm. */
303 smp_mb();
Andy Lutomirski71b3c122016-01-06 12:21:01 -0800304
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700305 /* Should we flush just the requested range? */
306 if ((end != TLB_FLUSH_ALL) &&
307 !(vmflag & VM_HUGETLB) &&
308 ((end - start) >> PAGE_SHIFT) <= tlb_single_page_flush_ceiling) {
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700309 info.start = start;
310 info.end = end;
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700311 } else {
312 info.start = 0UL;
313 info.end = TLB_FLUSH_ALL;
Dave Hansen4995ab92014-07-31 08:40:54 -0700314 }
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700315
Andy Lutomirski3d28ebc2017-05-28 10:00:15 -0700316 if (mm == this_cpu_read(cpu_tlbstate.loaded_mm))
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700317 flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN);
318 if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids)
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700319 flush_tlb_others(mm_cpumask(mm), &info);
Andy Lutomirski454bbad2017-05-28 10:00:12 -0700320 put_cpu();
Alex Shie7b52ff2012-06-28 09:02:17 +0800321}
322
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700323
Glauber Costac048fdf2008-03-03 14:12:54 -0300324static void do_flush_tlb_all(void *info)
325{
Mel Gormanec659932014-01-21 14:33:16 -0800326 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
Glauber Costac048fdf2008-03-03 14:12:54 -0300327 __flush_tlb_all();
Alex Shic6ae41e2012-05-11 15:35:27 +0800328 if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
Borislav Petkov3f8afb72010-07-21 14:47:05 +0200329 leave_mm(smp_processor_id());
Glauber Costac048fdf2008-03-03 14:12:54 -0300330}
331
332void flush_tlb_all(void)
333{
Mel Gormanec659932014-01-21 14:33:16 -0800334 count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200335 on_each_cpu(do_flush_tlb_all, NULL, 1);
Glauber Costac048fdf2008-03-03 14:12:54 -0300336}
Alex Shi3df32122012-06-28 09:02:20 +0800337
Alex Shieffee4b2012-06-28 09:02:24 +0800338static void do_kernel_range_flush(void *info)
339{
340 struct flush_tlb_info *f = info;
341 unsigned long addr;
342
343 /* flush range by one by one 'invlpg' */
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700344 for (addr = f->start; addr < f->end; addr += PAGE_SIZE)
Alex Shieffee4b2012-06-28 09:02:24 +0800345 __flush_tlb_single(addr);
346}
347
348void flush_tlb_kernel_range(unsigned long start, unsigned long end)
349{
Alex Shieffee4b2012-06-28 09:02:24 +0800350
351 /* Balance as user space task's flush, a bit conservative */
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700352 if (end == TLB_FLUSH_ALL ||
Andy Lutomirskibe4ffc02017-05-28 10:00:16 -0700353 (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
Alex Shieffee4b2012-06-28 09:02:24 +0800354 on_each_cpu(do_flush_tlb_all, NULL, 1);
Dave Hansene9f4e0a2014-07-31 08:40:55 -0700355 } else {
356 struct flush_tlb_info info;
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700357 info.start = start;
358 info.end = end;
Alex Shieffee4b2012-06-28 09:02:24 +0800359 on_each_cpu(do_kernel_range_flush, &info, 1);
360 }
361}
Dave Hansen2d040a12014-07-31 08:41:01 -0700362
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700363void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
364{
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700365 struct flush_tlb_info info = {
366 .mm = NULL,
367 .start = 0UL,
368 .end = TLB_FLUSH_ALL,
369 };
370
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700371 int cpu = get_cpu();
372
Andy Lutomirski3f79e4c2017-05-28 10:00:13 -0700373 if (cpumask_test_cpu(cpu, &batch->cpumask))
374 flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN);
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700375 if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
Andy Lutomirskia2055ab2017-05-28 10:00:10 -0700376 flush_tlb_others(&batch->cpumask, &info);
Andy Lutomirskie73ad5f2017-05-22 15:30:03 -0700377 cpumask_clear(&batch->cpumask);
378
379 put_cpu();
380}
381
Dave Hansen2d040a12014-07-31 08:41:01 -0700382static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
383 size_t count, loff_t *ppos)
384{
385 char buf[32];
386 unsigned int len;
387
388 len = sprintf(buf, "%ld\n", tlb_single_page_flush_ceiling);
389 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
390}
391
392static ssize_t tlbflush_write_file(struct file *file,
393 const char __user *user_buf, size_t count, loff_t *ppos)
394{
395 char buf[32];
396 ssize_t len;
397 int ceiling;
398
399 len = min(count, sizeof(buf) - 1);
400 if (copy_from_user(buf, user_buf, len))
401 return -EFAULT;
402
403 buf[len] = '\0';
404 if (kstrtoint(buf, 0, &ceiling))
405 return -EINVAL;
406
407 if (ceiling < 0)
408 return -EINVAL;
409
410 tlb_single_page_flush_ceiling = ceiling;
411 return count;
412}
413
414static const struct file_operations fops_tlbflush = {
415 .read = tlbflush_read_file,
416 .write = tlbflush_write_file,
417 .llseek = default_llseek,
418};
419
420static int __init create_tlb_single_page_flush_ceiling(void)
421{
422 debugfs_create_file("tlb_single_page_flush_ceiling", S_IRUSR | S_IWUSR,
423 arch_debugfs_dir, NULL, &fops_tlbflush);
424 return 0;
425}
426late_initcall(create_tlb_single_page_flush_ceiling);