1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGETLB_H
3 #define _LINUX_HUGETLB_H
6 #include <linux/mm_types.h>
7 #include <linux/mmdebug.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>
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) })
29 #ifdef CONFIG_HUGETLB_PAGE
31 #include <linux/mempolicy.h>
32 #include <linux/shm.h>
33 #include <asm/tlbflush.h>
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.
40 #define __NR_USED_SUBPAGE 3
42 struct hugepage_subpool {
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. */
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
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.
67 struct page_counter *reservation_counter;
68 unsigned long pages_per_hpage;
69 struct cgroup_subsys_state *css;
74 * Region tracking -- allows tracking of reservations and instantiated pages
75 * across the pages in a mapping.
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.
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.
89 * Interval notation of the form [from, to) will be used to indicate that
90 * the endpoint from is inclusive and to is exclusive.
93 struct list_head link;
96 #ifdef CONFIG_CGROUP_HUGETLB
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.
102 struct page_counter *reservation_counter;
103 struct cgroup_subsys_state *css;
107 struct hugetlb_vma_lock {
109 struct rw_semaphore rw_sema;
110 struct vm_area_struct *vma;
113 extern struct resv_map *resv_map_alloc(void);
114 void resv_map_release(struct kref *ref);
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)++)
121 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
123 void hugepage_put_subpool(struct hugepage_subpool *spool);
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 *,
130 int hugetlb_treat_movable_handler(struct ctl_table *, int, void *, size_t *,
132 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int, void *, size_t *,
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,
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,
147 void unmap_hugepage_range(struct vm_area_struct *,
148 unsigned long, unsigned long, struct page *,
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_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte,
162 struct vm_area_struct *dst_vma,
163 unsigned long dst_addr,
164 unsigned long src_addr,
165 enum mcopy_atomic_mode mode,
168 #endif /* CONFIG_USERFAULTFD */
169 bool hugetlb_reserve_pages(struct inode *inode, long from, long to,
170 struct vm_area_struct *vma,
171 vm_flags_t vm_flags);
172 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
174 bool isolate_hugetlb(struct folio *folio, struct list_head *list);
175 int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison);
176 int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
177 bool *migratable_cleared);
178 void folio_putback_active_hugetlb(struct folio *folio);
179 void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason);
180 void free_huge_page(struct page *page);
181 void hugetlb_fix_reserve_counts(struct inode *inode);
182 extern struct mutex *hugetlb_fault_mutex_table;
183 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx);
185 pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
186 unsigned long addr, pud_t *pud);
188 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage);
190 extern int sysctl_hugetlb_shm_group;
191 extern struct list_head huge_boot_pages;
195 pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
196 unsigned long addr, unsigned long sz);
198 * huge_pte_offset(): Walk the hugetlb pgtable until the last level PTE.
199 * Returns the pte_t* if found, or NULL if the address is not mapped.
201 * IMPORTANT: we should normally not directly call this function, instead
202 * this is only a common interface to implement arch-specific
203 * walker. Please use hugetlb_walk() instead, because that will attempt to
204 * verify the locking for you.
206 * Since this function will walk all the pgtable pages (including not only
207 * high-level pgtable page, but also PUD entry that can be unshared
208 * concurrently for VM_SHARED), the caller of this function should be
209 * responsible of its thread safety. One can follow this rule:
211 * (1) For private mappings: pmd unsharing is not possible, so holding the
212 * mmap_lock for either read or write is sufficient. Most callers
213 * already hold the mmap_lock, so normally, no special action is
216 * (2) For shared mappings: pmd unsharing is possible (so the PUD-ranged
217 * pgtable page can go away from under us! It can be done by a pmd
218 * unshare with a follow up munmap() on the other process), then we
221 * (2.1) hugetlb vma lock read or write held, to make sure pmd unshare
222 * won't happen upon the range (it also makes sure the pte_t we
223 * read is the right and stable one), or,
225 * (2.2) hugetlb mapping i_mmap_rwsem lock held read or write, to make
226 * sure even if unshare happened the racy unmap() will wait until
227 * i_mmap_rwsem is released.
229 * Option (2.1) is the safest, which guarantees pte stability from pmd
230 * sharing pov, until the vma lock released. Option (2.2) doesn't protect
231 * a concurrent pmd unshare, but it makes sure the pgtable page is safe to
234 pte_t *huge_pte_offset(struct mm_struct *mm,
235 unsigned long addr, unsigned long sz);
236 unsigned long hugetlb_mask_last_page(struct hstate *h);
237 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
238 unsigned long addr, pte_t *ptep);
239 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
240 unsigned long *start, unsigned long *end);
242 void hugetlb_vma_lock_read(struct vm_area_struct *vma);
243 void hugetlb_vma_unlock_read(struct vm_area_struct *vma);
244 void hugetlb_vma_lock_write(struct vm_area_struct *vma);
245 void hugetlb_vma_unlock_write(struct vm_area_struct *vma);
246 int hugetlb_vma_trylock_write(struct vm_area_struct *vma);
247 void hugetlb_vma_assert_locked(struct vm_area_struct *vma);
248 void hugetlb_vma_lock_release(struct kref *kref);
250 int pmd_huge(pmd_t pmd);
251 int pud_huge(pud_t pud);
252 long hugetlb_change_protection(struct vm_area_struct *vma,
253 unsigned long address, unsigned long end, pgprot_t newprot,
254 unsigned long cp_flags);
256 bool is_hugetlb_entry_migration(pte_t pte);
257 void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
259 #else /* !CONFIG_HUGETLB_PAGE */
261 static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
265 static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
269 static inline unsigned long hugetlb_total_pages(void)
274 static inline struct address_space *hugetlb_page_mapping_lock_write(
280 static inline int huge_pmd_unshare(struct mm_struct *mm,
281 struct vm_area_struct *vma,
282 unsigned long addr, pte_t *ptep)
287 static inline void adjust_range_if_pmd_sharing_possible(
288 struct vm_area_struct *vma,
289 unsigned long *start, unsigned long *end)
293 static inline struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,
294 unsigned long address, unsigned int flags)
296 BUILD_BUG(); /* should never be compiled in if !CONFIG_HUGETLB_PAGE*/
299 static inline long follow_hugetlb_page(struct mm_struct *mm,
300 struct vm_area_struct *vma, struct page **pages,
301 struct vm_area_struct **vmas, unsigned long *position,
302 unsigned long *nr_pages, long i, unsigned int flags,
309 static inline int copy_hugetlb_page_range(struct mm_struct *dst,
310 struct mm_struct *src,
311 struct vm_area_struct *dst_vma,
312 struct vm_area_struct *src_vma)
318 static inline int move_hugetlb_page_tables(struct vm_area_struct *vma,
319 struct vm_area_struct *new_vma,
320 unsigned long old_addr,
321 unsigned long new_addr,
328 static inline void hugetlb_report_meminfo(struct seq_file *m)
332 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
337 static inline void hugetlb_show_meminfo_node(int nid)
341 static inline int prepare_hugepage_range(struct file *file,
342 unsigned long addr, unsigned long len)
347 static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma)
351 static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
355 static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma)
359 static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
363 static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
368 static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
372 static inline int pmd_huge(pmd_t pmd)
377 static inline int pud_huge(pud_t pud)
382 static inline int is_hugepage_only_range(struct mm_struct *mm,
383 unsigned long addr, unsigned long len)
388 static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb,
389 unsigned long addr, unsigned long end,
390 unsigned long floor, unsigned long ceiling)
395 #ifdef CONFIG_USERFAULTFD
396 static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
398 struct vm_area_struct *dst_vma,
399 unsigned long dst_addr,
400 unsigned long src_addr,
401 enum mcopy_atomic_mode mode,
408 #endif /* CONFIG_USERFAULTFD */
410 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
416 static inline bool isolate_hugetlb(struct folio *folio, struct list_head *list)
421 static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
426 static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
427 bool *migratable_cleared)
432 static inline void folio_putback_active_hugetlb(struct folio *folio)
436 static inline void move_hugetlb_state(struct folio *old_folio,
437 struct folio *new_folio, int reason)
441 static inline long hugetlb_change_protection(
442 struct vm_area_struct *vma, unsigned long address,
443 unsigned long end, pgprot_t newprot,
444 unsigned long cp_flags)
449 static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
450 struct vm_area_struct *vma, unsigned long start,
451 unsigned long end, struct page *ref_page,
452 zap_flags_t zap_flags)
457 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
458 struct vm_area_struct *vma, unsigned long address,
465 static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
467 #endif /* !CONFIG_HUGETLB_PAGE */
469 * hugepages at page global directory. If arch support
470 * hugepages at pgd level, they need to define this.
473 #define pgd_huge(x) 0
476 #define p4d_huge(x) 0
480 static inline int pgd_write(pgd_t pgd)
487 #define HUGETLB_ANON_FILE "anon_hugepage"
491 * The file will be used as an shm file so shmfs accounting rules
494 HUGETLB_SHMFS_INODE = 1,
496 * The file is being created on the internal vfs mount and shmfs
497 * accounting rules do not apply
499 HUGETLB_ANONHUGE_INODE = 2,
502 #ifdef CONFIG_HUGETLBFS
503 struct hugetlbfs_sb_info {
504 long max_inodes; /* inodes allowed */
505 long free_inodes; /* inodes free */
506 spinlock_t stat_lock;
507 struct hstate *hstate;
508 struct hugepage_subpool *spool;
514 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
516 return sb->s_fs_info;
519 struct hugetlbfs_inode_info {
520 struct shared_policy policy;
521 struct inode vfs_inode;
525 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
527 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
530 extern const struct file_operations hugetlbfs_file_operations;
531 extern const struct vm_operations_struct hugetlb_vm_ops;
532 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
533 int creat_flags, int page_size_log);
535 static inline bool is_file_hugepages(struct file *file)
537 if (file->f_op == &hugetlbfs_file_operations)
540 return is_file_shm_hugepages(file);
543 static inline struct hstate *hstate_inode(struct inode *i)
545 return HUGETLBFS_SB(i->i_sb)->hstate;
547 #else /* !CONFIG_HUGETLBFS */
549 #define is_file_hugepages(file) false
550 static inline struct file *
551 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
552 int creat_flags, int page_size_log)
554 return ERR_PTR(-ENOSYS);
557 static inline struct hstate *hstate_inode(struct inode *i)
561 #endif /* !CONFIG_HUGETLBFS */
563 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
564 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
565 unsigned long len, unsigned long pgoff,
566 unsigned long flags);
567 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
570 generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
571 unsigned long len, unsigned long pgoff,
572 unsigned long flags);
575 * huegtlb page specific state flags. These flags are located in page.private
576 * of the hugetlb head page. Functions created via the below macros should be
577 * used to manipulate these flags.
579 * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
580 * allocation time. Cleared when page is fully instantiated. Free
581 * routine checks flag to restore a reservation on error paths.
582 * Synchronization: Examined or modified by code that knows it has
583 * the only reference to page. i.e. After allocation but before use
584 * or when the page is being freed.
585 * HPG_migratable - Set after a newly allocated page is added to the page
586 * cache and/or page tables. Indicates the page is a candidate for
588 * Synchronization: Initially set after new page allocation with no
589 * locking. When examined and modified during migration processing
590 * (isolate, migrate, putback) the hugetlb_lock is held.
591 * HPG_temporary - Set on a page that is temporarily allocated from the buddy
592 * allocator. Typically used for migration target pages when no pages
593 * are available in the pool. The hugetlb free page path will
594 * immediately free pages with this flag set to the buddy allocator.
595 * Synchronization: Can be set after huge page allocation from buddy when
596 * code knows it has only reference. All other examinations and
597 * modifications require hugetlb_lock.
598 * HPG_freed - Set when page is on the free lists.
599 * Synchronization: hugetlb_lock held for examination and modification.
600 * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed.
601 * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page
602 * that is not tracked by raw_hwp_page list.
604 enum hugetlb_page_flags {
605 HPG_restore_reserve = 0,
609 HPG_vmemmap_optimized,
610 HPG_raw_hwp_unreliable,
615 * Macros to create test, set and clear function definitions for
616 * hugetlb specific page flags.
618 #ifdef CONFIG_HUGETLB_PAGE
619 #define TESTHPAGEFLAG(uname, flname) \
620 static __always_inline \
621 bool folio_test_hugetlb_##flname(struct folio *folio) \
622 { void *private = &folio->private; \
623 return test_bit(HPG_##flname, private); \
625 static inline int HPage##uname(struct page *page) \
626 { return test_bit(HPG_##flname, &(page->private)); }
628 #define SETHPAGEFLAG(uname, flname) \
629 static __always_inline \
630 void folio_set_hugetlb_##flname(struct folio *folio) \
631 { void *private = &folio->private; \
632 set_bit(HPG_##flname, private); \
634 static inline void SetHPage##uname(struct page *page) \
635 { set_bit(HPG_##flname, &(page->private)); }
637 #define CLEARHPAGEFLAG(uname, flname) \
638 static __always_inline \
639 void folio_clear_hugetlb_##flname(struct folio *folio) \
640 { void *private = &folio->private; \
641 clear_bit(HPG_##flname, private); \
643 static inline void ClearHPage##uname(struct page *page) \
644 { clear_bit(HPG_##flname, &(page->private)); }
646 #define TESTHPAGEFLAG(uname, flname) \
648 folio_test_hugetlb_##flname(struct folio *folio) \
650 static inline int HPage##uname(struct page *page) \
653 #define SETHPAGEFLAG(uname, flname) \
655 folio_set_hugetlb_##flname(struct folio *folio) \
657 static inline void SetHPage##uname(struct page *page) \
660 #define CLEARHPAGEFLAG(uname, flname) \
662 folio_clear_hugetlb_##flname(struct folio *folio) \
664 static inline void ClearHPage##uname(struct page *page) \
668 #define HPAGEFLAG(uname, flname) \
669 TESTHPAGEFLAG(uname, flname) \
670 SETHPAGEFLAG(uname, flname) \
671 CLEARHPAGEFLAG(uname, flname) \
674 * Create functions associated with hugetlb page flags
676 HPAGEFLAG(RestoreReserve, restore_reserve)
677 HPAGEFLAG(Migratable, migratable)
678 HPAGEFLAG(Temporary, temporary)
679 HPAGEFLAG(Freed, freed)
680 HPAGEFLAG(VmemmapOptimized, vmemmap_optimized)
681 HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable)
683 #ifdef CONFIG_HUGETLB_PAGE
685 #define HSTATE_NAME_LEN 32
686 /* Defines one hugetlb page size */
688 struct mutex resize_lock;
689 int next_nid_to_alloc;
690 int next_nid_to_free;
692 unsigned int demote_order;
694 unsigned long max_huge_pages;
695 unsigned long nr_huge_pages;
696 unsigned long free_huge_pages;
697 unsigned long resv_huge_pages;
698 unsigned long surplus_huge_pages;
699 unsigned long nr_overcommit_huge_pages;
700 struct list_head hugepage_activelist;
701 struct list_head hugepage_freelists[MAX_NUMNODES];
702 unsigned int max_huge_pages_node[MAX_NUMNODES];
703 unsigned int nr_huge_pages_node[MAX_NUMNODES];
704 unsigned int free_huge_pages_node[MAX_NUMNODES];
705 unsigned int surplus_huge_pages_node[MAX_NUMNODES];
706 #ifdef CONFIG_CGROUP_HUGETLB
707 /* cgroup control files */
708 struct cftype cgroup_files_dfl[8];
709 struct cftype cgroup_files_legacy[10];
711 char name[HSTATE_NAME_LEN];
714 struct huge_bootmem_page {
715 struct list_head list;
716 struct hstate *hstate;
719 int isolate_or_dissolve_huge_page(struct page *page, struct list_head *list);
720 struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
721 unsigned long addr, int avoid_reserve);
722 struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
723 nodemask_t *nmask, gfp_t gfp_mask);
724 struct folio *alloc_hugetlb_folio_vma(struct hstate *h, struct vm_area_struct *vma,
725 unsigned long address);
726 int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
728 void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
729 unsigned long address, struct folio *folio);
732 int __init __alloc_bootmem_huge_page(struct hstate *h, int nid);
733 int __init alloc_bootmem_huge_page(struct hstate *h, int nid);
734 bool __init hugetlb_node_alloc_supported(void);
736 void __init hugetlb_add_hstate(unsigned order);
737 bool __init arch_hugetlb_valid_size(unsigned long size);
738 struct hstate *size_to_hstate(unsigned long size);
740 #ifndef HUGE_MAX_HSTATE
741 #define HUGE_MAX_HSTATE 1
744 extern struct hstate hstates[HUGE_MAX_HSTATE];
745 extern unsigned int default_hstate_idx;
747 #define default_hstate (hstates[default_hstate_idx])
749 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
751 return folio->_hugetlb_subpool;
755 * hugetlb page subpool pointer located in hpage[2].hugetlb_subpool
757 static inline struct hugepage_subpool *hugetlb_page_subpool(struct page *hpage)
759 return hugetlb_folio_subpool(page_folio(hpage));
762 static inline void hugetlb_set_folio_subpool(struct folio *folio,
763 struct hugepage_subpool *subpool)
765 folio->_hugetlb_subpool = subpool;
768 static inline void hugetlb_set_page_subpool(struct page *hpage,
769 struct hugepage_subpool *subpool)
771 hugetlb_set_folio_subpool(page_folio(hpage), subpool);
774 static inline struct hstate *hstate_file(struct file *f)
776 return hstate_inode(file_inode(f));
779 static inline struct hstate *hstate_sizelog(int page_size_log)
782 return &default_hstate;
784 if (page_size_log < BITS_PER_LONG)
785 return size_to_hstate(1UL << page_size_log);
790 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
792 return hstate_file(vma->vm_file);
795 static inline unsigned long huge_page_size(const struct hstate *h)
797 return (unsigned long)PAGE_SIZE << h->order;
800 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
802 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
804 static inline unsigned long huge_page_mask(struct hstate *h)
809 static inline unsigned int huge_page_order(struct hstate *h)
814 static inline unsigned huge_page_shift(struct hstate *h)
816 return h->order + PAGE_SHIFT;
819 static inline bool hstate_is_gigantic(struct hstate *h)
821 return huge_page_order(h) >= MAX_ORDER;
824 static inline unsigned int pages_per_huge_page(const struct hstate *h)
826 return 1 << h->order;
829 static inline unsigned int blocks_per_huge_page(struct hstate *h)
831 return huge_page_size(h) / 512;
834 #include <asm/hugetlb.h>
836 #ifndef is_hugepage_only_range
837 static inline int is_hugepage_only_range(struct mm_struct *mm,
838 unsigned long addr, unsigned long len)
842 #define is_hugepage_only_range is_hugepage_only_range
845 #ifndef arch_clear_hugepage_flags
846 static inline void arch_clear_hugepage_flags(struct page *page) { }
847 #define arch_clear_hugepage_flags arch_clear_hugepage_flags
850 #ifndef arch_make_huge_pte
851 static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift,
854 return pte_mkhuge(entry);
858 static inline struct hstate *folio_hstate(struct folio *folio)
860 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
861 return size_to_hstate(folio_size(folio));
864 static inline struct hstate *page_hstate(struct page *page)
866 return folio_hstate(page_folio(page));
869 static inline unsigned hstate_index_to_shift(unsigned index)
871 return hstates[index].order + PAGE_SHIFT;
874 static inline int hstate_index(struct hstate *h)
879 extern int dissolve_free_huge_page(struct page *page);
880 extern int dissolve_free_huge_pages(unsigned long start_pfn,
881 unsigned long end_pfn);
883 #ifdef CONFIG_MEMORY_FAILURE
884 extern void folio_clear_hugetlb_hwpoison(struct folio *folio);
886 static inline void folio_clear_hugetlb_hwpoison(struct folio *folio)
891 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
892 #ifndef arch_hugetlb_migration_supported
893 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
895 if ((huge_page_shift(h) == PMD_SHIFT) ||
896 (huge_page_shift(h) == PUD_SHIFT) ||
897 (huge_page_shift(h) == PGDIR_SHIFT))
904 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
910 static inline bool hugepage_migration_supported(struct hstate *h)
912 return arch_hugetlb_migration_supported(h);
916 * Movability check is different as compared to migration check.
917 * It determines whether or not a huge page should be placed on
918 * movable zone or not. Movability of any huge page should be
919 * required only if huge page size is supported for migration.
920 * There won't be any reason for the huge page to be movable if
921 * it is not migratable to start with. Also the size of the huge
922 * page should be large enough to be placed under a movable zone
923 * and still feasible enough to be migratable. Just the presence
924 * in movable zone does not make the migration feasible.
926 * So even though large huge page sizes like the gigantic ones
927 * are migratable they should not be movable because its not
928 * feasible to migrate them from movable zone.
930 static inline bool hugepage_movable_supported(struct hstate *h)
932 if (!hugepage_migration_supported(h))
935 if (hstate_is_gigantic(h))
940 /* Movability of hugepages depends on migration support. */
941 static inline gfp_t htlb_alloc_mask(struct hstate *h)
943 if (hugepage_movable_supported(h))
944 return GFP_HIGHUSER_MOVABLE;
949 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
951 gfp_t modified_mask = htlb_alloc_mask(h);
953 /* Some callers might want to enforce node */
954 modified_mask |= (gfp_mask & __GFP_THISNODE);
956 modified_mask |= (gfp_mask & __GFP_NOWARN);
958 return modified_mask;
961 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
962 struct mm_struct *mm, pte_t *pte)
964 if (huge_page_size(h) == PMD_SIZE)
965 return pmd_lockptr(mm, (pmd_t *) pte);
966 VM_BUG_ON(huge_page_size(h) == PAGE_SIZE);
967 return &mm->page_table_lock;
970 #ifndef hugepages_supported
972 * Some platform decide whether they support huge pages at boot
973 * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
974 * when there is no such support
976 #define hugepages_supported() (HPAGE_SHIFT != 0)
979 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
981 static inline void hugetlb_count_init(struct mm_struct *mm)
983 atomic_long_set(&mm->hugetlb_usage, 0);
986 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
988 atomic_long_add(l, &mm->hugetlb_usage);
991 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
993 atomic_long_sub(l, &mm->hugetlb_usage);
996 #ifndef huge_ptep_modify_prot_start
997 #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start
998 static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
999 unsigned long addr, pte_t *ptep)
1001 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
1005 #ifndef huge_ptep_modify_prot_commit
1006 #define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit
1007 static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
1008 unsigned long addr, pte_t *ptep,
1009 pte_t old_pte, pte_t pte)
1011 set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
1016 void hugetlb_register_node(struct node *node);
1017 void hugetlb_unregister_node(struct node *node);
1020 #else /* CONFIG_HUGETLB_PAGE */
1023 static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
1028 static inline struct hugepage_subpool *hugetlb_page_subpool(struct page *hpage)
1033 static inline int isolate_or_dissolve_huge_page(struct page *page,
1034 struct list_head *list)
1039 static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
1046 static inline struct folio *
1047 alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
1048 nodemask_t *nmask, gfp_t gfp_mask)
1053 static inline struct folio *alloc_hugetlb_folio_vma(struct hstate *h,
1054 struct vm_area_struct *vma,
1055 unsigned long address)
1060 static inline int __alloc_bootmem_huge_page(struct hstate *h)
1065 static inline struct hstate *hstate_file(struct file *f)
1070 static inline struct hstate *hstate_sizelog(int page_size_log)
1075 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
1080 static inline struct hstate *folio_hstate(struct folio *folio)
1085 static inline struct hstate *page_hstate(struct page *page)
1090 static inline struct hstate *size_to_hstate(unsigned long size)
1095 static inline unsigned long huge_page_size(struct hstate *h)
1100 static inline unsigned long huge_page_mask(struct hstate *h)
1105 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1110 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1115 static inline unsigned int huge_page_order(struct hstate *h)
1120 static inline unsigned int huge_page_shift(struct hstate *h)
1125 static inline bool hstate_is_gigantic(struct hstate *h)
1130 static inline unsigned int pages_per_huge_page(struct hstate *h)
1135 static inline unsigned hstate_index_to_shift(unsigned index)
1140 static inline int hstate_index(struct hstate *h)
1145 static inline int dissolve_free_huge_page(struct page *page)
1150 static inline int dissolve_free_huge_pages(unsigned long start_pfn,
1151 unsigned long end_pfn)
1156 static inline bool hugepage_migration_supported(struct hstate *h)
1161 static inline bool hugepage_movable_supported(struct hstate *h)
1166 static inline gfp_t htlb_alloc_mask(struct hstate *h)
1171 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
1176 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
1177 struct mm_struct *mm, pte_t *pte)
1179 return &mm->page_table_lock;
1182 static inline void hugetlb_count_init(struct mm_struct *mm)
1186 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
1190 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1194 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
1195 unsigned long addr, pte_t *ptep)
1200 static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
1201 pte_t *ptep, pte_t pte)
1205 static inline void hugetlb_register_node(struct node *node)
1209 static inline void hugetlb_unregister_node(struct node *node)
1212 #endif /* CONFIG_HUGETLB_PAGE */
1214 static inline spinlock_t *huge_pte_lock(struct hstate *h,
1215 struct mm_struct *mm, pte_t *pte)
1219 ptl = huge_pte_lockptr(h, mm, pte);
1224 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1225 extern void __init hugetlb_cma_reserve(int order);
1227 static inline __init void hugetlb_cma_reserve(int order)
1232 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
1233 static inline bool hugetlb_pmd_shared(pte_t *pte)
1235 return page_count(virt_to_page(pte)) > 1;
1238 static inline bool hugetlb_pmd_shared(pte_t *pte)
1244 bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
1246 #ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
1248 * ARCHes with special requirements for evicting HUGETLB backing TLB entries can
1251 #define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1254 static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
1256 return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
1260 * Safe version of huge_pte_offset() to check the locks. See comments
1261 * above huge_pte_offset().
1263 static inline pte_t *
1264 hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz)
1266 #if defined(CONFIG_HUGETLB_PAGE) && \
1267 defined(CONFIG_ARCH_WANT_HUGE_PMD_SHARE) && defined(CONFIG_LOCKDEP)
1268 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1271 * If pmd sharing possible, locking needed to safely walk the
1272 * hugetlb pgtables. More information can be found at the comment
1273 * above huge_pte_offset() in the same file.
1275 * NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP.
1277 if (__vma_shareable_lock(vma))
1278 WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) &&
1280 &vma->vm_file->f_mapping->i_mmap_rwsem));
1282 return huge_pte_offset(vma->vm_mm, addr, sz);
1285 #endif /* _LINUX_HUGETLB_H */