1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic hugetlb support.
4 * (C) Nadia Yvette Chambers, April 2004
6 #include <linux/list.h>
7 #include <linux/init.h>
9 #include <linux/seq_file.h>
10 #include <linux/sysctl.h>
11 #include <linux/highmem.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/nodemask.h>
14 #include <linux/pagemap.h>
15 #include <linux/mempolicy.h>
16 #include <linux/compiler.h>
17 #include <linux/cpuset.h>
18 #include <linux/mutex.h>
19 #include <linux/memblock.h>
20 #include <linux/sysfs.h>
21 #include <linux/slab.h>
22 #include <linux/sched/mm.h>
23 #include <linux/mmdebug.h>
24 #include <linux/sched/signal.h>
25 #include <linux/rmap.h>
26 #include <linux/string_helpers.h>
27 #include <linux/swap.h>
28 #include <linux/swapops.h>
29 #include <linux/jhash.h>
30 #include <linux/numa.h>
31 #include <linux/llist.h>
32 #include <linux/cma.h>
33 #include <linux/migrate.h>
34 #include <linux/nospec.h>
35 #include <linux/delayacct.h>
38 #include <asm/pgalloc.h>
42 #include <linux/hugetlb.h>
43 #include <linux/hugetlb_cgroup.h>
44 #include <linux/node.h>
45 #include <linux/page_owner.h>
47 #include "hugetlb_vmemmap.h"
49 int hugetlb_max_hstate __read_mostly;
50 unsigned int default_hstate_idx;
51 struct hstate hstates[HUGE_MAX_HSTATE];
54 static struct cma *hugetlb_cma[MAX_NUMNODES];
55 static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
56 static bool hugetlb_cma_page(struct page *page, unsigned int order)
58 return cma_pages_valid(hugetlb_cma[page_to_nid(page)], page,
62 static bool hugetlb_cma_page(struct page *page, unsigned int order)
67 static unsigned long hugetlb_cma_size __initdata;
69 __initdata LIST_HEAD(huge_boot_pages);
71 /* for command line parsing */
72 static struct hstate * __initdata parsed_hstate;
73 static unsigned long __initdata default_hstate_max_huge_pages;
74 static bool __initdata parsed_valid_hugepagesz = true;
75 static bool __initdata parsed_default_hugepagesz;
76 static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
79 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
80 * free_huge_pages, and surplus_huge_pages.
82 DEFINE_SPINLOCK(hugetlb_lock);
85 * Serializes faults on the same logical page. This is used to
86 * prevent spurious OOMs when the hugepage pool is fully utilized.
88 static int num_fault_mutexes;
89 struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
91 /* Forward declaration */
92 static int hugetlb_acct_memory(struct hstate *h, long delta);
94 static inline bool subpool_is_free(struct hugepage_subpool *spool)
98 if (spool->max_hpages != -1)
99 return spool->used_hpages == 0;
100 if (spool->min_hpages != -1)
101 return spool->rsv_hpages == spool->min_hpages;
106 static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
107 unsigned long irq_flags)
109 spin_unlock_irqrestore(&spool->lock, irq_flags);
111 /* If no pages are used, and no other handles to the subpool
112 * remain, give up any reservations based on minimum size and
113 * free the subpool */
114 if (subpool_is_free(spool)) {
115 if (spool->min_hpages != -1)
116 hugetlb_acct_memory(spool->hstate,
122 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
125 struct hugepage_subpool *spool;
127 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
131 spin_lock_init(&spool->lock);
133 spool->max_hpages = max_hpages;
135 spool->min_hpages = min_hpages;
137 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
141 spool->rsv_hpages = min_hpages;
146 void hugepage_put_subpool(struct hugepage_subpool *spool)
150 spin_lock_irqsave(&spool->lock, flags);
151 BUG_ON(!spool->count);
153 unlock_or_release_subpool(spool, flags);
157 * Subpool accounting for allocating and reserving pages.
158 * Return -ENOMEM if there are not enough resources to satisfy the
159 * request. Otherwise, return the number of pages by which the
160 * global pools must be adjusted (upward). The returned value may
161 * only be different than the passed value (delta) in the case where
162 * a subpool minimum size must be maintained.
164 static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
172 spin_lock_irq(&spool->lock);
174 if (spool->max_hpages != -1) { /* maximum size accounting */
175 if ((spool->used_hpages + delta) <= spool->max_hpages)
176 spool->used_hpages += delta;
183 /* minimum size accounting */
184 if (spool->min_hpages != -1 && spool->rsv_hpages) {
185 if (delta > spool->rsv_hpages) {
187 * Asking for more reserves than those already taken on
188 * behalf of subpool. Return difference.
190 ret = delta - spool->rsv_hpages;
191 spool->rsv_hpages = 0;
193 ret = 0; /* reserves already accounted for */
194 spool->rsv_hpages -= delta;
199 spin_unlock_irq(&spool->lock);
204 * Subpool accounting for freeing and unreserving pages.
205 * Return the number of global page reservations that must be dropped.
206 * The return value may only be different than the passed value (delta)
207 * in the case where a subpool minimum size must be maintained.
209 static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
218 spin_lock_irqsave(&spool->lock, flags);
220 if (spool->max_hpages != -1) /* maximum size accounting */
221 spool->used_hpages -= delta;
223 /* minimum size accounting */
224 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
225 if (spool->rsv_hpages + delta <= spool->min_hpages)
228 ret = spool->rsv_hpages + delta - spool->min_hpages;
230 spool->rsv_hpages += delta;
231 if (spool->rsv_hpages > spool->min_hpages)
232 spool->rsv_hpages = spool->min_hpages;
236 * If hugetlbfs_put_super couldn't free spool due to an outstanding
237 * quota reference, free it now.
239 unlock_or_release_subpool(spool, flags);
244 static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
246 return HUGETLBFS_SB(inode->i_sb)->spool;
249 static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
251 return subpool_inode(file_inode(vma->vm_file));
254 /* Helper that removes a struct file_region from the resv_map cache and returns
257 static struct file_region *
258 get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
260 struct file_region *nrg = NULL;
262 VM_BUG_ON(resv->region_cache_count <= 0);
264 resv->region_cache_count--;
265 nrg = list_first_entry(&resv->region_cache, struct file_region, link);
266 list_del(&nrg->link);
274 static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
275 struct file_region *rg)
277 #ifdef CONFIG_CGROUP_HUGETLB
278 nrg->reservation_counter = rg->reservation_counter;
285 /* Helper that records hugetlb_cgroup uncharge info. */
286 static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
288 struct resv_map *resv,
289 struct file_region *nrg)
291 #ifdef CONFIG_CGROUP_HUGETLB
293 nrg->reservation_counter =
294 &h_cg->rsvd_hugepage[hstate_index(h)];
295 nrg->css = &h_cg->css;
297 * The caller will hold exactly one h_cg->css reference for the
298 * whole contiguous reservation region. But this area might be
299 * scattered when there are already some file_regions reside in
300 * it. As a result, many file_regions may share only one css
301 * reference. In order to ensure that one file_region must hold
302 * exactly one h_cg->css reference, we should do css_get for
303 * each file_region and leave the reference held by caller
307 if (!resv->pages_per_hpage)
308 resv->pages_per_hpage = pages_per_huge_page(h);
309 /* pages_per_hpage should be the same for all entries in
312 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
314 nrg->reservation_counter = NULL;
320 static void put_uncharge_info(struct file_region *rg)
322 #ifdef CONFIG_CGROUP_HUGETLB
328 static bool has_same_uncharge_info(struct file_region *rg,
329 struct file_region *org)
331 #ifdef CONFIG_CGROUP_HUGETLB
332 return rg->reservation_counter == org->reservation_counter &&
340 static void coalesce_file_region(struct resv_map *resv, struct file_region *rg)
342 struct file_region *nrg = NULL, *prg = NULL;
344 prg = list_prev_entry(rg, link);
345 if (&prg->link != &resv->regions && prg->to == rg->from &&
346 has_same_uncharge_info(prg, rg)) {
350 put_uncharge_info(rg);
356 nrg = list_next_entry(rg, link);
357 if (&nrg->link != &resv->regions && nrg->from == rg->to &&
358 has_same_uncharge_info(nrg, rg)) {
359 nrg->from = rg->from;
362 put_uncharge_info(rg);
368 hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from,
369 long to, struct hstate *h, struct hugetlb_cgroup *cg,
370 long *regions_needed)
372 struct file_region *nrg;
374 if (!regions_needed) {
375 nrg = get_file_region_entry_from_cache(map, from, to);
376 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
377 list_add(&nrg->link, rg);
378 coalesce_file_region(map, nrg);
380 *regions_needed += 1;
386 * Must be called with resv->lock held.
388 * Calling this with regions_needed != NULL will count the number of pages
389 * to be added but will not modify the linked list. And regions_needed will
390 * indicate the number of file_regions needed in the cache to carry out to add
391 * the regions for this range.
393 static long add_reservation_in_range(struct resv_map *resv, long f, long t,
394 struct hugetlb_cgroup *h_cg,
395 struct hstate *h, long *regions_needed)
398 struct list_head *head = &resv->regions;
399 long last_accounted_offset = f;
400 struct file_region *iter, *trg = NULL;
401 struct list_head *rg = NULL;
406 /* In this loop, we essentially handle an entry for the range
407 * [last_accounted_offset, iter->from), at every iteration, with some
410 list_for_each_entry_safe(iter, trg, head, link) {
411 /* Skip irrelevant regions that start before our range. */
412 if (iter->from < f) {
413 /* If this region ends after the last accounted offset,
414 * then we need to update last_accounted_offset.
416 if (iter->to > last_accounted_offset)
417 last_accounted_offset = iter->to;
421 /* When we find a region that starts beyond our range, we've
424 if (iter->from >= t) {
425 rg = iter->link.prev;
429 /* Add an entry for last_accounted_offset -> iter->from, and
430 * update last_accounted_offset.
432 if (iter->from > last_accounted_offset)
433 add += hugetlb_resv_map_add(resv, iter->link.prev,
434 last_accounted_offset,
438 last_accounted_offset = iter->to;
441 /* Handle the case where our range extends beyond
442 * last_accounted_offset.
446 if (last_accounted_offset < t)
447 add += hugetlb_resv_map_add(resv, rg, last_accounted_offset,
448 t, h, h_cg, regions_needed);
453 /* Must be called with resv->lock acquired. Will drop lock to allocate entries.
455 static int allocate_file_region_entries(struct resv_map *resv,
457 __must_hold(&resv->lock)
459 LIST_HEAD(allocated_regions);
460 int to_allocate = 0, i = 0;
461 struct file_region *trg = NULL, *rg = NULL;
463 VM_BUG_ON(regions_needed < 0);
466 * Check for sufficient descriptors in the cache to accommodate
467 * the number of in progress add operations plus regions_needed.
469 * This is a while loop because when we drop the lock, some other call
470 * to region_add or region_del may have consumed some region_entries,
471 * so we keep looping here until we finally have enough entries for
472 * (adds_in_progress + regions_needed).
474 while (resv->region_cache_count <
475 (resv->adds_in_progress + regions_needed)) {
476 to_allocate = resv->adds_in_progress + regions_needed -
477 resv->region_cache_count;
479 /* At this point, we should have enough entries in the cache
480 * for all the existing adds_in_progress. We should only be
481 * needing to allocate for regions_needed.
483 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
485 spin_unlock(&resv->lock);
486 for (i = 0; i < to_allocate; i++) {
487 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
490 list_add(&trg->link, &allocated_regions);
493 spin_lock(&resv->lock);
495 list_splice(&allocated_regions, &resv->region_cache);
496 resv->region_cache_count += to_allocate;
502 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
510 * Add the huge page range represented by [f, t) to the reserve
511 * map. Regions will be taken from the cache to fill in this range.
512 * Sufficient regions should exist in the cache due to the previous
513 * call to region_chg with the same range, but in some cases the cache will not
514 * have sufficient entries due to races with other code doing region_add or
515 * region_del. The extra needed entries will be allocated.
517 * regions_needed is the out value provided by a previous call to region_chg.
519 * Return the number of new huge pages added to the map. This number is greater
520 * than or equal to zero. If file_region entries needed to be allocated for
521 * this operation and we were not able to allocate, it returns -ENOMEM.
522 * region_add of regions of length 1 never allocate file_regions and cannot
523 * fail; region_chg will always allocate at least 1 entry and a region_add for
524 * 1 page will only require at most 1 entry.
526 static long region_add(struct resv_map *resv, long f, long t,
527 long in_regions_needed, struct hstate *h,
528 struct hugetlb_cgroup *h_cg)
530 long add = 0, actual_regions_needed = 0;
532 spin_lock(&resv->lock);
535 /* Count how many regions are actually needed to execute this add. */
536 add_reservation_in_range(resv, f, t, NULL, NULL,
537 &actual_regions_needed);
540 * Check for sufficient descriptors in the cache to accommodate
541 * this add operation. Note that actual_regions_needed may be greater
542 * than in_regions_needed, as the resv_map may have been modified since
543 * the region_chg call. In this case, we need to make sure that we
544 * allocate extra entries, such that we have enough for all the
545 * existing adds_in_progress, plus the excess needed for this
548 if (actual_regions_needed > in_regions_needed &&
549 resv->region_cache_count <
550 resv->adds_in_progress +
551 (actual_regions_needed - in_regions_needed)) {
552 /* region_add operation of range 1 should never need to
553 * allocate file_region entries.
555 VM_BUG_ON(t - f <= 1);
557 if (allocate_file_region_entries(
558 resv, actual_regions_needed - in_regions_needed)) {
565 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL);
567 resv->adds_in_progress -= in_regions_needed;
569 spin_unlock(&resv->lock);
574 * Examine the existing reserve map and determine how many
575 * huge pages in the specified range [f, t) are NOT currently
576 * represented. This routine is called before a subsequent
577 * call to region_add that will actually modify the reserve
578 * map to add the specified range [f, t). region_chg does
579 * not change the number of huge pages represented by the
580 * map. A number of new file_region structures is added to the cache as a
581 * placeholder, for the subsequent region_add call to use. At least 1
582 * file_region structure is added.
584 * out_regions_needed is the number of regions added to the
585 * resv->adds_in_progress. This value needs to be provided to a follow up call
586 * to region_add or region_abort for proper accounting.
588 * Returns the number of huge pages that need to be added to the existing
589 * reservation map for the range [f, t). This number is greater or equal to
590 * zero. -ENOMEM is returned if a new file_region structure or cache entry
591 * is needed and can not be allocated.
593 static long region_chg(struct resv_map *resv, long f, long t,
594 long *out_regions_needed)
598 spin_lock(&resv->lock);
600 /* Count how many hugepages in this range are NOT represented. */
601 chg = add_reservation_in_range(resv, f, t, NULL, NULL,
604 if (*out_regions_needed == 0)
605 *out_regions_needed = 1;
607 if (allocate_file_region_entries(resv, *out_regions_needed))
610 resv->adds_in_progress += *out_regions_needed;
612 spin_unlock(&resv->lock);
617 * Abort the in progress add operation. The adds_in_progress field
618 * of the resv_map keeps track of the operations in progress between
619 * calls to region_chg and region_add. Operations are sometimes
620 * aborted after the call to region_chg. In such cases, region_abort
621 * is called to decrement the adds_in_progress counter. regions_needed
622 * is the value returned by the region_chg call, it is used to decrement
623 * the adds_in_progress counter.
625 * NOTE: The range arguments [f, t) are not needed or used in this
626 * routine. They are kept to make reading the calling code easier as
627 * arguments will match the associated region_chg call.
629 static void region_abort(struct resv_map *resv, long f, long t,
632 spin_lock(&resv->lock);
633 VM_BUG_ON(!resv->region_cache_count);
634 resv->adds_in_progress -= regions_needed;
635 spin_unlock(&resv->lock);
639 * Delete the specified range [f, t) from the reserve map. If the
640 * t parameter is LONG_MAX, this indicates that ALL regions after f
641 * should be deleted. Locate the regions which intersect [f, t)
642 * and either trim, delete or split the existing regions.
644 * Returns the number of huge pages deleted from the reserve map.
645 * In the normal case, the return value is zero or more. In the
646 * case where a region must be split, a new region descriptor must
647 * be allocated. If the allocation fails, -ENOMEM will be returned.
648 * NOTE: If the parameter t == LONG_MAX, then we will never split
649 * a region and possibly return -ENOMEM. Callers specifying
650 * t == LONG_MAX do not need to check for -ENOMEM error.
652 static long region_del(struct resv_map *resv, long f, long t)
654 struct list_head *head = &resv->regions;
655 struct file_region *rg, *trg;
656 struct file_region *nrg = NULL;
660 spin_lock(&resv->lock);
661 list_for_each_entry_safe(rg, trg, head, link) {
663 * Skip regions before the range to be deleted. file_region
664 * ranges are normally of the form [from, to). However, there
665 * may be a "placeholder" entry in the map which is of the form
666 * (from, to) with from == to. Check for placeholder entries
667 * at the beginning of the range to be deleted.
669 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
675 if (f > rg->from && t < rg->to) { /* Must split region */
677 * Check for an entry in the cache before dropping
678 * lock and attempting allocation.
681 resv->region_cache_count > resv->adds_in_progress) {
682 nrg = list_first_entry(&resv->region_cache,
685 list_del(&nrg->link);
686 resv->region_cache_count--;
690 spin_unlock(&resv->lock);
691 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
698 hugetlb_cgroup_uncharge_file_region(
699 resv, rg, t - f, false);
701 /* New entry for end of split region */
705 copy_hugetlb_cgroup_uncharge_info(nrg, rg);
707 INIT_LIST_HEAD(&nrg->link);
709 /* Original entry is trimmed */
712 list_add(&nrg->link, &rg->link);
717 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
718 del += rg->to - rg->from;
719 hugetlb_cgroup_uncharge_file_region(resv, rg,
720 rg->to - rg->from, true);
726 if (f <= rg->from) { /* Trim beginning of region */
727 hugetlb_cgroup_uncharge_file_region(resv, rg,
728 t - rg->from, false);
732 } else { /* Trim end of region */
733 hugetlb_cgroup_uncharge_file_region(resv, rg,
741 spin_unlock(&resv->lock);
747 * A rare out of memory error was encountered which prevented removal of
748 * the reserve map region for a page. The huge page itself was free'ed
749 * and removed from the page cache. This routine will adjust the subpool
750 * usage count, and the global reserve count if needed. By incrementing
751 * these counts, the reserve map entry which could not be deleted will
752 * appear as a "reserved" entry instead of simply dangling with incorrect
755 void hugetlb_fix_reserve_counts(struct inode *inode)
757 struct hugepage_subpool *spool = subpool_inode(inode);
759 bool reserved = false;
761 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
762 if (rsv_adjust > 0) {
763 struct hstate *h = hstate_inode(inode);
765 if (!hugetlb_acct_memory(h, 1))
767 } else if (!rsv_adjust) {
772 pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
776 * Count and return the number of huge pages in the reserve map
777 * that intersect with the range [f, t).
779 static long region_count(struct resv_map *resv, long f, long t)
781 struct list_head *head = &resv->regions;
782 struct file_region *rg;
785 spin_lock(&resv->lock);
786 /* Locate each segment we overlap with, and count that overlap. */
787 list_for_each_entry(rg, head, link) {
796 seg_from = max(rg->from, f);
797 seg_to = min(rg->to, t);
799 chg += seg_to - seg_from;
801 spin_unlock(&resv->lock);
807 * Convert the address within this vma to the page offset within
808 * the mapping, in pagecache page units; huge pages here.
810 static pgoff_t vma_hugecache_offset(struct hstate *h,
811 struct vm_area_struct *vma, unsigned long address)
813 return ((address - vma->vm_start) >> huge_page_shift(h)) +
814 (vma->vm_pgoff >> huge_page_order(h));
817 pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
818 unsigned long address)
820 return vma_hugecache_offset(hstate_vma(vma), vma, address);
822 EXPORT_SYMBOL_GPL(linear_hugepage_index);
825 * Return the size of the pages allocated when backing a VMA. In the majority
826 * cases this will be same size as used by the page table entries.
828 unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
830 if (vma->vm_ops && vma->vm_ops->pagesize)
831 return vma->vm_ops->pagesize(vma);
834 EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
837 * Return the page size being used by the MMU to back a VMA. In the majority
838 * of cases, the page size used by the kernel matches the MMU size. On
839 * architectures where it differs, an architecture-specific 'strong'
840 * version of this symbol is required.
842 __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
844 return vma_kernel_pagesize(vma);
848 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
849 * bits of the reservation map pointer, which are always clear due to
852 #define HPAGE_RESV_OWNER (1UL << 0)
853 #define HPAGE_RESV_UNMAPPED (1UL << 1)
854 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
857 * These helpers are used to track how many pages are reserved for
858 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
859 * is guaranteed to have their future faults succeed.
861 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
862 * the reserve counters are updated with the hugetlb_lock held. It is safe
863 * to reset the VMA at fork() time as it is not in use yet and there is no
864 * chance of the global counters getting corrupted as a result of the values.
866 * The private mapping reservation is represented in a subtly different
867 * manner to a shared mapping. A shared mapping has a region map associated
868 * with the underlying file, this region map represents the backing file
869 * pages which have ever had a reservation assigned which this persists even
870 * after the page is instantiated. A private mapping has a region map
871 * associated with the original mmap which is attached to all VMAs which
872 * reference it, this region map represents those offsets which have consumed
873 * reservation ie. where pages have been instantiated.
875 static unsigned long get_vma_private_data(struct vm_area_struct *vma)
877 return (unsigned long)vma->vm_private_data;
880 static void set_vma_private_data(struct vm_area_struct *vma,
883 vma->vm_private_data = (void *)value;
887 resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
888 struct hugetlb_cgroup *h_cg,
891 #ifdef CONFIG_CGROUP_HUGETLB
893 resv_map->reservation_counter = NULL;
894 resv_map->pages_per_hpage = 0;
895 resv_map->css = NULL;
897 resv_map->reservation_counter =
898 &h_cg->rsvd_hugepage[hstate_index(h)];
899 resv_map->pages_per_hpage = pages_per_huge_page(h);
900 resv_map->css = &h_cg->css;
905 struct resv_map *resv_map_alloc(void)
907 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
908 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
910 if (!resv_map || !rg) {
916 kref_init(&resv_map->refs);
917 spin_lock_init(&resv_map->lock);
918 INIT_LIST_HEAD(&resv_map->regions);
920 resv_map->adds_in_progress = 0;
922 * Initialize these to 0. On shared mappings, 0's here indicate these
923 * fields don't do cgroup accounting. On private mappings, these will be
924 * re-initialized to the proper values, to indicate that hugetlb cgroup
925 * reservations are to be un-charged from here.
927 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
929 INIT_LIST_HEAD(&resv_map->region_cache);
930 list_add(&rg->link, &resv_map->region_cache);
931 resv_map->region_cache_count = 1;
936 void resv_map_release(struct kref *ref)
938 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
939 struct list_head *head = &resv_map->region_cache;
940 struct file_region *rg, *trg;
942 /* Clear out any active regions before we release the map. */
943 region_del(resv_map, 0, LONG_MAX);
945 /* ... and any entries left in the cache */
946 list_for_each_entry_safe(rg, trg, head, link) {
951 VM_BUG_ON(resv_map->adds_in_progress);
956 static inline struct resv_map *inode_resv_map(struct inode *inode)
959 * At inode evict time, i_mapping may not point to the original
960 * address space within the inode. This original address space
961 * contains the pointer to the resv_map. So, always use the
962 * address space embedded within the inode.
963 * The VERY common case is inode->mapping == &inode->i_data but,
964 * this may not be true for device special inodes.
966 return (struct resv_map *)(&inode->i_data)->private_data;
969 static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
971 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
972 if (vma->vm_flags & VM_MAYSHARE) {
973 struct address_space *mapping = vma->vm_file->f_mapping;
974 struct inode *inode = mapping->host;
976 return inode_resv_map(inode);
979 return (struct resv_map *)(get_vma_private_data(vma) &
984 static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
986 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
987 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
989 set_vma_private_data(vma, (get_vma_private_data(vma) &
990 HPAGE_RESV_MASK) | (unsigned long)map);
993 static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
995 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
996 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
998 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
1001 static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1003 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1005 return (get_vma_private_data(vma) & flag) != 0;
1008 /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
1009 void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
1011 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1012 if (!(vma->vm_flags & VM_MAYSHARE))
1013 vma->vm_private_data = (void *)0;
1017 * Reset and decrement one ref on hugepage private reservation.
1018 * Called with mm->mmap_sem writer semaphore held.
1019 * This function should be only used by move_vma() and operate on
1020 * same sized vma. It should never come here with last ref on the
1023 void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1026 * Clear the old hugetlb private page reservation.
1027 * It has already been transferred to new_vma.
1029 * During a mremap() operation of a hugetlb vma we call move_vma()
1030 * which copies vma into new_vma and unmaps vma. After the copy
1031 * operation both new_vma and vma share a reference to the resv_map
1032 * struct, and at that point vma is about to be unmapped. We don't
1033 * want to return the reservation to the pool at unmap of vma because
1034 * the reservation still lives on in new_vma, so simply decrement the
1035 * ref here and remove the resv_map reference from this vma.
1037 struct resv_map *reservations = vma_resv_map(vma);
1039 if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1040 resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
1041 kref_put(&reservations->refs, resv_map_release);
1044 reset_vma_resv_huge_pages(vma);
1047 /* Returns true if the VMA has associated reserve pages */
1048 static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
1050 if (vma->vm_flags & VM_NORESERVE) {
1052 * This address is already reserved by other process(chg == 0),
1053 * so, we should decrement reserved count. Without decrementing,
1054 * reserve count remains after releasing inode, because this
1055 * allocated page will go into page cache and is regarded as
1056 * coming from reserved pool in releasing step. Currently, we
1057 * don't have any other solution to deal with this situation
1058 * properly, so add work-around here.
1060 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
1066 /* Shared mappings always use reserves */
1067 if (vma->vm_flags & VM_MAYSHARE) {
1069 * We know VM_NORESERVE is not set. Therefore, there SHOULD
1070 * be a region map for all pages. The only situation where
1071 * there is no region map is if a hole was punched via
1072 * fallocate. In this case, there really are no reserves to
1073 * use. This situation is indicated if chg != 0.
1082 * Only the process that called mmap() has reserves for
1085 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1087 * Like the shared case above, a hole punch or truncate
1088 * could have been performed on the private mapping.
1089 * Examine the value of chg to determine if reserves
1090 * actually exist or were previously consumed.
1091 * Very Subtle - The value of chg comes from a previous
1092 * call to vma_needs_reserves(). The reserve map for
1093 * private mappings has different (opposite) semantics
1094 * than that of shared mappings. vma_needs_reserves()
1095 * has already taken this difference in semantics into
1096 * account. Therefore, the meaning of chg is the same
1097 * as in the shared case above. Code could easily be
1098 * combined, but keeping it separate draws attention to
1099 * subtle differences.
1110 static void enqueue_huge_page(struct hstate *h, struct page *page)
1112 int nid = page_to_nid(page);
1114 lockdep_assert_held(&hugetlb_lock);
1115 VM_BUG_ON_PAGE(page_count(page), page);
1117 list_move(&page->lru, &h->hugepage_freelists[nid]);
1118 h->free_huge_pages++;
1119 h->free_huge_pages_node[nid]++;
1120 SetHPageFreed(page);
1123 static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
1126 bool pin = !!(current->flags & PF_MEMALLOC_PIN);
1128 lockdep_assert_held(&hugetlb_lock);
1129 list_for_each_entry(page, &h->hugepage_freelists[nid], lru) {
1130 if (pin && !is_longterm_pinnable_page(page))
1133 if (PageHWPoison(page))
1136 list_move(&page->lru, &h->hugepage_activelist);
1137 set_page_refcounted(page);
1138 ClearHPageFreed(page);
1139 h->free_huge_pages--;
1140 h->free_huge_pages_node[nid]--;
1147 static struct page *dequeue_huge_page_nodemask(struct hstate *h, gfp_t gfp_mask, int nid,
1150 unsigned int cpuset_mems_cookie;
1151 struct zonelist *zonelist;
1154 int node = NUMA_NO_NODE;
1156 zonelist = node_zonelist(nid, gfp_mask);
1159 cpuset_mems_cookie = read_mems_allowed_begin();
1160 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
1163 if (!cpuset_zone_allowed(zone, gfp_mask))
1166 * no need to ask again on the same node. Pool is node rather than
1169 if (zone_to_nid(zone) == node)
1171 node = zone_to_nid(zone);
1173 page = dequeue_huge_page_node_exact(h, node);
1177 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1183 static struct page *dequeue_huge_page_vma(struct hstate *h,
1184 struct vm_area_struct *vma,
1185 unsigned long address, int avoid_reserve,
1188 struct page *page = NULL;
1189 struct mempolicy *mpol;
1191 nodemask_t *nodemask;
1195 * A child process with MAP_PRIVATE mappings created by their parent
1196 * have no page reserves. This check ensures that reservations are
1197 * not "stolen". The child may still get SIGKILLed
1199 if (!vma_has_reserves(vma, chg) &&
1200 h->free_huge_pages - h->resv_huge_pages == 0)
1203 /* If reserves cannot be used, ensure enough pages are in the pool */
1204 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
1207 gfp_mask = htlb_alloc_mask(h);
1208 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1210 if (mpol_is_preferred_many(mpol)) {
1211 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
1213 /* Fallback to all nodes if page==NULL */
1218 page = dequeue_huge_page_nodemask(h, gfp_mask, nid, nodemask);
1220 if (page && !avoid_reserve && vma_has_reserves(vma, chg)) {
1221 SetHPageRestoreReserve(page);
1222 h->resv_huge_pages--;
1225 mpol_cond_put(mpol);
1233 * common helper functions for hstate_next_node_to_{alloc|free}.
1234 * We may have allocated or freed a huge page based on a different
1235 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1236 * be outside of *nodes_allowed. Ensure that we use an allowed
1237 * node for alloc or free.
1239 static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1241 nid = next_node_in(nid, *nodes_allowed);
1242 VM_BUG_ON(nid >= MAX_NUMNODES);
1247 static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1249 if (!node_isset(nid, *nodes_allowed))
1250 nid = next_node_allowed(nid, nodes_allowed);
1255 * returns the previously saved node ["this node"] from which to
1256 * allocate a persistent huge page for the pool and advance the
1257 * next node from which to allocate, handling wrap at end of node
1260 static int hstate_next_node_to_alloc(struct hstate *h,
1261 nodemask_t *nodes_allowed)
1265 VM_BUG_ON(!nodes_allowed);
1267 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
1268 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
1274 * helper for remove_pool_huge_page() - return the previously saved
1275 * node ["this node"] from which to free a huge page. Advance the
1276 * next node id whether or not we find a free huge page to free so
1277 * that the next attempt to free addresses the next node.
1279 static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1283 VM_BUG_ON(!nodes_allowed);
1285 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1286 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1291 #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
1292 for (nr_nodes = nodes_weight(*mask); \
1294 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
1297 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1298 for (nr_nodes = nodes_weight(*mask); \
1300 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1303 /* used to demote non-gigantic_huge pages as well */
1304 static void __destroy_compound_gigantic_page(struct page *page,
1305 unsigned int order, bool demote)
1308 int nr_pages = 1 << order;
1309 struct page *p = page + 1;
1311 atomic_set(compound_mapcount_ptr(page), 0);
1312 atomic_set(compound_pincount_ptr(page), 0);
1314 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
1316 clear_compound_head(p);
1318 set_page_refcounted(p);
1321 set_compound_order(page, 0);
1323 page[1].compound_nr = 0;
1325 __ClearPageHead(page);
1328 static void destroy_compound_hugetlb_page_for_demote(struct page *page,
1331 __destroy_compound_gigantic_page(page, order, true);
1334 #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1335 static void destroy_compound_gigantic_page(struct page *page,
1338 __destroy_compound_gigantic_page(page, order, false);
1341 static void free_gigantic_page(struct page *page, unsigned int order)
1344 * If the page isn't allocated using the cma allocator,
1345 * cma_release() returns false.
1348 if (cma_release(hugetlb_cma[page_to_nid(page)], page, 1 << order))
1352 free_contig_range(page_to_pfn(page), 1 << order);
1355 #ifdef CONFIG_CONTIG_ALLOC
1356 static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1357 int nid, nodemask_t *nodemask)
1359 unsigned long nr_pages = pages_per_huge_page(h);
1360 if (nid == NUMA_NO_NODE)
1361 nid = numa_mem_id();
1368 if (hugetlb_cma[nid]) {
1369 page = cma_alloc(hugetlb_cma[nid], nr_pages,
1370 huge_page_order(h), true);
1375 if (!(gfp_mask & __GFP_THISNODE)) {
1376 for_each_node_mask(node, *nodemask) {
1377 if (node == nid || !hugetlb_cma[node])
1380 page = cma_alloc(hugetlb_cma[node], nr_pages,
1381 huge_page_order(h), true);
1389 return alloc_contig_pages(nr_pages, gfp_mask, nid, nodemask);
1392 #else /* !CONFIG_CONTIG_ALLOC */
1393 static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1394 int nid, nodemask_t *nodemask)
1398 #endif /* CONFIG_CONTIG_ALLOC */
1400 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1401 static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask,
1402 int nid, nodemask_t *nodemask)
1406 static inline void free_gigantic_page(struct page *page, unsigned int order) { }
1407 static inline void destroy_compound_gigantic_page(struct page *page,
1408 unsigned int order) { }
1412 * Remove hugetlb page from lists, and update dtor so that page appears
1413 * as just a compound page.
1415 * A reference is held on the page, except in the case of demote.
1417 * Must be called with hugetlb lock held.
1419 static void __remove_hugetlb_page(struct hstate *h, struct page *page,
1420 bool adjust_surplus,
1423 int nid = page_to_nid(page);
1425 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
1426 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page_rsvd(page), page);
1428 lockdep_assert_held(&hugetlb_lock);
1429 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1432 list_del(&page->lru);
1434 if (HPageFreed(page)) {
1435 h->free_huge_pages--;
1436 h->free_huge_pages_node[nid]--;
1438 if (adjust_surplus) {
1439 h->surplus_huge_pages--;
1440 h->surplus_huge_pages_node[nid]--;
1446 * For non-gigantic pages set the destructor to the normal compound
1447 * page dtor. This is needed in case someone takes an additional
1448 * temporary ref to the page, and freeing is delayed until they drop
1451 * For gigantic pages set the destructor to the null dtor. This
1452 * destructor will never be called. Before freeing the gigantic
1453 * page destroy_compound_gigantic_page will turn the compound page
1454 * into a simple group of pages. After this the destructor does not
1457 * This handles the case where more than one ref is held when and
1458 * after update_and_free_page is called.
1460 * In the case of demote we do not ref count the page as it will soon
1461 * be turned into a page of smaller size.
1464 set_page_refcounted(page);
1465 if (hstate_is_gigantic(h))
1466 set_compound_page_dtor(page, NULL_COMPOUND_DTOR);
1468 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
1471 h->nr_huge_pages_node[nid]--;
1474 static void remove_hugetlb_page(struct hstate *h, struct page *page,
1475 bool adjust_surplus)
1477 __remove_hugetlb_page(h, page, adjust_surplus, false);
1480 static void remove_hugetlb_page_for_demote(struct hstate *h, struct page *page,
1481 bool adjust_surplus)
1483 __remove_hugetlb_page(h, page, adjust_surplus, true);
1486 static void add_hugetlb_page(struct hstate *h, struct page *page,
1487 bool adjust_surplus)
1490 int nid = page_to_nid(page);
1492 VM_BUG_ON_PAGE(!HPageVmemmapOptimized(page), page);
1494 lockdep_assert_held(&hugetlb_lock);
1496 INIT_LIST_HEAD(&page->lru);
1498 h->nr_huge_pages_node[nid]++;
1500 if (adjust_surplus) {
1501 h->surplus_huge_pages++;
1502 h->surplus_huge_pages_node[nid]++;
1505 set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
1506 set_page_private(page, 0);
1508 * We have to set HPageVmemmapOptimized again as above
1509 * set_page_private(page, 0) cleared it.
1511 SetHPageVmemmapOptimized(page);
1514 * This page is about to be managed by the hugetlb allocator and
1515 * should have no users. Drop our reference, and check for others
1518 zeroed = put_page_testzero(page);
1521 * It is VERY unlikely soneone else has taken a ref on
1522 * the page. In this case, we simply return as the
1523 * hugetlb destructor (free_huge_page) will be called
1524 * when this other ref is dropped.
1528 arch_clear_hugepage_flags(page);
1529 enqueue_huge_page(h, page);
1532 static void __update_and_free_page(struct hstate *h, struct page *page)
1535 struct page *subpage = page;
1537 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1541 * If we don't know which subpages are hwpoisoned, we can't free
1542 * the hugepage, so it's leaked intentionally.
1544 if (HPageRawHwpUnreliable(page))
1547 if (hugetlb_vmemmap_restore(h, page)) {
1548 spin_lock_irq(&hugetlb_lock);
1550 * If we cannot allocate vmemmap pages, just refuse to free the
1551 * page and put the page back on the hugetlb free list and treat
1552 * as a surplus page.
1554 add_hugetlb_page(h, page, true);
1555 spin_unlock_irq(&hugetlb_lock);
1560 * Move PageHWPoison flag from head page to the raw error pages,
1561 * which makes any healthy subpages reusable.
1563 if (unlikely(PageHWPoison(page)))
1564 hugetlb_clear_page_hwpoison(page);
1566 for (i = 0; i < pages_per_huge_page(h);
1567 i++, subpage = mem_map_next(subpage, page, i)) {
1568 subpage->flags &= ~(1 << PG_locked | 1 << PG_error |
1569 1 << PG_referenced | 1 << PG_dirty |
1570 1 << PG_active | 1 << PG_private |
1575 * Non-gigantic pages demoted from CMA allocated gigantic pages
1576 * need to be given back to CMA in free_gigantic_page.
1578 if (hstate_is_gigantic(h) ||
1579 hugetlb_cma_page(page, huge_page_order(h))) {
1580 destroy_compound_gigantic_page(page, huge_page_order(h));
1581 free_gigantic_page(page, huge_page_order(h));
1583 __free_pages(page, huge_page_order(h));
1588 * As update_and_free_page() can be called under any context, so we cannot
1589 * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the
1590 * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate
1591 * the vmemmap pages.
1593 * free_hpage_workfn() locklessly retrieves the linked list of pages to be
1594 * freed and frees them one-by-one. As the page->mapping pointer is going
1595 * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node
1596 * structure of a lockless linked list of huge pages to be freed.
1598 static LLIST_HEAD(hpage_freelist);
1600 static void free_hpage_workfn(struct work_struct *work)
1602 struct llist_node *node;
1604 node = llist_del_all(&hpage_freelist);
1610 page = container_of((struct address_space **)node,
1611 struct page, mapping);
1613 page->mapping = NULL;
1615 * The VM_BUG_ON_PAGE(!PageHuge(page), page) in page_hstate()
1616 * is going to trigger because a previous call to
1617 * remove_hugetlb_page() will set_compound_page_dtor(page,
1618 * NULL_COMPOUND_DTOR), so do not use page_hstate() directly.
1620 h = size_to_hstate(page_size(page));
1622 __update_and_free_page(h, page);
1627 static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1629 static inline void flush_free_hpage_work(struct hstate *h)
1631 if (hugetlb_vmemmap_optimizable(h))
1632 flush_work(&free_hpage_work);
1635 static void update_and_free_page(struct hstate *h, struct page *page,
1638 if (!HPageVmemmapOptimized(page) || !atomic) {
1639 __update_and_free_page(h, page);
1644 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages.
1646 * Only call schedule_work() if hpage_freelist is previously
1647 * empty. Otherwise, schedule_work() had been called but the workfn
1648 * hasn't retrieved the list yet.
1650 if (llist_add((struct llist_node *)&page->mapping, &hpage_freelist))
1651 schedule_work(&free_hpage_work);
1654 static void update_and_free_pages_bulk(struct hstate *h, struct list_head *list)
1656 struct page *page, *t_page;
1658 list_for_each_entry_safe(page, t_page, list, lru) {
1659 update_and_free_page(h, page, false);
1664 struct hstate *size_to_hstate(unsigned long size)
1668 for_each_hstate(h) {
1669 if (huge_page_size(h) == size)
1675 void free_huge_page(struct page *page)
1678 * Can't pass hstate in here because it is called from the
1679 * compound page destructor.
1681 struct hstate *h = page_hstate(page);
1682 int nid = page_to_nid(page);
1683 struct hugepage_subpool *spool = hugetlb_page_subpool(page);
1684 bool restore_reserve;
1685 unsigned long flags;
1687 VM_BUG_ON_PAGE(page_count(page), page);
1688 VM_BUG_ON_PAGE(page_mapcount(page), page);
1690 hugetlb_set_page_subpool(page, NULL);
1692 __ClearPageAnonExclusive(page);
1693 page->mapping = NULL;
1694 restore_reserve = HPageRestoreReserve(page);
1695 ClearHPageRestoreReserve(page);
1698 * If HPageRestoreReserve was set on page, page allocation consumed a
1699 * reservation. If the page was associated with a subpool, there
1700 * would have been a page reserved in the subpool before allocation
1701 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
1702 * reservation, do not call hugepage_subpool_put_pages() as this will
1703 * remove the reserved page from the subpool.
1705 if (!restore_reserve) {
1707 * A return code of zero implies that the subpool will be
1708 * under its minimum size if the reservation is not restored
1709 * after page is free. Therefore, force restore_reserve
1712 if (hugepage_subpool_put_pages(spool, 1) == 0)
1713 restore_reserve = true;
1716 spin_lock_irqsave(&hugetlb_lock, flags);
1717 ClearHPageMigratable(page);
1718 hugetlb_cgroup_uncharge_page(hstate_index(h),
1719 pages_per_huge_page(h), page);
1720 hugetlb_cgroup_uncharge_page_rsvd(hstate_index(h),
1721 pages_per_huge_page(h), page);
1722 if (restore_reserve)
1723 h->resv_huge_pages++;
1725 if (HPageTemporary(page)) {
1726 remove_hugetlb_page(h, page, false);
1727 spin_unlock_irqrestore(&hugetlb_lock, flags);
1728 update_and_free_page(h, page, true);
1729 } else if (h->surplus_huge_pages_node[nid]) {
1730 /* remove the page from active list */
1731 remove_hugetlb_page(h, page, true);
1732 spin_unlock_irqrestore(&hugetlb_lock, flags);
1733 update_and_free_page(h, page, true);
1735 arch_clear_hugepage_flags(page);
1736 enqueue_huge_page(h, page);
1737 spin_unlock_irqrestore(&hugetlb_lock, flags);
1742 * Must be called with the hugetlb lock held
1744 static void __prep_account_new_huge_page(struct hstate *h, int nid)
1746 lockdep_assert_held(&hugetlb_lock);
1748 h->nr_huge_pages_node[nid]++;
1751 static void __prep_new_huge_page(struct hstate *h, struct page *page)
1753 hugetlb_vmemmap_optimize(h, page);
1754 INIT_LIST_HEAD(&page->lru);
1755 set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
1756 hugetlb_set_page_subpool(page, NULL);
1757 set_hugetlb_cgroup(page, NULL);
1758 set_hugetlb_cgroup_rsvd(page, NULL);
1761 static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
1763 __prep_new_huge_page(h, page);
1764 spin_lock_irq(&hugetlb_lock);
1765 __prep_account_new_huge_page(h, nid);
1766 spin_unlock_irq(&hugetlb_lock);
1769 static bool __prep_compound_gigantic_page(struct page *page, unsigned int order,
1773 int nr_pages = 1 << order;
1774 struct page *p = page + 1;
1776 /* we rely on prep_new_huge_page to set the destructor */
1777 set_compound_order(page, order);
1778 __ClearPageReserved(page);
1779 __SetPageHead(page);
1780 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
1782 * For gigantic hugepages allocated through bootmem at
1783 * boot, it's safer to be consistent with the not-gigantic
1784 * hugepages and clear the PG_reserved bit from all tail pages
1785 * too. Otherwise drivers using get_user_pages() to access tail
1786 * pages may get the reference counting wrong if they see
1787 * PG_reserved set on a tail page (despite the head page not
1788 * having PG_reserved set). Enforcing this consistency between
1789 * head and tail pages allows drivers to optimize away a check
1790 * on the head page when they need know if put_page() is needed
1791 * after get_user_pages().
1793 __ClearPageReserved(p);
1795 * Subtle and very unlikely
1797 * Gigantic 'page allocators' such as memblock or cma will
1798 * return a set of pages with each page ref counted. We need
1799 * to turn this set of pages into a compound page with tail
1800 * page ref counts set to zero. Code such as speculative page
1801 * cache adding could take a ref on a 'to be' tail page.
1802 * We need to respect any increased ref count, and only set
1803 * the ref count to zero if count is currently 1. If count
1804 * is not 1, we return an error. An error return indicates
1805 * the set of pages can not be converted to a gigantic page.
1806 * The caller who allocated the pages should then discard the
1807 * pages using the appropriate free interface.
1809 * In the case of demote, the ref count will be zero.
1812 if (!page_ref_freeze(p, 1)) {
1813 pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n");
1817 VM_BUG_ON_PAGE(page_count(p), p);
1819 set_compound_head(p, page);
1821 atomic_set(compound_mapcount_ptr(page), -1);
1822 atomic_set(compound_pincount_ptr(page), 0);
1826 /* undo tail page modifications made above */
1828 for (j = 1; j < i; j++, p = mem_map_next(p, page, j)) {
1829 clear_compound_head(p);
1830 set_page_refcounted(p);
1832 /* need to clear PG_reserved on remaining tail pages */
1833 for (; j < nr_pages; j++, p = mem_map_next(p, page, j))
1834 __ClearPageReserved(p);
1835 set_compound_order(page, 0);
1837 page[1].compound_nr = 0;
1839 __ClearPageHead(page);
1843 static bool prep_compound_gigantic_page(struct page *page, unsigned int order)
1845 return __prep_compound_gigantic_page(page, order, false);
1848 static bool prep_compound_gigantic_page_for_demote(struct page *page,
1851 return __prep_compound_gigantic_page(page, order, true);
1855 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
1856 * transparent huge pages. See the PageTransHuge() documentation for more
1859 int PageHuge(struct page *page)
1861 if (!PageCompound(page))
1864 page = compound_head(page);
1865 return page[1].compound_dtor == HUGETLB_PAGE_DTOR;
1867 EXPORT_SYMBOL_GPL(PageHuge);
1870 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
1871 * normal or transparent huge pages.
1873 int PageHeadHuge(struct page *page_head)
1875 if (!PageHead(page_head))
1878 return page_head[1].compound_dtor == HUGETLB_PAGE_DTOR;
1880 EXPORT_SYMBOL_GPL(PageHeadHuge);
1883 * Find and lock address space (mapping) in write mode.
1885 * Upon entry, the page is locked which means that page_mapping() is
1886 * stable. Due to locking order, we can only trylock_write. If we can
1887 * not get the lock, simply return NULL to caller.
1889 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage)
1891 struct address_space *mapping = page_mapping(hpage);
1896 if (i_mmap_trylock_write(mapping))
1902 pgoff_t hugetlb_basepage_index(struct page *page)
1904 struct page *page_head = compound_head(page);
1905 pgoff_t index = page_index(page_head);
1906 unsigned long compound_idx;
1908 if (compound_order(page_head) >= MAX_ORDER)
1909 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
1911 compound_idx = page - page_head;
1913 return (index << compound_order(page_head)) + compound_idx;
1916 static struct page *alloc_buddy_huge_page(struct hstate *h,
1917 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1918 nodemask_t *node_alloc_noretry)
1920 int order = huge_page_order(h);
1922 bool alloc_try_hard = true;
1925 * By default we always try hard to allocate the page with
1926 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating pages in
1927 * a loop (to adjust global huge page counts) and previous allocation
1928 * failed, do not continue to try hard on the same node. Use the
1929 * node_alloc_noretry bitmap to manage this state information.
1931 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
1932 alloc_try_hard = false;
1933 gfp_mask |= __GFP_COMP|__GFP_NOWARN;
1935 gfp_mask |= __GFP_RETRY_MAYFAIL;
1936 if (nid == NUMA_NO_NODE)
1937 nid = numa_mem_id();
1938 page = __alloc_pages(gfp_mask, order, nid, nmask);
1940 __count_vm_event(HTLB_BUDDY_PGALLOC);
1942 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
1945 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a page this
1946 * indicates an overall state change. Clear bit so that we resume
1947 * normal 'try hard' allocations.
1949 if (node_alloc_noretry && page && !alloc_try_hard)
1950 node_clear(nid, *node_alloc_noretry);
1953 * If we tried hard to get a page but failed, set bit so that
1954 * subsequent attempts will not try as hard until there is an
1955 * overall state change.
1957 if (node_alloc_noretry && !page && alloc_try_hard)
1958 node_set(nid, *node_alloc_noretry);
1964 * Common helper to allocate a fresh hugetlb page. All specific allocators
1965 * should use this function to get new hugetlb pages
1967 static struct page *alloc_fresh_huge_page(struct hstate *h,
1968 gfp_t gfp_mask, int nid, nodemask_t *nmask,
1969 nodemask_t *node_alloc_noretry)
1975 if (hstate_is_gigantic(h))
1976 page = alloc_gigantic_page(h, gfp_mask, nid, nmask);
1978 page = alloc_buddy_huge_page(h, gfp_mask,
1979 nid, nmask, node_alloc_noretry);
1983 if (hstate_is_gigantic(h)) {
1984 if (!prep_compound_gigantic_page(page, huge_page_order(h))) {
1986 * Rare failure to convert pages to compound page.
1987 * Free pages and try again - ONCE!
1989 free_gigantic_page(page, huge_page_order(h));
1997 prep_new_huge_page(h, page, page_to_nid(page));
2003 * Allocates a fresh page to the hugetlb allocator pool in the node interleaved
2006 static int alloc_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
2007 nodemask_t *node_alloc_noretry)
2011 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2013 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
2014 page = alloc_fresh_huge_page(h, gfp_mask, node, nodes_allowed,
2015 node_alloc_noretry);
2023 put_page(page); /* free it into the hugepage allocator */
2029 * Remove huge page from pool from next node to free. Attempt to keep
2030 * persistent huge pages more or less balanced over allowed nodes.
2031 * This routine only 'removes' the hugetlb page. The caller must make
2032 * an additional call to free the page to low level allocators.
2033 * Called with hugetlb_lock locked.
2035 static struct page *remove_pool_huge_page(struct hstate *h,
2036 nodemask_t *nodes_allowed,
2040 struct page *page = NULL;
2042 lockdep_assert_held(&hugetlb_lock);
2043 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2045 * If we're returning unused surplus pages, only examine
2046 * nodes with surplus pages.
2048 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
2049 !list_empty(&h->hugepage_freelists[node])) {
2050 page = list_entry(h->hugepage_freelists[node].next,
2052 remove_hugetlb_page(h, page, acct_surplus);
2061 * Dissolve a given free hugepage into free buddy pages. This function does
2062 * nothing for in-use hugepages and non-hugepages.
2063 * This function returns values like below:
2065 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2066 * when the system is under memory pressure and the feature of
2067 * freeing unused vmemmap pages associated with each hugetlb page
2069 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
2070 * (allocated or reserved.)
2071 * 0: successfully dissolved free hugepages or the page is not a
2072 * hugepage (considered as already dissolved)
2074 int dissolve_free_huge_page(struct page *page)
2079 /* Not to disrupt normal path by vainly holding hugetlb_lock */
2080 if (!PageHuge(page))
2083 spin_lock_irq(&hugetlb_lock);
2084 if (!PageHuge(page)) {
2089 if (!page_count(page)) {
2090 struct page *head = compound_head(page);
2091 struct hstate *h = page_hstate(head);
2092 if (h->free_huge_pages - h->resv_huge_pages == 0)
2096 * We should make sure that the page is already on the free list
2097 * when it is dissolved.
2099 if (unlikely(!HPageFreed(head))) {
2100 spin_unlock_irq(&hugetlb_lock);
2104 * Theoretically, we should return -EBUSY when we
2105 * encounter this race. In fact, we have a chance
2106 * to successfully dissolve the page if we do a
2107 * retry. Because the race window is quite small.
2108 * If we seize this opportunity, it is an optimization
2109 * for increasing the success rate of dissolving page.
2114 remove_hugetlb_page(h, head, false);
2115 h->max_huge_pages--;
2116 spin_unlock_irq(&hugetlb_lock);
2119 * Normally update_and_free_page will allocate required vmemmmap
2120 * before freeing the page. update_and_free_page will fail to
2121 * free the page if it can not allocate required vmemmap. We
2122 * need to adjust max_huge_pages if the page is not freed.
2123 * Attempt to allocate vmemmmap here so that we can take
2124 * appropriate action on failure.
2126 rc = hugetlb_vmemmap_restore(h, head);
2128 update_and_free_page(h, head, false);
2130 spin_lock_irq(&hugetlb_lock);
2131 add_hugetlb_page(h, head, false);
2132 h->max_huge_pages++;
2133 spin_unlock_irq(&hugetlb_lock);
2139 spin_unlock_irq(&hugetlb_lock);
2144 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
2145 * make specified memory blocks removable from the system.
2146 * Note that this will dissolve a free gigantic hugepage completely, if any
2147 * part of it lies within the given range.
2148 * Also note that if dissolve_free_huge_page() returns with an error, all
2149 * free hugepages that were dissolved before that error are lost.
2151 int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
2159 if (!hugepages_supported())
2162 order = huge_page_order(&default_hstate);
2164 order = min(order, huge_page_order(h));
2166 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) {
2167 page = pfn_to_page(pfn);
2168 rc = dissolve_free_huge_page(page);
2177 * Allocates a fresh surplus page from the page allocator.
2179 static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
2180 int nid, nodemask_t *nmask, bool zero_ref)
2182 struct page *page = NULL;
2185 if (hstate_is_gigantic(h))
2188 spin_lock_irq(&hugetlb_lock);
2189 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
2191 spin_unlock_irq(&hugetlb_lock);
2194 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
2198 spin_lock_irq(&hugetlb_lock);
2200 * We could have raced with the pool size change.
2201 * Double check that and simply deallocate the new page
2202 * if we would end up overcommiting the surpluses. Abuse
2203 * temporary page to workaround the nasty free_huge_page
2206 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
2207 SetHPageTemporary(page);
2208 spin_unlock_irq(&hugetlb_lock);
2215 * Caller requires a page with zero ref count.
2216 * We will drop ref count here. If someone else is holding
2217 * a ref, the page will be freed when they drop it. Abuse
2218 * temporary page flag to accomplish this.
2220 SetHPageTemporary(page);
2221 if (!put_page_testzero(page)) {
2223 * Unexpected inflated ref count on freshly allocated
2226 pr_info("HugeTLB unexpected inflated ref count on freshly allocated page\n");
2227 spin_unlock_irq(&hugetlb_lock);
2234 ClearHPageTemporary(page);
2237 h->surplus_huge_pages++;
2238 h->surplus_huge_pages_node[page_to_nid(page)]++;
2241 spin_unlock_irq(&hugetlb_lock);
2246 static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
2247 int nid, nodemask_t *nmask)
2251 if (hstate_is_gigantic(h))
2254 page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL);
2259 * We do not account these pages as surplus because they are only
2260 * temporary and will be released properly on the last reference
2262 SetHPageTemporary(page);
2268 * Use the VMA's mpolicy to allocate a huge page from the buddy.
2271 struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,
2272 struct vm_area_struct *vma, unsigned long addr)
2274 struct page *page = NULL;
2275 struct mempolicy *mpol;
2276 gfp_t gfp_mask = htlb_alloc_mask(h);
2278 nodemask_t *nodemask;
2280 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
2281 if (mpol_is_preferred_many(mpol)) {
2282 gfp_t gfp = gfp_mask | __GFP_NOWARN;
2284 gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
2285 page = alloc_surplus_huge_page(h, gfp, nid, nodemask, false);
2287 /* Fallback to all nodes if page==NULL */
2292 page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask, false);
2293 mpol_cond_put(mpol);
2297 /* page migration callback function */
2298 struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
2299 nodemask_t *nmask, gfp_t gfp_mask)
2301 spin_lock_irq(&hugetlb_lock);
2302 if (h->free_huge_pages - h->resv_huge_pages > 0) {
2305 page = dequeue_huge_page_nodemask(h, gfp_mask, preferred_nid, nmask);
2307 spin_unlock_irq(&hugetlb_lock);
2311 spin_unlock_irq(&hugetlb_lock);
2313 return alloc_migrate_huge_page(h, gfp_mask, preferred_nid, nmask);
2316 /* mempolicy aware migration callback */
2317 struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
2318 unsigned long address)
2320 struct mempolicy *mpol;
2321 nodemask_t *nodemask;
2326 gfp_mask = htlb_alloc_mask(h);
2327 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
2328 page = alloc_huge_page_nodemask(h, node, nodemask, gfp_mask);
2329 mpol_cond_put(mpol);
2335 * Increase the hugetlb pool such that it can accommodate a reservation
2338 static int gather_surplus_pages(struct hstate *h, long delta)
2339 __must_hold(&hugetlb_lock)
2341 LIST_HEAD(surplus_list);
2342 struct page *page, *tmp;
2345 long needed, allocated;
2346 bool alloc_ok = true;
2348 lockdep_assert_held(&hugetlb_lock);
2349 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
2351 h->resv_huge_pages += delta;
2359 spin_unlock_irq(&hugetlb_lock);
2360 for (i = 0; i < needed; i++) {
2361 page = alloc_surplus_huge_page(h, htlb_alloc_mask(h),
2362 NUMA_NO_NODE, NULL, true);
2367 list_add(&page->lru, &surplus_list);
2373 * After retaking hugetlb_lock, we need to recalculate 'needed'
2374 * because either resv_huge_pages or free_huge_pages may have changed.
2376 spin_lock_irq(&hugetlb_lock);
2377 needed = (h->resv_huge_pages + delta) -
2378 (h->free_huge_pages + allocated);
2383 * We were not able to allocate enough pages to
2384 * satisfy the entire reservation so we free what
2385 * we've allocated so far.
2390 * The surplus_list now contains _at_least_ the number of extra pages
2391 * needed to accommodate the reservation. Add the appropriate number
2392 * of pages to the hugetlb pool and free the extras back to the buddy
2393 * allocator. Commit the entire reservation here to prevent another
2394 * process from stealing the pages as they are added to the pool but
2395 * before they are reserved.
2397 needed += allocated;
2398 h->resv_huge_pages += delta;
2401 /* Free the needed pages to the hugetlb pool */
2402 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
2405 /* Add the page to the hugetlb allocator */
2406 enqueue_huge_page(h, page);
2409 spin_unlock_irq(&hugetlb_lock);
2412 * Free unnecessary surplus pages to the buddy allocator.
2413 * Pages have no ref count, call free_huge_page directly.
2415 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
2416 free_huge_page(page);
2417 spin_lock_irq(&hugetlb_lock);
2423 * This routine has two main purposes:
2424 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2425 * in unused_resv_pages. This corresponds to the prior adjustments made
2426 * to the associated reservation map.
2427 * 2) Free any unused surplus pages that may have been allocated to satisfy
2428 * the reservation. As many as unused_resv_pages may be freed.
2430 static void return_unused_surplus_pages(struct hstate *h,
2431 unsigned long unused_resv_pages)
2433 unsigned long nr_pages;
2435 LIST_HEAD(page_list);
2437 lockdep_assert_held(&hugetlb_lock);
2438 /* Uncommit the reservation */
2439 h->resv_huge_pages -= unused_resv_pages;
2441 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2445 * Part (or even all) of the reservation could have been backed
2446 * by pre-allocated pages. Only free surplus pages.
2448 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
2451 * We want to release as many surplus pages as possible, spread
2452 * evenly across all nodes with memory. Iterate across these nodes
2453 * until we can no longer free unreserved surplus pages. This occurs
2454 * when the nodes with surplus pages have no free pages.
2455 * remove_pool_huge_page() will balance the freed pages across the
2456 * on-line nodes with memory and will handle the hstate accounting.
2458 while (nr_pages--) {
2459 page = remove_pool_huge_page(h, &node_states[N_MEMORY], 1);
2463 list_add(&page->lru, &page_list);
2467 spin_unlock_irq(&hugetlb_lock);
2468 update_and_free_pages_bulk(h, &page_list);
2469 spin_lock_irq(&hugetlb_lock);
2474 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
2475 * are used by the huge page allocation routines to manage reservations.
2477 * vma_needs_reservation is called to determine if the huge page at addr
2478 * within the vma has an associated reservation. If a reservation is
2479 * needed, the value 1 is returned. The caller is then responsible for
2480 * managing the global reservation and subpool usage counts. After
2481 * the huge page has been allocated, vma_commit_reservation is called
2482 * to add the page to the reservation map. If the page allocation fails,
2483 * the reservation must be ended instead of committed. vma_end_reservation
2484 * is called in such cases.
2486 * In the normal case, vma_commit_reservation returns the same value
2487 * as the preceding vma_needs_reservation call. The only time this
2488 * is not the case is if a reserve map was changed between calls. It
2489 * is the responsibility of the caller to notice the difference and
2490 * take appropriate action.
2492 * vma_add_reservation is used in error paths where a reservation must
2493 * be restored when a newly allocated huge page must be freed. It is
2494 * to be called after calling vma_needs_reservation to determine if a
2495 * reservation exists.
2497 * vma_del_reservation is used in error paths where an entry in the reserve
2498 * map was created during huge page allocation and must be removed. It is to
2499 * be called after calling vma_needs_reservation to determine if a reservation
2502 enum vma_resv_mode {
2509 static long __vma_reservation_common(struct hstate *h,
2510 struct vm_area_struct *vma, unsigned long addr,
2511 enum vma_resv_mode mode)
2513 struct resv_map *resv;
2516 long dummy_out_regions_needed;
2518 resv = vma_resv_map(vma);
2522 idx = vma_hugecache_offset(h, vma, addr);
2524 case VMA_NEEDS_RESV:
2525 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2526 /* We assume that vma_reservation_* routines always operate on
2527 * 1 page, and that adding to resv map a 1 page entry can only
2528 * ever require 1 region.
2530 VM_BUG_ON(dummy_out_regions_needed != 1);
2532 case VMA_COMMIT_RESV:
2533 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2534 /* region_add calls of range 1 should never fail. */
2538 region_abort(resv, idx, idx + 1, 1);
2542 if (vma->vm_flags & VM_MAYSHARE) {
2543 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2544 /* region_add calls of range 1 should never fail. */
2547 region_abort(resv, idx, idx + 1, 1);
2548 ret = region_del(resv, idx, idx + 1);
2552 if (vma->vm_flags & VM_MAYSHARE) {
2553 region_abort(resv, idx, idx + 1, 1);
2554 ret = region_del(resv, idx, idx + 1);
2556 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2557 /* region_add calls of range 1 should never fail. */
2565 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV)
2568 * We know private mapping must have HPAGE_RESV_OWNER set.
2570 * In most cases, reserves always exist for private mappings.
2571 * However, a file associated with mapping could have been
2572 * hole punched or truncated after reserves were consumed.
2573 * As subsequent fault on such a range will not use reserves.
2574 * Subtle - The reserve map for private mappings has the
2575 * opposite meaning than that of shared mappings. If NO
2576 * entry is in the reserve map, it means a reservation exists.
2577 * If an entry exists in the reserve map, it means the
2578 * reservation has already been consumed. As a result, the
2579 * return value of this routine is the opposite of the
2580 * value returned from reserve map manipulation routines above.
2589 static long vma_needs_reservation(struct hstate *h,
2590 struct vm_area_struct *vma, unsigned long addr)
2592 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
2595 static long vma_commit_reservation(struct hstate *h,
2596 struct vm_area_struct *vma, unsigned long addr)
2598 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2601 static void vma_end_reservation(struct hstate *h,
2602 struct vm_area_struct *vma, unsigned long addr)
2604 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
2607 static long vma_add_reservation(struct hstate *h,
2608 struct vm_area_struct *vma, unsigned long addr)
2610 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2613 static long vma_del_reservation(struct hstate *h,
2614 struct vm_area_struct *vma, unsigned long addr)
2616 return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV);
2620 * This routine is called to restore reservation information on error paths.
2621 * It should ONLY be called for pages allocated via alloc_huge_page(), and
2622 * the hugetlb mutex should remain held when calling this routine.
2624 * It handles two specific cases:
2625 * 1) A reservation was in place and the page consumed the reservation.
2626 * HPageRestoreReserve is set in the page.
2627 * 2) No reservation was in place for the page, so HPageRestoreReserve is
2628 * not set. However, alloc_huge_page always updates the reserve map.
2630 * In case 1, free_huge_page later in the error path will increment the
2631 * global reserve count. But, free_huge_page does not have enough context
2632 * to adjust the reservation map. This case deals primarily with private
2633 * mappings. Adjust the reserve map here to be consistent with global
2634 * reserve count adjustments to be made by free_huge_page. Make sure the
2635 * reserve map indicates there is a reservation present.
2637 * In case 2, simply undo reserve map modifications done by alloc_huge_page.
2639 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
2640 unsigned long address, struct page *page)
2642 long rc = vma_needs_reservation(h, vma, address);
2644 if (HPageRestoreReserve(page)) {
2645 if (unlikely(rc < 0))
2647 * Rare out of memory condition in reserve map
2648 * manipulation. Clear HPageRestoreReserve so that
2649 * global reserve count will not be incremented
2650 * by free_huge_page. This will make it appear
2651 * as though the reservation for this page was
2652 * consumed. This may prevent the task from
2653 * faulting in the page at a later time. This
2654 * is better than inconsistent global huge page
2655 * accounting of reserve counts.
2657 ClearHPageRestoreReserve(page);
2659 (void)vma_add_reservation(h, vma, address);
2661 vma_end_reservation(h, vma, address);
2665 * This indicates there is an entry in the reserve map
2666 * not added by alloc_huge_page. We know it was added
2667 * before the alloc_huge_page call, otherwise
2668 * HPageRestoreReserve would be set on the page.
2669 * Remove the entry so that a subsequent allocation
2670 * does not consume a reservation.
2672 rc = vma_del_reservation(h, vma, address);
2675 * VERY rare out of memory condition. Since
2676 * we can not delete the entry, set
2677 * HPageRestoreReserve so that the reserve
2678 * count will be incremented when the page
2679 * is freed. This reserve will be consumed
2680 * on a subsequent allocation.
2682 SetHPageRestoreReserve(page);
2683 } else if (rc < 0) {
2685 * Rare out of memory condition from
2686 * vma_needs_reservation call. Memory allocation is
2687 * only attempted if a new entry is needed. Therefore,
2688 * this implies there is not an entry in the
2691 * For shared mappings, no entry in the map indicates
2692 * no reservation. We are done.
2694 if (!(vma->vm_flags & VM_MAYSHARE))
2696 * For private mappings, no entry indicates
2697 * a reservation is present. Since we can
2698 * not add an entry, set SetHPageRestoreReserve
2699 * on the page so reserve count will be
2700 * incremented when freed. This reserve will
2701 * be consumed on a subsequent allocation.
2703 SetHPageRestoreReserve(page);
2706 * No reservation present, do nothing
2708 vma_end_reservation(h, vma, address);
2713 * alloc_and_dissolve_huge_page - Allocate a new page and dissolve the old one
2714 * @h: struct hstate old page belongs to
2715 * @old_page: Old page to dissolve
2716 * @list: List to isolate the page in case we need to
2717 * Returns 0 on success, otherwise negated error.
2719 static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page,
2720 struct list_head *list)
2722 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2723 int nid = page_to_nid(old_page);
2724 bool alloc_retry = false;
2725 struct page *new_page;
2729 * Before dissolving the page, we need to allocate a new one for the
2730 * pool to remain stable. Here, we allocate the page and 'prep' it
2731 * by doing everything but actually updating counters and adding to
2732 * the pool. This simplifies and let us do most of the processing
2736 new_page = alloc_buddy_huge_page(h, gfp_mask, nid, NULL, NULL);
2740 * If all goes well, this page will be directly added to the free
2741 * list in the pool. For this the ref count needs to be zero.
2742 * Attempt to drop now, and retry once if needed. It is VERY
2743 * unlikely there is another ref on the page.
2745 * If someone else has a reference to the page, it will be freed
2746 * when they drop their ref. Abuse temporary page flag to accomplish
2747 * this. Retry once if there is an inflated ref count.
2749 SetHPageTemporary(new_page);
2750 if (!put_page_testzero(new_page)) {
2757 ClearHPageTemporary(new_page);
2759 __prep_new_huge_page(h, new_page);
2762 spin_lock_irq(&hugetlb_lock);
2763 if (!PageHuge(old_page)) {
2765 * Freed from under us. Drop new_page too.
2768 } else if (page_count(old_page)) {
2770 * Someone has grabbed the page, try to isolate it here.
2771 * Fail with -EBUSY if not possible.
2773 spin_unlock_irq(&hugetlb_lock);
2774 ret = isolate_hugetlb(old_page, list);
2775 spin_lock_irq(&hugetlb_lock);
2777 } else if (!HPageFreed(old_page)) {
2779 * Page's refcount is 0 but it has not been enqueued in the
2780 * freelist yet. Race window is small, so we can succeed here if
2783 spin_unlock_irq(&hugetlb_lock);
2788 * Ok, old_page is still a genuine free hugepage. Remove it from
2789 * the freelist and decrease the counters. These will be
2790 * incremented again when calling __prep_account_new_huge_page()
2791 * and enqueue_huge_page() for new_page. The counters will remain
2792 * stable since this happens under the lock.
2794 remove_hugetlb_page(h, old_page, false);
2797 * Ref count on new page is already zero as it was dropped
2798 * earlier. It can be directly added to the pool free list.
2800 __prep_account_new_huge_page(h, nid);
2801 enqueue_huge_page(h, new_page);
2804 * Pages have been replaced, we can safely free the old one.
2806 spin_unlock_irq(&hugetlb_lock);
2807 update_and_free_page(h, old_page, false);
2813 spin_unlock_irq(&hugetlb_lock);
2814 /* Page has a zero ref count, but needs a ref to be freed */
2815 set_page_refcounted(new_page);
2816 update_and_free_page(h, new_page, false);
2821 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list)
2828 * The page might have been dissolved from under our feet, so make sure
2829 * to carefully check the state under the lock.
2830 * Return success when racing as if we dissolved the page ourselves.
2832 spin_lock_irq(&hugetlb_lock);
2833 if (PageHuge(page)) {
2834 head = compound_head(page);
2835 h = page_hstate(head);
2837 spin_unlock_irq(&hugetlb_lock);
2840 spin_unlock_irq(&hugetlb_lock);
2843 * Fence off gigantic pages as there is a cyclic dependency between
2844 * alloc_contig_range and them. Return -ENOMEM as this has the effect
2845 * of bailing out right away without further retrying.
2847 if (hstate_is_gigantic(h))
2850 if (page_count(head) && !isolate_hugetlb(head, list))
2852 else if (!page_count(head))
2853 ret = alloc_and_dissolve_huge_page(h, head, list);
2858 struct page *alloc_huge_page(struct vm_area_struct *vma,
2859 unsigned long addr, int avoid_reserve)
2861 struct hugepage_subpool *spool = subpool_vma(vma);
2862 struct hstate *h = hstate_vma(vma);
2864 long map_chg, map_commit;
2867 struct hugetlb_cgroup *h_cg;
2868 bool deferred_reserve;
2870 idx = hstate_index(h);
2872 * Examine the region/reserve map to determine if the process
2873 * has a reservation for the page to be allocated. A return
2874 * code of zero indicates a reservation exists (no change).
2876 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
2878 return ERR_PTR(-ENOMEM);
2881 * Processes that did not create the mapping will have no
2882 * reserves as indicated by the region/reserve map. Check
2883 * that the allocation will not exceed the subpool limit.
2884 * Allocations for MAP_NORESERVE mappings also need to be
2885 * checked against any subpool limit.
2887 if (map_chg || avoid_reserve) {
2888 gbl_chg = hugepage_subpool_get_pages(spool, 1);
2890 vma_end_reservation(h, vma, addr);
2891 return ERR_PTR(-ENOSPC);
2895 * Even though there was no reservation in the region/reserve
2896 * map, there could be reservations associated with the
2897 * subpool that can be used. This would be indicated if the
2898 * return value of hugepage_subpool_get_pages() is zero.
2899 * However, if avoid_reserve is specified we still avoid even
2900 * the subpool reservations.
2906 /* If this allocation is not consuming a reservation, charge it now.
2908 deferred_reserve = map_chg || avoid_reserve;
2909 if (deferred_reserve) {
2910 ret = hugetlb_cgroup_charge_cgroup_rsvd(
2911 idx, pages_per_huge_page(h), &h_cg);
2913 goto out_subpool_put;
2916 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
2918 goto out_uncharge_cgroup_reservation;
2920 spin_lock_irq(&hugetlb_lock);
2922 * glb_chg is passed to indicate whether or not a page must be taken
2923 * from the global free pool (global change). gbl_chg == 0 indicates
2924 * a reservation exists for the allocation.
2926 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
2928 spin_unlock_irq(&hugetlb_lock);
2929 page = alloc_buddy_huge_page_with_mpol(h, vma, addr);
2931 goto out_uncharge_cgroup;
2932 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
2933 SetHPageRestoreReserve(page);
2934 h->resv_huge_pages--;
2936 spin_lock_irq(&hugetlb_lock);
2937 list_add(&page->lru, &h->hugepage_activelist);
2940 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
2941 /* If allocation is not consuming a reservation, also store the
2942 * hugetlb_cgroup pointer on the page.
2944 if (deferred_reserve) {
2945 hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h),
2949 spin_unlock_irq(&hugetlb_lock);
2951 hugetlb_set_page_subpool(page, spool);
2953 map_commit = vma_commit_reservation(h, vma, addr);
2954 if (unlikely(map_chg > map_commit)) {
2956 * The page was added to the reservation map between
2957 * vma_needs_reservation and vma_commit_reservation.
2958 * This indicates a race with hugetlb_reserve_pages.
2959 * Adjust for the subpool count incremented above AND
2960 * in hugetlb_reserve_pages for the same page. Also,
2961 * the reservation count added in hugetlb_reserve_pages
2962 * no longer applies.
2966 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
2967 hugetlb_acct_memory(h, -rsv_adjust);
2968 if (deferred_reserve)
2969 hugetlb_cgroup_uncharge_page_rsvd(hstate_index(h),
2970 pages_per_huge_page(h), page);
2974 out_uncharge_cgroup:
2975 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
2976 out_uncharge_cgroup_reservation:
2977 if (deferred_reserve)
2978 hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
2981 if (map_chg || avoid_reserve)
2982 hugepage_subpool_put_pages(spool, 1);
2983 vma_end_reservation(h, vma, addr);
2984 return ERR_PTR(-ENOSPC);
2987 int alloc_bootmem_huge_page(struct hstate *h, int nid)
2988 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
2989 int __alloc_bootmem_huge_page(struct hstate *h, int nid)
2991 struct huge_bootmem_page *m = NULL; /* initialize for clang */
2994 /* do node specific alloc */
2995 if (nid != NUMA_NO_NODE) {
2996 m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h),
2997 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3002 /* allocate from next node when distributing huge pages */
3003 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
3004 m = memblock_alloc_try_nid_raw(
3005 huge_page_size(h), huge_page_size(h),
3006 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
3008 * Use the beginning of the huge page to store the
3009 * huge_bootmem_page struct (until gather_bootmem
3010 * puts them into the mem_map).
3018 /* Put them into a private list first because mem_map is not up yet */
3019 INIT_LIST_HEAD(&m->list);
3020 list_add(&m->list, &huge_boot_pages);
3026 * Put bootmem huge pages into the standard lists after mem_map is up.
3027 * Note: This only applies to gigantic (order > MAX_ORDER) pages.
3029 static void __init gather_bootmem_prealloc(void)
3031 struct huge_bootmem_page *m;
3033 list_for_each_entry(m, &huge_boot_pages, list) {
3034 struct page *page = virt_to_page(m);
3035 struct hstate *h = m->hstate;
3037 VM_BUG_ON(!hstate_is_gigantic(h));
3038 WARN_ON(page_count(page) != 1);
3039 if (prep_compound_gigantic_page(page, huge_page_order(h))) {
3040 WARN_ON(PageReserved(page));
3041 prep_new_huge_page(h, page, page_to_nid(page));
3042 put_page(page); /* add to the hugepage allocator */
3044 /* VERY unlikely inflated ref count on a tail page */
3045 free_gigantic_page(page, huge_page_order(h));
3049 * We need to restore the 'stolen' pages to totalram_pages
3050 * in order to fix confusing memory reports from free(1) and
3051 * other side-effects, like CommitLimit going negative.
3053 adjust_managed_page_count(page, pages_per_huge_page(h));
3057 static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
3062 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) {
3063 if (hstate_is_gigantic(h)) {
3064 if (!alloc_bootmem_huge_page(h, nid))
3068 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
3070 page = alloc_fresh_huge_page(h, gfp_mask, nid,
3071 &node_states[N_MEMORY], NULL);
3074 put_page(page); /* free it into the hugepage allocator */
3078 if (i == h->max_huge_pages_node[nid])
3081 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3082 pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n",
3083 h->max_huge_pages_node[nid], buf, nid, i);
3084 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
3085 h->max_huge_pages_node[nid] = i;
3088 static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
3091 nodemask_t *node_alloc_noretry;
3092 bool node_specific_alloc = false;
3094 /* skip gigantic hugepages allocation if hugetlb_cma enabled */
3095 if (hstate_is_gigantic(h) && hugetlb_cma_size) {
3096 pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
3100 /* do node specific alloc */
3101 for_each_online_node(i) {
3102 if (h->max_huge_pages_node[i] > 0) {
3103 hugetlb_hstate_alloc_pages_onenode(h, i);
3104 node_specific_alloc = true;
3108 if (node_specific_alloc)
3111 /* below will do all node balanced alloc */
3112 if (!hstate_is_gigantic(h)) {
3114 * Bit mask controlling how hard we retry per-node allocations.
3115 * Ignore errors as lower level routines can deal with
3116 * node_alloc_noretry == NULL. If this kmalloc fails at boot
3117 * time, we are likely in bigger trouble.
3119 node_alloc_noretry = kmalloc(sizeof(*node_alloc_noretry),
3122 /* allocations done at boot time */
3123 node_alloc_noretry = NULL;
3126 /* bit mask controlling how hard we retry per-node allocations */
3127 if (node_alloc_noretry)
3128 nodes_clear(*node_alloc_noretry);
3130 for (i = 0; i < h->max_huge_pages; ++i) {
3131 if (hstate_is_gigantic(h)) {
3132 if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE))
3134 } else if (!alloc_pool_huge_page(h,
3135 &node_states[N_MEMORY],
3136 node_alloc_noretry))
3140 if (i < h->max_huge_pages) {
3143 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3144 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
3145 h->max_huge_pages, buf, i);
3146 h->max_huge_pages = i;
3148 kfree(node_alloc_noretry);
3151 static void __init hugetlb_init_hstates(void)
3153 struct hstate *h, *h2;
3155 for_each_hstate(h) {
3156 /* oversize hugepages were init'ed in early boot */
3157 if (!hstate_is_gigantic(h))
3158 hugetlb_hstate_alloc_pages(h);
3161 * Set demote order for each hstate. Note that
3162 * h->demote_order is initially 0.
3163 * - We can not demote gigantic pages if runtime freeing
3164 * is not supported, so skip this.
3165 * - If CMA allocation is possible, we can not demote
3166 * HUGETLB_PAGE_ORDER or smaller size pages.
3168 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3170 if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER)
3172 for_each_hstate(h2) {
3175 if (h2->order < h->order &&
3176 h2->order > h->demote_order)
3177 h->demote_order = h2->order;
3182 static void __init report_hugepages(void)
3186 for_each_hstate(h) {
3189 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3190 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n",
3191 buf, h->free_huge_pages);
3192 pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
3193 hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf);
3197 #ifdef CONFIG_HIGHMEM
3198 static void try_to_free_low(struct hstate *h, unsigned long count,
3199 nodemask_t *nodes_allowed)
3202 LIST_HEAD(page_list);
3204 lockdep_assert_held(&hugetlb_lock);
3205 if (hstate_is_gigantic(h))
3209 * Collect pages to be freed on a list, and free after dropping lock
3211 for_each_node_mask(i, *nodes_allowed) {
3212 struct page *page, *next;
3213 struct list_head *freel = &h->hugepage_freelists[i];
3214 list_for_each_entry_safe(page, next, freel, lru) {
3215 if (count >= h->nr_huge_pages)
3217 if (PageHighMem(page))
3219 remove_hugetlb_page(h, page, false);
3220 list_add(&page->lru, &page_list);
3225 spin_unlock_irq(&hugetlb_lock);
3226 update_and_free_pages_bulk(h, &page_list);
3227 spin_lock_irq(&hugetlb_lock);
3230 static inline void try_to_free_low(struct hstate *h, unsigned long count,
3231 nodemask_t *nodes_allowed)
3237 * Increment or decrement surplus_huge_pages. Keep node-specific counters
3238 * balanced by operating on them in a round-robin fashion.
3239 * Returns 1 if an adjustment was made.
3241 static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
3246 lockdep_assert_held(&hugetlb_lock);
3247 VM_BUG_ON(delta != -1 && delta != 1);
3250 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
3251 if (h->surplus_huge_pages_node[node])
3255 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3256 if (h->surplus_huge_pages_node[node] <
3257 h->nr_huge_pages_node[node])
3264 h->surplus_huge_pages += delta;
3265 h->surplus_huge_pages_node[node] += delta;
3269 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
3270 static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
3271 nodemask_t *nodes_allowed)
3273 unsigned long min_count, ret;
3275 LIST_HEAD(page_list);
3276 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
3279 * Bit mask controlling how hard we retry per-node allocations.
3280 * If we can not allocate the bit mask, do not attempt to allocate
3281 * the requested huge pages.
3283 if (node_alloc_noretry)
3284 nodes_clear(*node_alloc_noretry);
3289 * resize_lock mutex prevents concurrent adjustments to number of
3290 * pages in hstate via the proc/sysfs interfaces.
3292 mutex_lock(&h->resize_lock);
3293 flush_free_hpage_work(h);
3294 spin_lock_irq(&hugetlb_lock);
3297 * Check for a node specific request.
3298 * Changing node specific huge page count may require a corresponding
3299 * change to the global count. In any case, the passed node mask
3300 * (nodes_allowed) will restrict alloc/free to the specified node.
3302 if (nid != NUMA_NO_NODE) {
3303 unsigned long old_count = count;
3305 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
3307 * User may have specified a large count value which caused the
3308 * above calculation to overflow. In this case, they wanted
3309 * to allocate as many huge pages as possible. Set count to
3310 * largest possible value to align with their intention.
3312 if (count < old_count)
3317 * Gigantic pages runtime allocation depend on the capability for large
3318 * page range allocation.
3319 * If the system does not provide this feature, return an error when
3320 * the user tries to allocate gigantic pages but let the user free the
3321 * boottime allocated gigantic pages.
3323 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
3324 if (count > persistent_huge_pages(h)) {
3325 spin_unlock_irq(&hugetlb_lock);
3326 mutex_unlock(&h->resize_lock);
3327 NODEMASK_FREE(node_alloc_noretry);
3330 /* Fall through to decrease pool */
3334 * Increase the pool size
3335 * First take pages out of surplus state. Then make up the
3336 * remaining difference by allocating fresh huge pages.
3338 * We might race with alloc_surplus_huge_page() here and be unable
3339 * to convert a surplus huge page to a normal huge page. That is
3340 * not critical, though, it just means the overall size of the
3341 * pool might be one hugepage larger than it needs to be, but
3342 * within all the constraints specified by the sysctls.
3344 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
3345 if (!adjust_pool_surplus(h, nodes_allowed, -1))
3349 while (count > persistent_huge_pages(h)) {
3351 * If this allocation races such that we no longer need the
3352 * page, free_huge_page will handle it by freeing the page
3353 * and reducing the surplus.
3355 spin_unlock_irq(&hugetlb_lock);
3357 /* yield cpu to avoid soft lockup */
3360 ret = alloc_pool_huge_page(h, nodes_allowed,
3361 node_alloc_noretry);
3362 spin_lock_irq(&hugetlb_lock);
3366 /* Bail for signals. Probably ctrl-c from user */
3367 if (signal_pending(current))
3372 * Decrease the pool size
3373 * First return free pages to the buddy allocator (being careful
3374 * to keep enough around to satisfy reservations). Then place
3375 * pages into surplus state as needed so the pool will shrink
3376 * to the desired size as pages become free.
3378 * By placing pages into the surplus state independent of the
3379 * overcommit value, we are allowing the surplus pool size to
3380 * exceed overcommit. There are few sane options here. Since
3381 * alloc_surplus_huge_page() is checking the global counter,
3382 * though, we'll note that we're not allowed to exceed surplus
3383 * and won't grow the pool anywhere else. Not until one of the
3384 * sysctls are changed, or the surplus pages go out of use.
3386 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
3387 min_count = max(count, min_count);
3388 try_to_free_low(h, min_count, nodes_allowed);
3391 * Collect pages to be removed on list without dropping lock
3393 while (min_count < persistent_huge_pages(h)) {
3394 page = remove_pool_huge_page(h, nodes_allowed, 0);
3398 list_add(&page->lru, &page_list);
3400 /* free the pages after dropping lock */
3401 spin_unlock_irq(&hugetlb_lock);
3402 update_and_free_pages_bulk(h, &page_list);
3403 flush_free_hpage_work(h);
3404 spin_lock_irq(&hugetlb_lock);
3406 while (count < persistent_huge_pages(h)) {
3407 if (!adjust_pool_surplus(h, nodes_allowed, 1))
3411 h->max_huge_pages = persistent_huge_pages(h);
3412 spin_unlock_irq(&hugetlb_lock);
3413 mutex_unlock(&h->resize_lock);
3415 NODEMASK_FREE(node_alloc_noretry);
3420 static int demote_free_huge_page(struct hstate *h, struct page *page)
3422 int i, nid = page_to_nid(page);
3423 struct hstate *target_hstate;
3424 struct page *subpage;
3427 target_hstate = size_to_hstate(PAGE_SIZE << h->demote_order);
3429 remove_hugetlb_page_for_demote(h, page, false);
3430 spin_unlock_irq(&hugetlb_lock);
3432 rc = hugetlb_vmemmap_restore(h, page);
3434 /* Allocation of vmemmmap failed, we can not demote page */
3435 spin_lock_irq(&hugetlb_lock);
3436 set_page_refcounted(page);
3437 add_hugetlb_page(h, page, false);
3442 * Use destroy_compound_hugetlb_page_for_demote for all huge page
3443 * sizes as it will not ref count pages.
3445 destroy_compound_hugetlb_page_for_demote(page, huge_page_order(h));
3448 * Taking target hstate mutex synchronizes with set_max_huge_pages.
3449 * Without the mutex, pages added to target hstate could be marked
3452 * Note that we already hold h->resize_lock. To prevent deadlock,
3453 * use the convention of always taking larger size hstate mutex first.
3455 mutex_lock(&target_hstate->resize_lock);
3456 for (i = 0; i < pages_per_huge_page(h);
3457 i += pages_per_huge_page(target_hstate)) {
3458 subpage = nth_page(page, i);
3459 if (hstate_is_gigantic(target_hstate))
3460 prep_compound_gigantic_page_for_demote(subpage,
3461 target_hstate->order);
3463 prep_compound_page(subpage, target_hstate->order);
3464 set_page_private(subpage, 0);
3465 set_page_refcounted(subpage);
3466 prep_new_huge_page(target_hstate, subpage, nid);
3469 mutex_unlock(&target_hstate->resize_lock);
3471 spin_lock_irq(&hugetlb_lock);
3474 * Not absolutely necessary, but for consistency update max_huge_pages
3475 * based on pool changes for the demoted page.
3477 h->max_huge_pages--;
3478 target_hstate->max_huge_pages +=
3479 pages_per_huge_page(h) / pages_per_huge_page(target_hstate);
3484 static int demote_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
3485 __must_hold(&hugetlb_lock)
3490 lockdep_assert_held(&hugetlb_lock);
3492 /* We should never get here if no demote order */
3493 if (!h->demote_order) {
3494 pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n");
3495 return -EINVAL; /* internal error */
3498 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3499 list_for_each_entry(page, &h->hugepage_freelists[node], lru) {
3500 if (PageHWPoison(page))
3503 return demote_free_huge_page(h, page);
3508 * Only way to get here is if all pages on free lists are poisoned.
3509 * Return -EBUSY so that caller will not retry.
3514 #define HSTATE_ATTR_RO(_name) \
3515 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
3517 #define HSTATE_ATTR_WO(_name) \
3518 static struct kobj_attribute _name##_attr = __ATTR_WO(_name)
3520 #define HSTATE_ATTR(_name) \
3521 static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
3523 static struct kobject *hugepages_kobj;
3524 static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
3526 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
3528 static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
3532 for (i = 0; i < HUGE_MAX_HSTATE; i++)
3533 if (hstate_kobjs[i] == kobj) {
3535 *nidp = NUMA_NO_NODE;
3539 return kobj_to_node_hstate(kobj, nidp);
3542 static ssize_t nr_hugepages_show_common(struct kobject *kobj,
3543 struct kobj_attribute *attr, char *buf)
3546 unsigned long nr_huge_pages;
3549 h = kobj_to_hstate(kobj, &nid);
3550 if (nid == NUMA_NO_NODE)
3551 nr_huge_pages = h->nr_huge_pages;
3553 nr_huge_pages = h->nr_huge_pages_node[nid];
3555 return sysfs_emit(buf, "%lu\n", nr_huge_pages);
3558 static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
3559 struct hstate *h, int nid,
3560 unsigned long count, size_t len)
3563 nodemask_t nodes_allowed, *n_mask;
3565 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3568 if (nid == NUMA_NO_NODE) {
3570 * global hstate attribute
3572 if (!(obey_mempolicy &&
3573 init_nodemask_of_mempolicy(&nodes_allowed)))
3574 n_mask = &node_states[N_MEMORY];
3576 n_mask = &nodes_allowed;
3579 * Node specific request. count adjustment happens in
3580 * set_max_huge_pages() after acquiring hugetlb_lock.
3582 init_nodemask_of_node(&nodes_allowed, nid);
3583 n_mask = &nodes_allowed;
3586 err = set_max_huge_pages(h, count, nid, n_mask);
3588 return err ? err : len;
3591 static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
3592 struct kobject *kobj, const char *buf,
3596 unsigned long count;
3600 err = kstrtoul(buf, 10, &count);
3604 h = kobj_to_hstate(kobj, &nid);
3605 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
3608 static ssize_t nr_hugepages_show(struct kobject *kobj,
3609 struct kobj_attribute *attr, char *buf)
3611 return nr_hugepages_show_common(kobj, attr, buf);
3614 static ssize_t nr_hugepages_store(struct kobject *kobj,
3615 struct kobj_attribute *attr, const char *buf, size_t len)
3617 return nr_hugepages_store_common(false, kobj, buf, len);
3619 HSTATE_ATTR(nr_hugepages);
3624 * hstate attribute for optionally mempolicy-based constraint on persistent
3625 * huge page alloc/free.
3627 static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
3628 struct kobj_attribute *attr,
3631 return nr_hugepages_show_common(kobj, attr, buf);
3634 static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
3635 struct kobj_attribute *attr, const char *buf, size_t len)
3637 return nr_hugepages_store_common(true, kobj, buf, len);
3639 HSTATE_ATTR(nr_hugepages_mempolicy);
3643 static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
3644 struct kobj_attribute *attr, char *buf)
3646 struct hstate *h = kobj_to_hstate(kobj, NULL);
3647 return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages);
3650 static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
3651 struct kobj_attribute *attr, const char *buf, size_t count)
3654 unsigned long input;
3655 struct hstate *h = kobj_to_hstate(kobj, NULL);
3657 if (hstate_is_gigantic(h))
3660 err = kstrtoul(buf, 10, &input);
3664 spin_lock_irq(&hugetlb_lock);
3665 h->nr_overcommit_huge_pages = input;
3666 spin_unlock_irq(&hugetlb_lock);
3670 HSTATE_ATTR(nr_overcommit_hugepages);
3672 static ssize_t free_hugepages_show(struct kobject *kobj,
3673 struct kobj_attribute *attr, char *buf)
3676 unsigned long free_huge_pages;
3679 h = kobj_to_hstate(kobj, &nid);
3680 if (nid == NUMA_NO_NODE)
3681 free_huge_pages = h->free_huge_pages;
3683 free_huge_pages = h->free_huge_pages_node[nid];
3685 return sysfs_emit(buf, "%lu\n", free_huge_pages);
3687 HSTATE_ATTR_RO(free_hugepages);
3689 static ssize_t resv_hugepages_show(struct kobject *kobj,
3690 struct kobj_attribute *attr, char *buf)
3692 struct hstate *h = kobj_to_hstate(kobj, NULL);
3693 return sysfs_emit(buf, "%lu\n", h->resv_huge_pages);
3695 HSTATE_ATTR_RO(resv_hugepages);
3697 static ssize_t surplus_hugepages_show(struct kobject *kobj,
3698 struct kobj_attribute *attr, char *buf)
3701 unsigned long surplus_huge_pages;
3704 h = kobj_to_hstate(kobj, &nid);
3705 if (nid == NUMA_NO_NODE)
3706 surplus_huge_pages = h->surplus_huge_pages;
3708 surplus_huge_pages = h->surplus_huge_pages_node[nid];
3710 return sysfs_emit(buf, "%lu\n", surplus_huge_pages);
3712 HSTATE_ATTR_RO(surplus_hugepages);
3714 static ssize_t demote_store(struct kobject *kobj,
3715 struct kobj_attribute *attr, const char *buf, size_t len)
3717 unsigned long nr_demote;
3718 unsigned long nr_available;
3719 nodemask_t nodes_allowed, *n_mask;
3724 err = kstrtoul(buf, 10, &nr_demote);
3727 h = kobj_to_hstate(kobj, &nid);
3729 if (nid != NUMA_NO_NODE) {
3730 init_nodemask_of_node(&nodes_allowed, nid);
3731 n_mask = &nodes_allowed;
3733 n_mask = &node_states[N_MEMORY];
3736 /* Synchronize with other sysfs operations modifying huge pages */
3737 mutex_lock(&h->resize_lock);
3738 spin_lock_irq(&hugetlb_lock);
3742 * Check for available pages to demote each time thorough the
3743 * loop as demote_pool_huge_page will drop hugetlb_lock.
3745 if (nid != NUMA_NO_NODE)
3746 nr_available = h->free_huge_pages_node[nid];
3748 nr_available = h->free_huge_pages;
3749 nr_available -= h->resv_huge_pages;
3753 err = demote_pool_huge_page(h, n_mask);
3760 spin_unlock_irq(&hugetlb_lock);
3761 mutex_unlock(&h->resize_lock);
3767 HSTATE_ATTR_WO(demote);
3769 static ssize_t demote_size_show(struct kobject *kobj,
3770 struct kobj_attribute *attr, char *buf)
3772 struct hstate *h = kobj_to_hstate(kobj, NULL);
3773 unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
3775 return sysfs_emit(buf, "%lukB\n", demote_size);
3778 static ssize_t demote_size_store(struct kobject *kobj,
3779 struct kobj_attribute *attr,
3780 const char *buf, size_t count)
3782 struct hstate *h, *demote_hstate;
3783 unsigned long demote_size;
3784 unsigned int demote_order;
3786 demote_size = (unsigned long)memparse(buf, NULL);
3788 demote_hstate = size_to_hstate(demote_size);
3791 demote_order = demote_hstate->order;
3792 if (demote_order < HUGETLB_PAGE_ORDER)
3795 /* demote order must be smaller than hstate order */
3796 h = kobj_to_hstate(kobj, NULL);
3797 if (demote_order >= h->order)
3800 /* resize_lock synchronizes access to demote size and writes */
3801 mutex_lock(&h->resize_lock);
3802 h->demote_order = demote_order;
3803 mutex_unlock(&h->resize_lock);
3807 HSTATE_ATTR(demote_size);
3809 static struct attribute *hstate_attrs[] = {
3810 &nr_hugepages_attr.attr,
3811 &nr_overcommit_hugepages_attr.attr,
3812 &free_hugepages_attr.attr,
3813 &resv_hugepages_attr.attr,
3814 &surplus_hugepages_attr.attr,
3816 &nr_hugepages_mempolicy_attr.attr,
3821 static const struct attribute_group hstate_attr_group = {
3822 .attrs = hstate_attrs,
3825 static struct attribute *hstate_demote_attrs[] = {
3826 &demote_size_attr.attr,
3831 static const struct attribute_group hstate_demote_attr_group = {
3832 .attrs = hstate_demote_attrs,
3835 static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
3836 struct kobject **hstate_kobjs,
3837 const struct attribute_group *hstate_attr_group)
3840 int hi = hstate_index(h);
3842 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
3843 if (!hstate_kobjs[hi])
3846 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
3848 kobject_put(hstate_kobjs[hi]);
3849 hstate_kobjs[hi] = NULL;
3853 if (h->demote_order) {
3854 retval = sysfs_create_group(hstate_kobjs[hi],
3855 &hstate_demote_attr_group);
3857 pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name);
3858 sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group);
3859 kobject_put(hstate_kobjs[hi]);
3860 hstate_kobjs[hi] = NULL;
3868 static void __init hugetlb_sysfs_init(void)
3873 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
3874 if (!hugepages_kobj)
3877 for_each_hstate(h) {
3878 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
3879 hstate_kobjs, &hstate_attr_group);
3881 pr_err("HugeTLB: Unable to add hstate %s", h->name);
3888 * node_hstate/s - associate per node hstate attributes, via their kobjects,
3889 * with node devices in node_devices[] using a parallel array. The array
3890 * index of a node device or _hstate == node id.
3891 * This is here to avoid any static dependency of the node device driver, in
3892 * the base kernel, on the hugetlb module.
3894 struct node_hstate {
3895 struct kobject *hugepages_kobj;
3896 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
3898 static struct node_hstate node_hstates[MAX_NUMNODES];
3901 * A subset of global hstate attributes for node devices
3903 static struct attribute *per_node_hstate_attrs[] = {
3904 &nr_hugepages_attr.attr,
3905 &free_hugepages_attr.attr,
3906 &surplus_hugepages_attr.attr,
3910 static const struct attribute_group per_node_hstate_attr_group = {
3911 .attrs = per_node_hstate_attrs,
3915 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
3916 * Returns node id via non-NULL nidp.
3918 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
3922 for (nid = 0; nid < nr_node_ids; nid++) {
3923 struct node_hstate *nhs = &node_hstates[nid];
3925 for (i = 0; i < HUGE_MAX_HSTATE; i++)
3926 if (nhs->hstate_kobjs[i] == kobj) {
3938 * Unregister hstate attributes from a single node device.
3939 * No-op if no hstate attributes attached.
3941 static void hugetlb_unregister_node(struct node *node)
3944 struct node_hstate *nhs = &node_hstates[node->dev.id];
3946 if (!nhs->hugepages_kobj)
3947 return; /* no hstate attributes */
3949 for_each_hstate(h) {
3950 int idx = hstate_index(h);
3951 struct kobject *hstate_kobj = nhs->hstate_kobjs[idx];
3955 if (h->demote_order)
3956 sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group);
3957 sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group);
3958 kobject_put(hstate_kobj);
3959 nhs->hstate_kobjs[idx] = NULL;
3962 kobject_put(nhs->hugepages_kobj);
3963 nhs->hugepages_kobj = NULL;
3968 * Register hstate attributes for a single node device.
3969 * No-op if attributes already registered.
3971 static void hugetlb_register_node(struct node *node)
3974 struct node_hstate *nhs = &node_hstates[node->dev.id];
3977 if (nhs->hugepages_kobj)
3978 return; /* already allocated */
3980 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
3982 if (!nhs->hugepages_kobj)
3985 for_each_hstate(h) {
3986 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
3988 &per_node_hstate_attr_group);
3990 pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
3991 h->name, node->dev.id);
3992 hugetlb_unregister_node(node);
3999 * hugetlb init time: register hstate attributes for all registered node
4000 * devices of nodes that have memory. All on-line nodes should have
4001 * registered their associated device by this time.
4003 static void __init hugetlb_register_all_nodes(void)
4007 for_each_node_state(nid, N_MEMORY) {
4008 struct node *node = node_devices[nid];
4009 if (node->dev.id == nid)
4010 hugetlb_register_node(node);
4014 * Let the node device driver know we're here so it can
4015 * [un]register hstate attributes on node hotplug.
4017 register_hugetlbfs_with_node(hugetlb_register_node,
4018 hugetlb_unregister_node);
4020 #else /* !CONFIG_NUMA */
4022 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4030 static void hugetlb_register_all_nodes(void) { }
4035 static void __init hugetlb_cma_check(void);
4037 static inline __init void hugetlb_cma_check(void)
4042 static int __init hugetlb_init(void)
4046 BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE <
4049 if (!hugepages_supported()) {
4050 if (hugetlb_max_hstate || default_hstate_max_huge_pages)
4051 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
4056 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some
4057 * architectures depend on setup being done here.
4059 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
4060 if (!parsed_default_hugepagesz) {
4062 * If we did not parse a default huge page size, set
4063 * default_hstate_idx to HPAGE_SIZE hstate. And, if the
4064 * number of huge pages for this default size was implicitly
4065 * specified, set that here as well.
4066 * Note that the implicit setting will overwrite an explicit
4067 * setting. A warning will be printed in this case.
4069 default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
4070 if (default_hstate_max_huge_pages) {
4071 if (default_hstate.max_huge_pages) {
4074 string_get_size(huge_page_size(&default_hstate),
4075 1, STRING_UNITS_2, buf, 32);
4076 pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
4077 default_hstate.max_huge_pages, buf);
4078 pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
4079 default_hstate_max_huge_pages);
4081 default_hstate.max_huge_pages =
4082 default_hstate_max_huge_pages;
4084 for_each_online_node(i)
4085 default_hstate.max_huge_pages_node[i] =
4086 default_hugepages_in_node[i];
4090 hugetlb_cma_check();
4091 hugetlb_init_hstates();
4092 gather_bootmem_prealloc();
4095 hugetlb_sysfs_init();
4096 hugetlb_register_all_nodes();
4097 hugetlb_cgroup_file_init();
4100 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
4102 num_fault_mutexes = 1;
4104 hugetlb_fault_mutex_table =
4105 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
4107 BUG_ON(!hugetlb_fault_mutex_table);
4109 for (i = 0; i < num_fault_mutexes; i++)
4110 mutex_init(&hugetlb_fault_mutex_table[i]);
4113 subsys_initcall(hugetlb_init);
4115 /* Overwritten by architectures with more huge page sizes */
4116 bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
4118 return size == HPAGE_SIZE;
4121 void __init hugetlb_add_hstate(unsigned int order)
4126 if (size_to_hstate(PAGE_SIZE << order)) {
4129 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
4131 h = &hstates[hugetlb_max_hstate++];
4132 mutex_init(&h->resize_lock);
4134 h->mask = ~(huge_page_size(h) - 1);
4135 for (i = 0; i < MAX_NUMNODES; ++i)
4136 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
4137 INIT_LIST_HEAD(&h->hugepage_activelist);
4138 h->next_nid_to_alloc = first_memory_node;
4139 h->next_nid_to_free = first_memory_node;
4140 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
4141 huge_page_size(h)/SZ_1K);
4146 bool __init __weak hugetlb_node_alloc_supported(void)
4151 static void __init hugepages_clear_pages_in_node(void)
4153 if (!hugetlb_max_hstate) {
4154 default_hstate_max_huge_pages = 0;
4155 memset(default_hugepages_in_node, 0,
4156 sizeof(default_hugepages_in_node));
4158 parsed_hstate->max_huge_pages = 0;
4159 memset(parsed_hstate->max_huge_pages_node, 0,
4160 sizeof(parsed_hstate->max_huge_pages_node));
4165 * hugepages command line processing
4166 * hugepages normally follows a valid hugepagsz or default_hugepagsz
4167 * specification. If not, ignore the hugepages value. hugepages can also
4168 * be the first huge page command line option in which case it implicitly
4169 * specifies the number of huge pages for the default size.
4171 static int __init hugepages_setup(char *s)
4174 static unsigned long *last_mhp;
4175 int node = NUMA_NO_NODE;
4180 if (!parsed_valid_hugepagesz) {
4181 pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
4182 parsed_valid_hugepagesz = true;
4187 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
4188 * yet, so this hugepages= parameter goes to the "default hstate".
4189 * Otherwise, it goes with the previously parsed hugepagesz or
4190 * default_hugepagesz.
4192 else if (!hugetlb_max_hstate)
4193 mhp = &default_hstate_max_huge_pages;
4195 mhp = &parsed_hstate->max_huge_pages;
4197 if (mhp == last_mhp) {
4198 pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
4204 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4206 /* Parameter is node format */
4207 if (p[count] == ':') {
4208 if (!hugetlb_node_alloc_supported()) {
4209 pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n");
4212 if (tmp >= MAX_NUMNODES || !node_online(tmp))
4214 node = array_index_nospec(tmp, MAX_NUMNODES);
4216 /* Parse hugepages */
4217 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4219 if (!hugetlb_max_hstate)
4220 default_hugepages_in_node[node] = tmp;
4222 parsed_hstate->max_huge_pages_node[node] = tmp;
4224 /* Go to parse next node*/
4225 if (p[count] == ',')
4238 * Global state is always initialized later in hugetlb_init.
4239 * But we need to allocate gigantic hstates here early to still
4240 * use the bootmem allocator.
4242 if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate))
4243 hugetlb_hstate_alloc_pages(parsed_hstate);
4250 pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
4251 hugepages_clear_pages_in_node();
4254 __setup("hugepages=", hugepages_setup);
4257 * hugepagesz command line processing
4258 * A specific huge page size can only be specified once with hugepagesz.
4259 * hugepagesz is followed by hugepages on the command line. The global
4260 * variable 'parsed_valid_hugepagesz' is used to determine if prior
4261 * hugepagesz argument was valid.
4263 static int __init hugepagesz_setup(char *s)
4268 parsed_valid_hugepagesz = false;
4269 size = (unsigned long)memparse(s, NULL);
4271 if (!arch_hugetlb_valid_size(size)) {
4272 pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
4276 h = size_to_hstate(size);
4279 * hstate for this size already exists. This is normally
4280 * an error, but is allowed if the existing hstate is the
4281 * default hstate. More specifically, it is only allowed if
4282 * the number of huge pages for the default hstate was not
4283 * previously specified.
4285 if (!parsed_default_hugepagesz || h != &default_hstate ||
4286 default_hstate.max_huge_pages) {
4287 pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
4292 * No need to call hugetlb_add_hstate() as hstate already
4293 * exists. But, do set parsed_hstate so that a following
4294 * hugepages= parameter will be applied to this hstate.
4297 parsed_valid_hugepagesz = true;
4301 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4302 parsed_valid_hugepagesz = true;
4305 __setup("hugepagesz=", hugepagesz_setup);
4308 * default_hugepagesz command line input
4309 * Only one instance of default_hugepagesz allowed on command line.
4311 static int __init default_hugepagesz_setup(char *s)
4316 parsed_valid_hugepagesz = false;
4317 if (parsed_default_hugepagesz) {
4318 pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
4322 size = (unsigned long)memparse(s, NULL);
4324 if (!arch_hugetlb_valid_size(size)) {
4325 pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
4329 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4330 parsed_valid_hugepagesz = true;
4331 parsed_default_hugepagesz = true;
4332 default_hstate_idx = hstate_index(size_to_hstate(size));
4335 * The number of default huge pages (for this size) could have been
4336 * specified as the first hugetlb parameter: hugepages=X. If so,
4337 * then default_hstate_max_huge_pages is set. If the default huge
4338 * page size is gigantic (>= MAX_ORDER), then the pages must be
4339 * allocated here from bootmem allocator.
4341 if (default_hstate_max_huge_pages) {
4342 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
4343 for_each_online_node(i)
4344 default_hstate.max_huge_pages_node[i] =
4345 default_hugepages_in_node[i];
4346 if (hstate_is_gigantic(&default_hstate))
4347 hugetlb_hstate_alloc_pages(&default_hstate);
4348 default_hstate_max_huge_pages = 0;
4353 __setup("default_hugepagesz=", default_hugepagesz_setup);
4355 static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
4358 struct mempolicy *mpol = get_task_policy(current);
4361 * Only enforce MPOL_BIND policy which overlaps with cpuset policy
4362 * (from policy_nodemask) specifically for hugetlb case
4364 if (mpol->mode == MPOL_BIND &&
4365 (apply_policy_zone(mpol, gfp_zone(gfp)) &&
4366 cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
4367 return &mpol->nodes;
4372 static unsigned int allowed_mems_nr(struct hstate *h)
4375 unsigned int nr = 0;
4376 nodemask_t *mbind_nodemask;
4377 unsigned int *array = h->free_huge_pages_node;
4378 gfp_t gfp_mask = htlb_alloc_mask(h);
4380 mbind_nodemask = policy_mbind_nodemask(gfp_mask);
4381 for_each_node_mask(node, cpuset_current_mems_allowed) {
4382 if (!mbind_nodemask || node_isset(node, *mbind_nodemask))
4389 #ifdef CONFIG_SYSCTL
4390 static int proc_hugetlb_doulongvec_minmax(struct ctl_table *table, int write,
4391 void *buffer, size_t *length,
4392 loff_t *ppos, unsigned long *out)
4394 struct ctl_table dup_table;
4397 * In order to avoid races with __do_proc_doulongvec_minmax(), we
4398 * can duplicate the @table and alter the duplicate of it.
4401 dup_table.data = out;
4403 return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos);
4406 static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
4407 struct ctl_table *table, int write,
4408 void *buffer, size_t *length, loff_t *ppos)
4410 struct hstate *h = &default_hstate;
4411 unsigned long tmp = h->max_huge_pages;
4414 if (!hugepages_supported())
4417 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4423 ret = __nr_hugepages_store_common(obey_mempolicy, h,
4424 NUMA_NO_NODE, tmp, *length);
4429 int hugetlb_sysctl_handler(struct ctl_table *table, int write,
4430 void *buffer, size_t *length, loff_t *ppos)
4433 return hugetlb_sysctl_handler_common(false, table, write,
4434 buffer, length, ppos);
4438 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
4439 void *buffer, size_t *length, loff_t *ppos)
4441 return hugetlb_sysctl_handler_common(true, table, write,
4442 buffer, length, ppos);
4444 #endif /* CONFIG_NUMA */
4446 int hugetlb_overcommit_handler(struct ctl_table *table, int write,
4447 void *buffer, size_t *length, loff_t *ppos)
4449 struct hstate *h = &default_hstate;
4453 if (!hugepages_supported())
4456 tmp = h->nr_overcommit_huge_pages;
4458 if (write && hstate_is_gigantic(h))
4461 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4467 spin_lock_irq(&hugetlb_lock);
4468 h->nr_overcommit_huge_pages = tmp;
4469 spin_unlock_irq(&hugetlb_lock);
4475 #endif /* CONFIG_SYSCTL */
4477 void hugetlb_report_meminfo(struct seq_file *m)
4480 unsigned long total = 0;
4482 if (!hugepages_supported())
4485 for_each_hstate(h) {
4486 unsigned long count = h->nr_huge_pages;
4488 total += huge_page_size(h) * count;
4490 if (h == &default_hstate)
4492 "HugePages_Total: %5lu\n"
4493 "HugePages_Free: %5lu\n"
4494 "HugePages_Rsvd: %5lu\n"
4495 "HugePages_Surp: %5lu\n"
4496 "Hugepagesize: %8lu kB\n",
4500 h->surplus_huge_pages,
4501 huge_page_size(h) / SZ_1K);
4504 seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K);
4507 int hugetlb_report_node_meminfo(char *buf, int len, int nid)
4509 struct hstate *h = &default_hstate;
4511 if (!hugepages_supported())
4514 return sysfs_emit_at(buf, len,
4515 "Node %d HugePages_Total: %5u\n"
4516 "Node %d HugePages_Free: %5u\n"
4517 "Node %d HugePages_Surp: %5u\n",
4518 nid, h->nr_huge_pages_node[nid],
4519 nid, h->free_huge_pages_node[nid],
4520 nid, h->surplus_huge_pages_node[nid]);
4523 void hugetlb_show_meminfo_node(int nid)
4527 if (!hugepages_supported())
4531 printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
4533 h->nr_huge_pages_node[nid],
4534 h->free_huge_pages_node[nid],
4535 h->surplus_huge_pages_node[nid],
4536 huge_page_size(h) / SZ_1K);
4539 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
4541 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
4542 atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
4545 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
4546 unsigned long hugetlb_total_pages(void)
4549 unsigned long nr_total_pages = 0;
4552 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
4553 return nr_total_pages;
4556 static int hugetlb_acct_memory(struct hstate *h, long delta)
4563 spin_lock_irq(&hugetlb_lock);
4565 * When cpuset is configured, it breaks the strict hugetlb page
4566 * reservation as the accounting is done on a global variable. Such
4567 * reservation is completely rubbish in the presence of cpuset because
4568 * the reservation is not checked against page availability for the
4569 * current cpuset. Application can still potentially OOM'ed by kernel
4570 * with lack of free htlb page in cpuset that the task is in.
4571 * Attempt to enforce strict accounting with cpuset is almost
4572 * impossible (or too ugly) because cpuset is too fluid that
4573 * task or memory node can be dynamically moved between cpusets.
4575 * The change of semantics for shared hugetlb mapping with cpuset is
4576 * undesirable. However, in order to preserve some of the semantics,
4577 * we fall back to check against current free page availability as
4578 * a best attempt and hopefully to minimize the impact of changing
4579 * semantics that cpuset has.
4581 * Apart from cpuset, we also have memory policy mechanism that
4582 * also determines from which node the kernel will allocate memory
4583 * in a NUMA system. So similar to cpuset, we also should consider
4584 * the memory policy of the current task. Similar to the description
4588 if (gather_surplus_pages(h, delta) < 0)
4591 if (delta > allowed_mems_nr(h)) {
4592 return_unused_surplus_pages(h, delta);
4599 return_unused_surplus_pages(h, (unsigned long) -delta);
4602 spin_unlock_irq(&hugetlb_lock);
4606 static void hugetlb_vm_op_open(struct vm_area_struct *vma)
4608 struct resv_map *resv = vma_resv_map(vma);
4611 * This new VMA should share its siblings reservation map if present.
4612 * The VMA will only ever have a valid reservation map pointer where
4613 * it is being copied for another still existing VMA. As that VMA
4614 * has a reference to the reservation map it cannot disappear until
4615 * after this open call completes. It is therefore safe to take a
4616 * new reference here without additional locking.
4618 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
4619 resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
4620 kref_get(&resv->refs);
4624 static void hugetlb_vm_op_close(struct vm_area_struct *vma)
4626 struct hstate *h = hstate_vma(vma);
4627 struct resv_map *resv = vma_resv_map(vma);
4628 struct hugepage_subpool *spool = subpool_vma(vma);
4629 unsigned long reserve, start, end;
4632 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
4635 start = vma_hugecache_offset(h, vma, vma->vm_start);
4636 end = vma_hugecache_offset(h, vma, vma->vm_end);
4638 reserve = (end - start) - region_count(resv, start, end);
4639 hugetlb_cgroup_uncharge_counter(resv, start, end);
4642 * Decrement reserve counts. The global reserve count may be
4643 * adjusted if the subpool has a minimum size.
4645 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
4646 hugetlb_acct_memory(h, -gbl_reserve);
4649 kref_put(&resv->refs, resv_map_release);
4652 static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
4654 if (addr & ~(huge_page_mask(hstate_vma(vma))))
4659 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
4661 return huge_page_size(hstate_vma(vma));
4665 * We cannot handle pagefaults against hugetlb pages at all. They cause
4666 * handle_mm_fault() to try to instantiate regular-sized pages in the
4667 * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get
4670 static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
4677 * When a new function is introduced to vm_operations_struct and added
4678 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
4679 * This is because under System V memory model, mappings created via
4680 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
4681 * their original vm_ops are overwritten with shm_vm_ops.
4683 const struct vm_operations_struct hugetlb_vm_ops = {
4684 .fault = hugetlb_vm_op_fault,
4685 .open = hugetlb_vm_op_open,
4686 .close = hugetlb_vm_op_close,
4687 .may_split = hugetlb_vm_op_split,
4688 .pagesize = hugetlb_vm_op_pagesize,
4691 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
4695 unsigned int shift = huge_page_shift(hstate_vma(vma));
4698 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
4699 vma->vm_page_prot)));
4701 entry = huge_pte_wrprotect(mk_huge_pte(page,
4702 vma->vm_page_prot));
4704 entry = pte_mkyoung(entry);
4705 entry = arch_make_huge_pte(entry, shift, vma->vm_flags);
4710 static void set_huge_ptep_writable(struct vm_area_struct *vma,
4711 unsigned long address, pte_t *ptep)
4715 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
4716 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
4717 update_mmu_cache(vma, address, ptep);
4720 bool is_hugetlb_entry_migration(pte_t pte)
4724 if (huge_pte_none(pte) || pte_present(pte))
4726 swp = pte_to_swp_entry(pte);
4727 if (is_migration_entry(swp))
4733 static bool is_hugetlb_entry_hwpoisoned(pte_t pte)
4737 if (huge_pte_none(pte) || pte_present(pte))
4739 swp = pte_to_swp_entry(pte);
4740 if (is_hwpoison_entry(swp))
4747 hugetlb_install_page(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr,
4748 struct page *new_page)
4750 __SetPageUptodate(new_page);
4751 hugepage_add_new_anon_rmap(new_page, vma, addr);
4752 set_huge_pte_at(vma->vm_mm, addr, ptep, make_huge_pte(vma, new_page, 1));
4753 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm);
4754 ClearHPageRestoreReserve(new_page);
4755 SetHPageMigratable(new_page);
4758 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
4759 struct vm_area_struct *dst_vma,
4760 struct vm_area_struct *src_vma)
4762 pte_t *src_pte, *dst_pte, entry;
4763 struct page *ptepage;
4765 bool cow = is_cow_mapping(src_vma->vm_flags);
4766 struct hstate *h = hstate_vma(src_vma);
4767 unsigned long sz = huge_page_size(h);
4768 unsigned long npages = pages_per_huge_page(h);
4769 struct address_space *mapping = src_vma->vm_file->f_mapping;
4770 struct mmu_notifier_range range;
4771 unsigned long last_addr_mask;
4775 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src_vma, src,
4778 mmu_notifier_invalidate_range_start(&range);
4779 mmap_assert_write_locked(src);
4780 raw_write_seqcount_begin(&src->write_protect_seq);
4783 * For shared mappings i_mmap_rwsem must be held to call
4784 * huge_pte_alloc, otherwise the returned ptep could go
4785 * away if part of a shared pmd and another thread calls
4788 i_mmap_lock_read(mapping);
4791 last_addr_mask = hugetlb_mask_last_page(h);
4792 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
4793 spinlock_t *src_ptl, *dst_ptl;
4794 src_pte = huge_pte_offset(src, addr, sz);
4796 addr |= last_addr_mask;
4799 dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz);
4806 * If the pagetables are shared don't copy or take references.
4808 * dst_pte == src_pte is the common case of src/dest sharing.
4809 * However, src could have 'unshared' and dst shares with
4810 * another vma. So page_count of ptep page is checked instead
4811 * to reliably determine whether pte is shared.
4813 if (page_count(virt_to_page(dst_pte)) > 1) {
4814 addr |= last_addr_mask;
4818 dst_ptl = huge_pte_lock(h, dst, dst_pte);
4819 src_ptl = huge_pte_lockptr(h, src, src_pte);
4820 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
4821 entry = huge_ptep_get(src_pte);
4823 if (huge_pte_none(entry)) {
4825 * Skip if src entry none.
4828 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
4829 bool uffd_wp = huge_pte_uffd_wp(entry);
4831 if (!userfaultfd_wp(dst_vma) && uffd_wp)
4832 entry = huge_pte_clear_uffd_wp(entry);
4833 set_huge_pte_at(dst, addr, dst_pte, entry);
4834 } else if (unlikely(is_hugetlb_entry_migration(entry))) {
4835 swp_entry_t swp_entry = pte_to_swp_entry(entry);
4836 bool uffd_wp = huge_pte_uffd_wp(entry);
4838 if (!is_readable_migration_entry(swp_entry) && cow) {
4840 * COW mappings require pages in both
4841 * parent and child to be set to read.
4843 swp_entry = make_readable_migration_entry(
4844 swp_offset(swp_entry));
4845 entry = swp_entry_to_pte(swp_entry);
4846 if (userfaultfd_wp(src_vma) && uffd_wp)
4847 entry = huge_pte_mkuffd_wp(entry);
4848 set_huge_pte_at(src, addr, src_pte, entry);
4850 if (!userfaultfd_wp(dst_vma) && uffd_wp)
4851 entry = huge_pte_clear_uffd_wp(entry);
4852 set_huge_pte_at(dst, addr, dst_pte, entry);
4853 } else if (unlikely(is_pte_marker(entry))) {
4855 * We copy the pte marker only if the dst vma has
4858 if (userfaultfd_wp(dst_vma))
4859 set_huge_pte_at(dst, addr, dst_pte, entry);
4861 entry = huge_ptep_get(src_pte);
4862 ptepage = pte_page(entry);
4866 * Failing to duplicate the anon rmap is a rare case
4867 * where we see pinned hugetlb pages while they're
4868 * prone to COW. We need to do the COW earlier during
4871 * When pre-allocating the page or copying data, we
4872 * need to be without the pgtable locks since we could
4873 * sleep during the process.
4875 if (!PageAnon(ptepage)) {
4876 page_dup_file_rmap(ptepage, true);
4877 } else if (page_try_dup_anon_rmap(ptepage, true,
4879 pte_t src_pte_old = entry;
4882 spin_unlock(src_ptl);
4883 spin_unlock(dst_ptl);
4884 /* Do not use reserve as it's private owned */
4885 new = alloc_huge_page(dst_vma, addr, 1);
4891 copy_user_huge_page(new, ptepage, addr, dst_vma,
4895 /* Install the new huge page if src pte stable */
4896 dst_ptl = huge_pte_lock(h, dst, dst_pte);
4897 src_ptl = huge_pte_lockptr(h, src, src_pte);
4898 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
4899 entry = huge_ptep_get(src_pte);
4900 if (!pte_same(src_pte_old, entry)) {
4901 restore_reserve_on_error(h, dst_vma, addr,
4904 /* huge_ptep of dst_pte won't change as in child */
4907 hugetlb_install_page(dst_vma, dst_pte, addr, new);
4908 spin_unlock(src_ptl);
4909 spin_unlock(dst_ptl);
4915 * No need to notify as we are downgrading page
4916 * table protection not changing it to point
4919 * See Documentation/mm/mmu_notifier.rst
4921 huge_ptep_set_wrprotect(src, addr, src_pte);
4922 entry = huge_pte_wrprotect(entry);
4925 set_huge_pte_at(dst, addr, dst_pte, entry);
4926 hugetlb_count_add(npages, dst);
4928 spin_unlock(src_ptl);
4929 spin_unlock(dst_ptl);
4933 raw_write_seqcount_end(&src->write_protect_seq);
4934 mmu_notifier_invalidate_range_end(&range);
4936 i_mmap_unlock_read(mapping);
4942 static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
4943 unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte)
4945 struct hstate *h = hstate_vma(vma);
4946 struct mm_struct *mm = vma->vm_mm;
4947 spinlock_t *src_ptl, *dst_ptl;
4950 dst_ptl = huge_pte_lock(h, mm, dst_pte);
4951 src_ptl = huge_pte_lockptr(h, mm, src_pte);
4954 * We don't have to worry about the ordering of src and dst ptlocks
4955 * because exclusive mmap_sem (or the i_mmap_lock) prevents deadlock.
4957 if (src_ptl != dst_ptl)
4958 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
4960 pte = huge_ptep_get_and_clear(mm, old_addr, src_pte);
4961 set_huge_pte_at(mm, new_addr, dst_pte, pte);
4963 if (src_ptl != dst_ptl)
4964 spin_unlock(src_ptl);
4965 spin_unlock(dst_ptl);
4968 int move_hugetlb_page_tables(struct vm_area_struct *vma,
4969 struct vm_area_struct *new_vma,
4970 unsigned long old_addr, unsigned long new_addr,
4973 struct hstate *h = hstate_vma(vma);
4974 struct address_space *mapping = vma->vm_file->f_mapping;
4975 unsigned long sz = huge_page_size(h);
4976 struct mm_struct *mm = vma->vm_mm;
4977 unsigned long old_end = old_addr + len;
4978 unsigned long last_addr_mask;
4979 pte_t *src_pte, *dst_pte;
4980 struct mmu_notifier_range range;
4981 bool shared_pmd = false;
4983 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, old_addr,
4985 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
4987 * In case of shared PMDs, we should cover the maximum possible
4990 flush_cache_range(vma, range.start, range.end);
4992 mmu_notifier_invalidate_range_start(&range);
4993 last_addr_mask = hugetlb_mask_last_page(h);
4994 /* Prevent race with file truncation */
4995 i_mmap_lock_write(mapping);
4996 for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
4997 src_pte = huge_pte_offset(mm, old_addr, sz);
4999 old_addr |= last_addr_mask;
5000 new_addr |= last_addr_mask;
5003 if (huge_pte_none(huge_ptep_get(src_pte)))
5006 if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) {
5008 old_addr |= last_addr_mask;
5009 new_addr |= last_addr_mask;
5013 dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
5017 move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte);
5021 flush_tlb_range(vma, range.start, range.end);
5023 flush_tlb_range(vma, old_end - len, old_end);
5024 mmu_notifier_invalidate_range_end(&range);
5025 i_mmap_unlock_write(mapping);
5027 return len + old_addr - old_end;
5030 static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
5031 unsigned long start, unsigned long end,
5032 struct page *ref_page, zap_flags_t zap_flags)
5034 struct mm_struct *mm = vma->vm_mm;
5035 unsigned long address;
5040 struct hstate *h = hstate_vma(vma);
5041 unsigned long sz = huge_page_size(h);
5042 struct mmu_notifier_range range;
5043 unsigned long last_addr_mask;
5044 bool force_flush = false;
5046 WARN_ON(!is_vm_hugetlb_page(vma));
5047 BUG_ON(start & ~huge_page_mask(h));
5048 BUG_ON(end & ~huge_page_mask(h));
5051 * This is a hugetlb vma, all the pte entries should point
5054 tlb_change_page_size(tlb, sz);
5055 tlb_start_vma(tlb, vma);
5058 * If sharing possible, alert mmu notifiers of worst case.
5060 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, mm, start,
5062 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5063 mmu_notifier_invalidate_range_start(&range);
5064 last_addr_mask = hugetlb_mask_last_page(h);
5066 for (; address < end; address += sz) {
5067 ptep = huge_pte_offset(mm, address, sz);
5069 address |= last_addr_mask;
5073 ptl = huge_pte_lock(h, mm, ptep);
5074 if (huge_pmd_unshare(mm, vma, address, ptep)) {
5076 tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
5078 address |= last_addr_mask;
5082 pte = huge_ptep_get(ptep);
5083 if (huge_pte_none(pte)) {
5089 * Migrating hugepage or HWPoisoned hugepage is already
5090 * unmapped and its refcount is dropped, so just clear pte here.
5092 if (unlikely(!pte_present(pte))) {
5094 * If the pte was wr-protected by uffd-wp in any of the
5095 * swap forms, meanwhile the caller does not want to
5096 * drop the uffd-wp bit in this zap, then replace the
5097 * pte with a marker.
5099 if (pte_swp_uffd_wp_any(pte) &&
5100 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5101 set_huge_pte_at(mm, address, ptep,
5102 make_pte_marker(PTE_MARKER_UFFD_WP));
5104 huge_pte_clear(mm, address, ptep, sz);
5109 page = pte_page(pte);
5111 * If a reference page is supplied, it is because a specific
5112 * page is being unmapped, not a range. Ensure the page we
5113 * are about to unmap is the actual page of interest.
5116 if (page != ref_page) {
5121 * Mark the VMA as having unmapped its page so that
5122 * future faults in this VMA will fail rather than
5123 * looking like data was lost
5125 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
5128 pte = huge_ptep_get_and_clear(mm, address, ptep);
5129 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
5130 if (huge_pte_dirty(pte))
5131 set_page_dirty(page);
5132 /* Leave a uffd-wp pte marker if needed */
5133 if (huge_pte_uffd_wp(pte) &&
5134 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5135 set_huge_pte_at(mm, address, ptep,
5136 make_pte_marker(PTE_MARKER_UFFD_WP));
5137 hugetlb_count_sub(pages_per_huge_page(h), mm);
5138 page_remove_rmap(page, vma, true);
5141 tlb_remove_page_size(tlb, page, huge_page_size(h));
5143 * Bail out after unmapping reference page if supplied
5148 mmu_notifier_invalidate_range_end(&range);
5149 tlb_end_vma(tlb, vma);
5152 * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
5153 * could defer the flush until now, since by holding i_mmap_rwsem we
5154 * guaranteed that the last refernece would not be dropped. But we must
5155 * do the flushing before we return, as otherwise i_mmap_rwsem will be
5156 * dropped and the last reference to the shared PMDs page might be
5159 * In theory we could defer the freeing of the PMD pages as well, but
5160 * huge_pmd_unshare() relies on the exact page_count for the PMD page to
5161 * detect sharing, so we cannot defer the release of the page either.
5162 * Instead, do flush now.
5165 tlb_flush_mmu_tlbonly(tlb);
5168 void __unmap_hugepage_range_final(struct mmu_gather *tlb,
5169 struct vm_area_struct *vma, unsigned long start,
5170 unsigned long end, struct page *ref_page,
5171 zap_flags_t zap_flags)
5173 __unmap_hugepage_range(tlb, vma, start, end, ref_page, zap_flags);
5176 * Clear this flag so that x86's huge_pmd_share page_table_shareable
5177 * test will fail on a vma being torn down, and not grab a page table
5178 * on its way out. We're lucky that the flag has such an appropriate
5179 * name, and can in fact be safely cleared here. We could clear it
5180 * before the __unmap_hugepage_range above, but all that's necessary
5181 * is to clear it before releasing the i_mmap_rwsem. This works
5182 * because in the context this is called, the VMA is about to be
5183 * destroyed and the i_mmap_rwsem is held.
5185 vma->vm_flags &= ~VM_MAYSHARE;
5188 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
5189 unsigned long end, struct page *ref_page,
5190 zap_flags_t zap_flags)
5192 struct mmu_gather tlb;
5194 tlb_gather_mmu(&tlb, vma->vm_mm);
5195 __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
5196 tlb_finish_mmu(&tlb);
5200 * This is called when the original mapper is failing to COW a MAP_PRIVATE
5201 * mapping it owns the reserve page for. The intention is to unmap the page
5202 * from other VMAs and let the children be SIGKILLed if they are faulting the
5205 static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
5206 struct page *page, unsigned long address)
5208 struct hstate *h = hstate_vma(vma);
5209 struct vm_area_struct *iter_vma;
5210 struct address_space *mapping;
5214 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
5215 * from page cache lookup which is in HPAGE_SIZE units.
5217 address = address & huge_page_mask(h);
5218 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
5220 mapping = vma->vm_file->f_mapping;
5223 * Take the mapping lock for the duration of the table walk. As
5224 * this mapping should be shared between all the VMAs,
5225 * __unmap_hugepage_range() is called as the lock is already held
5227 i_mmap_lock_write(mapping);
5228 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
5229 /* Do not unmap the current VMA */
5230 if (iter_vma == vma)
5234 * Shared VMAs have their own reserves and do not affect
5235 * MAP_PRIVATE accounting but it is possible that a shared
5236 * VMA is using the same page so check and skip such VMAs.
5238 if (iter_vma->vm_flags & VM_MAYSHARE)
5242 * Unmap the page from other VMAs without their own reserves.
5243 * They get marked to be SIGKILLed if they fault in these
5244 * areas. This is because a future no-page fault on this VMA
5245 * could insert a zeroed page instead of the data existing
5246 * from the time of fork. This would look like data corruption
5248 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
5249 unmap_hugepage_range(iter_vma, address,
5250 address + huge_page_size(h), page, 0);
5252 i_mmap_unlock_write(mapping);
5256 * hugetlb_wp() should be called with page lock of the original hugepage held.
5257 * Called with hugetlb_fault_mutex_table held and pte_page locked so we
5258 * cannot race with other handlers or page migration.
5259 * Keep the pte_same checks anyway to make transition from the mutex easier.
5261 static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma,
5262 unsigned long address, pte_t *ptep, unsigned int flags,
5263 struct page *pagecache_page, spinlock_t *ptl)
5265 const bool unshare = flags & FAULT_FLAG_UNSHARE;
5267 struct hstate *h = hstate_vma(vma);
5268 struct page *old_page, *new_page;
5269 int outside_reserve = 0;
5271 unsigned long haddr = address & huge_page_mask(h);
5272 struct mmu_notifier_range range;
5274 VM_BUG_ON(unshare && (flags & FOLL_WRITE));
5275 VM_BUG_ON(!unshare && !(flags & FOLL_WRITE));
5278 * hugetlb does not support FOLL_FORCE-style write faults that keep the
5279 * PTE mapped R/O such as maybe_mkwrite() would do.
5281 if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE)))
5282 return VM_FAULT_SIGSEGV;
5284 /* Let's take out MAP_SHARED mappings first. */
5285 if (vma->vm_flags & VM_MAYSHARE) {
5286 if (unlikely(unshare))
5288 set_huge_ptep_writable(vma, haddr, ptep);
5292 pte = huge_ptep_get(ptep);
5293 old_page = pte_page(pte);
5295 delayacct_wpcopy_start();
5299 * If no-one else is actually using this page, we're the exclusive
5300 * owner and can reuse this page.
5302 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
5303 if (!PageAnonExclusive(old_page))
5304 page_move_anon_rmap(old_page, vma);
5305 if (likely(!unshare))
5306 set_huge_ptep_writable(vma, haddr, ptep);
5308 delayacct_wpcopy_end();
5311 VM_BUG_ON_PAGE(PageAnon(old_page) && PageAnonExclusive(old_page),
5315 * If the process that created a MAP_PRIVATE mapping is about to
5316 * perform a COW due to a shared page count, attempt to satisfy
5317 * the allocation without using the existing reserves. The pagecache
5318 * page is used to determine if the reserve at this address was
5319 * consumed or not. If reserves were used, a partial faulted mapping
5320 * at the time of fork() could consume its reserves on COW instead
5321 * of the full address range.
5323 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
5324 old_page != pagecache_page)
5325 outside_reserve = 1;
5330 * Drop page table lock as buddy allocator may be called. It will
5331 * be acquired again before returning to the caller, as expected.
5334 new_page = alloc_huge_page(vma, haddr, outside_reserve);
5336 if (IS_ERR(new_page)) {
5338 * If a process owning a MAP_PRIVATE mapping fails to COW,
5339 * it is due to references held by a child and an insufficient
5340 * huge page pool. To guarantee the original mappers
5341 * reliability, unmap the page from child processes. The child
5342 * may get SIGKILLed if it later faults.
5344 if (outside_reserve) {
5345 struct address_space *mapping = vma->vm_file->f_mapping;
5351 * Drop hugetlb_fault_mutex and i_mmap_rwsem before
5352 * unmapping. unmapping needs to hold i_mmap_rwsem
5353 * in write mode. Dropping i_mmap_rwsem in read mode
5354 * here is OK as COW mappings do not interact with
5357 * Reacquire both after unmap operation.
5359 idx = vma_hugecache_offset(h, vma, haddr);
5360 hash = hugetlb_fault_mutex_hash(mapping, idx);
5361 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5362 i_mmap_unlock_read(mapping);
5364 unmap_ref_private(mm, vma, old_page, haddr);
5366 i_mmap_lock_read(mapping);
5367 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5369 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
5371 pte_same(huge_ptep_get(ptep), pte)))
5372 goto retry_avoidcopy;
5374 * race occurs while re-acquiring page table
5375 * lock, and our job is done.
5377 delayacct_wpcopy_end();
5381 ret = vmf_error(PTR_ERR(new_page));
5382 goto out_release_old;
5386 * When the original hugepage is shared one, it does not have
5387 * anon_vma prepared.
5389 if (unlikely(anon_vma_prepare(vma))) {
5391 goto out_release_all;
5394 copy_user_huge_page(new_page, old_page, address, vma,
5395 pages_per_huge_page(h));
5396 __SetPageUptodate(new_page);
5398 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, haddr,
5399 haddr + huge_page_size(h));
5400 mmu_notifier_invalidate_range_start(&range);
5403 * Retake the page table lock to check for racing updates
5404 * before the page tables are altered
5407 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
5408 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
5409 ClearHPageRestoreReserve(new_page);
5411 /* Break COW or unshare */
5412 huge_ptep_clear_flush(vma, haddr, ptep);
5413 mmu_notifier_invalidate_range(mm, range.start, range.end);
5414 page_remove_rmap(old_page, vma, true);
5415 hugepage_add_new_anon_rmap(new_page, vma, haddr);
5416 set_huge_pte_at(mm, haddr, ptep,
5417 make_huge_pte(vma, new_page, !unshare));
5418 SetHPageMigratable(new_page);
5419 /* Make the old page be freed below */
5420 new_page = old_page;
5423 mmu_notifier_invalidate_range_end(&range);
5426 * No restore in case of successful pagetable update (Break COW or
5429 if (new_page != old_page)
5430 restore_reserve_on_error(h, vma, haddr, new_page);
5435 spin_lock(ptl); /* Caller expects lock to be held */
5437 delayacct_wpcopy_end();
5442 * Return whether there is a pagecache page to back given address within VMA.
5443 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
5445 static bool hugetlbfs_pagecache_present(struct hstate *h,
5446 struct vm_area_struct *vma, unsigned long address)
5448 struct address_space *mapping;
5452 mapping = vma->vm_file->f_mapping;
5453 idx = vma_hugecache_offset(h, vma, address);
5455 page = find_get_page(mapping, idx);
5458 return page != NULL;
5461 int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
5464 struct folio *folio = page_folio(page);
5465 struct inode *inode = mapping->host;
5466 struct hstate *h = hstate_inode(inode);
5469 __folio_set_locked(folio);
5470 err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL);
5472 if (unlikely(err)) {
5473 __folio_clear_locked(folio);
5476 ClearHPageRestoreReserve(page);
5479 * mark folio dirty so that it will not be removed from cache/file
5480 * by non-hugetlbfs specific code paths.
5482 folio_mark_dirty(folio);
5484 spin_lock(&inode->i_lock);
5485 inode->i_blocks += blocks_per_huge_page(h);
5486 spin_unlock(&inode->i_lock);
5490 static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma,
5491 struct address_space *mapping,
5494 unsigned long haddr,
5496 unsigned long reason)
5500 struct vm_fault vmf = {
5503 .real_address = addr,
5507 * Hard to debug if it ends up being
5508 * used by a callee that assumes
5509 * something about the other
5510 * uninitialized fields... same as in
5516 * hugetlb_fault_mutex and i_mmap_rwsem must be
5517 * dropped before handling userfault. Reacquire
5518 * after handling fault to make calling code simpler.
5520 hash = hugetlb_fault_mutex_hash(mapping, idx);
5521 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5522 i_mmap_unlock_read(mapping);
5523 ret = handle_userfault(&vmf, reason);
5524 i_mmap_lock_read(mapping);
5525 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5530 static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
5531 struct vm_area_struct *vma,
5532 struct address_space *mapping, pgoff_t idx,
5533 unsigned long address, pte_t *ptep,
5534 pte_t old_pte, unsigned int flags)
5536 struct hstate *h = hstate_vma(vma);
5537 vm_fault_t ret = VM_FAULT_SIGBUS;
5543 unsigned long haddr = address & huge_page_mask(h);
5544 bool new_page, new_pagecache_page = false;
5547 * Currently, we are forced to kill the process in the event the
5548 * original mapper has unmapped pages from the child due to a failed
5549 * COW/unsharing. Warn that such a situation has occurred as it may not
5552 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
5553 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
5559 * We can not race with truncation due to holding i_mmap_rwsem.
5560 * i_size is modified when holding i_mmap_rwsem, so check here
5561 * once for faults beyond end of file.
5563 size = i_size_read(mapping->host) >> huge_page_shift(h);
5568 page = find_lock_page(mapping, idx);
5570 /* Check for page in userfault range */
5571 if (userfaultfd_missing(vma)) {
5572 ret = hugetlb_handle_userfault(vma, mapping, idx,
5573 flags, haddr, address,
5578 page = alloc_huge_page(vma, haddr, 0);
5581 * Returning error will result in faulting task being
5582 * sent SIGBUS. The hugetlb fault mutex prevents two
5583 * tasks from racing to fault in the same page which
5584 * could result in false unable to allocate errors.
5585 * Page migration does not take the fault mutex, but
5586 * does a clear then write of pte's under page table
5587 * lock. Page fault code could race with migration,
5588 * notice the clear pte and try to allocate a page
5589 * here. Before returning error, get ptl and make
5590 * sure there really is no pte entry.
5592 ptl = huge_pte_lock(h, mm, ptep);
5594 if (huge_pte_none(huge_ptep_get(ptep)))
5595 ret = vmf_error(PTR_ERR(page));
5599 clear_huge_page(page, address, pages_per_huge_page(h));
5600 __SetPageUptodate(page);
5603 if (vma->vm_flags & VM_MAYSHARE) {
5604 int err = huge_add_to_page_cache(page, mapping, idx);
5607 * err can't be -EEXIST which implies someone
5608 * else consumed the reservation since hugetlb
5609 * fault mutex is held when add a hugetlb page
5610 * to the page cache. So it's safe to call
5611 * restore_reserve_on_error() here.
5613 restore_reserve_on_error(h, vma, haddr, page);
5617 new_pagecache_page = true;
5620 if (unlikely(anon_vma_prepare(vma))) {
5622 goto backout_unlocked;
5628 * If memory error occurs between mmap() and fault, some process
5629 * don't have hwpoisoned swap entry for errored virtual address.
5630 * So we need to block hugepage fault by PG_hwpoison bit check.
5632 if (unlikely(PageHWPoison(page))) {
5633 ret = VM_FAULT_HWPOISON_LARGE |
5634 VM_FAULT_SET_HINDEX(hstate_index(h));
5635 goto backout_unlocked;
5638 /* Check for page in userfault range. */
5639 if (userfaultfd_minor(vma)) {
5642 ret = hugetlb_handle_userfault(vma, mapping, idx,
5643 flags, haddr, address,
5650 * If we are going to COW a private mapping later, we examine the
5651 * pending reservations for this page now. This will ensure that
5652 * any allocations necessary to record that reservation occur outside
5655 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
5656 if (vma_needs_reservation(h, vma, haddr) < 0) {
5658 goto backout_unlocked;
5660 /* Just decrements count, does not deallocate */
5661 vma_end_reservation(h, vma, haddr);
5664 ptl = huge_pte_lock(h, mm, ptep);
5666 /* If pte changed from under us, retry */
5667 if (!pte_same(huge_ptep_get(ptep), old_pte))
5671 ClearHPageRestoreReserve(page);
5672 hugepage_add_new_anon_rmap(page, vma, haddr);
5674 page_dup_file_rmap(page, true);
5675 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
5676 && (vma->vm_flags & VM_SHARED)));
5678 * If this pte was previously wr-protected, keep it wr-protected even
5681 if (unlikely(pte_marker_uffd_wp(old_pte)))
5682 new_pte = huge_pte_wrprotect(huge_pte_mkuffd_wp(new_pte));
5683 set_huge_pte_at(mm, haddr, ptep, new_pte);
5685 hugetlb_count_add(pages_per_huge_page(h), mm);
5686 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
5687 /* Optimization, do the COW without a second fault */
5688 ret = hugetlb_wp(mm, vma, address, ptep, flags, page, ptl);
5694 * Only set HPageMigratable in newly allocated pages. Existing pages
5695 * found in the pagecache may not have HPageMigratableset if they have
5696 * been isolated for migration.
5699 SetHPageMigratable(page);
5709 /* restore reserve for newly allocated pages not in page cache */
5710 if (new_page && !new_pagecache_page)
5711 restore_reserve_on_error(h, vma, haddr, page);
5717 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
5719 unsigned long key[2];
5722 key[0] = (unsigned long) mapping;
5725 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
5727 return hash & (num_fault_mutexes - 1);
5731 * For uniprocessor systems we always use a single mutex, so just
5732 * return 0 and avoid the hashing overhead.
5734 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
5740 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
5741 unsigned long address, unsigned int flags)
5748 struct page *page = NULL;
5749 struct page *pagecache_page = NULL;
5750 struct hstate *h = hstate_vma(vma);
5751 struct address_space *mapping;
5752 int need_wait_lock = 0;
5753 unsigned long haddr = address & huge_page_mask(h);
5755 ptep = huge_pte_offset(mm, haddr, huge_page_size(h));
5758 * Since we hold no locks, ptep could be stale. That is
5759 * OK as we are only making decisions based on content and
5760 * not actually modifying content here.
5762 entry = huge_ptep_get(ptep);
5763 if (unlikely(is_hugetlb_entry_migration(entry))) {
5764 migration_entry_wait_huge(vma, ptep);
5766 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
5767 return VM_FAULT_HWPOISON_LARGE |
5768 VM_FAULT_SET_HINDEX(hstate_index(h));
5772 * Acquire i_mmap_rwsem before calling huge_pte_alloc and hold
5773 * until finished with ptep. This serves two purposes:
5774 * 1) It prevents huge_pmd_unshare from being called elsewhere
5775 * and making the ptep no longer valid.
5776 * 2) It synchronizes us with i_size modifications during truncation.
5778 * ptep could have already be assigned via huge_pte_offset. That
5779 * is OK, as huge_pte_alloc will return the same value unless
5780 * something has changed.
5782 mapping = vma->vm_file->f_mapping;
5783 i_mmap_lock_read(mapping);
5784 ptep = huge_pte_alloc(mm, vma, haddr, huge_page_size(h));
5786 i_mmap_unlock_read(mapping);
5787 return VM_FAULT_OOM;
5791 * Serialize hugepage allocation and instantiation, so that we don't
5792 * get spurious allocation failures if two CPUs race to instantiate
5793 * the same page in the page cache.
5795 idx = vma_hugecache_offset(h, vma, haddr);
5796 hash = hugetlb_fault_mutex_hash(mapping, idx);
5797 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5799 entry = huge_ptep_get(ptep);
5800 /* PTE markers should be handled the same way as none pte */
5801 if (huge_pte_none_mostly(entry)) {
5802 ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep,
5810 * entry could be a migration/hwpoison entry at this point, so this
5811 * check prevents the kernel from going below assuming that we have
5812 * an active hugepage in pagecache. This goto expects the 2nd page
5813 * fault, and is_hugetlb_entry_(migration|hwpoisoned) check will
5814 * properly handle it.
5816 if (!pte_present(entry))
5820 * If we are going to COW/unshare the mapping later, we examine the
5821 * pending reservations for this page now. This will ensure that any
5822 * allocations necessary to record that reservation occur outside the
5823 * spinlock. Also lookup the pagecache page now as it is used to
5824 * determine if a reservation has been consumed.
5826 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
5827 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(entry)) {
5828 if (vma_needs_reservation(h, vma, haddr) < 0) {
5832 /* Just decrements count, does not deallocate */
5833 vma_end_reservation(h, vma, haddr);
5835 pagecache_page = find_lock_page(mapping, idx);
5838 ptl = huge_pte_lock(h, mm, ptep);
5840 /* Check for a racing update before calling hugetlb_wp() */
5841 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
5844 /* Handle userfault-wp first, before trying to lock more pages */
5845 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(ptep)) &&
5846 (flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
5847 struct vm_fault vmf = {
5850 .real_address = address,
5855 if (pagecache_page) {
5856 unlock_page(pagecache_page);
5857 put_page(pagecache_page);
5859 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5860 i_mmap_unlock_read(mapping);
5861 return handle_userfault(&vmf, VM_UFFD_WP);
5865 * hugetlb_wp() requires page locks of pte_page(entry) and
5866 * pagecache_page, so here we need take the former one
5867 * when page != pagecache_page or !pagecache_page.
5869 page = pte_page(entry);
5870 if (page != pagecache_page)
5871 if (!trylock_page(page)) {
5878 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
5879 if (!huge_pte_write(entry)) {
5880 ret = hugetlb_wp(mm, vma, address, ptep, flags,
5881 pagecache_page, ptl);
5883 } else if (likely(flags & FAULT_FLAG_WRITE)) {
5884 entry = huge_pte_mkdirty(entry);
5887 entry = pte_mkyoung(entry);
5888 if (huge_ptep_set_access_flags(vma, haddr, ptep, entry,
5889 flags & FAULT_FLAG_WRITE))
5890 update_mmu_cache(vma, haddr, ptep);
5892 if (page != pagecache_page)
5898 if (pagecache_page) {
5899 unlock_page(pagecache_page);
5900 put_page(pagecache_page);
5903 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5904 i_mmap_unlock_read(mapping);
5906 * Generally it's safe to hold refcount during waiting page lock. But
5907 * here we just wait to defer the next page fault to avoid busy loop and
5908 * the page is not used after unlocked before returning from the current
5909 * page fault. So we are safe from accessing freed page, even if we wait
5910 * here without taking refcount.
5913 wait_on_page_locked(page);
5917 #ifdef CONFIG_USERFAULTFD
5919 * Used by userfaultfd UFFDIO_COPY. Based on mcopy_atomic_pte with
5920 * modifications for huge pages.
5922 int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
5924 struct vm_area_struct *dst_vma,
5925 unsigned long dst_addr,
5926 unsigned long src_addr,
5927 enum mcopy_atomic_mode mode,
5928 struct page **pagep,
5931 bool is_continue = (mode == MCOPY_ATOMIC_CONTINUE);
5932 struct hstate *h = hstate_vma(dst_vma);
5933 struct address_space *mapping = dst_vma->vm_file->f_mapping;
5934 pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
5936 int vm_shared = dst_vma->vm_flags & VM_SHARED;
5942 bool page_in_pagecache = false;
5946 page = find_lock_page(mapping, idx);
5949 page_in_pagecache = true;
5950 } else if (!*pagep) {
5951 /* If a page already exists, then it's UFFDIO_COPY for
5952 * a non-missing case. Return -EEXIST.
5955 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
5960 page = alloc_huge_page(dst_vma, dst_addr, 0);
5966 ret = copy_huge_page_from_user(page,
5967 (const void __user *) src_addr,
5968 pages_per_huge_page(h), false);
5970 /* fallback to copy_from_user outside mmap_lock */
5971 if (unlikely(ret)) {
5973 /* Free the allocated page which may have
5974 * consumed a reservation.
5976 restore_reserve_on_error(h, dst_vma, dst_addr, page);
5979 /* Allocate a temporary page to hold the copied
5982 page = alloc_huge_page_vma(h, dst_vma, dst_addr);
5988 /* Set the outparam pagep and return to the caller to
5989 * copy the contents outside the lock. Don't free the
5996 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6003 page = alloc_huge_page(dst_vma, dst_addr, 0);
6010 copy_user_huge_page(page, *pagep, dst_addr, dst_vma,
6011 pages_per_huge_page(h));
6017 * The memory barrier inside __SetPageUptodate makes sure that
6018 * preceding stores to the page contents become visible before
6019 * the set_pte_at() write.
6021 __SetPageUptodate(page);
6023 /* Add shared, newly allocated pages to the page cache. */
6024 if (vm_shared && !is_continue) {
6025 size = i_size_read(mapping->host) >> huge_page_shift(h);
6028 goto out_release_nounlock;
6031 * Serialization between remove_inode_hugepages() and
6032 * huge_add_to_page_cache() below happens through the
6033 * hugetlb_fault_mutex_table that here must be hold by
6036 ret = huge_add_to_page_cache(page, mapping, idx);
6038 goto out_release_nounlock;
6039 page_in_pagecache = true;
6042 ptl = huge_pte_lock(h, dst_mm, dst_pte);
6045 * Recheck the i_size after holding PT lock to make sure not
6046 * to leave any page mapped (as page_mapped()) beyond the end
6047 * of the i_size (remove_inode_hugepages() is strict about
6048 * enforcing that). If we bail out here, we'll also leave a
6049 * page in the radix tree in the vm_shared case beyond the end
6050 * of the i_size, but remove_inode_hugepages() will take care
6051 * of it as soon as we drop the hugetlb_fault_mutex_table.
6053 size = i_size_read(mapping->host) >> huge_page_shift(h);
6056 goto out_release_unlock;
6060 * We allow to overwrite a pte marker: consider when both MISSING|WP
6061 * registered, we firstly wr-protect a none pte which has no page cache
6062 * page backing it, then access the page.
6064 if (!huge_pte_none_mostly(huge_ptep_get(dst_pte)))
6065 goto out_release_unlock;
6067 if (page_in_pagecache) {
6068 page_dup_file_rmap(page, true);
6070 ClearHPageRestoreReserve(page);
6071 hugepage_add_new_anon_rmap(page, dst_vma, dst_addr);
6075 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY
6076 * with wp flag set, don't set pte write bit.
6078 if (wp_copy || (is_continue && !vm_shared))
6081 writable = dst_vma->vm_flags & VM_WRITE;
6083 _dst_pte = make_huge_pte(dst_vma, page, writable);
6085 * Always mark UFFDIO_COPY page dirty; note that this may not be
6086 * extremely important for hugetlbfs for now since swapping is not
6087 * supported, but we should still be clear in that this page cannot be
6088 * thrown away at will, even if write bit not set.
6090 _dst_pte = huge_pte_mkdirty(_dst_pte);
6091 _dst_pte = pte_mkyoung(_dst_pte);
6094 _dst_pte = huge_pte_mkuffd_wp(_dst_pte);
6096 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
6098 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
6100 /* No need to invalidate - it was non-present before */
6101 update_mmu_cache(dst_vma, dst_addr, dst_pte);
6105 SetHPageMigratable(page);
6106 if (vm_shared || is_continue)
6113 if (vm_shared || is_continue)
6115 out_release_nounlock:
6116 if (!page_in_pagecache)
6117 restore_reserve_on_error(h, dst_vma, dst_addr, page);
6121 #endif /* CONFIG_USERFAULTFD */
6123 static void record_subpages_vmas(struct page *page, struct vm_area_struct *vma,
6124 int refs, struct page **pages,
6125 struct vm_area_struct **vmas)
6129 for (nr = 0; nr < refs; nr++) {
6131 pages[nr] = mem_map_offset(page, nr);
6137 static inline bool __follow_hugetlb_must_fault(unsigned int flags, pte_t *pte,
6140 pte_t pteval = huge_ptep_get(pte);
6143 if (is_swap_pte(pteval))
6145 if (huge_pte_write(pteval))
6147 if (flags & FOLL_WRITE)
6149 if (gup_must_unshare(flags, pte_page(pteval))) {
6156 long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
6157 struct page **pages, struct vm_area_struct **vmas,
6158 unsigned long *position, unsigned long *nr_pages,
6159 long i, unsigned int flags, int *locked)
6161 unsigned long pfn_offset;
6162 unsigned long vaddr = *position;
6163 unsigned long remainder = *nr_pages;
6164 struct hstate *h = hstate_vma(vma);
6165 int err = -EFAULT, refs;
6167 while (vaddr < vma->vm_end && remainder) {
6169 spinlock_t *ptl = NULL;
6170 bool unshare = false;
6175 * If we have a pending SIGKILL, don't keep faulting pages and
6176 * potentially allocating memory.
6178 if (fatal_signal_pending(current)) {
6184 * Some archs (sparc64, sh*) have multiple pte_ts to
6185 * each hugepage. We have to make sure we get the
6186 * first, for the page indexing below to work.
6188 * Note that page table lock is not held when pte is null.
6190 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h),
6193 ptl = huge_pte_lock(h, mm, pte);
6194 absent = !pte || huge_pte_none(huge_ptep_get(pte));
6197 * When coredumping, it suits get_dump_page if we just return
6198 * an error where there's an empty slot with no huge pagecache
6199 * to back it. This way, we avoid allocating a hugepage, and
6200 * the sparse dumpfile avoids allocating disk blocks, but its
6201 * huge holes still show up with zeroes where they need to be.
6203 if (absent && (flags & FOLL_DUMP) &&
6204 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
6212 * We need call hugetlb_fault for both hugepages under migration
6213 * (in which case hugetlb_fault waits for the migration,) and
6214 * hwpoisoned hugepages (in which case we need to prevent the
6215 * caller from accessing to them.) In order to do this, we use
6216 * here is_swap_pte instead of is_hugetlb_entry_migration and
6217 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
6218 * both cases, and because we can't follow correct pages
6219 * directly from any kind of swap entries.
6222 __follow_hugetlb_must_fault(flags, pte, &unshare)) {
6224 unsigned int fault_flags = 0;
6228 if (flags & FOLL_WRITE)
6229 fault_flags |= FAULT_FLAG_WRITE;
6231 fault_flags |= FAULT_FLAG_UNSHARE;
6233 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6234 FAULT_FLAG_KILLABLE;
6235 if (flags & FOLL_NOWAIT)
6236 fault_flags |= FAULT_FLAG_ALLOW_RETRY |
6237 FAULT_FLAG_RETRY_NOWAIT;
6238 if (flags & FOLL_TRIED) {
6240 * Note: FAULT_FLAG_ALLOW_RETRY and
6241 * FAULT_FLAG_TRIED can co-exist
6243 fault_flags |= FAULT_FLAG_TRIED;
6245 ret = hugetlb_fault(mm, vma, vaddr, fault_flags);
6246 if (ret & VM_FAULT_ERROR) {
6247 err = vm_fault_to_errno(ret, flags);
6251 if (ret & VM_FAULT_RETRY) {
6253 !(fault_flags & FAULT_FLAG_RETRY_NOWAIT))
6257 * VM_FAULT_RETRY must not return an
6258 * error, it will return zero
6261 * No need to update "position" as the
6262 * caller will not check it after
6263 * *nr_pages is set to 0.
6270 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
6271 page = pte_page(huge_ptep_get(pte));
6273 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
6274 !PageAnonExclusive(page), page);
6277 * If subpage information not requested, update counters
6278 * and skip the same_page loop below.
6280 if (!pages && !vmas && !pfn_offset &&
6281 (vaddr + huge_page_size(h) < vma->vm_end) &&
6282 (remainder >= pages_per_huge_page(h))) {
6283 vaddr += huge_page_size(h);
6284 remainder -= pages_per_huge_page(h);
6285 i += pages_per_huge_page(h);
6290 /* vaddr may not be aligned to PAGE_SIZE */
6291 refs = min3(pages_per_huge_page(h) - pfn_offset, remainder,
6292 (vma->vm_end - ALIGN_DOWN(vaddr, PAGE_SIZE)) >> PAGE_SHIFT);
6295 record_subpages_vmas(mem_map_offset(page, pfn_offset),
6297 likely(pages) ? pages + i : NULL,
6298 vmas ? vmas + i : NULL);
6302 * try_grab_folio() should always succeed here,
6303 * because: a) we hold the ptl lock, and b) we've just
6304 * checked that the huge page is present in the page
6305 * tables. If the huge page is present, then the tail
6306 * pages must also be present. The ptl prevents the
6307 * head page and tail pages from being rearranged in
6308 * any way. So this page must be available at this
6309 * point, unless the page refcount overflowed:
6311 if (WARN_ON_ONCE(!try_grab_folio(pages[i], refs,
6320 vaddr += (refs << PAGE_SHIFT);
6326 *nr_pages = remainder;
6328 * setting position is actually required only if remainder is
6329 * not zero but it's faster not to add a "if (remainder)"
6337 unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
6338 unsigned long address, unsigned long end,
6339 pgprot_t newprot, unsigned long cp_flags)
6341 struct mm_struct *mm = vma->vm_mm;
6342 unsigned long start = address;
6345 struct hstate *h = hstate_vma(vma);
6346 unsigned long pages = 0, psize = huge_page_size(h);
6347 bool shared_pmd = false;
6348 struct mmu_notifier_range range;
6349 unsigned long last_addr_mask;
6350 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
6351 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
6354 * In the case of shared PMDs, the area to flush could be beyond
6355 * start/end. Set range.start/range.end to cover the maximum possible
6356 * range if PMD sharing is possible.
6358 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
6359 0, vma, mm, start, end);
6360 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
6362 BUG_ON(address >= end);
6363 flush_cache_range(vma, range.start, range.end);
6365 mmu_notifier_invalidate_range_start(&range);
6366 last_addr_mask = hugetlb_mask_last_page(h);
6367 i_mmap_lock_write(vma->vm_file->f_mapping);
6368 for (; address < end; address += psize) {
6370 ptep = huge_pte_offset(mm, address, psize);
6372 address |= last_addr_mask;
6375 ptl = huge_pte_lock(h, mm, ptep);
6376 if (huge_pmd_unshare(mm, vma, address, ptep)) {
6378 * When uffd-wp is enabled on the vma, unshare
6379 * shouldn't happen at all. Warn about it if it
6380 * happened due to some reason.
6382 WARN_ON_ONCE(uffd_wp || uffd_wp_resolve);
6386 address |= last_addr_mask;
6389 pte = huge_ptep_get(ptep);
6390 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
6394 if (unlikely(is_hugetlb_entry_migration(pte))) {
6395 swp_entry_t entry = pte_to_swp_entry(pte);
6396 struct page *page = pfn_swap_entry_to_page(entry);
6398 if (!is_readable_migration_entry(entry)) {
6402 entry = make_readable_exclusive_migration_entry(
6405 entry = make_readable_migration_entry(
6407 newpte = swp_entry_to_pte(entry);
6409 newpte = pte_swp_mkuffd_wp(newpte);
6410 else if (uffd_wp_resolve)
6411 newpte = pte_swp_clear_uffd_wp(newpte);
6412 set_huge_pte_at(mm, address, ptep, newpte);
6418 if (unlikely(pte_marker_uffd_wp(pte))) {
6420 * This is changing a non-present pte into a none pte,
6421 * no need for huge_ptep_modify_prot_start/commit().
6423 if (uffd_wp_resolve)
6424 huge_pte_clear(mm, address, ptep, psize);
6426 if (!huge_pte_none(pte)) {
6428 unsigned int shift = huge_page_shift(hstate_vma(vma));
6430 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
6431 pte = huge_pte_modify(old_pte, newprot);
6432 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
6434 pte = huge_pte_mkuffd_wp(huge_pte_wrprotect(pte));
6435 else if (uffd_wp_resolve)
6436 pte = huge_pte_clear_uffd_wp(pte);
6437 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
6441 if (unlikely(uffd_wp))
6442 /* Safe to modify directly (none->non-present). */
6443 set_huge_pte_at(mm, address, ptep,
6444 make_pte_marker(PTE_MARKER_UFFD_WP));
6449 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
6450 * may have cleared our pud entry and done put_page on the page table:
6451 * once we release i_mmap_rwsem, another task can do the final put_page
6452 * and that page table be reused and filled with junk. If we actually
6453 * did unshare a page of pmds, flush the range corresponding to the pud.
6456 flush_hugetlb_tlb_range(vma, range.start, range.end);
6458 flush_hugetlb_tlb_range(vma, start, end);
6460 * No need to call mmu_notifier_invalidate_range() we are downgrading
6461 * page table protection not changing it to point to a new page.
6463 * See Documentation/mm/mmu_notifier.rst
6465 i_mmap_unlock_write(vma->vm_file->f_mapping);
6466 mmu_notifier_invalidate_range_end(&range);
6468 return pages << h->order;
6471 /* Return true if reservation was successful, false otherwise. */
6472 bool hugetlb_reserve_pages(struct inode *inode,
6474 struct vm_area_struct *vma,
6475 vm_flags_t vm_flags)
6478 struct hstate *h = hstate_inode(inode);
6479 struct hugepage_subpool *spool = subpool_inode(inode);
6480 struct resv_map *resv_map;
6481 struct hugetlb_cgroup *h_cg = NULL;
6482 long gbl_reserve, regions_needed = 0;
6484 /* This should never happen */
6486 VM_WARN(1, "%s called with a negative range\n", __func__);
6491 * Only apply hugepage reservation if asked. At fault time, an
6492 * attempt will be made for VM_NORESERVE to allocate a page
6493 * without using reserves
6495 if (vm_flags & VM_NORESERVE)
6499 * Shared mappings base their reservation on the number of pages that
6500 * are already allocated on behalf of the file. Private mappings need
6501 * to reserve the full area even if read-only as mprotect() may be
6502 * called to make the mapping read-write. Assume !vma is a shm mapping
6504 if (!vma || vma->vm_flags & VM_MAYSHARE) {
6506 * resv_map can not be NULL as hugetlb_reserve_pages is only
6507 * called for inodes for which resv_maps were created (see
6508 * hugetlbfs_get_inode).
6510 resv_map = inode_resv_map(inode);
6512 chg = region_chg(resv_map, from, to, ®ions_needed);
6515 /* Private mapping. */
6516 resv_map = resv_map_alloc();
6522 set_vma_resv_map(vma, resv_map);
6523 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
6529 if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
6530 chg * pages_per_huge_page(h), &h_cg) < 0)
6533 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
6534 /* For private mappings, the hugetlb_cgroup uncharge info hangs
6537 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
6541 * There must be enough pages in the subpool for the mapping. If
6542 * the subpool has a minimum size, there may be some global
6543 * reservations already in place (gbl_reserve).
6545 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
6546 if (gbl_reserve < 0)
6547 goto out_uncharge_cgroup;
6550 * Check enough hugepages are available for the reservation.
6551 * Hand the pages back to the subpool if there are not
6553 if (hugetlb_acct_memory(h, gbl_reserve) < 0)
6557 * Account for the reservations made. Shared mappings record regions
6558 * that have reservations as they are shared by multiple VMAs.
6559 * When the last VMA disappears, the region map says how much
6560 * the reservation was and the page cache tells how much of
6561 * the reservation was consumed. Private mappings are per-VMA and
6562 * only the consumed reservations are tracked. When the VMA
6563 * disappears, the original reservation is the VMA size and the
6564 * consumed reservations are stored in the map. Hence, nothing
6565 * else has to be done for private mappings here
6567 if (!vma || vma->vm_flags & VM_MAYSHARE) {
6568 add = region_add(resv_map, from, to, regions_needed, h, h_cg);
6570 if (unlikely(add < 0)) {
6571 hugetlb_acct_memory(h, -gbl_reserve);
6573 } else if (unlikely(chg > add)) {
6575 * pages in this range were added to the reserve
6576 * map between region_chg and region_add. This
6577 * indicates a race with alloc_huge_page. Adjust
6578 * the subpool and reserve counts modified above
6579 * based on the difference.
6584 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the
6585 * reference to h_cg->css. See comment below for detail.
6587 hugetlb_cgroup_uncharge_cgroup_rsvd(
6589 (chg - add) * pages_per_huge_page(h), h_cg);
6591 rsv_adjust = hugepage_subpool_put_pages(spool,
6593 hugetlb_acct_memory(h, -rsv_adjust);
6596 * The file_regions will hold their own reference to
6597 * h_cg->css. So we should release the reference held
6598 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are
6601 hugetlb_cgroup_put_rsvd_cgroup(h_cg);
6607 /* put back original number of pages, chg */
6608 (void)hugepage_subpool_put_pages(spool, chg);
6609 out_uncharge_cgroup:
6610 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
6611 chg * pages_per_huge_page(h), h_cg);
6613 if (!vma || vma->vm_flags & VM_MAYSHARE)
6614 /* Only call region_abort if the region_chg succeeded but the
6615 * region_add failed or didn't run.
6617 if (chg >= 0 && add < 0)
6618 region_abort(resv_map, from, to, regions_needed);
6619 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
6620 kref_put(&resv_map->refs, resv_map_release);
6624 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
6627 struct hstate *h = hstate_inode(inode);
6628 struct resv_map *resv_map = inode_resv_map(inode);
6630 struct hugepage_subpool *spool = subpool_inode(inode);
6634 * Since this routine can be called in the evict inode path for all
6635 * hugetlbfs inodes, resv_map could be NULL.
6638 chg = region_del(resv_map, start, end);
6640 * region_del() can fail in the rare case where a region
6641 * must be split and another region descriptor can not be
6642 * allocated. If end == LONG_MAX, it will not fail.
6648 spin_lock(&inode->i_lock);
6649 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
6650 spin_unlock(&inode->i_lock);
6653 * If the subpool has a minimum size, the number of global
6654 * reservations to be released may be adjusted.
6656 * Note that !resv_map implies freed == 0. So (chg - freed)
6657 * won't go negative.
6659 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
6660 hugetlb_acct_memory(h, -gbl_reserve);
6665 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
6666 static unsigned long page_table_shareable(struct vm_area_struct *svma,
6667 struct vm_area_struct *vma,
6668 unsigned long addr, pgoff_t idx)
6670 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
6672 unsigned long sbase = saddr & PUD_MASK;
6673 unsigned long s_end = sbase + PUD_SIZE;
6675 /* Allow segments to share if only one is marked locked */
6676 unsigned long vm_flags = vma->vm_flags & VM_LOCKED_CLEAR_MASK;
6677 unsigned long svm_flags = svma->vm_flags & VM_LOCKED_CLEAR_MASK;
6680 * match the virtual addresses, permission and the alignment of the
6683 if (pmd_index(addr) != pmd_index(saddr) ||
6684 vm_flags != svm_flags ||
6685 !range_in_vma(svma, sbase, s_end))
6691 static bool vma_shareable(struct vm_area_struct *vma, unsigned long addr)
6693 unsigned long base = addr & PUD_MASK;
6694 unsigned long end = base + PUD_SIZE;
6697 * check on proper vm_flags and page table alignment
6699 if (vma->vm_flags & VM_MAYSHARE && range_in_vma(vma, base, end))
6704 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
6706 #ifdef CONFIG_USERFAULTFD
6707 if (uffd_disable_huge_pmd_share(vma))
6710 return vma_shareable(vma, addr);
6714 * Determine if start,end range within vma could be mapped by shared pmd.
6715 * If yes, adjust start and end to cover range associated with possible
6716 * shared pmd mappings.
6718 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
6719 unsigned long *start, unsigned long *end)
6721 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
6722 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
6725 * vma needs to span at least one aligned PUD size, and the range
6726 * must be at least partially within in.
6728 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
6729 (*end <= v_start) || (*start >= v_end))
6732 /* Extend the range to be PUD aligned for a worst case scenario */
6733 if (*start > v_start)
6734 *start = ALIGN_DOWN(*start, PUD_SIZE);
6737 *end = ALIGN(*end, PUD_SIZE);
6741 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
6742 * and returns the corresponding pte. While this is not necessary for the
6743 * !shared pmd case because we can allocate the pmd later as well, it makes the
6744 * code much cleaner.
6746 * This routine must be called with i_mmap_rwsem held in at least read mode if
6747 * sharing is possible. For hugetlbfs, this prevents removal of any page
6748 * table entries associated with the address space. This is important as we
6749 * are setting up sharing based on existing page table entries (mappings).
6751 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
6752 unsigned long addr, pud_t *pud)
6754 struct address_space *mapping = vma->vm_file->f_mapping;
6755 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
6757 struct vm_area_struct *svma;
6758 unsigned long saddr;
6763 i_mmap_assert_locked(mapping);
6764 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
6768 saddr = page_table_shareable(svma, vma, addr, idx);
6770 spte = huge_pte_offset(svma->vm_mm, saddr,
6771 vma_mmu_pagesize(svma));
6773 get_page(virt_to_page(spte));
6782 ptl = huge_pte_lock(hstate_vma(vma), mm, spte);
6783 if (pud_none(*pud)) {
6784 pud_populate(mm, pud,
6785 (pmd_t *)((unsigned long)spte & PAGE_MASK));
6788 put_page(virt_to_page(spte));
6792 pte = (pte_t *)pmd_alloc(mm, pud, addr);
6797 * unmap huge page backed by shared pte.
6799 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
6800 * indicated by page_count > 1, unmap is achieved by clearing pud and
6801 * decrementing the ref count. If count == 1, the pte page is not shared.
6803 * Called with page table lock held and i_mmap_rwsem held in write mode.
6805 * returns: 1 successfully unmapped a shared pte page
6806 * 0 the underlying pte page is not shared, or it is the last user
6808 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
6809 unsigned long addr, pte_t *ptep)
6811 pgd_t *pgd = pgd_offset(mm, addr);
6812 p4d_t *p4d = p4d_offset(pgd, addr);
6813 pud_t *pud = pud_offset(p4d, addr);
6815 i_mmap_assert_write_locked(vma->vm_file->f_mapping);
6816 BUG_ON(page_count(virt_to_page(ptep)) == 0);
6817 if (page_count(virt_to_page(ptep)) == 1)
6821 put_page(virt_to_page(ptep));
6826 #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
6827 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
6828 unsigned long addr, pud_t *pud)
6833 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
6834 unsigned long addr, pte_t *ptep)
6839 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
6840 unsigned long *start, unsigned long *end)
6844 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
6848 #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
6850 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
6851 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
6852 unsigned long addr, unsigned long sz)
6859 pgd = pgd_offset(mm, addr);
6860 p4d = p4d_alloc(mm, pgd, addr);
6863 pud = pud_alloc(mm, p4d, addr);
6865 if (sz == PUD_SIZE) {
6868 BUG_ON(sz != PMD_SIZE);
6869 if (want_pmd_share(vma, addr) && pud_none(*pud))
6870 pte = huge_pmd_share(mm, vma, addr, pud);
6872 pte = (pte_t *)pmd_alloc(mm, pud, addr);
6875 BUG_ON(pte && pte_present(*pte) && !pte_huge(*pte));
6881 * huge_pte_offset() - Walk the page table to resolve the hugepage
6882 * entry at address @addr
6884 * Return: Pointer to page table entry (PUD or PMD) for
6885 * address @addr, or NULL if a !p*d_present() entry is encountered and the
6886 * size @sz doesn't match the hugepage size at this level of the page
6889 pte_t *huge_pte_offset(struct mm_struct *mm,
6890 unsigned long addr, unsigned long sz)
6897 pgd = pgd_offset(mm, addr);
6898 if (!pgd_present(*pgd))
6900 p4d = p4d_offset(pgd, addr);
6901 if (!p4d_present(*p4d))
6904 pud = pud_offset(p4d, addr);
6906 /* must be pud huge, non-present or none */
6907 return (pte_t *)pud;
6908 if (!pud_present(*pud))
6910 /* must have a valid entry and size to go further */
6912 pmd = pmd_offset(pud, addr);
6913 /* must be pmd huge, non-present or none */
6914 return (pte_t *)pmd;
6918 * Return a mask that can be used to update an address to the last huge
6919 * page in a page table page mapping size. Used to skip non-present
6920 * page table entries when linearly scanning address ranges. Architectures
6921 * with unique huge page to page table relationships can define their own
6922 * version of this routine.
6924 unsigned long hugetlb_mask_last_page(struct hstate *h)
6926 unsigned long hp_size = huge_page_size(h);
6928 if (hp_size == PUD_SIZE)
6929 return P4D_SIZE - PUD_SIZE;
6930 else if (hp_size == PMD_SIZE)
6931 return PUD_SIZE - PMD_SIZE;
6938 /* See description above. Architectures can provide their own version. */
6939 __weak unsigned long hugetlb_mask_last_page(struct hstate *h)
6941 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
6942 if (huge_page_size(h) == PMD_SIZE)
6943 return PUD_SIZE - PMD_SIZE;
6948 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
6951 * These functions are overwritable if your architecture needs its own
6954 struct page * __weak
6955 follow_huge_addr(struct mm_struct *mm, unsigned long address,
6958 return ERR_PTR(-EINVAL);
6961 struct page * __weak
6962 follow_huge_pd(struct vm_area_struct *vma,
6963 unsigned long address, hugepd_t hpd, int flags, int pdshift)
6965 WARN(1, "hugepd follow called with no support for hugepage directory format\n");
6969 struct page * __weak
6970 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
6971 pmd_t *pmd, int flags)
6973 struct page *page = NULL;
6978 * FOLL_PIN is not supported for follow_page(). Ordinary GUP goes via
6979 * follow_hugetlb_page().
6981 if (WARN_ON_ONCE(flags & FOLL_PIN))
6985 ptl = pmd_lockptr(mm, pmd);
6988 * make sure that the address range covered by this pmd is not
6989 * unmapped from other threads.
6991 if (!pmd_huge(*pmd))
6993 pte = huge_ptep_get((pte_t *)pmd);
6994 if (pte_present(pte)) {
6995 page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
6997 * try_grab_page() should always succeed here, because: a) we
6998 * hold the pmd (ptl) lock, and b) we've just checked that the
6999 * huge pmd (head) page is present in the page tables. The ptl
7000 * prevents the head page and tail pages from being rearranged
7001 * in any way. So this page must be available at this point,
7002 * unless the page refcount overflowed:
7004 if (WARN_ON_ONCE(!try_grab_page(page, flags))) {
7009 if (is_hugetlb_entry_migration(pte)) {
7011 __migration_entry_wait_huge((pte_t *)pmd, ptl);
7015 * hwpoisoned entry is treated as no_page_table in
7016 * follow_page_mask().
7024 struct page * __weak
7025 follow_huge_pud(struct mm_struct *mm, unsigned long address,
7026 pud_t *pud, int flags)
7028 struct page *page = NULL;
7032 if (WARN_ON_ONCE(flags & FOLL_PIN))
7036 ptl = huge_pte_lock(hstate_sizelog(PUD_SHIFT), mm, (pte_t *)pud);
7037 if (!pud_huge(*pud))
7039 pte = huge_ptep_get((pte_t *)pud);
7040 if (pte_present(pte)) {
7041 page = pud_page(*pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
7042 if (WARN_ON_ONCE(!try_grab_page(page, flags))) {
7047 if (is_hugetlb_entry_migration(pte)) {
7049 __migration_entry_wait(mm, (pte_t *)pud, ptl);
7053 * hwpoisoned entry is treated as no_page_table in
7054 * follow_page_mask().
7062 struct page * __weak
7063 follow_huge_pgd(struct mm_struct *mm, unsigned long address, pgd_t *pgd, int flags)
7065 if (flags & (FOLL_GET | FOLL_PIN))
7068 return pte_page(*(pte_t *)pgd) + ((address & ~PGDIR_MASK) >> PAGE_SHIFT);
7071 int isolate_hugetlb(struct page *page, struct list_head *list)
7075 spin_lock_irq(&hugetlb_lock);
7076 if (!PageHeadHuge(page) ||
7077 !HPageMigratable(page) ||
7078 !get_page_unless_zero(page)) {
7082 ClearHPageMigratable(page);
7083 list_move_tail(&page->lru, list);
7085 spin_unlock_irq(&hugetlb_lock);
7089 int get_hwpoison_huge_page(struct page *page, bool *hugetlb)
7094 spin_lock_irq(&hugetlb_lock);
7095 if (PageHeadHuge(page)) {
7097 if (HPageFreed(page))
7099 else if (HPageMigratable(page))
7100 ret = get_page_unless_zero(page);
7104 spin_unlock_irq(&hugetlb_lock);
7108 int get_huge_page_for_hwpoison(unsigned long pfn, int flags)
7112 spin_lock_irq(&hugetlb_lock);
7113 ret = __get_huge_page_for_hwpoison(pfn, flags);
7114 spin_unlock_irq(&hugetlb_lock);
7118 void putback_active_hugepage(struct page *page)
7120 spin_lock_irq(&hugetlb_lock);
7121 SetHPageMigratable(page);
7122 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
7123 spin_unlock_irq(&hugetlb_lock);
7127 void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
7129 struct hstate *h = page_hstate(oldpage);
7131 hugetlb_cgroup_migrate(oldpage, newpage);
7132 set_page_owner_migrate_reason(newpage, reason);
7135 * transfer temporary state of the new huge page. This is
7136 * reverse to other transitions because the newpage is going to
7137 * be final while the old one will be freed so it takes over
7138 * the temporary status.
7140 * Also note that we have to transfer the per-node surplus state
7141 * here as well otherwise the global surplus count will not match
7144 if (HPageTemporary(newpage)) {
7145 int old_nid = page_to_nid(oldpage);
7146 int new_nid = page_to_nid(newpage);
7148 SetHPageTemporary(oldpage);
7149 ClearHPageTemporary(newpage);
7152 * There is no need to transfer the per-node surplus state
7153 * when we do not cross the node.
7155 if (new_nid == old_nid)
7157 spin_lock_irq(&hugetlb_lock);
7158 if (h->surplus_huge_pages_node[old_nid]) {
7159 h->surplus_huge_pages_node[old_nid]--;
7160 h->surplus_huge_pages_node[new_nid]++;
7162 spin_unlock_irq(&hugetlb_lock);
7167 * This function will unconditionally remove all the shared pmd pgtable entries
7168 * within the specific vma for a hugetlbfs memory range.
7170 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
7172 struct hstate *h = hstate_vma(vma);
7173 unsigned long sz = huge_page_size(h);
7174 struct mm_struct *mm = vma->vm_mm;
7175 struct mmu_notifier_range range;
7176 unsigned long address, start, end;
7180 if (!(vma->vm_flags & VM_MAYSHARE))
7183 start = ALIGN(vma->vm_start, PUD_SIZE);
7184 end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
7189 flush_cache_range(vma, start, end);
7191 * No need to call adjust_range_if_pmd_sharing_possible(), because
7192 * we have already done the PUD_SIZE alignment.
7194 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm,
7196 mmu_notifier_invalidate_range_start(&range);
7197 i_mmap_lock_write(vma->vm_file->f_mapping);
7198 for (address = start; address < end; address += PUD_SIZE) {
7199 ptep = huge_pte_offset(mm, address, sz);
7202 ptl = huge_pte_lock(h, mm, ptep);
7203 huge_pmd_unshare(mm, vma, address, ptep);
7206 flush_hugetlb_tlb_range(vma, start, end);
7207 i_mmap_unlock_write(vma->vm_file->f_mapping);
7209 * No need to call mmu_notifier_invalidate_range(), see
7210 * Documentation/mm/mmu_notifier.rst.
7212 mmu_notifier_invalidate_range_end(&range);
7216 static bool cma_reserve_called __initdata;
7218 static int __init cmdline_parse_hugetlb_cma(char *p)
7225 if (sscanf(s, "%lu%n", &tmp, &count) != 1)
7228 if (s[count] == ':') {
7229 if (tmp >= MAX_NUMNODES)
7231 nid = array_index_nospec(tmp, MAX_NUMNODES);
7234 tmp = memparse(s, &s);
7235 hugetlb_cma_size_in_node[nid] = tmp;
7236 hugetlb_cma_size += tmp;
7239 * Skip the separator if have one, otherwise
7240 * break the parsing.
7247 hugetlb_cma_size = memparse(p, &p);
7255 early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
7257 void __init hugetlb_cma_reserve(int order)
7259 unsigned long size, reserved, per_node;
7260 bool node_specific_cma_alloc = false;
7263 cma_reserve_called = true;
7265 if (!hugetlb_cma_size)
7268 for (nid = 0; nid < MAX_NUMNODES; nid++) {
7269 if (hugetlb_cma_size_in_node[nid] == 0)
7272 if (!node_online(nid)) {
7273 pr_warn("hugetlb_cma: invalid node %d specified\n", nid);
7274 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7275 hugetlb_cma_size_in_node[nid] = 0;
7279 if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) {
7280 pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n",
7281 nid, (PAGE_SIZE << order) / SZ_1M);
7282 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7283 hugetlb_cma_size_in_node[nid] = 0;
7285 node_specific_cma_alloc = true;
7289 /* Validate the CMA size again in case some invalid nodes specified. */
7290 if (!hugetlb_cma_size)
7293 if (hugetlb_cma_size < (PAGE_SIZE << order)) {
7294 pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n",
7295 (PAGE_SIZE << order) / SZ_1M);
7296 hugetlb_cma_size = 0;
7300 if (!node_specific_cma_alloc) {
7302 * If 3 GB area is requested on a machine with 4 numa nodes,
7303 * let's allocate 1 GB on first three nodes and ignore the last one.
7305 per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes);
7306 pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
7307 hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
7311 for_each_online_node(nid) {
7313 char name[CMA_MAX_NAME];
7315 if (node_specific_cma_alloc) {
7316 if (hugetlb_cma_size_in_node[nid] == 0)
7319 size = hugetlb_cma_size_in_node[nid];
7321 size = min(per_node, hugetlb_cma_size - reserved);
7324 size = round_up(size, PAGE_SIZE << order);
7326 snprintf(name, sizeof(name), "hugetlb%d", nid);
7328 * Note that 'order per bit' is based on smallest size that
7329 * may be returned to CMA allocator in the case of
7330 * huge page demotion.
7332 res = cma_declare_contiguous_nid(0, size, 0,
7333 PAGE_SIZE << HUGETLB_PAGE_ORDER,
7335 &hugetlb_cma[nid], nid);
7337 pr_warn("hugetlb_cma: reservation failed: err %d, node %d",
7343 pr_info("hugetlb_cma: reserved %lu MiB on node %d\n",
7346 if (reserved >= hugetlb_cma_size)
7352 * hugetlb_cma_size is used to determine if allocations from
7353 * cma are possible. Set to zero if no cma regions are set up.
7355 hugetlb_cma_size = 0;
7358 static void __init hugetlb_cma_check(void)
7360 if (!hugetlb_cma_size || cma_reserve_called)
7363 pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
7366 #endif /* CONFIG_CMA */