2 * IA-32 Huge TLB Page Support for Kernel.
4 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
7 #include <linux/init.h>
10 #include <linux/hugetlb.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/sysctl.h>
17 #include <asm/tlbflush.h>
19 static unsigned long page_table_shareable(struct vm_area_struct *svma,
20 struct vm_area_struct *vma,
21 unsigned long addr, pgoff_t idx)
23 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
25 unsigned long sbase = saddr & PUD_MASK;
26 unsigned long s_end = sbase + PUD_SIZE;
29 * match the virtual addresses, permission and the alignment of the
32 if (pmd_index(addr) != pmd_index(saddr) ||
33 vma->vm_flags != svma->vm_flags ||
34 sbase < svma->vm_start || svma->vm_end < s_end)
40 static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
42 unsigned long base = addr & PUD_MASK;
43 unsigned long end = base + PUD_SIZE;
46 * check on proper vm_flags and page table alignment
48 if (vma->vm_flags & VM_MAYSHARE &&
49 vma->vm_start <= base && end <= vma->vm_end)
55 * search for a shareable pmd page for hugetlb.
57 static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
59 struct vm_area_struct *vma = find_vma(mm, addr);
60 struct address_space *mapping = vma->vm_file->f_mapping;
61 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
63 struct prio_tree_iter iter;
64 struct vm_area_struct *svma;
68 if (!vma_shareable(vma, addr))
71 spin_lock(&mapping->i_mmap_lock);
72 vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) {
76 saddr = page_table_shareable(svma, vma, addr, idx);
78 spte = huge_pte_offset(svma->vm_mm, saddr);
80 get_page(virt_to_page(spte));
89 spin_lock(&mm->page_table_lock);
91 pud_populate(mm, pud, (unsigned long) spte & PAGE_MASK);
93 put_page(virt_to_page(spte));
94 spin_unlock(&mm->page_table_lock);
96 spin_unlock(&mapping->i_mmap_lock);
100 * unmap huge page backed by shared pte.
102 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
103 * indicated by page_count > 1, unmap is achieved by clearing pud and
104 * decrementing the ref count. If count == 1, the pte page is not shared.
106 * called with vma->vm_mm->page_table_lock held.
108 * returns: 1 successfully unmapped a shared pte page
109 * 0 the underlying pte page is not shared, or it is the last user
111 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
113 pgd_t *pgd = pgd_offset(mm, *addr);
114 pud_t *pud = pud_offset(pgd, *addr);
116 BUG_ON(page_count(virt_to_page(ptep)) == 0);
117 if (page_count(virt_to_page(ptep)) == 1)
121 put_page(virt_to_page(ptep));
122 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
126 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
132 pgd = pgd_offset(mm, addr);
133 pud = pud_alloc(mm, pgd, addr);
136 huge_pmd_share(mm, addr, pud);
137 pte = (pte_t *) pmd_alloc(mm, pud, addr);
139 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
144 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
150 pgd = pgd_offset(mm, addr);
151 if (pgd_present(*pgd)) {
152 pud = pud_offset(pgd, addr);
153 if (pud_present(*pud))
154 pmd = pmd_offset(pud, addr);
156 return (pte_t *) pmd;
159 #if 0 /* This is just for testing */
161 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
163 unsigned long start = address;
167 struct vm_area_struct *vma;
169 vma = find_vma(mm, addr);
170 if (!vma || !is_vm_hugetlb_page(vma))
171 return ERR_PTR(-EINVAL);
173 pte = huge_pte_offset(mm, address);
175 /* hugetlb should be locked, and hence, prefaulted */
176 WARN_ON(!pte || pte_none(*pte));
178 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
180 WARN_ON(!PageCompound(page));
185 int pmd_huge(pmd_t pmd)
191 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
192 pmd_t *pmd, int write)
200 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
202 return ERR_PTR(-EINVAL);
205 int pmd_huge(pmd_t pmd)
207 return !!(pmd_val(pmd) & _PAGE_PSE);
211 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
212 pmd_t *pmd, int write)
216 page = pte_page(*(pte_t *)pmd);
218 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
223 /* x86_64 also uses this file */
225 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
226 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
227 unsigned long addr, unsigned long len,
228 unsigned long pgoff, unsigned long flags)
230 struct mm_struct *mm = current->mm;
231 struct vm_area_struct *vma;
232 unsigned long start_addr;
234 if (len > mm->cached_hole_size) {
235 start_addr = mm->free_area_cache;
237 start_addr = TASK_UNMAPPED_BASE;
238 mm->cached_hole_size = 0;
242 addr = ALIGN(start_addr, HPAGE_SIZE);
244 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
245 /* At this point: (!vma || addr < vma->vm_end). */
246 if (TASK_SIZE - len < addr) {
248 * Start a new search - just in case we missed
251 if (start_addr != TASK_UNMAPPED_BASE) {
252 start_addr = TASK_UNMAPPED_BASE;
253 mm->cached_hole_size = 0;
258 if (!vma || addr + len <= vma->vm_start) {
259 mm->free_area_cache = addr + len;
262 if (addr + mm->cached_hole_size < vma->vm_start)
263 mm->cached_hole_size = vma->vm_start - addr;
264 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
268 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
269 unsigned long addr0, unsigned long len,
270 unsigned long pgoff, unsigned long flags)
272 struct mm_struct *mm = current->mm;
273 struct vm_area_struct *vma, *prev_vma;
274 unsigned long base = mm->mmap_base, addr = addr0;
275 unsigned long largest_hole = mm->cached_hole_size;
278 /* don't allow allocations above current base */
279 if (mm->free_area_cache > base)
280 mm->free_area_cache = base;
282 if (len <= largest_hole) {
284 mm->free_area_cache = base;
287 /* make sure it can fit in the remaining address space */
288 if (mm->free_area_cache < len)
291 /* either no address requested or cant fit in requested address hole */
292 addr = (mm->free_area_cache - len) & HPAGE_MASK;
295 * Lookup failure means no vma is above this address,
296 * i.e. return with success:
298 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
302 * new region fits between prev_vma->vm_end and
303 * vma->vm_start, use it:
305 if (addr + len <= vma->vm_start &&
306 (!prev_vma || (addr >= prev_vma->vm_end))) {
307 /* remember the address as a hint for next time */
308 mm->cached_hole_size = largest_hole;
309 return (mm->free_area_cache = addr);
311 /* pull free_area_cache down to the first hole */
312 if (mm->free_area_cache == vma->vm_end) {
313 mm->free_area_cache = vma->vm_start;
314 mm->cached_hole_size = largest_hole;
318 /* remember the largest hole we saw so far */
319 if (addr + largest_hole < vma->vm_start)
320 largest_hole = vma->vm_start - addr;
322 /* try just below the current vma->vm_start */
323 addr = (vma->vm_start - len) & HPAGE_MASK;
324 } while (len <= vma->vm_start);
328 * if hint left us with no space for the requested
329 * mapping then try again:
332 mm->free_area_cache = base;
338 * A failed mmap() very likely causes application failure,
339 * so fall back to the bottom-up function here. This scenario
340 * can happen with large stack limits and large mmap()
343 mm->free_area_cache = TASK_UNMAPPED_BASE;
344 mm->cached_hole_size = ~0UL;
345 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
349 * Restore the topdown base:
351 mm->free_area_cache = base;
352 mm->cached_hole_size = ~0UL;
358 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
359 unsigned long len, unsigned long pgoff, unsigned long flags)
361 struct mm_struct *mm = current->mm;
362 struct vm_area_struct *vma;
364 if (len & ~HPAGE_MASK)
369 if (flags & MAP_FIXED) {
370 if (prepare_hugepage_range(addr, len, pgoff))
376 addr = ALIGN(addr, HPAGE_SIZE);
377 vma = find_vma(mm, addr);
378 if (TASK_SIZE - len >= addr &&
379 (!vma || addr + len <= vma->vm_start))
382 if (mm->get_unmapped_area == arch_get_unmapped_area)
383 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
386 return hugetlb_get_unmapped_area_topdown(file, addr, len,
390 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/