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/seq_file.h>
11 #include <linux/sysctl.h>
12 #include <linux/highmem.h>
13 #include <linux/mmu_notifier.h>
14 #include <linux/nodemask.h>
15 #include <linux/pagemap.h>
16 #include <linux/mempolicy.h>
17 #include <linux/cpuset.h>
18 #include <linux/mutex.h>
19 #include <linux/bootmem.h>
20 #include <linux/sysfs.h>
23 #include <asm/pgtable.h>
26 #include <linux/hugetlb.h>
29 const unsigned long hugetlb_zero
= 0, hugetlb_infinity
= ~0UL;
30 static gfp_t htlb_alloc_mask
= GFP_HIGHUSER
;
31 unsigned long hugepages_treat_as_movable
;
33 static int max_hstate
;
34 unsigned int default_hstate_idx
;
35 struct hstate hstates
[HUGE_MAX_HSTATE
];
37 __initdata
LIST_HEAD(huge_boot_pages
);
39 /* for command line parsing */
40 static struct hstate
* __initdata parsed_hstate
;
41 static unsigned long __initdata default_hstate_max_huge_pages
;
42 static unsigned long __initdata default_hstate_size
;
44 #define for_each_hstate(h) \
45 for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
48 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
50 static DEFINE_SPINLOCK(hugetlb_lock
);
52 static inline void unlock_or_release_subpool(struct hugepage_subpool
*spool
)
54 bool free
= (spool
->count
== 0) && (spool
->used_hpages
== 0);
56 spin_unlock(&spool
->lock
);
58 /* If no pages are used, and no other handles to the subpool
59 * remain, free the subpool the subpool remain */
64 struct hugepage_subpool
*hugepage_new_subpool(long nr_blocks
)
66 struct hugepage_subpool
*spool
;
68 spool
= kmalloc(sizeof(*spool
), GFP_KERNEL
);
72 spin_lock_init(&spool
->lock
);
74 spool
->max_hpages
= nr_blocks
;
75 spool
->used_hpages
= 0;
80 void hugepage_put_subpool(struct hugepage_subpool
*spool
)
82 spin_lock(&spool
->lock
);
83 BUG_ON(!spool
->count
);
85 unlock_or_release_subpool(spool
);
88 static int hugepage_subpool_get_pages(struct hugepage_subpool
*spool
,
96 spin_lock(&spool
->lock
);
97 if ((spool
->used_hpages
+ delta
) <= spool
->max_hpages
) {
98 spool
->used_hpages
+= delta
;
102 spin_unlock(&spool
->lock
);
107 static void hugepage_subpool_put_pages(struct hugepage_subpool
*spool
,
113 spin_lock(&spool
->lock
);
114 spool
->used_hpages
-= delta
;
115 /* If hugetlbfs_put_super couldn't free spool due to
116 * an outstanding quota reference, free it now. */
117 unlock_or_release_subpool(spool
);
120 static inline struct hugepage_subpool
*subpool_inode(struct inode
*inode
)
122 return HUGETLBFS_SB(inode
->i_sb
)->spool
;
125 static inline struct hugepage_subpool
*subpool_vma(struct vm_area_struct
*vma
)
127 return subpool_inode(vma
->vm_file
->f_dentry
->d_inode
);
131 * Region tracking -- allows tracking of reservations and instantiated pages
132 * across the pages in a mapping.
134 * The region data structures are protected by a combination of the mmap_sem
135 * and the hugetlb_instantion_mutex. To access or modify a region the caller
136 * must either hold the mmap_sem for write, or the mmap_sem for read and
137 * the hugetlb_instantiation mutex:
139 * down_write(&mm->mmap_sem);
141 * down_read(&mm->mmap_sem);
142 * mutex_lock(&hugetlb_instantiation_mutex);
145 struct list_head link
;
150 static long region_add(struct list_head
*head
, long f
, long t
)
152 struct file_region
*rg
, *nrg
, *trg
;
154 /* Locate the region we are either in or before. */
155 list_for_each_entry(rg
, head
, link
)
159 /* Round our left edge to the current segment if it encloses us. */
163 /* Check for and consume any regions we now overlap with. */
165 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
166 if (&rg
->link
== head
)
171 /* If this area reaches higher then extend our area to
172 * include it completely. If this is not the first area
173 * which we intend to reuse, free it. */
186 static long region_chg(struct list_head
*head
, long f
, long t
)
188 struct file_region
*rg
, *nrg
;
191 /* Locate the region we are before or in. */
192 list_for_each_entry(rg
, head
, link
)
196 /* If we are below the current region then a new region is required.
197 * Subtle, allocate a new region at the position but make it zero
198 * size such that we can guarantee to record the reservation. */
199 if (&rg
->link
== head
|| t
< rg
->from
) {
200 nrg
= kmalloc(sizeof(*nrg
), GFP_KERNEL
);
205 INIT_LIST_HEAD(&nrg
->link
);
206 list_add(&nrg
->link
, rg
->link
.prev
);
211 /* Round our left edge to the current segment if it encloses us. */
216 /* Check for and consume any regions we now overlap with. */
217 list_for_each_entry(rg
, rg
->link
.prev
, link
) {
218 if (&rg
->link
== head
)
223 /* We overlap with this area, if it extends futher than
224 * us then we must extend ourselves. Account for its
225 * existing reservation. */
230 chg
-= rg
->to
- rg
->from
;
235 static long region_truncate(struct list_head
*head
, long end
)
237 struct file_region
*rg
, *trg
;
240 /* Locate the region we are either in or before. */
241 list_for_each_entry(rg
, head
, link
)
244 if (&rg
->link
== head
)
247 /* If we are in the middle of a region then adjust it. */
248 if (end
> rg
->from
) {
251 rg
= list_entry(rg
->link
.next
, typeof(*rg
), link
);
254 /* Drop any remaining regions. */
255 list_for_each_entry_safe(rg
, trg
, rg
->link
.prev
, link
) {
256 if (&rg
->link
== head
)
258 chg
+= rg
->to
- rg
->from
;
265 static long region_count(struct list_head
*head
, long f
, long t
)
267 struct file_region
*rg
;
270 /* Locate each segment we overlap with, and count that overlap. */
271 list_for_each_entry(rg
, head
, link
) {
280 seg_from
= max(rg
->from
, f
);
281 seg_to
= min(rg
->to
, t
);
283 chg
+= seg_to
- seg_from
;
290 * Convert the address within this vma to the page offset within
291 * the mapping, in pagecache page units; huge pages here.
293 static pgoff_t
vma_hugecache_offset(struct hstate
*h
,
294 struct vm_area_struct
*vma
, unsigned long address
)
296 return ((address
- vma
->vm_start
) >> huge_page_shift(h
)) +
297 (vma
->vm_pgoff
>> huge_page_order(h
));
301 * Return the size of the pages allocated when backing a VMA. In the majority
302 * cases this will be same size as used by the page table entries.
304 unsigned long vma_kernel_pagesize(struct vm_area_struct
*vma
)
306 struct hstate
*hstate
;
308 if (!is_vm_hugetlb_page(vma
))
311 hstate
= hstate_vma(vma
);
313 return 1UL << (hstate
->order
+ PAGE_SHIFT
);
315 EXPORT_SYMBOL_GPL(vma_kernel_pagesize
);
318 * Return the page size being used by the MMU to back a VMA. In the majority
319 * of cases, the page size used by the kernel matches the MMU size. On
320 * architectures where it differs, an architecture-specific version of this
321 * function is required.
323 #ifndef vma_mmu_pagesize
324 unsigned long vma_mmu_pagesize(struct vm_area_struct
*vma
)
326 return vma_kernel_pagesize(vma
);
331 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
332 * bits of the reservation map pointer, which are always clear due to
335 #define HPAGE_RESV_OWNER (1UL << 0)
336 #define HPAGE_RESV_UNMAPPED (1UL << 1)
337 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
340 * These helpers are used to track how many pages are reserved for
341 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
342 * is guaranteed to have their future faults succeed.
344 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
345 * the reserve counters are updated with the hugetlb_lock held. It is safe
346 * to reset the VMA at fork() time as it is not in use yet and there is no
347 * chance of the global counters getting corrupted as a result of the values.
349 * The private mapping reservation is represented in a subtly different
350 * manner to a shared mapping. A shared mapping has a region map associated
351 * with the underlying file, this region map represents the backing file
352 * pages which have ever had a reservation assigned which this persists even
353 * after the page is instantiated. A private mapping has a region map
354 * associated with the original mmap which is attached to all VMAs which
355 * reference it, this region map represents those offsets which have consumed
356 * reservation ie. where pages have been instantiated.
358 static unsigned long get_vma_private_data(struct vm_area_struct
*vma
)
360 return (unsigned long)vma
->vm_private_data
;
363 static void set_vma_private_data(struct vm_area_struct
*vma
,
366 vma
->vm_private_data
= (void *)value
;
371 struct list_head regions
;
374 static struct resv_map
*resv_map_alloc(void)
376 struct resv_map
*resv_map
= kmalloc(sizeof(*resv_map
), GFP_KERNEL
);
380 kref_init(&resv_map
->refs
);
381 INIT_LIST_HEAD(&resv_map
->regions
);
386 static void resv_map_release(struct kref
*ref
)
388 struct resv_map
*resv_map
= container_of(ref
, struct resv_map
, refs
);
390 /* Clear out any active regions before we release the map. */
391 region_truncate(&resv_map
->regions
, 0);
395 static struct resv_map
*vma_resv_map(struct vm_area_struct
*vma
)
397 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
398 if (!(vma
->vm_flags
& VM_MAYSHARE
))
399 return (struct resv_map
*)(get_vma_private_data(vma
) &
404 static void set_vma_resv_map(struct vm_area_struct
*vma
, struct resv_map
*map
)
406 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
407 VM_BUG_ON(vma
->vm_flags
& VM_MAYSHARE
);
409 set_vma_private_data(vma
, (get_vma_private_data(vma
) &
410 HPAGE_RESV_MASK
) | (unsigned long)map
);
413 static void set_vma_resv_flags(struct vm_area_struct
*vma
, unsigned long flags
)
415 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
416 VM_BUG_ON(vma
->vm_flags
& VM_MAYSHARE
);
418 set_vma_private_data(vma
, get_vma_private_data(vma
) | flags
);
421 static int is_vma_resv_set(struct vm_area_struct
*vma
, unsigned long flag
)
423 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
425 return (get_vma_private_data(vma
) & flag
) != 0;
428 /* Decrement the reserved pages in the hugepage pool by one */
429 static void decrement_hugepage_resv_vma(struct hstate
*h
,
430 struct vm_area_struct
*vma
)
432 if (vma
->vm_flags
& VM_NORESERVE
)
435 if (vma
->vm_flags
& VM_MAYSHARE
) {
436 /* Shared mappings always use reserves */
437 h
->resv_huge_pages
--;
438 } else if (is_vma_resv_set(vma
, HPAGE_RESV_OWNER
)) {
440 * Only the process that called mmap() has reserves for
443 h
->resv_huge_pages
--;
447 /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
448 void reset_vma_resv_huge_pages(struct vm_area_struct
*vma
)
450 VM_BUG_ON(!is_vm_hugetlb_page(vma
));
451 if (!(vma
->vm_flags
& VM_MAYSHARE
))
452 vma
->vm_private_data
= (void *)0;
455 /* Returns true if the VMA has associated reserve pages */
456 static int vma_has_reserves(struct vm_area_struct
*vma
)
458 if (vma
->vm_flags
& VM_MAYSHARE
)
460 if (is_vma_resv_set(vma
, HPAGE_RESV_OWNER
))
465 static void clear_gigantic_page(struct page
*page
,
466 unsigned long addr
, unsigned long sz
)
469 struct page
*p
= page
;
472 for (i
= 0; i
< sz
/PAGE_SIZE
; i
++, p
= mem_map_next(p
, page
, i
)) {
474 clear_user_highpage(p
, addr
+ i
* PAGE_SIZE
);
477 static void clear_huge_page(struct page
*page
,
478 unsigned long addr
, unsigned long sz
)
482 if (unlikely(sz
/PAGE_SIZE
> MAX_ORDER_NR_PAGES
)) {
483 clear_gigantic_page(page
, addr
, sz
);
488 for (i
= 0; i
< sz
/PAGE_SIZE
; i
++) {
490 clear_user_highpage(page
+ i
, addr
+ i
* PAGE_SIZE
);
494 static void copy_gigantic_page(struct page
*dst
, struct page
*src
,
495 unsigned long addr
, struct vm_area_struct
*vma
)
498 struct hstate
*h
= hstate_vma(vma
);
499 struct page
*dst_base
= dst
;
500 struct page
*src_base
= src
;
502 for (i
= 0; i
< pages_per_huge_page(h
); ) {
504 copy_user_highpage(dst
, src
, addr
+ i
*PAGE_SIZE
, vma
);
507 dst
= mem_map_next(dst
, dst_base
, i
);
508 src
= mem_map_next(src
, src_base
, i
);
511 static void copy_huge_page(struct page
*dst
, struct page
*src
,
512 unsigned long addr
, struct vm_area_struct
*vma
)
515 struct hstate
*h
= hstate_vma(vma
);
517 if (unlikely(pages_per_huge_page(h
) > MAX_ORDER_NR_PAGES
)) {
518 copy_gigantic_page(dst
, src
, addr
, vma
);
523 for (i
= 0; i
< pages_per_huge_page(h
); i
++) {
525 copy_user_highpage(dst
+ i
, src
+ i
, addr
+ i
*PAGE_SIZE
, vma
);
529 static void enqueue_huge_page(struct hstate
*h
, struct page
*page
)
531 int nid
= page_to_nid(page
);
532 list_add(&page
->lru
, &h
->hugepage_freelists
[nid
]);
533 h
->free_huge_pages
++;
534 h
->free_huge_pages_node
[nid
]++;
537 static struct page
*dequeue_huge_page_vma(struct hstate
*h
,
538 struct vm_area_struct
*vma
,
539 unsigned long address
, int avoid_reserve
)
542 struct page
*page
= NULL
;
543 struct mempolicy
*mpol
;
544 nodemask_t
*nodemask
;
545 struct zonelist
*zonelist
= huge_zonelist(vma
, address
,
546 htlb_alloc_mask
, &mpol
, &nodemask
);
551 * A child process with MAP_PRIVATE mappings created by their parent
552 * have no page reserves. This check ensures that reservations are
553 * not "stolen". The child may still get SIGKILLed
555 if (!vma_has_reserves(vma
) &&
556 h
->free_huge_pages
- h
->resv_huge_pages
== 0)
559 /* If reserves cannot be used, ensure enough pages are in the pool */
560 if (avoid_reserve
&& h
->free_huge_pages
- h
->resv_huge_pages
== 0)
563 for_each_zone_zonelist_nodemask(zone
, z
, zonelist
,
564 MAX_NR_ZONES
- 1, nodemask
) {
565 nid
= zone_to_nid(zone
);
566 if (cpuset_zone_allowed_softwall(zone
, htlb_alloc_mask
) &&
567 !list_empty(&h
->hugepage_freelists
[nid
])) {
568 page
= list_entry(h
->hugepage_freelists
[nid
].next
,
570 list_del(&page
->lru
);
571 h
->free_huge_pages
--;
572 h
->free_huge_pages_node
[nid
]--;
575 decrement_hugepage_resv_vma(h
, vma
);
584 static void update_and_free_page(struct hstate
*h
, struct page
*page
)
588 VM_BUG_ON(h
->order
>= MAX_ORDER
);
591 h
->nr_huge_pages_node
[page_to_nid(page
)]--;
592 for (i
= 0; i
< pages_per_huge_page(h
); i
++) {
593 page
[i
].flags
&= ~(1 << PG_locked
| 1 << PG_error
| 1 << PG_referenced
|
594 1 << PG_dirty
| 1 << PG_active
| 1 << PG_reserved
|
595 1 << PG_private
| 1<< PG_writeback
);
597 set_compound_page_dtor(page
, NULL
);
598 set_page_refcounted(page
);
599 arch_release_hugepage(page
);
600 __free_pages(page
, huge_page_order(h
));
603 struct hstate
*size_to_hstate(unsigned long size
)
608 if (huge_page_size(h
) == size
)
614 static void free_huge_page(struct page
*page
)
617 * Can't pass hstate in here because it is called from the
618 * compound page destructor.
620 struct hstate
*h
= page_hstate(page
);
621 int nid
= page_to_nid(page
);
622 struct hugepage_subpool
*spool
=
623 (struct hugepage_subpool
*)page_private(page
);
625 set_page_private(page
, 0);
626 page
->mapping
= NULL
;
627 BUG_ON(page_count(page
));
628 INIT_LIST_HEAD(&page
->lru
);
630 spin_lock(&hugetlb_lock
);
631 if (h
->surplus_huge_pages_node
[nid
] && huge_page_order(h
) < MAX_ORDER
) {
632 update_and_free_page(h
, page
);
633 h
->surplus_huge_pages
--;
634 h
->surplus_huge_pages_node
[nid
]--;
636 enqueue_huge_page(h
, page
);
638 spin_unlock(&hugetlb_lock
);
639 hugepage_subpool_put_pages(spool
, 1);
642 static void prep_new_huge_page(struct hstate
*h
, struct page
*page
, int nid
)
644 set_compound_page_dtor(page
, free_huge_page
);
645 spin_lock(&hugetlb_lock
);
647 h
->nr_huge_pages_node
[nid
]++;
648 spin_unlock(&hugetlb_lock
);
649 put_page(page
); /* free it into the hugepage allocator */
652 static void prep_compound_gigantic_page(struct page
*page
, unsigned long order
)
655 int nr_pages
= 1 << order
;
656 struct page
*p
= page
+ 1;
658 /* we rely on prep_new_huge_page to set the destructor */
659 set_compound_order(page
, order
);
661 for (i
= 1; i
< nr_pages
; i
++, p
= mem_map_next(p
, page
, i
)) {
663 p
->first_page
= page
;
667 int PageHuge(struct page
*page
)
669 compound_page_dtor
*dtor
;
671 if (!PageCompound(page
))
674 page
= compound_head(page
);
675 dtor
= get_compound_page_dtor(page
);
677 return dtor
== free_huge_page
;
680 static struct page
*alloc_fresh_huge_page_node(struct hstate
*h
, int nid
)
684 if (h
->order
>= MAX_ORDER
)
687 page
= alloc_pages_exact_node(nid
,
688 htlb_alloc_mask
|__GFP_COMP
|__GFP_THISNODE
|
689 __GFP_REPEAT
|__GFP_NOWARN
,
692 if (arch_prepare_hugepage(page
)) {
693 __free_pages(page
, huge_page_order(h
));
696 prep_new_huge_page(h
, page
, nid
);
703 * Use a helper variable to find the next node and then
704 * copy it back to next_nid_to_alloc afterwards:
705 * otherwise there's a window in which a racer might
706 * pass invalid nid MAX_NUMNODES to alloc_pages_exact_node.
707 * But we don't need to use a spin_lock here: it really
708 * doesn't matter if occasionally a racer chooses the
709 * same nid as we do. Move nid forward in the mask even
710 * if we just successfully allocated a hugepage so that
711 * the next caller gets hugepages on the next node.
713 static int hstate_next_node_to_alloc(struct hstate
*h
)
716 next_nid
= next_node(h
->next_nid_to_alloc
, node_online_map
);
717 if (next_nid
== MAX_NUMNODES
)
718 next_nid
= first_node(node_online_map
);
719 h
->next_nid_to_alloc
= next_nid
;
723 static int alloc_fresh_huge_page(struct hstate
*h
)
730 start_nid
= h
->next_nid_to_alloc
;
731 next_nid
= start_nid
;
734 page
= alloc_fresh_huge_page_node(h
, next_nid
);
737 next_nid
= hstate_next_node_to_alloc(h
);
738 } while (!page
&& next_nid
!= start_nid
);
741 count_vm_event(HTLB_BUDDY_PGALLOC
);
743 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
749 * helper for free_pool_huge_page() - find next node
750 * from which to free a huge page
752 static int hstate_next_node_to_free(struct hstate
*h
)
755 next_nid
= next_node(h
->next_nid_to_free
, node_online_map
);
756 if (next_nid
== MAX_NUMNODES
)
757 next_nid
= first_node(node_online_map
);
758 h
->next_nid_to_free
= next_nid
;
763 * Free huge page from pool from next node to free.
764 * Attempt to keep persistent huge pages more or less
765 * balanced over allowed nodes.
766 * Called with hugetlb_lock locked.
768 static int free_pool_huge_page(struct hstate
*h
, bool acct_surplus
)
774 start_nid
= h
->next_nid_to_free
;
775 next_nid
= start_nid
;
779 * If we're returning unused surplus pages, only examine
780 * nodes with surplus pages.
782 if ((!acct_surplus
|| h
->surplus_huge_pages_node
[next_nid
]) &&
783 !list_empty(&h
->hugepage_freelists
[next_nid
])) {
785 list_entry(h
->hugepage_freelists
[next_nid
].next
,
787 list_del(&page
->lru
);
788 h
->free_huge_pages
--;
789 h
->free_huge_pages_node
[next_nid
]--;
791 h
->surplus_huge_pages
--;
792 h
->surplus_huge_pages_node
[next_nid
]--;
794 update_and_free_page(h
, page
);
797 next_nid
= hstate_next_node_to_free(h
);
798 } while (!ret
&& next_nid
!= start_nid
);
803 static struct page
*alloc_buddy_huge_page(struct hstate
*h
,
804 struct vm_area_struct
*vma
, unsigned long address
)
809 if (h
->order
>= MAX_ORDER
)
813 * Assume we will successfully allocate the surplus page to
814 * prevent racing processes from causing the surplus to exceed
817 * This however introduces a different race, where a process B
818 * tries to grow the static hugepage pool while alloc_pages() is
819 * called by process A. B will only examine the per-node
820 * counters in determining if surplus huge pages can be
821 * converted to normal huge pages in adjust_pool_surplus(). A
822 * won't be able to increment the per-node counter, until the
823 * lock is dropped by B, but B doesn't drop hugetlb_lock until
824 * no more huge pages can be converted from surplus to normal
825 * state (and doesn't try to convert again). Thus, we have a
826 * case where a surplus huge page exists, the pool is grown, and
827 * the surplus huge page still exists after, even though it
828 * should just have been converted to a normal huge page. This
829 * does not leak memory, though, as the hugepage will be freed
830 * once it is out of use. It also does not allow the counters to
831 * go out of whack in adjust_pool_surplus() as we don't modify
832 * the node values until we've gotten the hugepage and only the
833 * per-node value is checked there.
835 spin_lock(&hugetlb_lock
);
836 if (h
->surplus_huge_pages
>= h
->nr_overcommit_huge_pages
) {
837 spin_unlock(&hugetlb_lock
);
841 h
->surplus_huge_pages
++;
843 spin_unlock(&hugetlb_lock
);
845 page
= alloc_pages(htlb_alloc_mask
|__GFP_COMP
|
846 __GFP_REPEAT
|__GFP_NOWARN
,
849 if (page
&& arch_prepare_hugepage(page
)) {
850 __free_pages(page
, huge_page_order(h
));
854 spin_lock(&hugetlb_lock
);
857 * This page is now managed by the hugetlb allocator and has
858 * no users -- drop the buddy allocator's reference.
860 put_page_testzero(page
);
861 VM_BUG_ON(page_count(page
));
862 nid
= page_to_nid(page
);
863 set_compound_page_dtor(page
, free_huge_page
);
865 * We incremented the global counters already
867 h
->nr_huge_pages_node
[nid
]++;
868 h
->surplus_huge_pages_node
[nid
]++;
869 __count_vm_event(HTLB_BUDDY_PGALLOC
);
872 h
->surplus_huge_pages
--;
873 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL
);
875 spin_unlock(&hugetlb_lock
);
881 * Increase the hugetlb pool such that it can accomodate a reservation
884 static int gather_surplus_pages(struct hstate
*h
, int delta
)
886 struct list_head surplus_list
;
887 struct page
*page
, *tmp
;
889 int needed
, allocated
;
891 needed
= (h
->resv_huge_pages
+ delta
) - h
->free_huge_pages
;
893 h
->resv_huge_pages
+= delta
;
898 INIT_LIST_HEAD(&surplus_list
);
902 spin_unlock(&hugetlb_lock
);
903 for (i
= 0; i
< needed
; i
++) {
904 page
= alloc_buddy_huge_page(h
, NULL
, 0);
907 * We were not able to allocate enough pages to
908 * satisfy the entire reservation so we free what
909 * we've allocated so far.
911 spin_lock(&hugetlb_lock
);
916 list_add(&page
->lru
, &surplus_list
);
921 * After retaking hugetlb_lock, we need to recalculate 'needed'
922 * because either resv_huge_pages or free_huge_pages may have changed.
924 spin_lock(&hugetlb_lock
);
925 needed
= (h
->resv_huge_pages
+ delta
) -
926 (h
->free_huge_pages
+ allocated
);
931 * The surplus_list now contains _at_least_ the number of extra pages
932 * needed to accomodate the reservation. Add the appropriate number
933 * of pages to the hugetlb pool and free the extras back to the buddy
934 * allocator. Commit the entire reservation here to prevent another
935 * process from stealing the pages as they are added to the pool but
936 * before they are reserved.
939 h
->resv_huge_pages
+= delta
;
942 /* Free the needed pages to the hugetlb pool */
943 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
946 list_del(&page
->lru
);
947 enqueue_huge_page(h
, page
);
950 /* Free unnecessary surplus pages to the buddy allocator */
951 if (!list_empty(&surplus_list
)) {
952 spin_unlock(&hugetlb_lock
);
953 list_for_each_entry_safe(page
, tmp
, &surplus_list
, lru
) {
954 list_del(&page
->lru
);
956 * The page has a reference count of zero already, so
957 * call free_huge_page directly instead of using
958 * put_page. This must be done with hugetlb_lock
959 * unlocked which is safe because free_huge_page takes
960 * hugetlb_lock before deciding how to free the page.
962 free_huge_page(page
);
964 spin_lock(&hugetlb_lock
);
971 * When releasing a hugetlb pool reservation, any surplus pages that were
972 * allocated to satisfy the reservation must be explicitly freed if they were
974 * Called with hugetlb_lock held.
976 static void return_unused_surplus_pages(struct hstate
*h
,
977 unsigned long unused_resv_pages
)
979 unsigned long nr_pages
;
981 /* Uncommit the reservation */
982 h
->resv_huge_pages
-= unused_resv_pages
;
984 /* Cannot return gigantic pages currently */
985 if (h
->order
>= MAX_ORDER
)
988 nr_pages
= min(unused_resv_pages
, h
->surplus_huge_pages
);
991 * We want to release as many surplus pages as possible, spread
992 * evenly across all nodes. Iterate across all nodes until we
993 * can no longer free unreserved surplus pages. This occurs when
994 * the nodes with surplus pages have no free pages.
995 * free_pool_huge_page() will balance the the frees across the
996 * on-line nodes for us and will handle the hstate accounting.
999 if (!free_pool_huge_page(h
, 1))
1005 * Determine if the huge page at addr within the vma has an associated
1006 * reservation. Where it does not we will need to logically increase
1007 * reservation and actually increase subpool usage before an allocation
1008 * can occur. Where any new reservation would be required the
1009 * reservation change is prepared, but not committed. Once the page
1010 * has been allocated from the subpool and instantiated the change should
1011 * be committed via vma_commit_reservation. No action is required on
1014 static long vma_needs_reservation(struct hstate
*h
,
1015 struct vm_area_struct
*vma
, unsigned long addr
)
1017 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1018 struct inode
*inode
= mapping
->host
;
1020 if (vma
->vm_flags
& VM_MAYSHARE
) {
1021 pgoff_t idx
= vma_hugecache_offset(h
, vma
, addr
);
1022 return region_chg(&inode
->i_mapping
->private_list
,
1025 } else if (!is_vma_resv_set(vma
, HPAGE_RESV_OWNER
)) {
1030 pgoff_t idx
= vma_hugecache_offset(h
, vma
, addr
);
1031 struct resv_map
*reservations
= vma_resv_map(vma
);
1033 err
= region_chg(&reservations
->regions
, idx
, idx
+ 1);
1039 static void vma_commit_reservation(struct hstate
*h
,
1040 struct vm_area_struct
*vma
, unsigned long addr
)
1042 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1043 struct inode
*inode
= mapping
->host
;
1045 if (vma
->vm_flags
& VM_MAYSHARE
) {
1046 pgoff_t idx
= vma_hugecache_offset(h
, vma
, addr
);
1047 region_add(&inode
->i_mapping
->private_list
, idx
, idx
+ 1);
1049 } else if (is_vma_resv_set(vma
, HPAGE_RESV_OWNER
)) {
1050 pgoff_t idx
= vma_hugecache_offset(h
, vma
, addr
);
1051 struct resv_map
*reservations
= vma_resv_map(vma
);
1053 /* Mark this page used in the map. */
1054 region_add(&reservations
->regions
, idx
, idx
+ 1);
1058 static struct page
*alloc_huge_page(struct vm_area_struct
*vma
,
1059 unsigned long addr
, int avoid_reserve
)
1061 struct hugepage_subpool
*spool
= subpool_vma(vma
);
1062 struct hstate
*h
= hstate_vma(vma
);
1067 * Processes that did not create the mapping will have no
1068 * reserves and will not have accounted against subpool
1069 * limit. Check that the subpool limit can be made before
1070 * satisfying the allocation MAP_NORESERVE mappings may also
1071 * need pages and subpool limit allocated allocated if no reserve
1074 chg
= vma_needs_reservation(h
, vma
, addr
);
1076 return ERR_PTR(-VM_FAULT_OOM
);
1078 if (hugepage_subpool_get_pages(spool
, chg
))
1079 return ERR_PTR(-VM_FAULT_SIGBUS
);
1081 spin_lock(&hugetlb_lock
);
1082 page
= dequeue_huge_page_vma(h
, vma
, addr
, avoid_reserve
);
1083 spin_unlock(&hugetlb_lock
);
1086 page
= alloc_buddy_huge_page(h
, vma
, addr
);
1088 hugepage_subpool_put_pages(spool
, chg
);
1089 return ERR_PTR(-VM_FAULT_SIGBUS
);
1093 set_page_refcounted(page
);
1094 set_page_private(page
, (unsigned long)spool
);
1096 vma_commit_reservation(h
, vma
, addr
);
1101 int __weak
alloc_bootmem_huge_page(struct hstate
*h
)
1103 struct huge_bootmem_page
*m
;
1104 int nr_nodes
= nodes_weight(node_online_map
);
1109 addr
= __alloc_bootmem_node_nopanic(
1110 NODE_DATA(h
->next_nid_to_alloc
),
1111 huge_page_size(h
), huge_page_size(h
), 0);
1113 hstate_next_node_to_alloc(h
);
1116 * Use the beginning of the huge page to store the
1117 * huge_bootmem_page struct (until gather_bootmem
1118 * puts them into the mem_map).
1128 BUG_ON((unsigned long)virt_to_phys(m
) & (huge_page_size(h
) - 1));
1129 /* Put them into a private list first because mem_map is not up yet */
1130 list_add(&m
->list
, &huge_boot_pages
);
1135 static void prep_compound_huge_page(struct page
*page
, int order
)
1137 if (unlikely(order
> (MAX_ORDER
- 1)))
1138 prep_compound_gigantic_page(page
, order
);
1140 prep_compound_page(page
, order
);
1143 /* Put bootmem huge pages into the standard lists after mem_map is up */
1144 static void __init
gather_bootmem_prealloc(void)
1146 struct huge_bootmem_page
*m
;
1148 list_for_each_entry(m
, &huge_boot_pages
, list
) {
1149 struct page
*page
= virt_to_page(m
);
1150 struct hstate
*h
= m
->hstate
;
1151 __ClearPageReserved(page
);
1152 WARN_ON(page_count(page
) != 1);
1153 prep_compound_huge_page(page
, h
->order
);
1154 prep_new_huge_page(h
, page
, page_to_nid(page
));
1156 * If we had gigantic hugepages allocated at boot time, we need
1157 * to restore the 'stolen' pages to totalram_pages in order to
1158 * fix confusing memory reports from free(1) and another
1159 * side-effects, like CommitLimit going negative.
1161 if (h
->order
> (MAX_ORDER
- 1))
1162 totalram_pages
+= 1 << h
->order
;
1166 static void __init
hugetlb_hstate_alloc_pages(struct hstate
*h
)
1170 for (i
= 0; i
< h
->max_huge_pages
; ++i
) {
1171 if (h
->order
>= MAX_ORDER
) {
1172 if (!alloc_bootmem_huge_page(h
))
1174 } else if (!alloc_fresh_huge_page(h
))
1177 h
->max_huge_pages
= i
;
1180 static void __init
hugetlb_init_hstates(void)
1184 for_each_hstate(h
) {
1185 /* oversize hugepages were init'ed in early boot */
1186 if (h
->order
< MAX_ORDER
)
1187 hugetlb_hstate_alloc_pages(h
);
1191 static char * __init
memfmt(char *buf
, unsigned long n
)
1193 if (n
>= (1UL << 30))
1194 sprintf(buf
, "%lu GB", n
>> 30);
1195 else if (n
>= (1UL << 20))
1196 sprintf(buf
, "%lu MB", n
>> 20);
1198 sprintf(buf
, "%lu KB", n
>> 10);
1202 static void __init
report_hugepages(void)
1206 for_each_hstate(h
) {
1208 printk(KERN_INFO
"HugeTLB registered %s page size, "
1209 "pre-allocated %ld pages\n",
1210 memfmt(buf
, huge_page_size(h
)),
1211 h
->free_huge_pages
);
1215 #ifdef CONFIG_HIGHMEM
1216 static void try_to_free_low(struct hstate
*h
, unsigned long count
)
1220 if (h
->order
>= MAX_ORDER
)
1223 for (i
= 0; i
< MAX_NUMNODES
; ++i
) {
1224 struct page
*page
, *next
;
1225 struct list_head
*freel
= &h
->hugepage_freelists
[i
];
1226 list_for_each_entry_safe(page
, next
, freel
, lru
) {
1227 if (count
>= h
->nr_huge_pages
)
1229 if (PageHighMem(page
))
1231 list_del(&page
->lru
);
1232 update_and_free_page(h
, page
);
1233 h
->free_huge_pages
--;
1234 h
->free_huge_pages_node
[page_to_nid(page
)]--;
1239 static inline void try_to_free_low(struct hstate
*h
, unsigned long count
)
1245 * Increment or decrement surplus_huge_pages. Keep node-specific counters
1246 * balanced by operating on them in a round-robin fashion.
1247 * Returns 1 if an adjustment was made.
1249 static int adjust_pool_surplus(struct hstate
*h
, int delta
)
1251 int start_nid
, next_nid
;
1254 VM_BUG_ON(delta
!= -1 && delta
!= 1);
1257 start_nid
= h
->next_nid_to_alloc
;
1259 start_nid
= h
->next_nid_to_free
;
1260 next_nid
= start_nid
;
1265 next_nid
= hstate_next_node_to_alloc(h
);
1267 * To shrink on this node, there must be a surplus page
1269 if (!h
->surplus_huge_pages_node
[nid
])
1273 next_nid
= hstate_next_node_to_free(h
);
1275 * Surplus cannot exceed the total number of pages
1277 if (h
->surplus_huge_pages_node
[nid
] >=
1278 h
->nr_huge_pages_node
[nid
])
1282 h
->surplus_huge_pages
+= delta
;
1283 h
->surplus_huge_pages_node
[nid
] += delta
;
1286 } while (next_nid
!= start_nid
);
1291 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
1292 static unsigned long set_max_huge_pages(struct hstate
*h
, unsigned long count
)
1294 unsigned long min_count
, ret
;
1296 if (h
->order
>= MAX_ORDER
)
1297 return h
->max_huge_pages
;
1300 * Increase the pool size
1301 * First take pages out of surplus state. Then make up the
1302 * remaining difference by allocating fresh huge pages.
1304 * We might race with alloc_buddy_huge_page() here and be unable
1305 * to convert a surplus huge page to a normal huge page. That is
1306 * not critical, though, it just means the overall size of the
1307 * pool might be one hugepage larger than it needs to be, but
1308 * within all the constraints specified by the sysctls.
1310 spin_lock(&hugetlb_lock
);
1311 while (h
->surplus_huge_pages
&& count
> persistent_huge_pages(h
)) {
1312 if (!adjust_pool_surplus(h
, -1))
1316 while (count
> persistent_huge_pages(h
)) {
1318 * If this allocation races such that we no longer need the
1319 * page, free_huge_page will handle it by freeing the page
1320 * and reducing the surplus.
1322 spin_unlock(&hugetlb_lock
);
1323 ret
= alloc_fresh_huge_page(h
);
1324 spin_lock(&hugetlb_lock
);
1331 * Decrease the pool size
1332 * First return free pages to the buddy allocator (being careful
1333 * to keep enough around to satisfy reservations). Then place
1334 * pages into surplus state as needed so the pool will shrink
1335 * to the desired size as pages become free.
1337 * By placing pages into the surplus state independent of the
1338 * overcommit value, we are allowing the surplus pool size to
1339 * exceed overcommit. There are few sane options here. Since
1340 * alloc_buddy_huge_page() is checking the global counter,
1341 * though, we'll note that we're not allowed to exceed surplus
1342 * and won't grow the pool anywhere else. Not until one of the
1343 * sysctls are changed, or the surplus pages go out of use.
1345 min_count
= h
->resv_huge_pages
+ h
->nr_huge_pages
- h
->free_huge_pages
;
1346 min_count
= max(count
, min_count
);
1347 try_to_free_low(h
, min_count
);
1348 while (min_count
< persistent_huge_pages(h
)) {
1349 if (!free_pool_huge_page(h
, 0))
1352 while (count
< persistent_huge_pages(h
)) {
1353 if (!adjust_pool_surplus(h
, 1))
1357 ret
= persistent_huge_pages(h
);
1358 spin_unlock(&hugetlb_lock
);
1362 #define HSTATE_ATTR_RO(_name) \
1363 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1365 #define HSTATE_ATTR(_name) \
1366 static struct kobj_attribute _name##_attr = \
1367 __ATTR(_name, 0644, _name##_show, _name##_store)
1369 static struct kobject
*hugepages_kobj
;
1370 static struct kobject
*hstate_kobjs
[HUGE_MAX_HSTATE
];
1372 static struct hstate
*kobj_to_hstate(struct kobject
*kobj
)
1375 for (i
= 0; i
< HUGE_MAX_HSTATE
; i
++)
1376 if (hstate_kobjs
[i
] == kobj
)
1382 static ssize_t
nr_hugepages_show(struct kobject
*kobj
,
1383 struct kobj_attribute
*attr
, char *buf
)
1385 struct hstate
*h
= kobj_to_hstate(kobj
);
1386 return sprintf(buf
, "%lu\n", h
->nr_huge_pages
);
1388 static ssize_t
nr_hugepages_store(struct kobject
*kobj
,
1389 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
1392 unsigned long input
;
1393 struct hstate
*h
= kobj_to_hstate(kobj
);
1395 err
= strict_strtoul(buf
, 10, &input
);
1399 h
->max_huge_pages
= set_max_huge_pages(h
, input
);
1403 HSTATE_ATTR(nr_hugepages
);
1405 static ssize_t
nr_overcommit_hugepages_show(struct kobject
*kobj
,
1406 struct kobj_attribute
*attr
, char *buf
)
1408 struct hstate
*h
= kobj_to_hstate(kobj
);
1409 return sprintf(buf
, "%lu\n", h
->nr_overcommit_huge_pages
);
1411 static ssize_t
nr_overcommit_hugepages_store(struct kobject
*kobj
,
1412 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
1415 unsigned long input
;
1416 struct hstate
*h
= kobj_to_hstate(kobj
);
1418 err
= strict_strtoul(buf
, 10, &input
);
1422 spin_lock(&hugetlb_lock
);
1423 h
->nr_overcommit_huge_pages
= input
;
1424 spin_unlock(&hugetlb_lock
);
1428 HSTATE_ATTR(nr_overcommit_hugepages
);
1430 static ssize_t
free_hugepages_show(struct kobject
*kobj
,
1431 struct kobj_attribute
*attr
, char *buf
)
1433 struct hstate
*h
= kobj_to_hstate(kobj
);
1434 return sprintf(buf
, "%lu\n", h
->free_huge_pages
);
1436 HSTATE_ATTR_RO(free_hugepages
);
1438 static ssize_t
resv_hugepages_show(struct kobject
*kobj
,
1439 struct kobj_attribute
*attr
, char *buf
)
1441 struct hstate
*h
= kobj_to_hstate(kobj
);
1442 return sprintf(buf
, "%lu\n", h
->resv_huge_pages
);
1444 HSTATE_ATTR_RO(resv_hugepages
);
1446 static ssize_t
surplus_hugepages_show(struct kobject
*kobj
,
1447 struct kobj_attribute
*attr
, char *buf
)
1449 struct hstate
*h
= kobj_to_hstate(kobj
);
1450 return sprintf(buf
, "%lu\n", h
->surplus_huge_pages
);
1452 HSTATE_ATTR_RO(surplus_hugepages
);
1454 static struct attribute
*hstate_attrs
[] = {
1455 &nr_hugepages_attr
.attr
,
1456 &nr_overcommit_hugepages_attr
.attr
,
1457 &free_hugepages_attr
.attr
,
1458 &resv_hugepages_attr
.attr
,
1459 &surplus_hugepages_attr
.attr
,
1463 static struct attribute_group hstate_attr_group
= {
1464 .attrs
= hstate_attrs
,
1467 static int __init
hugetlb_sysfs_add_hstate(struct hstate
*h
)
1471 hstate_kobjs
[h
- hstates
] = kobject_create_and_add(h
->name
,
1473 if (!hstate_kobjs
[h
- hstates
])
1476 retval
= sysfs_create_group(hstate_kobjs
[h
- hstates
],
1477 &hstate_attr_group
);
1479 kobject_put(hstate_kobjs
[h
- hstates
]);
1484 static void __init
hugetlb_sysfs_init(void)
1489 hugepages_kobj
= kobject_create_and_add("hugepages", mm_kobj
);
1490 if (!hugepages_kobj
)
1493 for_each_hstate(h
) {
1494 err
= hugetlb_sysfs_add_hstate(h
);
1496 printk(KERN_ERR
"Hugetlb: Unable to add hstate %s",
1501 static void __exit
hugetlb_exit(void)
1505 for_each_hstate(h
) {
1506 kobject_put(hstate_kobjs
[h
- hstates
]);
1509 kobject_put(hugepages_kobj
);
1511 module_exit(hugetlb_exit
);
1513 static int __init
hugetlb_init(void)
1515 /* Some platform decide whether they support huge pages at boot
1516 * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
1517 * there is no such support
1519 if (HPAGE_SHIFT
== 0)
1522 if (!size_to_hstate(default_hstate_size
)) {
1523 default_hstate_size
= HPAGE_SIZE
;
1524 if (!size_to_hstate(default_hstate_size
))
1525 hugetlb_add_hstate(HUGETLB_PAGE_ORDER
);
1527 default_hstate_idx
= size_to_hstate(default_hstate_size
) - hstates
;
1528 if (default_hstate_max_huge_pages
)
1529 default_hstate
.max_huge_pages
= default_hstate_max_huge_pages
;
1531 hugetlb_init_hstates();
1533 gather_bootmem_prealloc();
1537 hugetlb_sysfs_init();
1541 module_init(hugetlb_init
);
1543 /* Should be called on processing a hugepagesz=... option */
1544 void __init
hugetlb_add_hstate(unsigned order
)
1549 if (size_to_hstate(PAGE_SIZE
<< order
)) {
1550 printk(KERN_WARNING
"hugepagesz= specified twice, ignoring\n");
1553 BUG_ON(max_hstate
>= HUGE_MAX_HSTATE
);
1555 h
= &hstates
[max_hstate
++];
1557 h
->mask
= ~((1ULL << (order
+ PAGE_SHIFT
)) - 1);
1558 h
->nr_huge_pages
= 0;
1559 h
->free_huge_pages
= 0;
1560 for (i
= 0; i
< MAX_NUMNODES
; ++i
)
1561 INIT_LIST_HEAD(&h
->hugepage_freelists
[i
]);
1562 h
->next_nid_to_alloc
= first_node(node_online_map
);
1563 h
->next_nid_to_free
= first_node(node_online_map
);
1564 snprintf(h
->name
, HSTATE_NAME_LEN
, "hugepages-%lukB",
1565 huge_page_size(h
)/1024);
1570 static int __init
hugetlb_nrpages_setup(char *s
)
1573 static unsigned long *last_mhp
;
1576 * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
1577 * so this hugepages= parameter goes to the "default hstate".
1580 mhp
= &default_hstate_max_huge_pages
;
1582 mhp
= &parsed_hstate
->max_huge_pages
;
1584 if (mhp
== last_mhp
) {
1585 printk(KERN_WARNING
"hugepages= specified twice without "
1586 "interleaving hugepagesz=, ignoring\n");
1590 if (sscanf(s
, "%lu", mhp
) <= 0)
1594 * Global state is always initialized later in hugetlb_init.
1595 * But we need to allocate >= MAX_ORDER hstates here early to still
1596 * use the bootmem allocator.
1598 if (max_hstate
&& parsed_hstate
->order
>= MAX_ORDER
)
1599 hugetlb_hstate_alloc_pages(parsed_hstate
);
1605 __setup("hugepages=", hugetlb_nrpages_setup
);
1607 static int __init
hugetlb_default_setup(char *s
)
1609 default_hstate_size
= memparse(s
, &s
);
1612 __setup("default_hugepagesz=", hugetlb_default_setup
);
1614 static unsigned int cpuset_mems_nr(unsigned int *array
)
1617 unsigned int nr
= 0;
1619 for_each_node_mask(node
, cpuset_current_mems_allowed
)
1625 #ifdef CONFIG_SYSCTL
1626 int hugetlb_sysctl_handler(struct ctl_table
*table
, int write
,
1627 void __user
*buffer
,
1628 size_t *length
, loff_t
*ppos
)
1630 struct hstate
*h
= &default_hstate
;
1634 tmp
= h
->max_huge_pages
;
1637 table
->maxlen
= sizeof(unsigned long);
1638 proc_doulongvec_minmax(table
, write
, buffer
, length
, ppos
);
1641 h
->max_huge_pages
= set_max_huge_pages(h
, tmp
);
1646 int hugetlb_treat_movable_handler(struct ctl_table
*table
, int write
,
1647 void __user
*buffer
,
1648 size_t *length
, loff_t
*ppos
)
1650 proc_dointvec(table
, write
, buffer
, length
, ppos
);
1651 if (hugepages_treat_as_movable
)
1652 htlb_alloc_mask
= GFP_HIGHUSER_MOVABLE
;
1654 htlb_alloc_mask
= GFP_HIGHUSER
;
1658 int hugetlb_overcommit_handler(struct ctl_table
*table
, int write
,
1659 void __user
*buffer
,
1660 size_t *length
, loff_t
*ppos
)
1662 struct hstate
*h
= &default_hstate
;
1666 tmp
= h
->nr_overcommit_huge_pages
;
1669 table
->maxlen
= sizeof(unsigned long);
1670 proc_doulongvec_minmax(table
, write
, buffer
, length
, ppos
);
1673 spin_lock(&hugetlb_lock
);
1674 h
->nr_overcommit_huge_pages
= tmp
;
1675 spin_unlock(&hugetlb_lock
);
1681 #endif /* CONFIG_SYSCTL */
1683 void hugetlb_report_meminfo(struct seq_file
*m
)
1685 struct hstate
*h
= &default_hstate
;
1687 "HugePages_Total: %5lu\n"
1688 "HugePages_Free: %5lu\n"
1689 "HugePages_Rsvd: %5lu\n"
1690 "HugePages_Surp: %5lu\n"
1691 "Hugepagesize: %8lu kB\n",
1695 h
->surplus_huge_pages
,
1696 1UL << (huge_page_order(h
) + PAGE_SHIFT
- 10));
1699 int hugetlb_report_node_meminfo(int nid
, char *buf
)
1701 struct hstate
*h
= &default_hstate
;
1703 "Node %d HugePages_Total: %5u\n"
1704 "Node %d HugePages_Free: %5u\n"
1705 "Node %d HugePages_Surp: %5u\n",
1706 nid
, h
->nr_huge_pages_node
[nid
],
1707 nid
, h
->free_huge_pages_node
[nid
],
1708 nid
, h
->surplus_huge_pages_node
[nid
]);
1711 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
1712 unsigned long hugetlb_total_pages(void)
1714 struct hstate
*h
= &default_hstate
;
1715 return h
->nr_huge_pages
* pages_per_huge_page(h
);
1718 static int hugetlb_acct_memory(struct hstate
*h
, long delta
)
1722 spin_lock(&hugetlb_lock
);
1724 * When cpuset is configured, it breaks the strict hugetlb page
1725 * reservation as the accounting is done on a global variable. Such
1726 * reservation is completely rubbish in the presence of cpuset because
1727 * the reservation is not checked against page availability for the
1728 * current cpuset. Application can still potentially OOM'ed by kernel
1729 * with lack of free htlb page in cpuset that the task is in.
1730 * Attempt to enforce strict accounting with cpuset is almost
1731 * impossible (or too ugly) because cpuset is too fluid that
1732 * task or memory node can be dynamically moved between cpusets.
1734 * The change of semantics for shared hugetlb mapping with cpuset is
1735 * undesirable. However, in order to preserve some of the semantics,
1736 * we fall back to check against current free page availability as
1737 * a best attempt and hopefully to minimize the impact of changing
1738 * semantics that cpuset has.
1741 if (gather_surplus_pages(h
, delta
) < 0)
1744 if (delta
> cpuset_mems_nr(h
->free_huge_pages_node
)) {
1745 return_unused_surplus_pages(h
, delta
);
1752 return_unused_surplus_pages(h
, (unsigned long) -delta
);
1755 spin_unlock(&hugetlb_lock
);
1759 static void hugetlb_vm_op_open(struct vm_area_struct
*vma
)
1761 struct resv_map
*reservations
= vma_resv_map(vma
);
1764 * This new VMA should share its siblings reservation map if present.
1765 * The VMA will only ever have a valid reservation map pointer where
1766 * it is being copied for another still existing VMA. As that VMA
1767 * has a reference to the reservation map it cannot dissappear until
1768 * after this open call completes. It is therefore safe to take a
1769 * new reference here without additional locking.
1772 kref_get(&reservations
->refs
);
1775 static void hugetlb_vm_op_close(struct vm_area_struct
*vma
)
1777 struct hstate
*h
= hstate_vma(vma
);
1778 struct resv_map
*reservations
= vma_resv_map(vma
);
1779 struct hugepage_subpool
*spool
= subpool_vma(vma
);
1780 unsigned long reserve
;
1781 unsigned long start
;
1785 start
= vma_hugecache_offset(h
, vma
, vma
->vm_start
);
1786 end
= vma_hugecache_offset(h
, vma
, vma
->vm_end
);
1788 reserve
= (end
- start
) -
1789 region_count(&reservations
->regions
, start
, end
);
1791 kref_put(&reservations
->refs
, resv_map_release
);
1794 hugetlb_acct_memory(h
, -reserve
);
1795 hugepage_subpool_put_pages(spool
, reserve
);
1801 * We cannot handle pagefaults against hugetlb pages at all. They cause
1802 * handle_mm_fault() to try to instantiate regular-sized pages in the
1803 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
1806 static int hugetlb_vm_op_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1812 const struct vm_operations_struct hugetlb_vm_ops
= {
1813 .fault
= hugetlb_vm_op_fault
,
1814 .open
= hugetlb_vm_op_open
,
1815 .close
= hugetlb_vm_op_close
,
1818 static pte_t
make_huge_pte(struct vm_area_struct
*vma
, struct page
*page
,
1825 pte_mkwrite(pte_mkdirty(mk_pte(page
, vma
->vm_page_prot
)));
1827 entry
= huge_pte_wrprotect(mk_pte(page
, vma
->vm_page_prot
));
1829 entry
= pte_mkyoung(entry
);
1830 entry
= pte_mkhuge(entry
);
1835 static void set_huge_ptep_writable(struct vm_area_struct
*vma
,
1836 unsigned long address
, pte_t
*ptep
)
1840 entry
= pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep
)));
1841 if (huge_ptep_set_access_flags(vma
, address
, ptep
, entry
, 1)) {
1842 update_mmu_cache(vma
, address
, entry
);
1847 int copy_hugetlb_page_range(struct mm_struct
*dst
, struct mm_struct
*src
,
1848 struct vm_area_struct
*vma
)
1850 pte_t
*src_pte
, *dst_pte
, entry
;
1851 struct page
*ptepage
;
1854 struct hstate
*h
= hstate_vma(vma
);
1855 unsigned long sz
= huge_page_size(h
);
1857 cow
= (vma
->vm_flags
& (VM_SHARED
| VM_MAYWRITE
)) == VM_MAYWRITE
;
1859 for (addr
= vma
->vm_start
; addr
< vma
->vm_end
; addr
+= sz
) {
1860 src_pte
= huge_pte_offset(src
, addr
);
1863 dst_pte
= huge_pte_alloc(dst
, addr
, sz
);
1867 /* If the pagetables are shared don't copy or take references */
1868 if (dst_pte
== src_pte
)
1871 spin_lock(&dst
->page_table_lock
);
1872 spin_lock_nested(&src
->page_table_lock
, SINGLE_DEPTH_NESTING
);
1873 if (!huge_pte_none(huge_ptep_get(src_pte
))) {
1875 huge_ptep_set_wrprotect(src
, addr
, src_pte
);
1876 entry
= huge_ptep_get(src_pte
);
1877 ptepage
= pte_page(entry
);
1879 set_huge_pte_at(dst
, addr
, dst_pte
, entry
);
1881 spin_unlock(&src
->page_table_lock
);
1882 spin_unlock(&dst
->page_table_lock
);
1890 void __unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
1891 unsigned long end
, struct page
*ref_page
)
1893 struct mm_struct
*mm
= vma
->vm_mm
;
1894 unsigned long address
;
1899 struct hstate
*h
= hstate_vma(vma
);
1900 unsigned long sz
= huge_page_size(h
);
1903 * A page gathering list, protected by per file i_mmap_lock. The
1904 * lock is used to avoid list corruption from multiple unmapping
1905 * of the same page since we are using page->lru.
1907 LIST_HEAD(page_list
);
1909 WARN_ON(!is_vm_hugetlb_page(vma
));
1910 BUG_ON(start
& ~huge_page_mask(h
));
1911 BUG_ON(end
& ~huge_page_mask(h
));
1913 mmu_notifier_invalidate_range_start(mm
, start
, end
);
1914 spin_lock(&mm
->page_table_lock
);
1915 for (address
= start
; address
< end
; address
+= sz
) {
1916 ptep
= huge_pte_offset(mm
, address
);
1920 if (huge_pmd_unshare(mm
, &address
, ptep
))
1924 * If a reference page is supplied, it is because a specific
1925 * page is being unmapped, not a range. Ensure the page we
1926 * are about to unmap is the actual page of interest.
1929 pte
= huge_ptep_get(ptep
);
1930 if (huge_pte_none(pte
))
1932 page
= pte_page(pte
);
1933 if (page
!= ref_page
)
1937 * Mark the VMA as having unmapped its page so that
1938 * future faults in this VMA will fail rather than
1939 * looking like data was lost
1941 set_vma_resv_flags(vma
, HPAGE_RESV_UNMAPPED
);
1944 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
1945 if (huge_pte_none(pte
))
1948 page
= pte_page(pte
);
1950 set_page_dirty(page
);
1951 list_add(&page
->lru
, &page_list
);
1953 spin_unlock(&mm
->page_table_lock
);
1954 flush_tlb_range(vma
, start
, end
);
1955 mmu_notifier_invalidate_range_end(mm
, start
, end
);
1956 list_for_each_entry_safe(page
, tmp
, &page_list
, lru
) {
1957 list_del(&page
->lru
);
1962 void unmap_hugepage_range(struct vm_area_struct
*vma
, unsigned long start
,
1963 unsigned long end
, struct page
*ref_page
)
1965 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1966 __unmap_hugepage_range(vma
, start
, end
, ref_page
);
1967 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
1971 * This is called when the original mapper is failing to COW a MAP_PRIVATE
1972 * mappping it owns the reserve page for. The intention is to unmap the page
1973 * from other VMAs and let the children be SIGKILLed if they are faulting the
1976 static int unmap_ref_private(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
1977 struct page
*page
, unsigned long address
)
1979 struct hstate
*h
= hstate_vma(vma
);
1980 struct vm_area_struct
*iter_vma
;
1981 struct address_space
*mapping
;
1982 struct prio_tree_iter iter
;
1986 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
1987 * from page cache lookup which is in HPAGE_SIZE units.
1989 address
= address
& huge_page_mask(h
);
1990 pgoff
= ((address
- vma
->vm_start
) >> PAGE_SHIFT
)
1991 + (vma
->vm_pgoff
>> PAGE_SHIFT
);
1992 mapping
= vma
->vm_file
->f_dentry
->d_inode
->i_mapping
;
1994 vma_prio_tree_foreach(iter_vma
, &iter
, &mapping
->i_mmap
, pgoff
, pgoff
) {
1995 /* Do not unmap the current VMA */
1996 if (iter_vma
== vma
)
2000 * Unmap the page from other VMAs without their own reserves.
2001 * They get marked to be SIGKILLed if they fault in these
2002 * areas. This is because a future no-page fault on this VMA
2003 * could insert a zeroed page instead of the data existing
2004 * from the time of fork. This would look like data corruption
2006 if (!is_vma_resv_set(iter_vma
, HPAGE_RESV_OWNER
))
2007 unmap_hugepage_range(iter_vma
,
2008 address
, address
+ huge_page_size(h
),
2015 static int hugetlb_cow(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2016 unsigned long address
, pte_t
*ptep
, pte_t pte
,
2017 struct page
*pagecache_page
)
2019 struct hstate
*h
= hstate_vma(vma
);
2020 struct page
*old_page
, *new_page
;
2022 int outside_reserve
= 0;
2024 old_page
= pte_page(pte
);
2027 /* If no-one else is actually using this page, avoid the copy
2028 * and just make the page writable */
2029 avoidcopy
= (page_count(old_page
) == 1);
2031 set_huge_ptep_writable(vma
, address
, ptep
);
2036 * If the process that created a MAP_PRIVATE mapping is about to
2037 * perform a COW due to a shared page count, attempt to satisfy
2038 * the allocation without using the existing reserves. The pagecache
2039 * page is used to determine if the reserve at this address was
2040 * consumed or not. If reserves were used, a partial faulted mapping
2041 * at the time of fork() could consume its reserves on COW instead
2042 * of the full address range.
2044 if (!(vma
->vm_flags
& VM_MAYSHARE
) &&
2045 is_vma_resv_set(vma
, HPAGE_RESV_OWNER
) &&
2046 old_page
!= pagecache_page
)
2047 outside_reserve
= 1;
2049 page_cache_get(old_page
);
2050 new_page
= alloc_huge_page(vma
, address
, outside_reserve
);
2052 if (IS_ERR(new_page
)) {
2053 page_cache_release(old_page
);
2056 * If a process owning a MAP_PRIVATE mapping fails to COW,
2057 * it is due to references held by a child and an insufficient
2058 * huge page pool. To guarantee the original mappers
2059 * reliability, unmap the page from child processes. The child
2060 * may get SIGKILLed if it later faults.
2062 if (outside_reserve
) {
2063 BUG_ON(huge_pte_none(pte
));
2064 if (unmap_ref_private(mm
, vma
, old_page
, address
)) {
2065 BUG_ON(page_count(old_page
) != 1);
2066 BUG_ON(huge_pte_none(pte
));
2067 goto retry_avoidcopy
;
2072 return -PTR_ERR(new_page
);
2075 spin_unlock(&mm
->page_table_lock
);
2076 copy_huge_page(new_page
, old_page
, address
, vma
);
2077 __SetPageUptodate(new_page
);
2078 spin_lock(&mm
->page_table_lock
);
2080 ptep
= huge_pte_offset(mm
, address
& huge_page_mask(h
));
2081 if (likely(pte_same(huge_ptep_get(ptep
), pte
))) {
2083 huge_ptep_clear_flush(vma
, address
, ptep
);
2084 set_huge_pte_at(mm
, address
, ptep
,
2085 make_huge_pte(vma
, new_page
, 1));
2086 /* Make the old page be freed below */
2087 new_page
= old_page
;
2089 page_cache_release(new_page
);
2090 page_cache_release(old_page
);
2094 /* Return the pagecache page at a given address within a VMA */
2095 static struct page
*hugetlbfs_pagecache_page(struct hstate
*h
,
2096 struct vm_area_struct
*vma
, unsigned long address
)
2098 struct address_space
*mapping
;
2101 mapping
= vma
->vm_file
->f_mapping
;
2102 idx
= vma_hugecache_offset(h
, vma
, address
);
2104 return find_lock_page(mapping
, idx
);
2108 * Return whether there is a pagecache page to back given address within VMA.
2109 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
2111 static bool hugetlbfs_pagecache_present(struct hstate
*h
,
2112 struct vm_area_struct
*vma
, unsigned long address
)
2114 struct address_space
*mapping
;
2118 mapping
= vma
->vm_file
->f_mapping
;
2119 idx
= vma_hugecache_offset(h
, vma
, address
);
2121 page
= find_get_page(mapping
, idx
);
2124 return page
!= NULL
;
2127 static int hugetlb_no_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2128 unsigned long address
, pte_t
*ptep
, unsigned int flags
)
2130 struct hstate
*h
= hstate_vma(vma
);
2131 int ret
= VM_FAULT_SIGBUS
;
2135 struct address_space
*mapping
;
2139 * Currently, we are forced to kill the process in the event the
2140 * original mapper has unmapped pages from the child due to a failed
2141 * COW. Warn that such a situation has occured as it may not be obvious
2143 if (is_vma_resv_set(vma
, HPAGE_RESV_UNMAPPED
)) {
2145 "PID %d killed due to inadequate hugepage pool\n",
2150 mapping
= vma
->vm_file
->f_mapping
;
2151 idx
= vma_hugecache_offset(h
, vma
, address
);
2154 * Use page lock to guard against racing truncation
2155 * before we get page_table_lock.
2158 page
= find_lock_page(mapping
, idx
);
2160 size
= i_size_read(mapping
->host
) >> huge_page_shift(h
);
2163 page
= alloc_huge_page(vma
, address
, 0);
2165 ret
= -PTR_ERR(page
);
2168 clear_huge_page(page
, address
, huge_page_size(h
));
2169 __SetPageUptodate(page
);
2171 if (vma
->vm_flags
& VM_MAYSHARE
) {
2173 struct inode
*inode
= mapping
->host
;
2175 err
= add_to_page_cache(page
, mapping
, idx
, GFP_KERNEL
);
2183 spin_lock(&inode
->i_lock
);
2184 inode
->i_blocks
+= blocks_per_huge_page(h
);
2185 spin_unlock(&inode
->i_lock
);
2188 page
->mapping
= HUGETLB_POISON
;
2193 * If we are going to COW a private mapping later, we examine the
2194 * pending reservations for this page now. This will ensure that
2195 * any allocations necessary to record that reservation occur outside
2198 if ((flags
& FAULT_FLAG_WRITE
) && !(vma
->vm_flags
& VM_SHARED
))
2199 if (vma_needs_reservation(h
, vma
, address
) < 0) {
2201 goto backout_unlocked
;
2204 spin_lock(&mm
->page_table_lock
);
2205 size
= i_size_read(mapping
->host
) >> huge_page_shift(h
);
2210 if (!huge_pte_none(huge_ptep_get(ptep
)))
2213 new_pte
= make_huge_pte(vma
, page
, ((vma
->vm_flags
& VM_WRITE
)
2214 && (vma
->vm_flags
& VM_SHARED
)));
2215 set_huge_pte_at(mm
, address
, ptep
, new_pte
);
2217 if ((flags
& FAULT_FLAG_WRITE
) && !(vma
->vm_flags
& VM_SHARED
)) {
2218 /* Optimization, do the COW without a second fault */
2219 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, new_pte
, page
);
2222 spin_unlock(&mm
->page_table_lock
);
2228 spin_unlock(&mm
->page_table_lock
);
2235 int hugetlb_fault(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2236 unsigned long address
, unsigned int flags
)
2241 struct page
*pagecache_page
= NULL
;
2242 static DEFINE_MUTEX(hugetlb_instantiation_mutex
);
2243 struct hstate
*h
= hstate_vma(vma
);
2245 ptep
= huge_pte_alloc(mm
, address
, huge_page_size(h
));
2247 return VM_FAULT_OOM
;
2250 * Serialize hugepage allocation and instantiation, so that we don't
2251 * get spurious allocation failures if two CPUs race to instantiate
2252 * the same page in the page cache.
2254 mutex_lock(&hugetlb_instantiation_mutex
);
2255 entry
= huge_ptep_get(ptep
);
2256 if (huge_pte_none(entry
)) {
2257 ret
= hugetlb_no_page(mm
, vma
, address
, ptep
, flags
);
2264 * If we are going to COW the mapping later, we examine the pending
2265 * reservations for this page now. This will ensure that any
2266 * allocations necessary to record that reservation occur outside the
2267 * spinlock. For private mappings, we also lookup the pagecache
2268 * page now as it is used to determine if a reservation has been
2271 if ((flags
& FAULT_FLAG_WRITE
) && !pte_write(entry
)) {
2272 if (vma_needs_reservation(h
, vma
, address
) < 0) {
2277 if (!(vma
->vm_flags
& VM_MAYSHARE
))
2278 pagecache_page
= hugetlbfs_pagecache_page(h
,
2282 spin_lock(&mm
->page_table_lock
);
2283 /* Check for a racing update before calling hugetlb_cow */
2284 if (unlikely(!pte_same(entry
, huge_ptep_get(ptep
))))
2285 goto out_page_table_lock
;
2288 if (flags
& FAULT_FLAG_WRITE
) {
2289 if (!pte_write(entry
)) {
2290 ret
= hugetlb_cow(mm
, vma
, address
, ptep
, entry
,
2292 goto out_page_table_lock
;
2294 entry
= pte_mkdirty(entry
);
2296 entry
= pte_mkyoung(entry
);
2297 if (huge_ptep_set_access_flags(vma
, address
, ptep
, entry
,
2298 flags
& FAULT_FLAG_WRITE
))
2299 update_mmu_cache(vma
, address
, entry
);
2301 out_page_table_lock
:
2302 spin_unlock(&mm
->page_table_lock
);
2304 if (pagecache_page
) {
2305 unlock_page(pagecache_page
);
2306 put_page(pagecache_page
);
2310 mutex_unlock(&hugetlb_instantiation_mutex
);
2315 /* Can be overriden by architectures */
2316 __attribute__((weak
)) struct page
*
2317 follow_huge_pud(struct mm_struct
*mm
, unsigned long address
,
2318 pud_t
*pud
, int write
)
2324 int follow_hugetlb_page(struct mm_struct
*mm
, struct vm_area_struct
*vma
,
2325 struct page
**pages
, struct vm_area_struct
**vmas
,
2326 unsigned long *position
, int *length
, int i
,
2329 unsigned long pfn_offset
;
2330 unsigned long vaddr
= *position
;
2331 int remainder
= *length
;
2332 struct hstate
*h
= hstate_vma(vma
);
2334 spin_lock(&mm
->page_table_lock
);
2335 while (vaddr
< vma
->vm_end
&& remainder
) {
2341 * Some archs (sparc64, sh*) have multiple pte_ts to
2342 * each hugepage. We have to make sure we get the
2343 * first, for the page indexing below to work.
2345 pte
= huge_pte_offset(mm
, vaddr
& huge_page_mask(h
));
2346 absent
= !pte
|| huge_pte_none(huge_ptep_get(pte
));
2349 * When coredumping, it suits get_dump_page if we just return
2350 * an error where there's an empty slot with no huge pagecache
2351 * to back it. This way, we avoid allocating a hugepage, and
2352 * the sparse dumpfile avoids allocating disk blocks, but its
2353 * huge holes still show up with zeroes where they need to be.
2355 if (absent
&& (flags
& FOLL_DUMP
) &&
2356 !hugetlbfs_pagecache_present(h
, vma
, vaddr
)) {
2362 ((flags
& FOLL_WRITE
) && !pte_write(huge_ptep_get(pte
)))) {
2365 spin_unlock(&mm
->page_table_lock
);
2366 ret
= hugetlb_fault(mm
, vma
, vaddr
,
2367 (flags
& FOLL_WRITE
) ? FAULT_FLAG_WRITE
: 0);
2368 spin_lock(&mm
->page_table_lock
);
2369 if (!(ret
& VM_FAULT_ERROR
))
2376 pfn_offset
= (vaddr
& ~huge_page_mask(h
)) >> PAGE_SHIFT
;
2377 page
= pte_page(huge_ptep_get(pte
));
2380 pages
[i
] = mem_map_offset(page
, pfn_offset
);
2391 if (vaddr
< vma
->vm_end
&& remainder
&&
2392 pfn_offset
< pages_per_huge_page(h
)) {
2394 * We use pfn_offset to avoid touching the pageframes
2395 * of this compound page.
2400 spin_unlock(&mm
->page_table_lock
);
2401 *length
= remainder
;
2404 return i
? i
: -EFAULT
;
2407 void hugetlb_change_protection(struct vm_area_struct
*vma
,
2408 unsigned long address
, unsigned long end
, pgprot_t newprot
)
2410 struct mm_struct
*mm
= vma
->vm_mm
;
2411 unsigned long start
= address
;
2414 struct hstate
*h
= hstate_vma(vma
);
2416 BUG_ON(address
>= end
);
2417 flush_cache_range(vma
, address
, end
);
2419 spin_lock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
2420 spin_lock(&mm
->page_table_lock
);
2421 for (; address
< end
; address
+= huge_page_size(h
)) {
2422 ptep
= huge_pte_offset(mm
, address
);
2425 if (huge_pmd_unshare(mm
, &address
, ptep
))
2427 if (!huge_pte_none(huge_ptep_get(ptep
))) {
2428 pte
= huge_ptep_get_and_clear(mm
, address
, ptep
);
2429 pte
= pte_mkhuge(pte_modify(pte
, newprot
));
2430 set_huge_pte_at(mm
, address
, ptep
, pte
);
2433 spin_unlock(&mm
->page_table_lock
);
2434 spin_unlock(&vma
->vm_file
->f_mapping
->i_mmap_lock
);
2436 flush_tlb_range(vma
, start
, end
);
2439 int hugetlb_reserve_pages(struct inode
*inode
,
2441 struct vm_area_struct
*vma
,
2445 struct hstate
*h
= hstate_inode(inode
);
2446 struct hugepage_subpool
*spool
= subpool_inode(inode
);
2449 * Only apply hugepage reservation if asked. At fault time, an
2450 * attempt will be made for VM_NORESERVE to allocate a page
2451 * without using reserves
2453 if (acctflag
& VM_NORESERVE
)
2457 * Shared mappings base their reservation on the number of pages that
2458 * are already allocated on behalf of the file. Private mappings need
2459 * to reserve the full area even if read-only as mprotect() may be
2460 * called to make the mapping read-write. Assume !vma is a shm mapping
2462 if (!vma
|| vma
->vm_flags
& VM_MAYSHARE
)
2463 chg
= region_chg(&inode
->i_mapping
->private_list
, from
, to
);
2465 struct resv_map
*resv_map
= resv_map_alloc();
2471 set_vma_resv_map(vma
, resv_map
);
2472 set_vma_resv_flags(vma
, HPAGE_RESV_OWNER
);
2478 /* There must be enough pages in the subpool for the mapping */
2479 if (hugepage_subpool_get_pages(spool
, chg
))
2483 * Check enough hugepages are available for the reservation.
2484 * Hand the pages back to the subpool if there are not
2486 ret
= hugetlb_acct_memory(h
, chg
);
2488 hugepage_subpool_put_pages(spool
, chg
);
2493 * Account for the reservations made. Shared mappings record regions
2494 * that have reservations as they are shared by multiple VMAs.
2495 * When the last VMA disappears, the region map says how much
2496 * the reservation was and the page cache tells how much of
2497 * the reservation was consumed. Private mappings are per-VMA and
2498 * only the consumed reservations are tracked. When the VMA
2499 * disappears, the original reservation is the VMA size and the
2500 * consumed reservations are stored in the map. Hence, nothing
2501 * else has to be done for private mappings here
2503 if (!vma
|| vma
->vm_flags
& VM_MAYSHARE
)
2504 region_add(&inode
->i_mapping
->private_list
, from
, to
);
2508 void hugetlb_unreserve_pages(struct inode
*inode
, long offset
, long freed
)
2510 struct hstate
*h
= hstate_inode(inode
);
2511 long chg
= region_truncate(&inode
->i_mapping
->private_list
, offset
);
2512 struct hugepage_subpool
*spool
= subpool_inode(inode
);
2514 spin_lock(&inode
->i_lock
);
2515 inode
->i_blocks
-= (blocks_per_huge_page(h
) * freed
);
2516 spin_unlock(&inode
->i_lock
);
2518 hugepage_subpool_put_pages(spool
, (chg
- freed
));
2519 hugetlb_acct_memory(h
, -(chg
- freed
));