xref: /linux/include/linux/huge_mm.h (revision 7203ca412fc8e8a0588e9adc0f777d3163f8dff3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGE_MM_H
3 #define _LINUX_HUGE_MM_H
4 
5 #include <linux/mm_types.h>
6 
7 #include <linux/fs.h> /* only for vma_is_dax() */
8 #include <linux/kobject.h>
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 bool 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
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);
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, unsigned long pfn,
41 			      bool write);
42 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, unsigned long pfn,
43 			      bool write);
44 vm_fault_t vmf_insert_folio_pmd(struct vm_fault *vmf, struct folio *folio,
45 				bool write);
46 vm_fault_t vmf_insert_folio_pud(struct vm_fault *vmf, struct folio *folio,
47 				bool write);
48 
49 enum transparent_hugepage_flag {
50 	TRANSPARENT_HUGEPAGE_UNSUPPORTED,
51 	TRANSPARENT_HUGEPAGE_FLAG,
52 	TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
53 	TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
54 	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
55 	TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
56 	TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
57 	TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
58 	TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
59 };
60 
61 struct kobject;
62 struct kobj_attribute;
63 
64 ssize_t single_hugepage_flag_store(struct kobject *kobj,
65 				   struct kobj_attribute *attr,
66 				   const char *buf, size_t count,
67 				   enum transparent_hugepage_flag flag);
68 ssize_t single_hugepage_flag_show(struct kobject *kobj,
69 				  struct kobj_attribute *attr, char *buf,
70 				  enum transparent_hugepage_flag flag);
71 extern struct kobj_attribute shmem_enabled_attr;
72 extern struct kobj_attribute thpsize_shmem_enabled_attr;
73 
74 /*
75  * Mask of all large folio orders supported for anonymous THP; all orders up to
76  * and including PMD_ORDER, except order-0 (which is not "huge") and order-1
77  * (which is a limitation of the THP implementation).
78  */
79 #define THP_ORDERS_ALL_ANON	((BIT(PMD_ORDER + 1) - 1) & ~(BIT(0) | BIT(1)))
80 
81 /*
82  * Mask of all large folio orders supported for file THP. Folios in a DAX
83  * file is never split and the MAX_PAGECACHE_ORDER limit does not apply to
84  * it.  Same to PFNMAPs where there's neither page* nor pagecache.
85  */
86 #define THP_ORDERS_ALL_SPECIAL		\
87 	(BIT(PMD_ORDER) | BIT(PUD_ORDER))
88 #define THP_ORDERS_ALL_FILE_DEFAULT	\
89 	((BIT(MAX_PAGECACHE_ORDER + 1) - 1) & ~BIT(0))
90 
91 /*
92  * Mask of all large folio orders supported for THP.
93  */
94 #define THP_ORDERS_ALL	\
95 	(THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_SPECIAL | THP_ORDERS_ALL_FILE_DEFAULT)
96 
97 enum tva_type {
98 	TVA_SMAPS,		/* Exposing "THPeligible:" in smaps. */
99 	TVA_PAGEFAULT,		/* Serving a page fault. */
100 	TVA_KHUGEPAGED,		/* Khugepaged collapse. */
101 	TVA_FORCED_COLLAPSE,	/* Forced collapse (e.g. MADV_COLLAPSE). */
102 };
103 
104 #define thp_vma_allowable_order(vma, vm_flags, type, order) \
105 	(!!thp_vma_allowable_orders(vma, vm_flags, type, BIT(order)))
106 
107 #define split_folio(f) split_folio_to_list(f, NULL)
108 
109 #ifdef CONFIG_PGTABLE_HAS_HUGE_LEAVES
110 #define HPAGE_PMD_SHIFT PMD_SHIFT
111 #define HPAGE_PUD_SHIFT PUD_SHIFT
112 #else
113 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
114 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
115 #endif
116 
117 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
118 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
119 #define HPAGE_PMD_MASK	(~(HPAGE_PMD_SIZE - 1))
120 #define HPAGE_PMD_SIZE	((1UL) << HPAGE_PMD_SHIFT)
121 
122 #define HPAGE_PUD_ORDER (HPAGE_PUD_SHIFT-PAGE_SHIFT)
123 #define HPAGE_PUD_NR (1<<HPAGE_PUD_ORDER)
124 #define HPAGE_PUD_MASK	(~(HPAGE_PUD_SIZE - 1))
125 #define HPAGE_PUD_SIZE	((1UL) << HPAGE_PUD_SHIFT)
126 
127 enum mthp_stat_item {
128 	MTHP_STAT_ANON_FAULT_ALLOC,
129 	MTHP_STAT_ANON_FAULT_FALLBACK,
130 	MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE,
131 	MTHP_STAT_ZSWPOUT,
132 	MTHP_STAT_SWPIN,
133 	MTHP_STAT_SWPIN_FALLBACK,
134 	MTHP_STAT_SWPIN_FALLBACK_CHARGE,
135 	MTHP_STAT_SWPOUT,
136 	MTHP_STAT_SWPOUT_FALLBACK,
137 	MTHP_STAT_SHMEM_ALLOC,
138 	MTHP_STAT_SHMEM_FALLBACK,
139 	MTHP_STAT_SHMEM_FALLBACK_CHARGE,
140 	MTHP_STAT_SPLIT,
141 	MTHP_STAT_SPLIT_FAILED,
142 	MTHP_STAT_SPLIT_DEFERRED,
143 	MTHP_STAT_NR_ANON,
144 	MTHP_STAT_NR_ANON_PARTIALLY_MAPPED,
145 	__MTHP_STAT_COUNT
146 };
147 
148 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
149 struct mthp_stat {
150 	unsigned long stats[ilog2(MAX_PTRS_PER_PTE) + 1][__MTHP_STAT_COUNT];
151 };
152 
153 DECLARE_PER_CPU(struct mthp_stat, mthp_stats);
154 
mod_mthp_stat(int order,enum mthp_stat_item item,int delta)155 static inline void mod_mthp_stat(int order, enum mthp_stat_item item, int delta)
156 {
157 	if (order <= 0 || order > PMD_ORDER)
158 		return;
159 
160 	this_cpu_add(mthp_stats.stats[order][item], delta);
161 }
162 
count_mthp_stat(int order,enum mthp_stat_item item)163 static inline void count_mthp_stat(int order, enum mthp_stat_item item)
164 {
165 	mod_mthp_stat(order, item, 1);
166 }
167 
168 #else
mod_mthp_stat(int order,enum mthp_stat_item item,int delta)169 static inline void mod_mthp_stat(int order, enum mthp_stat_item item, int delta)
170 {
171 }
172 
count_mthp_stat(int order,enum mthp_stat_item item)173 static inline void count_mthp_stat(int order, enum mthp_stat_item item)
174 {
175 }
176 #endif
177 
178 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
179 
180 extern unsigned long transparent_hugepage_flags;
181 extern unsigned long huge_anon_orders_always;
182 extern unsigned long huge_anon_orders_madvise;
183 extern unsigned long huge_anon_orders_inherit;
184 
hugepage_global_enabled(void)185 static inline bool hugepage_global_enabled(void)
186 {
187 	return transparent_hugepage_flags &
188 			((1<<TRANSPARENT_HUGEPAGE_FLAG) |
189 			(1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG));
190 }
191 
hugepage_global_always(void)192 static inline bool hugepage_global_always(void)
193 {
194 	return transparent_hugepage_flags &
195 			(1<<TRANSPARENT_HUGEPAGE_FLAG);
196 }
197 
highest_order(unsigned long orders)198 static inline int highest_order(unsigned long orders)
199 {
200 	return fls_long(orders) - 1;
201 }
202 
next_order(unsigned long * orders,int prev)203 static inline int next_order(unsigned long *orders, int prev)
204 {
205 	*orders &= ~BIT(prev);
206 	return highest_order(*orders);
207 }
208 
209 /*
210  * Do the below checks:
211  *   - For file vma, check if the linear page offset of vma is
212  *     order-aligned within the file.  The hugepage is
213  *     guaranteed to be order-aligned within the file, but we must
214  *     check that the order-aligned addresses in the VMA map to
215  *     order-aligned offsets within the file, else the hugepage will
216  *     not be mappable.
217  *   - For all vmas, check if the haddr is in an aligned hugepage
218  *     area.
219  */
thp_vma_suitable_order(struct vm_area_struct * vma,unsigned long addr,int order)220 static inline bool thp_vma_suitable_order(struct vm_area_struct *vma,
221 		unsigned long addr, int order)
222 {
223 	unsigned long hpage_size = PAGE_SIZE << order;
224 	unsigned long haddr;
225 
226 	/* Don't have to check pgoff for anonymous vma */
227 	if (!vma_is_anonymous(vma)) {
228 		if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
229 				hpage_size >> PAGE_SHIFT))
230 			return false;
231 	}
232 
233 	haddr = ALIGN_DOWN(addr, hpage_size);
234 
235 	if (haddr < vma->vm_start || haddr + hpage_size > vma->vm_end)
236 		return false;
237 	return true;
238 }
239 
240 /*
241  * Filter the bitfield of input orders to the ones suitable for use in the vma.
242  * See thp_vma_suitable_order().
243  * All orders that pass the checks are returned as a bitfield.
244  */
thp_vma_suitable_orders(struct vm_area_struct * vma,unsigned long addr,unsigned long orders)245 static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
246 		unsigned long addr, unsigned long orders)
247 {
248 	int order;
249 
250 	/*
251 	 * Iterate over orders, highest to lowest, removing orders that don't
252 	 * meet alignment requirements from the set. Exit loop at first order
253 	 * that meets requirements, since all lower orders must also meet
254 	 * requirements.
255 	 */
256 
257 	order = highest_order(orders);
258 
259 	while (orders) {
260 		if (thp_vma_suitable_order(vma, addr, order))
261 			break;
262 		order = next_order(&orders, order);
263 	}
264 
265 	return orders;
266 }
267 
268 unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,
269 					 vm_flags_t vm_flags,
270 					 enum tva_type type,
271 					 unsigned long orders);
272 
273 /**
274  * thp_vma_allowable_orders - determine hugepage orders that are allowed for vma
275  * @vma:  the vm area to check
276  * @vm_flags: use these vm_flags instead of vma->vm_flags
277  * @type: TVA type
278  * @orders: bitfield of all orders to consider
279  *
280  * Calculates the intersection of the requested hugepage orders and the allowed
281  * hugepage orders for the provided vma. Permitted orders are encoded as a set
282  * bit at the corresponding bit position (bit-2 corresponds to order-2, bit-3
283  * corresponds to order-3, etc). Order-0 is never considered a hugepage order.
284  *
285  * Return: bitfield of orders allowed for hugepage in the vma. 0 if no hugepage
286  * orders are allowed.
287  */
288 static inline
thp_vma_allowable_orders(struct vm_area_struct * vma,vm_flags_t vm_flags,enum tva_type type,unsigned long orders)289 unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
290 				       vm_flags_t vm_flags,
291 				       enum tva_type type,
292 				       unsigned long orders)
293 {
294 	/*
295 	 * Optimization to check if required orders are enabled early. Only
296 	 * forced collapse ignores sysfs configs.
297 	 */
298 	if (type != TVA_FORCED_COLLAPSE && vma_is_anonymous(vma)) {
299 		unsigned long mask = READ_ONCE(huge_anon_orders_always);
300 
301 		if (vm_flags & VM_HUGEPAGE)
302 			mask |= READ_ONCE(huge_anon_orders_madvise);
303 		if (hugepage_global_always() ||
304 		    ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled()))
305 			mask |= READ_ONCE(huge_anon_orders_inherit);
306 
307 		orders &= mask;
308 		if (!orders)
309 			return 0;
310 	}
311 
312 	return __thp_vma_allowable_orders(vma, vm_flags, type, orders);
313 }
314 
315 struct thpsize {
316 	struct kobject kobj;
317 	struct list_head node;
318 	int order;
319 };
320 
321 #define to_thpsize(kobj) container_of(kobj, struct thpsize, kobj)
322 
323 #define transparent_hugepage_use_zero_page()				\
324 	(transparent_hugepage_flags &					\
325 	 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
326 
327 /*
328  * Check whether THPs are explicitly disabled for this VMA, for example,
329  * through madvise or prctl.
330  */
vma_thp_disabled(struct vm_area_struct * vma,vm_flags_t vm_flags,bool forced_collapse)331 static inline bool vma_thp_disabled(struct vm_area_struct *vma,
332 		vm_flags_t vm_flags, bool forced_collapse)
333 {
334 	/* Are THPs disabled for this VMA? */
335 	if (vm_flags & VM_NOHUGEPAGE)
336 		return true;
337 	/* Are THPs disabled for all VMAs in the whole process? */
338 	if (mm_flags_test(MMF_DISABLE_THP_COMPLETELY, vma->vm_mm))
339 		return true;
340 	/*
341 	 * Are THPs disabled only for VMAs where we didn't get an explicit
342 	 * advise to use them?
343 	 */
344 	if (vm_flags & VM_HUGEPAGE)
345 		return false;
346 	/*
347 	 * Forcing a collapse (e.g., madv_collapse), is a clear advice to
348 	 * use THPs.
349 	 */
350 	if (forced_collapse)
351 		return false;
352 	return mm_flags_test(MMF_DISABLE_THP_EXCEPT_ADVISED, vma->vm_mm);
353 }
354 
thp_disabled_by_hw(void)355 static inline bool thp_disabled_by_hw(void)
356 {
357 	/* If the hardware/firmware marked hugepage support disabled. */
358 	return transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED);
359 }
360 
361 unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
362 		unsigned long len, unsigned long pgoff, unsigned long flags);
363 unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
364 		unsigned long len, unsigned long pgoff, unsigned long flags,
365 		vm_flags_t vm_flags);
366 
367 enum split_type {
368 	SPLIT_TYPE_UNIFORM,
369 	SPLIT_TYPE_NON_UNIFORM,
370 };
371 
372 bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins);
373 int __split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
374 		unsigned int new_order);
375 int folio_split_unmapped(struct folio *folio, unsigned int new_order);
376 int min_order_for_split(struct folio *folio);
377 int split_folio_to_list(struct folio *folio, struct list_head *list);
378 bool folio_split_supported(struct folio *folio, unsigned int new_order,
379 		enum split_type split_type, bool warns);
380 int folio_split(struct folio *folio, unsigned int new_order, struct page *page,
381 		struct list_head *list);
382 
split_huge_page_to_list_to_order(struct page * page,struct list_head * list,unsigned int new_order)383 static inline int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
384 		unsigned int new_order)
385 {
386 	return __split_huge_page_to_list_to_order(page, list, new_order);
387 }
split_huge_page_to_order(struct page * page,unsigned int new_order)388 static inline int split_huge_page_to_order(struct page *page, unsigned int new_order)
389 {
390 	return split_huge_page_to_list_to_order(page, NULL, new_order);
391 }
392 
393 /**
394  * try_folio_split_to_order() - try to split a @folio at @page to @new_order
395  * using non uniform split.
396  * @folio: folio to be split
397  * @page: split to @new_order at the given page
398  * @new_order: the target split order
399  *
400  * Try to split a @folio at @page using non uniform split to @new_order, if
401  * non uniform split is not supported, fall back to uniform split. After-split
402  * folios are put back to LRU list. Use min_order_for_split() to get the lower
403  * bound of @new_order.
404  *
405  * Return: 0 - split is successful, otherwise split failed.
406  */
try_folio_split_to_order(struct folio * folio,struct page * page,unsigned int new_order)407 static inline int try_folio_split_to_order(struct folio *folio,
408 		struct page *page, unsigned int new_order)
409 {
410 	if (!folio_split_supported(folio, new_order, SPLIT_TYPE_NON_UNIFORM, /* warns= */ false))
411 		return split_huge_page_to_order(&folio->page, new_order);
412 	return folio_split(folio, new_order, page, NULL);
413 }
split_huge_page(struct page * page)414 static inline int split_huge_page(struct page *page)
415 {
416 	return split_huge_page_to_list_to_order(page, NULL, 0);
417 }
418 void deferred_split_folio(struct folio *folio, bool partially_mapped);
419 #ifdef CONFIG_MEMCG
420 void reparent_deferred_split_queue(struct mem_cgroup *memcg);
421 #endif
422 
423 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
424 		unsigned long address, bool freeze);
425 
426 /**
427  * pmd_is_huge() - Is this PMD either a huge PMD entry or a software leaf entry?
428  * @pmd: The PMD to check.
429  *
430  * A huge PMD entry is a non-empty entry which is present and marked huge or a
431  * software leaf entry. This check be performed without the appropriate locks
432  * held, in which case the condition should be rechecked after they are
433  * acquired.
434  *
435  * Returns: true if this PMD is huge, false otherwise.
436  */
pmd_is_huge(pmd_t pmd)437 static inline bool pmd_is_huge(pmd_t pmd)
438 {
439 	if (pmd_present(pmd)) {
440 		return pmd_trans_huge(pmd);
441 	} else if (!pmd_none(pmd)) {
442 		/*
443 		 * Non-present PMDs must be valid huge non-present entries. We
444 		 * cannot assert that here due to header dependency issues.
445 		 */
446 		return true;
447 	}
448 
449 	return false;
450 }
451 
452 #define split_huge_pmd(__vma, __pmd, __address)				\
453 	do {								\
454 		pmd_t *____pmd = (__pmd);				\
455 		if (pmd_is_huge(*____pmd))				\
456 			__split_huge_pmd(__vma, __pmd, __address,	\
457 					 false);			\
458 	}  while (0)
459 
460 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
461 		bool freeze);
462 
463 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
464 		unsigned long address);
465 
466 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
467 int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
468 		    pud_t *pudp, unsigned long addr, pgprot_t newprot,
469 		    unsigned long cp_flags);
470 #else
471 static inline int
change_huge_pud(struct mmu_gather * tlb,struct vm_area_struct * vma,pud_t * pudp,unsigned long addr,pgprot_t newprot,unsigned long cp_flags)472 change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
473 		pud_t *pudp, unsigned long addr, pgprot_t newprot,
474 		unsigned long cp_flags) { return 0; }
475 #endif
476 
477 #define split_huge_pud(__vma, __pud, __address)				\
478 	do {								\
479 		pud_t *____pud = (__pud);				\
480 		if (pud_trans_huge(*____pud))				\
481 			__split_huge_pud(__vma, __pud, __address);	\
482 	}  while (0)
483 
484 int hugepage_madvise(struct vm_area_struct *vma, vm_flags_t *vm_flags,
485 		     int advice);
486 int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
487 		     unsigned long end, bool *lock_dropped);
488 void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
489 			   unsigned long end, struct vm_area_struct *next);
490 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
491 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
492 
493 /* mmap_lock must be held on entry */
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)494 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
495 		struct vm_area_struct *vma)
496 {
497 	if (pmd_is_huge(*pmd))
498 		return __pmd_trans_huge_lock(pmd, vma);
499 
500 	return NULL;
501 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)502 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
503 		struct vm_area_struct *vma)
504 {
505 	if (pud_trans_huge(*pud))
506 		return __pud_trans_huge_lock(pud, vma);
507 	else
508 		return NULL;
509 }
510 
511 /**
512  * folio_test_pmd_mappable - Can we map this folio with a PMD?
513  * @folio: The folio to test
514  *
515  * Return: true - @folio can be mapped, false - @folio cannot be mapped.
516  */
folio_test_pmd_mappable(struct folio * folio)517 static inline bool folio_test_pmd_mappable(struct folio *folio)
518 {
519 	return folio_order(folio) >= HPAGE_PMD_ORDER;
520 }
521 
522 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
523 
524 vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf);
525 
526 extern struct folio *huge_zero_folio;
527 extern unsigned long huge_zero_pfn;
528 
is_huge_zero_folio(const struct folio * folio)529 static inline bool is_huge_zero_folio(const struct folio *folio)
530 {
531 	VM_WARN_ON_ONCE(!folio);
532 
533 	return READ_ONCE(huge_zero_folio) == folio;
534 }
535 
is_huge_zero_pfn(unsigned long pfn)536 static inline bool is_huge_zero_pfn(unsigned long pfn)
537 {
538 	return READ_ONCE(huge_zero_pfn) == (pfn & ~(HPAGE_PMD_NR - 1));
539 }
540 
is_huge_zero_pmd(pmd_t pmd)541 static inline bool is_huge_zero_pmd(pmd_t pmd)
542 {
543 	return pmd_present(pmd) && is_huge_zero_pfn(pmd_pfn(pmd));
544 }
545 
546 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
547 void mm_put_huge_zero_folio(struct mm_struct *mm);
548 
get_persistent_huge_zero_folio(void)549 static inline struct folio *get_persistent_huge_zero_folio(void)
550 {
551 	if (!IS_ENABLED(CONFIG_PERSISTENT_HUGE_ZERO_FOLIO))
552 		return NULL;
553 
554 	if (unlikely(!huge_zero_folio))
555 		return NULL;
556 
557 	return huge_zero_folio;
558 }
559 
thp_migration_supported(void)560 static inline bool thp_migration_supported(void)
561 {
562 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
563 }
564 
565 void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
566 			   pmd_t *pmd, bool freeze);
567 bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
568 			   pmd_t *pmdp, struct folio *folio);
569 void map_anon_folio_pmd_nopf(struct folio *folio, pmd_t *pmd,
570 		struct vm_area_struct *vma, unsigned long haddr);
571 
572 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
573 
folio_test_pmd_mappable(struct folio * folio)574 static inline bool folio_test_pmd_mappable(struct folio *folio)
575 {
576 	return false;
577 }
578 
thp_vma_suitable_order(struct vm_area_struct * vma,unsigned long addr,int order)579 static inline bool thp_vma_suitable_order(struct vm_area_struct *vma,
580 		unsigned long addr, int order)
581 {
582 	return false;
583 }
584 
thp_vma_suitable_orders(struct vm_area_struct * vma,unsigned long addr,unsigned long orders)585 static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
586 		unsigned long addr, unsigned long orders)
587 {
588 	return 0;
589 }
590 
thp_vma_allowable_orders(struct vm_area_struct * vma,vm_flags_t vm_flags,enum tva_type type,unsigned long orders)591 static inline unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
592 					vm_flags_t vm_flags,
593 					enum tva_type type,
594 					unsigned long orders)
595 {
596 	return 0;
597 }
598 
599 #define transparent_hugepage_flags 0UL
600 
601 #define thp_get_unmapped_area	NULL
602 
603 static inline unsigned long
thp_get_unmapped_area_vmflags(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags)604 thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
605 			      unsigned long len, unsigned long pgoff,
606 			      unsigned long flags, vm_flags_t vm_flags)
607 {
608 	return 0;
609 }
610 
611 static inline bool
can_split_folio(struct folio * folio,int caller_pins,int * pextra_pins)612 can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins)
613 {
614 	return false;
615 }
616 static inline int
split_huge_page_to_list_to_order(struct page * page,struct list_head * list,unsigned int new_order)617 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
618 		unsigned int new_order)
619 {
620 	VM_WARN_ON_ONCE_PAGE(1, page);
621 	return -EINVAL;
622 }
split_huge_page_to_order(struct page * page,unsigned int new_order)623 static inline int split_huge_page_to_order(struct page *page, unsigned int new_order)
624 {
625 	VM_WARN_ON_ONCE_PAGE(1, page);
626 	return -EINVAL;
627 }
split_huge_page(struct page * page)628 static inline int split_huge_page(struct page *page)
629 {
630 	VM_WARN_ON_ONCE_PAGE(1, page);
631 	return -EINVAL;
632 }
633 
min_order_for_split(struct folio * folio)634 static inline int min_order_for_split(struct folio *folio)
635 {
636 	VM_WARN_ON_ONCE_FOLIO(1, folio);
637 	return -EINVAL;
638 }
639 
split_folio_to_list(struct folio * folio,struct list_head * list)640 static inline int split_folio_to_list(struct folio *folio, struct list_head *list)
641 {
642 	VM_WARN_ON_ONCE_FOLIO(1, folio);
643 	return -EINVAL;
644 }
645 
try_folio_split_to_order(struct folio * folio,struct page * page,unsigned int new_order)646 static inline int try_folio_split_to_order(struct folio *folio,
647 		struct page *page, unsigned int new_order)
648 {
649 	VM_WARN_ON_ONCE_FOLIO(1, folio);
650 	return -EINVAL;
651 }
652 
deferred_split_folio(struct folio * folio,bool partially_mapped)653 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
reparent_deferred_split_queue(struct mem_cgroup * memcg)654 static inline void reparent_deferred_split_queue(struct mem_cgroup *memcg) {}
655 #define split_huge_pmd(__vma, __pmd, __address)	\
656 	do { } while (0)
657 
__split_huge_pmd(struct vm_area_struct * vma,pmd_t * pmd,unsigned long address,bool freeze)658 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
659 		unsigned long address, bool freeze) {}
split_huge_pmd_address(struct vm_area_struct * vma,unsigned long address,bool freeze)660 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
661 		unsigned long address, bool freeze) {}
split_huge_pmd_locked(struct vm_area_struct * vma,unsigned long address,pmd_t * pmd,bool freeze)662 static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
663 					 unsigned long address, pmd_t *pmd,
664 					 bool freeze) {}
665 
unmap_huge_pmd_locked(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmdp,struct folio * folio)666 static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma,
667 					 unsigned long addr, pmd_t *pmdp,
668 					 struct folio *folio)
669 {
670 	return false;
671 }
672 
673 #define split_huge_pud(__vma, __pmd, __address)	\
674 	do { } while (0)
675 
hugepage_madvise(struct vm_area_struct * vma,vm_flags_t * vm_flags,int advice)676 static inline int hugepage_madvise(struct vm_area_struct *vma,
677 				   vm_flags_t *vm_flags, int advice)
678 {
679 	return -EINVAL;
680 }
681 
madvise_collapse(struct vm_area_struct * vma,unsigned long start,unsigned long end,bool * lock_dropped)682 static inline int madvise_collapse(struct vm_area_struct *vma,
683 				   unsigned long start,
684 				   unsigned long end, bool *lock_dropped)
685 {
686 	return -EINVAL;
687 }
688 
vma_adjust_trans_huge(struct vm_area_struct * vma,unsigned long start,unsigned long end,struct vm_area_struct * next)689 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
690 					 unsigned long start,
691 					 unsigned long end,
692 					 struct vm_area_struct *next)
693 {
694 }
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)695 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
696 		struct vm_area_struct *vma)
697 {
698 	return NULL;
699 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)700 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
701 		struct vm_area_struct *vma)
702 {
703 	return NULL;
704 }
705 
do_huge_pmd_numa_page(struct vm_fault * vmf)706 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
707 {
708 	return 0;
709 }
710 
do_huge_pmd_device_private(struct vm_fault * vmf)711 static inline vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)
712 {
713 	return 0;
714 }
715 
is_huge_zero_folio(const struct folio * folio)716 static inline bool is_huge_zero_folio(const struct folio *folio)
717 {
718 	return false;
719 }
720 
is_huge_zero_pfn(unsigned long pfn)721 static inline bool is_huge_zero_pfn(unsigned long pfn)
722 {
723 	return false;
724 }
725 
is_huge_zero_pmd(pmd_t pmd)726 static inline bool is_huge_zero_pmd(pmd_t pmd)
727 {
728 	return false;
729 }
730 
mm_put_huge_zero_folio(struct mm_struct * mm)731 static inline void mm_put_huge_zero_folio(struct mm_struct *mm)
732 {
733 	return;
734 }
735 
thp_migration_supported(void)736 static inline bool thp_migration_supported(void)
737 {
738 	return false;
739 }
740 
highest_order(unsigned long orders)741 static inline int highest_order(unsigned long orders)
742 {
743 	return 0;
744 }
745 
next_order(unsigned long * orders,int prev)746 static inline int next_order(unsigned long *orders, int prev)
747 {
748 	return 0;
749 }
750 
__split_huge_pud(struct vm_area_struct * vma,pud_t * pud,unsigned long address)751 static inline void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
752 				    unsigned long address)
753 {
754 }
755 
change_huge_pud(struct mmu_gather * tlb,struct vm_area_struct * vma,pud_t * pudp,unsigned long addr,pgprot_t newprot,unsigned long cp_flags)756 static inline int change_huge_pud(struct mmu_gather *tlb,
757 				  struct vm_area_struct *vma, pud_t *pudp,
758 				  unsigned long addr, pgprot_t newprot,
759 				  unsigned long cp_flags)
760 {
761 	return 0;
762 }
763 
get_persistent_huge_zero_folio(void)764 static inline struct folio *get_persistent_huge_zero_folio(void)
765 {
766 	return NULL;
767 }
768 
pmd_is_huge(pmd_t pmd)769 static inline bool pmd_is_huge(pmd_t pmd)
770 {
771 	return false;
772 }
773 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
774 
split_folio_to_list_to_order(struct folio * folio,struct list_head * list,int new_order)775 static inline int split_folio_to_list_to_order(struct folio *folio,
776 		struct list_head *list, int new_order)
777 {
778 	return split_huge_page_to_list_to_order(&folio->page, list, new_order);
779 }
780 
split_folio_to_order(struct folio * folio,int new_order)781 static inline int split_folio_to_order(struct folio *folio, int new_order)
782 {
783 	return split_folio_to_list_to_order(folio, NULL, new_order);
784 }
785 
786 /**
787  * largest_zero_folio - Get the largest zero size folio available
788  *
789  * This function shall be used when mm_get_huge_zero_folio() cannot be
790  * used as there is no appropriate mm lifetime to tie the huge zero folio
791  * from the caller.
792  *
793  * Deduce the size of the folio with folio_size instead of assuming the
794  * folio size.
795  *
796  * Return: pointer to PMD sized zero folio if CONFIG_PERSISTENT_HUGE_ZERO_FOLIO
797  * is enabled or a single page sized zero folio
798  */
largest_zero_folio(void)799 static inline struct folio *largest_zero_folio(void)
800 {
801 	struct folio *folio = get_persistent_huge_zero_folio();
802 
803 	if (folio)
804 		return folio;
805 
806 	return page_folio(ZERO_PAGE(0));
807 }
808 #endif /* _LINUX_HUGE_MM_H */
809