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 mm->rss += (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;
267 /* the context change must make it to memory before the flush,
268 * so that further SLB misses do the right thing. */
270 on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
275 int prepare_hugepage_range(unsigned long addr, unsigned long len)
277 if (within_hugepage_high_range(addr, len))
279 else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
281 /* Yes, we need both tests, in case addr+len overflows
282 * 64-bit arithmetic */
283 err = open_low_hpage_segs(current->mm,
284 LOW_ESID_MASK(addr, len));
286 printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
287 " failed (segs: 0x%04hx)\n", addr, len,
288 LOW_ESID_MASK(addr, len));
295 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
296 struct vm_area_struct *vma)
298 pte_t *src_pte, *dst_pte, entry;
299 struct page *ptepage;
300 unsigned long addr = vma->vm_start;
301 unsigned long end = vma->vm_end;
305 dst_pte = huge_pte_alloc(dst, addr);
309 src_pte = huge_pte_offset(src, addr);
312 ptepage = pte_page(entry);
314 dst->rss += (HPAGE_SIZE / PAGE_SIZE);
315 set_pte(dst_pte, entry);
326 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
327 struct page **pages, struct vm_area_struct **vmas,
328 unsigned long *position, int *length, int i)
330 unsigned long vpfn, vaddr = *position;
331 int remainder = *length;
333 WARN_ON(!is_vm_hugetlb_page(vma));
335 vpfn = vaddr/PAGE_SIZE;
336 while (vaddr < vma->vm_end && remainder) {
341 pte = huge_pte_offset(mm, vaddr);
343 /* hugetlb should be locked, and hence, prefaulted */
344 WARN_ON(!pte || pte_none(*pte));
346 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
348 WARN_ON(!PageCompound(page));
370 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
375 if (! in_hugepage_area(mm->context, address))
376 return ERR_PTR(-EINVAL);
378 ptep = huge_pte_offset(mm, address);
379 page = pte_page(*ptep);
381 page += (address % HPAGE_SIZE) / PAGE_SIZE;
386 int pmd_huge(pmd_t pmd)
392 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
393 pmd_t *pmd, int write)
399 void unmap_hugepage_range(struct vm_area_struct *vma,
400 unsigned long start, unsigned long end)
402 struct mm_struct *mm = vma->vm_mm;
407 WARN_ON(!is_vm_hugetlb_page(vma));
408 BUG_ON((start % HPAGE_SIZE) != 0);
409 BUG_ON((end % HPAGE_SIZE) != 0);
411 for (addr = start; addr < end; addr += HPAGE_SIZE) {
414 ptep = huge_pte_offset(mm, addr);
415 if (!ptep || pte_none(*ptep))
419 page = pte_page(pte);
424 mm->rss -= (end - start) >> PAGE_SHIFT;
428 void hugetlb_free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
429 unsigned long start, unsigned long end)
431 /* Because the huge pgtables are only 2 level, they can take
432 * at most around 4M, much less than one hugepage which the
433 * process is presumably entitled to use. So we don't bother
434 * freeing up the pagetables on unmap, and wait until
435 * destroy_context() to clean up the lot. */
438 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
440 struct mm_struct *mm = current->mm;
444 WARN_ON(!is_vm_hugetlb_page(vma));
445 BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
446 BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
448 spin_lock(&mm->page_table_lock);
449 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
451 pte_t *pte = huge_pte_alloc(mm, addr);
458 if (! pte_none(*pte))
461 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
462 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
463 page = find_get_page(mapping, idx);
465 /* charge the fs quota first */
466 if (hugetlb_get_quota(mapping)) {
470 page = alloc_huge_page();
472 hugetlb_put_quota(mapping);
476 ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
480 hugetlb_put_quota(mapping);
481 free_huge_page(page);
485 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
488 spin_unlock(&mm->page_table_lock);
492 /* Because we have an exclusive hugepage region which lies within the
493 * normal user address space, we have to take special measures to make
494 * non-huge mmap()s evade the hugepage reserved regions. */
495 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
496 unsigned long len, unsigned long pgoff,
499 struct mm_struct *mm = current->mm;
500 struct vm_area_struct *vma;
501 unsigned long start_addr;
507 addr = PAGE_ALIGN(addr);
508 vma = find_vma(mm, addr);
509 if (((TASK_SIZE - len) >= addr)
510 && (!vma || (addr+len) <= vma->vm_start)
511 && !is_hugepage_only_range(addr,len))
514 start_addr = addr = mm->free_area_cache;
517 vma = find_vma(mm, addr);
518 while (TASK_SIZE - len >= addr) {
519 BUG_ON(vma && (addr >= vma->vm_end));
521 if (touches_hugepage_low_range(addr, len)) {
522 addr = ALIGN(addr+1, 1<<SID_SHIFT);
523 vma = find_vma(mm, addr);
526 if (touches_hugepage_high_range(addr, len)) {
527 addr = TASK_HPAGE_END;
528 vma = find_vma(mm, addr);
531 if (!vma || addr + len <= vma->vm_start) {
533 * Remember the place where we stopped the search:
535 mm->free_area_cache = addr + len;
542 /* Make sure we didn't miss any holes */
543 if (start_addr != TASK_UNMAPPED_BASE) {
544 start_addr = addr = TASK_UNMAPPED_BASE;
551 * This mmap-allocator allocates new areas top-down from below the
552 * stack's low limit (the base):
554 * Because we have an exclusive hugepage region which lies within the
555 * normal user address space, we have to take special measures to make
556 * non-huge mmap()s evade the hugepage reserved regions.
559 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
560 const unsigned long len, const unsigned long pgoff,
561 const unsigned long flags)
563 struct vm_area_struct *vma, *prev_vma;
564 struct mm_struct *mm = current->mm;
565 unsigned long base = mm->mmap_base, addr = addr0;
568 /* requested length too big for entire address space */
572 /* dont allow allocations above current base */
573 if (mm->free_area_cache > base)
574 mm->free_area_cache = base;
576 /* requesting a specific address */
578 addr = PAGE_ALIGN(addr);
579 vma = find_vma(mm, addr);
580 if (TASK_SIZE - len >= addr &&
581 (!vma || addr + len <= vma->vm_start)
582 && !is_hugepage_only_range(addr,len))
587 /* make sure it can fit in the remaining address space */
588 if (mm->free_area_cache < len)
591 /* either no address requested or cant fit in requested address hole */
592 addr = (mm->free_area_cache - len) & PAGE_MASK;
595 if (touches_hugepage_low_range(addr, len)) {
596 addr = (addr & ((~0) << SID_SHIFT)) - len;
597 goto hugepage_recheck;
598 } else if (touches_hugepage_high_range(addr, len)) {
599 addr = TASK_HPAGE_BASE - len;
603 * Lookup failure means no vma is above this address,
604 * i.e. return with success:
606 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
610 * new region fits between prev_vma->vm_end and
611 * vma->vm_start, use it:
613 if (addr+len <= vma->vm_start &&
614 (!prev_vma || (addr >= prev_vma->vm_end)))
615 /* remember the address as a hint for next time */
616 return (mm->free_area_cache = addr);
618 /* pull free_area_cache down to the first hole */
619 if (mm->free_area_cache == vma->vm_end)
620 mm->free_area_cache = vma->vm_start;
622 /* try just below the current vma->vm_start */
623 addr = vma->vm_start-len;
624 } while (len <= vma->vm_start);
628 * if hint left us with no space for the requested
629 * mapping then try again:
632 mm->free_area_cache = base;
637 * A failed mmap() very likely causes application failure,
638 * so fall back to the bottom-up function here. This scenario
639 * can happen with large stack limits and large mmap()
642 mm->free_area_cache = TASK_UNMAPPED_BASE;
643 addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
645 * Restore the topdown base:
647 mm->free_area_cache = base;
652 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
654 unsigned long addr = 0;
655 struct vm_area_struct *vma;
657 vma = find_vma(current->mm, addr);
658 while (addr + len <= 0x100000000UL) {
659 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
661 if (! __within_hugepage_low_range(addr, len, segmask)) {
662 addr = ALIGN(addr+1, 1<<SID_SHIFT);
663 vma = find_vma(current->mm, addr);
667 if (!vma || (addr + len) <= vma->vm_start)
669 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
670 /* Depending on segmask this might not be a confirmed
671 * hugepage region, so the ALIGN could have skipped
673 vma = find_vma(current->mm, addr);
679 static unsigned long htlb_get_high_area(unsigned long len)
681 unsigned long addr = TASK_HPAGE_BASE;
682 struct vm_area_struct *vma;
684 vma = find_vma(current->mm, addr);
685 for (vma = find_vma(current->mm, addr);
686 addr + len <= TASK_HPAGE_END;
687 vma = vma->vm_next) {
688 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
689 BUG_ON(! within_hugepage_high_range(addr, len));
691 if (!vma || (addr + len) <= vma->vm_start)
693 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
694 /* Because we're in a hugepage region, this alignment
695 * should not skip us over any VMAs */
701 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
702 unsigned long len, unsigned long pgoff,
705 if (len & ~HPAGE_MASK)
708 if (!(cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE))
711 if (test_thread_flag(TIF_32BIT)) {
713 u16 segmask, cursegs = current->mm->context.htlb_segs;
715 /* First see if we can do the mapping in the existing
716 * low hpage segments */
717 addr = htlb_get_low_area(len, cursegs);
721 for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
722 ! lastshift; segmask >>=1) {
726 addr = htlb_get_low_area(len, cursegs | segmask);
727 if ((addr != -ENOMEM)
728 && open_low_hpage_segs(current->mm, segmask) == 0)
731 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
732 " enough segments\n");
735 return htlb_get_high_area(len);
739 void hugetlb_mm_free_pgd(struct mm_struct *mm)
744 spin_lock(&mm->page_table_lock);
746 pgdir = mm->context.huge_pgdir;
750 mm->context.huge_pgdir = NULL;
752 /* cleanup any hugepte pages leftover */
753 for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
754 pgd_t *pgd = pgdir + i;
756 if (! pgd_none(*pgd)) {
757 pte_t *pte = (pte_t *)pgd_page(*pgd);
758 struct page *ptepage = virt_to_page(pte);
760 ptepage->mapping = NULL;
762 BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
763 kmem_cache_free(zero_cache, pte);
768 BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
769 kmem_cache_free(zero_cache, pgdir);
772 spin_unlock(&mm->page_table_lock);
775 int hash_huge_page(struct mm_struct *mm, unsigned long access,
776 unsigned long ea, unsigned long vsid, int local)
779 unsigned long va, vpn;
781 pte_t old_pte, new_pte;
782 unsigned long hpteflags, prpn;
786 spin_lock(&mm->page_table_lock);
788 ptep = huge_pte_offset(mm, ea);
790 /* Search the Linux page table for a match with va */
791 va = (vsid << 28) | (ea & 0x0fffffff);
792 vpn = va >> HPAGE_SHIFT;
795 * If no pte found or not present, send the problem up to
798 if (unlikely(!ptep || pte_none(*ptep)))
801 /* BUG_ON(pte_bad(*ptep)); */
804 * Check the user's access rights to the page. If access should be
805 * prevented then send the problem up to do_page_fault.
807 is_write = access & _PAGE_RW;
808 if (unlikely(is_write && !(pte_val(*ptep) & _PAGE_RW)))
811 * At this point, we have a pte (old_pte) which can be used to build
812 * or update an HPTE. There are 2 cases:
814 * 1. There is a valid (present) pte with no associated HPTE (this is
815 * the most common case)
816 * 2. There is a valid (present) pte with an associated HPTE. The
817 * current values of the pp bits in the HPTE prevent access
818 * because we are doing software DIRTY bit management and the
819 * page is currently not DIRTY.
826 hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
828 /* Check if pte already has an hpte (case 2) */
829 if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
830 /* There MIGHT be an HPTE for this pte */
831 unsigned long hash, slot;
833 hash = hpt_hash(vpn, 1);
834 if (pte_val(old_pte) & _PAGE_SECONDARY)
836 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
837 slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
839 if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
840 pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
843 if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
844 unsigned long hash = hpt_hash(vpn, 1);
845 unsigned long hpte_group;
847 prpn = pte_pfn(old_pte);
850 hpte_group = ((hash & htab_hash_mask) *
851 HPTES_PER_GROUP) & ~0x7UL;
853 /* Update the linux pte with the HPTE slot */
854 pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
855 pte_val(new_pte) |= _PAGE_HASHPTE;
857 /* Add in WIMG bits */
858 /* XXX We should store these in the pte */
859 hpteflags |= _PAGE_COHERENT;
861 slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
864 /* Primary is full, try the secondary */
865 if (unlikely(slot == -1)) {
866 pte_val(new_pte) |= _PAGE_SECONDARY;
867 hpte_group = ((~hash & htab_hash_mask) *
868 HPTES_PER_GROUP) & ~0x7UL;
869 slot = ppc_md.hpte_insert(hpte_group, va, prpn,
873 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
875 ppc_md.hpte_remove(hpte_group);
880 if (unlikely(slot == -2))
881 panic("hash_huge_page: pte_insert failed\n");
883 pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
886 * No need to use ldarx/stdcx here because all who
887 * might be updating the pte will hold the
896 spin_unlock(&mm->page_table_lock);