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