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/bootmem.h>
19 #include <linux/moduleparam.h>
20 #include <asm/pgtable.h>
21 #include <asm/pgalloc.h>
23 #include <asm/setup.h>
24 #include <asm/hugetlb.h>
26 #ifdef CONFIG_HUGETLB_PAGE
28 #define PAGE_SHIFT_64K 16
29 #define PAGE_SHIFT_512K 19
30 #define PAGE_SHIFT_8M 23
31 #define PAGE_SHIFT_16M 24
32 #define PAGE_SHIFT_16G 34
34 unsigned int HPAGE_SHIFT;
35 EXPORT_SYMBOL(HPAGE_SHIFT);
38 * Tracks gpages after the device tree is scanned and before the
39 * huge_boot_pages list is ready. On non-Freescale implementations, this is
40 * just used to track 16G pages and so is a single array. FSL-based
41 * implementations may have more than one gpage size, so we need multiple
44 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
45 #define MAX_NUMBER_GPAGES 128
47 u64 gpage_list[MAX_NUMBER_GPAGES];
48 unsigned int nr_gpages;
50 static struct psize_gpages gpage_freearray[MMU_PAGE_COUNT];
52 #define MAX_NUMBER_GPAGES 1024
53 static u64 gpage_freearray[MAX_NUMBER_GPAGES];
54 static unsigned nr_gpages;
57 #define hugepd_none(hpd) (hpd_val(hpd) == 0)
59 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
61 /* Only called for hugetlbfs pages, hence can ignore THP */
62 return __find_linux_pte_or_hugepte(mm->pgd, addr, NULL, NULL);
65 static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
66 unsigned long address, unsigned pdshift, unsigned pshift)
68 struct kmem_cache *cachep;
73 if (pshift >= pdshift) {
74 cachep = hugepte_cache;
75 num_hugepd = 1 << (pshift - pdshift);
77 cachep = PGT_CACHE(pdshift - pshift);
81 new = kmem_cache_zalloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
83 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
84 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
90 * Make sure other cpus find the hugepd set only after a
91 * properly initialized page table is visible to them.
92 * For more details look for comment in __pte_alloc().
96 spin_lock(&mm->page_table_lock);
99 * We have multiple higher-level entries that point to the same
100 * actual pte location. Fill in each as we go and backtrack on error.
101 * We need all of these so the DTLB pgtable walk code can find the
102 * right higher-level entry without knowing if it's a hugepage or not.
104 for (i = 0; i < num_hugepd; i++, hpdp++) {
105 if (unlikely(!hugepd_none(*hpdp)))
108 #ifdef CONFIG_PPC_BOOK3S_64
109 *hpdp = __hugepd(__pa(new) |
110 (shift_to_mmu_psize(pshift) << 2));
111 #elif defined(CONFIG_PPC_8xx)
112 *hpdp = __hugepd(__pa(new) |
113 (pshift == PAGE_SHIFT_8M ? _PMD_PAGE_8M :
114 _PMD_PAGE_512K) | _PMD_PRESENT);
116 /* We use the old format for PPC_FSL_BOOK3E */
117 *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift);
121 /* If we bailed from the for loop early, an error occurred, clean up */
122 if (i < num_hugepd) {
123 for (i = i - 1 ; i >= 0; i--, hpdp--)
125 kmem_cache_free(cachep, new);
127 spin_unlock(&mm->page_table_lock);
132 * These macros define how to determine which level of the page table holds
135 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
136 #define HUGEPD_PGD_SHIFT PGDIR_SHIFT
137 #define HUGEPD_PUD_SHIFT PUD_SHIFT
139 #define HUGEPD_PGD_SHIFT PUD_SHIFT
140 #define HUGEPD_PUD_SHIFT PMD_SHIFT
144 * At this point we do the placement change only for BOOK3S 64. This would
145 * possibly work on other subarchs.
147 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
152 hugepd_t *hpdp = NULL;
153 unsigned pshift = __ffs(sz);
154 unsigned pdshift = PGDIR_SHIFT;
157 pg = pgd_offset(mm, addr);
159 #ifdef CONFIG_PPC_BOOK3S_64
160 if (pshift == PGDIR_SHIFT)
163 else if (pshift > PUD_SHIFT)
165 * We need to use hugepd table
167 hpdp = (hugepd_t *)pg;
170 pu = pud_alloc(mm, pg, addr);
171 if (pshift == PUD_SHIFT)
173 else if (pshift > PMD_SHIFT)
174 hpdp = (hugepd_t *)pu;
177 pm = pmd_alloc(mm, pu, addr);
178 if (pshift == PMD_SHIFT)
182 hpdp = (hugepd_t *)pm;
186 if (pshift >= HUGEPD_PGD_SHIFT) {
187 hpdp = (hugepd_t *)pg;
190 pu = pud_alloc(mm, pg, addr);
191 if (pshift >= HUGEPD_PUD_SHIFT) {
192 hpdp = (hugepd_t *)pu;
195 pm = pmd_alloc(mm, pu, addr);
196 hpdp = (hugepd_t *)pm;
203 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
205 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr, pdshift, pshift))
208 return hugepte_offset(*hpdp, addr, pdshift);
211 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
212 /* Build list of addresses of gigantic pages. This function is used in early
213 * boot before the buddy allocator is setup.
215 void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
217 unsigned int idx = shift_to_mmu_psize(__ffs(page_size));
223 gpage_freearray[idx].nr_gpages = number_of_pages;
225 for (i = 0; i < number_of_pages; i++) {
226 gpage_freearray[idx].gpage_list[i] = addr;
232 * Moves the gigantic page addresses from the temporary list to the
233 * huge_boot_pages list.
235 int alloc_bootmem_huge_page(struct hstate *hstate)
237 struct huge_bootmem_page *m;
238 int idx = shift_to_mmu_psize(huge_page_shift(hstate));
239 int nr_gpages = gpage_freearray[idx].nr_gpages;
244 #ifdef CONFIG_HIGHMEM
246 * If gpages can be in highmem we can't use the trick of storing the
247 * data structure in the page; allocate space for this
249 m = memblock_virt_alloc(sizeof(struct huge_bootmem_page), 0);
250 m->phys = gpage_freearray[idx].gpage_list[--nr_gpages];
252 m = phys_to_virt(gpage_freearray[idx].gpage_list[--nr_gpages]);
255 list_add(&m->list, &huge_boot_pages);
256 gpage_freearray[idx].nr_gpages = nr_gpages;
257 gpage_freearray[idx].gpage_list[nr_gpages] = 0;
263 * Scan the command line hugepagesz= options for gigantic pages; store those in
264 * a list that we use to allocate the memory once all options are parsed.
267 unsigned long gpage_npages[MMU_PAGE_COUNT];
269 static int __init do_gpage_early_setup(char *param, char *val,
270 const char *unused, void *arg)
272 static phys_addr_t size;
273 unsigned long npages;
276 * The hugepagesz and hugepages cmdline options are interleaved. We
277 * use the size variable to keep track of whether or not this was done
278 * properly and skip over instances where it is incorrect. Other
279 * command-line parsing code will issue warnings, so we don't need to.
282 if ((strcmp(param, "default_hugepagesz") == 0) ||
283 (strcmp(param, "hugepagesz") == 0)) {
284 size = memparse(val, NULL);
285 } else if (strcmp(param, "hugepages") == 0) {
287 if (sscanf(val, "%lu", &npages) <= 0)
289 if (npages > MAX_NUMBER_GPAGES) {
290 pr_warn("MMU: %lu pages requested for page "
291 #ifdef CONFIG_PHYS_ADDR_T_64BIT
292 "size %llu KB, limiting to "
294 "size %u KB, limiting to "
296 __stringify(MAX_NUMBER_GPAGES) "\n",
297 npages, size / 1024);
298 npages = MAX_NUMBER_GPAGES;
300 gpage_npages[shift_to_mmu_psize(__ffs(size))] = npages;
309 * This function allocates physical space for pages that are larger than the
310 * buddy allocator can handle. We want to allocate these in highmem because
311 * the amount of lowmem is limited. This means that this function MUST be
312 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
313 * allocate to grab highmem.
315 void __init reserve_hugetlb_gpages(void)
317 static __initdata char cmdline[COMMAND_LINE_SIZE];
318 phys_addr_t size, base;
321 strlcpy(cmdline, boot_command_line, COMMAND_LINE_SIZE);
322 parse_args("hugetlb gpages", cmdline, NULL, 0, 0, 0,
323 NULL, &do_gpage_early_setup);
326 * Walk gpage list in reverse, allocating larger page sizes first.
327 * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
328 * When we reach the point in the list where pages are no longer
329 * considered gpages, we're done.
331 for (i = MMU_PAGE_COUNT-1; i >= 0; i--) {
332 if (mmu_psize_defs[i].shift == 0 || gpage_npages[i] == 0)
334 else if (mmu_psize_to_shift(i) < (MAX_ORDER + PAGE_SHIFT))
337 size = (phys_addr_t)(1ULL << mmu_psize_to_shift(i));
338 base = memblock_alloc_base(size * gpage_npages[i], size,
339 MEMBLOCK_ALLOC_ANYWHERE);
340 add_gpage(base, size, gpage_npages[i]);
344 #else /* !PPC_FSL_BOOK3E */
346 /* Build list of addresses of gigantic pages. This function is used in early
347 * boot before the buddy allocator is setup.
349 void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
353 while (number_of_pages > 0) {
354 gpage_freearray[nr_gpages] = addr;
361 /* Moves the gigantic page addresses from the temporary list to the
362 * huge_boot_pages list.
364 int alloc_bootmem_huge_page(struct hstate *hstate)
366 struct huge_bootmem_page *m;
369 m = phys_to_virt(gpage_freearray[--nr_gpages]);
370 gpage_freearray[nr_gpages] = 0;
371 list_add(&m->list, &huge_boot_pages);
377 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
378 #define HUGEPD_FREELIST_SIZE \
379 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
381 struct hugepd_freelist {
387 static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
389 static void hugepd_free_rcu_callback(struct rcu_head *head)
391 struct hugepd_freelist *batch =
392 container_of(head, struct hugepd_freelist, rcu);
395 for (i = 0; i < batch->index; i++)
396 kmem_cache_free(hugepte_cache, batch->ptes[i]);
398 free_page((unsigned long)batch);
401 static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
403 struct hugepd_freelist **batchp;
405 batchp = &get_cpu_var(hugepd_freelist_cur);
407 if (atomic_read(&tlb->mm->mm_users) < 2 ||
408 cpumask_equal(mm_cpumask(tlb->mm),
409 cpumask_of(smp_processor_id()))) {
410 kmem_cache_free(hugepte_cache, hugepte);
411 put_cpu_var(hugepd_freelist_cur);
415 if (*batchp == NULL) {
416 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
417 (*batchp)->index = 0;
420 (*batchp)->ptes[(*batchp)->index++] = hugepte;
421 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
422 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
425 put_cpu_var(hugepd_freelist_cur);
428 static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
431 static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
432 unsigned long start, unsigned long end,
433 unsigned long floor, unsigned long ceiling)
435 pte_t *hugepte = hugepd_page(*hpdp);
438 unsigned long pdmask = ~((1UL << pdshift) - 1);
439 unsigned int num_hugepd = 1;
440 unsigned int shift = hugepd_shift(*hpdp);
442 /* Note: On fsl the hpdp may be the first of several */
444 num_hugepd = 1 << (shift - pdshift);
454 if (end - 1 > ceiling - 1)
457 for (i = 0; i < num_hugepd; i++, hpdp++)
460 if (shift >= pdshift)
461 hugepd_free(tlb, hugepte);
463 pgtable_free_tlb(tlb, hugepte, pdshift - shift);
466 static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
467 unsigned long addr, unsigned long end,
468 unsigned long floor, unsigned long ceiling)
478 pmd = pmd_offset(pud, addr);
479 next = pmd_addr_end(addr, end);
480 if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
482 * if it is not hugepd pointer, we should already find
485 WARN_ON(!pmd_none_or_clear_bad(pmd));
489 * Increment next by the size of the huge mapping since
490 * there may be more than one entry at this level for a
491 * single hugepage, but all of them point to
492 * the same kmem cache that holds the hugepte.
494 more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
498 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
499 addr, next, floor, ceiling);
500 } while (addr = next, addr != end);
510 if (end - 1 > ceiling - 1)
513 pmd = pmd_offset(pud, start);
515 pmd_free_tlb(tlb, pmd, start);
516 mm_dec_nr_pmds(tlb->mm);
519 static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
520 unsigned long addr, unsigned long end,
521 unsigned long floor, unsigned long ceiling)
529 pud = pud_offset(pgd, addr);
530 next = pud_addr_end(addr, end);
531 if (!is_hugepd(__hugepd(pud_val(*pud)))) {
532 if (pud_none_or_clear_bad(pud))
534 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
539 * Increment next by the size of the huge mapping since
540 * there may be more than one entry at this level for a
541 * single hugepage, but all of them point to
542 * the same kmem cache that holds the hugepte.
544 more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
548 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
549 addr, next, floor, ceiling);
551 } while (addr = next, addr != end);
557 ceiling &= PGDIR_MASK;
561 if (end - 1 > ceiling - 1)
564 pud = pud_offset(pgd, start);
566 pud_free_tlb(tlb, pud, start);
570 * This function frees user-level page tables of a process.
572 void hugetlb_free_pgd_range(struct mmu_gather *tlb,
573 unsigned long addr, unsigned long end,
574 unsigned long floor, unsigned long ceiling)
580 * Because there are a number of different possible pagetable
581 * layouts for hugepage ranges, we limit knowledge of how
582 * things should be laid out to the allocation path
583 * (huge_pte_alloc(), above). Everything else works out the
584 * structure as it goes from information in the hugepd
585 * pointers. That means that we can't here use the
586 * optimization used in the normal page free_pgd_range(), of
587 * checking whether we're actually covering a large enough
588 * range to have to do anything at the top level of the walk
589 * instead of at the bottom.
591 * To make sense of this, you should probably go read the big
592 * block comment at the top of the normal free_pgd_range(),
597 next = pgd_addr_end(addr, end);
598 pgd = pgd_offset(tlb->mm, addr);
599 if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
600 if (pgd_none_or_clear_bad(pgd))
602 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
606 * Increment next by the size of the huge mapping since
607 * there may be more than one entry at the pgd level
608 * for a single hugepage, but all of them point to the
609 * same kmem cache that holds the hugepte.
611 more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
615 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
616 addr, next, floor, ceiling);
618 } while (addr = next, addr != end);
622 * We are holding mmap_sem, so a parallel huge page collapse cannot run.
623 * To prevent hugepage split, disable irq.
626 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
631 unsigned long mask, flags;
632 struct page *page = ERR_PTR(-EINVAL);
634 local_irq_save(flags);
635 ptep = find_linux_pte_or_hugepte(mm->pgd, address, &is_thp, &shift);
638 pte = READ_ONCE(*ptep);
640 * Verify it is a huge page else bail.
641 * Transparent hugepages are handled by generic code. We can skip them
644 if (!shift || is_thp)
647 if (!pte_present(pte)) {
651 mask = (1UL << shift) - 1;
652 page = pte_page(pte);
654 page += (address & mask) / PAGE_SIZE;
657 local_irq_restore(flags);
662 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
663 pmd_t *pmd, int write)
670 follow_huge_pud(struct mm_struct *mm, unsigned long address,
671 pud_t *pud, int write)
677 static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
680 unsigned long __boundary = (addr + sz) & ~(sz-1);
681 return (__boundary - 1 < end - 1) ? __boundary : end;
684 int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
685 unsigned long end, int write, struct page **pages, int *nr)
688 unsigned long sz = 1UL << hugepd_shift(hugepd);
691 ptep = hugepte_offset(hugepd, addr, pdshift);
693 next = hugepte_addr_end(addr, end, sz);
694 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
696 } while (ptep++, addr = next, addr != end);
701 #ifdef CONFIG_PPC_MM_SLICES
702 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
703 unsigned long len, unsigned long pgoff,
706 struct hstate *hstate = hstate_file(file);
707 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
710 return radix__hugetlb_get_unmapped_area(file, addr, len,
712 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
716 unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
718 #ifdef CONFIG_PPC_MM_SLICES
719 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
720 /* With radix we don't use slice, so derive it from vma*/
721 if (!radix_enabled())
722 return 1UL << mmu_psize_to_shift(psize);
724 if (!is_vm_hugetlb_page(vma))
727 return huge_page_size(hstate_vma(vma));
730 static inline bool is_power_of_4(unsigned long x)
732 if (is_power_of_2(x))
733 return (__ilog2(x) % 2) ? false : true;
737 static int __init add_huge_page_size(unsigned long long size)
739 int shift = __ffs(size);
742 /* Check that it is a page size supported by the hardware and
743 * that it fits within pagetable and slice limits. */
744 if (size <= PAGE_SIZE)
746 #if defined(CONFIG_PPC_FSL_BOOK3E)
747 if (!is_power_of_4(size))
749 #elif !defined(CONFIG_PPC_8xx)
750 if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
754 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
757 #ifdef CONFIG_PPC_BOOK3S_64
759 * We need to make sure that for different page sizes reported by
760 * firmware we only add hugetlb support for page sizes that can be
761 * supported by linux page table layout.
766 if (radix_enabled()) {
767 if (mmu_psize != MMU_PAGE_2M)
770 if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
775 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
777 /* Return if huge page size has already been setup */
778 if (size_to_hstate(size))
781 hugetlb_add_hstate(shift - PAGE_SHIFT);
786 static int __init hugepage_setup_sz(char *str)
788 unsigned long long size;
790 size = memparse(str, &str);
792 if (add_huge_page_size(size) != 0) {
794 pr_err("Invalid huge page size specified(%llu)\n", size);
799 __setup("hugepagesz=", hugepage_setup_sz);
801 struct kmem_cache *hugepte_cache;
802 static int __init hugetlbpage_init(void)
806 #if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
807 if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
810 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
814 if (!mmu_psize_defs[psize].shift)
817 shift = mmu_psize_to_shift(psize);
819 if (add_huge_page_size(1ULL << shift) < 0)
822 if (shift < HUGEPD_PUD_SHIFT)
824 else if (shift < HUGEPD_PGD_SHIFT)
827 pdshift = PGDIR_SHIFT;
829 * if we have pdshift and shift value same, we don't
830 * use pgt cache for hugepd.
833 pgtable_cache_add(pdshift - shift, NULL);
834 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
835 else if (!hugepte_cache) {
837 * Create a kmem cache for hugeptes. The bottom bits in
838 * the pte have size information encoded in them, so
839 * align them to allow this
841 hugepte_cache = kmem_cache_create("hugepte-cache",
843 HUGEPD_SHIFT_MASK + 1,
845 if (hugepte_cache == NULL)
846 panic("%s: Unable to create kmem cache "
847 "for hugeptes\n", __func__);
853 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
854 /* Default hpage size = 4M on FSL_BOOK3E and 512k on 8xx */
855 if (mmu_psize_defs[MMU_PAGE_4M].shift)
856 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
857 else if (mmu_psize_defs[MMU_PAGE_512K].shift)
858 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
860 /* Set default large page size. Currently, we pick 16M or 1M
861 * depending on what is available
863 if (mmu_psize_defs[MMU_PAGE_16M].shift)
864 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
865 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
866 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
867 else if (mmu_psize_defs[MMU_PAGE_2M].shift)
868 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
873 arch_initcall(hugetlbpage_init);
875 void flush_dcache_icache_hugepage(struct page *page)
880 BUG_ON(!PageCompound(page));
882 for (i = 0; i < (1UL << compound_order(page)); i++) {
883 if (!PageHighMem(page)) {
884 __flush_dcache_icache(page_address(page+i));
886 start = kmap_atomic(page+i);
887 __flush_dcache_icache(start);
888 kunmap_atomic(start);
893 #endif /* CONFIG_HUGETLB_PAGE */
896 * We have 4 cases for pgds and pmds:
897 * (1) invalid (all zeroes)
898 * (2) pointer to next table, as normal; bottom 6 bits == 0
899 * (3) leaf pte for huge page _PAGE_PTE set
900 * (4) hugepd pointer, _PAGE_PTE = 0 and bits [2..6] indicate size of table
902 * So long as we atomically load page table pointers we are safe against teardown,
903 * we can follow the address down to the the page and take a ref on it.
904 * This function need to be called with interrupts disabled. We use this variant
905 * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
908 pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
909 bool *is_thp, unsigned *shift)
915 hugepd_t *hpdp = NULL;
916 unsigned pdshift = PGDIR_SHIFT;
924 pgdp = pgdir + pgd_index(ea);
925 pgd = READ_ONCE(*pgdp);
927 * Always operate on the local stack value. This make sure the
928 * value don't get updated by a parallel THP split/collapse,
929 * page fault or a page unmap. The return pte_t * is still not
930 * stable. So should be checked there for above conditions.
934 else if (pgd_huge(pgd)) {
935 ret_pte = (pte_t *) pgdp;
937 } else if (is_hugepd(__hugepd(pgd_val(pgd))))
938 hpdp = (hugepd_t *)&pgd;
941 * Even if we end up with an unmap, the pgtable will not
942 * be freed, because we do an rcu free and here we are
946 pudp = pud_offset(&pgd, ea);
947 pud = READ_ONCE(*pudp);
951 else if (pud_huge(pud)) {
952 ret_pte = (pte_t *) pudp;
954 } else if (is_hugepd(__hugepd(pud_val(pud))))
955 hpdp = (hugepd_t *)&pud;
958 pmdp = pmd_offset(&pud, ea);
959 pmd = READ_ONCE(*pmdp);
961 * A hugepage collapse is captured by pmd_none, because
962 * it mark the pmd none and do a hpte invalidate.
967 if (pmd_trans_huge(pmd)) {
970 ret_pte = (pte_t *) pmdp;
975 ret_pte = (pte_t *) pmdp;
977 } else if (is_hugepd(__hugepd(pmd_val(pmd))))
978 hpdp = (hugepd_t *)&pmd;
980 return pte_offset_kernel(&pmd, ea);
986 ret_pte = hugepte_offset(*hpdp, ea, pdshift);
987 pdshift = hugepd_shift(*hpdp);
993 EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte);
995 int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
996 unsigned long end, int write, struct page **pages, int *nr)
999 unsigned long pte_end;
1000 struct page *head, *page;
1004 pte_end = (addr + sz) & ~(sz-1);
1008 pte = READ_ONCE(*ptep);
1009 mask = _PAGE_PRESENT | _PAGE_READ;
1012 * On some CPUs like the 8xx, _PAGE_RW hence _PAGE_WRITE is defined
1013 * as 0 and _PAGE_RO has to be set when a page is not writable
1016 mask |= _PAGE_WRITE;
1020 if ((pte_val(pte) & mask) != mask)
1023 /* hugepages are never "special" */
1024 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
1027 head = pte_page(pte);
1029 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
1031 VM_BUG_ON(compound_head(page) != head);
1036 } while (addr += PAGE_SIZE, addr != end);
1038 if (!page_cache_add_speculative(head, refs)) {
1043 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
1044 /* Could be optimized better */