1
2
3
4
5
6
7
8
9
10
11#include <linux/mm.h>
12#include <linux/io.h>
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 <linux/swap.h>
21#include <linux/swapops.h>
22#include <asm/pgtable.h>
23#include <asm/pgalloc.h>
24#include <asm/tlb.h>
25#include <asm/setup.h>
26#include <asm/hugetlb.h>
27#include <asm/pte-walk.h>
28
29
30#ifdef CONFIG_HUGETLB_PAGE
31
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
37
38bool hugetlb_disabled = false;
39
40unsigned int HPAGE_SHIFT;
41EXPORT_SYMBOL(HPAGE_SHIFT);
42
43#define hugepd_none(hpd) (hpd_val(hpd) == 0)
44
45pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz)
46{
47
48
49
50
51 return __find_linux_pte(mm->pgd, addr, NULL, NULL);
52}
53
54static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
55 unsigned long address, unsigned int pdshift,
56 unsigned int pshift, spinlock_t *ptl)
57{
58 struct kmem_cache *cachep;
59 pte_t *new;
60 int i;
61 int num_hugepd;
62
63 if (pshift >= pdshift) {
64 cachep = hugepte_cache;
65 num_hugepd = 1 << (pshift - pdshift);
66 } else {
67 cachep = PGT_CACHE(pdshift - pshift);
68 num_hugepd = 1;
69 }
70
71 new = kmem_cache_zalloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
72
73 BUG_ON(pshift > HUGEPD_SHIFT_MASK);
74 BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
75
76 if (! new)
77 return -ENOMEM;
78
79
80
81
82
83
84 smp_wmb();
85
86 spin_lock(ptl);
87
88
89
90
91
92
93 for (i = 0; i < num_hugepd; i++, hpdp++) {
94 if (unlikely(!hugepd_none(*hpdp)))
95 break;
96 else {
97#ifdef CONFIG_PPC_BOOK3S_64
98 *hpdp = __hugepd(__pa(new) |
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);
104#else
105
106 *hpdp = __hugepd(((unsigned long)new & ~PD_HUGE) | pshift);
107#endif
108 }
109 }
110
111 if (i < num_hugepd) {
112 for (i = i - 1 ; i >= 0; i--, hpdp--)
113 *hpdp = __hugepd(0);
114 kmem_cache_free(cachep, new);
115 }
116 spin_unlock(ptl);
117 return 0;
118}
119
120
121
122
123
124#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
125#define HUGEPD_PGD_SHIFT PGDIR_SHIFT
126#define HUGEPD_PUD_SHIFT PUD_SHIFT
127#endif
128
129
130
131
132
133pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
134{
135 pgd_t *pg;
136 pud_t *pu;
137 pmd_t *pm;
138 hugepd_t *hpdp = NULL;
139 unsigned pshift = __ffs(sz);
140 unsigned pdshift = PGDIR_SHIFT;
141 spinlock_t *ptl;
142
143 addr &= ~(sz-1);
144 pg = pgd_offset(mm, addr);
145
146#ifdef CONFIG_PPC_BOOK3S_64
147 if (pshift == PGDIR_SHIFT)
148
149 return (pte_t *) pg;
150 else if (pshift > PUD_SHIFT) {
151
152
153
154 ptl = &mm->page_table_lock;
155 hpdp = (hugepd_t *)pg;
156 } else {
157 pdshift = PUD_SHIFT;
158 pu = pud_alloc(mm, pg, addr);
159 if (pshift == PUD_SHIFT)
160 return (pte_t *)pu;
161 else if (pshift > PMD_SHIFT) {
162 ptl = pud_lockptr(mm, pu);
163 hpdp = (hugepd_t *)pu;
164 } else {
165 pdshift = PMD_SHIFT;
166 pm = pmd_alloc(mm, pu, addr);
167 if (pshift == PMD_SHIFT)
168
169 return (pte_t *)pm;
170 else {
171 ptl = pmd_lockptr(mm, pm);
172 hpdp = (hugepd_t *)pm;
173 }
174 }
175 }
176#else
177 if (pshift >= HUGEPD_PGD_SHIFT) {
178 ptl = &mm->page_table_lock;
179 hpdp = (hugepd_t *)pg;
180 } else {
181 pdshift = PUD_SHIFT;
182 pu = pud_alloc(mm, pg, addr);
183 if (pshift >= HUGEPD_PUD_SHIFT) {
184 ptl = pud_lockptr(mm, pu);
185 hpdp = (hugepd_t *)pu;
186 } else {
187 pdshift = PMD_SHIFT;
188 pm = pmd_alloc(mm, pu, addr);
189 ptl = pmd_lockptr(mm, pm);
190 hpdp = (hugepd_t *)pm;
191 }
192 }
193#endif
194 if (!hpdp)
195 return NULL;
196
197 BUG_ON(!hugepd_none(*hpdp) && !hugepd_ok(*hpdp));
198
199 if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr,
200 pdshift, pshift, ptl))
201 return NULL;
202
203 return hugepte_offset(*hpdp, addr, pdshift);
204}
205
206#ifdef CONFIG_PPC_BOOK3S_64
207
208
209
210
211#define MAX_NUMBER_GPAGES 1024
212__initdata static u64 gpage_freearray[MAX_NUMBER_GPAGES];
213__initdata static unsigned nr_gpages;
214
215
216
217
218
219void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages)
220{
221 if (!addr)
222 return;
223 while (number_of_pages > 0) {
224 gpage_freearray[nr_gpages] = addr;
225 nr_gpages++;
226 number_of_pages--;
227 addr += page_size;
228 }
229}
230
231int __init pseries_alloc_bootmem_huge_page(struct hstate *hstate)
232{
233 struct huge_bootmem_page *m;
234 if (nr_gpages == 0)
235 return 0;
236 m = phys_to_virt(gpage_freearray[--nr_gpages]);
237 gpage_freearray[nr_gpages] = 0;
238 list_add(&m->list, &huge_boot_pages);
239 m->hstate = hstate;
240 return 1;
241}
242#endif
243
244
245int __init alloc_bootmem_huge_page(struct hstate *h)
246{
247
248#ifdef CONFIG_PPC_BOOK3S_64
249 if (firmware_has_feature(FW_FEATURE_LPAR) && !radix_enabled())
250 return pseries_alloc_bootmem_huge_page(h);
251#endif
252 return __alloc_bootmem_huge_page(h);
253}
254
255#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
256#define HUGEPD_FREELIST_SIZE \
257 ((PAGE_SIZE - sizeof(struct hugepd_freelist)) / sizeof(pte_t))
258
259struct hugepd_freelist {
260 struct rcu_head rcu;
261 unsigned int index;
262 void *ptes[0];
263};
264
265static DEFINE_PER_CPU(struct hugepd_freelist *, hugepd_freelist_cur);
266
267static void hugepd_free_rcu_callback(struct rcu_head *head)
268{
269 struct hugepd_freelist *batch =
270 container_of(head, struct hugepd_freelist, rcu);
271 unsigned int i;
272
273 for (i = 0; i < batch->index; i++)
274 kmem_cache_free(hugepte_cache, batch->ptes[i]);
275
276 free_page((unsigned long)batch);
277}
278
279static void hugepd_free(struct mmu_gather *tlb, void *hugepte)
280{
281 struct hugepd_freelist **batchp;
282
283 batchp = &get_cpu_var(hugepd_freelist_cur);
284
285 if (atomic_read(&tlb->mm->mm_users) < 2 ||
286 mm_is_thread_local(tlb->mm)) {
287 kmem_cache_free(hugepte_cache, hugepte);
288 put_cpu_var(hugepd_freelist_cur);
289 return;
290 }
291
292 if (*batchp == NULL) {
293 *batchp = (struct hugepd_freelist *)__get_free_page(GFP_ATOMIC);
294 (*batchp)->index = 0;
295 }
296
297 (*batchp)->ptes[(*batchp)->index++] = hugepte;
298 if ((*batchp)->index == HUGEPD_FREELIST_SIZE) {
299 call_rcu_sched(&(*batchp)->rcu, hugepd_free_rcu_callback);
300 *batchp = NULL;
301 }
302 put_cpu_var(hugepd_freelist_cur);
303}
304#else
305static inline void hugepd_free(struct mmu_gather *tlb, void *hugepte) {}
306#endif
307
308static void free_hugepd_range(struct mmu_gather *tlb, hugepd_t *hpdp, int pdshift,
309 unsigned long start, unsigned long end,
310 unsigned long floor, unsigned long ceiling)
311{
312 pte_t *hugepte = hugepd_page(*hpdp);
313 int i;
314
315 unsigned long pdmask = ~((1UL << pdshift) - 1);
316 unsigned int num_hugepd = 1;
317 unsigned int shift = hugepd_shift(*hpdp);
318
319
320 if (shift > pdshift)
321 num_hugepd = 1 << (shift - pdshift);
322
323 start &= pdmask;
324 if (start < floor)
325 return;
326 if (ceiling) {
327 ceiling &= pdmask;
328 if (! ceiling)
329 return;
330 }
331 if (end - 1 > ceiling - 1)
332 return;
333
334 for (i = 0; i < num_hugepd; i++, hpdp++)
335 *hpdp = __hugepd(0);
336
337 if (shift >= pdshift)
338 hugepd_free(tlb, hugepte);
339 else
340 pgtable_free_tlb(tlb, hugepte,
341 get_hugepd_cache_index(pdshift - shift));
342}
343
344static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
345 unsigned long addr, unsigned long end,
346 unsigned long floor, unsigned long ceiling)
347{
348 pmd_t *pmd;
349 unsigned long next;
350 unsigned long start;
351
352 start = addr;
353 do {
354 unsigned long more;
355
356 pmd = pmd_offset(pud, addr);
357 next = pmd_addr_end(addr, end);
358 if (!is_hugepd(__hugepd(pmd_val(*pmd)))) {
359
360
361
362
363 WARN_ON(!pmd_none_or_clear_bad(pmd));
364 continue;
365 }
366
367
368
369
370
371
372 more = addr + (1 << hugepd_shift(*(hugepd_t *)pmd));
373 if (more > next)
374 next = more;
375
376 free_hugepd_range(tlb, (hugepd_t *)pmd, PMD_SHIFT,
377 addr, next, floor, ceiling);
378 } while (addr = next, addr != end);
379
380 start &= PUD_MASK;
381 if (start < floor)
382 return;
383 if (ceiling) {
384 ceiling &= PUD_MASK;
385 if (!ceiling)
386 return;
387 }
388 if (end - 1 > ceiling - 1)
389 return;
390
391 pmd = pmd_offset(pud, start);
392 pud_clear(pud);
393 pmd_free_tlb(tlb, pmd, start);
394 mm_dec_nr_pmds(tlb->mm);
395}
396
397static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
398 unsigned long addr, unsigned long end,
399 unsigned long floor, unsigned long ceiling)
400{
401 pud_t *pud;
402 unsigned long next;
403 unsigned long start;
404
405 start = addr;
406 do {
407 pud = pud_offset(pgd, addr);
408 next = pud_addr_end(addr, end);
409 if (!is_hugepd(__hugepd(pud_val(*pud)))) {
410 if (pud_none_or_clear_bad(pud))
411 continue;
412 hugetlb_free_pmd_range(tlb, pud, addr, next, floor,
413 ceiling);
414 } else {
415 unsigned long more;
416
417
418
419
420
421
422 more = addr + (1 << hugepd_shift(*(hugepd_t *)pud));
423 if (more > next)
424 next = more;
425
426 free_hugepd_range(tlb, (hugepd_t *)pud, PUD_SHIFT,
427 addr, next, floor, ceiling);
428 }
429 } while (addr = next, addr != end);
430
431 start &= PGDIR_MASK;
432 if (start < floor)
433 return;
434 if (ceiling) {
435 ceiling &= PGDIR_MASK;
436 if (!ceiling)
437 return;
438 }
439 if (end - 1 > ceiling - 1)
440 return;
441
442 pud = pud_offset(pgd, start);
443 pgd_clear(pgd);
444 pud_free_tlb(tlb, pud, start);
445 mm_dec_nr_puds(tlb->mm);
446}
447
448
449
450
451void hugetlb_free_pgd_range(struct mmu_gather *tlb,
452 unsigned long addr, unsigned long end,
453 unsigned long floor, unsigned long ceiling)
454{
455 pgd_t *pgd;
456 unsigned long next;
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475 do {
476 next = pgd_addr_end(addr, end);
477 pgd = pgd_offset(tlb->mm, addr);
478 if (!is_hugepd(__hugepd(pgd_val(*pgd)))) {
479 if (pgd_none_or_clear_bad(pgd))
480 continue;
481 hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
482 } else {
483 unsigned long more;
484
485
486
487
488
489
490 more = addr + (1 << hugepd_shift(*(hugepd_t *)pgd));
491 if (more > next)
492 next = more;
493
494 free_hugepd_range(tlb, (hugepd_t *)pgd, PGDIR_SHIFT,
495 addr, next, floor, ceiling);
496 }
497 } while (addr = next, addr != end);
498}
499
500struct page *follow_huge_pd(struct vm_area_struct *vma,
501 unsigned long address, hugepd_t hpd,
502 int flags, int pdshift)
503{
504 pte_t *ptep;
505 spinlock_t *ptl;
506 struct page *page = NULL;
507 unsigned long mask;
508 int shift = hugepd_shift(hpd);
509 struct mm_struct *mm = vma->vm_mm;
510
511retry:
512
513
514
515
516 ptl = &mm->page_table_lock;
517 spin_lock(ptl);
518
519 ptep = hugepte_offset(hpd, address, pdshift);
520 if (pte_present(*ptep)) {
521 mask = (1UL << shift) - 1;
522 page = pte_page(*ptep);
523 page += ((address & mask) >> PAGE_SHIFT);
524 if (flags & FOLL_GET)
525 get_page(page);
526 } else {
527 if (is_hugetlb_entry_migration(*ptep)) {
528 spin_unlock(ptl);
529 __migration_entry_wait(mm, ptep, ptl);
530 goto retry;
531 }
532 }
533 spin_unlock(ptl);
534 return page;
535}
536
537static unsigned long hugepte_addr_end(unsigned long addr, unsigned long end,
538 unsigned long sz)
539{
540 unsigned long __boundary = (addr + sz) & ~(sz-1);
541 return (__boundary - 1 < end - 1) ? __boundary : end;
542}
543
544int gup_huge_pd(hugepd_t hugepd, unsigned long addr, unsigned pdshift,
545 unsigned long end, int write, struct page **pages, int *nr)
546{
547 pte_t *ptep;
548 unsigned long sz = 1UL << hugepd_shift(hugepd);
549 unsigned long next;
550
551 ptep = hugepte_offset(hugepd, addr, pdshift);
552 do {
553 next = hugepte_addr_end(addr, end, sz);
554 if (!gup_hugepte(ptep, sz, addr, end, write, pages, nr))
555 return 0;
556 } while (ptep++, addr = next, addr != end);
557
558 return 1;
559}
560
561#ifdef CONFIG_PPC_MM_SLICES
562unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
563 unsigned long len, unsigned long pgoff,
564 unsigned long flags)
565{
566 struct hstate *hstate = hstate_file(file);
567 int mmu_psize = shift_to_mmu_psize(huge_page_shift(hstate));
568
569#ifdef CONFIG_PPC_RADIX_MMU
570 if (radix_enabled())
571 return radix__hugetlb_get_unmapped_area(file, addr, len,
572 pgoff, flags);
573#endif
574 return slice_get_unmapped_area(addr, len, flags, mmu_psize, 1);
575}
576#endif
577
578unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
579{
580#ifdef CONFIG_PPC_MM_SLICES
581
582 if (!radix_enabled()) {
583 unsigned int psize = get_slice_psize(vma->vm_mm, vma->vm_start);
584
585 return 1UL << mmu_psize_to_shift(psize);
586 }
587#endif
588 return vma_kernel_pagesize(vma);
589}
590
591static inline bool is_power_of_4(unsigned long x)
592{
593 if (is_power_of_2(x))
594 return (__ilog2(x) % 2) ? false : true;
595 return false;
596}
597
598static int __init add_huge_page_size(unsigned long long size)
599{
600 int shift = __ffs(size);
601 int mmu_psize;
602
603
604
605 if (size <= PAGE_SIZE)
606 return -EINVAL;
607#if defined(CONFIG_PPC_FSL_BOOK3E)
608 if (!is_power_of_4(size))
609 return -EINVAL;
610#elif !defined(CONFIG_PPC_8xx)
611 if (!is_power_of_2(size) || (shift > SLICE_HIGH_SHIFT))
612 return -EINVAL;
613#endif
614
615 if ((mmu_psize = shift_to_mmu_psize(shift)) < 0)
616 return -EINVAL;
617
618#ifdef CONFIG_PPC_BOOK3S_64
619
620
621
622
623
624
625
626
627 if (radix_enabled()) {
628 if (mmu_psize != MMU_PAGE_2M && mmu_psize != MMU_PAGE_1G)
629 return -EINVAL;
630 } else {
631 if (mmu_psize != MMU_PAGE_16M && mmu_psize != MMU_PAGE_16G)
632 return -EINVAL;
633 }
634#endif
635
636 BUG_ON(mmu_psize_defs[mmu_psize].shift != shift);
637
638
639 if (size_to_hstate(size))
640 return 0;
641
642 hugetlb_add_hstate(shift - PAGE_SHIFT);
643
644 return 0;
645}
646
647static int __init hugepage_setup_sz(char *str)
648{
649 unsigned long long size;
650
651 size = memparse(str, &str);
652
653 if (add_huge_page_size(size) != 0) {
654 hugetlb_bad_size();
655 pr_err("Invalid huge page size specified(%llu)\n", size);
656 }
657
658 return 1;
659}
660__setup("hugepagesz=", hugepage_setup_sz);
661
662struct kmem_cache *hugepte_cache;
663static int __init hugetlbpage_init(void)
664{
665 int psize;
666
667 if (hugetlb_disabled) {
668 pr_info("HugeTLB support is disabled!\n");
669 return 0;
670 }
671
672#if !defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_PPC_8xx)
673 if (!radix_enabled() && !mmu_has_feature(MMU_FTR_16M_PAGE))
674 return -ENODEV;
675#endif
676 for (psize = 0; psize < MMU_PAGE_COUNT; ++psize) {
677 unsigned shift;
678 unsigned pdshift;
679
680 if (!mmu_psize_defs[psize].shift)
681 continue;
682
683 shift = mmu_psize_to_shift(psize);
684
685#ifdef CONFIG_PPC_BOOK3S_64
686 if (shift > PGDIR_SHIFT)
687 continue;
688 else if (shift > PUD_SHIFT)
689 pdshift = PGDIR_SHIFT;
690 else if (shift > PMD_SHIFT)
691 pdshift = PUD_SHIFT;
692 else
693 pdshift = PMD_SHIFT;
694#else
695 if (shift < HUGEPD_PUD_SHIFT)
696 pdshift = PMD_SHIFT;
697 else if (shift < HUGEPD_PGD_SHIFT)
698 pdshift = PUD_SHIFT;
699 else
700 pdshift = PGDIR_SHIFT;
701#endif
702
703 if (add_huge_page_size(1ULL << shift) < 0)
704 continue;
705
706
707
708
709 if (pdshift > shift)
710 pgtable_cache_add(pdshift - shift, NULL);
711#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
712 else if (!hugepte_cache) {
713
714
715
716
717
718 hugepte_cache = kmem_cache_create("hugepte-cache",
719 sizeof(pte_t),
720 HUGEPD_SHIFT_MASK + 1,
721 0, NULL);
722 if (hugepte_cache == NULL)
723 panic("%s: Unable to create kmem cache "
724 "for hugeptes\n", __func__);
725
726 }
727#endif
728 }
729
730#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_8xx)
731
732 if (mmu_psize_defs[MMU_PAGE_4M].shift)
733 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_4M].shift;
734 else if (mmu_psize_defs[MMU_PAGE_512K].shift)
735 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_512K].shift;
736#else
737
738
739
740 if (mmu_psize_defs[MMU_PAGE_16M].shift)
741 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_16M].shift;
742 else if (mmu_psize_defs[MMU_PAGE_1M].shift)
743 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_1M].shift;
744 else if (mmu_psize_defs[MMU_PAGE_2M].shift)
745 HPAGE_SHIFT = mmu_psize_defs[MMU_PAGE_2M].shift;
746#endif
747 return 0;
748}
749
750arch_initcall(hugetlbpage_init);
751
752void flush_dcache_icache_hugepage(struct page *page)
753{
754 int i;
755 void *start;
756
757 BUG_ON(!PageCompound(page));
758
759 for (i = 0; i < (1UL << compound_order(page)); i++) {
760 if (!PageHighMem(page)) {
761 __flush_dcache_icache(page_address(page+i));
762 } else {
763 start = kmap_atomic(page+i);
764 __flush_dcache_icache(start);
765 kunmap_atomic(start);
766 }
767 }
768}
769
770#endif
771
772
773
774
775
776
777
778
779
780
781
782
783
784pte_t *__find_linux_pte(pgd_t *pgdir, unsigned long ea,
785 bool *is_thp, unsigned *hpage_shift)
786{
787 pgd_t pgd, *pgdp;
788 pud_t pud, *pudp;
789 pmd_t pmd, *pmdp;
790 pte_t *ret_pte;
791 hugepd_t *hpdp = NULL;
792 unsigned pdshift = PGDIR_SHIFT;
793
794 if (hpage_shift)
795 *hpage_shift = 0;
796
797 if (is_thp)
798 *is_thp = false;
799
800 pgdp = pgdir + pgd_index(ea);
801 pgd = READ_ONCE(*pgdp);
802
803
804
805
806
807
808 if (pgd_none(pgd))
809 return NULL;
810 else if (pgd_huge(pgd)) {
811 ret_pte = (pte_t *) pgdp;
812 goto out;
813 } else if (is_hugepd(__hugepd(pgd_val(pgd))))
814 hpdp = (hugepd_t *)&pgd;
815 else {
816
817
818
819
820
821 pdshift = PUD_SHIFT;
822 pudp = pud_offset(&pgd, ea);
823 pud = READ_ONCE(*pudp);
824
825 if (pud_none(pud))
826 return NULL;
827 else if (pud_huge(pud)) {
828 ret_pte = (pte_t *) pudp;
829 goto out;
830 } else if (is_hugepd(__hugepd(pud_val(pud))))
831 hpdp = (hugepd_t *)&pud;
832 else {
833 pdshift = PMD_SHIFT;
834 pmdp = pmd_offset(&pud, ea);
835 pmd = READ_ONCE(*pmdp);
836
837
838
839
840 if (pmd_none(pmd))
841 return NULL;
842
843 if (pmd_trans_huge(pmd) || pmd_devmap(pmd)) {
844 if (is_thp)
845 *is_thp = true;
846 ret_pte = (pte_t *) pmdp;
847 goto out;
848 }
849
850 if (pmd_huge(pmd)) {
851 ret_pte = (pte_t *) pmdp;
852 goto out;
853 } else if (is_hugepd(__hugepd(pmd_val(pmd))))
854 hpdp = (hugepd_t *)&pmd;
855 else
856 return pte_offset_kernel(&pmd, ea);
857 }
858 }
859 if (!hpdp)
860 return NULL;
861
862 ret_pte = hugepte_offset(*hpdp, ea, pdshift);
863 pdshift = hugepd_shift(*hpdp);
864out:
865 if (hpage_shift)
866 *hpage_shift = pdshift;
867 return ret_pte;
868}
869EXPORT_SYMBOL_GPL(__find_linux_pte);
870
871int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
872 unsigned long end, int write, struct page **pages, int *nr)
873{
874 unsigned long pte_end;
875 struct page *head, *page;
876 pte_t pte;
877 int refs;
878
879 pte_end = (addr + sz) & ~(sz-1);
880 if (pte_end < end)
881 end = pte_end;
882
883 pte = READ_ONCE(*ptep);
884
885 if (!pte_access_permitted(pte, write))
886 return 0;
887
888
889 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
890
891 refs = 0;
892 head = pte_page(pte);
893
894 page = head + ((addr & (sz-1)) >> PAGE_SHIFT);
895 do {
896 VM_BUG_ON(compound_head(page) != head);
897 pages[*nr] = page;
898 (*nr)++;
899 page++;
900 refs++;
901 } while (addr += PAGE_SIZE, addr != end);
902
903 if (!page_cache_add_speculative(head, refs)) {
904 *nr -= refs;
905 return 0;
906 }
907
908 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
909
910 *nr -= refs;
911 while (refs--)
912 put_page(head);
913 return 0;
914 }
915
916 return 1;
917}
918