1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_HUGE_MM_H
3 #define _LINUX_HUGE_MM_H
5 #include <linux/sched/coredump.h>
6 #include <linux/mm_types.h>
8 #include <linux/fs.h> /* only for vma_is_dax() */
10 extern vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf);
11 extern int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
12 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
13 struct vm_area_struct *vma);
14 extern void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd);
15 extern int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
16 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
17 struct vm_area_struct *vma);
19 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
20 extern void 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)
27 extern vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd);
28 extern struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
32 extern bool madvise_free_huge_pmd(struct mmu_gather *tlb,
33 struct vm_area_struct *vma,
34 pmd_t *pmd, unsigned long addr, unsigned long next);
35 extern int zap_huge_pmd(struct mmu_gather *tlb,
36 struct vm_area_struct *vma,
37 pmd_t *pmd, unsigned long addr);
38 extern int zap_huge_pud(struct mmu_gather *tlb,
39 struct vm_area_struct *vma,
40 pud_t *pud, unsigned long addr);
41 extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
42 unsigned long new_addr,
43 pmd_t *old_pmd, pmd_t *new_pmd);
44 extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
45 unsigned long addr, pgprot_t newprot,
46 unsigned long cp_flags);
47 vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
48 pgprot_t pgprot, bool write);
51 * vmf_insert_pfn_pmd - insert a pmd size pfn
52 * @vmf: Structure describing the fault
54 * @pgprot: page protection to use
55 * @write: whether it's a write fault
57 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
59 * Return: vm_fault_t value.
61 static inline vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn,
64 return vmf_insert_pfn_pmd_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
66 vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
67 pgprot_t pgprot, bool write);
70 * vmf_insert_pfn_pud - insert a pud size pfn
71 * @vmf: Structure describing the fault
73 * @pgprot: page protection to use
74 * @write: whether it's a write fault
76 * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
78 * Return: vm_fault_t value.
80 static inline vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn,
83 return vmf_insert_pfn_pud_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
86 enum transparent_hugepage_flag {
87 TRANSPARENT_HUGEPAGE_FLAG,
88 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
89 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
90 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
91 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
92 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
93 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
94 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
95 #ifdef CONFIG_DEBUG_VM
96 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG,
101 struct kobj_attribute;
103 extern ssize_t single_hugepage_flag_store(struct kobject *kobj,
104 struct kobj_attribute *attr,
105 const char *buf, size_t count,
106 enum transparent_hugepage_flag flag);
107 extern ssize_t single_hugepage_flag_show(struct kobject *kobj,
108 struct kobj_attribute *attr, char *buf,
109 enum transparent_hugepage_flag flag);
110 extern struct kobj_attribute shmem_enabled_attr;
112 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
113 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
115 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
116 #define HPAGE_PMD_SHIFT PMD_SHIFT
117 #define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT)
118 #define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1))
120 #define HPAGE_PUD_SHIFT PUD_SHIFT
121 #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT)
122 #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1))
124 extern unsigned long transparent_hugepage_flags;
127 * to be used on vmas which are known to support THP.
128 * Use transparent_hugepage_enabled otherwise
130 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
132 if (vma->vm_flags & VM_NOHUGEPAGE)
135 if (vma_is_temporary_stack(vma))
138 if (test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
141 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
144 * For dax vmas, try to always use hugepage mappings. If the kernel does
145 * not support hugepages, fsdax mappings will fallback to PAGE_SIZE
146 * mappings, and device-dax namespaces, that try to guarantee a given
147 * mapping size, will fail to enable
152 if (transparent_hugepage_flags &
153 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
154 return !!(vma->vm_flags & VM_HUGEPAGE);
159 bool transparent_hugepage_enabled(struct vm_area_struct *vma);
161 #define HPAGE_CACHE_INDEX_MASK (HPAGE_PMD_NR - 1)
163 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
166 /* Don't have to check pgoff for anonymous vma */
167 if (!vma_is_anonymous(vma)) {
168 if (((vma->vm_start >> PAGE_SHIFT) & HPAGE_CACHE_INDEX_MASK) !=
169 (vma->vm_pgoff & HPAGE_CACHE_INDEX_MASK))
173 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
178 #define transparent_hugepage_use_zero_page() \
179 (transparent_hugepage_flags & \
180 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
182 extern unsigned long thp_get_unmapped_area(struct file *filp,
183 unsigned long addr, unsigned long len, unsigned long pgoff,
184 unsigned long flags);
186 extern void prep_transhuge_page(struct page *page);
187 extern void free_transhuge_page(struct page *page);
188 bool is_transparent_hugepage(struct page *page);
190 bool can_split_huge_page(struct page *page, int *pextra_pins);
191 int split_huge_page_to_list(struct page *page, struct list_head *list);
192 static inline int split_huge_page(struct page *page)
194 return split_huge_page_to_list(page, NULL);
196 void deferred_split_huge_page(struct page *page);
198 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
199 unsigned long address, bool freeze, struct page *page);
201 #define split_huge_pmd(__vma, __pmd, __address) \
203 pmd_t *____pmd = (__pmd); \
204 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \
205 || pmd_devmap(*____pmd)) \
206 __split_huge_pmd(__vma, __pmd, __address, \
211 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
212 bool freeze, struct page *page);
214 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
215 unsigned long address);
217 #define split_huge_pud(__vma, __pud, __address) \
219 pud_t *____pud = (__pud); \
220 if (pud_trans_huge(*____pud) \
221 || pud_devmap(*____pud)) \
222 __split_huge_pud(__vma, __pud, __address); \
225 extern int hugepage_madvise(struct vm_area_struct *vma,
226 unsigned long *vm_flags, int advice);
227 extern void vma_adjust_trans_huge(struct vm_area_struct *vma,
231 extern spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd,
232 struct vm_area_struct *vma);
233 extern spinlock_t *__pud_trans_huge_lock(pud_t *pud,
234 struct vm_area_struct *vma);
236 static inline int is_swap_pmd(pmd_t pmd)
238 return !pmd_none(pmd) && !pmd_present(pmd);
241 /* mmap_lock must be held on entry */
242 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
243 struct vm_area_struct *vma)
245 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
246 return __pmd_trans_huge_lock(pmd, vma);
250 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
251 struct vm_area_struct *vma)
253 if (pud_trans_huge(*pud) || pud_devmap(*pud))
254 return __pud_trans_huge_lock(pud, vma);
260 * thp_head - Head page of a transparent huge page.
261 * @page: Any page (tail, head or regular) found in the page cache.
263 static inline struct page *thp_head(struct page *page)
265 return compound_head(page);
269 * thp_order - Order of a transparent huge page.
270 * @page: Head page of a transparent huge page.
272 static inline unsigned int thp_order(struct page *page)
274 VM_BUG_ON_PGFLAGS(PageTail(page), page);
276 return HPAGE_PMD_ORDER;
281 * thp_nr_pages - The number of regular pages in this huge page.
282 * @page: The head page of a huge page.
284 static inline int thp_nr_pages(struct page *page)
286 VM_BUG_ON_PGFLAGS(PageTail(page), page);
292 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
293 pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
294 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
295 pud_t *pud, int flags, struct dev_pagemap **pgmap);
297 extern vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t orig_pmd);
299 extern struct page *huge_zero_page;
301 static inline bool is_huge_zero_page(struct page *page)
303 return READ_ONCE(huge_zero_page) == page;
306 static inline bool is_huge_zero_pmd(pmd_t pmd)
308 return is_huge_zero_page(pmd_page(pmd));
311 static inline bool is_huge_zero_pud(pud_t pud)
316 struct page *mm_get_huge_zero_page(struct mm_struct *mm);
317 void mm_put_huge_zero_page(struct mm_struct *mm);
319 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
321 static inline bool thp_migration_supported(void)
323 return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
326 static inline struct list_head *page_deferred_list(struct page *page)
329 * Global or memcg deferred list in the second tail pages is
330 * occupied by compound_head.
332 return &page[2].deferred_list;
335 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
336 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
337 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
338 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
340 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
341 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
342 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
344 static inline struct page *thp_head(struct page *page)
346 VM_BUG_ON_PGFLAGS(PageTail(page), page);
350 static inline unsigned int thp_order(struct page *page)
352 VM_BUG_ON_PGFLAGS(PageTail(page), page);
356 static inline int thp_nr_pages(struct page *page)
358 VM_BUG_ON_PGFLAGS(PageTail(page), page);
362 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
367 static inline bool transparent_hugepage_enabled(struct vm_area_struct *vma)
372 static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
378 static inline void prep_transhuge_page(struct page *page) {}
380 static inline bool is_transparent_hugepage(struct page *page)
385 #define transparent_hugepage_flags 0UL
387 #define thp_get_unmapped_area NULL
390 can_split_huge_page(struct page *page, int *pextra_pins)
396 split_huge_page_to_list(struct page *page, struct list_head *list)
400 static inline int split_huge_page(struct page *page)
404 static inline void deferred_split_huge_page(struct page *page) {}
405 #define split_huge_pmd(__vma, __pmd, __address) \
408 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
409 unsigned long address, bool freeze, struct page *page) {}
410 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
411 unsigned long address, bool freeze, struct page *page) {}
413 #define split_huge_pud(__vma, __pmd, __address) \
416 static inline int hugepage_madvise(struct vm_area_struct *vma,
417 unsigned long *vm_flags, int advice)
422 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
428 static inline int is_swap_pmd(pmd_t pmd)
432 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
433 struct vm_area_struct *vma)
437 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
438 struct vm_area_struct *vma)
443 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf,
449 static inline bool is_huge_zero_page(struct page *page)
454 static inline bool is_huge_zero_pud(pud_t pud)
459 static inline void mm_put_huge_zero_page(struct mm_struct *mm)
464 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
465 unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
470 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
471 unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap)
476 static inline bool thp_migration_supported(void)
480 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
483 * thp_size - Size of a transparent huge page.
484 * @page: Head page of a transparent huge page.
486 * Return: Number of bytes in this page.
488 static inline unsigned long thp_size(struct page *page)
490 return PAGE_SIZE << thp_order(page);
493 #endif /* _LINUX_HUGE_MM_H */