[PATCH] remove set_page_count(page, 0) users (outside mm)
[linux-2.6-block.git] / mm / hugetlb.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
10#include <linux/sysctl.h>
11#include <linux/highmem.h>
12#include <linux/nodemask.h>
63551ae0 13#include <linux/pagemap.h>
5da7ca86 14#include <linux/mempolicy.h>
aea47ff3 15#include <linux/cpuset.h>
5da7ca86 16
63551ae0
DG
17#include <asm/page.h>
18#include <asm/pgtable.h>
19
20#include <linux/hugetlb.h>
1da177e4
LT
21
22const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
23static unsigned long nr_huge_pages, free_huge_pages;
24unsigned long max_huge_pages;
25static struct list_head hugepage_freelists[MAX_NUMNODES];
26static unsigned int nr_huge_pages_node[MAX_NUMNODES];
27static unsigned int free_huge_pages_node[MAX_NUMNODES];
0bd0f9fb
EP
28
29/*
30 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
31 */
1da177e4
LT
32static DEFINE_SPINLOCK(hugetlb_lock);
33
34static void enqueue_huge_page(struct page *page)
35{
36 int nid = page_to_nid(page);
37 list_add(&page->lru, &hugepage_freelists[nid]);
38 free_huge_pages++;
39 free_huge_pages_node[nid]++;
40}
41
5da7ca86
CL
42static struct page *dequeue_huge_page(struct vm_area_struct *vma,
43 unsigned long address)
1da177e4
LT
44{
45 int nid = numa_node_id();
46 struct page *page = NULL;
5da7ca86 47 struct zonelist *zonelist = huge_zonelist(vma, address);
96df9333 48 struct zone **z;
1da177e4 49
96df9333
CL
50 for (z = zonelist->zones; *z; z++) {
51 nid = (*z)->zone_pgdat->node_id;
aea47ff3
CL
52 if (cpuset_zone_allowed(*z, GFP_HIGHUSER) &&
53 !list_empty(&hugepage_freelists[nid]))
96df9333 54 break;
1da177e4 55 }
96df9333
CL
56
57 if (*z) {
1da177e4
LT
58 page = list_entry(hugepage_freelists[nid].next,
59 struct page, lru);
60 list_del(&page->lru);
61 free_huge_pages--;
62 free_huge_pages_node[nid]--;
63 }
64 return page;
65}
66
a482289d 67static int alloc_fresh_huge_page(void)
1da177e4
LT
68{
69 static int nid = 0;
70 struct page *page;
71 page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN,
72 HUGETLB_PAGE_ORDER);
73 nid = (nid + 1) % num_online_nodes();
74 if (page) {
a482289d 75 page[1].lru.next = (void *)free_huge_page; /* dtor */
0bd0f9fb 76 spin_lock(&hugetlb_lock);
1da177e4
LT
77 nr_huge_pages++;
78 nr_huge_pages_node[page_to_nid(page)]++;
0bd0f9fb 79 spin_unlock(&hugetlb_lock);
a482289d
NP
80 put_page(page); /* free it into the hugepage allocator */
81 return 1;
1da177e4 82 }
a482289d 83 return 0;
1da177e4
LT
84}
85
86void free_huge_page(struct page *page)
87{
88 BUG_ON(page_count(page));
89
90 INIT_LIST_HEAD(&page->lru);
1da177e4
LT
91
92 spin_lock(&hugetlb_lock);
93 enqueue_huge_page(page);
94 spin_unlock(&hugetlb_lock);
95}
96
5da7ca86 97struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr)
1da177e4
LT
98{
99 struct page *page;
100 int i;
101
102 spin_lock(&hugetlb_lock);
5da7ca86 103 page = dequeue_huge_page(vma, addr);
1da177e4
LT
104 if (!page) {
105 spin_unlock(&hugetlb_lock);
106 return NULL;
107 }
108 spin_unlock(&hugetlb_lock);
109 set_page_count(page, 1);
1da177e4 110 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i)
a2dfef69 111 clear_user_highpage(&page[i], addr);
1da177e4
LT
112 return page;
113}
114
115static int __init hugetlb_init(void)
116{
117 unsigned long i;
1da177e4 118
3c726f8d
BH
119 if (HPAGE_SHIFT == 0)
120 return 0;
121
1da177e4
LT
122 for (i = 0; i < MAX_NUMNODES; ++i)
123 INIT_LIST_HEAD(&hugepage_freelists[i]);
124
125 for (i = 0; i < max_huge_pages; ++i) {
a482289d 126 if (!alloc_fresh_huge_page())
1da177e4 127 break;
1da177e4
LT
128 }
129 max_huge_pages = free_huge_pages = nr_huge_pages = i;
130 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
131 return 0;
132}
133module_init(hugetlb_init);
134
135static int __init hugetlb_setup(char *s)
136{
137 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
138 max_huge_pages = 0;
139 return 1;
140}
141__setup("hugepages=", hugetlb_setup);
142
143#ifdef CONFIG_SYSCTL
144static void update_and_free_page(struct page *page)
145{
146 int i;
147 nr_huge_pages--;
148 nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--;
149 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
150 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
151 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
152 1 << PG_private | 1<< PG_writeback);
1da177e4 153 }
a482289d 154 page[1].lru.next = NULL;
1da177e4
LT
155 set_page_count(page, 1);
156 __free_pages(page, HUGETLB_PAGE_ORDER);
157}
158
159#ifdef CONFIG_HIGHMEM
160static void try_to_free_low(unsigned long count)
161{
162 int i, nid;
163 for (i = 0; i < MAX_NUMNODES; ++i) {
164 struct page *page, *next;
165 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
166 if (PageHighMem(page))
167 continue;
168 list_del(&page->lru);
169 update_and_free_page(page);
170 nid = page_zone(page)->zone_pgdat->node_id;
171 free_huge_pages--;
172 free_huge_pages_node[nid]--;
173 if (count >= nr_huge_pages)
174 return;
175 }
176 }
177}
178#else
179static inline void try_to_free_low(unsigned long count)
180{
181}
182#endif
183
184static unsigned long set_max_huge_pages(unsigned long count)
185{
186 while (count > nr_huge_pages) {
a482289d 187 if (!alloc_fresh_huge_page())
1da177e4 188 return nr_huge_pages;
1da177e4
LT
189 }
190 if (count >= nr_huge_pages)
191 return nr_huge_pages;
192
193 spin_lock(&hugetlb_lock);
194 try_to_free_low(count);
195 while (count < nr_huge_pages) {
5da7ca86 196 struct page *page = dequeue_huge_page(NULL, 0);
1da177e4
LT
197 if (!page)
198 break;
199 update_and_free_page(page);
200 }
201 spin_unlock(&hugetlb_lock);
202 return nr_huge_pages;
203}
204
205int hugetlb_sysctl_handler(struct ctl_table *table, int write,
206 struct file *file, void __user *buffer,
207 size_t *length, loff_t *ppos)
208{
209 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
210 max_huge_pages = set_max_huge_pages(max_huge_pages);
211 return 0;
212}
213#endif /* CONFIG_SYSCTL */
214
215int hugetlb_report_meminfo(char *buf)
216{
217 return sprintf(buf,
218 "HugePages_Total: %5lu\n"
219 "HugePages_Free: %5lu\n"
220 "Hugepagesize: %5lu kB\n",
221 nr_huge_pages,
222 free_huge_pages,
223 HPAGE_SIZE/1024);
224}
225
226int hugetlb_report_node_meminfo(int nid, char *buf)
227{
228 return sprintf(buf,
229 "Node %d HugePages_Total: %5u\n"
230 "Node %d HugePages_Free: %5u\n",
231 nid, nr_huge_pages_node[nid],
232 nid, free_huge_pages_node[nid]);
233}
234
235int is_hugepage_mem_enough(size_t size)
236{
237 return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages;
238}
239
240/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
241unsigned long hugetlb_total_pages(void)
242{
243 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
244}
1da177e4
LT
245
246/*
247 * We cannot handle pagefaults against hugetlb pages at all. They cause
248 * handle_mm_fault() to try to instantiate regular-sized pages in the
249 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
250 * this far.
251 */
252static struct page *hugetlb_nopage(struct vm_area_struct *vma,
253 unsigned long address, int *unused)
254{
255 BUG();
256 return NULL;
257}
258
259struct vm_operations_struct hugetlb_vm_ops = {
260 .nopage = hugetlb_nopage,
261};
262
1e8f889b
DG
263static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
264 int writable)
63551ae0
DG
265{
266 pte_t entry;
267
1e8f889b 268 if (writable) {
63551ae0
DG
269 entry =
270 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
271 } else {
272 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
273 }
274 entry = pte_mkyoung(entry);
275 entry = pte_mkhuge(entry);
276
277 return entry;
278}
279
1e8f889b
DG
280static void set_huge_ptep_writable(struct vm_area_struct *vma,
281 unsigned long address, pte_t *ptep)
282{
283 pte_t entry;
284
285 entry = pte_mkwrite(pte_mkdirty(*ptep));
286 ptep_set_access_flags(vma, address, ptep, entry, 1);
287 update_mmu_cache(vma, address, entry);
288 lazy_mmu_prot_update(entry);
289}
290
291
63551ae0
DG
292int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
293 struct vm_area_struct *vma)
294{
295 pte_t *src_pte, *dst_pte, entry;
296 struct page *ptepage;
1c59827d 297 unsigned long addr;
1e8f889b
DG
298 int cow;
299
300 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
63551ae0 301
1c59827d 302 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
c74df32c
HD
303 src_pte = huge_pte_offset(src, addr);
304 if (!src_pte)
305 continue;
63551ae0
DG
306 dst_pte = huge_pte_alloc(dst, addr);
307 if (!dst_pte)
308 goto nomem;
c74df32c 309 spin_lock(&dst->page_table_lock);
1c59827d 310 spin_lock(&src->page_table_lock);
c74df32c 311 if (!pte_none(*src_pte)) {
1e8f889b
DG
312 if (cow)
313 ptep_set_wrprotect(src, addr, src_pte);
1c59827d
HD
314 entry = *src_pte;
315 ptepage = pte_page(entry);
316 get_page(ptepage);
4294621f 317 add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE);
1c59827d
HD
318 set_huge_pte_at(dst, addr, dst_pte, entry);
319 }
320 spin_unlock(&src->page_table_lock);
c74df32c 321 spin_unlock(&dst->page_table_lock);
63551ae0
DG
322 }
323 return 0;
324
325nomem:
326 return -ENOMEM;
327}
328
329void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
330 unsigned long end)
331{
332 struct mm_struct *mm = vma->vm_mm;
333 unsigned long address;
c7546f8f 334 pte_t *ptep;
63551ae0
DG
335 pte_t pte;
336 struct page *page;
337
338 WARN_ON(!is_vm_hugetlb_page(vma));
339 BUG_ON(start & ~HPAGE_MASK);
340 BUG_ON(end & ~HPAGE_MASK);
341
508034a3
HD
342 spin_lock(&mm->page_table_lock);
343
365e9c87
HD
344 /* Update high watermark before we lower rss */
345 update_hiwater_rss(mm);
346
63551ae0 347 for (address = start; address < end; address += HPAGE_SIZE) {
c7546f8f 348 ptep = huge_pte_offset(mm, address);
4c887265 349 if (!ptep)
c7546f8f
DG
350 continue;
351
352 pte = huge_ptep_get_and_clear(mm, address, ptep);
63551ae0
DG
353 if (pte_none(pte))
354 continue;
c7546f8f 355
63551ae0
DG
356 page = pte_page(pte);
357 put_page(page);
4294621f 358 add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE));
63551ae0 359 }
63551ae0 360
1da177e4 361 spin_unlock(&mm->page_table_lock);
508034a3 362 flush_tlb_range(vma, start, end);
1da177e4 363}
63551ae0 364
1e8f889b
DG
365static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
366 unsigned long address, pte_t *ptep, pte_t pte)
367{
368 struct page *old_page, *new_page;
369 int i, avoidcopy;
370
371 old_page = pte_page(pte);
372
373 /* If no-one else is actually using this page, avoid the copy
374 * and just make the page writable */
375 avoidcopy = (page_count(old_page) == 1);
376 if (avoidcopy) {
377 set_huge_ptep_writable(vma, address, ptep);
378 return VM_FAULT_MINOR;
379 }
380
381 page_cache_get(old_page);
5da7ca86 382 new_page = alloc_huge_page(vma, address);
1e8f889b
DG
383
384 if (!new_page) {
385 page_cache_release(old_page);
0df420d8 386 return VM_FAULT_OOM;
1e8f889b
DG
387 }
388
389 spin_unlock(&mm->page_table_lock);
390 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++)
391 copy_user_highpage(new_page + i, old_page + i,
392 address + i*PAGE_SIZE);
393 spin_lock(&mm->page_table_lock);
394
395 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
396 if (likely(pte_same(*ptep, pte))) {
397 /* Break COW */
398 set_huge_pte_at(mm, address, ptep,
399 make_huge_pte(vma, new_page, 1));
400 /* Make the old page be freed below */
401 new_page = old_page;
402 }
403 page_cache_release(new_page);
404 page_cache_release(old_page);
405 return VM_FAULT_MINOR;
406}
407
86e5216f 408int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1e8f889b 409 unsigned long address, pte_t *ptep, int write_access)
ac9b9c66
HD
410{
411 int ret = VM_FAULT_SIGBUS;
4c887265
AL
412 unsigned long idx;
413 unsigned long size;
4c887265
AL
414 struct page *page;
415 struct address_space *mapping;
1e8f889b 416 pte_t new_pte;
4c887265 417
4c887265
AL
418 mapping = vma->vm_file->f_mapping;
419 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
420 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
421
422 /*
423 * Use page lock to guard against racing truncation
424 * before we get page_table_lock.
425 */
6bda666a
CL
426retry:
427 page = find_lock_page(mapping, idx);
428 if (!page) {
429 if (hugetlb_get_quota(mapping))
430 goto out;
431 page = alloc_huge_page(vma, address);
432 if (!page) {
433 hugetlb_put_quota(mapping);
0df420d8 434 ret = VM_FAULT_OOM;
6bda666a
CL
435 goto out;
436 }
ac9b9c66 437
6bda666a
CL
438 if (vma->vm_flags & VM_SHARED) {
439 int err;
440
441 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
442 if (err) {
443 put_page(page);
444 hugetlb_put_quota(mapping);
445 if (err == -EEXIST)
446 goto retry;
447 goto out;
448 }
449 } else
450 lock_page(page);
451 }
1e8f889b 452
ac9b9c66 453 spin_lock(&mm->page_table_lock);
4c887265
AL
454 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
455 if (idx >= size)
456 goto backout;
457
458 ret = VM_FAULT_MINOR;
86e5216f 459 if (!pte_none(*ptep))
4c887265
AL
460 goto backout;
461
462 add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE);
1e8f889b
DG
463 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
464 && (vma->vm_flags & VM_SHARED)));
465 set_huge_pte_at(mm, address, ptep, new_pte);
466
467 if (write_access && !(vma->vm_flags & VM_SHARED)) {
468 /* Optimization, do the COW without a second fault */
469 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
470 }
471
ac9b9c66 472 spin_unlock(&mm->page_table_lock);
4c887265
AL
473 unlock_page(page);
474out:
ac9b9c66 475 return ret;
4c887265
AL
476
477backout:
478 spin_unlock(&mm->page_table_lock);
479 hugetlb_put_quota(mapping);
480 unlock_page(page);
481 put_page(page);
482 goto out;
ac9b9c66
HD
483}
484
86e5216f
AL
485int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
486 unsigned long address, int write_access)
487{
488 pte_t *ptep;
489 pte_t entry;
1e8f889b 490 int ret;
86e5216f
AL
491
492 ptep = huge_pte_alloc(mm, address);
493 if (!ptep)
494 return VM_FAULT_OOM;
495
496 entry = *ptep;
497 if (pte_none(entry))
1e8f889b 498 return hugetlb_no_page(mm, vma, address, ptep, write_access);
86e5216f 499
1e8f889b
DG
500 ret = VM_FAULT_MINOR;
501
502 spin_lock(&mm->page_table_lock);
503 /* Check for a racing update before calling hugetlb_cow */
504 if (likely(pte_same(entry, *ptep)))
505 if (write_access && !pte_write(entry))
506 ret = hugetlb_cow(mm, vma, address, ptep, entry);
507 spin_unlock(&mm->page_table_lock);
508
509 return ret;
86e5216f
AL
510}
511
63551ae0
DG
512int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
513 struct page **pages, struct vm_area_struct **vmas,
514 unsigned long *position, int *length, int i)
515{
516 unsigned long vpfn, vaddr = *position;
517 int remainder = *length;
518
63551ae0 519 vpfn = vaddr/PAGE_SIZE;
1c59827d 520 spin_lock(&mm->page_table_lock);
63551ae0 521 while (vaddr < vma->vm_end && remainder) {
4c887265
AL
522 pte_t *pte;
523 struct page *page;
63551ae0 524
4c887265
AL
525 /*
526 * Some archs (sparc64, sh*) have multiple pte_ts to
527 * each hugepage. We have to make * sure we get the
528 * first, for the page indexing below to work.
529 */
530 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
63551ae0 531
4c887265
AL
532 if (!pte || pte_none(*pte)) {
533 int ret;
63551ae0 534
4c887265
AL
535 spin_unlock(&mm->page_table_lock);
536 ret = hugetlb_fault(mm, vma, vaddr, 0);
537 spin_lock(&mm->page_table_lock);
538 if (ret == VM_FAULT_MINOR)
539 continue;
63551ae0 540
4c887265
AL
541 remainder = 0;
542 if (!i)
543 i = -EFAULT;
544 break;
545 }
546
547 if (pages) {
548 page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
63551ae0
DG
549 get_page(page);
550 pages[i] = page;
551 }
552
553 if (vmas)
554 vmas[i] = vma;
555
556 vaddr += PAGE_SIZE;
557 ++vpfn;
558 --remainder;
559 ++i;
560 }
1c59827d 561 spin_unlock(&mm->page_table_lock);
63551ae0
DG
562 *length = remainder;
563 *position = vaddr;
564
565 return i;
566}