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