OMAP: Add new function to check wether there is irq pending
[linux-ginger.git] / arch / x86 / mm / hugetlbpage.c
blob8f307d914c2ec860c3b9093796362e9c4719ab41
1 /*
2 * IA-32 Huge TLB Page Support for Kernel.
4 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
5 */
7 #include <linux/init.h>
8 #include <linux/fs.h>
9 #include <linux/mm.h>
10 #include <linux/hugetlb.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/sysctl.h>
15 #include <asm/mman.h>
16 #include <asm/tlb.h>
17 #include <asm/tlbflush.h>
18 #include <asm/pgalloc.h>
20 static unsigned long page_table_shareable(struct vm_area_struct *svma,
21 struct vm_area_struct *vma,
22 unsigned long addr, pgoff_t idx)
24 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
25 svma->vm_start;
26 unsigned long sbase = saddr & PUD_MASK;
27 unsigned long s_end = sbase + PUD_SIZE;
30 * match the virtual addresses, permission and the alignment of the
31 * page table page.
33 if (pmd_index(addr) != pmd_index(saddr) ||
34 vma->vm_flags != svma->vm_flags ||
35 sbase < svma->vm_start || svma->vm_end < s_end)
36 return 0;
38 return saddr;
41 static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
43 unsigned long base = addr & PUD_MASK;
44 unsigned long end = base + PUD_SIZE;
47 * check on proper vm_flags and page table alignment
49 if (vma->vm_flags & VM_MAYSHARE &&
50 vma->vm_start <= base && end <= vma->vm_end)
51 return 1;
52 return 0;
56 * search for a shareable pmd page for hugetlb.
58 static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
60 struct vm_area_struct *vma = find_vma(mm, addr);
61 struct address_space *mapping = vma->vm_file->f_mapping;
62 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
63 vma->vm_pgoff;
64 struct prio_tree_iter iter;
65 struct vm_area_struct *svma;
66 unsigned long saddr;
67 pte_t *spte = NULL;
69 if (!vma_shareable(vma, addr))
70 return;
72 spin_lock(&mapping->i_mmap_lock);
73 vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) {
74 if (svma == vma)
75 continue;
77 saddr = page_table_shareable(svma, vma, addr, idx);
78 if (saddr) {
79 spte = huge_pte_offset(svma->vm_mm, saddr);
80 if (spte) {
81 get_page(virt_to_page(spte));
82 break;
87 if (!spte)
88 goto out;
90 spin_lock(&mm->page_table_lock);
91 if (pud_none(*pud))
92 pud_populate(mm, pud, (pmd_t *)((unsigned long)spte & PAGE_MASK));
93 else
94 put_page(virt_to_page(spte));
95 spin_unlock(&mm->page_table_lock);
96 out:
97 spin_unlock(&mapping->i_mmap_lock);
101 * unmap huge page backed by shared pte.
103 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
104 * indicated by page_count > 1, unmap is achieved by clearing pud and
105 * decrementing the ref count. If count == 1, the pte page is not shared.
107 * called with vma->vm_mm->page_table_lock held.
109 * returns: 1 successfully unmapped a shared pte page
110 * 0 the underlying pte page is not shared, or it is the last user
112 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
114 pgd_t *pgd = pgd_offset(mm, *addr);
115 pud_t *pud = pud_offset(pgd, *addr);
117 BUG_ON(page_count(virt_to_page(ptep)) == 0);
118 if (page_count(virt_to_page(ptep)) == 1)
119 return 0;
121 pud_clear(pud);
122 put_page(virt_to_page(ptep));
123 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
124 return 1;
127 pte_t *huge_pte_alloc(struct mm_struct *mm,
128 unsigned long addr, unsigned long sz)
130 pgd_t *pgd;
131 pud_t *pud;
132 pte_t *pte = NULL;
134 pgd = pgd_offset(mm, addr);
135 pud = pud_alloc(mm, pgd, addr);
136 if (pud) {
137 if (sz == PUD_SIZE) {
138 pte = (pte_t *)pud;
139 } else {
140 BUG_ON(sz != PMD_SIZE);
141 if (pud_none(*pud))
142 huge_pmd_share(mm, addr, pud);
143 pte = (pte_t *) pmd_alloc(mm, pud, addr);
146 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
148 return pte;
151 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
153 pgd_t *pgd;
154 pud_t *pud;
155 pmd_t *pmd = NULL;
157 pgd = pgd_offset(mm, addr);
158 if (pgd_present(*pgd)) {
159 pud = pud_offset(pgd, addr);
160 if (pud_present(*pud)) {
161 if (pud_large(*pud))
162 return (pte_t *)pud;
163 pmd = pmd_offset(pud, addr);
166 return (pte_t *) pmd;
169 #if 0 /* This is just for testing */
170 struct page *
171 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
173 unsigned long start = address;
174 int length = 1;
175 int nr;
176 struct page *page;
177 struct vm_area_struct *vma;
179 vma = find_vma(mm, addr);
180 if (!vma || !is_vm_hugetlb_page(vma))
181 return ERR_PTR(-EINVAL);
183 pte = huge_pte_offset(mm, address);
185 /* hugetlb should be locked, and hence, prefaulted */
186 WARN_ON(!pte || pte_none(*pte));
188 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
190 WARN_ON(!PageHead(page));
192 return page;
195 int pmd_huge(pmd_t pmd)
197 return 0;
200 int pud_huge(pud_t pud)
202 return 0;
205 struct page *
206 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
207 pmd_t *pmd, int write)
209 return NULL;
212 #else
214 struct page *
215 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
217 return ERR_PTR(-EINVAL);
220 int pmd_huge(pmd_t pmd)
222 return !!(pmd_val(pmd) & _PAGE_PSE);
225 int pud_huge(pud_t pud)
227 return !!(pud_val(pud) & _PAGE_PSE);
230 struct page *
231 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
232 pmd_t *pmd, int write)
234 struct page *page;
236 page = pte_page(*(pte_t *)pmd);
237 if (page)
238 page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
239 return page;
242 struct page *
243 follow_huge_pud(struct mm_struct *mm, unsigned long address,
244 pud_t *pud, int write)
246 struct page *page;
248 page = pte_page(*(pte_t *)pud);
249 if (page)
250 page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
251 return page;
254 #endif
256 /* x86_64 also uses this file */
258 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
259 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
260 unsigned long addr, unsigned long len,
261 unsigned long pgoff, unsigned long flags)
263 struct hstate *h = hstate_file(file);
264 struct mm_struct *mm = current->mm;
265 struct vm_area_struct *vma;
266 unsigned long start_addr;
268 if (len > mm->cached_hole_size) {
269 start_addr = mm->free_area_cache;
270 } else {
271 start_addr = TASK_UNMAPPED_BASE;
272 mm->cached_hole_size = 0;
275 full_search:
276 addr = ALIGN(start_addr, huge_page_size(h));
278 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
279 /* At this point: (!vma || addr < vma->vm_end). */
280 if (TASK_SIZE - len < addr) {
282 * Start a new search - just in case we missed
283 * some holes.
285 if (start_addr != TASK_UNMAPPED_BASE) {
286 start_addr = TASK_UNMAPPED_BASE;
287 mm->cached_hole_size = 0;
288 goto full_search;
290 return -ENOMEM;
292 if (!vma || addr + len <= vma->vm_start) {
293 mm->free_area_cache = addr + len;
294 return addr;
296 if (addr + mm->cached_hole_size < vma->vm_start)
297 mm->cached_hole_size = vma->vm_start - addr;
298 addr = ALIGN(vma->vm_end, huge_page_size(h));
302 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
303 unsigned long addr0, unsigned long len,
304 unsigned long pgoff, unsigned long flags)
306 struct hstate *h = hstate_file(file);
307 struct mm_struct *mm = current->mm;
308 struct vm_area_struct *vma, *prev_vma;
309 unsigned long base = mm->mmap_base, addr = addr0;
310 unsigned long largest_hole = mm->cached_hole_size;
311 int first_time = 1;
313 /* don't allow allocations above current base */
314 if (mm->free_area_cache > base)
315 mm->free_area_cache = base;
317 if (len <= largest_hole) {
318 largest_hole = 0;
319 mm->free_area_cache = base;
321 try_again:
322 /* make sure it can fit in the remaining address space */
323 if (mm->free_area_cache < len)
324 goto fail;
326 /* either no address requested or cant fit in requested address hole */
327 addr = (mm->free_area_cache - len) & huge_page_mask(h);
328 do {
330 * Lookup failure means no vma is above this address,
331 * i.e. return with success:
333 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
334 return addr;
337 * new region fits between prev_vma->vm_end and
338 * vma->vm_start, use it:
340 if (addr + len <= vma->vm_start &&
341 (!prev_vma || (addr >= prev_vma->vm_end))) {
342 /* remember the address as a hint for next time */
343 mm->cached_hole_size = largest_hole;
344 return (mm->free_area_cache = addr);
345 } else {
346 /* pull free_area_cache down to the first hole */
347 if (mm->free_area_cache == vma->vm_end) {
348 mm->free_area_cache = vma->vm_start;
349 mm->cached_hole_size = largest_hole;
353 /* remember the largest hole we saw so far */
354 if (addr + largest_hole < vma->vm_start)
355 largest_hole = vma->vm_start - addr;
357 /* try just below the current vma->vm_start */
358 addr = (vma->vm_start - len) & huge_page_mask(h);
359 } while (len <= vma->vm_start);
361 fail:
363 * if hint left us with no space for the requested
364 * mapping then try again:
366 if (first_time) {
367 mm->free_area_cache = base;
368 largest_hole = 0;
369 first_time = 0;
370 goto try_again;
373 * A failed mmap() very likely causes application failure,
374 * so fall back to the bottom-up function here. This scenario
375 * can happen with large stack limits and large mmap()
376 * allocations.
378 mm->free_area_cache = TASK_UNMAPPED_BASE;
379 mm->cached_hole_size = ~0UL;
380 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
381 len, pgoff, flags);
384 * Restore the topdown base:
386 mm->free_area_cache = base;
387 mm->cached_hole_size = ~0UL;
389 return addr;
392 unsigned long
393 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
394 unsigned long len, unsigned long pgoff, unsigned long flags)
396 struct hstate *h = hstate_file(file);
397 struct mm_struct *mm = current->mm;
398 struct vm_area_struct *vma;
400 if (len & ~huge_page_mask(h))
401 return -EINVAL;
402 if (len > TASK_SIZE)
403 return -ENOMEM;
405 if (flags & MAP_FIXED) {
406 if (prepare_hugepage_range(file, addr, len))
407 return -EINVAL;
408 return addr;
411 if (addr) {
412 addr = ALIGN(addr, huge_page_size(h));
413 vma = find_vma(mm, addr);
414 if (TASK_SIZE - len >= addr &&
415 (!vma || addr + len <= vma->vm_start))
416 return addr;
418 if (mm->get_unmapped_area == arch_get_unmapped_area)
419 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
420 pgoff, flags);
421 else
422 return hugetlb_get_unmapped_area_topdown(file, addr, len,
423 pgoff, flags);
426 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
428 #ifdef CONFIG_X86_64
429 static __init int setup_hugepagesz(char *opt)
431 unsigned long ps = memparse(opt, &opt);
432 if (ps == PMD_SIZE) {
433 hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
434 } else if (ps == PUD_SIZE && cpu_has_gbpages) {
435 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
436 } else {
437 printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
438 ps >> 20);
439 return 0;
441 return 1;
443 __setup("hugepagesz=", setup_hugepagesz);
444 #endif