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/hugetlb.h>
11 #include <linux/sysctl.h>
12 #include <linux/highmem.h>
14 const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
15 static unsigned long nr_huge_pages, free_huge_pages;
16 unsigned long max_huge_pages;
17 static struct list_head hugepage_freelists[MAX_NUMNODES];
18 static spinlock_t hugetlb_lock = SPIN_LOCK_UNLOCKED;
20 static void enqueue_huge_page(struct page *page)
23 &hugepage_freelists[page_zone(page)->zone_pgdat->node_id]);
26 static struct page *dequeue_huge_page(void)
28 int nid = numa_node_id();
29 struct page *page = NULL;
31 if (list_empty(&hugepage_freelists[nid])) {
32 for (nid = 0; nid < MAX_NUMNODES; ++nid)
33 if (!list_empty(&hugepage_freelists[nid]))
36 if (nid >= 0 && nid < MAX_NUMNODES &&
37 !list_empty(&hugepage_freelists[nid])) {
38 page = list_entry(hugepage_freelists[nid].next,
45 static struct page *alloc_fresh_huge_page(void)
49 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP,
51 nid = (nid + 1) % numnodes;
55 void free_huge_page(struct page *page)
57 BUG_ON(page_count(page));
59 INIT_LIST_HEAD(&page->lru);
61 spin_lock(&hugetlb_lock);
62 enqueue_huge_page(page);
64 spin_unlock(&hugetlb_lock);
67 struct page *alloc_huge_page(void)
72 spin_lock(&hugetlb_lock);
73 page = dequeue_huge_page();
75 spin_unlock(&hugetlb_lock);
79 spin_unlock(&hugetlb_lock);
80 set_page_count(page, 1);
81 page[1].mapping = (void *)free_huge_page;
82 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
83 clear_highpage(&page[i]);
87 static int __init hugetlb_init(void)
92 for (i = 0; i < MAX_NUMNODES; ++i)
93 INIT_LIST_HEAD(&hugepage_freelists[i]);
95 for (i = 0; i < max_huge_pages; ++i) {
96 page = alloc_fresh_huge_page();
99 spin_lock(&hugetlb_lock);
100 enqueue_huge_page(page);
101 spin_unlock(&hugetlb_lock);
103 max_huge_pages = free_huge_pages = nr_huge_pages = i;
104 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
107 module_init(hugetlb_init);
109 static int __init hugetlb_setup(char *s)
111 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
115 __setup("hugepages=", hugetlb_setup);
117 static void update_and_free_page(struct page *page)
121 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
122 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
123 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
124 1 << PG_private | 1<< PG_writeback);
125 set_page_count(&page[i], 0);
127 set_page_count(page, 1);
128 __free_pages(page, HUGETLB_PAGE_ORDER);
131 #ifdef CONFIG_HIGHMEM
132 static int try_to_free_low(unsigned long count)
135 for (i = 0; i < MAX_NUMNODES; ++i) {
137 list_for_each_entry(page, &hugepage_freelists[i], lru) {
138 if (PageHighMem(page))
140 list_del(&page->lru);
141 update_and_free_page(page);
150 static inline int try_to_free_low(unsigned long count)
156 static unsigned long set_max_huge_pages(unsigned long count)
158 while (count > nr_huge_pages) {
159 struct page *page = alloc_fresh_huge_page();
161 return nr_huge_pages;
162 spin_lock(&hugetlb_lock);
163 enqueue_huge_page(page);
166 spin_unlock(&hugetlb_lock);
168 if (count >= nr_huge_pages)
169 return nr_huge_pages;
171 spin_lock(&hugetlb_lock);
172 for (count = try_to_free_low(count); count < nr_huge_pages; --free_huge_pages) {
173 struct page *page = dequeue_huge_page();
176 update_and_free_page(page);
178 spin_unlock(&hugetlb_lock);
179 return nr_huge_pages;
183 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
184 struct file *file, void *buffer, size_t *length)
186 proc_doulongvec_minmax(table, write, file, buffer, length);
187 max_huge_pages = set_max_huge_pages(max_huge_pages);
190 #endif /* CONFIG_SYSCTL */
192 int hugetlb_report_meminfo(char *buf)
195 "HugePages_Total: %5lu\n"
196 "HugePages_Free: %5lu\n"
197 "Hugepagesize: %5lu kB\n",
203 int is_hugepage_mem_enough(size_t size)
205 return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
208 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
209 unsigned long hugetlb_total_pages(void)
211 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
213 EXPORT_SYMBOL(hugetlb_total_pages);
216 * We cannot handle pagefaults against hugetlb pages at all. They cause
217 * handle_mm_fault() to try to instantiate regular-sized pages in the
218 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
221 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
222 unsigned long address, int *unused)
228 struct vm_operations_struct hugetlb_vm_ops = {
229 .nopage = hugetlb_nopage,
232 void zap_hugepage_range(struct vm_area_struct *vma,
233 unsigned long start, unsigned long length)
235 struct mm_struct *mm = vma->vm_mm;
237 spin_lock(&mm->page_table_lock);
238 unmap_hugepage_range(vma, start, start + length);
239 spin_unlock(&mm->page_table_lock);