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