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>
17 #include <asm/pgtable.h>
19 #include <linux/hugetlb.h>
21 const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
22 static unsigned long nr_huge_pages, free_huge_pages;
23 unsigned long max_huge_pages;
24 static struct list_head hugepage_freelists[MAX_NUMNODES];
25 static unsigned int nr_huge_pages_node[MAX_NUMNODES];
26 static unsigned int free_huge_pages_node[MAX_NUMNODES];
29 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
31 static DEFINE_SPINLOCK(hugetlb_lock);
33 static void enqueue_huge_page(struct page *page)
35 int nid = page_to_nid(page);
36 list_add(&page->lru, &hugepage_freelists[nid]);
38 free_huge_pages_node[nid]++;
41 static struct page *dequeue_huge_page(struct vm_area_struct *vma,
42 unsigned long address)
44 int nid = numa_node_id();
45 struct page *page = NULL;
46 struct zonelist *zonelist = huge_zonelist(vma, address);
49 for (z = zonelist->zones; *z; z++) {
50 nid = (*z)->zone_pgdat->node_id;
51 if (!list_empty(&hugepage_freelists[nid]))
56 page = list_entry(hugepage_freelists[nid].next,
60 free_huge_pages_node[nid]--;
65 static struct page *alloc_fresh_huge_page(void)
69 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
71 nid = (nid + 1) % num_online_nodes();
73 spin_lock(&hugetlb_lock);
75 nr_huge_pages_node[page_to_nid(page)]++;
76 spin_unlock(&hugetlb_lock);
81 void free_huge_page(struct page *page)
83 BUG_ON(page_count(page));
85 INIT_LIST_HEAD(&page->lru);
86 page[1].mapping = NULL;
88 spin_lock(&hugetlb_lock);
89 enqueue_huge_page(page);
90 spin_unlock(&hugetlb_lock);
93 struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr)
98 spin_lock(&hugetlb_lock);
99 page = dequeue_huge_page(vma, addr);
101 spin_unlock(&hugetlb_lock);
104 spin_unlock(&hugetlb_lock);
105 set_page_count(page, 1);
106 page[1].mapping = (void *)free_huge_page;
107 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
108 clear_highpage(&page[i]);
112 static int __init hugetlb_init(void)
117 if (HPAGE_SHIFT == 0)
120 for (i = 0; i < MAX_NUMNODES; ++i)
121 INIT_LIST_HEAD(&hugepage_freelists[i]);
123 for (i = 0; i < max_huge_pages; ++i) {
124 page = alloc_fresh_huge_page();
127 spin_lock(&hugetlb_lock);
128 enqueue_huge_page(page);
129 spin_unlock(&hugetlb_lock);
131 max_huge_pages = free_huge_pages = nr_huge_pages = i;
132 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
135 module_init(hugetlb_init);
137 static int __init hugetlb_setup(char *s)
139 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
143 __setup("hugepages=", hugetlb_setup);
146 static void update_and_free_page(struct page *page)
150 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
151 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
152 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
153 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
154 1 << PG_private | 1<< PG_writeback);
155 set_page_count(&page[i], 0);
157 set_page_count(page, 1);
158 __free_pages(page, HUGETLB_PAGE_ORDER);
161 #ifdef CONFIG_HIGHMEM
162 static void try_to_free_low(unsigned long count)
165 for (i = 0; i < MAX_NUMNODES; ++i) {
166 struct page *page, *next;
167 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
168 if (PageHighMem(page))
170 list_del(&page->lru);
171 update_and_free_page(page);
172 nid = page_zone(page)->zone_pgdat->node_id;
174 free_huge_pages_node[nid]--;
175 if (count >= nr_huge_pages)
181 static inline void try_to_free_low(unsigned long count)
186 static unsigned long set_max_huge_pages(unsigned long count)
188 while (count > nr_huge_pages) {
189 struct page *page = alloc_fresh_huge_page();
191 return nr_huge_pages;
192 spin_lock(&hugetlb_lock);
193 enqueue_huge_page(page);
194 spin_unlock(&hugetlb_lock);
196 if (count >= nr_huge_pages)
197 return nr_huge_pages;
199 spin_lock(&hugetlb_lock);
200 try_to_free_low(count);
201 while (count < nr_huge_pages) {
202 struct page *page = dequeue_huge_page(NULL, 0);
205 update_and_free_page(page);
207 spin_unlock(&hugetlb_lock);
208 return nr_huge_pages;
211 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
212 struct file *file, void __user *buffer,
213 size_t *length, loff_t *ppos)
215 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
216 max_huge_pages = set_max_huge_pages(max_huge_pages);
219 #endif /* CONFIG_SYSCTL */
221 int hugetlb_report_meminfo(char *buf)
224 "HugePages_Total: %5lu\n"
225 "HugePages_Free: %5lu\n"
226 "Hugepagesize: %5lu kB\n",
232 int hugetlb_report_node_meminfo(int nid, char *buf)
235 "Node %d HugePages_Total: %5u\n"
236 "Node %d HugePages_Free: %5u\n",
237 nid, nr_huge_pages_node[nid],
238 nid, free_huge_pages_node[nid]);
241 int is_hugepage_mem_enough(size_t size)
243 return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
246 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
247 unsigned long hugetlb_total_pages(void)
249 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
253 * We cannot handle pagefaults against hugetlb pages at all. They cause
254 * handle_mm_fault() to try to instantiate regular-sized pages in the
255 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
258 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
259 unsigned long address, int *unused)
265 struct vm_operations_struct hugetlb_vm_ops = {
266 .nopage = hugetlb_nopage,
269 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
276 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
278 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
280 entry = pte_mkyoung(entry);
281 entry = pte_mkhuge(entry);
286 static void set_huge_ptep_writable(struct vm_area_struct *vma,
287 unsigned long address, pte_t *ptep)
291 entry = pte_mkwrite(pte_mkdirty(*ptep));
292 ptep_set_access_flags(vma, address, ptep, entry, 1);
293 update_mmu_cache(vma, address, entry);
294 lazy_mmu_prot_update(entry);
298 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
299 struct vm_area_struct *vma)
301 pte_t *src_pte, *dst_pte, entry;
302 struct page *ptepage;
306 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
308 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
309 src_pte = huge_pte_offset(src, addr);
312 dst_pte = huge_pte_alloc(dst, addr);
315 spin_lock(&dst->page_table_lock);
316 spin_lock(&src->page_table_lock);
317 if (!pte_none(*src_pte)) {
319 ptep_set_wrprotect(src, addr, src_pte);
321 ptepage = pte_page(entry);
323 add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
324 set_huge_pte_at(dst, addr, dst_pte, entry);
326 spin_unlock(&src->page_table_lock);
327 spin_unlock(&dst->page_table_lock);
335 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
338 struct mm_struct *mm = vma->vm_mm;
339 unsigned long address;
344 WARN_ON(!is_vm_hugetlb_page(vma));
345 BUG_ON(start & ~HPAGE_MASK);
346 BUG_ON(end & ~HPAGE_MASK);
348 spin_lock(&mm->page_table_lock);
350 /* Update high watermark before we lower rss */
351 update_hiwater_rss(mm);
353 for (address = start; address < end; address += HPAGE_SIZE) {
354 ptep = huge_pte_offset(mm, address);
358 pte = huge_ptep_get_and_clear(mm, address, ptep);
362 page = pte_page(pte);
364 add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
367 spin_unlock(&mm->page_table_lock);
368 flush_tlb_range(vma, start, end);
371 static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
372 unsigned long address, pte_t *ptep, pte_t pte)
374 struct page *old_page, *new_page;
377 old_page = pte_page(pte);
379 /* If no-one else is actually using this page, avoid the copy
380 * and just make the page writable */
381 avoidcopy = (page_count(old_page) == 1);
383 set_huge_ptep_writable(vma, address, ptep);
384 return VM_FAULT_MINOR;
387 page_cache_get(old_page);
388 new_page = alloc_huge_page(vma, address);
391 page_cache_release(old_page);
393 /* Logically this is OOM, not a SIGBUS, but an OOM
394 * could cause the kernel to go killing other
395 * processes which won't help the hugepage situation
397 return VM_FAULT_SIGBUS;
400 spin_unlock(&mm->page_table_lock);
401 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++)
402 copy_user_highpage(new_page + i, old_page + i,
403 address + i*PAGE_SIZE);
404 spin_lock(&mm->page_table_lock);
406 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
407 if (likely(pte_same(*ptep, pte))) {
409 set_huge_pte_at(mm, address, ptep,
410 make_huge_pte(vma, new_page, 1));
411 /* Make the old page be freed below */
414 page_cache_release(new_page);
415 page_cache_release(old_page);
416 return VM_FAULT_MINOR;
419 int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
420 unsigned long address, pte_t *ptep, int write_access)
422 int ret = VM_FAULT_SIGBUS;
426 struct address_space *mapping;
429 mapping = vma->vm_file->f_mapping;
430 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
431 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
434 * Use page lock to guard against racing truncation
435 * before we get page_table_lock.
438 page = find_lock_page(mapping, idx);
440 if (hugetlb_get_quota(mapping))
442 page = alloc_huge_page(vma, address);
444 hugetlb_put_quota(mapping);
448 if (vma->vm_flags & VM_SHARED) {
451 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
454 hugetlb_put_quota(mapping);
463 spin_lock(&mm->page_table_lock);
464 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
468 ret = VM_FAULT_MINOR;
469 if (!pte_none(*ptep))
472 add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
473 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
474 && (vma->vm_flags & VM_SHARED)));
475 set_huge_pte_at(mm, address, ptep, new_pte);
477 if (write_access && !(vma->vm_flags & VM_SHARED)) {
478 /* Optimization, do the COW without a second fault */
479 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
482 spin_unlock(&mm->page_table_lock);
488 spin_unlock(&mm->page_table_lock);
489 hugetlb_put_quota(mapping);
495 int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
496 unsigned long address, int write_access)
502 ptep = huge_pte_alloc(mm, address);
508 return hugetlb_no_page(mm, vma, address, ptep, write_access);
510 ret = VM_FAULT_MINOR;
512 spin_lock(&mm->page_table_lock);
513 /* Check for a racing update before calling hugetlb_cow */
514 if (likely(pte_same(entry, *ptep)))
515 if (write_access && !pte_write(entry))
516 ret = hugetlb_cow(mm, vma, address, ptep, entry);
517 spin_unlock(&mm->page_table_lock);
522 int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
523 struct page **pages, struct vm_area_struct **vmas,
524 unsigned long *position, int *length, int i)
526 unsigned long vpfn, vaddr = *position;
527 int remainder = *length;
529 vpfn = vaddr/PAGE_SIZE;
530 spin_lock(&mm->page_table_lock);
531 while (vaddr < vma->vm_end && remainder) {
536 * Some archs (sparc64, sh*) have multiple pte_ts to
537 * each hugepage. We have to make * sure we get the
538 * first, for the page indexing below to work.
540 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
542 if (!pte || pte_none(*pte)) {
545 spin_unlock(&mm->page_table_lock);
546 ret = hugetlb_fault(mm, vma, vaddr, 0);
547 spin_lock(&mm->page_table_lock);
548 if (ret == VM_FAULT_MINOR)
558 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
571 spin_unlock(&mm->page_table_lock);