xref: /linux-6.15/include/linux/huge_mm.h (revision ab0f4ced)
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 bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
29 			   pmd_t *pmd, unsigned long addr, unsigned long next);
30 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd,
31 		 unsigned long addr);
32 int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud,
33 		 unsigned long addr);
34 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
35 		   unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd);
36 int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
37 		    pmd_t *pmd, unsigned long addr, pgprot_t newprot,
38 		    unsigned long cp_flags);
39 
40 vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write);
41 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write);
42 
43 enum transparent_hugepage_flag {
44 	TRANSPARENT_HUGEPAGE_UNSUPPORTED,
45 	TRANSPARENT_HUGEPAGE_FLAG,
46 	TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
47 	TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
48 	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
49 	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
50 	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
51 	TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
52 	TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
53 };
54 
55 struct kobject;
56 struct kobj_attribute;
57 
58 ssize_t single_hugepage_flag_store(struct kobject *kobj,
59 				   struct kobj_attribute *attr,
60 				   const char *buf, size_t count,
61 				   enum transparent_hugepage_flag flag);
62 ssize_t single_hugepage_flag_show(struct kobject *kobj,
63 				  struct kobj_attribute *attr, char *buf,
64 				  enum transparent_hugepage_flag flag);
65 extern struct kobj_attribute shmem_enabled_attr;
66 
67 /*
68  * Mask of all large folio orders supported for anonymous THP; all orders up to
69  * and including PMD_ORDER, except order-0 (which is not "huge") and order-1
70  * (which is a limitation of the THP implementation).
71  */
72 #define THP_ORDERS_ALL_ANON	((BIT(PMD_ORDER + 1) - 1) & ~(BIT(0) | BIT(1)))
73 
74 /*
75  * Mask of all large folio orders supported for file THP.
76  */
77 #define THP_ORDERS_ALL_FILE	(BIT(PMD_ORDER) | BIT(PUD_ORDER))
78 
79 /*
80  * Mask of all large folio orders supported for THP.
81  */
82 #define THP_ORDERS_ALL		(THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_FILE)
83 
84 #define TVA_SMAPS		(1 << 0)	/* Will be used for procfs */
85 #define TVA_IN_PF		(1 << 1)	/* Page fault handler */
86 #define TVA_ENFORCE_SYSFS	(1 << 2)	/* Obey sysfs configuration */
87 
88 #define thp_vma_allowable_order(vma, vm_flags, tva_flags, order) \
89 	(!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order)))
90 
91 #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES
92 #define HPAGE_PMD_SHIFT PMD_SHIFT
93 #define HPAGE_PUD_SHIFT PUD_SHIFT
94 #else
95 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
96 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
97 #endif
98 
99 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
100 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
101 #define HPAGE_PMD_MASK	(~(HPAGE_PMD_SIZE - 1))
102 #define HPAGE_PMD_SIZE	((1UL) << HPAGE_PMD_SHIFT)
103 
104 #define HPAGE_PUD_ORDER (HPAGE_PUD_SHIFT-PAGE_SHIFT)
105 #define HPAGE_PUD_NR (1<<HPAGE_PUD_ORDER)
106 #define HPAGE_PUD_MASK	(~(HPAGE_PUD_SIZE - 1))
107 #define HPAGE_PUD_SIZE	((1UL) << HPAGE_PUD_SHIFT)
108 
109 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
110 
111 extern unsigned long transparent_hugepage_flags;
112 extern unsigned long huge_anon_orders_always;
113 extern unsigned long huge_anon_orders_madvise;
114 extern unsigned long huge_anon_orders_inherit;
115 
116 static inline bool hugepage_global_enabled(void)
117 {
118 	return transparent_hugepage_flags &
119 			((1<<TRANSPARENT_HUGEPAGE_FLAG) |
120 			(1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG));
121 }
122 
123 static inline bool hugepage_global_always(void)
124 {
125 	return transparent_hugepage_flags &
126 			(1<<TRANSPARENT_HUGEPAGE_FLAG);
127 }
128 
129 static inline bool hugepage_flags_enabled(void)
130 {
131 	/*
132 	 * We cover both the anon and the file-backed case here; we must return
133 	 * true if globally enabled, even when all anon sizes are set to never.
134 	 * So we don't need to look at huge_anon_orders_inherit.
135 	 */
136 	return hugepage_global_enabled() ||
137 	       huge_anon_orders_always ||
138 	       huge_anon_orders_madvise;
139 }
140 
141 static inline int highest_order(unsigned long orders)
142 {
143 	return fls_long(orders) - 1;
144 }
145 
146 static inline int next_order(unsigned long *orders, int prev)
147 {
148 	*orders &= ~BIT(prev);
149 	return highest_order(*orders);
150 }
151 
152 /*
153  * Do the below checks:
154  *   - For file vma, check if the linear page offset of vma is
155  *     order-aligned within the file.  The hugepage is
156  *     guaranteed to be order-aligned within the file, but we must
157  *     check that the order-aligned addresses in the VMA map to
158  *     order-aligned offsets within the file, else the hugepage will
159  *     not be mappable.
160  *   - For all vmas, check if the haddr is in an aligned hugepage
161  *     area.
162  */
163 static inline bool thp_vma_suitable_order(struct vm_area_struct *vma,
164 		unsigned long addr, int order)
165 {
166 	unsigned long hpage_size = PAGE_SIZE << order;
167 	unsigned long haddr;
168 
169 	/* Don't have to check pgoff for anonymous vma */
170 	if (!vma_is_anonymous(vma)) {
171 		if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
172 				hpage_size >> PAGE_SHIFT))
173 			return false;
174 	}
175 
176 	haddr = ALIGN_DOWN(addr, hpage_size);
177 
178 	if (haddr < vma->vm_start || haddr + hpage_size > vma->vm_end)
179 		return false;
180 	return true;
181 }
182 
183 /*
184  * Filter the bitfield of input orders to the ones suitable for use in the vma.
185  * See thp_vma_suitable_order().
186  * All orders that pass the checks are returned as a bitfield.
187  */
188 static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
189 		unsigned long addr, unsigned long orders)
190 {
191 	int order;
192 
193 	/*
194 	 * Iterate over orders, highest to lowest, removing orders that don't
195 	 * meet alignment requirements from the set. Exit loop at first order
196 	 * that meets requirements, since all lower orders must also meet
197 	 * requirements.
198 	 */
199 
200 	order = highest_order(orders);
201 
202 	while (orders) {
203 		if (thp_vma_suitable_order(vma, addr, order))
204 			break;
205 		order = next_order(&orders, order);
206 	}
207 
208 	return orders;
209 }
210 
211 static inline bool file_thp_enabled(struct vm_area_struct *vma)
212 {
213 	struct inode *inode;
214 
215 	if (!vma->vm_file)
216 		return false;
217 
218 	inode = vma->vm_file->f_inode;
219 
220 	return (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS)) &&
221 	       !inode_is_open_for_write(inode) && S_ISREG(inode->i_mode);
222 }
223 
224 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
225 					 unsigned long vm_flags,
226 					 unsigned long tva_flags,
227 					 unsigned long orders);
228 
229 /**
230  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
231  * @vma:  the vm area to check
232  * @vm_flags: use these vm_flags instead of vma->vm_flags
233  * @tva_flags: Which TVA flags to honour
234  * @orders: bitfield of all orders to consider
235  *
236  * Calculates the intersection of the requested hugepage orders and the allowed
237  * hugepage orders for the provided vma. Permitted orders are encoded as a set
238  * bit at the corresponding bit position (bit-2 corresponds to order-2, bit-3
239  * corresponds to order-3, etc). Order-0 is never considered a hugepage order.
240  *
241  * Return: bitfield of orders allowed for hugepage in the vma. 0 if no hugepage
242  * orders are allowed.
243  */
244 static inline
245 unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
246 				       unsigned long vm_flags,
247 				       unsigned long tva_flags,
248 				       unsigned long orders)
249 {
250 	/* Optimization to check if required orders are enabled early. */
251 	if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) {
252 		unsigned long mask = READ_ONCE(huge_anon_orders_always);
253 
254 		if (vm_flags & VM_HUGEPAGE)
255 			mask |= READ_ONCE(huge_anon_orders_madvise);
256 		if (hugepage_global_always() ||
257 		    ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
258 			mask |= READ_ONCE(huge_anon_orders_inherit);
259 
260 		orders &= mask;
261 		if (!orders)
262 			return 0;
263 	}
264 
265 	return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders);
266 }
267 
268 enum mthp_stat_item {
269 	MTHP_STAT_ANON_FAULT_ALLOC,
270 	MTHP_STAT_ANON_FAULT_FALLBACK,
271 	MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE,
272 	MTHP_STAT_SWPOUT,
273 	MTHP_STAT_SWPOUT_FALLBACK,
274 	__MTHP_STAT_COUNT
275 };
276 
277 struct mthp_stat {
278 	unsigned long stats[ilog2(MAX_PTRS_PER_PTE) + 1][__MTHP_STAT_COUNT];
279 };
280 
281 #ifdef CONFIG_SYSFS
282 DECLARE_PER_CPU(struct mthp_stat, mthp_stats);
283 
284 static inline void count_mthp_stat(int order, enum mthp_stat_item item)
285 {
286 	if (order <= 0 || order > PMD_ORDER)
287 		return;
288 
289 	this_cpu_inc(mthp_stats.stats[order][item]);
290 }
291 #else
292 static inline void count_mthp_stat(int order, enum mthp_stat_item item)
293 {
294 }
295 #endif
296 
297 #define transparent_hugepage_use_zero_page()				\
298 	(transparent_hugepage_flags &					\
299 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
300 
301 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
302 		unsigned long len, unsigned long pgoff, unsigned long flags);
303 unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
304 		unsigned long len, unsigned long pgoff, unsigned long flags,
305 		vm_flags_t vm_flags);
306 
307 bool can_split_folio(struct folio *folio, int *pextra_pins);
308 int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
309 		unsigned int new_order);
310 static inline int split_huge_page(struct page *page)
311 {
312 	return split_huge_page_to_list_to_order(page, NULL, 0);
313 }
314 void deferred_split_folio(struct folio *folio);
315 
316 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
317 		unsigned long address, bool freeze, struct folio *folio);
318 
319 #define split_huge_pmd(__vma, __pmd, __address)				\
320 	do {								\
321 		pmd_t *____pmd = (__pmd);				\
322 		if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd)	\
323 					|| pmd_devmap(*____pmd))	\
324 			__split_huge_pmd(__vma, __pmd, __address,	\
325 						false, NULL);		\
326 	}  while (0)
327 
328 
329 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
330 		bool freeze, struct folio *folio);
331 
332 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
333 		unsigned long address);
334 
335 #define split_huge_pud(__vma, __pud, __address)				\
336 	do {								\
337 		pud_t *____pud = (__pud);				\
338 		if (pud_trans_huge(*____pud)				\
339 					|| pud_devmap(*____pud))	\
340 			__split_huge_pud(__vma, __pud, __address);	\
341 	}  while (0)
342 
343 int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags,
344 		     int advice);
345 int madvise_collapse(struct vm_area_struct *vma,
346 		     struct vm_area_struct **prev,
347 		     unsigned long start, unsigned long end);
348 void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
349 			   unsigned long end, long adjust_next);
350 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
351 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
352 
353 static inline int is_swap_pmd(pmd_t pmd)
354 {
355 	return !pmd_none(pmd) && !pmd_present(pmd);
356 }
357 
358 /* mmap_lock must be held on entry */
359 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
360 		struct vm_area_struct *vma)
361 {
362 	if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
363 		return __pmd_trans_huge_lock(pmd, vma);
364 	else
365 		return NULL;
366 }
367 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
368 		struct vm_area_struct *vma)
369 {
370 	if (pud_trans_huge(*pud) || pud_devmap(*pud))
371 		return __pud_trans_huge_lock(pud, vma);
372 	else
373 		return NULL;
374 }
375 
376 /**
377  * folio_test_pmd_mappable - Can we map this folio with a PMD?
378  * @folio: The folio to test
379  */
380 static inline bool folio_test_pmd_mappable(struct folio *folio)
381 {
382 	return folio_order(folio) >= HPAGE_PMD_ORDER;
383 }
384 
385 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
386 		pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
387 
388 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
389 
390 extern struct folio *huge_zero_folio;
391 extern unsigned long huge_zero_pfn;
392 
393 static inline bool is_huge_zero_folio(const struct folio *folio)
394 {
395 	return READ_ONCE(huge_zero_folio) == folio;
396 }
397 
398 static inline bool is_huge_zero_pmd(pmd_t pmd)
399 {
400 	return pmd_present(pmd) && READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd);
401 }
402 
403 static inline bool is_huge_zero_pud(pud_t pud)
404 {
405 	return false;
406 }
407 
408 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
409 void mm_put_huge_zero_folio(struct mm_struct *mm);
410 
411 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
412 
413 static inline bool thp_migration_supported(void)
414 {
415 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
416 }
417 
418 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
419 
420 static inline bool folio_test_pmd_mappable(struct folio *folio)
421 {
422 	return false;
423 }
424 
425 static inline bool thp_vma_suitable_order(struct vm_area_struct *vma,
426 		unsigned long addr, int order)
427 {
428 	return false;
429 }
430 
431 static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
432 		unsigned long addr, unsigned long orders)
433 {
434 	return 0;
435 }
436 
437 static inline unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
438 					unsigned long vm_flags,
439 					unsigned long tva_flags,
440 					unsigned long orders)
441 {
442 	return 0;
443 }
444 
445 #define transparent_hugepage_flags 0UL
446 
447 #define thp_get_unmapped_area	NULL
448 
449 static inline unsigned long
450 thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
451 			      unsigned long len, unsigned long pgoff,
452 			      unsigned long flags, vm_flags_t vm_flags)
453 {
454 	return 0;
455 }
456 
457 static inline bool
458 can_split_folio(struct folio *folio, int *pextra_pins)
459 {
460 	return false;
461 }
462 static inline int
463 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
464 		unsigned int new_order)
465 {
466 	return 0;
467 }
468 static inline int split_huge_page(struct page *page)
469 {
470 	return 0;
471 }
472 static inline void deferred_split_folio(struct folio *folio) {}
473 #define split_huge_pmd(__vma, __pmd, __address)	\
474 	do { } while (0)
475 
476 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
477 		unsigned long address, bool freeze, struct folio *folio) {}
478 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
479 		unsigned long address, bool freeze, struct folio *folio) {}
480 
481 #define split_huge_pud(__vma, __pmd, __address)	\
482 	do { } while (0)
483 
484 static inline int hugepage_madvise(struct vm_area_struct *vma,
485 				   unsigned long *vm_flags, int advice)
486 {
487 	return -EINVAL;
488 }
489 
490 static inline int madvise_collapse(struct vm_area_struct *vma,
491 				   struct vm_area_struct **prev,
492 				   unsigned long start, unsigned long end)
493 {
494 	return -EINVAL;
495 }
496 
497 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
498 					 unsigned long start,
499 					 unsigned long end,
500 					 long adjust_next)
501 {
502 }
503 static inline int is_swap_pmd(pmd_t pmd)
504 {
505 	return 0;
506 }
507 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
508 		struct vm_area_struct *vma)
509 {
510 	return NULL;
511 }
512 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
513 		struct vm_area_struct *vma)
514 {
515 	return NULL;
516 }
517 
518 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
519 {
520 	return 0;
521 }
522 
523 static inline bool is_huge_zero_folio(const struct folio *folio)
524 {
525 	return false;
526 }
527 
528 static inline bool is_huge_zero_pmd(pmd_t pmd)
529 {
530 	return false;
531 }
532 
533 static inline bool is_huge_zero_pud(pud_t pud)
534 {
535 	return false;
536 }
537 
538 static inline void mm_put_huge_zero_folio(struct mm_struct *mm)
539 {
540 	return;
541 }
542 
543 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
544 	unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
545 {
546 	return NULL;
547 }
548 
549 static inline bool thp_migration_supported(void)
550 {
551 	return false;
552 }
553 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
554 
555 static inline int split_folio_to_list_to_order(struct folio *folio,
556 		struct list_head *list, int new_order)
557 {
558 	return split_huge_page_to_list_to_order(&folio->page, list, new_order);
559 }
560 
561 static inline int split_folio_to_order(struct folio *folio, int new_order)
562 {
563 	return split_folio_to_list_to_order(folio, NULL, new_order);
564 }
565 
566 #define split_folio_to_list(f, l) split_folio_to_list_to_order(f, l, 0)
567 #define split_folio(f) split_folio_to_order(f, 0)
568 
569 #endif /* _LINUX_HUGE_MM_H */
570