xref: /openbmc/linux/arch/x86/mm/tlb.c (revision 4995ab9cf512e9a6cc07dfd6b1d4e2fc48ce7fef)
155f4949fSIngo Molnar #include <linux/init.h>
255f4949fSIngo Molnar 
355f4949fSIngo Molnar #include <linux/mm.h>
455f4949fSIngo Molnar #include <linux/spinlock.h>
555f4949fSIngo Molnar #include <linux/smp.h>
655f4949fSIngo Molnar #include <linux/interrupt.h>
755f4949fSIngo Molnar #include <linux/module.h>
893296720SShaohua Li #include <linux/cpu.h>
955f4949fSIngo Molnar 
1055f4949fSIngo Molnar #include <asm/tlbflush.h>
1155f4949fSIngo Molnar #include <asm/mmu_context.h>
12350f8f56SJan Beulich #include <asm/cache.h>
1355f4949fSIngo Molnar #include <asm/apic.h>
1455f4949fSIngo Molnar #include <asm/uv/uv.h>
153df3212fSAlex Shi #include <linux/debugfs.h>
1655f4949fSIngo Molnar 
1755f4949fSIngo Molnar DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate)
1855f4949fSIngo Molnar 			= { &init_mm, 0, };
1955f4949fSIngo Molnar 
2055f4949fSIngo Molnar /*
2155f4949fSIngo Molnar  *	Smarter SMP flushing macros.
2255f4949fSIngo Molnar  *		c/o Linus Torvalds.
2355f4949fSIngo Molnar  *
2455f4949fSIngo Molnar  *	These mean you can really definitely utterly forget about
2555f4949fSIngo Molnar  *	writing to user space from interrupts. (Its not allowed anyway).
2655f4949fSIngo Molnar  *
2755f4949fSIngo Molnar  *	Optimizations Manfred Spraul <manfred@colorfullife.com>
2855f4949fSIngo Molnar  *
2955f4949fSIngo Molnar  *	More scalable flush, from Andi Kleen
3055f4949fSIngo Molnar  *
3152aec330SAlex Shi  *	Implement flush IPI by CALL_FUNCTION_VECTOR, Alex Shi
3255f4949fSIngo Molnar  */
3355f4949fSIngo Molnar 
3452aec330SAlex Shi struct flush_tlb_info {
3555f4949fSIngo Molnar 	struct mm_struct *flush_mm;
36e7b52ffdSAlex Shi 	unsigned long flush_start;
37e7b52ffdSAlex Shi 	unsigned long flush_end;
3855f4949fSIngo Molnar };
3993296720SShaohua Li 
4055f4949fSIngo Molnar /*
4155f4949fSIngo Molnar  * We cannot call mmdrop() because we are in interrupt context,
4255f4949fSIngo Molnar  * instead update mm->cpu_vm_mask.
4355f4949fSIngo Molnar  */
4455f4949fSIngo Molnar void leave_mm(int cpu)
4555f4949fSIngo Molnar {
4602171b4aSLinus Torvalds 	struct mm_struct *active_mm = this_cpu_read(cpu_tlbstate.active_mm);
47c6ae41e7SAlex Shi 	if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
4855f4949fSIngo Molnar 		BUG();
49a6fca40fSSuresh Siddha 	if (cpumask_test_cpu(cpu, mm_cpumask(active_mm))) {
50a6fca40fSSuresh Siddha 		cpumask_clear_cpu(cpu, mm_cpumask(active_mm));
5155f4949fSIngo Molnar 		load_cr3(swapper_pg_dir);
5255f4949fSIngo Molnar 	}
53a6fca40fSSuresh Siddha }
5455f4949fSIngo Molnar EXPORT_SYMBOL_GPL(leave_mm);
5555f4949fSIngo Molnar 
5655f4949fSIngo Molnar /*
5755f4949fSIngo Molnar  * The flush IPI assumes that a thread switch happens in this order:
5855f4949fSIngo Molnar  * [cpu0: the cpu that switches]
5955f4949fSIngo Molnar  * 1) switch_mm() either 1a) or 1b)
6055f4949fSIngo Molnar  * 1a) thread switch to a different mm
6152aec330SAlex Shi  * 1a1) set cpu_tlbstate to TLBSTATE_OK
6252aec330SAlex Shi  *	Now the tlb flush NMI handler flush_tlb_func won't call leave_mm
6352aec330SAlex Shi  *	if cpu0 was in lazy tlb mode.
6452aec330SAlex Shi  * 1a2) update cpu active_mm
6555f4949fSIngo Molnar  *	Now cpu0 accepts tlb flushes for the new mm.
6652aec330SAlex Shi  * 1a3) cpu_set(cpu, new_mm->cpu_vm_mask);
6755f4949fSIngo Molnar  *	Now the other cpus will send tlb flush ipis.
6855f4949fSIngo Molnar  * 1a4) change cr3.
6952aec330SAlex Shi  * 1a5) cpu_clear(cpu, old_mm->cpu_vm_mask);
7052aec330SAlex Shi  *	Stop ipi delivery for the old mm. This is not synchronized with
7152aec330SAlex Shi  *	the other cpus, but flush_tlb_func ignore flush ipis for the wrong
7252aec330SAlex Shi  *	mm, and in the worst case we perform a superfluous tlb flush.
7355f4949fSIngo Molnar  * 1b) thread switch without mm change
7452aec330SAlex Shi  *	cpu active_mm is correct, cpu0 already handles flush ipis.
7552aec330SAlex Shi  * 1b1) set cpu_tlbstate to TLBSTATE_OK
7655f4949fSIngo Molnar  * 1b2) test_and_set the cpu bit in cpu_vm_mask.
7755f4949fSIngo Molnar  *	Atomically set the bit [other cpus will start sending flush ipis],
7855f4949fSIngo Molnar  *	and test the bit.
7955f4949fSIngo Molnar  * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
8055f4949fSIngo Molnar  * 2) switch %%esp, ie current
8155f4949fSIngo Molnar  *
8255f4949fSIngo Molnar  * The interrupt must handle 2 special cases:
8355f4949fSIngo Molnar  * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
8455f4949fSIngo Molnar  * - the cpu performs speculative tlb reads, i.e. even if the cpu only
8555f4949fSIngo Molnar  *   runs in kernel space, the cpu could load tlb entries for user space
8655f4949fSIngo Molnar  *   pages.
8755f4949fSIngo Molnar  *
8852aec330SAlex Shi  * The good news is that cpu_tlbstate is local to each cpu, no
8955f4949fSIngo Molnar  * write/read ordering problems.
9055f4949fSIngo Molnar  */
9155f4949fSIngo Molnar 
9255f4949fSIngo Molnar /*
9352aec330SAlex Shi  * TLB flush funcation:
9455f4949fSIngo Molnar  * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
9555f4949fSIngo Molnar  * 2) Leave the mm if we are in the lazy tlb mode.
9655f4949fSIngo Molnar  */
9752aec330SAlex Shi static void flush_tlb_func(void *info)
9855f4949fSIngo Molnar {
9952aec330SAlex Shi 	struct flush_tlb_info *f = info;
10055f4949fSIngo Molnar 
101fd0f5869STomoki Sekiyama 	inc_irq_stat(irq_tlb_count);
102fd0f5869STomoki Sekiyama 
10352aec330SAlex Shi 	if (f->flush_mm != this_cpu_read(cpu_tlbstate.active_mm))
10452aec330SAlex Shi 		return;
10555f4949fSIngo Molnar 
106ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
107c6ae41e7SAlex Shi 	if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) {
108094ab1dbSH. Peter Anvin 		if (f->flush_end == TLB_FLUSH_ALL)
10955f4949fSIngo Molnar 			local_flush_tlb();
110e7b52ffdSAlex Shi 		else if (!f->flush_end)
111e7b52ffdSAlex Shi 			__flush_tlb_single(f->flush_start);
112e7b52ffdSAlex Shi 		else {
113e7b52ffdSAlex Shi 			unsigned long addr;
114e7b52ffdSAlex Shi 			addr = f->flush_start;
115e7b52ffdSAlex Shi 			while (addr < f->flush_end) {
116e7b52ffdSAlex Shi 				__flush_tlb_single(addr);
117e7b52ffdSAlex Shi 				addr += PAGE_SIZE;
118e7b52ffdSAlex Shi 			}
119e7b52ffdSAlex Shi 		}
12055f4949fSIngo Molnar 	} else
12152aec330SAlex Shi 		leave_mm(smp_processor_id());
12255f4949fSIngo Molnar 
12355f4949fSIngo Molnar }
12455f4949fSIngo Molnar 
12555f4949fSIngo Molnar void native_flush_tlb_others(const struct cpumask *cpumask,
126e7b52ffdSAlex Shi 				 struct mm_struct *mm, unsigned long start,
127e7b52ffdSAlex Shi 				 unsigned long end)
12855f4949fSIngo Molnar {
12952aec330SAlex Shi 	struct flush_tlb_info info;
13052aec330SAlex Shi 	info.flush_mm = mm;
13152aec330SAlex Shi 	info.flush_start = start;
13252aec330SAlex Shi 	info.flush_end = end;
13352aec330SAlex Shi 
134ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
13555f4949fSIngo Molnar 	if (is_uv_system()) {
13655f4949fSIngo Molnar 		unsigned int cpu;
13755f4949fSIngo Molnar 
13825542c64SXiao Guangrong 		cpu = smp_processor_id();
139e7b52ffdSAlex Shi 		cpumask = uv_flush_tlb_others(cpumask, mm, start, end, cpu);
14055f4949fSIngo Molnar 		if (cpumask)
14152aec330SAlex Shi 			smp_call_function_many(cpumask, flush_tlb_func,
14252aec330SAlex Shi 								&info, 1);
14355f4949fSIngo Molnar 		return;
14455f4949fSIngo Molnar 	}
14552aec330SAlex Shi 	smp_call_function_many(cpumask, flush_tlb_func, &info, 1);
14655f4949fSIngo Molnar }
14755f4949fSIngo Molnar 
14855f4949fSIngo Molnar void flush_tlb_current_task(void)
14955f4949fSIngo Molnar {
15055f4949fSIngo Molnar 	struct mm_struct *mm = current->mm;
15155f4949fSIngo Molnar 
15255f4949fSIngo Molnar 	preempt_disable();
15355f4949fSIngo Molnar 
154ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
15555f4949fSIngo Molnar 	local_flush_tlb();
15678f1c4d6SRusty Russell 	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
157e7b52ffdSAlex Shi 		flush_tlb_others(mm_cpumask(mm), mm, 0UL, TLB_FLUSH_ALL);
15855f4949fSIngo Molnar 	preempt_enable();
15955f4949fSIngo Molnar }
16055f4949fSIngo Molnar 
161611ae8e3SAlex Shi void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
162611ae8e3SAlex Shi 				unsigned long end, unsigned long vmflag)
163611ae8e3SAlex Shi {
164*4995ab9cSDave Hansen 	bool need_flush_others_all = true;
165611ae8e3SAlex Shi 	unsigned long addr;
166e7b52ffdSAlex Shi 	unsigned act_entries, tlb_entries = 0;
16715aa3682SMel Gorman 	unsigned long nr_base_pages;
168e7b52ffdSAlex Shi 
169611ae8e3SAlex Shi 	preempt_disable();
170611ae8e3SAlex Shi 	if (current->active_mm != mm)
171*4995ab9cSDave Hansen 		goto out;
172611ae8e3SAlex Shi 
173611ae8e3SAlex Shi 	if (!current->mm) {
174611ae8e3SAlex Shi 		leave_mm(smp_processor_id());
175*4995ab9cSDave Hansen 		goto out;
176611ae8e3SAlex Shi 	}
177611ae8e3SAlex Shi 
178611ae8e3SAlex Shi 	if (end == TLB_FLUSH_ALL || tlb_flushall_shift == -1
179ddd32b42SJoonsoo Kim 					|| vmflag & VM_HUGETLB) {
180611ae8e3SAlex Shi 		local_flush_tlb();
181*4995ab9cSDave Hansen 		goto out;
182611ae8e3SAlex Shi 	}
183611ae8e3SAlex Shi 
184611ae8e3SAlex Shi 	/* In modern CPU, last level tlb used for both data/ins */
185e7b52ffdSAlex Shi 	if (vmflag & VM_EXEC)
186e7b52ffdSAlex Shi 		tlb_entries = tlb_lli_4k[ENTRIES];
187e7b52ffdSAlex Shi 	else
188e7b52ffdSAlex Shi 		tlb_entries = tlb_lld_4k[ENTRIES];
18915aa3682SMel Gorman 
190611ae8e3SAlex Shi 	/* Assume all of TLB entries was occupied by this task */
19115aa3682SMel Gorman 	act_entries = tlb_entries >> tlb_flushall_shift;
19215aa3682SMel Gorman 	act_entries = mm->total_vm > act_entries ? act_entries : mm->total_vm;
19315aa3682SMel Gorman 	nr_base_pages = (end - start) >> PAGE_SHIFT;
194e7b52ffdSAlex Shi 
195611ae8e3SAlex Shi 	/* tlb_flushall_shift is on balance point, details in commit log */
19671b54f82SMel Gorman 	if (nr_base_pages > act_entries) {
197ec659934SMel Gorman 		count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ALL);
198e7b52ffdSAlex Shi 		local_flush_tlb();
1999824cf97SDave Hansen 	} else {
200*4995ab9cSDave Hansen 		need_flush_others_all = false;
201611ae8e3SAlex Shi 		/* flush range by one by one 'invlpg' */
2029824cf97SDave Hansen 		for (addr = start; addr < end;	addr += PAGE_SIZE) {
203ec659934SMel Gorman 			count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
204e7b52ffdSAlex Shi 			__flush_tlb_single(addr);
2059824cf97SDave Hansen 		}
206e7b52ffdSAlex Shi 	}
207*4995ab9cSDave Hansen out:
208*4995ab9cSDave Hansen 	if (need_flush_others_all) {
209*4995ab9cSDave Hansen 		start = 0UL;
210*4995ab9cSDave Hansen 		end = TLB_FLUSH_ALL;
211*4995ab9cSDave Hansen 	}
212e7b52ffdSAlex Shi 	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
213*4995ab9cSDave Hansen 		flush_tlb_others(mm_cpumask(mm), mm, start, end);
214e7b52ffdSAlex Shi 	preempt_enable();
215e7b52ffdSAlex Shi }
216e7b52ffdSAlex Shi 
217e7b52ffdSAlex Shi void flush_tlb_page(struct vm_area_struct *vma, unsigned long start)
21855f4949fSIngo Molnar {
21955f4949fSIngo Molnar 	struct mm_struct *mm = vma->vm_mm;
22055f4949fSIngo Molnar 
22155f4949fSIngo Molnar 	preempt_disable();
22255f4949fSIngo Molnar 
22355f4949fSIngo Molnar 	if (current->active_mm == mm) {
22455f4949fSIngo Molnar 		if (current->mm)
225e7b52ffdSAlex Shi 			__flush_tlb_one(start);
22655f4949fSIngo Molnar 		else
22755f4949fSIngo Molnar 			leave_mm(smp_processor_id());
22855f4949fSIngo Molnar 	}
22955f4949fSIngo Molnar 
23078f1c4d6SRusty Russell 	if (cpumask_any_but(mm_cpumask(mm), smp_processor_id()) < nr_cpu_ids)
231e7b52ffdSAlex Shi 		flush_tlb_others(mm_cpumask(mm), mm, start, 0UL);
23255f4949fSIngo Molnar 
23355f4949fSIngo Molnar 	preempt_enable();
23455f4949fSIngo Molnar }
23555f4949fSIngo Molnar 
23655f4949fSIngo Molnar static void do_flush_tlb_all(void *info)
23755f4949fSIngo Molnar {
238ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED);
23955f4949fSIngo Molnar 	__flush_tlb_all();
240c6ae41e7SAlex Shi 	if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_LAZY)
2413f8afb77SBorislav Petkov 		leave_mm(smp_processor_id());
24255f4949fSIngo Molnar }
24355f4949fSIngo Molnar 
24455f4949fSIngo Molnar void flush_tlb_all(void)
24555f4949fSIngo Molnar {
246ec659934SMel Gorman 	count_vm_tlb_event(NR_TLB_REMOTE_FLUSH);
24755f4949fSIngo Molnar 	on_each_cpu(do_flush_tlb_all, NULL, 1);
24855f4949fSIngo Molnar }
2493df3212fSAlex Shi 
250effee4b9SAlex Shi static void do_kernel_range_flush(void *info)
251effee4b9SAlex Shi {
252effee4b9SAlex Shi 	struct flush_tlb_info *f = info;
253effee4b9SAlex Shi 	unsigned long addr;
254effee4b9SAlex Shi 
255effee4b9SAlex Shi 	/* flush range by one by one 'invlpg' */
2566df46865SDave Hansen 	for (addr = f->flush_start; addr < f->flush_end; addr += PAGE_SIZE)
257effee4b9SAlex Shi 		__flush_tlb_single(addr);
258effee4b9SAlex Shi }
259effee4b9SAlex Shi 
260effee4b9SAlex Shi void flush_tlb_kernel_range(unsigned long start, unsigned long end)
261effee4b9SAlex Shi {
262effee4b9SAlex Shi 	unsigned act_entries;
263effee4b9SAlex Shi 	struct flush_tlb_info info;
264effee4b9SAlex Shi 
265effee4b9SAlex Shi 	/* In modern CPU, last level tlb used for both data/ins */
266effee4b9SAlex Shi 	act_entries = tlb_lld_4k[ENTRIES];
267effee4b9SAlex Shi 
268effee4b9SAlex Shi 	/* Balance as user space task's flush, a bit conservative */
269effee4b9SAlex Shi 	if (end == TLB_FLUSH_ALL || tlb_flushall_shift == -1 ||
270effee4b9SAlex Shi 		(end - start) >> PAGE_SHIFT > act_entries >> tlb_flushall_shift)
271effee4b9SAlex Shi 
272effee4b9SAlex Shi 		on_each_cpu(do_flush_tlb_all, NULL, 1);
273effee4b9SAlex Shi 	else {
274effee4b9SAlex Shi 		info.flush_start = start;
275effee4b9SAlex Shi 		info.flush_end = end;
276effee4b9SAlex Shi 		on_each_cpu(do_kernel_range_flush, &info, 1);
277effee4b9SAlex Shi 	}
278effee4b9SAlex Shi }
279effee4b9SAlex Shi 
2803df3212fSAlex Shi #ifdef CONFIG_DEBUG_TLBFLUSH
2813df3212fSAlex Shi static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,
2823df3212fSAlex Shi 			     size_t count, loff_t *ppos)
2833df3212fSAlex Shi {
2843df3212fSAlex Shi 	char buf[32];
2853df3212fSAlex Shi 	unsigned int len;
2863df3212fSAlex Shi 
2873df3212fSAlex Shi 	len = sprintf(buf, "%hd\n", tlb_flushall_shift);
2883df3212fSAlex Shi 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
2893df3212fSAlex Shi }
2903df3212fSAlex Shi 
2913df3212fSAlex Shi static ssize_t tlbflush_write_file(struct file *file,
2923df3212fSAlex Shi 		 const char __user *user_buf, size_t count, loff_t *ppos)
2933df3212fSAlex Shi {
2943df3212fSAlex Shi 	char buf[32];
2953df3212fSAlex Shi 	ssize_t len;
2963df3212fSAlex Shi 	s8 shift;
2973df3212fSAlex Shi 
2983df3212fSAlex Shi 	len = min(count, sizeof(buf) - 1);
2993df3212fSAlex Shi 	if (copy_from_user(buf, user_buf, len))
3003df3212fSAlex Shi 		return -EFAULT;
3013df3212fSAlex Shi 
3023df3212fSAlex Shi 	buf[len] = '\0';
3033df3212fSAlex Shi 	if (kstrtos8(buf, 0, &shift))
3043df3212fSAlex Shi 		return -EINVAL;
3053df3212fSAlex Shi 
306d4c9dbc6SJan Beulich 	if (shift < -1 || shift >= BITS_PER_LONG)
3073df3212fSAlex Shi 		return -EINVAL;
3083df3212fSAlex Shi 
3093df3212fSAlex Shi 	tlb_flushall_shift = shift;
3103df3212fSAlex Shi 	return count;
3113df3212fSAlex Shi }
3123df3212fSAlex Shi 
3133df3212fSAlex Shi static const struct file_operations fops_tlbflush = {
3143df3212fSAlex Shi 	.read = tlbflush_read_file,
3153df3212fSAlex Shi 	.write = tlbflush_write_file,
3163df3212fSAlex Shi 	.llseek = default_llseek,
3173df3212fSAlex Shi };
3183df3212fSAlex Shi 
3199611dc7aSJan Beulich static int __init create_tlb_flushall_shift(void)
3203df3212fSAlex Shi {
3213df3212fSAlex Shi 	debugfs_create_file("tlb_flushall_shift", S_IRUSR | S_IWUSR,
3223df3212fSAlex Shi 			    arch_debugfs_dir, NULL, &fops_tlbflush);
3233df3212fSAlex Shi 	return 0;
3243df3212fSAlex Shi }
3253df3212fSAlex Shi late_initcall(create_tlb_flushall_shift);
3263df3212fSAlex Shi #endif
327