xref: /linux/include/linux/huge_mm.h (revision 537d196186e0a0ce28e494ca1881885accc35a12)
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 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
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 bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins);
368 int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
369 		unsigned int new_order);
370 int min_order_for_split(struct folio *folio);
371 int split_folio_to_list(struct folio *folio, struct list_head *list);
372 bool uniform_split_supported(struct folio *folio, unsigned int new_order,
373 		bool warns);
374 bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
375 		bool warns);
376 int folio_split(struct folio *folio, unsigned int new_order, struct page *page,
377 		struct list_head *list);
378 /*
379  * try_folio_split_to_order - try to split a @folio at @page to @new_order using
380  * non uniform split.
381  * @folio: folio to be split
382  * @page: split to @new_order at the given page
383  * @new_order: the target split order
384  *
385  * Try to split a @folio at @page using non uniform split to @new_order, if
386  * non uniform split is not supported, fall back to uniform split. After-split
387  * folios are put back to LRU list. Use min_order_for_split() to get the lower
388  * bound of @new_order.
389  *
390  * Return: 0: split is successful, otherwise split failed.
391  */
try_folio_split_to_order(struct folio * folio,struct page * page,unsigned int new_order)392 static inline int try_folio_split_to_order(struct folio *folio,
393 		struct page *page, unsigned int new_order)
394 {
395 	if (!non_uniform_split_supported(folio, new_order, /* warns= */ false))
396 		return split_huge_page_to_list_to_order(&folio->page, NULL,
397 				new_order);
398 	return folio_split(folio, new_order, page, NULL);
399 }
split_huge_page(struct page * page)400 static inline int split_huge_page(struct page *page)
401 {
402 	return split_huge_page_to_list_to_order(page, NULL, 0);
403 }
404 void deferred_split_folio(struct folio *folio, bool partially_mapped);
405 
406 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
407 		unsigned long address, bool freeze);
408 
409 #define split_huge_pmd(__vma, __pmd, __address)				\
410 	do {								\
411 		pmd_t *____pmd = (__pmd);				\
412 		if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd))	\
413 			__split_huge_pmd(__vma, __pmd, __address,	\
414 					 false);			\
415 	}  while (0)
416 
417 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
418 		bool freeze);
419 
420 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
421 		unsigned long address);
422 
423 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
424 int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
425 		    pud_t *pudp, unsigned long addr, pgprot_t newprot,
426 		    unsigned long cp_flags);
427 #else
428 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)429 change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
430 		pud_t *pudp, unsigned long addr, pgprot_t newprot,
431 		unsigned long cp_flags) { return 0; }
432 #endif
433 
434 #define split_huge_pud(__vma, __pud, __address)				\
435 	do {								\
436 		pud_t *____pud = (__pud);				\
437 		if (pud_trans_huge(*____pud))				\
438 			__split_huge_pud(__vma, __pud, __address);	\
439 	}  while (0)
440 
441 int hugepage_madvise(struct vm_area_struct *vma, vm_flags_t *vm_flags,
442 		     int advice);
443 int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
444 		     unsigned long end, bool *lock_dropped);
445 void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
446 			   unsigned long end, struct vm_area_struct *next);
447 spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
448 spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
449 
is_swap_pmd(pmd_t pmd)450 static inline int is_swap_pmd(pmd_t pmd)
451 {
452 	return !pmd_none(pmd) && !pmd_present(pmd);
453 }
454 
455 /* mmap_lock must be held on entry */
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)456 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
457 		struct vm_area_struct *vma)
458 {
459 	if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd))
460 		return __pmd_trans_huge_lock(pmd, vma);
461 	else
462 		return NULL;
463 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)464 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
465 		struct vm_area_struct *vma)
466 {
467 	if (pud_trans_huge(*pud))
468 		return __pud_trans_huge_lock(pud, vma);
469 	else
470 		return NULL;
471 }
472 
473 /**
474  * folio_test_pmd_mappable - Can we map this folio with a PMD?
475  * @folio: The folio to test
476  */
folio_test_pmd_mappable(struct folio * folio)477 static inline bool folio_test_pmd_mappable(struct folio *folio)
478 {
479 	return folio_order(folio) >= HPAGE_PMD_ORDER;
480 }
481 
482 vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
483 
484 extern struct folio *huge_zero_folio;
485 extern unsigned long huge_zero_pfn;
486 
is_huge_zero_folio(const struct folio * folio)487 static inline bool is_huge_zero_folio(const struct folio *folio)
488 {
489 	VM_WARN_ON_ONCE(!folio);
490 
491 	return READ_ONCE(huge_zero_folio) == folio;
492 }
493 
is_huge_zero_pfn(unsigned long pfn)494 static inline bool is_huge_zero_pfn(unsigned long pfn)
495 {
496 	return READ_ONCE(huge_zero_pfn) == (pfn & ~(HPAGE_PMD_NR - 1));
497 }
498 
is_huge_zero_pmd(pmd_t pmd)499 static inline bool is_huge_zero_pmd(pmd_t pmd)
500 {
501 	return pmd_present(pmd) && is_huge_zero_pfn(pmd_pfn(pmd));
502 }
503 
504 struct folio *mm_get_huge_zero_folio(struct mm_struct *mm);
505 void mm_put_huge_zero_folio(struct mm_struct *mm);
506 
get_persistent_huge_zero_folio(void)507 static inline struct folio *get_persistent_huge_zero_folio(void)
508 {
509 	if (!IS_ENABLED(CONFIG_PERSISTENT_HUGE_ZERO_FOLIO))
510 		return NULL;
511 
512 	if (unlikely(!huge_zero_folio))
513 		return NULL;
514 
515 	return huge_zero_folio;
516 }
517 
thp_migration_supported(void)518 static inline bool thp_migration_supported(void)
519 {
520 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
521 }
522 
523 void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
524 			   pmd_t *pmd, bool freeze);
525 bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
526 			   pmd_t *pmdp, struct folio *folio);
527 
528 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
529 
folio_test_pmd_mappable(struct folio * folio)530 static inline bool folio_test_pmd_mappable(struct folio *folio)
531 {
532 	return false;
533 }
534 
thp_vma_suitable_order(struct vm_area_struct * vma,unsigned long addr,int order)535 static inline bool thp_vma_suitable_order(struct vm_area_struct *vma,
536 		unsigned long addr, int order)
537 {
538 	return false;
539 }
540 
thp_vma_suitable_orders(struct vm_area_struct * vma,unsigned long addr,unsigned long orders)541 static inline unsigned long thp_vma_suitable_orders(struct vm_area_struct *vma,
542 		unsigned long addr, unsigned long orders)
543 {
544 	return 0;
545 }
546 
thp_vma_allowable_orders(struct vm_area_struct * vma,vm_flags_t vm_flags,enum tva_type type,unsigned long orders)547 static inline unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
548 					vm_flags_t vm_flags,
549 					enum tva_type type,
550 					unsigned long orders)
551 {
552 	return 0;
553 }
554 
555 #define transparent_hugepage_flags 0UL
556 
557 #define thp_get_unmapped_area	NULL
558 
559 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)560 thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addr,
561 			      unsigned long len, unsigned long pgoff,
562 			      unsigned long flags, vm_flags_t vm_flags)
563 {
564 	return 0;
565 }
566 
567 static inline bool
can_split_folio(struct folio * folio,int caller_pins,int * pextra_pins)568 can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins)
569 {
570 	return false;
571 }
572 static inline int
split_huge_page_to_list_to_order(struct page * page,struct list_head * list,unsigned int new_order)573 split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
574 		unsigned int new_order)
575 {
576 	VM_WARN_ON_ONCE_PAGE(1, page);
577 	return -EINVAL;
578 }
split_huge_page(struct page * page)579 static inline int split_huge_page(struct page *page)
580 {
581 	VM_WARN_ON_ONCE_PAGE(1, page);
582 	return -EINVAL;
583 }
584 
min_order_for_split(struct folio * folio)585 static inline int min_order_for_split(struct folio *folio)
586 {
587 	VM_WARN_ON_ONCE_FOLIO(1, folio);
588 	return -EINVAL;
589 }
590 
split_folio_to_list(struct folio * folio,struct list_head * list)591 static inline int split_folio_to_list(struct folio *folio, struct list_head *list)
592 {
593 	VM_WARN_ON_ONCE_FOLIO(1, folio);
594 	return -EINVAL;
595 }
596 
try_folio_split_to_order(struct folio * folio,struct page * page,unsigned int new_order)597 static inline int try_folio_split_to_order(struct folio *folio,
598 		struct page *page, unsigned int new_order)
599 {
600 	VM_WARN_ON_ONCE_FOLIO(1, folio);
601 	return -EINVAL;
602 }
603 
deferred_split_folio(struct folio * folio,bool partially_mapped)604 static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {}
605 #define split_huge_pmd(__vma, __pmd, __address)	\
606 	do { } while (0)
607 
__split_huge_pmd(struct vm_area_struct * vma,pmd_t * pmd,unsigned long address,bool freeze)608 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
609 		unsigned long address, bool freeze) {}
split_huge_pmd_address(struct vm_area_struct * vma,unsigned long address,bool freeze)610 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
611 		unsigned long address, bool freeze) {}
split_huge_pmd_locked(struct vm_area_struct * vma,unsigned long address,pmd_t * pmd,bool freeze)612 static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
613 					 unsigned long address, pmd_t *pmd,
614 					 bool freeze) {}
615 
unmap_huge_pmd_locked(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmdp,struct folio * folio)616 static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma,
617 					 unsigned long addr, pmd_t *pmdp,
618 					 struct folio *folio)
619 {
620 	return false;
621 }
622 
623 #define split_huge_pud(__vma, __pmd, __address)	\
624 	do { } while (0)
625 
hugepage_madvise(struct vm_area_struct * vma,vm_flags_t * vm_flags,int advice)626 static inline int hugepage_madvise(struct vm_area_struct *vma,
627 				   vm_flags_t *vm_flags, int advice)
628 {
629 	return -EINVAL;
630 }
631 
madvise_collapse(struct vm_area_struct * vma,unsigned long start,unsigned long end,bool * lock_dropped)632 static inline int madvise_collapse(struct vm_area_struct *vma,
633 				   unsigned long start,
634 				   unsigned long end, bool *lock_dropped)
635 {
636 	return -EINVAL;
637 }
638 
vma_adjust_trans_huge(struct vm_area_struct * vma,unsigned long start,unsigned long end,struct vm_area_struct * next)639 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
640 					 unsigned long start,
641 					 unsigned long end,
642 					 struct vm_area_struct *next)
643 {
644 }
is_swap_pmd(pmd_t pmd)645 static inline int is_swap_pmd(pmd_t pmd)
646 {
647 	return 0;
648 }
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)649 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
650 		struct vm_area_struct *vma)
651 {
652 	return NULL;
653 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)654 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
655 		struct vm_area_struct *vma)
656 {
657 	return NULL;
658 }
659 
do_huge_pmd_numa_page(struct vm_fault * vmf)660 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
661 {
662 	return 0;
663 }
664 
is_huge_zero_folio(const struct folio * folio)665 static inline bool is_huge_zero_folio(const struct folio *folio)
666 {
667 	return false;
668 }
669 
is_huge_zero_pfn(unsigned long pfn)670 static inline bool is_huge_zero_pfn(unsigned long pfn)
671 {
672 	return false;
673 }
674 
is_huge_zero_pmd(pmd_t pmd)675 static inline bool is_huge_zero_pmd(pmd_t pmd)
676 {
677 	return false;
678 }
679 
mm_put_huge_zero_folio(struct mm_struct * mm)680 static inline void mm_put_huge_zero_folio(struct mm_struct *mm)
681 {
682 	return;
683 }
684 
follow_devmap_pmd(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmd,int flags,struct dev_pagemap ** pgmap)685 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
686 	unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
687 {
688 	return NULL;
689 }
690 
thp_migration_supported(void)691 static inline bool thp_migration_supported(void)
692 {
693 	return false;
694 }
695 
highest_order(unsigned long orders)696 static inline int highest_order(unsigned long orders)
697 {
698 	return 0;
699 }
700 
next_order(unsigned long * orders,int prev)701 static inline int next_order(unsigned long *orders, int prev)
702 {
703 	return 0;
704 }
705 
__split_huge_pud(struct vm_area_struct * vma,pud_t * pud,unsigned long address)706 static inline void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
707 				    unsigned long address)
708 {
709 }
710 
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)711 static inline int change_huge_pud(struct mmu_gather *tlb,
712 				  struct vm_area_struct *vma, pud_t *pudp,
713 				  unsigned long addr, pgprot_t newprot,
714 				  unsigned long cp_flags)
715 {
716 	return 0;
717 }
718 
get_persistent_huge_zero_folio(void)719 static inline struct folio *get_persistent_huge_zero_folio(void)
720 {
721 	return NULL;
722 }
723 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
724 
split_folio_to_list_to_order(struct folio * folio,struct list_head * list,int new_order)725 static inline int split_folio_to_list_to_order(struct folio *folio,
726 		struct list_head *list, int new_order)
727 {
728 	return split_huge_page_to_list_to_order(&folio->page, list, new_order);
729 }
730 
split_folio_to_order(struct folio * folio,int new_order)731 static inline int split_folio_to_order(struct folio *folio, int new_order)
732 {
733 	return split_folio_to_list_to_order(folio, NULL, new_order);
734 }
735 
736 /**
737  * largest_zero_folio - Get the largest zero size folio available
738  *
739  * This function shall be used when mm_get_huge_zero_folio() cannot be
740  * used as there is no appropriate mm lifetime to tie the huge zero folio
741  * from the caller.
742  *
743  * Deduce the size of the folio with folio_size instead of assuming the
744  * folio size.
745  *
746  * Return: pointer to PMD sized zero folio if CONFIG_PERSISTENT_HUGE_ZERO_FOLIO
747  * is enabled or a single page sized zero folio
748  */
largest_zero_folio(void)749 static inline struct folio *largest_zero_folio(void)
750 {
751 	struct folio *folio = get_persistent_huge_zero_folio();
752 
753 	if (folio)
754 		return folio;
755 
756 	return page_folio(ZERO_PAGE(0));
757 }
758 #endif /* _LINUX_HUGE_MM_H */
759