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