drm/radeon/kms: memset the allocated framebuffer before using it.
[linux-2.6/mini2440.git] / arch / x86 / mm / hugetlbpage.c
blobf46c340727b8be21fb8270618ff9d03962667409
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;
29 /* Allow segments to share if only one is marked locked */
30 unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
31 unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
34 * match the virtual addresses, permission and the alignment of the
35 * page table page.
37 if (pmd_index(addr) != pmd_index(saddr) ||
38 vm_flags != svm_flags ||
39 sbase < svma->vm_start || svma->vm_end < s_end)
40 return 0;
42 return saddr;
45 static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
47 unsigned long base = addr & PUD_MASK;
48 unsigned long end = base + PUD_SIZE;
51 * check on proper vm_flags and page table alignment
53 if (vma->vm_flags & VM_MAYSHARE &&
54 vma->vm_start <= base && end <= vma->vm_end)
55 return 1;
56 return 0;
60 * search for a shareable pmd page for hugetlb.
62 static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
64 struct vm_area_struct *vma = find_vma(mm, addr);
65 struct address_space *mapping = vma->vm_file->f_mapping;
66 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
67 vma->vm_pgoff;
68 struct prio_tree_iter iter;
69 struct vm_area_struct *svma;
70 unsigned long saddr;
71 pte_t *spte = NULL;
73 if (!vma_shareable(vma, addr))
74 return;
76 spin_lock(&mapping->i_mmap_lock);
77 vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) {
78 if (svma == vma)
79 continue;
81 saddr = page_table_shareable(svma, vma, addr, idx);
82 if (saddr) {
83 spte = huge_pte_offset(svma->vm_mm, saddr);
84 if (spte) {
85 get_page(virt_to_page(spte));
86 break;
91 if (!spte)
92 goto out;
94 spin_lock(&mm->page_table_lock);
95 if (pud_none(*pud))
96 pud_populate(mm, pud, (pmd_t *)((unsigned long)spte & PAGE_MASK));
97 else
98 put_page(virt_to_page(spte));
99 spin_unlock(&mm->page_table_lock);
100 out:
101 spin_unlock(&mapping->i_mmap_lock);
105 * unmap huge page backed by shared pte.
107 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
108 * indicated by page_count > 1, unmap is achieved by clearing pud and
109 * decrementing the ref count. If count == 1, the pte page is not shared.
111 * called with vma->vm_mm->page_table_lock held.
113 * returns: 1 successfully unmapped a shared pte page
114 * 0 the underlying pte page is not shared, or it is the last user
116 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
118 pgd_t *pgd = pgd_offset(mm, *addr);
119 pud_t *pud = pud_offset(pgd, *addr);
121 BUG_ON(page_count(virt_to_page(ptep)) == 0);
122 if (page_count(virt_to_page(ptep)) == 1)
123 return 0;
125 pud_clear(pud);
126 put_page(virt_to_page(ptep));
127 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
128 return 1;
131 pte_t *huge_pte_alloc(struct mm_struct *mm,
132 unsigned long addr, unsigned long sz)
134 pgd_t *pgd;
135 pud_t *pud;
136 pte_t *pte = NULL;
138 pgd = pgd_offset(mm, addr);
139 pud = pud_alloc(mm, pgd, addr);
140 if (pud) {
141 if (sz == PUD_SIZE) {
142 pte = (pte_t *)pud;
143 } else {
144 BUG_ON(sz != PMD_SIZE);
145 if (pud_none(*pud))
146 huge_pmd_share(mm, addr, pud);
147 pte = (pte_t *) pmd_alloc(mm, pud, addr);
150 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
152 return pte;
155 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
157 pgd_t *pgd;
158 pud_t *pud;
159 pmd_t *pmd = NULL;
161 pgd = pgd_offset(mm, addr);
162 if (pgd_present(*pgd)) {
163 pud = pud_offset(pgd, addr);
164 if (pud_present(*pud)) {
165 if (pud_large(*pud))
166 return (pte_t *)pud;
167 pmd = pmd_offset(pud, addr);
170 return (pte_t *) pmd;
173 #if 0 /* This is just for testing */
174 struct page *
175 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
177 unsigned long start = address;
178 int length = 1;
179 int nr;
180 struct page *page;
181 struct vm_area_struct *vma;
183 vma = find_vma(mm, addr);
184 if (!vma || !is_vm_hugetlb_page(vma))
185 return ERR_PTR(-EINVAL);
187 pte = huge_pte_offset(mm, address);
189 /* hugetlb should be locked, and hence, prefaulted */
190 WARN_ON(!pte || pte_none(*pte));
192 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
194 WARN_ON(!PageHead(page));
196 return page;
199 int pmd_huge(pmd_t pmd)
201 return 0;
204 int pud_huge(pud_t pud)
206 return 0;
209 struct page *
210 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
211 pmd_t *pmd, int write)
213 return NULL;
216 #else
218 struct page *
219 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
221 return ERR_PTR(-EINVAL);
224 int pmd_huge(pmd_t pmd)
226 return !!(pmd_val(pmd) & _PAGE_PSE);
229 int pud_huge(pud_t pud)
231 return !!(pud_val(pud) & _PAGE_PSE);
234 struct page *
235 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
236 pmd_t *pmd, int write)
238 struct page *page;
240 page = pte_page(*(pte_t *)pmd);
241 if (page)
242 page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
243 return page;
246 struct page *
247 follow_huge_pud(struct mm_struct *mm, unsigned long address,
248 pud_t *pud, int write)
250 struct page *page;
252 page = pte_page(*(pte_t *)pud);
253 if (page)
254 page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
255 return page;
258 #endif
260 /* x86_64 also uses this file */
262 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
263 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
264 unsigned long addr, unsigned long len,
265 unsigned long pgoff, unsigned long flags)
267 struct hstate *h = hstate_file(file);
268 struct mm_struct *mm = current->mm;
269 struct vm_area_struct *vma;
270 unsigned long start_addr;
272 if (len > mm->cached_hole_size) {
273 start_addr = mm->free_area_cache;
274 } else {
275 start_addr = TASK_UNMAPPED_BASE;
276 mm->cached_hole_size = 0;
279 full_search:
280 addr = ALIGN(start_addr, huge_page_size(h));
282 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
283 /* At this point: (!vma || addr < vma->vm_end). */
284 if (TASK_SIZE - len < addr) {
286 * Start a new search - just in case we missed
287 * some holes.
289 if (start_addr != TASK_UNMAPPED_BASE) {
290 start_addr = TASK_UNMAPPED_BASE;
291 mm->cached_hole_size = 0;
292 goto full_search;
294 return -ENOMEM;
296 if (!vma || addr + len <= vma->vm_start) {
297 mm->free_area_cache = addr + len;
298 return addr;
300 if (addr + mm->cached_hole_size < vma->vm_start)
301 mm->cached_hole_size = vma->vm_start - addr;
302 addr = ALIGN(vma->vm_end, huge_page_size(h));
306 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
307 unsigned long addr0, unsigned long len,
308 unsigned long pgoff, unsigned long flags)
310 struct hstate *h = hstate_file(file);
311 struct mm_struct *mm = current->mm;
312 struct vm_area_struct *vma, *prev_vma;
313 unsigned long base = mm->mmap_base, addr = addr0;
314 unsigned long largest_hole = mm->cached_hole_size;
315 int first_time = 1;
317 /* don't allow allocations above current base */
318 if (mm->free_area_cache > base)
319 mm->free_area_cache = base;
321 if (len <= largest_hole) {
322 largest_hole = 0;
323 mm->free_area_cache = base;
325 try_again:
326 /* make sure it can fit in the remaining address space */
327 if (mm->free_area_cache < len)
328 goto fail;
330 /* either no address requested or cant fit in requested address hole */
331 addr = (mm->free_area_cache - len) & huge_page_mask(h);
332 do {
334 * Lookup failure means no vma is above this address,
335 * i.e. return with success:
337 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
338 return addr;
341 * new region fits between prev_vma->vm_end and
342 * vma->vm_start, use it:
344 if (addr + len <= vma->vm_start &&
345 (!prev_vma || (addr >= prev_vma->vm_end))) {
346 /* remember the address as a hint for next time */
347 mm->cached_hole_size = largest_hole;
348 return (mm->free_area_cache = addr);
349 } else {
350 /* pull free_area_cache down to the first hole */
351 if (mm->free_area_cache == vma->vm_end) {
352 mm->free_area_cache = vma->vm_start;
353 mm->cached_hole_size = largest_hole;
357 /* remember the largest hole we saw so far */
358 if (addr + largest_hole < vma->vm_start)
359 largest_hole = vma->vm_start - addr;
361 /* try just below the current vma->vm_start */
362 addr = (vma->vm_start - len) & huge_page_mask(h);
363 } while (len <= vma->vm_start);
365 fail:
367 * if hint left us with no space for the requested
368 * mapping then try again:
370 if (first_time) {
371 mm->free_area_cache = base;
372 largest_hole = 0;
373 first_time = 0;
374 goto try_again;
377 * A failed mmap() very likely causes application failure,
378 * so fall back to the bottom-up function here. This scenario
379 * can happen with large stack limits and large mmap()
380 * allocations.
382 mm->free_area_cache = TASK_UNMAPPED_BASE;
383 mm->cached_hole_size = ~0UL;
384 addr = hugetlb_get_unmapped_area_bottomup(file, addr0,
385 len, pgoff, flags);
388 * Restore the topdown base:
390 mm->free_area_cache = base;
391 mm->cached_hole_size = ~0UL;
393 return addr;
396 unsigned long
397 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
398 unsigned long len, unsigned long pgoff, unsigned long flags)
400 struct hstate *h = hstate_file(file);
401 struct mm_struct *mm = current->mm;
402 struct vm_area_struct *vma;
404 if (len & ~huge_page_mask(h))
405 return -EINVAL;
406 if (len > TASK_SIZE)
407 return -ENOMEM;
409 if (flags & MAP_FIXED) {
410 if (prepare_hugepage_range(file, addr, len))
411 return -EINVAL;
412 return addr;
415 if (addr) {
416 addr = ALIGN(addr, huge_page_size(h));
417 vma = find_vma(mm, addr);
418 if (TASK_SIZE - len >= addr &&
419 (!vma || addr + len <= vma->vm_start))
420 return addr;
422 if (mm->get_unmapped_area == arch_get_unmapped_area)
423 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
424 pgoff, flags);
425 else
426 return hugetlb_get_unmapped_area_topdown(file, addr, len,
427 pgoff, flags);
430 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
432 #ifdef CONFIG_X86_64
433 static __init int setup_hugepagesz(char *opt)
435 unsigned long ps = memparse(opt, &opt);
436 if (ps == PMD_SIZE) {
437 hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
438 } else if (ps == PUD_SIZE && cpu_has_gbpages) {
439 hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
440 } else {
441 printk(KERN_ERR "hugepagesz: Unsupported page size %lu M\n",
442 ps >> 20);
443 return 0;
445 return 1;
447 __setup("hugepagesz=", setup_hugepagesz);
448 #endif