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,
53 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
55 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE);
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 = (*z)->zone_pgdat->node_id;
77 if (cpuset_zone_allowed(*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 page[1].lru.next = (void *)free_huge_page; /* dtor */
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_zone(page)->zone_pgdat->node_id]--;
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)
196 for (i = 0; i < MAX_NUMNODES; ++i) {
197 struct page *page, *next;
198 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
199 if (PageHighMem(page))
201 list_del(&page->lru);
202 update_and_free_page(page);
203 nid = page_zone(page)->zone_pgdat->node_id;
205 free_huge_pages_node[nid]--;
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 add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
349 set_huge_pte_at(dst, addr, dst_pte, entry);
351 spin_unlock(&src->page_table_lock);
352 spin_unlock(&dst->page_table_lock);
360 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
363 struct mm_struct *mm = vma->vm_mm;
364 unsigned long address;
369 WARN_ON(!is_vm_hugetlb_page(vma));
370 BUG_ON(start & ~HPAGE_MASK);
371 BUG_ON(end & ~HPAGE_MASK);
373 spin_lock(&mm->page_table_lock);
375 /* Update high watermark before we lower rss */
376 update_hiwater_rss(mm);
378 for (address = start; address < end; address += HPAGE_SIZE) {
379 ptep = huge_pte_offset(mm, address);
383 pte = huge_ptep_get_and_clear(mm, address, ptep);
387 page = pte_page(pte);
389 add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
392 spin_unlock(&mm->page_table_lock);
393 flush_tlb_range(vma, start, end);
396 static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
397 unsigned long address, pte_t *ptep, pte_t pte)
399 struct page *old_page, *new_page;
402 old_page = pte_page(pte);
404 /* If no-one else is actually using this page, avoid the copy
405 * and just make the page writable */
406 avoidcopy = (page_count(old_page) == 1);
408 set_huge_ptep_writable(vma, address, ptep);
409 return VM_FAULT_MINOR;
412 page_cache_get(old_page);
413 new_page = alloc_huge_page(vma, address);
416 page_cache_release(old_page);
420 spin_unlock(&mm->page_table_lock);
421 copy_huge_page(new_page, old_page, address);
422 spin_lock(&mm->page_table_lock);
424 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
425 if (likely(pte_same(*ptep, pte))) {
427 set_huge_pte_at(mm, address, ptep,
428 make_huge_pte(vma, new_page, 1));
429 /* Make the old page be freed below */
432 page_cache_release(new_page);
433 page_cache_release(old_page);
434 return VM_FAULT_MINOR;
437 int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
438 unsigned long address, pte_t *ptep, int write_access)
440 int ret = VM_FAULT_SIGBUS;
444 struct address_space *mapping;
447 mapping = vma->vm_file->f_mapping;
448 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
449 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
452 * Use page lock to guard against racing truncation
453 * before we get page_table_lock.
456 page = find_lock_page(mapping, idx);
458 if (hugetlb_get_quota(mapping))
460 page = alloc_huge_page(vma, address);
462 hugetlb_put_quota(mapping);
466 clear_huge_page(page, address);
468 if (vma->vm_flags & VM_SHARED) {
471 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
474 hugetlb_put_quota(mapping);
483 spin_lock(&mm->page_table_lock);
484 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
488 ret = VM_FAULT_MINOR;
489 if (!pte_none(*ptep))
492 add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
493 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
494 && (vma->vm_flags & VM_SHARED)));
495 set_huge_pte_at(mm, address, ptep, new_pte);
497 if (write_access && !(vma->vm_flags & VM_SHARED)) {
498 /* Optimization, do the COW without a second fault */
499 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
502 spin_unlock(&mm->page_table_lock);
508 spin_unlock(&mm->page_table_lock);
509 hugetlb_put_quota(mapping);
515 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
516 unsigned long address, int write_access)
521 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
523 ptep = huge_pte_alloc(mm, address);
528 * Serialize hugepage allocation and instantiation, so that we don't
529 * get spurious allocation failures if two CPUs race to instantiate
530 * the same page in the page cache.
532 mutex_lock(&hugetlb_instantiation_mutex);
534 if (pte_none(entry)) {
535 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
536 mutex_unlock(&hugetlb_instantiation_mutex);
540 ret = VM_FAULT_MINOR;
542 spin_lock(&mm->page_table_lock);
543 /* Check for a racing update before calling hugetlb_cow */
544 if (likely(pte_same(entry, *ptep)))
545 if (write_access && !pte_write(entry))
546 ret = hugetlb_cow(mm, vma, address, ptep, entry);
547 spin_unlock(&mm->page_table_lock);
548 mutex_unlock(&hugetlb_instantiation_mutex);
553 int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
554 struct page **pages, struct vm_area_struct **vmas,
555 unsigned long *position, int *length, int i)
557 unsigned long pfn_offset;
558 unsigned long vaddr = *position;
559 int remainder = *length;
561 spin_lock(&mm->page_table_lock);
562 while (vaddr < vma->vm_end && remainder) {
567 * Some archs (sparc64, sh*) have multiple pte_ts to
568 * each hugepage. We have to make * sure we get the
569 * first, for the page indexing below to work.
571 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
573 if (!pte || pte_none(*pte)) {
576 spin_unlock(&mm->page_table_lock);
577 ret = hugetlb_fault(mm, vma, vaddr, 0);
578 spin_lock(&mm->page_table_lock);
579 if (ret == VM_FAULT_MINOR)
588 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
589 page = pte_page(*pte);
593 pages[i] = page + pfn_offset;
603 if (vaddr < vma->vm_end && remainder &&
604 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
606 * We use pfn_offset to avoid touching the pageframes
607 * of this compound page.
612 spin_unlock(&mm->page_table_lock);
619 void hugetlb_change_protection(struct vm_area_struct *vma,
620 unsigned long address, unsigned long end, pgprot_t newprot)
622 struct mm_struct *mm = vma->vm_mm;
623 unsigned long start = address;
627 BUG_ON(address >= end);
628 flush_cache_range(vma, address, end);
630 spin_lock(&mm->page_table_lock);
631 for (; address < end; address += HPAGE_SIZE) {
632 ptep = huge_pte_offset(mm, address);
635 if (!pte_none(*ptep)) {
636 pte = huge_ptep_get_and_clear(mm, address, ptep);
637 pte = pte_mkhuge(pte_modify(pte, newprot));
638 set_huge_pte_at(mm, address, ptep, pte);
639 lazy_mmu_prot_update(pte);
642 spin_unlock(&mm->page_table_lock);
644 flush_tlb_range(vma, start, end);
648 struct list_head link;
653 static long region_add(struct list_head *head, long f, long t)
655 struct file_region *rg, *nrg, *trg;
657 /* Locate the region we are either in or before. */
658 list_for_each_entry(rg, head, link)
662 /* Round our left edge to the current segment if it encloses us. */
666 /* Check for and consume any regions we now overlap with. */
668 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
669 if (&rg->link == head)
674 /* If this area reaches higher then extend our area to
675 * include it completely. If this is not the first area
676 * which we intend to reuse, free it. */
689 static long region_chg(struct list_head *head, long f, long t)
691 struct file_region *rg, *nrg;
694 /* Locate the region we are before or in. */
695 list_for_each_entry(rg, head, link)
699 /* If we are below the current region then a new region is required.
700 * Subtle, allocate a new region at the position but make it zero
701 * size such that we can guarentee to record the reservation. */
702 if (&rg->link == head || t < rg->from) {
703 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
708 INIT_LIST_HEAD(&nrg->link);
709 list_add(&nrg->link, rg->link.prev);
714 /* Round our left edge to the current segment if it encloses us. */
719 /* Check for and consume any regions we now overlap with. */
720 list_for_each_entry(rg, rg->link.prev, link) {
721 if (&rg->link == head)
726 /* We overlap with this area, if it extends futher than
727 * us then we must extend ourselves. Account for its
728 * existing reservation. */
733 chg -= rg->to - rg->from;
738 static long region_truncate(struct list_head *head, long end)
740 struct file_region *rg, *trg;
743 /* Locate the region we are either in or before. */
744 list_for_each_entry(rg, head, link)
747 if (&rg->link == head)
750 /* If we are in the middle of a region then adjust it. */
751 if (end > rg->from) {
754 rg = list_entry(rg->link.next, typeof(*rg), link);
757 /* Drop any remaining regions. */
758 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
759 if (&rg->link == head)
761 chg += rg->to - rg->from;
768 static int hugetlb_acct_memory(long delta)
772 spin_lock(&hugetlb_lock);
773 if ((delta + resv_huge_pages) <= free_huge_pages) {
774 resv_huge_pages += delta;
777 spin_unlock(&hugetlb_lock);
781 int hugetlb_reserve_pages(struct inode *inode, long from, long to)
785 chg = region_chg(&inode->i_mapping->private_list, from, to);
788 ret = hugetlb_acct_memory(chg);
791 region_add(&inode->i_mapping->private_list, from, to);
795 void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
797 long chg = region_truncate(&inode->i_mapping->private_list, offset);
798 hugetlb_acct_memory(freed - chg);