xref: /openbmc/linux/arch/arm64/mm/hugetlbpage.c (revision 57b8b211)
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 	/*
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 
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
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 
89 int pmd_huge(pmd_t pmd)
90 {
91 	return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
92 }
93 
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 
103 /*
104  * Select all bits except the pfn
105  */
106 static inline pgprot_t pte_pgprot(pte_t pte)
107 {
108 	unsigned long pfn = pte_pfn(pte);
109 
110 	return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte));
111 }
112 
113 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
114 			   pte_t *ptep, size_t *pgsize)
115 {
116 	pgd_t *pgdp = pgd_offset(mm, addr);
117 	p4d_t *p4dp;
118 	pud_t *pudp;
119 	pmd_t *pmdp;
120 
121 	*pgsize = PAGE_SIZE;
122 	p4dp = p4d_offset(pgdp, addr);
123 	pudp = pud_offset(p4dp, addr);
124 	pmdp = pmd_offset(pudp, addr);
125 	if ((pte_t *)pmdp == ptep) {
126 		*pgsize = PMD_SIZE;
127 		return CONT_PMDS;
128 	}
129 	return CONT_PTES;
130 }
131 
132 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
133 {
134 	int contig_ptes = 0;
135 
136 	*pgsize = size;
137 
138 	switch (size) {
139 #ifndef __PAGETABLE_PMD_FOLDED
140 	case PUD_SIZE:
141 		if (pud_sect_supported())
142 			contig_ptes = 1;
143 		break;
144 #endif
145 	case PMD_SIZE:
146 		contig_ptes = 1;
147 		break;
148 	case CONT_PMD_SIZE:
149 		*pgsize = PMD_SIZE;
150 		contig_ptes = CONT_PMDS;
151 		break;
152 	case CONT_PTE_SIZE:
153 		*pgsize = PAGE_SIZE;
154 		contig_ptes = CONT_PTES;
155 		break;
156 	}
157 
158 	return contig_ptes;
159 }
160 
161 pte_t huge_ptep_get(pte_t *ptep)
162 {
163 	int ncontig, i;
164 	size_t pgsize;
165 	pte_t orig_pte = ptep_get(ptep);
166 
167 	if (!pte_present(orig_pte) || !pte_cont(orig_pte))
168 		return orig_pte;
169 
170 	ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
171 	for (i = 0; i < ncontig; i++, ptep++) {
172 		pte_t pte = ptep_get(ptep);
173 
174 		if (pte_dirty(pte))
175 			orig_pte = pte_mkdirty(orig_pte);
176 
177 		if (pte_young(pte))
178 			orig_pte = pte_mkyoung(orig_pte);
179 	}
180 	return orig_pte;
181 }
182 
183 /*
184  * Changing some bits of contiguous entries requires us to follow a
185  * Break-Before-Make approach, breaking the whole contiguous set
186  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
187  * "Misprogramming of the Contiguous bit", page D4-1762.
188  *
189  * This helper performs the break step.
190  */
191 static pte_t get_clear_contig(struct mm_struct *mm,
192 			     unsigned long addr,
193 			     pte_t *ptep,
194 			     unsigned long pgsize,
195 			     unsigned long ncontig)
196 {
197 	pte_t orig_pte = ptep_get(ptep);
198 	unsigned long i;
199 
200 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
201 		pte_t pte = ptep_get_and_clear(mm, addr, ptep);
202 
203 		/*
204 		 * If HW_AFDBM is enabled, then the HW could turn on
205 		 * the dirty or accessed bit for any page in the set,
206 		 * so check them all.
207 		 */
208 		if (pte_dirty(pte))
209 			orig_pte = pte_mkdirty(orig_pte);
210 
211 		if (pte_young(pte))
212 			orig_pte = pte_mkyoung(orig_pte);
213 	}
214 	return orig_pte;
215 }
216 
217 /*
218  * Changing some bits of contiguous entries requires us to follow a
219  * Break-Before-Make approach, breaking the whole contiguous set
220  * before we can change any entries. See ARM DDI 0487A.k_iss10775,
221  * "Misprogramming of the Contiguous bit", page D4-1762.
222  *
223  * This helper performs the break step for use cases where the
224  * original pte is not needed.
225  */
226 static void clear_flush(struct mm_struct *mm,
227 			     unsigned long addr,
228 			     pte_t *ptep,
229 			     unsigned long pgsize,
230 			     unsigned long ncontig)
231 {
232 	struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
233 	unsigned long i, saddr = addr;
234 
235 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
236 		pte_clear(mm, addr, ptep);
237 
238 	flush_tlb_range(&vma, saddr, addr);
239 }
240 
241 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
242 			    pte_t *ptep, pte_t pte)
243 {
244 	size_t pgsize;
245 	int i;
246 	int ncontig;
247 	unsigned long pfn, dpfn;
248 	pgprot_t hugeprot;
249 
250 	/*
251 	 * Code needs to be expanded to handle huge swap and migration
252 	 * entries. Needed for HUGETLB and MEMORY_FAILURE.
253 	 */
254 	WARN_ON(!pte_present(pte));
255 
256 	if (!pte_cont(pte)) {
257 		set_pte_at(mm, addr, ptep, pte);
258 		return;
259 	}
260 
261 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
262 	pfn = pte_pfn(pte);
263 	dpfn = pgsize >> PAGE_SHIFT;
264 	hugeprot = pte_pgprot(pte);
265 
266 	clear_flush(mm, addr, ptep, pgsize, ncontig);
267 
268 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
269 		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
270 }
271 
272 void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
273 			  pte_t *ptep, pte_t pte, unsigned long sz)
274 {
275 	int i, ncontig;
276 	size_t pgsize;
277 
278 	ncontig = num_contig_ptes(sz, &pgsize);
279 
280 	for (i = 0; i < ncontig; i++, ptep++)
281 		set_pte(ptep, pte);
282 }
283 
284 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
285 		      unsigned long addr, unsigned long sz)
286 {
287 	pgd_t *pgdp;
288 	p4d_t *p4dp;
289 	pud_t *pudp;
290 	pmd_t *pmdp;
291 	pte_t *ptep = NULL;
292 
293 	pgdp = pgd_offset(mm, addr);
294 	p4dp = p4d_offset(pgdp, addr);
295 	pudp = pud_alloc(mm, p4dp, addr);
296 	if (!pudp)
297 		return NULL;
298 
299 	if (sz == PUD_SIZE) {
300 		ptep = (pte_t *)pudp;
301 	} else if (sz == (CONT_PTE_SIZE)) {
302 		pmdp = pmd_alloc(mm, pudp, addr);
303 		if (!pmdp)
304 			return NULL;
305 
306 		WARN_ON(addr & (sz - 1));
307 		/*
308 		 * Note that if this code were ever ported to the
309 		 * 32-bit arm platform then it will cause trouble in
310 		 * the case where CONFIG_HIGHPTE is set, since there
311 		 * will be no pte_unmap() to correspond with this
312 		 * pte_alloc_map().
313 		 */
314 		ptep = pte_alloc_map(mm, pmdp, addr);
315 	} else if (sz == PMD_SIZE) {
316 		if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp)))
317 			ptep = huge_pmd_share(mm, vma, addr, pudp);
318 		else
319 			ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
320 	} else if (sz == (CONT_PMD_SIZE)) {
321 		pmdp = pmd_alloc(mm, pudp, addr);
322 		WARN_ON(addr & (sz - 1));
323 		return (pte_t *)pmdp;
324 	}
325 
326 	return ptep;
327 }
328 
329 pte_t *huge_pte_offset(struct mm_struct *mm,
330 		       unsigned long addr, unsigned long sz)
331 {
332 	pgd_t *pgdp;
333 	p4d_t *p4dp;
334 	pud_t *pudp, pud;
335 	pmd_t *pmdp, pmd;
336 
337 	pgdp = pgd_offset(mm, addr);
338 	if (!pgd_present(READ_ONCE(*pgdp)))
339 		return NULL;
340 
341 	p4dp = p4d_offset(pgdp, addr);
342 	if (!p4d_present(READ_ONCE(*p4dp)))
343 		return NULL;
344 
345 	pudp = pud_offset(p4dp, addr);
346 	pud = READ_ONCE(*pudp);
347 	if (sz != PUD_SIZE && pud_none(pud))
348 		return NULL;
349 	/* hugepage or swap? */
350 	if (pud_huge(pud) || !pud_present(pud))
351 		return (pte_t *)pudp;
352 	/* table; check the next level */
353 
354 	if (sz == CONT_PMD_SIZE)
355 		addr &= CONT_PMD_MASK;
356 
357 	pmdp = pmd_offset(pudp, addr);
358 	pmd = READ_ONCE(*pmdp);
359 	if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
360 	    pmd_none(pmd))
361 		return NULL;
362 	if (pmd_huge(pmd) || !pmd_present(pmd))
363 		return (pte_t *)pmdp;
364 
365 	if (sz == CONT_PTE_SIZE)
366 		return pte_offset_kernel(pmdp, (addr & CONT_PTE_MASK));
367 
368 	return NULL;
369 }
370 
371 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
372 {
373 	size_t pagesize = 1UL << shift;
374 
375 	entry = pte_mkhuge(entry);
376 	if (pagesize == CONT_PTE_SIZE) {
377 		entry = pte_mkcont(entry);
378 	} else if (pagesize == CONT_PMD_SIZE) {
379 		entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
380 	} else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
381 		pr_warn("%s: unrecognized huge page size 0x%lx\n",
382 			__func__, pagesize);
383 	}
384 	return entry;
385 }
386 
387 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
388 		    pte_t *ptep, unsigned long sz)
389 {
390 	int i, ncontig;
391 	size_t pgsize;
392 
393 	ncontig = num_contig_ptes(sz, &pgsize);
394 
395 	for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
396 		pte_clear(mm, addr, ptep);
397 }
398 
399 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
400 			      unsigned long addr, pte_t *ptep)
401 {
402 	int ncontig;
403 	size_t pgsize;
404 	pte_t orig_pte = ptep_get(ptep);
405 
406 	if (!pte_cont(orig_pte))
407 		return ptep_get_and_clear(mm, addr, ptep);
408 
409 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
410 
411 	return get_clear_contig(mm, addr, ptep, pgsize, ncontig);
412 }
413 
414 /*
415  * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
416  * and write permission.
417  *
418  * For a contiguous huge pte range we need to check whether or not write
419  * permission has to change only on the first pte in the set. Then for
420  * all the contiguous ptes we need to check whether or not there is a
421  * discrepancy between dirty or young.
422  */
423 static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
424 {
425 	int i;
426 
427 	if (pte_write(pte) != pte_write(ptep_get(ptep)))
428 		return 1;
429 
430 	for (i = 0; i < ncontig; i++) {
431 		pte_t orig_pte = ptep_get(ptep + i);
432 
433 		if (pte_dirty(pte) != pte_dirty(orig_pte))
434 			return 1;
435 
436 		if (pte_young(pte) != pte_young(orig_pte))
437 			return 1;
438 	}
439 
440 	return 0;
441 }
442 
443 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
444 			       unsigned long addr, pte_t *ptep,
445 			       pte_t pte, int dirty)
446 {
447 	int ncontig, i;
448 	size_t pgsize = 0;
449 	unsigned long pfn = pte_pfn(pte), dpfn;
450 	pgprot_t hugeprot;
451 	pte_t orig_pte;
452 
453 	if (!pte_cont(pte))
454 		return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
455 
456 	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
457 	dpfn = pgsize >> PAGE_SHIFT;
458 
459 	if (!__cont_access_flags_changed(ptep, pte, ncontig))
460 		return 0;
461 
462 	orig_pte = get_clear_contig(vma->vm_mm, addr, ptep, pgsize, ncontig);
463 
464 	/* Make sure we don't lose the dirty or young state */
465 	if (pte_dirty(orig_pte))
466 		pte = pte_mkdirty(pte);
467 
468 	if (pte_young(orig_pte))
469 		pte = pte_mkyoung(pte);
470 
471 	hugeprot = pte_pgprot(pte);
472 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
473 		set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
474 
475 	return 1;
476 }
477 
478 void huge_ptep_set_wrprotect(struct mm_struct *mm,
479 			     unsigned long addr, pte_t *ptep)
480 {
481 	unsigned long pfn, dpfn;
482 	pgprot_t hugeprot;
483 	int ncontig, i;
484 	size_t pgsize;
485 	pte_t pte;
486 
487 	if (!pte_cont(READ_ONCE(*ptep))) {
488 		ptep_set_wrprotect(mm, addr, ptep);
489 		return;
490 	}
491 
492 	ncontig = find_num_contig(mm, addr, ptep, &pgsize);
493 	dpfn = pgsize >> PAGE_SHIFT;
494 
495 	pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
496 	pte = pte_wrprotect(pte);
497 
498 	hugeprot = pte_pgprot(pte);
499 	pfn = pte_pfn(pte);
500 
501 	for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
502 		set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
503 }
504 
505 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
506 			    unsigned long addr, pte_t *ptep)
507 {
508 	size_t pgsize;
509 	int ncontig;
510 	pte_t orig_pte;
511 
512 	if (!pte_cont(READ_ONCE(*ptep)))
513 		return ptep_clear_flush(vma, addr, ptep);
514 
515 	ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
516 	orig_pte = get_clear_contig(vma->vm_mm, addr, ptep, pgsize, ncontig);
517 	flush_tlb_range(vma, addr, addr + pgsize * ncontig);
518 	return orig_pte;
519 }
520 
521 static int __init hugetlbpage_init(void)
522 {
523 	if (pud_sect_supported())
524 		hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
525 
526 	hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
527 	hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
528 	hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
529 
530 	return 0;
531 }
532 arch_initcall(hugetlbpage_init);
533 
534 bool __init arch_hugetlb_valid_size(unsigned long size)
535 {
536 	return __hugetlb_valid_size(size);
537 }
538