2 * Generic hugetlb support.
3 * (C) William Irwin, April 2004
6 #include <linux/list.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <linux/sysctl.h>
11 #include <linux/highmem.h>
12 #include <linux/nodemask.h>
13 #include <linux/pagemap.h>
14 #include <linux/mempolicy.h>
15 #include <linux/cpuset.h>
16 #include <linux/mutex.h>
19 #include <asm/pgtable.h>
21 #include <linux/hugetlb.h>
24 const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
25 static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
26 unsigned long max_huge_pages;
27 static struct list_head hugepage_freelists[MAX_NUMNODES];
28 static unsigned int nr_huge_pages_node[MAX_NUMNODES];
29 static unsigned int free_huge_pages_node[MAX_NUMNODES];
30 static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
31 unsigned long hugepages_treat_as_movable;
34 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
36 static DEFINE_SPINLOCK(hugetlb_lock);
38 static void clear_huge_page(struct page *page, unsigned long addr)
43 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
45 clear_user_highpage(page + i, addr);
49 static void copy_huge_page(struct page *dst, struct page *src,
50 unsigned long addr, struct vm_area_struct *vma)
55 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
57 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
61 static void enqueue_huge_page(struct page *page)
63 int nid = page_to_nid(page);
64 list_add(&page->lru, &hugepage_freelists[nid]);
66 free_huge_pages_node[nid]++;
69 static struct page *dequeue_huge_page(struct vm_area_struct *vma,
70 unsigned long address)
73 struct page *page = NULL;
74 struct zonelist *zonelist = huge_zonelist(vma, address,
78 for (z = zonelist->zones; *z; z++) {
79 nid = zone_to_nid(*z);
80 if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) &&
81 !list_empty(&hugepage_freelists[nid])) {
82 page = list_entry(hugepage_freelists[nid].next,
86 free_huge_pages_node[nid]--;
93 static void free_huge_page(struct page *page)
95 BUG_ON(page_count(page));
97 INIT_LIST_HEAD(&page->lru);
99 spin_lock(&hugetlb_lock);
100 enqueue_huge_page(page);
101 spin_unlock(&hugetlb_lock);
104 static int alloc_fresh_huge_page(void)
111 * Copy static prev_nid to local nid, work on that, then copy it
112 * back to prev_nid afterwards: otherwise there's a window in which
113 * a racer might pass invalid nid MAX_NUMNODES to alloc_pages_node.
114 * But we don't need to use a spin_lock here: it really doesn't
115 * matter if occasionally a racer chooses the same nid as we do.
117 nid = next_node(prev_nid, node_online_map);
118 if (nid == MAX_NUMNODES)
119 nid = first_node(node_online_map);
122 page = alloc_pages_node(nid, htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN,
125 set_compound_page_dtor(page, free_huge_page);
126 spin_lock(&hugetlb_lock);
128 nr_huge_pages_node[page_to_nid(page)]++;
129 spin_unlock(&hugetlb_lock);
130 put_page(page); /* free it into the hugepage allocator */
136 static struct page *alloc_huge_page(struct vm_area_struct *vma,
141 spin_lock(&hugetlb_lock);
142 if (vma->vm_flags & VM_MAYSHARE)
144 else if (free_huge_pages <= resv_huge_pages)
147 page = dequeue_huge_page(vma, addr);
151 spin_unlock(&hugetlb_lock);
152 set_page_refcounted(page);
156 if (vma->vm_flags & VM_MAYSHARE)
158 spin_unlock(&hugetlb_lock);
162 static int __init hugetlb_init(void)
166 if (HPAGE_SHIFT == 0)
169 for (i = 0; i < MAX_NUMNODES; ++i)
170 INIT_LIST_HEAD(&hugepage_freelists[i]);
172 for (i = 0; i < max_huge_pages; ++i) {
173 if (!alloc_fresh_huge_page())
176 max_huge_pages = free_huge_pages = nr_huge_pages = i;
177 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
180 module_init(hugetlb_init);
182 static int __init hugetlb_setup(char *s)
184 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
188 __setup("hugepages=", hugetlb_setup);
190 static unsigned int cpuset_mems_nr(unsigned int *array)
195 for_each_node_mask(node, cpuset_current_mems_allowed)
202 static void update_and_free_page(struct page *page)
206 nr_huge_pages_node[page_to_nid(page)]--;
207 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
208 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
209 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
210 1 << PG_private | 1<< PG_writeback);
212 set_compound_page_dtor(page, NULL);
213 set_page_refcounted(page);
214 __free_pages(page, HUGETLB_PAGE_ORDER);
217 #ifdef CONFIG_HIGHMEM
218 static void try_to_free_low(unsigned long count)
222 for (i = 0; i < MAX_NUMNODES; ++i) {
223 struct page *page, *next;
224 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
225 if (PageHighMem(page))
227 list_del(&page->lru);
228 update_and_free_page(page);
230 free_huge_pages_node[page_to_nid(page)]--;
231 if (count >= nr_huge_pages)
237 static inline void try_to_free_low(unsigned long count)
242 static unsigned long set_max_huge_pages(unsigned long count)
244 while (count > nr_huge_pages) {
245 if (!alloc_fresh_huge_page())
246 return nr_huge_pages;
248 if (count >= nr_huge_pages)
249 return nr_huge_pages;
251 spin_lock(&hugetlb_lock);
252 count = max(count, resv_huge_pages);
253 try_to_free_low(count);
254 while (count < nr_huge_pages) {
255 struct page *page = dequeue_huge_page(NULL, 0);
258 update_and_free_page(page);
260 spin_unlock(&hugetlb_lock);
261 return nr_huge_pages;
264 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
265 struct file *file, void __user *buffer,
266 size_t *length, loff_t *ppos)
268 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
269 max_huge_pages = set_max_huge_pages(max_huge_pages);
273 int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
274 struct file *file, void __user *buffer,
275 size_t *length, loff_t *ppos)
277 proc_dointvec(table, write, file, buffer, length, ppos);
278 if (hugepages_treat_as_movable)
279 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
281 htlb_alloc_mask = GFP_HIGHUSER;
285 #endif /* CONFIG_SYSCTL */
287 int hugetlb_report_meminfo(char *buf)
290 "HugePages_Total: %5lu\n"
291 "HugePages_Free: %5lu\n"
292 "HugePages_Rsvd: %5lu\n"
293 "Hugepagesize: %5lu kB\n",
300 int hugetlb_report_node_meminfo(int nid, char *buf)
303 "Node %d HugePages_Total: %5u\n"
304 "Node %d HugePages_Free: %5u\n",
305 nid, nr_huge_pages_node[nid],
306 nid, free_huge_pages_node[nid]);
309 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
310 unsigned long hugetlb_total_pages(void)
312 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
316 * We cannot handle pagefaults against hugetlb pages at all. They cause
317 * handle_mm_fault() to try to instantiate regular-sized pages in the
318 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
321 static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
327 struct vm_operations_struct hugetlb_vm_ops = {
328 .fault = hugetlb_vm_op_fault,
331 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
338 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
340 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
342 entry = pte_mkyoung(entry);
343 entry = pte_mkhuge(entry);
348 static void set_huge_ptep_writable(struct vm_area_struct *vma,
349 unsigned long address, pte_t *ptep)
353 entry = pte_mkwrite(pte_mkdirty(*ptep));
354 if (ptep_set_access_flags(vma, address, ptep, entry, 1)) {
355 update_mmu_cache(vma, address, entry);
356 lazy_mmu_prot_update(entry);
361 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
362 struct vm_area_struct *vma)
364 pte_t *src_pte, *dst_pte, entry;
365 struct page *ptepage;
369 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
371 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
372 src_pte = huge_pte_offset(src, addr);
375 dst_pte = huge_pte_alloc(dst, addr);
378 spin_lock(&dst->page_table_lock);
379 spin_lock(&src->page_table_lock);
380 if (!pte_none(*src_pte)) {
382 ptep_set_wrprotect(src, addr, src_pte);
384 ptepage = pte_page(entry);
386 set_huge_pte_at(dst, addr, dst_pte, entry);
388 spin_unlock(&src->page_table_lock);
389 spin_unlock(&dst->page_table_lock);
397 void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
400 struct mm_struct *mm = vma->vm_mm;
401 unsigned long address;
407 * A page gathering list, protected by per file i_mmap_lock. The
408 * lock is used to avoid list corruption from multiple unmapping
409 * of the same page since we are using page->lru.
411 LIST_HEAD(page_list);
413 WARN_ON(!is_vm_hugetlb_page(vma));
414 BUG_ON(start & ~HPAGE_MASK);
415 BUG_ON(end & ~HPAGE_MASK);
417 spin_lock(&mm->page_table_lock);
418 for (address = start; address < end; address += HPAGE_SIZE) {
419 ptep = huge_pte_offset(mm, address);
423 if (huge_pmd_unshare(mm, &address, ptep))
426 pte = huge_ptep_get_and_clear(mm, address, ptep);
430 page = pte_page(pte);
432 set_page_dirty(page);
433 list_add(&page->lru, &page_list);
435 spin_unlock(&mm->page_table_lock);
436 flush_tlb_range(vma, start, end);
437 list_for_each_entry_safe(page, tmp, &page_list, lru) {
438 list_del(&page->lru);
443 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
447 * It is undesirable to test vma->vm_file as it should be non-null
448 * for valid hugetlb area. However, vm_file will be NULL in the error
449 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
450 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
451 * to clean up. Since no pte has actually been setup, it is safe to
452 * do nothing in this case.
455 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
456 __unmap_hugepage_range(vma, start, end);
457 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
461 static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
462 unsigned long address, pte_t *ptep, pte_t pte)
464 struct page *old_page, *new_page;
467 old_page = pte_page(pte);
469 /* If no-one else is actually using this page, avoid the copy
470 * and just make the page writable */
471 avoidcopy = (page_count(old_page) == 1);
473 set_huge_ptep_writable(vma, address, ptep);
477 page_cache_get(old_page);
478 new_page = alloc_huge_page(vma, address);
481 page_cache_release(old_page);
485 spin_unlock(&mm->page_table_lock);
486 copy_huge_page(new_page, old_page, address, vma);
487 spin_lock(&mm->page_table_lock);
489 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
490 if (likely(pte_same(*ptep, pte))) {
492 set_huge_pte_at(mm, address, ptep,
493 make_huge_pte(vma, new_page, 1));
494 /* Make the old page be freed below */
497 page_cache_release(new_page);
498 page_cache_release(old_page);
502 static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
503 unsigned long address, pte_t *ptep, int write_access)
505 int ret = VM_FAULT_SIGBUS;
509 struct address_space *mapping;
512 mapping = vma->vm_file->f_mapping;
513 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
514 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
517 * Use page lock to guard against racing truncation
518 * before we get page_table_lock.
521 page = find_lock_page(mapping, idx);
523 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
526 if (hugetlb_get_quota(mapping))
528 page = alloc_huge_page(vma, address);
530 hugetlb_put_quota(mapping);
534 clear_huge_page(page, address);
536 if (vma->vm_flags & VM_SHARED) {
539 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
542 hugetlb_put_quota(mapping);
551 spin_lock(&mm->page_table_lock);
552 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
557 if (!pte_none(*ptep))
560 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
561 && (vma->vm_flags & VM_SHARED)));
562 set_huge_pte_at(mm, address, ptep, new_pte);
564 if (write_access && !(vma->vm_flags & VM_SHARED)) {
565 /* Optimization, do the COW without a second fault */
566 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
569 spin_unlock(&mm->page_table_lock);
575 spin_unlock(&mm->page_table_lock);
576 hugetlb_put_quota(mapping);
582 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
583 unsigned long address, int write_access)
588 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
590 ptep = huge_pte_alloc(mm, address);
595 * Serialize hugepage allocation and instantiation, so that we don't
596 * get spurious allocation failures if two CPUs race to instantiate
597 * the same page in the page cache.
599 mutex_lock(&hugetlb_instantiation_mutex);
601 if (pte_none(entry)) {
602 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
603 mutex_unlock(&hugetlb_instantiation_mutex);
609 spin_lock(&mm->page_table_lock);
610 /* Check for a racing update before calling hugetlb_cow */
611 if (likely(pte_same(entry, *ptep)))
612 if (write_access && !pte_write(entry))
613 ret = hugetlb_cow(mm, vma, address, ptep, entry);
614 spin_unlock(&mm->page_table_lock);
615 mutex_unlock(&hugetlb_instantiation_mutex);
620 int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
621 struct page **pages, struct vm_area_struct **vmas,
622 unsigned long *position, int *length, int i)
624 unsigned long pfn_offset;
625 unsigned long vaddr = *position;
626 int remainder = *length;
628 spin_lock(&mm->page_table_lock);
629 while (vaddr < vma->vm_end && remainder) {
634 * Some archs (sparc64, sh*) have multiple pte_ts to
635 * each hugepage. We have to make * sure we get the
636 * first, for the page indexing below to work.
638 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
640 if (!pte || pte_none(*pte)) {
643 spin_unlock(&mm->page_table_lock);
644 ret = hugetlb_fault(mm, vma, vaddr, 0);
645 spin_lock(&mm->page_table_lock);
646 if (!(ret & VM_FAULT_ERROR))
655 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
656 page = pte_page(*pte);
660 pages[i] = page + pfn_offset;
670 if (vaddr < vma->vm_end && remainder &&
671 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
673 * We use pfn_offset to avoid touching the pageframes
674 * of this compound page.
679 spin_unlock(&mm->page_table_lock);
686 void hugetlb_change_protection(struct vm_area_struct *vma,
687 unsigned long address, unsigned long end, pgprot_t newprot)
689 struct mm_struct *mm = vma->vm_mm;
690 unsigned long start = address;
694 BUG_ON(address >= end);
695 flush_cache_range(vma, address, end);
697 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
698 spin_lock(&mm->page_table_lock);
699 for (; address < end; address += HPAGE_SIZE) {
700 ptep = huge_pte_offset(mm, address);
703 if (huge_pmd_unshare(mm, &address, ptep))
705 if (!pte_none(*ptep)) {
706 pte = huge_ptep_get_and_clear(mm, address, ptep);
707 pte = pte_mkhuge(pte_modify(pte, newprot));
708 set_huge_pte_at(mm, address, ptep, pte);
709 lazy_mmu_prot_update(pte);
712 spin_unlock(&mm->page_table_lock);
713 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
715 flush_tlb_range(vma, start, end);
719 struct list_head link;
724 static long region_add(struct list_head *head, long f, long t)
726 struct file_region *rg, *nrg, *trg;
728 /* Locate the region we are either in or before. */
729 list_for_each_entry(rg, head, link)
733 /* Round our left edge to the current segment if it encloses us. */
737 /* Check for and consume any regions we now overlap with. */
739 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
740 if (&rg->link == head)
745 /* If this area reaches higher then extend our area to
746 * include it completely. If this is not the first area
747 * which we intend to reuse, free it. */
760 static long region_chg(struct list_head *head, long f, long t)
762 struct file_region *rg, *nrg;
765 /* Locate the region we are before or in. */
766 list_for_each_entry(rg, head, link)
770 /* If we are below the current region then a new region is required.
771 * Subtle, allocate a new region at the position but make it zero
772 * size such that we can guarentee to record the reservation. */
773 if (&rg->link == head || t < rg->from) {
774 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
779 INIT_LIST_HEAD(&nrg->link);
780 list_add(&nrg->link, rg->link.prev);
785 /* Round our left edge to the current segment if it encloses us. */
790 /* Check for and consume any regions we now overlap with. */
791 list_for_each_entry(rg, rg->link.prev, link) {
792 if (&rg->link == head)
797 /* We overlap with this area, if it extends futher than
798 * us then we must extend ourselves. Account for its
799 * existing reservation. */
804 chg -= rg->to - rg->from;
809 static long region_truncate(struct list_head *head, long end)
811 struct file_region *rg, *trg;
814 /* Locate the region we are either in or before. */
815 list_for_each_entry(rg, head, link)
818 if (&rg->link == head)
821 /* If we are in the middle of a region then adjust it. */
822 if (end > rg->from) {
825 rg = list_entry(rg->link.next, typeof(*rg), link);
828 /* Drop any remaining regions. */
829 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
830 if (&rg->link == head)
832 chg += rg->to - rg->from;
839 static int hugetlb_acct_memory(long delta)
843 spin_lock(&hugetlb_lock);
844 if ((delta + resv_huge_pages) <= free_huge_pages) {
845 resv_huge_pages += delta;
848 spin_unlock(&hugetlb_lock);
852 int hugetlb_reserve_pages(struct inode *inode, long from, long to)
856 chg = region_chg(&inode->i_mapping->private_list, from, to);
860 * When cpuset is configured, it breaks the strict hugetlb page
861 * reservation as the accounting is done on a global variable. Such
862 * reservation is completely rubbish in the presence of cpuset because
863 * the reservation is not checked against page availability for the
864 * current cpuset. Application can still potentially OOM'ed by kernel
865 * with lack of free htlb page in cpuset that the task is in.
866 * Attempt to enforce strict accounting with cpuset is almost
867 * impossible (or too ugly) because cpuset is too fluid that
868 * task or memory node can be dynamically moved between cpusets.
870 * The change of semantics for shared hugetlb mapping with cpuset is
871 * undesirable. However, in order to preserve some of the semantics,
872 * we fall back to check against current free page availability as
873 * a best attempt and hopefully to minimize the impact of changing
874 * semantics that cpuset has.
876 if (chg > cpuset_mems_nr(free_huge_pages_node))
879 ret = hugetlb_acct_memory(chg);
882 region_add(&inode->i_mapping->private_list, from, to);
886 void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
888 long chg = region_truncate(&inode->i_mapping->private_list, offset);
889 hugetlb_acct_memory(freed - chg);