[PATCH] hugepage: is_aligned_hugepage_range() cleanup
[linux-2.6.git] / arch / i386 / mm / hugetlbpage.c
1 /*
2  * IA-32 Huge TLB Page Support for Kernel.
3  *
4  * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
5  */
6
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>
20
21 pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
22 {
23         pgd_t *pgd;
24         pud_t *pud;
25         pte_t *pte = NULL;
26
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));
32
33         return pte;
34 }
35
36 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
37 {
38         pgd_t *pgd;
39         pud_t *pud;
40         pmd_t *pmd = NULL;
41
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);
47         }
48         return (pte_t *) pmd;
49 }
50
51 #if 0   /* This is just for testing */
52 struct page *
53 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
54 {
55         unsigned long start = address;
56         int length = 1;
57         int nr;
58         struct page *page;
59         struct vm_area_struct *vma;
60
61         vma = find_vma(mm, addr);
62         if (!vma || !is_vm_hugetlb_page(vma))
63                 return ERR_PTR(-EINVAL);
64
65         pte = huge_pte_offset(mm, address);
66
67         /* hugetlb should be locked, and hence, prefaulted */
68         WARN_ON(!pte || pte_none(*pte));
69
70         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
71
72         WARN_ON(!PageCompound(page));
73
74         return page;
75 }
76
77 int pmd_huge(pmd_t pmd)
78 {
79         return 0;
80 }
81
82 struct page *
83 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
84                 pmd_t *pmd, int write)
85 {
86         return NULL;
87 }
88
89 #else
90
91 struct page *
92 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
93 {
94         return ERR_PTR(-EINVAL);
95 }
96
97 int pmd_huge(pmd_t pmd)
98 {
99         return !!(pmd_val(pmd) & _PAGE_PSE);
100 }
101
102 struct page *
103 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
104                 pmd_t *pmd, int write)
105 {
106         struct page *page;
107
108         page = pte_page(*(pte_t *)pmd);
109         if (page)
110                 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
111         return page;
112 }
113 #endif
114
115 /* x86_64 also uses this file */
116
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)
121 {
122         struct mm_struct *mm = current->mm;
123         struct vm_area_struct *vma;
124         unsigned long start_addr;
125
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;
131         }
132
133 full_search:
134         addr = ALIGN(start_addr, HPAGE_SIZE);
135
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) {
139                         /*
140                          * Start a new search - just in case we missed
141                          * some holes.
142                          */
143                         if (start_addr != TASK_UNMAPPED_BASE) {
144                                 start_addr = TASK_UNMAPPED_BASE;
145                                 mm->cached_hole_size = 0;
146                                 goto full_search;
147                         }
148                         return -ENOMEM;
149                 }
150                 if (!vma || addr + len <= vma->vm_start) {
151                         mm->free_area_cache = addr + len;
152                         return addr;
153                 }
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);
157         }
158 }
159
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)
163 {
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;
169
170         /* don't allow allocations above current base */
171         if (mm->free_area_cache > base)
172                 mm->free_area_cache = base;
173
174         if (len <= largest_hole) {
175                 largest_hole = 0;
176                 mm->free_area_cache  = base;
177         }
178 try_again:
179         /* make sure it can fit in the remaining address space */
180         if (mm->free_area_cache < len)
181                 goto fail;
182
183         /* either no address requested or cant fit in requested address hole */
184         addr = (mm->free_area_cache - len) & HPAGE_MASK;
185         do {
186                 /*
187                  * Lookup failure means no vma is above this address,
188                  * i.e. return with success:
189                  */
190                 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
191                         return addr;
192
193                 /*
194                  * new region fits between prev_vma->vm_end and
195                  * vma->vm_start, use it:
196                  */
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;
207                         }
208                 }
209
210                 /* remember the largest hole we saw so far */
211                 if (addr + largest_hole < vma->vm_start)
212                         largest_hole = vma->vm_start - addr;
213
214                 /* try just below the current vma->vm_start */
215                 addr = (vma->vm_start - len) & HPAGE_MASK;
216         } while (len <= vma->vm_start);
217
218 fail:
219         /*
220          * if hint left us with no space for the requested
221          * mapping then try again:
222          */
223         if (first_time) {
224                 mm->free_area_cache = base;
225                 largest_hole = 0;
226                 first_time = 0;
227                 goto try_again;
228         }
229         /*
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.
234          */
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);
239
240         /*
241          * Restore the topdown base:
242          */
243         mm->free_area_cache = base;
244         mm->cached_hole_size = ~0UL;
245
246         return addr;
247 }
248
249 unsigned long
250 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
251                 unsigned long len, unsigned long pgoff, unsigned long flags)
252 {
253         struct mm_struct *mm = current->mm;
254         struct vm_area_struct *vma;
255
256         if (len & ~HPAGE_MASK)
257                 return -EINVAL;
258         if (len > TASK_SIZE)
259                 return -ENOMEM;
260
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;
267         }
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);
274 }
275
276 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
277