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