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