2 * IA-32 Huge TLB Page Support for Kernel.
4 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
7 #include <linux/init.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>
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
) +
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
37 if (pmd_index(addr
) != pmd_index(saddr
) ||
38 vm_flags
!= svm_flags
||
39 sbase
< svma
->vm_start
|| svma
->vm_end
< s_end
)
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
)
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
) +
68 struct prio_tree_iter iter
;
69 struct vm_area_struct
*svma
;
73 if (!vma_shareable(vma
, addr
))
76 spin_lock(&mapping
->i_mmap_lock
);
77 vma_prio_tree_foreach(svma
, &iter
, &mapping
->i_mmap
, idx
, idx
) {
81 saddr
= page_table_shareable(svma
, vma
, addr
, idx
);
83 spte
= huge_pte_offset(svma
->vm_mm
, saddr
);
85 get_page(virt_to_page(spte
));
94 spin_lock(&mm
->page_table_lock
);
96 pud_populate(mm
, pud
, (pmd_t
*)((unsigned long)spte
& PAGE_MASK
));
98 put_page(virt_to_page(spte
));
99 spin_unlock(&mm
->page_table_lock
);
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)
126 put_page(virt_to_page(ptep
));
127 *addr
= ALIGN(*addr
, HPAGE_SIZE
* PTRS_PER_PTE
) - HPAGE_SIZE
;
131 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
132 unsigned long addr
, unsigned long sz
)
138 pgd
= pgd_offset(mm
, addr
);
139 pud
= pud_alloc(mm
, pgd
, addr
);
141 if (sz
== PUD_SIZE
) {
144 BUG_ON(sz
!= PMD_SIZE
);
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
));
155 pte_t
*huge_pte_offset(struct mm_struct
*mm
, unsigned long addr
)
161 pgd
= pgd_offset(mm
, addr
);
162 if (pgd_present(*pgd
)) {
163 pud
= pud_offset(pgd
, addr
);
164 if (pud_present(*pud
)) {
167 pmd
= pmd_offset(pud
, addr
);
170 return (pte_t
*) pmd
;
173 #if 0 /* This is just for testing */
175 follow_huge_addr(struct mm_struct
*mm
, unsigned long address
, int write
)
177 unsigned long start
= address
;
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
));
199 int pmd_huge(pmd_t pmd
)
204 int pud_huge(pud_t pud
)
210 follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
211 pmd_t
*pmd
, int write
)
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
);
235 follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
236 pmd_t
*pmd
, int write
)
240 page
= pte_page(*(pte_t
*)pmd
);
242 page
+= ((address
& ~PMD_MASK
) >> PAGE_SHIFT
);
247 follow_huge_pud(struct mm_struct
*mm
, unsigned long address
,
248 pud_t
*pud
, int write
)
252 page
= pte_page(*(pte_t
*)pud
);
254 page
+= ((address
& ~PUD_MASK
) >> PAGE_SHIFT
);
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
;
275 start_addr
= TASK_UNMAPPED_BASE
;
276 mm
->cached_hole_size
= 0;
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
289 if (start_addr
!= TASK_UNMAPPED_BASE
) {
290 start_addr
= TASK_UNMAPPED_BASE
;
291 mm
->cached_hole_size
= 0;
296 if (!vma
|| addr
+ len
<= vma
->vm_start
) {
297 mm
->free_area_cache
= addr
+ len
;
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
;
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
) {
323 mm
->free_area_cache
= base
;
326 /* make sure it can fit in the remaining address space */
327 if (mm
->free_area_cache
< len
)
330 /* either no address requested or cant fit in requested address hole */
331 addr
= (mm
->free_area_cache
- len
) & huge_page_mask(h
);
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
)))
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
);
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
);
367 * if hint left us with no space for the requested
368 * mapping then try again:
371 mm
->free_area_cache
= base
;
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()
382 mm
->free_area_cache
= TASK_UNMAPPED_BASE
;
383 mm
->cached_hole_size
= ~0UL;
384 addr
= hugetlb_get_unmapped_area_bottomup(file
, addr0
,
388 * Restore the topdown base:
390 mm
->free_area_cache
= base
;
391 mm
->cached_hole_size
= ~0UL;
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
))
409 if (flags
& MAP_FIXED
) {
410 if (prepare_hugepage_range(file
, addr
, len
))
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
))
422 if (mm
->get_unmapped_area
== arch_get_unmapped_area
)
423 return hugetlb_get_unmapped_area_bottomup(file
, addr
, len
,
426 return hugetlb_get_unmapped_area_topdown(file
, addr
, len
,
430 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
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
);
441 printk(KERN_ERR
"hugepagesz: Unsupported page size %lu M\n",
447 __setup("hugepagesz=", setup_hugepagesz
);