hugetlbfs: per mount huge page sizes
[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;
396faf03
MG
25static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
26unsigned long hugepages_treat_as_movable;
a5516438 27
e5ff2159
AK
28static int max_hstate;
29unsigned int default_hstate_idx;
30struct hstate hstates[HUGE_MAX_HSTATE];
31
32/* for command line parsing */
33static struct hstate * __initdata parsed_hstate;
34static unsigned long __initdata default_hstate_max_huge_pages;
35
36#define for_each_hstate(h) \
37 for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
396faf03 38
3935baa9
DG
39/*
40 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
41 */
42static DEFINE_SPINLOCK(hugetlb_lock);
0bd0f9fb 43
96822904
AW
44/*
45 * Region tracking -- allows tracking of reservations and instantiated pages
46 * across the pages in a mapping.
84afd99b
AW
47 *
48 * The region data structures are protected by a combination of the mmap_sem
49 * and the hugetlb_instantion_mutex. To access or modify a region the caller
50 * must either hold the mmap_sem for write, or the mmap_sem for read and
51 * the hugetlb_instantiation mutex:
52 *
53 * down_write(&mm->mmap_sem);
54 * or
55 * down_read(&mm->mmap_sem);
56 * mutex_lock(&hugetlb_instantiation_mutex);
96822904
AW
57 */
58struct file_region {
59 struct list_head link;
60 long from;
61 long to;
62};
63
64static long region_add(struct list_head *head, long f, long t)
65{
66 struct file_region *rg, *nrg, *trg;
67
68 /* Locate the region we are either in or before. */
69 list_for_each_entry(rg, head, link)
70 if (f <= rg->to)
71 break;
72
73 /* Round our left edge to the current segment if it encloses us. */
74 if (f > rg->from)
75 f = rg->from;
76
77 /* Check for and consume any regions we now overlap with. */
78 nrg = rg;
79 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
80 if (&rg->link == head)
81 break;
82 if (rg->from > t)
83 break;
84
85 /* If this area reaches higher then extend our area to
86 * include it completely. If this is not the first area
87 * which we intend to reuse, free it. */
88 if (rg->to > t)
89 t = rg->to;
90 if (rg != nrg) {
91 list_del(&rg->link);
92 kfree(rg);
93 }
94 }
95 nrg->from = f;
96 nrg->to = t;
97 return 0;
98}
99
100static long region_chg(struct list_head *head, long f, long t)
101{
102 struct file_region *rg, *nrg;
103 long chg = 0;
104
105 /* Locate the region we are before or in. */
106 list_for_each_entry(rg, head, link)
107 if (f <= rg->to)
108 break;
109
110 /* If we are below the current region then a new region is required.
111 * Subtle, allocate a new region at the position but make it zero
112 * size such that we can guarantee to record the reservation. */
113 if (&rg->link == head || t < rg->from) {
114 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
115 if (!nrg)
116 return -ENOMEM;
117 nrg->from = f;
118 nrg->to = f;
119 INIT_LIST_HEAD(&nrg->link);
120 list_add(&nrg->link, rg->link.prev);
121
122 return t - f;
123 }
124
125 /* Round our left edge to the current segment if it encloses us. */
126 if (f > rg->from)
127 f = rg->from;
128 chg = t - f;
129
130 /* Check for and consume any regions we now overlap with. */
131 list_for_each_entry(rg, rg->link.prev, link) {
132 if (&rg->link == head)
133 break;
134 if (rg->from > t)
135 return chg;
136
137 /* We overlap with this area, if it extends futher than
138 * us then we must extend ourselves. Account for its
139 * existing reservation. */
140 if (rg->to > t) {
141 chg += rg->to - t;
142 t = rg->to;
143 }
144 chg -= rg->to - rg->from;
145 }
146 return chg;
147}
148
149static long region_truncate(struct list_head *head, long end)
150{
151 struct file_region *rg, *trg;
152 long chg = 0;
153
154 /* Locate the region we are either in or before. */
155 list_for_each_entry(rg, head, link)
156 if (end <= rg->to)
157 break;
158 if (&rg->link == head)
159 return 0;
160
161 /* If we are in the middle of a region then adjust it. */
162 if (end > rg->from) {
163 chg = rg->to - end;
164 rg->to = end;
165 rg = list_entry(rg->link.next, typeof(*rg), link);
166 }
167
168 /* Drop any remaining regions. */
169 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
170 if (&rg->link == head)
171 break;
172 chg += rg->to - rg->from;
173 list_del(&rg->link);
174 kfree(rg);
175 }
176 return chg;
177}
178
84afd99b
AW
179static long region_count(struct list_head *head, long f, long t)
180{
181 struct file_region *rg;
182 long chg = 0;
183
184 /* Locate each segment we overlap with, and count that overlap. */
185 list_for_each_entry(rg, head, link) {
186 int seg_from;
187 int seg_to;
188
189 if (rg->to <= f)
190 continue;
191 if (rg->from >= t)
192 break;
193
194 seg_from = max(rg->from, f);
195 seg_to = min(rg->to, t);
196
197 chg += seg_to - seg_from;
198 }
199
200 return chg;
201}
202
e7c4b0bf
AW
203/*
204 * Convert the address within this vma to the page offset within
205 * the mapping, in pagecache page units; huge pages here.
206 */
a5516438
AK
207static pgoff_t vma_hugecache_offset(struct hstate *h,
208 struct vm_area_struct *vma, unsigned long address)
e7c4b0bf 209{
a5516438
AK
210 return ((address - vma->vm_start) >> huge_page_shift(h)) +
211 (vma->vm_pgoff >> huge_page_order(h));
e7c4b0bf
AW
212}
213
84afd99b
AW
214/*
215 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
216 * bits of the reservation map pointer, which are always clear due to
217 * alignment.
218 */
219#define HPAGE_RESV_OWNER (1UL << 0)
220#define HPAGE_RESV_UNMAPPED (1UL << 1)
04f2cbe3 221#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
84afd99b 222
a1e78772
MG
223/*
224 * These helpers are used to track how many pages are reserved for
225 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
226 * is guaranteed to have their future faults succeed.
227 *
228 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
229 * the reserve counters are updated with the hugetlb_lock held. It is safe
230 * to reset the VMA at fork() time as it is not in use yet and there is no
231 * chance of the global counters getting corrupted as a result of the values.
84afd99b
AW
232 *
233 * The private mapping reservation is represented in a subtly different
234 * manner to a shared mapping. A shared mapping has a region map associated
235 * with the underlying file, this region map represents the backing file
236 * pages which have ever had a reservation assigned which this persists even
237 * after the page is instantiated. A private mapping has a region map
238 * associated with the original mmap which is attached to all VMAs which
239 * reference it, this region map represents those offsets which have consumed
240 * reservation ie. where pages have been instantiated.
a1e78772 241 */
e7c4b0bf
AW
242static unsigned long get_vma_private_data(struct vm_area_struct *vma)
243{
244 return (unsigned long)vma->vm_private_data;
245}
246
247static void set_vma_private_data(struct vm_area_struct *vma,
248 unsigned long value)
249{
250 vma->vm_private_data = (void *)value;
251}
252
84afd99b
AW
253struct resv_map {
254 struct kref refs;
255 struct list_head regions;
256};
257
258struct resv_map *resv_map_alloc(void)
259{
260 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
261 if (!resv_map)
262 return NULL;
263
264 kref_init(&resv_map->refs);
265 INIT_LIST_HEAD(&resv_map->regions);
266
267 return resv_map;
268}
269
270void resv_map_release(struct kref *ref)
271{
272 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
273
274 /* Clear out any active regions before we release the map. */
275 region_truncate(&resv_map->regions, 0);
276 kfree(resv_map);
277}
278
279static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
a1e78772
MG
280{
281 VM_BUG_ON(!is_vm_hugetlb_page(vma));
282 if (!(vma->vm_flags & VM_SHARED))
84afd99b
AW
283 return (struct resv_map *)(get_vma_private_data(vma) &
284 ~HPAGE_RESV_MASK);
a1e78772
MG
285 return 0;
286}
287
84afd99b 288static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
a1e78772
MG
289{
290 VM_BUG_ON(!is_vm_hugetlb_page(vma));
291 VM_BUG_ON(vma->vm_flags & VM_SHARED);
292
84afd99b
AW
293 set_vma_private_data(vma, (get_vma_private_data(vma) &
294 HPAGE_RESV_MASK) | (unsigned long)map);
04f2cbe3
MG
295}
296
297static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
298{
04f2cbe3 299 VM_BUG_ON(!is_vm_hugetlb_page(vma));
e7c4b0bf
AW
300 VM_BUG_ON(vma->vm_flags & VM_SHARED);
301
302 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
04f2cbe3
MG
303}
304
305static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
306{
307 VM_BUG_ON(!is_vm_hugetlb_page(vma));
e7c4b0bf
AW
308
309 return (get_vma_private_data(vma) & flag) != 0;
a1e78772
MG
310}
311
312/* Decrement the reserved pages in the hugepage pool by one */
a5516438
AK
313static void decrement_hugepage_resv_vma(struct hstate *h,
314 struct vm_area_struct *vma)
a1e78772 315{
c37f9fb1
AW
316 if (vma->vm_flags & VM_NORESERVE)
317 return;
318
a1e78772
MG
319 if (vma->vm_flags & VM_SHARED) {
320 /* Shared mappings always use reserves */
a5516438 321 h->resv_huge_pages--;
84afd99b 322 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
a1e78772
MG
323 /*
324 * Only the process that called mmap() has reserves for
325 * private mappings.
326 */
a5516438 327 h->resv_huge_pages--;
a1e78772
MG
328 }
329}
330
04f2cbe3 331/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
a1e78772
MG
332void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
333{
334 VM_BUG_ON(!is_vm_hugetlb_page(vma));
335 if (!(vma->vm_flags & VM_SHARED))
336 vma->vm_private_data = (void *)0;
337}
338
339/* Returns true if the VMA has associated reserve pages */
340static int vma_has_private_reserves(struct vm_area_struct *vma)
341{
342 if (vma->vm_flags & VM_SHARED)
343 return 0;
84afd99b 344 if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER))
a1e78772
MG
345 return 0;
346 return 1;
347}
348
a5516438
AK
349static void clear_huge_page(struct page *page,
350 unsigned long addr, unsigned long sz)
79ac6ba4
DG
351{
352 int i;
353
354 might_sleep();
a5516438 355 for (i = 0; i < sz/PAGE_SIZE; i++) {
79ac6ba4 356 cond_resched();
281e0e3b 357 clear_user_highpage(page + i, addr + i * PAGE_SIZE);
79ac6ba4
DG
358 }
359}
360
361static void copy_huge_page(struct page *dst, struct page *src,
9de455b2 362 unsigned long addr, struct vm_area_struct *vma)
79ac6ba4
DG
363{
364 int i;
a5516438 365 struct hstate *h = hstate_vma(vma);
79ac6ba4
DG
366
367 might_sleep();
a5516438 368 for (i = 0; i < pages_per_huge_page(h); i++) {
79ac6ba4 369 cond_resched();
9de455b2 370 copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
79ac6ba4
DG
371 }
372}
373
a5516438 374static void enqueue_huge_page(struct hstate *h, struct page *page)
1da177e4
LT
375{
376 int nid = page_to_nid(page);
a5516438
AK
377 list_add(&page->lru, &h->hugepage_freelists[nid]);
378 h->free_huge_pages++;
379 h->free_huge_pages_node[nid]++;
1da177e4
LT
380}
381
a5516438 382static struct page *dequeue_huge_page(struct hstate *h)
348e1e04
NA
383{
384 int nid;
385 struct page *page = NULL;
386
387 for (nid = 0; nid < MAX_NUMNODES; ++nid) {
a5516438
AK
388 if (!list_empty(&h->hugepage_freelists[nid])) {
389 page = list_entry(h->hugepage_freelists[nid].next,
348e1e04
NA
390 struct page, lru);
391 list_del(&page->lru);
a5516438
AK
392 h->free_huge_pages--;
393 h->free_huge_pages_node[nid]--;
348e1e04
NA
394 break;
395 }
396 }
397 return page;
398}
399
a5516438
AK
400static struct page *dequeue_huge_page_vma(struct hstate *h,
401 struct vm_area_struct *vma,
04f2cbe3 402 unsigned long address, int avoid_reserve)
1da177e4 403{
31a5c6e4 404 int nid;
1da177e4 405 struct page *page = NULL;
480eccf9 406 struct mempolicy *mpol;
19770b32 407 nodemask_t *nodemask;
396faf03 408 struct zonelist *zonelist = huge_zonelist(vma, address,
19770b32 409 htlb_alloc_mask, &mpol, &nodemask);
dd1a239f
MG
410 struct zone *zone;
411 struct zoneref *z;
1da177e4 412
a1e78772
MG
413 /*
414 * A child process with MAP_PRIVATE mappings created by their parent
415 * have no page reserves. This check ensures that reservations are
416 * not "stolen". The child may still get SIGKILLed
417 */
418 if (!vma_has_private_reserves(vma) &&
a5516438 419 h->free_huge_pages - h->resv_huge_pages == 0)
a1e78772
MG
420 return NULL;
421
04f2cbe3 422 /* If reserves cannot be used, ensure enough pages are in the pool */
a5516438 423 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
04f2cbe3
MG
424 return NULL;
425
19770b32
MG
426 for_each_zone_zonelist_nodemask(zone, z, zonelist,
427 MAX_NR_ZONES - 1, nodemask) {
54a6eb5c
MG
428 nid = zone_to_nid(zone);
429 if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
a5516438
AK
430 !list_empty(&h->hugepage_freelists[nid])) {
431 page = list_entry(h->hugepage_freelists[nid].next,
3abf7afd
AM
432 struct page, lru);
433 list_del(&page->lru);
a5516438
AK
434 h->free_huge_pages--;
435 h->free_huge_pages_node[nid]--;
04f2cbe3
MG
436
437 if (!avoid_reserve)
a5516438 438 decrement_hugepage_resv_vma(h, vma);
a1e78772 439
5ab3ee7b 440 break;
3abf7afd 441 }
1da177e4 442 }
52cd3b07 443 mpol_cond_put(mpol);
1da177e4
LT
444 return page;
445}
446
a5516438 447static void update_and_free_page(struct hstate *h, struct page *page)
6af2acb6
AL
448{
449 int i;
a5516438
AK
450
451 h->nr_huge_pages--;
452 h->nr_huge_pages_node[page_to_nid(page)]--;
453 for (i = 0; i < pages_per_huge_page(h); i++) {
6af2acb6
AL
454 page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
455 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
456 1 << PG_private | 1<< PG_writeback);
457 }
458 set_compound_page_dtor(page, NULL);
459 set_page_refcounted(page);
7f2e9525 460 arch_release_hugepage(page);
a5516438 461 __free_pages(page, huge_page_order(h));
6af2acb6
AL
462}
463
e5ff2159
AK
464struct hstate *size_to_hstate(unsigned long size)
465{
466 struct hstate *h;
467
468 for_each_hstate(h) {
469 if (huge_page_size(h) == size)
470 return h;
471 }
472 return NULL;
473}
474
27a85ef1
DG
475static void free_huge_page(struct page *page)
476{
a5516438
AK
477 /*
478 * Can't pass hstate in here because it is called from the
479 * compound page destructor.
480 */
e5ff2159 481 struct hstate *h = page_hstate(page);
7893d1d5 482 int nid = page_to_nid(page);
c79fb75e 483 struct address_space *mapping;
27a85ef1 484
c79fb75e 485 mapping = (struct address_space *) page_private(page);
e5df70ab 486 set_page_private(page, 0);
7893d1d5 487 BUG_ON(page_count(page));
27a85ef1
DG
488 INIT_LIST_HEAD(&page->lru);
489
490 spin_lock(&hugetlb_lock);
a5516438
AK
491 if (h->surplus_huge_pages_node[nid]) {
492 update_and_free_page(h, page);
493 h->surplus_huge_pages--;
494 h->surplus_huge_pages_node[nid]--;
7893d1d5 495 } else {
a5516438 496 enqueue_huge_page(h, page);
7893d1d5 497 }
27a85ef1 498 spin_unlock(&hugetlb_lock);
c79fb75e 499 if (mapping)
9a119c05 500 hugetlb_put_quota(mapping, 1);
27a85ef1
DG
501}
502
7893d1d5
AL
503/*
504 * Increment or decrement surplus_huge_pages. Keep node-specific counters
505 * balanced by operating on them in a round-robin fashion.
506 * Returns 1 if an adjustment was made.
507 */
a5516438 508static int adjust_pool_surplus(struct hstate *h, int delta)
7893d1d5
AL
509{
510 static int prev_nid;
511 int nid = prev_nid;
512 int ret = 0;
513
514 VM_BUG_ON(delta != -1 && delta != 1);
515 do {
516 nid = next_node(nid, node_online_map);
517 if (nid == MAX_NUMNODES)
518 nid = first_node(node_online_map);
519
520 /* To shrink on this node, there must be a surplus page */
a5516438 521 if (delta < 0 && !h->surplus_huge_pages_node[nid])
7893d1d5
AL
522 continue;
523 /* Surplus cannot exceed the total number of pages */
a5516438
AK
524 if (delta > 0 && h->surplus_huge_pages_node[nid] >=
525 h->nr_huge_pages_node[nid])
7893d1d5
AL
526 continue;
527
a5516438
AK
528 h->surplus_huge_pages += delta;
529 h->surplus_huge_pages_node[nid] += delta;
7893d1d5
AL
530 ret = 1;
531 break;
532 } while (nid != prev_nid);
533
534 prev_nid = nid;
535 return ret;
536}
537
a5516438 538static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
b7ba30c6
AK
539{
540 set_compound_page_dtor(page, free_huge_page);
541 spin_lock(&hugetlb_lock);
a5516438
AK
542 h->nr_huge_pages++;
543 h->nr_huge_pages_node[nid]++;
b7ba30c6
AK
544 spin_unlock(&hugetlb_lock);
545 put_page(page); /* free it into the hugepage allocator */
546}
547
a5516438 548static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
1da177e4 549{
1da177e4 550 struct page *page;
f96efd58 551
63b4613c 552 page = alloc_pages_node(nid,
551883ae
NA
553 htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
554 __GFP_REPEAT|__GFP_NOWARN,
a5516438 555 huge_page_order(h));
1da177e4 556 if (page) {
7f2e9525
GS
557 if (arch_prepare_hugepage(page)) {
558 __free_pages(page, HUGETLB_PAGE_ORDER);
7b8ee84d 559 return NULL;
7f2e9525 560 }
a5516438 561 prep_new_huge_page(h, page, nid);
1da177e4 562 }
63b4613c
NA
563
564 return page;
565}
566
a5516438 567static int alloc_fresh_huge_page(struct hstate *h)
63b4613c
NA
568{
569 struct page *page;
570 int start_nid;
571 int next_nid;
572 int ret = 0;
573
a5516438 574 start_nid = h->hugetlb_next_nid;
63b4613c
NA
575
576 do {
a5516438 577 page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid);
63b4613c
NA
578 if (page)
579 ret = 1;
580 /*
581 * Use a helper variable to find the next node and then
582 * copy it back to hugetlb_next_nid afterwards:
583 * otherwise there's a window in which a racer might
584 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
585 * But we don't need to use a spin_lock here: it really
586 * doesn't matter if occasionally a racer chooses the
587 * same nid as we do. Move nid forward in the mask even
588 * if we just successfully allocated a hugepage so that
589 * the next caller gets hugepages on the next node.
590 */
a5516438 591 next_nid = next_node(h->hugetlb_next_nid, node_online_map);
63b4613c
NA
592 if (next_nid == MAX_NUMNODES)
593 next_nid = first_node(node_online_map);
a5516438
AK
594 h->hugetlb_next_nid = next_nid;
595 } while (!page && h->hugetlb_next_nid != start_nid);
63b4613c 596
3b116300
AL
597 if (ret)
598 count_vm_event(HTLB_BUDDY_PGALLOC);
599 else
600 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
601
63b4613c 602 return ret;
1da177e4
LT
603}
604
a5516438
AK
605static struct page *alloc_buddy_huge_page(struct hstate *h,
606 struct vm_area_struct *vma, unsigned long address)
7893d1d5
AL
607{
608 struct page *page;
d1c3fb1f 609 unsigned int nid;
7893d1d5 610
d1c3fb1f
NA
611 /*
612 * Assume we will successfully allocate the surplus page to
613 * prevent racing processes from causing the surplus to exceed
614 * overcommit
615 *
616 * This however introduces a different race, where a process B
617 * tries to grow the static hugepage pool while alloc_pages() is
618 * called by process A. B will only examine the per-node
619 * counters in determining if surplus huge pages can be
620 * converted to normal huge pages in adjust_pool_surplus(). A
621 * won't be able to increment the per-node counter, until the
622 * lock is dropped by B, but B doesn't drop hugetlb_lock until
623 * no more huge pages can be converted from surplus to normal
624 * state (and doesn't try to convert again). Thus, we have a
625 * case where a surplus huge page exists, the pool is grown, and
626 * the surplus huge page still exists after, even though it
627 * should just have been converted to a normal huge page. This
628 * does not leak memory, though, as the hugepage will be freed
629 * once it is out of use. It also does not allow the counters to
630 * go out of whack in adjust_pool_surplus() as we don't modify
631 * the node values until we've gotten the hugepage and only the
632 * per-node value is checked there.
633 */
634 spin_lock(&hugetlb_lock);
a5516438 635 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
d1c3fb1f
NA
636 spin_unlock(&hugetlb_lock);
637 return NULL;
638 } else {
a5516438
AK
639 h->nr_huge_pages++;
640 h->surplus_huge_pages++;
d1c3fb1f
NA
641 }
642 spin_unlock(&hugetlb_lock);
643
551883ae
NA
644 page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
645 __GFP_REPEAT|__GFP_NOWARN,
a5516438 646 huge_page_order(h));
d1c3fb1f
NA
647
648 spin_lock(&hugetlb_lock);
7893d1d5 649 if (page) {
2668db91
AL
650 /*
651 * This page is now managed by the hugetlb allocator and has
652 * no users -- drop the buddy allocator's reference.
653 */
654 put_page_testzero(page);
655 VM_BUG_ON(page_count(page));
d1c3fb1f 656 nid = page_to_nid(page);
7893d1d5 657 set_compound_page_dtor(page, free_huge_page);
d1c3fb1f
NA
658 /*
659 * We incremented the global counters already
660 */
a5516438
AK
661 h->nr_huge_pages_node[nid]++;
662 h->surplus_huge_pages_node[nid]++;
3b116300 663 __count_vm_event(HTLB_BUDDY_PGALLOC);
d1c3fb1f 664 } else {
a5516438
AK
665 h->nr_huge_pages--;
666 h->surplus_huge_pages--;
3b116300 667 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
7893d1d5 668 }
d1c3fb1f 669 spin_unlock(&hugetlb_lock);
7893d1d5
AL
670
671 return page;
672}
673
e4e574b7
AL
674/*
675 * Increase the hugetlb pool such that it can accomodate a reservation
676 * of size 'delta'.
677 */
a5516438 678static int gather_surplus_pages(struct hstate *h, int delta)
e4e574b7
AL
679{
680 struct list_head surplus_list;
681 struct page *page, *tmp;
682 int ret, i;
683 int needed, allocated;
684
a5516438 685 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
ac09b3a1 686 if (needed <= 0) {
a5516438 687 h->resv_huge_pages += delta;
e4e574b7 688 return 0;
ac09b3a1 689 }
e4e574b7
AL
690
691 allocated = 0;
692 INIT_LIST_HEAD(&surplus_list);
693
694 ret = -ENOMEM;
695retry:
696 spin_unlock(&hugetlb_lock);
697 for (i = 0; i < needed; i++) {
a5516438 698 page = alloc_buddy_huge_page(h, NULL, 0);
e4e574b7
AL
699 if (!page) {
700 /*
701 * We were not able to allocate enough pages to
702 * satisfy the entire reservation so we free what
703 * we've allocated so far.
704 */
705 spin_lock(&hugetlb_lock);
706 needed = 0;
707 goto free;
708 }
709
710 list_add(&page->lru, &surplus_list);
711 }
712 allocated += needed;
713
714 /*
715 * After retaking hugetlb_lock, we need to recalculate 'needed'
716 * because either resv_huge_pages or free_huge_pages may have changed.
717 */
718 spin_lock(&hugetlb_lock);
a5516438
AK
719 needed = (h->resv_huge_pages + delta) -
720 (h->free_huge_pages + allocated);
e4e574b7
AL
721 if (needed > 0)
722 goto retry;
723
724 /*
725 * The surplus_list now contains _at_least_ the number of extra pages
726 * needed to accomodate the reservation. Add the appropriate number
727 * of pages to the hugetlb pool and free the extras back to the buddy
ac09b3a1
AL
728 * allocator. Commit the entire reservation here to prevent another
729 * process from stealing the pages as they are added to the pool but
730 * before they are reserved.
e4e574b7
AL
731 */
732 needed += allocated;
a5516438 733 h->resv_huge_pages += delta;
e4e574b7
AL
734 ret = 0;
735free:
19fc3f0a 736 /* Free the needed pages to the hugetlb pool */
e4e574b7 737 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
19fc3f0a
AL
738 if ((--needed) < 0)
739 break;
e4e574b7 740 list_del(&page->lru);
a5516438 741 enqueue_huge_page(h, page);
19fc3f0a
AL
742 }
743
744 /* Free unnecessary surplus pages to the buddy allocator */
745 if (!list_empty(&surplus_list)) {
746 spin_unlock(&hugetlb_lock);
747 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
748 list_del(&page->lru);
af767cbd 749 /*
2668db91
AL
750 * The page has a reference count of zero already, so
751 * call free_huge_page directly instead of using
752 * put_page. This must be done with hugetlb_lock
af767cbd
AL
753 * unlocked which is safe because free_huge_page takes
754 * hugetlb_lock before deciding how to free the page.
755 */
2668db91 756 free_huge_page(page);
af767cbd 757 }
19fc3f0a 758 spin_lock(&hugetlb_lock);
e4e574b7
AL
759 }
760
761 return ret;
762}
763
764/*
765 * When releasing a hugetlb pool reservation, any surplus pages that were
766 * allocated to satisfy the reservation must be explicitly freed if they were
767 * never used.
768 */
a5516438
AK
769static void return_unused_surplus_pages(struct hstate *h,
770 unsigned long unused_resv_pages)
e4e574b7
AL
771{
772 static int nid = -1;
773 struct page *page;
774 unsigned long nr_pages;
775
11320d17
NA
776 /*
777 * We want to release as many surplus pages as possible, spread
778 * evenly across all nodes. Iterate across all nodes until we
779 * can no longer free unreserved surplus pages. This occurs when
780 * the nodes with surplus pages have no free pages.
781 */
782 unsigned long remaining_iterations = num_online_nodes();
783
ac09b3a1 784 /* Uncommit the reservation */
a5516438 785 h->resv_huge_pages -= unused_resv_pages;
ac09b3a1 786
a5516438 787 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
e4e574b7 788
11320d17 789 while (remaining_iterations-- && nr_pages) {
e4e574b7
AL
790 nid = next_node(nid, node_online_map);
791 if (nid == MAX_NUMNODES)
792 nid = first_node(node_online_map);
793
a5516438 794 if (!h->surplus_huge_pages_node[nid])
e4e574b7
AL
795 continue;
796
a5516438
AK
797 if (!list_empty(&h->hugepage_freelists[nid])) {
798 page = list_entry(h->hugepage_freelists[nid].next,
e4e574b7
AL
799 struct page, lru);
800 list_del(&page->lru);
a5516438
AK
801 update_and_free_page(h, page);
802 h->free_huge_pages--;
803 h->free_huge_pages_node[nid]--;
804 h->surplus_huge_pages--;
805 h->surplus_huge_pages_node[nid]--;
e4e574b7 806 nr_pages--;
11320d17 807 remaining_iterations = num_online_nodes();
e4e574b7
AL
808 }
809 }
810}
811
c37f9fb1
AW
812/*
813 * Determine if the huge page at addr within the vma has an associated
814 * reservation. Where it does not we will need to logically increase
815 * reservation and actually increase quota before an allocation can occur.
816 * Where any new reservation would be required the reservation change is
817 * prepared, but not committed. Once the page has been quota'd allocated
818 * an instantiated the change should be committed via vma_commit_reservation.
819 * No action is required on failure.
820 */
a5516438
AK
821static int vma_needs_reservation(struct hstate *h,
822 struct vm_area_struct *vma, unsigned long addr)
c37f9fb1
AW
823{
824 struct address_space *mapping = vma->vm_file->f_mapping;
825 struct inode *inode = mapping->host;
826
827 if (vma->vm_flags & VM_SHARED) {
a5516438 828 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
c37f9fb1
AW
829 return region_chg(&inode->i_mapping->private_list,
830 idx, idx + 1);
831
84afd99b
AW
832 } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
833 return 1;
c37f9fb1 834
84afd99b
AW
835 } else {
836 int err;
a5516438 837 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
84afd99b
AW
838 struct resv_map *reservations = vma_resv_map(vma);
839
840 err = region_chg(&reservations->regions, idx, idx + 1);
841 if (err < 0)
842 return err;
843 return 0;
844 }
c37f9fb1 845}
a5516438
AK
846static void vma_commit_reservation(struct hstate *h,
847 struct vm_area_struct *vma, unsigned long addr)
c37f9fb1
AW
848{
849 struct address_space *mapping = vma->vm_file->f_mapping;
850 struct inode *inode = mapping->host;
851
852 if (vma->vm_flags & VM_SHARED) {
a5516438 853 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
c37f9fb1 854 region_add(&inode->i_mapping->private_list, idx, idx + 1);
84afd99b
AW
855
856 } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
a5516438 857 pgoff_t idx = vma_hugecache_offset(h, vma, addr);
84afd99b
AW
858 struct resv_map *reservations = vma_resv_map(vma);
859
860 /* Mark this page used in the map. */
861 region_add(&reservations->regions, idx, idx + 1);
c37f9fb1
AW
862 }
863}
864
a1e78772 865static struct page *alloc_huge_page(struct vm_area_struct *vma,
04f2cbe3 866 unsigned long addr, int avoid_reserve)
1da177e4 867{
a5516438 868 struct hstate *h = hstate_vma(vma);
348ea204 869 struct page *page;
a1e78772
MG
870 struct address_space *mapping = vma->vm_file->f_mapping;
871 struct inode *inode = mapping->host;
c37f9fb1 872 unsigned int chg;
a1e78772
MG
873
874 /*
875 * Processes that did not create the mapping will have no reserves and
876 * will not have accounted against quota. Check that the quota can be
877 * made before satisfying the allocation
c37f9fb1
AW
878 * MAP_NORESERVE mappings may also need pages and quota allocated
879 * if no reserve mapping overlaps.
a1e78772 880 */
a5516438 881 chg = vma_needs_reservation(h, vma, addr);
c37f9fb1
AW
882 if (chg < 0)
883 return ERR_PTR(chg);
884 if (chg)
a1e78772
MG
885 if (hugetlb_get_quota(inode->i_mapping, chg))
886 return ERR_PTR(-ENOSPC);
1da177e4
LT
887
888 spin_lock(&hugetlb_lock);
a5516438 889 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
1da177e4 890 spin_unlock(&hugetlb_lock);
b45b5bd6 891
68842c9b 892 if (!page) {
a5516438 893 page = alloc_buddy_huge_page(h, vma, addr);
68842c9b 894 if (!page) {
a1e78772 895 hugetlb_put_quota(inode->i_mapping, chg);
68842c9b
KC
896 return ERR_PTR(-VM_FAULT_OOM);
897 }
898 }
348ea204 899
a1e78772
MG
900 set_page_refcounted(page);
901 set_page_private(page, (unsigned long) mapping);
90d8b7e6 902
a5516438 903 vma_commit_reservation(h, vma, addr);
c37f9fb1 904
90d8b7e6 905 return page;
b45b5bd6
DG
906}
907
e5ff2159 908static void __init hugetlb_init_one_hstate(struct hstate *h)
1da177e4
LT
909{
910 unsigned long i;
a5516438 911
1da177e4 912 for (i = 0; i < MAX_NUMNODES; ++i)
a5516438 913 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
1da177e4 914
a5516438 915 h->hugetlb_next_nid = first_node(node_online_map);
63b4613c 916
e5ff2159 917 for (i = 0; i < h->max_huge_pages; ++i) {
a5516438 918 if (!alloc_fresh_huge_page(h))
1da177e4 919 break;
1da177e4 920 }
e5ff2159
AK
921 h->max_huge_pages = h->free_huge_pages = h->nr_huge_pages = i;
922}
923
924static void __init hugetlb_init_hstates(void)
925{
926 struct hstate *h;
927
928 for_each_hstate(h) {
929 hugetlb_init_one_hstate(h);
930 }
931}
932
933static void __init report_hugepages(void)
934{
935 struct hstate *h;
936
937 for_each_hstate(h) {
938 printk(KERN_INFO "Total HugeTLB memory allocated, "
939 "%ld %dMB pages\n",
940 h->free_huge_pages,
941 1 << (h->order + PAGE_SHIFT - 20));
942 }
943}
944
945static int __init hugetlb_init(void)
946{
947 BUILD_BUG_ON(HPAGE_SHIFT == 0);
948
949 if (!size_to_hstate(HPAGE_SIZE)) {
950 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
951 parsed_hstate->max_huge_pages = default_hstate_max_huge_pages;
952 }
953 default_hstate_idx = size_to_hstate(HPAGE_SIZE) - hstates;
954
955 hugetlb_init_hstates();
956
957 report_hugepages();
958
1da177e4
LT
959 return 0;
960}
961module_init(hugetlb_init);
962
e5ff2159
AK
963/* Should be called on processing a hugepagesz=... option */
964void __init hugetlb_add_hstate(unsigned order)
965{
966 struct hstate *h;
967 if (size_to_hstate(PAGE_SIZE << order)) {
968 printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
969 return;
970 }
971 BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
972 BUG_ON(order == 0);
973 h = &hstates[max_hstate++];
974 h->order = order;
975 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
976 hugetlb_init_one_hstate(h);
977 parsed_hstate = h;
978}
979
1da177e4
LT
980static int __init hugetlb_setup(char *s)
981{
e5ff2159
AK
982 unsigned long *mhp;
983
984 /*
985 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
986 * so this hugepages= parameter goes to the "default hstate".
987 */
988 if (!max_hstate)
989 mhp = &default_hstate_max_huge_pages;
990 else
991 mhp = &parsed_hstate->max_huge_pages;
992
993 if (sscanf(s, "%lu", mhp) <= 0)
994 *mhp = 0;
995
1da177e4
LT
996 return 1;
997}
998__setup("hugepages=", hugetlb_setup);
999
8a630112
KC
1000static unsigned int cpuset_mems_nr(unsigned int *array)
1001{
1002 int node;
1003 unsigned int nr = 0;
1004
1005 for_each_node_mask(node, cpuset_current_mems_allowed)
1006 nr += array[node];
1007
1008 return nr;
1009}
1010
1da177e4 1011#ifdef CONFIG_SYSCTL
1da177e4 1012#ifdef CONFIG_HIGHMEM
a5516438 1013static void try_to_free_low(struct hstate *h, unsigned long count)
1da177e4 1014{
4415cc8d
CL
1015 int i;
1016
1da177e4
LT
1017 for (i = 0; i < MAX_NUMNODES; ++i) {
1018 struct page *page, *next;
a5516438
AK
1019 struct list_head *freel = &h->hugepage_freelists[i];
1020 list_for_each_entry_safe(page, next, freel, lru) {
1021 if (count >= h->nr_huge_pages)
6b0c880d 1022 return;
1da177e4
LT
1023 if (PageHighMem(page))
1024 continue;
1025 list_del(&page->lru);
e5ff2159 1026 update_and_free_page(h, page);
a5516438
AK
1027 h->free_huge_pages--;
1028 h->free_huge_pages_node[page_to_nid(page)]--;
1da177e4
LT
1029 }
1030 }
1031}
1032#else
a5516438 1033static inline void try_to_free_low(struct hstate *h, unsigned long count)
1da177e4
LT
1034{
1035}
1036#endif
1037
a5516438 1038#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
e5ff2159 1039static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
1da177e4 1040{
7893d1d5 1041 unsigned long min_count, ret;
1da177e4 1042
7893d1d5
AL
1043 /*
1044 * Increase the pool size
1045 * First take pages out of surplus state. Then make up the
1046 * remaining difference by allocating fresh huge pages.
d1c3fb1f
NA
1047 *
1048 * We might race with alloc_buddy_huge_page() here and be unable
1049 * to convert a surplus huge page to a normal huge page. That is
1050 * not critical, though, it just means the overall size of the
1051 * pool might be one hugepage larger than it needs to be, but
1052 * within all the constraints specified by the sysctls.
7893d1d5 1053 */
1da177e4 1054 spin_lock(&hugetlb_lock);
a5516438
AK
1055 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
1056 if (!adjust_pool_surplus(h, -1))
7893d1d5
AL
1057 break;
1058 }
1059
a5516438 1060 while (count > persistent_huge_pages(h)) {
7893d1d5
AL
1061 /*
1062 * If this allocation races such that we no longer need the
1063 * page, free_huge_page will handle it by freeing the page
1064 * and reducing the surplus.
1065 */
1066 spin_unlock(&hugetlb_lock);
a5516438 1067 ret = alloc_fresh_huge_page(h);
7893d1d5
AL
1068 spin_lock(&hugetlb_lock);
1069 if (!ret)
1070 goto out;
1071
1072 }
7893d1d5
AL
1073
1074 /*
1075 * Decrease the pool size
1076 * First return free pages to the buddy allocator (being careful
1077 * to keep enough around to satisfy reservations). Then place
1078 * pages into surplus state as needed so the pool will shrink
1079 * to the desired size as pages become free.
d1c3fb1f
NA
1080 *
1081 * By placing pages into the surplus state independent of the
1082 * overcommit value, we are allowing the surplus pool size to
1083 * exceed overcommit. There are few sane options here. Since
1084 * alloc_buddy_huge_page() is checking the global counter,
1085 * though, we'll note that we're not allowed to exceed surplus
1086 * and won't grow the pool anywhere else. Not until one of the
1087 * sysctls are changed, or the surplus pages go out of use.
7893d1d5 1088 */
a5516438 1089 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
6b0c880d 1090 min_count = max(count, min_count);
a5516438
AK
1091 try_to_free_low(h, min_count);
1092 while (min_count < persistent_huge_pages(h)) {
1093 struct page *page = dequeue_huge_page(h);
1da177e4
LT
1094 if (!page)
1095 break;
a5516438 1096 update_and_free_page(h, page);
1da177e4 1097 }
a5516438
AK
1098 while (count < persistent_huge_pages(h)) {
1099 if (!adjust_pool_surplus(h, 1))
7893d1d5
AL
1100 break;
1101 }
1102out:
a5516438 1103 ret = persistent_huge_pages(h);
1da177e4 1104 spin_unlock(&hugetlb_lock);
7893d1d5 1105 return ret;
1da177e4
LT
1106}
1107
1108int hugetlb_sysctl_handler(struct ctl_table *table, int write,
1109 struct file *file, void __user *buffer,
1110 size_t *length, loff_t *ppos)
1111{
e5ff2159
AK
1112 struct hstate *h = &default_hstate;
1113 unsigned long tmp;
1114
1115 if (!write)
1116 tmp = h->max_huge_pages;
1117
1118 table->data = &tmp;
1119 table->maxlen = sizeof(unsigned long);
1da177e4 1120 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
e5ff2159
AK
1121
1122 if (write)
1123 h->max_huge_pages = set_max_huge_pages(h, tmp);
1124
1da177e4
LT
1125 return 0;
1126}
396faf03
MG
1127
1128int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
1129 struct file *file, void __user *buffer,
1130 size_t *length, loff_t *ppos)
1131{
1132 proc_dointvec(table, write, file, buffer, length, ppos);
1133 if (hugepages_treat_as_movable)
1134 htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
1135 else
1136 htlb_alloc_mask = GFP_HIGHUSER;
1137 return 0;
1138}
1139
a3d0c6aa
NA
1140int hugetlb_overcommit_handler(struct ctl_table *table, int write,
1141 struct file *file, void __user *buffer,
1142 size_t *length, loff_t *ppos)
1143{
a5516438 1144 struct hstate *h = &default_hstate;
e5ff2159
AK
1145 unsigned long tmp;
1146
1147 if (!write)
1148 tmp = h->nr_overcommit_huge_pages;
1149
1150 table->data = &tmp;
1151 table->maxlen = sizeof(unsigned long);
a3d0c6aa 1152 proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
e5ff2159
AK
1153
1154 if (write) {
1155 spin_lock(&hugetlb_lock);
1156 h->nr_overcommit_huge_pages = tmp;
1157 spin_unlock(&hugetlb_lock);
1158 }
1159
a3d0c6aa
NA
1160 return 0;
1161}
1162
1da177e4
LT
1163#endif /* CONFIG_SYSCTL */
1164
1165int hugetlb_report_meminfo(char *buf)
1166{
a5516438 1167 struct hstate *h = &default_hstate;
1da177e4
LT
1168 return sprintf(buf,
1169 "HugePages_Total: %5lu\n"
1170 "HugePages_Free: %5lu\n"
a43a8c39 1171 "HugePages_Rsvd: %5lu\n"
7893d1d5 1172 "HugePages_Surp: %5lu\n"
1da177e4 1173 "Hugepagesize: %5lu kB\n",
a5516438
AK
1174 h->nr_huge_pages,
1175 h->free_huge_pages,
1176 h->resv_huge_pages,
1177 h->surplus_huge_pages,
1178 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
1da177e4
LT
1179}
1180
1181int hugetlb_report_node_meminfo(int nid, char *buf)
1182{
a5516438 1183 struct hstate *h = &default_hstate;
1da177e4
LT
1184 return sprintf(buf,
1185 "Node %d HugePages_Total: %5u\n"
a1de0919
NA
1186 "Node %d HugePages_Free: %5u\n"
1187 "Node %d HugePages_Surp: %5u\n",
a5516438
AK
1188 nid, h->nr_huge_pages_node[nid],
1189 nid, h->free_huge_pages_node[nid],
1190 nid, h->surplus_huge_pages_node[nid]);
1da177e4
LT
1191}
1192
1da177e4
LT
1193/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1194unsigned long hugetlb_total_pages(void)
1195{
a5516438
AK
1196 struct hstate *h = &default_hstate;
1197 return h->nr_huge_pages * pages_per_huge_page(h);
1da177e4 1198}
1da177e4 1199
a5516438 1200static int hugetlb_acct_memory(struct hstate *h, long delta)
fc1b8a73
MG
1201{
1202 int ret = -ENOMEM;
1203
1204 spin_lock(&hugetlb_lock);
1205 /*
1206 * When cpuset is configured, it breaks the strict hugetlb page
1207 * reservation as the accounting is done on a global variable. Such
1208 * reservation is completely rubbish in the presence of cpuset because
1209 * the reservation is not checked against page availability for the
1210 * current cpuset. Application can still potentially OOM'ed by kernel
1211 * with lack of free htlb page in cpuset that the task is in.
1212 * Attempt to enforce strict accounting with cpuset is almost
1213 * impossible (or too ugly) because cpuset is too fluid that
1214 * task or memory node can be dynamically moved between cpusets.
1215 *
1216 * The change of semantics for shared hugetlb mapping with cpuset is
1217 * undesirable. However, in order to preserve some of the semantics,
1218 * we fall back to check against current free page availability as
1219 * a best attempt and hopefully to minimize the impact of changing
1220 * semantics that cpuset has.
1221 */
1222 if (delta > 0) {
a5516438 1223 if (gather_surplus_pages(h, delta) < 0)
fc1b8a73
MG
1224 goto out;
1225
a5516438
AK
1226 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
1227 return_unused_surplus_pages(h, delta);
fc1b8a73
MG
1228 goto out;
1229 }
1230 }
1231
1232 ret = 0;
1233 if (delta < 0)
a5516438 1234 return_unused_surplus_pages(h, (unsigned long) -delta);
fc1b8a73
MG
1235
1236out:
1237 spin_unlock(&hugetlb_lock);
1238 return ret;
1239}
1240
84afd99b
AW
1241static void hugetlb_vm_op_open(struct vm_area_struct *vma)
1242{
1243 struct resv_map *reservations = vma_resv_map(vma);
1244
1245 /*
1246 * This new VMA should share its siblings reservation map if present.
1247 * The VMA will only ever have a valid reservation map pointer where
1248 * it is being copied for another still existing VMA. As that VMA
1249 * has a reference to the reservation map it cannot dissappear until
1250 * after this open call completes. It is therefore safe to take a
1251 * new reference here without additional locking.
1252 */
1253 if (reservations)
1254 kref_get(&reservations->refs);
1255}
1256
a1e78772
MG
1257static void hugetlb_vm_op_close(struct vm_area_struct *vma)
1258{
a5516438 1259 struct hstate *h = hstate_vma(vma);
84afd99b
AW
1260 struct resv_map *reservations = vma_resv_map(vma);
1261 unsigned long reserve;
1262 unsigned long start;
1263 unsigned long end;
1264
1265 if (reservations) {
a5516438
AK
1266 start = vma_hugecache_offset(h, vma, vma->vm_start);
1267 end = vma_hugecache_offset(h, vma, vma->vm_end);
84afd99b
AW
1268
1269 reserve = (end - start) -
1270 region_count(&reservations->regions, start, end);
1271
1272 kref_put(&reservations->refs, resv_map_release);
1273
1274 if (reserve)
a5516438 1275 hugetlb_acct_memory(h, -reserve);
84afd99b 1276 }
a1e78772
MG
1277}
1278
1da177e4
LT
1279/*
1280 * We cannot handle pagefaults against hugetlb pages at all. They cause
1281 * handle_mm_fault() to try to instantiate regular-sized pages in the
1282 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1283 * this far.
1284 */
d0217ac0 1285static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1da177e4
LT
1286{
1287 BUG();
d0217ac0 1288 return 0;
1da177e4
LT
1289}
1290
1291struct vm_operations_struct hugetlb_vm_ops = {
d0217ac0 1292 .fault = hugetlb_vm_op_fault,
84afd99b 1293 .open = hugetlb_vm_op_open,
a1e78772 1294 .close = hugetlb_vm_op_close,
1da177e4
LT
1295};
1296
1e8f889b
DG
1297static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
1298 int writable)
63551ae0
DG
1299{
1300 pte_t entry;
1301
1e8f889b 1302 if (writable) {
63551ae0
DG
1303 entry =
1304 pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1305 } else {
7f2e9525 1306 entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
63551ae0
DG
1307 }
1308 entry = pte_mkyoung(entry);
1309 entry = pte_mkhuge(entry);
1310
1311 return entry;
1312}
1313
1e8f889b
DG
1314static void set_huge_ptep_writable(struct vm_area_struct *vma,
1315 unsigned long address, pte_t *ptep)
1316{
1317 pte_t entry;
1318
7f2e9525
GS
1319 entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
1320 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
8dab5241 1321 update_mmu_cache(vma, address, entry);
8dab5241 1322 }
1e8f889b
DG
1323}
1324
1325
63551ae0
DG
1326int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
1327 struct vm_area_struct *vma)
1328{
1329 pte_t *src_pte, *dst_pte, entry;
1330 struct page *ptepage;
1c59827d 1331 unsigned long addr;
1e8f889b 1332 int cow;
a5516438
AK
1333 struct hstate *h = hstate_vma(vma);
1334 unsigned long sz = huge_page_size(h);
1e8f889b
DG
1335
1336 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
63551ae0 1337
a5516438 1338 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
c74df32c
HD
1339 src_pte = huge_pte_offset(src, addr);
1340 if (!src_pte)
1341 continue;
a5516438 1342 dst_pte = huge_pte_alloc(dst, addr, sz);
63551ae0
DG
1343 if (!dst_pte)
1344 goto nomem;
c5c99429
LW
1345
1346 /* If the pagetables are shared don't copy or take references */
1347 if (dst_pte == src_pte)
1348 continue;
1349
c74df32c 1350 spin_lock(&dst->page_table_lock);
46478758 1351 spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
7f2e9525 1352 if (!huge_pte_none(huge_ptep_get(src_pte))) {
1e8f889b 1353 if (cow)
7f2e9525
GS
1354 huge_ptep_set_wrprotect(src, addr, src_pte);
1355 entry = huge_ptep_get(src_pte);
1c59827d
HD
1356 ptepage = pte_page(entry);
1357 get_page(ptepage);
1c59827d
HD
1358 set_huge_pte_at(dst, addr, dst_pte, entry);
1359 }
1360 spin_unlock(&src->page_table_lock);
c74df32c 1361 spin_unlock(&dst->page_table_lock);
63551ae0
DG
1362 }
1363 return 0;
1364
1365nomem:
1366 return -ENOMEM;
1367}
1368
502717f4 1369void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
04f2cbe3 1370 unsigned long end, struct page *ref_page)
63551ae0
DG
1371{
1372 struct mm_struct *mm = vma->vm_mm;
1373 unsigned long address;
c7546f8f 1374 pte_t *ptep;
63551ae0
DG
1375 pte_t pte;
1376 struct page *page;
fe1668ae 1377 struct page *tmp;
a5516438
AK
1378 struct hstate *h = hstate_vma(vma);
1379 unsigned long sz = huge_page_size(h);
1380
c0a499c2
CK
1381 /*
1382 * A page gathering list, protected by per file i_mmap_lock. The
1383 * lock is used to avoid list corruption from multiple unmapping
1384 * of the same page since we are using page->lru.
1385 */
fe1668ae 1386 LIST_HEAD(page_list);
63551ae0
DG
1387
1388 WARN_ON(!is_vm_hugetlb_page(vma));
a5516438
AK
1389 BUG_ON(start & ~huge_page_mask(h));
1390 BUG_ON(end & ~huge_page_mask(h));
63551ae0 1391
508034a3 1392 spin_lock(&mm->page_table_lock);
a5516438 1393 for (address = start; address < end; address += sz) {
c7546f8f 1394 ptep = huge_pte_offset(mm, address);
4c887265 1395 if (!ptep)
c7546f8f
DG
1396 continue;
1397
39dde65c
CK
1398 if (huge_pmd_unshare(mm, &address, ptep))
1399 continue;
1400
04f2cbe3
MG
1401 /*
1402 * If a reference page is supplied, it is because a specific
1403 * page is being unmapped, not a range. Ensure the page we
1404 * are about to unmap is the actual page of interest.
1405 */
1406 if (ref_page) {
1407 pte = huge_ptep_get(ptep);
1408 if (huge_pte_none(pte))
1409 continue;
1410 page = pte_page(pte);
1411 if (page != ref_page)
1412 continue;
1413
1414 /*
1415 * Mark the VMA as having unmapped its page so that
1416 * future faults in this VMA will fail rather than
1417 * looking like data was lost
1418 */
1419 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
1420 }
1421
c7546f8f 1422 pte = huge_ptep_get_and_clear(mm, address, ptep);
7f2e9525 1423 if (huge_pte_none(pte))
63551ae0 1424 continue;
c7546f8f 1425
63551ae0 1426 page = pte_page(pte);
6649a386
KC
1427 if (pte_dirty(pte))
1428 set_page_dirty(page);
fe1668ae 1429 list_add(&page->lru, &page_list);
63551ae0 1430 }
1da177e4 1431 spin_unlock(&mm->page_table_lock);
508034a3 1432 flush_tlb_range(vma, start, end);
fe1668ae
CK
1433 list_for_each_entry_safe(page, tmp, &page_list, lru) {
1434 list_del(&page->lru);
1435 put_page(page);
1436 }
1da177e4 1437}
63551ae0 1438
502717f4 1439void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
04f2cbe3 1440 unsigned long end, struct page *ref_page)
502717f4 1441{
a137e1cc
AK
1442 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
1443 __unmap_hugepage_range(vma, start, end, ref_page);
1444 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
502717f4
CK
1445}
1446
04f2cbe3
MG
1447/*
1448 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1449 * mappping it owns the reserve page for. The intention is to unmap the page
1450 * from other VMAs and let the children be SIGKILLed if they are faulting the
1451 * same region.
1452 */
1453int unmap_ref_private(struct mm_struct *mm,
1454 struct vm_area_struct *vma,
1455 struct page *page,
1456 unsigned long address)
1457{
1458 struct vm_area_struct *iter_vma;
1459 struct address_space *mapping;
1460 struct prio_tree_iter iter;
1461 pgoff_t pgoff;
1462
1463 /*
1464 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1465 * from page cache lookup which is in HPAGE_SIZE units.
1466 */
1467 address = address & huge_page_mask(hstate_vma(vma));
1468 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
1469 + (vma->vm_pgoff >> PAGE_SHIFT);
1470 mapping = (struct address_space *)page_private(page);
1471
1472 vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1473 /* Do not unmap the current VMA */
1474 if (iter_vma == vma)
1475 continue;
1476
1477 /*
1478 * Unmap the page from other VMAs without their own reserves.
1479 * They get marked to be SIGKILLed if they fault in these
1480 * areas. This is because a future no-page fault on this VMA
1481 * could insert a zeroed page instead of the data existing
1482 * from the time of fork. This would look like data corruption
1483 */
1484 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
1485 unmap_hugepage_range(iter_vma,
1486 address, address + HPAGE_SIZE,
1487 page);
1488 }
1489
1490 return 1;
1491}
1492
1e8f889b 1493static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
04f2cbe3
MG
1494 unsigned long address, pte_t *ptep, pte_t pte,
1495 struct page *pagecache_page)
1e8f889b 1496{
a5516438 1497 struct hstate *h = hstate_vma(vma);
1e8f889b 1498 struct page *old_page, *new_page;
79ac6ba4 1499 int avoidcopy;
04f2cbe3 1500 int outside_reserve = 0;
1e8f889b
DG
1501
1502 old_page = pte_page(pte);
1503
04f2cbe3 1504retry_avoidcopy:
1e8f889b
DG
1505 /* If no-one else is actually using this page, avoid the copy
1506 * and just make the page writable */
1507 avoidcopy = (page_count(old_page) == 1);
1508 if (avoidcopy) {
1509 set_huge_ptep_writable(vma, address, ptep);
83c54070 1510 return 0;
1e8f889b
DG
1511 }
1512
04f2cbe3
MG
1513 /*
1514 * If the process that created a MAP_PRIVATE mapping is about to
1515 * perform a COW due to a shared page count, attempt to satisfy
1516 * the allocation without using the existing reserves. The pagecache
1517 * page is used to determine if the reserve at this address was
1518 * consumed or not. If reserves were used, a partial faulted mapping
1519 * at the time of fork() could consume its reserves on COW instead
1520 * of the full address range.
1521 */
1522 if (!(vma->vm_flags & VM_SHARED) &&
1523 is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
1524 old_page != pagecache_page)
1525 outside_reserve = 1;
1526
1e8f889b 1527 page_cache_get(old_page);
04f2cbe3 1528 new_page = alloc_huge_page(vma, address, outside_reserve);
1e8f889b 1529
2fc39cec 1530 if (IS_ERR(new_page)) {
1e8f889b 1531 page_cache_release(old_page);
04f2cbe3
MG
1532
1533 /*
1534 * If a process owning a MAP_PRIVATE mapping fails to COW,
1535 * it is due to references held by a child and an insufficient
1536 * huge page pool. To guarantee the original mappers
1537 * reliability, unmap the page from child processes. The child
1538 * may get SIGKILLed if it later faults.
1539 */
1540 if (outside_reserve) {
1541 BUG_ON(huge_pte_none(pte));
1542 if (unmap_ref_private(mm, vma, old_page, address)) {
1543 BUG_ON(page_count(old_page) != 1);
1544 BUG_ON(huge_pte_none(pte));
1545 goto retry_avoidcopy;
1546 }
1547 WARN_ON_ONCE(1);
1548 }
1549
2fc39cec 1550 return -PTR_ERR(new_page);
1e8f889b
DG
1551 }
1552
1553 spin_unlock(&mm->page_table_lock);
9de455b2 1554 copy_huge_page(new_page, old_page, address, vma);
0ed361de 1555 __SetPageUptodate(new_page);
1e8f889b
DG
1556 spin_lock(&mm->page_table_lock);
1557
a5516438 1558 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
7f2e9525 1559 if (likely(pte_same(huge_ptep_get(ptep), pte))) {
1e8f889b 1560 /* Break COW */
8fe627ec 1561 huge_ptep_clear_flush(vma, address, ptep);
1e8f889b
DG
1562 set_huge_pte_at(mm, address, ptep,
1563 make_huge_pte(vma, new_page, 1));
1564 /* Make the old page be freed below */
1565 new_page = old_page;
1566 }
1567 page_cache_release(new_page);
1568 page_cache_release(old_page);
83c54070 1569 return 0;
1e8f889b
DG
1570}
1571
04f2cbe3 1572/* Return the pagecache page at a given address within a VMA */
a5516438
AK
1573static struct page *hugetlbfs_pagecache_page(struct hstate *h,
1574 struct vm_area_struct *vma, unsigned long address)
04f2cbe3
MG
1575{
1576 struct address_space *mapping;
e7c4b0bf 1577 pgoff_t idx;
04f2cbe3
MG
1578
1579 mapping = vma->vm_file->f_mapping;
a5516438 1580 idx = vma_hugecache_offset(h, vma, address);
04f2cbe3
MG
1581
1582 return find_lock_page(mapping, idx);
1583}
1584
a1ed3dda 1585static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1e8f889b 1586 unsigned long address, pte_t *ptep, int write_access)
ac9b9c66 1587{
a5516438 1588 struct hstate *h = hstate_vma(vma);
ac9b9c66 1589 int ret = VM_FAULT_SIGBUS;
e7c4b0bf 1590 pgoff_t idx;
4c887265 1591 unsigned long size;
4c887265
AL
1592 struct page *page;
1593 struct address_space *mapping;
1e8f889b 1594 pte_t new_pte;
4c887265 1595
04f2cbe3
MG
1596 /*
1597 * Currently, we are forced to kill the process in the event the
1598 * original mapper has unmapped pages from the child due to a failed
1599 * COW. Warn that such a situation has occured as it may not be obvious
1600 */
1601 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
1602 printk(KERN_WARNING
1603 "PID %d killed due to inadequate hugepage pool\n",
1604 current->pid);
1605 return ret;
1606 }
1607
4c887265 1608 mapping = vma->vm_file->f_mapping;
a5516438 1609 idx = vma_hugecache_offset(h, vma, address);
4c887265
AL
1610
1611 /*
1612 * Use page lock to guard against racing truncation
1613 * before we get page_table_lock.
1614 */
6bda666a
CL
1615retry:
1616 page = find_lock_page(mapping, idx);
1617 if (!page) {
a5516438 1618 size = i_size_read(mapping->host) >> huge_page_shift(h);
ebed4bfc
HD
1619 if (idx >= size)
1620 goto out;
04f2cbe3 1621 page = alloc_huge_page(vma, address, 0);
2fc39cec
AL
1622 if (IS_ERR(page)) {
1623 ret = -PTR_ERR(page);
6bda666a
CL
1624 goto out;
1625 }
a5516438 1626 clear_huge_page(page, address, huge_page_size(h));
0ed361de 1627 __SetPageUptodate(page);
ac9b9c66 1628
6bda666a
CL
1629 if (vma->vm_flags & VM_SHARED) {
1630 int err;
45c682a6 1631 struct inode *inode = mapping->host;
6bda666a
CL
1632
1633 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
1634 if (err) {
1635 put_page(page);
6bda666a
CL
1636 if (err == -EEXIST)
1637 goto retry;
1638 goto out;
1639 }
45c682a6
KC
1640
1641 spin_lock(&inode->i_lock);
a5516438 1642 inode->i_blocks += blocks_per_huge_page(h);
45c682a6 1643 spin_unlock(&inode->i_lock);
6bda666a
CL
1644 } else
1645 lock_page(page);
1646 }
1e8f889b 1647
ac9b9c66 1648 spin_lock(&mm->page_table_lock);
a5516438 1649 size = i_size_read(mapping->host) >> huge_page_shift(h);
4c887265
AL
1650 if (idx >= size)
1651 goto backout;
1652
83c54070 1653 ret = 0;
7f2e9525 1654 if (!huge_pte_none(huge_ptep_get(ptep)))
4c887265
AL
1655 goto backout;
1656
1e8f889b
DG
1657 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
1658 && (vma->vm_flags & VM_SHARED)));
1659 set_huge_pte_at(mm, address, ptep, new_pte);
1660
1661 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1662 /* Optimization, do the COW without a second fault */
04f2cbe3 1663 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
1e8f889b
DG
1664 }
1665
ac9b9c66 1666 spin_unlock(&mm->page_table_lock);
4c887265
AL
1667 unlock_page(page);
1668out:
ac9b9c66 1669 return ret;
4c887265
AL
1670
1671backout:
1672 spin_unlock(&mm->page_table_lock);
4c887265
AL
1673 unlock_page(page);
1674 put_page(page);
1675 goto out;
ac9b9c66
HD
1676}
1677
86e5216f
AL
1678int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1679 unsigned long address, int write_access)
1680{
1681 pte_t *ptep;
1682 pte_t entry;
1e8f889b 1683 int ret;
3935baa9 1684 static DEFINE_MUTEX(hugetlb_instantiation_mutex);
a5516438 1685 struct hstate *h = hstate_vma(vma);
86e5216f 1686
a5516438 1687 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
86e5216f
AL
1688 if (!ptep)
1689 return VM_FAULT_OOM;
1690
3935baa9
DG
1691 /*
1692 * Serialize hugepage allocation and instantiation, so that we don't
1693 * get spurious allocation failures if two CPUs race to instantiate
1694 * the same page in the page cache.
1695 */
1696 mutex_lock(&hugetlb_instantiation_mutex);
7f2e9525
GS
1697 entry = huge_ptep_get(ptep);
1698 if (huge_pte_none(entry)) {
3935baa9
DG
1699 ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
1700 mutex_unlock(&hugetlb_instantiation_mutex);
1701 return ret;
1702 }
86e5216f 1703
83c54070 1704 ret = 0;
1e8f889b
DG
1705
1706 spin_lock(&mm->page_table_lock);
1707 /* Check for a racing update before calling hugetlb_cow */
7f2e9525 1708 if (likely(pte_same(entry, huge_ptep_get(ptep))))
04f2cbe3
MG
1709 if (write_access && !pte_write(entry)) {
1710 struct page *page;
a5516438 1711 page = hugetlbfs_pagecache_page(h, vma, address);
04f2cbe3
MG
1712 ret = hugetlb_cow(mm, vma, address, ptep, entry, page);
1713 if (page) {
1714 unlock_page(page);
1715 put_page(page);
1716 }
1717 }
1e8f889b 1718 spin_unlock(&mm->page_table_lock);
3935baa9 1719 mutex_unlock(&hugetlb_instantiation_mutex);
1e8f889b
DG
1720
1721 return ret;
86e5216f
AL
1722}
1723
63551ae0
DG
1724int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
1725 struct page **pages, struct vm_area_struct **vmas,
5b23dbe8
AL
1726 unsigned long *position, int *length, int i,
1727 int write)
63551ae0 1728{
d5d4b0aa
CK
1729 unsigned long pfn_offset;
1730 unsigned long vaddr = *position;
63551ae0 1731 int remainder = *length;
a5516438 1732 struct hstate *h = hstate_vma(vma);
63551ae0 1733
1c59827d 1734 spin_lock(&mm->page_table_lock);
63551ae0 1735 while (vaddr < vma->vm_end && remainder) {
4c887265
AL
1736 pte_t *pte;
1737 struct page *page;
63551ae0 1738
4c887265
AL
1739 /*
1740 * Some archs (sparc64, sh*) have multiple pte_ts to
1741 * each hugepage. We have to make * sure we get the
1742 * first, for the page indexing below to work.
1743 */
a5516438 1744 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
63551ae0 1745
7f2e9525
GS
1746 if (!pte || huge_pte_none(huge_ptep_get(pte)) ||
1747 (write && !pte_write(huge_ptep_get(pte)))) {
4c887265 1748 int ret;
63551ae0 1749
4c887265 1750 spin_unlock(&mm->page_table_lock);
5b23dbe8 1751 ret = hugetlb_fault(mm, vma, vaddr, write);
4c887265 1752 spin_lock(&mm->page_table_lock);
a89182c7 1753 if (!(ret & VM_FAULT_ERROR))
4c887265 1754 continue;
63551ae0 1755
4c887265
AL
1756 remainder = 0;
1757 if (!i)
1758 i = -EFAULT;
1759 break;
1760 }
1761
a5516438 1762 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
7f2e9525 1763 page = pte_page(huge_ptep_get(pte));
d5d4b0aa 1764same_page:
d6692183
CK
1765 if (pages) {
1766 get_page(page);
d5d4b0aa 1767 pages[i] = page + pfn_offset;
d6692183 1768 }
63551ae0
DG
1769
1770 if (vmas)
1771 vmas[i] = vma;
1772
1773 vaddr += PAGE_SIZE;
d5d4b0aa 1774 ++pfn_offset;
63551ae0
DG
1775 --remainder;
1776 ++i;
d5d4b0aa 1777 if (vaddr < vma->vm_end && remainder &&
a5516438 1778 pfn_offset < pages_per_huge_page(h)) {
d5d4b0aa
CK
1779 /*
1780 * We use pfn_offset to avoid touching the pageframes
1781 * of this compound page.
1782 */
1783 goto same_page;
1784 }
63551ae0 1785 }
1c59827d 1786 spin_unlock(&mm->page_table_lock);
63551ae0
DG
1787 *length = remainder;
1788 *position = vaddr;
1789
1790 return i;
1791}
8f860591
ZY
1792
1793void hugetlb_change_protection(struct vm_area_struct *vma,
1794 unsigned long address, unsigned long end, pgprot_t newprot)
1795{
1796 struct mm_struct *mm = vma->vm_mm;
1797 unsigned long start = address;
1798 pte_t *ptep;
1799 pte_t pte;
a5516438 1800 struct hstate *h = hstate_vma(vma);
8f860591
ZY
1801
1802 BUG_ON(address >= end);
1803 flush_cache_range(vma, address, end);
1804
39dde65c 1805 spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
8f860591 1806 spin_lock(&mm->page_table_lock);
a5516438 1807 for (; address < end; address += huge_page_size(h)) {
8f860591
ZY
1808 ptep = huge_pte_offset(mm, address);
1809 if (!ptep)
1810 continue;
39dde65c
CK
1811 if (huge_pmd_unshare(mm, &address, ptep))
1812 continue;
7f2e9525 1813 if (!huge_pte_none(huge_ptep_get(ptep))) {
8f860591
ZY
1814 pte = huge_ptep_get_and_clear(mm, address, ptep);
1815 pte = pte_mkhuge(pte_modify(pte, newprot));
1816 set_huge_pte_at(mm, address, ptep, pte);
8f860591
ZY
1817 }
1818 }
1819 spin_unlock(&mm->page_table_lock);
39dde65c 1820 spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
8f860591
ZY
1821
1822 flush_tlb_range(vma, start, end);
1823}
1824
a1e78772
MG
1825int hugetlb_reserve_pages(struct inode *inode,
1826 long from, long to,
1827 struct vm_area_struct *vma)
e4e574b7
AL
1828{
1829 long ret, chg;
a5516438 1830 struct hstate *h = hstate_inode(inode);
e4e574b7 1831
c37f9fb1
AW
1832 if (vma && vma->vm_flags & VM_NORESERVE)
1833 return 0;
1834
a1e78772
MG
1835 /*
1836 * Shared mappings base their reservation on the number of pages that
1837 * are already allocated on behalf of the file. Private mappings need
1838 * to reserve the full area even if read-only as mprotect() may be
1839 * called to make the mapping read-write. Assume !vma is a shm mapping
1840 */
1841 if (!vma || vma->vm_flags & VM_SHARED)
1842 chg = region_chg(&inode->i_mapping->private_list, from, to);
1843 else {
84afd99b
AW
1844 struct resv_map *resv_map = resv_map_alloc();
1845 if (!resv_map)
1846 return -ENOMEM;
1847
a1e78772 1848 chg = to - from;
84afd99b
AW
1849
1850 set_vma_resv_map(vma, resv_map);
04f2cbe3 1851 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
a1e78772
MG
1852 }
1853
e4e574b7
AL
1854 if (chg < 0)
1855 return chg;
8a630112 1856
90d8b7e6
AL
1857 if (hugetlb_get_quota(inode->i_mapping, chg))
1858 return -ENOSPC;
a5516438 1859 ret = hugetlb_acct_memory(h, chg);
68842c9b
KC
1860 if (ret < 0) {
1861 hugetlb_put_quota(inode->i_mapping, chg);
a43a8c39 1862 return ret;
68842c9b 1863 }
a1e78772
MG
1864 if (!vma || vma->vm_flags & VM_SHARED)
1865 region_add(&inode->i_mapping->private_list, from, to);
a43a8c39
CK
1866 return 0;
1867}
1868
1869void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
1870{
a5516438 1871 struct hstate *h = hstate_inode(inode);
a43a8c39 1872 long chg = region_truncate(&inode->i_mapping->private_list, offset);
45c682a6
KC
1873
1874 spin_lock(&inode->i_lock);
a5516438 1875 inode->i_blocks -= blocks_per_huge_page(h);
45c682a6
KC
1876 spin_unlock(&inode->i_lock);
1877
90d8b7e6 1878 hugetlb_put_quota(inode->i_mapping, (chg - freed));
a5516438 1879 hugetlb_acct_memory(h, -(chg - freed));
a43a8c39 1880}