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 unsigned int nr_huge_pages_node[MAX_NUMNODES];
19 static unsigned int free_huge_pages_node[MAX_NUMNODES];
20 static spinlock_t hugetlb_lock = SPIN_LOCK_UNLOCKED;
22 static void enqueue_huge_page(struct page *page)
24 int nid = page_to_nid(page);
25 list_add(&page->lru, &hugepage_freelists[nid]);
27 free_huge_pages_node[nid]++;
30 static struct page *dequeue_huge_page(void)
32 int nid = numa_node_id();
33 struct page *page = NULL;
35 if (list_empty(&hugepage_freelists[nid])) {
36 for (nid = 0; nid < MAX_NUMNODES; ++nid)
37 if (!list_empty(&hugepage_freelists[nid]))
40 if (nid >= 0 && nid < MAX_NUMNODES &&
41 !list_empty(&hugepage_freelists[nid])) {
42 page = list_entry(hugepage_freelists[nid].next,
46 free_huge_pages_node[nid]--;
51 static struct page *alloc_fresh_huge_page(void)
55 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
57 nid = (nid + 1) % numnodes;
60 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]++;
65 void free_huge_page(struct page *page)
67 BUG_ON(page_count(page));
69 INIT_LIST_HEAD(&page->lru);
70 page[1].mapping = NULL;
72 spin_lock(&hugetlb_lock);
73 enqueue_huge_page(page);
74 spin_unlock(&hugetlb_lock);
77 struct page *alloc_huge_page(void)
82 spin_lock(&hugetlb_lock);
83 page = dequeue_huge_page();
85 spin_unlock(&hugetlb_lock);
88 spin_unlock(&hugetlb_lock);
89 set_page_count(page, 1);
90 page[1].mapping = (void *)free_huge_page;
91 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
92 clear_highpage(&page[i]);
96 static int __init hugetlb_init(void)
101 for (i = 0; i < MAX_NUMNODES; ++i)
102 INIT_LIST_HEAD(&hugepage_freelists[i]);
104 for (i = 0; i < max_huge_pages; ++i) {
105 page = alloc_fresh_huge_page();
108 spin_lock(&hugetlb_lock);
109 enqueue_huge_page(page);
110 spin_unlock(&hugetlb_lock);
112 max_huge_pages = free_huge_pages = nr_huge_pages = i;
113 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
116 module_init(hugetlb_init);
118 static int __init hugetlb_setup(char *s)
120 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
124 __setup("hugepages=", hugetlb_setup);
127 static void update_and_free_page(struct page *page)
131 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
132 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
133 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
134 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
135 1 << PG_private | 1<< PG_writeback);
136 set_page_count(&page[i], 0);
138 set_page_count(page, 1);
139 __free_pages(page, HUGETLB_PAGE_ORDER);
142 #ifdef CONFIG_HIGHMEM
143 static void try_to_free_low(unsigned long count)
146 for (i = 0; i < MAX_NUMNODES; ++i) {
147 struct page *page, *next;
148 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
149 if (PageHighMem(page))
151 list_del(&page->lru);
152 update_and_free_page(page);
153 nid = page_zone(page)->zone_pgdat->node_id;
155 free_huge_pages_node[nid]--;
156 if (count >= nr_huge_pages)
162 static inline void try_to_free_low(unsigned long count)
167 static unsigned long set_max_huge_pages(unsigned long count)
169 while (count > nr_huge_pages) {
170 struct page *page = alloc_fresh_huge_page();
172 return nr_huge_pages;
173 spin_lock(&hugetlb_lock);
174 enqueue_huge_page(page);
175 spin_unlock(&hugetlb_lock);
177 if (count >= nr_huge_pages)
178 return nr_huge_pages;
180 spin_lock(&hugetlb_lock);
181 try_to_free_low(count);
182 while (count < nr_huge_pages) {
183 struct page *page = dequeue_huge_page();
186 update_and_free_page(page);
188 spin_unlock(&hugetlb_lock);
189 return nr_huge_pages;
192 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
193 struct file *file, void __user *buffer,
194 size_t *length, loff_t *ppos)
196 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
197 max_huge_pages = set_max_huge_pages(max_huge_pages);
200 #endif /* CONFIG_SYSCTL */
202 int hugetlb_report_meminfo(char *buf)
205 "HugePages_Total: %5lu\n"
206 "HugePages_Free: %5lu\n"
207 "Hugepagesize: %5lu kB\n",
213 int hugetlb_report_node_meminfo(int nid, char *buf)
216 "Node %d HugePages_Total: %5u\n"
217 "Node %d HugePages_Free: %5u\n",
218 nid, nr_huge_pages_node[nid],
219 nid, free_huge_pages_node[nid]);
222 int is_hugepage_mem_enough(size_t size)
224 return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
227 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
228 unsigned long hugetlb_total_pages(void)
230 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
232 EXPORT_SYMBOL(hugetlb_total_pages);
235 * We cannot handle pagefaults against hugetlb pages at all. They cause
236 * handle_mm_fault() to try to instantiate regular-sized pages in the
237 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
240 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
241 unsigned long address, int *unused)
247 struct vm_operations_struct hugetlb_vm_ops = {
248 .nopage = hugetlb_nopage,
251 void zap_hugepage_range(struct vm_area_struct *vma,
252 unsigned long start, unsigned long length)
254 struct mm_struct *mm = vma->vm_mm;
256 spin_lock(&mm->page_table_lock);
257 unmap_hugepage_range(vma, start, start + length);
258 spin_unlock(&mm->page_table_lock);