2 * PPC64 (POWER4) Huge TLB Page Support for Kernel.
4 * Copyright (C) 2003 David Gibson, IBM Corporation.
6 * Based on the IA-32 version:
7 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
10 #include <linux/init.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/smp_lock.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/sysctl.h>
20 #include <asm/pgalloc.h>
22 #include <asm/tlbflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/machdep.h>
25 #include <asm/cputable.h>
28 #include <linux/sysctl.h>
30 #define HUGEPGDIR_SHIFT (HPAGE_SHIFT + PAGE_SHIFT - 3)
31 #define HUGEPGDIR_SIZE (1UL << HUGEPGDIR_SHIFT)
32 #define HUGEPGDIR_MASK (~(HUGEPGDIR_SIZE-1))
34 #define HUGEPTE_INDEX_SIZE 9
35 #define HUGEPGD_INDEX_SIZE 10
37 #define PTRS_PER_HUGEPTE (1 << HUGEPTE_INDEX_SIZE)
38 #define PTRS_PER_HUGEPGD (1 << HUGEPGD_INDEX_SIZE)
40 static inline int hugepgd_index(unsigned long addr)
42 return (addr & ~REGION_MASK) >> HUGEPGDIR_SHIFT;
45 static pgd_t *hugepgd_offset(struct mm_struct *mm, unsigned long addr)
49 if (! mm->context.huge_pgdir)
53 index = hugepgd_index(addr);
54 BUG_ON(index >= PTRS_PER_HUGEPGD);
55 return mm->context.huge_pgdir + index;
58 static inline pte_t *hugepte_offset(pgd_t *dir, unsigned long addr)
65 index = (addr >> HPAGE_SHIFT) % PTRS_PER_HUGEPTE;
66 return (pte_t *)pgd_page(*dir) + index;
69 static pgd_t *hugepgd_alloc(struct mm_struct *mm, unsigned long addr)
71 BUG_ON(! in_hugepage_area(mm->context, addr));
73 if (! mm->context.huge_pgdir) {
75 spin_unlock(&mm->page_table_lock);
76 /* Don't use pgd_alloc(), because we want __GFP_REPEAT */
77 new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
78 BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
79 spin_lock(&mm->page_table_lock);
82 * Because we dropped the lock, we should re-check the
83 * entry, as somebody else could have populated it..
85 if (mm->context.huge_pgdir)
88 mm->context.huge_pgdir = new;
90 return hugepgd_offset(mm, addr);
93 static pte_t *hugepte_alloc(struct mm_struct *mm, pgd_t *dir,
96 if (! pgd_present(*dir)) {
99 spin_unlock(&mm->page_table_lock);
100 new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
101 BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
102 spin_lock(&mm->page_table_lock);
104 * Because we dropped the lock, we should re-check the
105 * entry, as somebody else could have populated it..
107 if (pgd_present(*dir)) {
109 kmem_cache_free(zero_cache, new);
111 struct page *ptepage;
115 ptepage = virt_to_page(new);
116 ptepage->mapping = (void *) mm;
117 ptepage->index = addr & HUGEPGDIR_MASK;
118 pgd_populate(mm, dir, new);
122 return hugepte_offset(dir, addr);
125 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
129 BUG_ON(! in_hugepage_area(mm->context, addr));
131 pgd = hugepgd_offset(mm, addr);
135 return hugepte_offset(pgd, addr);
138 static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
142 BUG_ON(! in_hugepage_area(mm->context, addr));
144 pgd = hugepgd_alloc(mm, addr);
148 return hugepte_alloc(mm, pgd, addr);
151 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma,
152 struct page *page, pte_t *ptep, int write_access)
156 vx_rsspages_add(mm, HPAGE_SIZE / PAGE_SIZE);
159 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
161 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
163 entry = pte_mkyoung(entry);
164 entry = pte_mkhuge(entry);
166 set_pte(ptep, entry);
170 * This function checks for proper alignment of input addr and len parameters.
172 int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
174 if (len & ~HPAGE_MASK)
176 if (addr & ~HPAGE_MASK)
178 if (! (within_hugepage_low_range(addr, len)
179 || within_hugepage_high_range(addr, len)) )
184 static void flush_segments(void *parm)
186 u16 segs = (unsigned long) parm;
189 asm volatile("isync" : : : "memory");
191 for (i = 0; i < 16; i++) {
192 if (! (segs & (1U << i)))
194 asm volatile("slbie %0" : : "r" (i << SID_SHIFT));
197 asm volatile("isync" : : : "memory");
200 static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg)
202 unsigned long start = seg << SID_SHIFT;
203 unsigned long end = (seg+1) << SID_SHIFT;
204 struct vm_area_struct *vma;
206 struct mmu_gather *tlb;
210 /* Check no VMAs are in the region */
211 vma = find_vma(mm, start);
212 if (vma && (vma->vm_start < end))
215 /* Clean up any leftover PTE pages in the region */
216 spin_lock(&mm->page_table_lock);
217 tlb = tlb_gather_mmu(mm, 0);
218 for (addr = start; addr < end; addr += PMD_SIZE) {
219 pgd_t *pgd = pgd_offset(mm, addr);
227 pmd = pmd_offset(pgd, addr);
228 if (!pmd || pmd_none(*pmd))
235 pte = (pte_t *)pmd_page_kernel(*pmd);
236 /* No VMAs, so there should be no PTEs, check just in case. */
237 for (i = 0; i < PTRS_PER_PTE; i++) {
238 BUG_ON(!pte_none(*pte));
241 page = pmd_page(*pmd);
244 dec_page_state(nr_page_table_pages);
245 pte_free_tlb(tlb, page);
247 tlb_finish_mmu(tlb, start, end);
248 spin_unlock(&mm->page_table_lock);
253 static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
257 newsegs &= ~(mm->context.htlb_segs);
259 return 0; /* The segments we want are already open */
261 for (i = 0; i < 16; i++)
262 if ((1 << i) & newsegs)
263 if (prepare_low_seg_for_htlb(mm, i) != 0)
266 mm->context.htlb_segs |= newsegs;
268 /* update the paca copy of the context struct */
269 get_paca()->context = mm->context;
271 /* the context change must make it to memory before the flush,
272 * so that further SLB misses do the right thing. */
274 on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
279 int prepare_hugepage_range(unsigned long addr, unsigned long len)
281 if (within_hugepage_high_range(addr, len))
283 else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
285 /* Yes, we need both tests, in case addr+len overflows
286 * 64-bit arithmetic */
287 err = open_low_hpage_segs(current->mm,
288 LOW_ESID_MASK(addr, len));
290 printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
291 " failed (segs: 0x%04hx)\n", addr, len,
292 LOW_ESID_MASK(addr, len));
299 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
300 struct vm_area_struct *vma)
302 pte_t *src_pte, *dst_pte, entry;
303 struct page *ptepage;
304 unsigned long addr = vma->vm_start;
305 unsigned long end = vma->vm_end;
309 dst_pte = huge_pte_alloc(dst, addr);
313 src_pte = huge_pte_offset(src, addr);
316 ptepage = pte_page(entry);
318 vx_rsspages_add(dst, HPAGE_SIZE / PAGE_SIZE);
319 set_pte(dst_pte, entry);
330 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
331 struct page **pages, struct vm_area_struct **vmas,
332 unsigned long *position, int *length, int i)
334 unsigned long vpfn, vaddr = *position;
335 int remainder = *length;
337 WARN_ON(!is_vm_hugetlb_page(vma));
339 vpfn = vaddr/PAGE_SIZE;
340 while (vaddr < vma->vm_end && remainder) {
345 pte = huge_pte_offset(mm, vaddr);
347 /* hugetlb should be locked, and hence, prefaulted */
348 WARN_ON(!pte || pte_none(*pte));
350 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
352 WARN_ON(!PageCompound(page));
374 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
379 if (! in_hugepage_area(mm->context, address))
380 return ERR_PTR(-EINVAL);
382 ptep = huge_pte_offset(mm, address);
383 page = pte_page(*ptep);
385 page += (address % HPAGE_SIZE) / PAGE_SIZE;
390 int pmd_huge(pmd_t pmd)
396 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
397 pmd_t *pmd, int write)
403 void unmap_hugepage_range(struct vm_area_struct *vma,
404 unsigned long start, unsigned long end)
406 struct mm_struct *mm = vma->vm_mm;
411 WARN_ON(!is_vm_hugetlb_page(vma));
412 BUG_ON((start % HPAGE_SIZE) != 0);
413 BUG_ON((end % HPAGE_SIZE) != 0);
415 for (addr = start; addr < end; addr += HPAGE_SIZE) {
418 ptep = huge_pte_offset(mm, addr);
419 if (!ptep || pte_none(*ptep))
423 page = pte_page(pte);
428 vx_rsspages_sub(mm, (end - start) >> PAGE_SHIFT);
432 void hugetlb_free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
433 unsigned long start, unsigned long end)
435 /* Because the huge pgtables are only 2 level, they can take
436 * at most around 4M, much less than one hugepage which the
437 * process is presumably entitled to use. So we don't bother
438 * freeing up the pagetables on unmap, and wait until
439 * destroy_context() to clean up the lot. */
442 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
444 struct mm_struct *mm = current->mm;
448 WARN_ON(!is_vm_hugetlb_page(vma));
449 BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
450 BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
452 spin_lock(&mm->page_table_lock);
453 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
455 pte_t *pte = huge_pte_alloc(mm, addr);
462 if (! pte_none(*pte))
465 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
466 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
467 page = find_get_page(mapping, idx);
469 /* charge the fs quota first */
470 if (hugetlb_get_quota(mapping)) {
474 page = alloc_huge_page();
476 hugetlb_put_quota(mapping);
480 ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
484 hugetlb_put_quota(mapping);
485 free_huge_page(page);
489 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
492 spin_unlock(&mm->page_table_lock);
496 /* Because we have an exclusive hugepage region which lies within the
497 * normal user address space, we have to take special measures to make
498 * non-huge mmap()s evade the hugepage reserved regions. */
499 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
500 unsigned long len, unsigned long pgoff,
503 struct mm_struct *mm = current->mm;
504 struct vm_area_struct *vma;
505 unsigned long start_addr;
511 addr = PAGE_ALIGN(addr);
512 vma = find_vma(mm, addr);
513 if (((TASK_SIZE - len) >= addr)
514 && (!vma || (addr+len) <= vma->vm_start)
515 && !is_hugepage_only_range(addr,len))
518 start_addr = addr = mm->free_area_cache;
521 vma = find_vma(mm, addr);
522 while (TASK_SIZE - len >= addr) {
523 BUG_ON(vma && (addr >= vma->vm_end));
525 if (touches_hugepage_low_range(addr, len)) {
526 addr = ALIGN(addr+1, 1<<SID_SHIFT);
527 vma = find_vma(mm, addr);
530 if (touches_hugepage_high_range(addr, len)) {
531 addr = TASK_HPAGE_END;
532 vma = find_vma(mm, addr);
535 if (!vma || addr + len <= vma->vm_start) {
537 * Remember the place where we stopped the search:
539 mm->free_area_cache = addr + len;
546 /* Make sure we didn't miss any holes */
547 if (start_addr != TASK_UNMAPPED_BASE) {
548 start_addr = addr = TASK_UNMAPPED_BASE;
555 * This mmap-allocator allocates new areas top-down from below the
556 * stack's low limit (the base):
558 * Because we have an exclusive hugepage region which lies within the
559 * normal user address space, we have to take special measures to make
560 * non-huge mmap()s evade the hugepage reserved regions.
563 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
564 const unsigned long len, const unsigned long pgoff,
565 const unsigned long flags)
567 struct vm_area_struct *vma, *prev_vma;
568 struct mm_struct *mm = current->mm;
569 unsigned long base = mm->mmap_base, addr = addr0;
572 /* requested length too big for entire address space */
576 /* dont allow allocations above current base */
577 if (mm->free_area_cache > base)
578 mm->free_area_cache = base;
580 /* requesting a specific address */
582 addr = PAGE_ALIGN(addr);
583 vma = find_vma(mm, addr);
584 if (TASK_SIZE - len >= addr &&
585 (!vma || addr + len <= vma->vm_start)
586 && !is_hugepage_only_range(addr,len))
591 /* make sure it can fit in the remaining address space */
592 if (mm->free_area_cache < len)
595 /* either no address requested or cant fit in requested address hole */
596 addr = (mm->free_area_cache - len) & PAGE_MASK;
599 if (touches_hugepage_low_range(addr, len)) {
600 addr = (addr & ((~0) << SID_SHIFT)) - len;
601 goto hugepage_recheck;
602 } else if (touches_hugepage_high_range(addr, len)) {
603 addr = TASK_HPAGE_BASE - len;
607 * Lookup failure means no vma is above this address,
608 * i.e. return with success:
610 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
614 * new region fits between prev_vma->vm_end and
615 * vma->vm_start, use it:
617 if (addr+len <= vma->vm_start &&
618 (!prev_vma || (addr >= prev_vma->vm_end)))
619 /* remember the address as a hint for next time */
620 return (mm->free_area_cache = addr);
622 /* pull free_area_cache down to the first hole */
623 if (mm->free_area_cache == vma->vm_end)
624 mm->free_area_cache = vma->vm_start;
626 /* try just below the current vma->vm_start */
627 addr = vma->vm_start-len;
628 } while (len <= vma->vm_start);
632 * if hint left us with no space for the requested
633 * mapping then try again:
636 mm->free_area_cache = base;
641 * A failed mmap() very likely causes application failure,
642 * so fall back to the bottom-up function here. This scenario
643 * can happen with large stack limits and large mmap()
646 mm->free_area_cache = TASK_UNMAPPED_BASE;
647 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
649 * Restore the topdown base:
651 mm->free_area_cache = base;
656 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
658 unsigned long addr = 0;
659 struct vm_area_struct *vma;
661 vma = find_vma(current->mm, addr);
662 while (addr + len <= 0x100000000UL) {
663 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
665 if (! __within_hugepage_low_range(addr, len, segmask)) {
666 addr = ALIGN(addr+1, 1<<SID_SHIFT);
667 vma = find_vma(current->mm, addr);
671 if (!vma || (addr + len) <= vma->vm_start)
673 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
674 /* Depending on segmask this might not be a confirmed
675 * hugepage region, so the ALIGN could have skipped
677 vma = find_vma(current->mm, addr);
683 static unsigned long htlb_get_high_area(unsigned long len)
685 unsigned long addr = TASK_HPAGE_BASE;
686 struct vm_area_struct *vma;
688 vma = find_vma(current->mm, addr);
689 for (vma = find_vma(current->mm, addr);
690 addr + len <= TASK_HPAGE_END;
691 vma = vma->vm_next) {
692 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
693 BUG_ON(! within_hugepage_high_range(addr, len));
695 if (!vma || (addr + len) <= vma->vm_start)
697 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
698 /* Because we're in a hugepage region, this alignment
699 * should not skip us over any VMAs */
705 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
706 unsigned long len, unsigned long pgoff,
709 if (len & ~HPAGE_MASK)
712 if (!(cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE))
715 if (test_thread_flag(TIF_32BIT)) {
717 u16 segmask, cursegs = current->mm->context.htlb_segs;
719 /* First see if we can do the mapping in the existing
720 * low hpage segments */
721 addr = htlb_get_low_area(len, cursegs);
725 for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
726 ! lastshift; segmask >>=1) {
730 addr = htlb_get_low_area(len, cursegs | segmask);
731 if ((addr != -ENOMEM)
732 && open_low_hpage_segs(current->mm, segmask) == 0)
735 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
736 " enough segments\n");
739 return htlb_get_high_area(len);
743 void hugetlb_mm_free_pgd(struct mm_struct *mm)
748 spin_lock(&mm->page_table_lock);
750 pgdir = mm->context.huge_pgdir;
754 mm->context.huge_pgdir = NULL;
756 /* cleanup any hugepte pages leftover */
757 for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
758 pgd_t *pgd = pgdir + i;
760 if (! pgd_none(*pgd)) {
761 pte_t *pte = (pte_t *)pgd_page(*pgd);
762 struct page *ptepage = virt_to_page(pte);
764 ptepage->mapping = NULL;
766 BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
767 kmem_cache_free(zero_cache, pte);
772 BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
773 kmem_cache_free(zero_cache, pgdir);
776 spin_unlock(&mm->page_table_lock);
779 int hash_huge_page(struct mm_struct *mm, unsigned long access,
780 unsigned long ea, unsigned long vsid, int local)
783 unsigned long va, vpn;
785 pte_t old_pte, new_pte;
786 unsigned long hpteflags, prpn;
790 spin_lock(&mm->page_table_lock);
792 ptep = huge_pte_offset(mm, ea);
794 /* Search the Linux page table for a match with va */
795 va = (vsid << 28) | (ea & 0x0fffffff);
796 vpn = va >> HPAGE_SHIFT;
799 * If no pte found or not present, send the problem up to
802 if (unlikely(!ptep || pte_none(*ptep)))
805 /* BUG_ON(pte_bad(*ptep)); */
808 * Check the user's access rights to the page. If access should be
809 * prevented then send the problem up to do_page_fault.
811 is_write = access & _PAGE_RW;
812 if (unlikely(is_write && !(pte_val(*ptep) & _PAGE_RW)))
815 * At this point, we have a pte (old_pte) which can be used to build
816 * or update an HPTE. There are 2 cases:
818 * 1. There is a valid (present) pte with no associated HPTE (this is
819 * the most common case)
820 * 2. There is a valid (present) pte with an associated HPTE. The
821 * current values of the pp bits in the HPTE prevent access
822 * because we are doing software DIRTY bit management and the
823 * page is currently not DIRTY.
830 hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
832 /* Check if pte already has an hpte (case 2) */
833 if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
834 /* There MIGHT be an HPTE for this pte */
835 unsigned long hash, slot;
837 hash = hpt_hash(vpn, 1);
838 if (pte_val(old_pte) & _PAGE_SECONDARY)
840 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
841 slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
843 if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
844 pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
847 if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
848 unsigned long hash = hpt_hash(vpn, 1);
849 unsigned long hpte_group;
851 prpn = pte_pfn(old_pte);
854 hpte_group = ((hash & htab_hash_mask) *
855 HPTES_PER_GROUP) & ~0x7UL;
857 /* Update the linux pte with the HPTE slot */
858 pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
859 pte_val(new_pte) |= _PAGE_HASHPTE;
861 /* Add in WIMG bits */
862 /* XXX We should store these in the pte */
863 hpteflags |= _PAGE_COHERENT;
865 slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
868 /* Primary is full, try the secondary */
869 if (unlikely(slot == -1)) {
870 pte_val(new_pte) |= _PAGE_SECONDARY;
871 hpte_group = ((~hash & htab_hash_mask) *
872 HPTES_PER_GROUP) & ~0x7UL;
873 slot = ppc_md.hpte_insert(hpte_group, va, prpn,
877 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
879 ppc_md.hpte_remove(hpte_group);
884 if (unlikely(slot == -2))
885 panic("hash_huge_page: pte_insert failed\n");
887 pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
890 * No need to use ldarx/stdcx here because all who
891 * might be updating the pte will hold the
900 spin_unlock(&mm->page_table_lock);