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