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