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