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