blob: 57c486f0e896c71056fcd139a6b3e1faec925ba0 [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 pmd_t *pmd;
26 pte_t *pte = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 pgd = pgd_offset(mm, addr);
29 pud = pud_alloc(mm, pgd, addr);
30 pmd = pmd_alloc(mm, pud, addr);
Adam Litke7bf07f32005-09-03 15:55:00 -070031
32 if (!pmd)
33 goto out;
34
35 pte = (pte_t *) pmd;
36 if (!pte_none(*pte) && !pte_huge(*pte))
37 hugetlb_clean_stale_pgtable(pte);
38out:
39 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
David Gibson63551ae2005-06-21 17:14:44 -070042pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 pgd_t *pgd;
45 pud_t *pud;
46 pmd_t *pmd = NULL;
47
48 pgd = pgd_offset(mm, addr);
49 pud = pud_offset(pgd, addr);
50 pmd = pmd_offset(pud, addr);
51 return (pte_t *) pmd;
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * This function checks for proper alignment of input addr and len parameters.
56 */
57int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
58{
59 if (len & ~HPAGE_MASK)
60 return -EINVAL;
61 if (addr & ~HPAGE_MASK)
62 return -EINVAL;
63 return 0;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#if 0 /* This is just for testing */
67struct page *
68follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
69{
70 unsigned long start = address;
71 int length = 1;
72 int nr;
73 struct page *page;
74 struct vm_area_struct *vma;
75
76 vma = find_vma(mm, addr);
77 if (!vma || !is_vm_hugetlb_page(vma))
78 return ERR_PTR(-EINVAL);
79
80 pte = huge_pte_offset(mm, address);
81
82 /* hugetlb should be locked, and hence, prefaulted */
83 WARN_ON(!pte || pte_none(*pte));
84
85 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
86
87 WARN_ON(!PageCompound(page));
88
89 return page;
90}
91
92int pmd_huge(pmd_t pmd)
93{
94 return 0;
95}
96
97struct page *
98follow_huge_pmd(struct mm_struct *mm, unsigned long address,
99 pmd_t *pmd, int write)
100{
101 return NULL;
102}
103
104#else
105
106struct page *
107follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
108{
109 return ERR_PTR(-EINVAL);
110}
111
112int pmd_huge(pmd_t pmd)
113{
114 return !!(pmd_val(pmd) & _PAGE_PSE);
115}
116
117struct page *
118follow_huge_pmd(struct mm_struct *mm, unsigned long address,
119 pmd_t *pmd, int write)
120{
121 struct page *page;
122
123 page = pte_page(*(pte_t *)pmd);
124 if (page)
125 page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
126 return page;
127}
128#endif
129
David Gibson63551ae2005-06-21 17:14:44 -0700130void hugetlb_clean_stale_pgtable(pte_t *pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
David Gibson63551ae2005-06-21 17:14:44 -0700132 pmd_t *pmd = (pmd_t *) pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct page *page;
134
David Gibson63551ae2005-06-21 17:14:44 -0700135 page = pmd_page(*pmd);
136 pmd_clear(pmd);
137 dec_page_state(nr_page_table_pages);
138 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141/* x86_64 also uses this file */
142
143#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
144static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
145 unsigned long addr, unsigned long len,
146 unsigned long pgoff, unsigned long flags)
147{
148 struct mm_struct *mm = current->mm;
149 struct vm_area_struct *vma;
150 unsigned long start_addr;
151
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700152 if (len > mm->cached_hole_size) {
153 start_addr = mm->free_area_cache;
154 } else {
155 start_addr = TASK_UNMAPPED_BASE;
156 mm->cached_hole_size = 0;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159full_search:
160 addr = ALIGN(start_addr, HPAGE_SIZE);
161
162 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
163 /* At this point: (!vma || addr < vma->vm_end). */
164 if (TASK_SIZE - len < addr) {
165 /*
166 * Start a new search - just in case we missed
167 * some holes.
168 */
169 if (start_addr != TASK_UNMAPPED_BASE) {
170 start_addr = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700171 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto full_search;
173 }
174 return -ENOMEM;
175 }
176 if (!vma || addr + len <= vma->vm_start) {
177 mm->free_area_cache = addr + len;
178 return addr;
179 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700180 if (addr + mm->cached_hole_size < vma->vm_start)
181 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
183 }
184}
185
186static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
187 unsigned long addr0, unsigned long len,
188 unsigned long pgoff, unsigned long flags)
189{
190 struct mm_struct *mm = current->mm;
191 struct vm_area_struct *vma, *prev_vma;
192 unsigned long base = mm->mmap_base, addr = addr0;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700193 unsigned long largest_hole = mm->cached_hole_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int first_time = 1;
195
196 /* don't allow allocations above current base */
197 if (mm->free_area_cache > base)
198 mm->free_area_cache = base;
199
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700200 if (len <= largest_hole) {
201 largest_hole = 0;
202 mm->free_area_cache = base;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204try_again:
205 /* make sure it can fit in the remaining address space */
206 if (mm->free_area_cache < len)
207 goto fail;
208
209 /* either no address requested or cant fit in requested address hole */
210 addr = (mm->free_area_cache - len) & HPAGE_MASK;
211 do {
212 /*
213 * Lookup failure means no vma is above this address,
214 * i.e. return with success:
215 */
216 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
217 return addr;
218
219 /*
220 * new region fits between prev_vma->vm_end and
221 * vma->vm_start, use it:
222 */
223 if (addr + len <= vma->vm_start &&
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700224 (!prev_vma || (addr >= prev_vma->vm_end))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* remember the address as a hint for next time */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700226 mm->cached_hole_size = largest_hole;
227 return (mm->free_area_cache = addr);
228 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* pull free_area_cache down to the first hole */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700230 if (mm->free_area_cache == vma->vm_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 mm->free_area_cache = vma->vm_start;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700232 mm->cached_hole_size = largest_hole;
233 }
234 }
235
236 /* remember the largest hole we saw so far */
237 if (addr + largest_hole < vma->vm_start)
238 largest_hole = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* try just below the current vma->vm_start */
241 addr = (vma->vm_start - len) & HPAGE_MASK;
242 } while (len <= vma->vm_start);
243
244fail:
245 /*
246 * if hint left us with no space for the requested
247 * mapping then try again:
248 */
249 if (first_time) {
250 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700251 largest_hole = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 first_time = 0;
253 goto try_again;
254 }
255 /*
256 * A failed mmap() very likely causes application failure,
257 * so fall back to the bottom-up function here. This scenario
258 * can happen with large stack limits and large mmap()
259 * allocations.
260 */
261 mm->free_area_cache = TASK_UNMAPPED_BASE;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700262 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
264 len, pgoff, flags);
265
266 /*
267 * Restore the topdown base:
268 */
269 mm->free_area_cache = base;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700270 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return addr;
273}
274
275unsigned long
276hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
277 unsigned long len, unsigned long pgoff, unsigned long flags)
278{
279 struct mm_struct *mm = current->mm;
280 struct vm_area_struct *vma;
281
282 if (len & ~HPAGE_MASK)
283 return -EINVAL;
284 if (len > TASK_SIZE)
285 return -ENOMEM;
286
287 if (addr) {
288 addr = ALIGN(addr, HPAGE_SIZE);
289 vma = find_vma(mm, addr);
290 if (TASK_SIZE - len >= addr &&
291 (!vma || addr + len <= vma->vm_start))
292 return addr;
293 }
294 if (mm->get_unmapped_area == arch_get_unmapped_area)
295 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
296 pgoff, flags);
297 else
298 return hugetlb_get_unmapped_area_topdown(file, addr, len,
299 pgoff, flags);
300}
301
302#endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
303