xref: /openbmc/linux/arch/sparc/mm/tlb.c (revision cd99b9eb)
1 // SPDX-License-Identifier: GPL-2.0
2 /* arch/sparc64/mm/tlb.c
3  *
4  * Copyright (C) 2004 David S. Miller <davem@redhat.com>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/percpu.h>
9 #include <linux/mm.h>
10 #include <linux/swap.h>
11 #include <linux/preempt.h>
12 #include <linux/pagemap.h>
13 
14 #include <asm/tlbflush.h>
15 #include <asm/cacheflush.h>
16 #include <asm/mmu_context.h>
17 #include <asm/tlb.h>
18 
19 /* Heavily inspired by the ppc64 code.  */
20 
21 static DEFINE_PER_CPU(struct tlb_batch, tlb_batch);
22 
23 void flush_tlb_pending(void)
24 {
25 	struct tlb_batch *tb = &get_cpu_var(tlb_batch);
26 	struct mm_struct *mm = tb->mm;
27 
28 	if (!tb->tlb_nr)
29 		goto out;
30 
31 	flush_tsb_user(tb);
32 
33 	if (CTX_VALID(mm->context)) {
34 		if (tb->tlb_nr == 1) {
35 			global_flush_tlb_page(mm, tb->vaddrs[0]);
36 		} else {
37 #ifdef CONFIG_SMP
38 			smp_flush_tlb_pending(tb->mm, tb->tlb_nr,
39 					      &tb->vaddrs[0]);
40 #else
41 			__flush_tlb_pending(CTX_HWBITS(tb->mm->context),
42 					    tb->tlb_nr, &tb->vaddrs[0]);
43 #endif
44 		}
45 	}
46 
47 	tb->tlb_nr = 0;
48 
49 out:
50 	put_cpu_var(tlb_batch);
51 }
52 
53 void arch_enter_lazy_mmu_mode(void)
54 {
55 	struct tlb_batch *tb = this_cpu_ptr(&tlb_batch);
56 
57 	tb->active = 1;
58 }
59 
60 void arch_leave_lazy_mmu_mode(void)
61 {
62 	struct tlb_batch *tb = this_cpu_ptr(&tlb_batch);
63 
64 	if (tb->tlb_nr)
65 		flush_tlb_pending();
66 	tb->active = 0;
67 }
68 
69 static void tlb_batch_add_one(struct mm_struct *mm, unsigned long vaddr,
70 			      bool exec, unsigned int hugepage_shift)
71 {
72 	struct tlb_batch *tb = &get_cpu_var(tlb_batch);
73 	unsigned long nr;
74 
75 	vaddr &= PAGE_MASK;
76 	if (exec)
77 		vaddr |= 0x1UL;
78 
79 	nr = tb->tlb_nr;
80 
81 	if (unlikely(nr != 0 && mm != tb->mm)) {
82 		flush_tlb_pending();
83 		nr = 0;
84 	}
85 
86 	if (!tb->active) {
87 		flush_tsb_user_page(mm, vaddr, hugepage_shift);
88 		global_flush_tlb_page(mm, vaddr);
89 		goto out;
90 	}
91 
92 	if (nr == 0) {
93 		tb->mm = mm;
94 		tb->hugepage_shift = hugepage_shift;
95 	}
96 
97 	if (tb->hugepage_shift != hugepage_shift) {
98 		flush_tlb_pending();
99 		tb->hugepage_shift = hugepage_shift;
100 		nr = 0;
101 	}
102 
103 	tb->vaddrs[nr] = vaddr;
104 	tb->tlb_nr = ++nr;
105 	if (nr >= TLB_BATCH_NR)
106 		flush_tlb_pending();
107 
108 out:
109 	put_cpu_var(tlb_batch);
110 }
111 
112 void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr,
113 		   pte_t *ptep, pte_t orig, int fullmm,
114 		   unsigned int hugepage_shift)
115 {
116 	if (tlb_type != hypervisor &&
117 	    pte_dirty(orig)) {
118 		unsigned long paddr, pfn = pte_pfn(orig);
119 		struct address_space *mapping;
120 		struct page *page;
121 		struct folio *folio;
122 
123 		if (!pfn_valid(pfn))
124 			goto no_cache_flush;
125 
126 		page = pfn_to_page(pfn);
127 		if (PageReserved(page))
128 			goto no_cache_flush;
129 
130 		/* A real file page? */
131 		mapping = folio_flush_mapping(folio);
132 		if (!mapping)
133 			goto no_cache_flush;
134 
135 		paddr = (unsigned long) page_address(page);
136 		if ((paddr ^ vaddr) & (1 << 13))
137 			flush_dcache_folio_all(mm, folio);
138 	}
139 
140 no_cache_flush:
141 	if (!fullmm)
142 		tlb_batch_add_one(mm, vaddr, pte_exec(orig), hugepage_shift);
143 }
144 
145 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
146 static void tlb_batch_pmd_scan(struct mm_struct *mm, unsigned long vaddr,
147 			       pmd_t pmd)
148 {
149 	unsigned long end;
150 	pte_t *pte;
151 
152 	pte = pte_offset_map(&pmd, vaddr);
153 	if (!pte)
154 		return;
155 	end = vaddr + HPAGE_SIZE;
156 	while (vaddr < end) {
157 		if (pte_val(*pte) & _PAGE_VALID) {
158 			bool exec = pte_exec(*pte);
159 
160 			tlb_batch_add_one(mm, vaddr, exec, PAGE_SHIFT);
161 		}
162 		pte++;
163 		vaddr += PAGE_SIZE;
164 	}
165 	pte_unmap(pte);
166 }
167 
168 
169 static void __set_pmd_acct(struct mm_struct *mm, unsigned long addr,
170 			   pmd_t orig, pmd_t pmd)
171 {
172 	if (mm == &init_mm)
173 		return;
174 
175 	if ((pmd_val(pmd) ^ pmd_val(orig)) & _PAGE_PMD_HUGE) {
176 		/*
177 		 * Note that this routine only sets pmds for THP pages.
178 		 * Hugetlb pages are handled elsewhere.  We need to check
179 		 * for huge zero page.  Huge zero pages are like hugetlb
180 		 * pages in that there is no RSS, but there is the need
181 		 * for TSB entries.  So, huge zero page counts go into
182 		 * hugetlb_pte_count.
183 		 */
184 		if (pmd_val(pmd) & _PAGE_PMD_HUGE) {
185 			if (is_huge_zero_page(pmd_page(pmd)))
186 				mm->context.hugetlb_pte_count++;
187 			else
188 				mm->context.thp_pte_count++;
189 		} else {
190 			if (is_huge_zero_page(pmd_page(orig)))
191 				mm->context.hugetlb_pte_count--;
192 			else
193 				mm->context.thp_pte_count--;
194 		}
195 
196 		/* Do not try to allocate the TSB hash table if we
197 		 * don't have one already.  We have various locks held
198 		 * and thus we'll end up doing a GFP_KERNEL allocation
199 		 * in an atomic context.
200 		 *
201 		 * Instead, we let the first TLB miss on a hugepage
202 		 * take care of this.
203 		 */
204 	}
205 
206 	if (!pmd_none(orig)) {
207 		addr &= HPAGE_MASK;
208 		if (pmd_trans_huge(orig)) {
209 			pte_t orig_pte = __pte(pmd_val(orig));
210 			bool exec = pte_exec(orig_pte);
211 
212 			tlb_batch_add_one(mm, addr, exec, REAL_HPAGE_SHIFT);
213 			tlb_batch_add_one(mm, addr + REAL_HPAGE_SIZE, exec,
214 					  REAL_HPAGE_SHIFT);
215 		} else {
216 			tlb_batch_pmd_scan(mm, addr, orig);
217 		}
218 	}
219 }
220 
221 void set_pmd_at(struct mm_struct *mm, unsigned long addr,
222 		pmd_t *pmdp, pmd_t pmd)
223 {
224 	pmd_t orig = *pmdp;
225 
226 	*pmdp = pmd;
227 	__set_pmd_acct(mm, addr, orig, pmd);
228 }
229 
230 static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
231 		unsigned long address, pmd_t *pmdp, pmd_t pmd)
232 {
233 	pmd_t old;
234 
235 	do {
236 		old = *pmdp;
237 	} while (cmpxchg64(&pmdp->pmd, old.pmd, pmd.pmd) != old.pmd);
238 	__set_pmd_acct(vma->vm_mm, address, old, pmd);
239 
240 	return old;
241 }
242 
243 /*
244  * This routine is only called when splitting a THP
245  */
246 pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
247 		     pmd_t *pmdp)
248 {
249 	pmd_t old, entry;
250 
251 	entry = __pmd(pmd_val(*pmdp) & ~_PAGE_VALID);
252 	old = pmdp_establish(vma, address, pmdp, entry);
253 	flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
254 
255 	/*
256 	 * set_pmd_at() will not be called in a way to decrement
257 	 * thp_pte_count when splitting a THP, so do it now.
258 	 * Sanity check pmd before doing the actual decrement.
259 	 */
260 	if ((pmd_val(entry) & _PAGE_PMD_HUGE) &&
261 	    !is_huge_zero_page(pmd_page(entry)))
262 		(vma->vm_mm)->context.thp_pte_count--;
263 
264 	return old;
265 }
266 
267 void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
268 				pgtable_t pgtable)
269 {
270 	struct list_head *lh = (struct list_head *) pgtable;
271 
272 	assert_spin_locked(&mm->page_table_lock);
273 
274 	/* FIFO */
275 	if (!pmd_huge_pte(mm, pmdp))
276 		INIT_LIST_HEAD(lh);
277 	else
278 		list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
279 	pmd_huge_pte(mm, pmdp) = pgtable;
280 }
281 
282 pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
283 {
284 	struct list_head *lh;
285 	pgtable_t pgtable;
286 
287 	assert_spin_locked(&mm->page_table_lock);
288 
289 	/* FIFO */
290 	pgtable = pmd_huge_pte(mm, pmdp);
291 	lh = (struct list_head *) pgtable;
292 	if (list_empty(lh))
293 		pmd_huge_pte(mm, pmdp) = NULL;
294 	else {
295 		pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
296 		list_del(lh);
297 	}
298 	pte_val(pgtable[0]) = 0;
299 	pte_val(pgtable[1]) = 0;
300 
301 	return pgtable;
302 }
303 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
304