Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / arm64 / mm / hugetlbpage.c
blob3215adf48a1b6c7a68f48b61bd2dbc21cd917450
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * arch/arm64/mm/hugetlbpage.c
5 * Copyright (C) 2013 Linaro Ltd.
7 * Based on arch/x86/mm/hugetlbpage.c.
8 */
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>
22 * HugeTLB Support Matrix
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 * ---------------------------------------------------
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.
38 #ifdef CONFIG_CMA
39 void __init arm64_hugetlb_cma_reserve(void)
41 int order;
43 if (pud_sect_supported())
44 order = PUD_SHIFT - PAGE_SHIFT;
45 else
46 order = CONT_PMD_SHIFT - PAGE_SHIFT;
48 hugetlb_cma_reserve(order);
50 #endif /* CONFIG_CMA */
52 static bool __hugetlb_valid_size(unsigned long size)
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;
65 return false;
68 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
69 bool arch_hugetlb_migration_supported(struct hstate *h)
71 size_t pagesize = huge_page_size(h);
73 if (!__hugetlb_valid_size(pagesize)) {
74 pr_warn("%s: unrecognized huge page size 0x%lx\n",
75 __func__, pagesize);
76 return false;
78 return true;
80 #endif
82 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
83 pte_t *ptep, size_t *pgsize)
85 pgd_t *pgdp = pgd_offset(mm, addr);
86 p4d_t *p4dp;
87 pud_t *pudp;
88 pmd_t *pmdp;
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;
98 return CONT_PTES;
101 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
103 int contig_ptes = 0;
105 *pgsize = size;
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;
127 return contig_ptes;
130 pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
132 int ncontig, i;
133 size_t pgsize;
134 pte_t orig_pte = __ptep_get(ptep);
136 if (!pte_present(orig_pte) || !pte_cont(orig_pte))
137 return orig_pte;
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);
143 if (pte_dirty(pte))
144 orig_pte = pte_mkdirty(orig_pte);
146 if (pte_young(pte))
147 orig_pte = pte_mkyoung(orig_pte);
149 return orig_pte;
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.
158 * This helper performs the break step.
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)
166 pte_t orig_pte = __ptep_get(ptep);
167 unsigned long i;
169 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
170 pte_t pte = __ptep_get_and_clear(mm, addr, ptep);
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.
177 if (pte_dirty(pte))
178 orig_pte = pte_mkdirty(orig_pte);
180 if (pte_young(pte))
181 orig_pte = pte_mkyoung(orig_pte);
183 return orig_pte;
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)
192 pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
193 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
195 flush_tlb_range(&vma, addr, addr + (pgsize * ncontig));
196 return orig_pte;
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.
205 * This helper performs the break step for use cases where the
206 * original pte is not needed.
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)
214 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
215 unsigned long i, saddr = addr;
217 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
218 __ptep_get_and_clear(mm, addr, ptep);
220 flush_tlb_range(&vma, saddr, addr);
223 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
224 pte_t *ptep, pte_t pte, unsigned long sz)
226 size_t pgsize;
227 int i;
228 int ncontig;
229 unsigned long pfn, dpfn;
230 pgprot_t hugeprot;
232 ncontig = num_contig_ptes(sz, &pgsize);
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;
240 if (!pte_cont(pte)) {
241 __set_ptes(mm, addr, ptep, pte, 1);
242 return;
245 pfn = pte_pfn(pte);
246 dpfn = pgsize >> PAGE_SHIFT;
247 hugeprot = pte_pgprot(pte);
249 clear_flush(mm, addr, ptep, pgsize, ncontig);
251 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
252 __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
255 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
256 unsigned long addr, unsigned long sz)
258 pgd_t *pgdp;
259 p4d_t *p4dp;
260 pud_t *pudp;
261 pmd_t *pmdp;
262 pte_t *ptep = NULL;
264 pgdp = pgd_offset(mm, addr);
265 p4dp = p4d_alloc(mm, pgdp, addr);
266 if (!p4dp)
267 return NULL;
269 pudp = pud_alloc(mm, p4dp, addr);
270 if (!pudp)
271 return NULL;
273 if (sz == PUD_SIZE) {
274 ptep = (pte_t *)pudp;
275 } else if (sz == (CONT_PTE_SIZE)) {
276 pmdp = pmd_alloc(mm, pudp, addr);
277 if (!pmdp)
278 return NULL;
280 WARN_ON(addr & (sz - 1));
281 ptep = pte_alloc_huge(mm, pmdp, addr);
282 } else if (sz == PMD_SIZE) {
283 if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp)))
284 ptep = huge_pmd_share(mm, vma, addr, pudp);
285 else
286 ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
287 } else if (sz == (CONT_PMD_SIZE)) {
288 pmdp = pmd_alloc(mm, pudp, addr);
289 WARN_ON(addr & (sz - 1));
290 return (pte_t *)pmdp;
293 return ptep;
296 pte_t *huge_pte_offset(struct mm_struct *mm,
297 unsigned long addr, unsigned long sz)
299 pgd_t *pgdp;
300 p4d_t *p4dp;
301 pud_t *pudp, pud;
302 pmd_t *pmdp, pmd;
304 pgdp = pgd_offset(mm, addr);
305 if (!pgd_present(READ_ONCE(*pgdp)))
306 return NULL;
308 p4dp = p4d_offset(pgdp, addr);
309 if (!p4d_present(READ_ONCE(*p4dp)))
310 return NULL;
312 pudp = pud_offset(p4dp, addr);
313 pud = READ_ONCE(*pudp);
314 if (sz != PUD_SIZE && pud_none(pud))
315 return NULL;
316 /* hugepage or swap? */
317 if (pud_leaf(pud) || !pud_present(pud))
318 return (pte_t *)pudp;
319 /* table; check the next level */
321 if (sz == CONT_PMD_SIZE)
322 addr &= CONT_PMD_MASK;
324 pmdp = pmd_offset(pudp, addr);
325 pmd = READ_ONCE(*pmdp);
326 if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
327 pmd_none(pmd))
328 return NULL;
329 if (pmd_leaf(pmd) || !pmd_present(pmd))
330 return (pte_t *)pmdp;
332 if (sz == CONT_PTE_SIZE)
333 return pte_offset_huge(pmdp, (addr & CONT_PTE_MASK));
335 return NULL;
338 unsigned long hugetlb_mask_last_page(struct hstate *h)
340 unsigned long hp_size = huge_page_size(h);
342 switch (hp_size) {
343 #ifndef __PAGETABLE_PMD_FOLDED
344 case PUD_SIZE:
345 return PGDIR_SIZE - PUD_SIZE;
346 #endif
347 case CONT_PMD_SIZE:
348 return PUD_SIZE - CONT_PMD_SIZE;
349 case PMD_SIZE:
350 return PUD_SIZE - PMD_SIZE;
351 case CONT_PTE_SIZE:
352 return PMD_SIZE - CONT_PTE_SIZE;
353 default:
354 break;
357 return 0UL;
360 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
362 size_t pagesize = 1UL << shift;
364 switch (pagesize) {
365 #ifndef __PAGETABLE_PMD_FOLDED
366 case PUD_SIZE:
367 entry = pud_pte(pud_mkhuge(pte_pud(entry)));
368 break;
369 #endif
370 case CONT_PMD_SIZE:
371 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
372 fallthrough;
373 case PMD_SIZE:
374 entry = pmd_pte(pmd_mkhuge(pte_pmd(entry)));
375 break;
376 case CONT_PTE_SIZE:
377 entry = pte_mkcont(entry);
378 break;
379 default:
380 pr_warn("%s: unrecognized huge page size 0x%lx\n",
381 __func__, pagesize);
382 break;
384 return entry;
387 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
388 pte_t *ptep, unsigned long sz)
390 int i, ncontig;
391 size_t pgsize;
393 ncontig = num_contig_ptes(sz, &pgsize);
395 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
396 __pte_clear(mm, addr, ptep);
399 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
400 unsigned long addr, pte_t *ptep)
402 int ncontig;
403 size_t pgsize;
404 pte_t orig_pte = __ptep_get(ptep);
406 if (!pte_cont(orig_pte))
407 return __ptep_get_and_clear(mm, addr, ptep);
409 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
411 return get_clear_contig(mm, addr, ptep, pgsize, ncontig);
415 * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
416 * and write permission.
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.
423 static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
425 int i;
427 if (pte_write(pte) != pte_write(__ptep_get(ptep)))
428 return 1;
430 for (i = 0; i < ncontig; i++) {
431 pte_t orig_pte = __ptep_get(ptep + i);
433 if (pte_dirty(pte) != pte_dirty(orig_pte))
434 return 1;
436 if (pte_young(pte) != pte_young(orig_pte))
437 return 1;
440 return 0;
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)
447 int ncontig, i;
448 size_t pgsize = 0;
449 unsigned long pfn = pte_pfn(pte), dpfn;
450 struct mm_struct *mm = vma->vm_mm;
451 pgprot_t hugeprot;
452 pte_t orig_pte;
454 if (!pte_cont(pte))
455 return __ptep_set_access_flags(vma, addr, ptep, pte, dirty);
457 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
458 dpfn = pgsize >> PAGE_SHIFT;
460 if (!__cont_access_flags_changed(ptep, pte, ncontig))
461 return 0;
463 orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
465 /* Make sure we don't lose the dirty or young state */
466 if (pte_dirty(orig_pte))
467 pte = pte_mkdirty(pte);
469 if (pte_young(orig_pte))
470 pte = pte_mkyoung(pte);
472 hugeprot = pte_pgprot(pte);
473 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
474 __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
476 return 1;
479 void huge_ptep_set_wrprotect(struct mm_struct *mm,
480 unsigned long addr, pte_t *ptep)
482 unsigned long pfn, dpfn;
483 pgprot_t hugeprot;
484 int ncontig, i;
485 size_t pgsize;
486 pte_t pte;
488 if (!pte_cont(__ptep_get(ptep))) {
489 __ptep_set_wrprotect(mm, addr, ptep);
490 return;
493 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
494 dpfn = pgsize >> PAGE_SHIFT;
496 pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
497 pte = pte_wrprotect(pte);
499 hugeprot = pte_pgprot(pte);
500 pfn = pte_pfn(pte);
502 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
503 __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
506 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
507 unsigned long addr, pte_t *ptep)
509 struct mm_struct *mm = vma->vm_mm;
510 size_t pgsize;
511 int ncontig;
513 if (!pte_cont(__ptep_get(ptep)))
514 return ptep_clear_flush(vma, addr, ptep);
516 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
517 return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
520 static int __init hugetlbpage_init(void)
522 if (pud_sect_supported())
523 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
525 hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
526 hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
527 hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
529 return 0;
531 arch_initcall(hugetlbpage_init);
533 bool __init arch_hugetlb_valid_size(unsigned long size)
535 return __hugetlb_valid_size(size);
538 pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
540 if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
542 * Break-before-make (BBM) is required for all user space mappings
543 * when the permission changes from executable to non-executable
544 * in cases where cpu is affected with errata #2645198.
546 if (pte_user_exec(__ptep_get(ptep)))
547 return huge_ptep_clear_flush(vma, addr, ptep);
549 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
552 void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
553 pte_t old_pte, pte_t pte)
555 unsigned long psize = huge_page_size(hstate_vma(vma));
557 set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);