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, reserved_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,
127 struct inode *inode = vma->vm_file->f_dentry->d_inode;
132 spin_lock(&hugetlb_lock);
134 if (vma->vm_flags & VM_MAYSHARE) {
136 /* idx = radix tree index, i.e. offset into file in
137 * HPAGE_SIZE units */
138 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
139 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
141 /* The hugetlbfs specific inode info stores the number
142 * of "guaranteed available" (huge) pages. That is,
143 * the first 'prereserved_hpages' pages of the inode
144 * are either already instantiated, or have been
145 * pre-reserved (by hugetlb_reserve_for_inode()). Here
146 * we're in the process of instantiating the page, so
147 * we use this to determine whether to draw from the
148 * pre-reserved pool or the truly free pool. */
149 if (idx < HUGETLBFS_I(inode)->prereserved_hpages)
154 if (free_huge_pages <= reserved_huge_pages)
157 BUG_ON(reserved_huge_pages == 0);
158 reserved_huge_pages--;
161 page = dequeue_huge_page(vma, addr);
165 spin_unlock(&hugetlb_lock);
166 set_page_refcounted(page);
170 WARN_ON(use_reserve); /* reserved allocations shouldn't fail */
171 spin_unlock(&hugetlb_lock);
175 /* hugetlb_extend_reservation()
177 * Ensure that at least 'atleast' hugepages are, and will remain,
178 * available to instantiate the first 'atleast' pages of the given
179 * inode. If the inode doesn't already have this many pages reserved
180 * or instantiated, set aside some hugepages in the reserved pool to
181 * satisfy later faults (or fail now if there aren't enough, rather
182 * than getting the SIGBUS later).
184 int hugetlb_extend_reservation(struct hugetlbfs_inode_info *info,
185 unsigned long atleast)
187 struct inode *inode = &info->vfs_inode;
188 unsigned long change_in_reserve = 0;
191 spin_lock(&hugetlb_lock);
192 read_lock_irq(&inode->i_mapping->tree_lock);
194 if (info->prereserved_hpages >= atleast)
197 /* Because we always call this on shared mappings, none of the
198 * pages beyond info->prereserved_hpages can have been
199 * instantiated, so we need to reserve all of them now. */
200 change_in_reserve = atleast - info->prereserved_hpages;
202 if ((reserved_huge_pages + change_in_reserve) > free_huge_pages) {
207 reserved_huge_pages += change_in_reserve;
208 info->prereserved_hpages = atleast;
211 read_unlock_irq(&inode->i_mapping->tree_lock);
212 spin_unlock(&hugetlb_lock);
217 /* hugetlb_truncate_reservation()
219 * This returns pages reserved for the given inode to the general free
220 * hugepage pool. If the inode has any pages prereserved, but not
221 * instantiated, beyond offset (atmost << HPAGE_SIZE), then release
224 void hugetlb_truncate_reservation(struct hugetlbfs_inode_info *info,
225 unsigned long atmost)
227 struct inode *inode = &info->vfs_inode;
228 struct address_space *mapping = inode->i_mapping;
230 unsigned long change_in_reserve = 0;
233 spin_lock(&hugetlb_lock);
234 read_lock_irq(&inode->i_mapping->tree_lock);
236 if (info->prereserved_hpages <= atmost)
239 /* Count pages which were reserved, but not instantiated, and
240 * which we can now release. */
241 for (idx = atmost; idx < info->prereserved_hpages; idx++) {
242 page = radix_tree_lookup(&mapping->page_tree, idx);
244 /* Pages which are already instantiated can't
245 * be unreserved (and in fact have already
246 * been removed from the reserved pool) */
250 BUG_ON(reserved_huge_pages < change_in_reserve);
251 reserved_huge_pages -= change_in_reserve;
252 info->prereserved_hpages = atmost;
255 read_unlock_irq(&inode->i_mapping->tree_lock);
256 spin_unlock(&hugetlb_lock);
259 static int __init hugetlb_init(void)
263 if (HPAGE_SHIFT == 0)
266 for (i = 0; i < MAX_NUMNODES; ++i)
267 INIT_LIST_HEAD(&hugepage_freelists[i]);
269 for (i = 0; i < max_huge_pages; ++i) {
270 if (!alloc_fresh_huge_page())
273 max_huge_pages = free_huge_pages = nr_huge_pages = i;
274 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
277 module_init(hugetlb_init);
279 static int __init hugetlb_setup(char *s)
281 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
285 __setup("hugepages=", hugetlb_setup);
288 static void update_and_free_page(struct page *page)
292 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
293 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
294 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
295 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
296 1 << PG_private | 1<< PG_writeback);
298 page[1].lru.next = NULL;
299 set_page_refcounted(page);
300 __free_pages(page, HUGETLB_PAGE_ORDER);
303 #ifdef CONFIG_HIGHMEM
304 static void try_to_free_low(unsigned long count)
307 for (i = 0; i < MAX_NUMNODES; ++i) {
308 struct page *page, *next;
309 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
310 if (PageHighMem(page))
312 list_del(&page->lru);
313 update_and_free_page(page);
314 nid = page_zone(page)->zone_pgdat->node_id;
316 free_huge_pages_node[nid]--;
317 if (count >= nr_huge_pages)
323 static inline void try_to_free_low(unsigned long count)
328 static unsigned long set_max_huge_pages(unsigned long count)
330 while (count > nr_huge_pages) {
331 if (!alloc_fresh_huge_page())
332 return nr_huge_pages;
334 if (count >= nr_huge_pages)
335 return nr_huge_pages;
337 spin_lock(&hugetlb_lock);
338 count = max(count, reserved_huge_pages);
339 try_to_free_low(count);
340 while (count < nr_huge_pages) {
341 struct page *page = dequeue_huge_page(NULL, 0);
344 update_and_free_page(page);
346 spin_unlock(&hugetlb_lock);
347 return nr_huge_pages;
350 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
351 struct file *file, void __user *buffer,
352 size_t *length, loff_t *ppos)
354 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
355 max_huge_pages = set_max_huge_pages(max_huge_pages);
358 #endif /* CONFIG_SYSCTL */
360 int hugetlb_report_meminfo(char *buf)
363 "HugePages_Total: %5lu\n"
364 "HugePages_Free: %5lu\n"
365 "HugePages_Rsvd: %5lu\n"
366 "Hugepagesize: %5lu kB\n",
373 int hugetlb_report_node_meminfo(int nid, char *buf)
376 "Node %d HugePages_Total: %5u\n"
377 "Node %d HugePages_Free: %5u\n",
378 nid, nr_huge_pages_node[nid],
379 nid, free_huge_pages_node[nid]);
382 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
383 unsigned long hugetlb_total_pages(void)
385 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
389 * We cannot handle pagefaults against hugetlb pages at all. They cause
390 * handle_mm_fault() to try to instantiate regular-sized pages in the
391 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
394 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
395 unsigned long address, int *unused)
401 struct vm_operations_struct hugetlb_vm_ops = {
402 .nopage = hugetlb_nopage,
405 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
412 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
414 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
416 entry = pte_mkyoung(entry);
417 entry = pte_mkhuge(entry);
422 static void set_huge_ptep_writable(struct vm_area_struct *vma,
423 unsigned long address, pte_t *ptep)
427 entry = pte_mkwrite(pte_mkdirty(*ptep));
428 ptep_set_access_flags(vma, address, ptep, entry, 1);
429 update_mmu_cache(vma, address, entry);
430 lazy_mmu_prot_update(entry);
434 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
435 struct vm_area_struct *vma)
437 pte_t *src_pte, *dst_pte, entry;
438 struct page *ptepage;
442 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
444 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
445 src_pte = huge_pte_offset(src, addr);
448 dst_pte = huge_pte_alloc(dst, addr);
451 spin_lock(&dst->page_table_lock);
452 spin_lock(&src->page_table_lock);
453 if (!pte_none(*src_pte)) {
455 ptep_set_wrprotect(src, addr, src_pte);
457 ptepage = pte_page(entry);
459 add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
460 set_huge_pte_at(dst, addr, dst_pte, entry);
462 spin_unlock(&src->page_table_lock);
463 spin_unlock(&dst->page_table_lock);
471 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
474 struct mm_struct *mm = vma->vm_mm;
475 unsigned long address;
480 WARN_ON(!is_vm_hugetlb_page(vma));
481 BUG_ON(start & ~HPAGE_MASK);
482 BUG_ON(end & ~HPAGE_MASK);
484 spin_lock(&mm->page_table_lock);
486 /* Update high watermark before we lower rss */
487 update_hiwater_rss(mm);
489 for (address = start; address < end; address += HPAGE_SIZE) {
490 ptep = huge_pte_offset(mm, address);
494 pte = huge_ptep_get_and_clear(mm, address, ptep);
498 page = pte_page(pte);
500 add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
503 spin_unlock(&mm->page_table_lock);
504 flush_tlb_range(vma, start, end);
507 static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
508 unsigned long address, pte_t *ptep, pte_t pte)
510 struct page *old_page, *new_page;
513 old_page = pte_page(pte);
515 /* If no-one else is actually using this page, avoid the copy
516 * and just make the page writable */
517 avoidcopy = (page_count(old_page) == 1);
519 set_huge_ptep_writable(vma, address, ptep);
520 return VM_FAULT_MINOR;
523 page_cache_get(old_page);
524 new_page = alloc_huge_page(vma, address);
527 page_cache_release(old_page);
531 spin_unlock(&mm->page_table_lock);
532 copy_huge_page(new_page, old_page, address);
533 spin_lock(&mm->page_table_lock);
535 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
536 if (likely(pte_same(*ptep, pte))) {
538 set_huge_pte_at(mm, address, ptep,
539 make_huge_pte(vma, new_page, 1));
540 /* Make the old page be freed below */
543 page_cache_release(new_page);
544 page_cache_release(old_page);
545 return VM_FAULT_MINOR;
548 int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
549 unsigned long address, pte_t *ptep, int write_access)
551 int ret = VM_FAULT_SIGBUS;
555 struct address_space *mapping;
558 mapping = vma->vm_file->f_mapping;
559 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
560 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
563 * Use page lock to guard against racing truncation
564 * before we get page_table_lock.
567 page = find_lock_page(mapping, idx);
569 if (hugetlb_get_quota(mapping))
571 page = alloc_huge_page(vma, address);
573 hugetlb_put_quota(mapping);
577 clear_huge_page(page, address);
579 if (vma->vm_flags & VM_SHARED) {
582 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
585 hugetlb_put_quota(mapping);
594 spin_lock(&mm->page_table_lock);
595 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
599 ret = VM_FAULT_MINOR;
600 if (!pte_none(*ptep))
603 add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
604 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
605 && (vma->vm_flags & VM_SHARED)));
606 set_huge_pte_at(mm, address, ptep, new_pte);
608 if (write_access && !(vma->vm_flags & VM_SHARED)) {
609 /* Optimization, do the COW without a second fault */
610 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
613 spin_unlock(&mm->page_table_lock);
619 spin_unlock(&mm->page_table_lock);
620 hugetlb_put_quota(mapping);
626 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
627 unsigned long address, int write_access)
632 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
634 ptep = huge_pte_alloc(mm, address);
639 * Serialize hugepage allocation and instantiation, so that we don't
640 * get spurious allocation failures if two CPUs race to instantiate
641 * the same page in the page cache.
643 mutex_lock(&hugetlb_instantiation_mutex);
645 if (pte_none(entry)) {
646 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
647 mutex_unlock(&hugetlb_instantiation_mutex);
651 ret = VM_FAULT_MINOR;
653 spin_lock(&mm->page_table_lock);
654 /* Check for a racing update before calling hugetlb_cow */
655 if (likely(pte_same(entry, *ptep)))
656 if (write_access && !pte_write(entry))
657 ret = hugetlb_cow(mm, vma, address, ptep, entry);
658 spin_unlock(&mm->page_table_lock);
659 mutex_unlock(&hugetlb_instantiation_mutex);
664 int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
665 struct page **pages, struct vm_area_struct **vmas,
666 unsigned long *position, int *length, int i)
668 unsigned long pfn_offset;
669 unsigned long vaddr = *position;
670 int remainder = *length;
672 spin_lock(&mm->page_table_lock);
673 while (vaddr < vma->vm_end && remainder) {
678 * Some archs (sparc64, sh*) have multiple pte_ts to
679 * each hugepage. We have to make * sure we get the
680 * first, for the page indexing below to work.
682 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
684 if (!pte || pte_none(*pte)) {
687 spin_unlock(&mm->page_table_lock);
688 ret = hugetlb_fault(mm, vma, vaddr, 0);
689 spin_lock(&mm->page_table_lock);
690 if (ret == VM_FAULT_MINOR)
699 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
700 page = pte_page(*pte);
704 pages[i] = page + pfn_offset;
714 if (vaddr < vma->vm_end && remainder &&
715 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
717 * We use pfn_offset to avoid touching the pageframes
718 * of this compound page.
723 spin_unlock(&mm->page_table_lock);
730 void hugetlb_change_protection(struct vm_area_struct *vma,
731 unsigned long address, unsigned long end, pgprot_t newprot)
733 struct mm_struct *mm = vma->vm_mm;
734 unsigned long start = address;
738 BUG_ON(address >= end);
739 flush_cache_range(vma, address, end);
741 spin_lock(&mm->page_table_lock);
742 for (; address < end; address += HPAGE_SIZE) {
743 ptep = huge_pte_offset(mm, address);
746 if (!pte_none(*ptep)) {
747 pte = huge_ptep_get_and_clear(mm, address, ptep);
748 pte = pte_mkhuge(pte_modify(pte, newprot));
749 set_huge_pte_at(mm, address, ptep, pte);
750 lazy_mmu_prot_update(pte);
753 spin_unlock(&mm->page_table_lock);
755 flush_tlb_range(vma, start, end);