[PATCH] hugepage: is_aligned_hugepage_range() cleanup
[linux-2.6/x86.git] / arch / i386 / mm / hugetlbpage.c
bloba7d8915854116264048f62c84d63dc495fc212b7
1 /*
2 * IA-32 Huge TLB Page Support for Kernel.
4 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
5 */
7 #include <linux/config.h>
8 #include <linux/init.h>
9 #include <linux/fs.h>
10 #include <linux/mm.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>
17 #include <asm/mman.h>
18 #include <asm/tlb.h>
19 #include <asm/tlbflush.h>
21 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
23 pgd_t *pgd;
24 pud_t *pud;
25 pte_t *pte = NULL;
27 pgd = pgd_offset(mm, addr);
28 pud = pud_alloc(mm, pgd, addr);
29 if (pud)
30 pte = (pte_t *) pmd_alloc(mm, pud, addr);
31 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
33 return pte;
36 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
38 pgd_t *pgd;
39 pud_t *pud;
40 pmd_t *pmd = NULL;
42 pgd = pgd_offset(mm, addr);
43 if (pgd_present(*pgd)) {
44 pud = pud_offset(pgd, addr);
45 if (pud_present(*pud))
46 pmd = pmd_offset(pud, addr);
48 return (pte_t *) pmd;
51 #if 0 /* This is just for testing */
52 struct page *
53 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
55 unsigned long start = address;
56 int length = 1;
57 int nr;
58 struct page *page;
59 struct vm_area_struct *vma;
61 vma = find_vma(mm, addr);
62 if (!vma || !is_vm_hugetlb_page(vma))
63 return ERR_PTR(-EINVAL);
65 pte = huge_pte_offset(mm, address);
67 /* hugetlb should be locked, and hence, prefaulted */
68 WARN_ON(!pte || pte_none(*pte));
70 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
72 WARN_ON(!PageCompound(page));
74 return page;
77 int pmd_huge(pmd_t pmd)
79 return 0;
82 struct page *
83 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
84 pmd_t *pmd, int write)
86 return NULL;
89 #else
91 struct page *
92 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
94 return ERR_PTR(-EINVAL);
97 int pmd_huge(pmd_t pmd)
99 return !!(pmd_val(pmd) & _PAGE_PSE);
102 struct page *
103 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
104 pmd_t *pmd, int write)
106 struct page *page;
108 page = pte_page(*(pte_t *)pmd);
109 if (page)
110 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
111 return page;
113 #endif
115 /* x86_64 also uses this file */
117 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
118 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
119 unsigned long addr, unsigned long len,
120 unsigned long pgoff, unsigned long flags)
122 struct mm_struct *mm = current->mm;
123 struct vm_area_struct *vma;
124 unsigned long start_addr;
126 if (len > mm->cached_hole_size) {
127 start_addr = mm->free_area_cache;
128 } else {
129 start_addr = TASK_UNMAPPED_BASE;
130 mm->cached_hole_size = 0;
133 full_search:
134 addr = ALIGN(start_addr, HPAGE_SIZE);
136 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
137 /* At this point: (!vma || addr < vma->vm_end). */
138 if (TASK_SIZE - len < addr) {
140 * Start a new search - just in case we missed
141 * some holes.
143 if (start_addr != TASK_UNMAPPED_BASE) {
144 start_addr = TASK_UNMAPPED_BASE;
145 mm->cached_hole_size = 0;
146 goto full_search;
148 return -ENOMEM;
150 if (!vma || addr + len <= vma->vm_start) {
151 mm->free_area_cache = addr + len;
152 return addr;
154 if (addr + mm->cached_hole_size < vma->vm_start)
155 mm->cached_hole_size = vma->vm_start - addr;
156 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
160 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
161 unsigned long addr0, unsigned long len,
162 unsigned long pgoff, unsigned long flags)
164 struct mm_struct *mm = current->mm;
165 struct vm_area_struct *vma, *prev_vma;
166 unsigned long base = mm->mmap_base, addr = addr0;
167 unsigned long largest_hole = mm->cached_hole_size;
168 int first_time = 1;
170 /* don't allow allocations above current base */
171 if (mm->free_area_cache > base)
172 mm->free_area_cache = base;
174 if (len <= largest_hole) {
175 largest_hole = 0;
176 mm->free_area_cache = base;
178 try_again:
179 /* make sure it can fit in the remaining address space */
180 if (mm->free_area_cache < len)
181 goto fail;
183 /* either no address requested or cant fit in requested address hole */
184 addr = (mm->free_area_cache - len) & HPAGE_MASK;
185 do {
187 * Lookup failure means no vma is above this address,
188 * i.e. return with success:
190 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
191 return addr;
194 * new region fits between prev_vma->vm_end and
195 * vma->vm_start, use it:
197 if (addr + len <= vma->vm_start &&
198 (!prev_vma || (addr >= prev_vma->vm_end))) {
199 /* remember the address as a hint for next time */
200 mm->cached_hole_size = largest_hole;
201 return (mm->free_area_cache = addr);
202 } else {
203 /* pull free_area_cache down to the first hole */
204 if (mm->free_area_cache == vma->vm_end) {
205 mm->free_area_cache = vma->vm_start;
206 mm->cached_hole_size = largest_hole;
210 /* remember the largest hole we saw so far */
211 if (addr + largest_hole < vma->vm_start)
212 largest_hole = vma->vm_start - addr;
214 /* try just below the current vma->vm_start */
215 addr = (vma->vm_start - len) & HPAGE_MASK;
216 } while (len <= vma->vm_start);
218 fail:
220 * if hint left us with no space for the requested
221 * mapping then try again:
223 if (first_time) {
224 mm->free_area_cache = base;
225 largest_hole = 0;
226 first_time = 0;
227 goto try_again;
230 * A failed mmap() very likely causes application failure,
231 * so fall back to the bottom-up function here. This scenario
232 * can happen with large stack limits and large mmap()
233 * allocations.
235 mm->free_area_cache = TASK_UNMAPPED_BASE;
236 mm->cached_hole_size = ~0UL;
237 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
238 len, pgoff, flags);
241 * Restore the topdown base:
243 mm->free_area_cache = base;
244 mm->cached_hole_size = ~0UL;
246 return addr;
249 unsigned long
250 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
251 unsigned long len, unsigned long pgoff, unsigned long flags)
253 struct mm_struct *mm = current->mm;
254 struct vm_area_struct *vma;
256 if (len & ~HPAGE_MASK)
257 return -EINVAL;
258 if (len > TASK_SIZE)
259 return -ENOMEM;
261 if (addr) {
262 addr = ALIGN(addr, HPAGE_SIZE);
263 vma = find_vma(mm, addr);
264 if (TASK_SIZE - len >= addr &&
265 (!vma || addr + len <= vma->vm_start))
266 return addr;
268 if (mm->get_unmapped_area == arch_get_unmapped_area)
269 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
270 pgoff, flags);
271 else
272 return hugetlb_get_unmapped_area_topdown(file, addr, len,
273 pgoff, flags);
276 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/