Merge tag 'amd-drm-next-5.7-2020-04-01' of git://people.freedesktop.org/~agd5f/linux...
[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 extern vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
11 extern 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 *vma);
14 extern void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd);
15 extern 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 extern 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 extern vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd);
28 extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
29                                           unsigned long addr,
30                                           pmd_t *pmd,
31                                           unsigned int flags);
32 extern bool madvise_free_huge_pmd(struct mmu_gather *tlb,
33                         struct vm_area_struct *vma,
34                         pmd_t *pmd, unsigned long addr, unsigned long next);
35 extern int zap_huge_pmd(struct mmu_gather *tlb,
36                         struct vm_area_struct *vma,
37                         pmd_t *pmd, unsigned long addr);
38 extern int zap_huge_pud(struct mmu_gather *tlb,
39                         struct vm_area_struct *vma,
40                         pud_t *pud, unsigned long addr);
41 extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
42                         unsigned long addr, unsigned long end,
43                         unsigned char *vec);
44 extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
45                          unsigned long new_addr, unsigned long old_end,
46                          pmd_t *old_pmd, pmd_t *new_pmd);
47 extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
48                         unsigned long addr, pgprot_t newprot,
49                         int prot_numa);
50 vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
51                                    pgprot_t pgprot, bool write);
52
53 /**
54  * vmf_insert_pfn_pmd - insert a pmd size pfn
55  * @vmf: Structure describing the fault
56  * @pfn: pfn to insert
57  * @pgprot: page protection to use
58  * @write: whether it's a write fault
59  *
60  * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
61  *
62  * Return: vm_fault_t value.
63  */
64 static inline vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn,
65                                             bool write)
66 {
67         return vmf_insert_pfn_pmd_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
68 }
69 vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
70                                    pgprot_t pgprot, bool write);
71
72 /**
73  * vmf_insert_pfn_pud - insert a pud size pfn
74  * @vmf: Structure describing the fault
75  * @pfn: pfn to insert
76  * @pgprot: page protection to use
77  * @write: whether it's a write fault
78  *
79  * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
80  *
81  * Return: vm_fault_t value.
82  */
83 static inline vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn,
84                                             bool write)
85 {
86         return vmf_insert_pfn_pud_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
87 }
88
89 enum transparent_hugepage_flag {
90         TRANSPARENT_HUGEPAGE_FLAG,
91         TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
92         TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
93         TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
94         TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
95         TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
96         TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
97         TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
98 #ifdef CONFIG_DEBUG_VM
99         TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG,
100 #endif
101 };
102
103 struct kobject;
104 struct kobj_attribute;
105
106 extern ssize_t single_hugepage_flag_store(struct kobject *kobj,
107                                  struct kobj_attribute *attr,
108                                  const char *buf, size_t count,
109                                  enum transparent_hugepage_flag flag);
110 extern ssize_t single_hugepage_flag_show(struct kobject *kobj,
111                                 struct kobj_attribute *attr, char *buf,
112                                 enum transparent_hugepage_flag flag);
113 extern struct kobj_attribute shmem_enabled_attr;
114
115 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
116 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
117
118 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
119 #define HPAGE_PMD_SHIFT PMD_SHIFT
120 #define HPAGE_PMD_SIZE  ((1UL) << HPAGE_PMD_SHIFT)
121 #define HPAGE_PMD_MASK  (~(HPAGE_PMD_SIZE - 1))
122
123 #define HPAGE_PUD_SHIFT PUD_SHIFT
124 #define HPAGE_PUD_SIZE  ((1UL) << HPAGE_PUD_SHIFT)
125 #define HPAGE_PUD_MASK  (~(HPAGE_PUD_SIZE - 1))
126
127 extern bool is_vma_temporary_stack(struct vm_area_struct *vma);
128
129 extern unsigned long transparent_hugepage_flags;
130
131 /*
132  * to be used on vmas which are known to support THP.
133  * Use transparent_hugepage_enabled otherwise
134  */
135 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
136 {
137         if (vma->vm_flags & VM_NOHUGEPAGE)
138                 return false;
139
140         if (is_vma_temporary_stack(vma))
141                 return false;
142
143         if (test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
144                 return false;
145
146         if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
147                 return true;
148         /*
149          * For dax vmas, try to always use hugepage mappings. If the kernel does
150          * not support hugepages, fsdax mappings will fallback to PAGE_SIZE
151          * mappings, and device-dax namespaces, that try to guarantee a given
152          * mapping size, will fail to enable
153          */
154         if (vma_is_dax(vma))
155                 return true;
156
157         if (transparent_hugepage_flags &
158                                 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
159                 return !!(vma->vm_flags & VM_HUGEPAGE);
160
161         return false;
162 }
163
164 bool transparent_hugepage_enabled(struct vm_area_struct *vma);
165
166 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
167
168 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
169                 unsigned long haddr)
170 {
171         /* Don't have to check pgoff for anonymous vma */
172         if (!vma_is_anonymous(vma)) {
173                 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
174                         (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
175                         return false;
176         }
177
178         if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
179                 return false;
180         return true;
181 }
182
183 #define transparent_hugepage_use_zero_page()                            \
184         (transparent_hugepage_flags &                                   \
185          (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
186 #ifdef CONFIG_DEBUG_VM
187 #define transparent_hugepage_debug_cow()                                \
188         (transparent_hugepage_flags &                                   \
189          (1<<TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG))
190 #else /* CONFIG_DEBUG_VM */
191 #define transparent_hugepage_debug_cow() 0
192 #endif /* CONFIG_DEBUG_VM */
193
194 extern unsigned long thp_get_unmapped_area(struct file *filp,
195                 unsigned long addr, unsigned long len, unsigned long pgoff,
196                 unsigned long flags);
197
198 extern void prep_transhuge_page(struct page *page);
199 extern void free_transhuge_page(struct page *page);
200 bool is_transparent_hugepage(struct page *page);
201
202 bool can_split_huge_page(struct page *page, int *pextra_pins);
203 int split_huge_page_to_list(struct page *page, struct list_head *list);
204 static inline int split_huge_page(struct page *page)
205 {
206         return split_huge_page_to_list(page, NULL);
207 }
208 void deferred_split_huge_page(struct page *page);
209
210 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
211                 unsigned long address, bool freeze, struct page *page);
212
213 #define split_huge_pmd(__vma, __pmd, __address)                         \
214         do {                                                            \
215                 pmd_t *____pmd = (__pmd);                               \
216                 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd)   \
217                                         || pmd_devmap(*____pmd))        \
218                         __split_huge_pmd(__vma, __pmd, __address,       \
219                                                 false, NULL);           \
220         }  while (0)
221
222
223 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
224                 bool freeze, struct page *page);
225
226 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
227                 unsigned long address);
228
229 #define split_huge_pud(__vma, __pud, __address)                         \
230         do {                                                            \
231                 pud_t *____pud = (__pud);                               \
232                 if (pud_trans_huge(*____pud)                            \
233                                         || pud_devmap(*____pud))        \
234                         __split_huge_pud(__vma, __pud, __address);      \
235         }  while (0)
236
237 extern int hugepage_madvise(struct vm_area_struct *vma,
238                             unsigned long *vm_flags, int advice);
239 extern void vma_adjust_trans_huge(struct vm_area_struct *vma,
240                                     unsigned long start,
241                                     unsigned long end,
242                                     long adjust_next);
243 extern spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd,
244                 struct vm_area_struct *vma);
245 extern spinlock_t *__pud_trans_huge_lock(pud_t *pud,
246                 struct vm_area_struct *vma);
247
248 static inline int is_swap_pmd(pmd_t pmd)
249 {
250         return !pmd_none(pmd) && !pmd_present(pmd);
251 }
252
253 /* mmap_sem must be held on entry */
254 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
255                 struct vm_area_struct *vma)
256 {
257         if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
258                 return __pmd_trans_huge_lock(pmd, vma);
259         else
260                 return NULL;
261 }
262 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
263                 struct vm_area_struct *vma)
264 {
265         if (pud_trans_huge(*pud) || pud_devmap(*pud))
266                 return __pud_trans_huge_lock(pud, vma);
267         else
268                 return NULL;
269 }
270 static inline int hpage_nr_pages(struct page *page)
271 {
272         if (unlikely(PageTransHuge(page)))
273                 return HPAGE_PMD_NR;
274         return 1;
275 }
276
277 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
278                 pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
279 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
280                 pud_t *pud, int flags, struct dev_pagemap **pgmap);
281
282 extern vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t orig_pmd);
283
284 extern struct page *huge_zero_page;
285
286 static inline bool is_huge_zero_page(struct page *page)
287 {
288         return READ_ONCE(huge_zero_page) == page;
289 }
290
291 static inline bool is_huge_zero_pmd(pmd_t pmd)
292 {
293         return is_huge_zero_page(pmd_page(pmd));
294 }
295
296 static inline bool is_huge_zero_pud(pud_t pud)
297 {
298         return false;
299 }
300
301 struct page *mm_get_huge_zero_page(struct mm_struct *mm);
302 void mm_put_huge_zero_page(struct mm_struct *mm);
303
304 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
305
306 static inline bool thp_migration_supported(void)
307 {
308         return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
309 }
310
311 static inline struct list_head *page_deferred_list(struct page *page)
312 {
313         /*
314          * Global or memcg deferred list in the second tail pages is
315          * occupied by compound_head.
316          */
317         return &page[2].deferred_list;
318 }
319
320 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
321 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
322 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
323 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
324
325 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
326 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
327 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
328
329 #define hpage_nr_pages(x) 1
330
331 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
332 {
333         return false;
334 }
335
336 static inline bool transparent_hugepage_enabled(struct vm_area_struct *vma)
337 {
338         return false;
339 }
340
341 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
342                 unsigned long haddr)
343 {
344         return false;
345 }
346
347 static inline void prep_transhuge_page(struct page *page) {}
348
349 static inline bool is_transparent_hugepage(struct page *page)
350 {
351         return false;
352 }
353
354 #define transparent_hugepage_flags 0UL
355
356 #define thp_get_unmapped_area   NULL
357
358 static inline bool
359 can_split_huge_page(struct page *page, int *pextra_pins)
360 {
361         BUILD_BUG();
362         return false;
363 }
364 static inline int
365 split_huge_page_to_list(struct page *page, struct list_head *list)
366 {
367         return 0;
368 }
369 static inline int split_huge_page(struct page *page)
370 {
371         return 0;
372 }
373 static inline void deferred_split_huge_page(struct page *page) {}
374 #define split_huge_pmd(__vma, __pmd, __address) \
375         do { } while (0)
376
377 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
378                 unsigned long address, bool freeze, struct page *page) {}
379 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
380                 unsigned long address, bool freeze, struct page *page) {}
381
382 #define split_huge_pud(__vma, __pmd, __address) \
383         do { } while (0)
384
385 static inline int hugepage_madvise(struct vm_area_struct *vma,
386                                    unsigned long *vm_flags, int advice)
387 {
388         BUG();
389         return 0;
390 }
391 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
392                                          unsigned long start,
393                                          unsigned long end,
394                                          long adjust_next)
395 {
396 }
397 static inline int is_swap_pmd(pmd_t pmd)
398 {
399         return 0;
400 }
401 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
402                 struct vm_area_struct *vma)
403 {
404         return NULL;
405 }
406 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
407                 struct vm_area_struct *vma)
408 {
409         return NULL;
410 }
411
412 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf,
413                 pmd_t orig_pmd)
414 {
415         return 0;
416 }
417
418 static inline bool is_huge_zero_page(struct page *page)
419 {
420         return false;
421 }
422
423 static inline bool is_huge_zero_pud(pud_t pud)
424 {
425         return false;
426 }
427
428 static inline void mm_put_huge_zero_page(struct mm_struct *mm)
429 {
430         return;
431 }
432
433 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
434         unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
435 {
436         return NULL;
437 }
438
439 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
440         unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap)
441 {
442         return NULL;
443 }
444
445 static inline bool thp_migration_supported(void)
446 {
447         return false;
448 }
449 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
450
451 #endif /* _LINUX_HUGE_MM_H */