2 * Generic hugetlb support.
3 * (C) William Irwin, April 2004
6 #include <linux/list.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
10 #include <linux/sysctl.h>
11 #include <linux/highmem.h>
12 #include <linux/nodemask.h>
13 #include <linux/pagemap.h>
14 #include <linux/mempolicy.h>
15 #include <linux/cpuset.h>
16 #include <linux/mutex.h>
19 #include <asm/pgtable.h>
21 #include <linux/hugetlb.h>
24 const unsigned long hugetlb_zero
= 0, hugetlb_infinity
= ~0UL;
25 static unsigned long nr_huge_pages
, free_huge_pages
, resv_huge_pages
;
26 static unsigned long surplus_huge_pages
;
27 static unsigned long nr_overcommit_huge_pages
;
28 unsigned long max_huge_pages
;
29 unsigned long sysctl_overcommit_huge_pages
;
30 static struct list_head hugepage_freelists
[MAX_NUMNODES
];
31 static unsigned int nr_huge_pages_node
[MAX_NUMNODES
];
32 static unsigned int free_huge_pages_node
[MAX_NUMNODES
];
33 static unsigned int surplus_huge_pages_node
[MAX_NUMNODES
];
34 static gfp_t htlb_alloc_mask
= GFP_HIGHUSER
;
35 unsigned long hugepages_treat_as_movable
;
36 static int hugetlb_next_nid
;
39 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
41 static DEFINE_SPINLOCK(hugetlb_lock
);
43 #define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1))
44 #define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2))
45 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
47 * These helpers are used to track how many pages are reserved for
48 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
49 * is guaranteed to have their future faults succeed.
51 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
52 * the reserve counters are updated with the hugetlb_lock held. It is safe
53 * to reset the VMA at fork() time as it is not in use yet and there is no
54 * chance of the global counters getting corrupted as a result of the values.
56 static unsigned long vma_resv_huge_pages(struct vm_area_struct
*vma
)
58 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
59 if (!(vma
->vm_flags
& VM_SHARED
))
60 return (unsigned long)vma
->vm_private_data
& ~HPAGE_RESV_MASK
;
64 static void set_vma_resv_huge_pages(struct vm_area_struct
*vma
,
65 unsigned long reserve
)
68 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
69 VM_BUG_ON(vma
->vm_flags
& VM_SHARED
);
71 flags
= (unsigned long)vma
->vm_private_data
& HPAGE_RESV_MASK
;
72 vma
->vm_private_data
= (void *)(reserve
| flags
);
75 static void set_vma_resv_flags(struct vm_area_struct
*vma
, unsigned long flags
)
77 unsigned long reserveflags
= (unsigned long)vma
->vm_private_data
;
78 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
79 vma
->vm_private_data
= (void *)(reserveflags
| flags
);
82 static int is_vma_resv_set(struct vm_area_struct
*vma
, unsigned long flag
)
84 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
85 return ((unsigned long)vma
->vm_private_data
& flag
) != 0;
88 /* Decrement the reserved pages in the hugepage pool by one */
89 static void decrement_hugepage_resv_vma(struct vm_area_struct
*vma
)
91 if (vma
->vm_flags
& VM_SHARED
) {
92 /* Shared mappings always use reserves */
96 * Only the process that called mmap() has reserves for
99 if (is_vma_resv_set(vma
, HPAGE_RESV_OWNER
)) {
100 unsigned long flags
, reserve
;
102 flags
= (unsigned long)vma
->vm_private_data
&
104 reserve
= (unsigned long)vma
->vm_private_data
- 1;
105 vma
->vm_private_data
= (void *)(reserve
| flags
);
110 /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
111 void reset_vma_resv_huge_pages(struct vm_area_struct
*vma
)
113 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
114 if (!(vma
->vm_flags
& VM_SHARED
))
115 vma
->vm_private_data
= (void *)0;
118 /* Returns true if the VMA has associated reserve pages */
119 static int vma_has_private_reserves(struct vm_area_struct
*vma
)
121 if (vma
->vm_flags
& VM_SHARED
)
123 if (!vma_resv_huge_pages(vma
))
128 static void clear_huge_page(struct page
*page
, unsigned long addr
)
133 for (i
= 0; i
< (HPAGE_SIZE
/PAGE_SIZE
); i
++) {
135 clear_user_highpage(page
+ i
, addr
+ i
* PAGE_SIZE
);
139 static void copy_huge_page(struct page
*dst
, struct page
*src
,
140 unsigned long addr
, struct vm_area_struct
*vma
)
145 for (i
= 0; i
< HPAGE_SIZE
/PAGE_SIZE
; i
++) {
147 copy_user_highpage(dst
+ i
, src
+ i
, addr
+ i
*PAGE_SIZE
, vma
);
151 static void enqueue_huge_page(struct page
*page
)
153 int nid
= page_to_nid(page
);
154 list_add(&page
->lru
, &hugepage_freelists
[nid
]);
156 free_huge_pages_node
[nid
]++;
159 static struct page
*dequeue_huge_page(void)
162 struct page
*page
= NULL
;
164 for (nid
= 0; nid
< MAX_NUMNODES
; ++nid
) {
165 if (!list_empty(&hugepage_freelists
[nid
])) {
166 page
= list_entry(hugepage_freelists
[nid
].next
,
168 list_del(&page
->lru
);
170 free_huge_pages_node
[nid
]--;
177 static struct page
*dequeue_huge_page_vma(struct vm_area_struct
*vma
,
178 unsigned long address
, int avoid_reserve
)
181 struct page
*page
= NULL
;
182 struct mempolicy
*mpol
;
183 nodemask_t
*nodemask
;
184 struct zonelist
*zonelist
= huge_zonelist(vma
, address
,
185 htlb_alloc_mask
, &mpol
, &nodemask
);
190 * A child process with MAP_PRIVATE mappings created by their parent
191 * have no page reserves. This check ensures that reservations are
192 * not "stolen". The child may still get SIGKILLed
194 if (!vma_has_private_reserves(vma
) &&
195 free_huge_pages
- resv_huge_pages
== 0)
198 /* If reserves cannot be used, ensure enough pages are in the pool */
199 if (avoid_reserve
&& free_huge_pages
- resv_huge_pages
== 0)
202 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
,
203 MAX_NR_ZONES
- 1, nodemask
) {
204 nid
= zone_to_nid(zone
);
205 if (cpuset_zone_allowed_softwall(zone
, htlb_alloc_mask
) &&
206 !list_empty(&hugepage_freelists
[nid
])) {
207 page
= list_entry(hugepage_freelists
[nid
].next
,
209 list_del(&page
->lru
);
211 free_huge_pages_node
[nid
]--;
214 decrement_hugepage_resv_vma(vma
);
223 static void update_and_free_page(struct page
*page
)
227 nr_huge_pages_node
[page_to_nid(page
)]--;
228 for (i
= 0; i
< (HPAGE_SIZE
/ PAGE_SIZE
); i
++) {
229 page
[i
].flags
&= ~(1 << PG_locked
| 1 << PG_error
| 1 << PG_referenced
|
230 1 << PG_dirty
| 1 << PG_active
| 1 << PG_reserved
|
231 1 << PG_private
| 1<< PG_writeback
);
233 set_compound_page_dtor(page
, NULL
);
234 set_page_refcounted(page
);
235 arch_release_hugepage(page
);
236 __free_pages(page
, HUGETLB_PAGE_ORDER
);
239 static void free_huge_page(struct page
*page
)
241 int nid
= page_to_nid(page
);
242 struct address_space
*mapping
;
244 mapping
= (struct address_space
*) page_private(page
);
245 set_page_private(page
, 0);
246 BUG_ON(page_count(page
));
247 INIT_LIST_HEAD(&page
->lru
);
249 spin_lock(&hugetlb_lock
);
250 if (surplus_huge_pages_node
[nid
]) {
251 update_and_free_page(page
);
252 surplus_huge_pages
--;
253 surplus_huge_pages_node
[nid
]--;
255 enqueue_huge_page(page
);
257 spin_unlock(&hugetlb_lock
);
259 hugetlb_put_quota(mapping
, 1);
263 * Increment or decrement surplus_huge_pages. Keep node-specific counters
264 * balanced by operating on them in a round-robin fashion.
265 * Returns 1 if an adjustment was made.
267 static int adjust_pool_surplus(int delta
)
273 VM_BUG_ON(delta
!= -1 && delta
!= 1);
275 nid
= next_node(nid
, node_online_map
);
276 if (nid
== MAX_NUMNODES
)
277 nid
= first_node(node_online_map
);
279 /* To shrink on this node, there must be a surplus page */
280 if (delta
< 0 && !surplus_huge_pages_node
[nid
])
282 /* Surplus cannot exceed the total number of pages */
283 if (delta
> 0 && surplus_huge_pages_node
[nid
] >=
284 nr_huge_pages_node
[nid
])
287 surplus_huge_pages
+= delta
;
288 surplus_huge_pages_node
[nid
] += delta
;
291 } while (nid
!= prev_nid
);
297 static struct page
*alloc_fresh_huge_page_node(int nid
)
301 page
= alloc_pages_node(nid
,
302 htlb_alloc_mask
|__GFP_COMP
|__GFP_THISNODE
|
303 __GFP_REPEAT
|__GFP_NOWARN
,
306 if (arch_prepare_hugepage(page
)) {
307 __free_pages(page
, HUGETLB_PAGE_ORDER
);
310 set_compound_page_dtor(page
, free_huge_page
);
311 spin_lock(&hugetlb_lock
);
313 nr_huge_pages_node
[nid
]++;
314 spin_unlock(&hugetlb_lock
);
315 put_page(page
); /* free it into the hugepage allocator */
321 static int alloc_fresh_huge_page(void)
328 start_nid
= hugetlb_next_nid
;
331 page
= alloc_fresh_huge_page_node(hugetlb_next_nid
);
335 * Use a helper variable to find the next node and then
336 * copy it back to hugetlb_next_nid afterwards:
337 * otherwise there's a window in which a racer might
338 * pass invalid nid MAX_NUMNODES to alloc_pages_node.
339 * But we don't need to use a spin_lock here: it really
340 * doesn't matter if occasionally a racer chooses the
341 * same nid as we do. Move nid forward in the mask even
342 * if we just successfully allocated a hugepage so that
343 * the next caller gets hugepages on the next node.
345 next_nid
= next_node(hugetlb_next_nid
, node_online_map
);
346 if (next_nid
== MAX_NUMNODES
)
347 next_nid
= first_node(node_online_map
);
348 hugetlb_next_nid
= next_nid
;
349 } while (!page
&& hugetlb_next_nid
!= start_nid
);
352 count_vm_event(HTLB_BUDDY_PGALLOC
);
354 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
359 static struct page
*alloc_buddy_huge_page(struct vm_area_struct
*vma
,
360 unsigned long address
)
366 * Assume we will successfully allocate the surplus page to
367 * prevent racing processes from causing the surplus to exceed
370 * This however introduces a different race, where a process B
371 * tries to grow the static hugepage pool while alloc_pages() is
372 * called by process A. B will only examine the per-node
373 * counters in determining if surplus huge pages can be
374 * converted to normal huge pages in adjust_pool_surplus(). A
375 * won't be able to increment the per-node counter, until the
376 * lock is dropped by B, but B doesn't drop hugetlb_lock until
377 * no more huge pages can be converted from surplus to normal
378 * state (and doesn't try to convert again). Thus, we have a
379 * case where a surplus huge page exists, the pool is grown, and
380 * the surplus huge page still exists after, even though it
381 * should just have been converted to a normal huge page. This
382 * does not leak memory, though, as the hugepage will be freed
383 * once it is out of use. It also does not allow the counters to
384 * go out of whack in adjust_pool_surplus() as we don't modify
385 * the node values until we've gotten the hugepage and only the
386 * per-node value is checked there.
388 spin_lock(&hugetlb_lock
);
389 if (surplus_huge_pages
>= nr_overcommit_huge_pages
) {
390 spin_unlock(&hugetlb_lock
);
394 surplus_huge_pages
++;
396 spin_unlock(&hugetlb_lock
);
398 page
= alloc_pages(htlb_alloc_mask
|__GFP_COMP
|
399 __GFP_REPEAT
|__GFP_NOWARN
,
402 spin_lock(&hugetlb_lock
);
405 * This page is now managed by the hugetlb allocator and has
406 * no users -- drop the buddy allocator's reference.
408 put_page_testzero(page
);
409 VM_BUG_ON(page_count(page
));
410 nid
= page_to_nid(page
);
411 set_compound_page_dtor(page
, free_huge_page
);
413 * We incremented the global counters already
415 nr_huge_pages_node
[nid
]++;
416 surplus_huge_pages_node
[nid
]++;
417 __count_vm_event(HTLB_BUDDY_PGALLOC
);
420 surplus_huge_pages
--;
421 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
423 spin_unlock(&hugetlb_lock
);
429 * Increase the hugetlb pool such that it can accomodate a reservation
432 static int gather_surplus_pages(int delta
)
434 struct list_head surplus_list
;
435 struct page
*page
, *tmp
;
437 int needed
, allocated
;
439 needed
= (resv_huge_pages
+ delta
) - free_huge_pages
;
441 resv_huge_pages
+= delta
;
446 INIT_LIST_HEAD(&surplus_list
);
450 spin_unlock(&hugetlb_lock
);
451 for (i
= 0; i
< needed
; i
++) {
452 page
= alloc_buddy_huge_page(NULL
, 0);
455 * We were not able to allocate enough pages to
456 * satisfy the entire reservation so we free what
457 * we've allocated so far.
459 spin_lock(&hugetlb_lock
);
464 list_add(&page
->lru
, &surplus_list
);
469 * After retaking hugetlb_lock, we need to recalculate 'needed'
470 * because either resv_huge_pages or free_huge_pages may have changed.
472 spin_lock(&hugetlb_lock
);
473 needed
= (resv_huge_pages
+ delta
) - (free_huge_pages
+ allocated
);
478 * The surplus_list now contains _at_least_ the number of extra pages
479 * needed to accomodate the reservation. Add the appropriate number
480 * of pages to the hugetlb pool and free the extras back to the buddy
481 * allocator. Commit the entire reservation here to prevent another
482 * process from stealing the pages as they are added to the pool but
483 * before they are reserved.
486 resv_huge_pages
+= delta
;
489 /* Free the needed pages to the hugetlb pool */
490 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
493 list_del(&page
->lru
);
494 enqueue_huge_page(page
);
497 /* Free unnecessary surplus pages to the buddy allocator */
498 if (!list_empty(&surplus_list
)) {
499 spin_unlock(&hugetlb_lock
);
500 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
501 list_del(&page
->lru
);
503 * The page has a reference count of zero already, so
504 * call free_huge_page directly instead of using
505 * put_page. This must be done with hugetlb_lock
506 * unlocked which is safe because free_huge_page takes
507 * hugetlb_lock before deciding how to free the page.
509 free_huge_page(page
);
511 spin_lock(&hugetlb_lock
);
518 * When releasing a hugetlb pool reservation, any surplus pages that were
519 * allocated to satisfy the reservation must be explicitly freed if they were
522 static void return_unused_surplus_pages(unsigned long unused_resv_pages
)
526 unsigned long nr_pages
;
529 * We want to release as many surplus pages as possible, spread
530 * evenly across all nodes. Iterate across all nodes until we
531 * can no longer free unreserved surplus pages. This occurs when
532 * the nodes with surplus pages have no free pages.
534 unsigned long remaining_iterations
= num_online_nodes();
536 /* Uncommit the reservation */
537 resv_huge_pages
-= unused_resv_pages
;
539 nr_pages
= min(unused_resv_pages
, surplus_huge_pages
);
541 while (remaining_iterations
-- && nr_pages
) {
542 nid
= next_node(nid
, node_online_map
);
543 if (nid
== MAX_NUMNODES
)
544 nid
= first_node(node_online_map
);
546 if (!surplus_huge_pages_node
[nid
])
549 if (!list_empty(&hugepage_freelists
[nid
])) {
550 page
= list_entry(hugepage_freelists
[nid
].next
,
552 list_del(&page
->lru
);
553 update_and_free_page(page
);
555 free_huge_pages_node
[nid
]--;
556 surplus_huge_pages
--;
557 surplus_huge_pages_node
[nid
]--;
559 remaining_iterations
= num_online_nodes();
564 static struct page
*alloc_huge_page(struct vm_area_struct
*vma
,
565 unsigned long addr
, int avoid_reserve
)
568 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
569 struct inode
*inode
= mapping
->host
;
570 unsigned int chg
= 0;
573 * Processes that did not create the mapping will have no reserves and
574 * will not have accounted against quota. Check that the quota can be
575 * made before satisfying the allocation
577 if (!(vma
->vm_flags
& VM_SHARED
) &&
578 !is_vma_resv_set(vma
, HPAGE_RESV_OWNER
)) {
580 if (hugetlb_get_quota(inode
->i_mapping
, chg
))
581 return ERR_PTR(-ENOSPC
);
584 spin_lock(&hugetlb_lock
);
585 page
= dequeue_huge_page_vma(vma
, addr
, avoid_reserve
);
586 spin_unlock(&hugetlb_lock
);
589 page
= alloc_buddy_huge_page(vma
, addr
);
591 hugetlb_put_quota(inode
->i_mapping
, chg
);
592 return ERR_PTR(-VM_FAULT_OOM
);
596 set_page_refcounted(page
);
597 set_page_private(page
, (unsigned long) mapping
);
602 static int __init
hugetlb_init(void)
606 if (HPAGE_SHIFT
== 0)
609 for (i
= 0; i
< MAX_NUMNODES
; ++i
)
610 INIT_LIST_HEAD(&hugepage_freelists
[i
]);
612 hugetlb_next_nid
= first_node(node_online_map
);
614 for (i
= 0; i
< max_huge_pages
; ++i
) {
615 if (!alloc_fresh_huge_page())
618 max_huge_pages
= free_huge_pages
= nr_huge_pages
= i
;
619 printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages
);
622 module_init(hugetlb_init
);
624 static int __init
hugetlb_setup(char *s
)
626 if (sscanf(s
, "%lu", &max_huge_pages
) <= 0)
630 __setup("hugepages=", hugetlb_setup
);
632 static unsigned int cpuset_mems_nr(unsigned int *array
)
637 for_each_node_mask(node
, cpuset_current_mems_allowed
)
644 #ifdef CONFIG_HIGHMEM
645 static void try_to_free_low(unsigned long count
)
649 for (i
= 0; i
< MAX_NUMNODES
; ++i
) {
650 struct page
*page
, *next
;
651 list_for_each_entry_safe(page
, next
, &hugepage_freelists
[i
], lru
) {
652 if (count
>= nr_huge_pages
)
654 if (PageHighMem(page
))
656 list_del(&page
->lru
);
657 update_and_free_page(page
);
659 free_huge_pages_node
[page_to_nid(page
)]--;
664 static inline void try_to_free_low(unsigned long count
)
669 #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages)
670 static unsigned long set_max_huge_pages(unsigned long count
)
672 unsigned long min_count
, ret
;
675 * Increase the pool size
676 * First take pages out of surplus state. Then make up the
677 * remaining difference by allocating fresh huge pages.
679 * We might race with alloc_buddy_huge_page() here and be unable
680 * to convert a surplus huge page to a normal huge page. That is
681 * not critical, though, it just means the overall size of the
682 * pool might be one hugepage larger than it needs to be, but
683 * within all the constraints specified by the sysctls.
685 spin_lock(&hugetlb_lock
);
686 while (surplus_huge_pages
&& count
> persistent_huge_pages
) {
687 if (!adjust_pool_surplus(-1))
691 while (count
> persistent_huge_pages
) {
693 * If this allocation races such that we no longer need the
694 * page, free_huge_page will handle it by freeing the page
695 * and reducing the surplus.
697 spin_unlock(&hugetlb_lock
);
698 ret
= alloc_fresh_huge_page();
699 spin_lock(&hugetlb_lock
);
706 * Decrease the pool size
707 * First return free pages to the buddy allocator (being careful
708 * to keep enough around to satisfy reservations). Then place
709 * pages into surplus state as needed so the pool will shrink
710 * to the desired size as pages become free.
712 * By placing pages into the surplus state independent of the
713 * overcommit value, we are allowing the surplus pool size to
714 * exceed overcommit. There are few sane options here. Since
715 * alloc_buddy_huge_page() is checking the global counter,
716 * though, we'll note that we're not allowed to exceed surplus
717 * and won't grow the pool anywhere else. Not until one of the
718 * sysctls are changed, or the surplus pages go out of use.
720 min_count
= resv_huge_pages
+ nr_huge_pages
- free_huge_pages
;
721 min_count
= max(count
, min_count
);
722 try_to_free_low(min_count
);
723 while (min_count
< persistent_huge_pages
) {
724 struct page
*page
= dequeue_huge_page();
727 update_and_free_page(page
);
729 while (count
< persistent_huge_pages
) {
730 if (!adjust_pool_surplus(1))
734 ret
= persistent_huge_pages
;
735 spin_unlock(&hugetlb_lock
);
739 int hugetlb_sysctl_handler(struct ctl_table
*table
, int write
,
740 struct file
*file
, void __user
*buffer
,
741 size_t *length
, loff_t
*ppos
)
743 proc_doulongvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
744 max_huge_pages
= set_max_huge_pages(max_huge_pages
);
748 int hugetlb_treat_movable_handler(struct ctl_table
*table
, int write
,
749 struct file
*file
, void __user
*buffer
,
750 size_t *length
, loff_t
*ppos
)
752 proc_dointvec(table
, write
, file
, buffer
, length
, ppos
);
753 if (hugepages_treat_as_movable
)
754 htlb_alloc_mask
= GFP_HIGHUSER_MOVABLE
;
756 htlb_alloc_mask
= GFP_HIGHUSER
;
760 int hugetlb_overcommit_handler(struct ctl_table
*table
, int write
,
761 struct file
*file
, void __user
*buffer
,
762 size_t *length
, loff_t
*ppos
)
764 proc_doulongvec_minmax(table
, write
, file
, buffer
, length
, ppos
);
765 spin_lock(&hugetlb_lock
);
766 nr_overcommit_huge_pages
= sysctl_overcommit_huge_pages
;
767 spin_unlock(&hugetlb_lock
);
771 #endif /* CONFIG_SYSCTL */
773 int hugetlb_report_meminfo(char *buf
)
776 "HugePages_Total: %5lu\n"
777 "HugePages_Free: %5lu\n"
778 "HugePages_Rsvd: %5lu\n"
779 "HugePages_Surp: %5lu\n"
780 "Hugepagesize: %5lu kB\n",
788 int hugetlb_report_node_meminfo(int nid
, char *buf
)
791 "Node %d HugePages_Total: %5u\n"
792 "Node %d HugePages_Free: %5u\n"
793 "Node %d HugePages_Surp: %5u\n",
794 nid
, nr_huge_pages_node
[nid
],
795 nid
, free_huge_pages_node
[nid
],
796 nid
, surplus_huge_pages_node
[nid
]);
799 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
800 unsigned long hugetlb_total_pages(void)
802 return nr_huge_pages
* (HPAGE_SIZE
/ PAGE_SIZE
);
805 static int hugetlb_acct_memory(long delta
)
809 spin_lock(&hugetlb_lock
);
811 * When cpuset is configured, it breaks the strict hugetlb page
812 * reservation as the accounting is done on a global variable. Such
813 * reservation is completely rubbish in the presence of cpuset because
814 * the reservation is not checked against page availability for the
815 * current cpuset. Application can still potentially OOM'ed by kernel
816 * with lack of free htlb page in cpuset that the task is in.
817 * Attempt to enforce strict accounting with cpuset is almost
818 * impossible (or too ugly) because cpuset is too fluid that
819 * task or memory node can be dynamically moved between cpusets.
821 * The change of semantics for shared hugetlb mapping with cpuset is
822 * undesirable. However, in order to preserve some of the semantics,
823 * we fall back to check against current free page availability as
824 * a best attempt and hopefully to minimize the impact of changing
825 * semantics that cpuset has.
828 if (gather_surplus_pages(delta
) < 0)
831 if (delta
> cpuset_mems_nr(free_huge_pages_node
)) {
832 return_unused_surplus_pages(delta
);
839 return_unused_surplus_pages((unsigned long) -delta
);
842 spin_unlock(&hugetlb_lock
);
846 static void hugetlb_vm_op_close(struct vm_area_struct
*vma
)
848 unsigned long reserve
= vma_resv_huge_pages(vma
);
850 hugetlb_acct_memory(-reserve
);
854 * We cannot handle pagefaults against hugetlb pages at all. They cause
855 * handle_mm_fault() to try to instantiate regular-sized pages in the
856 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
859 static int hugetlb_vm_op_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
865 struct vm_operations_struct hugetlb_vm_ops
= {
866 .fault
= hugetlb_vm_op_fault
,
867 .close
= hugetlb_vm_op_close
,
870 static pte_t
make_huge_pte(struct vm_area_struct
*vma
, struct page
*page
,
877 pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
879 entry
= huge_pte_wrprotect(mk_pte(page
, vma
->vm_page_prot
));
881 entry
= pte_mkyoung(entry
);
882 entry
= pte_mkhuge(entry
);
887 static void set_huge_ptep_writable(struct vm_area_struct
*vma
,
888 unsigned long address
, pte_t
*ptep
)
892 entry
= pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep
)));
893 if (huge_ptep_set_access_flags(vma
, address
, ptep
, entry
, 1)) {
894 update_mmu_cache(vma
, address
, entry
);
899 int copy_hugetlb_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
900 struct vm_area_struct
*vma
)
902 pte_t
*src_pte
, *dst_pte
, entry
;
903 struct page
*ptepage
;
907 cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
909 for (addr
= vma
->vm_start
; addr
< vma
->vm_end
; addr
+= HPAGE_SIZE
) {
910 src_pte
= huge_pte_offset(src
, addr
);
913 dst_pte
= huge_pte_alloc(dst
, addr
);
917 /* If the pagetables are shared don't copy or take references */
918 if (dst_pte
== src_pte
)
921 spin_lock(&dst
->page_table_lock
);
922 spin_lock_nested(&src
->page_table_lock
, SINGLE_DEPTH_NESTING
);
923 if (!huge_pte_none(huge_ptep_get(src_pte
))) {
925 huge_ptep_set_wrprotect(src
, addr
, src_pte
);
926 entry
= huge_ptep_get(src_pte
);
927 ptepage
= pte_page(entry
);
929 set_huge_pte_at(dst
, addr
, dst_pte
, entry
);
931 spin_unlock(&src
->page_table_lock
);
932 spin_unlock(&dst
->page_table_lock
);
940 void __unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
941 unsigned long end
, struct page
*ref_page
)
943 struct mm_struct
*mm
= vma
->vm_mm
;
944 unsigned long address
;
950 * A page gathering list, protected by per file i_mmap_lock. The
951 * lock is used to avoid list corruption from multiple unmapping
952 * of the same page since we are using page->lru.
954 LIST_HEAD(page_list
);
956 WARN_ON(!is_vm_hugetlb_page(vma
));
957 BUG_ON(start
& ~HPAGE_MASK
);
958 BUG_ON(end
& ~HPAGE_MASK
);
960 spin_lock(&mm
->page_table_lock
);
961 for (address
= start
; address
< end
; address
+= HPAGE_SIZE
) {
962 ptep
= huge_pte_offset(mm
, address
);
966 if (huge_pmd_unshare(mm
, &address
, ptep
))
970 * If a reference page is supplied, it is because a specific
971 * page is being unmapped, not a range. Ensure the page we
972 * are about to unmap is the actual page of interest.
975 pte
= huge_ptep_get(ptep
);
976 if (huge_pte_none(pte
))
978 page
= pte_page(pte
);
979 if (page
!= ref_page
)
983 * Mark the VMA as having unmapped its page so that
984 * future faults in this VMA will fail rather than
985 * looking like data was lost
987 set_vma_resv_flags(vma
, HPAGE_RESV_UNMAPPED
);
990 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
991 if (huge_pte_none(pte
))
994 page
= pte_page(pte
);
996 set_page_dirty(page
);
997 list_add(&page
->lru
, &page_list
);
999 spin_unlock(&mm
->page_table_lock
);
1000 flush_tlb_range(vma
, start
, end
);
1001 list_for_each_entry_safe(page
, tmp
, &page_list
, lru
) {
1002 list_del(&page
->lru
);
1007 void unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
1008 unsigned long end
, struct page
*ref_page
)
1011 * It is undesirable to test vma->vm_file as it should be non-null
1012 * for valid hugetlb area. However, vm_file will be NULL in the error
1013 * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails,
1014 * do_mmap_pgoff() nullifies vma->vm_file before calling this function
1015 * to clean up. Since no pte has actually been setup, it is safe to
1016 * do nothing in this case.
1019 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1020 __unmap_hugepage_range(vma
, start
, end
, ref_page
);
1021 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1026 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1027 * mappping it owns the reserve page for. The intention is to unmap the page
1028 * from other VMAs and let the children be SIGKILLed if they are faulting the
1031 int unmap_ref_private(struct mm_struct
*mm
,
1032 struct vm_area_struct
*vma
,
1034 unsigned long address
)
1036 struct vm_area_struct
*iter_vma
;
1037 struct address_space
*mapping
;
1038 struct prio_tree_iter iter
;
1042 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1043 * from page cache lookup which is in HPAGE_SIZE units.
1045 address
= address
& huge_page_mask(hstate_vma(vma
));
1046 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
)
1047 + (vma
->vm_pgoff
>> PAGE_SHIFT
);
1048 mapping
= (struct address_space
*)page_private(page
);
1050 vma_prio_tree_foreach(iter_vma
, &iter
, &mapping
->i_mmap
, pgoff
, pgoff
) {
1051 /* Do not unmap the current VMA */
1052 if (iter_vma
== vma
)
1056 * Unmap the page from other VMAs without their own reserves.
1057 * They get marked to be SIGKILLed if they fault in these
1058 * areas. This is because a future no-page fault on this VMA
1059 * could insert a zeroed page instead of the data existing
1060 * from the time of fork. This would look like data corruption
1062 if (!is_vma_resv_set(iter_vma
, HPAGE_RESV_OWNER
))
1063 unmap_hugepage_range(iter_vma
,
1064 address
, address
+ HPAGE_SIZE
,
1071 static int hugetlb_cow(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1072 unsigned long address
, pte_t
*ptep
, pte_t pte
,
1073 struct page
*pagecache_page
)
1075 struct page
*old_page
, *new_page
;
1077 int outside_reserve
= 0;
1079 old_page
= pte_page(pte
);
1082 /* If no-one else is actually using this page, avoid the copy
1083 * and just make the page writable */
1084 avoidcopy
= (page_count(old_page
) == 1);
1086 set_huge_ptep_writable(vma
, address
, ptep
);
1091 * If the process that created a MAP_PRIVATE mapping is about to
1092 * perform a COW due to a shared page count, attempt to satisfy
1093 * the allocation without using the existing reserves. The pagecache
1094 * page is used to determine if the reserve at this address was
1095 * consumed or not. If reserves were used, a partial faulted mapping
1096 * at the time of fork() could consume its reserves on COW instead
1097 * of the full address range.
1099 if (!(vma
->vm_flags
& VM_SHARED
) &&
1100 is_vma_resv_set(vma
, HPAGE_RESV_OWNER
) &&
1101 old_page
!= pagecache_page
)
1102 outside_reserve
= 1;
1104 page_cache_get(old_page
);
1105 new_page
= alloc_huge_page(vma
, address
, outside_reserve
);
1107 if (IS_ERR(new_page
)) {
1108 page_cache_release(old_page
);
1111 * If a process owning a MAP_PRIVATE mapping fails to COW,
1112 * it is due to references held by a child and an insufficient
1113 * huge page pool. To guarantee the original mappers
1114 * reliability, unmap the page from child processes. The child
1115 * may get SIGKILLed if it later faults.
1117 if (outside_reserve
) {
1118 BUG_ON(huge_pte_none(pte
));
1119 if (unmap_ref_private(mm
, vma
, old_page
, address
)) {
1120 BUG_ON(page_count(old_page
) != 1);
1121 BUG_ON(huge_pte_none(pte
));
1122 goto retry_avoidcopy
;
1127 return -PTR_ERR(new_page
);
1130 spin_unlock(&mm
->page_table_lock
);
1131 copy_huge_page(new_page
, old_page
, address
, vma
);
1132 __SetPageUptodate(new_page
);
1133 spin_lock(&mm
->page_table_lock
);
1135 ptep
= huge_pte_offset(mm
, address
& HPAGE_MASK
);
1136 if (likely(pte_same(huge_ptep_get(ptep
), pte
))) {
1138 huge_ptep_clear_flush(vma
, address
, ptep
);
1139 set_huge_pte_at(mm
, address
, ptep
,
1140 make_huge_pte(vma
, new_page
, 1));
1141 /* Make the old page be freed below */
1142 new_page
= old_page
;
1144 page_cache_release(new_page
);
1145 page_cache_release(old_page
);
1149 /* Return the pagecache page at a given address within a VMA */
1150 static struct page
*hugetlbfs_pagecache_page(struct vm_area_struct
*vma
,
1151 unsigned long address
)
1153 struct address_space
*mapping
;
1156 mapping
= vma
->vm_file
->f_mapping
;
1157 idx
= ((address
- vma
->vm_start
) >> HPAGE_SHIFT
)
1158 + (vma
->vm_pgoff
>> (HPAGE_SHIFT
- PAGE_SHIFT
));
1160 return find_lock_page(mapping
, idx
);
1163 static int hugetlb_no_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1164 unsigned long address
, pte_t
*ptep
, int write_access
)
1166 int ret
= VM_FAULT_SIGBUS
;
1170 struct address_space
*mapping
;
1174 * Currently, we are forced to kill the process in the event the
1175 * original mapper has unmapped pages from the child due to a failed
1176 * COW. Warn that such a situation has occured as it may not be obvious
1178 if (is_vma_resv_set(vma
, HPAGE_RESV_UNMAPPED
)) {
1180 "PID %d killed due to inadequate hugepage pool\n",
1185 mapping
= vma
->vm_file
->f_mapping
;
1186 idx
= ((address
- vma
->vm_start
) >> HPAGE_SHIFT
)
1187 + (vma
->vm_pgoff
>> (HPAGE_SHIFT
- PAGE_SHIFT
));
1190 * Use page lock to guard against racing truncation
1191 * before we get page_table_lock.
1194 page
= find_lock_page(mapping
, idx
);
1196 size
= i_size_read(mapping
->host
) >> HPAGE_SHIFT
;
1199 page
= alloc_huge_page(vma
, address
, 0);
1201 ret
= -PTR_ERR(page
);
1204 clear_huge_page(page
, address
);
1205 __SetPageUptodate(page
);
1207 if (vma
->vm_flags
& VM_SHARED
) {
1209 struct inode
*inode
= mapping
->host
;
1211 err
= add_to_page_cache(page
, mapping
, idx
, GFP_KERNEL
);
1219 spin_lock(&inode
->i_lock
);
1220 inode
->i_blocks
+= BLOCKS_PER_HUGEPAGE
;
1221 spin_unlock(&inode
->i_lock
);
1226 spin_lock(&mm
->page_table_lock
);
1227 size
= i_size_read(mapping
->host
) >> HPAGE_SHIFT
;
1232 if (!huge_pte_none(huge_ptep_get(ptep
)))
1235 new_pte
= make_huge_pte(vma
, page
, ((vma
->vm_flags
& VM_WRITE
)
1236 && (vma
->vm_flags
& VM_SHARED
)));
1237 set_huge_pte_at(mm
, address
, ptep
, new_pte
);
1239 if (write_access
&& !(vma
->vm_flags
& VM_SHARED
)) {
1240 /* Optimization, do the COW without a second fault */
1241 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, new_pte
, page
);
1244 spin_unlock(&mm
->page_table_lock
);
1250 spin_unlock(&mm
->page_table_lock
);
1256 int hugetlb_fault(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1257 unsigned long address
, int write_access
)
1262 static DEFINE_MUTEX(hugetlb_instantiation_mutex
);
1264 ptep
= huge_pte_alloc(mm
, address
);
1266 return VM_FAULT_OOM
;
1269 * Serialize hugepage allocation and instantiation, so that we don't
1270 * get spurious allocation failures if two CPUs race to instantiate
1271 * the same page in the page cache.
1273 mutex_lock(&hugetlb_instantiation_mutex
);
1274 entry
= huge_ptep_get(ptep
);
1275 if (huge_pte_none(entry
)) {
1276 ret
= hugetlb_no_page(mm
, vma
, address
, ptep
, write_access
);
1277 mutex_unlock(&hugetlb_instantiation_mutex
);
1283 spin_lock(&mm
->page_table_lock
);
1284 /* Check for a racing update before calling hugetlb_cow */
1285 if (likely(pte_same(entry
, huge_ptep_get(ptep
))))
1286 if (write_access
&& !pte_write(entry
)) {
1288 page
= hugetlbfs_pagecache_page(vma
, address
);
1289 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, entry
, page
);
1295 spin_unlock(&mm
->page_table_lock
);
1296 mutex_unlock(&hugetlb_instantiation_mutex
);
1301 int follow_hugetlb_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1302 struct page
**pages
, struct vm_area_struct
**vmas
,
1303 unsigned long *position
, int *length
, int i
,
1306 unsigned long pfn_offset
;
1307 unsigned long vaddr
= *position
;
1308 int remainder
= *length
;
1310 spin_lock(&mm
->page_table_lock
);
1311 while (vaddr
< vma
->vm_end
&& remainder
) {
1316 * Some archs (sparc64, sh*) have multiple pte_ts to
1317 * each hugepage. We have to make * sure we get the
1318 * first, for the page indexing below to work.
1320 pte
= huge_pte_offset(mm
, vaddr
& HPAGE_MASK
);
1322 if (!pte
|| huge_pte_none(huge_ptep_get(pte
)) ||
1323 (write
&& !pte_write(huge_ptep_get(pte
)))) {
1326 spin_unlock(&mm
->page_table_lock
);
1327 ret
= hugetlb_fault(mm
, vma
, vaddr
, write
);
1328 spin_lock(&mm
->page_table_lock
);
1329 if (!(ret
& VM_FAULT_ERROR
))
1338 pfn_offset
= (vaddr
& ~HPAGE_MASK
) >> PAGE_SHIFT
;
1339 page
= pte_page(huge_ptep_get(pte
));
1343 pages
[i
] = page
+ pfn_offset
;
1353 if (vaddr
< vma
->vm_end
&& remainder
&&
1354 pfn_offset
< HPAGE_SIZE
/PAGE_SIZE
) {
1356 * We use pfn_offset to avoid touching the pageframes
1357 * of this compound page.
1362 spin_unlock(&mm
->page_table_lock
);
1363 *length
= remainder
;
1369 void hugetlb_change_protection(struct vm_area_struct
*vma
,
1370 unsigned long address
, unsigned long end
, pgprot_t newprot
)
1372 struct mm_struct
*mm
= vma
->vm_mm
;
1373 unsigned long start
= address
;
1377 BUG_ON(address
>= end
);
1378 flush_cache_range(vma
, address
, end
);
1380 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1381 spin_lock(&mm
->page_table_lock
);
1382 for (; address
< end
; address
+= HPAGE_SIZE
) {
1383 ptep
= huge_pte_offset(mm
, address
);
1386 if (huge_pmd_unshare(mm
, &address
, ptep
))
1388 if (!huge_pte_none(huge_ptep_get(ptep
))) {
1389 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
1390 pte
= pte_mkhuge(pte_modify(pte
, newprot
));
1391 set_huge_pte_at(mm
, address
, ptep
, pte
);
1394 spin_unlock(&mm
->page_table_lock
);
1395 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1397 flush_tlb_range(vma
, start
, end
);
1400 struct file_region
{
1401 struct list_head link
;
1406 static long region_add(struct list_head
*head
, long f
, long t
)
1408 struct file_region
*rg
, *nrg
, *trg
;
1410 /* Locate the region we are either in or before. */
1411 list_for_each_entry(rg
, head
, link
)
1415 /* Round our left edge to the current segment if it encloses us. */
1419 /* Check for and consume any regions we now overlap with. */
1421 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
1422 if (&rg
->link
== head
)
1427 /* If this area reaches higher then extend our area to
1428 * include it completely. If this is not the first area
1429 * which we intend to reuse, free it. */
1433 list_del(&rg
->link
);
1442 static long region_chg(struct list_head
*head
, long f
, long t
)
1444 struct file_region
*rg
, *nrg
;
1447 /* Locate the region we are before or in. */
1448 list_for_each_entry(rg
, head
, link
)
1452 /* If we are below the current region then a new region is required.
1453 * Subtle, allocate a new region at the position but make it zero
1454 * size such that we can guarantee to record the reservation. */
1455 if (&rg
->link
== head
|| t
< rg
->from
) {
1456 nrg
= kmalloc(sizeof(*nrg
), GFP_KERNEL
);
1461 INIT_LIST_HEAD(&nrg
->link
);
1462 list_add(&nrg
->link
, rg
->link
.prev
);
1467 /* Round our left edge to the current segment if it encloses us. */
1472 /* Check for and consume any regions we now overlap with. */
1473 list_for_each_entry(rg
, rg
->link
.prev
, link
) {
1474 if (&rg
->link
== head
)
1479 /* We overlap with this area, if it extends futher than
1480 * us then we must extend ourselves. Account for its
1481 * existing reservation. */
1486 chg
-= rg
->to
- rg
->from
;
1491 static long region_truncate(struct list_head
*head
, long end
)
1493 struct file_region
*rg
, *trg
;
1496 /* Locate the region we are either in or before. */
1497 list_for_each_entry(rg
, head
, link
)
1500 if (&rg
->link
== head
)
1503 /* If we are in the middle of a region then adjust it. */
1504 if (end
> rg
->from
) {
1507 rg
= list_entry(rg
->link
.next
, typeof(*rg
), link
);
1510 /* Drop any remaining regions. */
1511 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
1512 if (&rg
->link
== head
)
1514 chg
+= rg
->to
- rg
->from
;
1515 list_del(&rg
->link
);
1521 int hugetlb_reserve_pages(struct inode
*inode
,
1523 struct vm_area_struct
*vma
)
1528 * Shared mappings base their reservation on the number of pages that
1529 * are already allocated on behalf of the file. Private mappings need
1530 * to reserve the full area even if read-only as mprotect() may be
1531 * called to make the mapping read-write. Assume !vma is a shm mapping
1533 if (!vma
|| vma
->vm_flags
& VM_SHARED
)
1534 chg
= region_chg(&inode
->i_mapping
->private_list
, from
, to
);
1537 set_vma_resv_huge_pages(vma
, chg
);
1538 set_vma_resv_flags(vma
, HPAGE_RESV_OWNER
);
1544 if (hugetlb_get_quota(inode
->i_mapping
, chg
))
1546 ret
= hugetlb_acct_memory(chg
);
1548 hugetlb_put_quota(inode
->i_mapping
, chg
);
1551 if (!vma
|| vma
->vm_flags
& VM_SHARED
)
1552 region_add(&inode
->i_mapping
->private_list
, from
, to
);
1556 void hugetlb_unreserve_pages(struct inode
*inode
, long offset
, long freed
)
1558 long chg
= region_truncate(&inode
->i_mapping
->private_list
, offset
);
1560 spin_lock(&inode
->i_lock
);
1561 inode
->i_blocks
-= BLOCKS_PER_HUGEPAGE
* freed
;
1562 spin_unlock(&inode
->i_lock
);
1564 hugetlb_put_quota(inode
->i_mapping
, (chg
- freed
));
1565 hugetlb_acct_memory(-(chg
- freed
));