Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / mm / hugetlb.c
blobea2ed8e301ef2c22dec0b0a32f9a1ff747a7c3b4
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic hugetlb support.
4 * (C) Nadia Yvette Chambers, April 2004
5 */
6 #include <linux/list.h>
7 #include <linux/init.h>
8 #include <linux/mm.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>
36 #include <linux/memory.h>
37 #include <linux/mm_inline.h>
38 #include <linux/padata.h>
40 #include <asm/page.h>
41 #include <asm/pgalloc.h>
42 #include <asm/tlb.h>
44 #include <linux/io.h>
45 #include <linux/hugetlb.h>
46 #include <linux/hugetlb_cgroup.h>
47 #include <linux/node.h>
48 #include <linux/page_owner.h>
49 #include "internal.h"
50 #include "hugetlb_vmemmap.h"
52 int hugetlb_max_hstate __read_mostly;
53 unsigned int default_hstate_idx;
54 struct hstate hstates[HUGE_MAX_HSTATE];
56 #ifdef CONFIG_CMA
57 static struct cma *hugetlb_cma[MAX_NUMNODES];
58 static unsigned long hugetlb_cma_size_in_node[MAX_NUMNODES] __initdata;
59 #endif
60 static unsigned long hugetlb_cma_size __initdata;
62 __initdata struct list_head huge_boot_pages[MAX_NUMNODES];
64 /* for command line parsing */
65 static struct hstate * __initdata parsed_hstate;
66 static unsigned long __initdata default_hstate_max_huge_pages;
67 static bool __initdata parsed_valid_hugepagesz = true;
68 static bool __initdata parsed_default_hugepagesz;
69 static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
72 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
73 * free_huge_pages, and surplus_huge_pages.
75 __cacheline_aligned_in_smp DEFINE_SPINLOCK(hugetlb_lock);
78 * Serializes faults on the same logical page. This is used to
79 * prevent spurious OOMs when the hugepage pool is fully utilized.
81 static int num_fault_mutexes __ro_after_init;
82 struct mutex *hugetlb_fault_mutex_table __ro_after_init;
84 /* Forward declaration */
85 static int hugetlb_acct_memory(struct hstate *h, long delta);
86 static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
87 static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
88 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
89 static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
90 unsigned long start, unsigned long end);
91 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
93 static void hugetlb_free_folio(struct folio *folio)
95 #ifdef CONFIG_CMA
96 int nid = folio_nid(folio);
98 if (cma_free_folio(hugetlb_cma[nid], folio))
99 return;
100 #endif
101 folio_put(folio);
104 static inline bool subpool_is_free(struct hugepage_subpool *spool)
106 if (spool->count)
107 return false;
108 if (spool->max_hpages != -1)
109 return spool->used_hpages == 0;
110 if (spool->min_hpages != -1)
111 return spool->rsv_hpages == spool->min_hpages;
113 return true;
116 static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
117 unsigned long irq_flags)
119 spin_unlock_irqrestore(&spool->lock, irq_flags);
121 /* If no pages are used, and no other handles to the subpool
122 * remain, give up any reservations based on minimum size and
123 * free the subpool */
124 if (subpool_is_free(spool)) {
125 if (spool->min_hpages != -1)
126 hugetlb_acct_memory(spool->hstate,
127 -spool->min_hpages);
128 kfree(spool);
132 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
133 long min_hpages)
135 struct hugepage_subpool *spool;
137 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
138 if (!spool)
139 return NULL;
141 spin_lock_init(&spool->lock);
142 spool->count = 1;
143 spool->max_hpages = max_hpages;
144 spool->hstate = h;
145 spool->min_hpages = min_hpages;
147 if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
148 kfree(spool);
149 return NULL;
151 spool->rsv_hpages = min_hpages;
153 return spool;
156 void hugepage_put_subpool(struct hugepage_subpool *spool)
158 unsigned long flags;
160 spin_lock_irqsave(&spool->lock, flags);
161 BUG_ON(!spool->count);
162 spool->count--;
163 unlock_or_release_subpool(spool, flags);
167 * Subpool accounting for allocating and reserving pages.
168 * Return -ENOMEM if there are not enough resources to satisfy the
169 * request. Otherwise, return the number of pages by which the
170 * global pools must be adjusted (upward). The returned value may
171 * only be different than the passed value (delta) in the case where
172 * a subpool minimum size must be maintained.
174 static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
175 long delta)
177 long ret = delta;
179 if (!spool)
180 return ret;
182 spin_lock_irq(&spool->lock);
184 if (spool->max_hpages != -1) { /* maximum size accounting */
185 if ((spool->used_hpages + delta) <= spool->max_hpages)
186 spool->used_hpages += delta;
187 else {
188 ret = -ENOMEM;
189 goto unlock_ret;
193 /* minimum size accounting */
194 if (spool->min_hpages != -1 && spool->rsv_hpages) {
195 if (delta > spool->rsv_hpages) {
197 * Asking for more reserves than those already taken on
198 * behalf of subpool. Return difference.
200 ret = delta - spool->rsv_hpages;
201 spool->rsv_hpages = 0;
202 } else {
203 ret = 0; /* reserves already accounted for */
204 spool->rsv_hpages -= delta;
208 unlock_ret:
209 spin_unlock_irq(&spool->lock);
210 return ret;
214 * Subpool accounting for freeing and unreserving pages.
215 * Return the number of global page reservations that must be dropped.
216 * The return value may only be different than the passed value (delta)
217 * in the case where a subpool minimum size must be maintained.
219 static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
220 long delta)
222 long ret = delta;
223 unsigned long flags;
225 if (!spool)
226 return delta;
228 spin_lock_irqsave(&spool->lock, flags);
230 if (spool->max_hpages != -1) /* maximum size accounting */
231 spool->used_hpages -= delta;
233 /* minimum size accounting */
234 if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
235 if (spool->rsv_hpages + delta <= spool->min_hpages)
236 ret = 0;
237 else
238 ret = spool->rsv_hpages + delta - spool->min_hpages;
240 spool->rsv_hpages += delta;
241 if (spool->rsv_hpages > spool->min_hpages)
242 spool->rsv_hpages = spool->min_hpages;
246 * If hugetlbfs_put_super couldn't free spool due to an outstanding
247 * quota reference, free it now.
249 unlock_or_release_subpool(spool, flags);
251 return ret;
254 static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
256 return HUGETLBFS_SB(inode->i_sb)->spool;
259 static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
261 return subpool_inode(file_inode(vma->vm_file));
265 * hugetlb vma_lock helper routines
267 void hugetlb_vma_lock_read(struct vm_area_struct *vma)
269 if (__vma_shareable_lock(vma)) {
270 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
272 down_read(&vma_lock->rw_sema);
273 } else if (__vma_private_lock(vma)) {
274 struct resv_map *resv_map = vma_resv_map(vma);
276 down_read(&resv_map->rw_sema);
280 void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
282 if (__vma_shareable_lock(vma)) {
283 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
285 up_read(&vma_lock->rw_sema);
286 } else if (__vma_private_lock(vma)) {
287 struct resv_map *resv_map = vma_resv_map(vma);
289 up_read(&resv_map->rw_sema);
293 void hugetlb_vma_lock_write(struct vm_area_struct *vma)
295 if (__vma_shareable_lock(vma)) {
296 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
298 down_write(&vma_lock->rw_sema);
299 } else if (__vma_private_lock(vma)) {
300 struct resv_map *resv_map = vma_resv_map(vma);
302 down_write(&resv_map->rw_sema);
306 void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
308 if (__vma_shareable_lock(vma)) {
309 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
311 up_write(&vma_lock->rw_sema);
312 } else if (__vma_private_lock(vma)) {
313 struct resv_map *resv_map = vma_resv_map(vma);
315 up_write(&resv_map->rw_sema);
319 int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
322 if (__vma_shareable_lock(vma)) {
323 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
325 return down_write_trylock(&vma_lock->rw_sema);
326 } else if (__vma_private_lock(vma)) {
327 struct resv_map *resv_map = vma_resv_map(vma);
329 return down_write_trylock(&resv_map->rw_sema);
332 return 1;
335 void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
337 if (__vma_shareable_lock(vma)) {
338 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
340 lockdep_assert_held(&vma_lock->rw_sema);
341 } else if (__vma_private_lock(vma)) {
342 struct resv_map *resv_map = vma_resv_map(vma);
344 lockdep_assert_held(&resv_map->rw_sema);
348 void hugetlb_vma_lock_release(struct kref *kref)
350 struct hugetlb_vma_lock *vma_lock = container_of(kref,
351 struct hugetlb_vma_lock, refs);
353 kfree(vma_lock);
356 static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
358 struct vm_area_struct *vma = vma_lock->vma;
361 * vma_lock structure may or not be released as a result of put,
362 * it certainly will no longer be attached to vma so clear pointer.
363 * Semaphore synchronizes access to vma_lock->vma field.
365 vma_lock->vma = NULL;
366 vma->vm_private_data = NULL;
367 up_write(&vma_lock->rw_sema);
368 kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
371 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
373 if (__vma_shareable_lock(vma)) {
374 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
376 __hugetlb_vma_unlock_write_put(vma_lock);
377 } else if (__vma_private_lock(vma)) {
378 struct resv_map *resv_map = vma_resv_map(vma);
380 /* no free for anon vmas, but still need to unlock */
381 up_write(&resv_map->rw_sema);
385 static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
388 * Only present in sharable vmas.
390 if (!vma || !__vma_shareable_lock(vma))
391 return;
393 if (vma->vm_private_data) {
394 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
396 down_write(&vma_lock->rw_sema);
397 __hugetlb_vma_unlock_write_put(vma_lock);
401 static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
403 struct hugetlb_vma_lock *vma_lock;
405 /* Only establish in (flags) sharable vmas */
406 if (!vma || !(vma->vm_flags & VM_MAYSHARE))
407 return;
409 /* Should never get here with non-NULL vm_private_data */
410 if (vma->vm_private_data)
411 return;
413 vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
414 if (!vma_lock) {
416 * If we can not allocate structure, then vma can not
417 * participate in pmd sharing. This is only a possible
418 * performance enhancement and memory saving issue.
419 * However, the lock is also used to synchronize page
420 * faults with truncation. If the lock is not present,
421 * unlikely races could leave pages in a file past i_size
422 * until the file is removed. Warn in the unlikely case of
423 * allocation failure.
425 pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
426 return;
429 kref_init(&vma_lock->refs);
430 init_rwsem(&vma_lock->rw_sema);
431 vma_lock->vma = vma;
432 vma->vm_private_data = vma_lock;
435 /* Helper that removes a struct file_region from the resv_map cache and returns
436 * it for use.
438 static struct file_region *
439 get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
441 struct file_region *nrg;
443 VM_BUG_ON(resv->region_cache_count <= 0);
445 resv->region_cache_count--;
446 nrg = list_first_entry(&resv->region_cache, struct file_region, link);
447 list_del(&nrg->link);
449 nrg->from = from;
450 nrg->to = to;
452 return nrg;
455 static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
456 struct file_region *rg)
458 #ifdef CONFIG_CGROUP_HUGETLB
459 nrg->reservation_counter = rg->reservation_counter;
460 nrg->css = rg->css;
461 if (rg->css)
462 css_get(rg->css);
463 #endif
466 /* Helper that records hugetlb_cgroup uncharge info. */
467 static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
468 struct hstate *h,
469 struct resv_map *resv,
470 struct file_region *nrg)
472 #ifdef CONFIG_CGROUP_HUGETLB
473 if (h_cg) {
474 nrg->reservation_counter =
475 &h_cg->rsvd_hugepage[hstate_index(h)];
476 nrg->css = &h_cg->css;
478 * The caller will hold exactly one h_cg->css reference for the
479 * whole contiguous reservation region. But this area might be
480 * scattered when there are already some file_regions reside in
481 * it. As a result, many file_regions may share only one css
482 * reference. In order to ensure that one file_region must hold
483 * exactly one h_cg->css reference, we should do css_get for
484 * each file_region and leave the reference held by caller
485 * untouched.
487 css_get(&h_cg->css);
488 if (!resv->pages_per_hpage)
489 resv->pages_per_hpage = pages_per_huge_page(h);
490 /* pages_per_hpage should be the same for all entries in
491 * a resv_map.
493 VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
494 } else {
495 nrg->reservation_counter = NULL;
496 nrg->css = NULL;
498 #endif
501 static void put_uncharge_info(struct file_region *rg)
503 #ifdef CONFIG_CGROUP_HUGETLB
504 if (rg->css)
505 css_put(rg->css);
506 #endif
509 static bool has_same_uncharge_info(struct file_region *rg,
510 struct file_region *org)
512 #ifdef CONFIG_CGROUP_HUGETLB
513 return rg->reservation_counter == org->reservation_counter &&
514 rg->css == org->css;
516 #else
517 return true;
518 #endif
521 static void coalesce_file_region(struct resv_map *resv, struct file_region *rg)
523 struct file_region *nrg, *prg;
525 prg = list_prev_entry(rg, link);
526 if (&prg->link != &resv->regions && prg->to == rg->from &&
527 has_same_uncharge_info(prg, rg)) {
528 prg->to = rg->to;
530 list_del(&rg->link);
531 put_uncharge_info(rg);
532 kfree(rg);
534 rg = prg;
537 nrg = list_next_entry(rg, link);
538 if (&nrg->link != &resv->regions && nrg->from == rg->to &&
539 has_same_uncharge_info(nrg, rg)) {
540 nrg->from = rg->from;
542 list_del(&rg->link);
543 put_uncharge_info(rg);
544 kfree(rg);
548 static inline long
549 hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from,
550 long to, struct hstate *h, struct hugetlb_cgroup *cg,
551 long *regions_needed)
553 struct file_region *nrg;
555 if (!regions_needed) {
556 nrg = get_file_region_entry_from_cache(map, from, to);
557 record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
558 list_add(&nrg->link, rg);
559 coalesce_file_region(map, nrg);
560 } else
561 *regions_needed += 1;
563 return to - from;
567 * Must be called with resv->lock held.
569 * Calling this with regions_needed != NULL will count the number of pages
570 * to be added but will not modify the linked list. And regions_needed will
571 * indicate the number of file_regions needed in the cache to carry out to add
572 * the regions for this range.
574 static long add_reservation_in_range(struct resv_map *resv, long f, long t,
575 struct hugetlb_cgroup *h_cg,
576 struct hstate *h, long *regions_needed)
578 long add = 0;
579 struct list_head *head = &resv->regions;
580 long last_accounted_offset = f;
581 struct file_region *iter, *trg = NULL;
582 struct list_head *rg = NULL;
584 if (regions_needed)
585 *regions_needed = 0;
587 /* In this loop, we essentially handle an entry for the range
588 * [last_accounted_offset, iter->from), at every iteration, with some
589 * bounds checking.
591 list_for_each_entry_safe(iter, trg, head, link) {
592 /* Skip irrelevant regions that start before our range. */
593 if (iter->from < f) {
594 /* If this region ends after the last accounted offset,
595 * then we need to update last_accounted_offset.
597 if (iter->to > last_accounted_offset)
598 last_accounted_offset = iter->to;
599 continue;
602 /* When we find a region that starts beyond our range, we've
603 * finished.
605 if (iter->from >= t) {
606 rg = iter->link.prev;
607 break;
610 /* Add an entry for last_accounted_offset -> iter->from, and
611 * update last_accounted_offset.
613 if (iter->from > last_accounted_offset)
614 add += hugetlb_resv_map_add(resv, iter->link.prev,
615 last_accounted_offset,
616 iter->from, h, h_cg,
617 regions_needed);
619 last_accounted_offset = iter->to;
622 /* Handle the case where our range extends beyond
623 * last_accounted_offset.
625 if (!rg)
626 rg = head->prev;
627 if (last_accounted_offset < t)
628 add += hugetlb_resv_map_add(resv, rg, last_accounted_offset,
629 t, h, h_cg, regions_needed);
631 return add;
634 /* Must be called with resv->lock acquired. Will drop lock to allocate entries.
636 static int allocate_file_region_entries(struct resv_map *resv,
637 int regions_needed)
638 __must_hold(&resv->lock)
640 LIST_HEAD(allocated_regions);
641 int to_allocate = 0, i = 0;
642 struct file_region *trg = NULL, *rg = NULL;
644 VM_BUG_ON(regions_needed < 0);
647 * Check for sufficient descriptors in the cache to accommodate
648 * the number of in progress add operations plus regions_needed.
650 * This is a while loop because when we drop the lock, some other call
651 * to region_add or region_del may have consumed some region_entries,
652 * so we keep looping here until we finally have enough entries for
653 * (adds_in_progress + regions_needed).
655 while (resv->region_cache_count <
656 (resv->adds_in_progress + regions_needed)) {
657 to_allocate = resv->adds_in_progress + regions_needed -
658 resv->region_cache_count;
660 /* At this point, we should have enough entries in the cache
661 * for all the existing adds_in_progress. We should only be
662 * needing to allocate for regions_needed.
664 VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
666 spin_unlock(&resv->lock);
667 for (i = 0; i < to_allocate; i++) {
668 trg = kmalloc(sizeof(*trg), GFP_KERNEL);
669 if (!trg)
670 goto out_of_memory;
671 list_add(&trg->link, &allocated_regions);
674 spin_lock(&resv->lock);
676 list_splice(&allocated_regions, &resv->region_cache);
677 resv->region_cache_count += to_allocate;
680 return 0;
682 out_of_memory:
683 list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
684 list_del(&rg->link);
685 kfree(rg);
687 return -ENOMEM;
691 * Add the huge page range represented by [f, t) to the reserve
692 * map. Regions will be taken from the cache to fill in this range.
693 * Sufficient regions should exist in the cache due to the previous
694 * call to region_chg with the same range, but in some cases the cache will not
695 * have sufficient entries due to races with other code doing region_add or
696 * region_del. The extra needed entries will be allocated.
698 * regions_needed is the out value provided by a previous call to region_chg.
700 * Return the number of new huge pages added to the map. This number is greater
701 * than or equal to zero. If file_region entries needed to be allocated for
702 * this operation and we were not able to allocate, it returns -ENOMEM.
703 * region_add of regions of length 1 never allocate file_regions and cannot
704 * fail; region_chg will always allocate at least 1 entry and a region_add for
705 * 1 page will only require at most 1 entry.
707 static long region_add(struct resv_map *resv, long f, long t,
708 long in_regions_needed, struct hstate *h,
709 struct hugetlb_cgroup *h_cg)
711 long add = 0, actual_regions_needed = 0;
713 spin_lock(&resv->lock);
714 retry:
716 /* Count how many regions are actually needed to execute this add. */
717 add_reservation_in_range(resv, f, t, NULL, NULL,
718 &actual_regions_needed);
721 * Check for sufficient descriptors in the cache to accommodate
722 * this add operation. Note that actual_regions_needed may be greater
723 * than in_regions_needed, as the resv_map may have been modified since
724 * the region_chg call. In this case, we need to make sure that we
725 * allocate extra entries, such that we have enough for all the
726 * existing adds_in_progress, plus the excess needed for this
727 * operation.
729 if (actual_regions_needed > in_regions_needed &&
730 resv->region_cache_count <
731 resv->adds_in_progress +
732 (actual_regions_needed - in_regions_needed)) {
733 /* region_add operation of range 1 should never need to
734 * allocate file_region entries.
736 VM_BUG_ON(t - f <= 1);
738 if (allocate_file_region_entries(
739 resv, actual_regions_needed - in_regions_needed)) {
740 return -ENOMEM;
743 goto retry;
746 add = add_reservation_in_range(resv, f, t, h_cg, h, NULL);
748 resv->adds_in_progress -= in_regions_needed;
750 spin_unlock(&resv->lock);
751 return add;
755 * Examine the existing reserve map and determine how many
756 * huge pages in the specified range [f, t) are NOT currently
757 * represented. This routine is called before a subsequent
758 * call to region_add that will actually modify the reserve
759 * map to add the specified range [f, t). region_chg does
760 * not change the number of huge pages represented by the
761 * map. A number of new file_region structures is added to the cache as a
762 * placeholder, for the subsequent region_add call to use. At least 1
763 * file_region structure is added.
765 * out_regions_needed is the number of regions added to the
766 * resv->adds_in_progress. This value needs to be provided to a follow up call
767 * to region_add or region_abort for proper accounting.
769 * Returns the number of huge pages that need to be added to the existing
770 * reservation map for the range [f, t). This number is greater or equal to
771 * zero. -ENOMEM is returned if a new file_region structure or cache entry
772 * is needed and can not be allocated.
774 static long region_chg(struct resv_map *resv, long f, long t,
775 long *out_regions_needed)
777 long chg = 0;
779 spin_lock(&resv->lock);
781 /* Count how many hugepages in this range are NOT represented. */
782 chg = add_reservation_in_range(resv, f, t, NULL, NULL,
783 out_regions_needed);
785 if (*out_regions_needed == 0)
786 *out_regions_needed = 1;
788 if (allocate_file_region_entries(resv, *out_regions_needed))
789 return -ENOMEM;
791 resv->adds_in_progress += *out_regions_needed;
793 spin_unlock(&resv->lock);
794 return chg;
798 * Abort the in progress add operation. The adds_in_progress field
799 * of the resv_map keeps track of the operations in progress between
800 * calls to region_chg and region_add. Operations are sometimes
801 * aborted after the call to region_chg. In such cases, region_abort
802 * is called to decrement the adds_in_progress counter. regions_needed
803 * is the value returned by the region_chg call, it is used to decrement
804 * the adds_in_progress counter.
806 * NOTE: The range arguments [f, t) are not needed or used in this
807 * routine. They are kept to make reading the calling code easier as
808 * arguments will match the associated region_chg call.
810 static void region_abort(struct resv_map *resv, long f, long t,
811 long regions_needed)
813 spin_lock(&resv->lock);
814 VM_BUG_ON(!resv->region_cache_count);
815 resv->adds_in_progress -= regions_needed;
816 spin_unlock(&resv->lock);
820 * Delete the specified range [f, t) from the reserve map. If the
821 * t parameter is LONG_MAX, this indicates that ALL regions after f
822 * should be deleted. Locate the regions which intersect [f, t)
823 * and either trim, delete or split the existing regions.
825 * Returns the number of huge pages deleted from the reserve map.
826 * In the normal case, the return value is zero or more. In the
827 * case where a region must be split, a new region descriptor must
828 * be allocated. If the allocation fails, -ENOMEM will be returned.
829 * NOTE: If the parameter t == LONG_MAX, then we will never split
830 * a region and possibly return -ENOMEM. Callers specifying
831 * t == LONG_MAX do not need to check for -ENOMEM error.
833 static long region_del(struct resv_map *resv, long f, long t)
835 struct list_head *head = &resv->regions;
836 struct file_region *rg, *trg;
837 struct file_region *nrg = NULL;
838 long del = 0;
840 retry:
841 spin_lock(&resv->lock);
842 list_for_each_entry_safe(rg, trg, head, link) {
844 * Skip regions before the range to be deleted. file_region
845 * ranges are normally of the form [from, to). However, there
846 * may be a "placeholder" entry in the map which is of the form
847 * (from, to) with from == to. Check for placeholder entries
848 * at the beginning of the range to be deleted.
850 if (rg->to <= f && (rg->to != rg->from || rg->to != f))
851 continue;
853 if (rg->from >= t)
854 break;
856 if (f > rg->from && t < rg->to) { /* Must split region */
858 * Check for an entry in the cache before dropping
859 * lock and attempting allocation.
861 if (!nrg &&
862 resv->region_cache_count > resv->adds_in_progress) {
863 nrg = list_first_entry(&resv->region_cache,
864 struct file_region,
865 link);
866 list_del(&nrg->link);
867 resv->region_cache_count--;
870 if (!nrg) {
871 spin_unlock(&resv->lock);
872 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
873 if (!nrg)
874 return -ENOMEM;
875 goto retry;
878 del += t - f;
879 hugetlb_cgroup_uncharge_file_region(
880 resv, rg, t - f, false);
882 /* New entry for end of split region */
883 nrg->from = t;
884 nrg->to = rg->to;
886 copy_hugetlb_cgroup_uncharge_info(nrg, rg);
888 INIT_LIST_HEAD(&nrg->link);
890 /* Original entry is trimmed */
891 rg->to = f;
893 list_add(&nrg->link, &rg->link);
894 nrg = NULL;
895 break;
898 if (f <= rg->from && t >= rg->to) { /* Remove entire region */
899 del += rg->to - rg->from;
900 hugetlb_cgroup_uncharge_file_region(resv, rg,
901 rg->to - rg->from, true);
902 list_del(&rg->link);
903 kfree(rg);
904 continue;
907 if (f <= rg->from) { /* Trim beginning of region */
908 hugetlb_cgroup_uncharge_file_region(resv, rg,
909 t - rg->from, false);
911 del += t - rg->from;
912 rg->from = t;
913 } else { /* Trim end of region */
914 hugetlb_cgroup_uncharge_file_region(resv, rg,
915 rg->to - f, false);
917 del += rg->to - f;
918 rg->to = f;
922 spin_unlock(&resv->lock);
923 kfree(nrg);
924 return del;
928 * A rare out of memory error was encountered which prevented removal of
929 * the reserve map region for a page. The huge page itself was free'ed
930 * and removed from the page cache. This routine will adjust the subpool
931 * usage count, and the global reserve count if needed. By incrementing
932 * these counts, the reserve map entry which could not be deleted will
933 * appear as a "reserved" entry instead of simply dangling with incorrect
934 * counts.
936 void hugetlb_fix_reserve_counts(struct inode *inode)
938 struct hugepage_subpool *spool = subpool_inode(inode);
939 long rsv_adjust;
940 bool reserved = false;
942 rsv_adjust = hugepage_subpool_get_pages(spool, 1);
943 if (rsv_adjust > 0) {
944 struct hstate *h = hstate_inode(inode);
946 if (!hugetlb_acct_memory(h, 1))
947 reserved = true;
948 } else if (!rsv_adjust) {
949 reserved = true;
952 if (!reserved)
953 pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
957 * Count and return the number of huge pages in the reserve map
958 * that intersect with the range [f, t).
960 static long region_count(struct resv_map *resv, long f, long t)
962 struct list_head *head = &resv->regions;
963 struct file_region *rg;
964 long chg = 0;
966 spin_lock(&resv->lock);
967 /* Locate each segment we overlap with, and count that overlap. */
968 list_for_each_entry(rg, head, link) {
969 long seg_from;
970 long seg_to;
972 if (rg->to <= f)
973 continue;
974 if (rg->from >= t)
975 break;
977 seg_from = max(rg->from, f);
978 seg_to = min(rg->to, t);
980 chg += seg_to - seg_from;
982 spin_unlock(&resv->lock);
984 return chg;
988 * Convert the address within this vma to the page offset within
989 * the mapping, huge page units here.
991 static pgoff_t vma_hugecache_offset(struct hstate *h,
992 struct vm_area_struct *vma, unsigned long address)
994 return ((address - vma->vm_start) >> huge_page_shift(h)) +
995 (vma->vm_pgoff >> huge_page_order(h));
999 * vma_kernel_pagesize - Page size granularity for this VMA.
1000 * @vma: The user mapping.
1002 * Folios in this VMA will be aligned to, and at least the size of the
1003 * number of bytes returned by this function.
1005 * Return: The default size of the folios allocated when backing a VMA.
1007 unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1009 if (vma->vm_ops && vma->vm_ops->pagesize)
1010 return vma->vm_ops->pagesize(vma);
1011 return PAGE_SIZE;
1013 EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
1016 * Return the page size being used by the MMU to back a VMA. In the majority
1017 * of cases, the page size used by the kernel matches the MMU size. On
1018 * architectures where it differs, an architecture-specific 'strong'
1019 * version of this symbol is required.
1021 __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1023 return vma_kernel_pagesize(vma);
1027 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
1028 * bits of the reservation map pointer, which are always clear due to
1029 * alignment.
1031 #define HPAGE_RESV_OWNER (1UL << 0)
1032 #define HPAGE_RESV_UNMAPPED (1UL << 1)
1033 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
1036 * These helpers are used to track how many pages are reserved for
1037 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
1038 * is guaranteed to have their future faults succeed.
1040 * With the exception of hugetlb_dup_vma_private() which is called at fork(),
1041 * the reserve counters are updated with the hugetlb_lock held. It is safe
1042 * to reset the VMA at fork() time as it is not in use yet and there is no
1043 * chance of the global counters getting corrupted as a result of the values.
1045 * The private mapping reservation is represented in a subtly different
1046 * manner to a shared mapping. A shared mapping has a region map associated
1047 * with the underlying file, this region map represents the backing file
1048 * pages which have ever had a reservation assigned which this persists even
1049 * after the page is instantiated. A private mapping has a region map
1050 * associated with the original mmap which is attached to all VMAs which
1051 * reference it, this region map represents those offsets which have consumed
1052 * reservation ie. where pages have been instantiated.
1054 static unsigned long get_vma_private_data(struct vm_area_struct *vma)
1056 return (unsigned long)vma->vm_private_data;
1059 static void set_vma_private_data(struct vm_area_struct *vma,
1060 unsigned long value)
1062 vma->vm_private_data = (void *)value;
1065 static void
1066 resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
1067 struct hugetlb_cgroup *h_cg,
1068 struct hstate *h)
1070 #ifdef CONFIG_CGROUP_HUGETLB
1071 if (!h_cg || !h) {
1072 resv_map->reservation_counter = NULL;
1073 resv_map->pages_per_hpage = 0;
1074 resv_map->css = NULL;
1075 } else {
1076 resv_map->reservation_counter =
1077 &h_cg->rsvd_hugepage[hstate_index(h)];
1078 resv_map->pages_per_hpage = pages_per_huge_page(h);
1079 resv_map->css = &h_cg->css;
1081 #endif
1084 struct resv_map *resv_map_alloc(void)
1086 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
1087 struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
1089 if (!resv_map || !rg) {
1090 kfree(resv_map);
1091 kfree(rg);
1092 return NULL;
1095 kref_init(&resv_map->refs);
1096 spin_lock_init(&resv_map->lock);
1097 INIT_LIST_HEAD(&resv_map->regions);
1098 init_rwsem(&resv_map->rw_sema);
1100 resv_map->adds_in_progress = 0;
1102 * Initialize these to 0. On shared mappings, 0's here indicate these
1103 * fields don't do cgroup accounting. On private mappings, these will be
1104 * re-initialized to the proper values, to indicate that hugetlb cgroup
1105 * reservations are to be un-charged from here.
1107 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
1109 INIT_LIST_HEAD(&resv_map->region_cache);
1110 list_add(&rg->link, &resv_map->region_cache);
1111 resv_map->region_cache_count = 1;
1113 return resv_map;
1116 void resv_map_release(struct kref *ref)
1118 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
1119 struct list_head *head = &resv_map->region_cache;
1120 struct file_region *rg, *trg;
1122 /* Clear out any active regions before we release the map. */
1123 region_del(resv_map, 0, LONG_MAX);
1125 /* ... and any entries left in the cache */
1126 list_for_each_entry_safe(rg, trg, head, link) {
1127 list_del(&rg->link);
1128 kfree(rg);
1131 VM_BUG_ON(resv_map->adds_in_progress);
1133 kfree(resv_map);
1136 static inline struct resv_map *inode_resv_map(struct inode *inode)
1139 * At inode evict time, i_mapping may not point to the original
1140 * address space within the inode. This original address space
1141 * contains the pointer to the resv_map. So, always use the
1142 * address space embedded within the inode.
1143 * The VERY common case is inode->mapping == &inode->i_data but,
1144 * this may not be true for device special inodes.
1146 return (struct resv_map *)(&inode->i_data)->i_private_data;
1149 static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
1151 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1152 if (vma->vm_flags & VM_MAYSHARE) {
1153 struct address_space *mapping = vma->vm_file->f_mapping;
1154 struct inode *inode = mapping->host;
1156 return inode_resv_map(inode);
1158 } else {
1159 return (struct resv_map *)(get_vma_private_data(vma) &
1160 ~HPAGE_RESV_MASK);
1164 static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
1166 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1167 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
1169 set_vma_private_data(vma, (unsigned long)map);
1172 static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
1174 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1175 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
1177 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
1180 static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1182 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1184 return (get_vma_private_data(vma) & flag) != 0;
1187 bool __vma_private_lock(struct vm_area_struct *vma)
1189 return !(vma->vm_flags & VM_MAYSHARE) &&
1190 get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
1191 is_vma_resv_set(vma, HPAGE_RESV_OWNER);
1194 void hugetlb_dup_vma_private(struct vm_area_struct *vma)
1196 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1198 * Clear vm_private_data
1199 * - For shared mappings this is a per-vma semaphore that may be
1200 * allocated in a subsequent call to hugetlb_vm_op_open.
1201 * Before clearing, make sure pointer is not associated with vma
1202 * as this will leak the structure. This is the case when called
1203 * via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already
1204 * been called to allocate a new structure.
1205 * - For MAP_PRIVATE mappings, this is the reserve map which does
1206 * not apply to children. Faults generated by the children are
1207 * not guaranteed to succeed, even if read-only.
1209 if (vma->vm_flags & VM_MAYSHARE) {
1210 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1212 if (vma_lock && vma_lock->vma != vma)
1213 vma->vm_private_data = NULL;
1214 } else
1215 vma->vm_private_data = NULL;
1219 * Reset and decrement one ref on hugepage private reservation.
1220 * Called with mm->mmap_lock writer semaphore held.
1221 * This function should be only used by move_vma() and operate on
1222 * same sized vma. It should never come here with last ref on the
1223 * reservation.
1225 void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1228 * Clear the old hugetlb private page reservation.
1229 * It has already been transferred to new_vma.
1231 * During a mremap() operation of a hugetlb vma we call move_vma()
1232 * which copies vma into new_vma and unmaps vma. After the copy
1233 * operation both new_vma and vma share a reference to the resv_map
1234 * struct, and at that point vma is about to be unmapped. We don't
1235 * want to return the reservation to the pool at unmap of vma because
1236 * the reservation still lives on in new_vma, so simply decrement the
1237 * ref here and remove the resv_map reference from this vma.
1239 struct resv_map *reservations = vma_resv_map(vma);
1241 if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1242 resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
1243 kref_put(&reservations->refs, resv_map_release);
1246 hugetlb_dup_vma_private(vma);
1249 /* Returns true if the VMA has associated reserve pages */
1250 static bool vma_has_reserves(struct vm_area_struct *vma, long chg)
1252 if (vma->vm_flags & VM_NORESERVE) {
1254 * This address is already reserved by other process(chg == 0),
1255 * so, we should decrement reserved count. Without decrementing,
1256 * reserve count remains after releasing inode, because this
1257 * allocated page will go into page cache and is regarded as
1258 * coming from reserved pool in releasing step. Currently, we
1259 * don't have any other solution to deal with this situation
1260 * properly, so add work-around here.
1262 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
1263 return true;
1264 else
1265 return false;
1268 /* Shared mappings always use reserves */
1269 if (vma->vm_flags & VM_MAYSHARE) {
1271 * We know VM_NORESERVE is not set. Therefore, there SHOULD
1272 * be a region map for all pages. The only situation where
1273 * there is no region map is if a hole was punched via
1274 * fallocate. In this case, there really are no reserves to
1275 * use. This situation is indicated if chg != 0.
1277 if (chg)
1278 return false;
1279 else
1280 return true;
1284 * Only the process that called mmap() has reserves for
1285 * private mappings.
1287 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1289 * Like the shared case above, a hole punch or truncate
1290 * could have been performed on the private mapping.
1291 * Examine the value of chg to determine if reserves
1292 * actually exist or were previously consumed.
1293 * Very Subtle - The value of chg comes from a previous
1294 * call to vma_needs_reserves(). The reserve map for
1295 * private mappings has different (opposite) semantics
1296 * than that of shared mappings. vma_needs_reserves()
1297 * has already taken this difference in semantics into
1298 * account. Therefore, the meaning of chg is the same
1299 * as in the shared case above. Code could easily be
1300 * combined, but keeping it separate draws attention to
1301 * subtle differences.
1303 if (chg)
1304 return false;
1305 else
1306 return true;
1309 return false;
1312 static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio)
1314 int nid = folio_nid(folio);
1316 lockdep_assert_held(&hugetlb_lock);
1317 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1319 list_move(&folio->lru, &h->hugepage_freelists[nid]);
1320 h->free_huge_pages++;
1321 h->free_huge_pages_node[nid]++;
1322 folio_set_hugetlb_freed(folio);
1325 static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,
1326 int nid)
1328 struct folio *folio;
1329 bool pin = !!(current->flags & PF_MEMALLOC_PIN);
1331 lockdep_assert_held(&hugetlb_lock);
1332 list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) {
1333 if (pin && !folio_is_longterm_pinnable(folio))
1334 continue;
1336 if (folio_test_hwpoison(folio))
1337 continue;
1339 list_move(&folio->lru, &h->hugepage_activelist);
1340 folio_ref_unfreeze(folio, 1);
1341 folio_clear_hugetlb_freed(folio);
1342 h->free_huge_pages--;
1343 h->free_huge_pages_node[nid]--;
1344 return folio;
1347 return NULL;
1350 static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask,
1351 int nid, nodemask_t *nmask)
1353 unsigned int cpuset_mems_cookie;
1354 struct zonelist *zonelist;
1355 struct zone *zone;
1356 struct zoneref *z;
1357 int node = NUMA_NO_NODE;
1359 /* 'nid' should not be NUMA_NO_NODE. Try to catch any misuse of it and rectifiy. */
1360 if (nid == NUMA_NO_NODE)
1361 nid = numa_node_id();
1363 zonelist = node_zonelist(nid, gfp_mask);
1365 retry_cpuset:
1366 cpuset_mems_cookie = read_mems_allowed_begin();
1367 for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
1368 struct folio *folio;
1370 if (!cpuset_zone_allowed(zone, gfp_mask))
1371 continue;
1373 * no need to ask again on the same node. Pool is node rather than
1374 * zone aware
1376 if (zone_to_nid(zone) == node)
1377 continue;
1378 node = zone_to_nid(zone);
1380 folio = dequeue_hugetlb_folio_node_exact(h, node);
1381 if (folio)
1382 return folio;
1384 if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1385 goto retry_cpuset;
1387 return NULL;
1390 static unsigned long available_huge_pages(struct hstate *h)
1392 return h->free_huge_pages - h->resv_huge_pages;
1395 static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h,
1396 struct vm_area_struct *vma,
1397 unsigned long address, int avoid_reserve,
1398 long chg)
1400 struct folio *folio = NULL;
1401 struct mempolicy *mpol;
1402 gfp_t gfp_mask;
1403 nodemask_t *nodemask;
1404 int nid;
1407 * A child process with MAP_PRIVATE mappings created by their parent
1408 * have no page reserves. This check ensures that reservations are
1409 * not "stolen". The child may still get SIGKILLed
1411 if (!vma_has_reserves(vma, chg) && !available_huge_pages(h))
1412 goto err;
1414 /* If reserves cannot be used, ensure enough pages are in the pool */
1415 if (avoid_reserve && !available_huge_pages(h))
1416 goto err;
1418 gfp_mask = htlb_alloc_mask(h);
1419 nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1421 if (mpol_is_preferred_many(mpol)) {
1422 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1423 nid, nodemask);
1425 /* Fallback to all nodes if page==NULL */
1426 nodemask = NULL;
1429 if (!folio)
1430 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1431 nid, nodemask);
1433 if (folio && !avoid_reserve && vma_has_reserves(vma, chg)) {
1434 folio_set_hugetlb_restore_reserve(folio);
1435 h->resv_huge_pages--;
1438 mpol_cond_put(mpol);
1439 return folio;
1441 err:
1442 return NULL;
1446 * common helper functions for hstate_next_node_to_{alloc|free}.
1447 * We may have allocated or freed a huge page based on a different
1448 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1449 * be outside of *nodes_allowed. Ensure that we use an allowed
1450 * node for alloc or free.
1452 static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1454 nid = next_node_in(nid, *nodes_allowed);
1455 VM_BUG_ON(nid >= MAX_NUMNODES);
1457 return nid;
1460 static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1462 if (!node_isset(nid, *nodes_allowed))
1463 nid = next_node_allowed(nid, nodes_allowed);
1464 return nid;
1468 * returns the previously saved node ["this node"] from which to
1469 * allocate a persistent huge page for the pool and advance the
1470 * next node from which to allocate, handling wrap at end of node
1471 * mask.
1473 static int hstate_next_node_to_alloc(int *next_node,
1474 nodemask_t *nodes_allowed)
1476 int nid;
1478 VM_BUG_ON(!nodes_allowed);
1480 nid = get_valid_node_allowed(*next_node, nodes_allowed);
1481 *next_node = next_node_allowed(nid, nodes_allowed);
1483 return nid;
1487 * helper for remove_pool_hugetlb_folio() - return the previously saved
1488 * node ["this node"] from which to free a huge page. Advance the
1489 * next node id whether or not we find a free huge page to free so
1490 * that the next attempt to free addresses the next node.
1492 static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1494 int nid;
1496 VM_BUG_ON(!nodes_allowed);
1498 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1499 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1501 return nid;
1504 #define for_each_node_mask_to_alloc(next_node, nr_nodes, node, mask) \
1505 for (nr_nodes = nodes_weight(*mask); \
1506 nr_nodes > 0 && \
1507 ((node = hstate_next_node_to_alloc(next_node, mask)) || 1); \
1508 nr_nodes--)
1510 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
1511 for (nr_nodes = nodes_weight(*mask); \
1512 nr_nodes > 0 && \
1513 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
1514 nr_nodes--)
1516 #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1517 #ifdef CONFIG_CONTIG_ALLOC
1518 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1519 int nid, nodemask_t *nodemask)
1521 struct folio *folio;
1522 int order = huge_page_order(h);
1523 bool retried = false;
1525 if (nid == NUMA_NO_NODE)
1526 nid = numa_mem_id();
1527 retry:
1528 folio = NULL;
1529 #ifdef CONFIG_CMA
1531 int node;
1533 if (hugetlb_cma[nid])
1534 folio = cma_alloc_folio(hugetlb_cma[nid], order, gfp_mask);
1536 if (!folio && !(gfp_mask & __GFP_THISNODE)) {
1537 for_each_node_mask(node, *nodemask) {
1538 if (node == nid || !hugetlb_cma[node])
1539 continue;
1541 folio = cma_alloc_folio(hugetlb_cma[node], order, gfp_mask);
1542 if (folio)
1543 break;
1547 #endif
1548 if (!folio) {
1549 folio = folio_alloc_gigantic(order, gfp_mask, nid, nodemask);
1550 if (!folio)
1551 return NULL;
1554 if (folio_ref_freeze(folio, 1))
1555 return folio;
1557 pr_warn("HugeTLB: unexpected refcount on PFN %lu\n", folio_pfn(folio));
1558 hugetlb_free_folio(folio);
1559 if (!retried) {
1560 retried = true;
1561 goto retry;
1563 return NULL;
1566 #else /* !CONFIG_CONTIG_ALLOC */
1567 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1568 int nid, nodemask_t *nodemask)
1570 return NULL;
1572 #endif /* CONFIG_CONTIG_ALLOC */
1574 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1575 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1576 int nid, nodemask_t *nodemask)
1578 return NULL;
1580 #endif
1583 * Remove hugetlb folio from lists.
1584 * If vmemmap exists for the folio, clear the hugetlb flag so that the
1585 * folio appears as just a compound page. Otherwise, wait until after
1586 * allocating vmemmap to clear the flag.
1588 * Must be called with hugetlb lock held.
1590 static void remove_hugetlb_folio(struct hstate *h, struct folio *folio,
1591 bool adjust_surplus)
1593 int nid = folio_nid(folio);
1595 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio);
1596 VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio);
1598 lockdep_assert_held(&hugetlb_lock);
1599 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1600 return;
1602 list_del(&folio->lru);
1604 if (folio_test_hugetlb_freed(folio)) {
1605 folio_clear_hugetlb_freed(folio);
1606 h->free_huge_pages--;
1607 h->free_huge_pages_node[nid]--;
1609 if (adjust_surplus) {
1610 h->surplus_huge_pages--;
1611 h->surplus_huge_pages_node[nid]--;
1615 * We can only clear the hugetlb flag after allocating vmemmap
1616 * pages. Otherwise, someone (memory error handling) may try to write
1617 * to tail struct pages.
1619 if (!folio_test_hugetlb_vmemmap_optimized(folio))
1620 __folio_clear_hugetlb(folio);
1622 h->nr_huge_pages--;
1623 h->nr_huge_pages_node[nid]--;
1626 static void add_hugetlb_folio(struct hstate *h, struct folio *folio,
1627 bool adjust_surplus)
1629 int nid = folio_nid(folio);
1631 VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio);
1633 lockdep_assert_held(&hugetlb_lock);
1635 INIT_LIST_HEAD(&folio->lru);
1636 h->nr_huge_pages++;
1637 h->nr_huge_pages_node[nid]++;
1639 if (adjust_surplus) {
1640 h->surplus_huge_pages++;
1641 h->surplus_huge_pages_node[nid]++;
1644 __folio_set_hugetlb(folio);
1645 folio_change_private(folio, NULL);
1647 * We have to set hugetlb_vmemmap_optimized again as above
1648 * folio_change_private(folio, NULL) cleared it.
1650 folio_set_hugetlb_vmemmap_optimized(folio);
1652 arch_clear_hugetlb_flags(folio);
1653 enqueue_hugetlb_folio(h, folio);
1656 static void __update_and_free_hugetlb_folio(struct hstate *h,
1657 struct folio *folio)
1659 bool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio);
1661 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1662 return;
1665 * If we don't know which subpages are hwpoisoned, we can't free
1666 * the hugepage, so it's leaked intentionally.
1668 if (folio_test_hugetlb_raw_hwp_unreliable(folio))
1669 return;
1672 * If folio is not vmemmap optimized (!clear_flag), then the folio
1673 * is no longer identified as a hugetlb page. hugetlb_vmemmap_restore_folio
1674 * can only be passed hugetlb pages and will BUG otherwise.
1676 if (clear_flag && hugetlb_vmemmap_restore_folio(h, folio)) {
1677 spin_lock_irq(&hugetlb_lock);
1679 * If we cannot allocate vmemmap pages, just refuse to free the
1680 * page and put the page back on the hugetlb free list and treat
1681 * as a surplus page.
1683 add_hugetlb_folio(h, folio, true);
1684 spin_unlock_irq(&hugetlb_lock);
1685 return;
1689 * If vmemmap pages were allocated above, then we need to clear the
1690 * hugetlb flag under the hugetlb lock.
1692 if (folio_test_hugetlb(folio)) {
1693 spin_lock_irq(&hugetlb_lock);
1694 __folio_clear_hugetlb(folio);
1695 spin_unlock_irq(&hugetlb_lock);
1699 * Move PageHWPoison flag from head page to the raw error pages,
1700 * which makes any healthy subpages reusable.
1702 if (unlikely(folio_test_hwpoison(folio)))
1703 folio_clear_hugetlb_hwpoison(folio);
1705 folio_ref_unfreeze(folio, 1);
1707 INIT_LIST_HEAD(&folio->_deferred_list);
1708 hugetlb_free_folio(folio);
1712 * As update_and_free_hugetlb_folio() can be called under any context, so we cannot
1713 * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the
1714 * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate
1715 * the vmemmap pages.
1717 * free_hpage_workfn() locklessly retrieves the linked list of pages to be
1718 * freed and frees them one-by-one. As the page->mapping pointer is going
1719 * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node
1720 * structure of a lockless linked list of huge pages to be freed.
1722 static LLIST_HEAD(hpage_freelist);
1724 static void free_hpage_workfn(struct work_struct *work)
1726 struct llist_node *node;
1728 node = llist_del_all(&hpage_freelist);
1730 while (node) {
1731 struct folio *folio;
1732 struct hstate *h;
1734 folio = container_of((struct address_space **)node,
1735 struct folio, mapping);
1736 node = node->next;
1737 folio->mapping = NULL;
1739 * The VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio) in
1740 * folio_hstate() is going to trigger because a previous call to
1741 * remove_hugetlb_folio() will clear the hugetlb bit, so do
1742 * not use folio_hstate() directly.
1744 h = size_to_hstate(folio_size(folio));
1746 __update_and_free_hugetlb_folio(h, folio);
1748 cond_resched();
1751 static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1753 static inline void flush_free_hpage_work(struct hstate *h)
1755 if (hugetlb_vmemmap_optimizable(h))
1756 flush_work(&free_hpage_work);
1759 static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio,
1760 bool atomic)
1762 if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) {
1763 __update_and_free_hugetlb_folio(h, folio);
1764 return;
1768 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages.
1770 * Only call schedule_work() if hpage_freelist is previously
1771 * empty. Otherwise, schedule_work() had been called but the workfn
1772 * hasn't retrieved the list yet.
1774 if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist))
1775 schedule_work(&free_hpage_work);
1778 static void bulk_vmemmap_restore_error(struct hstate *h,
1779 struct list_head *folio_list,
1780 struct list_head *non_hvo_folios)
1782 struct folio *folio, *t_folio;
1784 if (!list_empty(non_hvo_folios)) {
1786 * Free any restored hugetlb pages so that restore of the
1787 * entire list can be retried.
1788 * The idea is that in the common case of ENOMEM errors freeing
1789 * hugetlb pages with vmemmap we will free up memory so that we
1790 * can allocate vmemmap for more hugetlb pages.
1792 list_for_each_entry_safe(folio, t_folio, non_hvo_folios, lru) {
1793 list_del(&folio->lru);
1794 spin_lock_irq(&hugetlb_lock);
1795 __folio_clear_hugetlb(folio);
1796 spin_unlock_irq(&hugetlb_lock);
1797 update_and_free_hugetlb_folio(h, folio, false);
1798 cond_resched();
1800 } else {
1802 * In the case where there are no folios which can be
1803 * immediately freed, we loop through the list trying to restore
1804 * vmemmap individually in the hope that someone elsewhere may
1805 * have done something to cause success (such as freeing some
1806 * memory). If unable to restore a hugetlb page, the hugetlb
1807 * page is made a surplus page and removed from the list.
1808 * If are able to restore vmemmap and free one hugetlb page, we
1809 * quit processing the list to retry the bulk operation.
1811 list_for_each_entry_safe(folio, t_folio, folio_list, lru)
1812 if (hugetlb_vmemmap_restore_folio(h, folio)) {
1813 list_del(&folio->lru);
1814 spin_lock_irq(&hugetlb_lock);
1815 add_hugetlb_folio(h, folio, true);
1816 spin_unlock_irq(&hugetlb_lock);
1817 } else {
1818 list_del(&folio->lru);
1819 spin_lock_irq(&hugetlb_lock);
1820 __folio_clear_hugetlb(folio);
1821 spin_unlock_irq(&hugetlb_lock);
1822 update_and_free_hugetlb_folio(h, folio, false);
1823 cond_resched();
1824 break;
1829 static void update_and_free_pages_bulk(struct hstate *h,
1830 struct list_head *folio_list)
1832 long ret;
1833 struct folio *folio, *t_folio;
1834 LIST_HEAD(non_hvo_folios);
1837 * First allocate required vmemmmap (if necessary) for all folios.
1838 * Carefully handle errors and free up any available hugetlb pages
1839 * in an effort to make forward progress.
1841 retry:
1842 ret = hugetlb_vmemmap_restore_folios(h, folio_list, &non_hvo_folios);
1843 if (ret < 0) {
1844 bulk_vmemmap_restore_error(h, folio_list, &non_hvo_folios);
1845 goto retry;
1849 * At this point, list should be empty, ret should be >= 0 and there
1850 * should only be pages on the non_hvo_folios list.
1851 * Do note that the non_hvo_folios list could be empty.
1852 * Without HVO enabled, ret will be 0 and there is no need to call
1853 * __folio_clear_hugetlb as this was done previously.
1855 VM_WARN_ON(!list_empty(folio_list));
1856 VM_WARN_ON(ret < 0);
1857 if (!list_empty(&non_hvo_folios) && ret) {
1858 spin_lock_irq(&hugetlb_lock);
1859 list_for_each_entry(folio, &non_hvo_folios, lru)
1860 __folio_clear_hugetlb(folio);
1861 spin_unlock_irq(&hugetlb_lock);
1864 list_for_each_entry_safe(folio, t_folio, &non_hvo_folios, lru) {
1865 update_and_free_hugetlb_folio(h, folio, false);
1866 cond_resched();
1870 struct hstate *size_to_hstate(unsigned long size)
1872 struct hstate *h;
1874 for_each_hstate(h) {
1875 if (huge_page_size(h) == size)
1876 return h;
1878 return NULL;
1881 void free_huge_folio(struct folio *folio)
1884 * Can't pass hstate in here because it is called from the
1885 * generic mm code.
1887 struct hstate *h = folio_hstate(folio);
1888 int nid = folio_nid(folio);
1889 struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
1890 bool restore_reserve;
1891 unsigned long flags;
1893 VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1894 VM_BUG_ON_FOLIO(folio_mapcount(folio), folio);
1896 hugetlb_set_folio_subpool(folio, NULL);
1897 if (folio_test_anon(folio))
1898 __ClearPageAnonExclusive(&folio->page);
1899 folio->mapping = NULL;
1900 restore_reserve = folio_test_hugetlb_restore_reserve(folio);
1901 folio_clear_hugetlb_restore_reserve(folio);
1904 * If HPageRestoreReserve was set on page, page allocation consumed a
1905 * reservation. If the page was associated with a subpool, there
1906 * would have been a page reserved in the subpool before allocation
1907 * via hugepage_subpool_get_pages(). Since we are 'restoring' the
1908 * reservation, do not call hugepage_subpool_put_pages() as this will
1909 * remove the reserved page from the subpool.
1911 if (!restore_reserve) {
1913 * A return code of zero implies that the subpool will be
1914 * under its minimum size if the reservation is not restored
1915 * after page is free. Therefore, force restore_reserve
1916 * operation.
1918 if (hugepage_subpool_put_pages(spool, 1) == 0)
1919 restore_reserve = true;
1922 spin_lock_irqsave(&hugetlb_lock, flags);
1923 folio_clear_hugetlb_migratable(folio);
1924 hugetlb_cgroup_uncharge_folio(hstate_index(h),
1925 pages_per_huge_page(h), folio);
1926 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
1927 pages_per_huge_page(h), folio);
1928 lruvec_stat_mod_folio(folio, NR_HUGETLB, -pages_per_huge_page(h));
1929 mem_cgroup_uncharge(folio);
1930 if (restore_reserve)
1931 h->resv_huge_pages++;
1933 if (folio_test_hugetlb_temporary(folio)) {
1934 remove_hugetlb_folio(h, folio, false);
1935 spin_unlock_irqrestore(&hugetlb_lock, flags);
1936 update_and_free_hugetlb_folio(h, folio, true);
1937 } else if (h->surplus_huge_pages_node[nid]) {
1938 /* remove the page from active list */
1939 remove_hugetlb_folio(h, folio, true);
1940 spin_unlock_irqrestore(&hugetlb_lock, flags);
1941 update_and_free_hugetlb_folio(h, folio, true);
1942 } else {
1943 arch_clear_hugetlb_flags(folio);
1944 enqueue_hugetlb_folio(h, folio);
1945 spin_unlock_irqrestore(&hugetlb_lock, flags);
1950 * Must be called with the hugetlb lock held
1952 static void __prep_account_new_huge_page(struct hstate *h, int nid)
1954 lockdep_assert_held(&hugetlb_lock);
1955 h->nr_huge_pages++;
1956 h->nr_huge_pages_node[nid]++;
1959 static void init_new_hugetlb_folio(struct hstate *h, struct folio *folio)
1961 __folio_set_hugetlb(folio);
1962 INIT_LIST_HEAD(&folio->lru);
1963 hugetlb_set_folio_subpool(folio, NULL);
1964 set_hugetlb_cgroup(folio, NULL);
1965 set_hugetlb_cgroup_rsvd(folio, NULL);
1968 static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio)
1970 init_new_hugetlb_folio(h, folio);
1971 hugetlb_vmemmap_optimize_folio(h, folio);
1974 static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid)
1976 __prep_new_hugetlb_folio(h, folio);
1977 spin_lock_irq(&hugetlb_lock);
1978 __prep_account_new_huge_page(h, nid);
1979 spin_unlock_irq(&hugetlb_lock);
1983 * Find and lock address space (mapping) in write mode.
1985 * Upon entry, the folio is locked which means that folio_mapping() is
1986 * stable. Due to locking order, we can only trylock_write. If we can
1987 * not get the lock, simply return NULL to caller.
1989 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
1991 struct address_space *mapping = folio_mapping(folio);
1993 if (!mapping)
1994 return mapping;
1996 if (i_mmap_trylock_write(mapping))
1997 return mapping;
1999 return NULL;
2002 static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
2003 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2004 nodemask_t *node_alloc_noretry)
2006 int order = huge_page_order(h);
2007 struct folio *folio;
2008 bool alloc_try_hard = true;
2009 bool retry = true;
2012 * By default we always try hard to allocate the folio with
2013 * __GFP_RETRY_MAYFAIL flag. However, if we are allocating folios in
2014 * a loop (to adjust global huge page counts) and previous allocation
2015 * failed, do not continue to try hard on the same node. Use the
2016 * node_alloc_noretry bitmap to manage this state information.
2018 if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
2019 alloc_try_hard = false;
2020 if (alloc_try_hard)
2021 gfp_mask |= __GFP_RETRY_MAYFAIL;
2022 if (nid == NUMA_NO_NODE)
2023 nid = numa_mem_id();
2024 retry:
2025 folio = __folio_alloc(gfp_mask, order, nid, nmask);
2026 /* Ensure hugetlb folio won't have large_rmappable flag set. */
2027 if (folio)
2028 folio_clear_large_rmappable(folio);
2030 if (folio && !folio_ref_freeze(folio, 1)) {
2031 folio_put(folio);
2032 if (retry) { /* retry once */
2033 retry = false;
2034 goto retry;
2036 /* WOW! twice in a row. */
2037 pr_warn("HugeTLB unexpected inflated folio ref count\n");
2038 folio = NULL;
2042 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
2043 * folio this indicates an overall state change. Clear bit so
2044 * that we resume normal 'try hard' allocations.
2046 if (node_alloc_noretry && folio && !alloc_try_hard)
2047 node_clear(nid, *node_alloc_noretry);
2050 * If we tried hard to get a folio but failed, set bit so that
2051 * subsequent attempts will not try as hard until there is an
2052 * overall state change.
2054 if (node_alloc_noretry && !folio && alloc_try_hard)
2055 node_set(nid, *node_alloc_noretry);
2057 if (!folio) {
2058 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
2059 return NULL;
2062 __count_vm_event(HTLB_BUDDY_PGALLOC);
2063 return folio;
2066 static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h,
2067 gfp_t gfp_mask, int nid, nodemask_t *nmask,
2068 nodemask_t *node_alloc_noretry)
2070 struct folio *folio;
2072 if (hstate_is_gigantic(h))
2073 folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
2074 else
2075 folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, node_alloc_noretry);
2076 if (folio)
2077 init_new_hugetlb_folio(h, folio);
2078 return folio;
2082 * Common helper to allocate a fresh hugetlb page. All specific allocators
2083 * should use this function to get new hugetlb pages
2085 * Note that returned page is 'frozen': ref count of head page and all tail
2086 * pages is zero.
2088 static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,
2089 gfp_t gfp_mask, int nid, nodemask_t *nmask)
2091 struct folio *folio;
2093 if (hstate_is_gigantic(h))
2094 folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
2095 else
2096 folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2097 if (!folio)
2098 return NULL;
2100 prep_new_hugetlb_folio(h, folio, folio_nid(folio));
2101 return folio;
2104 static void prep_and_add_allocated_folios(struct hstate *h,
2105 struct list_head *folio_list)
2107 unsigned long flags;
2108 struct folio *folio, *tmp_f;
2110 /* Send list for bulk vmemmap optimization processing */
2111 hugetlb_vmemmap_optimize_folios(h, folio_list);
2113 /* Add all new pool pages to free lists in one lock cycle */
2114 spin_lock_irqsave(&hugetlb_lock, flags);
2115 list_for_each_entry_safe(folio, tmp_f, folio_list, lru) {
2116 __prep_account_new_huge_page(h, folio_nid(folio));
2117 enqueue_hugetlb_folio(h, folio);
2119 spin_unlock_irqrestore(&hugetlb_lock, flags);
2123 * Allocates a fresh hugetlb page in a node interleaved manner. The page
2124 * will later be added to the appropriate hugetlb pool.
2126 static struct folio *alloc_pool_huge_folio(struct hstate *h,
2127 nodemask_t *nodes_allowed,
2128 nodemask_t *node_alloc_noretry,
2129 int *next_node)
2131 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2132 int nr_nodes, node;
2134 for_each_node_mask_to_alloc(next_node, nr_nodes, node, nodes_allowed) {
2135 struct folio *folio;
2137 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, node,
2138 nodes_allowed, node_alloc_noretry);
2139 if (folio)
2140 return folio;
2143 return NULL;
2147 * Remove huge page from pool from next node to free. Attempt to keep
2148 * persistent huge pages more or less balanced over allowed nodes.
2149 * This routine only 'removes' the hugetlb page. The caller must make
2150 * an additional call to free the page to low level allocators.
2151 * Called with hugetlb_lock locked.
2153 static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
2154 nodemask_t *nodes_allowed, bool acct_surplus)
2156 int nr_nodes, node;
2157 struct folio *folio = NULL;
2159 lockdep_assert_held(&hugetlb_lock);
2160 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2162 * If we're returning unused surplus pages, only examine
2163 * nodes with surplus pages.
2165 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
2166 !list_empty(&h->hugepage_freelists[node])) {
2167 folio = list_entry(h->hugepage_freelists[node].next,
2168 struct folio, lru);
2169 remove_hugetlb_folio(h, folio, acct_surplus);
2170 break;
2174 return folio;
2178 * Dissolve a given free hugetlb folio into free buddy pages. This function
2179 * does nothing for in-use hugetlb folios and non-hugetlb folios.
2180 * This function returns values like below:
2182 * -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2183 * when the system is under memory pressure and the feature of
2184 * freeing unused vmemmap pages associated with each hugetlb page
2185 * is enabled.
2186 * -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
2187 * (allocated or reserved.)
2188 * 0: successfully dissolved free hugepages or the page is not a
2189 * hugepage (considered as already dissolved)
2191 int dissolve_free_hugetlb_folio(struct folio *folio)
2193 int rc = -EBUSY;
2195 retry:
2196 /* Not to disrupt normal path by vainly holding hugetlb_lock */
2197 if (!folio_test_hugetlb(folio))
2198 return 0;
2200 spin_lock_irq(&hugetlb_lock);
2201 if (!folio_test_hugetlb(folio)) {
2202 rc = 0;
2203 goto out;
2206 if (!folio_ref_count(folio)) {
2207 struct hstate *h = folio_hstate(folio);
2208 if (!available_huge_pages(h))
2209 goto out;
2212 * We should make sure that the page is already on the free list
2213 * when it is dissolved.
2215 if (unlikely(!folio_test_hugetlb_freed(folio))) {
2216 spin_unlock_irq(&hugetlb_lock);
2217 cond_resched();
2220 * Theoretically, we should return -EBUSY when we
2221 * encounter this race. In fact, we have a chance
2222 * to successfully dissolve the page if we do a
2223 * retry. Because the race window is quite small.
2224 * If we seize this opportunity, it is an optimization
2225 * for increasing the success rate of dissolving page.
2227 goto retry;
2230 remove_hugetlb_folio(h, folio, false);
2231 h->max_huge_pages--;
2232 spin_unlock_irq(&hugetlb_lock);
2235 * Normally update_and_free_hugtlb_folio will allocate required vmemmmap
2236 * before freeing the page. update_and_free_hugtlb_folio will fail to
2237 * free the page if it can not allocate required vmemmap. We
2238 * need to adjust max_huge_pages if the page is not freed.
2239 * Attempt to allocate vmemmmap here so that we can take
2240 * appropriate action on failure.
2242 * The folio_test_hugetlb check here is because
2243 * remove_hugetlb_folio will clear hugetlb folio flag for
2244 * non-vmemmap optimized hugetlb folios.
2246 if (folio_test_hugetlb(folio)) {
2247 rc = hugetlb_vmemmap_restore_folio(h, folio);
2248 if (rc) {
2249 spin_lock_irq(&hugetlb_lock);
2250 add_hugetlb_folio(h, folio, false);
2251 h->max_huge_pages++;
2252 goto out;
2254 } else
2255 rc = 0;
2257 update_and_free_hugetlb_folio(h, folio, false);
2258 return rc;
2260 out:
2261 spin_unlock_irq(&hugetlb_lock);
2262 return rc;
2266 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
2267 * make specified memory blocks removable from the system.
2268 * Note that this will dissolve a free gigantic hugepage completely, if any
2269 * part of it lies within the given range.
2270 * Also note that if dissolve_free_hugetlb_folio() returns with an error, all
2271 * free hugetlb folios that were dissolved before that error are lost.
2273 int dissolve_free_hugetlb_folios(unsigned long start_pfn, unsigned long end_pfn)
2275 unsigned long pfn;
2276 struct folio *folio;
2277 int rc = 0;
2278 unsigned int order;
2279 struct hstate *h;
2281 if (!hugepages_supported())
2282 return rc;
2284 order = huge_page_order(&default_hstate);
2285 for_each_hstate(h)
2286 order = min(order, huge_page_order(h));
2288 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) {
2289 folio = pfn_folio(pfn);
2290 rc = dissolve_free_hugetlb_folio(folio);
2291 if (rc)
2292 break;
2295 return rc;
2299 * Allocates a fresh surplus page from the page allocator.
2301 static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,
2302 gfp_t gfp_mask, int nid, nodemask_t *nmask)
2304 struct folio *folio = NULL;
2306 if (hstate_is_gigantic(h))
2307 return NULL;
2309 spin_lock_irq(&hugetlb_lock);
2310 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
2311 goto out_unlock;
2312 spin_unlock_irq(&hugetlb_lock);
2314 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask);
2315 if (!folio)
2316 return NULL;
2318 spin_lock_irq(&hugetlb_lock);
2320 * We could have raced with the pool size change.
2321 * Double check that and simply deallocate the new page
2322 * if we would end up overcommiting the surpluses. Abuse
2323 * temporary page to workaround the nasty free_huge_folio
2324 * codeflow
2326 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
2327 folio_set_hugetlb_temporary(folio);
2328 spin_unlock_irq(&hugetlb_lock);
2329 free_huge_folio(folio);
2330 return NULL;
2333 h->surplus_huge_pages++;
2334 h->surplus_huge_pages_node[folio_nid(folio)]++;
2336 out_unlock:
2337 spin_unlock_irq(&hugetlb_lock);
2339 return folio;
2342 static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
2343 int nid, nodemask_t *nmask)
2345 struct folio *folio;
2347 if (hstate_is_gigantic(h))
2348 return NULL;
2350 folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask);
2351 if (!folio)
2352 return NULL;
2354 /* fresh huge pages are frozen */
2355 folio_ref_unfreeze(folio, 1);
2357 * We do not account these pages as surplus because they are only
2358 * temporary and will be released properly on the last reference
2360 folio_set_hugetlb_temporary(folio);
2362 return folio;
2366 * Use the VMA's mpolicy to allocate a huge page from the buddy.
2368 static
2369 struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h,
2370 struct vm_area_struct *vma, unsigned long addr)
2372 struct folio *folio = NULL;
2373 struct mempolicy *mpol;
2374 gfp_t gfp_mask = htlb_alloc_mask(h);
2375 int nid;
2376 nodemask_t *nodemask;
2378 nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
2379 if (mpol_is_preferred_many(mpol)) {
2380 gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
2382 folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask);
2384 /* Fallback to all nodes if page==NULL */
2385 nodemask = NULL;
2388 if (!folio)
2389 folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask);
2390 mpol_cond_put(mpol);
2391 return folio;
2394 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
2395 nodemask_t *nmask, gfp_t gfp_mask)
2397 struct folio *folio;
2399 spin_lock_irq(&hugetlb_lock);
2400 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, preferred_nid,
2401 nmask);
2402 if (folio) {
2403 VM_BUG_ON(!h->resv_huge_pages);
2404 h->resv_huge_pages--;
2407 spin_unlock_irq(&hugetlb_lock);
2408 return folio;
2411 /* folio migration callback function */
2412 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
2413 nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback)
2415 spin_lock_irq(&hugetlb_lock);
2416 if (available_huge_pages(h)) {
2417 struct folio *folio;
2419 folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
2420 preferred_nid, nmask);
2421 if (folio) {
2422 spin_unlock_irq(&hugetlb_lock);
2423 return folio;
2426 spin_unlock_irq(&hugetlb_lock);
2428 /* We cannot fallback to other nodes, as we could break the per-node pool. */
2429 if (!allow_alloc_fallback)
2430 gfp_mask |= __GFP_THISNODE;
2432 return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask);
2435 static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
2437 #ifdef CONFIG_NUMA
2438 struct mempolicy *mpol = get_task_policy(current);
2441 * Only enforce MPOL_BIND policy which overlaps with cpuset policy
2442 * (from policy_nodemask) specifically for hugetlb case
2444 if (mpol->mode == MPOL_BIND &&
2445 (apply_policy_zone(mpol, gfp_zone(gfp)) &&
2446 cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
2447 return &mpol->nodes;
2448 #endif
2449 return NULL;
2453 * Increase the hugetlb pool such that it can accommodate a reservation
2454 * of size 'delta'.
2456 static int gather_surplus_pages(struct hstate *h, long delta)
2457 __must_hold(&hugetlb_lock)
2459 LIST_HEAD(surplus_list);
2460 struct folio *folio, *tmp;
2461 int ret;
2462 long i;
2463 long needed, allocated;
2464 bool alloc_ok = true;
2465 int node;
2466 nodemask_t *mbind_nodemask = policy_mbind_nodemask(htlb_alloc_mask(h));
2468 lockdep_assert_held(&hugetlb_lock);
2469 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
2470 if (needed <= 0) {
2471 h->resv_huge_pages += delta;
2472 return 0;
2475 allocated = 0;
2477 ret = -ENOMEM;
2478 retry:
2479 spin_unlock_irq(&hugetlb_lock);
2480 for (i = 0; i < needed; i++) {
2481 folio = NULL;
2482 for_each_node_mask(node, cpuset_current_mems_allowed) {
2483 if (!mbind_nodemask || node_isset(node, *mbind_nodemask)) {
2484 folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2485 node, NULL);
2486 if (folio)
2487 break;
2490 if (!folio) {
2491 alloc_ok = false;
2492 break;
2494 list_add(&folio->lru, &surplus_list);
2495 cond_resched();
2497 allocated += i;
2500 * After retaking hugetlb_lock, we need to recalculate 'needed'
2501 * because either resv_huge_pages or free_huge_pages may have changed.
2503 spin_lock_irq(&hugetlb_lock);
2504 needed = (h->resv_huge_pages + delta) -
2505 (h->free_huge_pages + allocated);
2506 if (needed > 0) {
2507 if (alloc_ok)
2508 goto retry;
2510 * We were not able to allocate enough pages to
2511 * satisfy the entire reservation so we free what
2512 * we've allocated so far.
2514 goto free;
2517 * The surplus_list now contains _at_least_ the number of extra pages
2518 * needed to accommodate the reservation. Add the appropriate number
2519 * of pages to the hugetlb pool and free the extras back to the buddy
2520 * allocator. Commit the entire reservation here to prevent another
2521 * process from stealing the pages as they are added to the pool but
2522 * before they are reserved.
2524 needed += allocated;
2525 h->resv_huge_pages += delta;
2526 ret = 0;
2528 /* Free the needed pages to the hugetlb pool */
2529 list_for_each_entry_safe(folio, tmp, &surplus_list, lru) {
2530 if ((--needed) < 0)
2531 break;
2532 /* Add the page to the hugetlb allocator */
2533 enqueue_hugetlb_folio(h, folio);
2535 free:
2536 spin_unlock_irq(&hugetlb_lock);
2539 * Free unnecessary surplus pages to the buddy allocator.
2540 * Pages have no ref count, call free_huge_folio directly.
2542 list_for_each_entry_safe(folio, tmp, &surplus_list, lru)
2543 free_huge_folio(folio);
2544 spin_lock_irq(&hugetlb_lock);
2546 return ret;
2550 * This routine has two main purposes:
2551 * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2552 * in unused_resv_pages. This corresponds to the prior adjustments made
2553 * to the associated reservation map.
2554 * 2) Free any unused surplus pages that may have been allocated to satisfy
2555 * the reservation. As many as unused_resv_pages may be freed.
2557 static void return_unused_surplus_pages(struct hstate *h,
2558 unsigned long unused_resv_pages)
2560 unsigned long nr_pages;
2561 LIST_HEAD(page_list);
2563 lockdep_assert_held(&hugetlb_lock);
2564 /* Uncommit the reservation */
2565 h->resv_huge_pages -= unused_resv_pages;
2567 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2568 goto out;
2571 * Part (or even all) of the reservation could have been backed
2572 * by pre-allocated pages. Only free surplus pages.
2574 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
2577 * We want to release as many surplus pages as possible, spread
2578 * evenly across all nodes with memory. Iterate across these nodes
2579 * until we can no longer free unreserved surplus pages. This occurs
2580 * when the nodes with surplus pages have no free pages.
2581 * remove_pool_hugetlb_folio() will balance the freed pages across the
2582 * on-line nodes with memory and will handle the hstate accounting.
2584 while (nr_pages--) {
2585 struct folio *folio;
2587 folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY], 1);
2588 if (!folio)
2589 goto out;
2591 list_add(&folio->lru, &page_list);
2594 out:
2595 spin_unlock_irq(&hugetlb_lock);
2596 update_and_free_pages_bulk(h, &page_list);
2597 spin_lock_irq(&hugetlb_lock);
2602 * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
2603 * are used by the huge page allocation routines to manage reservations.
2605 * vma_needs_reservation is called to determine if the huge page at addr
2606 * within the vma has an associated reservation. If a reservation is
2607 * needed, the value 1 is returned. The caller is then responsible for
2608 * managing the global reservation and subpool usage counts. After
2609 * the huge page has been allocated, vma_commit_reservation is called
2610 * to add the page to the reservation map. If the page allocation fails,
2611 * the reservation must be ended instead of committed. vma_end_reservation
2612 * is called in such cases.
2614 * In the normal case, vma_commit_reservation returns the same value
2615 * as the preceding vma_needs_reservation call. The only time this
2616 * is not the case is if a reserve map was changed between calls. It
2617 * is the responsibility of the caller to notice the difference and
2618 * take appropriate action.
2620 * vma_add_reservation is used in error paths where a reservation must
2621 * be restored when a newly allocated huge page must be freed. It is
2622 * to be called after calling vma_needs_reservation to determine if a
2623 * reservation exists.
2625 * vma_del_reservation is used in error paths where an entry in the reserve
2626 * map was created during huge page allocation and must be removed. It is to
2627 * be called after calling vma_needs_reservation to determine if a reservation
2628 * exists.
2630 enum vma_resv_mode {
2631 VMA_NEEDS_RESV,
2632 VMA_COMMIT_RESV,
2633 VMA_END_RESV,
2634 VMA_ADD_RESV,
2635 VMA_DEL_RESV,
2637 static long __vma_reservation_common(struct hstate *h,
2638 struct vm_area_struct *vma, unsigned long addr,
2639 enum vma_resv_mode mode)
2641 struct resv_map *resv;
2642 pgoff_t idx;
2643 long ret;
2644 long dummy_out_regions_needed;
2646 resv = vma_resv_map(vma);
2647 if (!resv)
2648 return 1;
2650 idx = vma_hugecache_offset(h, vma, addr);
2651 switch (mode) {
2652 case VMA_NEEDS_RESV:
2653 ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2654 /* We assume that vma_reservation_* routines always operate on
2655 * 1 page, and that adding to resv map a 1 page entry can only
2656 * ever require 1 region.
2658 VM_BUG_ON(dummy_out_regions_needed != 1);
2659 break;
2660 case VMA_COMMIT_RESV:
2661 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2662 /* region_add calls of range 1 should never fail. */
2663 VM_BUG_ON(ret < 0);
2664 break;
2665 case VMA_END_RESV:
2666 region_abort(resv, idx, idx + 1, 1);
2667 ret = 0;
2668 break;
2669 case VMA_ADD_RESV:
2670 if (vma->vm_flags & VM_MAYSHARE) {
2671 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2672 /* region_add calls of range 1 should never fail. */
2673 VM_BUG_ON(ret < 0);
2674 } else {
2675 region_abort(resv, idx, idx + 1, 1);
2676 ret = region_del(resv, idx, idx + 1);
2678 break;
2679 case VMA_DEL_RESV:
2680 if (vma->vm_flags & VM_MAYSHARE) {
2681 region_abort(resv, idx, idx + 1, 1);
2682 ret = region_del(resv, idx, idx + 1);
2683 } else {
2684 ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2685 /* region_add calls of range 1 should never fail. */
2686 VM_BUG_ON(ret < 0);
2688 break;
2689 default:
2690 BUG();
2693 if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV)
2694 return ret;
2696 * We know private mapping must have HPAGE_RESV_OWNER set.
2698 * In most cases, reserves always exist for private mappings.
2699 * However, a file associated with mapping could have been
2700 * hole punched or truncated after reserves were consumed.
2701 * As subsequent fault on such a range will not use reserves.
2702 * Subtle - The reserve map for private mappings has the
2703 * opposite meaning than that of shared mappings. If NO
2704 * entry is in the reserve map, it means a reservation exists.
2705 * If an entry exists in the reserve map, it means the
2706 * reservation has already been consumed. As a result, the
2707 * return value of this routine is the opposite of the
2708 * value returned from reserve map manipulation routines above.
2710 if (ret > 0)
2711 return 0;
2712 if (ret == 0)
2713 return 1;
2714 return ret;
2717 static long vma_needs_reservation(struct hstate *h,
2718 struct vm_area_struct *vma, unsigned long addr)
2720 return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
2723 static long vma_commit_reservation(struct hstate *h,
2724 struct vm_area_struct *vma, unsigned long addr)
2726 return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2729 static void vma_end_reservation(struct hstate *h,
2730 struct vm_area_struct *vma, unsigned long addr)
2732 (void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
2735 static long vma_add_reservation(struct hstate *h,
2736 struct vm_area_struct *vma, unsigned long addr)
2738 return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2741 static long vma_del_reservation(struct hstate *h,
2742 struct vm_area_struct *vma, unsigned long addr)
2744 return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV);
2748 * This routine is called to restore reservation information on error paths.
2749 * It should ONLY be called for folios allocated via alloc_hugetlb_folio(),
2750 * and the hugetlb mutex should remain held when calling this routine.
2752 * It handles two specific cases:
2753 * 1) A reservation was in place and the folio consumed the reservation.
2754 * hugetlb_restore_reserve is set in the folio.
2755 * 2) No reservation was in place for the page, so hugetlb_restore_reserve is
2756 * not set. However, alloc_hugetlb_folio always updates the reserve map.
2758 * In case 1, free_huge_folio later in the error path will increment the
2759 * global reserve count. But, free_huge_folio does not have enough context
2760 * to adjust the reservation map. This case deals primarily with private
2761 * mappings. Adjust the reserve map here to be consistent with global
2762 * reserve count adjustments to be made by free_huge_folio. Make sure the
2763 * reserve map indicates there is a reservation present.
2765 * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio.
2767 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
2768 unsigned long address, struct folio *folio)
2770 long rc = vma_needs_reservation(h, vma, address);
2772 if (folio_test_hugetlb_restore_reserve(folio)) {
2773 if (unlikely(rc < 0))
2775 * Rare out of memory condition in reserve map
2776 * manipulation. Clear hugetlb_restore_reserve so
2777 * that global reserve count will not be incremented
2778 * by free_huge_folio. This will make it appear
2779 * as though the reservation for this folio was
2780 * consumed. This may prevent the task from
2781 * faulting in the folio at a later time. This
2782 * is better than inconsistent global huge page
2783 * accounting of reserve counts.
2785 folio_clear_hugetlb_restore_reserve(folio);
2786 else if (rc)
2787 (void)vma_add_reservation(h, vma, address);
2788 else
2789 vma_end_reservation(h, vma, address);
2790 } else {
2791 if (!rc) {
2793 * This indicates there is an entry in the reserve map
2794 * not added by alloc_hugetlb_folio. We know it was added
2795 * before the alloc_hugetlb_folio call, otherwise
2796 * hugetlb_restore_reserve would be set on the folio.
2797 * Remove the entry so that a subsequent allocation
2798 * does not consume a reservation.
2800 rc = vma_del_reservation(h, vma, address);
2801 if (rc < 0)
2803 * VERY rare out of memory condition. Since
2804 * we can not delete the entry, set
2805 * hugetlb_restore_reserve so that the reserve
2806 * count will be incremented when the folio
2807 * is freed. This reserve will be consumed
2808 * on a subsequent allocation.
2810 folio_set_hugetlb_restore_reserve(folio);
2811 } else if (rc < 0) {
2813 * Rare out of memory condition from
2814 * vma_needs_reservation call. Memory allocation is
2815 * only attempted if a new entry is needed. Therefore,
2816 * this implies there is not an entry in the
2817 * reserve map.
2819 * For shared mappings, no entry in the map indicates
2820 * no reservation. We are done.
2822 if (!(vma->vm_flags & VM_MAYSHARE))
2824 * For private mappings, no entry indicates
2825 * a reservation is present. Since we can
2826 * not add an entry, set hugetlb_restore_reserve
2827 * on the folio so reserve count will be
2828 * incremented when freed. This reserve will
2829 * be consumed on a subsequent allocation.
2831 folio_set_hugetlb_restore_reserve(folio);
2832 } else
2834 * No reservation present, do nothing
2836 vma_end_reservation(h, vma, address);
2841 * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
2842 * the old one
2843 * @h: struct hstate old page belongs to
2844 * @old_folio: Old folio to dissolve
2845 * @list: List to isolate the page in case we need to
2846 * Returns 0 on success, otherwise negated error.
2848 static int alloc_and_dissolve_hugetlb_folio(struct hstate *h,
2849 struct folio *old_folio, struct list_head *list)
2851 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2852 int nid = folio_nid(old_folio);
2853 struct folio *new_folio = NULL;
2854 int ret = 0;
2856 retry:
2857 spin_lock_irq(&hugetlb_lock);
2858 if (!folio_test_hugetlb(old_folio)) {
2860 * Freed from under us. Drop new_folio too.
2862 goto free_new;
2863 } else if (folio_ref_count(old_folio)) {
2864 bool isolated;
2867 * Someone has grabbed the folio, try to isolate it here.
2868 * Fail with -EBUSY if not possible.
2870 spin_unlock_irq(&hugetlb_lock);
2871 isolated = isolate_hugetlb(old_folio, list);
2872 ret = isolated ? 0 : -EBUSY;
2873 spin_lock_irq(&hugetlb_lock);
2874 goto free_new;
2875 } else if (!folio_test_hugetlb_freed(old_folio)) {
2877 * Folio's refcount is 0 but it has not been enqueued in the
2878 * freelist yet. Race window is small, so we can succeed here if
2879 * we retry.
2881 spin_unlock_irq(&hugetlb_lock);
2882 cond_resched();
2883 goto retry;
2884 } else {
2885 if (!new_folio) {
2886 spin_unlock_irq(&hugetlb_lock);
2887 new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
2888 NULL, NULL);
2889 if (!new_folio)
2890 return -ENOMEM;
2891 __prep_new_hugetlb_folio(h, new_folio);
2892 goto retry;
2896 * Ok, old_folio is still a genuine free hugepage. Remove it from
2897 * the freelist and decrease the counters. These will be
2898 * incremented again when calling __prep_account_new_huge_page()
2899 * and enqueue_hugetlb_folio() for new_folio. The counters will
2900 * remain stable since this happens under the lock.
2902 remove_hugetlb_folio(h, old_folio, false);
2905 * Ref count on new_folio is already zero as it was dropped
2906 * earlier. It can be directly added to the pool free list.
2908 __prep_account_new_huge_page(h, nid);
2909 enqueue_hugetlb_folio(h, new_folio);
2912 * Folio has been replaced, we can safely free the old one.
2914 spin_unlock_irq(&hugetlb_lock);
2915 update_and_free_hugetlb_folio(h, old_folio, false);
2918 return ret;
2920 free_new:
2921 spin_unlock_irq(&hugetlb_lock);
2922 if (new_folio)
2923 update_and_free_hugetlb_folio(h, new_folio, false);
2925 return ret;
2928 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list)
2930 struct hstate *h;
2931 struct folio *folio = page_folio(page);
2932 int ret = -EBUSY;
2935 * The page might have been dissolved from under our feet, so make sure
2936 * to carefully check the state under the lock.
2937 * Return success when racing as if we dissolved the page ourselves.
2939 spin_lock_irq(&hugetlb_lock);
2940 if (folio_test_hugetlb(folio)) {
2941 h = folio_hstate(folio);
2942 } else {
2943 spin_unlock_irq(&hugetlb_lock);
2944 return 0;
2946 spin_unlock_irq(&hugetlb_lock);
2949 * Fence off gigantic pages as there is a cyclic dependency between
2950 * alloc_contig_range and them. Return -ENOMEM as this has the effect
2951 * of bailing out right away without further retrying.
2953 if (hstate_is_gigantic(h))
2954 return -ENOMEM;
2956 if (folio_ref_count(folio) && isolate_hugetlb(folio, list))
2957 ret = 0;
2958 else if (!folio_ref_count(folio))
2959 ret = alloc_and_dissolve_hugetlb_folio(h, folio, list);
2961 return ret;
2964 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
2965 unsigned long addr, int avoid_reserve)
2967 struct hugepage_subpool *spool = subpool_vma(vma);
2968 struct hstate *h = hstate_vma(vma);
2969 struct folio *folio;
2970 long map_chg, map_commit, nr_pages = pages_per_huge_page(h);
2971 long gbl_chg;
2972 int memcg_charge_ret, ret, idx;
2973 struct hugetlb_cgroup *h_cg = NULL;
2974 struct mem_cgroup *memcg;
2975 bool deferred_reserve;
2976 gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL;
2978 memcg = get_mem_cgroup_from_current();
2979 memcg_charge_ret = mem_cgroup_hugetlb_try_charge(memcg, gfp, nr_pages);
2980 if (memcg_charge_ret == -ENOMEM) {
2981 mem_cgroup_put(memcg);
2982 return ERR_PTR(-ENOMEM);
2985 idx = hstate_index(h);
2987 * Examine the region/reserve map to determine if the process
2988 * has a reservation for the page to be allocated. A return
2989 * code of zero indicates a reservation exists (no change).
2991 map_chg = gbl_chg = vma_needs_reservation(h, vma, addr);
2992 if (map_chg < 0) {
2993 if (!memcg_charge_ret)
2994 mem_cgroup_cancel_charge(memcg, nr_pages);
2995 mem_cgroup_put(memcg);
2996 return ERR_PTR(-ENOMEM);
3000 * Processes that did not create the mapping will have no
3001 * reserves as indicated by the region/reserve map. Check
3002 * that the allocation will not exceed the subpool limit.
3003 * Allocations for MAP_NORESERVE mappings also need to be
3004 * checked against any subpool limit.
3006 if (map_chg || avoid_reserve) {
3007 gbl_chg = hugepage_subpool_get_pages(spool, 1);
3008 if (gbl_chg < 0)
3009 goto out_end_reservation;
3012 * Even though there was no reservation in the region/reserve
3013 * map, there could be reservations associated with the
3014 * subpool that can be used. This would be indicated if the
3015 * return value of hugepage_subpool_get_pages() is zero.
3016 * However, if avoid_reserve is specified we still avoid even
3017 * the subpool reservations.
3019 if (avoid_reserve)
3020 gbl_chg = 1;
3023 /* If this allocation is not consuming a reservation, charge it now.
3025 deferred_reserve = map_chg || avoid_reserve;
3026 if (deferred_reserve) {
3027 ret = hugetlb_cgroup_charge_cgroup_rsvd(
3028 idx, pages_per_huge_page(h), &h_cg);
3029 if (ret)
3030 goto out_subpool_put;
3033 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
3034 if (ret)
3035 goto out_uncharge_cgroup_reservation;
3037 spin_lock_irq(&hugetlb_lock);
3039 * glb_chg is passed to indicate whether or not a page must be taken
3040 * from the global free pool (global change). gbl_chg == 0 indicates
3041 * a reservation exists for the allocation.
3043 folio = dequeue_hugetlb_folio_vma(h, vma, addr, avoid_reserve, gbl_chg);
3044 if (!folio) {
3045 spin_unlock_irq(&hugetlb_lock);
3046 folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr);
3047 if (!folio)
3048 goto out_uncharge_cgroup;
3049 spin_lock_irq(&hugetlb_lock);
3050 if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
3051 folio_set_hugetlb_restore_reserve(folio);
3052 h->resv_huge_pages--;
3054 list_add(&folio->lru, &h->hugepage_activelist);
3055 folio_ref_unfreeze(folio, 1);
3056 /* Fall through */
3059 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, folio);
3060 /* If allocation is not consuming a reservation, also store the
3061 * hugetlb_cgroup pointer on the page.
3063 if (deferred_reserve) {
3064 hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h),
3065 h_cg, folio);
3068 spin_unlock_irq(&hugetlb_lock);
3070 hugetlb_set_folio_subpool(folio, spool);
3072 map_commit = vma_commit_reservation(h, vma, addr);
3073 if (unlikely(map_chg > map_commit)) {
3075 * The page was added to the reservation map between
3076 * vma_needs_reservation and vma_commit_reservation.
3077 * This indicates a race with hugetlb_reserve_pages.
3078 * Adjust for the subpool count incremented above AND
3079 * in hugetlb_reserve_pages for the same page. Also,
3080 * the reservation count added in hugetlb_reserve_pages
3081 * no longer applies.
3083 long rsv_adjust;
3085 rsv_adjust = hugepage_subpool_put_pages(spool, 1);
3086 hugetlb_acct_memory(h, -rsv_adjust);
3087 if (deferred_reserve) {
3088 spin_lock_irq(&hugetlb_lock);
3089 hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
3090 pages_per_huge_page(h), folio);
3091 spin_unlock_irq(&hugetlb_lock);
3095 if (!memcg_charge_ret)
3096 mem_cgroup_commit_charge(folio, memcg);
3097 lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h));
3098 mem_cgroup_put(memcg);
3100 return folio;
3102 out_uncharge_cgroup:
3103 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
3104 out_uncharge_cgroup_reservation:
3105 if (deferred_reserve)
3106 hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
3107 h_cg);
3108 out_subpool_put:
3109 if (map_chg || avoid_reserve)
3110 hugepage_subpool_put_pages(spool, 1);
3111 out_end_reservation:
3112 vma_end_reservation(h, vma, addr);
3113 if (!memcg_charge_ret)
3114 mem_cgroup_cancel_charge(memcg, nr_pages);
3115 mem_cgroup_put(memcg);
3116 return ERR_PTR(-ENOSPC);
3119 int alloc_bootmem_huge_page(struct hstate *h, int nid)
3120 __attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
3121 int __alloc_bootmem_huge_page(struct hstate *h, int nid)
3123 struct huge_bootmem_page *m = NULL; /* initialize for clang */
3124 int nr_nodes, node = nid;
3126 /* do node specific alloc */
3127 if (nid != NUMA_NO_NODE) {
3128 m = memblock_alloc_try_nid_raw(huge_page_size(h), huge_page_size(h),
3129 0, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3130 if (!m)
3131 return 0;
3132 goto found;
3134 /* allocate from next node when distributing huge pages */
3135 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, &node_states[N_MEMORY]) {
3136 m = memblock_alloc_try_nid_raw(
3137 huge_page_size(h), huge_page_size(h),
3138 0, MEMBLOCK_ALLOC_ACCESSIBLE, node);
3140 * Use the beginning of the huge page to store the
3141 * huge_bootmem_page struct (until gather_bootmem
3142 * puts them into the mem_map).
3144 if (!m)
3145 return 0;
3146 goto found;
3149 found:
3152 * Only initialize the head struct page in memmap_init_reserved_pages,
3153 * rest of the struct pages will be initialized by the HugeTLB
3154 * subsystem itself.
3155 * The head struct page is used to get folio information by the HugeTLB
3156 * subsystem like zone id and node id.
3158 memblock_reserved_mark_noinit(virt_to_phys((void *)m + PAGE_SIZE),
3159 huge_page_size(h) - PAGE_SIZE);
3160 /* Put them into a private list first because mem_map is not up yet */
3161 INIT_LIST_HEAD(&m->list);
3162 list_add(&m->list, &huge_boot_pages[node]);
3163 m->hstate = h;
3164 return 1;
3167 /* Initialize [start_page:end_page_number] tail struct pages of a hugepage */
3168 static void __init hugetlb_folio_init_tail_vmemmap(struct folio *folio,
3169 unsigned long start_page_number,
3170 unsigned long end_page_number)
3172 enum zone_type zone = zone_idx(folio_zone(folio));
3173 int nid = folio_nid(folio);
3174 unsigned long head_pfn = folio_pfn(folio);
3175 unsigned long pfn, end_pfn = head_pfn + end_page_number;
3176 int ret;
3178 for (pfn = head_pfn + start_page_number; pfn < end_pfn; pfn++) {
3179 struct page *page = pfn_to_page(pfn);
3181 __ClearPageReserved(folio_page(folio, pfn - head_pfn));
3182 __init_single_page(page, pfn, zone, nid);
3183 prep_compound_tail((struct page *)folio, pfn - head_pfn);
3184 ret = page_ref_freeze(page, 1);
3185 VM_BUG_ON(!ret);
3189 static void __init hugetlb_folio_init_vmemmap(struct folio *folio,
3190 struct hstate *h,
3191 unsigned long nr_pages)
3193 int ret;
3195 /* Prepare folio head */
3196 __folio_clear_reserved(folio);
3197 __folio_set_head(folio);
3198 ret = folio_ref_freeze(folio, 1);
3199 VM_BUG_ON(!ret);
3200 /* Initialize the necessary tail struct pages */
3201 hugetlb_folio_init_tail_vmemmap(folio, 1, nr_pages);
3202 prep_compound_head((struct page *)folio, huge_page_order(h));
3205 static void __init prep_and_add_bootmem_folios(struct hstate *h,
3206 struct list_head *folio_list)
3208 unsigned long flags;
3209 struct folio *folio, *tmp_f;
3211 /* Send list for bulk vmemmap optimization processing */
3212 hugetlb_vmemmap_optimize_folios(h, folio_list);
3214 list_for_each_entry_safe(folio, tmp_f, folio_list, lru) {
3215 if (!folio_test_hugetlb_vmemmap_optimized(folio)) {
3217 * If HVO fails, initialize all tail struct pages
3218 * We do not worry about potential long lock hold
3219 * time as this is early in boot and there should
3220 * be no contention.
3222 hugetlb_folio_init_tail_vmemmap(folio,
3223 HUGETLB_VMEMMAP_RESERVE_PAGES,
3224 pages_per_huge_page(h));
3226 /* Subdivide locks to achieve better parallel performance */
3227 spin_lock_irqsave(&hugetlb_lock, flags);
3228 __prep_account_new_huge_page(h, folio_nid(folio));
3229 enqueue_hugetlb_folio(h, folio);
3230 spin_unlock_irqrestore(&hugetlb_lock, flags);
3235 * Put bootmem huge pages into the standard lists after mem_map is up.
3236 * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages.
3238 static void __init gather_bootmem_prealloc_node(unsigned long nid)
3240 LIST_HEAD(folio_list);
3241 struct huge_bootmem_page *m;
3242 struct hstate *h = NULL, *prev_h = NULL;
3244 list_for_each_entry(m, &huge_boot_pages[nid], list) {
3245 struct page *page = virt_to_page(m);
3246 struct folio *folio = (void *)page;
3248 h = m->hstate;
3250 * It is possible to have multiple huge page sizes (hstates)
3251 * in this list. If so, process each size separately.
3253 if (h != prev_h && prev_h != NULL)
3254 prep_and_add_bootmem_folios(prev_h, &folio_list);
3255 prev_h = h;
3257 VM_BUG_ON(!hstate_is_gigantic(h));
3258 WARN_ON(folio_ref_count(folio) != 1);
3260 hugetlb_folio_init_vmemmap(folio, h,
3261 HUGETLB_VMEMMAP_RESERVE_PAGES);
3262 init_new_hugetlb_folio(h, folio);
3263 list_add(&folio->lru, &folio_list);
3266 * We need to restore the 'stolen' pages to totalram_pages
3267 * in order to fix confusing memory reports from free(1) and
3268 * other side-effects, like CommitLimit going negative.
3270 adjust_managed_page_count(page, pages_per_huge_page(h));
3271 cond_resched();
3274 prep_and_add_bootmem_folios(h, &folio_list);
3277 static void __init gather_bootmem_prealloc_parallel(unsigned long start,
3278 unsigned long end, void *arg)
3280 int nid;
3282 for (nid = start; nid < end; nid++)
3283 gather_bootmem_prealloc_node(nid);
3286 static void __init gather_bootmem_prealloc(void)
3288 struct padata_mt_job job = {
3289 .thread_fn = gather_bootmem_prealloc_parallel,
3290 .fn_arg = NULL,
3291 .start = 0,
3292 .size = num_node_state(N_MEMORY),
3293 .align = 1,
3294 .min_chunk = 1,
3295 .max_threads = num_node_state(N_MEMORY),
3296 .numa_aware = true,
3299 padata_do_multithreaded(&job);
3302 static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
3304 unsigned long i;
3305 char buf[32];
3306 LIST_HEAD(folio_list);
3308 for (i = 0; i < h->max_huge_pages_node[nid]; ++i) {
3309 if (hstate_is_gigantic(h)) {
3310 if (!alloc_bootmem_huge_page(h, nid))
3311 break;
3312 } else {
3313 struct folio *folio;
3314 gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
3316 folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
3317 &node_states[N_MEMORY], NULL);
3318 if (!folio)
3319 break;
3320 list_add(&folio->lru, &folio_list);
3322 cond_resched();
3325 if (!list_empty(&folio_list))
3326 prep_and_add_allocated_folios(h, &folio_list);
3328 if (i == h->max_huge_pages_node[nid])
3329 return;
3331 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3332 pr_warn("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n",
3333 h->max_huge_pages_node[nid], buf, nid, i);
3334 h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
3335 h->max_huge_pages_node[nid] = i;
3338 static bool __init hugetlb_hstate_alloc_pages_specific_nodes(struct hstate *h)
3340 int i;
3341 bool node_specific_alloc = false;
3343 for_each_online_node(i) {
3344 if (h->max_huge_pages_node[i] > 0) {
3345 hugetlb_hstate_alloc_pages_onenode(h, i);
3346 node_specific_alloc = true;
3350 return node_specific_alloc;
3353 static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated, struct hstate *h)
3355 if (allocated < h->max_huge_pages) {
3356 char buf[32];
3358 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3359 pr_warn("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n",
3360 h->max_huge_pages, buf, allocated);
3361 h->max_huge_pages = allocated;
3365 static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned long end, void *arg)
3367 struct hstate *h = (struct hstate *)arg;
3368 int i, num = end - start;
3369 nodemask_t node_alloc_noretry;
3370 LIST_HEAD(folio_list);
3371 int next_node = first_online_node;
3373 /* Bit mask controlling how hard we retry per-node allocations.*/
3374 nodes_clear(node_alloc_noretry);
3376 for (i = 0; i < num; ++i) {
3377 struct folio *folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY],
3378 &node_alloc_noretry, &next_node);
3379 if (!folio)
3380 break;
3382 list_move(&folio->lru, &folio_list);
3383 cond_resched();
3386 prep_and_add_allocated_folios(h, &folio_list);
3389 static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h)
3391 unsigned long i;
3393 for (i = 0; i < h->max_huge_pages; ++i) {
3394 if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE))
3395 break;
3396 cond_resched();
3399 return i;
3402 static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h)
3404 struct padata_mt_job job = {
3405 .fn_arg = h,
3406 .align = 1,
3407 .numa_aware = true
3410 job.thread_fn = hugetlb_pages_alloc_boot_node;
3411 job.start = 0;
3412 job.size = h->max_huge_pages;
3415 * job.max_threads is twice the num_node_state(N_MEMORY),
3417 * Tests below indicate that a multiplier of 2 significantly improves
3418 * performance, and although larger values also provide improvements,
3419 * the gains are marginal.
3421 * Therefore, choosing 2 as the multiplier strikes a good balance between
3422 * enhancing parallel processing capabilities and maintaining efficient
3423 * resource management.
3425 * +------------+-------+-------+-------+-------+-------+
3426 * | multiplier | 1 | 2 | 3 | 4 | 5 |
3427 * +------------+-------+-------+-------+-------+-------+
3428 * | 256G 2node | 358ms | 215ms | 157ms | 134ms | 126ms |
3429 * | 2T 4node | 979ms | 679ms | 543ms | 489ms | 481ms |
3430 * | 50G 2node | 71ms | 44ms | 37ms | 30ms | 31ms |
3431 * +------------+-------+-------+-------+-------+-------+
3433 job.max_threads = num_node_state(N_MEMORY) * 2;
3434 job.min_chunk = h->max_huge_pages / num_node_state(N_MEMORY) / 2;
3435 padata_do_multithreaded(&job);
3437 return h->nr_huge_pages;
3441 * NOTE: this routine is called in different contexts for gigantic and
3442 * non-gigantic pages.
3443 * - For gigantic pages, this is called early in the boot process and
3444 * pages are allocated from memblock allocated or something similar.
3445 * Gigantic pages are actually added to pools later with the routine
3446 * gather_bootmem_prealloc.
3447 * - For non-gigantic pages, this is called later in the boot process after
3448 * all of mm is up and functional. Pages are allocated from buddy and
3449 * then added to hugetlb pools.
3451 static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
3453 unsigned long allocated;
3454 static bool initialized __initdata;
3456 /* skip gigantic hugepages allocation if hugetlb_cma enabled */
3457 if (hstate_is_gigantic(h) && hugetlb_cma_size) {
3458 pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
3459 return;
3462 /* hugetlb_hstate_alloc_pages will be called many times, initialize huge_boot_pages once */
3463 if (!initialized) {
3464 int i = 0;
3466 for (i = 0; i < MAX_NUMNODES; i++)
3467 INIT_LIST_HEAD(&huge_boot_pages[i]);
3468 initialized = true;
3471 /* do node specific alloc */
3472 if (hugetlb_hstate_alloc_pages_specific_nodes(h))
3473 return;
3475 /* below will do all node balanced alloc */
3476 if (hstate_is_gigantic(h))
3477 allocated = hugetlb_gigantic_pages_alloc_boot(h);
3478 else
3479 allocated = hugetlb_pages_alloc_boot(h);
3481 hugetlb_hstate_alloc_pages_errcheck(allocated, h);
3484 static void __init hugetlb_init_hstates(void)
3486 struct hstate *h, *h2;
3488 for_each_hstate(h) {
3489 /* oversize hugepages were init'ed in early boot */
3490 if (!hstate_is_gigantic(h))
3491 hugetlb_hstate_alloc_pages(h);
3494 * Set demote order for each hstate. Note that
3495 * h->demote_order is initially 0.
3496 * - We can not demote gigantic pages if runtime freeing
3497 * is not supported, so skip this.
3498 * - If CMA allocation is possible, we can not demote
3499 * HUGETLB_PAGE_ORDER or smaller size pages.
3501 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3502 continue;
3503 if (hugetlb_cma_size && h->order <= HUGETLB_PAGE_ORDER)
3504 continue;
3505 for_each_hstate(h2) {
3506 if (h2 == h)
3507 continue;
3508 if (h2->order < h->order &&
3509 h2->order > h->demote_order)
3510 h->demote_order = h2->order;
3515 static void __init report_hugepages(void)
3517 struct hstate *h;
3519 for_each_hstate(h) {
3520 char buf[32];
3522 string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3523 pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n",
3524 buf, h->free_huge_pages);
3525 pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
3526 hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf);
3530 #ifdef CONFIG_HIGHMEM
3531 static void try_to_free_low(struct hstate *h, unsigned long count,
3532 nodemask_t *nodes_allowed)
3534 int i;
3535 LIST_HEAD(page_list);
3537 lockdep_assert_held(&hugetlb_lock);
3538 if (hstate_is_gigantic(h))
3539 return;
3542 * Collect pages to be freed on a list, and free after dropping lock
3544 for_each_node_mask(i, *nodes_allowed) {
3545 struct folio *folio, *next;
3546 struct list_head *freel = &h->hugepage_freelists[i];
3547 list_for_each_entry_safe(folio, next, freel, lru) {
3548 if (count >= h->nr_huge_pages)
3549 goto out;
3550 if (folio_test_highmem(folio))
3551 continue;
3552 remove_hugetlb_folio(h, folio, false);
3553 list_add(&folio->lru, &page_list);
3557 out:
3558 spin_unlock_irq(&hugetlb_lock);
3559 update_and_free_pages_bulk(h, &page_list);
3560 spin_lock_irq(&hugetlb_lock);
3562 #else
3563 static inline void try_to_free_low(struct hstate *h, unsigned long count,
3564 nodemask_t *nodes_allowed)
3567 #endif
3570 * Increment or decrement surplus_huge_pages. Keep node-specific counters
3571 * balanced by operating on them in a round-robin fashion.
3572 * Returns 1 if an adjustment was made.
3574 static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
3575 int delta)
3577 int nr_nodes, node;
3579 lockdep_assert_held(&hugetlb_lock);
3580 VM_BUG_ON(delta != -1 && delta != 1);
3582 if (delta < 0) {
3583 for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, nodes_allowed) {
3584 if (h->surplus_huge_pages_node[node])
3585 goto found;
3587 } else {
3588 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3589 if (h->surplus_huge_pages_node[node] <
3590 h->nr_huge_pages_node[node])
3591 goto found;
3594 return 0;
3596 found:
3597 h->surplus_huge_pages += delta;
3598 h->surplus_huge_pages_node[node] += delta;
3599 return 1;
3602 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
3603 static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
3604 nodemask_t *nodes_allowed)
3606 unsigned long min_count;
3607 unsigned long allocated;
3608 struct folio *folio;
3609 LIST_HEAD(page_list);
3610 NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
3613 * Bit mask controlling how hard we retry per-node allocations.
3614 * If we can not allocate the bit mask, do not attempt to allocate
3615 * the requested huge pages.
3617 if (node_alloc_noretry)
3618 nodes_clear(*node_alloc_noretry);
3619 else
3620 return -ENOMEM;
3623 * resize_lock mutex prevents concurrent adjustments to number of
3624 * pages in hstate via the proc/sysfs interfaces.
3626 mutex_lock(&h->resize_lock);
3627 flush_free_hpage_work(h);
3628 spin_lock_irq(&hugetlb_lock);
3631 * Check for a node specific request.
3632 * Changing node specific huge page count may require a corresponding
3633 * change to the global count. In any case, the passed node mask
3634 * (nodes_allowed) will restrict alloc/free to the specified node.
3636 if (nid != NUMA_NO_NODE) {
3637 unsigned long old_count = count;
3639 count += persistent_huge_pages(h) -
3640 (h->nr_huge_pages_node[nid] -
3641 h->surplus_huge_pages_node[nid]);
3643 * User may have specified a large count value which caused the
3644 * above calculation to overflow. In this case, they wanted
3645 * to allocate as many huge pages as possible. Set count to
3646 * largest possible value to align with their intention.
3648 if (count < old_count)
3649 count = ULONG_MAX;
3653 * Gigantic pages runtime allocation depend on the capability for large
3654 * page range allocation.
3655 * If the system does not provide this feature, return an error when
3656 * the user tries to allocate gigantic pages but let the user free the
3657 * boottime allocated gigantic pages.
3659 if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
3660 if (count > persistent_huge_pages(h)) {
3661 spin_unlock_irq(&hugetlb_lock);
3662 mutex_unlock(&h->resize_lock);
3663 NODEMASK_FREE(node_alloc_noretry);
3664 return -EINVAL;
3666 /* Fall through to decrease pool */
3670 * Increase the pool size
3671 * First take pages out of surplus state. Then make up the
3672 * remaining difference by allocating fresh huge pages.
3674 * We might race with alloc_surplus_hugetlb_folio() here and be unable
3675 * to convert a surplus huge page to a normal huge page. That is
3676 * not critical, though, it just means the overall size of the
3677 * pool might be one hugepage larger than it needs to be, but
3678 * within all the constraints specified by the sysctls.
3680 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
3681 if (!adjust_pool_surplus(h, nodes_allowed, -1))
3682 break;
3685 allocated = 0;
3686 while (count > (persistent_huge_pages(h) + allocated)) {
3688 * If this allocation races such that we no longer need the
3689 * page, free_huge_folio will handle it by freeing the page
3690 * and reducing the surplus.
3692 spin_unlock_irq(&hugetlb_lock);
3694 /* yield cpu to avoid soft lockup */
3695 cond_resched();
3697 folio = alloc_pool_huge_folio(h, nodes_allowed,
3698 node_alloc_noretry,
3699 &h->next_nid_to_alloc);
3700 if (!folio) {
3701 prep_and_add_allocated_folios(h, &page_list);
3702 spin_lock_irq(&hugetlb_lock);
3703 goto out;
3706 list_add(&folio->lru, &page_list);
3707 allocated++;
3709 /* Bail for signals. Probably ctrl-c from user */
3710 if (signal_pending(current)) {
3711 prep_and_add_allocated_folios(h, &page_list);
3712 spin_lock_irq(&hugetlb_lock);
3713 goto out;
3716 spin_lock_irq(&hugetlb_lock);
3719 /* Add allocated pages to the pool */
3720 if (!list_empty(&page_list)) {
3721 spin_unlock_irq(&hugetlb_lock);
3722 prep_and_add_allocated_folios(h, &page_list);
3723 spin_lock_irq(&hugetlb_lock);
3727 * Decrease the pool size
3728 * First return free pages to the buddy allocator (being careful
3729 * to keep enough around to satisfy reservations). Then place
3730 * pages into surplus state as needed so the pool will shrink
3731 * to the desired size as pages become free.
3733 * By placing pages into the surplus state independent of the
3734 * overcommit value, we are allowing the surplus pool size to
3735 * exceed overcommit. There are few sane options here. Since
3736 * alloc_surplus_hugetlb_folio() is checking the global counter,
3737 * though, we'll note that we're not allowed to exceed surplus
3738 * and won't grow the pool anywhere else. Not until one of the
3739 * sysctls are changed, or the surplus pages go out of use.
3741 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
3742 min_count = max(count, min_count);
3743 try_to_free_low(h, min_count, nodes_allowed);
3746 * Collect pages to be removed on list without dropping lock
3748 while (min_count < persistent_huge_pages(h)) {
3749 folio = remove_pool_hugetlb_folio(h, nodes_allowed, 0);
3750 if (!folio)
3751 break;
3753 list_add(&folio->lru, &page_list);
3755 /* free the pages after dropping lock */
3756 spin_unlock_irq(&hugetlb_lock);
3757 update_and_free_pages_bulk(h, &page_list);
3758 flush_free_hpage_work(h);
3759 spin_lock_irq(&hugetlb_lock);
3761 while (count < persistent_huge_pages(h)) {
3762 if (!adjust_pool_surplus(h, nodes_allowed, 1))
3763 break;
3765 out:
3766 h->max_huge_pages = persistent_huge_pages(h);
3767 spin_unlock_irq(&hugetlb_lock);
3768 mutex_unlock(&h->resize_lock);
3770 NODEMASK_FREE(node_alloc_noretry);
3772 return 0;
3775 static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
3776 struct list_head *src_list)
3778 long rc;
3779 struct folio *folio, *next;
3780 LIST_HEAD(dst_list);
3781 LIST_HEAD(ret_list);
3783 rc = hugetlb_vmemmap_restore_folios(src, src_list, &ret_list);
3784 list_splice_init(&ret_list, src_list);
3787 * Taking target hstate mutex synchronizes with set_max_huge_pages.
3788 * Without the mutex, pages added to target hstate could be marked
3789 * as surplus.
3791 * Note that we already hold src->resize_lock. To prevent deadlock,
3792 * use the convention of always taking larger size hstate mutex first.
3794 mutex_lock(&dst->resize_lock);
3796 list_for_each_entry_safe(folio, next, src_list, lru) {
3797 int i;
3799 if (folio_test_hugetlb_vmemmap_optimized(folio))
3800 continue;
3802 list_del(&folio->lru);
3804 split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
3805 pgalloc_tag_split(folio, huge_page_order(src), huge_page_order(dst));
3807 for (i = 0; i < pages_per_huge_page(src); i += pages_per_huge_page(dst)) {
3808 struct page *page = folio_page(folio, i);
3810 page->mapping = NULL;
3811 clear_compound_head(page);
3812 prep_compound_page(page, dst->order);
3814 init_new_hugetlb_folio(dst, page_folio(page));
3815 list_add(&page->lru, &dst_list);
3819 prep_and_add_allocated_folios(dst, &dst_list);
3821 mutex_unlock(&dst->resize_lock);
3823 return rc;
3826 static long demote_pool_huge_page(struct hstate *src, nodemask_t *nodes_allowed,
3827 unsigned long nr_to_demote)
3828 __must_hold(&hugetlb_lock)
3830 int nr_nodes, node;
3831 struct hstate *dst;
3832 long rc = 0;
3833 long nr_demoted = 0;
3835 lockdep_assert_held(&hugetlb_lock);
3837 /* We should never get here if no demote order */
3838 if (!src->demote_order) {
3839 pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n");
3840 return -EINVAL; /* internal error */
3842 dst = size_to_hstate(PAGE_SIZE << src->demote_order);
3844 for_each_node_mask_to_free(src, nr_nodes, node, nodes_allowed) {
3845 LIST_HEAD(list);
3846 struct folio *folio, *next;
3848 list_for_each_entry_safe(folio, next, &src->hugepage_freelists[node], lru) {
3849 if (folio_test_hwpoison(folio))
3850 continue;
3852 remove_hugetlb_folio(src, folio, false);
3853 list_add(&folio->lru, &list);
3855 if (++nr_demoted == nr_to_demote)
3856 break;
3859 spin_unlock_irq(&hugetlb_lock);
3861 rc = demote_free_hugetlb_folios(src, dst, &list);
3863 spin_lock_irq(&hugetlb_lock);
3865 list_for_each_entry_safe(folio, next, &list, lru) {
3866 list_del(&folio->lru);
3867 add_hugetlb_folio(src, folio, false);
3869 nr_demoted--;
3872 if (rc < 0 || nr_demoted == nr_to_demote)
3873 break;
3877 * Not absolutely necessary, but for consistency update max_huge_pages
3878 * based on pool changes for the demoted page.
3880 src->max_huge_pages -= nr_demoted;
3881 dst->max_huge_pages += nr_demoted << (huge_page_order(src) - huge_page_order(dst));
3883 if (rc < 0)
3884 return rc;
3886 if (nr_demoted)
3887 return nr_demoted;
3889 * Only way to get here is if all pages on free lists are poisoned.
3890 * Return -EBUSY so that caller will not retry.
3892 return -EBUSY;
3895 #define HSTATE_ATTR_RO(_name) \
3896 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
3898 #define HSTATE_ATTR_WO(_name) \
3899 static struct kobj_attribute _name##_attr = __ATTR_WO(_name)
3901 #define HSTATE_ATTR(_name) \
3902 static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
3904 static struct kobject *hugepages_kobj;
3905 static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
3907 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
3909 static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
3911 int i;
3913 for (i = 0; i < HUGE_MAX_HSTATE; i++)
3914 if (hstate_kobjs[i] == kobj) {
3915 if (nidp)
3916 *nidp = NUMA_NO_NODE;
3917 return &hstates[i];
3920 return kobj_to_node_hstate(kobj, nidp);
3923 static ssize_t nr_hugepages_show_common(struct kobject *kobj,
3924 struct kobj_attribute *attr, char *buf)
3926 struct hstate *h;
3927 unsigned long nr_huge_pages;
3928 int nid;
3930 h = kobj_to_hstate(kobj, &nid);
3931 if (nid == NUMA_NO_NODE)
3932 nr_huge_pages = h->nr_huge_pages;
3933 else
3934 nr_huge_pages = h->nr_huge_pages_node[nid];
3936 return sysfs_emit(buf, "%lu\n", nr_huge_pages);
3939 static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
3940 struct hstate *h, int nid,
3941 unsigned long count, size_t len)
3943 int err;
3944 nodemask_t nodes_allowed, *n_mask;
3946 if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3947 return -EINVAL;
3949 if (nid == NUMA_NO_NODE) {
3951 * global hstate attribute
3953 if (!(obey_mempolicy &&
3954 init_nodemask_of_mempolicy(&nodes_allowed)))
3955 n_mask = &node_states[N_MEMORY];
3956 else
3957 n_mask = &nodes_allowed;
3958 } else {
3960 * Node specific request. count adjustment happens in
3961 * set_max_huge_pages() after acquiring hugetlb_lock.
3963 init_nodemask_of_node(&nodes_allowed, nid);
3964 n_mask = &nodes_allowed;
3967 err = set_max_huge_pages(h, count, nid, n_mask);
3969 return err ? err : len;
3972 static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
3973 struct kobject *kobj, const char *buf,
3974 size_t len)
3976 struct hstate *h;
3977 unsigned long count;
3978 int nid;
3979 int err;
3981 err = kstrtoul(buf, 10, &count);
3982 if (err)
3983 return err;
3985 h = kobj_to_hstate(kobj, &nid);
3986 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
3989 static ssize_t nr_hugepages_show(struct kobject *kobj,
3990 struct kobj_attribute *attr, char *buf)
3992 return nr_hugepages_show_common(kobj, attr, buf);
3995 static ssize_t nr_hugepages_store(struct kobject *kobj,
3996 struct kobj_attribute *attr, const char *buf, size_t len)
3998 return nr_hugepages_store_common(false, kobj, buf, len);
4000 HSTATE_ATTR(nr_hugepages);
4002 #ifdef CONFIG_NUMA
4005 * hstate attribute for optionally mempolicy-based constraint on persistent
4006 * huge page alloc/free.
4008 static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
4009 struct kobj_attribute *attr,
4010 char *buf)
4012 return nr_hugepages_show_common(kobj, attr, buf);
4015 static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
4016 struct kobj_attribute *attr, const char *buf, size_t len)
4018 return nr_hugepages_store_common(true, kobj, buf, len);
4020 HSTATE_ATTR(nr_hugepages_mempolicy);
4021 #endif
4024 static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
4025 struct kobj_attribute *attr, char *buf)
4027 struct hstate *h = kobj_to_hstate(kobj, NULL);
4028 return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages);
4031 static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
4032 struct kobj_attribute *attr, const char *buf, size_t count)
4034 int err;
4035 unsigned long input;
4036 struct hstate *h = kobj_to_hstate(kobj, NULL);
4038 if (hstate_is_gigantic(h))
4039 return -EINVAL;
4041 err = kstrtoul(buf, 10, &input);
4042 if (err)
4043 return err;
4045 spin_lock_irq(&hugetlb_lock);
4046 h->nr_overcommit_huge_pages = input;
4047 spin_unlock_irq(&hugetlb_lock);
4049 return count;
4051 HSTATE_ATTR(nr_overcommit_hugepages);
4053 static ssize_t free_hugepages_show(struct kobject *kobj,
4054 struct kobj_attribute *attr, char *buf)
4056 struct hstate *h;
4057 unsigned long free_huge_pages;
4058 int nid;
4060 h = kobj_to_hstate(kobj, &nid);
4061 if (nid == NUMA_NO_NODE)
4062 free_huge_pages = h->free_huge_pages;
4063 else
4064 free_huge_pages = h->free_huge_pages_node[nid];
4066 return sysfs_emit(buf, "%lu\n", free_huge_pages);
4068 HSTATE_ATTR_RO(free_hugepages);
4070 static ssize_t resv_hugepages_show(struct kobject *kobj,
4071 struct kobj_attribute *attr, char *buf)
4073 struct hstate *h = kobj_to_hstate(kobj, NULL);
4074 return sysfs_emit(buf, "%lu\n", h->resv_huge_pages);
4076 HSTATE_ATTR_RO(resv_hugepages);
4078 static ssize_t surplus_hugepages_show(struct kobject *kobj,
4079 struct kobj_attribute *attr, char *buf)
4081 struct hstate *h;
4082 unsigned long surplus_huge_pages;
4083 int nid;
4085 h = kobj_to_hstate(kobj, &nid);
4086 if (nid == NUMA_NO_NODE)
4087 surplus_huge_pages = h->surplus_huge_pages;
4088 else
4089 surplus_huge_pages = h->surplus_huge_pages_node[nid];
4091 return sysfs_emit(buf, "%lu\n", surplus_huge_pages);
4093 HSTATE_ATTR_RO(surplus_hugepages);
4095 static ssize_t demote_store(struct kobject *kobj,
4096 struct kobj_attribute *attr, const char *buf, size_t len)
4098 unsigned long nr_demote;
4099 unsigned long nr_available;
4100 nodemask_t nodes_allowed, *n_mask;
4101 struct hstate *h;
4102 int err;
4103 int nid;
4105 err = kstrtoul(buf, 10, &nr_demote);
4106 if (err)
4107 return err;
4108 h = kobj_to_hstate(kobj, &nid);
4110 if (nid != NUMA_NO_NODE) {
4111 init_nodemask_of_node(&nodes_allowed, nid);
4112 n_mask = &nodes_allowed;
4113 } else {
4114 n_mask = &node_states[N_MEMORY];
4117 /* Synchronize with other sysfs operations modifying huge pages */
4118 mutex_lock(&h->resize_lock);
4119 spin_lock_irq(&hugetlb_lock);
4121 while (nr_demote) {
4122 long rc;
4125 * Check for available pages to demote each time thorough the
4126 * loop as demote_pool_huge_page will drop hugetlb_lock.
4128 if (nid != NUMA_NO_NODE)
4129 nr_available = h->free_huge_pages_node[nid];
4130 else
4131 nr_available = h->free_huge_pages;
4132 nr_available -= h->resv_huge_pages;
4133 if (!nr_available)
4134 break;
4136 rc = demote_pool_huge_page(h, n_mask, nr_demote);
4137 if (rc < 0) {
4138 err = rc;
4139 break;
4142 nr_demote -= rc;
4145 spin_unlock_irq(&hugetlb_lock);
4146 mutex_unlock(&h->resize_lock);
4148 if (err)
4149 return err;
4150 return len;
4152 HSTATE_ATTR_WO(demote);
4154 static ssize_t demote_size_show(struct kobject *kobj,
4155 struct kobj_attribute *attr, char *buf)
4157 struct hstate *h = kobj_to_hstate(kobj, NULL);
4158 unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
4160 return sysfs_emit(buf, "%lukB\n", demote_size);
4163 static ssize_t demote_size_store(struct kobject *kobj,
4164 struct kobj_attribute *attr,
4165 const char *buf, size_t count)
4167 struct hstate *h, *demote_hstate;
4168 unsigned long demote_size;
4169 unsigned int demote_order;
4171 demote_size = (unsigned long)memparse(buf, NULL);
4173 demote_hstate = size_to_hstate(demote_size);
4174 if (!demote_hstate)
4175 return -EINVAL;
4176 demote_order = demote_hstate->order;
4177 if (demote_order < HUGETLB_PAGE_ORDER)
4178 return -EINVAL;
4180 /* demote order must be smaller than hstate order */
4181 h = kobj_to_hstate(kobj, NULL);
4182 if (demote_order >= h->order)
4183 return -EINVAL;
4185 /* resize_lock synchronizes access to demote size and writes */
4186 mutex_lock(&h->resize_lock);
4187 h->demote_order = demote_order;
4188 mutex_unlock(&h->resize_lock);
4190 return count;
4192 HSTATE_ATTR(demote_size);
4194 static struct attribute *hstate_attrs[] = {
4195 &nr_hugepages_attr.attr,
4196 &nr_overcommit_hugepages_attr.attr,
4197 &free_hugepages_attr.attr,
4198 &resv_hugepages_attr.attr,
4199 &surplus_hugepages_attr.attr,
4200 #ifdef CONFIG_NUMA
4201 &nr_hugepages_mempolicy_attr.attr,
4202 #endif
4203 NULL,
4206 static const struct attribute_group hstate_attr_group = {
4207 .attrs = hstate_attrs,
4210 static struct attribute *hstate_demote_attrs[] = {
4211 &demote_size_attr.attr,
4212 &demote_attr.attr,
4213 NULL,
4216 static const struct attribute_group hstate_demote_attr_group = {
4217 .attrs = hstate_demote_attrs,
4220 static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
4221 struct kobject **hstate_kobjs,
4222 const struct attribute_group *hstate_attr_group)
4224 int retval;
4225 int hi = hstate_index(h);
4227 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
4228 if (!hstate_kobjs[hi])
4229 return -ENOMEM;
4231 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
4232 if (retval) {
4233 kobject_put(hstate_kobjs[hi]);
4234 hstate_kobjs[hi] = NULL;
4235 return retval;
4238 if (h->demote_order) {
4239 retval = sysfs_create_group(hstate_kobjs[hi],
4240 &hstate_demote_attr_group);
4241 if (retval) {
4242 pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name);
4243 sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group);
4244 kobject_put(hstate_kobjs[hi]);
4245 hstate_kobjs[hi] = NULL;
4246 return retval;
4250 return 0;
4253 #ifdef CONFIG_NUMA
4254 static bool hugetlb_sysfs_initialized __ro_after_init;
4257 * node_hstate/s - associate per node hstate attributes, via their kobjects,
4258 * with node devices in node_devices[] using a parallel array. The array
4259 * index of a node device or _hstate == node id.
4260 * This is here to avoid any static dependency of the node device driver, in
4261 * the base kernel, on the hugetlb module.
4263 struct node_hstate {
4264 struct kobject *hugepages_kobj;
4265 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
4267 static struct node_hstate node_hstates[MAX_NUMNODES];
4270 * A subset of global hstate attributes for node devices
4272 static struct attribute *per_node_hstate_attrs[] = {
4273 &nr_hugepages_attr.attr,
4274 &free_hugepages_attr.attr,
4275 &surplus_hugepages_attr.attr,
4276 NULL,
4279 static const struct attribute_group per_node_hstate_attr_group = {
4280 .attrs = per_node_hstate_attrs,
4284 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
4285 * Returns node id via non-NULL nidp.
4287 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4289 int nid;
4291 for (nid = 0; nid < nr_node_ids; nid++) {
4292 struct node_hstate *nhs = &node_hstates[nid];
4293 int i;
4294 for (i = 0; i < HUGE_MAX_HSTATE; i++)
4295 if (nhs->hstate_kobjs[i] == kobj) {
4296 if (nidp)
4297 *nidp = nid;
4298 return &hstates[i];
4302 BUG();
4303 return NULL;
4307 * Unregister hstate attributes from a single node device.
4308 * No-op if no hstate attributes attached.
4310 void hugetlb_unregister_node(struct node *node)
4312 struct hstate *h;
4313 struct node_hstate *nhs = &node_hstates[node->dev.id];
4315 if (!nhs->hugepages_kobj)
4316 return; /* no hstate attributes */
4318 for_each_hstate(h) {
4319 int idx = hstate_index(h);
4320 struct kobject *hstate_kobj = nhs->hstate_kobjs[idx];
4322 if (!hstate_kobj)
4323 continue;
4324 if (h->demote_order)
4325 sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group);
4326 sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group);
4327 kobject_put(hstate_kobj);
4328 nhs->hstate_kobjs[idx] = NULL;
4331 kobject_put(nhs->hugepages_kobj);
4332 nhs->hugepages_kobj = NULL;
4337 * Register hstate attributes for a single node device.
4338 * No-op if attributes already registered.
4340 void hugetlb_register_node(struct node *node)
4342 struct hstate *h;
4343 struct node_hstate *nhs = &node_hstates[node->dev.id];
4344 int err;
4346 if (!hugetlb_sysfs_initialized)
4347 return;
4349 if (nhs->hugepages_kobj)
4350 return; /* already allocated */
4352 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
4353 &node->dev.kobj);
4354 if (!nhs->hugepages_kobj)
4355 return;
4357 for_each_hstate(h) {
4358 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
4359 nhs->hstate_kobjs,
4360 &per_node_hstate_attr_group);
4361 if (err) {
4362 pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
4363 h->name, node->dev.id);
4364 hugetlb_unregister_node(node);
4365 break;
4371 * hugetlb init time: register hstate attributes for all registered node
4372 * devices of nodes that have memory. All on-line nodes should have
4373 * registered their associated device by this time.
4375 static void __init hugetlb_register_all_nodes(void)
4377 int nid;
4379 for_each_online_node(nid)
4380 hugetlb_register_node(node_devices[nid]);
4382 #else /* !CONFIG_NUMA */
4384 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4386 BUG();
4387 if (nidp)
4388 *nidp = -1;
4389 return NULL;
4392 static void hugetlb_register_all_nodes(void) { }
4394 #endif
4396 #ifdef CONFIG_CMA
4397 static void __init hugetlb_cma_check(void);
4398 #else
4399 static inline __init void hugetlb_cma_check(void)
4402 #endif
4404 static void __init hugetlb_sysfs_init(void)
4406 struct hstate *h;
4407 int err;
4409 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
4410 if (!hugepages_kobj)
4411 return;
4413 for_each_hstate(h) {
4414 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
4415 hstate_kobjs, &hstate_attr_group);
4416 if (err)
4417 pr_err("HugeTLB: Unable to add hstate %s", h->name);
4420 #ifdef CONFIG_NUMA
4421 hugetlb_sysfs_initialized = true;
4422 #endif
4423 hugetlb_register_all_nodes();
4426 #ifdef CONFIG_SYSCTL
4427 static void hugetlb_sysctl_init(void);
4428 #else
4429 static inline void hugetlb_sysctl_init(void) { }
4430 #endif
4432 static int __init hugetlb_init(void)
4434 int i;
4436 BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE <
4437 __NR_HPAGEFLAGS);
4439 if (!hugepages_supported()) {
4440 if (hugetlb_max_hstate || default_hstate_max_huge_pages)
4441 pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
4442 return 0;
4446 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some
4447 * architectures depend on setup being done here.
4449 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
4450 if (!parsed_default_hugepagesz) {
4452 * If we did not parse a default huge page size, set
4453 * default_hstate_idx to HPAGE_SIZE hstate. And, if the
4454 * number of huge pages for this default size was implicitly
4455 * specified, set that here as well.
4456 * Note that the implicit setting will overwrite an explicit
4457 * setting. A warning will be printed in this case.
4459 default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
4460 if (default_hstate_max_huge_pages) {
4461 if (default_hstate.max_huge_pages) {
4462 char buf[32];
4464 string_get_size(huge_page_size(&default_hstate),
4465 1, STRING_UNITS_2, buf, 32);
4466 pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
4467 default_hstate.max_huge_pages, buf);
4468 pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
4469 default_hstate_max_huge_pages);
4471 default_hstate.max_huge_pages =
4472 default_hstate_max_huge_pages;
4474 for_each_online_node(i)
4475 default_hstate.max_huge_pages_node[i] =
4476 default_hugepages_in_node[i];
4480 hugetlb_cma_check();
4481 hugetlb_init_hstates();
4482 gather_bootmem_prealloc();
4483 report_hugepages();
4485 hugetlb_sysfs_init();
4486 hugetlb_cgroup_file_init();
4487 hugetlb_sysctl_init();
4489 #ifdef CONFIG_SMP
4490 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
4491 #else
4492 num_fault_mutexes = 1;
4493 #endif
4494 hugetlb_fault_mutex_table =
4495 kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
4496 GFP_KERNEL);
4497 BUG_ON(!hugetlb_fault_mutex_table);
4499 for (i = 0; i < num_fault_mutexes; i++)
4500 mutex_init(&hugetlb_fault_mutex_table[i]);
4501 return 0;
4503 subsys_initcall(hugetlb_init);
4505 /* Overwritten by architectures with more huge page sizes */
4506 bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
4508 return size == HPAGE_SIZE;
4511 void __init hugetlb_add_hstate(unsigned int order)
4513 struct hstate *h;
4514 unsigned long i;
4516 if (size_to_hstate(PAGE_SIZE << order)) {
4517 return;
4519 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
4520 BUG_ON(order < order_base_2(__NR_USED_SUBPAGE));
4521 h = &hstates[hugetlb_max_hstate++];
4522 __mutex_init(&h->resize_lock, "resize mutex", &h->resize_key);
4523 h->order = order;
4524 h->mask = ~(huge_page_size(h) - 1);
4525 for (i = 0; i < MAX_NUMNODES; ++i)
4526 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
4527 INIT_LIST_HEAD(&h->hugepage_activelist);
4528 h->next_nid_to_alloc = first_memory_node;
4529 h->next_nid_to_free = first_memory_node;
4530 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
4531 huge_page_size(h)/SZ_1K);
4533 parsed_hstate = h;
4536 bool __init __weak hugetlb_node_alloc_supported(void)
4538 return true;
4541 static void __init hugepages_clear_pages_in_node(void)
4543 if (!hugetlb_max_hstate) {
4544 default_hstate_max_huge_pages = 0;
4545 memset(default_hugepages_in_node, 0,
4546 sizeof(default_hugepages_in_node));
4547 } else {
4548 parsed_hstate->max_huge_pages = 0;
4549 memset(parsed_hstate->max_huge_pages_node, 0,
4550 sizeof(parsed_hstate->max_huge_pages_node));
4555 * hugepages command line processing
4556 * hugepages normally follows a valid hugepagsz or default_hugepagsz
4557 * specification. If not, ignore the hugepages value. hugepages can also
4558 * be the first huge page command line option in which case it implicitly
4559 * specifies the number of huge pages for the default size.
4561 static int __init hugepages_setup(char *s)
4563 unsigned long *mhp;
4564 static unsigned long *last_mhp;
4565 int node = NUMA_NO_NODE;
4566 int count;
4567 unsigned long tmp;
4568 char *p = s;
4570 if (!parsed_valid_hugepagesz) {
4571 pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
4572 parsed_valid_hugepagesz = true;
4573 return 1;
4577 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
4578 * yet, so this hugepages= parameter goes to the "default hstate".
4579 * Otherwise, it goes with the previously parsed hugepagesz or
4580 * default_hugepagesz.
4582 else if (!hugetlb_max_hstate)
4583 mhp = &default_hstate_max_huge_pages;
4584 else
4585 mhp = &parsed_hstate->max_huge_pages;
4587 if (mhp == last_mhp) {
4588 pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
4589 return 1;
4592 while (*p) {
4593 count = 0;
4594 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4595 goto invalid;
4596 /* Parameter is node format */
4597 if (p[count] == ':') {
4598 if (!hugetlb_node_alloc_supported()) {
4599 pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n");
4600 return 1;
4602 if (tmp >= MAX_NUMNODES || !node_online(tmp))
4603 goto invalid;
4604 node = array_index_nospec(tmp, MAX_NUMNODES);
4605 p += count + 1;
4606 /* Parse hugepages */
4607 if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4608 goto invalid;
4609 if (!hugetlb_max_hstate)
4610 default_hugepages_in_node[node] = tmp;
4611 else
4612 parsed_hstate->max_huge_pages_node[node] = tmp;
4613 *mhp += tmp;
4614 /* Go to parse next node*/
4615 if (p[count] == ',')
4616 p += count + 1;
4617 else
4618 break;
4619 } else {
4620 if (p != s)
4621 goto invalid;
4622 *mhp = tmp;
4623 break;
4628 * Global state is always initialized later in hugetlb_init.
4629 * But we need to allocate gigantic hstates here early to still
4630 * use the bootmem allocator.
4632 if (hugetlb_max_hstate && hstate_is_gigantic(parsed_hstate))
4633 hugetlb_hstate_alloc_pages(parsed_hstate);
4635 last_mhp = mhp;
4637 return 1;
4639 invalid:
4640 pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
4641 hugepages_clear_pages_in_node();
4642 return 1;
4644 __setup("hugepages=", hugepages_setup);
4647 * hugepagesz command line processing
4648 * A specific huge page size can only be specified once with hugepagesz.
4649 * hugepagesz is followed by hugepages on the command line. The global
4650 * variable 'parsed_valid_hugepagesz' is used to determine if prior
4651 * hugepagesz argument was valid.
4653 static int __init hugepagesz_setup(char *s)
4655 unsigned long size;
4656 struct hstate *h;
4658 parsed_valid_hugepagesz = false;
4659 size = (unsigned long)memparse(s, NULL);
4661 if (!arch_hugetlb_valid_size(size)) {
4662 pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
4663 return 1;
4666 h = size_to_hstate(size);
4667 if (h) {
4669 * hstate for this size already exists. This is normally
4670 * an error, but is allowed if the existing hstate is the
4671 * default hstate. More specifically, it is only allowed if
4672 * the number of huge pages for the default hstate was not
4673 * previously specified.
4675 if (!parsed_default_hugepagesz || h != &default_hstate ||
4676 default_hstate.max_huge_pages) {
4677 pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
4678 return 1;
4682 * No need to call hugetlb_add_hstate() as hstate already
4683 * exists. But, do set parsed_hstate so that a following
4684 * hugepages= parameter will be applied to this hstate.
4686 parsed_hstate = h;
4687 parsed_valid_hugepagesz = true;
4688 return 1;
4691 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4692 parsed_valid_hugepagesz = true;
4693 return 1;
4695 __setup("hugepagesz=", hugepagesz_setup);
4698 * default_hugepagesz command line input
4699 * Only one instance of default_hugepagesz allowed on command line.
4701 static int __init default_hugepagesz_setup(char *s)
4703 unsigned long size;
4704 int i;
4706 parsed_valid_hugepagesz = false;
4707 if (parsed_default_hugepagesz) {
4708 pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
4709 return 1;
4712 size = (unsigned long)memparse(s, NULL);
4714 if (!arch_hugetlb_valid_size(size)) {
4715 pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
4716 return 1;
4719 hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4720 parsed_valid_hugepagesz = true;
4721 parsed_default_hugepagesz = true;
4722 default_hstate_idx = hstate_index(size_to_hstate(size));
4725 * The number of default huge pages (for this size) could have been
4726 * specified as the first hugetlb parameter: hugepages=X. If so,
4727 * then default_hstate_max_huge_pages is set. If the default huge
4728 * page size is gigantic (> MAX_PAGE_ORDER), then the pages must be
4729 * allocated here from bootmem allocator.
4731 if (default_hstate_max_huge_pages) {
4732 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
4733 for_each_online_node(i)
4734 default_hstate.max_huge_pages_node[i] =
4735 default_hugepages_in_node[i];
4736 if (hstate_is_gigantic(&default_hstate))
4737 hugetlb_hstate_alloc_pages(&default_hstate);
4738 default_hstate_max_huge_pages = 0;
4741 return 1;
4743 __setup("default_hugepagesz=", default_hugepagesz_setup);
4745 static unsigned int allowed_mems_nr(struct hstate *h)
4747 int node;
4748 unsigned int nr = 0;
4749 nodemask_t *mbind_nodemask;
4750 unsigned int *array = h->free_huge_pages_node;
4751 gfp_t gfp_mask = htlb_alloc_mask(h);
4753 mbind_nodemask = policy_mbind_nodemask(gfp_mask);
4754 for_each_node_mask(node, cpuset_current_mems_allowed) {
4755 if (!mbind_nodemask || node_isset(node, *mbind_nodemask))
4756 nr += array[node];
4759 return nr;
4762 #ifdef CONFIG_SYSCTL
4763 static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, int write,
4764 void *buffer, size_t *length,
4765 loff_t *ppos, unsigned long *out)
4767 struct ctl_table dup_table;
4770 * In order to avoid races with __do_proc_doulongvec_minmax(), we
4771 * can duplicate the @table and alter the duplicate of it.
4773 dup_table = *table;
4774 dup_table.data = out;
4776 return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos);
4779 static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
4780 const struct ctl_table *table, int write,
4781 void *buffer, size_t *length, loff_t *ppos)
4783 struct hstate *h = &default_hstate;
4784 unsigned long tmp = h->max_huge_pages;
4785 int ret;
4787 if (!hugepages_supported())
4788 return -EOPNOTSUPP;
4790 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4791 &tmp);
4792 if (ret)
4793 goto out;
4795 if (write)
4796 ret = __nr_hugepages_store_common(obey_mempolicy, h,
4797 NUMA_NO_NODE, tmp, *length);
4798 out:
4799 return ret;
4802 static int hugetlb_sysctl_handler(const struct ctl_table *table, int write,
4803 void *buffer, size_t *length, loff_t *ppos)
4806 return hugetlb_sysctl_handler_common(false, table, write,
4807 buffer, length, ppos);
4810 #ifdef CONFIG_NUMA
4811 static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table, int write,
4812 void *buffer, size_t *length, loff_t *ppos)
4814 return hugetlb_sysctl_handler_common(true, table, write,
4815 buffer, length, ppos);
4817 #endif /* CONFIG_NUMA */
4819 static int hugetlb_overcommit_handler(const struct ctl_table *table, int write,
4820 void *buffer, size_t *length, loff_t *ppos)
4822 struct hstate *h = &default_hstate;
4823 unsigned long tmp;
4824 int ret;
4826 if (!hugepages_supported())
4827 return -EOPNOTSUPP;
4829 tmp = h->nr_overcommit_huge_pages;
4831 if (write && hstate_is_gigantic(h))
4832 return -EINVAL;
4834 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
4835 &tmp);
4836 if (ret)
4837 goto out;
4839 if (write) {
4840 spin_lock_irq(&hugetlb_lock);
4841 h->nr_overcommit_huge_pages = tmp;
4842 spin_unlock_irq(&hugetlb_lock);
4844 out:
4845 return ret;
4848 static struct ctl_table hugetlb_table[] = {
4850 .procname = "nr_hugepages",
4851 .data = NULL,
4852 .maxlen = sizeof(unsigned long),
4853 .mode = 0644,
4854 .proc_handler = hugetlb_sysctl_handler,
4856 #ifdef CONFIG_NUMA
4858 .procname = "nr_hugepages_mempolicy",
4859 .data = NULL,
4860 .maxlen = sizeof(unsigned long),
4861 .mode = 0644,
4862 .proc_handler = &hugetlb_mempolicy_sysctl_handler,
4864 #endif
4866 .procname = "hugetlb_shm_group",
4867 .data = &sysctl_hugetlb_shm_group,
4868 .maxlen = sizeof(gid_t),
4869 .mode = 0644,
4870 .proc_handler = proc_dointvec,
4873 .procname = "nr_overcommit_hugepages",
4874 .data = NULL,
4875 .maxlen = sizeof(unsigned long),
4876 .mode = 0644,
4877 .proc_handler = hugetlb_overcommit_handler,
4881 static void hugetlb_sysctl_init(void)
4883 register_sysctl_init("vm", hugetlb_table);
4885 #endif /* CONFIG_SYSCTL */
4887 void hugetlb_report_meminfo(struct seq_file *m)
4889 struct hstate *h;
4890 unsigned long total = 0;
4892 if (!hugepages_supported())
4893 return;
4895 for_each_hstate(h) {
4896 unsigned long count = h->nr_huge_pages;
4898 total += huge_page_size(h) * count;
4900 if (h == &default_hstate)
4901 seq_printf(m,
4902 "HugePages_Total: %5lu\n"
4903 "HugePages_Free: %5lu\n"
4904 "HugePages_Rsvd: %5lu\n"
4905 "HugePages_Surp: %5lu\n"
4906 "Hugepagesize: %8lu kB\n",
4907 count,
4908 h->free_huge_pages,
4909 h->resv_huge_pages,
4910 h->surplus_huge_pages,
4911 huge_page_size(h) / SZ_1K);
4914 seq_printf(m, "Hugetlb: %8lu kB\n", total / SZ_1K);
4917 int hugetlb_report_node_meminfo(char *buf, int len, int nid)
4919 struct hstate *h = &default_hstate;
4921 if (!hugepages_supported())
4922 return 0;
4924 return sysfs_emit_at(buf, len,
4925 "Node %d HugePages_Total: %5u\n"
4926 "Node %d HugePages_Free: %5u\n"
4927 "Node %d HugePages_Surp: %5u\n",
4928 nid, h->nr_huge_pages_node[nid],
4929 nid, h->free_huge_pages_node[nid],
4930 nid, h->surplus_huge_pages_node[nid]);
4933 void hugetlb_show_meminfo_node(int nid)
4935 struct hstate *h;
4937 if (!hugepages_supported())
4938 return;
4940 for_each_hstate(h)
4941 printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
4942 nid,
4943 h->nr_huge_pages_node[nid],
4944 h->free_huge_pages_node[nid],
4945 h->surplus_huge_pages_node[nid],
4946 huge_page_size(h) / SZ_1K);
4949 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
4951 seq_printf(m, "HugetlbPages:\t%8lu kB\n",
4952 K(atomic_long_read(&mm->hugetlb_usage)));
4955 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
4956 unsigned long hugetlb_total_pages(void)
4958 struct hstate *h;
4959 unsigned long nr_total_pages = 0;
4961 for_each_hstate(h)
4962 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
4963 return nr_total_pages;
4966 static int hugetlb_acct_memory(struct hstate *h, long delta)
4968 int ret = -ENOMEM;
4970 if (!delta)
4971 return 0;
4973 spin_lock_irq(&hugetlb_lock);
4975 * When cpuset is configured, it breaks the strict hugetlb page
4976 * reservation as the accounting is done on a global variable. Such
4977 * reservation is completely rubbish in the presence of cpuset because
4978 * the reservation is not checked against page availability for the
4979 * current cpuset. Application can still potentially OOM'ed by kernel
4980 * with lack of free htlb page in cpuset that the task is in.
4981 * Attempt to enforce strict accounting with cpuset is almost
4982 * impossible (or too ugly) because cpuset is too fluid that
4983 * task or memory node can be dynamically moved between cpusets.
4985 * The change of semantics for shared hugetlb mapping with cpuset is
4986 * undesirable. However, in order to preserve some of the semantics,
4987 * we fall back to check against current free page availability as
4988 * a best attempt and hopefully to minimize the impact of changing
4989 * semantics that cpuset has.
4991 * Apart from cpuset, we also have memory policy mechanism that
4992 * also determines from which node the kernel will allocate memory
4993 * in a NUMA system. So similar to cpuset, we also should consider
4994 * the memory policy of the current task. Similar to the description
4995 * above.
4997 if (delta > 0) {
4998 if (gather_surplus_pages(h, delta) < 0)
4999 goto out;
5001 if (delta > allowed_mems_nr(h)) {
5002 return_unused_surplus_pages(h, delta);
5003 goto out;
5007 ret = 0;
5008 if (delta < 0)
5009 return_unused_surplus_pages(h, (unsigned long) -delta);
5011 out:
5012 spin_unlock_irq(&hugetlb_lock);
5013 return ret;
5016 static void hugetlb_vm_op_open(struct vm_area_struct *vma)
5018 struct resv_map *resv = vma_resv_map(vma);
5021 * HPAGE_RESV_OWNER indicates a private mapping.
5022 * This new VMA should share its siblings reservation map if present.
5023 * The VMA will only ever have a valid reservation map pointer where
5024 * it is being copied for another still existing VMA. As that VMA
5025 * has a reference to the reservation map it cannot disappear until
5026 * after this open call completes. It is therefore safe to take a
5027 * new reference here without additional locking.
5029 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
5030 resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
5031 kref_get(&resv->refs);
5035 * vma_lock structure for sharable mappings is vma specific.
5036 * Clear old pointer (if copied via vm_area_dup) and allocate
5037 * new structure. Before clearing, make sure vma_lock is not
5038 * for this vma.
5040 if (vma->vm_flags & VM_MAYSHARE) {
5041 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
5043 if (vma_lock) {
5044 if (vma_lock->vma != vma) {
5045 vma->vm_private_data = NULL;
5046 hugetlb_vma_lock_alloc(vma);
5047 } else
5048 pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__);
5049 } else
5050 hugetlb_vma_lock_alloc(vma);
5054 static void hugetlb_vm_op_close(struct vm_area_struct *vma)
5056 struct hstate *h = hstate_vma(vma);
5057 struct resv_map *resv;
5058 struct hugepage_subpool *spool = subpool_vma(vma);
5059 unsigned long reserve, start, end;
5060 long gbl_reserve;
5062 hugetlb_vma_lock_free(vma);
5064 resv = vma_resv_map(vma);
5065 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
5066 return;
5068 start = vma_hugecache_offset(h, vma, vma->vm_start);
5069 end = vma_hugecache_offset(h, vma, vma->vm_end);
5071 reserve = (end - start) - region_count(resv, start, end);
5072 hugetlb_cgroup_uncharge_counter(resv, start, end);
5073 if (reserve) {
5075 * Decrement reserve counts. The global reserve count may be
5076 * adjusted if the subpool has a minimum size.
5078 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
5079 hugetlb_acct_memory(h, -gbl_reserve);
5082 kref_put(&resv->refs, resv_map_release);
5085 static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
5087 if (addr & ~(huge_page_mask(hstate_vma(vma))))
5088 return -EINVAL;
5091 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
5092 * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
5093 * split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
5095 if (addr & ~PUD_MASK) {
5097 * hugetlb_vm_op_split is called right before we attempt to
5098 * split the VMA. We will need to unshare PMDs in the old and
5099 * new VMAs, so let's unshare before we split.
5101 unsigned long floor = addr & PUD_MASK;
5102 unsigned long ceil = floor + PUD_SIZE;
5104 if (floor >= vma->vm_start && ceil <= vma->vm_end)
5105 hugetlb_unshare_pmds(vma, floor, ceil);
5108 return 0;
5111 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
5113 return huge_page_size(hstate_vma(vma));
5117 * We cannot handle pagefaults against hugetlb pages at all. They cause
5118 * handle_mm_fault() to try to instantiate regular-sized pages in the
5119 * hugepage VMA. do_page_fault() is supposed to trap this, so BUG is we get
5120 * this far.
5122 static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
5124 BUG();
5125 return 0;
5129 * When a new function is introduced to vm_operations_struct and added
5130 * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
5131 * This is because under System V memory model, mappings created via
5132 * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
5133 * their original vm_ops are overwritten with shm_vm_ops.
5135 const struct vm_operations_struct hugetlb_vm_ops = {
5136 .fault = hugetlb_vm_op_fault,
5137 .open = hugetlb_vm_op_open,
5138 .close = hugetlb_vm_op_close,
5139 .may_split = hugetlb_vm_op_split,
5140 .pagesize = hugetlb_vm_op_pagesize,
5143 static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
5144 int writable)
5146 pte_t entry;
5147 unsigned int shift = huge_page_shift(hstate_vma(vma));
5149 if (writable) {
5150 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
5151 vma->vm_page_prot)));
5152 } else {
5153 entry = huge_pte_wrprotect(mk_huge_pte(page,
5154 vma->vm_page_prot));
5156 entry = pte_mkyoung(entry);
5157 entry = arch_make_huge_pte(entry, shift, vma->vm_flags);
5159 return entry;
5162 static void set_huge_ptep_writable(struct vm_area_struct *vma,
5163 unsigned long address, pte_t *ptep)
5165 pte_t entry;
5167 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(vma->vm_mm, address, ptep)));
5168 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
5169 update_mmu_cache(vma, address, ptep);
5172 bool is_hugetlb_entry_migration(pte_t pte)
5174 swp_entry_t swp;
5176 if (huge_pte_none(pte) || pte_present(pte))
5177 return false;
5178 swp = pte_to_swp_entry(pte);
5179 if (is_migration_entry(swp))
5180 return true;
5181 else
5182 return false;
5185 bool is_hugetlb_entry_hwpoisoned(pte_t pte)
5187 swp_entry_t swp;
5189 if (huge_pte_none(pte) || pte_present(pte))
5190 return false;
5191 swp = pte_to_swp_entry(pte);
5192 if (is_hwpoison_entry(swp))
5193 return true;
5194 else
5195 return false;
5198 static void
5199 hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr,
5200 struct folio *new_folio, pte_t old, unsigned long sz)
5202 pte_t newpte = make_huge_pte(vma, &new_folio->page, 1);
5204 __folio_mark_uptodate(new_folio);
5205 hugetlb_add_new_anon_rmap(new_folio, vma, addr);
5206 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old))
5207 newpte = huge_pte_mkuffd_wp(newpte);
5208 set_huge_pte_at(vma->vm_mm, addr, ptep, newpte, sz);
5209 hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm);
5210 folio_set_hugetlb_migratable(new_folio);
5213 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
5214 struct vm_area_struct *dst_vma,
5215 struct vm_area_struct *src_vma)
5217 pte_t *src_pte, *dst_pte, entry;
5218 struct folio *pte_folio;
5219 unsigned long addr;
5220 bool cow = is_cow_mapping(src_vma->vm_flags);
5221 struct hstate *h = hstate_vma(src_vma);
5222 unsigned long sz = huge_page_size(h);
5223 unsigned long npages = pages_per_huge_page(h);
5224 struct mmu_notifier_range range;
5225 unsigned long last_addr_mask;
5226 int ret = 0;
5228 if (cow) {
5229 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src,
5230 src_vma->vm_start,
5231 src_vma->vm_end);
5232 mmu_notifier_invalidate_range_start(&range);
5233 vma_assert_write_locked(src_vma);
5234 raw_write_seqcount_begin(&src->write_protect_seq);
5235 } else {
5237 * For shared mappings the vma lock must be held before
5238 * calling hugetlb_walk() in the src vma. Otherwise, the
5239 * returned ptep could go away if part of a shared pmd and
5240 * another thread calls huge_pmd_unshare.
5242 hugetlb_vma_lock_read(src_vma);
5245 last_addr_mask = hugetlb_mask_last_page(h);
5246 for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
5247 spinlock_t *src_ptl, *dst_ptl;
5248 src_pte = hugetlb_walk(src_vma, addr, sz);
5249 if (!src_pte) {
5250 addr |= last_addr_mask;
5251 continue;
5253 dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz);
5254 if (!dst_pte) {
5255 ret = -ENOMEM;
5256 break;
5260 * If the pagetables are shared don't copy or take references.
5262 * dst_pte == src_pte is the common case of src/dest sharing.
5263 * However, src could have 'unshared' and dst shares with
5264 * another vma. So page_count of ptep page is checked instead
5265 * to reliably determine whether pte is shared.
5267 if (page_count(virt_to_page(dst_pte)) > 1) {
5268 addr |= last_addr_mask;
5269 continue;
5272 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5273 src_ptl = huge_pte_lockptr(h, src, src_pte);
5274 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5275 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5276 again:
5277 if (huge_pte_none(entry)) {
5279 * Skip if src entry none.
5282 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
5283 if (!userfaultfd_wp(dst_vma))
5284 entry = huge_pte_clear_uffd_wp(entry);
5285 set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5286 } else if (unlikely(is_hugetlb_entry_migration(entry))) {
5287 swp_entry_t swp_entry = pte_to_swp_entry(entry);
5288 bool uffd_wp = pte_swp_uffd_wp(entry);
5290 if (!is_readable_migration_entry(swp_entry) && cow) {
5292 * COW mappings require pages in both
5293 * parent and child to be set to read.
5295 swp_entry = make_readable_migration_entry(
5296 swp_offset(swp_entry));
5297 entry = swp_entry_to_pte(swp_entry);
5298 if (userfaultfd_wp(src_vma) && uffd_wp)
5299 entry = pte_swp_mkuffd_wp(entry);
5300 set_huge_pte_at(src, addr, src_pte, entry, sz);
5302 if (!userfaultfd_wp(dst_vma))
5303 entry = huge_pte_clear_uffd_wp(entry);
5304 set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5305 } else if (unlikely(is_pte_marker(entry))) {
5306 pte_marker marker = copy_pte_marker(
5307 pte_to_swp_entry(entry), dst_vma);
5309 if (marker)
5310 set_huge_pte_at(dst, addr, dst_pte,
5311 make_pte_marker(marker), sz);
5312 } else {
5313 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5314 pte_folio = page_folio(pte_page(entry));
5315 folio_get(pte_folio);
5318 * Failing to duplicate the anon rmap is a rare case
5319 * where we see pinned hugetlb pages while they're
5320 * prone to COW. We need to do the COW earlier during
5321 * fork.
5323 * When pre-allocating the page or copying data, we
5324 * need to be without the pgtable locks since we could
5325 * sleep during the process.
5327 if (!folio_test_anon(pte_folio)) {
5328 hugetlb_add_file_rmap(pte_folio);
5329 } else if (hugetlb_try_dup_anon_rmap(pte_folio, src_vma)) {
5330 pte_t src_pte_old = entry;
5331 struct folio *new_folio;
5333 spin_unlock(src_ptl);
5334 spin_unlock(dst_ptl);
5335 /* Do not use reserve as it's private owned */
5336 new_folio = alloc_hugetlb_folio(dst_vma, addr, 1);
5337 if (IS_ERR(new_folio)) {
5338 folio_put(pte_folio);
5339 ret = PTR_ERR(new_folio);
5340 break;
5342 ret = copy_user_large_folio(new_folio, pte_folio,
5343 ALIGN_DOWN(addr, sz), dst_vma);
5344 folio_put(pte_folio);
5345 if (ret) {
5346 folio_put(new_folio);
5347 break;
5350 /* Install the new hugetlb folio if src pte stable */
5351 dst_ptl = huge_pte_lock(h, dst, dst_pte);
5352 src_ptl = huge_pte_lockptr(h, src, src_pte);
5353 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5354 entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5355 if (!pte_same(src_pte_old, entry)) {
5356 restore_reserve_on_error(h, dst_vma, addr,
5357 new_folio);
5358 folio_put(new_folio);
5359 /* huge_ptep of dst_pte won't change as in child */
5360 goto again;
5362 hugetlb_install_folio(dst_vma, dst_pte, addr,
5363 new_folio, src_pte_old, sz);
5364 spin_unlock(src_ptl);
5365 spin_unlock(dst_ptl);
5366 continue;
5369 if (cow) {
5371 * No need to notify as we are downgrading page
5372 * table protection not changing it to point
5373 * to a new page.
5375 * See Documentation/mm/mmu_notifier.rst
5377 huge_ptep_set_wrprotect(src, addr, src_pte);
5378 entry = huge_pte_wrprotect(entry);
5381 if (!userfaultfd_wp(dst_vma))
5382 entry = huge_pte_clear_uffd_wp(entry);
5384 set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5385 hugetlb_count_add(npages, dst);
5387 spin_unlock(src_ptl);
5388 spin_unlock(dst_ptl);
5391 if (cow) {
5392 raw_write_seqcount_end(&src->write_protect_seq);
5393 mmu_notifier_invalidate_range_end(&range);
5394 } else {
5395 hugetlb_vma_unlock_read(src_vma);
5398 return ret;
5401 static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
5402 unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte,
5403 unsigned long sz)
5405 struct hstate *h = hstate_vma(vma);
5406 struct mm_struct *mm = vma->vm_mm;
5407 spinlock_t *src_ptl, *dst_ptl;
5408 pte_t pte;
5410 dst_ptl = huge_pte_lock(h, mm, dst_pte);
5411 src_ptl = huge_pte_lockptr(h, mm, src_pte);
5414 * We don't have to worry about the ordering of src and dst ptlocks
5415 * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
5417 if (src_ptl != dst_ptl)
5418 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5420 pte = huge_ptep_get_and_clear(mm, old_addr, src_pte);
5421 set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
5423 if (src_ptl != dst_ptl)
5424 spin_unlock(src_ptl);
5425 spin_unlock(dst_ptl);
5428 int move_hugetlb_page_tables(struct vm_area_struct *vma,
5429 struct vm_area_struct *new_vma,
5430 unsigned long old_addr, unsigned long new_addr,
5431 unsigned long len)
5433 struct hstate *h = hstate_vma(vma);
5434 struct address_space *mapping = vma->vm_file->f_mapping;
5435 unsigned long sz = huge_page_size(h);
5436 struct mm_struct *mm = vma->vm_mm;
5437 unsigned long old_end = old_addr + len;
5438 unsigned long last_addr_mask;
5439 pte_t *src_pte, *dst_pte;
5440 struct mmu_notifier_range range;
5441 bool shared_pmd = false;
5443 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr,
5444 old_end);
5445 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5447 * In case of shared PMDs, we should cover the maximum possible
5448 * range.
5450 flush_cache_range(vma, range.start, range.end);
5452 mmu_notifier_invalidate_range_start(&range);
5453 last_addr_mask = hugetlb_mask_last_page(h);
5454 /* Prevent race with file truncation */
5455 hugetlb_vma_lock_write(vma);
5456 i_mmap_lock_write(mapping);
5457 for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
5458 src_pte = hugetlb_walk(vma, old_addr, sz);
5459 if (!src_pte) {
5460 old_addr |= last_addr_mask;
5461 new_addr |= last_addr_mask;
5462 continue;
5464 if (huge_pte_none(huge_ptep_get(mm, old_addr, src_pte)))
5465 continue;
5467 if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) {
5468 shared_pmd = true;
5469 old_addr |= last_addr_mask;
5470 new_addr |= last_addr_mask;
5471 continue;
5474 dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
5475 if (!dst_pte)
5476 break;
5478 move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte, sz);
5481 if (shared_pmd)
5482 flush_hugetlb_tlb_range(vma, range.start, range.end);
5483 else
5484 flush_hugetlb_tlb_range(vma, old_end - len, old_end);
5485 mmu_notifier_invalidate_range_end(&range);
5486 i_mmap_unlock_write(mapping);
5487 hugetlb_vma_unlock_write(vma);
5489 return len + old_addr - old_end;
5492 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
5493 unsigned long start, unsigned long end,
5494 struct page *ref_page, zap_flags_t zap_flags)
5496 struct mm_struct *mm = vma->vm_mm;
5497 unsigned long address;
5498 pte_t *ptep;
5499 pte_t pte;
5500 spinlock_t *ptl;
5501 struct page *page;
5502 struct hstate *h = hstate_vma(vma);
5503 unsigned long sz = huge_page_size(h);
5504 bool adjust_reservation = false;
5505 unsigned long last_addr_mask;
5506 bool force_flush = false;
5508 WARN_ON(!is_vm_hugetlb_page(vma));
5509 BUG_ON(start & ~huge_page_mask(h));
5510 BUG_ON(end & ~huge_page_mask(h));
5513 * This is a hugetlb vma, all the pte entries should point
5514 * to huge page.
5516 tlb_change_page_size(tlb, sz);
5517 tlb_start_vma(tlb, vma);
5519 last_addr_mask = hugetlb_mask_last_page(h);
5520 address = start;
5521 for (; address < end; address += sz) {
5522 ptep = hugetlb_walk(vma, address, sz);
5523 if (!ptep) {
5524 address |= last_addr_mask;
5525 continue;
5528 ptl = huge_pte_lock(h, mm, ptep);
5529 if (huge_pmd_unshare(mm, vma, address, ptep)) {
5530 spin_unlock(ptl);
5531 tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
5532 force_flush = true;
5533 address |= last_addr_mask;
5534 continue;
5537 pte = huge_ptep_get(mm, address, ptep);
5538 if (huge_pte_none(pte)) {
5539 spin_unlock(ptl);
5540 continue;
5544 * Migrating hugepage or HWPoisoned hugepage is already
5545 * unmapped and its refcount is dropped, so just clear pte here.
5547 if (unlikely(!pte_present(pte))) {
5549 * If the pte was wr-protected by uffd-wp in any of the
5550 * swap forms, meanwhile the caller does not want to
5551 * drop the uffd-wp bit in this zap, then replace the
5552 * pte with a marker.
5554 if (pte_swp_uffd_wp_any(pte) &&
5555 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5556 set_huge_pte_at(mm, address, ptep,
5557 make_pte_marker(PTE_MARKER_UFFD_WP),
5558 sz);
5559 else
5560 huge_pte_clear(mm, address, ptep, sz);
5561 spin_unlock(ptl);
5562 continue;
5565 page = pte_page(pte);
5567 * If a reference page is supplied, it is because a specific
5568 * page is being unmapped, not a range. Ensure the page we
5569 * are about to unmap is the actual page of interest.
5571 if (ref_page) {
5572 if (page != ref_page) {
5573 spin_unlock(ptl);
5574 continue;
5577 * Mark the VMA as having unmapped its page so that
5578 * future faults in this VMA will fail rather than
5579 * looking like data was lost
5581 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
5584 pte = huge_ptep_get_and_clear(mm, address, ptep);
5585 tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
5586 if (huge_pte_dirty(pte))
5587 set_page_dirty(page);
5588 /* Leave a uffd-wp pte marker if needed */
5589 if (huge_pte_uffd_wp(pte) &&
5590 !(zap_flags & ZAP_FLAG_DROP_MARKER))
5591 set_huge_pte_at(mm, address, ptep,
5592 make_pte_marker(PTE_MARKER_UFFD_WP),
5593 sz);
5594 hugetlb_count_sub(pages_per_huge_page(h), mm);
5595 hugetlb_remove_rmap(page_folio(page));
5598 * Restore the reservation for anonymous page, otherwise the
5599 * backing page could be stolen by someone.
5600 * If there we are freeing a surplus, do not set the restore
5601 * reservation bit.
5603 if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
5604 folio_test_anon(page_folio(page))) {
5605 folio_set_hugetlb_restore_reserve(page_folio(page));
5606 /* Reservation to be adjusted after the spin lock */
5607 adjust_reservation = true;
5610 spin_unlock(ptl);
5613 * Adjust the reservation for the region that will have the
5614 * reserve restored. Keep in mind that vma_needs_reservation() changes
5615 * resv->adds_in_progress if it succeeds. If this is not done,
5616 * do_exit() will not see it, and will keep the reservation
5617 * forever.
5619 if (adjust_reservation) {
5620 int rc = vma_needs_reservation(h, vma, address);
5622 if (rc < 0)
5623 /* Pressumably allocate_file_region_entries failed
5624 * to allocate a file_region struct. Clear
5625 * hugetlb_restore_reserve so that global reserve
5626 * count will not be incremented by free_huge_folio.
5627 * Act as if we consumed the reservation.
5629 folio_clear_hugetlb_restore_reserve(page_folio(page));
5630 else if (rc)
5631 vma_add_reservation(h, vma, address);
5634 tlb_remove_page_size(tlb, page, huge_page_size(h));
5636 * Bail out after unmapping reference page if supplied
5638 if (ref_page)
5639 break;
5641 tlb_end_vma(tlb, vma);
5644 * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
5645 * could defer the flush until now, since by holding i_mmap_rwsem we
5646 * guaranteed that the last refernece would not be dropped. But we must
5647 * do the flushing before we return, as otherwise i_mmap_rwsem will be
5648 * dropped and the last reference to the shared PMDs page might be
5649 * dropped as well.
5651 * In theory we could defer the freeing of the PMD pages as well, but
5652 * huge_pmd_unshare() relies on the exact page_count for the PMD page to
5653 * detect sharing, so we cannot defer the release of the page either.
5654 * Instead, do flush now.
5656 if (force_flush)
5657 tlb_flush_mmu_tlbonly(tlb);
5660 void __hugetlb_zap_begin(struct vm_area_struct *vma,
5661 unsigned long *start, unsigned long *end)
5663 if (!vma->vm_file) /* hugetlbfs_file_mmap error */
5664 return;
5666 adjust_range_if_pmd_sharing_possible(vma, start, end);
5667 hugetlb_vma_lock_write(vma);
5668 if (vma->vm_file)
5669 i_mmap_lock_write(vma->vm_file->f_mapping);
5672 void __hugetlb_zap_end(struct vm_area_struct *vma,
5673 struct zap_details *details)
5675 zap_flags_t zap_flags = details ? details->zap_flags : 0;
5677 if (!vma->vm_file) /* hugetlbfs_file_mmap error */
5678 return;
5680 if (zap_flags & ZAP_FLAG_UNMAP) { /* final unmap */
5682 * Unlock and free the vma lock before releasing i_mmap_rwsem.
5683 * When the vma_lock is freed, this makes the vma ineligible
5684 * for pmd sharing. And, i_mmap_rwsem is required to set up
5685 * pmd sharing. This is important as page tables for this
5686 * unmapped range will be asynchrously deleted. If the page
5687 * tables are shared, there will be issues when accessed by
5688 * someone else.
5690 __hugetlb_vma_unlock_write_free(vma);
5691 } else {
5692 hugetlb_vma_unlock_write(vma);
5695 if (vma->vm_file)
5696 i_mmap_unlock_write(vma->vm_file->f_mapping);
5699 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
5700 unsigned long end, struct page *ref_page,
5701 zap_flags_t zap_flags)
5703 struct mmu_notifier_range range;
5704 struct mmu_gather tlb;
5706 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
5707 start, end);
5708 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5709 mmu_notifier_invalidate_range_start(&range);
5710 tlb_gather_mmu(&tlb, vma->vm_mm);
5712 __unmap_hugepage_range(&tlb, vma, start, end, ref_page, zap_flags);
5714 mmu_notifier_invalidate_range_end(&range);
5715 tlb_finish_mmu(&tlb);
5719 * This is called when the original mapper is failing to COW a MAP_PRIVATE
5720 * mapping it owns the reserve page for. The intention is to unmap the page
5721 * from other VMAs and let the children be SIGKILLed if they are faulting the
5722 * same region.
5724 static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
5725 struct page *page, unsigned long address)
5727 struct hstate *h = hstate_vma(vma);
5728 struct vm_area_struct *iter_vma;
5729 struct address_space *mapping;
5730 pgoff_t pgoff;
5733 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
5734 * from page cache lookup which is in HPAGE_SIZE units.
5736 address = address & huge_page_mask(h);
5737 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
5738 vma->vm_pgoff;
5739 mapping = vma->vm_file->f_mapping;
5742 * Take the mapping lock for the duration of the table walk. As
5743 * this mapping should be shared between all the VMAs,
5744 * __unmap_hugepage_range() is called as the lock is already held
5746 i_mmap_lock_write(mapping);
5747 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
5748 /* Do not unmap the current VMA */
5749 if (iter_vma == vma)
5750 continue;
5753 * Shared VMAs have their own reserves and do not affect
5754 * MAP_PRIVATE accounting but it is possible that a shared
5755 * VMA is using the same page so check and skip such VMAs.
5757 if (iter_vma->vm_flags & VM_MAYSHARE)
5758 continue;
5761 * Unmap the page from other VMAs without their own reserves.
5762 * They get marked to be SIGKILLed if they fault in these
5763 * areas. This is because a future no-page fault on this VMA
5764 * could insert a zeroed page instead of the data existing
5765 * from the time of fork. This would look like data corruption
5767 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
5768 unmap_hugepage_range(iter_vma, address,
5769 address + huge_page_size(h), page, 0);
5771 i_mmap_unlock_write(mapping);
5775 * hugetlb_wp() should be called with page lock of the original hugepage held.
5776 * Called with hugetlb_fault_mutex_table held and pte_page locked so we
5777 * cannot race with other handlers or page migration.
5778 * Keep the pte_same checks anyway to make transition from the mutex easier.
5780 static vm_fault_t hugetlb_wp(struct folio *pagecache_folio,
5781 struct vm_fault *vmf)
5783 struct vm_area_struct *vma = vmf->vma;
5784 struct mm_struct *mm = vma->vm_mm;
5785 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
5786 pte_t pte = huge_ptep_get(mm, vmf->address, vmf->pte);
5787 struct hstate *h = hstate_vma(vma);
5788 struct folio *old_folio;
5789 struct folio *new_folio;
5790 int outside_reserve = 0;
5791 vm_fault_t ret = 0;
5792 struct mmu_notifier_range range;
5795 * Never handle CoW for uffd-wp protected pages. It should be only
5796 * handled when the uffd-wp protection is removed.
5798 * Note that only the CoW optimization path (in hugetlb_no_page())
5799 * can trigger this, because hugetlb_fault() will always resolve
5800 * uffd-wp bit first.
5802 if (!unshare && huge_pte_uffd_wp(pte))
5803 return 0;
5806 * hugetlb does not support FOLL_FORCE-style write faults that keep the
5807 * PTE mapped R/O such as maybe_mkwrite() would do.
5809 if (WARN_ON_ONCE(!unshare && !(vma->vm_flags & VM_WRITE)))
5810 return VM_FAULT_SIGSEGV;
5812 /* Let's take out MAP_SHARED mappings first. */
5813 if (vma->vm_flags & VM_MAYSHARE) {
5814 set_huge_ptep_writable(vma, vmf->address, vmf->pte);
5815 return 0;
5818 old_folio = page_folio(pte_page(pte));
5820 delayacct_wpcopy_start();
5822 retry_avoidcopy:
5824 * If no-one else is actually using this page, we're the exclusive
5825 * owner and can reuse this page.
5827 * Note that we don't rely on the (safer) folio refcount here, because
5828 * copying the hugetlb folio when there are unexpected (temporary)
5829 * folio references could harm simple fork()+exit() users when
5830 * we run out of free hugetlb folios: we would have to kill processes
5831 * in scenarios that used to work. As a side effect, there can still
5832 * be leaks between processes, for example, with FOLL_GET users.
5834 if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) {
5835 if (!PageAnonExclusive(&old_folio->page)) {
5836 folio_move_anon_rmap(old_folio, vma);
5837 SetPageAnonExclusive(&old_folio->page);
5839 if (likely(!unshare))
5840 set_huge_ptep_writable(vma, vmf->address, vmf->pte);
5842 delayacct_wpcopy_end();
5843 return 0;
5845 VM_BUG_ON_PAGE(folio_test_anon(old_folio) &&
5846 PageAnonExclusive(&old_folio->page), &old_folio->page);
5849 * If the process that created a MAP_PRIVATE mapping is about to
5850 * perform a COW due to a shared page count, attempt to satisfy
5851 * the allocation without using the existing reserves. The pagecache
5852 * page is used to determine if the reserve at this address was
5853 * consumed or not. If reserves were used, a partial faulted mapping
5854 * at the time of fork() could consume its reserves on COW instead
5855 * of the full address range.
5857 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
5858 old_folio != pagecache_folio)
5859 outside_reserve = 1;
5861 folio_get(old_folio);
5864 * Drop page table lock as buddy allocator may be called. It will
5865 * be acquired again before returning to the caller, as expected.
5867 spin_unlock(vmf->ptl);
5868 new_folio = alloc_hugetlb_folio(vma, vmf->address, outside_reserve);
5870 if (IS_ERR(new_folio)) {
5872 * If a process owning a MAP_PRIVATE mapping fails to COW,
5873 * it is due to references held by a child and an insufficient
5874 * huge page pool. To guarantee the original mappers
5875 * reliability, unmap the page from child processes. The child
5876 * may get SIGKILLed if it later faults.
5878 if (outside_reserve) {
5879 struct address_space *mapping = vma->vm_file->f_mapping;
5880 pgoff_t idx;
5881 u32 hash;
5883 folio_put(old_folio);
5885 * Drop hugetlb_fault_mutex and vma_lock before
5886 * unmapping. unmapping needs to hold vma_lock
5887 * in write mode. Dropping vma_lock in read mode
5888 * here is OK as COW mappings do not interact with
5889 * PMD sharing.
5891 * Reacquire both after unmap operation.
5893 idx = vma_hugecache_offset(h, vma, vmf->address);
5894 hash = hugetlb_fault_mutex_hash(mapping, idx);
5895 hugetlb_vma_unlock_read(vma);
5896 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
5898 unmap_ref_private(mm, vma, &old_folio->page,
5899 vmf->address);
5901 mutex_lock(&hugetlb_fault_mutex_table[hash]);
5902 hugetlb_vma_lock_read(vma);
5903 spin_lock(vmf->ptl);
5904 vmf->pte = hugetlb_walk(vma, vmf->address,
5905 huge_page_size(h));
5906 if (likely(vmf->pte &&
5907 pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte)))
5908 goto retry_avoidcopy;
5910 * race occurs while re-acquiring page table
5911 * lock, and our job is done.
5913 delayacct_wpcopy_end();
5914 return 0;
5917 ret = vmf_error(PTR_ERR(new_folio));
5918 goto out_release_old;
5922 * When the original hugepage is shared one, it does not have
5923 * anon_vma prepared.
5925 ret = __vmf_anon_prepare(vmf);
5926 if (unlikely(ret))
5927 goto out_release_all;
5929 if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) {
5930 ret = VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h));
5931 goto out_release_all;
5933 __folio_mark_uptodate(new_folio);
5935 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address,
5936 vmf->address + huge_page_size(h));
5937 mmu_notifier_invalidate_range_start(&range);
5940 * Retake the page table lock to check for racing updates
5941 * before the page tables are altered
5943 spin_lock(vmf->ptl);
5944 vmf->pte = hugetlb_walk(vma, vmf->address, huge_page_size(h));
5945 if (likely(vmf->pte && pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) {
5946 pte_t newpte = make_huge_pte(vma, &new_folio->page, !unshare);
5948 /* Break COW or unshare */
5949 huge_ptep_clear_flush(vma, vmf->address, vmf->pte);
5950 hugetlb_remove_rmap(old_folio);
5951 hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address);
5952 if (huge_pte_uffd_wp(pte))
5953 newpte = huge_pte_mkuffd_wp(newpte);
5954 set_huge_pte_at(mm, vmf->address, vmf->pte, newpte,
5955 huge_page_size(h));
5956 folio_set_hugetlb_migratable(new_folio);
5957 /* Make the old page be freed below */
5958 new_folio = old_folio;
5960 spin_unlock(vmf->ptl);
5961 mmu_notifier_invalidate_range_end(&range);
5962 out_release_all:
5964 * No restore in case of successful pagetable update (Break COW or
5965 * unshare)
5967 if (new_folio != old_folio)
5968 restore_reserve_on_error(h, vma, vmf->address, new_folio);
5969 folio_put(new_folio);
5970 out_release_old:
5971 folio_put(old_folio);
5973 spin_lock(vmf->ptl); /* Caller expects lock to be held */
5975 delayacct_wpcopy_end();
5976 return ret;
5980 * Return whether there is a pagecache page to back given address within VMA.
5982 bool hugetlbfs_pagecache_present(struct hstate *h,
5983 struct vm_area_struct *vma, unsigned long address)
5985 struct address_space *mapping = vma->vm_file->f_mapping;
5986 pgoff_t idx = linear_page_index(vma, address);
5987 struct folio *folio;
5989 folio = filemap_get_folio(mapping, idx);
5990 if (IS_ERR(folio))
5991 return false;
5992 folio_put(folio);
5993 return true;
5996 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
5997 pgoff_t idx)
5999 struct inode *inode = mapping->host;
6000 struct hstate *h = hstate_inode(inode);
6001 int err;
6003 idx <<= huge_page_order(h);
6004 __folio_set_locked(folio);
6005 err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL);
6007 if (unlikely(err)) {
6008 __folio_clear_locked(folio);
6009 return err;
6011 folio_clear_hugetlb_restore_reserve(folio);
6014 * mark folio dirty so that it will not be removed from cache/file
6015 * by non-hugetlbfs specific code paths.
6017 folio_mark_dirty(folio);
6019 spin_lock(&inode->i_lock);
6020 inode->i_blocks += blocks_per_huge_page(h);
6021 spin_unlock(&inode->i_lock);
6022 return 0;
6025 static inline vm_fault_t hugetlb_handle_userfault(struct vm_fault *vmf,
6026 struct address_space *mapping,
6027 unsigned long reason)
6029 u32 hash;
6032 * vma_lock and hugetlb_fault_mutex must be dropped before handling
6033 * userfault. Also mmap_lock could be dropped due to handling
6034 * userfault, any vma operation should be careful from here.
6036 hugetlb_vma_unlock_read(vmf->vma);
6037 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
6038 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6039 return handle_userfault(vmf, reason);
6043 * Recheck pte with pgtable lock. Returns true if pte didn't change, or
6044 * false if pte changed or is changing.
6046 static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm, unsigned long addr,
6047 pte_t *ptep, pte_t old_pte)
6049 spinlock_t *ptl;
6050 bool same;
6052 ptl = huge_pte_lock(h, mm, ptep);
6053 same = pte_same(huge_ptep_get(mm, addr, ptep), old_pte);
6054 spin_unlock(ptl);
6056 return same;
6059 static vm_fault_t hugetlb_no_page(struct address_space *mapping,
6060 struct vm_fault *vmf)
6062 struct vm_area_struct *vma = vmf->vma;
6063 struct mm_struct *mm = vma->vm_mm;
6064 struct hstate *h = hstate_vma(vma);
6065 vm_fault_t ret = VM_FAULT_SIGBUS;
6066 int anon_rmap = 0;
6067 unsigned long size;
6068 struct folio *folio;
6069 pte_t new_pte;
6070 bool new_folio, new_pagecache_folio = false;
6071 u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
6074 * Currently, we are forced to kill the process in the event the
6075 * original mapper has unmapped pages from the child due to a failed
6076 * COW/unsharing. Warn that such a situation has occurred as it may not
6077 * be obvious.
6079 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
6080 pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
6081 current->pid);
6082 goto out;
6086 * Use page lock to guard against racing truncation
6087 * before we get page_table_lock.
6089 new_folio = false;
6090 folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff);
6091 if (IS_ERR(folio)) {
6092 size = i_size_read(mapping->host) >> huge_page_shift(h);
6093 if (vmf->pgoff >= size)
6094 goto out;
6095 /* Check for page in userfault range */
6096 if (userfaultfd_missing(vma)) {
6098 * Since hugetlb_no_page() was examining pte
6099 * without pgtable lock, we need to re-test under
6100 * lock because the pte may not be stable and could
6101 * have changed from under us. Try to detect
6102 * either changed or during-changing ptes and retry
6103 * properly when needed.
6105 * Note that userfaultfd is actually fine with
6106 * false positives (e.g. caused by pte changed),
6107 * but not wrong logical events (e.g. caused by
6108 * reading a pte during changing). The latter can
6109 * confuse the userspace, so the strictness is very
6110 * much preferred. E.g., MISSING event should
6111 * never happen on the page after UFFDIO_COPY has
6112 * correctly installed the page and returned.
6114 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) {
6115 ret = 0;
6116 goto out;
6119 return hugetlb_handle_userfault(vmf, mapping,
6120 VM_UFFD_MISSING);
6123 if (!(vma->vm_flags & VM_MAYSHARE)) {
6124 ret = __vmf_anon_prepare(vmf);
6125 if (unlikely(ret))
6126 goto out;
6129 folio = alloc_hugetlb_folio(vma, vmf->address, 0);
6130 if (IS_ERR(folio)) {
6132 * Returning error will result in faulting task being
6133 * sent SIGBUS. The hugetlb fault mutex prevents two
6134 * tasks from racing to fault in the same page which
6135 * could result in false unable to allocate errors.
6136 * Page migration does not take the fault mutex, but
6137 * does a clear then write of pte's under page table
6138 * lock. Page fault code could race with migration,
6139 * notice the clear pte and try to allocate a page
6140 * here. Before returning error, get ptl and make
6141 * sure there really is no pte entry.
6143 if (hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte))
6144 ret = vmf_error(PTR_ERR(folio));
6145 else
6146 ret = 0;
6147 goto out;
6149 folio_zero_user(folio, vmf->real_address);
6150 __folio_mark_uptodate(folio);
6151 new_folio = true;
6153 if (vma->vm_flags & VM_MAYSHARE) {
6154 int err = hugetlb_add_to_page_cache(folio, mapping,
6155 vmf->pgoff);
6156 if (err) {
6158 * err can't be -EEXIST which implies someone
6159 * else consumed the reservation since hugetlb
6160 * fault mutex is held when add a hugetlb page
6161 * to the page cache. So it's safe to call
6162 * restore_reserve_on_error() here.
6164 restore_reserve_on_error(h, vma, vmf->address,
6165 folio);
6166 folio_put(folio);
6167 ret = VM_FAULT_SIGBUS;
6168 goto out;
6170 new_pagecache_folio = true;
6171 } else {
6172 folio_lock(folio);
6173 anon_rmap = 1;
6175 } else {
6177 * If memory error occurs between mmap() and fault, some process
6178 * don't have hwpoisoned swap entry for errored virtual address.
6179 * So we need to block hugepage fault by PG_hwpoison bit check.
6181 if (unlikely(folio_test_hwpoison(folio))) {
6182 ret = VM_FAULT_HWPOISON_LARGE |
6183 VM_FAULT_SET_HINDEX(hstate_index(h));
6184 goto backout_unlocked;
6187 /* Check for page in userfault range. */
6188 if (userfaultfd_minor(vma)) {
6189 folio_unlock(folio);
6190 folio_put(folio);
6191 /* See comment in userfaultfd_missing() block above */
6192 if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) {
6193 ret = 0;
6194 goto out;
6196 return hugetlb_handle_userfault(vmf, mapping,
6197 VM_UFFD_MINOR);
6202 * If we are going to COW a private mapping later, we examine the
6203 * pending reservations for this page now. This will ensure that
6204 * any allocations necessary to record that reservation occur outside
6205 * the spinlock.
6207 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
6208 if (vma_needs_reservation(h, vma, vmf->address) < 0) {
6209 ret = VM_FAULT_OOM;
6210 goto backout_unlocked;
6212 /* Just decrements count, does not deallocate */
6213 vma_end_reservation(h, vma, vmf->address);
6216 vmf->ptl = huge_pte_lock(h, mm, vmf->pte);
6217 ret = 0;
6218 /* If pte changed from under us, retry */
6219 if (!pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), vmf->orig_pte))
6220 goto backout;
6222 if (anon_rmap)
6223 hugetlb_add_new_anon_rmap(folio, vma, vmf->address);
6224 else
6225 hugetlb_add_file_rmap(folio);
6226 new_pte = make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE)
6227 && (vma->vm_flags & VM_SHARED)));
6229 * If this pte was previously wr-protected, keep it wr-protected even
6230 * if populated.
6232 if (unlikely(pte_marker_uffd_wp(vmf->orig_pte)))
6233 new_pte = huge_pte_mkuffd_wp(new_pte);
6234 set_huge_pte_at(mm, vmf->address, vmf->pte, new_pte, huge_page_size(h));
6236 hugetlb_count_add(pages_per_huge_page(h), mm);
6237 if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
6238 /* Optimization, do the COW without a second fault */
6239 ret = hugetlb_wp(folio, vmf);
6242 spin_unlock(vmf->ptl);
6245 * Only set hugetlb_migratable in newly allocated pages. Existing pages
6246 * found in the pagecache may not have hugetlb_migratable if they have
6247 * been isolated for migration.
6249 if (new_folio)
6250 folio_set_hugetlb_migratable(folio);
6252 folio_unlock(folio);
6253 out:
6254 hugetlb_vma_unlock_read(vma);
6257 * We must check to release the per-VMA lock. __vmf_anon_prepare() is
6258 * the only way ret can be set to VM_FAULT_RETRY.
6260 if (unlikely(ret & VM_FAULT_RETRY))
6261 vma_end_read(vma);
6263 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6264 return ret;
6266 backout:
6267 spin_unlock(vmf->ptl);
6268 backout_unlocked:
6269 if (new_folio && !new_pagecache_folio)
6270 restore_reserve_on_error(h, vma, vmf->address, folio);
6272 folio_unlock(folio);
6273 folio_put(folio);
6274 goto out;
6277 #ifdef CONFIG_SMP
6278 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
6280 unsigned long key[2];
6281 u32 hash;
6283 key[0] = (unsigned long) mapping;
6284 key[1] = idx;
6286 hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
6288 return hash & (num_fault_mutexes - 1);
6290 #else
6292 * For uniprocessor systems we always use a single mutex, so just
6293 * return 0 and avoid the hashing overhead.
6295 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
6297 return 0;
6299 #endif
6301 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
6302 unsigned long address, unsigned int flags)
6304 vm_fault_t ret;
6305 u32 hash;
6306 struct folio *folio = NULL;
6307 struct folio *pagecache_folio = NULL;
6308 struct hstate *h = hstate_vma(vma);
6309 struct address_space *mapping;
6310 int need_wait_lock = 0;
6311 struct vm_fault vmf = {
6312 .vma = vma,
6313 .address = address & huge_page_mask(h),
6314 .real_address = address,
6315 .flags = flags,
6316 .pgoff = vma_hugecache_offset(h, vma,
6317 address & huge_page_mask(h)),
6318 /* TODO: Track hugetlb faults using vm_fault */
6321 * Some fields may not be initialized, be careful as it may
6322 * be hard to debug if called functions make assumptions
6327 * Serialize hugepage allocation and instantiation, so that we don't
6328 * get spurious allocation failures if two CPUs race to instantiate
6329 * the same page in the page cache.
6331 mapping = vma->vm_file->f_mapping;
6332 hash = hugetlb_fault_mutex_hash(mapping, vmf.pgoff);
6333 mutex_lock(&hugetlb_fault_mutex_table[hash]);
6336 * Acquire vma lock before calling huge_pte_alloc and hold
6337 * until finished with vmf.pte. This prevents huge_pmd_unshare from
6338 * being called elsewhere and making the vmf.pte no longer valid.
6340 hugetlb_vma_lock_read(vma);
6341 vmf.pte = huge_pte_alloc(mm, vma, vmf.address, huge_page_size(h));
6342 if (!vmf.pte) {
6343 hugetlb_vma_unlock_read(vma);
6344 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6345 return VM_FAULT_OOM;
6348 vmf.orig_pte = huge_ptep_get(mm, vmf.address, vmf.pte);
6349 if (huge_pte_none_mostly(vmf.orig_pte)) {
6350 if (is_pte_marker(vmf.orig_pte)) {
6351 pte_marker marker =
6352 pte_marker_get(pte_to_swp_entry(vmf.orig_pte));
6354 if (marker & PTE_MARKER_POISONED) {
6355 ret = VM_FAULT_HWPOISON_LARGE |
6356 VM_FAULT_SET_HINDEX(hstate_index(h));
6357 goto out_mutex;
6358 } else if (WARN_ON_ONCE(marker & PTE_MARKER_GUARD)) {
6359 /* This isn't supported in hugetlb. */
6360 ret = VM_FAULT_SIGSEGV;
6361 goto out_mutex;
6366 * Other PTE markers should be handled the same way as none PTE.
6368 * hugetlb_no_page will drop vma lock and hugetlb fault
6369 * mutex internally, which make us return immediately.
6371 return hugetlb_no_page(mapping, &vmf);
6374 ret = 0;
6377 * vmf.orig_pte could be a migration/hwpoison vmf.orig_pte at this
6378 * point, so this check prevents the kernel from going below assuming
6379 * that we have an active hugepage in pagecache. This goto expects
6380 * the 2nd page fault, and is_hugetlb_entry_(migration|hwpoisoned)
6381 * check will properly handle it.
6383 if (!pte_present(vmf.orig_pte)) {
6384 if (unlikely(is_hugetlb_entry_migration(vmf.orig_pte))) {
6386 * Release the hugetlb fault lock now, but retain
6387 * the vma lock, because it is needed to guard the
6388 * huge_pte_lockptr() later in
6389 * migration_entry_wait_huge(). The vma lock will
6390 * be released there.
6392 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6393 migration_entry_wait_huge(vma, vmf.address, vmf.pte);
6394 return 0;
6395 } else if (unlikely(is_hugetlb_entry_hwpoisoned(vmf.orig_pte)))
6396 ret = VM_FAULT_HWPOISON_LARGE |
6397 VM_FAULT_SET_HINDEX(hstate_index(h));
6398 goto out_mutex;
6402 * If we are going to COW/unshare the mapping later, we examine the
6403 * pending reservations for this page now. This will ensure that any
6404 * allocations necessary to record that reservation occur outside the
6405 * spinlock. Also lookup the pagecache page now as it is used to
6406 * determine if a reservation has been consumed.
6408 if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
6409 !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) {
6410 if (vma_needs_reservation(h, vma, vmf.address) < 0) {
6411 ret = VM_FAULT_OOM;
6412 goto out_mutex;
6414 /* Just decrements count, does not deallocate */
6415 vma_end_reservation(h, vma, vmf.address);
6417 pagecache_folio = filemap_lock_hugetlb_folio(h, mapping,
6418 vmf.pgoff);
6419 if (IS_ERR(pagecache_folio))
6420 pagecache_folio = NULL;
6423 vmf.ptl = huge_pte_lock(h, mm, vmf.pte);
6425 /* Check for a racing update before calling hugetlb_wp() */
6426 if (unlikely(!pte_same(vmf.orig_pte, huge_ptep_get(mm, vmf.address, vmf.pte))))
6427 goto out_ptl;
6429 /* Handle userfault-wp first, before trying to lock more pages */
6430 if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(mm, vmf.address, vmf.pte)) &&
6431 (flags & FAULT_FLAG_WRITE) && !huge_pte_write(vmf.orig_pte)) {
6432 if (!userfaultfd_wp_async(vma)) {
6433 spin_unlock(vmf.ptl);
6434 if (pagecache_folio) {
6435 folio_unlock(pagecache_folio);
6436 folio_put(pagecache_folio);
6438 hugetlb_vma_unlock_read(vma);
6439 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6440 return handle_userfault(&vmf, VM_UFFD_WP);
6443 vmf.orig_pte = huge_pte_clear_uffd_wp(vmf.orig_pte);
6444 set_huge_pte_at(mm, vmf.address, vmf.pte, vmf.orig_pte,
6445 huge_page_size(hstate_vma(vma)));
6446 /* Fallthrough to CoW */
6450 * hugetlb_wp() requires page locks of pte_page(vmf.orig_pte) and
6451 * pagecache_folio, so here we need take the former one
6452 * when folio != pagecache_folio or !pagecache_folio.
6454 folio = page_folio(pte_page(vmf.orig_pte));
6455 if (folio != pagecache_folio)
6456 if (!folio_trylock(folio)) {
6457 need_wait_lock = 1;
6458 goto out_ptl;
6461 folio_get(folio);
6463 if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
6464 if (!huge_pte_write(vmf.orig_pte)) {
6465 ret = hugetlb_wp(pagecache_folio, &vmf);
6466 goto out_put_page;
6467 } else if (likely(flags & FAULT_FLAG_WRITE)) {
6468 vmf.orig_pte = huge_pte_mkdirty(vmf.orig_pte);
6471 vmf.orig_pte = pte_mkyoung(vmf.orig_pte);
6472 if (huge_ptep_set_access_flags(vma, vmf.address, vmf.pte, vmf.orig_pte,
6473 flags & FAULT_FLAG_WRITE))
6474 update_mmu_cache(vma, vmf.address, vmf.pte);
6475 out_put_page:
6476 if (folio != pagecache_folio)
6477 folio_unlock(folio);
6478 folio_put(folio);
6479 out_ptl:
6480 spin_unlock(vmf.ptl);
6482 if (pagecache_folio) {
6483 folio_unlock(pagecache_folio);
6484 folio_put(pagecache_folio);
6486 out_mutex:
6487 hugetlb_vma_unlock_read(vma);
6490 * We must check to release the per-VMA lock. __vmf_anon_prepare() in
6491 * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY.
6493 if (unlikely(ret & VM_FAULT_RETRY))
6494 vma_end_read(vma);
6496 mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6498 * Generally it's safe to hold refcount during waiting page lock. But
6499 * here we just wait to defer the next page fault to avoid busy loop and
6500 * the page is not used after unlocked before returning from the current
6501 * page fault. So we are safe from accessing freed page, even if we wait
6502 * here without taking refcount.
6504 if (need_wait_lock)
6505 folio_wait_locked(folio);
6506 return ret;
6509 #ifdef CONFIG_USERFAULTFD
6511 * Can probably be eliminated, but still used by hugetlb_mfill_atomic_pte().
6513 static struct folio *alloc_hugetlb_folio_vma(struct hstate *h,
6514 struct vm_area_struct *vma, unsigned long address)
6516 struct mempolicy *mpol;
6517 nodemask_t *nodemask;
6518 struct folio *folio;
6519 gfp_t gfp_mask;
6520 int node;
6522 gfp_mask = htlb_alloc_mask(h);
6523 node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
6525 * This is used to allocate a temporary hugetlb to hold the copied
6526 * content, which will then be copied again to the final hugetlb
6527 * consuming a reservation. Set the alloc_fallback to false to indicate
6528 * that breaking the per-node hugetlb pool is not allowed in this case.
6530 folio = alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask, false);
6531 mpol_cond_put(mpol);
6533 return folio;
6537 * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte
6538 * with modifications for hugetlb pages.
6540 int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
6541 struct vm_area_struct *dst_vma,
6542 unsigned long dst_addr,
6543 unsigned long src_addr,
6544 uffd_flags_t flags,
6545 struct folio **foliop)
6547 struct mm_struct *dst_mm = dst_vma->vm_mm;
6548 bool is_continue = uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE);
6549 bool wp_enabled = (flags & MFILL_ATOMIC_WP);
6550 struct hstate *h = hstate_vma(dst_vma);
6551 struct address_space *mapping = dst_vma->vm_file->f_mapping;
6552 pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
6553 unsigned long size = huge_page_size(h);
6554 int vm_shared = dst_vma->vm_flags & VM_SHARED;
6555 pte_t _dst_pte;
6556 spinlock_t *ptl;
6557 int ret = -ENOMEM;
6558 struct folio *folio;
6559 int writable;
6560 bool folio_in_pagecache = false;
6562 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
6563 ptl = huge_pte_lock(h, dst_mm, dst_pte);
6565 /* Don't overwrite any existing PTEs (even markers) */
6566 if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
6567 spin_unlock(ptl);
6568 return -EEXIST;
6571 _dst_pte = make_pte_marker(PTE_MARKER_POISONED);
6572 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size);
6574 /* No need to invalidate - it was non-present before */
6575 update_mmu_cache(dst_vma, dst_addr, dst_pte);
6577 spin_unlock(ptl);
6578 return 0;
6581 if (is_continue) {
6582 ret = -EFAULT;
6583 folio = filemap_lock_hugetlb_folio(h, mapping, idx);
6584 if (IS_ERR(folio))
6585 goto out;
6586 folio_in_pagecache = true;
6587 } else if (!*foliop) {
6588 /* If a folio already exists, then it's UFFDIO_COPY for
6589 * a non-missing case. Return -EEXIST.
6591 if (vm_shared &&
6592 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6593 ret = -EEXIST;
6594 goto out;
6597 folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
6598 if (IS_ERR(folio)) {
6599 ret = -ENOMEM;
6600 goto out;
6603 ret = copy_folio_from_user(folio, (const void __user *) src_addr,
6604 false);
6606 /* fallback to copy_from_user outside mmap_lock */
6607 if (unlikely(ret)) {
6608 ret = -ENOENT;
6609 /* Free the allocated folio which may have
6610 * consumed a reservation.
6612 restore_reserve_on_error(h, dst_vma, dst_addr, folio);
6613 folio_put(folio);
6615 /* Allocate a temporary folio to hold the copied
6616 * contents.
6618 folio = alloc_hugetlb_folio_vma(h, dst_vma, dst_addr);
6619 if (!folio) {
6620 ret = -ENOMEM;
6621 goto out;
6623 *foliop = folio;
6624 /* Set the outparam foliop and return to the caller to
6625 * copy the contents outside the lock. Don't free the
6626 * folio.
6628 goto out;
6630 } else {
6631 if (vm_shared &&
6632 hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6633 folio_put(*foliop);
6634 ret = -EEXIST;
6635 *foliop = NULL;
6636 goto out;
6639 folio = alloc_hugetlb_folio(dst_vma, dst_addr, 0);
6640 if (IS_ERR(folio)) {
6641 folio_put(*foliop);
6642 ret = -ENOMEM;
6643 *foliop = NULL;
6644 goto out;
6646 ret = copy_user_large_folio(folio, *foliop,
6647 ALIGN_DOWN(dst_addr, size), dst_vma);
6648 folio_put(*foliop);
6649 *foliop = NULL;
6650 if (ret) {
6651 folio_put(folio);
6652 goto out;
6657 * If we just allocated a new page, we need a memory barrier to ensure
6658 * that preceding stores to the page become visible before the
6659 * set_pte_at() write. The memory barrier inside __folio_mark_uptodate
6660 * is what we need.
6662 * In the case where we have not allocated a new page (is_continue),
6663 * the page must already be uptodate. UFFDIO_CONTINUE already includes
6664 * an earlier smp_wmb() to ensure that prior stores will be visible
6665 * before the set_pte_at() write.
6667 if (!is_continue)
6668 __folio_mark_uptodate(folio);
6669 else
6670 WARN_ON_ONCE(!folio_test_uptodate(folio));
6672 /* Add shared, newly allocated pages to the page cache. */
6673 if (vm_shared && !is_continue) {
6674 ret = -EFAULT;
6675 if (idx >= (i_size_read(mapping->host) >> huge_page_shift(h)))
6676 goto out_release_nounlock;
6679 * Serialization between remove_inode_hugepages() and
6680 * hugetlb_add_to_page_cache() below happens through the
6681 * hugetlb_fault_mutex_table that here must be hold by
6682 * the caller.
6684 ret = hugetlb_add_to_page_cache(folio, mapping, idx);
6685 if (ret)
6686 goto out_release_nounlock;
6687 folio_in_pagecache = true;
6690 ptl = huge_pte_lock(h, dst_mm, dst_pte);
6692 ret = -EIO;
6693 if (folio_test_hwpoison(folio))
6694 goto out_release_unlock;
6697 * We allow to overwrite a pte marker: consider when both MISSING|WP
6698 * registered, we firstly wr-protect a none pte which has no page cache
6699 * page backing it, then access the page.
6701 ret = -EEXIST;
6702 if (!huge_pte_none_mostly(huge_ptep_get(dst_mm, dst_addr, dst_pte)))
6703 goto out_release_unlock;
6705 if (folio_in_pagecache)
6706 hugetlb_add_file_rmap(folio);
6707 else
6708 hugetlb_add_new_anon_rmap(folio, dst_vma, dst_addr);
6711 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY
6712 * with wp flag set, don't set pte write bit.
6714 if (wp_enabled || (is_continue && !vm_shared))
6715 writable = 0;
6716 else
6717 writable = dst_vma->vm_flags & VM_WRITE;
6719 _dst_pte = make_huge_pte(dst_vma, &folio->page, writable);
6721 * Always mark UFFDIO_COPY page dirty; note that this may not be
6722 * extremely important for hugetlbfs for now since swapping is not
6723 * supported, but we should still be clear in that this page cannot be
6724 * thrown away at will, even if write bit not set.
6726 _dst_pte = huge_pte_mkdirty(_dst_pte);
6727 _dst_pte = pte_mkyoung(_dst_pte);
6729 if (wp_enabled)
6730 _dst_pte = huge_pte_mkuffd_wp(_dst_pte);
6732 set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size);
6734 hugetlb_count_add(pages_per_huge_page(h), dst_mm);
6736 /* No need to invalidate - it was non-present before */
6737 update_mmu_cache(dst_vma, dst_addr, dst_pte);
6739 spin_unlock(ptl);
6740 if (!is_continue)
6741 folio_set_hugetlb_migratable(folio);
6742 if (vm_shared || is_continue)
6743 folio_unlock(folio);
6744 ret = 0;
6745 out:
6746 return ret;
6747 out_release_unlock:
6748 spin_unlock(ptl);
6749 if (vm_shared || is_continue)
6750 folio_unlock(folio);
6751 out_release_nounlock:
6752 if (!folio_in_pagecache)
6753 restore_reserve_on_error(h, dst_vma, dst_addr, folio);
6754 folio_put(folio);
6755 goto out;
6757 #endif /* CONFIG_USERFAULTFD */
6759 long hugetlb_change_protection(struct vm_area_struct *vma,
6760 unsigned long address, unsigned long end,
6761 pgprot_t newprot, unsigned long cp_flags)
6763 struct mm_struct *mm = vma->vm_mm;
6764 unsigned long start = address;
6765 pte_t *ptep;
6766 pte_t pte;
6767 struct hstate *h = hstate_vma(vma);
6768 long pages = 0, psize = huge_page_size(h);
6769 bool shared_pmd = false;
6770 struct mmu_notifier_range range;
6771 unsigned long last_addr_mask;
6772 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
6773 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
6776 * In the case of shared PMDs, the area to flush could be beyond
6777 * start/end. Set range.start/range.end to cover the maximum possible
6778 * range if PMD sharing is possible.
6780 mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
6781 0, mm, start, end);
6782 adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
6784 BUG_ON(address >= end);
6785 flush_cache_range(vma, range.start, range.end);
6787 mmu_notifier_invalidate_range_start(&range);
6788 hugetlb_vma_lock_write(vma);
6789 i_mmap_lock_write(vma->vm_file->f_mapping);
6790 last_addr_mask = hugetlb_mask_last_page(h);
6791 for (; address < end; address += psize) {
6792 spinlock_t *ptl;
6793 ptep = hugetlb_walk(vma, address, psize);
6794 if (!ptep) {
6795 if (!uffd_wp) {
6796 address |= last_addr_mask;
6797 continue;
6800 * Userfaultfd wr-protect requires pgtable
6801 * pre-allocations to install pte markers.
6803 ptep = huge_pte_alloc(mm, vma, address, psize);
6804 if (!ptep) {
6805 pages = -ENOMEM;
6806 break;
6809 ptl = huge_pte_lock(h, mm, ptep);
6810 if (huge_pmd_unshare(mm, vma, address, ptep)) {
6812 * When uffd-wp is enabled on the vma, unshare
6813 * shouldn't happen at all. Warn about it if it
6814 * happened due to some reason.
6816 WARN_ON_ONCE(uffd_wp || uffd_wp_resolve);
6817 pages++;
6818 spin_unlock(ptl);
6819 shared_pmd = true;
6820 address |= last_addr_mask;
6821 continue;
6823 pte = huge_ptep_get(mm, address, ptep);
6824 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
6825 /* Nothing to do. */
6826 } else if (unlikely(is_hugetlb_entry_migration(pte))) {
6827 swp_entry_t entry = pte_to_swp_entry(pte);
6828 struct page *page = pfn_swap_entry_to_page(entry);
6829 pte_t newpte = pte;
6831 if (is_writable_migration_entry(entry)) {
6832 if (PageAnon(page))
6833 entry = make_readable_exclusive_migration_entry(
6834 swp_offset(entry));
6835 else
6836 entry = make_readable_migration_entry(
6837 swp_offset(entry));
6838 newpte = swp_entry_to_pte(entry);
6839 pages++;
6842 if (uffd_wp)
6843 newpte = pte_swp_mkuffd_wp(newpte);
6844 else if (uffd_wp_resolve)
6845 newpte = pte_swp_clear_uffd_wp(newpte);
6846 if (!pte_same(pte, newpte))
6847 set_huge_pte_at(mm, address, ptep, newpte, psize);
6848 } else if (unlikely(is_pte_marker(pte))) {
6850 * Do nothing on a poison marker; page is
6851 * corrupted, permissons do not apply. Here
6852 * pte_marker_uffd_wp()==true implies !poison
6853 * because they're mutual exclusive.
6855 if (pte_marker_uffd_wp(pte) && uffd_wp_resolve)
6856 /* Safe to modify directly (non-present->none). */
6857 huge_pte_clear(mm, address, ptep, psize);
6858 } else if (!huge_pte_none(pte)) {
6859 pte_t old_pte;
6860 unsigned int shift = huge_page_shift(hstate_vma(vma));
6862 old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
6863 pte = huge_pte_modify(old_pte, newprot);
6864 pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
6865 if (uffd_wp)
6866 pte = huge_pte_mkuffd_wp(pte);
6867 else if (uffd_wp_resolve)
6868 pte = huge_pte_clear_uffd_wp(pte);
6869 huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
6870 pages++;
6871 } else {
6872 /* None pte */
6873 if (unlikely(uffd_wp))
6874 /* Safe to modify directly (none->non-present). */
6875 set_huge_pte_at(mm, address, ptep,
6876 make_pte_marker(PTE_MARKER_UFFD_WP),
6877 psize);
6879 spin_unlock(ptl);
6882 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
6883 * may have cleared our pud entry and done put_page on the page table:
6884 * once we release i_mmap_rwsem, another task can do the final put_page
6885 * and that page table be reused and filled with junk. If we actually
6886 * did unshare a page of pmds, flush the range corresponding to the pud.
6888 if (shared_pmd)
6889 flush_hugetlb_tlb_range(vma, range.start, range.end);
6890 else
6891 flush_hugetlb_tlb_range(vma, start, end);
6893 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs() we are
6894 * downgrading page table protection not changing it to point to a new
6895 * page.
6897 * See Documentation/mm/mmu_notifier.rst
6899 i_mmap_unlock_write(vma->vm_file->f_mapping);
6900 hugetlb_vma_unlock_write(vma);
6901 mmu_notifier_invalidate_range_end(&range);
6903 return pages > 0 ? (pages << h->order) : pages;
6906 /* Return true if reservation was successful, false otherwise. */
6907 bool hugetlb_reserve_pages(struct inode *inode,
6908 long from, long to,
6909 struct vm_area_struct *vma,
6910 vm_flags_t vm_flags)
6912 long chg = -1, add = -1;
6913 struct hstate *h = hstate_inode(inode);
6914 struct hugepage_subpool *spool = subpool_inode(inode);
6915 struct resv_map *resv_map;
6916 struct hugetlb_cgroup *h_cg = NULL;
6917 long gbl_reserve, regions_needed = 0;
6919 /* This should never happen */
6920 if (from > to) {
6921 VM_WARN(1, "%s called with a negative range\n", __func__);
6922 return false;
6926 * vma specific semaphore used for pmd sharing and fault/truncation
6927 * synchronization
6929 hugetlb_vma_lock_alloc(vma);
6932 * Only apply hugepage reservation if asked. At fault time, an
6933 * attempt will be made for VM_NORESERVE to allocate a page
6934 * without using reserves
6936 if (vm_flags & VM_NORESERVE)
6937 return true;
6940 * Shared mappings base their reservation on the number of pages that
6941 * are already allocated on behalf of the file. Private mappings need
6942 * to reserve the full area even if read-only as mprotect() may be
6943 * called to make the mapping read-write. Assume !vma is a shm mapping
6945 if (!vma || vma->vm_flags & VM_MAYSHARE) {
6947 * resv_map can not be NULL as hugetlb_reserve_pages is only
6948 * called for inodes for which resv_maps were created (see
6949 * hugetlbfs_get_inode).
6951 resv_map = inode_resv_map(inode);
6953 chg = region_chg(resv_map, from, to, &regions_needed);
6954 } else {
6955 /* Private mapping. */
6956 resv_map = resv_map_alloc();
6957 if (!resv_map)
6958 goto out_err;
6960 chg = to - from;
6962 set_vma_resv_map(vma, resv_map);
6963 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
6966 if (chg < 0)
6967 goto out_err;
6969 if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
6970 chg * pages_per_huge_page(h), &h_cg) < 0)
6971 goto out_err;
6973 if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
6974 /* For private mappings, the hugetlb_cgroup uncharge info hangs
6975 * of the resv_map.
6977 resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
6981 * There must be enough pages in the subpool for the mapping. If
6982 * the subpool has a minimum size, there may be some global
6983 * reservations already in place (gbl_reserve).
6985 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
6986 if (gbl_reserve < 0)
6987 goto out_uncharge_cgroup;
6990 * Check enough hugepages are available for the reservation.
6991 * Hand the pages back to the subpool if there are not
6993 if (hugetlb_acct_memory(h, gbl_reserve) < 0)
6994 goto out_put_pages;
6997 * Account for the reservations made. Shared mappings record regions
6998 * that have reservations as they are shared by multiple VMAs.
6999 * When the last VMA disappears, the region map says how much
7000 * the reservation was and the page cache tells how much of
7001 * the reservation was consumed. Private mappings are per-VMA and
7002 * only the consumed reservations are tracked. When the VMA
7003 * disappears, the original reservation is the VMA size and the
7004 * consumed reservations are stored in the map. Hence, nothing
7005 * else has to be done for private mappings here
7007 if (!vma || vma->vm_flags & VM_MAYSHARE) {
7008 add = region_add(resv_map, from, to, regions_needed, h, h_cg);
7010 if (unlikely(add < 0)) {
7011 hugetlb_acct_memory(h, -gbl_reserve);
7012 goto out_put_pages;
7013 } else if (unlikely(chg > add)) {
7015 * pages in this range were added to the reserve
7016 * map between region_chg and region_add. This
7017 * indicates a race with alloc_hugetlb_folio. Adjust
7018 * the subpool and reserve counts modified above
7019 * based on the difference.
7021 long rsv_adjust;
7024 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the
7025 * reference to h_cg->css. See comment below for detail.
7027 hugetlb_cgroup_uncharge_cgroup_rsvd(
7028 hstate_index(h),
7029 (chg - add) * pages_per_huge_page(h), h_cg);
7031 rsv_adjust = hugepage_subpool_put_pages(spool,
7032 chg - add);
7033 hugetlb_acct_memory(h, -rsv_adjust);
7034 } else if (h_cg) {
7036 * The file_regions will hold their own reference to
7037 * h_cg->css. So we should release the reference held
7038 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are
7039 * done.
7041 hugetlb_cgroup_put_rsvd_cgroup(h_cg);
7044 return true;
7046 out_put_pages:
7047 /* put back original number of pages, chg */
7048 (void)hugepage_subpool_put_pages(spool, chg);
7049 out_uncharge_cgroup:
7050 hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
7051 chg * pages_per_huge_page(h), h_cg);
7052 out_err:
7053 hugetlb_vma_lock_free(vma);
7054 if (!vma || vma->vm_flags & VM_MAYSHARE)
7055 /* Only call region_abort if the region_chg succeeded but the
7056 * region_add failed or didn't run.
7058 if (chg >= 0 && add < 0)
7059 region_abort(resv_map, from, to, regions_needed);
7060 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
7061 kref_put(&resv_map->refs, resv_map_release);
7062 set_vma_resv_map(vma, NULL);
7064 return false;
7067 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
7068 long freed)
7070 struct hstate *h = hstate_inode(inode);
7071 struct resv_map *resv_map = inode_resv_map(inode);
7072 long chg = 0;
7073 struct hugepage_subpool *spool = subpool_inode(inode);
7074 long gbl_reserve;
7077 * Since this routine can be called in the evict inode path for all
7078 * hugetlbfs inodes, resv_map could be NULL.
7080 if (resv_map) {
7081 chg = region_del(resv_map, start, end);
7083 * region_del() can fail in the rare case where a region
7084 * must be split and another region descriptor can not be
7085 * allocated. If end == LONG_MAX, it will not fail.
7087 if (chg < 0)
7088 return chg;
7091 spin_lock(&inode->i_lock);
7092 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
7093 spin_unlock(&inode->i_lock);
7096 * If the subpool has a minimum size, the number of global
7097 * reservations to be released may be adjusted.
7099 * Note that !resv_map implies freed == 0. So (chg - freed)
7100 * won't go negative.
7102 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
7103 hugetlb_acct_memory(h, -gbl_reserve);
7105 return 0;
7108 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
7109 static unsigned long page_table_shareable(struct vm_area_struct *svma,
7110 struct vm_area_struct *vma,
7111 unsigned long addr, pgoff_t idx)
7113 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
7114 svma->vm_start;
7115 unsigned long sbase = saddr & PUD_MASK;
7116 unsigned long s_end = sbase + PUD_SIZE;
7118 /* Allow segments to share if only one is marked locked */
7119 unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED_MASK;
7120 unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED_MASK;
7123 * match the virtual addresses, permission and the alignment of the
7124 * page table page.
7126 * Also, vma_lock (vm_private_data) is required for sharing.
7128 if (pmd_index(addr) != pmd_index(saddr) ||
7129 vm_flags != svm_flags ||
7130 !range_in_vma(svma, sbase, s_end) ||
7131 !svma->vm_private_data)
7132 return 0;
7134 return saddr;
7137 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7139 unsigned long start = addr & PUD_MASK;
7140 unsigned long end = start + PUD_SIZE;
7142 #ifdef CONFIG_USERFAULTFD
7143 if (uffd_disable_huge_pmd_share(vma))
7144 return false;
7145 #endif
7147 * check on proper vm_flags and page table alignment
7149 if (!(vma->vm_flags & VM_MAYSHARE))
7150 return false;
7151 if (!vma->vm_private_data) /* vma lock required for sharing */
7152 return false;
7153 if (!range_in_vma(vma, start, end))
7154 return false;
7155 return true;
7159 * Determine if start,end range within vma could be mapped by shared pmd.
7160 * If yes, adjust start and end to cover range associated with possible
7161 * shared pmd mappings.
7163 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7164 unsigned long *start, unsigned long *end)
7166 unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
7167 v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
7170 * vma needs to span at least one aligned PUD size, and the range
7171 * must be at least partially within in.
7173 if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
7174 (*end <= v_start) || (*start >= v_end))
7175 return;
7177 /* Extend the range to be PUD aligned for a worst case scenario */
7178 if (*start > v_start)
7179 *start = ALIGN_DOWN(*start, PUD_SIZE);
7181 if (*end < v_end)
7182 *end = ALIGN(*end, PUD_SIZE);
7186 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
7187 * and returns the corresponding pte. While this is not necessary for the
7188 * !shared pmd case because we can allocate the pmd later as well, it makes the
7189 * code much cleaner. pmd allocation is essential for the shared case because
7190 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
7191 * racing tasks could either miss the sharing (see huge_pte_offset) or select a
7192 * bad pmd for sharing.
7194 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7195 unsigned long addr, pud_t *pud)
7197 struct address_space *mapping = vma->vm_file->f_mapping;
7198 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
7199 vma->vm_pgoff;
7200 struct vm_area_struct *svma;
7201 unsigned long saddr;
7202 pte_t *spte = NULL;
7203 pte_t *pte;
7205 i_mmap_lock_read(mapping);
7206 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
7207 if (svma == vma)
7208 continue;
7210 saddr = page_table_shareable(svma, vma, addr, idx);
7211 if (saddr) {
7212 spte = hugetlb_walk(svma, saddr,
7213 vma_mmu_pagesize(svma));
7214 if (spte) {
7215 get_page(virt_to_page(spte));
7216 break;
7221 if (!spte)
7222 goto out;
7224 spin_lock(&mm->page_table_lock);
7225 if (pud_none(*pud)) {
7226 pud_populate(mm, pud,
7227 (pmd_t *)((unsigned long)spte & PAGE_MASK));
7228 mm_inc_nr_pmds(mm);
7229 } else {
7230 put_page(virt_to_page(spte));
7232 spin_unlock(&mm->page_table_lock);
7233 out:
7234 pte = (pte_t *)pmd_alloc(mm, pud, addr);
7235 i_mmap_unlock_read(mapping);
7236 return pte;
7240 * unmap huge page backed by shared pte.
7242 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
7243 * indicated by page_count > 1, unmap is achieved by clearing pud and
7244 * decrementing the ref count. If count == 1, the pte page is not shared.
7246 * Called with page table lock held.
7248 * returns: 1 successfully unmapped a shared pte page
7249 * 0 the underlying pte page is not shared, or it is the last user
7251 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
7252 unsigned long addr, pte_t *ptep)
7254 pgd_t *pgd = pgd_offset(mm, addr);
7255 p4d_t *p4d = p4d_offset(pgd, addr);
7256 pud_t *pud = pud_offset(p4d, addr);
7258 i_mmap_assert_write_locked(vma->vm_file->f_mapping);
7259 hugetlb_vma_assert_locked(vma);
7260 BUG_ON(page_count(virt_to_page(ptep)) == 0);
7261 if (page_count(virt_to_page(ptep)) == 1)
7262 return 0;
7264 pud_clear(pud);
7265 put_page(virt_to_page(ptep));
7266 mm_dec_nr_pmds(mm);
7267 return 1;
7270 #else /* !CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */
7272 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7273 unsigned long addr, pud_t *pud)
7275 return NULL;
7278 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
7279 unsigned long addr, pte_t *ptep)
7281 return 0;
7284 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7285 unsigned long *start, unsigned long *end)
7289 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7291 return false;
7293 #endif /* CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */
7295 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
7296 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
7297 unsigned long addr, unsigned long sz)
7299 pgd_t *pgd;
7300 p4d_t *p4d;
7301 pud_t *pud;
7302 pte_t *pte = NULL;
7304 pgd = pgd_offset(mm, addr);
7305 p4d = p4d_alloc(mm, pgd, addr);
7306 if (!p4d)
7307 return NULL;
7308 pud = pud_alloc(mm, p4d, addr);
7309 if (pud) {
7310 if (sz == PUD_SIZE) {
7311 pte = (pte_t *)pud;
7312 } else {
7313 BUG_ON(sz != PMD_SIZE);
7314 if (want_pmd_share(vma, addr) && pud_none(*pud))
7315 pte = huge_pmd_share(mm, vma, addr, pud);
7316 else
7317 pte = (pte_t *)pmd_alloc(mm, pud, addr);
7321 if (pte) {
7322 pte_t pteval = ptep_get_lockless(pte);
7324 BUG_ON(pte_present(pteval) && !pte_huge(pteval));
7327 return pte;
7331 * huge_pte_offset() - Walk the page table to resolve the hugepage
7332 * entry at address @addr
7334 * Return: Pointer to page table entry (PUD or PMD) for
7335 * address @addr, or NULL if a !p*d_present() entry is encountered and the
7336 * size @sz doesn't match the hugepage size at this level of the page
7337 * table.
7339 pte_t *huge_pte_offset(struct mm_struct *mm,
7340 unsigned long addr, unsigned long sz)
7342 pgd_t *pgd;
7343 p4d_t *p4d;
7344 pud_t *pud;
7345 pmd_t *pmd;
7347 pgd = pgd_offset(mm, addr);
7348 if (!pgd_present(*pgd))
7349 return NULL;
7350 p4d = p4d_offset(pgd, addr);
7351 if (!p4d_present(*p4d))
7352 return NULL;
7354 pud = pud_offset(p4d, addr);
7355 if (sz == PUD_SIZE)
7356 /* must be pud huge, non-present or none */
7357 return (pte_t *)pud;
7358 if (!pud_present(*pud))
7359 return NULL;
7360 /* must have a valid entry and size to go further */
7362 pmd = pmd_offset(pud, addr);
7363 /* must be pmd huge, non-present or none */
7364 return (pte_t *)pmd;
7368 * Return a mask that can be used to update an address to the last huge
7369 * page in a page table page mapping size. Used to skip non-present
7370 * page table entries when linearly scanning address ranges. Architectures
7371 * with unique huge page to page table relationships can define their own
7372 * version of this routine.
7374 unsigned long hugetlb_mask_last_page(struct hstate *h)
7376 unsigned long hp_size = huge_page_size(h);
7378 if (hp_size == PUD_SIZE)
7379 return P4D_SIZE - PUD_SIZE;
7380 else if (hp_size == PMD_SIZE)
7381 return PUD_SIZE - PMD_SIZE;
7382 else
7383 return 0UL;
7386 #else
7388 /* See description above. Architectures can provide their own version. */
7389 __weak unsigned long hugetlb_mask_last_page(struct hstate *h)
7391 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
7392 if (huge_page_size(h) == PMD_SIZE)
7393 return PUD_SIZE - PMD_SIZE;
7394 #endif
7395 return 0UL;
7398 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
7400 bool isolate_hugetlb(struct folio *folio, struct list_head *list)
7402 bool ret = true;
7404 spin_lock_irq(&hugetlb_lock);
7405 if (!folio_test_hugetlb(folio) ||
7406 !folio_test_hugetlb_migratable(folio) ||
7407 !folio_try_get(folio)) {
7408 ret = false;
7409 goto unlock;
7411 folio_clear_hugetlb_migratable(folio);
7412 list_move_tail(&folio->lru, list);
7413 unlock:
7414 spin_unlock_irq(&hugetlb_lock);
7415 return ret;
7418 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
7420 int ret = 0;
7422 *hugetlb = false;
7423 spin_lock_irq(&hugetlb_lock);
7424 if (folio_test_hugetlb(folio)) {
7425 *hugetlb = true;
7426 if (folio_test_hugetlb_freed(folio))
7427 ret = 0;
7428 else if (folio_test_hugetlb_migratable(folio) || unpoison)
7429 ret = folio_try_get(folio);
7430 else
7431 ret = -EBUSY;
7433 spin_unlock_irq(&hugetlb_lock);
7434 return ret;
7437 int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
7438 bool *migratable_cleared)
7440 int ret;
7442 spin_lock_irq(&hugetlb_lock);
7443 ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared);
7444 spin_unlock_irq(&hugetlb_lock);
7445 return ret;
7448 void folio_putback_active_hugetlb(struct folio *folio)
7450 spin_lock_irq(&hugetlb_lock);
7451 folio_set_hugetlb_migratable(folio);
7452 list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist);
7453 spin_unlock_irq(&hugetlb_lock);
7454 folio_put(folio);
7457 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason)
7459 struct hstate *h = folio_hstate(old_folio);
7461 hugetlb_cgroup_migrate(old_folio, new_folio);
7462 set_page_owner_migrate_reason(&new_folio->page, reason);
7465 * transfer temporary state of the new hugetlb folio. This is
7466 * reverse to other transitions because the newpage is going to
7467 * be final while the old one will be freed so it takes over
7468 * the temporary status.
7470 * Also note that we have to transfer the per-node surplus state
7471 * here as well otherwise the global surplus count will not match
7472 * the per-node's.
7474 if (folio_test_hugetlb_temporary(new_folio)) {
7475 int old_nid = folio_nid(old_folio);
7476 int new_nid = folio_nid(new_folio);
7478 folio_set_hugetlb_temporary(old_folio);
7479 folio_clear_hugetlb_temporary(new_folio);
7483 * There is no need to transfer the per-node surplus state
7484 * when we do not cross the node.
7486 if (new_nid == old_nid)
7487 return;
7488 spin_lock_irq(&hugetlb_lock);
7489 if (h->surplus_huge_pages_node[old_nid]) {
7490 h->surplus_huge_pages_node[old_nid]--;
7491 h->surplus_huge_pages_node[new_nid]++;
7493 spin_unlock_irq(&hugetlb_lock);
7497 static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
7498 unsigned long start,
7499 unsigned long end)
7501 struct hstate *h = hstate_vma(vma);
7502 unsigned long sz = huge_page_size(h);
7503 struct mm_struct *mm = vma->vm_mm;
7504 struct mmu_notifier_range range;
7505 unsigned long address;
7506 spinlock_t *ptl;
7507 pte_t *ptep;
7509 if (!(vma->vm_flags & VM_MAYSHARE))
7510 return;
7512 if (start >= end)
7513 return;
7515 flush_cache_range(vma, start, end);
7517 * No need to call adjust_range_if_pmd_sharing_possible(), because
7518 * we have already done the PUD_SIZE alignment.
7520 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
7521 start, end);
7522 mmu_notifier_invalidate_range_start(&range);
7523 hugetlb_vma_lock_write(vma);
7524 i_mmap_lock_write(vma->vm_file->f_mapping);
7525 for (address = start; address < end; address += PUD_SIZE) {
7526 ptep = hugetlb_walk(vma, address, sz);
7527 if (!ptep)
7528 continue;
7529 ptl = huge_pte_lock(h, mm, ptep);
7530 huge_pmd_unshare(mm, vma, address, ptep);
7531 spin_unlock(ptl);
7533 flush_hugetlb_tlb_range(vma, start, end);
7534 i_mmap_unlock_write(vma->vm_file->f_mapping);
7535 hugetlb_vma_unlock_write(vma);
7537 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs(), see
7538 * Documentation/mm/mmu_notifier.rst.
7540 mmu_notifier_invalidate_range_end(&range);
7544 * This function will unconditionally remove all the shared pmd pgtable entries
7545 * within the specific vma for a hugetlbfs memory range.
7547 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
7549 hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7550 ALIGN_DOWN(vma->vm_end, PUD_SIZE));
7553 #ifdef CONFIG_CMA
7554 static bool cma_reserve_called __initdata;
7556 static int __init cmdline_parse_hugetlb_cma(char *p)
7558 int nid, count = 0;
7559 unsigned long tmp;
7560 char *s = p;
7562 while (*s) {
7563 if (sscanf(s, "%lu%n", &tmp, &count) != 1)
7564 break;
7566 if (s[count] == ':') {
7567 if (tmp >= MAX_NUMNODES)
7568 break;
7569 nid = array_index_nospec(tmp, MAX_NUMNODES);
7571 s += count + 1;
7572 tmp = memparse(s, &s);
7573 hugetlb_cma_size_in_node[nid] = tmp;
7574 hugetlb_cma_size += tmp;
7577 * Skip the separator if have one, otherwise
7578 * break the parsing.
7580 if (*s == ',')
7581 s++;
7582 else
7583 break;
7584 } else {
7585 hugetlb_cma_size = memparse(p, &p);
7586 break;
7590 return 0;
7593 early_param("hugetlb_cma", cmdline_parse_hugetlb_cma);
7595 void __init hugetlb_cma_reserve(int order)
7597 unsigned long size, reserved, per_node;
7598 bool node_specific_cma_alloc = false;
7599 int nid;
7602 * HugeTLB CMA reservation is required for gigantic
7603 * huge pages which could not be allocated via the
7604 * page allocator. Just warn if there is any change
7605 * breaking this assumption.
7607 VM_WARN_ON(order <= MAX_PAGE_ORDER);
7608 cma_reserve_called = true;
7610 if (!hugetlb_cma_size)
7611 return;
7613 for (nid = 0; nid < MAX_NUMNODES; nid++) {
7614 if (hugetlb_cma_size_in_node[nid] == 0)
7615 continue;
7617 if (!node_online(nid)) {
7618 pr_warn("hugetlb_cma: invalid node %d specified\n", nid);
7619 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7620 hugetlb_cma_size_in_node[nid] = 0;
7621 continue;
7624 if (hugetlb_cma_size_in_node[nid] < (PAGE_SIZE << order)) {
7625 pr_warn("hugetlb_cma: cma area of node %d should be at least %lu MiB\n",
7626 nid, (PAGE_SIZE << order) / SZ_1M);
7627 hugetlb_cma_size -= hugetlb_cma_size_in_node[nid];
7628 hugetlb_cma_size_in_node[nid] = 0;
7629 } else {
7630 node_specific_cma_alloc = true;
7634 /* Validate the CMA size again in case some invalid nodes specified. */
7635 if (!hugetlb_cma_size)
7636 return;
7638 if (hugetlb_cma_size < (PAGE_SIZE << order)) {
7639 pr_warn("hugetlb_cma: cma area should be at least %lu MiB\n",
7640 (PAGE_SIZE << order) / SZ_1M);
7641 hugetlb_cma_size = 0;
7642 return;
7645 if (!node_specific_cma_alloc) {
7647 * If 3 GB area is requested on a machine with 4 numa nodes,
7648 * let's allocate 1 GB on first three nodes and ignore the last one.
7650 per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes);
7651 pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n",
7652 hugetlb_cma_size / SZ_1M, per_node / SZ_1M);
7655 reserved = 0;
7656 for_each_online_node(nid) {
7657 int res;
7658 char name[CMA_MAX_NAME];
7660 if (node_specific_cma_alloc) {
7661 if (hugetlb_cma_size_in_node[nid] == 0)
7662 continue;
7664 size = hugetlb_cma_size_in_node[nid];
7665 } else {
7666 size = min(per_node, hugetlb_cma_size - reserved);
7669 size = round_up(size, PAGE_SIZE << order);
7671 snprintf(name, sizeof(name), "hugetlb%d", nid);
7673 * Note that 'order per bit' is based on smallest size that
7674 * may be returned to CMA allocator in the case of
7675 * huge page demotion.
7677 res = cma_declare_contiguous_nid(0, size, 0,
7678 PAGE_SIZE << order,
7679 HUGETLB_PAGE_ORDER, false, name,
7680 &hugetlb_cma[nid], nid);
7681 if (res) {
7682 pr_warn("hugetlb_cma: reservation failed: err %d, node %d",
7683 res, nid);
7684 continue;
7687 reserved += size;
7688 pr_info("hugetlb_cma: reserved %lu MiB on node %d\n",
7689 size / SZ_1M, nid);
7691 if (reserved >= hugetlb_cma_size)
7692 break;
7695 if (!reserved)
7697 * hugetlb_cma_size is used to determine if allocations from
7698 * cma are possible. Set to zero if no cma regions are set up.
7700 hugetlb_cma_size = 0;
7703 static void __init hugetlb_cma_check(void)
7705 if (!hugetlb_cma_size || cma_reserve_called)
7706 return;
7708 pr_warn("hugetlb_cma: the option isn't supported by current arch\n");
7711 #endif /* CONFIG_CMA */