GNU Linux-libre 5.10.217-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 <linux/pgtable.h>
14 #include <linux/gfp.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 struct hugepage_subpool {
33         spinlock_t lock;
34         long count;
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 */
41                                 /* sasitfy minimum size. */
42 };
43
44 struct resv_map {
45         struct kref refs;
46         spinlock_t lock;
47         struct list_head regions;
48         long adds_in_progress;
49         struct list_head region_cache;
50         long region_cache_count;
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
61 };
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  */
82 struct 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
97 extern struct resv_map *resv_map_alloc(void);
98 void resv_map_release(struct kref *ref);
99
100 extern spinlock_t hugetlb_lock;
101 extern int hugetlb_max_hstate __read_mostly;
102 #define for_each_hstate(h) \
103         for ((h) = hstates; (h) < &hstates[hugetlb_max_hstate]; (h)++)
104
105 struct hugepage_subpool *hugepage_new_subpool(struct hstate *h, long max_hpages,
106                                                 long min_hpages);
107 void hugepage_put_subpool(struct hugepage_subpool *spool);
108
109 void reset_vma_resv_huge_pages(struct vm_area_struct *vma);
110 int hugetlb_sysctl_handler(struct ctl_table *, int, void *, size_t *, loff_t *);
111 int hugetlb_overcommit_handler(struct ctl_table *, int, void *, size_t *,
112                 loff_t *);
113 int hugetlb_treat_movable_handler(struct ctl_table *, int, void *, size_t *,
114                 loff_t *);
115 int hugetlb_mempolicy_sysctl_handler(struct ctl_table *, int, void *, size_t *,
116                 loff_t *);
117
118 int copy_hugetlb_page_range(struct mm_struct *, struct mm_struct *, struct vm_area_struct *);
119 long follow_hugetlb_page(struct mm_struct *, struct vm_area_struct *,
120                          struct page **, struct vm_area_struct **,
121                          unsigned long *, unsigned long *, long, unsigned int,
122                          int *);
123 void unmap_hugepage_range(struct vm_area_struct *,
124                           unsigned long, unsigned long, struct page *);
125 void __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);
129 void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
130                                 unsigned long start, unsigned long end,
131                                 struct page *ref_page);
132 void hugetlb_report_meminfo(struct seq_file *);
133 int hugetlb_report_node_meminfo(char *buf, int len, int nid);
134 void hugetlb_show_meminfo(void);
135 unsigned long hugetlb_total_pages(void);
136 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
137                         unsigned long address, unsigned int flags);
138 int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, pte_t *dst_pte,
139                                 struct vm_area_struct *dst_vma,
140                                 unsigned long dst_addr,
141                                 unsigned long src_addr,
142                                 struct page **pagep);
143 bool hugetlb_reserve_pages(struct inode *inode, long from, long to,
144                                                 struct vm_area_struct *vma,
145                                                 vm_flags_t vm_flags);
146 long hugetlb_unreserve_pages(struct inode *inode, long start, long end,
147                                                 long freed);
148 int isolate_hugetlb(struct page *page, struct list_head *list);
149 void putback_active_hugepage(struct page *page);
150 void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason);
151 void free_huge_page(struct page *page);
152 void hugetlb_fix_reserve_counts(struct inode *inode);
153 extern struct mutex *hugetlb_fault_mutex_table;
154 u32 hugetlb_fault_mutex_hash(struct address_space *mapping, pgoff_t idx);
155
156 pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud);
157
158 struct address_space *hugetlb_page_mapping_lock_write(struct page *hpage);
159
160 extern int sysctl_hugetlb_shm_group;
161 extern struct list_head huge_boot_pages;
162
163 /* arch callbacks */
164
165 pte_t *huge_pte_alloc(struct mm_struct *mm,
166                         unsigned long addr, unsigned long sz);
167 pte_t *huge_pte_offset(struct mm_struct *mm,
168                        unsigned long addr, unsigned long sz);
169 int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,
170                                 unsigned long *addr, pte_t *ptep);
171 void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma,
172                                 unsigned long *start, unsigned long *end);
173 struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address,
174                               int write);
175 struct page *follow_huge_pd(struct vm_area_struct *vma,
176                             unsigned long address, hugepd_t hpd,
177                             int flags, int pdshift);
178 struct page *follow_huge_pmd_pte(struct vm_area_struct *vma, unsigned long address,
179                                  int flags);
180 struct page *follow_huge_pud(struct mm_struct *mm, unsigned long address,
181                                 pud_t *pud, int flags);
182 struct page *follow_huge_pgd(struct mm_struct *mm, unsigned long address,
183                              pgd_t *pgd, int flags);
184
185 int pmd_huge(pmd_t pmd);
186 int pud_huge(pud_t pud);
187 unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
188                 unsigned long address, unsigned long end, pgprot_t newprot);
189
190 bool is_hugetlb_entry_migration(pte_t pte);
191
192 #else /* !CONFIG_HUGETLB_PAGE */
193
194 static inline void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
195 {
196 }
197
198 static inline unsigned long hugetlb_total_pages(void)
199 {
200         return 0;
201 }
202
203 static inline struct address_space *hugetlb_page_mapping_lock_write(
204                                                         struct page *hpage)
205 {
206         return NULL;
207 }
208
209 static inline int huge_pmd_unshare(struct mm_struct *mm,
210                                         struct vm_area_struct *vma,
211                                         unsigned long *addr, pte_t *ptep)
212 {
213         return 0;
214 }
215
216 static inline void adjust_range_if_pmd_sharing_possible(
217                                 struct vm_area_struct *vma,
218                                 unsigned long *start, unsigned long *end)
219 {
220 }
221
222 static inline long follow_hugetlb_page(struct mm_struct *mm,
223                         struct vm_area_struct *vma, struct page **pages,
224                         struct vm_area_struct **vmas, unsigned long *position,
225                         unsigned long *nr_pages, long i, unsigned int flags,
226                         int *nonblocking)
227 {
228         BUG();
229         return 0;
230 }
231
232 static inline struct page *follow_huge_addr(struct mm_struct *mm,
233                                         unsigned long address, int write)
234 {
235         return ERR_PTR(-EINVAL);
236 }
237
238 static inline int copy_hugetlb_page_range(struct mm_struct *dst,
239                         struct mm_struct *src, struct vm_area_struct *vma)
240 {
241         BUG();
242         return 0;
243 }
244
245 static inline void hugetlb_report_meminfo(struct seq_file *m)
246 {
247 }
248
249 static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
250 {
251         return 0;
252 }
253
254 static inline void hugetlb_show_meminfo(void)
255 {
256 }
257
258 static inline struct page *follow_huge_pd(struct vm_area_struct *vma,
259                                 unsigned long address, hugepd_t hpd, int flags,
260                                 int pdshift)
261 {
262         return NULL;
263 }
264
265 static inline struct page *follow_huge_pmd_pte(struct vm_area_struct *vma,
266                                 unsigned long address, int flags)
267 {
268         return NULL;
269 }
270
271 static inline struct page *follow_huge_pud(struct mm_struct *mm,
272                                 unsigned long address, pud_t *pud, int flags)
273 {
274         return NULL;
275 }
276
277 static inline struct page *follow_huge_pgd(struct mm_struct *mm,
278                                 unsigned long address, pgd_t *pgd, int flags)
279 {
280         return NULL;
281 }
282
283 static inline int prepare_hugepage_range(struct file *file,
284                                 unsigned long addr, unsigned long len)
285 {
286         return -EINVAL;
287 }
288
289 static inline int pmd_huge(pmd_t pmd)
290 {
291         return 0;
292 }
293
294 static inline int pud_huge(pud_t pud)
295 {
296         return 0;
297 }
298
299 static inline int is_hugepage_only_range(struct mm_struct *mm,
300                                         unsigned long addr, unsigned long len)
301 {
302         return 0;
303 }
304
305 static inline void hugetlb_free_pgd_range(struct mmu_gather *tlb,
306                                 unsigned long addr, unsigned long end,
307                                 unsigned long floor, unsigned long ceiling)
308 {
309         BUG();
310 }
311
312 static inline int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
313                                                 pte_t *dst_pte,
314                                                 struct vm_area_struct *dst_vma,
315                                                 unsigned long dst_addr,
316                                                 unsigned long src_addr,
317                                                 struct page **pagep)
318 {
319         BUG();
320         return 0;
321 }
322
323 static inline pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr,
324                                         unsigned long sz)
325 {
326         return NULL;
327 }
328
329 static inline int isolate_hugetlb(struct page *page, struct list_head *list)
330 {
331         return -EBUSY;
332 }
333
334 static inline void putback_active_hugepage(struct page *page)
335 {
336 }
337
338 static inline void move_hugetlb_state(struct page *oldpage,
339                                         struct page *newpage, int reason)
340 {
341 }
342
343 static inline unsigned long hugetlb_change_protection(
344                         struct vm_area_struct *vma, unsigned long address,
345                         unsigned long end, pgprot_t newprot)
346 {
347         return 0;
348 }
349
350 static inline void __unmap_hugepage_range_final(struct mmu_gather *tlb,
351                         struct vm_area_struct *vma, unsigned long start,
352                         unsigned long end, struct page *ref_page)
353 {
354         BUG();
355 }
356
357 static inline void __unmap_hugepage_range(struct mmu_gather *tlb,
358                         struct vm_area_struct *vma, unsigned long start,
359                         unsigned long end, struct page *ref_page)
360 {
361         BUG();
362 }
363
364 static inline vm_fault_t hugetlb_fault(struct mm_struct *mm,
365                         struct vm_area_struct *vma, unsigned long address,
366                         unsigned int flags)
367 {
368         BUG();
369         return 0;
370 }
371
372 #endif /* !CONFIG_HUGETLB_PAGE */
373 /*
374  * hugepages at page global directory. If arch support
375  * hugepages at pgd level, they need to define this.
376  */
377 #ifndef pgd_huge
378 #define pgd_huge(x)     0
379 #endif
380 #ifndef p4d_huge
381 #define p4d_huge(x)     0
382 #endif
383
384 #ifndef pgd_write
385 static inline int pgd_write(pgd_t pgd)
386 {
387         BUG();
388         return 0;
389 }
390 #endif
391
392 #define HUGETLB_ANON_FILE "anon_hugepage"
393
394 enum {
395         /*
396          * The file will be used as an shm file so shmfs accounting rules
397          * apply
398          */
399         HUGETLB_SHMFS_INODE     = 1,
400         /*
401          * The file is being created on the internal vfs mount and shmfs
402          * accounting rules do not apply
403          */
404         HUGETLB_ANONHUGE_INODE  = 2,
405 };
406
407 #ifdef CONFIG_HUGETLBFS
408 struct hugetlbfs_sb_info {
409         long    max_inodes;   /* inodes allowed */
410         long    free_inodes;  /* inodes free */
411         spinlock_t      stat_lock;
412         struct hstate *hstate;
413         struct hugepage_subpool *spool;
414         kuid_t  uid;
415         kgid_t  gid;
416         umode_t mode;
417 };
418
419 static inline struct hugetlbfs_sb_info *HUGETLBFS_SB(struct super_block *sb)
420 {
421         return sb->s_fs_info;
422 }
423
424 struct hugetlbfs_inode_info {
425         struct shared_policy policy;
426         struct inode vfs_inode;
427         unsigned int seals;
428 };
429
430 static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
431 {
432         return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
433 }
434
435 extern const struct file_operations hugetlbfs_file_operations;
436 extern const struct vm_operations_struct hugetlb_vm_ops;
437 struct file *hugetlb_file_setup(const char *name, size_t size, vm_flags_t acct,
438                                 struct user_struct **user, int creat_flags,
439                                 int page_size_log);
440
441 static inline bool is_file_hugepages(struct file *file)
442 {
443         if (file->f_op == &hugetlbfs_file_operations)
444                 return true;
445
446         return is_file_shm_hugepages(file);
447 }
448
449 static inline struct hstate *hstate_inode(struct inode *i)
450 {
451         return HUGETLBFS_SB(i->i_sb)->hstate;
452 }
453 #else /* !CONFIG_HUGETLBFS */
454
455 #define is_file_hugepages(file)                 false
456 static inline struct file *
457 hugetlb_file_setup(const char *name, size_t size, vm_flags_t acctflag,
458                 struct user_struct **user, int creat_flags,
459                 int page_size_log)
460 {
461         return ERR_PTR(-ENOSYS);
462 }
463
464 static inline struct hstate *hstate_inode(struct inode *i)
465 {
466         return NULL;
467 }
468 #endif /* !CONFIG_HUGETLBFS */
469
470 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
471 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
472                                         unsigned long len, unsigned long pgoff,
473                                         unsigned long flags);
474 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
475
476 #ifdef CONFIG_HUGETLB_PAGE
477
478 #define HSTATE_NAME_LEN 32
479 /* Defines one hugetlb page size */
480 struct hstate {
481         int next_nid_to_alloc;
482         int next_nid_to_free;
483         unsigned int order;
484         unsigned long mask;
485         unsigned long max_huge_pages;
486         unsigned long nr_huge_pages;
487         unsigned long free_huge_pages;
488         unsigned long resv_huge_pages;
489         unsigned long surplus_huge_pages;
490         unsigned long nr_overcommit_huge_pages;
491         struct list_head hugepage_activelist;
492         struct list_head hugepage_freelists[MAX_NUMNODES];
493         unsigned int nr_huge_pages_node[MAX_NUMNODES];
494         unsigned int free_huge_pages_node[MAX_NUMNODES];
495         unsigned int surplus_huge_pages_node[MAX_NUMNODES];
496 #ifdef CONFIG_CGROUP_HUGETLB
497         /* cgroup control files */
498         struct cftype cgroup_files_dfl[7];
499         struct cftype cgroup_files_legacy[9];
500 #endif
501         char name[HSTATE_NAME_LEN];
502 };
503
504 struct huge_bootmem_page {
505         struct list_head list;
506         struct hstate *hstate;
507 };
508
509 struct page *alloc_huge_page(struct vm_area_struct *vma,
510                                 unsigned long addr, int avoid_reserve);
511 struct page *alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
512                                 nodemask_t *nmask, gfp_t gfp_mask);
513 struct page *alloc_huge_page_vma(struct hstate *h, struct vm_area_struct *vma,
514                                 unsigned long address);
515 int huge_add_to_page_cache(struct page *page, struct address_space *mapping,
516                         pgoff_t idx);
517
518 /* arch callback */
519 int __init __alloc_bootmem_huge_page(struct hstate *h);
520 int __init alloc_bootmem_huge_page(struct hstate *h);
521
522 void __init hugetlb_add_hstate(unsigned order);
523 bool __init arch_hugetlb_valid_size(unsigned long size);
524 struct hstate *size_to_hstate(unsigned long size);
525
526 #ifndef HUGE_MAX_HSTATE
527 #define HUGE_MAX_HSTATE 1
528 #endif
529
530 extern struct hstate hstates[HUGE_MAX_HSTATE];
531 extern unsigned int default_hstate_idx;
532
533 #define default_hstate (hstates[default_hstate_idx])
534
535 static inline struct hstate *hstate_file(struct file *f)
536 {
537         return hstate_inode(file_inode(f));
538 }
539
540 static inline struct hstate *hstate_sizelog(int page_size_log)
541 {
542         if (!page_size_log)
543                 return &default_hstate;
544
545         if (page_size_log < BITS_PER_LONG)
546                 return size_to_hstate(1UL << page_size_log);
547
548         return NULL;
549 }
550
551 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
552 {
553         return hstate_file(vma->vm_file);
554 }
555
556 static inline unsigned long huge_page_size(struct hstate *h)
557 {
558         return (unsigned long)PAGE_SIZE << h->order;
559 }
560
561 extern unsigned long vma_kernel_pagesize(struct vm_area_struct *vma);
562
563 extern unsigned long vma_mmu_pagesize(struct vm_area_struct *vma);
564
565 static inline unsigned long huge_page_mask(struct hstate *h)
566 {
567         return h->mask;
568 }
569
570 static inline unsigned int huge_page_order(struct hstate *h)
571 {
572         return h->order;
573 }
574
575 static inline unsigned huge_page_shift(struct hstate *h)
576 {
577         return h->order + PAGE_SHIFT;
578 }
579
580 static inline bool hstate_is_gigantic(struct hstate *h)
581 {
582         return huge_page_order(h) >= MAX_ORDER;
583 }
584
585 static inline unsigned int pages_per_huge_page(struct hstate *h)
586 {
587         return 1 << h->order;
588 }
589
590 static inline unsigned int blocks_per_huge_page(struct hstate *h)
591 {
592         return huge_page_size(h) / 512;
593 }
594
595 #include <asm/hugetlb.h>
596
597 #ifndef is_hugepage_only_range
598 static inline int is_hugepage_only_range(struct mm_struct *mm,
599                                         unsigned long addr, unsigned long len)
600 {
601         return 0;
602 }
603 #define is_hugepage_only_range is_hugepage_only_range
604 #endif
605
606 #ifndef arch_clear_hugepage_flags
607 static inline void arch_clear_hugepage_flags(struct page *page) { }
608 #define arch_clear_hugepage_flags arch_clear_hugepage_flags
609 #endif
610
611 #ifndef arch_make_huge_pte
612 static inline pte_t arch_make_huge_pte(pte_t entry, struct vm_area_struct *vma,
613                                        struct page *page, int writable)
614 {
615         return entry;
616 }
617 #endif
618
619 static inline struct hstate *page_hstate(struct page *page)
620 {
621         VM_BUG_ON_PAGE(!PageHuge(page), page);
622         return size_to_hstate(page_size(page));
623 }
624
625 static inline unsigned hstate_index_to_shift(unsigned index)
626 {
627         return hstates[index].order + PAGE_SHIFT;
628 }
629
630 static inline int hstate_index(struct hstate *h)
631 {
632         return h - hstates;
633 }
634
635 extern int dissolve_free_huge_page(struct page *page);
636 extern int dissolve_free_huge_pages(unsigned long start_pfn,
637                                     unsigned long end_pfn);
638
639 #ifdef CONFIG_ARCH_ENABLE_HUGEPAGE_MIGRATION
640 #ifndef arch_hugetlb_migration_supported
641 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
642 {
643         if ((huge_page_shift(h) == PMD_SHIFT) ||
644                 (huge_page_shift(h) == PUD_SHIFT) ||
645                         (huge_page_shift(h) == PGDIR_SHIFT))
646                 return true;
647         else
648                 return false;
649 }
650 #endif
651 #else
652 static inline bool arch_hugetlb_migration_supported(struct hstate *h)
653 {
654         return false;
655 }
656 #endif
657
658 static inline bool hugepage_migration_supported(struct hstate *h)
659 {
660         return arch_hugetlb_migration_supported(h);
661 }
662
663 /*
664  * Movability check is different as compared to migration check.
665  * It determines whether or not a huge page should be placed on
666  * movable zone or not. Movability of any huge page should be
667  * required only if huge page size is supported for migration.
668  * There wont be any reason for the huge page to be movable if
669  * it is not migratable to start with. Also the size of the huge
670  * page should be large enough to be placed under a movable zone
671  * and still feasible enough to be migratable. Just the presence
672  * in movable zone does not make the migration feasible.
673  *
674  * So even though large huge page sizes like the gigantic ones
675  * are migratable they should not be movable because its not
676  * feasible to migrate them from movable zone.
677  */
678 static inline bool hugepage_movable_supported(struct hstate *h)
679 {
680         if (!hugepage_migration_supported(h))
681                 return false;
682
683         if (hstate_is_gigantic(h))
684                 return false;
685         return true;
686 }
687
688 /* Movability of hugepages depends on migration support. */
689 static inline gfp_t htlb_alloc_mask(struct hstate *h)
690 {
691         if (hugepage_movable_supported(h))
692                 return GFP_HIGHUSER_MOVABLE;
693         else
694                 return GFP_HIGHUSER;
695 }
696
697 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
698 {
699         gfp_t modified_mask = htlb_alloc_mask(h);
700
701         /* Some callers might want to enforce node */
702         modified_mask |= (gfp_mask & __GFP_THISNODE);
703
704         modified_mask |= (gfp_mask & __GFP_NOWARN);
705
706         return modified_mask;
707 }
708
709 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
710                                            struct mm_struct *mm, pte_t *pte)
711 {
712         if (huge_page_size(h) == PMD_SIZE)
713                 return pmd_lockptr(mm, (pmd_t *) pte);
714         VM_BUG_ON(huge_page_size(h) == PAGE_SIZE);
715         return &mm->page_table_lock;
716 }
717
718 #ifndef hugepages_supported
719 /*
720  * Some platform decide whether they support huge pages at boot
721  * time. Some of them, such as powerpc, set HPAGE_SHIFT to 0
722  * when there is no such support
723  */
724 #define hugepages_supported() (HPAGE_SHIFT != 0)
725 #endif
726
727 void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm);
728
729 static inline void hugetlb_count_init(struct mm_struct *mm)
730 {
731         atomic_long_set(&mm->hugetlb_usage, 0);
732 }
733
734 static inline void hugetlb_count_add(long l, struct mm_struct *mm)
735 {
736         atomic_long_add(l, &mm->hugetlb_usage);
737 }
738
739 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
740 {
741         atomic_long_sub(l, &mm->hugetlb_usage);
742 }
743
744 #ifndef set_huge_swap_pte_at
745 static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
746                                         pte_t *ptep, pte_t pte, unsigned long sz)
747 {
748         set_huge_pte_at(mm, addr, ptep, pte);
749 }
750 #endif
751
752 #ifndef huge_ptep_modify_prot_start
753 #define huge_ptep_modify_prot_start huge_ptep_modify_prot_start
754 static inline pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
755                                                 unsigned long addr, pte_t *ptep)
756 {
757         return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep);
758 }
759 #endif
760
761 #ifndef huge_ptep_modify_prot_commit
762 #define huge_ptep_modify_prot_commit huge_ptep_modify_prot_commit
763 static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
764                                                 unsigned long addr, pte_t *ptep,
765                                                 pte_t old_pte, pte_t pte)
766 {
767         set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
768 }
769 #endif
770
771 void set_page_huge_active(struct page *page);
772
773 #else   /* CONFIG_HUGETLB_PAGE */
774 struct hstate {};
775
776 static inline struct page *alloc_huge_page(struct vm_area_struct *vma,
777                                            unsigned long addr,
778                                            int avoid_reserve)
779 {
780         return NULL;
781 }
782
783 static inline struct page *
784 alloc_huge_page_nodemask(struct hstate *h, int preferred_nid,
785                         nodemask_t *nmask, gfp_t gfp_mask)
786 {
787         return NULL;
788 }
789
790 static inline struct page *alloc_huge_page_vma(struct hstate *h,
791                                                struct vm_area_struct *vma,
792                                                unsigned long address)
793 {
794         return NULL;
795 }
796
797 static inline int __alloc_bootmem_huge_page(struct hstate *h)
798 {
799         return 0;
800 }
801
802 static inline struct hstate *hstate_file(struct file *f)
803 {
804         return NULL;
805 }
806
807 static inline struct hstate *hstate_sizelog(int page_size_log)
808 {
809         return NULL;
810 }
811
812 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
813 {
814         return NULL;
815 }
816
817 static inline struct hstate *page_hstate(struct page *page)
818 {
819         return NULL;
820 }
821
822 static inline unsigned long huge_page_size(struct hstate *h)
823 {
824         return PAGE_SIZE;
825 }
826
827 static inline unsigned long huge_page_mask(struct hstate *h)
828 {
829         return PAGE_MASK;
830 }
831
832 static inline unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
833 {
834         return PAGE_SIZE;
835 }
836
837 static inline unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
838 {
839         return PAGE_SIZE;
840 }
841
842 static inline unsigned int huge_page_order(struct hstate *h)
843 {
844         return 0;
845 }
846
847 static inline unsigned int huge_page_shift(struct hstate *h)
848 {
849         return PAGE_SHIFT;
850 }
851
852 static inline bool hstate_is_gigantic(struct hstate *h)
853 {
854         return false;
855 }
856
857 static inline unsigned int pages_per_huge_page(struct hstate *h)
858 {
859         return 1;
860 }
861
862 static inline unsigned hstate_index_to_shift(unsigned index)
863 {
864         return 0;
865 }
866
867 static inline int hstate_index(struct hstate *h)
868 {
869         return 0;
870 }
871
872 static inline int dissolve_free_huge_page(struct page *page)
873 {
874         return 0;
875 }
876
877 static inline int dissolve_free_huge_pages(unsigned long start_pfn,
878                                            unsigned long end_pfn)
879 {
880         return 0;
881 }
882
883 static inline bool hugepage_migration_supported(struct hstate *h)
884 {
885         return false;
886 }
887
888 static inline bool hugepage_movable_supported(struct hstate *h)
889 {
890         return false;
891 }
892
893 static inline gfp_t htlb_alloc_mask(struct hstate *h)
894 {
895         return 0;
896 }
897
898 static inline gfp_t htlb_modify_alloc_mask(struct hstate *h, gfp_t gfp_mask)
899 {
900         return 0;
901 }
902
903 static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
904                                            struct mm_struct *mm, pte_t *pte)
905 {
906         return &mm->page_table_lock;
907 }
908
909 static inline void hugetlb_count_init(struct mm_struct *mm)
910 {
911 }
912
913 static inline void hugetlb_report_usage(struct seq_file *f, struct mm_struct *m)
914 {
915 }
916
917 static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
918 {
919 }
920
921 static inline void set_huge_swap_pte_at(struct mm_struct *mm, unsigned long addr,
922                                         pte_t *ptep, pte_t pte, unsigned long sz)
923 {
924 }
925 #endif  /* CONFIG_HUGETLB_PAGE */
926
927 static inline spinlock_t *huge_pte_lock(struct hstate *h,
928                                         struct mm_struct *mm, pte_t *pte)
929 {
930         spinlock_t *ptl;
931
932         ptl = huge_pte_lockptr(h, mm, pte);
933         spin_lock(ptl);
934         return ptl;
935 }
936
937 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
938 extern void __init hugetlb_cma_reserve(int order);
939 extern void __init hugetlb_cma_check(void);
940 #else
941 static inline __init void hugetlb_cma_reserve(int order)
942 {
943 }
944 static inline __init void hugetlb_cma_check(void)
945 {
946 }
947 #endif
948
949 #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
950 static inline bool hugetlb_pmd_shared(pte_t *pte)
951 {
952         return page_count(virt_to_page(pte)) > 1;
953 }
954 #else
955 static inline bool hugetlb_pmd_shared(pte_t *pte)
956 {
957         return false;
958 }
959 #endif
960
961 #endif /* _LINUX_HUGETLB_H */