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);
253 dec_page_state(nr_page_table_pages);
254 page_cache_release(page);
257 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
258 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
259 page = find_get_page(mapping, idx);
261 /* charge the fs quota first */
262 if (hugetlb_get_quota(mapping)) {
266 page = alloc_huge_page();
268 hugetlb_put_quota(mapping);
272 ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
276 hugetlb_put_quota(mapping);
277 free_huge_page(page);
281 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
284 spin_unlock(&mm->page_table_lock);
288 /* x86_64 also uses this file */
290 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
291 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
292 unsigned long addr, unsigned long len,
293 unsigned long pgoff, unsigned long flags)
295 struct mm_struct *mm = current->mm;
296 struct vm_area_struct *vma;
297 unsigned long start_addr;
299 start_addr = mm->free_area_cache;
302 addr = ALIGN(start_addr, HPAGE_SIZE);
304 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
305 /* At this point: (!vma || addr < vma->vm_end). */
306 if (TASK_SIZE - len < addr) {
308 * Start a new search - just in case we missed
311 if (start_addr != TASK_UNMAPPED_BASE) {
312 start_addr = TASK_UNMAPPED_BASE;
317 if (!vma || addr + len <= vma->vm_start) {
318 mm->free_area_cache = addr + len;
321 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
325 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
326 unsigned long addr0, unsigned long len,
327 unsigned long pgoff, unsigned long flags)
329 struct mm_struct *mm = current->mm;
330 struct vm_area_struct *vma, *prev_vma;
331 unsigned long base = mm->mmap_base, addr = addr0;
334 /* don't allow allocations above current base */
335 if (mm->free_area_cache > base)
336 mm->free_area_cache = base;
339 /* make sure it can fit in the remaining address space */
340 if (mm->free_area_cache < len)
343 /* either no address requested or cant fit in requested address hole */
344 addr = (mm->free_area_cache - len) & HPAGE_MASK;
347 * Lookup failure means no vma is above this address,
348 * i.e. return with success:
350 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
354 * new region fits between prev_vma->vm_end and
355 * vma->vm_start, use it:
357 if (addr + len <= vma->vm_start &&
358 (!prev_vma || (addr >= prev_vma->vm_end)))
359 /* remember the address as a hint for next time */
360 return (mm->free_area_cache = addr);
362 /* pull free_area_cache down to the first hole */
363 if (mm->free_area_cache == vma->vm_end)
364 mm->free_area_cache = vma->vm_start;
366 /* try just below the current vma->vm_start */
367 addr = (vma->vm_start - len) & HPAGE_MASK;
368 } while (len <= vma->vm_start);
372 * if hint left us with no space for the requested
373 * mapping then try again:
376 mm->free_area_cache = base;
381 * A failed mmap() very likely causes application failure,
382 * so fall back to the bottom-up function here. This scenario
383 * can happen with large stack limits and large mmap()
386 mm->free_area_cache = TASK_UNMAPPED_BASE;
387 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
391 * Restore the topdown base:
393 mm->free_area_cache = base;
399 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
400 unsigned long len, unsigned long pgoff, unsigned long flags)
402 struct mm_struct *mm = current->mm;
403 struct vm_area_struct *vma;
405 if (len & ~HPAGE_MASK)
411 addr = ALIGN(addr, HPAGE_SIZE);
412 vma = find_vma(mm, addr);
413 if (TASK_SIZE - len >= addr &&
414 (!vma || addr + len <= vma->vm_start))
417 if (mm->get_unmapped_area == arch_get_unmapped_area)
418 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
421 return hugetlb_get_unmapped_area_topdown(file, addr, len,
425 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/