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)
26 pgd = pgd_offset(mm, addr);
27 pmd = pmd_alloc(mm, pgd, addr);
31 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
36 pgd = pgd_offset(mm, addr);
37 pmd = pmd_offset(pgd, addr);
41 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma, struct page *page, pte_t * page_table, int write_access)
45 // mm->rss += (HPAGE_SIZE / PAGE_SIZE);
46 vx_rsspages_add(mm, HPAGE_SIZE / PAGE_SIZE);
49 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
51 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
52 entry = pte_mkyoung(entry);
54 set_pte(page_table, entry);
58 * This function checks for proper alignment of input addr and len parameters.
60 int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
62 if (len & ~HPAGE_MASK)
64 if (addr & ~HPAGE_MASK)
69 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
70 struct vm_area_struct *vma)
72 pte_t *src_pte, *dst_pte, entry;
74 unsigned long addr = vma->vm_start;
75 unsigned long end = vma->vm_end;
78 dst_pte = huge_pte_alloc(dst, addr);
81 src_pte = huge_pte_offset(src, addr);
83 ptepage = pte_page(entry);
85 set_pte(dst_pte, entry);
86 // dst->rss += (HPAGE_SIZE / PAGE_SIZE);
87 vx_rsspages_add(dst, HPAGE_SIZE / PAGE_SIZE);
97 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
98 struct page **pages, struct vm_area_struct **vmas,
99 unsigned long *position, int *length, int i)
101 unsigned long vpfn, vaddr = *position;
102 int remainder = *length;
104 WARN_ON(!is_vm_hugetlb_page(vma));
106 vpfn = vaddr/PAGE_SIZE;
107 while (vaddr < vma->vm_end && remainder) {
113 pte = huge_pte_offset(mm, vaddr);
115 /* hugetlb should be locked, and hence, prefaulted */
116 WARN_ON(!pte || pte_none(*pte));
118 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
120 WARN_ON(!PageCompound(page));
141 #if 0 /* This is just for testing */
143 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
145 unsigned long start = address;
149 struct vm_area_struct *vma;
151 vma = find_vma(mm, addr);
152 if (!vma || !is_vm_hugetlb_page(vma))
153 return ERR_PTR(-EINVAL);
155 pte = huge_pte_offset(mm, address);
157 /* hugetlb should be locked, and hence, prefaulted */
158 WARN_ON(!pte || pte_none(*pte));
160 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
162 WARN_ON(!PageCompound(page));
167 int pmd_huge(pmd_t pmd)
173 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
174 pmd_t *pmd, int write)
182 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
184 return ERR_PTR(-EINVAL);
187 int pmd_huge(pmd_t pmd)
189 return !!(pmd_val(pmd) & _PAGE_PSE);
193 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
194 pmd_t *pmd, int write)
198 page = pte_page(*(pte_t *)pmd);
200 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
205 void unmap_hugepage_range(struct vm_area_struct *vma,
206 unsigned long start, unsigned long end)
208 struct mm_struct *mm = vma->vm_mm;
209 unsigned long address;
213 BUG_ON(start & (HPAGE_SIZE - 1));
214 BUG_ON(end & (HPAGE_SIZE - 1));
216 for (address = start; address < end; address += HPAGE_SIZE) {
217 pte = ptep_get_and_clear(huge_pte_offset(mm, address));
220 page = pte_page(pte);
223 // mm->rss -= (end - start) >> PAGE_SHIFT;
224 vx_rsspages_sub(mm, (end - start) >> PAGE_SHIFT);
225 flush_tlb_range(vma, start, end);
228 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
230 struct mm_struct *mm = current->mm;
234 BUG_ON(vma->vm_start & ~HPAGE_MASK);
235 BUG_ON(vma->vm_end & ~HPAGE_MASK);
237 spin_lock(&mm->page_table_lock);
238 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
240 pte_t *pte = huge_pte_alloc(mm, addr);
248 if (!pte_none(*pte)) {
249 pmd_t *pmd = (pmd_t *) pte;
251 page = pmd_page(*pmd);
254 dec_page_state(nr_page_table_pages);
255 page_cache_release(page);
258 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
259 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
260 page = find_get_page(mapping, idx);
262 /* charge the fs quota first */
263 if (hugetlb_get_quota(mapping)) {
267 page = alloc_huge_page();
269 hugetlb_put_quota(mapping);
273 ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
277 hugetlb_put_quota(mapping);
278 free_huge_page(page);
282 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
285 spin_unlock(&mm->page_table_lock);
289 /* x86_64 also uses this file */
291 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
292 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
293 unsigned long addr, unsigned long len,
294 unsigned long pgoff, unsigned long flags)
296 struct mm_struct *mm = current->mm;
297 struct vm_area_struct *vma;
298 unsigned long start_addr;
300 start_addr = mm->free_area_cache;
303 addr = ALIGN(start_addr, HPAGE_SIZE);
305 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
306 /* At this point: (!vma || addr < vma->vm_end). */
307 if (TASK_SIZE - len < addr) {
309 * Start a new search - just in case we missed
312 if (start_addr != TASK_UNMAPPED_BASE) {
313 start_addr = TASK_UNMAPPED_BASE;
318 if (!vma || addr + len <= vma->vm_start) {
319 mm->free_area_cache = addr + len;
322 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
326 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
327 unsigned long addr0, unsigned long len,
328 unsigned long pgoff, unsigned long flags)
330 struct mm_struct *mm = current->mm;
331 struct vm_area_struct *vma, *prev_vma;
332 unsigned long base = mm->mmap_base, addr = addr0;
335 /* don't allow allocations above current base */
336 if (mm->free_area_cache > base)
337 mm->free_area_cache = base;
340 /* make sure it can fit in the remaining address space */
341 if (mm->free_area_cache < len)
344 /* either no address requested or cant fit in requested address hole */
345 addr = (mm->free_area_cache - len) & HPAGE_MASK;
348 * Lookup failure means no vma is above this address,
349 * i.e. return with success:
351 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
355 * new region fits between prev_vma->vm_end and
356 * vma->vm_start, use it:
358 if (addr + len <= vma->vm_start &&
359 (!prev_vma || (addr >= prev_vma->vm_end)))
360 /* remember the address as a hint for next time */
361 return (mm->free_area_cache = addr);
363 /* pull free_area_cache down to the first hole */
364 if (mm->free_area_cache == vma->vm_end)
365 mm->free_area_cache = vma->vm_start;
367 /* try just below the current vma->vm_start */
368 addr = (vma->vm_start - len) & HPAGE_MASK;
369 } while (len <= vma->vm_start);
373 * if hint left us with no space for the requested
374 * mapping then try again:
377 mm->free_area_cache = base;
382 * A failed mmap() very likely causes application failure,
383 * so fall back to the bottom-up function here. This scenario
384 * can happen with large stack limits and large mmap()
387 mm->free_area_cache = TASK_UNMAPPED_BASE;
388 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
392 * Restore the topdown base:
394 mm->free_area_cache = base;
400 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
401 unsigned long len, unsigned long pgoff, unsigned long flags)
403 struct mm_struct *mm = current->mm;
404 struct vm_area_struct *vma;
406 if (len & ~HPAGE_MASK)
412 addr = ALIGN(addr, HPAGE_SIZE);
413 vma = find_vma(mm, addr);
414 if (TASK_SIZE - len >= addr &&
415 (!vma || addr + len <= vma->vm_start))
418 if (mm->get_unmapped_area == arch_get_unmapped_area)
419 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
422 return hugetlb_get_unmapped_area_topdown(file, addr, len,
426 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/