GNU Linux-libre 4.19.286-gnu1
[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/page_ref.h>
11 #include <linux/list.h>
12 #include <linux/kref.h>
13 #include <asm/pgtable.h>
14
15 struct ctl_table;
16 struct user_struct;
17 struct mmu_gather;
18
19 #ifndef is_hugepd
20 /*
21  * Some architectures requires a hugepage directory format that is
22  * required to support multiple hugepage sizes. For example
23  * a4fe3ce76 "powerpc/mm: Allow more flexible layouts for hugepage pagetables"
24  * introduced the same on powerpc. This allows for a more flexible hugepage
25  * pagetable layout.
26  */
27 typedef struct { unsigned long pd; } hugepd_t;
28 #define is_hugepd(hugepd) (0)
29 #define __hugepd(x) ((hugepd_t) { (x) })
30 static inline int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
31                               unsigned pdshift, unsigned long end,
32                               int write, struct page **pages, int *nr)
33 {
34         return 0;
35 }
36 #else
37 extern int gup_huge_pd(hugepd_t hugepd, unsigned long addr,
38                        unsigned pdshift, unsigned long end,
39                        int write, struct page **pages, int *nr);
40 #endif
41
42
43 #ifdef CONFIG_HUGETLB_PAGE
44
45 #include <linux/mempolicy.h>
46 #include <linux/shm.h>
47 #include <asm/tlbflush.h>
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 alloced 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                                 /* sasitfy 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 };
69 extern struct resv_map *resv_map_alloc(void);
70 void resv_map_release(struct kref *ref);
71
72 extern spinlock_t hugetlb_lock;
73 extern int hugetlb_max_hstate __read_mostly;
74 #define for_each_hstate(h) \
75         for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
76
77 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
78                                                 long min_hpages);
79 void hugepage_put_subpool(struct hugepage_subpool *spool);
80
81 void reset_vma_resv_huge_pages(struct vm_area_struct *vma);
82 int hugetlb_sysctl_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
83 int hugetlb_overcommit_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
84 int hugetlb_treat_movable_handler(struct ctl_table *, int, void __user *, size_t *, loff_t *);
85
86 #ifdef CONFIG_NUMA
87 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int,
88                                         void __user *, size_t *, loff_t *);
89 #endif
90
91 int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, struct vm_area_struct *);
92 long follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
93                          struct page **, struct vm_area_struct **,
94                          unsigned long *, unsigned long *, long, unsigned int,
95                          int *);
96 void unmap_hugepage_range(struct vm_area_struct *,
97                           unsigned long, unsigned long, struct page *);
98 void __unmap_hugepage_range_final(struct mmu_gather *tlb,
99                           struct vm_area_struct *vma,
100                           unsigned long start, unsigned long end,
101                           struct page *ref_page);
102 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
103                                 unsigned long start, unsigned long end,
104                                 struct page *ref_page);
105 void hugetlb_report_meminfo(struct seq_file *);
106 int hugetlb_report_node_meminfo(int, char *);
107 void hugetlb_show_meminfo(void);
108 unsigned long hugetlb_total_pages(void);
109 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
110                         unsigned long address, unsigned int flags);
111 int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte,
112                                 struct vm_area_struct *dst_vma,
113                                 unsigned long dst_addr,
114                                 unsigned long src_addr,
115                                 struct page **pagep);
116 int hugetlb_reserve_pages(struct inode *inode, long from, long to,
117                                                 struct vm_area_struct *vma,
118                                                 vm_flags_t vm_flags);
119 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
120                                                 long freed);
121 bool isolate_huge_page(struct page *page, struct list_head *list);
122 void putback_active_hugepage(struct page *page);
123 void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason);
124 void free_huge_page(struct page *page);
125 void hugetlb_fix_reserve_counts(struct inode *inode);
126 extern struct mutex *hugetlb_fault_mutex_table;
127 u32 hugetlb_fault_mutex_hash(struct hstate *h, struct address_space *mapping,
128                                 pgoff_t idx);
129
130 pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud);
131
132 extern int sysctl_hugetlb_shm_group;
133 extern struct list_head huge_boot_pages;
134
135 /* arch callbacks */
136
137 pte_t *huge_pte_alloc(struct mm_struct *mm,
138                         unsigned long addr, unsigned long sz);
139 pte_t *huge_pte_offset(struct mm_struct *mm,
140                        unsigned long addr, unsigned long sz);
141 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep);
142 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
143                                 unsigned long *start, unsigned long *end);
144 struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address,
145                               int write);
146 struct page *follow_huge_pd(struct vm_area_struct *vma,
147                             unsigned long address, hugepd_t hpd,
148                             int flags, int pdshift);
149 struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
150                                 pmd_t *pmd, int flags);
151 struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address,
152                                 pud_t *pud, int flags);
153 struct page *follow_huge_pgd(struct mm_struct *mm, unsigned long address,
154                              pgd_t *pgd, int flags);
155
156 int pmd_huge(pmd_t pmd);
157 int pud_huge(pud_t pud);
158 unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
159                 unsigned long address, unsigned long end, pgprot_t newprot);
160
161 bool is_hugetlb_entry_migration(pte_t pte);
162
163 #else /* !CONFIG_HUGETLB_PAGE */
164
165 static inline void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
166 {
167 }
168
169 static inline unsigned long hugetlb_total_pages(void)
170 {
171         return 0;
172 }
173
174 static inline int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr,
175                                         pte_t *ptep)
176 {
177         return 0;
178 }
179
180 static inline void adjust_range_if_pmd_sharing_possible(
181                                 struct vm_area_struct *vma,
182                                 unsigned long *start, unsigned long *end)
183 {
184 }
185
186 #define follow_hugetlb_page(m,v,p,vs,a,b,i,w,n) ({ BUG(); 0; })
187 #define follow_huge_addr(mm, addr, write)       ERR_PTR(-EINVAL)
188 #define copy_hugetlb_page_range(src, dst, vma)  ({ BUG(); 0; })
189 static inline void hugetlb_report_meminfo(struct seq_file *m)
190 {
191 }
192 #define hugetlb_report_node_meminfo(n, buf)     0
193 static inline void hugetlb_show_meminfo(void)
194 {
195 }
196 #define follow_huge_pd(vma, addr, hpd, flags, pdshift) NULL
197 #define follow_huge_pmd(mm, addr, pmd, flags)   NULL
198 #define follow_huge_pud(mm, addr, pud, flags)   NULL
199 #define follow_huge_pgd(mm, addr, pgd, flags)   NULL
200 #define prepare_hugepage_range(file, addr, len) (-EINVAL)
201 #define pmd_huge(x)     0
202 #define pud_huge(x)     0
203 #define is_hugepage_only_range(mm, addr, len)   0
204 #define hugetlb_free_pgd_range(tlb, addr, end, floor, ceiling) ({BUG(); 0; })
205 #define hugetlb_fault(mm, vma, addr, flags)     ({ BUG(); 0; })
206 #define hugetlb_mcopy_atomic_pte(dst_mm, dst_pte, dst_vma, dst_addr, \
207                                 src_addr, pagep)        ({ BUG(); 0; })
208 #define huge_pte_offset(mm, address, sz)        0
209
210 static inline bool isolate_huge_page(struct page *page, struct list_head *list)
211 {
212         return false;
213 }
214 #define putback_active_hugepage(p)      do {} while (0)
215 #define move_hugetlb_state(old, new, reason)    do {} while (0)
216
217 static inline unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
218                 unsigned long address, unsigned long end, pgprot_t newprot)
219 {
220         return 0;
221 }
222
223 static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
224                         struct vm_area_struct *vma, unsigned long start,
225                         unsigned long end, struct page *ref_page)
226 {
227         BUG();
228 }
229
230 static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
231                         struct vm_area_struct *vma, unsigned long start,
232                         unsigned long end, struct page *ref_page)
233 {
234         BUG();
235 }
236
237 #endif /* !CONFIG_HUGETLB_PAGE */
238 /*
239  * hugepages at page global directory. If arch support
240  * hugepages at pgd level, they need to define this.
241  */
242 #ifndef pgd_huge
243 #define pgd_huge(x)     0
244 #endif
245 #ifndef p4d_huge
246 #define p4d_huge(x)     0
247 #endif
248
249 #ifndef pgd_write
250 static inline int pgd_write(pgd_t pgd)
251 {
252         BUG();
253         return 0;
254 }
255 #endif
256
257 #define HUGETLB_ANON_FILE "anon_hugepage"
258
259 enum {
260         /*
261          * The file will be used as an shm file so shmfs accounting rules
262          * apply
263          */
264         HUGETLB_SHMFS_INODE     = 1,
265         /*
266          * The file is being created on the internal vfs mount and shmfs
267          * accounting rules do not apply
268          */
269         HUGETLB_ANONHUGE_INODE  = 2,
270 };
271
272 #ifdef CONFIG_HUGETLBFS
273 struct hugetlbfs_sb_info {
274         long    max_inodes;   /* inodes allowed */
275         long    free_inodes;  /* inodes free */
276         spinlock_t      stat_lock;
277         struct hstate *hstate;
278         struct hugepage_subpool *spool;
279         kuid_t  uid;
280         kgid_t  gid;
281         umode_t mode;
282 };
283
284 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
285 {
286         return sb->s_fs_info;
287 }
288
289 struct hugetlbfs_inode_info {
290         struct shared_policy policy;
291         struct inode vfs_inode;
292         unsigned int seals;
293 };
294
295 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
296 {
297         return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
298 }
299
300 extern const struct file_operations hugetlbfs_file_operations;
301 extern const struct vm_operations_struct hugetlb_vm_ops;
302 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
303                                 struct user_struct **user, int creat_flags,
304                                 int page_size_log);
305
306 static inline bool is_file_hugepages(struct file *file)
307 {
308         if (file->f_op == &hugetlbfs_file_operations)
309                 return true;
310
311         return is_file_shm_hugepages(file);
312 }
313
314
315 #else /* !CONFIG_HUGETLBFS */
316
317 #define is_file_hugepages(file)                 false
318 static inline struct file *
319 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
320                 struct user_struct **user, int creat_flags,
321                 int page_size_log)
322 {
323         return ERR_PTR(-ENOSYS);
324 }
325
326 #endif /* !CONFIG_HUGETLBFS */
327
328 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
329 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
330                                         unsigned long len, unsigned long pgoff,
331                                         unsigned long flags);
332 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
333
334 #ifdef CONFIG_HUGETLB_PAGE
335
336 #define HSTATE_NAME_LEN 32
337 /* Defines one hugetlb page size */
338 struct hstate {
339         int next_nid_to_alloc;
340         int next_nid_to_free;
341         unsigned int order;
342         unsigned long mask;
343         unsigned long max_huge_pages;
344         unsigned long nr_huge_pages;
345         unsigned long free_huge_pages;
346         unsigned long resv_huge_pages;
347         unsigned long surplus_huge_pages;
348         unsigned long nr_overcommit_huge_pages;
349         struct list_head hugepage_activelist;
350         struct list_head hugepage_freelists[MAX_NUMNODES];
351         unsigned int nr_huge_pages_node[MAX_NUMNODES];
352         unsigned int free_huge_pages_node[MAX_NUMNODES];
353         unsigned int surplus_huge_pages_node[MAX_NUMNODES];
354 #ifdef CONFIG_CGROUP_HUGETLB
355         /* cgroup control files */
356         struct cftype cgroup_files[5];
357 #endif
358         char name[HSTATE_NAME_LEN];
359 };
360
361 struct huge_bootmem_page {
362         struct list_head list;
363         struct hstate *hstate;
364 };
365
366 struct page *alloc_huge_page(struct vm_area_struct *vma,
367                                 unsigned long addr, int avoid_reserve);
368 struct page *alloc_huge_page_node(struct hstate *h, int nid);
369 struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
370                                 nodemask_t *nmask);
371 struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
372                                 unsigned long address);
373 int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
374                         pgoff_t idx);
375
376 /* arch callback */
377 int __init __alloc_bootmem_huge_page(struct hstate *h);
378 int __init alloc_bootmem_huge_page(struct hstate *h);
379
380 void __init hugetlb_bad_size(void);
381 void __init hugetlb_add_hstate(unsigned order);
382 struct hstate *size_to_hstate(unsigned long size);
383
384 #ifndef HUGE_MAX_HSTATE
385 #define HUGE_MAX_HSTATE 1
386 #endif
387
388 extern struct hstate hstates[HUGE_MAX_HSTATE];
389 extern unsigned int default_hstate_idx;
390
391 #define default_hstate (hstates[default_hstate_idx])
392
393 static inline struct hstate *hstate_inode(struct inode *i)
394 {
395         return HUGETLBFS_SB(i->i_sb)->hstate;
396 }
397
398 static inline struct hstate *hstate_file(struct file *f)
399 {
400         return hstate_inode(file_inode(f));
401 }
402
403 static inline struct hstate *hstate_sizelog(int page_size_log)
404 {
405         if (!page_size_log)
406                 return &default_hstate;
407
408         if (page_size_log < BITS_PER_LONG)
409                 return size_to_hstate(1UL << page_size_log);
410
411         return NULL;
412 }
413
414 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
415 {
416         return hstate_file(vma->vm_file);
417 }
418
419 static inline unsigned long huge_page_size(struct hstate *h)
420 {
421         return (unsigned long)PAGE_SIZE << h->order;
422 }
423
424 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
425
426 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
427
428 static inline unsigned long huge_page_mask(struct hstate *h)
429 {
430         return h->mask;
431 }
432
433 static inline unsigned int huge_page_order(struct hstate *h)
434 {
435         return h->order;
436 }
437
438 static inline unsigned huge_page_shift(struct hstate *h)
439 {
440         return h->order + PAGE_SHIFT;
441 }
442
443 static inline bool hstate_is_gigantic(struct hstate *h)
444 {
445         return huge_page_order(h) >= MAX_ORDER;
446 }
447
448 static inline unsigned int pages_per_huge_page(struct hstate *h)
449 {
450         return 1 << h->order;
451 }
452
453 static inline unsigned int blocks_per_huge_page(struct hstate *h)
454 {
455         return huge_page_size(h) / 512;
456 }
457
458 #include <asm/hugetlb.h>
459
460 #ifndef arch_make_huge_pte
461 static inline pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
462                                        struct page *page, int writable)
463 {
464         return entry;
465 }
466 #endif
467
468 static inline struct hstate *page_hstate(struct page *page)
469 {
470         VM_BUG_ON_PAGE(!PageHuge(page), page);
471         return size_to_hstate(PAGE_SIZE << compound_order(page));
472 }
473
474 static inline unsigned hstate_index_to_shift(unsigned index)
475 {
476         return hstates[index].order + PAGE_SHIFT;
477 }
478
479 static inline int hstate_index(struct hstate *h)
480 {
481         return h - hstates;
482 }
483
484 extern int dissolve_free_huge_page(struct page *page);
485 extern int dissolve_free_huge_pages(unsigned long start_pfn,
486                                     unsigned long end_pfn);
487 static inline bool hugepage_migration_supported(struct hstate *h)
488 {
489 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
490         if ((huge_page_shift(h) == PMD_SHIFT) ||
491                 (huge_page_shift(h) == PGDIR_SHIFT))
492                 return true;
493         else
494                 return false;
495 #else
496         return false;
497 #endif
498 }
499
500 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
501                                            struct mm_struct *mm, pte_t *pte)
502 {
503         if (huge_page_size(h) == PMD_SIZE)
504                 return pmd_lockptr(mm, (pmd_t *) pte);
505         VM_BUG_ON(huge_page_size(h) == PAGE_SIZE);
506         return &mm->page_table_lock;
507 }
508
509 #ifndef hugepages_supported
510 /*
511  * Some platform decide whether they support huge pages at boot
512  * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
513  * when there is no such support
514  */
515 #define hugepages_supported() (HPAGE_SHIFT != 0)
516 #endif
517
518 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
519
520 static inline void hugetlb_count_init(struct mm_struct *mm)
521 {
522         atomic_long_set(&mm->hugetlb_usage, 0);
523 }
524
525 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
526 {
527         atomic_long_add(l, &mm->hugetlb_usage);
528 }
529
530 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
531 {
532         atomic_long_sub(l, &mm->hugetlb_usage);
533 }
534
535 #ifndef set_huge_swap_pte_at
536 static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
537                                         pte_t *ptep, pte_t pte, unsigned long sz)
538 {
539         set_huge_pte_at(mm, addr, ptep, pte);
540 }
541 #endif
542
543 void set_page_huge_active(struct page *page);
544
545 #else   /* CONFIG_HUGETLB_PAGE */
546 struct hstate {};
547 #define alloc_huge_page(v, a, r) NULL
548 #define alloc_huge_page_node(h, nid) NULL
549 #define alloc_huge_page_nodemask(h, preferred_nid, nmask) NULL
550 #define alloc_huge_page_vma(h, vma, address) NULL
551 #define alloc_bootmem_huge_page(h) NULL
552 #define hstate_file(f) NULL
553 #define hstate_sizelog(s) NULL
554 #define hstate_vma(v) NULL
555 #define hstate_inode(i) NULL
556 #define page_hstate(page) NULL
557 #define huge_page_size(h) PAGE_SIZE
558 #define huge_page_mask(h) PAGE_MASK
559 #define vma_kernel_pagesize(v) PAGE_SIZE
560 #define vma_mmu_pagesize(v) PAGE_SIZE
561 #define huge_page_order(h) 0
562 #define huge_page_shift(h) PAGE_SHIFT
563 static inline bool hstate_is_gigantic(struct hstate *h)
564 {
565         return false;
566 }
567
568 static inline unsigned int pages_per_huge_page(struct hstate *h)
569 {
570         return 1;
571 }
572
573 static inline unsigned hstate_index_to_shift(unsigned index)
574 {
575         return 0;
576 }
577
578 static inline int hstate_index(struct hstate *h)
579 {
580         return 0;
581 }
582
583 static inline int dissolve_free_huge_page(struct page *page)
584 {
585         return 0;
586 }
587
588 static inline int dissolve_free_huge_pages(unsigned long start_pfn,
589                                            unsigned long end_pfn)
590 {
591         return 0;
592 }
593
594 static inline bool hugepage_migration_supported(struct hstate *h)
595 {
596         return false;
597 }
598
599 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
600                                            struct mm_struct *mm, pte_t *pte)
601 {
602         return &mm->page_table_lock;
603 }
604
605 static inline void hugetlb_count_init(struct mm_struct *mm)
606 {
607 }
608
609 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
610 {
611 }
612
613 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
614 {
615 }
616
617 static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
618                                         pte_t *ptep, pte_t pte, unsigned long sz)
619 {
620 }
621 #endif  /* CONFIG_HUGETLB_PAGE */
622
623 static inline spinlock_t *huge_pte_lock(struct hstate *h,
624                                         struct mm_struct *mm, pte_t *pte)
625 {
626         spinlock_t *ptl;
627
628         ptl = huge_pte_lockptr(h, mm, pte);
629         spin_lock(ptl);
630         return ptl;
631 }
632
633 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
634 static inline bool hugetlb_pmd_shared(pte_t *pte)
635 {
636         return page_count(virt_to_page(pte)) > 1;
637 }
638 #else
639 static inline bool hugetlb_pmd_shared(pte_t *pte)
640 {
641         return false;
642 }
643 #endif
644
645 #endif /* _LINUX_HUGETLB_H */