blob: d524127c9afc3b2f92b3e5ad6b3dbce20bec42f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
David Gibson63551ae2005-06-21 17:14:44 -070021pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 pgd_t *pgd;
24 pud_t *pud;
Adam Litke7bf07f32005-09-03 15:55:00 -070025 pte_t *pte = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 pgd = pgd_offset(mm, addr);
28 pud = pud_alloc(mm, pgd, addr);
Chen, Kenneth W0e5c9f32005-09-03 15:55:02 -070029 if (pud)
30 pte = (pte_t *) pmd_alloc(mm, pud, addr);
31 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
Adam Litke7bf07f32005-09-03 15:55:00 -070032
Adam Litke7bf07f32005-09-03 15:55:00 -070033 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
David Gibson63551ae2005-06-21 17:14:44 -070036pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 pgd_t *pgd;
39 pud_t *pud;
40 pmd_t *pmd = NULL;
41
42 pgd = pgd_offset(mm, addr);
Adam Litke02b0cce2005-09-03 15:55:01 -070043 if (pgd_present(*pgd)) {
44 pud = pud_offset(pgd, addr);
45 if (pud_present(*pud))
46 pmd = pmd_offset(pud, addr);
47 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return (pte_t *) pmd;
49}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * This function checks for proper alignment of input addr and len parameters.
53 */
54int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
55{
56 if (len & ~HPAGE_MASK)
57 return -EINVAL;
58 if (addr & ~HPAGE_MASK)
59 return -EINVAL;
60 return 0;
61}
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#if 0 /* This is just for testing */
64struct page *
65follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
66{
67 unsigned long start = address;
68 int length = 1;
69 int nr;
70 struct page *page;
71 struct vm_area_struct *vma;
72
73 vma = find_vma(mm, addr);
74 if (!vma || !is_vm_hugetlb_page(vma))
75 return ERR_PTR(-EINVAL);
76
77 pte = huge_pte_offset(mm, address);
78
79 /* hugetlb should be locked, and hence, prefaulted */
80 WARN_ON(!pte || pte_none(*pte));
81
82 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
83
84 WARN_ON(!PageCompound(page));
85
86 return page;
87}
88
89int pmd_huge(pmd_t pmd)
90{
91 return 0;
92}
93
94struct page *
95follow_huge_pmd(struct mm_struct *mm, unsigned long address,
96 pmd_t *pmd, int write)
97{
98 return NULL;
99}
100
101#else
102
103struct page *
104follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
105{
106 return ERR_PTR(-EINVAL);
107}
108
109int pmd_huge(pmd_t pmd)
110{
111 return !!(pmd_val(pmd) & _PAGE_PSE);
112}
113
114struct page *
115follow_huge_pmd(struct mm_struct *mm, unsigned long address,
116 pmd_t *pmd, int write)
117{
118 struct page *page;
119
120 page = pte_page(*(pte_t *)pmd);
121 if (page)
122 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
123 return page;
124}
125#endif
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* x86_64 also uses this file */
128
129#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
130static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
131 unsigned long addr, unsigned long len,
132 unsigned long pgoff, unsigned long flags)
133{
134 struct mm_struct *mm = current->mm;
135 struct vm_area_struct *vma;
136 unsigned long start_addr;
137
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700138 if (len > mm->cached_hole_size) {
139 start_addr = mm->free_area_cache;
140 } else {
141 start_addr = TASK_UNMAPPED_BASE;
142 mm->cached_hole_size = 0;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145full_search:
146 addr = ALIGN(start_addr, HPAGE_SIZE);
147
148 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
149 /* At this point: (!vma || addr < vma->vm_end). */
150 if (TASK_SIZE - len < addr) {
151 /*
152 * Start a new search - just in case we missed
153 * some holes.
154 */
155 if (start_addr != TASK_UNMAPPED_BASE) {
156 start_addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700157 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto full_search;
159 }
160 return -ENOMEM;
161 }
162 if (!vma || addr + len <= vma->vm_start) {
163 mm->free_area_cache = addr + len;
164 return addr;
165 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700166 if (addr + mm->cached_hole_size < vma->vm_start)
167 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
169 }
170}
171
172static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
173 unsigned long addr0, unsigned long len,
174 unsigned long pgoff, unsigned long flags)
175{
176 struct mm_struct *mm = current->mm;
177 struct vm_area_struct *vma, *prev_vma;
178 unsigned long base = mm->mmap_base, addr = addr0;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700179 unsigned long largest_hole = mm->cached_hole_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int first_time = 1;
181
182 /* don't allow allocations above current base */
183 if (mm->free_area_cache > base)
184 mm->free_area_cache = base;
185
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700186 if (len <= largest_hole) {
187 largest_hole = 0;
188 mm->free_area_cache = base;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190try_again:
191 /* make sure it can fit in the remaining address space */
192 if (mm->free_area_cache < len)
193 goto fail;
194
195 /* either no address requested or cant fit in requested address hole */
196 addr = (mm->free_area_cache - len) & HPAGE_MASK;
197 do {
198 /*
199 * Lookup failure means no vma is above this address,
200 * i.e. return with success:
201 */
202 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
203 return addr;
204
205 /*
206 * new region fits between prev_vma->vm_end and
207 * vma->vm_start, use it:
208 */
209 if (addr + len <= vma->vm_start &&
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700210 (!prev_vma || (addr >= prev_vma->vm_end))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* remember the address as a hint for next time */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700212 mm->cached_hole_size = largest_hole;
213 return (mm->free_area_cache = addr);
214 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* pull free_area_cache down to the first hole */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700216 if (mm->free_area_cache == vma->vm_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mm->free_area_cache = vma->vm_start;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700218 mm->cached_hole_size = largest_hole;
219 }
220 }
221
222 /* remember the largest hole we saw so far */
223 if (addr + largest_hole < vma->vm_start)
224 largest_hole = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* try just below the current vma->vm_start */
227 addr = (vma->vm_start - len) & HPAGE_MASK;
228 } while (len <= vma->vm_start);
229
230fail:
231 /*
232 * if hint left us with no space for the requested
233 * mapping then try again:
234 */
235 if (first_time) {
236 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700237 largest_hole = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 first_time = 0;
239 goto try_again;
240 }
241 /*
242 * A failed mmap() very likely causes application failure,
243 * so fall back to the bottom-up function here. This scenario
244 * can happen with large stack limits and large mmap()
245 * allocations.
246 */
247 mm->free_area_cache = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700248 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
250 len, pgoff, flags);
251
252 /*
253 * Restore the topdown base:
254 */
255 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700256 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return addr;
259}
260
261unsigned long
262hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
263 unsigned long len, unsigned long pgoff, unsigned long flags)
264{
265 struct mm_struct *mm = current->mm;
266 struct vm_area_struct *vma;
267
268 if (len & ~HPAGE_MASK)
269 return -EINVAL;
270 if (len > TASK_SIZE)
271 return -ENOMEM;
272
273 if (addr) {
274 addr = ALIGN(addr, HPAGE_SIZE);
275 vma = find_vma(mm, addr);
276 if (TASK_SIZE - len >= addr &&
277 (!vma || addr + len <= vma->vm_start))
278 return addr;
279 }
280 if (mm->get_unmapped_area == arch_get_unmapped_area)
281 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
282 pgoff, flags);
283 else
284 return hugetlb_get_unmapped_area_topdown(file, addr, len,
285 pgoff, flags);
286}
287
288#endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
289