xref: /linux/mm/hugetlb.c (revision 84efbefa26df36a845a9210ee962aa6866f99bb7)
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/cpumask.h>
18 #include <linux/cpuset.h>
19 #include <linux/mutex.h>
20 #include <linux/memblock.h>
21 #include <linux/minmax.h>
22 #include <linux/sysfs.h>
23 #include <linux/slab.h>
24 #include <linux/sched/mm.h>
25 #include <linux/mmdebug.h>
26 #include <linux/sched/signal.h>
27 #include <linux/rmap.h>
28 #include <linux/string_choices.h>
29 #include <linux/string_helpers.h>
30 #include <linux/swap.h>
31 #include <linux/swapops.h>
32 #include <linux/jhash.h>
33 #include <linux/numa.h>
34 #include <linux/llist.h>
35 #include <linux/cma.h>
36 #include <linux/migrate.h>
37 #include <linux/nospec.h>
38 #include <linux/delayacct.h>
39 #include <linux/memory.h>
40 #include <linux/mm_inline.h>
41 #include <linux/padata.h>
42 
43 #include <asm/page.h>
44 #include <asm/pgalloc.h>
45 #include <asm/tlb.h>
46 #include <asm/setup.h>
47 
48 #include <linux/io.h>
49 #include <linux/hugetlb.h>
50 #include <linux/hugetlb_cgroup.h>
51 #include <linux/node.h>
52 #include <linux/page_owner.h>
53 #include "internal.h"
54 #include "hugetlb_vmemmap.h"
55 #include "hugetlb_cma.h"
56 #include <linux/page-isolation.h>
57 
58 int hugetlb_max_hstate __read_mostly;
59 unsigned int default_hstate_idx;
60 struct hstate hstates[HUGE_MAX_HSTATE];
61 
62 __initdata nodemask_t hugetlb_bootmem_nodes;
63 __initdata struct list_head huge_boot_pages[MAX_NUMNODES];
64 static unsigned long hstate_boot_nrinvalid[HUGE_MAX_HSTATE] __initdata;
65 
66 /*
67  * Due to ordering constraints across the init code for various
68  * architectures, hugetlb hstate cmdline parameters can't simply
69  * be early_param. early_param might call the setup function
70  * before valid hugetlb page sizes are determined, leading to
71  * incorrect rejection of valid hugepagesz= options.
72  *
73  * So, record the parameters early and consume them whenever the
74  * init code is ready for them, by calling hugetlb_parse_params().
75  */
76 
77 /* one (hugepagesz=,hugepages=) pair per hstate, one default_hugepagesz */
78 #define HUGE_MAX_CMDLINE_ARGS	(2 * HUGE_MAX_HSTATE + 1)
79 struct hugetlb_cmdline {
80 	char *val;
81 	int (*setup)(char *val);
82 };
83 
84 /* for command line parsing */
85 static struct hstate * __initdata parsed_hstate;
86 static unsigned long __initdata default_hstate_max_huge_pages;
87 static bool __initdata parsed_valid_hugepagesz = true;
88 static bool __initdata parsed_default_hugepagesz;
89 static unsigned int default_hugepages_in_node[MAX_NUMNODES] __initdata;
90 static unsigned long hugepage_allocation_threads __initdata;
91 
92 static char hstate_cmdline_buf[COMMAND_LINE_SIZE] __initdata;
93 static int hstate_cmdline_index __initdata;
94 static struct hugetlb_cmdline hugetlb_params[HUGE_MAX_CMDLINE_ARGS] __initdata;
95 static int hugetlb_param_index __initdata;
96 static __init int hugetlb_add_param(char *s, int (*setup)(char *val));
97 static __init void hugetlb_parse_params(void);
98 
99 #define hugetlb_early_param(str, func) \
100 static __init int func##args(char *s) \
101 { \
102 	return hugetlb_add_param(s, func); \
103 } \
104 early_param(str, func##args)
105 
106 /*
107  * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
108  * free_huge_pages, and surplus_huge_pages.
109  */
110 __cacheline_aligned_in_smp DEFINE_SPINLOCK(hugetlb_lock);
111 
112 /*
113  * Serializes faults on the same logical page.  This is used to
114  * prevent spurious OOMs when the hugepage pool is fully utilized.
115  */
116 static int num_fault_mutexes __ro_after_init;
117 struct mutex *hugetlb_fault_mutex_table __ro_after_init;
118 
119 /* Forward declaration */
120 static int hugetlb_acct_memory(struct hstate *h, long delta);
121 static void hugetlb_vma_lock_free(struct vm_area_struct *vma);
122 static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);
123 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma);
124 static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
125 		unsigned long start, unsigned long end, bool take_locks);
126 static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
127 
128 static void hugetlb_free_folio(struct folio *folio)
129 {
130 	if (folio_test_hugetlb_cma(folio)) {
131 		hugetlb_cma_free_folio(folio);
132 		return;
133 	}
134 
135 	folio_put(folio);
136 }
137 
138 static inline bool subpool_is_free(struct hugepage_subpool *spool)
139 {
140 	if (spool->count)
141 		return false;
142 	if (spool->max_hpages != -1)
143 		return spool->used_hpages == 0;
144 	if (spool->min_hpages != -1)
145 		return spool->rsv_hpages == spool->min_hpages;
146 
147 	return true;
148 }
149 
150 static inline void unlock_or_release_subpool(struct hugepage_subpool *spool,
151 						unsigned long irq_flags)
152 {
153 	spin_unlock_irqrestore(&spool->lock, irq_flags);
154 
155 	/* If no pages are used, and no other handles to the subpool
156 	 * remain, give up any reservations based on minimum size and
157 	 * free the subpool */
158 	if (subpool_is_free(spool)) {
159 		if (spool->min_hpages != -1)
160 			hugetlb_acct_memory(spool->hstate,
161 						-spool->min_hpages);
162 		kfree(spool);
163 	}
164 }
165 
166 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
167 						long min_hpages)
168 {
169 	struct hugepage_subpool *spool;
170 
171 	spool = kzalloc(sizeof(*spool), GFP_KERNEL);
172 	if (!spool)
173 		return NULL;
174 
175 	spin_lock_init(&spool->lock);
176 	spool->count = 1;
177 	spool->max_hpages = max_hpages;
178 	spool->hstate = h;
179 	spool->min_hpages = min_hpages;
180 
181 	if (min_hpages != -1 && hugetlb_acct_memory(h, min_hpages)) {
182 		kfree(spool);
183 		return NULL;
184 	}
185 	spool->rsv_hpages = min_hpages;
186 
187 	return spool;
188 }
189 
190 void hugepage_put_subpool(struct hugepage_subpool *spool)
191 {
192 	unsigned long flags;
193 
194 	spin_lock_irqsave(&spool->lock, flags);
195 	BUG_ON(!spool->count);
196 	spool->count--;
197 	unlock_or_release_subpool(spool, flags);
198 }
199 
200 /*
201  * Subpool accounting for allocating and reserving pages.
202  * Return -ENOMEM if there are not enough resources to satisfy the
203  * request.  Otherwise, return the number of pages by which the
204  * global pools must be adjusted (upward).  The returned value may
205  * only be different than the passed value (delta) in the case where
206  * a subpool minimum size must be maintained.
207  */
208 static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
209 				      long delta)
210 {
211 	long ret = delta;
212 
213 	if (!spool)
214 		return ret;
215 
216 	spin_lock_irq(&spool->lock);
217 
218 	if (spool->max_hpages != -1) {		/* maximum size accounting */
219 		if ((spool->used_hpages + delta) <= spool->max_hpages)
220 			spool->used_hpages += delta;
221 		else {
222 			ret = -ENOMEM;
223 			goto unlock_ret;
224 		}
225 	}
226 
227 	/* minimum size accounting */
228 	if (spool->min_hpages != -1 && spool->rsv_hpages) {
229 		if (delta > spool->rsv_hpages) {
230 			/*
231 			 * Asking for more reserves than those already taken on
232 			 * behalf of subpool.  Return difference.
233 			 */
234 			ret = delta - spool->rsv_hpages;
235 			spool->rsv_hpages = 0;
236 		} else {
237 			ret = 0;	/* reserves already accounted for */
238 			spool->rsv_hpages -= delta;
239 		}
240 	}
241 
242 unlock_ret:
243 	spin_unlock_irq(&spool->lock);
244 	return ret;
245 }
246 
247 /*
248  * Subpool accounting for freeing and unreserving pages.
249  * Return the number of global page reservations that must be dropped.
250  * The return value may only be different than the passed value (delta)
251  * in the case where a subpool minimum size must be maintained.
252  */
253 static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
254 				       long delta)
255 {
256 	long ret = delta;
257 	unsigned long flags;
258 
259 	if (!spool)
260 		return delta;
261 
262 	spin_lock_irqsave(&spool->lock, flags);
263 
264 	if (spool->max_hpages != -1)		/* maximum size accounting */
265 		spool->used_hpages -= delta;
266 
267 	 /* minimum size accounting */
268 	if (spool->min_hpages != -1 && spool->used_hpages < spool->min_hpages) {
269 		if (spool->rsv_hpages + delta <= spool->min_hpages)
270 			ret = 0;
271 		else
272 			ret = spool->rsv_hpages + delta - spool->min_hpages;
273 
274 		spool->rsv_hpages += delta;
275 		if (spool->rsv_hpages > spool->min_hpages)
276 			spool->rsv_hpages = spool->min_hpages;
277 	}
278 
279 	/*
280 	 * If hugetlbfs_put_super couldn't free spool due to an outstanding
281 	 * quota reference, free it now.
282 	 */
283 	unlock_or_release_subpool(spool, flags);
284 
285 	return ret;
286 }
287 
288 static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
289 {
290 	return subpool_inode(file_inode(vma->vm_file));
291 }
292 
293 /*
294  * hugetlb vma_lock helper routines
295  */
296 void hugetlb_vma_lock_read(struct vm_area_struct *vma)
297 {
298 	if (__vma_shareable_lock(vma)) {
299 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
300 
301 		down_read(&vma_lock->rw_sema);
302 	} else if (__vma_private_lock(vma)) {
303 		struct resv_map *resv_map = vma_resv_map(vma);
304 
305 		down_read(&resv_map->rw_sema);
306 	}
307 }
308 
309 void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
310 {
311 	if (__vma_shareable_lock(vma)) {
312 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
313 
314 		up_read(&vma_lock->rw_sema);
315 	} else if (__vma_private_lock(vma)) {
316 		struct resv_map *resv_map = vma_resv_map(vma);
317 
318 		up_read(&resv_map->rw_sema);
319 	}
320 }
321 
322 void hugetlb_vma_lock_write(struct vm_area_struct *vma)
323 {
324 	if (__vma_shareable_lock(vma)) {
325 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
326 
327 		down_write(&vma_lock->rw_sema);
328 	} else if (__vma_private_lock(vma)) {
329 		struct resv_map *resv_map = vma_resv_map(vma);
330 
331 		down_write(&resv_map->rw_sema);
332 	}
333 }
334 
335 void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
336 {
337 	if (__vma_shareable_lock(vma)) {
338 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
339 
340 		up_write(&vma_lock->rw_sema);
341 	} else if (__vma_private_lock(vma)) {
342 		struct resv_map *resv_map = vma_resv_map(vma);
343 
344 		up_write(&resv_map->rw_sema);
345 	}
346 }
347 
348 int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
349 {
350 
351 	if (__vma_shareable_lock(vma)) {
352 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
353 
354 		return down_write_trylock(&vma_lock->rw_sema);
355 	} else if (__vma_private_lock(vma)) {
356 		struct resv_map *resv_map = vma_resv_map(vma);
357 
358 		return down_write_trylock(&resv_map->rw_sema);
359 	}
360 
361 	return 1;
362 }
363 
364 void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
365 {
366 	if (__vma_shareable_lock(vma)) {
367 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
368 
369 		lockdep_assert_held(&vma_lock->rw_sema);
370 	} else if (__vma_private_lock(vma)) {
371 		struct resv_map *resv_map = vma_resv_map(vma);
372 
373 		lockdep_assert_held(&resv_map->rw_sema);
374 	}
375 }
376 
377 void hugetlb_vma_lock_release(struct kref *kref)
378 {
379 	struct hugetlb_vma_lock *vma_lock = container_of(kref,
380 			struct hugetlb_vma_lock, refs);
381 
382 	kfree(vma_lock);
383 }
384 
385 static void __hugetlb_vma_unlock_write_put(struct hugetlb_vma_lock *vma_lock)
386 {
387 	struct vm_area_struct *vma = vma_lock->vma;
388 
389 	/*
390 	 * vma_lock structure may or not be released as a result of put,
391 	 * it certainly will no longer be attached to vma so clear pointer.
392 	 * Semaphore synchronizes access to vma_lock->vma field.
393 	 */
394 	vma_lock->vma = NULL;
395 	vma->vm_private_data = NULL;
396 	up_write(&vma_lock->rw_sema);
397 	kref_put(&vma_lock->refs, hugetlb_vma_lock_release);
398 }
399 
400 static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)
401 {
402 	if (__vma_shareable_lock(vma)) {
403 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
404 
405 		__hugetlb_vma_unlock_write_put(vma_lock);
406 	} else if (__vma_private_lock(vma)) {
407 		struct resv_map *resv_map = vma_resv_map(vma);
408 
409 		/* no free for anon vmas, but still need to unlock */
410 		up_write(&resv_map->rw_sema);
411 	}
412 }
413 
414 static void hugetlb_vma_lock_free(struct vm_area_struct *vma)
415 {
416 	/*
417 	 * Only present in sharable vmas.
418 	 */
419 	if (!vma || !__vma_shareable_lock(vma))
420 		return;
421 
422 	if (vma->vm_private_data) {
423 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
424 
425 		down_write(&vma_lock->rw_sema);
426 		__hugetlb_vma_unlock_write_put(vma_lock);
427 	}
428 }
429 
430 static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma)
431 {
432 	struct hugetlb_vma_lock *vma_lock;
433 
434 	/* Only establish in (flags) sharable vmas */
435 	if (!vma || !(vma->vm_flags & VM_MAYSHARE))
436 		return;
437 
438 	/* Should never get here with non-NULL vm_private_data */
439 	if (vma->vm_private_data)
440 		return;
441 
442 	vma_lock = kmalloc(sizeof(*vma_lock), GFP_KERNEL);
443 	if (!vma_lock) {
444 		/*
445 		 * If we can not allocate structure, then vma can not
446 		 * participate in pmd sharing.  This is only a possible
447 		 * performance enhancement and memory saving issue.
448 		 * However, the lock is also used to synchronize page
449 		 * faults with truncation.  If the lock is not present,
450 		 * unlikely races could leave pages in a file past i_size
451 		 * until the file is removed.  Warn in the unlikely case of
452 		 * allocation failure.
453 		 */
454 		pr_warn_once("HugeTLB: unable to allocate vma specific lock\n");
455 		return;
456 	}
457 
458 	kref_init(&vma_lock->refs);
459 	init_rwsem(&vma_lock->rw_sema);
460 	vma_lock->vma = vma;
461 	vma->vm_private_data = vma_lock;
462 }
463 
464 /* Helper that removes a struct file_region from the resv_map cache and returns
465  * it for use.
466  */
467 static struct file_region *
468 get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)
469 {
470 	struct file_region *nrg;
471 
472 	VM_BUG_ON(resv->region_cache_count <= 0);
473 
474 	resv->region_cache_count--;
475 	nrg = list_first_entry(&resv->region_cache, struct file_region, link);
476 	list_del(&nrg->link);
477 
478 	nrg->from = from;
479 	nrg->to = to;
480 
481 	return nrg;
482 }
483 
484 static void copy_hugetlb_cgroup_uncharge_info(struct file_region *nrg,
485 					      struct file_region *rg)
486 {
487 #ifdef CONFIG_CGROUP_HUGETLB
488 	nrg->reservation_counter = rg->reservation_counter;
489 	nrg->css = rg->css;
490 	if (rg->css)
491 		css_get(rg->css);
492 #endif
493 }
494 
495 /* Helper that records hugetlb_cgroup uncharge info. */
496 static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,
497 						struct hstate *h,
498 						struct resv_map *resv,
499 						struct file_region *nrg)
500 {
501 #ifdef CONFIG_CGROUP_HUGETLB
502 	if (h_cg) {
503 		nrg->reservation_counter =
504 			&h_cg->rsvd_hugepage[hstate_index(h)];
505 		nrg->css = &h_cg->css;
506 		/*
507 		 * The caller will hold exactly one h_cg->css reference for the
508 		 * whole contiguous reservation region. But this area might be
509 		 * scattered when there are already some file_regions reside in
510 		 * it. As a result, many file_regions may share only one css
511 		 * reference. In order to ensure that one file_region must hold
512 		 * exactly one h_cg->css reference, we should do css_get for
513 		 * each file_region and leave the reference held by caller
514 		 * untouched.
515 		 */
516 		css_get(&h_cg->css);
517 		if (!resv->pages_per_hpage)
518 			resv->pages_per_hpage = pages_per_huge_page(h);
519 		/* pages_per_hpage should be the same for all entries in
520 		 * a resv_map.
521 		 */
522 		VM_BUG_ON(resv->pages_per_hpage != pages_per_huge_page(h));
523 	} else {
524 		nrg->reservation_counter = NULL;
525 		nrg->css = NULL;
526 	}
527 #endif
528 }
529 
530 static void put_uncharge_info(struct file_region *rg)
531 {
532 #ifdef CONFIG_CGROUP_HUGETLB
533 	if (rg->css)
534 		css_put(rg->css);
535 #endif
536 }
537 
538 static bool has_same_uncharge_info(struct file_region *rg,
539 				   struct file_region *org)
540 {
541 #ifdef CONFIG_CGROUP_HUGETLB
542 	return rg->reservation_counter == org->reservation_counter &&
543 	       rg->css == org->css;
544 
545 #else
546 	return true;
547 #endif
548 }
549 
550 static void coalesce_file_region(struct resv_map *resv, struct file_region *rg)
551 {
552 	struct file_region *nrg, *prg;
553 
554 	prg = list_prev_entry(rg, link);
555 	if (&prg->link != &resv->regions && prg->to == rg->from &&
556 	    has_same_uncharge_info(prg, rg)) {
557 		prg->to = rg->to;
558 
559 		list_del(&rg->link);
560 		put_uncharge_info(rg);
561 		kfree(rg);
562 
563 		rg = prg;
564 	}
565 
566 	nrg = list_next_entry(rg, link);
567 	if (&nrg->link != &resv->regions && nrg->from == rg->to &&
568 	    has_same_uncharge_info(nrg, rg)) {
569 		nrg->from = rg->from;
570 
571 		list_del(&rg->link);
572 		put_uncharge_info(rg);
573 		kfree(rg);
574 	}
575 }
576 
577 static inline long
578 hugetlb_resv_map_add(struct resv_map *map, struct list_head *rg, long from,
579 		     long to, struct hstate *h, struct hugetlb_cgroup *cg,
580 		     long *regions_needed)
581 {
582 	struct file_region *nrg;
583 
584 	if (!regions_needed) {
585 		nrg = get_file_region_entry_from_cache(map, from, to);
586 		record_hugetlb_cgroup_uncharge_info(cg, h, map, nrg);
587 		list_add(&nrg->link, rg);
588 		coalesce_file_region(map, nrg);
589 	} else
590 		*regions_needed += 1;
591 
592 	return to - from;
593 }
594 
595 /*
596  * Must be called with resv->lock held.
597  *
598  * Calling this with regions_needed != NULL will count the number of pages
599  * to be added but will not modify the linked list. And regions_needed will
600  * indicate the number of file_regions needed in the cache to carry out to add
601  * the regions for this range.
602  */
603 static long add_reservation_in_range(struct resv_map *resv, long f, long t,
604 				     struct hugetlb_cgroup *h_cg,
605 				     struct hstate *h, long *regions_needed)
606 {
607 	long add = 0;
608 	struct list_head *head = &resv->regions;
609 	long last_accounted_offset = f;
610 	struct file_region *iter, *trg = NULL;
611 	struct list_head *rg = NULL;
612 
613 	if (regions_needed)
614 		*regions_needed = 0;
615 
616 	/* In this loop, we essentially handle an entry for the range
617 	 * [last_accounted_offset, iter->from), at every iteration, with some
618 	 * bounds checking.
619 	 */
620 	list_for_each_entry_safe(iter, trg, head, link) {
621 		/* Skip irrelevant regions that start before our range. */
622 		if (iter->from < f) {
623 			/* If this region ends after the last accounted offset,
624 			 * then we need to update last_accounted_offset.
625 			 */
626 			if (iter->to > last_accounted_offset)
627 				last_accounted_offset = iter->to;
628 			continue;
629 		}
630 
631 		/* When we find a region that starts beyond our range, we've
632 		 * finished.
633 		 */
634 		if (iter->from >= t) {
635 			rg = iter->link.prev;
636 			break;
637 		}
638 
639 		/* Add an entry for last_accounted_offset -> iter->from, and
640 		 * update last_accounted_offset.
641 		 */
642 		if (iter->from > last_accounted_offset)
643 			add += hugetlb_resv_map_add(resv, iter->link.prev,
644 						    last_accounted_offset,
645 						    iter->from, h, h_cg,
646 						    regions_needed);
647 
648 		last_accounted_offset = iter->to;
649 	}
650 
651 	/* Handle the case where our range extends beyond
652 	 * last_accounted_offset.
653 	 */
654 	if (!rg)
655 		rg = head->prev;
656 	if (last_accounted_offset < t)
657 		add += hugetlb_resv_map_add(resv, rg, last_accounted_offset,
658 					    t, h, h_cg, regions_needed);
659 
660 	return add;
661 }
662 
663 /* Must be called with resv->lock acquired. Will drop lock to allocate entries.
664  */
665 static int allocate_file_region_entries(struct resv_map *resv,
666 					int regions_needed)
667 	__must_hold(&resv->lock)
668 {
669 	LIST_HEAD(allocated_regions);
670 	int to_allocate = 0, i = 0;
671 	struct file_region *trg = NULL, *rg = NULL;
672 
673 	VM_BUG_ON(regions_needed < 0);
674 
675 	/*
676 	 * Check for sufficient descriptors in the cache to accommodate
677 	 * the number of in progress add operations plus regions_needed.
678 	 *
679 	 * This is a while loop because when we drop the lock, some other call
680 	 * to region_add or region_del may have consumed some region_entries,
681 	 * so we keep looping here until we finally have enough entries for
682 	 * (adds_in_progress + regions_needed).
683 	 */
684 	while (resv->region_cache_count <
685 	       (resv->adds_in_progress + regions_needed)) {
686 		to_allocate = resv->adds_in_progress + regions_needed -
687 			      resv->region_cache_count;
688 
689 		/* At this point, we should have enough entries in the cache
690 		 * for all the existing adds_in_progress. We should only be
691 		 * needing to allocate for regions_needed.
692 		 */
693 		VM_BUG_ON(resv->region_cache_count < resv->adds_in_progress);
694 
695 		spin_unlock(&resv->lock);
696 		for (i = 0; i < to_allocate; i++) {
697 			trg = kmalloc(sizeof(*trg), GFP_KERNEL);
698 			if (!trg)
699 				goto out_of_memory;
700 			list_add(&trg->link, &allocated_regions);
701 		}
702 
703 		spin_lock(&resv->lock);
704 
705 		list_splice(&allocated_regions, &resv->region_cache);
706 		resv->region_cache_count += to_allocate;
707 	}
708 
709 	return 0;
710 
711 out_of_memory:
712 	list_for_each_entry_safe(rg, trg, &allocated_regions, link) {
713 		list_del(&rg->link);
714 		kfree(rg);
715 	}
716 	return -ENOMEM;
717 }
718 
719 /*
720  * Add the huge page range represented by [f, t) to the reserve
721  * map.  Regions will be taken from the cache to fill in this range.
722  * Sufficient regions should exist in the cache due to the previous
723  * call to region_chg with the same range, but in some cases the cache will not
724  * have sufficient entries due to races with other code doing region_add or
725  * region_del.  The extra needed entries will be allocated.
726  *
727  * regions_needed is the out value provided by a previous call to region_chg.
728  *
729  * Return the number of new huge pages added to the map.  This number is greater
730  * than or equal to zero.  If file_region entries needed to be allocated for
731  * this operation and we were not able to allocate, it returns -ENOMEM.
732  * region_add of regions of length 1 never allocate file_regions and cannot
733  * fail; region_chg will always allocate at least 1 entry and a region_add for
734  * 1 page will only require at most 1 entry.
735  */
736 static long region_add(struct resv_map *resv, long f, long t,
737 		       long in_regions_needed, struct hstate *h,
738 		       struct hugetlb_cgroup *h_cg)
739 {
740 	long add = 0, actual_regions_needed = 0;
741 
742 	spin_lock(&resv->lock);
743 retry:
744 
745 	/* Count how many regions are actually needed to execute this add. */
746 	add_reservation_in_range(resv, f, t, NULL, NULL,
747 				 &actual_regions_needed);
748 
749 	/*
750 	 * Check for sufficient descriptors in the cache to accommodate
751 	 * this add operation. Note that actual_regions_needed may be greater
752 	 * than in_regions_needed, as the resv_map may have been modified since
753 	 * the region_chg call. In this case, we need to make sure that we
754 	 * allocate extra entries, such that we have enough for all the
755 	 * existing adds_in_progress, plus the excess needed for this
756 	 * operation.
757 	 */
758 	if (actual_regions_needed > in_regions_needed &&
759 	    resv->region_cache_count <
760 		    resv->adds_in_progress +
761 			    (actual_regions_needed - in_regions_needed)) {
762 		/* region_add operation of range 1 should never need to
763 		 * allocate file_region entries.
764 		 */
765 		VM_BUG_ON(t - f <= 1);
766 
767 		if (allocate_file_region_entries(
768 			    resv, actual_regions_needed - in_regions_needed)) {
769 			return -ENOMEM;
770 		}
771 
772 		goto retry;
773 	}
774 
775 	add = add_reservation_in_range(resv, f, t, h_cg, h, NULL);
776 
777 	resv->adds_in_progress -= in_regions_needed;
778 
779 	spin_unlock(&resv->lock);
780 	return add;
781 }
782 
783 /*
784  * Examine the existing reserve map and determine how many
785  * huge pages in the specified range [f, t) are NOT currently
786  * represented.  This routine is called before a subsequent
787  * call to region_add that will actually modify the reserve
788  * map to add the specified range [f, t).  region_chg does
789  * not change the number of huge pages represented by the
790  * map.  A number of new file_region structures is added to the cache as a
791  * placeholder, for the subsequent region_add call to use. At least 1
792  * file_region structure is added.
793  *
794  * out_regions_needed is the number of regions added to the
795  * resv->adds_in_progress.  This value needs to be provided to a follow up call
796  * to region_add or region_abort for proper accounting.
797  *
798  * Returns the number of huge pages that need to be added to the existing
799  * reservation map for the range [f, t).  This number is greater or equal to
800  * zero.  -ENOMEM is returned if a new file_region structure or cache entry
801  * is needed and can not be allocated.
802  */
803 static long region_chg(struct resv_map *resv, long f, long t,
804 		       long *out_regions_needed)
805 {
806 	long chg = 0;
807 
808 	spin_lock(&resv->lock);
809 
810 	/* Count how many hugepages in this range are NOT represented. */
811 	chg = add_reservation_in_range(resv, f, t, NULL, NULL,
812 				       out_regions_needed);
813 
814 	if (*out_regions_needed == 0)
815 		*out_regions_needed = 1;
816 
817 	if (allocate_file_region_entries(resv, *out_regions_needed))
818 		return -ENOMEM;
819 
820 	resv->adds_in_progress += *out_regions_needed;
821 
822 	spin_unlock(&resv->lock);
823 	return chg;
824 }
825 
826 /*
827  * Abort the in progress add operation.  The adds_in_progress field
828  * of the resv_map keeps track of the operations in progress between
829  * calls to region_chg and region_add.  Operations are sometimes
830  * aborted after the call to region_chg.  In such cases, region_abort
831  * is called to decrement the adds_in_progress counter. regions_needed
832  * is the value returned by the region_chg call, it is used to decrement
833  * the adds_in_progress counter.
834  *
835  * NOTE: The range arguments [f, t) are not needed or used in this
836  * routine.  They are kept to make reading the calling code easier as
837  * arguments will match the associated region_chg call.
838  */
839 static void region_abort(struct resv_map *resv, long f, long t,
840 			 long regions_needed)
841 {
842 	spin_lock(&resv->lock);
843 	VM_BUG_ON(!resv->region_cache_count);
844 	resv->adds_in_progress -= regions_needed;
845 	spin_unlock(&resv->lock);
846 }
847 
848 /*
849  * Delete the specified range [f, t) from the reserve map.  If the
850  * t parameter is LONG_MAX, this indicates that ALL regions after f
851  * should be deleted.  Locate the regions which intersect [f, t)
852  * and either trim, delete or split the existing regions.
853  *
854  * Returns the number of huge pages deleted from the reserve map.
855  * In the normal case, the return value is zero or more.  In the
856  * case where a region must be split, a new region descriptor must
857  * be allocated.  If the allocation fails, -ENOMEM will be returned.
858  * NOTE: If the parameter t == LONG_MAX, then we will never split
859  * a region and possibly return -ENOMEM.  Callers specifying
860  * t == LONG_MAX do not need to check for -ENOMEM error.
861  */
862 static long region_del(struct resv_map *resv, long f, long t)
863 {
864 	struct list_head *head = &resv->regions;
865 	struct file_region *rg, *trg;
866 	struct file_region *nrg = NULL;
867 	long del = 0;
868 
869 retry:
870 	spin_lock(&resv->lock);
871 	list_for_each_entry_safe(rg, trg, head, link) {
872 		/*
873 		 * Skip regions before the range to be deleted.  file_region
874 		 * ranges are normally of the form [from, to).  However, there
875 		 * may be a "placeholder" entry in the map which is of the form
876 		 * (from, to) with from == to.  Check for placeholder entries
877 		 * at the beginning of the range to be deleted.
878 		 */
879 		if (rg->to <= f && (rg->to != rg->from || rg->to != f))
880 			continue;
881 
882 		if (rg->from >= t)
883 			break;
884 
885 		if (f > rg->from && t < rg->to) { /* Must split region */
886 			/*
887 			 * Check for an entry in the cache before dropping
888 			 * lock and attempting allocation.
889 			 */
890 			if (!nrg &&
891 			    resv->region_cache_count > resv->adds_in_progress) {
892 				nrg = list_first_entry(&resv->region_cache,
893 							struct file_region,
894 							link);
895 				list_del(&nrg->link);
896 				resv->region_cache_count--;
897 			}
898 
899 			if (!nrg) {
900 				spin_unlock(&resv->lock);
901 				nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
902 				if (!nrg)
903 					return -ENOMEM;
904 				goto retry;
905 			}
906 
907 			del += t - f;
908 			hugetlb_cgroup_uncharge_file_region(
909 				resv, rg, t - f, false);
910 
911 			/* New entry for end of split region */
912 			nrg->from = t;
913 			nrg->to = rg->to;
914 
915 			copy_hugetlb_cgroup_uncharge_info(nrg, rg);
916 
917 			INIT_LIST_HEAD(&nrg->link);
918 
919 			/* Original entry is trimmed */
920 			rg->to = f;
921 
922 			list_add(&nrg->link, &rg->link);
923 			nrg = NULL;
924 			break;
925 		}
926 
927 		if (f <= rg->from && t >= rg->to) { /* Remove entire region */
928 			del += rg->to - rg->from;
929 			hugetlb_cgroup_uncharge_file_region(resv, rg,
930 							    rg->to - rg->from, true);
931 			list_del(&rg->link);
932 			kfree(rg);
933 			continue;
934 		}
935 
936 		if (f <= rg->from) {	/* Trim beginning of region */
937 			hugetlb_cgroup_uncharge_file_region(resv, rg,
938 							    t - rg->from, false);
939 
940 			del += t - rg->from;
941 			rg->from = t;
942 		} else {		/* Trim end of region */
943 			hugetlb_cgroup_uncharge_file_region(resv, rg,
944 							    rg->to - f, false);
945 
946 			del += rg->to - f;
947 			rg->to = f;
948 		}
949 	}
950 
951 	spin_unlock(&resv->lock);
952 	kfree(nrg);
953 	return del;
954 }
955 
956 /*
957  * A rare out of memory error was encountered which prevented removal of
958  * the reserve map region for a page.  The huge page itself was free'ed
959  * and removed from the page cache.  This routine will adjust the subpool
960  * usage count, and the global reserve count if needed.  By incrementing
961  * these counts, the reserve map entry which could not be deleted will
962  * appear as a "reserved" entry instead of simply dangling with incorrect
963  * counts.
964  */
965 void hugetlb_fix_reserve_counts(struct inode *inode)
966 {
967 	struct hugepage_subpool *spool = subpool_inode(inode);
968 	long rsv_adjust;
969 	bool reserved = false;
970 
971 	rsv_adjust = hugepage_subpool_get_pages(spool, 1);
972 	if (rsv_adjust > 0) {
973 		struct hstate *h = hstate_inode(inode);
974 
975 		if (!hugetlb_acct_memory(h, 1))
976 			reserved = true;
977 	} else if (!rsv_adjust) {
978 		reserved = true;
979 	}
980 
981 	if (!reserved)
982 		pr_warn("hugetlb: Huge Page Reserved count may go negative.\n");
983 }
984 
985 /*
986  * Count and return the number of huge pages in the reserve map
987  * that intersect with the range [f, t).
988  */
989 static long region_count(struct resv_map *resv, long f, long t)
990 {
991 	struct list_head *head = &resv->regions;
992 	struct file_region *rg;
993 	long chg = 0;
994 
995 	spin_lock(&resv->lock);
996 	/* Locate each segment we overlap with, and count that overlap. */
997 	list_for_each_entry(rg, head, link) {
998 		long seg_from;
999 		long seg_to;
1000 
1001 		if (rg->to <= f)
1002 			continue;
1003 		if (rg->from >= t)
1004 			break;
1005 
1006 		seg_from = max(rg->from, f);
1007 		seg_to = min(rg->to, t);
1008 
1009 		chg += seg_to - seg_from;
1010 	}
1011 	spin_unlock(&resv->lock);
1012 
1013 	return chg;
1014 }
1015 
1016 /*
1017  * Convert the address within this vma to the page offset within
1018  * the mapping, huge page units here.
1019  */
1020 static pgoff_t vma_hugecache_offset(struct hstate *h,
1021 			struct vm_area_struct *vma, unsigned long address)
1022 {
1023 	return ((address - vma->vm_start) >> huge_page_shift(h)) +
1024 			(vma->vm_pgoff >> huge_page_order(h));
1025 }
1026 
1027 /**
1028  * vma_kernel_pagesize - Page size granularity for this VMA.
1029  * @vma: The user mapping.
1030  *
1031  * Folios in this VMA will be aligned to, and at least the size of the
1032  * number of bytes returned by this function.
1033  *
1034  * Return: The default size of the folios allocated when backing a VMA.
1035  */
1036 unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1037 {
1038 	if (vma->vm_ops && vma->vm_ops->pagesize)
1039 		return vma->vm_ops->pagesize(vma);
1040 	return PAGE_SIZE;
1041 }
1042 EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
1043 
1044 /*
1045  * Return the page size being used by the MMU to back a VMA. In the majority
1046  * of cases, the page size used by the kernel matches the MMU size. On
1047  * architectures where it differs, an architecture-specific 'strong'
1048  * version of this symbol is required.
1049  */
1050 __weak unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1051 {
1052 	return vma_kernel_pagesize(vma);
1053 }
1054 
1055 /*
1056  * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
1057  * bits of the reservation map pointer, which are always clear due to
1058  * alignment.
1059  */
1060 #define HPAGE_RESV_OWNER    (1UL << 0)
1061 #define HPAGE_RESV_UNMAPPED (1UL << 1)
1062 #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
1063 
1064 /*
1065  * These helpers are used to track how many pages are reserved for
1066  * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
1067  * is guaranteed to have their future faults succeed.
1068  *
1069  * With the exception of hugetlb_dup_vma_private() which is called at fork(),
1070  * the reserve counters are updated with the hugetlb_lock held. It is safe
1071  * to reset the VMA at fork() time as it is not in use yet and there is no
1072  * chance of the global counters getting corrupted as a result of the values.
1073  *
1074  * The private mapping reservation is represented in a subtly different
1075  * manner to a shared mapping.  A shared mapping has a region map associated
1076  * with the underlying file, this region map represents the backing file
1077  * pages which have ever had a reservation assigned which this persists even
1078  * after the page is instantiated.  A private mapping has a region map
1079  * associated with the original mmap which is attached to all VMAs which
1080  * reference it, this region map represents those offsets which have consumed
1081  * reservation ie. where pages have been instantiated.
1082  */
1083 static unsigned long get_vma_private_data(struct vm_area_struct *vma)
1084 {
1085 	return (unsigned long)vma->vm_private_data;
1086 }
1087 
1088 static void set_vma_private_data(struct vm_area_struct *vma,
1089 							unsigned long value)
1090 {
1091 	vma->vm_private_data = (void *)value;
1092 }
1093 
1094 static void
1095 resv_map_set_hugetlb_cgroup_uncharge_info(struct resv_map *resv_map,
1096 					  struct hugetlb_cgroup *h_cg,
1097 					  struct hstate *h)
1098 {
1099 #ifdef CONFIG_CGROUP_HUGETLB
1100 	if (!h_cg || !h) {
1101 		resv_map->reservation_counter = NULL;
1102 		resv_map->pages_per_hpage = 0;
1103 		resv_map->css = NULL;
1104 	} else {
1105 		resv_map->reservation_counter =
1106 			&h_cg->rsvd_hugepage[hstate_index(h)];
1107 		resv_map->pages_per_hpage = pages_per_huge_page(h);
1108 		resv_map->css = &h_cg->css;
1109 	}
1110 #endif
1111 }
1112 
1113 struct resv_map *resv_map_alloc(void)
1114 {
1115 	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
1116 	struct file_region *rg = kmalloc(sizeof(*rg), GFP_KERNEL);
1117 
1118 	if (!resv_map || !rg) {
1119 		kfree(resv_map);
1120 		kfree(rg);
1121 		return NULL;
1122 	}
1123 
1124 	kref_init(&resv_map->refs);
1125 	spin_lock_init(&resv_map->lock);
1126 	INIT_LIST_HEAD(&resv_map->regions);
1127 	init_rwsem(&resv_map->rw_sema);
1128 
1129 	resv_map->adds_in_progress = 0;
1130 	/*
1131 	 * Initialize these to 0. On shared mappings, 0's here indicate these
1132 	 * fields don't do cgroup accounting. On private mappings, these will be
1133 	 * re-initialized to the proper values, to indicate that hugetlb cgroup
1134 	 * reservations are to be un-charged from here.
1135 	 */
1136 	resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, NULL, NULL);
1137 
1138 	INIT_LIST_HEAD(&resv_map->region_cache);
1139 	list_add(&rg->link, &resv_map->region_cache);
1140 	resv_map->region_cache_count = 1;
1141 
1142 	return resv_map;
1143 }
1144 
1145 void resv_map_release(struct kref *ref)
1146 {
1147 	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
1148 	struct list_head *head = &resv_map->region_cache;
1149 	struct file_region *rg, *trg;
1150 
1151 	/* Clear out any active regions before we release the map. */
1152 	region_del(resv_map, 0, LONG_MAX);
1153 
1154 	/* ... and any entries left in the cache */
1155 	list_for_each_entry_safe(rg, trg, head, link) {
1156 		list_del(&rg->link);
1157 		kfree(rg);
1158 	}
1159 
1160 	VM_BUG_ON(resv_map->adds_in_progress);
1161 
1162 	kfree(resv_map);
1163 }
1164 
1165 static inline struct resv_map *inode_resv_map(struct inode *inode)
1166 {
1167 	/*
1168 	 * At inode evict time, i_mapping may not point to the original
1169 	 * address space within the inode.  This original address space
1170 	 * contains the pointer to the resv_map.  So, always use the
1171 	 * address space embedded within the inode.
1172 	 * The VERY common case is inode->mapping == &inode->i_data but,
1173 	 * this may not be true for device special inodes.
1174 	 */
1175 	return (struct resv_map *)(&inode->i_data)->i_private_data;
1176 }
1177 
1178 static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
1179 {
1180 	VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1181 	if (vma->vm_flags & VM_MAYSHARE) {
1182 		struct address_space *mapping = vma->vm_file->f_mapping;
1183 		struct inode *inode = mapping->host;
1184 
1185 		return inode_resv_map(inode);
1186 
1187 	} else {
1188 		return (struct resv_map *)(get_vma_private_data(vma) &
1189 							~HPAGE_RESV_MASK);
1190 	}
1191 }
1192 
1193 static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
1194 {
1195 	VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1196 	VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
1197 
1198 	set_vma_private_data(vma, (unsigned long)map);
1199 }
1200 
1201 static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
1202 {
1203 	VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1204 	VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
1205 
1206 	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
1207 }
1208 
1209 static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
1210 {
1211 	VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1212 
1213 	return (get_vma_private_data(vma) & flag) != 0;
1214 }
1215 
1216 bool __vma_private_lock(struct vm_area_struct *vma)
1217 {
1218 	return !(vma->vm_flags & VM_MAYSHARE) &&
1219 		get_vma_private_data(vma) & ~HPAGE_RESV_MASK &&
1220 		is_vma_resv_set(vma, HPAGE_RESV_OWNER);
1221 }
1222 
1223 void hugetlb_dup_vma_private(struct vm_area_struct *vma)
1224 {
1225 	VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
1226 	/*
1227 	 * Clear vm_private_data
1228 	 * - For shared mappings this is a per-vma semaphore that may be
1229 	 *   allocated in a subsequent call to hugetlb_vm_op_open.
1230 	 *   Before clearing, make sure pointer is not associated with vma
1231 	 *   as this will leak the structure.  This is the case when called
1232 	 *   via clear_vma_resv_huge_pages() and hugetlb_vm_op_open has already
1233 	 *   been called to allocate a new structure.
1234 	 * - For MAP_PRIVATE mappings, this is the reserve map which does
1235 	 *   not apply to children.  Faults generated by the children are
1236 	 *   not guaranteed to succeed, even if read-only.
1237 	 */
1238 	if (vma->vm_flags & VM_MAYSHARE) {
1239 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1240 
1241 		if (vma_lock && vma_lock->vma != vma)
1242 			vma->vm_private_data = NULL;
1243 	} else
1244 		vma->vm_private_data = NULL;
1245 }
1246 
1247 /*
1248  * Reset and decrement one ref on hugepage private reservation.
1249  * Called with mm->mmap_lock writer semaphore held.
1250  * This function should be only used by mremap and operate on
1251  * same sized vma. It should never come here with last ref on the
1252  * reservation.
1253  */
1254 void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
1255 {
1256 	/*
1257 	 * Clear the old hugetlb private page reservation.
1258 	 * It has already been transferred to new_vma.
1259 	 *
1260 	 * During a mremap() operation of a hugetlb vma we call move_vma()
1261 	 * which copies vma into new_vma and unmaps vma. After the copy
1262 	 * operation both new_vma and vma share a reference to the resv_map
1263 	 * struct, and at that point vma is about to be unmapped. We don't
1264 	 * want to return the reservation to the pool at unmap of vma because
1265 	 * the reservation still lives on in new_vma, so simply decrement the
1266 	 * ref here and remove the resv_map reference from this vma.
1267 	 */
1268 	struct resv_map *reservations = vma_resv_map(vma);
1269 
1270 	if (reservations && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
1271 		resv_map_put_hugetlb_cgroup_uncharge_info(reservations);
1272 		kref_put(&reservations->refs, resv_map_release);
1273 	}
1274 
1275 	hugetlb_dup_vma_private(vma);
1276 }
1277 
1278 static void enqueue_hugetlb_folio(struct hstate *h, struct folio *folio)
1279 {
1280 	int nid = folio_nid(folio);
1281 
1282 	lockdep_assert_held(&hugetlb_lock);
1283 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1284 
1285 	list_move(&folio->lru, &h->hugepage_freelists[nid]);
1286 	h->free_huge_pages++;
1287 	h->free_huge_pages_node[nid]++;
1288 	folio_set_hugetlb_freed(folio);
1289 }
1290 
1291 static struct folio *dequeue_hugetlb_folio_node_exact(struct hstate *h,
1292 								int nid)
1293 {
1294 	struct folio *folio;
1295 	bool pin = !!(current->flags & PF_MEMALLOC_PIN);
1296 
1297 	lockdep_assert_held(&hugetlb_lock);
1298 	list_for_each_entry(folio, &h->hugepage_freelists[nid], lru) {
1299 		if (pin && !folio_is_longterm_pinnable(folio))
1300 			continue;
1301 
1302 		if (folio_test_hwpoison(folio))
1303 			continue;
1304 
1305 		if (is_migrate_isolate_page(&folio->page))
1306 			continue;
1307 
1308 		list_move(&folio->lru, &h->hugepage_activelist);
1309 		folio_ref_unfreeze(folio, 1);
1310 		folio_clear_hugetlb_freed(folio);
1311 		h->free_huge_pages--;
1312 		h->free_huge_pages_node[nid]--;
1313 		return folio;
1314 	}
1315 
1316 	return NULL;
1317 }
1318 
1319 static struct folio *dequeue_hugetlb_folio_nodemask(struct hstate *h, gfp_t gfp_mask,
1320 							int nid, nodemask_t *nmask)
1321 {
1322 	unsigned int cpuset_mems_cookie;
1323 	struct zonelist *zonelist;
1324 	struct zone *zone;
1325 	struct zoneref *z;
1326 	int node = NUMA_NO_NODE;
1327 
1328 	/* 'nid' should not be NUMA_NO_NODE. Try to catch any misuse of it and rectifiy. */
1329 	if (nid == NUMA_NO_NODE)
1330 		nid = numa_node_id();
1331 
1332 	zonelist = node_zonelist(nid, gfp_mask);
1333 
1334 retry_cpuset:
1335 	cpuset_mems_cookie = read_mems_allowed_begin();
1336 	for_each_zone_zonelist_nodemask(zone, z, zonelist, gfp_zone(gfp_mask), nmask) {
1337 		struct folio *folio;
1338 
1339 		if (!cpuset_zone_allowed(zone, gfp_mask))
1340 			continue;
1341 		/*
1342 		 * no need to ask again on the same node. Pool is node rather than
1343 		 * zone aware
1344 		 */
1345 		if (zone_to_nid(zone) == node)
1346 			continue;
1347 		node = zone_to_nid(zone);
1348 
1349 		folio = dequeue_hugetlb_folio_node_exact(h, node);
1350 		if (folio)
1351 			return folio;
1352 	}
1353 	if (unlikely(read_mems_allowed_retry(cpuset_mems_cookie)))
1354 		goto retry_cpuset;
1355 
1356 	return NULL;
1357 }
1358 
1359 static unsigned long available_huge_pages(struct hstate *h)
1360 {
1361 	return h->free_huge_pages - h->resv_huge_pages;
1362 }
1363 
1364 static struct folio *dequeue_hugetlb_folio_vma(struct hstate *h,
1365 				struct vm_area_struct *vma,
1366 				unsigned long address, long gbl_chg)
1367 {
1368 	struct folio *folio = NULL;
1369 	struct mempolicy *mpol;
1370 	gfp_t gfp_mask;
1371 	nodemask_t *nodemask;
1372 	int nid;
1373 
1374 	/*
1375 	 * gbl_chg==1 means the allocation requires a new page that was not
1376 	 * reserved before.  Making sure there's at least one free page.
1377 	 */
1378 	if (gbl_chg && !available_huge_pages(h))
1379 		goto err;
1380 
1381 	gfp_mask = htlb_alloc_mask(h);
1382 	nid = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
1383 
1384 	if (mpol_is_preferred_many(mpol)) {
1385 		folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1386 							nid, nodemask);
1387 
1388 		/* Fallback to all nodes if page==NULL */
1389 		nodemask = NULL;
1390 	}
1391 
1392 	if (!folio)
1393 		folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
1394 							nid, nodemask);
1395 
1396 	mpol_cond_put(mpol);
1397 	return folio;
1398 
1399 err:
1400 	return NULL;
1401 }
1402 
1403 /*
1404  * common helper functions for hstate_next_node_to_{alloc|free}.
1405  * We may have allocated or freed a huge page based on a different
1406  * nodes_allowed previously, so h->next_node_to_{alloc|free} might
1407  * be outside of *nodes_allowed.  Ensure that we use an allowed
1408  * node for alloc or free.
1409  */
1410 static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
1411 {
1412 	nid = next_node_in(nid, *nodes_allowed);
1413 	VM_BUG_ON(nid >= MAX_NUMNODES);
1414 
1415 	return nid;
1416 }
1417 
1418 static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
1419 {
1420 	if (!node_isset(nid, *nodes_allowed))
1421 		nid = next_node_allowed(nid, nodes_allowed);
1422 	return nid;
1423 }
1424 
1425 /*
1426  * returns the previously saved node ["this node"] from which to
1427  * allocate a persistent huge page for the pool and advance the
1428  * next node from which to allocate, handling wrap at end of node
1429  * mask.
1430  */
1431 static int hstate_next_node_to_alloc(int *next_node,
1432 					nodemask_t *nodes_allowed)
1433 {
1434 	int nid;
1435 
1436 	VM_BUG_ON(!nodes_allowed);
1437 
1438 	nid = get_valid_node_allowed(*next_node, nodes_allowed);
1439 	*next_node = next_node_allowed(nid, nodes_allowed);
1440 
1441 	return nid;
1442 }
1443 
1444 /*
1445  * helper for remove_pool_hugetlb_folio() - return the previously saved
1446  * node ["this node"] from which to free a huge page.  Advance the
1447  * next node id whether or not we find a free huge page to free so
1448  * that the next attempt to free addresses the next node.
1449  */
1450 static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
1451 {
1452 	int nid;
1453 
1454 	VM_BUG_ON(!nodes_allowed);
1455 
1456 	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
1457 	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
1458 
1459 	return nid;
1460 }
1461 
1462 #define for_each_node_mask_to_alloc(next_node, nr_nodes, node, mask)		\
1463 	for (nr_nodes = nodes_weight(*mask);				\
1464 		nr_nodes > 0 &&						\
1465 		((node = hstate_next_node_to_alloc(next_node, mask)) || 1);	\
1466 		nr_nodes--)
1467 
1468 #define for_each_node_mask_to_free(hs, nr_nodes, node, mask)		\
1469 	for (nr_nodes = nodes_weight(*mask);				\
1470 		nr_nodes > 0 &&						\
1471 		((node = hstate_next_node_to_free(hs, mask)) || 1);	\
1472 		nr_nodes--)
1473 
1474 #ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
1475 #ifdef CONFIG_CONTIG_ALLOC
1476 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1477 		int nid, nodemask_t *nodemask)
1478 {
1479 	struct folio *folio;
1480 	int order = huge_page_order(h);
1481 	bool retried = false;
1482 
1483 	if (nid == NUMA_NO_NODE)
1484 		nid = numa_mem_id();
1485 retry:
1486 	folio = hugetlb_cma_alloc_folio(h, gfp_mask, nid, nodemask);
1487 	if (!folio) {
1488 		if (hugetlb_cma_exclusive_alloc())
1489 			return NULL;
1490 
1491 		folio = folio_alloc_gigantic(order, gfp_mask, nid, nodemask);
1492 		if (!folio)
1493 			return NULL;
1494 	}
1495 
1496 	if (folio_ref_freeze(folio, 1))
1497 		return folio;
1498 
1499 	pr_warn("HugeTLB: unexpected refcount on PFN %lu\n", folio_pfn(folio));
1500 	hugetlb_free_folio(folio);
1501 	if (!retried) {
1502 		retried = true;
1503 		goto retry;
1504 	}
1505 	return NULL;
1506 }
1507 
1508 #else /* !CONFIG_CONTIG_ALLOC */
1509 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1510 					int nid, nodemask_t *nodemask)
1511 {
1512 	return NULL;
1513 }
1514 #endif /* CONFIG_CONTIG_ALLOC */
1515 
1516 #else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE */
1517 static struct folio *alloc_gigantic_folio(struct hstate *h, gfp_t gfp_mask,
1518 					int nid, nodemask_t *nodemask)
1519 {
1520 	return NULL;
1521 }
1522 #endif
1523 
1524 /*
1525  * Remove hugetlb folio from lists.
1526  * If vmemmap exists for the folio, clear the hugetlb flag so that the
1527  * folio appears as just a compound page.  Otherwise, wait until after
1528  * allocating vmemmap to clear the flag.
1529  *
1530  * Must be called with hugetlb lock held.
1531  */
1532 static void remove_hugetlb_folio(struct hstate *h, struct folio *folio,
1533 							bool adjust_surplus)
1534 {
1535 	int nid = folio_nid(folio);
1536 
1537 	VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio(folio), folio);
1538 	VM_BUG_ON_FOLIO(hugetlb_cgroup_from_folio_rsvd(folio), folio);
1539 
1540 	lockdep_assert_held(&hugetlb_lock);
1541 	if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1542 		return;
1543 
1544 	list_del(&folio->lru);
1545 
1546 	if (folio_test_hugetlb_freed(folio)) {
1547 		folio_clear_hugetlb_freed(folio);
1548 		h->free_huge_pages--;
1549 		h->free_huge_pages_node[nid]--;
1550 	}
1551 	if (adjust_surplus) {
1552 		h->surplus_huge_pages--;
1553 		h->surplus_huge_pages_node[nid]--;
1554 	}
1555 
1556 	/*
1557 	 * We can only clear the hugetlb flag after allocating vmemmap
1558 	 * pages.  Otherwise, someone (memory error handling) may try to write
1559 	 * to tail struct pages.
1560 	 */
1561 	if (!folio_test_hugetlb_vmemmap_optimized(folio))
1562 		__folio_clear_hugetlb(folio);
1563 
1564 	h->nr_huge_pages--;
1565 	h->nr_huge_pages_node[nid]--;
1566 }
1567 
1568 static void add_hugetlb_folio(struct hstate *h, struct folio *folio,
1569 			     bool adjust_surplus)
1570 {
1571 	int nid = folio_nid(folio);
1572 
1573 	VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio);
1574 
1575 	lockdep_assert_held(&hugetlb_lock);
1576 
1577 	INIT_LIST_HEAD(&folio->lru);
1578 	h->nr_huge_pages++;
1579 	h->nr_huge_pages_node[nid]++;
1580 
1581 	if (adjust_surplus) {
1582 		h->surplus_huge_pages++;
1583 		h->surplus_huge_pages_node[nid]++;
1584 	}
1585 
1586 	__folio_set_hugetlb(folio);
1587 	folio_change_private(folio, NULL);
1588 	/*
1589 	 * We have to set hugetlb_vmemmap_optimized again as above
1590 	 * folio_change_private(folio, NULL) cleared it.
1591 	 */
1592 	folio_set_hugetlb_vmemmap_optimized(folio);
1593 
1594 	arch_clear_hugetlb_flags(folio);
1595 	enqueue_hugetlb_folio(h, folio);
1596 }
1597 
1598 static void __update_and_free_hugetlb_folio(struct hstate *h,
1599 						struct folio *folio)
1600 {
1601 	bool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio);
1602 
1603 	if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
1604 		return;
1605 
1606 	/*
1607 	 * If we don't know which subpages are hwpoisoned, we can't free
1608 	 * the hugepage, so it's leaked intentionally.
1609 	 */
1610 	if (folio_test_hugetlb_raw_hwp_unreliable(folio))
1611 		return;
1612 
1613 	/*
1614 	 * If folio is not vmemmap optimized (!clear_flag), then the folio
1615 	 * is no longer identified as a hugetlb page.  hugetlb_vmemmap_restore_folio
1616 	 * can only be passed hugetlb pages and will BUG otherwise.
1617 	 */
1618 	if (clear_flag && hugetlb_vmemmap_restore_folio(h, folio)) {
1619 		spin_lock_irq(&hugetlb_lock);
1620 		/*
1621 		 * If we cannot allocate vmemmap pages, just refuse to free the
1622 		 * page and put the page back on the hugetlb free list and treat
1623 		 * as a surplus page.
1624 		 */
1625 		add_hugetlb_folio(h, folio, true);
1626 		spin_unlock_irq(&hugetlb_lock);
1627 		return;
1628 	}
1629 
1630 	/*
1631 	 * If vmemmap pages were allocated above, then we need to clear the
1632 	 * hugetlb flag under the hugetlb lock.
1633 	 */
1634 	if (folio_test_hugetlb(folio)) {
1635 		spin_lock_irq(&hugetlb_lock);
1636 		__folio_clear_hugetlb(folio);
1637 		spin_unlock_irq(&hugetlb_lock);
1638 	}
1639 
1640 	/*
1641 	 * Move PageHWPoison flag from head page to the raw error pages,
1642 	 * which makes any healthy subpages reusable.
1643 	 */
1644 	if (unlikely(folio_test_hwpoison(folio)))
1645 		folio_clear_hugetlb_hwpoison(folio);
1646 
1647 	folio_ref_unfreeze(folio, 1);
1648 
1649 	hugetlb_free_folio(folio);
1650 }
1651 
1652 /*
1653  * As update_and_free_hugetlb_folio() can be called under any context, so we cannot
1654  * use GFP_KERNEL to allocate vmemmap pages. However, we can defer the
1655  * actual freeing in a workqueue to prevent from using GFP_ATOMIC to allocate
1656  * the vmemmap pages.
1657  *
1658  * free_hpage_workfn() locklessly retrieves the linked list of pages to be
1659  * freed and frees them one-by-one. As the page->mapping pointer is going
1660  * to be cleared in free_hpage_workfn() anyway, it is reused as the llist_node
1661  * structure of a lockless linked list of huge pages to be freed.
1662  */
1663 static LLIST_HEAD(hpage_freelist);
1664 
1665 static void free_hpage_workfn(struct work_struct *work)
1666 {
1667 	struct llist_node *node;
1668 
1669 	node = llist_del_all(&hpage_freelist);
1670 
1671 	while (node) {
1672 		struct folio *folio;
1673 		struct hstate *h;
1674 
1675 		folio = container_of((struct address_space **)node,
1676 				     struct folio, mapping);
1677 		node = node->next;
1678 		folio->mapping = NULL;
1679 		/*
1680 		 * The VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio) in
1681 		 * folio_hstate() is going to trigger because a previous call to
1682 		 * remove_hugetlb_folio() will clear the hugetlb bit, so do
1683 		 * not use folio_hstate() directly.
1684 		 */
1685 		h = size_to_hstate(folio_size(folio));
1686 
1687 		__update_and_free_hugetlb_folio(h, folio);
1688 
1689 		cond_resched();
1690 	}
1691 }
1692 static DECLARE_WORK(free_hpage_work, free_hpage_workfn);
1693 
1694 static inline void flush_free_hpage_work(struct hstate *h)
1695 {
1696 	if (hugetlb_vmemmap_optimizable(h))
1697 		flush_work(&free_hpage_work);
1698 }
1699 
1700 static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio,
1701 				 bool atomic)
1702 {
1703 	if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) {
1704 		__update_and_free_hugetlb_folio(h, folio);
1705 		return;
1706 	}
1707 
1708 	/*
1709 	 * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap pages.
1710 	 *
1711 	 * Only call schedule_work() if hpage_freelist is previously
1712 	 * empty. Otherwise, schedule_work() had been called but the workfn
1713 	 * hasn't retrieved the list yet.
1714 	 */
1715 	if (llist_add((struct llist_node *)&folio->mapping, &hpage_freelist))
1716 		schedule_work(&free_hpage_work);
1717 }
1718 
1719 static void bulk_vmemmap_restore_error(struct hstate *h,
1720 					struct list_head *folio_list,
1721 					struct list_head *non_hvo_folios)
1722 {
1723 	struct folio *folio, *t_folio;
1724 
1725 	if (!list_empty(non_hvo_folios)) {
1726 		/*
1727 		 * Free any restored hugetlb pages so that restore of the
1728 		 * entire list can be retried.
1729 		 * The idea is that in the common case of ENOMEM errors freeing
1730 		 * hugetlb pages with vmemmap we will free up memory so that we
1731 		 * can allocate vmemmap for more hugetlb pages.
1732 		 */
1733 		list_for_each_entry_safe(folio, t_folio, non_hvo_folios, lru) {
1734 			list_del(&folio->lru);
1735 			spin_lock_irq(&hugetlb_lock);
1736 			__folio_clear_hugetlb(folio);
1737 			spin_unlock_irq(&hugetlb_lock);
1738 			update_and_free_hugetlb_folio(h, folio, false);
1739 			cond_resched();
1740 		}
1741 	} else {
1742 		/*
1743 		 * In the case where there are no folios which can be
1744 		 * immediately freed, we loop through the list trying to restore
1745 		 * vmemmap individually in the hope that someone elsewhere may
1746 		 * have done something to cause success (such as freeing some
1747 		 * memory).  If unable to restore a hugetlb page, the hugetlb
1748 		 * page is made a surplus page and removed from the list.
1749 		 * If are able to restore vmemmap and free one hugetlb page, we
1750 		 * quit processing the list to retry the bulk operation.
1751 		 */
1752 		list_for_each_entry_safe(folio, t_folio, folio_list, lru)
1753 			if (hugetlb_vmemmap_restore_folio(h, folio)) {
1754 				list_del(&folio->lru);
1755 				spin_lock_irq(&hugetlb_lock);
1756 				add_hugetlb_folio(h, folio, true);
1757 				spin_unlock_irq(&hugetlb_lock);
1758 			} else {
1759 				list_del(&folio->lru);
1760 				spin_lock_irq(&hugetlb_lock);
1761 				__folio_clear_hugetlb(folio);
1762 				spin_unlock_irq(&hugetlb_lock);
1763 				update_and_free_hugetlb_folio(h, folio, false);
1764 				cond_resched();
1765 				break;
1766 			}
1767 	}
1768 }
1769 
1770 static void update_and_free_pages_bulk(struct hstate *h,
1771 						struct list_head *folio_list)
1772 {
1773 	long ret;
1774 	struct folio *folio, *t_folio;
1775 	LIST_HEAD(non_hvo_folios);
1776 
1777 	/*
1778 	 * First allocate required vmemmmap (if necessary) for all folios.
1779 	 * Carefully handle errors and free up any available hugetlb pages
1780 	 * in an effort to make forward progress.
1781 	 */
1782 retry:
1783 	ret = hugetlb_vmemmap_restore_folios(h, folio_list, &non_hvo_folios);
1784 	if (ret < 0) {
1785 		bulk_vmemmap_restore_error(h, folio_list, &non_hvo_folios);
1786 		goto retry;
1787 	}
1788 
1789 	/*
1790 	 * At this point, list should be empty, ret should be >= 0 and there
1791 	 * should only be pages on the non_hvo_folios list.
1792 	 * Do note that the non_hvo_folios list could be empty.
1793 	 * Without HVO enabled, ret will be 0 and there is no need to call
1794 	 * __folio_clear_hugetlb as this was done previously.
1795 	 */
1796 	VM_WARN_ON(!list_empty(folio_list));
1797 	VM_WARN_ON(ret < 0);
1798 	if (!list_empty(&non_hvo_folios) && ret) {
1799 		spin_lock_irq(&hugetlb_lock);
1800 		list_for_each_entry(folio, &non_hvo_folios, lru)
1801 			__folio_clear_hugetlb(folio);
1802 		spin_unlock_irq(&hugetlb_lock);
1803 	}
1804 
1805 	list_for_each_entry_safe(folio, t_folio, &non_hvo_folios, lru) {
1806 		update_and_free_hugetlb_folio(h, folio, false);
1807 		cond_resched();
1808 	}
1809 }
1810 
1811 struct hstate *size_to_hstate(unsigned long size)
1812 {
1813 	struct hstate *h;
1814 
1815 	for_each_hstate(h) {
1816 		if (huge_page_size(h) == size)
1817 			return h;
1818 	}
1819 	return NULL;
1820 }
1821 
1822 void free_huge_folio(struct folio *folio)
1823 {
1824 	/*
1825 	 * Can't pass hstate in here because it is called from the
1826 	 * generic mm code.
1827 	 */
1828 	struct hstate *h = folio_hstate(folio);
1829 	int nid = folio_nid(folio);
1830 	struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
1831 	bool restore_reserve;
1832 	unsigned long flags;
1833 
1834 	VM_BUG_ON_FOLIO(folio_ref_count(folio), folio);
1835 	VM_BUG_ON_FOLIO(folio_mapcount(folio), folio);
1836 
1837 	hugetlb_set_folio_subpool(folio, NULL);
1838 	if (folio_test_anon(folio))
1839 		__ClearPageAnonExclusive(&folio->page);
1840 	folio->mapping = NULL;
1841 	restore_reserve = folio_test_hugetlb_restore_reserve(folio);
1842 	folio_clear_hugetlb_restore_reserve(folio);
1843 
1844 	/*
1845 	 * If HPageRestoreReserve was set on page, page allocation consumed a
1846 	 * reservation.  If the page was associated with a subpool, there
1847 	 * would have been a page reserved in the subpool before allocation
1848 	 * via hugepage_subpool_get_pages().  Since we are 'restoring' the
1849 	 * reservation, do not call hugepage_subpool_put_pages() as this will
1850 	 * remove the reserved page from the subpool.
1851 	 */
1852 	if (!restore_reserve) {
1853 		/*
1854 		 * A return code of zero implies that the subpool will be
1855 		 * under its minimum size if the reservation is not restored
1856 		 * after page is free.  Therefore, force restore_reserve
1857 		 * operation.
1858 		 */
1859 		if (hugepage_subpool_put_pages(spool, 1) == 0)
1860 			restore_reserve = true;
1861 	}
1862 
1863 	spin_lock_irqsave(&hugetlb_lock, flags);
1864 	folio_clear_hugetlb_migratable(folio);
1865 	hugetlb_cgroup_uncharge_folio(hstate_index(h),
1866 				     pages_per_huge_page(h), folio);
1867 	hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h),
1868 					  pages_per_huge_page(h), folio);
1869 	lruvec_stat_mod_folio(folio, NR_HUGETLB, -pages_per_huge_page(h));
1870 	mem_cgroup_uncharge(folio);
1871 	if (restore_reserve)
1872 		h->resv_huge_pages++;
1873 
1874 	if (folio_test_hugetlb_temporary(folio)) {
1875 		remove_hugetlb_folio(h, folio, false);
1876 		spin_unlock_irqrestore(&hugetlb_lock, flags);
1877 		update_and_free_hugetlb_folio(h, folio, true);
1878 	} else if (h->surplus_huge_pages_node[nid]) {
1879 		/* remove the page from active list */
1880 		remove_hugetlb_folio(h, folio, true);
1881 		spin_unlock_irqrestore(&hugetlb_lock, flags);
1882 		update_and_free_hugetlb_folio(h, folio, true);
1883 	} else {
1884 		arch_clear_hugetlb_flags(folio);
1885 		enqueue_hugetlb_folio(h, folio);
1886 		spin_unlock_irqrestore(&hugetlb_lock, flags);
1887 	}
1888 }
1889 
1890 /*
1891  * Must be called with the hugetlb lock held
1892  */
1893 static void __prep_account_new_huge_page(struct hstate *h, int nid)
1894 {
1895 	lockdep_assert_held(&hugetlb_lock);
1896 	h->nr_huge_pages++;
1897 	h->nr_huge_pages_node[nid]++;
1898 }
1899 
1900 static void init_new_hugetlb_folio(struct hstate *h, struct folio *folio)
1901 {
1902 	__folio_set_hugetlb(folio);
1903 	INIT_LIST_HEAD(&folio->lru);
1904 	hugetlb_set_folio_subpool(folio, NULL);
1905 	set_hugetlb_cgroup(folio, NULL);
1906 	set_hugetlb_cgroup_rsvd(folio, NULL);
1907 }
1908 
1909 static void __prep_new_hugetlb_folio(struct hstate *h, struct folio *folio)
1910 {
1911 	init_new_hugetlb_folio(h, folio);
1912 	hugetlb_vmemmap_optimize_folio(h, folio);
1913 }
1914 
1915 static void prep_new_hugetlb_folio(struct hstate *h, struct folio *folio, int nid)
1916 {
1917 	__prep_new_hugetlb_folio(h, folio);
1918 	spin_lock_irq(&hugetlb_lock);
1919 	__prep_account_new_huge_page(h, nid);
1920 	spin_unlock_irq(&hugetlb_lock);
1921 }
1922 
1923 /*
1924  * Find and lock address space (mapping) in write mode.
1925  *
1926  * Upon entry, the folio is locked which means that folio_mapping() is
1927  * stable.  Due to locking order, we can only trylock_write.  If we can
1928  * not get the lock, simply return NULL to caller.
1929  */
1930 struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
1931 {
1932 	struct address_space *mapping = folio_mapping(folio);
1933 
1934 	if (!mapping)
1935 		return mapping;
1936 
1937 	if (i_mmap_trylock_write(mapping))
1938 		return mapping;
1939 
1940 	return NULL;
1941 }
1942 
1943 static struct folio *alloc_buddy_hugetlb_folio(struct hstate *h,
1944 		gfp_t gfp_mask, int nid, nodemask_t *nmask,
1945 		nodemask_t *node_alloc_noretry)
1946 {
1947 	int order = huge_page_order(h);
1948 	struct folio *folio;
1949 	bool alloc_try_hard = true;
1950 
1951 	/*
1952 	 * By default we always try hard to allocate the folio with
1953 	 * __GFP_RETRY_MAYFAIL flag.  However, if we are allocating folios in
1954 	 * a loop (to adjust global huge page counts) and previous allocation
1955 	 * failed, do not continue to try hard on the same node.  Use the
1956 	 * node_alloc_noretry bitmap to manage this state information.
1957 	 */
1958 	if (node_alloc_noretry && node_isset(nid, *node_alloc_noretry))
1959 		alloc_try_hard = false;
1960 	if (alloc_try_hard)
1961 		gfp_mask |= __GFP_RETRY_MAYFAIL;
1962 	if (nid == NUMA_NO_NODE)
1963 		nid = numa_mem_id();
1964 
1965 	folio = (struct folio *)__alloc_frozen_pages(gfp_mask, order, nid, nmask);
1966 
1967 	/*
1968 	 * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
1969 	 * folio this indicates an overall state change.  Clear bit so
1970 	 * that we resume normal 'try hard' allocations.
1971 	 */
1972 	if (node_alloc_noretry && folio && !alloc_try_hard)
1973 		node_clear(nid, *node_alloc_noretry);
1974 
1975 	/*
1976 	 * If we tried hard to get a folio but failed, set bit so that
1977 	 * subsequent attempts will not try as hard until there is an
1978 	 * overall state change.
1979 	 */
1980 	if (node_alloc_noretry && !folio && alloc_try_hard)
1981 		node_set(nid, *node_alloc_noretry);
1982 
1983 	if (!folio) {
1984 		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
1985 		return NULL;
1986 	}
1987 
1988 	__count_vm_event(HTLB_BUDDY_PGALLOC);
1989 	return folio;
1990 }
1991 
1992 static struct folio *only_alloc_fresh_hugetlb_folio(struct hstate *h,
1993 		gfp_t gfp_mask, int nid, nodemask_t *nmask,
1994 		nodemask_t *node_alloc_noretry)
1995 {
1996 	struct folio *folio;
1997 
1998 	if (hstate_is_gigantic(h))
1999 		folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
2000 	else
2001 		folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, node_alloc_noretry);
2002 	if (folio)
2003 		init_new_hugetlb_folio(h, folio);
2004 	return folio;
2005 }
2006 
2007 /*
2008  * Common helper to allocate a fresh hugetlb page. All specific allocators
2009  * should use this function to get new hugetlb pages
2010  *
2011  * Note that returned page is 'frozen':  ref count of head page and all tail
2012  * pages is zero.
2013  */
2014 static struct folio *alloc_fresh_hugetlb_folio(struct hstate *h,
2015 		gfp_t gfp_mask, int nid, nodemask_t *nmask)
2016 {
2017 	struct folio *folio;
2018 
2019 	if (hstate_is_gigantic(h))
2020 		folio = alloc_gigantic_folio(h, gfp_mask, nid, nmask);
2021 	else
2022 		folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2023 	if (!folio)
2024 		return NULL;
2025 
2026 	prep_new_hugetlb_folio(h, folio, folio_nid(folio));
2027 	return folio;
2028 }
2029 
2030 static void prep_and_add_allocated_folios(struct hstate *h,
2031 					struct list_head *folio_list)
2032 {
2033 	unsigned long flags;
2034 	struct folio *folio, *tmp_f;
2035 
2036 	/* Send list for bulk vmemmap optimization processing */
2037 	hugetlb_vmemmap_optimize_folios(h, folio_list);
2038 
2039 	/* Add all new pool pages to free lists in one lock cycle */
2040 	spin_lock_irqsave(&hugetlb_lock, flags);
2041 	list_for_each_entry_safe(folio, tmp_f, folio_list, lru) {
2042 		__prep_account_new_huge_page(h, folio_nid(folio));
2043 		enqueue_hugetlb_folio(h, folio);
2044 	}
2045 	spin_unlock_irqrestore(&hugetlb_lock, flags);
2046 }
2047 
2048 /*
2049  * Allocates a fresh hugetlb page in a node interleaved manner.  The page
2050  * will later be added to the appropriate hugetlb pool.
2051  */
2052 static struct folio *alloc_pool_huge_folio(struct hstate *h,
2053 					nodemask_t *nodes_allowed,
2054 					nodemask_t *node_alloc_noretry,
2055 					int *next_node)
2056 {
2057 	gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2058 	int nr_nodes, node;
2059 
2060 	for_each_node_mask_to_alloc(next_node, nr_nodes, node, nodes_allowed) {
2061 		struct folio *folio;
2062 
2063 		folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, node,
2064 					nodes_allowed, node_alloc_noretry);
2065 		if (folio)
2066 			return folio;
2067 	}
2068 
2069 	return NULL;
2070 }
2071 
2072 /*
2073  * Remove huge page from pool from next node to free.  Attempt to keep
2074  * persistent huge pages more or less balanced over allowed nodes.
2075  * This routine only 'removes' the hugetlb page.  The caller must make
2076  * an additional call to free the page to low level allocators.
2077  * Called with hugetlb_lock locked.
2078  */
2079 static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
2080 		nodemask_t *nodes_allowed, bool acct_surplus)
2081 {
2082 	int nr_nodes, node;
2083 	struct folio *folio = NULL;
2084 
2085 	lockdep_assert_held(&hugetlb_lock);
2086 	for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
2087 		/*
2088 		 * If we're returning unused surplus pages, only examine
2089 		 * nodes with surplus pages.
2090 		 */
2091 		if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
2092 		    !list_empty(&h->hugepage_freelists[node])) {
2093 			folio = list_entry(h->hugepage_freelists[node].next,
2094 					  struct folio, lru);
2095 			remove_hugetlb_folio(h, folio, acct_surplus);
2096 			break;
2097 		}
2098 	}
2099 
2100 	return folio;
2101 }
2102 
2103 /*
2104  * Dissolve a given free hugetlb folio into free buddy pages. This function
2105  * does nothing for in-use hugetlb folios and non-hugetlb folios.
2106  * This function returns values like below:
2107  *
2108  *  -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
2109  *           when the system is under memory pressure and the feature of
2110  *           freeing unused vmemmap pages associated with each hugetlb page
2111  *           is enabled.
2112  *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
2113  *           (allocated or reserved.)
2114  *       0:  successfully dissolved free hugepages or the page is not a
2115  *           hugepage (considered as already dissolved)
2116  */
2117 int dissolve_free_hugetlb_folio(struct folio *folio)
2118 {
2119 	int rc = -EBUSY;
2120 
2121 retry:
2122 	/* Not to disrupt normal path by vainly holding hugetlb_lock */
2123 	if (!folio_test_hugetlb(folio))
2124 		return 0;
2125 
2126 	spin_lock_irq(&hugetlb_lock);
2127 	if (!folio_test_hugetlb(folio)) {
2128 		rc = 0;
2129 		goto out;
2130 	}
2131 
2132 	if (!folio_ref_count(folio)) {
2133 		struct hstate *h = folio_hstate(folio);
2134 		bool adjust_surplus = false;
2135 
2136 		if (!available_huge_pages(h))
2137 			goto out;
2138 
2139 		/*
2140 		 * We should make sure that the page is already on the free list
2141 		 * when it is dissolved.
2142 		 */
2143 		if (unlikely(!folio_test_hugetlb_freed(folio))) {
2144 			spin_unlock_irq(&hugetlb_lock);
2145 			cond_resched();
2146 
2147 			/*
2148 			 * Theoretically, we should return -EBUSY when we
2149 			 * encounter this race. In fact, we have a chance
2150 			 * to successfully dissolve the page if we do a
2151 			 * retry. Because the race window is quite small.
2152 			 * If we seize this opportunity, it is an optimization
2153 			 * for increasing the success rate of dissolving page.
2154 			 */
2155 			goto retry;
2156 		}
2157 
2158 		if (h->surplus_huge_pages_node[folio_nid(folio)])
2159 			adjust_surplus = true;
2160 		remove_hugetlb_folio(h, folio, adjust_surplus);
2161 		h->max_huge_pages--;
2162 		spin_unlock_irq(&hugetlb_lock);
2163 
2164 		/*
2165 		 * Normally update_and_free_hugtlb_folio will allocate required vmemmmap
2166 		 * before freeing the page.  update_and_free_hugtlb_folio will fail to
2167 		 * free the page if it can not allocate required vmemmap.  We
2168 		 * need to adjust max_huge_pages if the page is not freed.
2169 		 * Attempt to allocate vmemmmap here so that we can take
2170 		 * appropriate action on failure.
2171 		 *
2172 		 * The folio_test_hugetlb check here is because
2173 		 * remove_hugetlb_folio will clear hugetlb folio flag for
2174 		 * non-vmemmap optimized hugetlb folios.
2175 		 */
2176 		if (folio_test_hugetlb(folio)) {
2177 			rc = hugetlb_vmemmap_restore_folio(h, folio);
2178 			if (rc) {
2179 				spin_lock_irq(&hugetlb_lock);
2180 				add_hugetlb_folio(h, folio, adjust_surplus);
2181 				h->max_huge_pages++;
2182 				goto out;
2183 			}
2184 		} else
2185 			rc = 0;
2186 
2187 		update_and_free_hugetlb_folio(h, folio, false);
2188 		return rc;
2189 	}
2190 out:
2191 	spin_unlock_irq(&hugetlb_lock);
2192 	return rc;
2193 }
2194 
2195 /*
2196  * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
2197  * make specified memory blocks removable from the system.
2198  * Note that this will dissolve a free gigantic hugepage completely, if any
2199  * part of it lies within the given range.
2200  * Also note that if dissolve_free_hugetlb_folio() returns with an error, all
2201  * free hugetlb folios that were dissolved before that error are lost.
2202  */
2203 int dissolve_free_hugetlb_folios(unsigned long start_pfn, unsigned long end_pfn)
2204 {
2205 	unsigned long pfn;
2206 	struct folio *folio;
2207 	int rc = 0;
2208 	unsigned int order;
2209 	struct hstate *h;
2210 
2211 	if (!hugepages_supported())
2212 		return rc;
2213 
2214 	order = huge_page_order(&default_hstate);
2215 	for_each_hstate(h)
2216 		order = min(order, huge_page_order(h));
2217 
2218 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order) {
2219 		folio = pfn_folio(pfn);
2220 		rc = dissolve_free_hugetlb_folio(folio);
2221 		if (rc)
2222 			break;
2223 	}
2224 
2225 	return rc;
2226 }
2227 
2228 /*
2229  * Allocates a fresh surplus page from the page allocator.
2230  */
2231 static struct folio *alloc_surplus_hugetlb_folio(struct hstate *h,
2232 				gfp_t gfp_mask,	int nid, nodemask_t *nmask)
2233 {
2234 	struct folio *folio = NULL;
2235 
2236 	if (hstate_is_gigantic(h))
2237 		return NULL;
2238 
2239 	spin_lock_irq(&hugetlb_lock);
2240 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages)
2241 		goto out_unlock;
2242 	spin_unlock_irq(&hugetlb_lock);
2243 
2244 	folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask, NULL);
2245 	if (!folio)
2246 		return NULL;
2247 
2248 	hugetlb_vmemmap_optimize_folio(h, folio);
2249 
2250 	spin_lock_irq(&hugetlb_lock);
2251 	/*
2252 	 * nr_huge_pages needs to be adjusted within the same lock cycle
2253 	 * as surplus_pages, otherwise it might confuse
2254 	 * persistent_huge_pages() momentarily.
2255 	 */
2256 	__prep_account_new_huge_page(h, folio_nid(folio));
2257 
2258 	/*
2259 	 * We could have raced with the pool size change.
2260 	 * Double check that and simply deallocate the new page
2261 	 * if we would end up overcommiting the surpluses. Abuse
2262 	 * temporary page to workaround the nasty free_huge_folio
2263 	 * codeflow
2264 	 */
2265 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
2266 		folio_set_hugetlb_temporary(folio);
2267 		spin_unlock_irq(&hugetlb_lock);
2268 		free_huge_folio(folio);
2269 		return NULL;
2270 	}
2271 
2272 	h->surplus_huge_pages++;
2273 	h->surplus_huge_pages_node[folio_nid(folio)]++;
2274 
2275 out_unlock:
2276 	spin_unlock_irq(&hugetlb_lock);
2277 
2278 	return folio;
2279 }
2280 
2281 static struct folio *alloc_migrate_hugetlb_folio(struct hstate *h, gfp_t gfp_mask,
2282 				     int nid, nodemask_t *nmask)
2283 {
2284 	struct folio *folio;
2285 
2286 	if (hstate_is_gigantic(h))
2287 		return NULL;
2288 
2289 	folio = alloc_fresh_hugetlb_folio(h, gfp_mask, nid, nmask);
2290 	if (!folio)
2291 		return NULL;
2292 
2293 	/* fresh huge pages are frozen */
2294 	folio_ref_unfreeze(folio, 1);
2295 	/*
2296 	 * We do not account these pages as surplus because they are only
2297 	 * temporary and will be released properly on the last reference
2298 	 */
2299 	folio_set_hugetlb_temporary(folio);
2300 
2301 	return folio;
2302 }
2303 
2304 /*
2305  * Use the VMA's mpolicy to allocate a huge page from the buddy.
2306  */
2307 static
2308 struct folio *alloc_buddy_hugetlb_folio_with_mpol(struct hstate *h,
2309 		struct vm_area_struct *vma, unsigned long addr)
2310 {
2311 	struct folio *folio = NULL;
2312 	struct mempolicy *mpol;
2313 	gfp_t gfp_mask = htlb_alloc_mask(h);
2314 	int nid;
2315 	nodemask_t *nodemask;
2316 
2317 	nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask);
2318 	if (mpol_is_preferred_many(mpol)) {
2319 		gfp_t gfp = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL);
2320 
2321 		folio = alloc_surplus_hugetlb_folio(h, gfp, nid, nodemask);
2322 
2323 		/* Fallback to all nodes if page==NULL */
2324 		nodemask = NULL;
2325 	}
2326 
2327 	if (!folio)
2328 		folio = alloc_surplus_hugetlb_folio(h, gfp_mask, nid, nodemask);
2329 	mpol_cond_put(mpol);
2330 	return folio;
2331 }
2332 
2333 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
2334 		nodemask_t *nmask, gfp_t gfp_mask)
2335 {
2336 	struct folio *folio;
2337 
2338 	spin_lock_irq(&hugetlb_lock);
2339 	if (!h->resv_huge_pages) {
2340 		spin_unlock_irq(&hugetlb_lock);
2341 		return NULL;
2342 	}
2343 
2344 	folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask, preferred_nid,
2345 					       nmask);
2346 	if (folio)
2347 		h->resv_huge_pages--;
2348 
2349 	spin_unlock_irq(&hugetlb_lock);
2350 	return folio;
2351 }
2352 
2353 /* folio migration callback function */
2354 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
2355 		nodemask_t *nmask, gfp_t gfp_mask, bool allow_alloc_fallback)
2356 {
2357 	spin_lock_irq(&hugetlb_lock);
2358 	if (available_huge_pages(h)) {
2359 		struct folio *folio;
2360 
2361 		folio = dequeue_hugetlb_folio_nodemask(h, gfp_mask,
2362 						preferred_nid, nmask);
2363 		if (folio) {
2364 			spin_unlock_irq(&hugetlb_lock);
2365 			return folio;
2366 		}
2367 	}
2368 	spin_unlock_irq(&hugetlb_lock);
2369 
2370 	/* We cannot fallback to other nodes, as we could break the per-node pool. */
2371 	if (!allow_alloc_fallback)
2372 		gfp_mask |= __GFP_THISNODE;
2373 
2374 	return alloc_migrate_hugetlb_folio(h, gfp_mask, preferred_nid, nmask);
2375 }
2376 
2377 static nodemask_t *policy_mbind_nodemask(gfp_t gfp)
2378 {
2379 #ifdef CONFIG_NUMA
2380 	struct mempolicy *mpol = get_task_policy(current);
2381 
2382 	/*
2383 	 * Only enforce MPOL_BIND policy which overlaps with cpuset policy
2384 	 * (from policy_nodemask) specifically for hugetlb case
2385 	 */
2386 	if (mpol->mode == MPOL_BIND &&
2387 		(apply_policy_zone(mpol, gfp_zone(gfp)) &&
2388 		 cpuset_nodemask_valid_mems_allowed(&mpol->nodes)))
2389 		return &mpol->nodes;
2390 #endif
2391 	return NULL;
2392 }
2393 
2394 /*
2395  * Increase the hugetlb pool such that it can accommodate a reservation
2396  * of size 'delta'.
2397  */
2398 static int gather_surplus_pages(struct hstate *h, long delta)
2399 	__must_hold(&hugetlb_lock)
2400 {
2401 	LIST_HEAD(surplus_list);
2402 	struct folio *folio, *tmp;
2403 	int ret;
2404 	long i;
2405 	long needed, allocated;
2406 	bool alloc_ok = true;
2407 	nodemask_t *mbind_nodemask, alloc_nodemask;
2408 
2409 	mbind_nodemask = policy_mbind_nodemask(htlb_alloc_mask(h));
2410 	if (mbind_nodemask)
2411 		nodes_and(alloc_nodemask, *mbind_nodemask, cpuset_current_mems_allowed);
2412 	else
2413 		alloc_nodemask = cpuset_current_mems_allowed;
2414 
2415 	lockdep_assert_held(&hugetlb_lock);
2416 	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
2417 	if (needed <= 0) {
2418 		h->resv_huge_pages += delta;
2419 		return 0;
2420 	}
2421 
2422 	allocated = 0;
2423 
2424 	ret = -ENOMEM;
2425 retry:
2426 	spin_unlock_irq(&hugetlb_lock);
2427 	for (i = 0; i < needed; i++) {
2428 		folio = NULL;
2429 
2430 		/*
2431 		 * It is okay to use NUMA_NO_NODE because we use numa_mem_id()
2432 		 * down the road to pick the current node if that is the case.
2433 		 */
2434 		folio = alloc_surplus_hugetlb_folio(h, htlb_alloc_mask(h),
2435 						    NUMA_NO_NODE, &alloc_nodemask);
2436 		if (!folio) {
2437 			alloc_ok = false;
2438 			break;
2439 		}
2440 		list_add(&folio->lru, &surplus_list);
2441 		cond_resched();
2442 	}
2443 	allocated += i;
2444 
2445 	/*
2446 	 * After retaking hugetlb_lock, we need to recalculate 'needed'
2447 	 * because either resv_huge_pages or free_huge_pages may have changed.
2448 	 */
2449 	spin_lock_irq(&hugetlb_lock);
2450 	needed = (h->resv_huge_pages + delta) -
2451 			(h->free_huge_pages + allocated);
2452 	if (needed > 0) {
2453 		if (alloc_ok)
2454 			goto retry;
2455 		/*
2456 		 * We were not able to allocate enough pages to
2457 		 * satisfy the entire reservation so we free what
2458 		 * we've allocated so far.
2459 		 */
2460 		goto free;
2461 	}
2462 	/*
2463 	 * The surplus_list now contains _at_least_ the number of extra pages
2464 	 * needed to accommodate the reservation.  Add the appropriate number
2465 	 * of pages to the hugetlb pool and free the extras back to the buddy
2466 	 * allocator.  Commit the entire reservation here to prevent another
2467 	 * process from stealing the pages as they are added to the pool but
2468 	 * before they are reserved.
2469 	 */
2470 	needed += allocated;
2471 	h->resv_huge_pages += delta;
2472 	ret = 0;
2473 
2474 	/* Free the needed pages to the hugetlb pool */
2475 	list_for_each_entry_safe(folio, tmp, &surplus_list, lru) {
2476 		if ((--needed) < 0)
2477 			break;
2478 		/* Add the page to the hugetlb allocator */
2479 		enqueue_hugetlb_folio(h, folio);
2480 	}
2481 free:
2482 	spin_unlock_irq(&hugetlb_lock);
2483 
2484 	/*
2485 	 * Free unnecessary surplus pages to the buddy allocator.
2486 	 * Pages have no ref count, call free_huge_folio directly.
2487 	 */
2488 	list_for_each_entry_safe(folio, tmp, &surplus_list, lru)
2489 		free_huge_folio(folio);
2490 	spin_lock_irq(&hugetlb_lock);
2491 
2492 	return ret;
2493 }
2494 
2495 /*
2496  * This routine has two main purposes:
2497  * 1) Decrement the reservation count (resv_huge_pages) by the value passed
2498  *    in unused_resv_pages.  This corresponds to the prior adjustments made
2499  *    to the associated reservation map.
2500  * 2) Free any unused surplus pages that may have been allocated to satisfy
2501  *    the reservation.  As many as unused_resv_pages may be freed.
2502  */
2503 static void return_unused_surplus_pages(struct hstate *h,
2504 					unsigned long unused_resv_pages)
2505 {
2506 	unsigned long nr_pages;
2507 	LIST_HEAD(page_list);
2508 
2509 	lockdep_assert_held(&hugetlb_lock);
2510 	/* Uncommit the reservation */
2511 	h->resv_huge_pages -= unused_resv_pages;
2512 
2513 	if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
2514 		goto out;
2515 
2516 	/*
2517 	 * Part (or even all) of the reservation could have been backed
2518 	 * by pre-allocated pages. Only free surplus pages.
2519 	 */
2520 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
2521 
2522 	/*
2523 	 * We want to release as many surplus pages as possible, spread
2524 	 * evenly across all nodes with memory. Iterate across these nodes
2525 	 * until we can no longer free unreserved surplus pages. This occurs
2526 	 * when the nodes with surplus pages have no free pages.
2527 	 * remove_pool_hugetlb_folio() will balance the freed pages across the
2528 	 * on-line nodes with memory and will handle the hstate accounting.
2529 	 */
2530 	while (nr_pages--) {
2531 		struct folio *folio;
2532 
2533 		folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY], 1);
2534 		if (!folio)
2535 			goto out;
2536 
2537 		list_add(&folio->lru, &page_list);
2538 	}
2539 
2540 out:
2541 	spin_unlock_irq(&hugetlb_lock);
2542 	update_and_free_pages_bulk(h, &page_list);
2543 	spin_lock_irq(&hugetlb_lock);
2544 }
2545 
2546 
2547 /*
2548  * vma_needs_reservation, vma_commit_reservation and vma_end_reservation
2549  * are used by the huge page allocation routines to manage reservations.
2550  *
2551  * vma_needs_reservation is called to determine if the huge page at addr
2552  * within the vma has an associated reservation.  If a reservation is
2553  * needed, the value 1 is returned.  The caller is then responsible for
2554  * managing the global reservation and subpool usage counts.  After
2555  * the huge page has been allocated, vma_commit_reservation is called
2556  * to add the page to the reservation map.  If the page allocation fails,
2557  * the reservation must be ended instead of committed.  vma_end_reservation
2558  * is called in such cases.
2559  *
2560  * In the normal case, vma_commit_reservation returns the same value
2561  * as the preceding vma_needs_reservation call.  The only time this
2562  * is not the case is if a reserve map was changed between calls.  It
2563  * is the responsibility of the caller to notice the difference and
2564  * take appropriate action.
2565  *
2566  * vma_add_reservation is used in error paths where a reservation must
2567  * be restored when a newly allocated huge page must be freed.  It is
2568  * to be called after calling vma_needs_reservation to determine if a
2569  * reservation exists.
2570  *
2571  * vma_del_reservation is used in error paths where an entry in the reserve
2572  * map was created during huge page allocation and must be removed.  It is to
2573  * be called after calling vma_needs_reservation to determine if a reservation
2574  * exists.
2575  */
2576 enum vma_resv_mode {
2577 	VMA_NEEDS_RESV,
2578 	VMA_COMMIT_RESV,
2579 	VMA_END_RESV,
2580 	VMA_ADD_RESV,
2581 	VMA_DEL_RESV,
2582 };
2583 static long __vma_reservation_common(struct hstate *h,
2584 				struct vm_area_struct *vma, unsigned long addr,
2585 				enum vma_resv_mode mode)
2586 {
2587 	struct resv_map *resv;
2588 	pgoff_t idx;
2589 	long ret;
2590 	long dummy_out_regions_needed;
2591 
2592 	resv = vma_resv_map(vma);
2593 	if (!resv)
2594 		return 1;
2595 
2596 	idx = vma_hugecache_offset(h, vma, addr);
2597 	switch (mode) {
2598 	case VMA_NEEDS_RESV:
2599 		ret = region_chg(resv, idx, idx + 1, &dummy_out_regions_needed);
2600 		/* We assume that vma_reservation_* routines always operate on
2601 		 * 1 page, and that adding to resv map a 1 page entry can only
2602 		 * ever require 1 region.
2603 		 */
2604 		VM_BUG_ON(dummy_out_regions_needed != 1);
2605 		break;
2606 	case VMA_COMMIT_RESV:
2607 		ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2608 		/* region_add calls of range 1 should never fail. */
2609 		VM_BUG_ON(ret < 0);
2610 		break;
2611 	case VMA_END_RESV:
2612 		region_abort(resv, idx, idx + 1, 1);
2613 		ret = 0;
2614 		break;
2615 	case VMA_ADD_RESV:
2616 		if (vma->vm_flags & VM_MAYSHARE) {
2617 			ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2618 			/* region_add calls of range 1 should never fail. */
2619 			VM_BUG_ON(ret < 0);
2620 		} else {
2621 			region_abort(resv, idx, idx + 1, 1);
2622 			ret = region_del(resv, idx, idx + 1);
2623 		}
2624 		break;
2625 	case VMA_DEL_RESV:
2626 		if (vma->vm_flags & VM_MAYSHARE) {
2627 			region_abort(resv, idx, idx + 1, 1);
2628 			ret = region_del(resv, idx, idx + 1);
2629 		} else {
2630 			ret = region_add(resv, idx, idx + 1, 1, NULL, NULL);
2631 			/* region_add calls of range 1 should never fail. */
2632 			VM_BUG_ON(ret < 0);
2633 		}
2634 		break;
2635 	default:
2636 		BUG();
2637 	}
2638 
2639 	if (vma->vm_flags & VM_MAYSHARE || mode == VMA_DEL_RESV)
2640 		return ret;
2641 	/*
2642 	 * We know private mapping must have HPAGE_RESV_OWNER set.
2643 	 *
2644 	 * In most cases, reserves always exist for private mappings.
2645 	 * However, a file associated with mapping could have been
2646 	 * hole punched or truncated after reserves were consumed.
2647 	 * As subsequent fault on such a range will not use reserves.
2648 	 * Subtle - The reserve map for private mappings has the
2649 	 * opposite meaning than that of shared mappings.  If NO
2650 	 * entry is in the reserve map, it means a reservation exists.
2651 	 * If an entry exists in the reserve map, it means the
2652 	 * reservation has already been consumed.  As a result, the
2653 	 * return value of this routine is the opposite of the
2654 	 * value returned from reserve map manipulation routines above.
2655 	 */
2656 	if (ret > 0)
2657 		return 0;
2658 	if (ret == 0)
2659 		return 1;
2660 	return ret;
2661 }
2662 
2663 static long vma_needs_reservation(struct hstate *h,
2664 			struct vm_area_struct *vma, unsigned long addr)
2665 {
2666 	return __vma_reservation_common(h, vma, addr, VMA_NEEDS_RESV);
2667 }
2668 
2669 static long vma_commit_reservation(struct hstate *h,
2670 			struct vm_area_struct *vma, unsigned long addr)
2671 {
2672 	return __vma_reservation_common(h, vma, addr, VMA_COMMIT_RESV);
2673 }
2674 
2675 static void vma_end_reservation(struct hstate *h,
2676 			struct vm_area_struct *vma, unsigned long addr)
2677 {
2678 	(void)__vma_reservation_common(h, vma, addr, VMA_END_RESV);
2679 }
2680 
2681 static long vma_add_reservation(struct hstate *h,
2682 			struct vm_area_struct *vma, unsigned long addr)
2683 {
2684 	return __vma_reservation_common(h, vma, addr, VMA_ADD_RESV);
2685 }
2686 
2687 static long vma_del_reservation(struct hstate *h,
2688 			struct vm_area_struct *vma, unsigned long addr)
2689 {
2690 	return __vma_reservation_common(h, vma, addr, VMA_DEL_RESV);
2691 }
2692 
2693 /*
2694  * This routine is called to restore reservation information on error paths.
2695  * It should ONLY be called for folios allocated via alloc_hugetlb_folio(),
2696  * and the hugetlb mutex should remain held when calling this routine.
2697  *
2698  * It handles two specific cases:
2699  * 1) A reservation was in place and the folio consumed the reservation.
2700  *    hugetlb_restore_reserve is set in the folio.
2701  * 2) No reservation was in place for the page, so hugetlb_restore_reserve is
2702  *    not set.  However, alloc_hugetlb_folio always updates the reserve map.
2703  *
2704  * In case 1, free_huge_folio later in the error path will increment the
2705  * global reserve count.  But, free_huge_folio does not have enough context
2706  * to adjust the reservation map.  This case deals primarily with private
2707  * mappings.  Adjust the reserve map here to be consistent with global
2708  * reserve count adjustments to be made by free_huge_folio.  Make sure the
2709  * reserve map indicates there is a reservation present.
2710  *
2711  * In case 2, simply undo reserve map modifications done by alloc_hugetlb_folio.
2712  */
2713 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
2714 			unsigned long address, struct folio *folio)
2715 {
2716 	long rc = vma_needs_reservation(h, vma, address);
2717 
2718 	if (folio_test_hugetlb_restore_reserve(folio)) {
2719 		if (unlikely(rc < 0))
2720 			/*
2721 			 * Rare out of memory condition in reserve map
2722 			 * manipulation.  Clear hugetlb_restore_reserve so
2723 			 * that global reserve count will not be incremented
2724 			 * by free_huge_folio.  This will make it appear
2725 			 * as though the reservation for this folio was
2726 			 * consumed.  This may prevent the task from
2727 			 * faulting in the folio at a later time.  This
2728 			 * is better than inconsistent global huge page
2729 			 * accounting of reserve counts.
2730 			 */
2731 			folio_clear_hugetlb_restore_reserve(folio);
2732 		else if (rc)
2733 			(void)vma_add_reservation(h, vma, address);
2734 		else
2735 			vma_end_reservation(h, vma, address);
2736 	} else {
2737 		if (!rc) {
2738 			/*
2739 			 * This indicates there is an entry in the reserve map
2740 			 * not added by alloc_hugetlb_folio.  We know it was added
2741 			 * before the alloc_hugetlb_folio call, otherwise
2742 			 * hugetlb_restore_reserve would be set on the folio.
2743 			 * Remove the entry so that a subsequent allocation
2744 			 * does not consume a reservation.
2745 			 */
2746 			rc = vma_del_reservation(h, vma, address);
2747 			if (rc < 0)
2748 				/*
2749 				 * VERY rare out of memory condition.  Since
2750 				 * we can not delete the entry, set
2751 				 * hugetlb_restore_reserve so that the reserve
2752 				 * count will be incremented when the folio
2753 				 * is freed.  This reserve will be consumed
2754 				 * on a subsequent allocation.
2755 				 */
2756 				folio_set_hugetlb_restore_reserve(folio);
2757 		} else if (rc < 0) {
2758 			/*
2759 			 * Rare out of memory condition from
2760 			 * vma_needs_reservation call.  Memory allocation is
2761 			 * only attempted if a new entry is needed.  Therefore,
2762 			 * this implies there is not an entry in the
2763 			 * reserve map.
2764 			 *
2765 			 * For shared mappings, no entry in the map indicates
2766 			 * no reservation.  We are done.
2767 			 */
2768 			if (!(vma->vm_flags & VM_MAYSHARE))
2769 				/*
2770 				 * For private mappings, no entry indicates
2771 				 * a reservation is present.  Since we can
2772 				 * not add an entry, set hugetlb_restore_reserve
2773 				 * on the folio so reserve count will be
2774 				 * incremented when freed.  This reserve will
2775 				 * be consumed on a subsequent allocation.
2776 				 */
2777 				folio_set_hugetlb_restore_reserve(folio);
2778 		} else
2779 			/*
2780 			 * No reservation present, do nothing
2781 			 */
2782 			 vma_end_reservation(h, vma, address);
2783 	}
2784 }
2785 
2786 /*
2787  * alloc_and_dissolve_hugetlb_folio - Allocate a new folio and dissolve
2788  * the old one
2789  * @old_folio: Old folio to dissolve
2790  * @list: List to isolate the page in case we need to
2791  * Returns 0 on success, otherwise negated error.
2792  */
2793 static int alloc_and_dissolve_hugetlb_folio(struct folio *old_folio,
2794 			struct list_head *list)
2795 {
2796 	gfp_t gfp_mask;
2797 	struct hstate *h;
2798 	int nid = folio_nid(old_folio);
2799 	struct folio *new_folio = NULL;
2800 	int ret = 0;
2801 
2802 retry:
2803 	/*
2804 	 * The old_folio might have been dissolved from under our feet, so make sure
2805 	 * to carefully check the state under the lock.
2806 	 */
2807 	spin_lock_irq(&hugetlb_lock);
2808 	if (!folio_test_hugetlb(old_folio)) {
2809 		/*
2810 		 * Freed from under us. Drop new_folio too.
2811 		 */
2812 		goto free_new;
2813 	} else if (folio_ref_count(old_folio)) {
2814 		bool isolated;
2815 
2816 		/*
2817 		 * Someone has grabbed the folio, try to isolate it here.
2818 		 * Fail with -EBUSY if not possible.
2819 		 */
2820 		spin_unlock_irq(&hugetlb_lock);
2821 		isolated = folio_isolate_hugetlb(old_folio, list);
2822 		ret = isolated ? 0 : -EBUSY;
2823 		spin_lock_irq(&hugetlb_lock);
2824 		goto free_new;
2825 	} else if (!folio_test_hugetlb_freed(old_folio)) {
2826 		/*
2827 		 * Folio's refcount is 0 but it has not been enqueued in the
2828 		 * freelist yet. Race window is small, so we can succeed here if
2829 		 * we retry.
2830 		 */
2831 		spin_unlock_irq(&hugetlb_lock);
2832 		cond_resched();
2833 		goto retry;
2834 	} else {
2835 		h = folio_hstate(old_folio);
2836 		if (!new_folio) {
2837 			spin_unlock_irq(&hugetlb_lock);
2838 			gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
2839 			new_folio = alloc_buddy_hugetlb_folio(h, gfp_mask, nid,
2840 							      NULL, NULL);
2841 			if (!new_folio)
2842 				return -ENOMEM;
2843 			__prep_new_hugetlb_folio(h, new_folio);
2844 			goto retry;
2845 		}
2846 
2847 		/*
2848 		 * Ok, old_folio is still a genuine free hugepage. Remove it from
2849 		 * the freelist and decrease the counters. These will be
2850 		 * incremented again when calling __prep_account_new_huge_page()
2851 		 * and enqueue_hugetlb_folio() for new_folio. The counters will
2852 		 * remain stable since this happens under the lock.
2853 		 */
2854 		remove_hugetlb_folio(h, old_folio, false);
2855 
2856 		/*
2857 		 * Ref count on new_folio is already zero as it was dropped
2858 		 * earlier.  It can be directly added to the pool free list.
2859 		 */
2860 		__prep_account_new_huge_page(h, nid);
2861 		enqueue_hugetlb_folio(h, new_folio);
2862 
2863 		/*
2864 		 * Folio has been replaced, we can safely free the old one.
2865 		 */
2866 		spin_unlock_irq(&hugetlb_lock);
2867 		update_and_free_hugetlb_folio(h, old_folio, false);
2868 	}
2869 
2870 	return ret;
2871 
2872 free_new:
2873 	spin_unlock_irq(&hugetlb_lock);
2874 	if (new_folio)
2875 		update_and_free_hugetlb_folio(h, new_folio, false);
2876 
2877 	return ret;
2878 }
2879 
2880 int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list)
2881 {
2882 	int ret = -EBUSY;
2883 
2884 	/* Not to disrupt normal path by vainly holding hugetlb_lock */
2885 	if (!folio_test_hugetlb(folio))
2886 		return 0;
2887 
2888 	/*
2889 	 * Fence off gigantic pages as there is a cyclic dependency between
2890 	 * alloc_contig_range and them. Return -ENOMEM as this has the effect
2891 	 * of bailing out right away without further retrying.
2892 	 */
2893 	if (folio_order(folio) > MAX_PAGE_ORDER)
2894 		return -ENOMEM;
2895 
2896 	if (folio_ref_count(folio) && folio_isolate_hugetlb(folio, list))
2897 		ret = 0;
2898 	else if (!folio_ref_count(folio))
2899 		ret = alloc_and_dissolve_hugetlb_folio(folio, list);
2900 
2901 	return ret;
2902 }
2903 
2904 /*
2905  *  replace_free_hugepage_folios - Replace free hugepage folios in a given pfn
2906  *  range with new folios.
2907  *  @start_pfn: start pfn of the given pfn range
2908  *  @end_pfn: end pfn of the given pfn range
2909  *  Returns 0 on success, otherwise negated error.
2910  */
2911 int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn)
2912 {
2913 	struct folio *folio;
2914 	int ret = 0;
2915 
2916 	LIST_HEAD(isolate_list);
2917 
2918 	while (start_pfn < end_pfn) {
2919 		folio = pfn_folio(start_pfn);
2920 
2921 		/* Not to disrupt normal path by vainly holding hugetlb_lock */
2922 		if (folio_test_hugetlb(folio) && !folio_ref_count(folio)) {
2923 			ret = alloc_and_dissolve_hugetlb_folio(folio, &isolate_list);
2924 			if (ret)
2925 				break;
2926 
2927 			putback_movable_pages(&isolate_list);
2928 		}
2929 		start_pfn++;
2930 	}
2931 
2932 	return ret;
2933 }
2934 
2935 void wait_for_freed_hugetlb_folios(void)
2936 {
2937 	if (llist_empty(&hpage_freelist))
2938 		return;
2939 
2940 	flush_work(&free_hpage_work);
2941 }
2942 
2943 typedef enum {
2944 	/*
2945 	 * For either 0/1: we checked the per-vma resv map, and one resv
2946 	 * count either can be reused (0), or an extra needed (1).
2947 	 */
2948 	MAP_CHG_REUSE = 0,
2949 	MAP_CHG_NEEDED = 1,
2950 	/*
2951 	 * Cannot use per-vma resv count can be used, hence a new resv
2952 	 * count is enforced.
2953 	 *
2954 	 * NOTE: This is mostly identical to MAP_CHG_NEEDED, except
2955 	 * that currently vma_needs_reservation() has an unwanted side
2956 	 * effect to either use end() or commit() to complete the
2957 	 * transaction.	 Hence it needs to differenciate from NEEDED.
2958 	 */
2959 	MAP_CHG_ENFORCED = 2,
2960 } map_chg_state;
2961 
2962 /*
2963  * NOTE! "cow_from_owner" represents a very hacky usage only used in CoW
2964  * faults of hugetlb private mappings on top of a non-page-cache folio (in
2965  * which case even if there's a private vma resv map it won't cover such
2966  * allocation).  New call sites should (probably) never set it to true!!
2967  * When it's set, the allocation will bypass all vma level reservations.
2968  */
2969 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
2970 				    unsigned long addr, bool cow_from_owner)
2971 {
2972 	struct hugepage_subpool *spool = subpool_vma(vma);
2973 	struct hstate *h = hstate_vma(vma);
2974 	struct folio *folio;
2975 	long retval, gbl_chg, gbl_reserve;
2976 	map_chg_state map_chg;
2977 	int ret, idx;
2978 	struct hugetlb_cgroup *h_cg = NULL;
2979 	gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL;
2980 
2981 	idx = hstate_index(h);
2982 
2983 	/* Whether we need a separate per-vma reservation? */
2984 	if (cow_from_owner) {
2985 		/*
2986 		 * Special case!  Since it's a CoW on top of a reserved
2987 		 * page, the private resv map doesn't count.  So it cannot
2988 		 * consume the per-vma resv map even if it's reserved.
2989 		 */
2990 		map_chg = MAP_CHG_ENFORCED;
2991 	} else {
2992 		/*
2993 		 * Examine the region/reserve map to determine if the process
2994 		 * has a reservation for the page to be allocated.  A return
2995 		 * code of zero indicates a reservation exists (no change).
2996 		 */
2997 		retval = vma_needs_reservation(h, vma, addr);
2998 		if (retval < 0)
2999 			return ERR_PTR(-ENOMEM);
3000 		map_chg = retval ? MAP_CHG_NEEDED : MAP_CHG_REUSE;
3001 	}
3002 
3003 	/*
3004 	 * Whether we need a separate global reservation?
3005 	 *
3006 	 * Processes that did not create the mapping will have no
3007 	 * reserves as indicated by the region/reserve map. Check
3008 	 * that the allocation will not exceed the subpool limit.
3009 	 * Or if it can get one from the pool reservation directly.
3010 	 */
3011 	if (map_chg) {
3012 		gbl_chg = hugepage_subpool_get_pages(spool, 1);
3013 		if (gbl_chg < 0)
3014 			goto out_end_reservation;
3015 	} else {
3016 		/*
3017 		 * If we have the vma reservation ready, no need for extra
3018 		 * global reservation.
3019 		 */
3020 		gbl_chg = 0;
3021 	}
3022 
3023 	/*
3024 	 * If this allocation is not consuming a per-vma reservation,
3025 	 * charge the hugetlb cgroup now.
3026 	 */
3027 	if (map_chg) {
3028 		ret = hugetlb_cgroup_charge_cgroup_rsvd(
3029 			idx, pages_per_huge_page(h), &h_cg);
3030 		if (ret)
3031 			goto out_subpool_put;
3032 	}
3033 
3034 	ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
3035 	if (ret)
3036 		goto out_uncharge_cgroup_reservation;
3037 
3038 	spin_lock_irq(&hugetlb_lock);
3039 	/*
3040 	 * glb_chg is passed to indicate whether or not a page must be taken
3041 	 * from the global free pool (global change).  gbl_chg == 0 indicates
3042 	 * a reservation exists for the allocation.
3043 	 */
3044 	folio = dequeue_hugetlb_folio_vma(h, vma, addr, gbl_chg);
3045 	if (!folio) {
3046 		spin_unlock_irq(&hugetlb_lock);
3047 		folio = alloc_buddy_hugetlb_folio_with_mpol(h, vma, addr);
3048 		if (!folio)
3049 			goto out_uncharge_cgroup;
3050 		spin_lock_irq(&hugetlb_lock);
3051 		list_add(&folio->lru, &h->hugepage_activelist);
3052 		folio_ref_unfreeze(folio, 1);
3053 		/* Fall through */
3054 	}
3055 
3056 	/*
3057 	 * Either dequeued or buddy-allocated folio needs to add special
3058 	 * mark to the folio when it consumes a global reservation.
3059 	 */
3060 	if (!gbl_chg) {
3061 		folio_set_hugetlb_restore_reserve(folio);
3062 		h->resv_huge_pages--;
3063 	}
3064 
3065 	hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, folio);
3066 	/* If allocation is not consuming a reservation, also store the
3067 	 * hugetlb_cgroup pointer on the page.
3068 	 */
3069 	if (map_chg) {
3070 		hugetlb_cgroup_commit_charge_rsvd(idx, pages_per_huge_page(h),
3071 						  h_cg, folio);
3072 	}
3073 
3074 	spin_unlock_irq(&hugetlb_lock);
3075 
3076 	hugetlb_set_folio_subpool(folio, spool);
3077 
3078 	if (map_chg != MAP_CHG_ENFORCED) {
3079 		/* commit() is only needed if the map_chg is not enforced */
3080 		retval = vma_commit_reservation(h, vma, addr);
3081 		/*
3082 		 * Check for possible race conditions. When it happens..
3083 		 * The page was added to the reservation map between
3084 		 * vma_needs_reservation and vma_commit_reservation.
3085 		 * This indicates a race with hugetlb_reserve_pages.
3086 		 * Adjust for the subpool count incremented above AND
3087 		 * in hugetlb_reserve_pages for the same page.	Also,
3088 		 * the reservation count added in hugetlb_reserve_pages
3089 		 * no longer applies.
3090 		 */
3091 		if (unlikely(map_chg == MAP_CHG_NEEDED && retval == 0)) {
3092 			long rsv_adjust;
3093 
3094 			rsv_adjust = hugepage_subpool_put_pages(spool, 1);
3095 			hugetlb_acct_memory(h, -rsv_adjust);
3096 			if (map_chg) {
3097 				spin_lock_irq(&hugetlb_lock);
3098 				hugetlb_cgroup_uncharge_folio_rsvd(
3099 				    hstate_index(h), pages_per_huge_page(h),
3100 				    folio);
3101 				spin_unlock_irq(&hugetlb_lock);
3102 			}
3103 		}
3104 	}
3105 
3106 	ret = mem_cgroup_charge_hugetlb(folio, gfp);
3107 	/*
3108 	 * Unconditionally increment NR_HUGETLB here. If it turns out that
3109 	 * mem_cgroup_charge_hugetlb failed, then immediately free the page and
3110 	 * decrement NR_HUGETLB.
3111 	 */
3112 	lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h));
3113 
3114 	if (ret == -ENOMEM) {
3115 		free_huge_folio(folio);
3116 		return ERR_PTR(-ENOMEM);
3117 	}
3118 
3119 	return folio;
3120 
3121 out_uncharge_cgroup:
3122 	hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
3123 out_uncharge_cgroup_reservation:
3124 	if (map_chg)
3125 		hugetlb_cgroup_uncharge_cgroup_rsvd(idx, pages_per_huge_page(h),
3126 						    h_cg);
3127 out_subpool_put:
3128 	/*
3129 	 * put page to subpool iff the quota of subpool's rsv_hpages is used
3130 	 * during hugepage_subpool_get_pages.
3131 	 */
3132 	if (map_chg && !gbl_chg) {
3133 		gbl_reserve = hugepage_subpool_put_pages(spool, 1);
3134 		hugetlb_acct_memory(h, -gbl_reserve);
3135 	}
3136 
3137 
3138 out_end_reservation:
3139 	if (map_chg != MAP_CHG_ENFORCED)
3140 		vma_end_reservation(h, vma, addr);
3141 	return ERR_PTR(-ENOSPC);
3142 }
3143 
3144 static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact)
3145 {
3146 	struct huge_bootmem_page *m;
3147 	int listnode = nid;
3148 
3149 	if (hugetlb_early_cma(h))
3150 		m = hugetlb_cma_alloc_bootmem(h, &listnode, node_exact);
3151 	else {
3152 		if (node_exact)
3153 			m = memblock_alloc_exact_nid_raw(huge_page_size(h),
3154 				huge_page_size(h), 0,
3155 				MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3156 		else {
3157 			m = memblock_alloc_try_nid_raw(huge_page_size(h),
3158 				huge_page_size(h), 0,
3159 				MEMBLOCK_ALLOC_ACCESSIBLE, nid);
3160 			/*
3161 			 * For pre-HVO to work correctly, pages need to be on
3162 			 * the list for the node they were actually allocated
3163 			 * from. That node may be different in the case of
3164 			 * fallback by memblock_alloc_try_nid_raw. So,
3165 			 * extract the actual node first.
3166 			 */
3167 			if (m)
3168 				listnode = early_pfn_to_nid(PHYS_PFN(virt_to_phys(m)));
3169 		}
3170 
3171 		if (m) {
3172 			m->flags = 0;
3173 			m->cma = NULL;
3174 		}
3175 	}
3176 
3177 	if (m) {
3178 		/*
3179 		 * Use the beginning of the huge page to store the
3180 		 * huge_bootmem_page struct (until gather_bootmem
3181 		 * puts them into the mem_map).
3182 		 *
3183 		 * Put them into a private list first because mem_map
3184 		 * is not up yet.
3185 		 */
3186 		INIT_LIST_HEAD(&m->list);
3187 		list_add(&m->list, &huge_boot_pages[listnode]);
3188 		m->hstate = h;
3189 	}
3190 
3191 	return m;
3192 }
3193 
3194 int alloc_bootmem_huge_page(struct hstate *h, int nid)
3195 	__attribute__ ((weak, alias("__alloc_bootmem_huge_page")));
3196 int __alloc_bootmem_huge_page(struct hstate *h, int nid)
3197 {
3198 	struct huge_bootmem_page *m = NULL; /* initialize for clang */
3199 	int nr_nodes, node = nid;
3200 
3201 	/* do node specific alloc */
3202 	if (nid != NUMA_NO_NODE) {
3203 		m = alloc_bootmem(h, node, true);
3204 		if (!m)
3205 			return 0;
3206 		goto found;
3207 	}
3208 
3209 	/* allocate from next node when distributing huge pages */
3210 	for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node,
3211 				    &hugetlb_bootmem_nodes) {
3212 		m = alloc_bootmem(h, node, false);
3213 		if (!m)
3214 			return 0;
3215 		goto found;
3216 	}
3217 
3218 found:
3219 
3220 	/*
3221 	 * Only initialize the head struct page in memmap_init_reserved_pages,
3222 	 * rest of the struct pages will be initialized by the HugeTLB
3223 	 * subsystem itself.
3224 	 * The head struct page is used to get folio information by the HugeTLB
3225 	 * subsystem like zone id and node id.
3226 	 */
3227 	memblock_reserved_mark_noinit(virt_to_phys((void *)m + PAGE_SIZE),
3228 		huge_page_size(h) - PAGE_SIZE);
3229 
3230 	return 1;
3231 }
3232 
3233 /* Initialize [start_page:end_page_number] tail struct pages of a hugepage */
3234 static void __init hugetlb_folio_init_tail_vmemmap(struct folio *folio,
3235 					unsigned long start_page_number,
3236 					unsigned long end_page_number)
3237 {
3238 	enum zone_type zone = zone_idx(folio_zone(folio));
3239 	int nid = folio_nid(folio);
3240 	struct page *page = folio_page(folio, start_page_number);
3241 	unsigned long head_pfn = folio_pfn(folio);
3242 	unsigned long pfn, end_pfn = head_pfn + end_page_number;
3243 
3244 	/*
3245 	 * As we marked all tail pages with memblock_reserved_mark_noinit(),
3246 	 * we must initialize them ourselves here.
3247 	 */
3248 	for (pfn = head_pfn + start_page_number; pfn < end_pfn; page++, pfn++) {
3249 		__init_single_page(page, pfn, zone, nid);
3250 		prep_compound_tail((struct page *)folio, pfn - head_pfn);
3251 		set_page_count(page, 0);
3252 	}
3253 }
3254 
3255 static void __init hugetlb_folio_init_vmemmap(struct folio *folio,
3256 					      struct hstate *h,
3257 					      unsigned long nr_pages)
3258 {
3259 	int ret;
3260 
3261 	/*
3262 	 * This is an open-coded prep_compound_page() whereby we avoid
3263 	 * walking pages twice by initializing/preparing+freezing them in the
3264 	 * same go.
3265 	 */
3266 	__folio_clear_reserved(folio);
3267 	__folio_set_head(folio);
3268 	ret = folio_ref_freeze(folio, 1);
3269 	VM_BUG_ON(!ret);
3270 	hugetlb_folio_init_tail_vmemmap(folio, 1, nr_pages);
3271 	prep_compound_head((struct page *)folio, huge_page_order(h));
3272 }
3273 
3274 static bool __init hugetlb_bootmem_page_prehvo(struct huge_bootmem_page *m)
3275 {
3276 	return m->flags & HUGE_BOOTMEM_HVO;
3277 }
3278 
3279 static bool __init hugetlb_bootmem_page_earlycma(struct huge_bootmem_page *m)
3280 {
3281 	return m->flags & HUGE_BOOTMEM_CMA;
3282 }
3283 
3284 /*
3285  * memblock-allocated pageblocks might not have the migrate type set
3286  * if marked with the 'noinit' flag. Set it to the default (MIGRATE_MOVABLE)
3287  * here, or MIGRATE_CMA if this was a page allocated through an early CMA
3288  * reservation.
3289  *
3290  * In case of vmemmap optimized folios, the tail vmemmap pages are mapped
3291  * read-only, but that's ok - for sparse vmemmap this does not write to
3292  * the page structure.
3293  */
3294 static void __init hugetlb_bootmem_init_migratetype(struct folio *folio,
3295 							  struct hstate *h)
3296 {
3297 	unsigned long nr_pages = pages_per_huge_page(h), i;
3298 
3299 	WARN_ON_ONCE(!pageblock_aligned(folio_pfn(folio)));
3300 
3301 	for (i = 0; i < nr_pages; i += pageblock_nr_pages) {
3302 		if (folio_test_hugetlb_cma(folio))
3303 			init_cma_pageblock(folio_page(folio, i));
3304 		else
3305 			init_pageblock_migratetype(folio_page(folio, i),
3306 					  MIGRATE_MOVABLE, false);
3307 	}
3308 }
3309 
3310 static void __init prep_and_add_bootmem_folios(struct hstate *h,
3311 					struct list_head *folio_list)
3312 {
3313 	unsigned long flags;
3314 	struct folio *folio, *tmp_f;
3315 
3316 	/* Send list for bulk vmemmap optimization processing */
3317 	hugetlb_vmemmap_optimize_bootmem_folios(h, folio_list);
3318 
3319 	list_for_each_entry_safe(folio, tmp_f, folio_list, lru) {
3320 		if (!folio_test_hugetlb_vmemmap_optimized(folio)) {
3321 			/*
3322 			 * If HVO fails, initialize all tail struct pages
3323 			 * We do not worry about potential long lock hold
3324 			 * time as this is early in boot and there should
3325 			 * be no contention.
3326 			 */
3327 			hugetlb_folio_init_tail_vmemmap(folio,
3328 					HUGETLB_VMEMMAP_RESERVE_PAGES,
3329 					pages_per_huge_page(h));
3330 		}
3331 		hugetlb_bootmem_init_migratetype(folio, h);
3332 		/* Subdivide locks to achieve better parallel performance */
3333 		spin_lock_irqsave(&hugetlb_lock, flags);
3334 		__prep_account_new_huge_page(h, folio_nid(folio));
3335 		enqueue_hugetlb_folio(h, folio);
3336 		spin_unlock_irqrestore(&hugetlb_lock, flags);
3337 	}
3338 }
3339 
3340 bool __init hugetlb_bootmem_page_zones_valid(int nid,
3341 					     struct huge_bootmem_page *m)
3342 {
3343 	unsigned long start_pfn;
3344 	bool valid;
3345 
3346 	if (m->flags & HUGE_BOOTMEM_ZONES_VALID) {
3347 		/*
3348 		 * Already validated, skip check.
3349 		 */
3350 		return true;
3351 	}
3352 
3353 	if (hugetlb_bootmem_page_earlycma(m)) {
3354 		valid = cma_validate_zones(m->cma);
3355 		goto out;
3356 	}
3357 
3358 	start_pfn = virt_to_phys(m) >> PAGE_SHIFT;
3359 
3360 	valid = !pfn_range_intersects_zones(nid, start_pfn,
3361 			pages_per_huge_page(m->hstate));
3362 out:
3363 	if (!valid)
3364 		hstate_boot_nrinvalid[hstate_index(m->hstate)]++;
3365 
3366 	return valid;
3367 }
3368 
3369 /*
3370  * Free a bootmem page that was found to be invalid (intersecting with
3371  * multiple zones).
3372  *
3373  * Since it intersects with multiple zones, we can't just do a free
3374  * operation on all pages at once, but instead have to walk all
3375  * pages, freeing them one by one.
3376  */
3377 static void __init hugetlb_bootmem_free_invalid_page(int nid, struct page *page,
3378 					     struct hstate *h)
3379 {
3380 	unsigned long npages = pages_per_huge_page(h);
3381 	unsigned long pfn;
3382 
3383 	while (npages--) {
3384 		pfn = page_to_pfn(page);
3385 		__init_page_from_nid(pfn, nid);
3386 		free_reserved_page(page);
3387 		page++;
3388 	}
3389 }
3390 
3391 /*
3392  * Put bootmem huge pages into the standard lists after mem_map is up.
3393  * Note: This only applies to gigantic (order > MAX_PAGE_ORDER) pages.
3394  */
3395 static void __init gather_bootmem_prealloc_node(unsigned long nid)
3396 {
3397 	LIST_HEAD(folio_list);
3398 	struct huge_bootmem_page *m, *tm;
3399 	struct hstate *h = NULL, *prev_h = NULL;
3400 
3401 	list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) {
3402 		struct page *page = virt_to_page(m);
3403 		struct folio *folio = (void *)page;
3404 
3405 		h = m->hstate;
3406 		if (!hugetlb_bootmem_page_zones_valid(nid, m)) {
3407 			/*
3408 			 * Can't use this page. Initialize the
3409 			 * page structures if that hasn't already
3410 			 * been done, and give them to the page
3411 			 * allocator.
3412 			 */
3413 			hugetlb_bootmem_free_invalid_page(nid, page, h);
3414 			continue;
3415 		}
3416 
3417 		/*
3418 		 * It is possible to have multiple huge page sizes (hstates)
3419 		 * in this list.  If so, process each size separately.
3420 		 */
3421 		if (h != prev_h && prev_h != NULL)
3422 			prep_and_add_bootmem_folios(prev_h, &folio_list);
3423 		prev_h = h;
3424 
3425 		VM_BUG_ON(!hstate_is_gigantic(h));
3426 		WARN_ON(folio_ref_count(folio) != 1);
3427 
3428 		hugetlb_folio_init_vmemmap(folio, h,
3429 					   HUGETLB_VMEMMAP_RESERVE_PAGES);
3430 		init_new_hugetlb_folio(h, folio);
3431 
3432 		if (hugetlb_bootmem_page_prehvo(m))
3433 			/*
3434 			 * If pre-HVO was done, just set the
3435 			 * flag, the HVO code will then skip
3436 			 * this folio.
3437 			 */
3438 			folio_set_hugetlb_vmemmap_optimized(folio);
3439 
3440 		if (hugetlb_bootmem_page_earlycma(m))
3441 			folio_set_hugetlb_cma(folio);
3442 
3443 		list_add(&folio->lru, &folio_list);
3444 
3445 		/*
3446 		 * We need to restore the 'stolen' pages to totalram_pages
3447 		 * in order to fix confusing memory reports from free(1) and
3448 		 * other side-effects, like CommitLimit going negative.
3449 		 *
3450 		 * For CMA pages, this is done in init_cma_pageblock
3451 		 * (via hugetlb_bootmem_init_migratetype), so skip it here.
3452 		 */
3453 		if (!folio_test_hugetlb_cma(folio))
3454 			adjust_managed_page_count(page, pages_per_huge_page(h));
3455 		cond_resched();
3456 	}
3457 
3458 	prep_and_add_bootmem_folios(h, &folio_list);
3459 }
3460 
3461 static void __init gather_bootmem_prealloc_parallel(unsigned long start,
3462 						    unsigned long end, void *arg)
3463 {
3464 	int nid;
3465 
3466 	for (nid = start; nid < end; nid++)
3467 		gather_bootmem_prealloc_node(nid);
3468 }
3469 
3470 static void __init gather_bootmem_prealloc(void)
3471 {
3472 	struct padata_mt_job job = {
3473 		.thread_fn	= gather_bootmem_prealloc_parallel,
3474 		.fn_arg		= NULL,
3475 		.start		= 0,
3476 		.size		= nr_node_ids,
3477 		.align		= 1,
3478 		.min_chunk	= 1,
3479 		.max_threads	= num_node_state(N_MEMORY),
3480 		.numa_aware	= true,
3481 	};
3482 
3483 	padata_do_multithreaded(&job);
3484 }
3485 
3486 static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)
3487 {
3488 	unsigned long i;
3489 	char buf[32];
3490 	LIST_HEAD(folio_list);
3491 
3492 	for (i = 0; i < h->max_huge_pages_node[nid]; ++i) {
3493 		if (hstate_is_gigantic(h)) {
3494 			if (!alloc_bootmem_huge_page(h, nid))
3495 				break;
3496 		} else {
3497 			struct folio *folio;
3498 			gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE;
3499 
3500 			folio = only_alloc_fresh_hugetlb_folio(h, gfp_mask, nid,
3501 					&node_states[N_MEMORY], NULL);
3502 			if (!folio)
3503 				break;
3504 			list_add(&folio->lru, &folio_list);
3505 		}
3506 		cond_resched();
3507 	}
3508 
3509 	if (!list_empty(&folio_list))
3510 		prep_and_add_allocated_folios(h, &folio_list);
3511 
3512 	if (i == h->max_huge_pages_node[nid])
3513 		return;
3514 
3515 	string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3516 	pr_warn("HugeTLB: allocating %u of page size %s failed node%d.  Only allocated %lu hugepages.\n",
3517 		h->max_huge_pages_node[nid], buf, nid, i);
3518 	h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
3519 	h->max_huge_pages_node[nid] = i;
3520 }
3521 
3522 static bool __init hugetlb_hstate_alloc_pages_specific_nodes(struct hstate *h)
3523 {
3524 	int i;
3525 	bool node_specific_alloc = false;
3526 
3527 	for_each_online_node(i) {
3528 		if (h->max_huge_pages_node[i] > 0) {
3529 			hugetlb_hstate_alloc_pages_onenode(h, i);
3530 			node_specific_alloc = true;
3531 		}
3532 	}
3533 
3534 	return node_specific_alloc;
3535 }
3536 
3537 static void __init hugetlb_hstate_alloc_pages_errcheck(unsigned long allocated, struct hstate *h)
3538 {
3539 	if (allocated < h->max_huge_pages) {
3540 		char buf[32];
3541 
3542 		string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3543 		pr_warn("HugeTLB: allocating %lu of page size %s failed.  Only allocated %lu hugepages.\n",
3544 			h->max_huge_pages, buf, allocated);
3545 		h->max_huge_pages = allocated;
3546 	}
3547 }
3548 
3549 static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned long end, void *arg)
3550 {
3551 	struct hstate *h = (struct hstate *)arg;
3552 	int i, num = end - start;
3553 	nodemask_t node_alloc_noretry;
3554 	LIST_HEAD(folio_list);
3555 	int next_node = first_online_node;
3556 
3557 	/* Bit mask controlling how hard we retry per-node allocations.*/
3558 	nodes_clear(node_alloc_noretry);
3559 
3560 	for (i = 0; i < num; ++i) {
3561 		struct folio *folio = alloc_pool_huge_folio(h, &node_states[N_MEMORY],
3562 						&node_alloc_noretry, &next_node);
3563 		if (!folio)
3564 			break;
3565 
3566 		list_move(&folio->lru, &folio_list);
3567 		cond_resched();
3568 	}
3569 
3570 	prep_and_add_allocated_folios(h, &folio_list);
3571 }
3572 
3573 static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h)
3574 {
3575 	unsigned long i;
3576 
3577 	for (i = 0; i < h->max_huge_pages; ++i) {
3578 		if (!alloc_bootmem_huge_page(h, NUMA_NO_NODE))
3579 			break;
3580 		cond_resched();
3581 	}
3582 
3583 	return i;
3584 }
3585 
3586 static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h)
3587 {
3588 	struct padata_mt_job job = {
3589 		.fn_arg		= h,
3590 		.align		= 1,
3591 		.numa_aware	= true
3592 	};
3593 
3594 	unsigned long jiffies_start;
3595 	unsigned long jiffies_end;
3596 
3597 	job.thread_fn	= hugetlb_pages_alloc_boot_node;
3598 	job.start	= 0;
3599 	job.size	= h->max_huge_pages;
3600 
3601 	/*
3602 	 * job.max_threads is 25% of the available cpu threads by default.
3603 	 *
3604 	 * On large servers with terabytes of memory, huge page allocation
3605 	 * can consume a considerably amount of time.
3606 	 *
3607 	 * Tests below show how long it takes to allocate 1 TiB of memory with 2MiB huge pages.
3608 	 * 2MiB huge pages. Using more threads can significantly improve allocation time.
3609 	 *
3610 	 * +-----------------------+-------+-------+-------+-------+-------+
3611 	 * | threads               |   8   |   16  |   32  |   64  |   128 |
3612 	 * +-----------------------+-------+-------+-------+-------+-------+
3613 	 * | skylake      144 cpus |   44s |   22s |   16s |   19s |   20s |
3614 	 * | cascade lake 192 cpus |   39s |   20s |   11s |   10s |    9s |
3615 	 * +-----------------------+-------+-------+-------+-------+-------+
3616 	 */
3617 	if (hugepage_allocation_threads == 0) {
3618 		hugepage_allocation_threads = num_online_cpus() / 4;
3619 		hugepage_allocation_threads = max(hugepage_allocation_threads, 1);
3620 	}
3621 
3622 	job.max_threads	= hugepage_allocation_threads;
3623 	job.min_chunk	= h->max_huge_pages / hugepage_allocation_threads;
3624 
3625 	jiffies_start = jiffies;
3626 	padata_do_multithreaded(&job);
3627 	jiffies_end = jiffies;
3628 
3629 	pr_info("HugeTLB: allocation took %dms with hugepage_allocation_threads=%ld\n",
3630 		jiffies_to_msecs(jiffies_end - jiffies_start),
3631 		hugepage_allocation_threads);
3632 
3633 	return h->nr_huge_pages;
3634 }
3635 
3636 /*
3637  * NOTE: this routine is called in different contexts for gigantic and
3638  * non-gigantic pages.
3639  * - For gigantic pages, this is called early in the boot process and
3640  *   pages are allocated from memblock allocated or something similar.
3641  *   Gigantic pages are actually added to pools later with the routine
3642  *   gather_bootmem_prealloc.
3643  * - For non-gigantic pages, this is called later in the boot process after
3644  *   all of mm is up and functional.  Pages are allocated from buddy and
3645  *   then added to hugetlb pools.
3646  */
3647 static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
3648 {
3649 	unsigned long allocated;
3650 
3651 	/*
3652 	 * Skip gigantic hugepages allocation if early CMA
3653 	 * reservations are not available.
3654 	 */
3655 	if (hstate_is_gigantic(h) && hugetlb_cma_total_size() &&
3656 	    !hugetlb_early_cma(h)) {
3657 		pr_warn_once("HugeTLB: hugetlb_cma is enabled, skip boot time allocation\n");
3658 		return;
3659 	}
3660 
3661 	if (!h->max_huge_pages)
3662 		return;
3663 
3664 	/* do node specific alloc */
3665 	if (hugetlb_hstate_alloc_pages_specific_nodes(h))
3666 		return;
3667 
3668 	/* below will do all node balanced alloc */
3669 	if (hstate_is_gigantic(h))
3670 		allocated = hugetlb_gigantic_pages_alloc_boot(h);
3671 	else
3672 		allocated = hugetlb_pages_alloc_boot(h);
3673 
3674 	hugetlb_hstate_alloc_pages_errcheck(allocated, h);
3675 }
3676 
3677 static void __init hugetlb_init_hstates(void)
3678 {
3679 	struct hstate *h, *h2;
3680 
3681 	for_each_hstate(h) {
3682 		/*
3683 		 * Always reset to first_memory_node here, even if
3684 		 * next_nid_to_alloc was set before - we can't
3685 		 * reference hugetlb_bootmem_nodes after init, and
3686 		 * first_memory_node is right for all further allocations.
3687 		 */
3688 		h->next_nid_to_alloc = first_memory_node;
3689 		h->next_nid_to_free = first_memory_node;
3690 
3691 		/* oversize hugepages were init'ed in early boot */
3692 		if (!hstate_is_gigantic(h))
3693 			hugetlb_hstate_alloc_pages(h);
3694 
3695 		/*
3696 		 * Set demote order for each hstate.  Note that
3697 		 * h->demote_order is initially 0.
3698 		 * - We can not demote gigantic pages if runtime freeing
3699 		 *   is not supported, so skip this.
3700 		 * - If CMA allocation is possible, we can not demote
3701 		 *   HUGETLB_PAGE_ORDER or smaller size pages.
3702 		 */
3703 		if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
3704 			continue;
3705 		if (hugetlb_cma_total_size() && h->order <= HUGETLB_PAGE_ORDER)
3706 			continue;
3707 		for_each_hstate(h2) {
3708 			if (h2 == h)
3709 				continue;
3710 			if (h2->order < h->order &&
3711 			    h2->order > h->demote_order)
3712 				h->demote_order = h2->order;
3713 		}
3714 	}
3715 }
3716 
3717 static void __init report_hugepages(void)
3718 {
3719 	struct hstate *h;
3720 	unsigned long nrinvalid;
3721 
3722 	for_each_hstate(h) {
3723 		char buf[32];
3724 
3725 		nrinvalid = hstate_boot_nrinvalid[hstate_index(h)];
3726 		h->max_huge_pages -= nrinvalid;
3727 
3728 		string_get_size(huge_page_size(h), 1, STRING_UNITS_2, buf, 32);
3729 		pr_info("HugeTLB: registered %s page size, pre-allocated %ld pages\n",
3730 			buf, h->nr_huge_pages);
3731 		if (nrinvalid)
3732 			pr_info("HugeTLB: %s page size: %lu invalid page%s discarded\n",
3733 					buf, nrinvalid, str_plural(nrinvalid));
3734 		pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
3735 			hugetlb_vmemmap_optimizable_size(h) / SZ_1K, buf);
3736 	}
3737 }
3738 
3739 #ifdef CONFIG_HIGHMEM
3740 static void try_to_free_low(struct hstate *h, unsigned long count,
3741 						nodemask_t *nodes_allowed)
3742 {
3743 	int i;
3744 	LIST_HEAD(page_list);
3745 
3746 	lockdep_assert_held(&hugetlb_lock);
3747 	if (hstate_is_gigantic(h))
3748 		return;
3749 
3750 	/*
3751 	 * Collect pages to be freed on a list, and free after dropping lock
3752 	 */
3753 	for_each_node_mask(i, *nodes_allowed) {
3754 		struct folio *folio, *next;
3755 		struct list_head *freel = &h->hugepage_freelists[i];
3756 		list_for_each_entry_safe(folio, next, freel, lru) {
3757 			if (count >= h->nr_huge_pages)
3758 				goto out;
3759 			if (folio_test_highmem(folio))
3760 				continue;
3761 			remove_hugetlb_folio(h, folio, false);
3762 			list_add(&folio->lru, &page_list);
3763 		}
3764 	}
3765 
3766 out:
3767 	spin_unlock_irq(&hugetlb_lock);
3768 	update_and_free_pages_bulk(h, &page_list);
3769 	spin_lock_irq(&hugetlb_lock);
3770 }
3771 #else
3772 static inline void try_to_free_low(struct hstate *h, unsigned long count,
3773 						nodemask_t *nodes_allowed)
3774 {
3775 }
3776 #endif
3777 
3778 /*
3779  * Increment or decrement surplus_huge_pages.  Keep node-specific counters
3780  * balanced by operating on them in a round-robin fashion.
3781  * Returns 1 if an adjustment was made.
3782  */
3783 static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
3784 				int delta)
3785 {
3786 	int nr_nodes, node;
3787 
3788 	lockdep_assert_held(&hugetlb_lock);
3789 	VM_BUG_ON(delta != -1 && delta != 1);
3790 
3791 	if (delta < 0) {
3792 		for_each_node_mask_to_alloc(&h->next_nid_to_alloc, nr_nodes, node, nodes_allowed) {
3793 			if (h->surplus_huge_pages_node[node])
3794 				goto found;
3795 		}
3796 	} else {
3797 		for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
3798 			if (h->surplus_huge_pages_node[node] <
3799 					h->nr_huge_pages_node[node])
3800 				goto found;
3801 		}
3802 	}
3803 	return 0;
3804 
3805 found:
3806 	h->surplus_huge_pages += delta;
3807 	h->surplus_huge_pages_node[node] += delta;
3808 	return 1;
3809 }
3810 
3811 #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
3812 static int set_max_huge_pages(struct hstate *h, unsigned long count, int nid,
3813 			      nodemask_t *nodes_allowed)
3814 {
3815 	unsigned long persistent_free_count;
3816 	unsigned long min_count;
3817 	unsigned long allocated;
3818 	struct folio *folio;
3819 	LIST_HEAD(page_list);
3820 	NODEMASK_ALLOC(nodemask_t, node_alloc_noretry, GFP_KERNEL);
3821 
3822 	/*
3823 	 * Bit mask controlling how hard we retry per-node allocations.
3824 	 * If we can not allocate the bit mask, do not attempt to allocate
3825 	 * the requested huge pages.
3826 	 */
3827 	if (node_alloc_noretry)
3828 		nodes_clear(*node_alloc_noretry);
3829 	else
3830 		return -ENOMEM;
3831 
3832 	/*
3833 	 * resize_lock mutex prevents concurrent adjustments to number of
3834 	 * pages in hstate via the proc/sysfs interfaces.
3835 	 */
3836 	mutex_lock(&h->resize_lock);
3837 	flush_free_hpage_work(h);
3838 	spin_lock_irq(&hugetlb_lock);
3839 
3840 	/*
3841 	 * Check for a node specific request.
3842 	 * Changing node specific huge page count may require a corresponding
3843 	 * change to the global count.  In any case, the passed node mask
3844 	 * (nodes_allowed) will restrict alloc/free to the specified node.
3845 	 */
3846 	if (nid != NUMA_NO_NODE) {
3847 		unsigned long old_count = count;
3848 
3849 		count += persistent_huge_pages(h) -
3850 			 (h->nr_huge_pages_node[nid] -
3851 			  h->surplus_huge_pages_node[nid]);
3852 		/*
3853 		 * User may have specified a large count value which caused the
3854 		 * above calculation to overflow.  In this case, they wanted
3855 		 * to allocate as many huge pages as possible.  Set count to
3856 		 * largest possible value to align with their intention.
3857 		 */
3858 		if (count < old_count)
3859 			count = ULONG_MAX;
3860 	}
3861 
3862 	/*
3863 	 * Gigantic pages runtime allocation depend on the capability for large
3864 	 * page range allocation.
3865 	 * If the system does not provide this feature, return an error when
3866 	 * the user tries to allocate gigantic pages but let the user free the
3867 	 * boottime allocated gigantic pages.
3868 	 */
3869 	if (hstate_is_gigantic(h) && !IS_ENABLED(CONFIG_CONTIG_ALLOC)) {
3870 		if (count > persistent_huge_pages(h)) {
3871 			spin_unlock_irq(&hugetlb_lock);
3872 			mutex_unlock(&h->resize_lock);
3873 			NODEMASK_FREE(node_alloc_noretry);
3874 			return -EINVAL;
3875 		}
3876 		/* Fall through to decrease pool */
3877 	}
3878 
3879 	/*
3880 	 * Increase the pool size
3881 	 * First take pages out of surplus state.  Then make up the
3882 	 * remaining difference by allocating fresh huge pages.
3883 	 *
3884 	 * We might race with alloc_surplus_hugetlb_folio() here and be unable
3885 	 * to convert a surplus huge page to a normal huge page. That is
3886 	 * not critical, though, it just means the overall size of the
3887 	 * pool might be one hugepage larger than it needs to be, but
3888 	 * within all the constraints specified by the sysctls.
3889 	 */
3890 	while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
3891 		if (!adjust_pool_surplus(h, nodes_allowed, -1))
3892 			break;
3893 	}
3894 
3895 	allocated = 0;
3896 	while (count > (persistent_huge_pages(h) + allocated)) {
3897 		/*
3898 		 * If this allocation races such that we no longer need the
3899 		 * page, free_huge_folio will handle it by freeing the page
3900 		 * and reducing the surplus.
3901 		 */
3902 		spin_unlock_irq(&hugetlb_lock);
3903 
3904 		/* yield cpu to avoid soft lockup */
3905 		cond_resched();
3906 
3907 		folio = alloc_pool_huge_folio(h, nodes_allowed,
3908 						node_alloc_noretry,
3909 						&h->next_nid_to_alloc);
3910 		if (!folio) {
3911 			prep_and_add_allocated_folios(h, &page_list);
3912 			spin_lock_irq(&hugetlb_lock);
3913 			goto out;
3914 		}
3915 
3916 		list_add(&folio->lru, &page_list);
3917 		allocated++;
3918 
3919 		/* Bail for signals. Probably ctrl-c from user */
3920 		if (signal_pending(current)) {
3921 			prep_and_add_allocated_folios(h, &page_list);
3922 			spin_lock_irq(&hugetlb_lock);
3923 			goto out;
3924 		}
3925 
3926 		spin_lock_irq(&hugetlb_lock);
3927 	}
3928 
3929 	/* Add allocated pages to the pool */
3930 	if (!list_empty(&page_list)) {
3931 		spin_unlock_irq(&hugetlb_lock);
3932 		prep_and_add_allocated_folios(h, &page_list);
3933 		spin_lock_irq(&hugetlb_lock);
3934 	}
3935 
3936 	/*
3937 	 * Decrease the pool size
3938 	 * First return free pages to the buddy allocator (being careful
3939 	 * to keep enough around to satisfy reservations).  Then place
3940 	 * pages into surplus state as needed so the pool will shrink
3941 	 * to the desired size as pages become free.
3942 	 *
3943 	 * By placing pages into the surplus state independent of the
3944 	 * overcommit value, we are allowing the surplus pool size to
3945 	 * exceed overcommit. There are few sane options here. Since
3946 	 * alloc_surplus_hugetlb_folio() is checking the global counter,
3947 	 * though, we'll note that we're not allowed to exceed surplus
3948 	 * and won't grow the pool anywhere else. Not until one of the
3949 	 * sysctls are changed, or the surplus pages go out of use.
3950 	 *
3951 	 * min_count is the expected number of persistent pages, we
3952 	 * shouldn't calculate min_count by using
3953 	 * resv_huge_pages + persistent_huge_pages() - free_huge_pages,
3954 	 * because there may exist free surplus huge pages, and this will
3955 	 * lead to subtracting twice. Free surplus huge pages come from HVO
3956 	 * failing to restore vmemmap, see comments in the callers of
3957 	 * hugetlb_vmemmap_restore_folio(). Thus, we should calculate
3958 	 * persistent free count first.
3959 	 */
3960 	persistent_free_count = h->free_huge_pages;
3961 	if (h->free_huge_pages > persistent_huge_pages(h)) {
3962 		if (h->free_huge_pages > h->surplus_huge_pages)
3963 			persistent_free_count -= h->surplus_huge_pages;
3964 		else
3965 			persistent_free_count = 0;
3966 	}
3967 	min_count = h->resv_huge_pages + persistent_huge_pages(h) - persistent_free_count;
3968 	min_count = max(count, min_count);
3969 	try_to_free_low(h, min_count, nodes_allowed);
3970 
3971 	/*
3972 	 * Collect pages to be removed on list without dropping lock
3973 	 */
3974 	while (min_count < persistent_huge_pages(h)) {
3975 		folio = remove_pool_hugetlb_folio(h, nodes_allowed, 0);
3976 		if (!folio)
3977 			break;
3978 
3979 		list_add(&folio->lru, &page_list);
3980 	}
3981 	/* free the pages after dropping lock */
3982 	spin_unlock_irq(&hugetlb_lock);
3983 	update_and_free_pages_bulk(h, &page_list);
3984 	flush_free_hpage_work(h);
3985 	spin_lock_irq(&hugetlb_lock);
3986 
3987 	while (count < persistent_huge_pages(h)) {
3988 		if (!adjust_pool_surplus(h, nodes_allowed, 1))
3989 			break;
3990 	}
3991 out:
3992 	h->max_huge_pages = persistent_huge_pages(h);
3993 	spin_unlock_irq(&hugetlb_lock);
3994 	mutex_unlock(&h->resize_lock);
3995 
3996 	NODEMASK_FREE(node_alloc_noretry);
3997 
3998 	return 0;
3999 }
4000 
4001 static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
4002 				       struct list_head *src_list)
4003 {
4004 	long rc;
4005 	struct folio *folio, *next;
4006 	LIST_HEAD(dst_list);
4007 	LIST_HEAD(ret_list);
4008 
4009 	rc = hugetlb_vmemmap_restore_folios(src, src_list, &ret_list);
4010 	list_splice_init(&ret_list, src_list);
4011 
4012 	/*
4013 	 * Taking target hstate mutex synchronizes with set_max_huge_pages.
4014 	 * Without the mutex, pages added to target hstate could be marked
4015 	 * as surplus.
4016 	 *
4017 	 * Note that we already hold src->resize_lock.  To prevent deadlock,
4018 	 * use the convention of always taking larger size hstate mutex first.
4019 	 */
4020 	mutex_lock(&dst->resize_lock);
4021 
4022 	list_for_each_entry_safe(folio, next, src_list, lru) {
4023 		int i;
4024 		bool cma;
4025 
4026 		if (folio_test_hugetlb_vmemmap_optimized(folio))
4027 			continue;
4028 
4029 		cma = folio_test_hugetlb_cma(folio);
4030 
4031 		list_del(&folio->lru);
4032 
4033 		split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
4034 		pgalloc_tag_split(folio, huge_page_order(src), huge_page_order(dst));
4035 
4036 		for (i = 0; i < pages_per_huge_page(src); i += pages_per_huge_page(dst)) {
4037 			struct page *page = folio_page(folio, i);
4038 			/* Careful: see __split_huge_page_tail() */
4039 			struct folio *new_folio = (struct folio *)page;
4040 
4041 			clear_compound_head(page);
4042 			prep_compound_page(page, dst->order);
4043 
4044 			new_folio->mapping = NULL;
4045 			init_new_hugetlb_folio(dst, new_folio);
4046 			/* Copy the CMA flag so that it is freed correctly */
4047 			if (cma)
4048 				folio_set_hugetlb_cma(new_folio);
4049 			list_add(&new_folio->lru, &dst_list);
4050 		}
4051 	}
4052 
4053 	prep_and_add_allocated_folios(dst, &dst_list);
4054 
4055 	mutex_unlock(&dst->resize_lock);
4056 
4057 	return rc;
4058 }
4059 
4060 static long demote_pool_huge_page(struct hstate *src, nodemask_t *nodes_allowed,
4061 				  unsigned long nr_to_demote)
4062 	__must_hold(&hugetlb_lock)
4063 {
4064 	int nr_nodes, node;
4065 	struct hstate *dst;
4066 	long rc = 0;
4067 	long nr_demoted = 0;
4068 
4069 	lockdep_assert_held(&hugetlb_lock);
4070 
4071 	/* We should never get here if no demote order */
4072 	if (!src->demote_order) {
4073 		pr_warn("HugeTLB: NULL demote order passed to demote_pool_huge_page.\n");
4074 		return -EINVAL;		/* internal error */
4075 	}
4076 	dst = size_to_hstate(PAGE_SIZE << src->demote_order);
4077 
4078 	for_each_node_mask_to_free(src, nr_nodes, node, nodes_allowed) {
4079 		LIST_HEAD(list);
4080 		struct folio *folio, *next;
4081 
4082 		list_for_each_entry_safe(folio, next, &src->hugepage_freelists[node], lru) {
4083 			if (folio_test_hwpoison(folio))
4084 				continue;
4085 
4086 			remove_hugetlb_folio(src, folio, false);
4087 			list_add(&folio->lru, &list);
4088 
4089 			if (++nr_demoted == nr_to_demote)
4090 				break;
4091 		}
4092 
4093 		spin_unlock_irq(&hugetlb_lock);
4094 
4095 		rc = demote_free_hugetlb_folios(src, dst, &list);
4096 
4097 		spin_lock_irq(&hugetlb_lock);
4098 
4099 		list_for_each_entry_safe(folio, next, &list, lru) {
4100 			list_del(&folio->lru);
4101 			add_hugetlb_folio(src, folio, false);
4102 
4103 			nr_demoted--;
4104 		}
4105 
4106 		if (rc < 0 || nr_demoted == nr_to_demote)
4107 			break;
4108 	}
4109 
4110 	/*
4111 	 * Not absolutely necessary, but for consistency update max_huge_pages
4112 	 * based on pool changes for the demoted page.
4113 	 */
4114 	src->max_huge_pages -= nr_demoted;
4115 	dst->max_huge_pages += nr_demoted << (huge_page_order(src) - huge_page_order(dst));
4116 
4117 	if (rc < 0)
4118 		return rc;
4119 
4120 	if (nr_demoted)
4121 		return nr_demoted;
4122 	/*
4123 	 * Only way to get here is if all pages on free lists are poisoned.
4124 	 * Return -EBUSY so that caller will not retry.
4125 	 */
4126 	return -EBUSY;
4127 }
4128 
4129 #define HSTATE_ATTR_RO(_name) \
4130 	static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
4131 
4132 #define HSTATE_ATTR_WO(_name) \
4133 	static struct kobj_attribute _name##_attr = __ATTR_WO(_name)
4134 
4135 #define HSTATE_ATTR(_name) \
4136 	static struct kobj_attribute _name##_attr = __ATTR_RW(_name)
4137 
4138 static struct kobject *hugepages_kobj;
4139 static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
4140 
4141 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
4142 
4143 static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
4144 {
4145 	int i;
4146 
4147 	for (i = 0; i < HUGE_MAX_HSTATE; i++)
4148 		if (hstate_kobjs[i] == kobj) {
4149 			if (nidp)
4150 				*nidp = NUMA_NO_NODE;
4151 			return &hstates[i];
4152 		}
4153 
4154 	return kobj_to_node_hstate(kobj, nidp);
4155 }
4156 
4157 static ssize_t nr_hugepages_show_common(struct kobject *kobj,
4158 					struct kobj_attribute *attr, char *buf)
4159 {
4160 	struct hstate *h;
4161 	unsigned long nr_huge_pages;
4162 	int nid;
4163 
4164 	h = kobj_to_hstate(kobj, &nid);
4165 	if (nid == NUMA_NO_NODE)
4166 		nr_huge_pages = h->nr_huge_pages;
4167 	else
4168 		nr_huge_pages = h->nr_huge_pages_node[nid];
4169 
4170 	return sysfs_emit(buf, "%lu\n", nr_huge_pages);
4171 }
4172 
4173 static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
4174 					   struct hstate *h, int nid,
4175 					   unsigned long count, size_t len)
4176 {
4177 	int err;
4178 	nodemask_t nodes_allowed, *n_mask;
4179 
4180 	if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
4181 		return -EINVAL;
4182 
4183 	if (nid == NUMA_NO_NODE) {
4184 		/*
4185 		 * global hstate attribute
4186 		 */
4187 		if (!(obey_mempolicy &&
4188 				init_nodemask_of_mempolicy(&nodes_allowed)))
4189 			n_mask = &node_states[N_MEMORY];
4190 		else
4191 			n_mask = &nodes_allowed;
4192 	} else {
4193 		/*
4194 		 * Node specific request.  count adjustment happens in
4195 		 * set_max_huge_pages() after acquiring hugetlb_lock.
4196 		 */
4197 		init_nodemask_of_node(&nodes_allowed, nid);
4198 		n_mask = &nodes_allowed;
4199 	}
4200 
4201 	err = set_max_huge_pages(h, count, nid, n_mask);
4202 
4203 	return err ? err : len;
4204 }
4205 
4206 static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
4207 					 struct kobject *kobj, const char *buf,
4208 					 size_t len)
4209 {
4210 	struct hstate *h;
4211 	unsigned long count;
4212 	int nid;
4213 	int err;
4214 
4215 	err = kstrtoul(buf, 10, &count);
4216 	if (err)
4217 		return err;
4218 
4219 	h = kobj_to_hstate(kobj, &nid);
4220 	return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
4221 }
4222 
4223 static ssize_t nr_hugepages_show(struct kobject *kobj,
4224 				       struct kobj_attribute *attr, char *buf)
4225 {
4226 	return nr_hugepages_show_common(kobj, attr, buf);
4227 }
4228 
4229 static ssize_t nr_hugepages_store(struct kobject *kobj,
4230 	       struct kobj_attribute *attr, const char *buf, size_t len)
4231 {
4232 	return nr_hugepages_store_common(false, kobj, buf, len);
4233 }
4234 HSTATE_ATTR(nr_hugepages);
4235 
4236 #ifdef CONFIG_NUMA
4237 
4238 /*
4239  * hstate attribute for optionally mempolicy-based constraint on persistent
4240  * huge page alloc/free.
4241  */
4242 static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
4243 					   struct kobj_attribute *attr,
4244 					   char *buf)
4245 {
4246 	return nr_hugepages_show_common(kobj, attr, buf);
4247 }
4248 
4249 static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
4250 	       struct kobj_attribute *attr, const char *buf, size_t len)
4251 {
4252 	return nr_hugepages_store_common(true, kobj, buf, len);
4253 }
4254 HSTATE_ATTR(nr_hugepages_mempolicy);
4255 #endif
4256 
4257 
4258 static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
4259 					struct kobj_attribute *attr, char *buf)
4260 {
4261 	struct hstate *h = kobj_to_hstate(kobj, NULL);
4262 	return sysfs_emit(buf, "%lu\n", h->nr_overcommit_huge_pages);
4263 }
4264 
4265 static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
4266 		struct kobj_attribute *attr, const char *buf, size_t count)
4267 {
4268 	int err;
4269 	unsigned long input;
4270 	struct hstate *h = kobj_to_hstate(kobj, NULL);
4271 
4272 	if (hstate_is_gigantic(h))
4273 		return -EINVAL;
4274 
4275 	err = kstrtoul(buf, 10, &input);
4276 	if (err)
4277 		return err;
4278 
4279 	spin_lock_irq(&hugetlb_lock);
4280 	h->nr_overcommit_huge_pages = input;
4281 	spin_unlock_irq(&hugetlb_lock);
4282 
4283 	return count;
4284 }
4285 HSTATE_ATTR(nr_overcommit_hugepages);
4286 
4287 static ssize_t free_hugepages_show(struct kobject *kobj,
4288 					struct kobj_attribute *attr, char *buf)
4289 {
4290 	struct hstate *h;
4291 	unsigned long free_huge_pages;
4292 	int nid;
4293 
4294 	h = kobj_to_hstate(kobj, &nid);
4295 	if (nid == NUMA_NO_NODE)
4296 		free_huge_pages = h->free_huge_pages;
4297 	else
4298 		free_huge_pages = h->free_huge_pages_node[nid];
4299 
4300 	return sysfs_emit(buf, "%lu\n", free_huge_pages);
4301 }
4302 HSTATE_ATTR_RO(free_hugepages);
4303 
4304 static ssize_t resv_hugepages_show(struct kobject *kobj,
4305 					struct kobj_attribute *attr, char *buf)
4306 {
4307 	struct hstate *h = kobj_to_hstate(kobj, NULL);
4308 	return sysfs_emit(buf, "%lu\n", h->resv_huge_pages);
4309 }
4310 HSTATE_ATTR_RO(resv_hugepages);
4311 
4312 static ssize_t surplus_hugepages_show(struct kobject *kobj,
4313 					struct kobj_attribute *attr, char *buf)
4314 {
4315 	struct hstate *h;
4316 	unsigned long surplus_huge_pages;
4317 	int nid;
4318 
4319 	h = kobj_to_hstate(kobj, &nid);
4320 	if (nid == NUMA_NO_NODE)
4321 		surplus_huge_pages = h->surplus_huge_pages;
4322 	else
4323 		surplus_huge_pages = h->surplus_huge_pages_node[nid];
4324 
4325 	return sysfs_emit(buf, "%lu\n", surplus_huge_pages);
4326 }
4327 HSTATE_ATTR_RO(surplus_hugepages);
4328 
4329 static ssize_t demote_store(struct kobject *kobj,
4330 	       struct kobj_attribute *attr, const char *buf, size_t len)
4331 {
4332 	unsigned long nr_demote;
4333 	unsigned long nr_available;
4334 	nodemask_t nodes_allowed, *n_mask;
4335 	struct hstate *h;
4336 	int err;
4337 	int nid;
4338 
4339 	err = kstrtoul(buf, 10, &nr_demote);
4340 	if (err)
4341 		return err;
4342 	h = kobj_to_hstate(kobj, &nid);
4343 
4344 	if (nid != NUMA_NO_NODE) {
4345 		init_nodemask_of_node(&nodes_allowed, nid);
4346 		n_mask = &nodes_allowed;
4347 	} else {
4348 		n_mask = &node_states[N_MEMORY];
4349 	}
4350 
4351 	/* Synchronize with other sysfs operations modifying huge pages */
4352 	mutex_lock(&h->resize_lock);
4353 	spin_lock_irq(&hugetlb_lock);
4354 
4355 	while (nr_demote) {
4356 		long rc;
4357 
4358 		/*
4359 		 * Check for available pages to demote each time thorough the
4360 		 * loop as demote_pool_huge_page will drop hugetlb_lock.
4361 		 */
4362 		if (nid != NUMA_NO_NODE)
4363 			nr_available = h->free_huge_pages_node[nid];
4364 		else
4365 			nr_available = h->free_huge_pages;
4366 		nr_available -= h->resv_huge_pages;
4367 		if (!nr_available)
4368 			break;
4369 
4370 		rc = demote_pool_huge_page(h, n_mask, nr_demote);
4371 		if (rc < 0) {
4372 			err = rc;
4373 			break;
4374 		}
4375 
4376 		nr_demote -= rc;
4377 	}
4378 
4379 	spin_unlock_irq(&hugetlb_lock);
4380 	mutex_unlock(&h->resize_lock);
4381 
4382 	if (err)
4383 		return err;
4384 	return len;
4385 }
4386 HSTATE_ATTR_WO(demote);
4387 
4388 static ssize_t demote_size_show(struct kobject *kobj,
4389 					struct kobj_attribute *attr, char *buf)
4390 {
4391 	struct hstate *h = kobj_to_hstate(kobj, NULL);
4392 	unsigned long demote_size = (PAGE_SIZE << h->demote_order) / SZ_1K;
4393 
4394 	return sysfs_emit(buf, "%lukB\n", demote_size);
4395 }
4396 
4397 static ssize_t demote_size_store(struct kobject *kobj,
4398 					struct kobj_attribute *attr,
4399 					const char *buf, size_t count)
4400 {
4401 	struct hstate *h, *demote_hstate;
4402 	unsigned long demote_size;
4403 	unsigned int demote_order;
4404 
4405 	demote_size = (unsigned long)memparse(buf, NULL);
4406 
4407 	demote_hstate = size_to_hstate(demote_size);
4408 	if (!demote_hstate)
4409 		return -EINVAL;
4410 	demote_order = demote_hstate->order;
4411 	if (demote_order < HUGETLB_PAGE_ORDER)
4412 		return -EINVAL;
4413 
4414 	/* demote order must be smaller than hstate order */
4415 	h = kobj_to_hstate(kobj, NULL);
4416 	if (demote_order >= h->order)
4417 		return -EINVAL;
4418 
4419 	/* resize_lock synchronizes access to demote size and writes */
4420 	mutex_lock(&h->resize_lock);
4421 	h->demote_order = demote_order;
4422 	mutex_unlock(&h->resize_lock);
4423 
4424 	return count;
4425 }
4426 HSTATE_ATTR(demote_size);
4427 
4428 static struct attribute *hstate_attrs[] = {
4429 	&nr_hugepages_attr.attr,
4430 	&nr_overcommit_hugepages_attr.attr,
4431 	&free_hugepages_attr.attr,
4432 	&resv_hugepages_attr.attr,
4433 	&surplus_hugepages_attr.attr,
4434 #ifdef CONFIG_NUMA
4435 	&nr_hugepages_mempolicy_attr.attr,
4436 #endif
4437 	NULL,
4438 };
4439 
4440 static const struct attribute_group hstate_attr_group = {
4441 	.attrs = hstate_attrs,
4442 };
4443 
4444 static struct attribute *hstate_demote_attrs[] = {
4445 	&demote_size_attr.attr,
4446 	&demote_attr.attr,
4447 	NULL,
4448 };
4449 
4450 static const struct attribute_group hstate_demote_attr_group = {
4451 	.attrs = hstate_demote_attrs,
4452 };
4453 
4454 static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
4455 				    struct kobject **hstate_kobjs,
4456 				    const struct attribute_group *hstate_attr_group)
4457 {
4458 	int retval;
4459 	int hi = hstate_index(h);
4460 
4461 	hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
4462 	if (!hstate_kobjs[hi])
4463 		return -ENOMEM;
4464 
4465 	retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
4466 	if (retval) {
4467 		kobject_put(hstate_kobjs[hi]);
4468 		hstate_kobjs[hi] = NULL;
4469 		return retval;
4470 	}
4471 
4472 	if (h->demote_order) {
4473 		retval = sysfs_create_group(hstate_kobjs[hi],
4474 					    &hstate_demote_attr_group);
4475 		if (retval) {
4476 			pr_warn("HugeTLB unable to create demote interfaces for %s\n", h->name);
4477 			sysfs_remove_group(hstate_kobjs[hi], hstate_attr_group);
4478 			kobject_put(hstate_kobjs[hi]);
4479 			hstate_kobjs[hi] = NULL;
4480 			return retval;
4481 		}
4482 	}
4483 
4484 	return 0;
4485 }
4486 
4487 #ifdef CONFIG_NUMA
4488 static bool hugetlb_sysfs_initialized __ro_after_init;
4489 
4490 /*
4491  * node_hstate/s - associate per node hstate attributes, via their kobjects,
4492  * with node devices in node_devices[] using a parallel array.  The array
4493  * index of a node device or _hstate == node id.
4494  * This is here to avoid any static dependency of the node device driver, in
4495  * the base kernel, on the hugetlb module.
4496  */
4497 struct node_hstate {
4498 	struct kobject		*hugepages_kobj;
4499 	struct kobject		*hstate_kobjs[HUGE_MAX_HSTATE];
4500 };
4501 static struct node_hstate node_hstates[MAX_NUMNODES];
4502 
4503 /*
4504  * A subset of global hstate attributes for node devices
4505  */
4506 static struct attribute *per_node_hstate_attrs[] = {
4507 	&nr_hugepages_attr.attr,
4508 	&free_hugepages_attr.attr,
4509 	&surplus_hugepages_attr.attr,
4510 	NULL,
4511 };
4512 
4513 static const struct attribute_group per_node_hstate_attr_group = {
4514 	.attrs = per_node_hstate_attrs,
4515 };
4516 
4517 /*
4518  * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
4519  * Returns node id via non-NULL nidp.
4520  */
4521 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4522 {
4523 	int nid;
4524 
4525 	for (nid = 0; nid < nr_node_ids; nid++) {
4526 		struct node_hstate *nhs = &node_hstates[nid];
4527 		int i;
4528 		for (i = 0; i < HUGE_MAX_HSTATE; i++)
4529 			if (nhs->hstate_kobjs[i] == kobj) {
4530 				if (nidp)
4531 					*nidp = nid;
4532 				return &hstates[i];
4533 			}
4534 	}
4535 
4536 	BUG();
4537 	return NULL;
4538 }
4539 
4540 /*
4541  * Unregister hstate attributes from a single node device.
4542  * No-op if no hstate attributes attached.
4543  */
4544 void hugetlb_unregister_node(struct node *node)
4545 {
4546 	struct hstate *h;
4547 	struct node_hstate *nhs = &node_hstates[node->dev.id];
4548 
4549 	if (!nhs->hugepages_kobj)
4550 		return;		/* no hstate attributes */
4551 
4552 	for_each_hstate(h) {
4553 		int idx = hstate_index(h);
4554 		struct kobject *hstate_kobj = nhs->hstate_kobjs[idx];
4555 
4556 		if (!hstate_kobj)
4557 			continue;
4558 		if (h->demote_order)
4559 			sysfs_remove_group(hstate_kobj, &hstate_demote_attr_group);
4560 		sysfs_remove_group(hstate_kobj, &per_node_hstate_attr_group);
4561 		kobject_put(hstate_kobj);
4562 		nhs->hstate_kobjs[idx] = NULL;
4563 	}
4564 
4565 	kobject_put(nhs->hugepages_kobj);
4566 	nhs->hugepages_kobj = NULL;
4567 }
4568 
4569 
4570 /*
4571  * Register hstate attributes for a single node device.
4572  * No-op if attributes already registered.
4573  */
4574 void hugetlb_register_node(struct node *node)
4575 {
4576 	struct hstate *h;
4577 	struct node_hstate *nhs = &node_hstates[node->dev.id];
4578 	int err;
4579 
4580 	if (!hugetlb_sysfs_initialized)
4581 		return;
4582 
4583 	if (nhs->hugepages_kobj)
4584 		return;		/* already allocated */
4585 
4586 	nhs->hugepages_kobj = kobject_create_and_add("hugepages",
4587 							&node->dev.kobj);
4588 	if (!nhs->hugepages_kobj)
4589 		return;
4590 
4591 	for_each_hstate(h) {
4592 		err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
4593 						nhs->hstate_kobjs,
4594 						&per_node_hstate_attr_group);
4595 		if (err) {
4596 			pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
4597 				h->name, node->dev.id);
4598 			hugetlb_unregister_node(node);
4599 			break;
4600 		}
4601 	}
4602 }
4603 
4604 /*
4605  * hugetlb init time:  register hstate attributes for all registered node
4606  * devices of nodes that have memory.  All on-line nodes should have
4607  * registered their associated device by this time.
4608  */
4609 static void __init hugetlb_register_all_nodes(void)
4610 {
4611 	int nid;
4612 
4613 	for_each_online_node(nid)
4614 		hugetlb_register_node(node_devices[nid]);
4615 }
4616 #else	/* !CONFIG_NUMA */
4617 
4618 static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
4619 {
4620 	BUG();
4621 	if (nidp)
4622 		*nidp = -1;
4623 	return NULL;
4624 }
4625 
4626 static void hugetlb_register_all_nodes(void) { }
4627 
4628 #endif
4629 
4630 static void __init hugetlb_sysfs_init(void)
4631 {
4632 	struct hstate *h;
4633 	int err;
4634 
4635 	hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
4636 	if (!hugepages_kobj)
4637 		return;
4638 
4639 	for_each_hstate(h) {
4640 		err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
4641 					 hstate_kobjs, &hstate_attr_group);
4642 		if (err)
4643 			pr_err("HugeTLB: Unable to add hstate %s\n", h->name);
4644 	}
4645 
4646 #ifdef CONFIG_NUMA
4647 	hugetlb_sysfs_initialized = true;
4648 #endif
4649 	hugetlb_register_all_nodes();
4650 }
4651 
4652 #ifdef CONFIG_SYSCTL
4653 static void hugetlb_sysctl_init(void);
4654 #else
4655 static inline void hugetlb_sysctl_init(void) { }
4656 #endif
4657 
4658 static int __init hugetlb_init(void)
4659 {
4660 	int i;
4661 
4662 	BUILD_BUG_ON(sizeof_field(struct page, private) * BITS_PER_BYTE <
4663 			__NR_HPAGEFLAGS);
4664 	BUILD_BUG_ON_INVALID(HUGETLB_PAGE_ORDER > MAX_FOLIO_ORDER);
4665 
4666 	if (!hugepages_supported()) {
4667 		if (hugetlb_max_hstate || default_hstate_max_huge_pages)
4668 			pr_warn("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n");
4669 		return 0;
4670 	}
4671 
4672 	/*
4673 	 * Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists.  Some
4674 	 * architectures depend on setup being done here.
4675 	 */
4676 	hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
4677 	if (!parsed_default_hugepagesz) {
4678 		/*
4679 		 * If we did not parse a default huge page size, set
4680 		 * default_hstate_idx to HPAGE_SIZE hstate. And, if the
4681 		 * number of huge pages for this default size was implicitly
4682 		 * specified, set that here as well.
4683 		 * Note that the implicit setting will overwrite an explicit
4684 		 * setting.  A warning will be printed in this case.
4685 		 */
4686 		default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
4687 		if (default_hstate_max_huge_pages) {
4688 			if (default_hstate.max_huge_pages) {
4689 				char buf[32];
4690 
4691 				string_get_size(huge_page_size(&default_hstate),
4692 					1, STRING_UNITS_2, buf, 32);
4693 				pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
4694 					default_hstate.max_huge_pages, buf);
4695 				pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
4696 					default_hstate_max_huge_pages);
4697 			}
4698 			default_hstate.max_huge_pages =
4699 				default_hstate_max_huge_pages;
4700 
4701 			for_each_online_node(i)
4702 				default_hstate.max_huge_pages_node[i] =
4703 					default_hugepages_in_node[i];
4704 		}
4705 	}
4706 
4707 	hugetlb_cma_check();
4708 	hugetlb_init_hstates();
4709 	gather_bootmem_prealloc();
4710 	report_hugepages();
4711 
4712 	hugetlb_sysfs_init();
4713 	hugetlb_cgroup_file_init();
4714 	hugetlb_sysctl_init();
4715 
4716 #ifdef CONFIG_SMP
4717 	num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
4718 #else
4719 	num_fault_mutexes = 1;
4720 #endif
4721 	hugetlb_fault_mutex_table =
4722 		kmalloc_array(num_fault_mutexes, sizeof(struct mutex),
4723 			      GFP_KERNEL);
4724 	BUG_ON(!hugetlb_fault_mutex_table);
4725 
4726 	for (i = 0; i < num_fault_mutexes; i++)
4727 		mutex_init(&hugetlb_fault_mutex_table[i]);
4728 	return 0;
4729 }
4730 subsys_initcall(hugetlb_init);
4731 
4732 /* Overwritten by architectures with more huge page sizes */
4733 bool __init __attribute((weak)) arch_hugetlb_valid_size(unsigned long size)
4734 {
4735 	return size == HPAGE_SIZE;
4736 }
4737 
4738 void __init hugetlb_add_hstate(unsigned int order)
4739 {
4740 	struct hstate *h;
4741 	unsigned long i;
4742 
4743 	if (size_to_hstate(PAGE_SIZE << order)) {
4744 		return;
4745 	}
4746 	BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
4747 	BUG_ON(order < order_base_2(__NR_USED_SUBPAGE));
4748 	WARN_ON(order > MAX_FOLIO_ORDER);
4749 	h = &hstates[hugetlb_max_hstate++];
4750 	__mutex_init(&h->resize_lock, "resize mutex", &h->resize_key);
4751 	h->order = order;
4752 	h->mask = ~(huge_page_size(h) - 1);
4753 	for (i = 0; i < MAX_NUMNODES; ++i)
4754 		INIT_LIST_HEAD(&h->hugepage_freelists[i]);
4755 	INIT_LIST_HEAD(&h->hugepage_activelist);
4756 	snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
4757 					huge_page_size(h)/SZ_1K);
4758 
4759 	parsed_hstate = h;
4760 }
4761 
4762 bool __init __weak hugetlb_node_alloc_supported(void)
4763 {
4764 	return true;
4765 }
4766 
4767 static void __init hugepages_clear_pages_in_node(void)
4768 {
4769 	if (!hugetlb_max_hstate) {
4770 		default_hstate_max_huge_pages = 0;
4771 		memset(default_hugepages_in_node, 0,
4772 			sizeof(default_hugepages_in_node));
4773 	} else {
4774 		parsed_hstate->max_huge_pages = 0;
4775 		memset(parsed_hstate->max_huge_pages_node, 0,
4776 			sizeof(parsed_hstate->max_huge_pages_node));
4777 	}
4778 }
4779 
4780 static __init int hugetlb_add_param(char *s, int (*setup)(char *))
4781 {
4782 	size_t len;
4783 	char *p;
4784 
4785 	if (hugetlb_param_index >= HUGE_MAX_CMDLINE_ARGS)
4786 		return -EINVAL;
4787 
4788 	len = strlen(s) + 1;
4789 	if (len + hstate_cmdline_index > sizeof(hstate_cmdline_buf))
4790 		return -EINVAL;
4791 
4792 	p = &hstate_cmdline_buf[hstate_cmdline_index];
4793 	memcpy(p, s, len);
4794 	hstate_cmdline_index += len;
4795 
4796 	hugetlb_params[hugetlb_param_index].val = p;
4797 	hugetlb_params[hugetlb_param_index].setup = setup;
4798 
4799 	hugetlb_param_index++;
4800 
4801 	return 0;
4802 }
4803 
4804 static __init void hugetlb_parse_params(void)
4805 {
4806 	int i;
4807 	struct hugetlb_cmdline *hcp;
4808 
4809 	for (i = 0; i < hugetlb_param_index; i++) {
4810 		hcp = &hugetlb_params[i];
4811 
4812 		hcp->setup(hcp->val);
4813 	}
4814 
4815 	hugetlb_cma_validate_params();
4816 }
4817 
4818 /*
4819  * hugepages command line processing
4820  * hugepages normally follows a valid hugepagsz or default_hugepagsz
4821  * specification.  If not, ignore the hugepages value.  hugepages can also
4822  * be the first huge page command line  option in which case it implicitly
4823  * specifies the number of huge pages for the default size.
4824  */
4825 static int __init hugepages_setup(char *s)
4826 {
4827 	unsigned long *mhp;
4828 	static unsigned long *last_mhp;
4829 	int node = NUMA_NO_NODE;
4830 	int count;
4831 	unsigned long tmp;
4832 	char *p = s;
4833 
4834 	if (!parsed_valid_hugepagesz) {
4835 		pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
4836 		parsed_valid_hugepagesz = true;
4837 		return -EINVAL;
4838 	}
4839 
4840 	/*
4841 	 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
4842 	 * yet, so this hugepages= parameter goes to the "default hstate".
4843 	 * Otherwise, it goes with the previously parsed hugepagesz or
4844 	 * default_hugepagesz.
4845 	 */
4846 	else if (!hugetlb_max_hstate)
4847 		mhp = &default_hstate_max_huge_pages;
4848 	else
4849 		mhp = &parsed_hstate->max_huge_pages;
4850 
4851 	if (mhp == last_mhp) {
4852 		pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
4853 		return 1;
4854 	}
4855 
4856 	while (*p) {
4857 		count = 0;
4858 		if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4859 			goto invalid;
4860 		/* Parameter is node format */
4861 		if (p[count] == ':') {
4862 			if (!hugetlb_node_alloc_supported()) {
4863 				pr_warn("HugeTLB: architecture can't support node specific alloc, ignoring!\n");
4864 				return 1;
4865 			}
4866 			if (tmp >= MAX_NUMNODES || !node_online(tmp))
4867 				goto invalid;
4868 			node = array_index_nospec(tmp, MAX_NUMNODES);
4869 			p += count + 1;
4870 			/* Parse hugepages */
4871 			if (sscanf(p, "%lu%n", &tmp, &count) != 1)
4872 				goto invalid;
4873 			if (!hugetlb_max_hstate)
4874 				default_hugepages_in_node[node] = tmp;
4875 			else
4876 				parsed_hstate->max_huge_pages_node[node] = tmp;
4877 			*mhp += tmp;
4878 			/* Go to parse next node*/
4879 			if (p[count] == ',')
4880 				p += count + 1;
4881 			else
4882 				break;
4883 		} else {
4884 			if (p != s)
4885 				goto invalid;
4886 			*mhp = tmp;
4887 			break;
4888 		}
4889 	}
4890 
4891 	last_mhp = mhp;
4892 
4893 	return 0;
4894 
4895 invalid:
4896 	pr_warn("HugeTLB: Invalid hugepages parameter %s\n", p);
4897 	hugepages_clear_pages_in_node();
4898 	return -EINVAL;
4899 }
4900 hugetlb_early_param("hugepages", hugepages_setup);
4901 
4902 /*
4903  * hugepagesz command line processing
4904  * A specific huge page size can only be specified once with hugepagesz.
4905  * hugepagesz is followed by hugepages on the command line.  The global
4906  * variable 'parsed_valid_hugepagesz' is used to determine if prior
4907  * hugepagesz argument was valid.
4908  */
4909 static int __init hugepagesz_setup(char *s)
4910 {
4911 	unsigned long size;
4912 	struct hstate *h;
4913 
4914 	parsed_valid_hugepagesz = false;
4915 	size = (unsigned long)memparse(s, NULL);
4916 
4917 	if (!arch_hugetlb_valid_size(size)) {
4918 		pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
4919 		return -EINVAL;
4920 	}
4921 
4922 	h = size_to_hstate(size);
4923 	if (h) {
4924 		/*
4925 		 * hstate for this size already exists.  This is normally
4926 		 * an error, but is allowed if the existing hstate is the
4927 		 * default hstate.  More specifically, it is only allowed if
4928 		 * the number of huge pages for the default hstate was not
4929 		 * previously specified.
4930 		 */
4931 		if (!parsed_default_hugepagesz ||  h != &default_hstate ||
4932 		    default_hstate.max_huge_pages) {
4933 			pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
4934 			return -EINVAL;
4935 		}
4936 
4937 		/*
4938 		 * No need to call hugetlb_add_hstate() as hstate already
4939 		 * exists.  But, do set parsed_hstate so that a following
4940 		 * hugepages= parameter will be applied to this hstate.
4941 		 */
4942 		parsed_hstate = h;
4943 		parsed_valid_hugepagesz = true;
4944 		return 0;
4945 	}
4946 
4947 	hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4948 	parsed_valid_hugepagesz = true;
4949 	return 0;
4950 }
4951 hugetlb_early_param("hugepagesz", hugepagesz_setup);
4952 
4953 /*
4954  * default_hugepagesz command line input
4955  * Only one instance of default_hugepagesz allowed on command line.
4956  */
4957 static int __init default_hugepagesz_setup(char *s)
4958 {
4959 	unsigned long size;
4960 	int i;
4961 
4962 	parsed_valid_hugepagesz = false;
4963 	if (parsed_default_hugepagesz) {
4964 		pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
4965 		return -EINVAL;
4966 	}
4967 
4968 	size = (unsigned long)memparse(s, NULL);
4969 
4970 	if (!arch_hugetlb_valid_size(size)) {
4971 		pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
4972 		return -EINVAL;
4973 	}
4974 
4975 	hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
4976 	parsed_valid_hugepagesz = true;
4977 	parsed_default_hugepagesz = true;
4978 	default_hstate_idx = hstate_index(size_to_hstate(size));
4979 
4980 	/*
4981 	 * The number of default huge pages (for this size) could have been
4982 	 * specified as the first hugetlb parameter: hugepages=X.  If so,
4983 	 * then default_hstate_max_huge_pages is set.  If the default huge
4984 	 * page size is gigantic (> MAX_PAGE_ORDER), then the pages must be
4985 	 * allocated here from bootmem allocator.
4986 	 */
4987 	if (default_hstate_max_huge_pages) {
4988 		default_hstate.max_huge_pages = default_hstate_max_huge_pages;
4989 		/*
4990 		 * Since this is an early parameter, we can't check
4991 		 * NUMA node state yet, so loop through MAX_NUMNODES.
4992 		 */
4993 		for (i = 0; i < MAX_NUMNODES; i++) {
4994 			if (default_hugepages_in_node[i] != 0)
4995 				default_hstate.max_huge_pages_node[i] =
4996 					default_hugepages_in_node[i];
4997 		}
4998 		default_hstate_max_huge_pages = 0;
4999 	}
5000 
5001 	return 0;
5002 }
5003 hugetlb_early_param("default_hugepagesz", default_hugepagesz_setup);
5004 
5005 void __init hugetlb_bootmem_set_nodes(void)
5006 {
5007 	int i, nid;
5008 	unsigned long start_pfn, end_pfn;
5009 
5010 	if (!nodes_empty(hugetlb_bootmem_nodes))
5011 		return;
5012 
5013 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
5014 		if (end_pfn > start_pfn)
5015 			node_set(nid, hugetlb_bootmem_nodes);
5016 	}
5017 }
5018 
5019 static bool __hugetlb_bootmem_allocated __initdata;
5020 
5021 bool __init hugetlb_bootmem_allocated(void)
5022 {
5023 	return __hugetlb_bootmem_allocated;
5024 }
5025 
5026 void __init hugetlb_bootmem_alloc(void)
5027 {
5028 	struct hstate *h;
5029 	int i;
5030 
5031 	if (__hugetlb_bootmem_allocated)
5032 		return;
5033 
5034 	hugetlb_bootmem_set_nodes();
5035 
5036 	for (i = 0; i < MAX_NUMNODES; i++)
5037 		INIT_LIST_HEAD(&huge_boot_pages[i]);
5038 
5039 	hugetlb_parse_params();
5040 
5041 	for_each_hstate(h) {
5042 		h->next_nid_to_alloc = first_online_node;
5043 
5044 		if (hstate_is_gigantic(h))
5045 			hugetlb_hstate_alloc_pages(h);
5046 	}
5047 
5048 	__hugetlb_bootmem_allocated = true;
5049 }
5050 
5051 /*
5052  * hugepage_alloc_threads command line parsing.
5053  *
5054  * When set, use this specific number of threads for the boot
5055  * allocation of hugepages.
5056  */
5057 static int __init hugepage_alloc_threads_setup(char *s)
5058 {
5059 	unsigned long allocation_threads;
5060 
5061 	if (kstrtoul(s, 0, &allocation_threads) != 0)
5062 		return 1;
5063 
5064 	if (allocation_threads == 0)
5065 		return 1;
5066 
5067 	hugepage_allocation_threads = allocation_threads;
5068 
5069 	return 1;
5070 }
5071 __setup("hugepage_alloc_threads=", hugepage_alloc_threads_setup);
5072 
5073 static unsigned int allowed_mems_nr(struct hstate *h)
5074 {
5075 	int node;
5076 	unsigned int nr = 0;
5077 	nodemask_t *mbind_nodemask;
5078 	unsigned int *array = h->free_huge_pages_node;
5079 	gfp_t gfp_mask = htlb_alloc_mask(h);
5080 
5081 	mbind_nodemask = policy_mbind_nodemask(gfp_mask);
5082 	for_each_node_mask(node, cpuset_current_mems_allowed) {
5083 		if (!mbind_nodemask || node_isset(node, *mbind_nodemask))
5084 			nr += array[node];
5085 	}
5086 
5087 	return nr;
5088 }
5089 
5090 #ifdef CONFIG_SYSCTL
5091 static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, int write,
5092 					  void *buffer, size_t *length,
5093 					  loff_t *ppos, unsigned long *out)
5094 {
5095 	struct ctl_table dup_table;
5096 
5097 	/*
5098 	 * In order to avoid races with __do_proc_doulongvec_minmax(), we
5099 	 * can duplicate the @table and alter the duplicate of it.
5100 	 */
5101 	dup_table = *table;
5102 	dup_table.data = out;
5103 
5104 	return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos);
5105 }
5106 
5107 static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
5108 			 const struct ctl_table *table, int write,
5109 			 void *buffer, size_t *length, loff_t *ppos)
5110 {
5111 	struct hstate *h = &default_hstate;
5112 	unsigned long tmp = h->max_huge_pages;
5113 	int ret;
5114 
5115 	if (!hugepages_supported())
5116 		return -EOPNOTSUPP;
5117 
5118 	ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
5119 					     &tmp);
5120 	if (ret)
5121 		goto out;
5122 
5123 	if (write)
5124 		ret = __nr_hugepages_store_common(obey_mempolicy, h,
5125 						  NUMA_NO_NODE, tmp, *length);
5126 out:
5127 	return ret;
5128 }
5129 
5130 static int hugetlb_sysctl_handler(const struct ctl_table *table, int write,
5131 			  void *buffer, size_t *length, loff_t *ppos)
5132 {
5133 
5134 	return hugetlb_sysctl_handler_common(false, table, write,
5135 							buffer, length, ppos);
5136 }
5137 
5138 #ifdef CONFIG_NUMA
5139 static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table, int write,
5140 			  void *buffer, size_t *length, loff_t *ppos)
5141 {
5142 	return hugetlb_sysctl_handler_common(true, table, write,
5143 							buffer, length, ppos);
5144 }
5145 #endif /* CONFIG_NUMA */
5146 
5147 static int hugetlb_overcommit_handler(const struct ctl_table *table, int write,
5148 		void *buffer, size_t *length, loff_t *ppos)
5149 {
5150 	struct hstate *h = &default_hstate;
5151 	unsigned long tmp;
5152 	int ret;
5153 
5154 	if (!hugepages_supported())
5155 		return -EOPNOTSUPP;
5156 
5157 	tmp = h->nr_overcommit_huge_pages;
5158 
5159 	if (write && hstate_is_gigantic(h))
5160 		return -EINVAL;
5161 
5162 	ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,
5163 					     &tmp);
5164 	if (ret)
5165 		goto out;
5166 
5167 	if (write) {
5168 		spin_lock_irq(&hugetlb_lock);
5169 		h->nr_overcommit_huge_pages = tmp;
5170 		spin_unlock_irq(&hugetlb_lock);
5171 	}
5172 out:
5173 	return ret;
5174 }
5175 
5176 static const struct ctl_table hugetlb_table[] = {
5177 	{
5178 		.procname	= "nr_hugepages",
5179 		.data		= NULL,
5180 		.maxlen		= sizeof(unsigned long),
5181 		.mode		= 0644,
5182 		.proc_handler	= hugetlb_sysctl_handler,
5183 	},
5184 #ifdef CONFIG_NUMA
5185 	{
5186 		.procname       = "nr_hugepages_mempolicy",
5187 		.data           = NULL,
5188 		.maxlen         = sizeof(unsigned long),
5189 		.mode           = 0644,
5190 		.proc_handler   = &hugetlb_mempolicy_sysctl_handler,
5191 	},
5192 #endif
5193 	{
5194 		.procname	= "hugetlb_shm_group",
5195 		.data		= &sysctl_hugetlb_shm_group,
5196 		.maxlen		= sizeof(gid_t),
5197 		.mode		= 0644,
5198 		.proc_handler	= proc_dointvec,
5199 	},
5200 	{
5201 		.procname	= "nr_overcommit_hugepages",
5202 		.data		= NULL,
5203 		.maxlen		= sizeof(unsigned long),
5204 		.mode		= 0644,
5205 		.proc_handler	= hugetlb_overcommit_handler,
5206 	},
5207 };
5208 
5209 static void __init hugetlb_sysctl_init(void)
5210 {
5211 	register_sysctl_init("vm", hugetlb_table);
5212 }
5213 #endif /* CONFIG_SYSCTL */
5214 
5215 void hugetlb_report_meminfo(struct seq_file *m)
5216 {
5217 	struct hstate *h;
5218 	unsigned long total = 0;
5219 
5220 	if (!hugepages_supported())
5221 		return;
5222 
5223 	for_each_hstate(h) {
5224 		unsigned long count = h->nr_huge_pages;
5225 
5226 		total += huge_page_size(h) * count;
5227 
5228 		if (h == &default_hstate)
5229 			seq_printf(m,
5230 				   "HugePages_Total:   %5lu\n"
5231 				   "HugePages_Free:    %5lu\n"
5232 				   "HugePages_Rsvd:    %5lu\n"
5233 				   "HugePages_Surp:    %5lu\n"
5234 				   "Hugepagesize:   %8lu kB\n",
5235 				   count,
5236 				   h->free_huge_pages,
5237 				   h->resv_huge_pages,
5238 				   h->surplus_huge_pages,
5239 				   huge_page_size(h) / SZ_1K);
5240 	}
5241 
5242 	seq_printf(m, "Hugetlb:        %8lu kB\n", total / SZ_1K);
5243 }
5244 
5245 int hugetlb_report_node_meminfo(char *buf, int len, int nid)
5246 {
5247 	struct hstate *h = &default_hstate;
5248 
5249 	if (!hugepages_supported())
5250 		return 0;
5251 
5252 	return sysfs_emit_at(buf, len,
5253 			     "Node %d HugePages_Total: %5u\n"
5254 			     "Node %d HugePages_Free:  %5u\n"
5255 			     "Node %d HugePages_Surp:  %5u\n",
5256 			     nid, h->nr_huge_pages_node[nid],
5257 			     nid, h->free_huge_pages_node[nid],
5258 			     nid, h->surplus_huge_pages_node[nid]);
5259 }
5260 
5261 void hugetlb_show_meminfo_node(int nid)
5262 {
5263 	struct hstate *h;
5264 
5265 	if (!hugepages_supported())
5266 		return;
5267 
5268 	for_each_hstate(h)
5269 		printk("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
5270 			nid,
5271 			h->nr_huge_pages_node[nid],
5272 			h->free_huge_pages_node[nid],
5273 			h->surplus_huge_pages_node[nid],
5274 			huge_page_size(h) / SZ_1K);
5275 }
5276 
5277 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
5278 {
5279 	seq_printf(m, "HugetlbPages:\t%8lu kB\n",
5280 		   K(atomic_long_read(&mm->hugetlb_usage)));
5281 }
5282 
5283 /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
5284 unsigned long hugetlb_total_pages(void)
5285 {
5286 	struct hstate *h;
5287 	unsigned long nr_total_pages = 0;
5288 
5289 	for_each_hstate(h)
5290 		nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
5291 	return nr_total_pages;
5292 }
5293 
5294 static int hugetlb_acct_memory(struct hstate *h, long delta)
5295 {
5296 	int ret = -ENOMEM;
5297 
5298 	if (!delta)
5299 		return 0;
5300 
5301 	spin_lock_irq(&hugetlb_lock);
5302 	/*
5303 	 * When cpuset is configured, it breaks the strict hugetlb page
5304 	 * reservation as the accounting is done on a global variable. Such
5305 	 * reservation is completely rubbish in the presence of cpuset because
5306 	 * the reservation is not checked against page availability for the
5307 	 * current cpuset. Application can still potentially OOM'ed by kernel
5308 	 * with lack of free htlb page in cpuset that the task is in.
5309 	 * Attempt to enforce strict accounting with cpuset is almost
5310 	 * impossible (or too ugly) because cpuset is too fluid that
5311 	 * task or memory node can be dynamically moved between cpusets.
5312 	 *
5313 	 * The change of semantics for shared hugetlb mapping with cpuset is
5314 	 * undesirable. However, in order to preserve some of the semantics,
5315 	 * we fall back to check against current free page availability as
5316 	 * a best attempt and hopefully to minimize the impact of changing
5317 	 * semantics that cpuset has.
5318 	 *
5319 	 * Apart from cpuset, we also have memory policy mechanism that
5320 	 * also determines from which node the kernel will allocate memory
5321 	 * in a NUMA system. So similar to cpuset, we also should consider
5322 	 * the memory policy of the current task. Similar to the description
5323 	 * above.
5324 	 */
5325 	if (delta > 0) {
5326 		if (gather_surplus_pages(h, delta) < 0)
5327 			goto out;
5328 
5329 		if (delta > allowed_mems_nr(h)) {
5330 			return_unused_surplus_pages(h, delta);
5331 			goto out;
5332 		}
5333 	}
5334 
5335 	ret = 0;
5336 	if (delta < 0)
5337 		return_unused_surplus_pages(h, (unsigned long) -delta);
5338 
5339 out:
5340 	spin_unlock_irq(&hugetlb_lock);
5341 	return ret;
5342 }
5343 
5344 static void hugetlb_vm_op_open(struct vm_area_struct *vma)
5345 {
5346 	struct resv_map *resv = vma_resv_map(vma);
5347 
5348 	/*
5349 	 * HPAGE_RESV_OWNER indicates a private mapping.
5350 	 * This new VMA should share its siblings reservation map if present.
5351 	 * The VMA will only ever have a valid reservation map pointer where
5352 	 * it is being copied for another still existing VMA.  As that VMA
5353 	 * has a reference to the reservation map it cannot disappear until
5354 	 * after this open call completes.  It is therefore safe to take a
5355 	 * new reference here without additional locking.
5356 	 */
5357 	if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
5358 		resv_map_dup_hugetlb_cgroup_uncharge_info(resv);
5359 		kref_get(&resv->refs);
5360 	}
5361 
5362 	/*
5363 	 * vma_lock structure for sharable mappings is vma specific.
5364 	 * Clear old pointer (if copied via vm_area_dup) and allocate
5365 	 * new structure.  Before clearing, make sure vma_lock is not
5366 	 * for this vma.
5367 	 */
5368 	if (vma->vm_flags & VM_MAYSHARE) {
5369 		struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
5370 
5371 		if (vma_lock) {
5372 			if (vma_lock->vma != vma) {
5373 				vma->vm_private_data = NULL;
5374 				hugetlb_vma_lock_alloc(vma);
5375 			} else
5376 				pr_warn("HugeTLB: vma_lock already exists in %s.\n", __func__);
5377 		} else
5378 			hugetlb_vma_lock_alloc(vma);
5379 	}
5380 }
5381 
5382 static void hugetlb_vm_op_close(struct vm_area_struct *vma)
5383 {
5384 	struct hstate *h = hstate_vma(vma);
5385 	struct resv_map *resv;
5386 	struct hugepage_subpool *spool = subpool_vma(vma);
5387 	unsigned long reserve, start, end;
5388 	long gbl_reserve;
5389 
5390 	hugetlb_vma_lock_free(vma);
5391 
5392 	resv = vma_resv_map(vma);
5393 	if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
5394 		return;
5395 
5396 	start = vma_hugecache_offset(h, vma, vma->vm_start);
5397 	end = vma_hugecache_offset(h, vma, vma->vm_end);
5398 
5399 	reserve = (end - start) - region_count(resv, start, end);
5400 	hugetlb_cgroup_uncharge_counter(resv, start, end);
5401 	if (reserve) {
5402 		/*
5403 		 * Decrement reserve counts.  The global reserve count may be
5404 		 * adjusted if the subpool has a minimum size.
5405 		 */
5406 		gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
5407 		hugetlb_acct_memory(h, -gbl_reserve);
5408 	}
5409 
5410 	kref_put(&resv->refs, resv_map_release);
5411 }
5412 
5413 static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
5414 {
5415 	if (addr & ~(huge_page_mask(hstate_vma(vma))))
5416 		return -EINVAL;
5417 	return 0;
5418 }
5419 
5420 void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
5421 {
5422 	/*
5423 	 * PMD sharing is only possible for PUD_SIZE-aligned address ranges
5424 	 * in HugeTLB VMAs. If we will lose PUD_SIZE alignment due to this
5425 	 * split, unshare PMDs in the PUD_SIZE interval surrounding addr now.
5426 	 * This function is called in the middle of a VMA split operation, with
5427 	 * MM, VMA and rmap all write-locked to prevent concurrent page table
5428 	 * walks (except hardware and gup_fast()).
5429 	 */
5430 	vma_assert_write_locked(vma);
5431 	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
5432 
5433 	if (addr & ~PUD_MASK) {
5434 		unsigned long floor = addr & PUD_MASK;
5435 		unsigned long ceil = floor + PUD_SIZE;
5436 
5437 		if (floor >= vma->vm_start && ceil <= vma->vm_end) {
5438 			/*
5439 			 * Locking:
5440 			 * Use take_locks=false here.
5441 			 * The file rmap lock is already held.
5442 			 * The hugetlb VMA lock can't be taken when we already
5443 			 * hold the file rmap lock, and we don't need it because
5444 			 * its purpose is to synchronize against concurrent page
5445 			 * table walks, which are not possible thanks to the
5446 			 * locks held by our caller.
5447 			 */
5448 			hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
5449 		}
5450 	}
5451 }
5452 
5453 static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
5454 {
5455 	return huge_page_size(hstate_vma(vma));
5456 }
5457 
5458 /*
5459  * We cannot handle pagefaults against hugetlb pages at all.  They cause
5460  * handle_mm_fault() to try to instantiate regular-sized pages in the
5461  * hugepage VMA.  do_page_fault() is supposed to trap this, so BUG is we get
5462  * this far.
5463  */
5464 static vm_fault_t hugetlb_vm_op_fault(struct vm_fault *vmf)
5465 {
5466 	BUG();
5467 	return 0;
5468 }
5469 
5470 /*
5471  * When a new function is introduced to vm_operations_struct and added
5472  * to hugetlb_vm_ops, please consider adding the function to shm_vm_ops.
5473  * This is because under System V memory model, mappings created via
5474  * shmget/shmat with "huge page" specified are backed by hugetlbfs files,
5475  * their original vm_ops are overwritten with shm_vm_ops.
5476  */
5477 const struct vm_operations_struct hugetlb_vm_ops = {
5478 	.fault = hugetlb_vm_op_fault,
5479 	.open = hugetlb_vm_op_open,
5480 	.close = hugetlb_vm_op_close,
5481 	.may_split = hugetlb_vm_op_split,
5482 	.pagesize = hugetlb_vm_op_pagesize,
5483 };
5484 
5485 static pte_t make_huge_pte(struct vm_area_struct *vma, struct folio *folio,
5486 		bool try_mkwrite)
5487 {
5488 	pte_t entry = folio_mk_pte(folio, vma->vm_page_prot);
5489 	unsigned int shift = huge_page_shift(hstate_vma(vma));
5490 
5491 	if (try_mkwrite && (vma->vm_flags & VM_WRITE)) {
5492 		entry = pte_mkwrite_novma(pte_mkdirty(entry));
5493 	} else {
5494 		entry = pte_wrprotect(entry);
5495 	}
5496 	entry = pte_mkyoung(entry);
5497 	entry = arch_make_huge_pte(entry, shift, vma->vm_flags);
5498 
5499 	return entry;
5500 }
5501 
5502 static void set_huge_ptep_writable(struct vm_area_struct *vma,
5503 				   unsigned long address, pte_t *ptep)
5504 {
5505 	pte_t entry;
5506 
5507 	entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(vma->vm_mm, address, ptep)));
5508 	if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
5509 		update_mmu_cache(vma, address, ptep);
5510 }
5511 
5512 static void set_huge_ptep_maybe_writable(struct vm_area_struct *vma,
5513 					 unsigned long address, pte_t *ptep)
5514 {
5515 	if (vma->vm_flags & VM_WRITE)
5516 		set_huge_ptep_writable(vma, address, ptep);
5517 }
5518 
5519 bool is_hugetlb_entry_migration(pte_t pte)
5520 {
5521 	swp_entry_t swp;
5522 
5523 	if (huge_pte_none(pte) || pte_present(pte))
5524 		return false;
5525 	swp = pte_to_swp_entry(pte);
5526 	if (is_migration_entry(swp))
5527 		return true;
5528 	else
5529 		return false;
5530 }
5531 
5532 bool is_hugetlb_entry_hwpoisoned(pte_t pte)
5533 {
5534 	swp_entry_t swp;
5535 
5536 	if (huge_pte_none(pte) || pte_present(pte))
5537 		return false;
5538 	swp = pte_to_swp_entry(pte);
5539 	if (is_hwpoison_entry(swp))
5540 		return true;
5541 	else
5542 		return false;
5543 }
5544 
5545 static void
5546 hugetlb_install_folio(struct vm_area_struct *vma, pte_t *ptep, unsigned long addr,
5547 		      struct folio *new_folio, pte_t old, unsigned long sz)
5548 {
5549 	pte_t newpte = make_huge_pte(vma, new_folio, true);
5550 
5551 	__folio_mark_uptodate(new_folio);
5552 	hugetlb_add_new_anon_rmap(new_folio, vma, addr);
5553 	if (userfaultfd_wp(vma) && huge_pte_uffd_wp(old))
5554 		newpte = huge_pte_mkuffd_wp(newpte);
5555 	set_huge_pte_at(vma->vm_mm, addr, ptep, newpte, sz);
5556 	hugetlb_count_add(pages_per_huge_page(hstate_vma(vma)), vma->vm_mm);
5557 	folio_set_hugetlb_migratable(new_folio);
5558 }
5559 
5560 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
5561 			    struct vm_area_struct *dst_vma,
5562 			    struct vm_area_struct *src_vma)
5563 {
5564 	pte_t *src_pte, *dst_pte, entry;
5565 	struct folio *pte_folio;
5566 	unsigned long addr;
5567 	bool cow = is_cow_mapping(src_vma->vm_flags);
5568 	struct hstate *h = hstate_vma(src_vma);
5569 	unsigned long sz = huge_page_size(h);
5570 	unsigned long npages = pages_per_huge_page(h);
5571 	struct mmu_notifier_range range;
5572 	unsigned long last_addr_mask;
5573 	int ret = 0;
5574 
5575 	if (cow) {
5576 		mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, src,
5577 					src_vma->vm_start,
5578 					src_vma->vm_end);
5579 		mmu_notifier_invalidate_range_start(&range);
5580 		vma_assert_write_locked(src_vma);
5581 		raw_write_seqcount_begin(&src->write_protect_seq);
5582 	} else {
5583 		/*
5584 		 * For shared mappings the vma lock must be held before
5585 		 * calling hugetlb_walk() in the src vma. Otherwise, the
5586 		 * returned ptep could go away if part of a shared pmd and
5587 		 * another thread calls huge_pmd_unshare.
5588 		 */
5589 		hugetlb_vma_lock_read(src_vma);
5590 	}
5591 
5592 	last_addr_mask = hugetlb_mask_last_page(h);
5593 	for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) {
5594 		spinlock_t *src_ptl, *dst_ptl;
5595 		src_pte = hugetlb_walk(src_vma, addr, sz);
5596 		if (!src_pte) {
5597 			addr |= last_addr_mask;
5598 			continue;
5599 		}
5600 		dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz);
5601 		if (!dst_pte) {
5602 			ret = -ENOMEM;
5603 			break;
5604 		}
5605 
5606 		/*
5607 		 * If the pagetables are shared don't copy or take references.
5608 		 *
5609 		 * dst_pte == src_pte is the common case of src/dest sharing.
5610 		 * However, src could have 'unshared' and dst shares with
5611 		 * another vma. So page_count of ptep page is checked instead
5612 		 * to reliably determine whether pte is shared.
5613 		 */
5614 		if (page_count(virt_to_page(dst_pte)) > 1) {
5615 			addr |= last_addr_mask;
5616 			continue;
5617 		}
5618 
5619 		dst_ptl = huge_pte_lock(h, dst, dst_pte);
5620 		src_ptl = huge_pte_lockptr(h, src, src_pte);
5621 		spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5622 		entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5623 again:
5624 		if (huge_pte_none(entry)) {
5625 			/*
5626 			 * Skip if src entry none.
5627 			 */
5628 			;
5629 		} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
5630 			if (!userfaultfd_wp(dst_vma))
5631 				entry = huge_pte_clear_uffd_wp(entry);
5632 			set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5633 		} else if (unlikely(is_hugetlb_entry_migration(entry))) {
5634 			swp_entry_t swp_entry = pte_to_swp_entry(entry);
5635 			bool uffd_wp = pte_swp_uffd_wp(entry);
5636 
5637 			if (!is_readable_migration_entry(swp_entry) && cow) {
5638 				/*
5639 				 * COW mappings require pages in both
5640 				 * parent and child to be set to read.
5641 				 */
5642 				swp_entry = make_readable_migration_entry(
5643 							swp_offset(swp_entry));
5644 				entry = swp_entry_to_pte(swp_entry);
5645 				if (userfaultfd_wp(src_vma) && uffd_wp)
5646 					entry = pte_swp_mkuffd_wp(entry);
5647 				set_huge_pte_at(src, addr, src_pte, entry, sz);
5648 			}
5649 			if (!userfaultfd_wp(dst_vma))
5650 				entry = huge_pte_clear_uffd_wp(entry);
5651 			set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5652 		} else if (unlikely(is_pte_marker(entry))) {
5653 			pte_marker marker = copy_pte_marker(
5654 				pte_to_swp_entry(entry), dst_vma);
5655 
5656 			if (marker)
5657 				set_huge_pte_at(dst, addr, dst_pte,
5658 						make_pte_marker(marker), sz);
5659 		} else {
5660 			entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5661 			pte_folio = page_folio(pte_page(entry));
5662 			folio_get(pte_folio);
5663 
5664 			/*
5665 			 * Failing to duplicate the anon rmap is a rare case
5666 			 * where we see pinned hugetlb pages while they're
5667 			 * prone to COW. We need to do the COW earlier during
5668 			 * fork.
5669 			 *
5670 			 * When pre-allocating the page or copying data, we
5671 			 * need to be without the pgtable locks since we could
5672 			 * sleep during the process.
5673 			 */
5674 			if (!folio_test_anon(pte_folio)) {
5675 				hugetlb_add_file_rmap(pte_folio);
5676 			} else if (hugetlb_try_dup_anon_rmap(pte_folio, src_vma)) {
5677 				pte_t src_pte_old = entry;
5678 				struct folio *new_folio;
5679 
5680 				spin_unlock(src_ptl);
5681 				spin_unlock(dst_ptl);
5682 				/* Do not use reserve as it's private owned */
5683 				new_folio = alloc_hugetlb_folio(dst_vma, addr, false);
5684 				if (IS_ERR(new_folio)) {
5685 					folio_put(pte_folio);
5686 					ret = PTR_ERR(new_folio);
5687 					break;
5688 				}
5689 				ret = copy_user_large_folio(new_folio, pte_folio,
5690 							    addr, dst_vma);
5691 				folio_put(pte_folio);
5692 				if (ret) {
5693 					folio_put(new_folio);
5694 					break;
5695 				}
5696 
5697 				/* Install the new hugetlb folio if src pte stable */
5698 				dst_ptl = huge_pte_lock(h, dst, dst_pte);
5699 				src_ptl = huge_pte_lockptr(h, src, src_pte);
5700 				spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5701 				entry = huge_ptep_get(src_vma->vm_mm, addr, src_pte);
5702 				if (!pte_same(src_pte_old, entry)) {
5703 					restore_reserve_on_error(h, dst_vma, addr,
5704 								new_folio);
5705 					folio_put(new_folio);
5706 					/* huge_ptep of dst_pte won't change as in child */
5707 					goto again;
5708 				}
5709 				hugetlb_install_folio(dst_vma, dst_pte, addr,
5710 						      new_folio, src_pte_old, sz);
5711 				spin_unlock(src_ptl);
5712 				spin_unlock(dst_ptl);
5713 				continue;
5714 			}
5715 
5716 			if (cow) {
5717 				/*
5718 				 * No need to notify as we are downgrading page
5719 				 * table protection not changing it to point
5720 				 * to a new page.
5721 				 *
5722 				 * See Documentation/mm/mmu_notifier.rst
5723 				 */
5724 				huge_ptep_set_wrprotect(src, addr, src_pte);
5725 				entry = huge_pte_wrprotect(entry);
5726 			}
5727 
5728 			if (!userfaultfd_wp(dst_vma))
5729 				entry = huge_pte_clear_uffd_wp(entry);
5730 
5731 			set_huge_pte_at(dst, addr, dst_pte, entry, sz);
5732 			hugetlb_count_add(npages, dst);
5733 		}
5734 		spin_unlock(src_ptl);
5735 		spin_unlock(dst_ptl);
5736 	}
5737 
5738 	if (cow) {
5739 		raw_write_seqcount_end(&src->write_protect_seq);
5740 		mmu_notifier_invalidate_range_end(&range);
5741 	} else {
5742 		hugetlb_vma_unlock_read(src_vma);
5743 	}
5744 
5745 	return ret;
5746 }
5747 
5748 static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,
5749 			  unsigned long new_addr, pte_t *src_pte, pte_t *dst_pte,
5750 			  unsigned long sz)
5751 {
5752 	bool need_clear_uffd_wp = vma_has_uffd_without_event_remap(vma);
5753 	struct hstate *h = hstate_vma(vma);
5754 	struct mm_struct *mm = vma->vm_mm;
5755 	spinlock_t *src_ptl, *dst_ptl;
5756 	pte_t pte;
5757 
5758 	dst_ptl = huge_pte_lock(h, mm, dst_pte);
5759 	src_ptl = huge_pte_lockptr(h, mm, src_pte);
5760 
5761 	/*
5762 	 * We don't have to worry about the ordering of src and dst ptlocks
5763 	 * because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock.
5764 	 */
5765 	if (src_ptl != dst_ptl)
5766 		spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
5767 
5768 	pte = huge_ptep_get_and_clear(mm, old_addr, src_pte, sz);
5769 
5770 	if (need_clear_uffd_wp && pte_marker_uffd_wp(pte))
5771 		huge_pte_clear(mm, new_addr, dst_pte, sz);
5772 	else {
5773 		if (need_clear_uffd_wp) {
5774 			if (pte_present(pte))
5775 				pte = huge_pte_clear_uffd_wp(pte);
5776 			else if (is_swap_pte(pte))
5777 				pte = pte_swp_clear_uffd_wp(pte);
5778 		}
5779 		set_huge_pte_at(mm, new_addr, dst_pte, pte, sz);
5780 	}
5781 
5782 	if (src_ptl != dst_ptl)
5783 		spin_unlock(src_ptl);
5784 	spin_unlock(dst_ptl);
5785 }
5786 
5787 int move_hugetlb_page_tables(struct vm_area_struct *vma,
5788 			     struct vm_area_struct *new_vma,
5789 			     unsigned long old_addr, unsigned long new_addr,
5790 			     unsigned long len)
5791 {
5792 	struct hstate *h = hstate_vma(vma);
5793 	struct address_space *mapping = vma->vm_file->f_mapping;
5794 	unsigned long sz = huge_page_size(h);
5795 	struct mm_struct *mm = vma->vm_mm;
5796 	unsigned long old_end = old_addr + len;
5797 	unsigned long last_addr_mask;
5798 	pte_t *src_pte, *dst_pte;
5799 	struct mmu_notifier_range range;
5800 	bool shared_pmd = false;
5801 
5802 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, old_addr,
5803 				old_end);
5804 	adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
5805 	/*
5806 	 * In case of shared PMDs, we should cover the maximum possible
5807 	 * range.
5808 	 */
5809 	flush_cache_range(vma, range.start, range.end);
5810 
5811 	mmu_notifier_invalidate_range_start(&range);
5812 	last_addr_mask = hugetlb_mask_last_page(h);
5813 	/* Prevent race with file truncation */
5814 	hugetlb_vma_lock_write(vma);
5815 	i_mmap_lock_write(mapping);
5816 	for (; old_addr < old_end; old_addr += sz, new_addr += sz) {
5817 		src_pte = hugetlb_walk(vma, old_addr, sz);
5818 		if (!src_pte) {
5819 			old_addr |= last_addr_mask;
5820 			new_addr |= last_addr_mask;
5821 			continue;
5822 		}
5823 		if (huge_pte_none(huge_ptep_get(mm, old_addr, src_pte)))
5824 			continue;
5825 
5826 		if (huge_pmd_unshare(mm, vma, old_addr, src_pte)) {
5827 			shared_pmd = true;
5828 			old_addr |= last_addr_mask;
5829 			new_addr |= last_addr_mask;
5830 			continue;
5831 		}
5832 
5833 		dst_pte = huge_pte_alloc(mm, new_vma, new_addr, sz);
5834 		if (!dst_pte)
5835 			break;
5836 
5837 		move_huge_pte(vma, old_addr, new_addr, src_pte, dst_pte, sz);
5838 	}
5839 
5840 	if (shared_pmd)
5841 		flush_hugetlb_tlb_range(vma, range.start, range.end);
5842 	else
5843 		flush_hugetlb_tlb_range(vma, old_end - len, old_end);
5844 	mmu_notifier_invalidate_range_end(&range);
5845 	i_mmap_unlock_write(mapping);
5846 	hugetlb_vma_unlock_write(vma);
5847 
5848 	return len + old_addr - old_end;
5849 }
5850 
5851 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
5852 			    unsigned long start, unsigned long end,
5853 			    struct folio *folio, zap_flags_t zap_flags)
5854 {
5855 	struct mm_struct *mm = vma->vm_mm;
5856 	const bool folio_provided = !!folio;
5857 	unsigned long address;
5858 	pte_t *ptep;
5859 	pte_t pte;
5860 	spinlock_t *ptl;
5861 	struct hstate *h = hstate_vma(vma);
5862 	unsigned long sz = huge_page_size(h);
5863 	bool adjust_reservation;
5864 	unsigned long last_addr_mask;
5865 	bool force_flush = false;
5866 
5867 	WARN_ON(!is_vm_hugetlb_page(vma));
5868 	BUG_ON(start & ~huge_page_mask(h));
5869 	BUG_ON(end & ~huge_page_mask(h));
5870 
5871 	/*
5872 	 * This is a hugetlb vma, all the pte entries should point
5873 	 * to huge page.
5874 	 */
5875 	tlb_change_page_size(tlb, sz);
5876 	tlb_start_vma(tlb, vma);
5877 
5878 	last_addr_mask = hugetlb_mask_last_page(h);
5879 	address = start;
5880 	for (; address < end; address += sz) {
5881 		ptep = hugetlb_walk(vma, address, sz);
5882 		if (!ptep) {
5883 			address |= last_addr_mask;
5884 			continue;
5885 		}
5886 
5887 		ptl = huge_pte_lock(h, mm, ptep);
5888 		if (huge_pmd_unshare(mm, vma, address, ptep)) {
5889 			spin_unlock(ptl);
5890 			tlb_flush_pmd_range(tlb, address & PUD_MASK, PUD_SIZE);
5891 			force_flush = true;
5892 			address |= last_addr_mask;
5893 			continue;
5894 		}
5895 
5896 		pte = huge_ptep_get(mm, address, ptep);
5897 		if (huge_pte_none(pte)) {
5898 			spin_unlock(ptl);
5899 			continue;
5900 		}
5901 
5902 		/*
5903 		 * Migrating hugepage or HWPoisoned hugepage is already
5904 		 * unmapped and its refcount is dropped, so just clear pte here.
5905 		 */
5906 		if (unlikely(!pte_present(pte))) {
5907 			/*
5908 			 * If the pte was wr-protected by uffd-wp in any of the
5909 			 * swap forms, meanwhile the caller does not want to
5910 			 * drop the uffd-wp bit in this zap, then replace the
5911 			 * pte with a marker.
5912 			 */
5913 			if (pte_swp_uffd_wp_any(pte) &&
5914 			    !(zap_flags & ZAP_FLAG_DROP_MARKER))
5915 				set_huge_pte_at(mm, address, ptep,
5916 						make_pte_marker(PTE_MARKER_UFFD_WP),
5917 						sz);
5918 			else
5919 				huge_pte_clear(mm, address, ptep, sz);
5920 			spin_unlock(ptl);
5921 			continue;
5922 		}
5923 
5924 		/*
5925 		 * If a folio is supplied, it is because a specific
5926 		 * folio is being unmapped, not a range. Ensure the folio we
5927 		 * are about to unmap is the actual folio of interest.
5928 		 */
5929 		if (folio_provided) {
5930 			if (folio != page_folio(pte_page(pte))) {
5931 				spin_unlock(ptl);
5932 				continue;
5933 			}
5934 			/*
5935 			 * Mark the VMA as having unmapped its page so that
5936 			 * future faults in this VMA will fail rather than
5937 			 * looking like data was lost
5938 			 */
5939 			set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
5940 		} else {
5941 			folio = page_folio(pte_page(pte));
5942 		}
5943 
5944 		pte = huge_ptep_get_and_clear(mm, address, ptep, sz);
5945 		tlb_remove_huge_tlb_entry(h, tlb, ptep, address);
5946 		if (huge_pte_dirty(pte))
5947 			folio_mark_dirty(folio);
5948 		/* Leave a uffd-wp pte marker if needed */
5949 		if (huge_pte_uffd_wp(pte) &&
5950 		    !(zap_flags & ZAP_FLAG_DROP_MARKER))
5951 			set_huge_pte_at(mm, address, ptep,
5952 					make_pte_marker(PTE_MARKER_UFFD_WP),
5953 					sz);
5954 		hugetlb_count_sub(pages_per_huge_page(h), mm);
5955 		hugetlb_remove_rmap(folio);
5956 		spin_unlock(ptl);
5957 
5958 		/*
5959 		 * Restore the reservation for anonymous page, otherwise the
5960 		 * backing page could be stolen by someone.
5961 		 * If there we are freeing a surplus, do not set the restore
5962 		 * reservation bit.
5963 		 */
5964 		adjust_reservation = false;
5965 
5966 		spin_lock_irq(&hugetlb_lock);
5967 		if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
5968 		    folio_test_anon(folio)) {
5969 			folio_set_hugetlb_restore_reserve(folio);
5970 			/* Reservation to be adjusted after the spin lock */
5971 			adjust_reservation = true;
5972 		}
5973 		spin_unlock_irq(&hugetlb_lock);
5974 
5975 		/*
5976 		 * Adjust the reservation for the region that will have the
5977 		 * reserve restored. Keep in mind that vma_needs_reservation() changes
5978 		 * resv->adds_in_progress if it succeeds. If this is not done,
5979 		 * do_exit() will not see it, and will keep the reservation
5980 		 * forever.
5981 		 */
5982 		if (adjust_reservation) {
5983 			int rc = vma_needs_reservation(h, vma, address);
5984 
5985 			if (rc < 0)
5986 				/* Pressumably allocate_file_region_entries failed
5987 				 * to allocate a file_region struct. Clear
5988 				 * hugetlb_restore_reserve so that global reserve
5989 				 * count will not be incremented by free_huge_folio.
5990 				 * Act as if we consumed the reservation.
5991 				 */
5992 				folio_clear_hugetlb_restore_reserve(folio);
5993 			else if (rc)
5994 				vma_add_reservation(h, vma, address);
5995 		}
5996 
5997 		tlb_remove_page_size(tlb, folio_page(folio, 0),
5998 				     folio_size(folio));
5999 		/*
6000 		 * If we were instructed to unmap a specific folio, we're done.
6001 		 */
6002 		if (folio_provided)
6003 			break;
6004 	}
6005 	tlb_end_vma(tlb, vma);
6006 
6007 	/*
6008 	 * If we unshared PMDs, the TLB flush was not recorded in mmu_gather. We
6009 	 * could defer the flush until now, since by holding i_mmap_rwsem we
6010 	 * guaranteed that the last refernece would not be dropped. But we must
6011 	 * do the flushing before we return, as otherwise i_mmap_rwsem will be
6012 	 * dropped and the last reference to the shared PMDs page might be
6013 	 * dropped as well.
6014 	 *
6015 	 * In theory we could defer the freeing of the PMD pages as well, but
6016 	 * huge_pmd_unshare() relies on the exact page_count for the PMD page to
6017 	 * detect sharing, so we cannot defer the release of the page either.
6018 	 * Instead, do flush now.
6019 	 */
6020 	if (force_flush)
6021 		tlb_flush_mmu_tlbonly(tlb);
6022 }
6023 
6024 void __hugetlb_zap_begin(struct vm_area_struct *vma,
6025 			 unsigned long *start, unsigned long *end)
6026 {
6027 	if (!vma->vm_file)	/* hugetlbfs_file_mmap error */
6028 		return;
6029 
6030 	adjust_range_if_pmd_sharing_possible(vma, start, end);
6031 	hugetlb_vma_lock_write(vma);
6032 	if (vma->vm_file)
6033 		i_mmap_lock_write(vma->vm_file->f_mapping);
6034 }
6035 
6036 void __hugetlb_zap_end(struct vm_area_struct *vma,
6037 		       struct zap_details *details)
6038 {
6039 	zap_flags_t zap_flags = details ? details->zap_flags : 0;
6040 
6041 	if (!vma->vm_file)	/* hugetlbfs_file_mmap error */
6042 		return;
6043 
6044 	if (zap_flags & ZAP_FLAG_UNMAP) {	/* final unmap */
6045 		/*
6046 		 * Unlock and free the vma lock before releasing i_mmap_rwsem.
6047 		 * When the vma_lock is freed, this makes the vma ineligible
6048 		 * for pmd sharing.  And, i_mmap_rwsem is required to set up
6049 		 * pmd sharing.  This is important as page tables for this
6050 		 * unmapped range will be asynchrously deleted.  If the page
6051 		 * tables are shared, there will be issues when accessed by
6052 		 * someone else.
6053 		 */
6054 		__hugetlb_vma_unlock_write_free(vma);
6055 	} else {
6056 		hugetlb_vma_unlock_write(vma);
6057 	}
6058 
6059 	if (vma->vm_file)
6060 		i_mmap_unlock_write(vma->vm_file->f_mapping);
6061 }
6062 
6063 void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
6064 			  unsigned long end, struct folio *folio,
6065 			  zap_flags_t zap_flags)
6066 {
6067 	struct mmu_notifier_range range;
6068 	struct mmu_gather tlb;
6069 
6070 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
6071 				start, end);
6072 	adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
6073 	mmu_notifier_invalidate_range_start(&range);
6074 	tlb_gather_mmu(&tlb, vma->vm_mm);
6075 
6076 	__unmap_hugepage_range(&tlb, vma, start, end,
6077 			       folio, zap_flags);
6078 
6079 	mmu_notifier_invalidate_range_end(&range);
6080 	tlb_finish_mmu(&tlb);
6081 }
6082 
6083 /*
6084  * This is called when the original mapper is failing to COW a MAP_PRIVATE
6085  * mapping it owns the reserve page for. The intention is to unmap the page
6086  * from other VMAs and let the children be SIGKILLed if they are faulting the
6087  * same region.
6088  */
6089 static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
6090 			      struct folio *folio, unsigned long address)
6091 {
6092 	struct hstate *h = hstate_vma(vma);
6093 	struct vm_area_struct *iter_vma;
6094 	struct address_space *mapping;
6095 	pgoff_t pgoff;
6096 
6097 	/*
6098 	 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
6099 	 * from page cache lookup which is in HPAGE_SIZE units.
6100 	 */
6101 	address = address & huge_page_mask(h);
6102 	pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
6103 			vma->vm_pgoff;
6104 	mapping = vma->vm_file->f_mapping;
6105 
6106 	/*
6107 	 * Take the mapping lock for the duration of the table walk. As
6108 	 * this mapping should be shared between all the VMAs,
6109 	 * __unmap_hugepage_range() is called as the lock is already held
6110 	 */
6111 	i_mmap_lock_write(mapping);
6112 	vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
6113 		/* Do not unmap the current VMA */
6114 		if (iter_vma == vma)
6115 			continue;
6116 
6117 		/*
6118 		 * Shared VMAs have their own reserves and do not affect
6119 		 * MAP_PRIVATE accounting but it is possible that a shared
6120 		 * VMA is using the same page so check and skip such VMAs.
6121 		 */
6122 		if (iter_vma->vm_flags & VM_MAYSHARE)
6123 			continue;
6124 
6125 		/*
6126 		 * Unmap the page from other VMAs without their own reserves.
6127 		 * They get marked to be SIGKILLed if they fault in these
6128 		 * areas. This is because a future no-page fault on this VMA
6129 		 * could insert a zeroed page instead of the data existing
6130 		 * from the time of fork. This would look like data corruption
6131 		 */
6132 		if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
6133 			unmap_hugepage_range(iter_vma, address,
6134 					     address + huge_page_size(h),
6135 					     folio, 0);
6136 	}
6137 	i_mmap_unlock_write(mapping);
6138 }
6139 
6140 /*
6141  * hugetlb_wp() should be called with page lock of the original hugepage held.
6142  * Called with hugetlb_fault_mutex_table held and pte_page locked so we
6143  * cannot race with other handlers or page migration.
6144  * Keep the pte_same checks anyway to make transition from the mutex easier.
6145  */
6146 static vm_fault_t hugetlb_wp(struct vm_fault *vmf)
6147 {
6148 	struct vm_area_struct *vma = vmf->vma;
6149 	struct mm_struct *mm = vma->vm_mm;
6150 	const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
6151 	pte_t pte = huge_ptep_get(mm, vmf->address, vmf->pte);
6152 	struct hstate *h = hstate_vma(vma);
6153 	struct folio *old_folio;
6154 	struct folio *new_folio;
6155 	bool cow_from_owner = 0;
6156 	vm_fault_t ret = 0;
6157 	struct mmu_notifier_range range;
6158 
6159 	/*
6160 	 * Never handle CoW for uffd-wp protected pages.  It should be only
6161 	 * handled when the uffd-wp protection is removed.
6162 	 *
6163 	 * Note that only the CoW optimization path (in hugetlb_no_page())
6164 	 * can trigger this, because hugetlb_fault() will always resolve
6165 	 * uffd-wp bit first.
6166 	 */
6167 	if (!unshare && huge_pte_uffd_wp(pte))
6168 		return 0;
6169 
6170 	/* Let's take out MAP_SHARED mappings first. */
6171 	if (vma->vm_flags & VM_MAYSHARE) {
6172 		set_huge_ptep_writable(vma, vmf->address, vmf->pte);
6173 		return 0;
6174 	}
6175 
6176 	old_folio = page_folio(pte_page(pte));
6177 
6178 	delayacct_wpcopy_start();
6179 
6180 retry_avoidcopy:
6181 	/*
6182 	 * If no-one else is actually using this page, we're the exclusive
6183 	 * owner and can reuse this page.
6184 	 *
6185 	 * Note that we don't rely on the (safer) folio refcount here, because
6186 	 * copying the hugetlb folio when there are unexpected (temporary)
6187 	 * folio references could harm simple fork()+exit() users when
6188 	 * we run out of free hugetlb folios: we would have to kill processes
6189 	 * in scenarios that used to work. As a side effect, there can still
6190 	 * be leaks between processes, for example, with FOLL_GET users.
6191 	 */
6192 	if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) {
6193 		if (!PageAnonExclusive(&old_folio->page)) {
6194 			folio_move_anon_rmap(old_folio, vma);
6195 			SetPageAnonExclusive(&old_folio->page);
6196 		}
6197 		if (likely(!unshare))
6198 			set_huge_ptep_maybe_writable(vma, vmf->address,
6199 						     vmf->pte);
6200 
6201 		delayacct_wpcopy_end();
6202 		return 0;
6203 	}
6204 	VM_BUG_ON_PAGE(folio_test_anon(old_folio) &&
6205 		       PageAnonExclusive(&old_folio->page), &old_folio->page);
6206 
6207 	/*
6208 	 * If the process that created a MAP_PRIVATE mapping is about to perform
6209 	 * a COW due to a shared page count, attempt to satisfy the allocation
6210 	 * without using the existing reserves.
6211 	 * In order to determine where this is a COW on a MAP_PRIVATE mapping it
6212 	 * is enough to check whether the old_folio is anonymous. This means that
6213 	 * the reserve for this address was consumed. If reserves were used, a
6214 	 * partial faulted mapping at the fime of fork() could consume its reserves
6215 	 * on COW instead of the full address range.
6216 	 */
6217 	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
6218 	    folio_test_anon(old_folio))
6219 		cow_from_owner = true;
6220 
6221 	folio_get(old_folio);
6222 
6223 	/*
6224 	 * Drop page table lock as buddy allocator may be called. It will
6225 	 * be acquired again before returning to the caller, as expected.
6226 	 */
6227 	spin_unlock(vmf->ptl);
6228 	new_folio = alloc_hugetlb_folio(vma, vmf->address, cow_from_owner);
6229 
6230 	if (IS_ERR(new_folio)) {
6231 		/*
6232 		 * If a process owning a MAP_PRIVATE mapping fails to COW,
6233 		 * it is due to references held by a child and an insufficient
6234 		 * huge page pool. To guarantee the original mappers
6235 		 * reliability, unmap the page from child processes. The child
6236 		 * may get SIGKILLed if it later faults.
6237 		 */
6238 		if (cow_from_owner) {
6239 			struct address_space *mapping = vma->vm_file->f_mapping;
6240 			pgoff_t idx;
6241 			u32 hash;
6242 
6243 			folio_put(old_folio);
6244 			/*
6245 			 * Drop hugetlb_fault_mutex and vma_lock before
6246 			 * unmapping.  unmapping needs to hold vma_lock
6247 			 * in write mode.  Dropping vma_lock in read mode
6248 			 * here is OK as COW mappings do not interact with
6249 			 * PMD sharing.
6250 			 *
6251 			 * Reacquire both after unmap operation.
6252 			 */
6253 			idx = vma_hugecache_offset(h, vma, vmf->address);
6254 			hash = hugetlb_fault_mutex_hash(mapping, idx);
6255 			hugetlb_vma_unlock_read(vma);
6256 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6257 
6258 			unmap_ref_private(mm, vma, old_folio, vmf->address);
6259 
6260 			mutex_lock(&hugetlb_fault_mutex_table[hash]);
6261 			hugetlb_vma_lock_read(vma);
6262 			spin_lock(vmf->ptl);
6263 			vmf->pte = hugetlb_walk(vma, vmf->address,
6264 					huge_page_size(h));
6265 			if (likely(vmf->pte &&
6266 				   pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte)))
6267 				goto retry_avoidcopy;
6268 			/*
6269 			 * race occurs while re-acquiring page table
6270 			 * lock, and our job is done.
6271 			 */
6272 			delayacct_wpcopy_end();
6273 			return 0;
6274 		}
6275 
6276 		ret = vmf_error(PTR_ERR(new_folio));
6277 		goto out_release_old;
6278 	}
6279 
6280 	/*
6281 	 * When the original hugepage is shared one, it does not have
6282 	 * anon_vma prepared.
6283 	 */
6284 	ret = __vmf_anon_prepare(vmf);
6285 	if (unlikely(ret))
6286 		goto out_release_all;
6287 
6288 	if (copy_user_large_folio(new_folio, old_folio, vmf->real_address, vma)) {
6289 		ret = VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h));
6290 		goto out_release_all;
6291 	}
6292 	__folio_mark_uptodate(new_folio);
6293 
6294 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, vmf->address,
6295 				vmf->address + huge_page_size(h));
6296 	mmu_notifier_invalidate_range_start(&range);
6297 
6298 	/*
6299 	 * Retake the page table lock to check for racing updates
6300 	 * before the page tables are altered
6301 	 */
6302 	spin_lock(vmf->ptl);
6303 	vmf->pte = hugetlb_walk(vma, vmf->address, huge_page_size(h));
6304 	if (likely(vmf->pte && pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), pte))) {
6305 		pte_t newpte = make_huge_pte(vma, new_folio, !unshare);
6306 
6307 		/* Break COW or unshare */
6308 		huge_ptep_clear_flush(vma, vmf->address, vmf->pte);
6309 		hugetlb_remove_rmap(old_folio);
6310 		hugetlb_add_new_anon_rmap(new_folio, vma, vmf->address);
6311 		if (huge_pte_uffd_wp(pte))
6312 			newpte = huge_pte_mkuffd_wp(newpte);
6313 		set_huge_pte_at(mm, vmf->address, vmf->pte, newpte,
6314 				huge_page_size(h));
6315 		folio_set_hugetlb_migratable(new_folio);
6316 		/* Make the old page be freed below */
6317 		new_folio = old_folio;
6318 	}
6319 	spin_unlock(vmf->ptl);
6320 	mmu_notifier_invalidate_range_end(&range);
6321 out_release_all:
6322 	/*
6323 	 * No restore in case of successful pagetable update (Break COW or
6324 	 * unshare)
6325 	 */
6326 	if (new_folio != old_folio)
6327 		restore_reserve_on_error(h, vma, vmf->address, new_folio);
6328 	folio_put(new_folio);
6329 out_release_old:
6330 	folio_put(old_folio);
6331 
6332 	spin_lock(vmf->ptl); /* Caller expects lock to be held */
6333 
6334 	delayacct_wpcopy_end();
6335 	return ret;
6336 }
6337 
6338 /*
6339  * Return whether there is a pagecache page to back given address within VMA.
6340  */
6341 bool hugetlbfs_pagecache_present(struct hstate *h,
6342 				 struct vm_area_struct *vma, unsigned long address)
6343 {
6344 	struct address_space *mapping = vma->vm_file->f_mapping;
6345 	pgoff_t idx = linear_page_index(vma, address);
6346 	struct folio *folio;
6347 
6348 	folio = filemap_get_folio(mapping, idx);
6349 	if (IS_ERR(folio))
6350 		return false;
6351 	folio_put(folio);
6352 	return true;
6353 }
6354 
6355 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
6356 			   pgoff_t idx)
6357 {
6358 	struct inode *inode = mapping->host;
6359 	struct hstate *h = hstate_inode(inode);
6360 	int err;
6361 
6362 	idx <<= huge_page_order(h);
6363 	__folio_set_locked(folio);
6364 	err = __filemap_add_folio(mapping, folio, idx, GFP_KERNEL, NULL);
6365 
6366 	if (unlikely(err)) {
6367 		__folio_clear_locked(folio);
6368 		return err;
6369 	}
6370 	folio_clear_hugetlb_restore_reserve(folio);
6371 
6372 	/*
6373 	 * mark folio dirty so that it will not be removed from cache/file
6374 	 * by non-hugetlbfs specific code paths.
6375 	 */
6376 	folio_mark_dirty(folio);
6377 
6378 	spin_lock(&inode->i_lock);
6379 	inode->i_blocks += blocks_per_huge_page(h);
6380 	spin_unlock(&inode->i_lock);
6381 	return 0;
6382 }
6383 
6384 static inline vm_fault_t hugetlb_handle_userfault(struct vm_fault *vmf,
6385 						  struct address_space *mapping,
6386 						  unsigned long reason)
6387 {
6388 	u32 hash;
6389 
6390 	/*
6391 	 * vma_lock and hugetlb_fault_mutex must be dropped before handling
6392 	 * userfault. Also mmap_lock could be dropped due to handling
6393 	 * userfault, any vma operation should be careful from here.
6394 	 */
6395 	hugetlb_vma_unlock_read(vmf->vma);
6396 	hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
6397 	mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6398 	return handle_userfault(vmf, reason);
6399 }
6400 
6401 /*
6402  * Recheck pte with pgtable lock.  Returns true if pte didn't change, or
6403  * false if pte changed or is changing.
6404  */
6405 static bool hugetlb_pte_stable(struct hstate *h, struct mm_struct *mm, unsigned long addr,
6406 			       pte_t *ptep, pte_t old_pte)
6407 {
6408 	spinlock_t *ptl;
6409 	bool same;
6410 
6411 	ptl = huge_pte_lock(h, mm, ptep);
6412 	same = pte_same(huge_ptep_get(mm, addr, ptep), old_pte);
6413 	spin_unlock(ptl);
6414 
6415 	return same;
6416 }
6417 
6418 static vm_fault_t hugetlb_no_page(struct address_space *mapping,
6419 			struct vm_fault *vmf)
6420 {
6421 	u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
6422 	bool new_folio, new_anon_folio = false;
6423 	struct vm_area_struct *vma = vmf->vma;
6424 	struct mm_struct *mm = vma->vm_mm;
6425 	struct hstate *h = hstate_vma(vma);
6426 	vm_fault_t ret = VM_FAULT_SIGBUS;
6427 	bool folio_locked = true;
6428 	struct folio *folio;
6429 	unsigned long size;
6430 	pte_t new_pte;
6431 
6432 	/*
6433 	 * Currently, we are forced to kill the process in the event the
6434 	 * original mapper has unmapped pages from the child due to a failed
6435 	 * COW/unsharing. Warn that such a situation has occurred as it may not
6436 	 * be obvious.
6437 	 */
6438 	if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
6439 		pr_warn_ratelimited("PID %d killed due to inadequate hugepage pool\n",
6440 			   current->pid);
6441 		goto out;
6442 	}
6443 
6444 	/*
6445 	 * Use page lock to guard against racing truncation
6446 	 * before we get page_table_lock.
6447 	 */
6448 	new_folio = false;
6449 	folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff);
6450 	if (IS_ERR(folio)) {
6451 		size = i_size_read(mapping->host) >> huge_page_shift(h);
6452 		if (vmf->pgoff >= size)
6453 			goto out;
6454 		/* Check for page in userfault range */
6455 		if (userfaultfd_missing(vma)) {
6456 			/*
6457 			 * Since hugetlb_no_page() was examining pte
6458 			 * without pgtable lock, we need to re-test under
6459 			 * lock because the pte may not be stable and could
6460 			 * have changed from under us.  Try to detect
6461 			 * either changed or during-changing ptes and retry
6462 			 * properly when needed.
6463 			 *
6464 			 * Note that userfaultfd is actually fine with
6465 			 * false positives (e.g. caused by pte changed),
6466 			 * but not wrong logical events (e.g. caused by
6467 			 * reading a pte during changing).  The latter can
6468 			 * confuse the userspace, so the strictness is very
6469 			 * much preferred.  E.g., MISSING event should
6470 			 * never happen on the page after UFFDIO_COPY has
6471 			 * correctly installed the page and returned.
6472 			 */
6473 			if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) {
6474 				ret = 0;
6475 				goto out;
6476 			}
6477 
6478 			return hugetlb_handle_userfault(vmf, mapping,
6479 							VM_UFFD_MISSING);
6480 		}
6481 
6482 		if (!(vma->vm_flags & VM_MAYSHARE)) {
6483 			ret = __vmf_anon_prepare(vmf);
6484 			if (unlikely(ret))
6485 				goto out;
6486 		}
6487 
6488 		folio = alloc_hugetlb_folio(vma, vmf->address, false);
6489 		if (IS_ERR(folio)) {
6490 			/*
6491 			 * Returning error will result in faulting task being
6492 			 * sent SIGBUS.  The hugetlb fault mutex prevents two
6493 			 * tasks from racing to fault in the same page which
6494 			 * could result in false unable to allocate errors.
6495 			 * Page migration does not take the fault mutex, but
6496 			 * does a clear then write of pte's under page table
6497 			 * lock.  Page fault code could race with migration,
6498 			 * notice the clear pte and try to allocate a page
6499 			 * here.  Before returning error, get ptl and make
6500 			 * sure there really is no pte entry.
6501 			 */
6502 			if (hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte))
6503 				ret = vmf_error(PTR_ERR(folio));
6504 			else
6505 				ret = 0;
6506 			goto out;
6507 		}
6508 		folio_zero_user(folio, vmf->real_address);
6509 		__folio_mark_uptodate(folio);
6510 		new_folio = true;
6511 
6512 		if (vma->vm_flags & VM_MAYSHARE) {
6513 			int err = hugetlb_add_to_page_cache(folio, mapping,
6514 							vmf->pgoff);
6515 			if (err) {
6516 				/*
6517 				 * err can't be -EEXIST which implies someone
6518 				 * else consumed the reservation since hugetlb
6519 				 * fault mutex is held when add a hugetlb page
6520 				 * to the page cache. So it's safe to call
6521 				 * restore_reserve_on_error() here.
6522 				 */
6523 				restore_reserve_on_error(h, vma, vmf->address,
6524 							folio);
6525 				folio_put(folio);
6526 				ret = VM_FAULT_SIGBUS;
6527 				goto out;
6528 			}
6529 		} else {
6530 			new_anon_folio = true;
6531 			folio_lock(folio);
6532 		}
6533 	} else {
6534 		/*
6535 		 * If memory error occurs between mmap() and fault, some process
6536 		 * don't have hwpoisoned swap entry for errored virtual address.
6537 		 * So we need to block hugepage fault by PG_hwpoison bit check.
6538 		 */
6539 		if (unlikely(folio_test_hwpoison(folio))) {
6540 			ret = VM_FAULT_HWPOISON_LARGE |
6541 				VM_FAULT_SET_HINDEX(hstate_index(h));
6542 			goto backout_unlocked;
6543 		}
6544 
6545 		/* Check for page in userfault range. */
6546 		if (userfaultfd_minor(vma)) {
6547 			folio_unlock(folio);
6548 			folio_put(folio);
6549 			/* See comment in userfaultfd_missing() block above */
6550 			if (!hugetlb_pte_stable(h, mm, vmf->address, vmf->pte, vmf->orig_pte)) {
6551 				ret = 0;
6552 				goto out;
6553 			}
6554 			return hugetlb_handle_userfault(vmf, mapping,
6555 							VM_UFFD_MINOR);
6556 		}
6557 	}
6558 
6559 	/*
6560 	 * If we are going to COW a private mapping later, we examine the
6561 	 * pending reservations for this page now. This will ensure that
6562 	 * any allocations necessary to record that reservation occur outside
6563 	 * the spinlock.
6564 	 */
6565 	if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
6566 		if (vma_needs_reservation(h, vma, vmf->address) < 0) {
6567 			ret = VM_FAULT_OOM;
6568 			goto backout_unlocked;
6569 		}
6570 		/* Just decrements count, does not deallocate */
6571 		vma_end_reservation(h, vma, vmf->address);
6572 	}
6573 
6574 	vmf->ptl = huge_pte_lock(h, mm, vmf->pte);
6575 	ret = 0;
6576 	/* If pte changed from under us, retry */
6577 	if (!pte_same(huge_ptep_get(mm, vmf->address, vmf->pte), vmf->orig_pte))
6578 		goto backout;
6579 
6580 	if (new_anon_folio)
6581 		hugetlb_add_new_anon_rmap(folio, vma, vmf->address);
6582 	else
6583 		hugetlb_add_file_rmap(folio);
6584 	new_pte = make_huge_pte(vma, folio, vma->vm_flags & VM_SHARED);
6585 	/*
6586 	 * If this pte was previously wr-protected, keep it wr-protected even
6587 	 * if populated.
6588 	 */
6589 	if (unlikely(pte_marker_uffd_wp(vmf->orig_pte)))
6590 		new_pte = huge_pte_mkuffd_wp(new_pte);
6591 	set_huge_pte_at(mm, vmf->address, vmf->pte, new_pte, huge_page_size(h));
6592 
6593 	hugetlb_count_add(pages_per_huge_page(h), mm);
6594 	if ((vmf->flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
6595 		/*
6596 		 * No need to keep file folios locked. See comment in
6597 		 * hugetlb_fault().
6598 		 */
6599 		if (!new_anon_folio) {
6600 			folio_locked = false;
6601 			folio_unlock(folio);
6602 		}
6603 		/* Optimization, do the COW without a second fault */
6604 		ret = hugetlb_wp(vmf);
6605 	}
6606 
6607 	spin_unlock(vmf->ptl);
6608 
6609 	/*
6610 	 * Only set hugetlb_migratable in newly allocated pages.  Existing pages
6611 	 * found in the pagecache may not have hugetlb_migratable if they have
6612 	 * been isolated for migration.
6613 	 */
6614 	if (new_folio)
6615 		folio_set_hugetlb_migratable(folio);
6616 
6617 	if (folio_locked)
6618 		folio_unlock(folio);
6619 out:
6620 	hugetlb_vma_unlock_read(vma);
6621 
6622 	/*
6623 	 * We must check to release the per-VMA lock. __vmf_anon_prepare() is
6624 	 * the only way ret can be set to VM_FAULT_RETRY.
6625 	 */
6626 	if (unlikely(ret & VM_FAULT_RETRY))
6627 		vma_end_read(vma);
6628 
6629 	mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6630 	return ret;
6631 
6632 backout:
6633 	spin_unlock(vmf->ptl);
6634 backout_unlocked:
6635 	/* We only need to restore reservations for private mappings */
6636 	if (new_anon_folio)
6637 		restore_reserve_on_error(h, vma, vmf->address, folio);
6638 
6639 	folio_unlock(folio);
6640 	folio_put(folio);
6641 	goto out;
6642 }
6643 
6644 #ifdef CONFIG_SMP
6645 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
6646 {
6647 	unsigned long key[2];
6648 	u32 hash;
6649 
6650 	key[0] = (unsigned long) mapping;
6651 	key[1] = idx;
6652 
6653 	hash = jhash2((u32 *)&key, sizeof(key)/(sizeof(u32)), 0);
6654 
6655 	return hash & (num_fault_mutexes - 1);
6656 }
6657 #else
6658 /*
6659  * For uniprocessor systems we always use a single mutex, so just
6660  * return 0 and avoid the hashing overhead.
6661  */
6662 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx)
6663 {
6664 	return 0;
6665 }
6666 #endif
6667 
6668 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
6669 			unsigned long address, unsigned int flags)
6670 {
6671 	vm_fault_t ret;
6672 	u32 hash;
6673 	struct folio *folio = NULL;
6674 	struct hstate *h = hstate_vma(vma);
6675 	struct address_space *mapping;
6676 	bool need_wait_lock = false;
6677 	struct vm_fault vmf = {
6678 		.vma = vma,
6679 		.address = address & huge_page_mask(h),
6680 		.real_address = address,
6681 		.flags = flags,
6682 		.pgoff = vma_hugecache_offset(h, vma,
6683 				address & huge_page_mask(h)),
6684 		/* TODO: Track hugetlb faults using vm_fault */
6685 
6686 		/*
6687 		 * Some fields may not be initialized, be careful as it may
6688 		 * be hard to debug if called functions make assumptions
6689 		 */
6690 	};
6691 
6692 	/*
6693 	 * Serialize hugepage allocation and instantiation, so that we don't
6694 	 * get spurious allocation failures if two CPUs race to instantiate
6695 	 * the same page in the page cache.
6696 	 */
6697 	mapping = vma->vm_file->f_mapping;
6698 	hash = hugetlb_fault_mutex_hash(mapping, vmf.pgoff);
6699 	mutex_lock(&hugetlb_fault_mutex_table[hash]);
6700 
6701 	/*
6702 	 * Acquire vma lock before calling huge_pte_alloc and hold
6703 	 * until finished with vmf.pte.  This prevents huge_pmd_unshare from
6704 	 * being called elsewhere and making the vmf.pte no longer valid.
6705 	 */
6706 	hugetlb_vma_lock_read(vma);
6707 	vmf.pte = huge_pte_alloc(mm, vma, vmf.address, huge_page_size(h));
6708 	if (!vmf.pte) {
6709 		hugetlb_vma_unlock_read(vma);
6710 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6711 		return VM_FAULT_OOM;
6712 	}
6713 
6714 	vmf.orig_pte = huge_ptep_get(mm, vmf.address, vmf.pte);
6715 	if (huge_pte_none_mostly(vmf.orig_pte)) {
6716 		if (is_pte_marker(vmf.orig_pte)) {
6717 			pte_marker marker =
6718 				pte_marker_get(pte_to_swp_entry(vmf.orig_pte));
6719 
6720 			if (marker & PTE_MARKER_POISONED) {
6721 				ret = VM_FAULT_HWPOISON_LARGE |
6722 				      VM_FAULT_SET_HINDEX(hstate_index(h));
6723 				goto out_mutex;
6724 			} else if (WARN_ON_ONCE(marker & PTE_MARKER_GUARD)) {
6725 				/* This isn't supported in hugetlb. */
6726 				ret = VM_FAULT_SIGSEGV;
6727 				goto out_mutex;
6728 			}
6729 		}
6730 
6731 		/*
6732 		 * Other PTE markers should be handled the same way as none PTE.
6733 		 *
6734 		 * hugetlb_no_page will drop vma lock and hugetlb fault
6735 		 * mutex internally, which make us return immediately.
6736 		 */
6737 		return hugetlb_no_page(mapping, &vmf);
6738 	}
6739 
6740 	ret = 0;
6741 
6742 	/* Not present, either a migration or a hwpoisoned entry */
6743 	if (!pte_present(vmf.orig_pte)) {
6744 		if (is_hugetlb_entry_migration(vmf.orig_pte)) {
6745 			/*
6746 			 * Release the hugetlb fault lock now, but retain
6747 			 * the vma lock, because it is needed to guard the
6748 			 * huge_pte_lockptr() later in
6749 			 * migration_entry_wait_huge(). The vma lock will
6750 			 * be released there.
6751 			 */
6752 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6753 			migration_entry_wait_huge(vma, vmf.address, vmf.pte);
6754 			return 0;
6755 		} else if (is_hugetlb_entry_hwpoisoned(vmf.orig_pte))
6756 			ret = VM_FAULT_HWPOISON_LARGE |
6757 			    VM_FAULT_SET_HINDEX(hstate_index(h));
6758 		goto out_mutex;
6759 	}
6760 
6761 	/*
6762 	 * If we are going to COW/unshare the mapping later, we examine the
6763 	 * pending reservations for this page now. This will ensure that any
6764 	 * allocations necessary to record that reservation occur outside the
6765 	 * spinlock.
6766 	 */
6767 	if ((flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) &&
6768 	    !(vma->vm_flags & VM_MAYSHARE) && !huge_pte_write(vmf.orig_pte)) {
6769 		if (vma_needs_reservation(h, vma, vmf.address) < 0) {
6770 			ret = VM_FAULT_OOM;
6771 			goto out_mutex;
6772 		}
6773 		/* Just decrements count, does not deallocate */
6774 		vma_end_reservation(h, vma, vmf.address);
6775 	}
6776 
6777 	vmf.ptl = huge_pte_lock(h, mm, vmf.pte);
6778 
6779 	/* Check for a racing update before calling hugetlb_wp() */
6780 	if (unlikely(!pte_same(vmf.orig_pte, huge_ptep_get(mm, vmf.address, vmf.pte))))
6781 		goto out_ptl;
6782 
6783 	/* Handle userfault-wp first, before trying to lock more pages */
6784 	if (userfaultfd_wp(vma) && huge_pte_uffd_wp(huge_ptep_get(mm, vmf.address, vmf.pte)) &&
6785 	    (flags & FAULT_FLAG_WRITE) && !huge_pte_write(vmf.orig_pte)) {
6786 		if (!userfaultfd_wp_async(vma)) {
6787 			spin_unlock(vmf.ptl);
6788 			hugetlb_vma_unlock_read(vma);
6789 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6790 			return handle_userfault(&vmf, VM_UFFD_WP);
6791 		}
6792 
6793 		vmf.orig_pte = huge_pte_clear_uffd_wp(vmf.orig_pte);
6794 		set_huge_pte_at(mm, vmf.address, vmf.pte, vmf.orig_pte,
6795 				huge_page_size(hstate_vma(vma)));
6796 		/* Fallthrough to CoW */
6797 	}
6798 
6799 	if (flags & (FAULT_FLAG_WRITE|FAULT_FLAG_UNSHARE)) {
6800 		if (!huge_pte_write(vmf.orig_pte)) {
6801 			/*
6802 			 * Anonymous folios need to be lock since hugetlb_wp()
6803 			 * checks whether we can re-use the folio exclusively
6804 			 * for us in case we are the only user of it.
6805 			 */
6806 			folio = page_folio(pte_page(vmf.orig_pte));
6807 			if (folio_test_anon(folio) && !folio_trylock(folio)) {
6808 				need_wait_lock = true;
6809 				goto out_ptl;
6810 			}
6811 			folio_get(folio);
6812 			ret = hugetlb_wp(&vmf);
6813 			if (folio_test_anon(folio))
6814 				folio_unlock(folio);
6815 			folio_put(folio);
6816 			goto out_ptl;
6817 		} else if (likely(flags & FAULT_FLAG_WRITE)) {
6818 			vmf.orig_pte = huge_pte_mkdirty(vmf.orig_pte);
6819 		}
6820 	}
6821 	vmf.orig_pte = pte_mkyoung(vmf.orig_pte);
6822 	if (huge_ptep_set_access_flags(vma, vmf.address, vmf.pte, vmf.orig_pte,
6823 						flags & FAULT_FLAG_WRITE))
6824 		update_mmu_cache(vma, vmf.address, vmf.pte);
6825 out_ptl:
6826 	spin_unlock(vmf.ptl);
6827 out_mutex:
6828 	hugetlb_vma_unlock_read(vma);
6829 
6830 	/*
6831 	 * We must check to release the per-VMA lock. __vmf_anon_prepare() in
6832 	 * hugetlb_wp() is the only way ret can be set to VM_FAULT_RETRY.
6833 	 */
6834 	if (unlikely(ret & VM_FAULT_RETRY))
6835 		vma_end_read(vma);
6836 
6837 	mutex_unlock(&hugetlb_fault_mutex_table[hash]);
6838 	/*
6839 	 * hugetlb_wp drops all the locks, but the folio lock, before trying to
6840 	 * unmap the folio from other processes. During that window, if another
6841 	 * process mapping that folio faults in, it will take the mutex and then
6842 	 * it will wait on folio_lock, causing an ABBA deadlock.
6843 	 * Use trylock instead and bail out if we fail.
6844 	 *
6845 	 * Ideally, we should hold a refcount on the folio we wait for, but we do
6846 	 * not want to use the folio after it becomes unlocked, but rather just
6847 	 * wait for it to become unlocked, so hopefully next fault successes on
6848 	 * the trylock.
6849 	 */
6850 	if (need_wait_lock)
6851 		folio_wait_locked(folio);
6852 	return ret;
6853 }
6854 
6855 #ifdef CONFIG_USERFAULTFD
6856 /*
6857  * Can probably be eliminated, but still used by hugetlb_mfill_atomic_pte().
6858  */
6859 static struct folio *alloc_hugetlb_folio_vma(struct hstate *h,
6860 		struct vm_area_struct *vma, unsigned long address)
6861 {
6862 	struct mempolicy *mpol;
6863 	nodemask_t *nodemask;
6864 	struct folio *folio;
6865 	gfp_t gfp_mask;
6866 	int node;
6867 
6868 	gfp_mask = htlb_alloc_mask(h);
6869 	node = huge_node(vma, address, gfp_mask, &mpol, &nodemask);
6870 	/*
6871 	 * This is used to allocate a temporary hugetlb to hold the copied
6872 	 * content, which will then be copied again to the final hugetlb
6873 	 * consuming a reservation. Set the alloc_fallback to false to indicate
6874 	 * that breaking the per-node hugetlb pool is not allowed in this case.
6875 	 */
6876 	folio = alloc_hugetlb_folio_nodemask(h, node, nodemask, gfp_mask, false);
6877 	mpol_cond_put(mpol);
6878 
6879 	return folio;
6880 }
6881 
6882 /*
6883  * Used by userfaultfd UFFDIO_* ioctls. Based on userfaultfd's mfill_atomic_pte
6884  * with modifications for hugetlb pages.
6885  */
6886 int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
6887 			     struct vm_area_struct *dst_vma,
6888 			     unsigned long dst_addr,
6889 			     unsigned long src_addr,
6890 			     uffd_flags_t flags,
6891 			     struct folio **foliop)
6892 {
6893 	struct mm_struct *dst_mm = dst_vma->vm_mm;
6894 	bool is_continue = uffd_flags_mode_is(flags, MFILL_ATOMIC_CONTINUE);
6895 	bool wp_enabled = (flags & MFILL_ATOMIC_WP);
6896 	struct hstate *h = hstate_vma(dst_vma);
6897 	struct address_space *mapping = dst_vma->vm_file->f_mapping;
6898 	pgoff_t idx = vma_hugecache_offset(h, dst_vma, dst_addr);
6899 	unsigned long size = huge_page_size(h);
6900 	int vm_shared = dst_vma->vm_flags & VM_SHARED;
6901 	pte_t _dst_pte;
6902 	spinlock_t *ptl;
6903 	int ret = -ENOMEM;
6904 	struct folio *folio;
6905 	bool folio_in_pagecache = false;
6906 
6907 	if (uffd_flags_mode_is(flags, MFILL_ATOMIC_POISON)) {
6908 		ptl = huge_pte_lock(h, dst_mm, dst_pte);
6909 
6910 		/* Don't overwrite any existing PTEs (even markers) */
6911 		if (!huge_pte_none(huge_ptep_get(dst_mm, dst_addr, dst_pte))) {
6912 			spin_unlock(ptl);
6913 			return -EEXIST;
6914 		}
6915 
6916 		_dst_pte = make_pte_marker(PTE_MARKER_POISONED);
6917 		set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size);
6918 
6919 		/* No need to invalidate - it was non-present before */
6920 		update_mmu_cache(dst_vma, dst_addr, dst_pte);
6921 
6922 		spin_unlock(ptl);
6923 		return 0;
6924 	}
6925 
6926 	if (is_continue) {
6927 		ret = -EFAULT;
6928 		folio = filemap_lock_hugetlb_folio(h, mapping, idx);
6929 		if (IS_ERR(folio))
6930 			goto out;
6931 		folio_in_pagecache = true;
6932 	} else if (!*foliop) {
6933 		/* If a folio already exists, then it's UFFDIO_COPY for
6934 		 * a non-missing case. Return -EEXIST.
6935 		 */
6936 		if (vm_shared &&
6937 		    hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6938 			ret = -EEXIST;
6939 			goto out;
6940 		}
6941 
6942 		folio = alloc_hugetlb_folio(dst_vma, dst_addr, false);
6943 		if (IS_ERR(folio)) {
6944 			pte_t *actual_pte = hugetlb_walk(dst_vma, dst_addr, PMD_SIZE);
6945 			if (actual_pte) {
6946 				ret = -EEXIST;
6947 				goto out;
6948 			}
6949 			ret = -ENOMEM;
6950 			goto out;
6951 		}
6952 
6953 		ret = copy_folio_from_user(folio, (const void __user *) src_addr,
6954 					   false);
6955 
6956 		/* fallback to copy_from_user outside mmap_lock */
6957 		if (unlikely(ret)) {
6958 			ret = -ENOENT;
6959 			/* Free the allocated folio which may have
6960 			 * consumed a reservation.
6961 			 */
6962 			restore_reserve_on_error(h, dst_vma, dst_addr, folio);
6963 			folio_put(folio);
6964 
6965 			/* Allocate a temporary folio to hold the copied
6966 			 * contents.
6967 			 */
6968 			folio = alloc_hugetlb_folio_vma(h, dst_vma, dst_addr);
6969 			if (!folio) {
6970 				ret = -ENOMEM;
6971 				goto out;
6972 			}
6973 			*foliop = folio;
6974 			/* Set the outparam foliop and return to the caller to
6975 			 * copy the contents outside the lock. Don't free the
6976 			 * folio.
6977 			 */
6978 			goto out;
6979 		}
6980 	} else {
6981 		if (vm_shared &&
6982 		    hugetlbfs_pagecache_present(h, dst_vma, dst_addr)) {
6983 			folio_put(*foliop);
6984 			ret = -EEXIST;
6985 			*foliop = NULL;
6986 			goto out;
6987 		}
6988 
6989 		folio = alloc_hugetlb_folio(dst_vma, dst_addr, false);
6990 		if (IS_ERR(folio)) {
6991 			folio_put(*foliop);
6992 			ret = -ENOMEM;
6993 			*foliop = NULL;
6994 			goto out;
6995 		}
6996 		ret = copy_user_large_folio(folio, *foliop, dst_addr, dst_vma);
6997 		folio_put(*foliop);
6998 		*foliop = NULL;
6999 		if (ret) {
7000 			folio_put(folio);
7001 			goto out;
7002 		}
7003 	}
7004 
7005 	/*
7006 	 * If we just allocated a new page, we need a memory barrier to ensure
7007 	 * that preceding stores to the page become visible before the
7008 	 * set_pte_at() write. The memory barrier inside __folio_mark_uptodate
7009 	 * is what we need.
7010 	 *
7011 	 * In the case where we have not allocated a new page (is_continue),
7012 	 * the page must already be uptodate. UFFDIO_CONTINUE already includes
7013 	 * an earlier smp_wmb() to ensure that prior stores will be visible
7014 	 * before the set_pte_at() write.
7015 	 */
7016 	if (!is_continue)
7017 		__folio_mark_uptodate(folio);
7018 	else
7019 		WARN_ON_ONCE(!folio_test_uptodate(folio));
7020 
7021 	/* Add shared, newly allocated pages to the page cache. */
7022 	if (vm_shared && !is_continue) {
7023 		ret = -EFAULT;
7024 		if (idx >= (i_size_read(mapping->host) >> huge_page_shift(h)))
7025 			goto out_release_nounlock;
7026 
7027 		/*
7028 		 * Serialization between remove_inode_hugepages() and
7029 		 * hugetlb_add_to_page_cache() below happens through the
7030 		 * hugetlb_fault_mutex_table that here must be hold by
7031 		 * the caller.
7032 		 */
7033 		ret = hugetlb_add_to_page_cache(folio, mapping, idx);
7034 		if (ret)
7035 			goto out_release_nounlock;
7036 		folio_in_pagecache = true;
7037 	}
7038 
7039 	ptl = huge_pte_lock(h, dst_mm, dst_pte);
7040 
7041 	ret = -EIO;
7042 	if (folio_test_hwpoison(folio))
7043 		goto out_release_unlock;
7044 
7045 	/*
7046 	 * We allow to overwrite a pte marker: consider when both MISSING|WP
7047 	 * registered, we firstly wr-protect a none pte which has no page cache
7048 	 * page backing it, then access the page.
7049 	 */
7050 	ret = -EEXIST;
7051 	if (!huge_pte_none_mostly(huge_ptep_get(dst_mm, dst_addr, dst_pte)))
7052 		goto out_release_unlock;
7053 
7054 	if (folio_in_pagecache)
7055 		hugetlb_add_file_rmap(folio);
7056 	else
7057 		hugetlb_add_new_anon_rmap(folio, dst_vma, dst_addr);
7058 
7059 	/*
7060 	 * For either: (1) CONTINUE on a non-shared VMA, or (2) UFFDIO_COPY
7061 	 * with wp flag set, don't set pte write bit.
7062 	 */
7063 	_dst_pte = make_huge_pte(dst_vma, folio,
7064 				 !wp_enabled && !(is_continue && !vm_shared));
7065 	/*
7066 	 * Always mark UFFDIO_COPY page dirty; note that this may not be
7067 	 * extremely important for hugetlbfs for now since swapping is not
7068 	 * supported, but we should still be clear in that this page cannot be
7069 	 * thrown away at will, even if write bit not set.
7070 	 */
7071 	_dst_pte = huge_pte_mkdirty(_dst_pte);
7072 	_dst_pte = pte_mkyoung(_dst_pte);
7073 
7074 	if (wp_enabled)
7075 		_dst_pte = huge_pte_mkuffd_wp(_dst_pte);
7076 
7077 	set_huge_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte, size);
7078 
7079 	hugetlb_count_add(pages_per_huge_page(h), dst_mm);
7080 
7081 	/* No need to invalidate - it was non-present before */
7082 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
7083 
7084 	spin_unlock(ptl);
7085 	if (!is_continue)
7086 		folio_set_hugetlb_migratable(folio);
7087 	if (vm_shared || is_continue)
7088 		folio_unlock(folio);
7089 	ret = 0;
7090 out:
7091 	return ret;
7092 out_release_unlock:
7093 	spin_unlock(ptl);
7094 	if (vm_shared || is_continue)
7095 		folio_unlock(folio);
7096 out_release_nounlock:
7097 	if (!folio_in_pagecache)
7098 		restore_reserve_on_error(h, dst_vma, dst_addr, folio);
7099 	folio_put(folio);
7100 	goto out;
7101 }
7102 #endif /* CONFIG_USERFAULTFD */
7103 
7104 long hugetlb_change_protection(struct vm_area_struct *vma,
7105 		unsigned long address, unsigned long end,
7106 		pgprot_t newprot, unsigned long cp_flags)
7107 {
7108 	struct mm_struct *mm = vma->vm_mm;
7109 	unsigned long start = address;
7110 	pte_t *ptep;
7111 	pte_t pte;
7112 	struct hstate *h = hstate_vma(vma);
7113 	long pages = 0, psize = huge_page_size(h);
7114 	bool shared_pmd = false;
7115 	struct mmu_notifier_range range;
7116 	unsigned long last_addr_mask;
7117 	bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
7118 	bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
7119 
7120 	/*
7121 	 * In the case of shared PMDs, the area to flush could be beyond
7122 	 * start/end.  Set range.start/range.end to cover the maximum possible
7123 	 * range if PMD sharing is possible.
7124 	 */
7125 	mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA,
7126 				0, mm, start, end);
7127 	adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
7128 
7129 	BUG_ON(address >= end);
7130 	flush_cache_range(vma, range.start, range.end);
7131 
7132 	mmu_notifier_invalidate_range_start(&range);
7133 	hugetlb_vma_lock_write(vma);
7134 	i_mmap_lock_write(vma->vm_file->f_mapping);
7135 	last_addr_mask = hugetlb_mask_last_page(h);
7136 	for (; address < end; address += psize) {
7137 		spinlock_t *ptl;
7138 		ptep = hugetlb_walk(vma, address, psize);
7139 		if (!ptep) {
7140 			if (!uffd_wp) {
7141 				address |= last_addr_mask;
7142 				continue;
7143 			}
7144 			/*
7145 			 * Userfaultfd wr-protect requires pgtable
7146 			 * pre-allocations to install pte markers.
7147 			 */
7148 			ptep = huge_pte_alloc(mm, vma, address, psize);
7149 			if (!ptep) {
7150 				pages = -ENOMEM;
7151 				break;
7152 			}
7153 		}
7154 		ptl = huge_pte_lock(h, mm, ptep);
7155 		if (huge_pmd_unshare(mm, vma, address, ptep)) {
7156 			/*
7157 			 * When uffd-wp is enabled on the vma, unshare
7158 			 * shouldn't happen at all.  Warn about it if it
7159 			 * happened due to some reason.
7160 			 */
7161 			WARN_ON_ONCE(uffd_wp || uffd_wp_resolve);
7162 			pages++;
7163 			spin_unlock(ptl);
7164 			shared_pmd = true;
7165 			address |= last_addr_mask;
7166 			continue;
7167 		}
7168 		pte = huge_ptep_get(mm, address, ptep);
7169 		if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
7170 			/* Nothing to do. */
7171 		} else if (unlikely(is_hugetlb_entry_migration(pte))) {
7172 			swp_entry_t entry = pte_to_swp_entry(pte);
7173 			struct folio *folio = pfn_swap_entry_folio(entry);
7174 			pte_t newpte = pte;
7175 
7176 			if (is_writable_migration_entry(entry)) {
7177 				if (folio_test_anon(folio))
7178 					entry = make_readable_exclusive_migration_entry(
7179 								swp_offset(entry));
7180 				else
7181 					entry = make_readable_migration_entry(
7182 								swp_offset(entry));
7183 				newpte = swp_entry_to_pte(entry);
7184 				pages++;
7185 			}
7186 
7187 			if (uffd_wp)
7188 				newpte = pte_swp_mkuffd_wp(newpte);
7189 			else if (uffd_wp_resolve)
7190 				newpte = pte_swp_clear_uffd_wp(newpte);
7191 			if (!pte_same(pte, newpte))
7192 				set_huge_pte_at(mm, address, ptep, newpte, psize);
7193 		} else if (unlikely(is_pte_marker(pte))) {
7194 			/*
7195 			 * Do nothing on a poison marker; page is
7196 			 * corrupted, permissons do not apply.  Here
7197 			 * pte_marker_uffd_wp()==true implies !poison
7198 			 * because they're mutual exclusive.
7199 			 */
7200 			if (pte_marker_uffd_wp(pte) && uffd_wp_resolve)
7201 				/* Safe to modify directly (non-present->none). */
7202 				huge_pte_clear(mm, address, ptep, psize);
7203 		} else if (!huge_pte_none(pte)) {
7204 			pte_t old_pte;
7205 			unsigned int shift = huge_page_shift(hstate_vma(vma));
7206 
7207 			old_pte = huge_ptep_modify_prot_start(vma, address, ptep);
7208 			pte = huge_pte_modify(old_pte, newprot);
7209 			pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
7210 			if (uffd_wp)
7211 				pte = huge_pte_mkuffd_wp(pte);
7212 			else if (uffd_wp_resolve)
7213 				pte = huge_pte_clear_uffd_wp(pte);
7214 			huge_ptep_modify_prot_commit(vma, address, ptep, old_pte, pte);
7215 			pages++;
7216 		} else {
7217 			/* None pte */
7218 			if (unlikely(uffd_wp))
7219 				/* Safe to modify directly (none->non-present). */
7220 				set_huge_pte_at(mm, address, ptep,
7221 						make_pte_marker(PTE_MARKER_UFFD_WP),
7222 						psize);
7223 		}
7224 		spin_unlock(ptl);
7225 	}
7226 	/*
7227 	 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
7228 	 * may have cleared our pud entry and done put_page on the page table:
7229 	 * once we release i_mmap_rwsem, another task can do the final put_page
7230 	 * and that page table be reused and filled with junk.  If we actually
7231 	 * did unshare a page of pmds, flush the range corresponding to the pud.
7232 	 */
7233 	if (shared_pmd)
7234 		flush_hugetlb_tlb_range(vma, range.start, range.end);
7235 	else
7236 		flush_hugetlb_tlb_range(vma, start, end);
7237 	/*
7238 	 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs() we are
7239 	 * downgrading page table protection not changing it to point to a new
7240 	 * page.
7241 	 *
7242 	 * See Documentation/mm/mmu_notifier.rst
7243 	 */
7244 	i_mmap_unlock_write(vma->vm_file->f_mapping);
7245 	hugetlb_vma_unlock_write(vma);
7246 	mmu_notifier_invalidate_range_end(&range);
7247 
7248 	return pages > 0 ? (pages << h->order) : pages;
7249 }
7250 
7251 /*
7252  * Update the reservation map for the range [from, to].
7253  *
7254  * Returns the number of entries that would be added to the reservation map
7255  * associated with the range [from, to].  This number is greater or equal to
7256  * zero. -EINVAL or -ENOMEM is returned in case of any errors.
7257  */
7258 
7259 long hugetlb_reserve_pages(struct inode *inode,
7260 					long from, long to,
7261 					struct vm_area_struct *vma,
7262 					vm_flags_t vm_flags)
7263 {
7264 	long chg = -1, add = -1, spool_resv, gbl_resv;
7265 	struct hstate *h = hstate_inode(inode);
7266 	struct hugepage_subpool *spool = subpool_inode(inode);
7267 	struct resv_map *resv_map;
7268 	struct hugetlb_cgroup *h_cg = NULL;
7269 	long gbl_reserve, regions_needed = 0;
7270 
7271 	/* This should never happen */
7272 	if (from > to) {
7273 		VM_WARN(1, "%s called with a negative range\n", __func__);
7274 		return -EINVAL;
7275 	}
7276 
7277 	/*
7278 	 * vma specific semaphore used for pmd sharing and fault/truncation
7279 	 * synchronization
7280 	 */
7281 	hugetlb_vma_lock_alloc(vma);
7282 
7283 	/*
7284 	 * Only apply hugepage reservation if asked. At fault time, an
7285 	 * attempt will be made for VM_NORESERVE to allocate a page
7286 	 * without using reserves
7287 	 */
7288 	if (vm_flags & VM_NORESERVE)
7289 		return 0;
7290 
7291 	/*
7292 	 * Shared mappings base their reservation on the number of pages that
7293 	 * are already allocated on behalf of the file. Private mappings need
7294 	 * to reserve the full area even if read-only as mprotect() may be
7295 	 * called to make the mapping read-write. Assume !vma is a shm mapping
7296 	 */
7297 	if (!vma || vma->vm_flags & VM_MAYSHARE) {
7298 		/*
7299 		 * resv_map can not be NULL as hugetlb_reserve_pages is only
7300 		 * called for inodes for which resv_maps were created (see
7301 		 * hugetlbfs_get_inode).
7302 		 */
7303 		resv_map = inode_resv_map(inode);
7304 
7305 		chg = region_chg(resv_map, from, to, &regions_needed);
7306 	} else {
7307 		/* Private mapping. */
7308 		resv_map = resv_map_alloc();
7309 		if (!resv_map)
7310 			goto out_err;
7311 
7312 		chg = to - from;
7313 
7314 		set_vma_resv_map(vma, resv_map);
7315 		set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
7316 	}
7317 
7318 	if (chg < 0)
7319 		goto out_err;
7320 
7321 	if (hugetlb_cgroup_charge_cgroup_rsvd(hstate_index(h),
7322 				chg * pages_per_huge_page(h), &h_cg) < 0)
7323 		goto out_err;
7324 
7325 	if (vma && !(vma->vm_flags & VM_MAYSHARE) && h_cg) {
7326 		/* For private mappings, the hugetlb_cgroup uncharge info hangs
7327 		 * of the resv_map.
7328 		 */
7329 		resv_map_set_hugetlb_cgroup_uncharge_info(resv_map, h_cg, h);
7330 	}
7331 
7332 	/*
7333 	 * There must be enough pages in the subpool for the mapping. If
7334 	 * the subpool has a minimum size, there may be some global
7335 	 * reservations already in place (gbl_reserve).
7336 	 */
7337 	gbl_reserve = hugepage_subpool_get_pages(spool, chg);
7338 	if (gbl_reserve < 0)
7339 		goto out_uncharge_cgroup;
7340 
7341 	/*
7342 	 * Check enough hugepages are available for the reservation.
7343 	 * Hand the pages back to the subpool if there are not
7344 	 */
7345 	if (hugetlb_acct_memory(h, gbl_reserve) < 0)
7346 		goto out_put_pages;
7347 
7348 	/*
7349 	 * Account for the reservations made. Shared mappings record regions
7350 	 * that have reservations as they are shared by multiple VMAs.
7351 	 * When the last VMA disappears, the region map says how much
7352 	 * the reservation was and the page cache tells how much of
7353 	 * the reservation was consumed. Private mappings are per-VMA and
7354 	 * only the consumed reservations are tracked. When the VMA
7355 	 * disappears, the original reservation is the VMA size and the
7356 	 * consumed reservations are stored in the map. Hence, nothing
7357 	 * else has to be done for private mappings here
7358 	 */
7359 	if (!vma || vma->vm_flags & VM_MAYSHARE) {
7360 		add = region_add(resv_map, from, to, regions_needed, h, h_cg);
7361 
7362 		if (unlikely(add < 0)) {
7363 			hugetlb_acct_memory(h, -gbl_reserve);
7364 			goto out_put_pages;
7365 		} else if (unlikely(chg > add)) {
7366 			/*
7367 			 * pages in this range were added to the reserve
7368 			 * map between region_chg and region_add.  This
7369 			 * indicates a race with alloc_hugetlb_folio.  Adjust
7370 			 * the subpool and reserve counts modified above
7371 			 * based on the difference.
7372 			 */
7373 			long rsv_adjust;
7374 
7375 			/*
7376 			 * hugetlb_cgroup_uncharge_cgroup_rsvd() will put the
7377 			 * reference to h_cg->css. See comment below for detail.
7378 			 */
7379 			hugetlb_cgroup_uncharge_cgroup_rsvd(
7380 				hstate_index(h),
7381 				(chg - add) * pages_per_huge_page(h), h_cg);
7382 
7383 			rsv_adjust = hugepage_subpool_put_pages(spool,
7384 								chg - add);
7385 			hugetlb_acct_memory(h, -rsv_adjust);
7386 		} else if (h_cg) {
7387 			/*
7388 			 * The file_regions will hold their own reference to
7389 			 * h_cg->css. So we should release the reference held
7390 			 * via hugetlb_cgroup_charge_cgroup_rsvd() when we are
7391 			 * done.
7392 			 */
7393 			hugetlb_cgroup_put_rsvd_cgroup(h_cg);
7394 		}
7395 	}
7396 	return chg;
7397 
7398 out_put_pages:
7399 	spool_resv = chg - gbl_reserve;
7400 	if (spool_resv) {
7401 		/* put sub pool's reservation back, chg - gbl_reserve */
7402 		gbl_resv = hugepage_subpool_put_pages(spool, spool_resv);
7403 		/*
7404 		 * subpool's reserved pages can not be put back due to race,
7405 		 * return to hstate.
7406 		 */
7407 		hugetlb_acct_memory(h, -gbl_resv);
7408 	}
7409 out_uncharge_cgroup:
7410 	hugetlb_cgroup_uncharge_cgroup_rsvd(hstate_index(h),
7411 					    chg * pages_per_huge_page(h), h_cg);
7412 out_err:
7413 	hugetlb_vma_lock_free(vma);
7414 	if (!vma || vma->vm_flags & VM_MAYSHARE)
7415 		/* Only call region_abort if the region_chg succeeded but the
7416 		 * region_add failed or didn't run.
7417 		 */
7418 		if (chg >= 0 && add < 0)
7419 			region_abort(resv_map, from, to, regions_needed);
7420 	if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
7421 		kref_put(&resv_map->refs, resv_map_release);
7422 		set_vma_resv_map(vma, NULL);
7423 	}
7424 	return chg < 0 ? chg : add < 0 ? add : -EINVAL;
7425 }
7426 
7427 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
7428 								long freed)
7429 {
7430 	struct hstate *h = hstate_inode(inode);
7431 	struct resv_map *resv_map = inode_resv_map(inode);
7432 	long chg = 0;
7433 	struct hugepage_subpool *spool = subpool_inode(inode);
7434 	long gbl_reserve;
7435 
7436 	/*
7437 	 * Since this routine can be called in the evict inode path for all
7438 	 * hugetlbfs inodes, resv_map could be NULL.
7439 	 */
7440 	if (resv_map) {
7441 		chg = region_del(resv_map, start, end);
7442 		/*
7443 		 * region_del() can fail in the rare case where a region
7444 		 * must be split and another region descriptor can not be
7445 		 * allocated.  If end == LONG_MAX, it will not fail.
7446 		 */
7447 		if (chg < 0)
7448 			return chg;
7449 	}
7450 
7451 	spin_lock(&inode->i_lock);
7452 	inode->i_blocks -= (blocks_per_huge_page(h) * freed);
7453 	spin_unlock(&inode->i_lock);
7454 
7455 	/*
7456 	 * If the subpool has a minimum size, the number of global
7457 	 * reservations to be released may be adjusted.
7458 	 *
7459 	 * Note that !resv_map implies freed == 0. So (chg - freed)
7460 	 * won't go negative.
7461 	 */
7462 	gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
7463 	hugetlb_acct_memory(h, -gbl_reserve);
7464 
7465 	return 0;
7466 }
7467 
7468 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
7469 static unsigned long page_table_shareable(struct vm_area_struct *svma,
7470 				struct vm_area_struct *vma,
7471 				unsigned long addr, pgoff_t idx)
7472 {
7473 	unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
7474 				svma->vm_start;
7475 	unsigned long sbase = saddr & PUD_MASK;
7476 	unsigned long s_end = sbase + PUD_SIZE;
7477 
7478 	/* Allow segments to share if only one is marked locked */
7479 	vm_flags_t vm_flags = vma->vm_flags & ~VM_LOCKED_MASK;
7480 	vm_flags_t svm_flags = svma->vm_flags & ~VM_LOCKED_MASK;
7481 
7482 	/*
7483 	 * match the virtual addresses, permission and the alignment of the
7484 	 * page table page.
7485 	 *
7486 	 * Also, vma_lock (vm_private_data) is required for sharing.
7487 	 */
7488 	if (pmd_index(addr) != pmd_index(saddr) ||
7489 	    vm_flags != svm_flags ||
7490 	    !range_in_vma(svma, sbase, s_end) ||
7491 	    !svma->vm_private_data)
7492 		return 0;
7493 
7494 	return saddr;
7495 }
7496 
7497 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7498 {
7499 	unsigned long start = addr & PUD_MASK;
7500 	unsigned long end = start + PUD_SIZE;
7501 
7502 #ifdef CONFIG_USERFAULTFD
7503 	if (uffd_disable_huge_pmd_share(vma))
7504 		return false;
7505 #endif
7506 	/*
7507 	 * check on proper vm_flags and page table alignment
7508 	 */
7509 	if (!(vma->vm_flags & VM_MAYSHARE))
7510 		return false;
7511 	if (!vma->vm_private_data)	/* vma lock required for sharing */
7512 		return false;
7513 	if (!range_in_vma(vma, start, end))
7514 		return false;
7515 	return true;
7516 }
7517 
7518 /*
7519  * Determine if start,end range within vma could be mapped by shared pmd.
7520  * If yes, adjust start and end to cover range associated with possible
7521  * shared pmd mappings.
7522  */
7523 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7524 				unsigned long *start, unsigned long *end)
7525 {
7526 	unsigned long v_start = ALIGN(vma->vm_start, PUD_SIZE),
7527 		v_end = ALIGN_DOWN(vma->vm_end, PUD_SIZE);
7528 
7529 	/*
7530 	 * vma needs to span at least one aligned PUD size, and the range
7531 	 * must be at least partially within in.
7532 	 */
7533 	if (!(vma->vm_flags & VM_MAYSHARE) || !(v_end > v_start) ||
7534 		(*end <= v_start) || (*start >= v_end))
7535 		return;
7536 
7537 	/* Extend the range to be PUD aligned for a worst case scenario */
7538 	if (*start > v_start)
7539 		*start = ALIGN_DOWN(*start, PUD_SIZE);
7540 
7541 	if (*end < v_end)
7542 		*end = ALIGN(*end, PUD_SIZE);
7543 }
7544 
7545 /*
7546  * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
7547  * and returns the corresponding pte. While this is not necessary for the
7548  * !shared pmd case because we can allocate the pmd later as well, it makes the
7549  * code much cleaner. pmd allocation is essential for the shared case because
7550  * pud has to be populated inside the same i_mmap_rwsem section - otherwise
7551  * racing tasks could either miss the sharing (see huge_pte_offset) or select a
7552  * bad pmd for sharing.
7553  */
7554 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7555 		      unsigned long addr, pud_t *pud)
7556 {
7557 	struct address_space *mapping = vma->vm_file->f_mapping;
7558 	pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
7559 			vma->vm_pgoff;
7560 	struct vm_area_struct *svma;
7561 	unsigned long saddr;
7562 	pte_t *spte = NULL;
7563 	pte_t *pte;
7564 
7565 	i_mmap_lock_read(mapping);
7566 	vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
7567 		if (svma == vma)
7568 			continue;
7569 
7570 		saddr = page_table_shareable(svma, vma, addr, idx);
7571 		if (saddr) {
7572 			spte = hugetlb_walk(svma, saddr,
7573 					    vma_mmu_pagesize(svma));
7574 			if (spte) {
7575 				ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
7576 				break;
7577 			}
7578 		}
7579 	}
7580 
7581 	if (!spte)
7582 		goto out;
7583 
7584 	spin_lock(&mm->page_table_lock);
7585 	if (pud_none(*pud)) {
7586 		pud_populate(mm, pud,
7587 				(pmd_t *)((unsigned long)spte & PAGE_MASK));
7588 		mm_inc_nr_pmds(mm);
7589 	} else {
7590 		ptdesc_pmd_pts_dec(virt_to_ptdesc(spte));
7591 	}
7592 	spin_unlock(&mm->page_table_lock);
7593 out:
7594 	pte = (pte_t *)pmd_alloc(mm, pud, addr);
7595 	i_mmap_unlock_read(mapping);
7596 	return pte;
7597 }
7598 
7599 /*
7600  * unmap huge page backed by shared pte.
7601  *
7602  * Called with page table lock held.
7603  *
7604  * returns: 1 successfully unmapped a shared pte page
7605  *	    0 the underlying pte page is not shared, or it is the last user
7606  */
7607 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
7608 					unsigned long addr, pte_t *ptep)
7609 {
7610 	unsigned long sz = huge_page_size(hstate_vma(vma));
7611 	pgd_t *pgd = pgd_offset(mm, addr);
7612 	p4d_t *p4d = p4d_offset(pgd, addr);
7613 	pud_t *pud = pud_offset(p4d, addr);
7614 
7615 	i_mmap_assert_write_locked(vma->vm_file->f_mapping);
7616 	hugetlb_vma_assert_locked(vma);
7617 	if (sz != PMD_SIZE)
7618 		return 0;
7619 	if (!ptdesc_pmd_pts_count(virt_to_ptdesc(ptep)))
7620 		return 0;
7621 
7622 	pud_clear(pud);
7623 	/*
7624 	 * Once our caller drops the rmap lock, some other process might be
7625 	 * using this page table as a normal, non-hugetlb page table.
7626 	 * Wait for pending gup_fast() in other threads to finish before letting
7627 	 * that happen.
7628 	 */
7629 	tlb_remove_table_sync_one();
7630 	ptdesc_pmd_pts_dec(virt_to_ptdesc(ptep));
7631 	mm_dec_nr_pmds(mm);
7632 	return 1;
7633 }
7634 
7635 #else /* !CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */
7636 
7637 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
7638 		      unsigned long addr, pud_t *pud)
7639 {
7640 	return NULL;
7641 }
7642 
7643 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
7644 				unsigned long addr, pte_t *ptep)
7645 {
7646 	return 0;
7647 }
7648 
7649 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
7650 				unsigned long *start, unsigned long *end)
7651 {
7652 }
7653 
7654 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr)
7655 {
7656 	return false;
7657 }
7658 #endif /* CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING */
7659 
7660 #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
7661 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
7662 			unsigned long addr, unsigned long sz)
7663 {
7664 	pgd_t *pgd;
7665 	p4d_t *p4d;
7666 	pud_t *pud;
7667 	pte_t *pte = NULL;
7668 
7669 	pgd = pgd_offset(mm, addr);
7670 	p4d = p4d_alloc(mm, pgd, addr);
7671 	if (!p4d)
7672 		return NULL;
7673 	pud = pud_alloc(mm, p4d, addr);
7674 	if (pud) {
7675 		if (sz == PUD_SIZE) {
7676 			pte = (pte_t *)pud;
7677 		} else {
7678 			BUG_ON(sz != PMD_SIZE);
7679 			if (want_pmd_share(vma, addr) && pud_none(*pud))
7680 				pte = huge_pmd_share(mm, vma, addr, pud);
7681 			else
7682 				pte = (pte_t *)pmd_alloc(mm, pud, addr);
7683 		}
7684 	}
7685 
7686 	if (pte) {
7687 		pte_t pteval = ptep_get_lockless(pte);
7688 
7689 		BUG_ON(pte_present(pteval) && !pte_huge(pteval));
7690 	}
7691 
7692 	return pte;
7693 }
7694 
7695 /*
7696  * huge_pte_offset() - Walk the page table to resolve the hugepage
7697  * entry at address @addr
7698  *
7699  * Return: Pointer to page table entry (PUD or PMD) for
7700  * address @addr, or NULL if a !p*d_present() entry is encountered and the
7701  * size @sz doesn't match the hugepage size at this level of the page
7702  * table.
7703  */
7704 pte_t *huge_pte_offset(struct mm_struct *mm,
7705 		       unsigned long addr, unsigned long sz)
7706 {
7707 	pgd_t *pgd;
7708 	p4d_t *p4d;
7709 	pud_t *pud;
7710 	pmd_t *pmd;
7711 
7712 	pgd = pgd_offset(mm, addr);
7713 	if (!pgd_present(*pgd))
7714 		return NULL;
7715 	p4d = p4d_offset(pgd, addr);
7716 	if (!p4d_present(*p4d))
7717 		return NULL;
7718 
7719 	pud = pud_offset(p4d, addr);
7720 	if (sz == PUD_SIZE)
7721 		/* must be pud huge, non-present or none */
7722 		return (pte_t *)pud;
7723 	if (!pud_present(*pud))
7724 		return NULL;
7725 	/* must have a valid entry and size to go further */
7726 
7727 	pmd = pmd_offset(pud, addr);
7728 	/* must be pmd huge, non-present or none */
7729 	return (pte_t *)pmd;
7730 }
7731 
7732 /*
7733  * Return a mask that can be used to update an address to the last huge
7734  * page in a page table page mapping size.  Used to skip non-present
7735  * page table entries when linearly scanning address ranges.  Architectures
7736  * with unique huge page to page table relationships can define their own
7737  * version of this routine.
7738  */
7739 unsigned long hugetlb_mask_last_page(struct hstate *h)
7740 {
7741 	unsigned long hp_size = huge_page_size(h);
7742 
7743 	if (hp_size == PUD_SIZE)
7744 		return P4D_SIZE - PUD_SIZE;
7745 	else if (hp_size == PMD_SIZE)
7746 		return PUD_SIZE - PMD_SIZE;
7747 	else
7748 		return 0UL;
7749 }
7750 
7751 #else
7752 
7753 /* See description above.  Architectures can provide their own version. */
7754 __weak unsigned long hugetlb_mask_last_page(struct hstate *h)
7755 {
7756 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
7757 	if (huge_page_size(h) == PMD_SIZE)
7758 		return PUD_SIZE - PMD_SIZE;
7759 #endif
7760 	return 0UL;
7761 }
7762 
7763 #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
7764 
7765 /**
7766  * folio_isolate_hugetlb - try to isolate an allocated hugetlb folio
7767  * @folio: the folio to isolate
7768  * @list: the list to add the folio to on success
7769  *
7770  * Isolate an allocated (refcount > 0) hugetlb folio, marking it as
7771  * isolated/non-migratable, and moving it from the active list to the
7772  * given list.
7773  *
7774  * Isolation will fail if @folio is not an allocated hugetlb folio, or if
7775  * it is already isolated/non-migratable.
7776  *
7777  * On success, an additional folio reference is taken that must be dropped
7778  * using folio_putback_hugetlb() to undo the isolation.
7779  *
7780  * Return: True if isolation worked, otherwise False.
7781  */
7782 bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list)
7783 {
7784 	bool ret = true;
7785 
7786 	spin_lock_irq(&hugetlb_lock);
7787 	if (!folio_test_hugetlb(folio) ||
7788 	    !folio_test_hugetlb_migratable(folio) ||
7789 	    !folio_try_get(folio)) {
7790 		ret = false;
7791 		goto unlock;
7792 	}
7793 	folio_clear_hugetlb_migratable(folio);
7794 	list_move_tail(&folio->lru, list);
7795 unlock:
7796 	spin_unlock_irq(&hugetlb_lock);
7797 	return ret;
7798 }
7799 
7800 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
7801 {
7802 	int ret = 0;
7803 
7804 	*hugetlb = false;
7805 	spin_lock_irq(&hugetlb_lock);
7806 	if (folio_test_hugetlb(folio)) {
7807 		*hugetlb = true;
7808 		if (folio_test_hugetlb_freed(folio))
7809 			ret = 0;
7810 		else if (folio_test_hugetlb_migratable(folio) || unpoison)
7811 			ret = folio_try_get(folio);
7812 		else
7813 			ret = -EBUSY;
7814 	}
7815 	spin_unlock_irq(&hugetlb_lock);
7816 	return ret;
7817 }
7818 
7819 int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
7820 				bool *migratable_cleared)
7821 {
7822 	int ret;
7823 
7824 	spin_lock_irq(&hugetlb_lock);
7825 	ret = __get_huge_page_for_hwpoison(pfn, flags, migratable_cleared);
7826 	spin_unlock_irq(&hugetlb_lock);
7827 	return ret;
7828 }
7829 
7830 /**
7831  * folio_putback_hugetlb - unisolate a hugetlb folio
7832  * @folio: the isolated hugetlb folio
7833  *
7834  * Putback/un-isolate the hugetlb folio that was previous isolated using
7835  * folio_isolate_hugetlb(): marking it non-isolated/migratable and putting it
7836  * back onto the active list.
7837  *
7838  * Will drop the additional folio reference obtained through
7839  * folio_isolate_hugetlb().
7840  */
7841 void folio_putback_hugetlb(struct folio *folio)
7842 {
7843 	spin_lock_irq(&hugetlb_lock);
7844 	folio_set_hugetlb_migratable(folio);
7845 	list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist);
7846 	spin_unlock_irq(&hugetlb_lock);
7847 	folio_put(folio);
7848 }
7849 
7850 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason)
7851 {
7852 	struct hstate *h = folio_hstate(old_folio);
7853 
7854 	hugetlb_cgroup_migrate(old_folio, new_folio);
7855 	folio_set_owner_migrate_reason(new_folio, reason);
7856 
7857 	/*
7858 	 * transfer temporary state of the new hugetlb folio. This is
7859 	 * reverse to other transitions because the newpage is going to
7860 	 * be final while the old one will be freed so it takes over
7861 	 * the temporary status.
7862 	 *
7863 	 * Also note that we have to transfer the per-node surplus state
7864 	 * here as well otherwise the global surplus count will not match
7865 	 * the per-node's.
7866 	 */
7867 	if (folio_test_hugetlb_temporary(new_folio)) {
7868 		int old_nid = folio_nid(old_folio);
7869 		int new_nid = folio_nid(new_folio);
7870 
7871 		folio_set_hugetlb_temporary(old_folio);
7872 		folio_clear_hugetlb_temporary(new_folio);
7873 
7874 
7875 		/*
7876 		 * There is no need to transfer the per-node surplus state
7877 		 * when we do not cross the node.
7878 		 */
7879 		if (new_nid == old_nid)
7880 			return;
7881 		spin_lock_irq(&hugetlb_lock);
7882 		if (h->surplus_huge_pages_node[old_nid]) {
7883 			h->surplus_huge_pages_node[old_nid]--;
7884 			h->surplus_huge_pages_node[new_nid]++;
7885 		}
7886 		spin_unlock_irq(&hugetlb_lock);
7887 	}
7888 
7889 	/*
7890 	 * Our old folio is isolated and has "migratable" cleared until it
7891 	 * is putback. As migration succeeded, set the new folio "migratable"
7892 	 * and add it to the active list.
7893 	 */
7894 	spin_lock_irq(&hugetlb_lock);
7895 	folio_set_hugetlb_migratable(new_folio);
7896 	list_move_tail(&new_folio->lru, &(folio_hstate(new_folio))->hugepage_activelist);
7897 	spin_unlock_irq(&hugetlb_lock);
7898 }
7899 
7900 /*
7901  * If @take_locks is false, the caller must ensure that no concurrent page table
7902  * access can happen (except for gup_fast() and hardware page walks).
7903  * If @take_locks is true, we take the hugetlb VMA lock (to lock out things like
7904  * concurrent page fault handling) and the file rmap lock.
7905  */
7906 static void hugetlb_unshare_pmds(struct vm_area_struct *vma,
7907 				   unsigned long start,
7908 				   unsigned long end,
7909 				   bool take_locks)
7910 {
7911 	struct hstate *h = hstate_vma(vma);
7912 	unsigned long sz = huge_page_size(h);
7913 	struct mm_struct *mm = vma->vm_mm;
7914 	struct mmu_notifier_range range;
7915 	unsigned long address;
7916 	spinlock_t *ptl;
7917 	pte_t *ptep;
7918 
7919 	if (!(vma->vm_flags & VM_MAYSHARE))
7920 		return;
7921 
7922 	if (start >= end)
7923 		return;
7924 
7925 	flush_cache_range(vma, start, end);
7926 	/*
7927 	 * No need to call adjust_range_if_pmd_sharing_possible(), because
7928 	 * we have already done the PUD_SIZE alignment.
7929 	 */
7930 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm,
7931 				start, end);
7932 	mmu_notifier_invalidate_range_start(&range);
7933 	if (take_locks) {
7934 		hugetlb_vma_lock_write(vma);
7935 		i_mmap_lock_write(vma->vm_file->f_mapping);
7936 	} else {
7937 		i_mmap_assert_write_locked(vma->vm_file->f_mapping);
7938 	}
7939 	for (address = start; address < end; address += PUD_SIZE) {
7940 		ptep = hugetlb_walk(vma, address, sz);
7941 		if (!ptep)
7942 			continue;
7943 		ptl = huge_pte_lock(h, mm, ptep);
7944 		huge_pmd_unshare(mm, vma, address, ptep);
7945 		spin_unlock(ptl);
7946 	}
7947 	flush_hugetlb_tlb_range(vma, start, end);
7948 	if (take_locks) {
7949 		i_mmap_unlock_write(vma->vm_file->f_mapping);
7950 		hugetlb_vma_unlock_write(vma);
7951 	}
7952 	/*
7953 	 * No need to call mmu_notifier_arch_invalidate_secondary_tlbs(), see
7954 	 * Documentation/mm/mmu_notifier.rst.
7955 	 */
7956 	mmu_notifier_invalidate_range_end(&range);
7957 }
7958 
7959 /*
7960  * This function will unconditionally remove all the shared pmd pgtable entries
7961  * within the specific vma for a hugetlbfs memory range.
7962  */
7963 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma)
7964 {
7965 	hugetlb_unshare_pmds(vma, ALIGN(vma->vm_start, PUD_SIZE),
7966 			ALIGN_DOWN(vma->vm_end, PUD_SIZE),
7967 			/* take_locks = */ true);
7968 }
7969 
7970 /*
7971  * For hugetlb, mremap() is an odd edge case - while the VMA copying is
7972  * performed, we permit both the old and new VMAs to reference the same
7973  * reservation.
7974  *
7975  * We fix this up after the operation succeeds, or if a newly allocated VMA
7976  * is closed as a result of a failure to allocate memory.
7977  */
7978 void fixup_hugetlb_reservations(struct vm_area_struct *vma)
7979 {
7980 	if (is_vm_hugetlb_page(vma))
7981 		clear_vma_resv_huge_pages(vma);
7982 }
7983