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