xref: /linux-6.15/include/linux/hugetlb.h (revision daa60ae6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGETLB_H
3 #define _LINUX_HUGETLB_H
4 
5 #include <linux/mm.h>
6 #include <linux/mm_types.h>
7 #include <linux/mmdebug.h>
8 #include <linux/fs.h>
9 #include <linux/hugetlb_inline.h>
10 #include <linux/cgroup.h>
11 #include <linux/page_ref.h>
12 #include <linux/list.h>
13 #include <linux/kref.h>
14 #include <linux/pgtable.h>
15 #include <linux/gfp.h>
16 #include <linux/userfaultfd_k.h>
17 
18 struct ctl_table;
19 struct user_struct;
20 struct mmu_gather;
21 struct node;
22 
23 #ifndef CONFIG_ARCH_HAS_HUGEPD
24 typedef struct { unsigned long pd; } hugepd_t;
25 #define is_hugepd(hugepd) (0)
26 #define __hugepd(x) ((hugepd_t) { (x) })
27 #endif
28 
29 #ifdef CONFIG_HUGETLB_PAGE
30 
31 #include <linux/mempolicy.h>
32 #include <linux/shm.h>
33 #include <asm/tlbflush.h>
34 
35 /*
36  * For HugeTLB page, there are more metadata to save in the struct page. But
37  * the head struct page cannot meet our needs, so we have to abuse other tail
38  * struct page to store the metadata.
39  */
40 #define __NR_USED_SUBPAGE 3
41 
42 struct hugepage_subpool {
43 	spinlock_t lock;
44 	long count;
45 	long max_hpages;	/* Maximum huge pages or -1 if no maximum. */
46 	long used_hpages;	/* Used count against maximum, includes */
47 				/* both allocated and reserved pages. */
48 	struct hstate *hstate;
49 	long min_hpages;	/* Minimum huge pages or -1 if no minimum. */
50 	long rsv_hpages;	/* Pages reserved against global pool to */
51 				/* satisfy minimum size. */
52 };
53 
54 struct resv_map {
55 	struct kref refs;
56 	spinlock_t lock;
57 	struct list_head regions;
58 	long adds_in_progress;
59 	struct list_head region_cache;
60 	long region_cache_count;
61 #ifdef CONFIG_CGROUP_HUGETLB
62 	/*
63 	 * On private mappings, the counter to uncharge reservations is stored
64 	 * here. If these fields are 0, then either the mapping is shared, or
65 	 * cgroup accounting is disabled for this resv_map.
66 	 */
67 	struct page_counter *reservation_counter;
68 	unsigned long pages_per_hpage;
69 	struct cgroup_subsys_state *css;
70 #endif
71 };
72 
73 /*
74  * Region tracking -- allows tracking of reservations and instantiated pages
75  *                    across the pages in a mapping.
76  *
77  * The region data structures are embedded into a resv_map and protected
78  * by a resv_map's lock.  The set of regions within the resv_map represent
79  * reservations for huge pages, or huge pages that have already been
80  * instantiated within the map.  The from and to elements are huge page
81  * indices into the associated mapping.  from indicates the starting index
82  * of the region.  to represents the first index past the end of  the region.
83  *
84  * For example, a file region structure with from == 0 and to == 4 represents
85  * four huge pages in a mapping.  It is important to note that the to element
86  * represents the first element past the end of the region. This is used in
87  * arithmetic as 4(to) - 0(from) = 4 huge pages in the region.
88  *
89  * Interval notation of the form [from, to) will be used to indicate that
90  * the endpoint from is inclusive and to is exclusive.
91  */
92 struct file_region {
93 	struct list_head link;
94 	long from;
95 	long to;
96 #ifdef CONFIG_CGROUP_HUGETLB
97 	/*
98 	 * On shared mappings, each reserved region appears as a struct
99 	 * file_region in resv_map. These fields hold the info needed to
100 	 * uncharge each reservation.
101 	 */
102 	struct page_counter *reservation_counter;
103 	struct cgroup_subsys_state *css;
104 #endif
105 };
106 
107 struct hugetlb_vma_lock {
108 	struct kref refs;
109 	struct rw_semaphore rw_sema;
110 	struct vm_area_struct *vma;
111 };
112 
113 extern struct resv_map *resv_map_alloc(void);
114 void resv_map_release(struct kref *ref);
115 
116 extern spinlock_t hugetlb_lock;
117 extern int hugetlb_max_hstate __read_mostly;
118 #define for_each_hstate(h) \
119 	for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
120 
121 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
122 						long min_hpages);
123 void hugepage_put_subpool(struct hugepage_subpool *spool);
124 
125 void hugetlb_dup_vma_private(struct vm_area_struct *vma);
126 void clear_vma_resv_huge_pages(struct vm_area_struct *vma);
127 int move_hugetlb_page_tables(struct vm_area_struct *vma,
128 			     struct vm_area_struct *new_vma,
129 			     unsigned long old_addr, unsigned long new_addr,
130 			     unsigned long len);
131 int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *,
132 			    struct vm_area_struct *, struct vm_area_struct *);
133 struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
134 				      unsigned long address, unsigned int flags,
135 				      unsigned int *page_mask);
136 void unmap_hugepage_range(struct vm_area_struct *,
137 			  unsigned long, unsigned long, struct page *,
138 			  zap_flags_t);
139 void __unmap_hugepage_range_final(struct mmu_gather *tlb,
140 			  struct vm_area_struct *vma,
141 			  unsigned long start, unsigned long end,
142 			  struct page *ref_page, zap_flags_t zap_flags);
143 void hugetlb_report_meminfo(struct seq_file *);
144 int hugetlb_report_node_meminfo(char *buf, int len, int nid);
145 void hugetlb_show_meminfo_node(int nid);
146 unsigned long hugetlb_total_pages(void);
147 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
148 			unsigned long address, unsigned int flags);
149 #ifdef CONFIG_USERFAULTFD
150 int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
151 			     struct vm_area_struct *dst_vma,
152 			     unsigned long dst_addr,
153 			     unsigned long src_addr,
154 			     uffd_flags_t flags,
155 			     struct folio **foliop);
156 #endif /* CONFIG_USERFAULTFD */
157 bool hugetlb_reserve_pages(struct inode *inode, long from, long to,
158 						struct vm_area_struct *vma,
159 						vm_flags_t vm_flags);
160 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
161 						long freed);
162 bool isolate_hugetlb(struct folio *folio, struct list_head *list);
163 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison);
164 int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
165 				bool *migratable_cleared);
166 void folio_putback_active_hugetlb(struct folio *folio);
167 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason);
168 void free_huge_page(struct page *page);
169 void hugetlb_fix_reserve_counts(struct inode *inode);
170 extern struct mutex *hugetlb_fault_mutex_table;
171 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx);
172 
173 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
174 		      unsigned long addr, pud_t *pud);
175 
176 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage);
177 
178 extern int sysctl_hugetlb_shm_group;
179 extern struct list_head huge_boot_pages;
180 
181 /* arch callbacks */
182 
183 #ifndef CONFIG_HIGHPTE
184 /*
185  * pte_offset_huge() and pte_alloc_huge() are helpers for those architectures
186  * which may go down to the lowest PTE level in their huge_pte_offset() and
187  * huge_pte_alloc(): to avoid reliance on pte_offset_map() without pte_unmap().
188  */
189 static inline pte_t *pte_offset_huge(pmd_t *pmd, unsigned long address)
190 {
191 	return pte_offset_kernel(pmd, address);
192 }
193 static inline pte_t *pte_alloc_huge(struct mm_struct *mm, pmd_t *pmd,
194 				    unsigned long address)
195 {
196 	return pte_alloc(mm, pmd) ? NULL : pte_offset_huge(pmd, address);
197 }
198 #endif
199 
200 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
201 			unsigned long addr, unsigned long sz);
202 /*
203  * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE.
204  * Returns the pte_t* if found, or NULL if the address is not mapped.
205  *
206  * IMPORTANT: we should normally not directly call this function, instead
207  * this is only a common interface to implement arch-specific
208  * walker. Please use hugetlb_walk() instead, because that will attempt to
209  * verify the locking for you.
210  *
211  * Since this function will walk all the pgtable pages (including not only
212  * high-level pgtable page, but also PUD entry that can be unshared
213  * concurrently for VM_SHARED), the caller of this function should be
214  * responsible of its thread safety.  One can follow this rule:
215  *
216  *  (1) For private mappings: pmd unsharing is not possible, so holding the
217  *      mmap_lock for either read or write is sufficient. Most callers
218  *      already hold the mmap_lock, so normally, no special action is
219  *      required.
220  *
221  *  (2) For shared mappings: pmd unsharing is possible (so the PUD-ranged
222  *      pgtable page can go away from under us!  It can be done by a pmd
223  *      unshare with a follow up munmap() on the other process), then we
224  *      need either:
225  *
226  *     (2.1) hugetlb vma lock read or write held, to make sure pmd unshare
227  *           won't happen upon the range (it also makes sure the pte_t we
228  *           read is the right and stable one), or,
229  *
230  *     (2.2) hugetlb mapping i_mmap_rwsem lock held read or write, to make
231  *           sure even if unshare happened the racy unmap() will wait until
232  *           i_mmap_rwsem is released.
233  *
234  * Option (2.1) is the safest, which guarantees pte stability from pmd
235  * sharing pov, until the vma lock released.  Option (2.2) doesn't protect
236  * a concurrent pmd unshare, but it makes sure the pgtable page is safe to
237  * access.
238  */
239 pte_t *huge_pte_offset(struct mm_struct *mm,
240 		       unsigned long addr, unsigned long sz);
241 unsigned long hugetlb_mask_last_page(struct hstate *h);
242 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
243 				unsigned long addr, pte_t *ptep);
244 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
245 				unsigned long *start, unsigned long *end);
246 
247 void hugetlb_vma_lock_read(struct vm_area_struct *vma);
248 void hugetlb_vma_unlock_read(struct vm_area_struct *vma);
249 void hugetlb_vma_lock_write(struct vm_area_struct *vma);
250 void hugetlb_vma_unlock_write(struct vm_area_struct *vma);
251 int hugetlb_vma_trylock_write(struct vm_area_struct *vma);
252 void hugetlb_vma_assert_locked(struct vm_area_struct *vma);
253 void hugetlb_vma_lock_release(struct kref *kref);
254 
255 int pmd_huge(pmd_t pmd);
256 int pud_huge(pud_t pud);
257 long hugetlb_change_protection(struct vm_area_struct *vma,
258 		unsigned long address, unsigned long end, pgprot_t newprot,
259 		unsigned long cp_flags);
260 
261 bool is_hugetlb_entry_migration(pte_t pte);
262 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
263 
264 #else /* !CONFIG_HUGETLB_PAGE */
265 
266 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
267 {
268 }
269 
270 static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
271 {
272 }
273 
274 static inline unsigned long hugetlb_total_pages(void)
275 {
276 	return 0;
277 }
278 
279 static inline struct address_space *hugetlb_page_mapping_lock_write(
280 							struct page *hpage)
281 {
282 	return NULL;
283 }
284 
285 static inline int huge_pmd_unshare(struct mm_struct *mm,
286 					struct vm_area_struct *vma,
287 					unsigned long addr, pte_t *ptep)
288 {
289 	return 0;
290 }
291 
292 static inline void adjust_range_if_pmd_sharing_possible(
293 				struct vm_area_struct *vma,
294 				unsigned long *start, unsigned long *end)
295 {
296 }
297 
298 static inline struct page *hugetlb_follow_page_mask(
299     struct vm_area_struct *vma, unsigned long address, unsigned int flags,
300     unsigned int *page_mask)
301 {
302 	BUILD_BUG(); /* should never be compiled in if !CONFIG_HUGETLB_PAGE*/
303 }
304 
305 static inline int copy_hugetlb_page_range(struct mm_struct *dst,
306 					  struct mm_struct *src,
307 					  struct vm_area_struct *dst_vma,
308 					  struct vm_area_struct *src_vma)
309 {
310 	BUG();
311 	return 0;
312 }
313 
314 static inline int move_hugetlb_page_tables(struct vm_area_struct *vma,
315 					   struct vm_area_struct *new_vma,
316 					   unsigned long old_addr,
317 					   unsigned long new_addr,
318 					   unsigned long len)
319 {
320 	BUG();
321 	return 0;
322 }
323 
324 static inline void hugetlb_report_meminfo(struct seq_file *m)
325 {
326 }
327 
328 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
329 {
330 	return 0;
331 }
332 
333 static inline void hugetlb_show_meminfo_node(int nid)
334 {
335 }
336 
337 static inline int prepare_hugepage_range(struct file *file,
338 				unsigned long addr, unsigned long len)
339 {
340 	return -EINVAL;
341 }
342 
343 static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma)
344 {
345 }
346 
347 static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
348 {
349 }
350 
351 static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma)
352 {
353 }
354 
355 static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
356 {
357 }
358 
359 static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
360 {
361 	return 1;
362 }
363 
364 static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
365 {
366 }
367 
368 static inline int pmd_huge(pmd_t pmd)
369 {
370 	return 0;
371 }
372 
373 static inline int pud_huge(pud_t pud)
374 {
375 	return 0;
376 }
377 
378 static inline int is_hugepage_only_range(struct mm_struct *mm,
379 					unsigned long addr, unsigned long len)
380 {
381 	return 0;
382 }
383 
384 static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb,
385 				unsigned long addr, unsigned long end,
386 				unsigned long floor, unsigned long ceiling)
387 {
388 	BUG();
389 }
390 
391 #ifdef CONFIG_USERFAULTFD
392 static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
393 					   struct vm_area_struct *dst_vma,
394 					   unsigned long dst_addr,
395 					   unsigned long src_addr,
396 					   uffd_flags_t flags,
397 					   struct folio **foliop)
398 {
399 	BUG();
400 	return 0;
401 }
402 #endif /* CONFIG_USERFAULTFD */
403 
404 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
405 					unsigned long sz)
406 {
407 	return NULL;
408 }
409 
410 static inline bool isolate_hugetlb(struct folio *folio, struct list_head *list)
411 {
412 	return false;
413 }
414 
415 static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
416 {
417 	return 0;
418 }
419 
420 static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
421 					bool *migratable_cleared)
422 {
423 	return 0;
424 }
425 
426 static inline void folio_putback_active_hugetlb(struct folio *folio)
427 {
428 }
429 
430 static inline void move_hugetlb_state(struct folio *old_folio,
431 					struct folio *new_folio, int reason)
432 {
433 }
434 
435 static inline long hugetlb_change_protection(
436 			struct vm_area_struct *vma, unsigned long address,
437 			unsigned long end, pgprot_t newprot,
438 			unsigned long cp_flags)
439 {
440 	return 0;
441 }
442 
443 static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
444 			struct vm_area_struct *vma, unsigned long start,
445 			unsigned long end, struct page *ref_page,
446 			zap_flags_t zap_flags)
447 {
448 	BUG();
449 }
450 
451 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
452 			struct vm_area_struct *vma, unsigned long address,
453 			unsigned int flags)
454 {
455 	BUG();
456 	return 0;
457 }
458 
459 static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
460 
461 #endif /* !CONFIG_HUGETLB_PAGE */
462 /*
463  * hugepages at page global directory. If arch support
464  * hugepages at pgd level, they need to define this.
465  */
466 #ifndef pgd_huge
467 #define pgd_huge(x)	0
468 #endif
469 #ifndef p4d_huge
470 #define p4d_huge(x)	0
471 #endif
472 
473 #ifndef pgd_write
474 static inline int pgd_write(pgd_t pgd)
475 {
476 	BUG();
477 	return 0;
478 }
479 #endif
480 
481 #define HUGETLB_ANON_FILE "anon_hugepage"
482 
483 enum {
484 	/*
485 	 * The file will be used as an shm file so shmfs accounting rules
486 	 * apply
487 	 */
488 	HUGETLB_SHMFS_INODE     = 1,
489 	/*
490 	 * The file is being created on the internal vfs mount and shmfs
491 	 * accounting rules do not apply
492 	 */
493 	HUGETLB_ANONHUGE_INODE  = 2,
494 };
495 
496 #ifdef CONFIG_HUGETLBFS
497 struct hugetlbfs_sb_info {
498 	long	max_inodes;   /* inodes allowed */
499 	long	free_inodes;  /* inodes free */
500 	spinlock_t	stat_lock;
501 	struct hstate *hstate;
502 	struct hugepage_subpool *spool;
503 	kuid_t	uid;
504 	kgid_t	gid;
505 	umode_t mode;
506 };
507 
508 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
509 {
510 	return sb->s_fs_info;
511 }
512 
513 struct hugetlbfs_inode_info {
514 	struct shared_policy policy;
515 	struct inode vfs_inode;
516 	unsigned int seals;
517 };
518 
519 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
520 {
521 	return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
522 }
523 
524 extern const struct file_operations hugetlbfs_file_operations;
525 extern const struct vm_operations_struct hugetlb_vm_ops;
526 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
527 				int creat_flags, int page_size_log);
528 
529 static inline bool is_file_hugepages(struct file *file)
530 {
531 	if (file->f_op == &hugetlbfs_file_operations)
532 		return true;
533 
534 	return is_file_shm_hugepages(file);
535 }
536 
537 static inline struct hstate *hstate_inode(struct inode *i)
538 {
539 	return HUGETLBFS_SB(i->i_sb)->hstate;
540 }
541 #else /* !CONFIG_HUGETLBFS */
542 
543 #define is_file_hugepages(file)			false
544 static inline struct file *
545 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
546 		int creat_flags, int page_size_log)
547 {
548 	return ERR_PTR(-ENOSYS);
549 }
550 
551 static inline struct hstate *hstate_inode(struct inode *i)
552 {
553 	return NULL;
554 }
555 #endif /* !CONFIG_HUGETLBFS */
556 
557 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
558 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
559 					unsigned long len, unsigned long pgoff,
560 					unsigned long flags);
561 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
562 
563 unsigned long
564 generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
565 				  unsigned long len, unsigned long pgoff,
566 				  unsigned long flags);
567 
568 /*
569  * huegtlb page specific state flags.  These flags are located in page.private
570  * of the hugetlb head page.  Functions created via the below macros should be
571  * used to manipulate these flags.
572  *
573  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
574  *	allocation time.  Cleared when page is fully instantiated.  Free
575  *	routine checks flag to restore a reservation on error paths.
576  *	Synchronization:  Examined or modified by code that knows it has
577  *	the only reference to page.  i.e. After allocation but before use
578  *	or when the page is being freed.
579  * HPG_migratable  - Set after a newly allocated page is added to the page
580  *	cache and/or page tables.  Indicates the page is a candidate for
581  *	migration.
582  *	Synchronization:  Initially set after new page allocation with no
583  *	locking.  When examined and modified during migration processing
584  *	(isolate, migrate, putback) the hugetlb_lock is held.
585  * HPG_temporary - Set on a page that is temporarily allocated from the buddy
586  *	allocator.  Typically used for migration target pages when no pages
587  *	are available in the pool.  The hugetlb free page path will
588  *	immediately free pages with this flag set to the buddy allocator.
589  *	Synchronization: Can be set after huge page allocation from buddy when
590  *	code knows it has only reference.  All other examinations and
591  *	modifications require hugetlb_lock.
592  * HPG_freed - Set when page is on the free lists.
593  *	Synchronization: hugetlb_lock held for examination and modification.
594  * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed.
595  * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page
596  *     that is not tracked by raw_hwp_page list.
597  */
598 enum hugetlb_page_flags {
599 	HPG_restore_reserve = 0,
600 	HPG_migratable,
601 	HPG_temporary,
602 	HPG_freed,
603 	HPG_vmemmap_optimized,
604 	HPG_raw_hwp_unreliable,
605 	__NR_HPAGEFLAGS,
606 };
607 
608 /*
609  * Macros to create test, set and clear function definitions for
610  * hugetlb specific page flags.
611  */
612 #ifdef CONFIG_HUGETLB_PAGE
613 #define TESTHPAGEFLAG(uname, flname)				\
614 static __always_inline						\
615 bool folio_test_hugetlb_##flname(struct folio *folio)		\
616 	{	void *private = &folio->private;		\
617 		return test_bit(HPG_##flname, private);		\
618 	}							\
619 static inline int HPage##uname(struct page *page)		\
620 	{ return test_bit(HPG_##flname, &(page->private)); }
621 
622 #define SETHPAGEFLAG(uname, flname)				\
623 static __always_inline						\
624 void folio_set_hugetlb_##flname(struct folio *folio)		\
625 	{	void *private = &folio->private;		\
626 		set_bit(HPG_##flname, private);			\
627 	}							\
628 static inline void SetHPage##uname(struct page *page)		\
629 	{ set_bit(HPG_##flname, &(page->private)); }
630 
631 #define CLEARHPAGEFLAG(uname, flname)				\
632 static __always_inline						\
633 void folio_clear_hugetlb_##flname(struct folio *folio)		\
634 	{	void *private = &folio->private;		\
635 		clear_bit(HPG_##flname, private);		\
636 	}							\
637 static inline void ClearHPage##uname(struct page *page)		\
638 	{ clear_bit(HPG_##flname, &(page->private)); }
639 #else
640 #define TESTHPAGEFLAG(uname, flname)				\
641 static inline bool						\
642 folio_test_hugetlb_##flname(struct folio *folio)		\
643 	{ return 0; }						\
644 static inline int HPage##uname(struct page *page)		\
645 	{ return 0; }
646 
647 #define SETHPAGEFLAG(uname, flname)				\
648 static inline void						\
649 folio_set_hugetlb_##flname(struct folio *folio) 		\
650 	{ }							\
651 static inline void SetHPage##uname(struct page *page)		\
652 	{ }
653 
654 #define CLEARHPAGEFLAG(uname, flname)				\
655 static inline void						\
656 folio_clear_hugetlb_##flname(struct folio *folio)		\
657 	{ }							\
658 static inline void ClearHPage##uname(struct page *page)		\
659 	{ }
660 #endif
661 
662 #define HPAGEFLAG(uname, flname)				\
663 	TESTHPAGEFLAG(uname, flname)				\
664 	SETHPAGEFLAG(uname, flname)				\
665 	CLEARHPAGEFLAG(uname, flname)				\
666 
667 /*
668  * Create functions associated with hugetlb page flags
669  */
670 HPAGEFLAG(RestoreReserve, restore_reserve)
671 HPAGEFLAG(Migratable, migratable)
672 HPAGEFLAG(Temporary, temporary)
673 HPAGEFLAG(Freed, freed)
674 HPAGEFLAG(VmemmapOptimized, vmemmap_optimized)
675 HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable)
676 
677 #ifdef CONFIG_HUGETLB_PAGE
678 
679 #define HSTATE_NAME_LEN 32
680 /* Defines one hugetlb page size */
681 struct hstate {
682 	struct mutex resize_lock;
683 	int next_nid_to_alloc;
684 	int next_nid_to_free;
685 	unsigned int order;
686 	unsigned int demote_order;
687 	unsigned long mask;
688 	unsigned long max_huge_pages;
689 	unsigned long nr_huge_pages;
690 	unsigned long free_huge_pages;
691 	unsigned long resv_huge_pages;
692 	unsigned long surplus_huge_pages;
693 	unsigned long nr_overcommit_huge_pages;
694 	struct list_head hugepage_activelist;
695 	struct list_head hugepage_freelists[MAX_NUMNODES];
696 	unsigned int max_huge_pages_node[MAX_NUMNODES];
697 	unsigned int nr_huge_pages_node[MAX_NUMNODES];
698 	unsigned int free_huge_pages_node[MAX_NUMNODES];
699 	unsigned int surplus_huge_pages_node[MAX_NUMNODES];
700 #ifdef CONFIG_CGROUP_HUGETLB
701 	/* cgroup control files */
702 	struct cftype cgroup_files_dfl[8];
703 	struct cftype cgroup_files_legacy[10];
704 #endif
705 	char name[HSTATE_NAME_LEN];
706 };
707 
708 struct huge_bootmem_page {
709 	struct list_head list;
710 	struct hstate *hstate;
711 };
712 
713 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list);
714 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
715 				unsigned long addr, int avoid_reserve);
716 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
717 				nodemask_t *nmask, gfp_t gfp_mask);
718 struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *vma,
719 				unsigned long address);
720 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
721 			pgoff_t idx);
722 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
723 				unsigned long address, struct folio *folio);
724 
725 /* arch callback */
726 int __init __alloc_bootmem_huge_page(struct hstate *h, int nid);
727 int __init alloc_bootmem_huge_page(struct hstate *h, int nid);
728 bool __init hugetlb_node_alloc_supported(void);
729 
730 void __init hugetlb_add_hstate(unsigned order);
731 bool __init arch_hugetlb_valid_size(unsigned long size);
732 struct hstate *size_to_hstate(unsigned long size);
733 
734 #ifndef HUGE_MAX_HSTATE
735 #define HUGE_MAX_HSTATE 1
736 #endif
737 
738 extern struct hstate hstates[HUGE_MAX_HSTATE];
739 extern unsigned int default_hstate_idx;
740 
741 #define default_hstate (hstates[default_hstate_idx])
742 
743 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
744 {
745 	return folio->_hugetlb_subpool;
746 }
747 
748 static inline void hugetlb_set_folio_subpool(struct folio *folio,
749 					struct hugepage_subpool *subpool)
750 {
751 	folio->_hugetlb_subpool = subpool;
752 }
753 
754 static inline struct hstate *hstate_file(struct file *f)
755 {
756 	return hstate_inode(file_inode(f));
757 }
758 
759 static inline struct hstate *hstate_sizelog(int page_size_log)
760 {
761 	if (!page_size_log)
762 		return &default_hstate;
763 
764 	if (page_size_log < BITS_PER_LONG)
765 		return size_to_hstate(1UL << page_size_log);
766 
767 	return NULL;
768 }
769 
770 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
771 {
772 	return hstate_file(vma->vm_file);
773 }
774 
775 static inline unsigned long huge_page_size(const struct hstate *h)
776 {
777 	return (unsigned long)PAGE_SIZE << h->order;
778 }
779 
780 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
781 
782 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
783 
784 static inline unsigned long huge_page_mask(struct hstate *h)
785 {
786 	return h->mask;
787 }
788 
789 static inline unsigned int huge_page_order(struct hstate *h)
790 {
791 	return h->order;
792 }
793 
794 static inline unsigned huge_page_shift(struct hstate *h)
795 {
796 	return h->order + PAGE_SHIFT;
797 }
798 
799 static inline bool hstate_is_gigantic(struct hstate *h)
800 {
801 	return huge_page_order(h) > MAX_ORDER;
802 }
803 
804 static inline unsigned int pages_per_huge_page(const struct hstate *h)
805 {
806 	return 1 << h->order;
807 }
808 
809 static inline unsigned int blocks_per_huge_page(struct hstate *h)
810 {
811 	return huge_page_size(h) / 512;
812 }
813 
814 #include <asm/hugetlb.h>
815 
816 #ifndef is_hugepage_only_range
817 static inline int is_hugepage_only_range(struct mm_struct *mm,
818 					unsigned long addr, unsigned long len)
819 {
820 	return 0;
821 }
822 #define is_hugepage_only_range is_hugepage_only_range
823 #endif
824 
825 #ifndef arch_clear_hugepage_flags
826 static inline void arch_clear_hugepage_flags(struct page *page) { }
827 #define arch_clear_hugepage_flags arch_clear_hugepage_flags
828 #endif
829 
830 #ifndef arch_make_huge_pte
831 static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift,
832 				       vm_flags_t flags)
833 {
834 	return pte_mkhuge(entry);
835 }
836 #endif
837 
838 static inline struct hstate *folio_hstate(struct folio *folio)
839 {
840 	VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
841 	return size_to_hstate(folio_size(folio));
842 }
843 
844 static inline unsigned hstate_index_to_shift(unsigned index)
845 {
846 	return hstates[index].order + PAGE_SHIFT;
847 }
848 
849 static inline int hstate_index(struct hstate *h)
850 {
851 	return h - hstates;
852 }
853 
854 extern int dissolve_free_huge_page(struct page *page);
855 extern int dissolve_free_huge_pages(unsigned long start_pfn,
856 				    unsigned long end_pfn);
857 
858 #ifdef CONFIG_MEMORY_FAILURE
859 extern void folio_clear_hugetlb_hwpoison(struct folio *folio);
860 #else
861 static inline void folio_clear_hugetlb_hwpoison(struct folio *folio)
862 {
863 }
864 #endif
865 
866 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
867 #ifndef arch_hugetlb_migration_supported
868 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
869 {
870 	if ((huge_page_shift(h) == PMD_SHIFT) ||
871 		(huge_page_shift(h) == PUD_SHIFT) ||
872 			(huge_page_shift(h) == PGDIR_SHIFT))
873 		return true;
874 	else
875 		return false;
876 }
877 #endif
878 #else
879 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
880 {
881 	return false;
882 }
883 #endif
884 
885 static inline bool hugepage_migration_supported(struct hstate *h)
886 {
887 	return arch_hugetlb_migration_supported(h);
888 }
889 
890 /*
891  * Movability check is different as compared to migration check.
892  * It determines whether or not a huge page should be placed on
893  * movable zone or not. Movability of any huge page should be
894  * required only if huge page size is supported for migration.
895  * There won't be any reason for the huge page to be movable if
896  * it is not migratable to start with. Also the size of the huge
897  * page should be large enough to be placed under a movable zone
898  * and still feasible enough to be migratable. Just the presence
899  * in movable zone does not make the migration feasible.
900  *
901  * So even though large huge page sizes like the gigantic ones
902  * are migratable they should not be movable because its not
903  * feasible to migrate them from movable zone.
904  */
905 static inline bool hugepage_movable_supported(struct hstate *h)
906 {
907 	if (!hugepage_migration_supported(h))
908 		return false;
909 
910 	if (hstate_is_gigantic(h))
911 		return false;
912 	return true;
913 }
914 
915 /* Movability of hugepages depends on migration support. */
916 static inline gfp_t htlb_alloc_mask(struct hstate *h)
917 {
918 	if (hugepage_movable_supported(h))
919 		return GFP_HIGHUSER_MOVABLE;
920 	else
921 		return GFP_HIGHUSER;
922 }
923 
924 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
925 {
926 	gfp_t modified_mask = htlb_alloc_mask(h);
927 
928 	/* Some callers might want to enforce node */
929 	modified_mask |= (gfp_mask & __GFP_THISNODE);
930 
931 	modified_mask |= (gfp_mask & __GFP_NOWARN);
932 
933 	return modified_mask;
934 }
935 
936 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
937 					   struct mm_struct *mm, pte_t *pte)
938 {
939 	if (huge_page_size(h) == PMD_SIZE)
940 		return pmd_lockptr(mm, (pmd_t *) pte);
941 	VM_BUG_ON(huge_page_size(h) == PAGE_SIZE);
942 	return &mm->page_table_lock;
943 }
944 
945 #ifndef hugepages_supported
946 /*
947  * Some platform decide whether they support huge pages at boot
948  * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
949  * when there is no such support
950  */
951 #define hugepages_supported() (HPAGE_SHIFT != 0)
952 #endif
953 
954 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
955 
956 static inline void hugetlb_count_init(struct mm_struct *mm)
957 {
958 	atomic_long_set(&mm->hugetlb_usage, 0);
959 }
960 
961 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
962 {
963 	atomic_long_add(l, &mm->hugetlb_usage);
964 }
965 
966 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
967 {
968 	atomic_long_sub(l, &mm->hugetlb_usage);
969 }
970 
971 #ifndef huge_ptep_modify_prot_start
972 #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start
973 static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
974 						unsigned long addr, pte_t *ptep)
975 {
976 	return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
977 }
978 #endif
979 
980 #ifndef huge_ptep_modify_prot_commit
981 #define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit
982 static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
983 						unsigned long addr, pte_t *ptep,
984 						pte_t old_pte, pte_t pte)
985 {
986 	set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
987 }
988 #endif
989 
990 #ifdef CONFIG_NUMA
991 void hugetlb_register_node(struct node *node);
992 void hugetlb_unregister_node(struct node *node);
993 #endif
994 
995 /*
996  * Check if a given raw @page in a hugepage is HWPOISON.
997  */
998 bool is_raw_hwpoison_page_in_hugepage(struct page *page);
999 
1000 #else	/* CONFIG_HUGETLB_PAGE */
1001 struct hstate {};
1002 
1003 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
1004 {
1005 	return NULL;
1006 }
1007 
1008 static inline int isolate_or_dissolve_huge_page(struct page *page,
1009 						struct list_head *list)
1010 {
1011 	return -ENOMEM;
1012 }
1013 
1014 static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
1015 					   unsigned long addr,
1016 					   int avoid_reserve)
1017 {
1018 	return NULL;
1019 }
1020 
1021 static inline struct folio *
1022 alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
1023 			nodemask_t *nmask, gfp_t gfp_mask)
1024 {
1025 	return NULL;
1026 }
1027 
1028 static inline struct folio *alloc_hugetlb_folio_vma(struct hstate *h,
1029 					       struct vm_area_struct *vma,
1030 					       unsigned long address)
1031 {
1032 	return NULL;
1033 }
1034 
1035 static inline int __alloc_bootmem_huge_page(struct hstate *h)
1036 {
1037 	return 0;
1038 }
1039 
1040 static inline struct hstate *hstate_file(struct file *f)
1041 {
1042 	return NULL;
1043 }
1044 
1045 static inline struct hstate *hstate_sizelog(int page_size_log)
1046 {
1047 	return NULL;
1048 }
1049 
1050 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
1051 {
1052 	return NULL;
1053 }
1054 
1055 static inline struct hstate *folio_hstate(struct folio *folio)
1056 {
1057 	return NULL;
1058 }
1059 
1060 static inline struct hstate *size_to_hstate(unsigned long size)
1061 {
1062 	return NULL;
1063 }
1064 
1065 static inline unsigned long huge_page_size(struct hstate *h)
1066 {
1067 	return PAGE_SIZE;
1068 }
1069 
1070 static inline unsigned long huge_page_mask(struct hstate *h)
1071 {
1072 	return PAGE_MASK;
1073 }
1074 
1075 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1076 {
1077 	return PAGE_SIZE;
1078 }
1079 
1080 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1081 {
1082 	return PAGE_SIZE;
1083 }
1084 
1085 static inline unsigned int huge_page_order(struct hstate *h)
1086 {
1087 	return 0;
1088 }
1089 
1090 static inline unsigned int huge_page_shift(struct hstate *h)
1091 {
1092 	return PAGE_SHIFT;
1093 }
1094 
1095 static inline bool hstate_is_gigantic(struct hstate *h)
1096 {
1097 	return false;
1098 }
1099 
1100 static inline unsigned int pages_per_huge_page(struct hstate *h)
1101 {
1102 	return 1;
1103 }
1104 
1105 static inline unsigned hstate_index_to_shift(unsigned index)
1106 {
1107 	return 0;
1108 }
1109 
1110 static inline int hstate_index(struct hstate *h)
1111 {
1112 	return 0;
1113 }
1114 
1115 static inline int dissolve_free_huge_page(struct page *page)
1116 {
1117 	return 0;
1118 }
1119 
1120 static inline int dissolve_free_huge_pages(unsigned long start_pfn,
1121 					   unsigned long end_pfn)
1122 {
1123 	return 0;
1124 }
1125 
1126 static inline bool hugepage_migration_supported(struct hstate *h)
1127 {
1128 	return false;
1129 }
1130 
1131 static inline bool hugepage_movable_supported(struct hstate *h)
1132 {
1133 	return false;
1134 }
1135 
1136 static inline gfp_t htlb_alloc_mask(struct hstate *h)
1137 {
1138 	return 0;
1139 }
1140 
1141 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
1142 {
1143 	return 0;
1144 }
1145 
1146 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
1147 					   struct mm_struct *mm, pte_t *pte)
1148 {
1149 	return &mm->page_table_lock;
1150 }
1151 
1152 static inline void hugetlb_count_init(struct mm_struct *mm)
1153 {
1154 }
1155 
1156 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
1157 {
1158 }
1159 
1160 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1161 {
1162 }
1163 
1164 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
1165 					  unsigned long addr, pte_t *ptep)
1166 {
1167 #ifdef CONFIG_MMU
1168 	return ptep_get(ptep);
1169 #else
1170 	return *ptep;
1171 #endif
1172 }
1173 
1174 static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
1175 				   pte_t *ptep, pte_t pte)
1176 {
1177 }
1178 
1179 static inline void hugetlb_register_node(struct node *node)
1180 {
1181 }
1182 
1183 static inline void hugetlb_unregister_node(struct node *node)
1184 {
1185 }
1186 #endif	/* CONFIG_HUGETLB_PAGE */
1187 
1188 static inline spinlock_t *huge_pte_lock(struct hstate *h,
1189 					struct mm_struct *mm, pte_t *pte)
1190 {
1191 	spinlock_t *ptl;
1192 
1193 	ptl = huge_pte_lockptr(h, mm, pte);
1194 	spin_lock(ptl);
1195 	return ptl;
1196 }
1197 
1198 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1199 extern void __init hugetlb_cma_reserve(int order);
1200 #else
1201 static inline __init void hugetlb_cma_reserve(int order)
1202 {
1203 }
1204 #endif
1205 
1206 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
1207 static inline bool hugetlb_pmd_shared(pte_t *pte)
1208 {
1209 	return page_count(virt_to_page(pte)) > 1;
1210 }
1211 #else
1212 static inline bool hugetlb_pmd_shared(pte_t *pte)
1213 {
1214 	return false;
1215 }
1216 #endif
1217 
1218 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
1219 
1220 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
1221 /*
1222  * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
1223  * implement this.
1224  */
1225 #define flush_hugetlb_tlb_range(vma, addr, end)	flush_tlb_range(vma, addr, end)
1226 #endif
1227 
1228 static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
1229 {
1230 	return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
1231 }
1232 
1233 /*
1234  * Safe version of huge_pte_offset() to check the locks.  See comments
1235  * above huge_pte_offset().
1236  */
1237 static inline pte_t *
1238 hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz)
1239 {
1240 #if defined(CONFIG_HUGETLB_PAGE) && \
1241 	defined(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) && defined(CONFIG_LOCKDEP)
1242 	struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1243 
1244 	/*
1245 	 * If pmd sharing possible, locking needed to safely walk the
1246 	 * hugetlb pgtables.  More information can be found at the comment
1247 	 * above huge_pte_offset() in the same file.
1248 	 *
1249 	 * NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP.
1250 	 */
1251 	if (__vma_shareable_lock(vma))
1252 		WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) &&
1253 			     !lockdep_is_held(
1254 				 &vma->vm_file->f_mapping->i_mmap_rwsem));
1255 #endif
1256 	return huge_pte_offset(vma->vm_mm, addr, sz);
1257 }
1258 
1259 #endif /* _LINUX_HUGETLB_H */
1260