2 * IA-32 Huge TLB Page Support for Kernel.
4 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
7 #include <linux/config.h>
8 #include <linux/init.h>
11 #include <linux/hugetlb.h>
12 #include <linux/pagemap.h>
13 #include <linux/smp_lock.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/sysctl.h>
19 #include <asm/tlbflush.h>
21 static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
27 pgd = pgd_offset(mm, addr);
28 pud = pud_alloc(mm, pgd, addr);
29 pmd = pmd_alloc(mm, pud, addr);
33 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
39 pgd = pgd_offset(mm, addr);
40 pud = pud_offset(pgd, addr);
41 pmd = pmd_offset(pud, addr);
45 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma, struct page *page, pte_t * page_table, int write_access)
49 vx_rsspages_add(mm, HPAGE_SIZE / PAGE_SIZE);
52 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
54 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
55 entry = pte_mkyoung(entry);
57 set_pte(page_table, entry);
61 * This function checks for proper alignment of input addr and len parameters.
63 int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
65 if (len & ~HPAGE_MASK)
67 if (addr & ~HPAGE_MASK)
72 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
73 struct vm_area_struct *vma)
75 pte_t *src_pte, *dst_pte, entry;
77 unsigned long addr = vma->vm_start;
78 unsigned long end = vma->vm_end;
81 dst_pte = huge_pte_alloc(dst, addr);
84 src_pte = huge_pte_offset(src, addr);
86 ptepage = pte_page(entry);
88 set_pte(dst_pte, entry);
89 vx_rsspages_add(dst, HPAGE_SIZE / PAGE_SIZE);
99 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
100 struct page **pages, struct vm_area_struct **vmas,
101 unsigned long *position, int *length, int i)
103 unsigned long vpfn, vaddr = *position;
104 int remainder = *length;
106 WARN_ON(!is_vm_hugetlb_page(vma));
108 vpfn = vaddr/PAGE_SIZE;
109 while (vaddr < vma->vm_end && remainder) {
115 pte = huge_pte_offset(mm, vaddr);
117 /* hugetlb should be locked, and hence, prefaulted */
118 WARN_ON(!pte || pte_none(*pte));
120 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
122 WARN_ON(!PageCompound(page));
143 #if 0 /* This is just for testing */
145 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
147 unsigned long start = address;
151 struct vm_area_struct *vma;
153 vma = find_vma(mm, addr);
154 if (!vma || !is_vm_hugetlb_page(vma))
155 return ERR_PTR(-EINVAL);
157 pte = huge_pte_offset(mm, address);
159 /* hugetlb should be locked, and hence, prefaulted */
160 WARN_ON(!pte || pte_none(*pte));
162 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
164 WARN_ON(!PageCompound(page));
169 int pmd_huge(pmd_t pmd)
175 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
176 pmd_t *pmd, int write)
184 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
186 return ERR_PTR(-EINVAL);
189 int pmd_huge(pmd_t pmd)
191 return !!(pmd_val(pmd) & _PAGE_PSE);
195 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
196 pmd_t *pmd, int write)
200 page = pte_page(*(pte_t *)pmd);
202 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
207 void unmap_hugepage_range(struct vm_area_struct *vma,
208 unsigned long start, unsigned long end)
210 struct mm_struct *mm = vma->vm_mm;
211 unsigned long address;
215 BUG_ON(start & (HPAGE_SIZE - 1));
216 BUG_ON(end & (HPAGE_SIZE - 1));
218 for (address = start; address < end; address += HPAGE_SIZE) {
219 pte = ptep_get_and_clear(huge_pte_offset(mm, address));
222 page = pte_page(pte);
225 vx_rsspages_sub(mm, (end - start) >> PAGE_SHIFT);
226 flush_tlb_range(vma, start, end);
229 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
231 struct mm_struct *mm = current->mm;
235 BUG_ON(vma->vm_start & ~HPAGE_MASK);
236 BUG_ON(vma->vm_end & ~HPAGE_MASK);
238 spin_lock(&mm->page_table_lock);
239 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
241 pte_t *pte = huge_pte_alloc(mm, addr);
249 if (!pte_none(*pte)) {
250 pmd_t *pmd = (pmd_t *) pte;
252 page = pmd_page(*pmd);
255 dec_page_state(nr_page_table_pages);
256 page_cache_release(page);
259 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
260 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
261 page = find_get_page(mapping, idx);
263 /* charge the fs quota first */
264 if (hugetlb_get_quota(mapping)) {
268 page = alloc_huge_page();
270 hugetlb_put_quota(mapping);
274 ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
278 hugetlb_put_quota(mapping);
279 free_huge_page(page);
283 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
286 spin_unlock(&mm->page_table_lock);
290 /* x86_64 also uses this file */
292 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
293 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
294 unsigned long addr, unsigned long len,
295 unsigned long pgoff, unsigned long flags)
297 struct mm_struct *mm = current->mm;
298 struct vm_area_struct *vma;
299 unsigned long start_addr;
301 start_addr = mm->free_area_cache;
304 addr = ALIGN(start_addr, HPAGE_SIZE);
306 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
307 /* At this point: (!vma || addr < vma->vm_end). */
308 if (TASK_SIZE - len < addr) {
310 * Start a new search - just in case we missed
313 if (start_addr != TASK_UNMAPPED_BASE) {
314 start_addr = TASK_UNMAPPED_BASE;
319 if (!vma || addr + len <= vma->vm_start) {
320 mm->free_area_cache = addr + len;
323 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
327 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
328 unsigned long addr0, unsigned long len,
329 unsigned long pgoff, unsigned long flags)
331 struct mm_struct *mm = current->mm;
332 struct vm_area_struct *vma, *prev_vma;
333 unsigned long base = mm->mmap_base, addr = addr0;
336 /* don't allow allocations above current base */
337 if (mm->free_area_cache > base)
338 mm->free_area_cache = base;
341 /* make sure it can fit in the remaining address space */
342 if (mm->free_area_cache < len)
345 /* either no address requested or cant fit in requested address hole */
346 addr = (mm->free_area_cache - len) & HPAGE_MASK;
349 * Lookup failure means no vma is above this address,
350 * i.e. return with success:
352 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
356 * new region fits between prev_vma->vm_end and
357 * vma->vm_start, use it:
359 if (addr + len <= vma->vm_start &&
360 (!prev_vma || (addr >= prev_vma->vm_end)))
361 /* remember the address as a hint for next time */
362 return (mm->free_area_cache = addr);
364 /* pull free_area_cache down to the first hole */
365 if (mm->free_area_cache == vma->vm_end)
366 mm->free_area_cache = vma->vm_start;
368 /* try just below the current vma->vm_start */
369 addr = (vma->vm_start - len) & HPAGE_MASK;
370 } while (len <= vma->vm_start);
374 * if hint left us with no space for the requested
375 * mapping then try again:
378 mm->free_area_cache = base;
383 * A failed mmap() very likely causes application failure,
384 * so fall back to the bottom-up function here. This scenario
385 * can happen with large stack limits and large mmap()
388 mm->free_area_cache = TASK_UNMAPPED_BASE;
389 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
393 * Restore the topdown base:
395 mm->free_area_cache = base;
401 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
402 unsigned long len, unsigned long pgoff, unsigned long flags)
404 struct mm_struct *mm = current->mm;
405 struct vm_area_struct *vma;
407 if (len & ~HPAGE_MASK)
413 addr = ALIGN(addr, HPAGE_SIZE);
414 vma = find_vma(mm, addr);
415 if (TASK_SIZE - len >= addr &&
416 (!vma || addr + len <= vma->vm_start))
419 if (mm->get_unmapped_area == arch_get_unmapped_area)
420 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
423 return hugetlb_get_unmapped_area_topdown(file, addr, len,
427 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/