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_16M 24
30 #define PAGE_SHIFT_16G 34
32 unsigned int HPAGE_SHIFT
;
35 * Tracks gpages after the device tree is scanned and before the
36 * huge_boot_pages list is ready. On non-Freescale implementations, this is
37 * just used to track 16G pages and so is a single array. FSL-based
38 * implementations may have more than one gpage size, so we need multiple
41 #ifdef CONFIG_PPC_FSL_BOOK3E
42 #define MAX_NUMBER_GPAGES 128
44 u64 gpage_list
[MAX_NUMBER_GPAGES
];
45 unsigned int nr_gpages
;
47 static struct psize_gpages gpage_freearray
[MMU_PAGE_COUNT
];
49 #define MAX_NUMBER_GPAGES 1024
50 static u64 gpage_freearray
[MAX_NUMBER_GPAGES
];
51 static unsigned nr_gpages
;
54 #define hugepd_none(hpd) ((hpd).pd == 0)
56 pte_t
*huge_pte_offset(struct mm_struct
*mm
, unsigned long addr
)
58 /* Only called for hugetlbfs pages, hence can ignore THP */
59 return __find_linux_pte_or_hugepte(mm
->pgd
, addr
, NULL
, NULL
);
62 static int __hugepte_alloc(struct mm_struct
*mm
, hugepd_t
*hpdp
,
63 unsigned long address
, unsigned pdshift
, unsigned pshift
)
65 struct kmem_cache
*cachep
;
68 #ifdef CONFIG_PPC_FSL_BOOK3E
70 int num_hugepd
= 1 << (pshift
- pdshift
);
71 cachep
= hugepte_cache
;
73 cachep
= PGT_CACHE(pdshift
- pshift
);
76 new = kmem_cache_zalloc(cachep
, GFP_KERNEL
|__GFP_REPEAT
);
78 BUG_ON(pshift
> HUGEPD_SHIFT_MASK
);
79 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK
);
84 spin_lock(&mm
->page_table_lock
);
85 #ifdef CONFIG_PPC_FSL_BOOK3E
87 * We have multiple higher-level entries that point to the same
88 * actual pte location. Fill in each as we go and backtrack on error.
89 * We need all of these so the DTLB pgtable walk code can find the
90 * right higher-level entry without knowing if it's a hugepage or not.
92 for (i
= 0; i
< num_hugepd
; i
++, hpdp
++) {
93 if (unlikely(!hugepd_none(*hpdp
)))
96 /* We use the old format for PPC_FSL_BOOK3E */
97 hpdp
->pd
= ((unsigned long)new & ~PD_HUGE
) | pshift
;
99 /* If we bailed from the for loop early, an error occurred, clean up */
100 if (i
< num_hugepd
) {
101 for (i
= i
- 1 ; i
>= 0; i
--, hpdp
--)
103 kmem_cache_free(cachep
, new);
106 if (!hugepd_none(*hpdp
))
107 kmem_cache_free(cachep
, new);
109 #ifdef CONFIG_PPC_BOOK3S_64
110 hpdp
->pd
= (unsigned long)new |
111 (shift_to_mmu_psize(pshift
) << 2);
113 hpdp
->pd
= ((unsigned long)new & ~PD_HUGE
) | pshift
;
117 spin_unlock(&mm
->page_table_lock
);
122 * These macros define how to determine which level of the page table holds
125 #ifdef CONFIG_PPC_FSL_BOOK3E
126 #define HUGEPD_PGD_SHIFT PGDIR_SHIFT
127 #define HUGEPD_PUD_SHIFT PUD_SHIFT
129 #define HUGEPD_PGD_SHIFT PUD_SHIFT
130 #define HUGEPD_PUD_SHIFT PMD_SHIFT
133 #ifdef CONFIG_PPC_BOOK3S_64
135 * At this point we do the placement change only for BOOK3S 64. This would
136 * possibly work on other subarchs.
138 pte_t
*huge_pte_alloc(struct mm_struct
*mm
, unsigned long addr
, unsigned long sz
)
143 hugepd_t
*hpdp
= NULL
;
144 unsigned pshift
= __ffs(sz
);
145 unsigned pdshift
= PGDIR_SHIFT
;
148 pg
= pgd_offset(mm
, addr
);
150 if (pshift
== PGDIR_SHIFT
)
153 else if (pshift
> PUD_SHIFT
)
155 * We need to use hugepd table
157 hpdp
= (hugepd_t
*)pg
;
160 pu
= pud_alloc(mm
, pg
, addr
);
161 if (pshift
== PUD_SHIFT
)
163 else if (pshift
> PMD_SHIFT
)
164 hpdp
= (hugepd_t
*)pu
;
167 pm
= pmd_alloc(mm
, pu
, addr
);
168 if (pshift
== PMD_SHIFT
)
172 hpdp
= (hugepd_t
*)pm
;
178 BUG_ON(!hugepd_none(*hpdp
) && !hugepd_ok(*hpdp
));
180 if (hugepd_none(*hpdp
) && __hugepte_alloc(mm
, hpdp
, addr
, pdshift
, pshift
))
183 return hugepte_offset(*hpdp
, addr
, pdshift
);
188 pte_t
*huge_pte_alloc(struct mm_struct
*mm
, unsigned long addr
, unsigned long sz
)
193 hugepd_t
*hpdp
= NULL
;
194 unsigned pshift
= __ffs(sz
);
195 unsigned pdshift
= PGDIR_SHIFT
;
199 pg
= pgd_offset(mm
, addr
);
201 if (pshift
>= HUGEPD_PGD_SHIFT
) {
202 hpdp
= (hugepd_t
*)pg
;
205 pu
= pud_alloc(mm
, pg
, addr
);
206 if (pshift
>= HUGEPD_PUD_SHIFT
) {
207 hpdp
= (hugepd_t
*)pu
;
210 pm
= pmd_alloc(mm
, pu
, addr
);
211 hpdp
= (hugepd_t
*)pm
;
218 BUG_ON(!hugepd_none(*hpdp
) && !hugepd_ok(*hpdp
));
220 if (hugepd_none(*hpdp
) && __hugepte_alloc(mm
, hpdp
, addr
, pdshift
, pshift
))
223 return hugepte_offset(*hpdp
, addr
, pdshift
);
227 #ifdef CONFIG_PPC_FSL_BOOK3E
228 /* Build list of addresses of gigantic pages. This function is used in early
229 * boot before the buddy allocator is setup.
231 void add_gpage(u64 addr
, u64 page_size
, unsigned long number_of_pages
)
233 unsigned int idx
= shift_to_mmu_psize(__ffs(page_size
));
239 gpage_freearray
[idx
].nr_gpages
= number_of_pages
;
241 for (i
= 0; i
< number_of_pages
; i
++) {
242 gpage_freearray
[idx
].gpage_list
[i
] = addr
;
248 * Moves the gigantic page addresses from the temporary list to the
249 * huge_boot_pages list.
251 int alloc_bootmem_huge_page(struct hstate
*hstate
)
253 struct huge_bootmem_page
*m
;
254 int idx
= shift_to_mmu_psize(huge_page_shift(hstate
));
255 int nr_gpages
= gpage_freearray
[idx
].nr_gpages
;
260 #ifdef CONFIG_HIGHMEM
262 * If gpages can be in highmem we can't use the trick of storing the
263 * data structure in the page; allocate space for this
265 m
= memblock_virt_alloc(sizeof(struct huge_bootmem_page
), 0);
266 m
->phys
= gpage_freearray
[idx
].gpage_list
[--nr_gpages
];
268 m
= phys_to_virt(gpage_freearray
[idx
].gpage_list
[--nr_gpages
]);
271 list_add(&m
->list
, &huge_boot_pages
);
272 gpage_freearray
[idx
].nr_gpages
= nr_gpages
;
273 gpage_freearray
[idx
].gpage_list
[nr_gpages
] = 0;
279 * Scan the command line hugepagesz= options for gigantic pages; store those in
280 * a list that we use to allocate the memory once all options are parsed.
283 unsigned long gpage_npages
[MMU_PAGE_COUNT
];
285 static int __init
do_gpage_early_setup(char *param
, char *val
,
286 const char *unused
, void *arg
)
288 static phys_addr_t size
;
289 unsigned long npages
;
292 * The hugepagesz and hugepages cmdline options are interleaved. We
293 * use the size variable to keep track of whether or not this was done
294 * properly and skip over instances where it is incorrect. Other
295 * command-line parsing code will issue warnings, so we don't need to.
298 if ((strcmp(param
, "default_hugepagesz") == 0) ||
299 (strcmp(param
, "hugepagesz") == 0)) {
300 size
= memparse(val
, NULL
);
301 } else if (strcmp(param
, "hugepages") == 0) {
303 if (sscanf(val
, "%lu", &npages
) <= 0)
305 if (npages
> MAX_NUMBER_GPAGES
) {
306 pr_warn("MMU: %lu pages requested for page "
307 "size %llu KB, limiting to "
308 __stringify(MAX_NUMBER_GPAGES
) "\n",
309 npages
, size
/ 1024);
310 npages
= MAX_NUMBER_GPAGES
;
312 gpage_npages
[shift_to_mmu_psize(__ffs(size
))] = npages
;
321 * This function allocates physical space for pages that are larger than the
322 * buddy allocator can handle. We want to allocate these in highmem because
323 * the amount of lowmem is limited. This means that this function MUST be
324 * called before lowmem_end_addr is set up in MMU_init() in order for the lmb
325 * allocate to grab highmem.
327 void __init
reserve_hugetlb_gpages(void)
329 static __initdata
char cmdline
[COMMAND_LINE_SIZE
];
330 phys_addr_t size
, base
;
333 strlcpy(cmdline
, boot_command_line
, COMMAND_LINE_SIZE
);
334 parse_args("hugetlb gpages", cmdline
, NULL
, 0, 0, 0,
335 NULL
, &do_gpage_early_setup
);
338 * Walk gpage list in reverse, allocating larger page sizes first.
339 * Skip over unsupported sizes, or sizes that have 0 gpages allocated.
340 * When we reach the point in the list where pages are no longer
341 * considered gpages, we're done.
343 for (i
= MMU_PAGE_COUNT
-1; i
>= 0; i
--) {
344 if (mmu_psize_defs
[i
].shift
== 0 || gpage_npages
[i
] == 0)
346 else if (mmu_psize_to_shift(i
) < (MAX_ORDER
+ PAGE_SHIFT
))
349 size
= (phys_addr_t
)(1ULL << mmu_psize_to_shift(i
));
350 base
= memblock_alloc_base(size
* gpage_npages
[i
], size
,
351 MEMBLOCK_ALLOC_ANYWHERE
);
352 add_gpage(base
, size
, gpage_npages
[i
]);
356 #else /* !PPC_FSL_BOOK3E */
358 /* Build list of addresses of gigantic pages. This function is used in early
359 * boot before the buddy allocator is setup.
361 void add_gpage(u64 addr
, u64 page_size
, unsigned long number_of_pages
)
365 while (number_of_pages
> 0) {
366 gpage_freearray
[nr_gpages
] = addr
;
373 /* Moves the gigantic page addresses from the temporary list to the
374 * huge_boot_pages list.
376 int alloc_bootmem_huge_page(struct hstate
*hstate
)
378 struct huge_bootmem_page
*m
;
381 m
= phys_to_virt(gpage_freearray
[--nr_gpages
]);
382 gpage_freearray
[nr_gpages
] = 0;
383 list_add(&m
->list
, &huge_boot_pages
);
389 #ifdef CONFIG_PPC_FSL_BOOK3E
390 #define HUGEPD_FREELIST_SIZE \
391 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
393 struct hugepd_freelist
{
399 static DEFINE_PER_CPU(struct hugepd_freelist
*, hugepd_freelist_cur
);
401 static void hugepd_free_rcu_callback(struct rcu_head
*head
)
403 struct hugepd_freelist
*batch
=
404 container_of(head
, struct hugepd_freelist
, rcu
);
407 for (i
= 0; i
< batch
->index
; i
++)
408 kmem_cache_free(hugepte_cache
, batch
->ptes
[i
]);
410 free_page((unsigned long)batch
);
413 static void hugepd_free(struct mmu_gather
*tlb
, void *hugepte
)
415 struct hugepd_freelist
**batchp
;
417 batchp
= this_cpu_ptr(&hugepd_freelist_cur
);
419 if (atomic_read(&tlb
->mm
->mm_users
) < 2 ||
420 cpumask_equal(mm_cpumask(tlb
->mm
),
421 cpumask_of(smp_processor_id()))) {
422 kmem_cache_free(hugepte_cache
, hugepte
);
423 put_cpu_var(hugepd_freelist_cur
);
427 if (*batchp
== NULL
) {
428 *batchp
= (struct hugepd_freelist
*)__get_free_page(GFP_ATOMIC
);
429 (*batchp
)->index
= 0;
432 (*batchp
)->ptes
[(*batchp
)->index
++] = hugepte
;
433 if ((*batchp
)->index
== HUGEPD_FREELIST_SIZE
) {
434 call_rcu_sched(&(*batchp
)->rcu
, hugepd_free_rcu_callback
);
437 put_cpu_var(hugepd_freelist_cur
);
441 static void free_hugepd_range(struct mmu_gather
*tlb
, hugepd_t
*hpdp
, int pdshift
,
442 unsigned long start
, unsigned long end
,
443 unsigned long floor
, unsigned long ceiling
)
445 pte_t
*hugepte
= hugepd_page(*hpdp
);
448 unsigned long pdmask
= ~((1UL << pdshift
) - 1);
449 unsigned int num_hugepd
= 1;
451 #ifdef CONFIG_PPC_FSL_BOOK3E
452 /* Note: On fsl the hpdp may be the first of several */
453 num_hugepd
= (1 << (hugepd_shift(*hpdp
) - pdshift
));
455 unsigned int shift
= hugepd_shift(*hpdp
);
466 if (end
- 1 > ceiling
- 1)
469 for (i
= 0; i
< num_hugepd
; i
++, hpdp
++)
472 #ifdef CONFIG_PPC_FSL_BOOK3E
473 hugepd_free(tlb
, hugepte
);
475 pgtable_free_tlb(tlb
, hugepte
, pdshift
- shift
);
479 static void hugetlb_free_pmd_range(struct mmu_gather
*tlb
, pud_t
*pud
,
480 unsigned long addr
, unsigned long end
,
481 unsigned long floor
, unsigned long ceiling
)
489 pmd
= pmd_offset(pud
, addr
);
490 next
= pmd_addr_end(addr
, end
);
491 if (!is_hugepd(__hugepd(pmd_val(*pmd
)))) {
493 * if it is not hugepd pointer, we should already find
496 WARN_ON(!pmd_none_or_clear_bad(pmd
));
499 #ifdef CONFIG_PPC_FSL_BOOK3E
501 * Increment next by the size of the huge mapping since
502 * there may be more than one entry at this level for a
503 * single hugepage, but all of them point to
504 * the same kmem cache that holds the hugepte.
506 next
= addr
+ (1 << hugepd_shift(*(hugepd_t
*)pmd
));
508 free_hugepd_range(tlb
, (hugepd_t
*)pmd
, PMD_SHIFT
,
509 addr
, next
, floor
, ceiling
);
510 } while (addr
= next
, addr
!= end
);
520 if (end
- 1 > ceiling
- 1)
523 pmd
= pmd_offset(pud
, start
);
525 pmd_free_tlb(tlb
, pmd
, start
);
526 mm_dec_nr_pmds(tlb
->mm
);
529 static void hugetlb_free_pud_range(struct mmu_gather
*tlb
, pgd_t
*pgd
,
530 unsigned long addr
, unsigned long end
,
531 unsigned long floor
, unsigned long ceiling
)
539 pud
= pud_offset(pgd
, addr
);
540 next
= pud_addr_end(addr
, end
);
541 if (!is_hugepd(__hugepd(pud_val(*pud
)))) {
542 if (pud_none_or_clear_bad(pud
))
544 hugetlb_free_pmd_range(tlb
, pud
, addr
, next
, floor
,
547 #ifdef CONFIG_PPC_FSL_BOOK3E
549 * Increment next by the size of the huge mapping since
550 * there may be more than one entry at this level for a
551 * single hugepage, but all of them point to
552 * the same kmem cache that holds the hugepte.
554 next
= addr
+ (1 << hugepd_shift(*(hugepd_t
*)pud
));
556 free_hugepd_range(tlb
, (hugepd_t
*)pud
, PUD_SHIFT
,
557 addr
, next
, floor
, ceiling
);
559 } while (addr
= next
, addr
!= end
);
565 ceiling
&= PGDIR_MASK
;
569 if (end
- 1 > ceiling
- 1)
572 pud
= pud_offset(pgd
, start
);
574 pud_free_tlb(tlb
, pud
, start
);
578 * This function frees user-level page tables of a process.
580 void hugetlb_free_pgd_range(struct mmu_gather
*tlb
,
581 unsigned long addr
, unsigned long end
,
582 unsigned long floor
, unsigned long ceiling
)
588 * Because there are a number of different possible pagetable
589 * layouts for hugepage ranges, we limit knowledge of how
590 * things should be laid out to the allocation path
591 * (huge_pte_alloc(), above). Everything else works out the
592 * structure as it goes from information in the hugepd
593 * pointers. That means that we can't here use the
594 * optimization used in the normal page free_pgd_range(), of
595 * checking whether we're actually covering a large enough
596 * range to have to do anything at the top level of the walk
597 * instead of at the bottom.
599 * To make sense of this, you should probably go read the big
600 * block comment at the top of the normal free_pgd_range(),
605 next
= pgd_addr_end(addr
, end
);
606 pgd
= pgd_offset(tlb
->mm
, addr
);
607 if (!is_hugepd(__hugepd(pgd_val(*pgd
)))) {
608 if (pgd_none_or_clear_bad(pgd
))
610 hugetlb_free_pud_range(tlb
, pgd
, addr
, next
, floor
, ceiling
);
612 #ifdef CONFIG_PPC_FSL_BOOK3E
614 * Increment next by the size of the huge mapping since
615 * there may be more than one entry at the pgd level
616 * for a single hugepage, but all of them point to the
617 * same kmem cache that holds the hugepte.
619 next
= addr
+ (1 << hugepd_shift(*(hugepd_t
*)pgd
));
621 free_hugepd_range(tlb
, (hugepd_t
*)pgd
, PGDIR_SHIFT
,
622 addr
, next
, floor
, ceiling
);
624 } while (addr
= next
, addr
!= end
);
628 * We are holding mmap_sem, so a parallel huge page collapse cannot run.
629 * To prevent hugepage split, disable irq.
632 follow_huge_addr(struct mm_struct
*mm
, unsigned long address
, int write
)
637 unsigned long mask
, flags
;
638 struct page
*page
= ERR_PTR(-EINVAL
);
640 local_irq_save(flags
);
641 ptep
= find_linux_pte_or_hugepte(mm
->pgd
, address
, &is_thp
, &shift
);
644 pte
= READ_ONCE(*ptep
);
646 * Verify it is a huge page else bail.
647 * Transparent hugepages are handled by generic code. We can skip them
650 if (!shift
|| is_thp
)
653 if (!pte_present(pte
)) {
657 mask
= (1UL << shift
) - 1;
658 page
= pte_page(pte
);
660 page
+= (address
& mask
) / PAGE_SIZE
;
663 local_irq_restore(flags
);
668 follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
669 pmd_t
*pmd
, int write
)
676 follow_huge_pud(struct mm_struct
*mm
, unsigned long address
,
677 pud_t
*pud
, int write
)
683 static unsigned long hugepte_addr_end(unsigned long addr
, unsigned long end
,
686 unsigned long __boundary
= (addr
+ sz
) & ~(sz
-1);
687 return (__boundary
- 1 < end
- 1) ? __boundary
: end
;
690 int gup_huge_pd(hugepd_t hugepd
, unsigned long addr
, unsigned pdshift
,
691 unsigned long end
, int write
, struct page
**pages
, int *nr
)
694 unsigned long sz
= 1UL << hugepd_shift(hugepd
);
697 ptep
= hugepte_offset(hugepd
, addr
, pdshift
);
699 next
= hugepte_addr_end(addr
, end
, sz
);
700 if (!gup_hugepte(ptep
, sz
, addr
, end
, write
, pages
, nr
))
702 } while (ptep
++, addr
= next
, addr
!= end
);
707 #ifdef CONFIG_PPC_MM_SLICES
708 unsigned long hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
709 unsigned long len
, unsigned long pgoff
,
712 struct hstate
*hstate
= hstate_file(file
);
713 int mmu_psize
= shift_to_mmu_psize(huge_page_shift(hstate
));
715 return slice_get_unmapped_area(addr
, len
, flags
, mmu_psize
, 1);
719 unsigned long vma_mmu_pagesize(struct vm_area_struct
*vma
)
721 #ifdef CONFIG_PPC_MM_SLICES
722 unsigned int psize
= get_slice_psize(vma
->vm_mm
, vma
->vm_start
);
724 return 1UL << mmu_psize_to_shift(psize
);
726 if (!is_vm_hugetlb_page(vma
))
729 return huge_page_size(hstate_vma(vma
));
733 static inline bool is_power_of_4(unsigned long x
)
735 if (is_power_of_2(x
))
736 return (__ilog2(x
) % 2) ? false : true;
740 static int __init
add_huge_page_size(unsigned long long size
)
742 int shift
= __ffs(size
);
745 /* Check that it is a page size supported by the hardware and
746 * that it fits within pagetable and slice limits. */
747 #ifdef CONFIG_PPC_FSL_BOOK3E
748 if ((size
< PAGE_SIZE
) || !is_power_of_4(size
))
751 if (!is_power_of_2(size
)
752 || (shift
> SLICE_HIGH_SHIFT
) || (shift
<= PAGE_SHIFT
))
756 if ((mmu_psize
= shift_to_mmu_psize(shift
)) < 0)
759 BUG_ON(mmu_psize_defs
[mmu_psize
].shift
!= shift
);
761 /* Return if huge page size has already been setup */
762 if (size_to_hstate(size
))
765 hugetlb_add_hstate(shift
- PAGE_SHIFT
);
770 static int __init
hugepage_setup_sz(char *str
)
772 unsigned long long size
;
774 size
= memparse(str
, &str
);
776 if (add_huge_page_size(size
) != 0)
777 printk(KERN_WARNING
"Invalid huge page size specified(%llu)\n", size
);
781 __setup("hugepagesz=", hugepage_setup_sz
);
783 #ifdef CONFIG_PPC_FSL_BOOK3E
784 struct kmem_cache
*hugepte_cache
;
785 static int __init
hugetlbpage_init(void)
789 for (psize
= 0; psize
< MMU_PAGE_COUNT
; ++psize
) {
792 if (!mmu_psize_defs
[psize
].shift
)
795 shift
= mmu_psize_to_shift(psize
);
797 /* Don't treat normal page sizes as huge... */
798 if (shift
!= PAGE_SHIFT
)
799 if (add_huge_page_size(1ULL << shift
) < 0)
804 * Create a kmem cache for hugeptes. The bottom bits in the pte have
805 * size information encoded in them, so align them to allow this
807 hugepte_cache
= kmem_cache_create("hugepte-cache", sizeof(pte_t
),
808 HUGEPD_SHIFT_MASK
+ 1, 0, NULL
);
809 if (hugepte_cache
== NULL
)
810 panic("%s: Unable to create kmem cache for hugeptes\n",
813 /* Default hpage size = 4M */
814 if (mmu_psize_defs
[MMU_PAGE_4M
].shift
)
815 HPAGE_SHIFT
= mmu_psize_defs
[MMU_PAGE_4M
].shift
;
817 panic("%s: Unable to set default huge page size\n", __func__
);
823 static int __init
hugetlbpage_init(void)
827 if (!mmu_has_feature(MMU_FTR_16M_PAGE
))
830 for (psize
= 0; psize
< MMU_PAGE_COUNT
; ++psize
) {
834 if (!mmu_psize_defs
[psize
].shift
)
837 shift
= mmu_psize_to_shift(psize
);
839 if (add_huge_page_size(1ULL << shift
) < 0)
842 if (shift
< PMD_SHIFT
)
844 else if (shift
< PUD_SHIFT
)
847 pdshift
= PGDIR_SHIFT
;
849 * if we have pdshift and shift value same, we don't
850 * use pgt cache for hugepd.
852 if (pdshift
!= shift
) {
853 pgtable_cache_add(pdshift
- shift
, NULL
);
854 if (!PGT_CACHE(pdshift
- shift
))
855 panic("hugetlbpage_init(): could not create "
856 "pgtable cache for %d bit pagesize\n", 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
;
871 arch_initcall(hugetlbpage_init
);
873 void flush_dcache_icache_hugepage(struct page
*page
)
878 BUG_ON(!PageCompound(page
));
880 for (i
= 0; i
< (1UL << compound_order(page
)); i
++) {
881 if (!PageHighMem(page
)) {
882 __flush_dcache_icache(page_address(page
+i
));
884 start
= kmap_atomic(page
+i
);
885 __flush_dcache_icache(start
);
886 kunmap_atomic(start
);
891 #endif /* CONFIG_HUGETLB_PAGE */
894 * We have 4 cases for pgds and pmds:
895 * (1) invalid (all zeroes)
896 * (2) pointer to next table, as normal; bottom 6 bits == 0
897 * (3) leaf pte for huge page, bottom two bits != 00
898 * (4) hugepd pointer, bottom two bits == 00, next 4 bits indicate size of table
900 * So long as we atomically load page table pointers we are safe against teardown,
901 * we can follow the address down to the the page and take a ref on it.
902 * This function need to be called with interrupts disabled. We use this variant
903 * when we have MSR[EE] = 0 but the paca->soft_enabled = 1
906 pte_t
*__find_linux_pte_or_hugepte(pgd_t
*pgdir
, unsigned long ea
,
907 bool *is_thp
, unsigned *shift
)
913 hugepd_t
*hpdp
= NULL
;
914 unsigned pdshift
= PGDIR_SHIFT
;
922 pgdp
= pgdir
+ pgd_index(ea
);
923 pgd
= READ_ONCE(*pgdp
);
925 * Always operate on the local stack value. This make sure the
926 * value don't get updated by a parallel THP split/collapse,
927 * page fault or a page unmap. The return pte_t * is still not
928 * stable. So should be checked there for above conditions.
932 else if (pgd_huge(pgd
)) {
933 ret_pte
= (pte_t
*) pgdp
;
935 } else if (is_hugepd(__hugepd(pgd_val(pgd
))))
936 hpdp
= (hugepd_t
*)&pgd
;
939 * Even if we end up with an unmap, the pgtable will not
940 * be freed, because we do an rcu free and here we are
944 pudp
= pud_offset(&pgd
, ea
);
945 pud
= READ_ONCE(*pudp
);
949 else if (pud_huge(pud
)) {
950 ret_pte
= (pte_t
*) pudp
;
952 } else if (is_hugepd(__hugepd(pud_val(pud
))))
953 hpdp
= (hugepd_t
*)&pud
;
956 pmdp
= pmd_offset(&pud
, ea
);
957 pmd
= READ_ONCE(*pmdp
);
959 * A hugepage collapse is captured by pmd_none, because
960 * it mark the pmd none and do a hpte invalidate.
962 * We don't worry about pmd_trans_splitting here, The
963 * caller if it needs to handle the splitting case
964 * should check for that.
969 if (pmd_trans_huge(pmd
)) {
972 ret_pte
= (pte_t
*) pmdp
;
977 ret_pte
= (pte_t
*) pmdp
;
979 } else if (is_hugepd(__hugepd(pmd_val(pmd
))))
980 hpdp
= (hugepd_t
*)&pmd
;
982 return pte_offset_kernel(&pmd
, ea
);
988 ret_pte
= hugepte_offset(*hpdp
, ea
, pdshift
);
989 pdshift
= hugepd_shift(*hpdp
);
995 EXPORT_SYMBOL_GPL(__find_linux_pte_or_hugepte
);
997 int gup_hugepte(pte_t
*ptep
, unsigned long sz
, unsigned long addr
,
998 unsigned long end
, int write
, struct page
**pages
, int *nr
)
1001 unsigned long pte_end
;
1002 struct page
*head
, *page
, *tail
;
1006 pte_end
= (addr
+ sz
) & ~(sz
-1);
1010 pte
= READ_ONCE(*ptep
);
1011 mask
= _PAGE_PRESENT
| _PAGE_USER
;
1015 if ((pte_val(pte
) & mask
) != mask
)
1018 /* hugepages are never "special" */
1019 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
1022 head
= pte_page(pte
);
1024 page
= head
+ ((addr
& (sz
-1)) >> PAGE_SHIFT
);
1027 VM_BUG_ON(compound_head(page
) != head
);
1032 } while (addr
+= PAGE_SIZE
, addr
!= end
);
1034 if (!page_cache_add_speculative(head
, refs
)) {
1039 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
1040 /* Could be optimized better */
1048 * Any tail page need their mapcount reference taken before we
1053 get_huge_page_tail(tail
);