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