Merge tag 'trace-v5.15-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.git] / include / linux / huge_mm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGE_MM_H
3 #define _LINUX_HUGE_MM_H
4
5 #include <linux/sched/coredump.h>
6 #include <linux/mm_types.h>
7
8 #include <linux/fs.h> /* only for vma_is_dax() */
9
10 vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
11 int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
12                   pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
13                   struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
14 void huge_pmd_set_accessed(struct vm_fault *vmf);
15 int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
16                   pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
17                   struct vm_area_struct *vma);
18
19 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
20 void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud);
21 #else
22 static inline void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
23 {
24 }
25 #endif
26
27 vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf);
28 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
29                                    unsigned long addr, pmd_t *pmd,
30                                    unsigned int flags);
31 bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
32                            pmd_t *pmd, unsigned long addr, unsigned long next);
33 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd,
34                  unsigned long addr);
35 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud,
36                  unsigned long addr);
37 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
38                    unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd);
39 int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
40                     pgprot_t newprot, unsigned long cp_flags);
41 vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
42                                    pgprot_t pgprot, bool write);
43
44 /**
45  * vmf_insert_pfn_pmd - insert a pmd size pfn
46  * @vmf: Structure describing the fault
47  * @pfn: pfn to insert
48  * @pgprot: page protection to use
49  * @write: whether it's a write fault
50  *
51  * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
52  *
53  * Return: vm_fault_t value.
54  */
55 static inline vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn,
56                                             bool write)
57 {
58         return vmf_insert_pfn_pmd_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
59 }
60 vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
61                                    pgprot_t pgprot, bool write);
62
63 /**
64  * vmf_insert_pfn_pud - insert a pud size pfn
65  * @vmf: Structure describing the fault
66  * @pfn: pfn to insert
67  * @pgprot: page protection to use
68  * @write: whether it's a write fault
69  *
70  * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
71  *
72  * Return: vm_fault_t value.
73  */
74 static inline vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn,
75                                             bool write)
76 {
77         return vmf_insert_pfn_pud_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
78 }
79
80 enum transparent_hugepage_flag {
81         TRANSPARENT_HUGEPAGE_NEVER_DAX,
82         TRANSPARENT_HUGEPAGE_FLAG,
83         TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
84         TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
85         TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
86         TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
87         TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
88         TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
89         TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
90 };
91
92 struct kobject;
93 struct kobj_attribute;
94
95 ssize_t single_hugepage_flag_store(struct kobject *kobj,
96                                    struct kobj_attribute *attr,
97                                    const char *buf, size_t count,
98                                    enum transparent_hugepage_flag flag);
99 ssize_t single_hugepage_flag_show(struct kobject *kobj,
100                                   struct kobj_attribute *attr, char *buf,
101                                   enum transparent_hugepage_flag flag);
102 extern struct kobj_attribute shmem_enabled_attr;
103
104 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
105 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
106
107 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
108 #define HPAGE_PMD_SHIFT PMD_SHIFT
109 #define HPAGE_PMD_SIZE  ((1UL) << HPAGE_PMD_SHIFT)
110 #define HPAGE_PMD_MASK  (~(HPAGE_PMD_SIZE - 1))
111
112 #define HPAGE_PUD_SHIFT PUD_SHIFT
113 #define HPAGE_PUD_SIZE  ((1UL) << HPAGE_PUD_SHIFT)
114 #define HPAGE_PUD_MASK  (~(HPAGE_PUD_SIZE - 1))
115
116 extern unsigned long transparent_hugepage_flags;
117
118 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
119                 unsigned long haddr)
120 {
121         /* Don't have to check pgoff for anonymous vma */
122         if (!vma_is_anonymous(vma)) {
123                 if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
124                                 HPAGE_PMD_NR))
125                         return false;
126         }
127
128         if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
129                 return false;
130         return true;
131 }
132
133 static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
134                                           unsigned long vm_flags)
135 {
136         /* Explicitly disabled through madvise. */
137         if ((vm_flags & VM_NOHUGEPAGE) ||
138             test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
139                 return false;
140         return true;
141 }
142
143 /*
144  * to be used on vmas which are known to support THP.
145  * Use transparent_hugepage_active otherwise
146  */
147 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
148 {
149
150         /*
151          * If the hardware/firmware marked hugepage support disabled.
152          */
153         if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
154                 return false;
155
156         if (!transhuge_vma_enabled(vma, vma->vm_flags))
157                 return false;
158
159         if (vma_is_temporary_stack(vma))
160                 return false;
161
162         if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
163                 return true;
164
165         if (vma_is_dax(vma))
166                 return true;
167
168         if (transparent_hugepage_flags &
169                                 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
170                 return !!(vma->vm_flags & VM_HUGEPAGE);
171
172         return false;
173 }
174
175 bool transparent_hugepage_active(struct vm_area_struct *vma);
176
177 #define transparent_hugepage_use_zero_page()                            \
178         (transparent_hugepage_flags &                                   \
179          (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
180
181 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
182                 unsigned long len, unsigned long pgoff, unsigned long flags);
183
184 void prep_transhuge_page(struct page *page);
185 void free_transhuge_page(struct page *page);
186 bool is_transparent_hugepage(struct page *page);
187
188 bool can_split_huge_page(struct page *page, int *pextra_pins);
189 int split_huge_page_to_list(struct page *page, struct list_head *list);
190 static inline int split_huge_page(struct page *page)
191 {
192         return split_huge_page_to_list(page, NULL);
193 }
194 void deferred_split_huge_page(struct page *page);
195
196 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
197                 unsigned long address, bool freeze, struct page *page);
198
199 #define split_huge_pmd(__vma, __pmd, __address)                         \
200         do {                                                            \
201                 pmd_t *____pmd = (__pmd);                               \
202                 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd)   \
203                                         || pmd_devmap(*____pmd))        \
204                         __split_huge_pmd(__vma, __pmd, __address,       \
205                                                 false, NULL);           \
206         }  while (0)
207
208
209 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
210                 bool freeze, struct page *page);
211
212 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
213                 unsigned long address);
214
215 #define split_huge_pud(__vma, __pud, __address)                         \
216         do {                                                            \
217                 pud_t *____pud = (__pud);                               \
218                 if (pud_trans_huge(*____pud)                            \
219                                         || pud_devmap(*____pud))        \
220                         __split_huge_pud(__vma, __pud, __address);      \
221         }  while (0)
222
223 int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags,
224                      int advice);
225 void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
226                            unsigned long end, long adjust_next);
227 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
228 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
229
230 static inline int is_swap_pmd(pmd_t pmd)
231 {
232         return !pmd_none(pmd) && !pmd_present(pmd);
233 }
234
235 /* mmap_lock must be held on entry */
236 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
237                 struct vm_area_struct *vma)
238 {
239         if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
240                 return __pmd_trans_huge_lock(pmd, vma);
241         else
242                 return NULL;
243 }
244 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
245                 struct vm_area_struct *vma)
246 {
247         if (pud_trans_huge(*pud) || pud_devmap(*pud))
248                 return __pud_trans_huge_lock(pud, vma);
249         else
250                 return NULL;
251 }
252
253 /**
254  * thp_head - Head page of a transparent huge page.
255  * @page: Any page (tail, head or regular) found in the page cache.
256  */
257 static inline struct page *thp_head(struct page *page)
258 {
259         return compound_head(page);
260 }
261
262 /**
263  * thp_order - Order of a transparent huge page.
264  * @page: Head page of a transparent huge page.
265  */
266 static inline unsigned int thp_order(struct page *page)
267 {
268         VM_BUG_ON_PGFLAGS(PageTail(page), page);
269         if (PageHead(page))
270                 return HPAGE_PMD_ORDER;
271         return 0;
272 }
273
274 /**
275  * thp_nr_pages - The number of regular pages in this huge page.
276  * @page: The head page of a huge page.
277  */
278 static inline int thp_nr_pages(struct page *page)
279 {
280         VM_BUG_ON_PGFLAGS(PageTail(page), page);
281         if (PageHead(page))
282                 return HPAGE_PMD_NR;
283         return 1;
284 }
285
286 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
287                 pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
288 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
289                 pud_t *pud, int flags, struct dev_pagemap **pgmap);
290
291 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
292
293 extern struct page *huge_zero_page;
294 extern unsigned long huge_zero_pfn;
295
296 static inline bool is_huge_zero_page(struct page *page)
297 {
298         return READ_ONCE(huge_zero_page) == page;
299 }
300
301 static inline bool is_huge_zero_pmd(pmd_t pmd)
302 {
303         return READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd) && pmd_present(pmd);
304 }
305
306 static inline bool is_huge_zero_pud(pud_t pud)
307 {
308         return false;
309 }
310
311 struct page *mm_get_huge_zero_page(struct mm_struct *mm);
312 void mm_put_huge_zero_page(struct mm_struct *mm);
313
314 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
315
316 static inline bool thp_migration_supported(void)
317 {
318         return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
319 }
320
321 static inline struct list_head *page_deferred_list(struct page *page)
322 {
323         /*
324          * Global or memcg deferred list in the second tail pages is
325          * occupied by compound_head.
326          */
327         return &page[2].deferred_list;
328 }
329
330 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
331 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
332 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
333 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
334
335 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
336 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
337 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
338
339 static inline struct page *thp_head(struct page *page)
340 {
341         VM_BUG_ON_PGFLAGS(PageTail(page), page);
342         return page;
343 }
344
345 static inline unsigned int thp_order(struct page *page)
346 {
347         VM_BUG_ON_PGFLAGS(PageTail(page), page);
348         return 0;
349 }
350
351 static inline int thp_nr_pages(struct page *page)
352 {
353         VM_BUG_ON_PGFLAGS(PageTail(page), page);
354         return 1;
355 }
356
357 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
358 {
359         return false;
360 }
361
362 static inline bool transparent_hugepage_active(struct vm_area_struct *vma)
363 {
364         return false;
365 }
366
367 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
368                 unsigned long haddr)
369 {
370         return false;
371 }
372
373 static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
374                                           unsigned long vm_flags)
375 {
376         return false;
377 }
378
379 static inline void prep_transhuge_page(struct page *page) {}
380
381 static inline bool is_transparent_hugepage(struct page *page)
382 {
383         return false;
384 }
385
386 #define transparent_hugepage_flags 0UL
387
388 #define thp_get_unmapped_area   NULL
389
390 static inline bool
391 can_split_huge_page(struct page *page, int *pextra_pins)
392 {
393         BUILD_BUG();
394         return false;
395 }
396 static inline int
397 split_huge_page_to_list(struct page *page, struct list_head *list)
398 {
399         return 0;
400 }
401 static inline int split_huge_page(struct page *page)
402 {
403         return 0;
404 }
405 static inline void deferred_split_huge_page(struct page *page) {}
406 #define split_huge_pmd(__vma, __pmd, __address) \
407         do { } while (0)
408
409 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
410                 unsigned long address, bool freeze, struct page *page) {}
411 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
412                 unsigned long address, bool freeze, struct page *page) {}
413
414 #define split_huge_pud(__vma, __pmd, __address) \
415         do { } while (0)
416
417 static inline int hugepage_madvise(struct vm_area_struct *vma,
418                                    unsigned long *vm_flags, int advice)
419 {
420         BUG();
421         return 0;
422 }
423 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
424                                          unsigned long start,
425                                          unsigned long end,
426                                          long adjust_next)
427 {
428 }
429 static inline int is_swap_pmd(pmd_t pmd)
430 {
431         return 0;
432 }
433 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
434                 struct vm_area_struct *vma)
435 {
436         return NULL;
437 }
438 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
439                 struct vm_area_struct *vma)
440 {
441         return NULL;
442 }
443
444 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
445 {
446         return 0;
447 }
448
449 static inline bool is_huge_zero_page(struct page *page)
450 {
451         return false;
452 }
453
454 static inline bool is_huge_zero_pmd(pmd_t pmd)
455 {
456         return false;
457 }
458
459 static inline bool is_huge_zero_pud(pud_t pud)
460 {
461         return false;
462 }
463
464 static inline void mm_put_huge_zero_page(struct mm_struct *mm)
465 {
466         return;
467 }
468
469 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
470         unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
471 {
472         return NULL;
473 }
474
475 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
476         unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap)
477 {
478         return NULL;
479 }
480
481 static inline bool thp_migration_supported(void)
482 {
483         return false;
484 }
485 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
486
487 /**
488  * thp_size - Size of a transparent huge page.
489  * @page: Head page of a transparent huge page.
490  *
491  * Return: Number of bytes in this page.
492  */
493 static inline unsigned long thp_size(struct page *page)
494 {
495         return PAGE_SIZE << thp_order(page);
496 }
497
498 #endif /* _LINUX_HUGE_MM_H */