xref: /linux/arch/arm64/mm/hugetlbpage.c (revision 881f1bb5e25c8982ed963b2d319fc0fc732e55db)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch/arm64/mm/hugetlbpage.c
4  *
5  * Copyright (C) 2013 Linaro Ltd.
6  *
7  * Based on arch/x86/mm/hugetlbpage.c.
8  */
9 
10 #include <linux/init.h>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/err.h>
16 #include <linux/sysctl.h>
17 #include <asm/mman.h>
18 #include <asm/tlb.h>
19 #include <asm/tlbflush.h>
20 
21 /*
22  * HugeTLB Support Matrix
23  *
24  * ---------------------------------------------------
25  * | Page Size | CONT PTE |  PMD  | CONT PMD |  PUD  |
26  * ---------------------------------------------------
27  * |     4K    |   64K    |   2M  |    32M   |   1G  |
28  * |    16K    |    2M    |  32M  |     1G   |       |
29  * |    64K    |    2M    | 512M  |    16G   |       |
30  * ---------------------------------------------------
31  */
32 
33 /*
34  * Reserve CMA areas for the largest supported gigantic
35  * huge page when requested. Any other smaller gigantic
36  * huge pages could still be served from those areas.
37  */
38 #ifdef CONFIG_CMA
39 void __init arm64_hugetlb_cma_reserve(void)
40 {
41 	int order;
42 
43 	if (pud_sect_supported())
44 		order = PUD_SHIFT - PAGE_SHIFT;
45 	else
46 		order = CONT_PMD_SHIFT - PAGE_SHIFT;
47 
48 	hugetlb_cma_reserve(order);
49 }
50 #endif /* CONFIG_CMA */
51 
52 static bool __hugetlb_valid_size(unsigned long size)
53 {
54 	switch (size) {
55 #ifndef __PAGETABLE_PMD_FOLDED
56 	case PUD_SIZE:
57 		return pud_sect_supported();
58 #endif
59 	case CONT_PMD_SIZE:
60 	case PMD_SIZE:
61 	case CONT_PTE_SIZE:
62 		return true;
63 	}
64 
65 	return false;
66 }
67 
68 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
69 bool arch_hugetlb_migration_supported(struct hstate *h)
70 {
71 	size_t pagesize = huge_page_size(h);
72 
73 	if (!__hugetlb_valid_size(pagesize)) {
74 		pr_warn("%s: unrecognized huge page size 0x%lx\n",
75 			__func__, pagesize);
76 		return false;
77 	}
78 	return true;
79 }
80 #endif
81 
82 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
83 			   pte_t *ptep, size_t *pgsize)
84 {
85 	pgd_t *pgdp = pgd_offset(mm, addr);
86 	p4d_t *p4dp;
87 	pud_t *pudp;
88 	pmd_t *pmdp;
89 
90 	*pgsize = PAGE_SIZE;
91 	p4dp = p4d_offset(pgdp, addr);
92 	pudp = pud_offset(p4dp, addr);
93 	pmdp = pmd_offset(pudp, addr);
94 	if ((pte_t *)pmdp == ptep) {
95 		*pgsize = PMD_SIZE;
96 		return CONT_PMDS;
97 	}
98 	return CONT_PTES;
99 }
100 
101 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
102 {
103 	int contig_ptes = 0;
104 
105 	*pgsize = size;
106 
107 	switch (size) {
108 #ifndef __PAGETABLE_PMD_FOLDED
109 	case PUD_SIZE:
110 		if (pud_sect_supported())
111 			contig_ptes = 1;
112 		break;
113 #endif
114 	case PMD_SIZE:
115 		contig_ptes = 1;
116 		break;
117 	case CONT_PMD_SIZE:
118 		*pgsize = PMD_SIZE;
119 		contig_ptes = CONT_PMDS;
120 		break;
121 	case CONT_PTE_SIZE:
122 		*pgsize = PAGE_SIZE;
123 		contig_ptes = CONT_PTES;
124 		break;
125 	}
126 
127 	return contig_ptes;
128 }
129 
130 pte_t huge_ptep_get(pte_t *ptep)
131 {
132 	int ncontig, i;
133 	size_t pgsize;
134 	pte_t orig_pte = __ptep_get(ptep);
135 
136 	if (!pte_present(orig_pte) || !pte_cont(orig_pte))
137 		return orig_pte;
138 
139 	ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
140 	for (i = 0; i < ncontig; i++, ptep++) {
141 		pte_t pte = __ptep_get(ptep);
142 
143 		if (pte_dirty(pte))
144 			orig_pte = pte_mkdirty(orig_pte);
145 
146 		if (pte_young(pte))
147 			orig_pte = pte_mkyoung(orig_pte);
148 	}
149 	return orig_pte;
150 }
151 
152 /*
153  * Changing some bits of contiguous entries requires us to follow a
154  * Break-Before-Make approach, breaking the whole contiguous set
155  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
156  * "Misprogramming of the Contiguous bit", page D4-1762.
157  *
158  * This helper performs the break step.
159  */
160 static pte_t get_clear_contig(struct mm_struct *mm,
161 			     unsigned long addr,
162 			     pte_t *ptep,
163 			     unsigned long pgsize,
164 			     unsigned long ncontig)
165 {
166 	pte_t orig_pte = __ptep_get(ptep);
167 	unsigned long i;
168 
169 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
170 		pte_t pte = __ptep_get_and_clear(mm, addr, ptep);
171 
172 		/*
173 		 * If HW_AFDBM is enabled, then the HW could turn on
174 		 * the dirty or accessed bit for any page in the set,
175 		 * so check them all.
176 		 */
177 		if (pte_dirty(pte))
178 			orig_pte = pte_mkdirty(orig_pte);
179 
180 		if (pte_young(pte))
181 			orig_pte = pte_mkyoung(orig_pte);
182 	}
183 	return orig_pte;
184 }
185 
186 static pte_t get_clear_contig_flush(struct mm_struct *mm,
187 				    unsigned long addr,
188 				    pte_t *ptep,
189 				    unsigned long pgsize,
190 				    unsigned long ncontig)
191 {
192 	pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
193 	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
194 
195 	flush_tlb_range(&vma, addr, addr + (pgsize * ncontig));
196 	return orig_pte;
197 }
198 
199 /*
200  * Changing some bits of contiguous entries requires us to follow a
201  * Break-Before-Make approach, breaking the whole contiguous set
202  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
203  * "Misprogramming of the Contiguous bit", page D4-1762.
204  *
205  * This helper performs the break step for use cases where the
206  * original pte is not needed.
207  */
208 static void clear_flush(struct mm_struct *mm,
209 			     unsigned long addr,
210 			     pte_t *ptep,
211 			     unsigned long pgsize,
212 			     unsigned long ncontig)
213 {
214 	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
215 	unsigned long i, saddr = addr;
216 
217 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
218 		__ptep_get_and_clear(mm, addr, ptep);
219 
220 	flush_tlb_range(&vma, saddr, addr);
221 }
222 
223 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
224 			    pte_t *ptep, pte_t pte, unsigned long sz)
225 {
226 	size_t pgsize;
227 	int i;
228 	int ncontig;
229 	unsigned long pfn, dpfn;
230 	pgprot_t hugeprot;
231 
232 	ncontig = num_contig_ptes(sz, &pgsize);
233 
234 	if (!pte_present(pte)) {
235 		for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
236 			__set_ptes(mm, addr, ptep, pte, 1);
237 		return;
238 	}
239 
240 	if (!pte_cont(pte)) {
241 		__set_ptes(mm, addr, ptep, pte, 1);
242 		return;
243 	}
244 
245 	pfn = pte_pfn(pte);
246 	dpfn = pgsize >> PAGE_SHIFT;
247 	hugeprot = pte_pgprot(pte);
248 
249 	clear_flush(mm, addr, ptep, pgsize, ncontig);
250 
251 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
252 		__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
253 }
254 
255 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
256 		      unsigned long addr, unsigned long sz)
257 {
258 	pgd_t *pgdp;
259 	p4d_t *p4dp;
260 	pud_t *pudp;
261 	pmd_t *pmdp;
262 	pte_t *ptep = NULL;
263 
264 	pgdp = pgd_offset(mm, addr);
265 	p4dp = p4d_offset(pgdp, addr);
266 	pudp = pud_alloc(mm, p4dp, addr);
267 	if (!pudp)
268 		return NULL;
269 
270 	if (sz == PUD_SIZE) {
271 		ptep = (pte_t *)pudp;
272 	} else if (sz == (CONT_PTE_SIZE)) {
273 		pmdp = pmd_alloc(mm, pudp, addr);
274 		if (!pmdp)
275 			return NULL;
276 
277 		WARN_ON(addr & (sz - 1));
278 		ptep = pte_alloc_huge(mm, pmdp, addr);
279 	} else if (sz == PMD_SIZE) {
280 		if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp)))
281 			ptep = huge_pmd_share(mm, vma, addr, pudp);
282 		else
283 			ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
284 	} else if (sz == (CONT_PMD_SIZE)) {
285 		pmdp = pmd_alloc(mm, pudp, addr);
286 		WARN_ON(addr & (sz - 1));
287 		return (pte_t *)pmdp;
288 	}
289 
290 	return ptep;
291 }
292 
293 pte_t *huge_pte_offset(struct mm_struct *mm,
294 		       unsigned long addr, unsigned long sz)
295 {
296 	pgd_t *pgdp;
297 	p4d_t *p4dp;
298 	pud_t *pudp, pud;
299 	pmd_t *pmdp, pmd;
300 
301 	pgdp = pgd_offset(mm, addr);
302 	if (!pgd_present(READ_ONCE(*pgdp)))
303 		return NULL;
304 
305 	p4dp = p4d_offset(pgdp, addr);
306 	if (!p4d_present(READ_ONCE(*p4dp)))
307 		return NULL;
308 
309 	pudp = pud_offset(p4dp, addr);
310 	pud = READ_ONCE(*pudp);
311 	if (sz != PUD_SIZE && pud_none(pud))
312 		return NULL;
313 	/* hugepage or swap? */
314 	if (pud_leaf(pud) || !pud_present(pud))
315 		return (pte_t *)pudp;
316 	/* table; check the next level */
317 
318 	if (sz == CONT_PMD_SIZE)
319 		addr &= CONT_PMD_MASK;
320 
321 	pmdp = pmd_offset(pudp, addr);
322 	pmd = READ_ONCE(*pmdp);
323 	if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
324 	    pmd_none(pmd))
325 		return NULL;
326 	if (pmd_leaf(pmd) || !pmd_present(pmd))
327 		return (pte_t *)pmdp;
328 
329 	if (sz == CONT_PTE_SIZE)
330 		return pte_offset_huge(pmdp, (addr & CONT_PTE_MASK));
331 
332 	return NULL;
333 }
334 
335 unsigned long hugetlb_mask_last_page(struct hstate *h)
336 {
337 	unsigned long hp_size = huge_page_size(h);
338 
339 	switch (hp_size) {
340 #ifndef __PAGETABLE_PMD_FOLDED
341 	case PUD_SIZE:
342 		return PGDIR_SIZE - PUD_SIZE;
343 #endif
344 	case CONT_PMD_SIZE:
345 		return PUD_SIZE - CONT_PMD_SIZE;
346 	case PMD_SIZE:
347 		return PUD_SIZE - PMD_SIZE;
348 	case CONT_PTE_SIZE:
349 		return PMD_SIZE - CONT_PTE_SIZE;
350 	default:
351 		break;
352 	}
353 
354 	return 0UL;
355 }
356 
357 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
358 {
359 	size_t pagesize = 1UL << shift;
360 
361 	entry = pte_mkhuge(entry);
362 	if (pagesize == CONT_PTE_SIZE) {
363 		entry = pte_mkcont(entry);
364 	} else if (pagesize == CONT_PMD_SIZE) {
365 		entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
366 	} else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
367 		pr_warn("%s: unrecognized huge page size 0x%lx\n",
368 			__func__, pagesize);
369 	}
370 	return entry;
371 }
372 
373 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
374 		    pte_t *ptep, unsigned long sz)
375 {
376 	int i, ncontig;
377 	size_t pgsize;
378 
379 	ncontig = num_contig_ptes(sz, &pgsize);
380 
381 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
382 		__pte_clear(mm, addr, ptep);
383 }
384 
385 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
386 			      unsigned long addr, pte_t *ptep)
387 {
388 	int ncontig;
389 	size_t pgsize;
390 	pte_t orig_pte = __ptep_get(ptep);
391 
392 	if (!pte_cont(orig_pte))
393 		return __ptep_get_and_clear(mm, addr, ptep);
394 
395 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
396 
397 	return get_clear_contig(mm, addr, ptep, pgsize, ncontig);
398 }
399 
400 /*
401  * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
402  * and write permission.
403  *
404  * For a contiguous huge pte range we need to check whether or not write
405  * permission has to change only on the first pte in the set. Then for
406  * all the contiguous ptes we need to check whether or not there is a
407  * discrepancy between dirty or young.
408  */
409 static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
410 {
411 	int i;
412 
413 	if (pte_write(pte) != pte_write(__ptep_get(ptep)))
414 		return 1;
415 
416 	for (i = 0; i < ncontig; i++) {
417 		pte_t orig_pte = __ptep_get(ptep + i);
418 
419 		if (pte_dirty(pte) != pte_dirty(orig_pte))
420 			return 1;
421 
422 		if (pte_young(pte) != pte_young(orig_pte))
423 			return 1;
424 	}
425 
426 	return 0;
427 }
428 
429 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
430 			       unsigned long addr, pte_t *ptep,
431 			       pte_t pte, int dirty)
432 {
433 	int ncontig, i;
434 	size_t pgsize = 0;
435 	unsigned long pfn = pte_pfn(pte), dpfn;
436 	struct mm_struct *mm = vma->vm_mm;
437 	pgprot_t hugeprot;
438 	pte_t orig_pte;
439 
440 	if (!pte_cont(pte))
441 		return __ptep_set_access_flags(vma, addr, ptep, pte, dirty);
442 
443 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
444 	dpfn = pgsize >> PAGE_SHIFT;
445 
446 	if (!__cont_access_flags_changed(ptep, pte, ncontig))
447 		return 0;
448 
449 	orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
450 
451 	/* Make sure we don't lose the dirty or young state */
452 	if (pte_dirty(orig_pte))
453 		pte = pte_mkdirty(pte);
454 
455 	if (pte_young(orig_pte))
456 		pte = pte_mkyoung(pte);
457 
458 	hugeprot = pte_pgprot(pte);
459 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
460 		__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
461 
462 	return 1;
463 }
464 
465 void huge_ptep_set_wrprotect(struct mm_struct *mm,
466 			     unsigned long addr, pte_t *ptep)
467 {
468 	unsigned long pfn, dpfn;
469 	pgprot_t hugeprot;
470 	int ncontig, i;
471 	size_t pgsize;
472 	pte_t pte;
473 
474 	if (!pte_cont(__ptep_get(ptep))) {
475 		__ptep_set_wrprotect(mm, addr, ptep);
476 		return;
477 	}
478 
479 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
480 	dpfn = pgsize >> PAGE_SHIFT;
481 
482 	pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
483 	pte = pte_wrprotect(pte);
484 
485 	hugeprot = pte_pgprot(pte);
486 	pfn = pte_pfn(pte);
487 
488 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
489 		__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
490 }
491 
492 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
493 			    unsigned long addr, pte_t *ptep)
494 {
495 	struct mm_struct *mm = vma->vm_mm;
496 	size_t pgsize;
497 	int ncontig;
498 
499 	if (!pte_cont(__ptep_get(ptep)))
500 		return ptep_clear_flush(vma, addr, ptep);
501 
502 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
503 	return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
504 }
505 
506 static int __init hugetlbpage_init(void)
507 {
508 	if (pud_sect_supported())
509 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
510 
511 	hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
512 	hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
513 	hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
514 
515 	return 0;
516 }
517 arch_initcall(hugetlbpage_init);
518 
519 bool __init arch_hugetlb_valid_size(unsigned long size)
520 {
521 	return __hugetlb_valid_size(size);
522 }
523 
524 pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
525 {
526 	if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
527 		/*
528 		 * Break-before-make (BBM) is required for all user space mappings
529 		 * when the permission changes from executable to non-executable
530 		 * in cases where cpu is affected with errata #2645198.
531 		 */
532 		if (pte_user_exec(__ptep_get(ptep)))
533 			return huge_ptep_clear_flush(vma, addr, ptep);
534 	}
535 	return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
536 }
537 
538 void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
539 				  pte_t old_pte, pte_t pte)
540 {
541 	unsigned long psize = huge_page_size(hstate_vma(vma));
542 
543 	set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
544 }
545