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:
8 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
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>
29 bool hugetlb_disabled = false;
31 #define hugepd_none(hpd) (hpd_val(hpd) == 0)
33 #define PTE_T_ORDER (__builtin_ffs(sizeof(pte_basic_t)) - \
34 __builtin_ffs(sizeof(void *)))
36 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
39 * Only called for hugetlbfs pages, hence can ignore THP and the
42 return __find_linux_pte(mm->pgd, addr, NULL, NULL);
45 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
46 unsigned long address, unsigned int pdshift,
47 unsigned int pshift, spinlock_t *ptl)
49 struct kmem_cache *cachep;
54 if (pshift >= pdshift) {
55 cachep = PGT_CACHE(PTE_T_ORDER);
56 num_hugepd = 1 << (pshift - pdshift);
58 } else if (IS_ENABLED(CONFIG_PPC_8xx)) {
61 new = pte_alloc_one(mm);
63 cachep = PGT_CACHE(pdshift - pshift);
68 if (!cachep && !new) {
69 WARN_ONCE(1, "No page table cache created for hugetlb tables");
74 new = kmem_cache_alloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
76 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
77 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
83 * Make sure other cpus find the hugepd set only after a
84 * properly initialized page table is visible to them.
85 * For more details look for comment in __pte_alloc().
91 * We have multiple higher-level entries that point to the same
92 * actual pte location. Fill in each as we go and backtrack on error.
93 * We need all of these so the DTLB pgtable walk code can find the
94 * right higher-level entry without knowing if it's a hugepage or not.
96 for (i = 0; i < num_hugepd; i++, hpdp++) {
97 if (unlikely(!hugepd_none(*hpdp)))
99 hugepd_populate(hpdp, new, pshift);
101 /* If we bailed from the for loop early, an error occurred, clean up */
102 if (i < num_hugepd) {
103 for (i = i - 1 ; i >= 0; i--, hpdp--)
106 kmem_cache_free(cachep, new);
110 kmemleak_ignore(new);
117 * At this point we do the placement change only for BOOK3S 64. This would
118 * possibly work on other subarchs.
120 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
125 hugepd_t *hpdp = NULL;
126 unsigned pshift = __ffs(sz);
127 unsigned pdshift = PGDIR_SHIFT;
131 pg = pgd_offset(mm, addr);
133 #ifdef CONFIG_PPC_BOOK3S_64
134 if (pshift == PGDIR_SHIFT)
137 else if (pshift > PUD_SHIFT) {
139 * We need to use hugepd table
141 ptl = &mm->page_table_lock;
142 hpdp = (hugepd_t *)pg;
145 pu = pud_alloc(mm, pg, addr);
148 if (pshift == PUD_SHIFT)
150 else if (pshift > PMD_SHIFT) {
151 ptl = pud_lockptr(mm, pu);
152 hpdp = (hugepd_t *)pu;
155 pm = pmd_alloc(mm, pu, addr);
158 if (pshift == PMD_SHIFT)
162 ptl = pmd_lockptr(mm, pm);
163 hpdp = (hugepd_t *)pm;
168 if (pshift >= PGDIR_SHIFT) {
169 ptl = &mm->page_table_lock;
170 hpdp = (hugepd_t *)pg;
173 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);
184 ptl = pmd_lockptr(mm, pm);
185 hpdp = (hugepd_t *)pm;
192 if (IS_ENABLED(CONFIG_PPC_8xx) && sz == SZ_512K)
193 return pte_alloc_map(mm, (pmd_t *)hpdp, addr);
195 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
197 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr,
198 pdshift, pshift, ptl))
201 return hugepte_offset(*hpdp, addr, pdshift);
204 #ifdef CONFIG_PPC_BOOK3S_64
206 * Tracks gpages after the device tree is scanned and before the
207 * huge_boot_pages list is ready on pseries.
209 #define MAX_NUMBER_GPAGES 1024
210 __initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
211 __initdata static unsigned nr_gpages;
214 * Build list of addresses of gigantic pages. This function is used in early
215 * boot before the buddy allocator is setup.
217 void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
221 while (number_of_pages > 0) {
222 gpage_freearray[nr_gpages] = addr;
229 int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
231 struct huge_bootmem_page *m;
234 m = phys_to_virt(gpage_freearray[--nr_gpages]);
235 gpage_freearray[nr_gpages] = 0;
236 list_add(&m->list, &huge_boot_pages);
243 int __init alloc_bootmem_huge_page(struct hstate *h)
246 #ifdef CONFIG_PPC_BOOK3S_64
247 if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
248 return pseries_alloc_bootmem_huge_page(h);
250 return __alloc_bootmem_huge_page(h);
253 #ifndef CONFIG_PPC_BOOK3S_64
254 #define HUGEPD_FREELIST_SIZE \
255 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
257 struct hugepd_freelist {
263 static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
265 static void hugepd_free_rcu_callback(struct rcu_head *head)
267 struct hugepd_freelist *batch =
268 container_of(head, struct hugepd_freelist, rcu);
271 for (i = 0; i < batch->index; i++)
272 kmem_cache_free(PGT_CACHE(PTE_T_ORDER), batch->ptes[i]);
274 free_page((unsigned long)batch);
277 static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
279 struct hugepd_freelist **batchp;
281 batchp = &get_cpu_var(hugepd_freelist_cur);
283 if (atomic_read(&tlb->mm->mm_users) < 2 ||
284 mm_is_thread_local(tlb->mm)) {
285 kmem_cache_free(PGT_CACHE(PTE_T_ORDER), hugepte);
286 put_cpu_var(hugepd_freelist_cur);
290 if (*batchp == NULL) {
291 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
292 (*batchp)->index = 0;
295 (*batchp)->ptes[(*batchp)->index++] = hugepte;
296 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
297 call_rcu(&(*batchp)->rcu, hugepd_free_rcu_callback);
300 put_cpu_var(hugepd_freelist_cur);
303 static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
306 static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
307 unsigned long start, unsigned long end,
308 unsigned long floor, unsigned long ceiling)
310 pte_t *hugepte = hugepd_page(*hpdp);
313 unsigned long pdmask = ~((1UL << pdshift) - 1);
314 unsigned int num_hugepd = 1;
315 unsigned int shift = hugepd_shift(*hpdp);
317 /* Note: On fsl the hpdp may be the first of several */
319 num_hugepd = 1 << (shift - pdshift);
329 if (end - 1 > ceiling - 1)
332 for (i = 0; i < num_hugepd; i++, hpdp++)
335 if (shift >= pdshift)
336 hugepd_free(tlb, hugepte);
338 pgtable_free_tlb(tlb, hugepte,
339 get_hugepd_cache_index(pdshift - shift));
342 static void hugetlb_free_pte_range(struct mmu_gather *tlb, pmd_t *pmd, unsigned long addr)
344 pgtable_t token = pmd_pgtable(*pmd);
347 pte_free_tlb(tlb, token, addr);
348 mm_dec_nr_ptes(tlb->mm);
351 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
352 unsigned long addr, unsigned long end,
353 unsigned long floor, unsigned long ceiling)
363 pmd = pmd_offset(pud, addr);
364 next = pmd_addr_end(addr, end);
365 if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
366 if (pmd_none_or_clear_bad(pmd))
370 * if it is not hugepd pointer, we should already find
373 WARN_ON(!IS_ENABLED(CONFIG_PPC_8xx));
375 hugetlb_free_pte_range(tlb, pmd, addr);
380 * Increment next by the size of the huge mapping since
381 * there may be more than one entry at this level for a
382 * single hugepage, but all of them point to
383 * the same kmem cache that holds the hugepte.
385 more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
389 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
390 addr, next, floor, ceiling);
391 } while (addr = next, addr != end);
401 if (end - 1 > ceiling - 1)
404 pmd = pmd_offset(pud, start);
406 pmd_free_tlb(tlb, pmd, start);
407 mm_dec_nr_pmds(tlb->mm);
410 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
411 unsigned long addr, unsigned long end,
412 unsigned long floor, unsigned long ceiling)
420 pud = pud_offset(pgd, addr);
421 next = pud_addr_end(addr, end);
422 if (!is_hugepd(__hugepd(pud_val(*pud)))) {
423 if (pud_none_or_clear_bad(pud))
425 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
430 * Increment next by the size of the huge mapping since
431 * there may be more than one entry at this level for a
432 * single hugepage, but all of them point to
433 * the same kmem cache that holds the hugepte.
435 more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
439 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
440 addr, next, floor, ceiling);
442 } while (addr = next, addr != end);
448 ceiling &= PGDIR_MASK;
452 if (end - 1 > ceiling - 1)
455 pud = pud_offset(pgd, start);
457 pud_free_tlb(tlb, pud, start);
458 mm_dec_nr_puds(tlb->mm);
462 * This function frees user-level page tables of a process.
464 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
465 unsigned long addr, unsigned long end,
466 unsigned long floor, unsigned long ceiling)
472 * Because there are a number of different possible pagetable
473 * layouts for hugepage ranges, we limit knowledge of how
474 * things should be laid out to the allocation path
475 * (huge_pte_alloc(), above). Everything else works out the
476 * structure as it goes from information in the hugepd
477 * pointers. That means that we can't here use the
478 * optimization used in the normal page free_pgd_range(), of
479 * checking whether we're actually covering a large enough
480 * range to have to do anything at the top level of the walk
481 * instead of at the bottom.
483 * To make sense of this, you should probably go read the big
484 * block comment at the top of the normal free_pgd_range(),
489 next = pgd_addr_end(addr, end);
490 pgd = pgd_offset(tlb->mm, addr);
491 if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
492 if (pgd_none_or_clear_bad(pgd))
494 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
498 * Increment next by the size of the huge mapping since
499 * there may be more than one entry at the pgd level
500 * for a single hugepage, but all of them point to the
501 * same kmem cache that holds the hugepte.
503 more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
507 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
508 addr, next, floor, ceiling);
510 } while (addr = next, addr != end);
513 struct page *follow_huge_pd(struct vm_area_struct *vma,
514 unsigned long address, hugepd_t hpd,
515 int flags, int pdshift)
519 struct page *page = NULL;
521 int shift = hugepd_shift(hpd);
522 struct mm_struct *mm = vma->vm_mm;
526 * hugepage directory entries are protected by mm->page_table_lock
527 * Use this instead of huge_pte_lockptr
529 ptl = &mm->page_table_lock;
532 ptep = hugepte_offset(hpd, address, pdshift);
533 if (pte_present(*ptep)) {
534 mask = (1UL << shift) - 1;
535 page = pte_page(*ptep);
536 page += ((address & mask) >> PAGE_SHIFT);
537 if (flags & FOLL_GET)
540 if (is_hugetlb_entry_migration(*ptep)) {
542 __migration_entry_wait(mm, ptep, ptl);
550 #ifdef CONFIG_PPC_MM_SLICES
551 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
552 unsigned long len, unsigned long pgoff,
555 struct hstate *hstate = hstate_file(file);
556 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
558 #ifdef CONFIG_PPC_RADIX_MMU
560 return radix__hugetlb_get_unmapped_area(file, addr, len,
563 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
567 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
569 /* With radix we don't use slice, so derive it from vma*/
570 if (IS_ENABLED(CONFIG_PPC_MM_SLICES) && !radix_enabled()) {
571 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
573 return 1UL << mmu_psize_to_shift(psize);
575 return vma_kernel_pagesize(vma);
578 static int __init add_huge_page_size(unsigned long long size)
580 int shift = __ffs(size);
583 /* Check that it is a page size supported by the hardware and
584 * that it fits within pagetable and slice limits. */
585 if (size <= PAGE_SIZE || !is_power_of_2(size))
588 mmu_psize = check_and_get_huge_psize(shift);
592 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
594 /* Return if huge page size has already been setup */
595 if (size_to_hstate(size))
598 hugetlb_add_hstate(shift - PAGE_SHIFT);
603 static int __init hugepage_setup_sz(char *str)
605 unsigned long long size;
607 size = memparse(str, &str);
609 if (add_huge_page_size(size) != 0) {
611 pr_err("Invalid huge page size specified(%llu)\n", size);
616 __setup("hugepagesz=", hugepage_setup_sz);
618 static int __init hugetlbpage_init(void)
620 bool configured = false;
623 if (hugetlb_disabled) {
624 pr_info("HugeTLB support is disabled!\n");
628 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !radix_enabled() &&
629 !mmu_has_feature(MMU_FTR_16M_PAGE))
632 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
636 if (!mmu_psize_defs[psize].shift)
639 shift = mmu_psize_to_shift(psize);
641 #ifdef CONFIG_PPC_BOOK3S_64
642 if (shift > PGDIR_SHIFT)
644 else if (shift > PUD_SHIFT)
645 pdshift = PGDIR_SHIFT;
646 else if (shift > PMD_SHIFT)
651 if (shift < PUD_SHIFT)
653 else if (shift < PGDIR_SHIFT)
656 pdshift = PGDIR_SHIFT;
659 if (add_huge_page_size(1ULL << shift) < 0)
662 * if we have pdshift and shift value same, we don't
663 * use pgt cache for hugepd.
665 if (pdshift > shift) {
666 if (!IS_ENABLED(CONFIG_PPC_8xx))
667 pgtable_cache_add(pdshift - shift);
668 } else if (IS_ENABLED(CONFIG_PPC_FSL_BOOK3E) ||
669 IS_ENABLED(CONFIG_PPC_8xx)) {
670 pgtable_cache_add(PTE_T_ORDER);
677 if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE))
678 hugetlbpage_init_default();
680 pr_info("Failed to initialize. Disabling HugeTLB");
685 arch_initcall(hugetlbpage_init);
687 void flush_dcache_icache_hugepage(struct page *page)
692 BUG_ON(!PageCompound(page));
694 for (i = 0; i < compound_nr(page); i++) {
695 if (!PageHighMem(page)) {
696 __flush_dcache_icache(page_address(page+i));
698 start = kmap_atomic(page+i);
699 __flush_dcache_icache(start);
700 kunmap_atomic(start);