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