1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGETLB_H
3 #define _LINUX_HUGETLB_H
5 #include <linux/mm_types.h>
6 #include <linux/mmdebug.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>
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) })
28 #ifdef CONFIG_HUGETLB_PAGE
30 #include <linux/mempolicy.h>
31 #include <linux/shm.h>
32 #include <asm/tlbflush.h>
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.
39 #define __NR_USED_SUBPAGE 3
41 struct hugepage_subpool {
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. */
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
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.
66 struct page_counter *reservation_counter;
67 unsigned long pages_per_hpage;
68 struct cgroup_subsys_state *css;
73 * Region tracking -- allows tracking of reservations and instantiated pages
74 * across the pages in a mapping.
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.
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.
88 * Interval notation of the form [from, to) will be used to indicate that
89 * the endpoint from is inclusive and to is exclusive.
92 struct list_head link;
95 #ifdef CONFIG_CGROUP_HUGETLB
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.
101 struct page_counter *reservation_counter;
102 struct cgroup_subsys_state *css;
106 struct hugetlb_vma_lock {
108 struct rw_semaphore rw_sema;
109 struct vm_area_struct *vma;
112 extern struct resv_map *resv_map_alloc(void);
113 void resv_map_release(struct kref *ref);
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)++)
120 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
122 void hugepage_put_subpool(struct hugepage_subpool *spool);
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 *,
129 int hugetlb_treat_movable_handler(struct ctl_table *, int, void *, size_t *,
131 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int, void *, size_t *,
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,
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,
146 void unmap_hugepage_range(struct vm_area_struct *,
147 unsigned long, unsigned long, struct page *,
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,
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,
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);
184 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
185 unsigned long addr, pud_t *pud);
187 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage);
189 extern int sysctl_hugetlb_shm_group;
190 extern struct list_head huge_boot_pages;
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);
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);
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);
218 bool is_hugetlb_entry_migration(pte_t pte);
219 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
221 #else /* !CONFIG_HUGETLB_PAGE */
223 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
227 static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
231 static inline unsigned long hugetlb_total_pages(void)
236 static inline struct address_space *hugetlb_page_mapping_lock_write(
242 static inline int huge_pmd_unshare(struct mm_struct *mm,
243 struct vm_area_struct *vma,
244 unsigned long addr, pte_t *ptep)
249 static inline void adjust_range_if_pmd_sharing_possible(
250 struct vm_area_struct *vma,
251 unsigned long *start, unsigned long *end)
255 static inline struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
256 unsigned long address, unsigned int flags)
258 BUILD_BUG(); /* should never be compiled in if !CONFIG_HUGETLB_PAGE*/
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,
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)
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,
290 static inline void hugetlb_report_meminfo(struct seq_file *m)
294 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
299 static inline void hugetlb_show_meminfo_node(int nid)
303 static inline int prepare_hugepage_range(struct file *file,
304 unsigned long addr, unsigned long len)
309 static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma)
313 static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
317 static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma)
321 static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
325 static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
330 static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
334 static inline int pmd_huge(pmd_t pmd)
339 static inline int pud_huge(pud_t pud)
344 static inline int is_hugepage_only_range(struct mm_struct *mm,
345 unsigned long addr, unsigned long len)
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)
357 #ifdef CONFIG_USERFAULTFD
358 static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
360 struct vm_area_struct *dst_vma,
361 unsigned long dst_addr,
362 unsigned long src_addr,
363 enum mcopy_atomic_mode mode,
370 #endif /* CONFIG_USERFAULTFD */
372 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
378 static inline int isolate_hugetlb(struct page *page, struct list_head *list)
383 static inline int get_hwpoison_huge_page(struct page *page, bool *hugetlb, bool unpoison)
388 static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
389 bool *migratable_cleared)
394 static inline void putback_active_hugepage(struct page *page)
398 static inline void move_hugetlb_state(struct folio *old_folio,
399 struct folio *new_folio, int reason)
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)
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)
419 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
420 struct vm_area_struct *vma, unsigned long address,
427 static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
429 #endif /* !CONFIG_HUGETLB_PAGE */
431 * hugepages at page global directory. If arch support
432 * hugepages at pgd level, they need to define this.
435 #define pgd_huge(x) 0
438 #define p4d_huge(x) 0
442 static inline int pgd_write(pgd_t pgd)
449 #define HUGETLB_ANON_FILE "anon_hugepage"
453 * The file will be used as an shm file so shmfs accounting rules
456 HUGETLB_SHMFS_INODE = 1,
458 * The file is being created on the internal vfs mount and shmfs
459 * accounting rules do not apply
461 HUGETLB_ANONHUGE_INODE = 2,
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;
476 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
478 return sb->s_fs_info;
481 struct hugetlbfs_inode_info {
482 struct shared_policy policy;
483 struct inode vfs_inode;
487 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
489 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
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);
497 static inline bool is_file_hugepages(struct file *file)
499 if (file->f_op == &hugetlbfs_file_operations)
502 return is_file_shm_hugepages(file);
505 static inline struct hstate *hstate_inode(struct inode *i)
507 return HUGETLBFS_SB(i->i_sb)->hstate;
509 #else /* !CONFIG_HUGETLBFS */
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)
516 return ERR_PTR(-ENOSYS);
519 static inline struct hstate *hstate_inode(struct inode *i)
523 #endif /* !CONFIG_HUGETLBFS */
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 */
532 generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
533 unsigned long len, unsigned long pgoff,
534 unsigned long flags);
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.
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
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.
566 enum hugetlb_page_flags {
567 HPG_restore_reserve = 0,
571 HPG_vmemmap_optimized,
572 HPG_raw_hwp_unreliable,
577 * Macros to create test, set and clear function definitions for
578 * hugetlb specific page flags.
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); \
587 static inline int HPage##uname(struct page *page) \
588 { return test_bit(HPG_##flname, &(page->private)); }
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); \
596 static inline void SetHPage##uname(struct page *page) \
597 { set_bit(HPG_##flname, &(page->private)); }
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); \
605 static inline void ClearHPage##uname(struct page *page) \
606 { clear_bit(HPG_##flname, &(page->private)); }
608 #define TESTHPAGEFLAG(uname, flname) \
610 folio_test_hugetlb_##flname(struct folio *folio) \
612 static inline int HPage##uname(struct page *page) \
615 #define SETHPAGEFLAG(uname, flname) \
617 folio_set_hugetlb_##flname(struct folio *folio) \
619 static inline void SetHPage##uname(struct page *page) \
622 #define CLEARHPAGEFLAG(uname, flname) \
624 folio_clear_hugetlb_##flname(struct folio *folio) \
626 static inline void ClearHPage##uname(struct page *page) \
630 #define HPAGEFLAG(uname, flname) \
631 TESTHPAGEFLAG(uname, flname) \
632 SETHPAGEFLAG(uname, flname) \
633 CLEARHPAGEFLAG(uname, flname) \
636 * Create functions associated with hugetlb page flags
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)
645 #ifdef CONFIG_HUGETLB_PAGE
647 #define HSTATE_NAME_LEN 32
648 /* Defines one hugetlb page size */
650 struct mutex resize_lock;
651 int next_nid_to_alloc;
652 int next_nid_to_free;
654 unsigned int demote_order;
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];
673 char name[HSTATE_NAME_LEN];
676 struct huge_bootmem_page {
677 struct list_head list;
678 struct hstate *hstate;
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,
690 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
691 unsigned long address, struct page *page);
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);
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);
702 #ifndef HUGE_MAX_HSTATE
703 #define HUGE_MAX_HSTATE 1
706 extern struct hstate hstates[HUGE_MAX_HSTATE];
707 extern unsigned int default_hstate_idx;
709 #define default_hstate (hstates[default_hstate_idx])
711 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
713 return folio->_hugetlb_subpool;
717 * hugetlb page subpool pointer located in hpage[2].hugetlb_subpool
719 static inline struct hugepage_subpool *hugetlb_page_subpool(struct page *hpage)
721 return hugetlb_folio_subpool(page_folio(hpage));
724 static inline void hugetlb_set_folio_subpool(struct folio *folio,
725 struct hugepage_subpool *subpool)
727 folio->_hugetlb_subpool = subpool;
730 static inline void hugetlb_set_page_subpool(struct page *hpage,
731 struct hugepage_subpool *subpool)
733 hugetlb_set_folio_subpool(page_folio(hpage), subpool);
736 static inline struct hstate *hstate_file(struct file *f)
738 return hstate_inode(file_inode(f));
741 static inline struct hstate *hstate_sizelog(int page_size_log)
744 return &default_hstate;
746 return size_to_hstate(1UL << page_size_log);
749 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
751 return hstate_file(vma->vm_file);
754 static inline unsigned long huge_page_size(const struct hstate *h)
756 return (unsigned long)PAGE_SIZE << h->order;
759 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
761 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
763 static inline unsigned long huge_page_mask(struct hstate *h)
768 static inline unsigned int huge_page_order(struct hstate *h)
773 static inline unsigned huge_page_shift(struct hstate *h)
775 return h->order + PAGE_SHIFT;
778 static inline bool hstate_is_gigantic(struct hstate *h)
780 return huge_page_order(h) >= MAX_ORDER;
783 static inline unsigned int pages_per_huge_page(const struct hstate *h)
785 return 1 << h->order;
788 static inline unsigned int blocks_per_huge_page(struct hstate *h)
790 return huge_page_size(h) / 512;
793 #include <asm/hugetlb.h>
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)
801 #define is_hugepage_only_range is_hugepage_only_range
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
809 #ifndef arch_make_huge_pte
810 static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift,
813 return pte_mkhuge(entry);
817 static inline struct hstate *folio_hstate(struct folio *folio)
819 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
820 return size_to_hstate(folio_size(folio));
823 static inline struct hstate *page_hstate(struct page *page)
825 return folio_hstate(page_folio(page));
828 static inline unsigned hstate_index_to_shift(unsigned index)
830 return hstates[index].order + PAGE_SHIFT;
833 static inline int hstate_index(struct hstate *h)
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);
842 #ifdef CONFIG_MEMORY_FAILURE
843 extern void hugetlb_clear_page_hwpoison(struct page *hpage);
845 static inline void hugetlb_clear_page_hwpoison(struct page *hpage)
850 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
851 #ifndef arch_hugetlb_migration_supported
852 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
854 if ((huge_page_shift(h) == PMD_SHIFT) ||
855 (huge_page_shift(h) == PUD_SHIFT) ||
856 (huge_page_shift(h) == PGDIR_SHIFT))
863 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
869 static inline bool hugepage_migration_supported(struct hstate *h)
871 return arch_hugetlb_migration_supported(h);
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.
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.
889 static inline bool hugepage_movable_supported(struct hstate *h)
891 if (!hugepage_migration_supported(h))
894 if (hstate_is_gigantic(h))
899 /* Movability of hugepages depends on migration support. */
900 static inline gfp_t htlb_alloc_mask(struct hstate *h)
902 if (hugepage_movable_supported(h))
903 return GFP_HIGHUSER_MOVABLE;
908 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
910 gfp_t modified_mask = htlb_alloc_mask(h);
912 /* Some callers might want to enforce node */
913 modified_mask |= (gfp_mask & __GFP_THISNODE);
915 modified_mask |= (gfp_mask & __GFP_NOWARN);
917 return modified_mask;
920 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
921 struct mm_struct *mm, pte_t *pte)
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;
929 #ifndef hugepages_supported
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
935 #define hugepages_supported() (HPAGE_SHIFT != 0)
938 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
940 static inline void hugetlb_count_init(struct mm_struct *mm)
942 atomic_long_set(&mm->hugetlb_usage, 0);
945 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
947 atomic_long_add(l, &mm->hugetlb_usage);
950 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
952 atomic_long_sub(l, &mm->hugetlb_usage);
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)
960 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
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)
970 set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
975 void hugetlb_register_node(struct node *node);
976 void hugetlb_unregister_node(struct node *node);
979 #else /* CONFIG_HUGETLB_PAGE */
982 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
987 static inline struct hugepage_subpool *hugetlb_page_subpool(struct page *hpage)
992 static inline int isolate_or_dissolve_huge_page(struct page *page,
993 struct list_head *list)
998 static inline struct page *alloc_huge_page(struct vm_area_struct *vma,
1005 static inline struct page *
1006 alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
1007 nodemask_t *nmask, gfp_t gfp_mask)
1012 static inline struct page *alloc_huge_page_vma(struct hstate *h,
1013 struct vm_area_struct *vma,
1014 unsigned long address)
1019 static inline int __alloc_bootmem_huge_page(struct hstate *h)
1024 static inline struct hstate *hstate_file(struct file *f)
1029 static inline struct hstate *hstate_sizelog(int page_size_log)
1034 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
1039 static inline struct hstate *folio_hstate(struct folio *folio)
1044 static inline struct hstate *page_hstate(struct page *page)
1049 static inline struct hstate *size_to_hstate(unsigned long size)
1054 static inline unsigned long huge_page_size(struct hstate *h)
1059 static inline unsigned long huge_page_mask(struct hstate *h)
1064 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1069 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1074 static inline unsigned int huge_page_order(struct hstate *h)
1079 static inline unsigned int huge_page_shift(struct hstate *h)
1084 static inline bool hstate_is_gigantic(struct hstate *h)
1089 static inline unsigned int pages_per_huge_page(struct hstate *h)
1094 static inline unsigned hstate_index_to_shift(unsigned index)
1099 static inline int hstate_index(struct hstate *h)
1104 static inline int dissolve_free_huge_page(struct page *page)
1109 static inline int dissolve_free_huge_pages(unsigned long start_pfn,
1110 unsigned long end_pfn)
1115 static inline bool hugepage_migration_supported(struct hstate *h)
1120 static inline bool hugepage_movable_supported(struct hstate *h)
1125 static inline gfp_t htlb_alloc_mask(struct hstate *h)
1130 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
1135 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
1136 struct mm_struct *mm, pte_t *pte)
1138 return &mm->page_table_lock;
1141 static inline void hugetlb_count_init(struct mm_struct *mm)
1145 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
1149 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1153 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
1154 unsigned long addr, pte_t *ptep)
1159 static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
1160 pte_t *ptep, pte_t pte)
1164 static inline void hugetlb_register_node(struct node *node)
1168 static inline void hugetlb_unregister_node(struct node *node)
1171 #endif /* CONFIG_HUGETLB_PAGE */
1173 static inline spinlock_t *huge_pte_lock(struct hstate *h,
1174 struct mm_struct *mm, pte_t *pte)
1178 ptl = huge_pte_lockptr(h, mm, pte);
1183 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1184 extern void __init hugetlb_cma_reserve(int order);
1186 static inline __init void hugetlb_cma_reserve(int order)
1191 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
1192 static inline bool hugetlb_pmd_shared(pte_t *pte)
1194 return page_count(virt_to_page(pte)) > 1;
1197 static inline bool hugetlb_pmd_shared(pte_t *pte)
1203 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
1205 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
1207 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
1210 #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1213 #endif /* _LINUX_HUGETLB_H */