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