1
2#ifndef _LINUX_HUGETLB_H
3#define _LINUX_HUGETLB_H
4
5#include <linux/mm.h>
6#include <linux/mm_types.h>
7#include <linux/mmdebug.h>
8#include <linux/fs.h>
9#include <linux/hugetlb_inline.h>
10#include <linux/cgroup.h>
11#include <linux/page_ref.h>
12#include <linux/list.h>
13#include <linux/kref.h>
14#include <linux/pgtable.h>
15#include <linux/gfp.h>
16#include <linux/userfaultfd_k.h>
17#include <linux/nodemask.h>
18
19struct ctl_table;
20struct user_struct;
21struct mmu_gather;
22struct node;
23
24void free_huge_folio(struct folio *folio);
25
26#ifdef CONFIG_HUGETLB_PAGE
27
28#include <linux/pagemap.h>
29#include <linux/shm.h>
30#include <asm/tlbflush.h>
31
32
33
34
35
36
37#define __NR_USED_SUBPAGE 3
38
39struct hugepage_subpool {
40 spinlock_t lock;
41 long count;
42 long max_hpages;
43 long used_hpages;
44
45 struct hstate *hstate;
46 long min_hpages;
47 long rsv_hpages;
48
49};
50
51struct resv_map {
52 struct kref refs;
53 spinlock_t lock;
54 struct list_head regions;
55 long adds_in_progress;
56 struct list_head region_cache;
57 long region_cache_count;
58 struct rw_semaphore rw_sema;
59#ifdef CONFIG_CGROUP_HUGETLB
60
61
62
63
64
65 struct page_counter *reservation_counter;
66 unsigned long pages_per_hpage;
67 struct cgroup_subsys_state *css;
68#endif
69};
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90struct file_region {
91 struct list_head link;
92 long from;
93 long to;
94#ifdef CONFIG_CGROUP_HUGETLB
95
96
97
98
99
100 struct page_counter *reservation_counter;
101 struct cgroup_subsys_state *css;
102#endif
103};
104
105struct hugetlb_vma_lock {
106 struct kref refs;
107 struct rw_semaphore rw_sema;
108 struct vm_area_struct *vma;
109};
110
111extern struct resv_map *resv_map_alloc(void);
112void resv_map_release(struct kref *ref);
113
114extern spinlock_t hugetlb_lock;
115extern int hugetlb_max_hstate __read_mostly;
116#define for_each_hstate(h) \
117 for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
118
119struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
120 long min_hpages);
121void hugepage_put_subpool(struct hugepage_subpool *spool);
122
123void hugetlb_dup_vma_private(struct vm_area_struct *vma);
124void clear_vma_resv_huge_pages(struct vm_area_struct *vma);
125int move_hugetlb_page_tables(struct vm_area_struct *vma,
126 struct vm_area_struct *new_vma,
127 unsigned long old_addr, unsigned long new_addr,
128 unsigned long len);
129int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *,
130 struct vm_area_struct *, struct vm_area_struct *);
131void unmap_hugepage_range(struct vm_area_struct *,
132 unsigned long start, unsigned long end,
133 struct folio *, zap_flags_t);
134void __unmap_hugepage_range(struct mmu_gather *tlb,
135 struct vm_area_struct *vma,
136 unsigned long start, unsigned long end,
137 struct folio *, zap_flags_t zap_flags);
138void hugetlb_report_meminfo(struct seq_file *);
139int hugetlb_report_node_meminfo(char *buf, int len, int nid);
140void hugetlb_show_meminfo_node(int nid);
141unsigned long hugetlb_total_pages(void);
142vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
143 unsigned long address, unsigned int flags);
144#ifdef CONFIG_USERFAULTFD
145int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
146 struct vm_area_struct *dst_vma,
147 unsigned long dst_addr,
148 unsigned long src_addr,
149 uffd_flags_t flags,
150 struct folio **foliop);
151#endif
152long hugetlb_reserve_pages(struct inode *inode, long from, long to,
153 struct vm_area_struct *vma,
154 vm_flags_t vm_flags);
155long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
156 long freed);
157bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list);
158int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison);
159int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
160 bool *migratable_cleared);
161void folio_putback_hugetlb(struct folio *folio);
162void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, int reason);
163void hugetlb_fix_reserve_counts(struct inode *inode);
164extern struct mutex *hugetlb_fault_mutex_table;
165u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx);
166
167pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
168 unsigned long addr, pud_t *pud);
169bool hugetlbfs_pagecache_present(struct hstate *h,
170 struct vm_area_struct *vma,
171 unsigned long address);
172
173struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio);
174
175extern int sysctl_hugetlb_shm_group;
176extern struct list_head huge_boot_pages[MAX_NUMNODES];
177
178void hugetlb_bootmem_alloc(void);
179bool hugetlb_bootmem_allocated(void);
180extern nodemask_t hugetlb_bootmem_nodes;
181void hugetlb_bootmem_set_nodes(void);
182
183
184
185#ifndef CONFIG_HIGHPTE
186
187
188
189
190
191static inline pte_t *pte_offset_huge(pmd_t *pmd, unsigned long address)
192{
193 return pte_offset_kernel(pmd, address);
194}
195static inline pte_t *pte_alloc_huge(struct mm_struct *mm, pmd_t *pmd,
196 unsigned long address)
197{
198 return pte_alloc(mm, pmd) ? NULL : pte_offset_huge(pmd, address);
199}
200#endif
201
202pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
203 unsigned long addr, unsigned long sz);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241pte_t *huge_pte_offset(struct mm_struct *mm,
242 unsigned long addr, unsigned long sz);
243unsigned long hugetlb_mask_last_page(struct hstate *h);
244int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
245 unsigned long addr, pte_t *ptep);
246void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
247 unsigned long *start, unsigned long *end);
248
249extern void __hugetlb_zap_begin(struct vm_area_struct *vma,
250 unsigned long *begin, unsigned long *end);
251extern void __hugetlb_zap_end(struct vm_area_struct *vma,
252 struct zap_details *details);
253
254static inline void hugetlb_zap_begin(struct vm_area_struct *vma,
255 unsigned long *start, unsigned long *end)
256{
257 if (is_vm_hugetlb_page(vma))
258 __hugetlb_zap_begin(vma, start, end);
259}
260
261static inline void hugetlb_zap_end(struct vm_area_struct *vma,
262 struct zap_details *details)
263{
264 if (is_vm_hugetlb_page(vma))
265 __hugetlb_zap_end(vma, details);
266}
267
268void hugetlb_vma_lock_read(struct vm_area_struct *vma);
269void hugetlb_vma_unlock_read(struct vm_area_struct *vma);
270void hugetlb_vma_lock_write(struct vm_area_struct *vma);
271void hugetlb_vma_unlock_write(struct vm_area_struct *vma);
272int hugetlb_vma_trylock_write(struct vm_area_struct *vma);
273void hugetlb_vma_assert_locked(struct vm_area_struct *vma);
274void hugetlb_vma_lock_release(struct kref *kref);
275long hugetlb_change_protection(struct vm_area_struct *vma,
276 unsigned long address, unsigned long end, pgprot_t newprot,
277 unsigned long cp_flags);
278bool is_hugetlb_entry_migration(pte_t pte);
279bool is_hugetlb_entry_hwpoisoned(pte_t pte);
280void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
281void fixup_hugetlb_reservations(struct vm_area_struct *vma);
282void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
283
284#else
285
286static inline void hugetlb_dup_vma_private(struct vm_area_struct *vma)
287{
288}
289
290static inline void clear_vma_resv_huge_pages(struct vm_area_struct *vma)
291{
292}
293
294static inline unsigned long hugetlb_total_pages(void)
295{
296 return 0;
297}
298
299static inline struct address_space *hugetlb_folio_mapping_lock_write(
300 struct folio *folio)
301{
302 return NULL;
303}
304
305static inline int huge_pmd_unshare(struct mm_struct *mm,
306 struct vm_area_struct *vma,
307 unsigned long addr, pte_t *ptep)
308{
309 return 0;
310}
311
312static inline void adjust_range_if_pmd_sharing_possible(
313 struct vm_area_struct *vma,
314 unsigned long *start, unsigned long *end)
315{
316}
317
318static inline void hugetlb_zap_begin(
319 struct vm_area_struct *vma,
320 unsigned long *start, unsigned long *end)
321{
322}
323
324static inline void hugetlb_zap_end(
325 struct vm_area_struct *vma,
326 struct zap_details *details)
327{
328}
329
330static inline int copy_hugetlb_page_range(struct mm_struct *dst,
331 struct mm_struct *src,
332 struct vm_area_struct *dst_vma,
333 struct vm_area_struct *src_vma)
334{
335 BUG();
336 return 0;
337}
338
339static inline int move_hugetlb_page_tables(struct vm_area_struct *vma,
340 struct vm_area_struct *new_vma,
341 unsigned long old_addr,
342 unsigned long new_addr,
343 unsigned long len)
344{
345 BUG();
346 return 0;
347}
348
349static inline void hugetlb_report_meminfo(struct seq_file *m)
350{
351}
352
353static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
354{
355 return 0;
356}
357
358static inline void hugetlb_show_meminfo_node(int nid)
359{
360}
361
362static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma)
363{
364}
365
366static inline void hugetlb_vma_unlock_read(struct vm_area_struct *vma)
367{
368}
369
370static inline void hugetlb_vma_lock_write(struct vm_area_struct *vma)
371{
372}
373
374static inline void hugetlb_vma_unlock_write(struct vm_area_struct *vma)
375{
376}
377
378static inline int hugetlb_vma_trylock_write(struct vm_area_struct *vma)
379{
380 return 1;
381}
382
383static inline void hugetlb_vma_assert_locked(struct vm_area_struct *vma)
384{
385}
386
387static inline int is_hugepage_only_range(struct mm_struct *mm,
388 unsigned long addr, unsigned long len)
389{
390 return 0;
391}
392
393#ifdef CONFIG_USERFAULTFD
394static inline int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
395 struct vm_area_struct *dst_vma,
396 unsigned long dst_addr,
397 unsigned long src_addr,
398 uffd_flags_t flags,
399 struct folio **foliop)
400{
401 BUG();
402 return 0;
403}
404#endif
405
406static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
407 unsigned long sz)
408{
409 return NULL;
410}
411
412static inline bool folio_isolate_hugetlb(struct folio *folio, struct list_head *list)
413{
414 return false;
415}
416
417static inline int get_hwpoison_hugetlb_folio(struct folio *folio, bool *hugetlb, bool unpoison)
418{
419 return 0;
420}
421
422static inline int get_huge_page_for_hwpoison(unsigned long pfn, int flags,
423 bool *migratable_cleared)
424{
425 return 0;
426}
427
428static inline void folio_putback_hugetlb(struct folio *folio)
429{
430}
431
432static inline void move_hugetlb_state(struct folio *old_folio,
433 struct folio *new_folio, int reason)
434{
435}
436
437static inline long hugetlb_change_protection(
438 struct vm_area_struct *vma, unsigned long address,
439 unsigned long end, pgprot_t newprot,
440 unsigned long cp_flags)
441{
442 return 0;
443}
444
445static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
446 struct vm_area_struct *vma, unsigned long start,
447 unsigned long end, struct folio *folio,
448 zap_flags_t zap_flags)
449{
450 BUG();
451}
452
453static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
454 struct vm_area_struct *vma, unsigned long address,
455 unsigned int flags)
456{
457 BUG();
458 return 0;
459}
460
461static inline void hugetlb_unshare_all_pmds(struct vm_area_struct *vma) { }
462
463static inline void fixup_hugetlb_reservations(struct vm_area_struct *vma)
464{
465}
466
467static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
468
469#endif
470
471#ifndef pgd_write
472static inline int pgd_write(pgd_t pgd)
473{
474 BUG();
475 return 0;
476}
477#endif
478
479#define HUGETLB_ANON_FILE "anon_hugepage"
480
481enum {
482
483
484
485
486 HUGETLB_SHMFS_INODE = 1,
487
488
489
490
491 HUGETLB_ANONHUGE_INODE = 2,
492};
493
494#ifdef CONFIG_HUGETLBFS
495struct hugetlbfs_sb_info {
496 long max_inodes;
497 long free_inodes;
498 spinlock_t stat_lock;
499 struct hstate *hstate;
500 struct hugepage_subpool *spool;
501 kuid_t uid;
502 kgid_t gid;
503 umode_t mode;
504};
505
506static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
507{
508 return sb->s_fs_info;
509}
510
511struct hugetlbfs_inode_info {
512 struct inode vfs_inode;
513 unsigned int seals;
514};
515
516static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
517{
518 return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
519}
520
521extern const struct vm_operations_struct hugetlb_vm_ops;
522struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
523 int creat_flags, int page_size_log);
524
525static inline bool is_file_hugepages(const struct file *file)
526{
527 return file->f_op->fop_flags & FOP_HUGE_PAGES;
528}
529
530static inline struct hstate *hstate_inode(struct inode *i)
531{
532 return HUGETLBFS_SB(i->i_sb)->hstate;
533}
534#else
535
536#define is_file_hugepages(file) false
537static inline struct file *
538hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
539 int creat_flags, int page_size_log)
540{
541 return ERR_PTR(-ENOSYS);
542}
543
544static inline struct hstate *hstate_inode(struct inode *i)
545{
546 return NULL;
547}
548#endif
549
550unsigned long
551hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
552 unsigned long len, unsigned long pgoff,
553 unsigned long flags);
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585enum hugetlb_page_flags {
586 HPG_restore_reserve = 0,
587 HPG_migratable,
588 HPG_temporary,
589 HPG_freed,
590 HPG_vmemmap_optimized,
591 HPG_raw_hwp_unreliable,
592 HPG_cma,
593 __NR_HPAGEFLAGS,
594};
595
596
597
598
599
600#ifdef CONFIG_HUGETLB_PAGE
601#define TESTHPAGEFLAG(uname, flname) \
602static __always_inline \
603bool folio_test_hugetlb_##flname(struct folio *folio) \
604 { void *private = &folio->private; \
605 return test_bit(HPG_##flname, private); \
606 }
607
608#define SETHPAGEFLAG(uname, flname) \
609static __always_inline \
610void folio_set_hugetlb_##flname(struct folio *folio) \
611 { void *private = &folio->private; \
612 set_bit(HPG_##flname, private); \
613 }
614
615#define CLEARHPAGEFLAG(uname, flname) \
616static __always_inline \
617void folio_clear_hugetlb_##flname(struct folio *folio) \
618 { void *private = &folio->private; \
619 clear_bit(HPG_##flname, private); \
620 }
621#else
622#define TESTHPAGEFLAG(uname, flname) \
623static inline bool \
624folio_test_hugetlb_##flname(struct folio *folio) \
625 { return 0; }
626
627#define SETHPAGEFLAG(uname, flname) \
628static inline void \
629folio_set_hugetlb_##flname(struct folio *folio) \
630 { }
631
632#define CLEARHPAGEFLAG(uname, flname) \
633static inline void \
634folio_clear_hugetlb_##flname(struct folio *folio) \
635 { }
636#endif
637
638#define HPAGEFLAG(uname, flname) \
639 TESTHPAGEFLAG(uname, flname) \
640 SETHPAGEFLAG(uname, flname) \
641 CLEARHPAGEFLAG(uname, flname) \
642
643
644
645
646HPAGEFLAG(RestoreReserve, restore_reserve)
647HPAGEFLAG(Migratable, migratable)
648HPAGEFLAG(Temporary, temporary)
649HPAGEFLAG(Freed, freed)
650HPAGEFLAG(VmemmapOptimized, vmemmap_optimized)
651HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable)
652HPAGEFLAG(Cma, cma)
653
654#ifdef CONFIG_HUGETLB_PAGE
655
656#define HSTATE_NAME_LEN 32
657
658struct hstate {
659 struct mutex resize_lock;
660 struct lock_class_key resize_key;
661 int next_nid_to_alloc;
662 int next_nid_to_free;
663 unsigned int order;
664 unsigned int demote_order;
665 unsigned long mask;
666 unsigned long max_huge_pages;
667 unsigned long nr_huge_pages;
668 unsigned long free_huge_pages;
669 unsigned long resv_huge_pages;
670 unsigned long surplus_huge_pages;
671 unsigned long nr_overcommit_huge_pages;
672 struct list_head hugepage_activelist;
673 struct list_head hugepage_freelists[MAX_NUMNODES];
674 unsigned int max_huge_pages_node[MAX_NUMNODES];
675 unsigned int nr_huge_pages_node[MAX_NUMNODES];
676 unsigned int free_huge_pages_node[MAX_NUMNODES];
677 unsigned int surplus_huge_pages_node[MAX_NUMNODES];
678 char name[HSTATE_NAME_LEN];
679};
680
681struct cma;
682
683struct huge_bootmem_page {
684 struct list_head list;
685 struct hstate *hstate;
686 unsigned long flags;
687 struct cma *cma;
688};
689
690#define HUGE_BOOTMEM_HVO 0x0001
691#define HUGE_BOOTMEM_ZONES_VALID 0x0002
692#define HUGE_BOOTMEM_CMA 0x0004
693
694bool hugetlb_bootmem_page_zones_valid(int nid, struct huge_bootmem_page *m);
695
696int isolate_or_dissolve_huge_folio(struct folio *folio, struct list_head *list);
697int replace_free_hugepage_folios(unsigned long start_pfn, unsigned long end_pfn);
698void wait_for_freed_hugetlb_folios(void);
699struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
700 unsigned long addr, bool cow_from_owner);
701struct folio *alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
702 nodemask_t *nmask, gfp_t gfp_mask,
703 bool allow_alloc_fallback);
704struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
705 nodemask_t *nmask, gfp_t gfp_mask);
706
707int hugetlb_add_to_page_cache(struct folio *folio, struct address_space *mapping,
708 pgoff_t idx);
709void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma,
710 unsigned long address, struct folio *folio);
711
712
713int __init __alloc_bootmem_huge_page(struct hstate *h, int nid);
714int __init alloc_bootmem_huge_page(struct hstate *h, int nid);
715bool __init hugetlb_node_alloc_supported(void);
716
717void __init hugetlb_add_hstate(unsigned order);
718bool __init arch_hugetlb_valid_size(unsigned long size);
719struct hstate *size_to_hstate(unsigned long size);
720
721#ifndef HUGE_MAX_HSTATE
722#define HUGE_MAX_HSTATE 1
723#endif
724
725extern struct hstate hstates[HUGE_MAX_HSTATE];
726extern unsigned int default_hstate_idx;
727
728#define default_hstate (hstates[default_hstate_idx])
729
730static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
731{
732 return HUGETLBFS_SB(inode->i_sb)->spool;
733}
734
735static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
736{
737 return folio->_hugetlb_subpool;
738}
739
740static inline void hugetlb_set_folio_subpool(struct folio *folio,
741 struct hugepage_subpool *subpool)
742{
743 folio->_hugetlb_subpool = subpool;
744}
745
746static inline struct hstate *hstate_file(struct file *f)
747{
748 return hstate_inode(file_inode(f));
749}
750
751static inline struct hstate *hstate_sizelog(int page_size_log)
752{
753 if (!page_size_log)
754 return &default_hstate;
755
756 if (page_size_log < BITS_PER_LONG)
757 return size_to_hstate(1UL << page_size_log);
758
759 return NULL;
760}
761
762static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
763{
764 return hstate_file(vma->vm_file);
765}
766
767static inline unsigned long huge_page_size(const struct hstate *h)
768{
769 return (unsigned long)PAGE_SIZE << h->order;
770}
771
772extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
773
774extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
775
776static inline unsigned long huge_page_mask(struct hstate *h)
777{
778 return h->mask;
779}
780
781static inline unsigned int huge_page_order(struct hstate *h)
782{
783 return h->order;
784}
785
786static inline unsigned huge_page_shift(struct hstate *h)
787{
788 return h->order + PAGE_SHIFT;
789}
790
791static inline bool hstate_is_gigantic(struct hstate *h)
792{
793 return huge_page_order(h) > MAX_PAGE_ORDER;
794}
795
796static inline unsigned int pages_per_huge_page(const struct hstate *h)
797{
798 return 1 << h->order;
799}
800
801static inline unsigned int blocks_per_huge_page(struct hstate *h)
802{
803 return huge_page_size(h) / 512;
804}
805
806static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
807 struct address_space *mapping, pgoff_t idx)
808{
809 return filemap_lock_folio(mapping, idx << huge_page_order(h));
810}
811
812#include <asm/hugetlb.h>
813
814#ifndef is_hugepage_only_range
815static inline int is_hugepage_only_range(struct mm_struct *mm,
816 unsigned long addr, unsigned long len)
817{
818 return 0;
819}
820#define is_hugepage_only_range is_hugepage_only_range
821#endif
822
823#ifndef arch_clear_hugetlb_flags
824static inline void arch_clear_hugetlb_flags(struct folio *folio) { }
825#define arch_clear_hugetlb_flags arch_clear_hugetlb_flags
826#endif
827
828#ifndef arch_make_huge_pte
829static inline pte_t arch_make_huge_pte(pte_t entry, unsigned int shift,
830 vm_flags_t flags)
831{
832 return pte_mkhuge(entry);
833}
834#endif
835
836#ifndef arch_has_huge_bootmem_alloc
837
838
839
840
841static inline bool arch_has_huge_bootmem_alloc(void)
842{
843 return false;
844}
845#endif
846
847static inline struct hstate *folio_hstate(struct folio *folio)
848{
849 VM_BUG_ON_FOLIO(!folio_test_hugetlb(folio), folio);
850 return size_to_hstate(folio_size(folio));
851}
852
853static inline unsigned hstate_index_to_shift(unsigned index)
854{
855 return hstates[index].order + PAGE_SHIFT;
856}
857
858static inline int hstate_index(struct hstate *h)
859{
860 return h - hstates;
861}
862
863int dissolve_free_hugetlb_folio(struct folio *folio);
864int dissolve_free_hugetlb_folios(unsigned long start_pfn,
865 unsigned long end_pfn);
866
867#ifdef CONFIG_MEMORY_FAILURE
868extern void folio_clear_hugetlb_hwpoison(struct folio *folio);
869#else
870static inline void folio_clear_hugetlb_hwpoison(struct folio *folio)
871{
872}
873#endif
874
875#ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
876#ifndef arch_hugetlb_migration_supported
877static inline bool arch_hugetlb_migration_supported(struct hstate *h)
878{
879 if ((huge_page_shift(h) == PMD_SHIFT) ||
880 (huge_page_shift(h) == PUD_SHIFT) ||
881 (huge_page_shift(h) == PGDIR_SHIFT))
882 return true;
883 else
884 return false;
885}
886#endif
887#else
888static inline bool arch_hugetlb_migration_supported(struct hstate *h)
889{
890 return false;
891}
892#endif
893
894static inline bool hugepage_migration_supported(struct hstate *h)
895{
896 return arch_hugetlb_migration_supported(h);
897}
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914static inline bool hugepage_movable_supported(struct hstate *h)
915{
916 if (!hugepage_migration_supported(h))
917 return false;
918
919 if (hstate_is_gigantic(h))
920 return false;
921 return true;
922}
923
924
925static inline gfp_t htlb_alloc_mask(struct hstate *h)
926{
927 gfp_t gfp = __GFP_COMP | __GFP_NOWARN;
928
929 gfp |= hugepage_movable_supported(h) ? GFP_HIGHUSER_MOVABLE : GFP_HIGHUSER;
930
931 return gfp;
932}
933
934static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
935{
936 gfp_t modified_mask = htlb_alloc_mask(h);
937
938
939 modified_mask |= (gfp_mask & __GFP_THISNODE);
940
941 modified_mask |= (gfp_mask & __GFP_NOWARN);
942
943 return modified_mask;
944}
945
946static inline bool htlb_allow_alloc_fallback(int reason)
947{
948 bool allowed_fallback = false;
949
950
951
952
953
954
955
956 switch (reason) {
957 case MR_MEMORY_HOTPLUG:
958 case MR_MEMORY_FAILURE:
959 case MR_SYSCALL:
960 case MR_MEMPOLICY_MBIND:
961 allowed_fallback = true;
962 break;
963 default:
964 break;
965 }
966
967 return allowed_fallback;
968}
969
970static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
971 struct mm_struct *mm, pte_t *pte)
972{
973 const unsigned long size = huge_page_size(h);
974
975 VM_WARN_ON(size == PAGE_SIZE);
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998 if (size >= PUD_SIZE)
999 return pud_lockptr(mm, (pud_t *) pte);
1000 else if (size >= PMD_SIZE || IS_ENABLED(CONFIG_HIGHPTE))
1001 return pmd_lockptr(mm, (pmd_t *) pte);
1002
1003 return ptep_lockptr(mm, pte);
1004}
1005
1006#ifndef hugepages_supported
1007
1008
1009
1010
1011
1012#define hugepages_supported() (HPAGE_SHIFT != 0)
1013#endif
1014
1015void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
1016
1017static inline void hugetlb_count_init(struct mm_struct *mm)
1018{
1019 atomic_long_set(&mm->hugetlb_usage, 0);
1020}
1021
1022static inline void hugetlb_count_add(long l, struct mm_struct *mm)
1023{
1024 atomic_long_add(l, &mm->hugetlb_usage);
1025}
1026
1027static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1028{
1029 atomic_long_sub(l, &mm->hugetlb_usage);
1030}
1031
1032#ifndef huge_ptep_modify_prot_start
1033#define huge_ptep_modify_prot_start huge_ptep_modify_prot_start
1034static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
1035 unsigned long addr, pte_t *ptep)
1036{
1037 unsigned long psize = huge_page_size(hstate_vma(vma));
1038
1039 return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep, psize);
1040}
1041#endif
1042
1043#ifndef huge_ptep_modify_prot_commit
1044#define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit
1045static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
1046 unsigned long addr, pte_t *ptep,
1047 pte_t old_pte, pte_t pte)
1048{
1049 unsigned long psize = huge_page_size(hstate_vma(vma));
1050
1051 set_huge_pte_at(vma->vm_mm, addr, ptep, pte, psize);
1052}
1053#endif
1054
1055#ifdef CONFIG_NUMA
1056void hugetlb_register_node(struct node *node);
1057void hugetlb_unregister_node(struct node *node);
1058#endif
1059
1060
1061
1062
1063bool is_raw_hwpoison_page_in_hugepage(struct page *page);
1064
1065static inline unsigned long huge_page_mask_align(struct file *file)
1066{
1067 return PAGE_MASK & ~huge_page_mask(hstate_file(file));
1068}
1069
1070#else
1071struct hstate {};
1072
1073static inline unsigned long huge_page_mask_align(struct file *file)
1074{
1075 return 0;
1076}
1077
1078static inline struct hugepage_subpool *hugetlb_folio_subpool(struct folio *folio)
1079{
1080 return NULL;
1081}
1082
1083static inline struct folio *filemap_lock_hugetlb_folio(struct hstate *h,
1084 struct address_space *mapping, pgoff_t idx)
1085{
1086 return NULL;
1087}
1088
1089static inline int isolate_or_dissolve_huge_folio(struct folio *folio,
1090 struct list_head *list)
1091{
1092 return -ENOMEM;
1093}
1094
1095static inline int replace_free_hugepage_folios(unsigned long start_pfn,
1096 unsigned long end_pfn)
1097{
1098 return 0;
1099}
1100
1101static inline void wait_for_freed_hugetlb_folios(void)
1102{
1103}
1104
1105static inline struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
1106 unsigned long addr,
1107 bool cow_from_owner)
1108{
1109 return NULL;
1110}
1111
1112static inline struct folio *
1113alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,
1114 nodemask_t *nmask, gfp_t gfp_mask)
1115{
1116 return NULL;
1117}
1118
1119static inline struct folio *
1120alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,
1121 nodemask_t *nmask, gfp_t gfp_mask,
1122 bool allow_alloc_fallback)
1123{
1124 return NULL;
1125}
1126
1127static inline int __alloc_bootmem_huge_page(struct hstate *h)
1128{
1129 return 0;
1130}
1131
1132static inline struct hstate *hstate_file(struct file *f)
1133{
1134 return NULL;
1135}
1136
1137static inline struct hstate *hstate_sizelog(int page_size_log)
1138{
1139 return NULL;
1140}
1141
1142static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
1143{
1144 return NULL;
1145}
1146
1147static inline struct hstate *folio_hstate(struct folio *folio)
1148{
1149 return NULL;
1150}
1151
1152static inline struct hstate *size_to_hstate(unsigned long size)
1153{
1154 return NULL;
1155}
1156
1157static inline unsigned long huge_page_size(struct hstate *h)
1158{
1159 return PAGE_SIZE;
1160}
1161
1162static inline unsigned long huge_page_mask(struct hstate *h)
1163{
1164 return PAGE_MASK;
1165}
1166
1167static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
1168{
1169 return PAGE_SIZE;
1170}
1171
1172static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
1173{
1174 return PAGE_SIZE;
1175}
1176
1177static inline unsigned int huge_page_order(struct hstate *h)
1178{
1179 return 0;
1180}
1181
1182static inline unsigned int huge_page_shift(struct hstate *h)
1183{
1184 return PAGE_SHIFT;
1185}
1186
1187static inline bool hstate_is_gigantic(struct hstate *h)
1188{
1189 return false;
1190}
1191
1192static inline unsigned int pages_per_huge_page(struct hstate *h)
1193{
1194 return 1;
1195}
1196
1197static inline unsigned hstate_index_to_shift(unsigned index)
1198{
1199 return 0;
1200}
1201
1202static inline int hstate_index(struct hstate *h)
1203{
1204 return 0;
1205}
1206
1207static inline int dissolve_free_hugetlb_folio(struct folio *folio)
1208{
1209 return 0;
1210}
1211
1212static inline int dissolve_free_hugetlb_folios(unsigned long start_pfn,
1213 unsigned long end_pfn)
1214{
1215 return 0;
1216}
1217
1218static inline bool hugepage_migration_supported(struct hstate *h)
1219{
1220 return false;
1221}
1222
1223static inline bool hugepage_movable_supported(struct hstate *h)
1224{
1225 return false;
1226}
1227
1228static inline gfp_t htlb_alloc_mask(struct hstate *h)
1229{
1230 return 0;
1231}
1232
1233static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
1234{
1235 return 0;
1236}
1237
1238static inline bool htlb_allow_alloc_fallback(int reason)
1239{
1240 return false;
1241}
1242
1243static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
1244 struct mm_struct *mm, pte_t *pte)
1245{
1246 return &mm->page_table_lock;
1247}
1248
1249static inline void hugetlb_count_init(struct mm_struct *mm)
1250{
1251}
1252
1253static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
1254{
1255}
1256
1257static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
1258{
1259}
1260
1261static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
1262 unsigned long addr, pte_t *ptep)
1263{
1264#ifdef CONFIG_MMU
1265 return ptep_get(ptep);
1266#else
1267 return *ptep;
1268#endif
1269}
1270
1271static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
1272 pte_t *ptep, pte_t pte, unsigned long sz)
1273{
1274}
1275
1276static inline void hugetlb_register_node(struct node *node)
1277{
1278}
1279
1280static inline void hugetlb_unregister_node(struct node *node)
1281{
1282}
1283
1284static inline bool hugetlbfs_pagecache_present(
1285 struct hstate *h, struct vm_area_struct *vma, unsigned long address)
1286{
1287 return false;
1288}
1289
1290static inline void hugetlb_bootmem_alloc(void)
1291{
1292}
1293
1294static inline bool hugetlb_bootmem_allocated(void)
1295{
1296 return false;
1297}
1298#endif
1299
1300static inline spinlock_t *huge_pte_lock(struct hstate *h,
1301 struct mm_struct *mm, pte_t *pte)
1302{
1303 spinlock_t *ptl;
1304
1305 ptl = huge_pte_lockptr(h, mm, pte);
1306 spin_lock(ptl);
1307 return ptl;
1308}
1309
1310#if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
1311extern void __init hugetlb_cma_reserve(int order);
1312#else
1313static inline __init void hugetlb_cma_reserve(int order)
1314{
1315}
1316#endif
1317
1318#ifdef CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING
1319static inline bool hugetlb_pmd_shared(pte_t *pte)
1320{
1321 return page_count(virt_to_page(pte)) > 1;
1322}
1323#else
1324static inline bool hugetlb_pmd_shared(pte_t *pte)
1325{
1326 return false;
1327}
1328#endif
1329
1330bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
1331
1332#ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
1333
1334
1335
1336
1337#define flush_hugetlb_tlb_range(vma, addr, end) flush_tlb_range(vma, addr, end)
1338#endif
1339
1340static inline bool __vma_shareable_lock(struct vm_area_struct *vma)
1341{
1342 return (vma->vm_flags & VM_MAYSHARE) && vma->vm_private_data;
1343}
1344
1345bool __vma_private_lock(struct vm_area_struct *vma);
1346
1347
1348
1349
1350
1351static inline pte_t *
1352hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz)
1353{
1354#if defined(CONFIG_HUGETLB_PMD_PAGE_TABLE_SHARING) && defined(CONFIG_LOCKDEP)
1355 struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;
1356
1357
1358
1359
1360
1361
1362
1363
1364 if (__vma_shareable_lock(vma))
1365 WARN_ON_ONCE(!lockdep_is_held(&vma_lock->rw_sema) &&
1366 !lockdep_is_held(
1367 &vma->vm_file->f_mapping->i_mmap_rwsem));
1368#endif
1369 return huge_pte_offset(vma->vm_mm, addr, sz);
1370}
1371
1372#endif
1373