2 * PPC Huge TLB Page Support for Kernel.
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
5 * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
7 * Based on the IA-32 version:
13 #include <linux/slab.h>
14 #include <linux/hugetlb.h>
15 #include <linux/export.h>
16 #include <linux/of_fdt.h>
17 #include <linux/memblock.h>
18 #include <linux/moduleparam.h>
19 #include <linux/swap.h>
20 #include <linux/swapops.h>
21 #include <linux/kmemleak.h>
22 #include <asm/pgtable.h>
23 #include <asm/pgalloc.h>
25 #include <asm/setup.h>
26 #include <asm/hugetlb.h>
27 #include <asm/pte-walk.h>
30 #ifdef CONFIG_HUGETLB_PAGE
32 #define PAGE_SHIFT_64K 16
33 #define PAGE_SHIFT_512K 19
34 #define PAGE_SHIFT_8M 23
35 #define PAGE_SHIFT_16M 24
36 #define PAGE_SHIFT_16G 34
38 bool hugetlb_disabled = false;
40 unsigned int HPAGE_SHIFT;
41 EXPORT_SYMBOL(HPAGE_SHIFT);
43 #define hugepd_none(hpd) (hpd_val(hpd) == 0)
45 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
48 * Only called for hugetlbfs pages, hence can ignore THP and the
51 return __find_linux_pte(mm->pgd, addr, NULL, NULL);
54 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
55 unsigned long address, unsigned int pdshift,
56 unsigned int pshift, spinlock_t *ptl)
58 struct kmem_cache *cachep;
63 if (pshift >= pdshift) {
64 cachep = hugepte_cache;
65 num_hugepd = 1 << (pshift - pdshift);
67 cachep = PGT_CACHE(pdshift - pshift);
71 new = kmem_cache_zalloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
73 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
74 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
80 * Make sure other cpus find the hugepd set only after a
81 * properly initialized page table is visible to them.
82 * For more details look for comment in __pte_alloc().
88 * We have multiple higher-level entries that point to the same
89 * actual pte location. Fill in each as we go and backtrack on error.
90 * We need all of these so the DTLB pgtable walk code can find the
91 * right higher-level entry without knowing if it's a hugepage or not.
93 for (i = 0; i < num_hugepd; i++, hpdp++) {
94 if (unlikely(!hugepd_none(*hpdp)))
97 #ifdef CONFIG_PPC_BOOK3S_64
98 *hpdp = __hugepd(__pa(new) | HUGEPD_VAL_BITS |
99 (shift_to_mmu_psize(pshift) << 2));
100 #elif defined(CONFIG_PPC_8xx)
101 *hpdp = __hugepd(__pa(new) | _PMD_USER |
102 (pshift == PAGE_SHIFT_8M ? _PMD_PAGE_8M :
103 _PMD_PAGE_512K) | _PMD_PRESENT);
105 /* We use the old format for PPC_FSL_BOOK3E */
106 *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift);
110 /* If we bailed from the for loop early, an error occurred, clean up */
111 if (i < num_hugepd) {
112 for (i = i - 1 ; i >= 0; i--, hpdp--)
114 kmem_cache_free(cachep, new);
116 kmemleak_ignore(new);
123 * At this point we do the placement change only for BOOK3S 64. This would
124 * possibly work on other subarchs.
126 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
131 hugepd_t *hpdp = NULL;
132 unsigned pshift = __ffs(sz);
133 unsigned pdshift = PGDIR_SHIFT;
137 pg = pgd_offset(mm, addr);
139 #ifdef CONFIG_PPC_BOOK3S_64
140 if (pshift == PGDIR_SHIFT)
143 else if (pshift > PUD_SHIFT) {
145 * We need to use hugepd table
147 ptl = &mm->page_table_lock;
148 hpdp = (hugepd_t *)pg;
151 pu = pud_alloc(mm, pg, addr);
152 if (pshift == PUD_SHIFT)
154 else if (pshift > PMD_SHIFT) {
155 ptl = pud_lockptr(mm, pu);
156 hpdp = (hugepd_t *)pu;
159 pm = pmd_alloc(mm, pu, addr);
160 if (pshift == PMD_SHIFT)
164 ptl = pmd_lockptr(mm, pm);
165 hpdp = (hugepd_t *)pm;
170 if (pshift >= PGDIR_SHIFT) {
171 ptl = &mm->page_table_lock;
172 hpdp = (hugepd_t *)pg;
175 pu = pud_alloc(mm, pg, addr);
176 if (pshift >= PUD_SHIFT) {
177 ptl = pud_lockptr(mm, pu);
178 hpdp = (hugepd_t *)pu;
181 pm = pmd_alloc(mm, pu, addr);
182 ptl = pmd_lockptr(mm, pm);
183 hpdp = (hugepd_t *)pm;
190 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
192 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr,
193 pdshift, pshift, ptl))
196 return hugepte_offset(*hpdp, addr, pdshift);
199 #ifdef CONFIG_PPC_BOOK3S_64
201 * Tracks gpages after the device tree is scanned and before the
202 * huge_boot_pages list is ready on pseries.
204 #define MAX_NUMBER_GPAGES 1024
205 __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
206 __initdata static unsigned nr_gpages;
209 * Build list of addresses of gigantic pages. This function is used in early
210 * boot before the buddy allocator is setup.
212 void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
216 while (number_of_pages > 0) {
217 gpage_freearray[nr_gpages] = addr;
224 int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
226 struct huge_bootmem_page *m;
229 m = phys_to_virt(gpage_freearray[--nr_gpages]);
230 gpage_freearray[nr_gpages] = 0;
231 list_add(&m->list, &huge_boot_pages);
238 int __init alloc_bootmem_huge_page(struct hstate *h)
241 #ifdef CONFIG_PPC_BOOK3S_64
242 if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
243 return pseries_alloc_bootmem_huge_page(h);
245 return __alloc_bootmem_huge_page(h);
248 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
249 #define HUGEPD_FREELIST_SIZE \
250 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
252 struct hugepd_freelist {
258 static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
260 static void hugepd_free_rcu_callback(struct rcu_head *head)
262 struct hugepd_freelist *batch =
263 container_of(head, struct hugepd_freelist, rcu);
266 for (i = 0; i < batch->index; i++)
267 kmem_cache_free(hugepte_cache, batch->ptes[i]);
269 free_page((unsigned long)batch);
272 static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
274 struct hugepd_freelist **batchp;
276 batchp = &get_cpu_var(hugepd_freelist_cur);
278 if (atomic_read(&tlb->mm->mm_users) < 2 ||
279 mm_is_thread_local(tlb->mm)) {
280 kmem_cache_free(hugepte_cache, hugepte);
281 put_cpu_var(hugepd_freelist_cur);
285 if (*batchp == NULL) {
286 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
287 (*batchp)->index = 0;
290 (*batchp)->ptes[(*batchp)->index++] = hugepte;
291 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
292 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
295 put_cpu_var(hugepd_freelist_cur);
298 static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
301 static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
302 unsigned long start, unsigned long end,
303 unsigned long floor, unsigned long ceiling)
305 pte_t *hugepte = hugepd_page(*hpdp);
308 unsigned long pdmask = ~((1UL << pdshift) - 1);
309 unsigned int num_hugepd = 1;
310 unsigned int shift = hugepd_shift(*hpdp);
312 /* Note: On fsl the hpdp may be the first of several */
314 num_hugepd = 1 << (shift - pdshift);
324 if (end - 1 > ceiling - 1)
327 for (i = 0; i < num_hugepd; i++, hpdp++)
330 if (shift >= pdshift)
331 hugepd_free(tlb, hugepte);
333 pgtable_free_tlb(tlb, hugepte,
334 get_hugepd_cache_index(pdshift - shift));
337 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
338 unsigned long addr, unsigned long end,
339 unsigned long floor, unsigned long ceiling)
349 pmd = pmd_offset(pud, addr);
350 next = pmd_addr_end(addr, end);
351 if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
353 * if it is not hugepd pointer, we should already find
356 WARN_ON(!pmd_none_or_clear_bad(pmd));
360 * Increment next by the size of the huge mapping since
361 * there may be more than one entry at this level for a
362 * single hugepage, but all of them point to
363 * the same kmem cache that holds the hugepte.
365 more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
369 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
370 addr, next, floor, ceiling);
371 } while (addr = next, addr != end);
381 if (end - 1 > ceiling - 1)
384 pmd = pmd_offset(pud, start);
386 pmd_free_tlb(tlb, pmd, start);
387 mm_dec_nr_pmds(tlb->mm);
390 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
391 unsigned long addr, unsigned long end,
392 unsigned long floor, unsigned long ceiling)
400 pud = pud_offset(pgd, addr);
401 next = pud_addr_end(addr, end);
402 if (!is_hugepd(__hugepd(pud_val(*pud)))) {
403 if (pud_none_or_clear_bad(pud))
405 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
410 * Increment next by the size of the huge mapping since
411 * there may be more than one entry at this level for a
412 * single hugepage, but all of them point to
413 * the same kmem cache that holds the hugepte.
415 more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
419 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
420 addr, next, floor, ceiling);
422 } while (addr = next, addr != end);
428 ceiling &= PGDIR_MASK;
432 if (end - 1 > ceiling - 1)
435 pud = pud_offset(pgd, start);
437 pud_free_tlb(tlb, pud, start);
438 mm_dec_nr_puds(tlb->mm);
442 * This function frees user-level page tables of a process.
444 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
445 unsigned long addr, unsigned long end,
446 unsigned long floor, unsigned long ceiling)
452 * Because there are a number of different possible pagetable
453 * layouts for hugepage ranges, we limit knowledge of how
454 * things should be laid out to the allocation path
455 * (huge_pte_alloc(), above). Everything else works out the
456 * structure as it goes from information in the hugepd
457 * pointers. That means that we can't here use the
458 * optimization used in the normal page free_pgd_range(), of
459 * checking whether we're actually covering a large enough
460 * range to have to do anything at the top level of the walk
461 * instead of at the bottom.
463 * To make sense of this, you should probably go read the big
464 * block comment at the top of the normal free_pgd_range(),
469 next = pgd_addr_end(addr, end);
470 pgd = pgd_offset(tlb->mm, addr);
471 if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
472 if (pgd_none_or_clear_bad(pgd))
474 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
478 * Increment next by the size of the huge mapping since
479 * there may be more than one entry at the pgd level
480 * for a single hugepage, but all of them point to the
481 * same kmem cache that holds the hugepte.
483 more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
487 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
488 addr, next, floor, ceiling);
490 } while (addr = next, addr != end);
493 struct page *follow_huge_pd(struct vm_area_struct *vma,
494 unsigned long address, hugepd_t hpd,
495 int flags, int pdshift)
499 struct page *page = NULL;
501 int shift = hugepd_shift(hpd);
502 struct mm_struct *mm = vma->vm_mm;
506 * hugepage directory entries are protected by mm->page_table_lock
507 * Use this instead of huge_pte_lockptr
509 ptl = &mm->page_table_lock;
512 ptep = hugepte_offset(hpd, address, pdshift);
513 if (pte_present(*ptep)) {
514 mask = (1UL << shift) - 1;
515 page = pte_page(*ptep);
516 page += ((address & mask) >> PAGE_SHIFT);
517 if (flags & FOLL_GET)
520 if (is_hugetlb_entry_migration(*ptep)) {
522 __migration_entry_wait(mm, ptep, ptl);
530 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
533 unsigned long __boundary = (addr + sz) & ~(sz-1);
534 return (__boundary - 1 < end - 1) ? __boundary : end;
537 int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
538 unsigned long end, int write, struct page **pages, int *nr)
541 unsigned long sz = 1UL << hugepd_shift(hugepd);
544 ptep = hugepte_offset(hugepd, addr, pdshift);
546 next = hugepte_addr_end(addr, end, sz);
547 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
549 } while (ptep++, addr = next, addr != end);
554 #ifdef CONFIG_PPC_MM_SLICES
555 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
556 unsigned long len, unsigned long pgoff,
559 struct hstate *hstate = hstate_file(file);
560 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
562 #ifdef CONFIG_PPC_RADIX_MMU
564 return radix__hugetlb_get_unmapped_area(file, addr, len,
567 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
571 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
573 #ifdef CONFIG_PPC_MM_SLICES
574 /* With radix we don't use slice, so derive it from vma*/
575 if (!radix_enabled()) {
576 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
578 return 1UL << mmu_psize_to_shift(psize);
581 return vma_kernel_pagesize(vma);
584 static inline bool is_power_of_4(unsigned long x)
586 if (is_power_of_2(x))
587 return (__ilog2(x) % 2) ? false : true;
591 static int __init add_huge_page_size(unsigned long long size)
593 int shift = __ffs(size);
596 /* Check that it is a page size supported by the hardware and
597 * that it fits within pagetable and slice limits. */
598 if (size <= PAGE_SIZE)
600 #if defined(CONFIG_PPC_FSL_BOOK3E)
601 if (!is_power_of_4(size))
603 #elif !defined(CONFIG_PPC_8xx)
604 if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
608 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
611 #ifdef CONFIG_PPC_BOOK3S_64
613 * We need to make sure that for different page sizes reported by
614 * firmware we only add hugetlb support for page sizes that can be
615 * supported by linux page table layout.
620 if (radix_enabled()) {
621 if (mmu_psize != MMU_PAGE_2M && mmu_psize != MMU_PAGE_1G)
624 if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
629 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
631 /* Return if huge page size has already been setup */
632 if (size_to_hstate(size))
635 hugetlb_add_hstate(shift - PAGE_SHIFT);
640 static int __init hugepage_setup_sz(char *str)
642 unsigned long long size;
644 size = memparse(str, &str);
646 if (add_huge_page_size(size) != 0) {
648 pr_err("Invalid huge page size specified(%llu)\n", size);
653 __setup("hugepagesz=", hugepage_setup_sz);
655 struct kmem_cache *hugepte_cache;
656 static int __init hugetlbpage_init(void)
660 if (hugetlb_disabled) {
661 pr_info("HugeTLB support is disabled!\n");
665 #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
666 if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
669 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
673 if (!mmu_psize_defs[psize].shift)
676 shift = mmu_psize_to_shift(psize);
678 #ifdef CONFIG_PPC_BOOK3S_64
679 if (shift > PGDIR_SHIFT)
681 else if (shift > PUD_SHIFT)
682 pdshift = PGDIR_SHIFT;
683 else if (shift > PMD_SHIFT)
688 if (shift < PUD_SHIFT)
690 else if (shift < PGDIR_SHIFT)
693 pdshift = PGDIR_SHIFT;
696 if (add_huge_page_size(1ULL << shift) < 0)
699 * if we have pdshift and shift value same, we don't
700 * use pgt cache for hugepd.
703 pgtable_cache_add(pdshift - shift, NULL);
704 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
705 else if (!hugepte_cache) {
707 * Create a kmem cache for hugeptes. The bottom bits in
708 * the pte have size information encoded in them, so
709 * align them to allow this
711 hugepte_cache = kmem_cache_create("hugepte-cache",
713 HUGEPD_SHIFT_MASK + 1,
715 if (hugepte_cache == NULL)
716 panic("%s: Unable to create kmem cache "
717 "for hugeptes\n", __func__);
723 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
724 /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */
725 if (mmu_psize_defs[MMU_PAGE_4M].shift)
726 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
727 else if (mmu_psize_defs[MMU_PAGE_512K].shift)
728 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
730 /* Set default large page size. Currently, we pick 16M or 1M
731 * depending on what is available
733 if (mmu_psize_defs[MMU_PAGE_16M].shift)
734 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
735 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
736 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
737 else if (mmu_psize_defs[MMU_PAGE_2M].shift)
738 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
743 arch_initcall(hugetlbpage_init);
745 void flush_dcache_icache_hugepage(struct page *page)
750 BUG_ON(!PageCompound(page));
752 for (i = 0; i < (1UL << compound_order(page)); i++) {
753 if (!PageHighMem(page)) {
754 __flush_dcache_icache(page_address(page+i));
756 start = kmap_atomic(page+i);
757 __flush_dcache_icache(start);
758 kunmap_atomic(start);
763 #endif /* CONFIG_HUGETLB_PAGE */
766 * We have 4 cases for pgds and pmds:
767 * (1) invalid (all zeroes)
768 * (2) pointer to next table, as normal; bottom 6 bits == 0
769 * (3) leaf pte for huge page _PAGE_PTE set
770 * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
772 * So long as we atomically load page table pointers we are safe against teardown,
773 * we can follow the address down to the the page and take a ref on it.
774 * This function need to be called with interrupts disabled. We use this variant
775 * when we have MSR[EE] = 0 but the paca->irq_soft_mask = IRQS_ENABLED
777 pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
778 bool *is_thp, unsigned *hpage_shift)
784 hugepd_t *hpdp = NULL;
785 unsigned pdshift = PGDIR_SHIFT;
793 pgdp = pgdir + pgd_index(ea);
794 pgd = READ_ONCE(*pgdp);
796 * Always operate on the local stack value. This make sure the
797 * value don't get updated by a parallel THP split/collapse,
798 * page fault or a page unmap. The return pte_t * is still not
799 * stable. So should be checked there for above conditions.
803 else if (pgd_huge(pgd)) {
804 ret_pte = (pte_t *) pgdp;
806 } else if (is_hugepd(__hugepd(pgd_val(pgd))))
807 hpdp = (hugepd_t *)&pgd;
810 * Even if we end up with an unmap, the pgtable will not
811 * be freed, because we do an rcu free and here we are
815 pudp = pud_offset(&pgd, ea);
816 pud = READ_ONCE(*pudp);
820 else if (pud_huge(pud)) {
821 ret_pte = (pte_t *) pudp;
823 } else if (is_hugepd(__hugepd(pud_val(pud))))
824 hpdp = (hugepd_t *)&pud;
827 pmdp = pmd_offset(&pud, ea);
828 pmd = READ_ONCE(*pmdp);
830 * A hugepage collapse is captured by pmd_none, because
831 * it mark the pmd none and do a hpte invalidate.
836 if (pmd_trans_huge(pmd) || pmd_devmap(pmd)) {
839 ret_pte = (pte_t *) pmdp;
843 * pmd_large check below will handle the swap pmd pte
844 * we need to do both the check because they are config
847 if (pmd_huge(pmd) || pmd_large(pmd)) {
848 ret_pte = (pte_t *) pmdp;
850 } else if (is_hugepd(__hugepd(pmd_val(pmd))))
851 hpdp = (hugepd_t *)&pmd;
853 return pte_offset_kernel(&pmd, ea);
859 ret_pte = hugepte_offset(*hpdp, ea, pdshift);
860 pdshift = hugepd_shift(*hpdp);
863 *hpage_shift = pdshift;
866 EXPORT_SYMBOL_GPL(__find_linux_pte);
868 int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
869 unsigned long end, int write, struct page **pages, int *nr)
871 unsigned long pte_end;
872 struct page *head, *page;
876 pte_end = (addr + sz) & ~(sz-1);
880 pte = READ_ONCE(*ptep);
882 if (!pte_access_permitted(pte, write))
885 /* hugepages are never "special" */
886 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
889 head = pte_page(pte);
891 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
893 VM_BUG_ON(compound_head(page) != head);
898 } while (addr += PAGE_SIZE, addr != end);
900 if (!page_cache_add_speculative(head, refs)) {
905 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
906 /* Could be optimized better */