blob: a7d8915854116264048f62c84d63dc495fc212b7 [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#if 0 /* This is just for testing */
52struct page *
53follow_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
77int pmd_huge(pmd_t pmd)
78{
79 return 0;
80}
81
82struct page *
83follow_huge_pmd(struct mm_struct *mm, unsigned long address,
84 pmd_t *pmd, int write)
85{
86 return NULL;
87}
88
89#else
90
91struct page *
92follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
93{
94 return ERR_PTR(-EINVAL);
95}
96
97int pmd_huge(pmd_t pmd)
98{
99 return !!(pmd_val(pmd) & _PAGE_PSE);
100}
101
102struct page *
103follow_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* x86_64 also uses this file */
116
117#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
118static 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
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700126 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133full_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;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700145 mm->cached_hole_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 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 }
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700154 if (addr + mm->cached_hole_size < vma->vm_start)
155 mm->cached_hole_size = vma->vm_start - addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
157 }
158}
159
160static 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;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700167 unsigned long largest_hole = mm->cached_hole_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 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
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700174 if (len <= largest_hole) {
175 largest_hole = 0;
176 mm->free_area_cache = base;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178try_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 &&
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700198 (!prev_vma || (addr >= prev_vma->vm_end))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* remember the address as a hint for next time */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700200 mm->cached_hole_size = largest_hole;
201 return (mm->free_area_cache = addr);
202 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* pull free_area_cache down to the first hole */
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700204 if (mm->free_area_cache == vma->vm_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mm->free_area_cache = vma->vm_start;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700206 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* try just below the current vma->vm_start */
215 addr = (vma->vm_start - len) & HPAGE_MASK;
216 } while (len <= vma->vm_start);
217
218fail:
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;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700225 largest_hole = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 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;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700236 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 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;
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700244 mm->cached_hole_size = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return addr;
247}
248
249unsigned long
250hugetlb_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