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>
22 #include <linux/vs_memory.h>
25 const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
26 static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
27 unsigned long max_huge_pages;
28 static struct list_head hugepage_freelists[MAX_NUMNODES];
29 static unsigned int nr_huge_pages_node[MAX_NUMNODES];
30 static unsigned int free_huge_pages_node[MAX_NUMNODES];
32 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
34 static DEFINE_SPINLOCK(hugetlb_lock);
36 static void clear_huge_page(struct page *page, unsigned long addr)
41 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
43 clear_user_highpage(page + i, addr);
47 static void copy_huge_page(struct page *dst, struct page *src,
48 unsigned long addr, struct vm_area_struct *vma)
53 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
55 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
59 static void enqueue_huge_page(struct page *page)
61 int nid = page_to_nid(page);
62 list_add(&page->lru, &hugepage_freelists[nid]);
64 free_huge_pages_node[nid]++;
67 static struct page *dequeue_huge_page(struct vm_area_struct *vma,
68 unsigned long address)
70 int nid = numa_node_id();
71 struct page *page = NULL;
72 struct zonelist *zonelist = huge_zonelist(vma, address);
75 for (z = zonelist->zones; *z; z++) {
76 nid = zone_to_nid(*z);
77 if (cpuset_zone_allowed_softwall(*z, GFP_HIGHUSER) &&
78 !list_empty(&hugepage_freelists[nid]))
83 page = list_entry(hugepage_freelists[nid].next,
87 free_huge_pages_node[nid]--;
92 static void free_huge_page(struct page *page)
94 BUG_ON(page_count(page));
96 INIT_LIST_HEAD(&page->lru);
98 spin_lock(&hugetlb_lock);
99 enqueue_huge_page(page);
100 spin_unlock(&hugetlb_lock);
103 static int alloc_fresh_huge_page(void)
107 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
109 nid = next_node(nid, node_online_map);
110 if (nid == MAX_NUMNODES)
111 nid = first_node(node_online_map);
113 set_compound_page_dtor(page, free_huge_page);
114 spin_lock(&hugetlb_lock);
116 nr_huge_pages_node[page_to_nid(page)]++;
117 spin_unlock(&hugetlb_lock);
118 put_page(page); /* free it into the hugepage allocator */
124 static struct page *alloc_huge_page(struct vm_area_struct *vma,
129 spin_lock(&hugetlb_lock);
130 if (vma->vm_flags & VM_MAYSHARE)
132 else if (free_huge_pages <= resv_huge_pages)
135 page = dequeue_huge_page(vma, addr);
139 spin_unlock(&hugetlb_lock);
140 set_page_refcounted(page);
144 spin_unlock(&hugetlb_lock);
148 static int __init hugetlb_init(void)
152 if (HPAGE_SHIFT == 0)
155 for (i = 0; i < MAX_NUMNODES; ++i)
156 INIT_LIST_HEAD(&hugepage_freelists[i]);
158 for (i = 0; i < max_huge_pages; ++i) {
159 if (!alloc_fresh_huge_page())
162 max_huge_pages = free_huge_pages = nr_huge_pages = i;
163 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
166 module_init(hugetlb_init);
168 static int __init hugetlb_setup(char *s)
170 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
174 __setup("hugepages=", hugetlb_setup);
177 static void update_and_free_page(struct page *page)
181 nr_huge_pages_node[page_to_nid(page)]--;
182 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
183 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
184 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
185 1 << PG_private | 1<< PG_writeback);
187 page[1].lru.next = NULL;
188 set_page_refcounted(page);
189 __free_pages(page, HUGETLB_PAGE_ORDER);
192 #ifdef CONFIG_HIGHMEM
193 static void try_to_free_low(unsigned long count)
197 for (i = 0; i < MAX_NUMNODES; ++i) {
198 struct page *page, *next;
199 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
200 if (PageHighMem(page))
202 list_del(&page->lru);
203 update_and_free_page(page);
205 free_huge_pages_node[page_to_nid(page)]--;
206 if (count >= nr_huge_pages)
212 static inline void try_to_free_low(unsigned long count)
217 static unsigned long set_max_huge_pages(unsigned long count)
219 while (count > nr_huge_pages) {
220 if (!alloc_fresh_huge_page())
221 return nr_huge_pages;
223 if (count >= nr_huge_pages)
224 return nr_huge_pages;
226 spin_lock(&hugetlb_lock);
227 count = max(count, resv_huge_pages);
228 try_to_free_low(count);
229 while (count < nr_huge_pages) {
230 struct page *page = dequeue_huge_page(NULL, 0);
233 update_and_free_page(page);
235 spin_unlock(&hugetlb_lock);
236 return nr_huge_pages;
239 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
240 struct file *file, void __user *buffer,
241 size_t *length, loff_t *ppos)
243 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
244 max_huge_pages = set_max_huge_pages(max_huge_pages);
247 #endif /* CONFIG_SYSCTL */
249 int hugetlb_report_meminfo(char *buf)
252 "HugePages_Total: %5lu\n"
253 "HugePages_Free: %5lu\n"
254 "HugePages_Rsvd: %5lu\n"
255 "Hugepagesize: %5lu kB\n",
262 int hugetlb_report_node_meminfo(int nid, char *buf)
265 "Node %d HugePages_Total: %5u\n"
266 "Node %d HugePages_Free: %5u\n",
267 nid, nr_huge_pages_node[nid],
268 nid, free_huge_pages_node[nid]);
271 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
272 unsigned long hugetlb_total_pages(void)
274 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
278 * We cannot handle pagefaults against hugetlb pages at all. They cause
279 * handle_mm_fault() to try to instantiate regular-sized pages in the
280 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
283 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
284 unsigned long address, int *unused)
290 struct vm_operations_struct hugetlb_vm_ops = {
291 .nopage = hugetlb_nopage,
294 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
301 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
303 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
305 entry = pte_mkyoung(entry);
306 entry = pte_mkhuge(entry);
311 static void set_huge_ptep_writable(struct vm_area_struct *vma,
312 unsigned long address, pte_t *ptep)
316 entry = pte_mkwrite(pte_mkdirty(*ptep));
317 ptep_set_access_flags(vma, address, ptep, entry, 1);
318 update_mmu_cache(vma, address, entry);
319 lazy_mmu_prot_update(entry);
323 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
324 struct vm_area_struct *vma)
326 pte_t *src_pte, *dst_pte, entry;
327 struct page *ptepage;
331 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
333 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
334 src_pte = huge_pte_offset(src, addr);
337 dst_pte = huge_pte_alloc(dst, addr);
340 spin_lock(&dst->page_table_lock);
341 spin_lock(&src->page_table_lock);
342 if (!pte_none(*src_pte)) {
344 ptep_set_wrprotect(src, addr, src_pte);
346 ptepage = pte_page(entry);
348 set_huge_pte_at(dst, addr, dst_pte, entry);
350 spin_unlock(&src->page_table_lock);
351 spin_unlock(&dst->page_table_lock);
359 void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
362 struct mm_struct *mm = vma->vm_mm;
363 unsigned long address;
369 * A page gathering list, protected by per file i_mmap_lock. The
370 * lock is used to avoid list corruption from multiple unmapping
371 * of the same page since we are using page->lru.
373 LIST_HEAD(page_list);
375 WARN_ON(!is_vm_hugetlb_page(vma));
376 BUG_ON(start & ~HPAGE_MASK);
377 BUG_ON(end & ~HPAGE_MASK);
379 spin_lock(&mm->page_table_lock);
380 for (address = start; address < end; address += HPAGE_SIZE) {
381 ptep = huge_pte_offset(mm, address);
385 if (huge_pmd_unshare(mm, &address, ptep))
388 pte = huge_ptep_get_and_clear(mm, address, ptep);
392 page = pte_page(pte);
394 set_page_dirty(page);
395 list_add(&page->lru, &page_list);
397 spin_unlock(&mm->page_table_lock);
398 flush_tlb_range(vma, start, end);
399 list_for_each_entry_safe(page, tmp, &page_list, lru) {
400 list_del(&page->lru);
405 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
409 * It is undesirable to test vma->vm_file as it should be non-null
410 * for valid hugetlb area. However, vm_file will be NULL in the error
411 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
412 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
413 * to clean up. Since no pte has actually been setup, it is safe to
414 * do nothing in this case.
417 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
418 __unmap_hugepage_range(vma, start, end);
419 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
423 static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
424 unsigned long address, pte_t *ptep, pte_t pte)
426 struct page *old_page, *new_page;
429 old_page = pte_page(pte);
431 /* If no-one else is actually using this page, avoid the copy
432 * and just make the page writable */
433 avoidcopy = (page_count(old_page) == 1);
435 set_huge_ptep_writable(vma, address, ptep);
436 return VM_FAULT_MINOR;
439 page_cache_get(old_page);
440 new_page = alloc_huge_page(vma, address);
443 page_cache_release(old_page);
447 spin_unlock(&mm->page_table_lock);
448 copy_huge_page(new_page, old_page, address, vma);
449 spin_lock(&mm->page_table_lock);
451 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
452 if (likely(pte_same(*ptep, pte))) {
454 set_huge_pte_at(mm, address, ptep,
455 make_huge_pte(vma, new_page, 1));
456 /* Make the old page be freed below */
459 page_cache_release(new_page);
460 page_cache_release(old_page);
461 return VM_FAULT_MINOR;
464 int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
465 unsigned long address, pte_t *ptep, int write_access)
467 int ret = VM_FAULT_SIGBUS;
471 struct address_space *mapping;
474 mapping = vma->vm_file->f_mapping;
475 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
476 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
479 * Use page lock to guard against racing truncation
480 * before we get page_table_lock.
483 page = find_lock_page(mapping, idx);
485 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
488 if (hugetlb_get_quota(mapping))
490 page = alloc_huge_page(vma, address);
492 hugetlb_put_quota(mapping);
496 clear_huge_page(page, address);
498 if (vma->vm_flags & VM_SHARED) {
501 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
504 hugetlb_put_quota(mapping);
513 spin_lock(&mm->page_table_lock);
514 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
518 ret = VM_FAULT_MINOR;
519 if (!pte_none(*ptep))
522 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
523 && (vma->vm_flags & VM_SHARED)));
524 set_huge_pte_at(mm, address, ptep, new_pte);
526 if (write_access && !(vma->vm_flags & VM_SHARED)) {
527 /* Optimization, do the COW without a second fault */
528 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
531 spin_unlock(&mm->page_table_lock);
537 spin_unlock(&mm->page_table_lock);
538 hugetlb_put_quota(mapping);
544 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
545 unsigned long address, int write_access)
550 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
552 ptep = huge_pte_alloc(mm, address);
557 * Serialize hugepage allocation and instantiation, so that we don't
558 * get spurious allocation failures if two CPUs race to instantiate
559 * the same page in the page cache.
561 mutex_lock(&hugetlb_instantiation_mutex);
563 if (pte_none(entry)) {
564 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
565 mutex_unlock(&hugetlb_instantiation_mutex);
569 ret = VM_FAULT_MINOR;
571 spin_lock(&mm->page_table_lock);
572 /* Check for a racing update before calling hugetlb_cow */
573 if (likely(pte_same(entry, *ptep)))
574 if (write_access && !pte_write(entry))
575 ret = hugetlb_cow(mm, vma, address, ptep, entry);
576 spin_unlock(&mm->page_table_lock);
577 mutex_unlock(&hugetlb_instantiation_mutex);
582 int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
583 struct page **pages, struct vm_area_struct **vmas,
584 unsigned long *position, int *length, int i)
586 unsigned long pfn_offset;
587 unsigned long vaddr = *position;
588 int remainder = *length;
590 spin_lock(&mm->page_table_lock);
591 while (vaddr < vma->vm_end && remainder) {
596 * Some archs (sparc64, sh*) have multiple pte_ts to
597 * each hugepage. We have to make * sure we get the
598 * first, for the page indexing below to work.
600 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
602 if (!pte || pte_none(*pte)) {
605 spin_unlock(&mm->page_table_lock);
606 ret = hugetlb_fault(mm, vma, vaddr, 0);
607 spin_lock(&mm->page_table_lock);
608 if (ret == VM_FAULT_MINOR)
617 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
618 page = pte_page(*pte);
622 pages[i] = page + pfn_offset;
632 if (vaddr < vma->vm_end && remainder &&
633 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
635 * We use pfn_offset to avoid touching the pageframes
636 * of this compound page.
641 spin_unlock(&mm->page_table_lock);
648 void hugetlb_change_protection(struct vm_area_struct *vma,
649 unsigned long address, unsigned long end, pgprot_t newprot)
651 struct mm_struct *mm = vma->vm_mm;
652 unsigned long start = address;
656 BUG_ON(address >= end);
657 flush_cache_range(vma, address, end);
659 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
660 spin_lock(&mm->page_table_lock);
661 for (; address < end; address += HPAGE_SIZE) {
662 ptep = huge_pte_offset(mm, address);
665 if (huge_pmd_unshare(mm, &address, ptep))
667 if (!pte_none(*ptep)) {
668 pte = huge_ptep_get_and_clear(mm, address, ptep);
669 pte = pte_mkhuge(pte_modify(pte, newprot));
670 set_huge_pte_at(mm, address, ptep, pte);
671 lazy_mmu_prot_update(pte);
674 spin_unlock(&mm->page_table_lock);
675 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
677 flush_tlb_range(vma, start, end);
681 struct list_head link;
686 static long region_add(struct list_head *head, long f, long t)
688 struct file_region *rg, *nrg, *trg;
690 /* Locate the region we are either in or before. */
691 list_for_each_entry(rg, head, link)
695 /* Round our left edge to the current segment if it encloses us. */
699 /* Check for and consume any regions we now overlap with. */
701 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
702 if (&rg->link == head)
707 /* If this area reaches higher then extend our area to
708 * include it completely. If this is not the first area
709 * which we intend to reuse, free it. */
722 static long region_chg(struct list_head *head, long f, long t)
724 struct file_region *rg, *nrg;
727 /* Locate the region we are before or in. */
728 list_for_each_entry(rg, head, link)
732 /* If we are below the current region then a new region is required.
733 * Subtle, allocate a new region at the position but make it zero
734 * size such that we can guarentee to record the reservation. */
735 if (&rg->link == head || t < rg->from) {
736 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
741 INIT_LIST_HEAD(&nrg->link);
742 list_add(&nrg->link, rg->link.prev);
747 /* Round our left edge to the current segment if it encloses us. */
752 /* Check for and consume any regions we now overlap with. */
753 list_for_each_entry(rg, rg->link.prev, link) {
754 if (&rg->link == head)
759 /* We overlap with this area, if it extends futher than
760 * us then we must extend ourselves. Account for its
761 * existing reservation. */
766 chg -= rg->to - rg->from;
771 static long region_truncate(struct list_head *head, long end)
773 struct file_region *rg, *trg;
776 /* Locate the region we are either in or before. */
777 list_for_each_entry(rg, head, link)
780 if (&rg->link == head)
783 /* If we are in the middle of a region then adjust it. */
784 if (end > rg->from) {
787 rg = list_entry(rg->link.next, typeof(*rg), link);
790 /* Drop any remaining regions. */
791 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
792 if (&rg->link == head)
794 chg += rg->to - rg->from;
801 static int hugetlb_acct_memory(long delta)
805 spin_lock(&hugetlb_lock);
806 if ((delta + resv_huge_pages) <= free_huge_pages) {
807 resv_huge_pages += delta;
810 spin_unlock(&hugetlb_lock);
814 int hugetlb_reserve_pages(struct inode *inode, long from, long to)
818 chg = region_chg(&inode->i_mapping->private_list, from, to);
821 ret = hugetlb_acct_memory(chg);
824 region_add(&inode->i_mapping->private_list, from, to);
828 void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
830 long chg = region_truncate(&inode->i_mapping->private_list, offset);
831 hugetlb_acct_memory(freed - chg);