ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / mm / hugetlb.c
1 /*
2  * Generic hugetlb support.
3  * (C) William Irwin, April 2004
4  */
5 #include <linux/gfp.h>
6 #include <linux/list.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/hugetlb.h>
11 #include <linux/sysctl.h>
12 #include <linux/highmem.h>
13
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;
19
20 static void enqueue_huge_page(struct page *page)
21 {
22         list_add(&page->lru,
23                  &hugepage_freelists[page_zone(page)->zone_pgdat->node_id]);
24 }
25
26 static struct page *dequeue_huge_page(void)
27 {
28         int nid = numa_node_id();
29         struct page *page = NULL;
30
31         if (list_empty(&hugepage_freelists[nid])) {
32                 for (nid = 0; nid < MAX_NUMNODES; ++nid)
33                         if (!list_empty(&hugepage_freelists[nid]))
34                                 break;
35         }
36         if (nid >= 0 && nid < MAX_NUMNODES &&
37             !list_empty(&hugepage_freelists[nid])) {
38                 page = list_entry(hugepage_freelists[nid].next,
39                                   struct page, lru);
40                 list_del(&page->lru);
41         }
42         return page;
43 }
44
45 static struct page *alloc_fresh_huge_page(void)
46 {
47         static int nid = 0;
48         struct page *page;
49         page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP,
50                                         HUGETLB_PAGE_ORDER);
51         nid = (nid + 1) % numnodes;
52         return page;
53 }
54
55 void free_huge_page(struct page *page)
56 {
57         BUG_ON(page_count(page));
58
59         INIT_LIST_HEAD(&page->lru);
60
61         spin_lock(&hugetlb_lock);
62         enqueue_huge_page(page);
63         free_huge_pages++;
64         spin_unlock(&hugetlb_lock);
65 }
66
67 struct page *alloc_huge_page(void)
68 {
69         struct page *page;
70         int i;
71
72         spin_lock(&hugetlb_lock);
73         page = dequeue_huge_page();
74         if (!page) {
75                 spin_unlock(&hugetlb_lock);
76                 return NULL;
77         }
78         free_huge_pages--;
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]);
84         return page;
85 }
86
87 static int __init hugetlb_init(void)
88 {
89         unsigned long i;
90         struct page *page;
91
92         for (i = 0; i < MAX_NUMNODES; ++i)
93                 INIT_LIST_HEAD(&hugepage_freelists[i]);
94
95         for (i = 0; i < max_huge_pages; ++i) {
96                 page = alloc_fresh_huge_page();
97                 if (!page)
98                         break;
99                 spin_lock(&hugetlb_lock);
100                 enqueue_huge_page(page);
101                 spin_unlock(&hugetlb_lock);
102         }
103         max_huge_pages = free_huge_pages = nr_huge_pages = i;
104         printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
105         return 0;
106 }
107 module_init(hugetlb_init);
108
109 static int __init hugetlb_setup(char *s)
110 {
111         if (sscanf(s, "%lu", &max_huge_pages) <= 0)
112                 max_huge_pages = 0;
113         return 1;
114 }
115 __setup("hugepages=", hugetlb_setup);
116
117 static void update_and_free_page(struct page *page)
118 {
119         int i;
120         nr_huge_pages--;
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);
126         }
127         set_page_count(page, 1);
128         __free_pages(page, HUGETLB_PAGE_ORDER);
129 }
130
131 #ifdef CONFIG_HIGHMEM
132 static int try_to_free_low(unsigned long count)
133 {
134         int i;
135         for (i = 0; i < MAX_NUMNODES; ++i) {
136                 struct page *page;
137                 list_for_each_entry(page, &hugepage_freelists[i], lru) {
138                         if (PageHighMem(page))
139                                 continue;
140                         list_del(&page->lru);
141                         update_and_free_page(page);
142                         --free_huge_pages;
143                         if (!--count)
144                                 return 0;
145                 }
146         }
147         return count;
148 }
149 #else
150 static inline int try_to_free_low(unsigned long count)
151 {
152         return count;
153 }
154 #endif
155
156 static unsigned long set_max_huge_pages(unsigned long count)
157 {
158         while (count > nr_huge_pages) {
159                 struct page *page = alloc_fresh_huge_page();
160                 if (!page)
161                         return nr_huge_pages;
162                 spin_lock(&hugetlb_lock);
163                 enqueue_huge_page(page);
164                 free_huge_pages++;
165                 nr_huge_pages++;
166                 spin_unlock(&hugetlb_lock);
167         }
168         if (count >= nr_huge_pages)
169                 return nr_huge_pages;
170
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();
174                 if (!page)
175                         break;
176                 update_and_free_page(page);
177         }
178         spin_unlock(&hugetlb_lock);
179         return nr_huge_pages;
180 }
181
182 #ifdef CONFIG_SYSCTL
183 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
184                            struct file *file, void *buffer, size_t *length)
185 {
186         proc_doulongvec_minmax(table, write, file, buffer, length);
187         max_huge_pages = set_max_huge_pages(max_huge_pages);
188         return 0;
189 }
190 #endif /* CONFIG_SYSCTL */
191
192 int hugetlb_report_meminfo(char *buf)
193 {
194         return sprintf(buf,
195                         "HugePages_Total: %5lu\n"
196                         "HugePages_Free:  %5lu\n"
197                         "Hugepagesize:    %5lu kB\n",
198                         nr_huge_pages,
199                         free_huge_pages,
200                         HPAGE_SIZE/1024);
201 }
202
203 int is_hugepage_mem_enough(size_t size)
204 {
205         return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
206 }
207
208 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
209 unsigned long hugetlb_total_pages(void)
210 {
211         return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
212 }
213 EXPORT_SYMBOL(hugetlb_total_pages);
214
215 /*
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
219  * this far.
220  */
221 static struct page *hugetlb_nopage(struct vm_area_struct *vma,
222                                 unsigned long address, int *unused)
223 {
224         BUG();
225         return NULL;
226 }
227
228 struct vm_operations_struct hugetlb_vm_ops = {
229         .nopage = hugetlb_nopage,
230 };
231
232 void zap_hugepage_range(struct vm_area_struct *vma,
233                         unsigned long start, unsigned long length)
234 {
235         struct mm_struct *mm = vma->vm_mm;
236
237         spin_lock(&mm->page_table_lock);
238         unmap_hugepage_range(vma, start, start + length);
239         spin_unlock(&mm->page_table_lock);
240 }