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