1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm64/mm/hugetlbpage.c
5 * Copyright (C) 2013 Linaro Ltd.
7 * Based on arch/x86/mm/hugetlbpage.c.
10 #include <linux/init.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/err.h>
16 #include <linux/sysctl.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.
39 void __init arm64_hugetlb_cma_reserve(void)
43 if (pud_sect_supported())
44 order = PUD_SHIFT - PAGE_SHIFT;
46 order = CONT_PMD_SHIFT - PAGE_SHIFT;
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.
54 WARN_ON(order <= MAX_ORDER);
55 hugetlb_cma_reserve(order);
57 #endif /* CONFIG_CMA */
59 static bool __hugetlb_valid_size(unsigned long size)
62 #ifndef __PAGETABLE_PMD_FOLDED
64 return pud_sect_supported();
75 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
76 bool arch_hugetlb_migration_supported(struct hstate *h)
78 size_t pagesize = huge_page_size(h);
80 if (!__hugetlb_valid_size(pagesize)) {
81 pr_warn("%s: unrecognized huge page size 0x%lx\n",
89 int pmd_huge(pmd_t pmd)
91 return pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT);
94 int pud_huge(pud_t pud)
96 #ifndef __PAGETABLE_PMD_FOLDED
97 return pud_val(pud) && !(pud_val(pud) & PUD_TABLE_BIT);
103 static int find_num_contig(struct mm_struct *mm, unsigned long addr,
104 pte_t *ptep, size_t *pgsize)
106 pgd_t *pgdp = pgd_offset(mm, addr);
112 p4dp = p4d_offset(pgdp, addr);
113 pudp = pud_offset(p4dp, addr);
114 pmdp = pmd_offset(pudp, addr);
115 if ((pte_t *)pmdp == ptep) {
122 static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
129 #ifndef __PAGETABLE_PMD_FOLDED
131 if (pud_sect_supported())
140 contig_ptes = CONT_PMDS;
144 contig_ptes = CONT_PTES;
151 pte_t huge_ptep_get(pte_t *ptep)
155 pte_t orig_pte = ptep_get(ptep);
157 if (!pte_present(orig_pte) || !pte_cont(orig_pte))
160 ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize);
161 for (i = 0; i < ncontig; i++, ptep++) {
162 pte_t pte = ptep_get(ptep);
165 orig_pte = pte_mkdirty(orig_pte);
168 orig_pte = pte_mkyoung(orig_pte);
174 * Changing some bits of contiguous entries requires us to follow a
175 * Break-Before-Make approach, breaking the whole contiguous set
176 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
177 * "Misprogramming of the Contiguous bit", page D4-1762.
179 * This helper performs the break step.
181 static pte_t get_clear_contig(struct mm_struct *mm,
184 unsigned long pgsize,
185 unsigned long ncontig)
187 pte_t orig_pte = ptep_get(ptep);
190 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) {
191 pte_t pte = ptep_get_and_clear(mm, addr, ptep);
194 * If HW_AFDBM is enabled, then the HW could turn on
195 * the dirty or accessed bit for any page in the set,
199 orig_pte = pte_mkdirty(orig_pte);
202 orig_pte = pte_mkyoung(orig_pte);
207 static pte_t get_clear_contig_flush(struct mm_struct *mm,
210 unsigned long pgsize,
211 unsigned long ncontig)
213 pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
214 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
216 flush_tlb_range(&vma, addr, addr + (pgsize * ncontig));
221 * Changing some bits of contiguous entries requires us to follow a
222 * Break-Before-Make approach, breaking the whole contiguous set
223 * before we can change any entries. See ARM DDI 0487A.k_iss10775,
224 * "Misprogramming of the Contiguous bit", page D4-1762.
226 * This helper performs the break step for use cases where the
227 * original pte is not needed.
229 static void clear_flush(struct mm_struct *mm,
232 unsigned long pgsize,
233 unsigned long ncontig)
235 struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
236 unsigned long i, saddr = addr;
238 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
239 ptep_clear(mm, addr, ptep);
241 flush_tlb_range(&vma, saddr, addr);
244 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
245 pte_t *ptep, pte_t pte, unsigned long sz)
250 unsigned long pfn, dpfn;
253 ncontig = num_contig_ptes(sz, &pgsize);
255 if (!pte_present(pte)) {
256 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
257 set_pte_at(mm, addr, ptep, pte);
261 if (!pte_cont(pte)) {
262 set_pte_at(mm, addr, ptep, pte);
267 dpfn = pgsize >> PAGE_SHIFT;
268 hugeprot = pte_pgprot(pte);
270 clear_flush(mm, addr, ptep, pgsize, ncontig);
272 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
273 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
276 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
277 unsigned long addr, unsigned long sz)
285 pgdp = pgd_offset(mm, addr);
286 p4dp = p4d_offset(pgdp, addr);
287 pudp = pud_alloc(mm, p4dp, addr);
291 if (sz == PUD_SIZE) {
292 ptep = (pte_t *)pudp;
293 } else if (sz == (CONT_PTE_SIZE)) {
294 pmdp = pmd_alloc(mm, pudp, addr);
298 WARN_ON(addr & (sz - 1));
299 ptep = pte_alloc_huge(mm, pmdp, addr);
300 } else if (sz == PMD_SIZE) {
301 if (want_pmd_share(vma, addr) && pud_none(READ_ONCE(*pudp)))
302 ptep = huge_pmd_share(mm, vma, addr, pudp);
304 ptep = (pte_t *)pmd_alloc(mm, pudp, addr);
305 } else if (sz == (CONT_PMD_SIZE)) {
306 pmdp = pmd_alloc(mm, pudp, addr);
307 WARN_ON(addr & (sz - 1));
308 return (pte_t *)pmdp;
314 pte_t *huge_pte_offset(struct mm_struct *mm,
315 unsigned long addr, unsigned long sz)
322 pgdp = pgd_offset(mm, addr);
323 if (!pgd_present(READ_ONCE(*pgdp)))
326 p4dp = p4d_offset(pgdp, addr);
327 if (!p4d_present(READ_ONCE(*p4dp)))
330 pudp = pud_offset(p4dp, addr);
331 pud = READ_ONCE(*pudp);
332 if (sz != PUD_SIZE && pud_none(pud))
334 /* hugepage or swap? */
335 if (pud_huge(pud) || !pud_present(pud))
336 return (pte_t *)pudp;
337 /* table; check the next level */
339 if (sz == CONT_PMD_SIZE)
340 addr &= CONT_PMD_MASK;
342 pmdp = pmd_offset(pudp, addr);
343 pmd = READ_ONCE(*pmdp);
344 if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
347 if (pmd_huge(pmd) || !pmd_present(pmd))
348 return (pte_t *)pmdp;
350 if (sz == CONT_PTE_SIZE)
351 return pte_offset_huge(pmdp, (addr & CONT_PTE_MASK));
356 unsigned long hugetlb_mask_last_page(struct hstate *h)
358 unsigned long hp_size = huge_page_size(h);
361 #ifndef __PAGETABLE_PMD_FOLDED
363 return PGDIR_SIZE - PUD_SIZE;
366 return PUD_SIZE - CONT_PMD_SIZE;
368 return PUD_SIZE - PMD_SIZE;
370 return PMD_SIZE - CONT_PTE_SIZE;
378 pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
380 size_t pagesize = 1UL << shift;
382 entry = pte_mkhuge(entry);
383 if (pagesize == CONT_PTE_SIZE) {
384 entry = pte_mkcont(entry);
385 } else if (pagesize == CONT_PMD_SIZE) {
386 entry = pmd_pte(pmd_mkcont(pte_pmd(entry)));
387 } else if (pagesize != PUD_SIZE && pagesize != PMD_SIZE) {
388 pr_warn("%s: unrecognized huge page size 0x%lx\n",
394 void huge_pte_clear(struct mm_struct *mm, unsigned long addr,
395 pte_t *ptep, unsigned long sz)
400 ncontig = num_contig_ptes(sz, &pgsize);
402 for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
403 pte_clear(mm, addr, ptep);
406 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
407 unsigned long addr, pte_t *ptep)
411 pte_t orig_pte = ptep_get(ptep);
413 if (!pte_cont(orig_pte))
414 return ptep_get_and_clear(mm, addr, ptep);
416 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
418 return get_clear_contig(mm, addr, ptep, pgsize, ncontig);
422 * huge_ptep_set_access_flags will update access flags (dirty, accesssed)
423 * and write permission.
425 * For a contiguous huge pte range we need to check whether or not write
426 * permission has to change only on the first pte in the set. Then for
427 * all the contiguous ptes we need to check whether or not there is a
428 * discrepancy between dirty or young.
430 static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig)
434 if (pte_write(pte) != pte_write(ptep_get(ptep)))
437 for (i = 0; i < ncontig; i++) {
438 pte_t orig_pte = ptep_get(ptep + i);
440 if (pte_dirty(pte) != pte_dirty(orig_pte))
443 if (pte_young(pte) != pte_young(orig_pte))
450 int huge_ptep_set_access_flags(struct vm_area_struct *vma,
451 unsigned long addr, pte_t *ptep,
452 pte_t pte, int dirty)
456 unsigned long pfn = pte_pfn(pte), dpfn;
457 struct mm_struct *mm = vma->vm_mm;
462 return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
464 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
465 dpfn = pgsize >> PAGE_SHIFT;
467 if (!__cont_access_flags_changed(ptep, pte, ncontig))
470 orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
472 /* Make sure we don't lose the dirty or young state */
473 if (pte_dirty(orig_pte))
474 pte = pte_mkdirty(pte);
476 if (pte_young(orig_pte))
477 pte = pte_mkyoung(pte);
479 hugeprot = pte_pgprot(pte);
480 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
481 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
486 void huge_ptep_set_wrprotect(struct mm_struct *mm,
487 unsigned long addr, pte_t *ptep)
489 unsigned long pfn, dpfn;
495 if (!pte_cont(READ_ONCE(*ptep))) {
496 ptep_set_wrprotect(mm, addr, ptep);
500 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
501 dpfn = pgsize >> PAGE_SHIFT;
503 pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
504 pte = pte_wrprotect(pte);
506 hugeprot = pte_pgprot(pte);
509 for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
510 set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
513 pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
514 unsigned long addr, pte_t *ptep)
516 struct mm_struct *mm = vma->vm_mm;
520 if (!pte_cont(READ_ONCE(*ptep)))
521 return ptep_clear_flush(vma, addr, ptep);
523 ncontig = find_num_contig(mm, addr, ptep, &pgsize);
524 return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
527 static int __init hugetlbpage_init(void)
529 if (pud_sect_supported())
530 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
532 hugetlb_add_hstate(CONT_PMD_SHIFT - PAGE_SHIFT);
533 hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
534 hugetlb_add_hstate(CONT_PTE_SHIFT - PAGE_SHIFT);
538 arch_initcall(hugetlbpage_init);
540 bool __init arch_hugetlb_valid_size(unsigned long size)
542 return __hugetlb_valid_size(size);
545 pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
547 if (IS_ENABLED(CONFIG_ARM64_ERRATUM_2645198) &&
548 cpus_have_const_cap(ARM64_WORKAROUND_2645198)) {
550 * Break-before-make (BBM) is required for all user space mappings
551 * when the permission changes from executable to non-executable
552 * in cases where cpu is affected with errata #2645198.
554 if (pte_user_exec(READ_ONCE(*ptep)))
555 return huge_ptep_clear_flush(vma, addr, ptep);
557 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
560 void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
561 pte_t old_pte, pte_t pte)
563 unsigned long psize = huge_page_size(hstate_vma(vma));
565 set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);