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 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_active_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 /* arch callbacks */
178
179 #ifndef CONFIG_HIGHPTE
180 /*
181 * pte_offset_huge() and pte_alloc_huge() are helpers for those architectures
182 * which may go down to the lowest PTE level in their huge_pte_offset() and
183 * huge_pte_alloc(): to avoid reliance on pte_offset_map() without pte_unmap().
184 */
pte_offset_huge(pmd_t * pmd,unsigned long address)185 static inline pte_t *pte_offset_huge(pmd_t *pmd, unsigned long address)
186 {
187 return pte_offset_kernel(pmd, address);
188 }
pte_alloc_huge(struct mm_struct * mm,pmd_t * pmd,unsigned long address)189 static inline pte_t *pte_alloc_huge(struct mm_struct *mm, pmd_t *pmd,
190 unsigned long address)
191 {
192 return pte_alloc(mm, pmd) ? NULL : pte_offset_huge(pmd, address);
193 }
194 #endif
195
196 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
197 unsigned long addr, unsigned long sz);
198 /*
199 * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE.
200 * Returns the pte_t* if found, or NULL if the address is not mapped.
201 *
202 * IMPORTANT: we should normally not directly call this function, instead
203 * this is only a common interface to implement arch-specific
204 * walker. Please use hugetlb_walk() instead, because that will attempt to
205 * verify the locking for you.
206 *
207 * Since this function will walk all the pgtable pages (including not only
208 * high-level pgtable page, but also PUD entry that can be unshared
209 * concurrently for VM_SHARED), the caller of this function should be
210 * responsible of its thread safety. One can follow this rule:
211 *
212 * (1) For private mappings: pmd unsharing is not possible, so holding the
213 * mmap_lock for either read or write is sufficient. Most callers
214 * already hold the mmap_lock, so normally, no special action is
215 * required.
216 *
217 * (2) For shared mappings: pmd unsharing is possible (so the PUD-ranged
218 * pgtable page can go away from under us! It can be done by a pmd
219 * unshare with a follow up munmap() on the other process), then we
220 * need either:
221 *
222 * (2.1) hugetlb vma lock read or write held, to make sure pmd unshare
223 * won't happen upon the range (it also makes sure the pte_t we
224 * read is the right and stable one), or,
225 *
226 * (2.2) hugetlb mapping i_mmap_rwsem lock held read or write, to make
227 * sure even if unshare happened the racy unmap() will wait until
228 * i_mmap_rwsem is released.
229 *
230 * Option (2.1) is the safest, which guarantees pte stability from pmd
231 * sharing pov, until the vma lock released. Option (2.2) doesn't protect
232 * a concurrent pmd unshare, but it makes sure the pgtable page is safe to
233 * access.
234 */
235 pte_t *huge_pte_offset(struct mm_struct *mm,
236 unsigned long addr, unsigned long sz);
237 unsigned long hugetlb_mask_last_page(struct hstate *h);
238 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
239 unsigned long addr, pte_t *ptep);
240 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
241 unsigned long *start, unsigned long *end);
242
243 extern void __hugetlb_zap_begin(struct vm_area_struct *vma,
244 unsigned long *begin, unsigned long *end);
245 extern void __hugetlb_zap_end(struct vm_area_struct *vma,
246 struct zap_details *details);
247
hugetlb_zap_begin(struct vm_area_struct * vma,unsigned long * start,unsigned long * end)248 static inline void hugetlb_zap_begin(struct vm_area_struct *vma,
249 unsigned long *start, unsigned long *end)
250 {
251 if (is_vm_hugetlb_page(vma))
252 __hugetlb_zap_begin(vma, start, end);
253 }
254
hugetlb_zap_end(struct vm_area_struct * vma,struct zap_details * details)255 static inline void hugetlb_zap_end(struct vm_area_struct *vma,
256 struct zap_details *details)
257 {
258 if (is_vm_hugetlb_page(vma))
259 __hugetlb_zap_end(vma, details);
260 }
261
262 void hugetlb_vma_lock_read(struct vm_area_struct *vma);
263 void hugetlb_vma_unlock_read(struct vm_area_struct *vma);
264 void hugetlb_vma_lock_write(struct vm_area_struct *vma);
265 void hugetlb_vma_unlock_write(struct vm_area_struct *vma);
266 int hugetlb_vma_trylock_write(struct vm_area_struct *vma);
267 void hugetlb_vma_assert_locked(struct vm_area_struct *vma);
268 void hugetlb_vma_lock_release(struct kref *kref);
269 long hugetlb_change_protection(struct vm_area_struct *vma,
270 unsigned long address, unsigned long end, pgprot_t newprot,
271 unsigned long cp_flags);
272 bool is_hugetlb_entry_migration(pte_t pte);
273 bool is_hugetlb_entry_hwpoisoned(pte_t pte);
274 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
275 void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
276
277 #else /* !CONFIG_HUGETLB_PAGE */
278
hugetlb_dup_vma_private(struct vm_area_struct * vma)279 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
280 {
281 }
282
clear_vma_resv_huge_pages(struct vm_area_struct * vma)283 static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
284 {
285 }
286
hugetlb_total_pages(void)287 static inline unsigned long hugetlb_total_pages(void)
288 {
289 return 0;
290 }
291
hugetlb_folio_mapping_lock_write(struct folio * folio)292 static inline struct address_space *hugetlb_folio_mapping_lock_write(
293 struct folio *folio)
294 {
295 return NULL;
296 }
297
huge_pmd_unshare(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)298 static inline int huge_pmd_unshare(struct mm_struct *mm,
299 struct vm_area_struct *vma,
300 unsigned long addr, pte_t *ptep)
301 {
302 return 0;
303 }
304
adjust_range_if_pmd_sharing_possible(struct vm_area_struct * vma,unsigned long * start,unsigned long * end)305 static inline void adjust_range_if_pmd_sharing_possible(
306 struct vm_area_struct *vma,
307 unsigned long *start, unsigned long *end)
308 {
309 }
310
hugetlb_zap_begin(struct vm_area_struct * vma,unsigned long * start,unsigned long * end)311 static inline void hugetlb_zap_begin(
312 struct vm_area_struct *vma,
313 unsigned long *start, unsigned long *end)
314 {
315 }
316
hugetlb_zap_end(struct vm_area_struct * vma,struct zap_details * details)317 static inline void hugetlb_zap_end(
318 struct vm_area_struct *vma,
319 struct zap_details *details)
320 {
321 }
322
copy_hugetlb_page_range(struct mm_struct * dst,struct mm_struct * src,struct vm_area_struct * dst_vma,struct vm_area_struct * src_vma)323 static inline int copy_hugetlb_page_range(struct mm_struct *dst,
324 struct mm_struct *src,
325 struct vm_area_struct *dst_vma,
326 struct vm_area_struct *src_vma)
327 {
328 BUG();
329 return 0;
330 }
331
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)332 static inline int move_hugetlb_page_tables(struct vm_area_struct *vma,
333 struct vm_area_struct *new_vma,
334 unsigned long old_addr,
335 unsigned long new_addr,
336 unsigned long len)
337 {
338 BUG();
339 return 0;
340 }
341
hugetlb_report_meminfo(struct seq_file * m)342 static inline void hugetlb_report_meminfo(struct seq_file *m)
343 {
344 }
345
hugetlb_report_node_meminfo(char * buf,int len,int nid)346 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
347 {
348 return 0;
349 }
350
hugetlb_show_meminfo_node(int nid)351 static inline void hugetlb_show_meminfo_node(int nid)
352 {
353 }
354
prepare_hugepage_range(struct file * file,unsigned long addr,unsigned long len)355 static inline int prepare_hugepage_range(struct file *file,
356 unsigned long addr, unsigned long len)
357 {
358 return -EINVAL;
359 }
360
hugetlb_vma_lock_read(struct vm_area_struct * vma)361 static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma)
362 {
363 }
364
hugetlb_vma_unlock_read(struct vm_area_struct * vma)365 static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
366 {
367 }
368
hugetlb_vma_lock_write(struct vm_area_struct * vma)369 static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma)
370 {
371 }
372
hugetlb_vma_unlock_write(struct vm_area_struct * vma)373 static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
374 {
375 }
376
hugetlb_vma_trylock_write(struct vm_area_struct * vma)377 static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
378 {
379 return 1;
380 }
381
hugetlb_vma_assert_locked(struct vm_area_struct * vma)382 static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
383 {
384 }
385
is_hugepage_only_range(struct mm_struct * mm,unsigned long addr,unsigned long len)386 static inline int is_hugepage_only_range(struct mm_struct *mm,
387 unsigned long addr, unsigned long len)
388 {
389 return 0;
390 }
391
hugetlb_free_pgd_range(struct mmu_gather * tlb,unsigned long addr,unsigned long end,unsigned long floor,unsigned long ceiling)392 static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb,
393 unsigned long addr, unsigned long end,
394 unsigned long floor, unsigned long ceiling)
395 {
396 BUG();
397 }
398
399 #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)400 static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
401 struct vm_area_struct *dst_vma,
402 unsigned long dst_addr,
403 unsigned long src_addr,
404 uffd_flags_t flags,
405 struct folio **foliop)
406 {
407 BUG();
408 return 0;
409 }
410 #endif /* CONFIG_USERFAULTFD */
411
huge_pte_offset(struct mm_struct * mm,unsigned long addr,unsigned long sz)412 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
413 unsigned long sz)
414 {
415 return NULL;
416 }
417
isolate_hugetlb(struct folio * folio,struct list_head * list)418 static inline bool isolate_hugetlb(struct folio *folio, struct list_head *list)
419 {
420 return false;
421 }
422
get_hwpoison_hugetlb_folio(struct folio * folio,bool * hugetlb,bool unpoison)423 static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
424 {
425 return 0;
426 }
427
get_huge_page_for_hwpoison(unsigned long pfn,int flags,bool * migratable_cleared)428 static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
429 bool *migratable_cleared)
430 {
431 return 0;
432 }
433
folio_putback_active_hugetlb(struct folio * folio)434 static inline void folio_putback_active_hugetlb(struct folio *folio)
435 {
436 }
437
move_hugetlb_state(struct folio * old_folio,struct folio * new_folio,int reason)438 static inline void move_hugetlb_state(struct folio *old_folio,
439 struct folio *new_folio, int reason)
440 {
441 }
442
hugetlb_change_protection(struct vm_area_struct * vma,unsigned long address,unsigned long end,pgprot_t newprot,unsigned long cp_flags)443 static inline long hugetlb_change_protection(
444 struct vm_area_struct *vma, unsigned long address,
445 unsigned long end, pgprot_t newprot,
446 unsigned long cp_flags)
447 {
448 return 0;
449 }
450
__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)451 static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
452 struct vm_area_struct *vma, unsigned long start,
453 unsigned long end, struct page *ref_page,
454 zap_flags_t zap_flags)
455 {
456 BUG();
457 }
458
hugetlb_fault(struct mm_struct * mm,struct vm_area_struct * vma,unsigned long address,unsigned int flags)459 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
460 struct vm_area_struct *vma, unsigned long address,
461 unsigned int flags)
462 {
463 BUG();
464 return 0;
465 }
466
hugetlb_unshare_all_pmds(struct vm_area_struct * vma)467 static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
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 unsigned int seals;
516 };
517
HUGETLBFS_I(struct inode * inode)518 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
519 {
520 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
521 }
522
523 extern const struct vm_operations_struct hugetlb_vm_ops;
524 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
525 int creat_flags, int page_size_log);
526
is_file_hugepages(const struct file * file)527 static inline bool is_file_hugepages(const struct file *file)
528 {
529 return file->f_op->fop_flags & FOP_HUGE_PAGES;
530 }
531
hstate_inode(struct inode * i)532 static inline struct hstate *hstate_inode(struct inode *i)
533 {
534 return HUGETLBFS_SB(i->i_sb)->hstate;
535 }
536 #else /* !CONFIG_HUGETLBFS */
537
538 #define is_file_hugepages(file) false
539 static inline struct file *
hugetlb_file_setup(const char * name,size_t size,vm_flags_t acctflag,int creat_flags,int page_size_log)540 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
541 int creat_flags, int page_size_log)
542 {
543 return ERR_PTR(-ENOSYS);
544 }
545
hstate_inode(struct inode * i)546 static inline struct hstate *hstate_inode(struct inode *i)
547 {
548 return NULL;
549 }
550 #endif /* !CONFIG_HUGETLBFS */
551
552 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
553 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
554 unsigned long len, unsigned long pgoff,
555 unsigned long flags);
556 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
557
558 unsigned long
559 generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
560 unsigned long len, unsigned long pgoff,
561 unsigned long flags);
562
563 /*
564 * huegtlb page specific state flags. These flags are located in page.private
565 * of the hugetlb head page. Functions created via the below macros should be
566 * used to manipulate these flags.
567 *
568 * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
569 * allocation time. Cleared when page is fully instantiated. Free
570 * routine checks flag to restore a reservation on error paths.
571 * Synchronization: Examined or modified by code that knows it has
572 * the only reference to page. i.e. After allocation but before use
573 * or when the page is being freed.
574 * HPG_migratable - Set after a newly allocated page is added to the page
575 * cache and/or page tables. Indicates the page is a candidate for
576 * migration.
577 * Synchronization: Initially set after new page allocation with no
578 * locking. When examined and modified during migration processing
579 * (isolate, migrate, putback) the hugetlb_lock is held.
580 * HPG_temporary - Set on a page that is temporarily allocated from the buddy
581 * allocator. Typically used for migration target pages when no pages
582 * are available in the pool. The hugetlb free page path will
583 * immediately free pages with this flag set to the buddy allocator.
584 * Synchronization: Can be set after huge page allocation from buddy when
585 * code knows it has only reference. All other examinations and
586 * modifications require hugetlb_lock.
587 * HPG_freed - Set when page is on the free lists.
588 * Synchronization: hugetlb_lock held for examination and modification.
589 * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed.
590 * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page
591 * that is not tracked by raw_hwp_page list.
592 */
593 enum hugetlb_page_flags {
594 HPG_restore_reserve = 0,
595 HPG_migratable,
596 HPG_temporary,
597 HPG_freed,
598 HPG_vmemmap_optimized,
599 HPG_raw_hwp_unreliable,
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
660 #ifdef CONFIG_HUGETLB_PAGE
661
662 #define HSTATE_NAME_LEN 32
663 /* Defines one hugetlb page size */
664 struct hstate {
665 struct mutex resize_lock;
666 struct lock_class_key resize_key;
667 int next_nid_to_alloc;
668 int next_nid_to_free;
669 unsigned int order;
670 unsigned int demote_order;
671 unsigned long mask;
672 unsigned long max_huge_pages;
673 unsigned long nr_huge_pages;
674 unsigned long free_huge_pages;
675 unsigned long resv_huge_pages;
676 unsigned long surplus_huge_pages;
677 unsigned long nr_overcommit_huge_pages;
678 struct list_head hugepage_activelist;
679 struct list_head hugepage_freelists[MAX_NUMNODES];
680 unsigned int max_huge_pages_node[MAX_NUMNODES];
681 unsigned int nr_huge_pages_node[MAX_NUMNODES];
682 unsigned int free_huge_pages_node[MAX_NUMNODES];
683 unsigned int surplus_huge_pages_node[MAX_NUMNODES];
684 char name[HSTATE_NAME_LEN];
685 };
686
687 struct huge_bootmem_page {
688 struct list_head list;
689 struct hstate *hstate;
690 };
691
692 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list);
693 void wait_for_freed_hugetlb_folios(void);
694 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
695 unsigned long addr, int avoid_reserve);
696 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
697 nodemask_t *nmask, gfp_t gfp_mask,
698 bool allow_alloc_fallback);
699 struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
700 nodemask_t *nmask, gfp_t gfp_mask);
701
702 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
703 pgoff_t idx);
704 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
705 unsigned long address, struct folio *folio);
706
707 /* arch callback */
708 int __init __alloc_bootmem_huge_page(struct hstate *h, int nid);
709 int __init alloc_bootmem_huge_page(struct hstate *h, int nid);
710 bool __init hugetlb_node_alloc_supported(void);
711
712 void __init hugetlb_add_hstate(unsigned order);
713 bool __init arch_hugetlb_valid_size(unsigned long size);
714 struct hstate *size_to_hstate(unsigned long size);
715
716 #ifndef HUGE_MAX_HSTATE
717 #define HUGE_MAX_HSTATE 1
718 #endif
719
720 extern struct hstate hstates[HUGE_MAX_HSTATE];
721 extern unsigned int default_hstate_idx;
722
723 #define default_hstate (hstates[default_hstate_idx])
724
hugetlb_folio_subpool(struct folio * folio)725 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
726 {
727 return folio->_hugetlb_subpool;
728 }
729
hugetlb_set_folio_subpool(struct folio * folio,struct hugepage_subpool * subpool)730 static inline void hugetlb_set_folio_subpool(struct folio *folio,
731 struct hugepage_subpool *subpool)
732 {
733 folio->_hugetlb_subpool = subpool;
734 }
735
hstate_file(struct file * f)736 static inline struct hstate *hstate_file(struct file *f)
737 {
738 return hstate_inode(file_inode(f));
739 }
740
hstate_sizelog(int page_size_log)741 static inline struct hstate *hstate_sizelog(int page_size_log)
742 {
743 if (!page_size_log)
744 return &default_hstate;
745
746 if (page_size_log < BITS_PER_LONG)
747 return size_to_hstate(1UL << page_size_log);
748
749 return NULL;
750 }
751
hstate_vma(struct vm_area_struct * vma)752 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
753 {
754 return hstate_file(vma->vm_file);
755 }
756
huge_page_size(const struct hstate * h)757 static inline unsigned long huge_page_size(const struct hstate *h)
758 {
759 return (unsigned long)PAGE_SIZE << h->order;
760 }
761
762 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
763
764 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
765
huge_page_mask(struct hstate * h)766 static inline unsigned long huge_page_mask(struct hstate *h)
767 {
768 return h->mask;
769 }
770
huge_page_order(struct hstate * h)771 static inline unsigned int huge_page_order(struct hstate *h)
772 {
773 return h->order;
774 }
775
huge_page_shift(struct hstate * h)776 static inline unsigned huge_page_shift(struct hstate *h)
777 {
778 return h->order + PAGE_SHIFT;
779 }
780
hstate_is_gigantic(struct hstate * h)781 static inline bool hstate_is_gigantic(struct hstate *h)
782 {
783 return huge_page_order(h) > MAX_PAGE_ORDER;
784 }
785
pages_per_huge_page(const struct hstate * h)786 static inline unsigned int pages_per_huge_page(const struct hstate *h)
787 {
788 return 1 << h->order;
789 }
790
blocks_per_huge_page(struct hstate * h)791 static inline unsigned int blocks_per_huge_page(struct hstate *h)
792 {
793 return huge_page_size(h) / 512;
794 }
795
filemap_lock_hugetlb_folio(struct hstate * h,struct address_space * mapping,pgoff_t idx)796 static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
797 struct address_space *mapping, pgoff_t idx)
798 {
799 return filemap_lock_folio(mapping, idx << huge_page_order(h));
800 }
801
802 #include <asm/hugetlb.h>
803
804 #ifndef is_hugepage_only_range
is_hugepage_only_range(struct mm_struct * mm,unsigned long addr,unsigned long len)805 static inline int is_hugepage_only_range(struct mm_struct *mm,
806 unsigned long addr, unsigned long len)
807 {
808 return 0;
809 }
810 #define is_hugepage_only_range is_hugepage_only_range
811 #endif
812
813 #ifndef arch_clear_hugetlb_flags
arch_clear_hugetlb_flags(struct folio * folio)814 static inline void arch_clear_hugetlb_flags(struct folio *folio) { }
815 #define arch_clear_hugetlb_flags arch_clear_hugetlb_flags
816 #endif
817
818 #ifndef arch_make_huge_pte
arch_make_huge_pte(pte_t entry,unsigned int shift,vm_flags_t flags)819 static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift,
820 vm_flags_t flags)
821 {
822 return pte_mkhuge(entry);
823 }
824 #endif
825
folio_hstate(struct folio * folio)826 static inline struct hstate *folio_hstate(struct folio *folio)
827 {
828 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
829 return size_to_hstate(folio_size(folio));
830 }
831
hstate_index_to_shift(unsigned index)832 static inline unsigned hstate_index_to_shift(unsigned index)
833 {
834 return hstates[index].order + PAGE_SHIFT;
835 }
836
hstate_index(struct hstate * h)837 static inline int hstate_index(struct hstate *h)
838 {
839 return h - hstates;
840 }
841
842 int dissolve_free_hugetlb_folio(struct folio *folio);
843 int dissolve_free_hugetlb_folios(unsigned long start_pfn,
844 unsigned long end_pfn);
845
846 #ifdef CONFIG_MEMORY_FAILURE
847 extern void folio_clear_hugetlb_hwpoison(struct folio *folio);
848 #else
folio_clear_hugetlb_hwpoison(struct folio * folio)849 static inline void folio_clear_hugetlb_hwpoison(struct folio *folio)
850 {
851 }
852 #endif
853
854 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
855 #ifndef arch_hugetlb_migration_supported
arch_hugetlb_migration_supported(struct hstate * h)856 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
857 {
858 if ((huge_page_shift(h) == PMD_SHIFT) ||
859 (huge_page_shift(h) == PUD_SHIFT) ||
860 (huge_page_shift(h) == PGDIR_SHIFT))
861 return true;
862 else
863 return false;
864 }
865 #endif
866 #else
arch_hugetlb_migration_supported(struct hstate * h)867 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
868 {
869 return false;
870 }
871 #endif
872
hugepage_migration_supported(struct hstate * h)873 static inline bool hugepage_migration_supported(struct hstate *h)
874 {
875 return arch_hugetlb_migration_supported(h);
876 }
877
878 /*
879 * Movability check is different as compared to migration check.
880 * It determines whether or not a huge page should be placed on
881 * movable zone or not. Movability of any huge page should be
882 * required only if huge page size is supported for migration.
883 * There won't be any reason for the huge page to be movable if
884 * it is not migratable to start with. Also the size of the huge
885 * page should be large enough to be placed under a movable zone
886 * and still feasible enough to be migratable. Just the presence
887 * in movable zone does not make the migration feasible.
888 *
889 * So even though large huge page sizes like the gigantic ones
890 * are migratable they should not be movable because its not
891 * feasible to migrate them from movable zone.
892 */
hugepage_movable_supported(struct hstate * h)893 static inline bool hugepage_movable_supported(struct hstate *h)
894 {
895 if (!hugepage_migration_supported(h))
896 return false;
897
898 if (hstate_is_gigantic(h))
899 return false;
900 return true;
901 }
902
903 /* Movability of hugepages depends on migration support. */
htlb_alloc_mask(struct hstate * h)904 static inline gfp_t htlb_alloc_mask(struct hstate *h)
905 {
906 gfp_t gfp = __GFP_COMP | __GFP_NOWARN;
907
908 gfp |= hugepage_movable_supported(h) ? GFP_HIGHUSER_MOVABLE : GFP_HIGHUSER;
909
910 return gfp;
911 }
912
htlb_modify_alloc_mask(struct hstate * h,gfp_t gfp_mask)913 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
914 {
915 gfp_t modified_mask = htlb_alloc_mask(h);
916
917 /* Some callers might want to enforce node */
918 modified_mask |= (gfp_mask & __GFP_THISNODE);
919
920 modified_mask |= (gfp_mask & __GFP_NOWARN);
921
922 return modified_mask;
923 }
924
htlb_allow_alloc_fallback(int reason)925 static inline bool htlb_allow_alloc_fallback(int reason)
926 {
927 bool allowed_fallback = false;
928
929 /*
930 * Note: the memory offline, memory failure and migration syscalls will
931 * be allowed to fallback to other nodes due to lack of a better chioce,
932 * that might break the per-node hugetlb pool. While other cases will
933 * set the __GFP_THISNODE to avoid breaking the per-node hugetlb pool.
934 */
935 switch (reason) {
936 case MR_MEMORY_HOTPLUG:
937 case MR_MEMORY_FAILURE:
938 case MR_SYSCALL:
939 case MR_MEMPOLICY_MBIND:
940 allowed_fallback = true;
941 break;
942 default:
943 break;
944 }
945
946 return allowed_fallback;
947 }
948
huge_pte_lockptr(struct hstate * h,struct mm_struct * mm,pte_t * pte)949 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
950 struct mm_struct *mm, pte_t *pte)
951 {
952 const unsigned long size = huge_page_size(h);
953
954 VM_WARN_ON(size == PAGE_SIZE);
955
956 /*
957 * hugetlb must use the exact same PT locks as core-mm page table
958 * walkers would. When modifying a PTE table, hugetlb must take the
959 * PTE PT lock, when modifying a PMD table, hugetlb must take the PMD
960 * PT lock etc.
961 *
962 * The expectation is that any hugetlb folio smaller than a PMD is
963 * always mapped into a single PTE table and that any hugetlb folio
964 * smaller than a PUD (but at least as big as a PMD) is always mapped
965 * into a single PMD table.
966 *
967 * If that does not hold for an architecture, then that architecture
968 * must disable split PT locks such that all *_lockptr() functions
969 * will give us the same result: the per-MM PT lock.
970 *
971 * Note that with e.g., CONFIG_PGTABLE_LEVELS=2 where
972 * PGDIR_SIZE==P4D_SIZE==PUD_SIZE==PMD_SIZE, we'd use pud_lockptr()
973 * and core-mm would use pmd_lockptr(). However, in such configurations
974 * split PMD locks are disabled -- they don't make sense on a single
975 * PGDIR page table -- and the end result is the same.
976 */
977 if (size >= PUD_SIZE)
978 return pud_lockptr(mm, (pud_t *) pte);
979 else if (size >= PMD_SIZE || IS_ENABLED(CONFIG_HIGHPTE))
980 return pmd_lockptr(mm, (pmd_t *) pte);
981 /* pte_alloc_huge() only applies with !CONFIG_HIGHPTE */
982 return ptep_lockptr(mm, pte);
983 }
984
985 #ifndef hugepages_supported
986 /*
987 * Some platform decide whether they support huge pages at boot
988 * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
989 * when there is no such support
990 */
991 #define hugepages_supported() (HPAGE_SHIFT != 0)
992 #endif
993
994 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
995
hugetlb_count_init(struct mm_struct * mm)996 static inline void hugetlb_count_init(struct mm_struct *mm)
997 {
998 atomic_long_set(&mm->hugetlb_usage, 0);
999 }
1000
hugetlb_count_add(long l,struct mm_struct * mm)1001 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
1002 {
1003 atomic_long_add(l, &mm->hugetlb_usage);
1004 }
1005
hugetlb_count_sub(long l,struct mm_struct * mm)1006 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1007 {
1008 atomic_long_sub(l, &mm->hugetlb_usage);
1009 }
1010
1011 #ifndef huge_ptep_modify_prot_start
1012 #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)1013 static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
1014 unsigned long addr, pte_t *ptep)
1015 {
1016 unsigned long psize = huge_page_size(hstate_vma(vma));
1017
1018 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep, psize);
1019 }
1020 #endif
1021
1022 #ifndef huge_ptep_modify_prot_commit
1023 #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)1024 static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
1025 unsigned long addr, pte_t *ptep,
1026 pte_t old_pte, pte_t pte)
1027 {
1028 unsigned long psize = huge_page_size(hstate_vma(vma));
1029
1030 set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
1031 }
1032 #endif
1033
1034 #ifdef CONFIG_NUMA
1035 void hugetlb_register_node(struct node *node);
1036 void hugetlb_unregister_node(struct node *node);
1037 #endif
1038
1039 /*
1040 * Check if a given raw @page in a hugepage is HWPOISON.
1041 */
1042 bool is_raw_hwpoison_page_in_hugepage(struct page *page);
1043
1044 #else /* CONFIG_HUGETLB_PAGE */
1045 struct hstate {};
1046
1047 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
1048 {
1049 return NULL;
1050 }
1051
1052 static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
1053 struct address_space *mapping, pgoff_t idx)
1054 {
1055 return NULL;
1056 }
1057
1058 static inline int isolate_or_dissolve_huge_page(struct page *page,
1059 struct list_head *list)
1060 {
1061 return -ENOMEM;
1062 }
1063
1064 static inline void wait_for_freed_hugetlb_folios(void)
1065 {
1066 }
1067
1068 static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
1069 unsigned long addr,
1070 int avoid_reserve)
1071 {
1072 return NULL;
1073 }
1074
1075 static inline struct folio *
1076 alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
1077 nodemask_t *nmask, gfp_t gfp_mask)
1078 {
1079 return NULL;
1080 }
1081
1082 static inline struct folio *
1083 alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
1084 nodemask_t *nmask, gfp_t gfp_mask,
1085 bool allow_alloc_fallback)
1086 {
1087 return NULL;
1088 }
1089
1090 static inline int __alloc_bootmem_huge_page(struct hstate *h)
1091 {
1092 return 0;
1093 }
1094
1095 static inline struct hstate *hstate_file(struct file *f)
1096 {
1097 return NULL;
1098 }
1099
1100 static inline struct hstate *hstate_sizelog(int page_size_log)
1101 {
1102 return NULL;
1103 }
1104
1105 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
1106 {
1107 return NULL;
1108 }
1109
1110 static inline struct hstate *folio_hstate(struct folio *folio)
1111 {
1112 return NULL;
1113 }
1114
1115 static inline struct hstate *size_to_hstate(unsigned long size)
1116 {
1117 return NULL;
1118 }
1119
1120 static inline unsigned long huge_page_size(struct hstate *h)
1121 {
1122 return PAGE_SIZE;
1123 }
1124
1125 static inline unsigned long huge_page_mask(struct hstate *h)
1126 {
1127 return PAGE_MASK;
1128 }
1129
1130 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1131 {
1132 return PAGE_SIZE;
1133 }
1134
1135 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1136 {
1137 return PAGE_SIZE;
1138 }
1139
1140 static inline unsigned int huge_page_order(struct hstate *h)
1141 {
1142 return 0;
1143 }
1144
1145 static inline unsigned int huge_page_shift(struct hstate *h)
1146 {
1147 return PAGE_SHIFT;
1148 }
1149
1150 static inline bool hstate_is_gigantic(struct hstate *h)
1151 {
1152 return false;
1153 }
1154
1155 static inline unsigned int pages_per_huge_page(struct hstate *h)
1156 {
1157 return 1;
1158 }
1159
1160 static inline unsigned hstate_index_to_shift(unsigned index)
1161 {
1162 return 0;
1163 }
1164
1165 static inline int hstate_index(struct hstate *h)
1166 {
1167 return 0;
1168 }
1169
1170 static inline int dissolve_free_hugetlb_folio(struct folio *folio)
1171 {
1172 return 0;
1173 }
1174
1175 static inline int dissolve_free_hugetlb_folios(unsigned long start_pfn,
1176 unsigned long end_pfn)
1177 {
1178 return 0;
1179 }
1180
1181 static inline bool hugepage_migration_supported(struct hstate *h)
1182 {
1183 return false;
1184 }
1185
1186 static inline bool hugepage_movable_supported(struct hstate *h)
1187 {
1188 return false;
1189 }
1190
1191 static inline gfp_t htlb_alloc_mask(struct hstate *h)
1192 {
1193 return 0;
1194 }
1195
1196 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
1197 {
1198 return 0;
1199 }
1200
1201 static inline bool htlb_allow_alloc_fallback(int reason)
1202 {
1203 return false;
1204 }
1205
1206 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
1207 struct mm_struct *mm, pte_t *pte)
1208 {
1209 return &mm->page_table_lock;
1210 }
1211
1212 static inline void hugetlb_count_init(struct mm_struct *mm)
1213 {
1214 }
1215
1216 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
1217 {
1218 }
1219
1220 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1221 {
1222 }
1223
1224 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
1225 unsigned long addr, pte_t *ptep)
1226 {
1227 #ifdef CONFIG_MMU
1228 return ptep_get(ptep);
1229 #else
1230 return *ptep;
1231 #endif
1232 }
1233
1234 static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
1235 pte_t *ptep, pte_t pte, unsigned long sz)
1236 {
1237 }
1238
1239 static inline void hugetlb_register_node(struct node *node)
1240 {
1241 }
1242
1243 static inline void hugetlb_unregister_node(struct node *node)
1244 {
1245 }
1246
1247 static inline bool hugetlbfs_pagecache_present(
1248 struct hstate *h, struct vm_area_struct *vma, unsigned long address)
1249 {
1250 return false;
1251 }
1252 #endif /* CONFIG_HUGETLB_PAGE */
1253
huge_pte_lock(struct hstate * h,struct mm_struct * mm,pte_t * pte)1254 static inline spinlock_t *huge_pte_lock(struct hstate *h,
1255 struct mm_struct *mm, pte_t *pte)
1256 {
1257 spinlock_t *ptl;
1258
1259 ptl = huge_pte_lockptr(h, mm, pte);
1260 spin_lock(ptl);
1261 return ptl;
1262 }
1263
1264 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1265 extern void __init hugetlb_cma_reserve(int order);
1266 #else
hugetlb_cma_reserve(int order)1267 static inline __init void hugetlb_cma_reserve(int order)
1268 {
1269 }
1270 #endif
1271
1272 #ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
hugetlb_pmd_shared(pte_t * pte)1273 static inline bool hugetlb_pmd_shared(pte_t *pte)
1274 {
1275 return page_count(virt_to_page(pte)) > 1;
1276 }
1277 #else
hugetlb_pmd_shared(pte_t * pte)1278 static inline bool hugetlb_pmd_shared(pte_t *pte)
1279 {
1280 return false;
1281 }
1282 #endif
1283
1284 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
1285
1286 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
1287 /*
1288 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
1289 * implement this.
1290 */
1291 #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1292 #endif
1293
__vma_shareable_lock(struct vm_area_struct * vma)1294 static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
1295 {
1296 return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
1297 }
1298
1299 bool __vma_private_lock(struct vm_area_struct *vma);
1300
1301 /*
1302 * Safe version of huge_pte_offset() to check the locks. See comments
1303 * above huge_pte_offset().
1304 */
1305 static inline pte_t *
hugetlb_walk(struct vm_area_struct * vma,unsigned long addr,unsigned long sz)1306 hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz)
1307 {
1308 #if defined(CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING) && defined(CONFIG_LOCKDEP)
1309 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1310
1311 /*
1312 * If pmd sharing possible, locking needed to safely walk the
1313 * hugetlb pgtables. More information can be found at the comment
1314 * above huge_pte_offset() in the same file.
1315 *
1316 * NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP.
1317 */
1318 if (__vma_shareable_lock(vma))
1319 WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) &&
1320 !lockdep_is_held(
1321 &vma->vm_file->f_mapping->i_mmap_rwsem));
1322 #endif
1323 return huge_pte_offset(vma->vm_mm, addr, sz);
1324 }
1325
1326 #endif /* _LINUX_HUGETLB_H */
1327