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