hugetlb: reserve huge pages for reliable MAP_PRIVATE hugetlbfs mappings until fork()
[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>
3935baa9 16#include <linux/mutex.h>
5da7ca86 17
63551ae0
DG
18#include <asm/page.h>
19#include <asm/pgtable.h>
20
21#include <linux/hugetlb.h>
7835e98b 22#include "internal.h"
1da177e4
LT
23
24const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
a43a8c39 25static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages;
7893d1d5 26static unsigned long surplus_huge_pages;
064d9efe 27static unsigned long nr_overcommit_huge_pages;
1da177e4 28unsigned long max_huge_pages;
064d9efe 29unsigned long sysctl_overcommit_huge_pages;
1da177e4
LT
30static struct list_head hugepage_freelists[MAX_NUMNODES];
31static unsigned int nr_huge_pages_node[MAX_NUMNODES];
32static unsigned int free_huge_pages_node[MAX_NUMNODES];
7893d1d5 33static unsigned int surplus_huge_pages_node[MAX_NUMNODES];
396faf03
MG
34static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
35unsigned long hugepages_treat_as_movable;
63b4613c 36static int hugetlb_next_nid;
396faf03 37
3935baa9
DG
38/*
39 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
40 */
41static DEFINE_SPINLOCK(hugetlb_lock);
0bd0f9fb 42
a1e78772
MG
43/*
44 * These helpers are used to track how many pages are reserved for
45 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
46 * is guaranteed to have their future faults succeed.
47 *
48 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
49 * the reserve counters are updated with the hugetlb_lock held. It is safe
50 * to reset the VMA at fork() time as it is not in use yet and there is no
51 * chance of the global counters getting corrupted as a result of the values.
52 */
53static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma)
54{
55 VM_BUG_ON(!is_vm_hugetlb_page(vma));
56 if (!(vma->vm_flags & VM_SHARED))
57 return (unsigned long)vma->vm_private_data;
58 return 0;
59}
60
61static void set_vma_resv_huge_pages(struct vm_area_struct *vma,
62 unsigned long reserve)
63{
64 VM_BUG_ON(!is_vm_hugetlb_page(vma));
65 VM_BUG_ON(vma->vm_flags & VM_SHARED);
66
67 vma->vm_private_data = (void *)reserve;
68}
69
70/* Decrement the reserved pages in the hugepage pool by one */
71static void decrement_hugepage_resv_vma(struct vm_area_struct *vma)
72{
73 if (vma->vm_flags & VM_SHARED) {
74 /* Shared mappings always use reserves */
75 resv_huge_pages--;
76 } else {
77 /*
78 * Only the process that called mmap() has reserves for
79 * private mappings.
80 */
81 if (vma_resv_huge_pages(vma)) {
82 resv_huge_pages--;
83 reserve = (unsigned long)vma->vm_private_data - 1;
84 vma->vm_private_data = (void *)reserve;
85 }
86 }
87}
88
89void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
90{
91 VM_BUG_ON(!is_vm_hugetlb_page(vma));
92 if (!(vma->vm_flags & VM_SHARED))
93 vma->vm_private_data = (void *)0;
94}
95
96/* Returns true if the VMA has associated reserve pages */
97static int vma_has_private_reserves(struct vm_area_struct *vma)
98{
99 if (vma->vm_flags & VM_SHARED)
100 return 0;
101 if (!vma_resv_huge_pages(vma))
102 return 0;
103 return 1;
104}
105
79ac6ba4
DG
106static void clear_huge_page(struct page *page, unsigned long addr)
107{
108 int i;
109
110 might_sleep();
111 for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) {
112 cond_resched();
281e0e3b 113 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
79ac6ba4
DG
114 }
115}
116
117static void copy_huge_page(struct page *dst, struct page *src,
9de455b2 118 unsigned long addr, struct vm_area_struct *vma)
79ac6ba4
DG
119{
120 int i;
121
122 might_sleep();
123 for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) {
124 cond_resched();
9de455b2 125 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
79ac6ba4
DG
126 }
127}
128
1da177e4
LT
129static void enqueue_huge_page(struct page *page)
130{
131 int nid = page_to_nid(page);
132 list_add(&page->lru, &hugepage_freelists[nid]);
133 free_huge_pages++;
134 free_huge_pages_node[nid]++;
135}
136
348e1e04
NA
137static struct page *dequeue_huge_page(void)
138{
139 int nid;
140 struct page *page = NULL;
141
142 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
143 if (!list_empty(&hugepage_freelists[nid])) {
144 page = list_entry(hugepage_freelists[nid].next,
145 struct page, lru);
146 list_del(&page->lru);
147 free_huge_pages--;
148 free_huge_pages_node[nid]--;
149 break;
150 }
151 }
152 return page;
153}
154
155static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma,
5da7ca86 156 unsigned long address)
1da177e4 157{
31a5c6e4 158 int nid;
1da177e4 159 struct page *page = NULL;
480eccf9 160 struct mempolicy *mpol;
19770b32 161 nodemask_t *nodemask;
396faf03 162 struct zonelist *zonelist = huge_zonelist(vma, address,
19770b32 163 htlb_alloc_mask, &mpol, &nodemask);
dd1a239f
MG
164 struct zone *zone;
165 struct zoneref *z;
1da177e4 166
a1e78772
MG
167 /*
168 * A child process with MAP_PRIVATE mappings created by their parent
169 * have no page reserves. This check ensures that reservations are
170 * not "stolen". The child may still get SIGKILLed
171 */
172 if (!vma_has_private_reserves(vma) &&
173 free_huge_pages - resv_huge_pages == 0)
174 return NULL;
175
19770b32
MG
176 for_each_zone_zonelist_nodemask(zone, z, zonelist,
177 MAX_NR_ZONES - 1, nodemask) {
54a6eb5c
MG
178 nid = zone_to_nid(zone);
179 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
3abf7afd
AM
180 !list_empty(&hugepage_freelists[nid])) {
181 page = list_entry(hugepage_freelists[nid].next,
182 struct page, lru);
183 list_del(&page->lru);
184 free_huge_pages--;
185 free_huge_pages_node[nid]--;
a1e78772
MG
186 decrement_hugepage_resv_vma(vma);
187
5ab3ee7b 188 break;
3abf7afd 189 }
1da177e4 190 }
52cd3b07 191 mpol_cond_put(mpol);
1da177e4
LT
192 return page;
193}
194
6af2acb6
AL
195static void update_and_free_page(struct page *page)
196{
197 int i;
198 nr_huge_pages--;
199 nr_huge_pages_node[page_to_nid(page)]--;
200 for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) {
201 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
202 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
203 1 << PG_private | 1<< PG_writeback);
204 }
205 set_compound_page_dtor(page, NULL);
206 set_page_refcounted(page);
7f2e9525 207 arch_release_hugepage(page);
6af2acb6
AL
208 __free_pages(page, HUGETLB_PAGE_ORDER);
209}
210
27a85ef1
DG
211static void free_huge_page(struct page *page)
212{
7893d1d5 213 int nid = page_to_nid(page);
c79fb75e 214 struct address_space *mapping;
27a85ef1 215
c79fb75e 216 mapping = (struct address_space *) page_private(page);
e5df70ab 217 set_page_private(page, 0);
7893d1d5 218 BUG_ON(page_count(page));
27a85ef1
DG
219 INIT_LIST_HEAD(&page->lru);
220
221 spin_lock(&hugetlb_lock);
7893d1d5
AL
222 if (surplus_huge_pages_node[nid]) {
223 update_and_free_page(page);
224 surplus_huge_pages--;
225 surplus_huge_pages_node[nid]--;
226 } else {
227 enqueue_huge_page(page);
228 }
27a85ef1 229 spin_unlock(&hugetlb_lock);
c79fb75e 230 if (mapping)
9a119c05 231 hugetlb_put_quota(mapping, 1);
27a85ef1
DG
232}
233
7893d1d5
AL
234/*
235 * Increment or decrement surplus_huge_pages. Keep node-specific counters
236 * balanced by operating on them in a round-robin fashion.
237 * Returns 1 if an adjustment was made.
238 */
239static int adjust_pool_surplus(int delta)
240{
241 static int prev_nid;
242 int nid = prev_nid;
243 int ret = 0;
244
245 VM_BUG_ON(delta != -1 && delta != 1);
246 do {
247 nid = next_node(nid, node_online_map);
248 if (nid == MAX_NUMNODES)
249 nid = first_node(node_online_map);
250
251 /* To shrink on this node, there must be a surplus page */
252 if (delta < 0 && !surplus_huge_pages_node[nid])
253 continue;
254 /* Surplus cannot exceed the total number of pages */
255 if (delta > 0 && surplus_huge_pages_node[nid] >=
256 nr_huge_pages_node[nid])
257 continue;
258
259 surplus_huge_pages += delta;
260 surplus_huge_pages_node[nid] += delta;
261 ret = 1;
262 break;
263 } while (nid != prev_nid);
264
265 prev_nid = nid;
266 return ret;
267}
268
63b4613c 269static struct page *alloc_fresh_huge_page_node(int nid)
1da177e4 270{
1da177e4 271 struct page *page;
f96efd58 272
63b4613c 273 page = alloc_pages_node(nid,
551883ae
NA
274 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
275 __GFP_REPEAT|__GFP_NOWARN,
63b4613c 276 HUGETLB_PAGE_ORDER);
1da177e4 277 if (page) {
7f2e9525
GS
278 if (arch_prepare_hugepage(page)) {
279 __free_pages(page, HUGETLB_PAGE_ORDER);
7b8ee84d 280 return NULL;
7f2e9525 281 }
33f2ef89 282 set_compound_page_dtor(page, free_huge_page);
0bd0f9fb 283 spin_lock(&hugetlb_lock);
1da177e4 284 nr_huge_pages++;
63b4613c 285 nr_huge_pages_node[nid]++;
0bd0f9fb 286 spin_unlock(&hugetlb_lock);
a482289d 287 put_page(page); /* free it into the hugepage allocator */
1da177e4 288 }
63b4613c
NA
289
290 return page;
291}
292
293static int alloc_fresh_huge_page(void)
294{
295 struct page *page;
296 int start_nid;
297 int next_nid;
298 int ret = 0;
299
300 start_nid = hugetlb_next_nid;
301
302 do {
303 page = alloc_fresh_huge_page_node(hugetlb_next_nid);
304 if (page)
305 ret = 1;
306 /*
307 * Use a helper variable to find the next node and then
308 * copy it back to hugetlb_next_nid afterwards:
309 * otherwise there's a window in which a racer might
310 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
311 * But we don't need to use a spin_lock here: it really
312 * doesn't matter if occasionally a racer chooses the
313 * same nid as we do. Move nid forward in the mask even
314 * if we just successfully allocated a hugepage so that
315 * the next caller gets hugepages on the next node.
316 */
317 next_nid = next_node(hugetlb_next_nid, node_online_map);
318 if (next_nid == MAX_NUMNODES)
319 next_nid = first_node(node_online_map);
320 hugetlb_next_nid = next_nid;
321 } while (!page && hugetlb_next_nid != start_nid);
322
3b116300
AL
323 if (ret)
324 count_vm_event(HTLB_BUDDY_PGALLOC);
325 else
326 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
327
63b4613c 328 return ret;
1da177e4
LT
329}
330
7893d1d5
AL
331static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma,
332 unsigned long address)
333{
334 struct page *page;
d1c3fb1f 335 unsigned int nid;
7893d1d5 336
d1c3fb1f
NA
337 /*
338 * Assume we will successfully allocate the surplus page to
339 * prevent racing processes from causing the surplus to exceed
340 * overcommit
341 *
342 * This however introduces a different race, where a process B
343 * tries to grow the static hugepage pool while alloc_pages() is
344 * called by process A. B will only examine the per-node
345 * counters in determining if surplus huge pages can be
346 * converted to normal huge pages in adjust_pool_surplus(). A
347 * won't be able to increment the per-node counter, until the
348 * lock is dropped by B, but B doesn't drop hugetlb_lock until
349 * no more huge pages can be converted from surplus to normal
350 * state (and doesn't try to convert again). Thus, we have a
351 * case where a surplus huge page exists, the pool is grown, and
352 * the surplus huge page still exists after, even though it
353 * should just have been converted to a normal huge page. This
354 * does not leak memory, though, as the hugepage will be freed
355 * once it is out of use. It also does not allow the counters to
356 * go out of whack in adjust_pool_surplus() as we don't modify
357 * the node values until we've gotten the hugepage and only the
358 * per-node value is checked there.
359 */
360 spin_lock(&hugetlb_lock);
361 if (surplus_huge_pages >= nr_overcommit_huge_pages) {
362 spin_unlock(&hugetlb_lock);
363 return NULL;
364 } else {
365 nr_huge_pages++;
366 surplus_huge_pages++;
367 }
368 spin_unlock(&hugetlb_lock);
369
551883ae
NA
370 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
371 __GFP_REPEAT|__GFP_NOWARN,
7893d1d5 372 HUGETLB_PAGE_ORDER);
d1c3fb1f
NA
373
374 spin_lock(&hugetlb_lock);
7893d1d5 375 if (page) {
2668db91
AL
376 /*
377 * This page is now managed by the hugetlb allocator and has
378 * no users -- drop the buddy allocator's reference.
379 */
380 put_page_testzero(page);
381 VM_BUG_ON(page_count(page));
d1c3fb1f 382 nid = page_to_nid(page);
7893d1d5 383 set_compound_page_dtor(page, free_huge_page);
d1c3fb1f
NA
384 /*
385 * We incremented the global counters already
386 */
387 nr_huge_pages_node[nid]++;
388 surplus_huge_pages_node[nid]++;
3b116300 389 __count_vm_event(HTLB_BUDDY_PGALLOC);
d1c3fb1f
NA
390 } else {
391 nr_huge_pages--;
392 surplus_huge_pages--;
3b116300 393 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
7893d1d5 394 }
d1c3fb1f 395 spin_unlock(&hugetlb_lock);
7893d1d5
AL
396
397 return page;
398}
399
e4e574b7
AL
400/*
401 * Increase the hugetlb pool such that it can accomodate a reservation
402 * of size 'delta'.
403 */
404static int gather_surplus_pages(int delta)
405{
406 struct list_head surplus_list;
407 struct page *page, *tmp;
408 int ret, i;
409 int needed, allocated;
410
411 needed = (resv_huge_pages + delta) - free_huge_pages;
ac09b3a1
AL
412 if (needed <= 0) {
413 resv_huge_pages += delta;
e4e574b7 414 return 0;
ac09b3a1 415 }
e4e574b7
AL
416
417 allocated = 0;
418 INIT_LIST_HEAD(&surplus_list);
419
420 ret = -ENOMEM;
421retry:
422 spin_unlock(&hugetlb_lock);
423 for (i = 0; i < needed; i++) {
424 page = alloc_buddy_huge_page(NULL, 0);
425 if (!page) {
426 /*
427 * We were not able to allocate enough pages to
428 * satisfy the entire reservation so we free what
429 * we've allocated so far.
430 */
431 spin_lock(&hugetlb_lock);
432 needed = 0;
433 goto free;
434 }
435
436 list_add(&page->lru, &surplus_list);
437 }
438 allocated += needed;
439
440 /*
441 * After retaking hugetlb_lock, we need to recalculate 'needed'
442 * because either resv_huge_pages or free_huge_pages may have changed.
443 */
444 spin_lock(&hugetlb_lock);
445 needed = (resv_huge_pages + delta) - (free_huge_pages + allocated);
446 if (needed > 0)
447 goto retry;
448
449 /*
450 * The surplus_list now contains _at_least_ the number of extra pages
451 * needed to accomodate the reservation. Add the appropriate number
452 * of pages to the hugetlb pool and free the extras back to the buddy
ac09b3a1
AL
453 * allocator. Commit the entire reservation here to prevent another
454 * process from stealing the pages as they are added to the pool but
455 * before they are reserved.
e4e574b7
AL
456 */
457 needed += allocated;
ac09b3a1 458 resv_huge_pages += delta;
e4e574b7
AL
459 ret = 0;
460free:
19fc3f0a 461 /* Free the needed pages to the hugetlb pool */
e4e574b7 462 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
19fc3f0a
AL
463 if ((--needed) < 0)
464 break;
e4e574b7 465 list_del(&page->lru);
19fc3f0a
AL
466 enqueue_huge_page(page);
467 }
468
469 /* Free unnecessary surplus pages to the buddy allocator */
470 if (!list_empty(&surplus_list)) {
471 spin_unlock(&hugetlb_lock);
472 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
473 list_del(&page->lru);
af767cbd 474 /*
2668db91
AL
475 * The page has a reference count of zero already, so
476 * call free_huge_page directly instead of using
477 * put_page. This must be done with hugetlb_lock
af767cbd
AL
478 * unlocked which is safe because free_huge_page takes
479 * hugetlb_lock before deciding how to free the page.
480 */
2668db91 481 free_huge_page(page);
af767cbd 482 }
19fc3f0a 483 spin_lock(&hugetlb_lock);
e4e574b7
AL
484 }
485
486 return ret;
487}
488
489/*
490 * When releasing a hugetlb pool reservation, any surplus pages that were
491 * allocated to satisfy the reservation must be explicitly freed if they were
492 * never used.
493 */
8cde045c 494static void return_unused_surplus_pages(unsigned long unused_resv_pages)
e4e574b7
AL
495{
496 static int nid = -1;
497 struct page *page;
498 unsigned long nr_pages;
499
11320d17
NA
500 /*
501 * We want to release as many surplus pages as possible, spread
502 * evenly across all nodes. Iterate across all nodes until we
503 * can no longer free unreserved surplus pages. This occurs when
504 * the nodes with surplus pages have no free pages.
505 */
506 unsigned long remaining_iterations = num_online_nodes();
507
ac09b3a1
AL
508 /* Uncommit the reservation */
509 resv_huge_pages -= unused_resv_pages;
510
e4e574b7
AL
511 nr_pages = min(unused_resv_pages, surplus_huge_pages);
512
11320d17 513 while (remaining_iterations-- && nr_pages) {
e4e574b7
AL
514 nid = next_node(nid, node_online_map);
515 if (nid == MAX_NUMNODES)
516 nid = first_node(node_online_map);
517
518 if (!surplus_huge_pages_node[nid])
519 continue;
520
521 if (!list_empty(&hugepage_freelists[nid])) {
522 page = list_entry(hugepage_freelists[nid].next,
523 struct page, lru);
524 list_del(&page->lru);
525 update_and_free_page(page);
526 free_huge_pages--;
527 free_huge_pages_node[nid]--;
528 surplus_huge_pages--;
529 surplus_huge_pages_node[nid]--;
530 nr_pages--;
11320d17 531 remaining_iterations = num_online_nodes();
e4e574b7
AL
532 }
533 }
534}
535
a1e78772
MG
536static struct page *alloc_huge_page(struct vm_area_struct *vma,
537 unsigned long addr)
1da177e4 538{
348ea204 539 struct page *page;
a1e78772
MG
540 struct address_space *mapping = vma->vm_file->f_mapping;
541 struct inode *inode = mapping->host;
542 unsigned int chg = 0;
543
544 /*
545 * Processes that did not create the mapping will have no reserves and
546 * will not have accounted against quota. Check that the quota can be
547 * made before satisfying the allocation
548 */
549 if (!vma_has_private_reserves(vma)) {
550 chg = 1;
551 if (hugetlb_get_quota(inode->i_mapping, chg))
552 return ERR_PTR(-ENOSPC);
553 }
1da177e4
LT
554
555 spin_lock(&hugetlb_lock);
348e1e04 556 page = dequeue_huge_page_vma(vma, addr);
1da177e4 557 spin_unlock(&hugetlb_lock);
b45b5bd6 558
68842c9b 559 if (!page) {
7893d1d5 560 page = alloc_buddy_huge_page(vma, addr);
68842c9b 561 if (!page) {
a1e78772 562 hugetlb_put_quota(inode->i_mapping, chg);
68842c9b
KC
563 return ERR_PTR(-VM_FAULT_OOM);
564 }
565 }
348ea204 566
a1e78772
MG
567 set_page_refcounted(page);
568 set_page_private(page, (unsigned long) mapping);
90d8b7e6 569
90d8b7e6 570 return page;
b45b5bd6
DG
571}
572
1da177e4
LT
573static int __init hugetlb_init(void)
574{
575 unsigned long i;
1da177e4 576
3c726f8d
BH
577 if (HPAGE_SHIFT == 0)
578 return 0;
579
1da177e4
LT
580 for (i = 0; i < MAX_NUMNODES; ++i)
581 INIT_LIST_HEAD(&hugepage_freelists[i]);
582
63b4613c
NA
583 hugetlb_next_nid = first_node(node_online_map);
584
1da177e4 585 for (i = 0; i < max_huge_pages; ++i) {
a482289d 586 if (!alloc_fresh_huge_page())
1da177e4 587 break;
1da177e4
LT
588 }
589 max_huge_pages = free_huge_pages = nr_huge_pages = i;
590 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages);
591 return 0;
592}
593module_init(hugetlb_init);
594
595static int __init hugetlb_setup(char *s)
596{
597 if (sscanf(s, "%lu", &max_huge_pages) <= 0)
598 max_huge_pages = 0;
599 return 1;
600}
601__setup("hugepages=", hugetlb_setup);
602
8a630112
KC
603static unsigned int cpuset_mems_nr(unsigned int *array)
604{
605 int node;
606 unsigned int nr = 0;
607
608 for_each_node_mask(node, cpuset_current_mems_allowed)
609 nr += array[node];
610
611 return nr;
612}
613
1da177e4 614#ifdef CONFIG_SYSCTL
1da177e4
LT
615#ifdef CONFIG_HIGHMEM
616static void try_to_free_low(unsigned long count)
617{
4415cc8d
CL
618 int i;
619
1da177e4
LT
620 for (i = 0; i < MAX_NUMNODES; ++i) {
621 struct page *page, *next;
622 list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) {
6b0c880d
AL
623 if (count >= nr_huge_pages)
624 return;
1da177e4
LT
625 if (PageHighMem(page))
626 continue;
627 list_del(&page->lru);
628 update_and_free_page(page);
1da177e4 629 free_huge_pages--;
4415cc8d 630 free_huge_pages_node[page_to_nid(page)]--;
1da177e4
LT
631 }
632 }
633}
634#else
635static inline void try_to_free_low(unsigned long count)
636{
637}
638#endif
639
7893d1d5 640#define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
1da177e4
LT
641static unsigned long set_max_huge_pages(unsigned long count)
642{
7893d1d5 643 unsigned long min_count, ret;
1da177e4 644
7893d1d5
AL
645 /*
646 * Increase the pool size
647 * First take pages out of surplus state. Then make up the
648 * remaining difference by allocating fresh huge pages.
d1c3fb1f
NA
649 *
650 * We might race with alloc_buddy_huge_page() here and be unable
651 * to convert a surplus huge page to a normal huge page. That is
652 * not critical, though, it just means the overall size of the
653 * pool might be one hugepage larger than it needs to be, but
654 * within all the constraints specified by the sysctls.
7893d1d5 655 */
1da177e4 656 spin_lock(&hugetlb_lock);
7893d1d5
AL
657 while (surplus_huge_pages && count > persistent_huge_pages) {
658 if (!adjust_pool_surplus(-1))
659 break;
660 }
661
662 while (count > persistent_huge_pages) {
7893d1d5
AL
663 /*
664 * If this allocation races such that we no longer need the
665 * page, free_huge_page will handle it by freeing the page
666 * and reducing the surplus.
667 */
668 spin_unlock(&hugetlb_lock);
669 ret = alloc_fresh_huge_page();
670 spin_lock(&hugetlb_lock);
671 if (!ret)
672 goto out;
673
674 }
7893d1d5
AL
675
676 /*
677 * Decrease the pool size
678 * First return free pages to the buddy allocator (being careful
679 * to keep enough around to satisfy reservations). Then place
680 * pages into surplus state as needed so the pool will shrink
681 * to the desired size as pages become free.
d1c3fb1f
NA
682 *
683 * By placing pages into the surplus state independent of the
684 * overcommit value, we are allowing the surplus pool size to
685 * exceed overcommit. There are few sane options here. Since
686 * alloc_buddy_huge_page() is checking the global counter,
687 * though, we'll note that we're not allowed to exceed surplus
688 * and won't grow the pool anywhere else. Not until one of the
689 * sysctls are changed, or the surplus pages go out of use.
7893d1d5 690 */
6b0c880d
AL
691 min_count = resv_huge_pages + nr_huge_pages - free_huge_pages;
692 min_count = max(count, min_count);
7893d1d5
AL
693 try_to_free_low(min_count);
694 while (min_count < persistent_huge_pages) {
348e1e04 695 struct page *page = dequeue_huge_page();
1da177e4
LT
696 if (!page)
697 break;
698 update_and_free_page(page);
699 }
7893d1d5
AL
700 while (count < persistent_huge_pages) {
701 if (!adjust_pool_surplus(1))
702 break;
703 }
704out:
705 ret = persistent_huge_pages;
1da177e4 706 spin_unlock(&hugetlb_lock);
7893d1d5 707 return ret;
1da177e4
LT
708}
709
710int hugetlb_sysctl_handler(struct ctl_table *table, int write,
711 struct file *file, void __user *buffer,
712 size_t *length, loff_t *ppos)
713{
714 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
715 max_huge_pages = set_max_huge_pages(max_huge_pages);
716 return 0;
717}
396faf03
MG
718
719int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
720 struct file *file, void __user *buffer,
721 size_t *length, loff_t *ppos)
722{
723 proc_dointvec(table, write, file, buffer, length, ppos);
724 if (hugepages_treat_as_movable)
725 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
726 else
727 htlb_alloc_mask = GFP_HIGHUSER;
728 return 0;
729}
730
a3d0c6aa
NA
731int hugetlb_overcommit_handler(struct ctl_table *table, int write,
732 struct file *file, void __user *buffer,
733 size_t *length, loff_t *ppos)
734{
a3d0c6aa 735 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
064d9efe
NA
736 spin_lock(&hugetlb_lock);
737 nr_overcommit_huge_pages = sysctl_overcommit_huge_pages;
a3d0c6aa
NA
738 spin_unlock(&hugetlb_lock);
739 return 0;
740}
741
1da177e4
LT
742#endif /* CONFIG_SYSCTL */
743
744int hugetlb_report_meminfo(char *buf)
745{
746 return sprintf(buf,
747 "HugePages_Total: %5lu\n"
748 "HugePages_Free: %5lu\n"
a43a8c39 749 "HugePages_Rsvd: %5lu\n"
7893d1d5 750 "HugePages_Surp: %5lu\n"
1da177e4
LT
751 "Hugepagesize: %5lu kB\n",
752 nr_huge_pages,
753 free_huge_pages,
a43a8c39 754 resv_huge_pages,
7893d1d5 755 surplus_huge_pages,
1da177e4
LT
756 HPAGE_SIZE/1024);
757}
758
759int hugetlb_report_node_meminfo(int nid, char *buf)
760{
761 return sprintf(buf,
762 "Node %d HugePages_Total: %5u\n"
a1de0919
NA
763 "Node %d HugePages_Free: %5u\n"
764 "Node %d HugePages_Surp: %5u\n",
1da177e4 765 nid, nr_huge_pages_node[nid],
a1de0919
NA
766 nid, free_huge_pages_node[nid],
767 nid, surplus_huge_pages_node[nid]);
1da177e4
LT
768}
769
1da177e4
LT
770/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
771unsigned long hugetlb_total_pages(void)
772{
773 return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE);
774}
1da177e4 775
fc1b8a73
MG
776static int hugetlb_acct_memory(long delta)
777{
778 int ret = -ENOMEM;
779
780 spin_lock(&hugetlb_lock);
781 /*
782 * When cpuset is configured, it breaks the strict hugetlb page
783 * reservation as the accounting is done on a global variable. Such
784 * reservation is completely rubbish in the presence of cpuset because
785 * the reservation is not checked against page availability for the
786 * current cpuset. Application can still potentially OOM'ed by kernel
787 * with lack of free htlb page in cpuset that the task is in.
788 * Attempt to enforce strict accounting with cpuset is almost
789 * impossible (or too ugly) because cpuset is too fluid that
790 * task or memory node can be dynamically moved between cpusets.
791 *
792 * The change of semantics for shared hugetlb mapping with cpuset is
793 * undesirable. However, in order to preserve some of the semantics,
794 * we fall back to check against current free page availability as
795 * a best attempt and hopefully to minimize the impact of changing
796 * semantics that cpuset has.
797 */
798 if (delta > 0) {
799 if (gather_surplus_pages(delta) < 0)
800 goto out;
801
802 if (delta > cpuset_mems_nr(free_huge_pages_node)) {
803 return_unused_surplus_pages(delta);
804 goto out;
805 }
806 }
807
808 ret = 0;
809 if (delta < 0)
810 return_unused_surplus_pages((unsigned long) -delta);
811
812out:
813 spin_unlock(&hugetlb_lock);
814 return ret;
815}
816
a1e78772
MG
817static void hugetlb_vm_op_close(struct vm_area_struct *vma)
818{
819 unsigned long reserve = vma_resv_huge_pages(vma);
820 if (reserve)
821 hugetlb_acct_memory(-reserve);
822}
823
1da177e4
LT
824/*
825 * We cannot handle pagefaults against hugetlb pages at all. They cause
826 * handle_mm_fault() to try to instantiate regular-sized pages in the
827 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
828 * this far.
829 */
d0217ac0 830static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4
LT
831{
832 BUG();
d0217ac0 833 return 0;
1da177e4
LT
834}
835
836struct vm_operations_struct hugetlb_vm_ops = {
d0217ac0 837 .fault = hugetlb_vm_op_fault,
a1e78772 838 .close = hugetlb_vm_op_close,
1da177e4
LT
839};
840
1e8f889b
DG
841static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
842 int writable)
63551ae0
DG
843{
844 pte_t entry;
845
1e8f889b 846 if (writable) {
63551ae0
DG
847 entry =
848 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
849 } else {
7f2e9525 850 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
63551ae0
DG
851 }
852 entry = pte_mkyoung(entry);
853 entry = pte_mkhuge(entry);
854
855 return entry;
856}
857
1e8f889b
DG
858static void set_huge_ptep_writable(struct vm_area_struct *vma,
859 unsigned long address, pte_t *ptep)
860{
861 pte_t entry;
862
7f2e9525
GS
863 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
864 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
8dab5241 865 update_mmu_cache(vma, address, entry);
8dab5241 866 }
1e8f889b
DG
867}
868
869
63551ae0
DG
870int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
871 struct vm_area_struct *vma)
872{
873 pte_t *src_pte, *dst_pte, entry;
874 struct page *ptepage;
1c59827d 875 unsigned long addr;
1e8f889b
DG
876 int cow;
877
878 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
63551ae0 879
1c59827d 880 for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
c74df32c
HD
881 src_pte = huge_pte_offset(src, addr);
882 if (!src_pte)
883 continue;
63551ae0
DG
884 dst_pte = huge_pte_alloc(dst, addr);
885 if (!dst_pte)
886 goto nomem;
c5c99429
LW
887
888 /* If the pagetables are shared don't copy or take references */
889 if (dst_pte == src_pte)
890 continue;
891
c74df32c 892 spin_lock(&dst->page_table_lock);
46478758 893 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
7f2e9525 894 if (!huge_pte_none(huge_ptep_get(src_pte))) {
1e8f889b 895 if (cow)
7f2e9525
GS
896 huge_ptep_set_wrprotect(src, addr, src_pte);
897 entry = huge_ptep_get(src_pte);
1c59827d
HD
898 ptepage = pte_page(entry);
899 get_page(ptepage);
1c59827d
HD
900 set_huge_pte_at(dst, addr, dst_pte, entry);
901 }
902 spin_unlock(&src->page_table_lock);
c74df32c 903 spin_unlock(&dst->page_table_lock);
63551ae0
DG
904 }
905 return 0;
906
907nomem:
908 return -ENOMEM;
909}
910
502717f4
CK
911void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
912 unsigned long end)
63551ae0
DG
913{
914 struct mm_struct *mm = vma->vm_mm;
915 unsigned long address;
c7546f8f 916 pte_t *ptep;
63551ae0
DG
917 pte_t pte;
918 struct page *page;
fe1668ae 919 struct page *tmp;
c0a499c2
CK
920 /*
921 * A page gathering list, protected by per file i_mmap_lock. The
922 * lock is used to avoid list corruption from multiple unmapping
923 * of the same page since we are using page->lru.
924 */
fe1668ae 925 LIST_HEAD(page_list);
63551ae0
DG
926
927 WARN_ON(!is_vm_hugetlb_page(vma));
928 BUG_ON(start & ~HPAGE_MASK);
929 BUG_ON(end & ~HPAGE_MASK);
930
508034a3 931 spin_lock(&mm->page_table_lock);
63551ae0 932 for (address = start; address < end; address += HPAGE_SIZE) {
c7546f8f 933 ptep = huge_pte_offset(mm, address);
4c887265 934 if (!ptep)
c7546f8f
DG
935 continue;
936
39dde65c
CK
937 if (huge_pmd_unshare(mm, &address, ptep))
938 continue;
939
c7546f8f 940 pte = huge_ptep_get_and_clear(mm, address, ptep);
7f2e9525 941 if (huge_pte_none(pte))
63551ae0 942 continue;
c7546f8f 943
63551ae0 944 page = pte_page(pte);
6649a386
KC
945 if (pte_dirty(pte))
946 set_page_dirty(page);
fe1668ae 947 list_add(&page->lru, &page_list);
63551ae0 948 }
1da177e4 949 spin_unlock(&mm->page_table_lock);
508034a3 950 flush_tlb_range(vma, start, end);
fe1668ae
CK
951 list_for_each_entry_safe(page, tmp, &page_list, lru) {
952 list_del(&page->lru);
953 put_page(page);
954 }
1da177e4 955}
63551ae0 956
502717f4
CK
957void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
958 unsigned long end)
959{
960 /*
961 * It is undesirable to test vma->vm_file as it should be non-null
962 * for valid hugetlb area. However, vm_file will be NULL in the error
963 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
964 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
965 * to clean up. Since no pte has actually been setup, it is safe to
966 * do nothing in this case.
967 */
968 if (vma->vm_file) {
969 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
970 __unmap_hugepage_range(vma, start, end);
971 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
972 }
973}
974
1e8f889b
DG
975static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
976 unsigned long address, pte_t *ptep, pte_t pte)
977{
978 struct page *old_page, *new_page;
79ac6ba4 979 int avoidcopy;
1e8f889b
DG
980
981 old_page = pte_page(pte);
982
983 /* If no-one else is actually using this page, avoid the copy
984 * and just make the page writable */
985 avoidcopy = (page_count(old_page) == 1);
986 if (avoidcopy) {
987 set_huge_ptep_writable(vma, address, ptep);
83c54070 988 return 0;
1e8f889b
DG
989 }
990
991 page_cache_get(old_page);
5da7ca86 992 new_page = alloc_huge_page(vma, address);
1e8f889b 993
2fc39cec 994 if (IS_ERR(new_page)) {
1e8f889b 995 page_cache_release(old_page);
2fc39cec 996 return -PTR_ERR(new_page);
1e8f889b
DG
997 }
998
999 spin_unlock(&mm->page_table_lock);
9de455b2 1000 copy_huge_page(new_page, old_page, address, vma);
0ed361de 1001 __SetPageUptodate(new_page);
1e8f889b
DG
1002 spin_lock(&mm->page_table_lock);
1003
1004 ptep = huge_pte_offset(mm, address & HPAGE_MASK);
7f2e9525 1005 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
1e8f889b 1006 /* Break COW */
8fe627ec 1007 huge_ptep_clear_flush(vma, address, ptep);
1e8f889b
DG
1008 set_huge_pte_at(mm, address, ptep,
1009 make_huge_pte(vma, new_page, 1));
1010 /* Make the old page be freed below */
1011 new_page = old_page;
1012 }
1013 page_cache_release(new_page);
1014 page_cache_release(old_page);
83c54070 1015 return 0;
1e8f889b
DG
1016}
1017
a1ed3dda 1018static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1e8f889b 1019 unsigned long address, pte_t *ptep, int write_access)
ac9b9c66
HD
1020{
1021 int ret = VM_FAULT_SIGBUS;
4c887265
AL
1022 unsigned long idx;
1023 unsigned long size;
4c887265
AL
1024 struct page *page;
1025 struct address_space *mapping;
1e8f889b 1026 pte_t new_pte;
4c887265 1027
4c887265
AL
1028 mapping = vma->vm_file->f_mapping;
1029 idx = ((address - vma->vm_start) >> HPAGE_SHIFT)
1030 + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
1031
1032 /*
1033 * Use page lock to guard against racing truncation
1034 * before we get page_table_lock.
1035 */
6bda666a
CL
1036retry:
1037 page = find_lock_page(mapping, idx);
1038 if (!page) {
ebed4bfc
HD
1039 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1040 if (idx >= size)
1041 goto out;
6bda666a 1042 page = alloc_huge_page(vma, address);
2fc39cec
AL
1043 if (IS_ERR(page)) {
1044 ret = -PTR_ERR(page);
6bda666a
CL
1045 goto out;
1046 }
79ac6ba4 1047 clear_huge_page(page, address);
0ed361de 1048 __SetPageUptodate(page);
ac9b9c66 1049
6bda666a
CL
1050 if (vma->vm_flags & VM_SHARED) {
1051 int err;
45c682a6 1052 struct inode *inode = mapping->host;
6bda666a
CL
1053
1054 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1055 if (err) {
1056 put_page(page);
6bda666a
CL
1057 if (err == -EEXIST)
1058 goto retry;
1059 goto out;
1060 }
45c682a6
KC
1061
1062 spin_lock(&inode->i_lock);
1063 inode->i_blocks += BLOCKS_PER_HUGEPAGE;
1064 spin_unlock(&inode->i_lock);
6bda666a
CL
1065 } else
1066 lock_page(page);
1067 }
1e8f889b 1068
ac9b9c66 1069 spin_lock(&mm->page_table_lock);
4c887265
AL
1070 size = i_size_read(mapping->host) >> HPAGE_SHIFT;
1071 if (idx >= size)
1072 goto backout;
1073
83c54070 1074 ret = 0;
7f2e9525 1075 if (!huge_pte_none(huge_ptep_get(ptep)))
4c887265
AL
1076 goto backout;
1077
1e8f889b
DG
1078 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1079 && (vma->vm_flags & VM_SHARED)));
1080 set_huge_pte_at(mm, address, ptep, new_pte);
1081
1082 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1083 /* Optimization, do the COW without a second fault */
1084 ret = hugetlb_cow(mm, vma, address, ptep, new_pte);
1085 }
1086
ac9b9c66 1087 spin_unlock(&mm->page_table_lock);
4c887265
AL
1088 unlock_page(page);
1089out:
ac9b9c66 1090 return ret;
4c887265
AL
1091
1092backout:
1093 spin_unlock(&mm->page_table_lock);
4c887265
AL
1094 unlock_page(page);
1095 put_page(page);
1096 goto out;
ac9b9c66
HD
1097}
1098
86e5216f
AL
1099int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1100 unsigned long address, int write_access)
1101{
1102 pte_t *ptep;
1103 pte_t entry;
1e8f889b 1104 int ret;
3935baa9 1105 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
86e5216f
AL
1106
1107 ptep = huge_pte_alloc(mm, address);
1108 if (!ptep)
1109 return VM_FAULT_OOM;
1110
3935baa9
DG
1111 /*
1112 * Serialize hugepage allocation and instantiation, so that we don't
1113 * get spurious allocation failures if two CPUs race to instantiate
1114 * the same page in the page cache.
1115 */
1116 mutex_lock(&hugetlb_instantiation_mutex);
7f2e9525
GS
1117 entry = huge_ptep_get(ptep);
1118 if (huge_pte_none(entry)) {
3935baa9
DG
1119 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1120 mutex_unlock(&hugetlb_instantiation_mutex);
1121 return ret;
1122 }
86e5216f 1123
83c54070 1124 ret = 0;
1e8f889b
DG
1125
1126 spin_lock(&mm->page_table_lock);
1127 /* Check for a racing update before calling hugetlb_cow */
7f2e9525 1128 if (likely(pte_same(entry, huge_ptep_get(ptep))))
1e8f889b
DG
1129 if (write_access && !pte_write(entry))
1130 ret = hugetlb_cow(mm, vma, address, ptep, entry);
1131 spin_unlock(&mm->page_table_lock);
3935baa9 1132 mutex_unlock(&hugetlb_instantiation_mutex);
1e8f889b
DG
1133
1134 return ret;
86e5216f
AL
1135}
1136
63551ae0
DG
1137int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1138 struct page **pages, struct vm_area_struct **vmas,
5b23dbe8
AL
1139 unsigned long *position, int *length, int i,
1140 int write)
63551ae0 1141{
d5d4b0aa
CK
1142 unsigned long pfn_offset;
1143 unsigned long vaddr = *position;
63551ae0
DG
1144 int remainder = *length;
1145
1c59827d 1146 spin_lock(&mm->page_table_lock);
63551ae0 1147 while (vaddr < vma->vm_end && remainder) {
4c887265
AL
1148 pte_t *pte;
1149 struct page *page;
63551ae0 1150
4c887265
AL
1151 /*
1152 * Some archs (sparc64, sh*) have multiple pte_ts to
1153 * each hugepage. We have to make * sure we get the
1154 * first, for the page indexing below to work.
1155 */
1156 pte = huge_pte_offset(mm, vaddr & HPAGE_MASK);
63551ae0 1157
7f2e9525
GS
1158 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1159 (write && !pte_write(huge_ptep_get(pte)))) {
4c887265 1160 int ret;
63551ae0 1161
4c887265 1162 spin_unlock(&mm->page_table_lock);
5b23dbe8 1163 ret = hugetlb_fault(mm, vma, vaddr, write);
4c887265 1164 spin_lock(&mm->page_table_lock);
a89182c7 1165 if (!(ret & VM_FAULT_ERROR))
4c887265 1166 continue;
63551ae0 1167
4c887265
AL
1168 remainder = 0;
1169 if (!i)
1170 i = -EFAULT;
1171 break;
1172 }
1173
d5d4b0aa 1174 pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT;
7f2e9525 1175 page = pte_page(huge_ptep_get(pte));
d5d4b0aa 1176same_page:
d6692183
CK
1177 if (pages) {
1178 get_page(page);
d5d4b0aa 1179 pages[i] = page + pfn_offset;
d6692183 1180 }
63551ae0
DG
1181
1182 if (vmas)
1183 vmas[i] = vma;
1184
1185 vaddr += PAGE_SIZE;
d5d4b0aa 1186 ++pfn_offset;
63551ae0
DG
1187 --remainder;
1188 ++i;
d5d4b0aa
CK
1189 if (vaddr < vma->vm_end && remainder &&
1190 pfn_offset < HPAGE_SIZE/PAGE_SIZE) {
1191 /*
1192 * We use pfn_offset to avoid touching the pageframes
1193 * of this compound page.
1194 */
1195 goto same_page;
1196 }
63551ae0 1197 }
1c59827d 1198 spin_unlock(&mm->page_table_lock);
63551ae0
DG
1199 *length = remainder;
1200 *position = vaddr;
1201
1202 return i;
1203}
8f860591
ZY
1204
1205void hugetlb_change_protection(struct vm_area_struct *vma,
1206 unsigned long address, unsigned long end, pgprot_t newprot)
1207{
1208 struct mm_struct *mm = vma->vm_mm;
1209 unsigned long start = address;
1210 pte_t *ptep;
1211 pte_t pte;
1212
1213 BUG_ON(address >= end);
1214 flush_cache_range(vma, address, end);
1215
39dde65c 1216 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
8f860591
ZY
1217 spin_lock(&mm->page_table_lock);
1218 for (; address < end; address += HPAGE_SIZE) {
1219 ptep = huge_pte_offset(mm, address);
1220 if (!ptep)
1221 continue;
39dde65c
CK
1222 if (huge_pmd_unshare(mm, &address, ptep))
1223 continue;
7f2e9525 1224 if (!huge_pte_none(huge_ptep_get(ptep))) {
8f860591
ZY
1225 pte = huge_ptep_get_and_clear(mm, address, ptep);
1226 pte = pte_mkhuge(pte_modify(pte, newprot));
1227 set_huge_pte_at(mm, address, ptep, pte);
8f860591
ZY
1228 }
1229 }
1230 spin_unlock(&mm->page_table_lock);
39dde65c 1231 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
8f860591
ZY
1232
1233 flush_tlb_range(vma, start, end);
1234}
1235
a43a8c39
CK
1236struct file_region {
1237 struct list_head link;
1238 long from;
1239 long to;
1240};
1241
1242static long region_add(struct list_head *head, long f, long t)
1243{
1244 struct file_region *rg, *nrg, *trg;
1245
1246 /* Locate the region we are either in or before. */
1247 list_for_each_entry(rg, head, link)
1248 if (f <= rg->to)
1249 break;
1250
1251 /* Round our left edge to the current segment if it encloses us. */
1252 if (f > rg->from)
1253 f = rg->from;
1254
1255 /* Check for and consume any regions we now overlap with. */
1256 nrg = rg;
1257 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1258 if (&rg->link == head)
1259 break;
1260 if (rg->from > t)
1261 break;
1262
1263 /* If this area reaches higher then extend our area to
1264 * include it completely. If this is not the first area
1265 * which we intend to reuse, free it. */
1266 if (rg->to > t)
1267 t = rg->to;
1268 if (rg != nrg) {
1269 list_del(&rg->link);
1270 kfree(rg);
1271 }
1272 }
1273 nrg->from = f;
1274 nrg->to = t;
1275 return 0;
1276}
1277
1278static long region_chg(struct list_head *head, long f, long t)
1279{
1280 struct file_region *rg, *nrg;
1281 long chg = 0;
1282
1283 /* Locate the region we are before or in. */
1284 list_for_each_entry(rg, head, link)
1285 if (f <= rg->to)
1286 break;
1287
1288 /* If we are below the current region then a new region is required.
1289 * Subtle, allocate a new region at the position but make it zero
183ff22b 1290 * size such that we can guarantee to record the reservation. */
a43a8c39
CK
1291 if (&rg->link == head || t < rg->from) {
1292 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
c80544dc 1293 if (!nrg)
a43a8c39
CK
1294 return -ENOMEM;
1295 nrg->from = f;
1296 nrg->to = f;
1297 INIT_LIST_HEAD(&nrg->link);
1298 list_add(&nrg->link, rg->link.prev);
1299
1300 return t - f;
1301 }
1302
1303 /* Round our left edge to the current segment if it encloses us. */
1304 if (f > rg->from)
1305 f = rg->from;
1306 chg = t - f;
1307
1308 /* Check for and consume any regions we now overlap with. */
1309 list_for_each_entry(rg, rg->link.prev, link) {
1310 if (&rg->link == head)
1311 break;
1312 if (rg->from > t)
1313 return chg;
1314
1315 /* We overlap with this area, if it extends futher than
1316 * us then we must extend ourselves. Account for its
1317 * existing reservation. */
1318 if (rg->to > t) {
1319 chg += rg->to - t;
1320 t = rg->to;
1321 }
1322 chg -= rg->to - rg->from;
1323 }
1324 return chg;
1325}
1326
1327static long region_truncate(struct list_head *head, long end)
1328{
1329 struct file_region *rg, *trg;
1330 long chg = 0;
1331
1332 /* Locate the region we are either in or before. */
1333 list_for_each_entry(rg, head, link)
1334 if (end <= rg->to)
1335 break;
1336 if (&rg->link == head)
1337 return 0;
1338
1339 /* If we are in the middle of a region then adjust it. */
1340 if (end > rg->from) {
1341 chg = rg->to - end;
1342 rg->to = end;
1343 rg = list_entry(rg->link.next, typeof(*rg), link);
1344 }
1345
1346 /* Drop any remaining regions. */
1347 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
1348 if (&rg->link == head)
1349 break;
1350 chg += rg->to - rg->from;
1351 list_del(&rg->link);
1352 kfree(rg);
1353 }
1354 return chg;
1355}
1356
a1e78772
MG
1357int hugetlb_reserve_pages(struct inode *inode,
1358 long from, long to,
1359 struct vm_area_struct *vma)
e4e574b7
AL
1360{
1361 long ret, chg;
1362
a1e78772
MG
1363 /*
1364 * Shared mappings base their reservation on the number of pages that
1365 * are already allocated on behalf of the file. Private mappings need
1366 * to reserve the full area even if read-only as mprotect() may be
1367 * called to make the mapping read-write. Assume !vma is a shm mapping
1368 */
1369 if (!vma || vma->vm_flags & VM_SHARED)
1370 chg = region_chg(&inode->i_mapping->private_list, from, to);
1371 else {
1372 chg = to - from;
1373 set_vma_resv_huge_pages(vma, chg);
1374 }
1375
e4e574b7
AL
1376 if (chg < 0)
1377 return chg;
8a630112 1378
90d8b7e6
AL
1379 if (hugetlb_get_quota(inode->i_mapping, chg))
1380 return -ENOSPC;
a43a8c39 1381 ret = hugetlb_acct_memory(chg);
68842c9b
KC
1382 if (ret < 0) {
1383 hugetlb_put_quota(inode->i_mapping, chg);
a43a8c39 1384 return ret;
68842c9b 1385 }
a1e78772
MG
1386 if (!vma || vma->vm_flags & VM_SHARED)
1387 region_add(&inode->i_mapping->private_list, from, to);
a43a8c39
CK
1388 return 0;
1389}
1390
1391void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1392{
1393 long chg = region_truncate(&inode->i_mapping->private_list, offset);
45c682a6
KC
1394
1395 spin_lock(&inode->i_lock);
1396 inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed;
1397 spin_unlock(&inode->i_lock);
1398
90d8b7e6
AL
1399 hugetlb_put_quota(inode->i_mapping, (chg - freed));
1400 hugetlb_acct_memory(-(chg - freed));
a43a8c39 1401}