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, pmd_t orig_pmd);
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
huge_pud_set_accessed(struct vm_fault * vmf,pud_t orig_pud)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, pmd_t orig_pmd);
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 */
vmf_insert_pfn_pmd(struct vm_fault * vmf,pfn_t pfn,bool write)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 */
vmf_insert_pfn_pud(struct vm_fault * vmf,pfn_t pfn,bool write)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 #ifdef CONFIG_DEBUG_VM
91 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG,
92 #endif
93 };
94
95 struct kobject;
96 struct kobj_attribute;
97
98 ssize_t single_hugepage_flag_store(struct kobject *kobj,
99 struct kobj_attribute *attr,
100 const char *buf, size_t count,
101 enum transparent_hugepage_flag flag);
102 ssize_t single_hugepage_flag_show(struct kobject *kobj,
103 struct kobj_attribute *attr, char *buf,
104 enum transparent_hugepage_flag flag);
105 extern struct kobj_attribute shmem_enabled_attr;
106
107 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
108 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
109
110 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
111 #define HPAGE_PMD_SHIFT PMD_SHIFT
112 #define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT)
113 #define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1))
114
115 #define HPAGE_PUD_SHIFT PUD_SHIFT
116 #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT)
117 #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1))
118
119 extern unsigned long transparent_hugepage_flags;
120
transhuge_vma_suitable(struct vm_area_struct * vma,unsigned long haddr)121 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
122 unsigned long haddr)
123 {
124 /* Don't have to check pgoff for anonymous vma */
125 if (!vma_is_anonymous(vma)) {
126 if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
127 HPAGE_PMD_NR))
128 return false;
129 }
130
131 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
132 return false;
133 return true;
134 }
135
transhuge_vma_enabled(struct vm_area_struct * vma,unsigned long vm_flags)136 static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
137 unsigned long vm_flags)
138 {
139 /* Explicitly disabled through madvise. */
140 if ((vm_flags & VM_NOHUGEPAGE) ||
141 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
142 return false;
143 return true;
144 }
145
146 /*
147 * to be used on vmas which are known to support THP.
148 * Use transparent_hugepage_active otherwise
149 */
__transparent_hugepage_enabled(struct vm_area_struct * vma)150 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
151 {
152
153 /*
154 * If the hardware/firmware marked hugepage support disabled.
155 */
156 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
157 return false;
158
159 if (!transhuge_vma_enabled(vma, vma->vm_flags))
160 return false;
161
162 if (vma_is_temporary_stack(vma))
163 return false;
164
165 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
166 return true;
167
168 if (vma_is_dax(vma))
169 return true;
170
171 if (transparent_hugepage_flags &
172 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
173 return !!(vma->vm_flags & VM_HUGEPAGE);
174
175 return false;
176 }
177
178 bool transparent_hugepage_active(struct vm_area_struct *vma);
179
180 #define transparent_hugepage_use_zero_page() \
181 (transparent_hugepage_flags & \
182 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
183
184 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
185 unsigned long len, unsigned long pgoff, unsigned long flags);
186
187 void prep_transhuge_page(struct page *page);
188 void free_transhuge_page(struct page *page);
189 bool is_transparent_hugepage(struct page *page);
190
191 bool can_split_huge_page(struct page *page, int *pextra_pins);
192 int split_huge_page_to_list(struct page *page, struct list_head *list);
split_huge_page(struct page * page)193 static inline int split_huge_page(struct page *page)
194 {
195 return split_huge_page_to_list(page, NULL);
196 }
197 void deferred_split_huge_page(struct page *page);
198
199 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
200 unsigned long address, bool freeze, struct page *page);
201
202 #define split_huge_pmd(__vma, __pmd, __address) \
203 do { \
204 pmd_t *____pmd = (__pmd); \
205 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \
206 || pmd_devmap(*____pmd)) \
207 __split_huge_pmd(__vma, __pmd, __address, \
208 false, NULL); \
209 } while (0)
210
211
212 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
213 bool freeze, struct page *page);
214
215 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
216 unsigned long address);
217
218 #define split_huge_pud(__vma, __pud, __address) \
219 do { \
220 pud_t *____pud = (__pud); \
221 if (pud_trans_huge(*____pud) \
222 || pud_devmap(*____pud)) \
223 __split_huge_pud(__vma, __pud, __address); \
224 } while (0)
225
226 int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags,
227 int advice);
228 void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
229 unsigned long end, long adjust_next);
230 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
231 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
232
is_swap_pmd(pmd_t pmd)233 static inline int is_swap_pmd(pmd_t pmd)
234 {
235 return !pmd_none(pmd) && !pmd_present(pmd);
236 }
237
238 /* mmap_lock must be held on entry */
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)239 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
240 struct vm_area_struct *vma)
241 {
242 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
243 return __pmd_trans_huge_lock(pmd, vma);
244 else
245 return NULL;
246 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)247 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
248 struct vm_area_struct *vma)
249 {
250 if (pud_trans_huge(*pud) || pud_devmap(*pud))
251 return __pud_trans_huge_lock(pud, vma);
252 else
253 return NULL;
254 }
255
256 /**
257 * thp_head - Head page of a transparent huge page.
258 * @page: Any page (tail, head or regular) found in the page cache.
259 */
thp_head(struct page * page)260 static inline struct page *thp_head(struct page *page)
261 {
262 return compound_head(page);
263 }
264
265 /**
266 * thp_order - Order of a transparent huge page.
267 * @page: Head page of a transparent huge page.
268 */
thp_order(struct page * page)269 static inline unsigned int thp_order(struct page *page)
270 {
271 VM_BUG_ON_PGFLAGS(PageTail(page), page);
272 if (PageHead(page))
273 return HPAGE_PMD_ORDER;
274 return 0;
275 }
276
277 /**
278 * thp_nr_pages - The number of regular pages in this huge page.
279 * @page: The head page of a huge page.
280 */
thp_nr_pages(struct page * page)281 static inline int thp_nr_pages(struct page *page)
282 {
283 VM_BUG_ON_PGFLAGS(PageTail(page), page);
284 if (PageHead(page))
285 return HPAGE_PMD_NR;
286 return 1;
287 }
288
289 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
290 pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
291 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
292 pud_t *pud, int flags, struct dev_pagemap **pgmap);
293
294 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t orig_pmd);
295
296 extern struct page *huge_zero_page;
297 extern unsigned long huge_zero_pfn;
298
is_huge_zero_page(struct page * page)299 static inline bool is_huge_zero_page(struct page *page)
300 {
301 return READ_ONCE(huge_zero_page) == page;
302 }
303
is_huge_zero_pmd(pmd_t pmd)304 static inline bool is_huge_zero_pmd(pmd_t pmd)
305 {
306 return READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd) && pmd_present(pmd);
307 }
308
is_huge_zero_pud(pud_t pud)309 static inline bool is_huge_zero_pud(pud_t pud)
310 {
311 return false;
312 }
313
314 struct page *mm_get_huge_zero_page(struct mm_struct *mm);
315 void mm_put_huge_zero_page(struct mm_struct *mm);
316
317 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
318
thp_migration_supported(void)319 static inline bool thp_migration_supported(void)
320 {
321 return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
322 }
323
page_deferred_list(struct page * page)324 static inline struct list_head *page_deferred_list(struct page *page)
325 {
326 /*
327 * Global or memcg deferred list in the second tail pages is
328 * occupied by compound_head.
329 */
330 return &page[2].deferred_list;
331 }
332
333 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
334 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
335 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
336 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
337
338 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
339 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
340 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
341
thp_head(struct page * page)342 static inline struct page *thp_head(struct page *page)
343 {
344 VM_BUG_ON_PGFLAGS(PageTail(page), page);
345 return page;
346 }
347
thp_order(struct page * page)348 static inline unsigned int thp_order(struct page *page)
349 {
350 VM_BUG_ON_PGFLAGS(PageTail(page), page);
351 return 0;
352 }
353
thp_nr_pages(struct page * page)354 static inline int thp_nr_pages(struct page *page)
355 {
356 VM_BUG_ON_PGFLAGS(PageTail(page), page);
357 return 1;
358 }
359
__transparent_hugepage_enabled(struct vm_area_struct * vma)360 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
361 {
362 return false;
363 }
364
transparent_hugepage_active(struct vm_area_struct * vma)365 static inline bool transparent_hugepage_active(struct vm_area_struct *vma)
366 {
367 return false;
368 }
369
transhuge_vma_suitable(struct vm_area_struct * vma,unsigned long haddr)370 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
371 unsigned long haddr)
372 {
373 return false;
374 }
375
transhuge_vma_enabled(struct vm_area_struct * vma,unsigned long vm_flags)376 static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
377 unsigned long vm_flags)
378 {
379 return false;
380 }
381
prep_transhuge_page(struct page * page)382 static inline void prep_transhuge_page(struct page *page) {}
383
is_transparent_hugepage(struct page * page)384 static inline bool is_transparent_hugepage(struct page *page)
385 {
386 return false;
387 }
388
389 #define transparent_hugepage_flags 0UL
390
391 #define thp_get_unmapped_area NULL
392
393 static inline bool
can_split_huge_page(struct page * page,int * pextra_pins)394 can_split_huge_page(struct page *page, int *pextra_pins)
395 {
396 BUILD_BUG();
397 return false;
398 }
399 static inline int
split_huge_page_to_list(struct page * page,struct list_head * list)400 split_huge_page_to_list(struct page *page, struct list_head *list)
401 {
402 return 0;
403 }
split_huge_page(struct page * page)404 static inline int split_huge_page(struct page *page)
405 {
406 return 0;
407 }
deferred_split_huge_page(struct page * page)408 static inline void deferred_split_huge_page(struct page *page) {}
409 #define split_huge_pmd(__vma, __pmd, __address) \
410 do { } while (0)
411
__split_huge_pmd(struct vm_area_struct * vma,pmd_t * pmd,unsigned long address,bool freeze,struct page * page)412 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
413 unsigned long address, bool freeze, struct page *page) {}
split_huge_pmd_address(struct vm_area_struct * vma,unsigned long address,bool freeze,struct page * page)414 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
415 unsigned long address, bool freeze, struct page *page) {}
416
417 #define split_huge_pud(__vma, __pmd, __address) \
418 do { } while (0)
419
hugepage_madvise(struct vm_area_struct * vma,unsigned long * vm_flags,int advice)420 static inline int hugepage_madvise(struct vm_area_struct *vma,
421 unsigned long *vm_flags, int advice)
422 {
423 BUG();
424 return 0;
425 }
vma_adjust_trans_huge(struct vm_area_struct * vma,unsigned long start,unsigned long end,long adjust_next)426 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
427 unsigned long start,
428 unsigned long end,
429 long adjust_next)
430 {
431 }
is_swap_pmd(pmd_t pmd)432 static inline int is_swap_pmd(pmd_t pmd)
433 {
434 return 0;
435 }
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)436 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
437 struct vm_area_struct *vma)
438 {
439 return NULL;
440 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)441 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
442 struct vm_area_struct *vma)
443 {
444 return NULL;
445 }
446
do_huge_pmd_numa_page(struct vm_fault * vmf,pmd_t orig_pmd)447 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf,
448 pmd_t orig_pmd)
449 {
450 return 0;
451 }
452
is_huge_zero_page(struct page * page)453 static inline bool is_huge_zero_page(struct page *page)
454 {
455 return false;
456 }
457
is_huge_zero_pmd(pmd_t pmd)458 static inline bool is_huge_zero_pmd(pmd_t pmd)
459 {
460 return false;
461 }
462
is_huge_zero_pud(pud_t pud)463 static inline bool is_huge_zero_pud(pud_t pud)
464 {
465 return false;
466 }
467
mm_put_huge_zero_page(struct mm_struct * mm)468 static inline void mm_put_huge_zero_page(struct mm_struct *mm)
469 {
470 return;
471 }
472
follow_devmap_pmd(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmd,int flags,struct dev_pagemap ** pgmap)473 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
474 unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
475 {
476 return NULL;
477 }
478
follow_devmap_pud(struct vm_area_struct * vma,unsigned long addr,pud_t * pud,int flags,struct dev_pagemap ** pgmap)479 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
480 unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap)
481 {
482 return NULL;
483 }
484
thp_migration_supported(void)485 static inline bool thp_migration_supported(void)
486 {
487 return false;
488 }
489 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
490
491 /**
492 * thp_size - Size of a transparent huge page.
493 * @page: Head page of a transparent huge page.
494 *
495 * Return: Number of bytes in this page.
496 */
thp_size(struct page * page)497 static inline unsigned long thp_size(struct page *page)
498 {
499 return PAGE_SIZE << thp_order(page);
500 }
501
502 #endif /* _LINUX_HUGE_MM_H */
503