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 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);
18
19 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
20 extern 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 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,
29 unsigned long addr,
30 pmd_t *pmd,
31 unsigned int flags);
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 int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
42 unsigned long addr, unsigned long end,
43 unsigned char *vec);
44 extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
45 unsigned long new_addr, unsigned long old_end,
46 pmd_t *old_pmd, pmd_t *new_pmd);
47 extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
48 unsigned long addr, pgprot_t newprot,
49 int prot_numa);
50 vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write);
51 vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write);
52 enum transparent_hugepage_flag {
53 TRANSPARENT_HUGEPAGE_FLAG,
54 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
55 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
56 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
57 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
58 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
59 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
60 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
61 #ifdef CONFIG_DEBUG_VM
62 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG,
63 #endif
64 };
65
66 struct kobject;
67 struct kobj_attribute;
68
69 extern ssize_t single_hugepage_flag_store(struct kobject *kobj,
70 struct kobj_attribute *attr,
71 const char *buf, size_t count,
72 enum transparent_hugepage_flag flag);
73 extern ssize_t single_hugepage_flag_show(struct kobject *kobj,
74 struct kobj_attribute *attr, char *buf,
75 enum transparent_hugepage_flag flag);
76 extern struct kobj_attribute shmem_enabled_attr;
77
78 #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
79 #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
80
81 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
82 #define HPAGE_PMD_SHIFT PMD_SHIFT
83 #define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT)
84 #define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1))
85
86 #define HPAGE_PUD_SHIFT PUD_SHIFT
87 #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT)
88 #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1))
89
90 extern bool is_vma_temporary_stack(struct vm_area_struct *vma);
91
92 extern unsigned long transparent_hugepage_flags;
93
94 /*
95 * to be used on vmas which are known to support THP.
96 * Use transparent_hugepage_enabled otherwise
97 */
__transparent_hugepage_enabled(struct vm_area_struct * vma)98 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
99 {
100 if (vma->vm_flags & VM_NOHUGEPAGE)
101 return false;
102
103 if (is_vma_temporary_stack(vma))
104 return false;
105
106 if (test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
107 return false;
108
109 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
110 return true;
111
112 if (vma_is_dax(vma))
113 return true;
114
115 if (transparent_hugepage_flags &
116 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
117 return !!(vma->vm_flags & VM_HUGEPAGE);
118
119 return false;
120 }
121
122 bool transparent_hugepage_enabled(struct vm_area_struct *vma);
123
124 #define transparent_hugepage_use_zero_page() \
125 (transparent_hugepage_flags & \
126 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
127 #ifdef CONFIG_DEBUG_VM
128 #define transparent_hugepage_debug_cow() \
129 (transparent_hugepage_flags & \
130 (1<<TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG))
131 #else /* CONFIG_DEBUG_VM */
132 #define transparent_hugepage_debug_cow() 0
133 #endif /* CONFIG_DEBUG_VM */
134
135 extern unsigned long thp_get_unmapped_area(struct file *filp,
136 unsigned long addr, unsigned long len, unsigned long pgoff,
137 unsigned long flags);
138
139 extern void prep_transhuge_page(struct page *page);
140 extern void free_transhuge_page(struct page *page);
141
142 bool can_split_huge_page(struct page *page, int *pextra_pins);
143 int split_huge_page_to_list(struct page *page, struct list_head *list);
split_huge_page(struct page * page)144 static inline int split_huge_page(struct page *page)
145 {
146 return split_huge_page_to_list(page, NULL);
147 }
148 void deferred_split_huge_page(struct page *page);
149
150 void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
151 unsigned long address, bool freeze, struct page *page);
152
153 #define split_huge_pmd(__vma, __pmd, __address) \
154 do { \
155 pmd_t *____pmd = (__pmd); \
156 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \
157 || pmd_devmap(*____pmd)) \
158 __split_huge_pmd(__vma, __pmd, __address, \
159 false, NULL); \
160 } while (0)
161
162
163 void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
164 bool freeze, struct page *page);
165
166 void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
167 unsigned long address);
168
169 #define split_huge_pud(__vma, __pud, __address) \
170 do { \
171 pud_t *____pud = (__pud); \
172 if (pud_trans_huge(*____pud) \
173 || pud_devmap(*____pud)) \
174 __split_huge_pud(__vma, __pud, __address); \
175 } while (0)
176
177 extern int hugepage_madvise(struct vm_area_struct *vma,
178 unsigned long *vm_flags, int advice);
179 extern void vma_adjust_trans_huge(struct vm_area_struct *vma,
180 unsigned long start,
181 unsigned long end,
182 long adjust_next);
183 extern spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd,
184 struct vm_area_struct *vma);
185 extern spinlock_t *__pud_trans_huge_lock(pud_t *pud,
186 struct vm_area_struct *vma);
187
is_swap_pmd(pmd_t pmd)188 static inline int is_swap_pmd(pmd_t pmd)
189 {
190 return !pmd_none(pmd) && !pmd_present(pmd);
191 }
192
193 /* mmap_sem must be held on entry */
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)194 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
195 struct vm_area_struct *vma)
196 {
197 VM_BUG_ON_VMA(!rwsem_is_locked(&vma->vm_mm->mmap_sem), vma);
198 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
199 return __pmd_trans_huge_lock(pmd, vma);
200 else
201 return NULL;
202 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)203 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
204 struct vm_area_struct *vma)
205 {
206 VM_BUG_ON_VMA(!rwsem_is_locked(&vma->vm_mm->mmap_sem), vma);
207 if (pud_trans_huge(*pud) || pud_devmap(*pud))
208 return __pud_trans_huge_lock(pud, vma);
209 else
210 return NULL;
211 }
hpage_nr_pages(struct page * page)212 static inline int hpage_nr_pages(struct page *page)
213 {
214 if (unlikely(PageTransHuge(page)))
215 return HPAGE_PMD_NR;
216 return 1;
217 }
218
219 struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
220 pmd_t *pmd, int flags);
221 struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
222 pud_t *pud, int flags);
223
224 extern vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t orig_pmd);
225
226 extern struct page *huge_zero_page;
227 extern unsigned long huge_zero_pfn;
228
is_huge_zero_page(struct page * page)229 static inline bool is_huge_zero_page(struct page *page)
230 {
231 return READ_ONCE(huge_zero_page) == page;
232 }
233
is_huge_zero_pmd(pmd_t pmd)234 static inline bool is_huge_zero_pmd(pmd_t pmd)
235 {
236 return READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd) && pmd_present(pmd);
237 }
238
is_huge_zero_pud(pud_t pud)239 static inline bool is_huge_zero_pud(pud_t pud)
240 {
241 return false;
242 }
243
244 struct page *mm_get_huge_zero_page(struct mm_struct *mm);
245 void mm_put_huge_zero_page(struct mm_struct *mm);
246
247 #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
248
thp_migration_supported(void)249 static inline bool thp_migration_supported(void)
250 {
251 return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
252 }
253
254 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
255 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
256 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
257 #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
258
259 #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
260 #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
261 #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
262
263 #define hpage_nr_pages(x) 1
264
__transparent_hugepage_enabled(struct vm_area_struct * vma)265 static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
266 {
267 return false;
268 }
269
transparent_hugepage_enabled(struct vm_area_struct * vma)270 static inline bool transparent_hugepage_enabled(struct vm_area_struct *vma)
271 {
272 return false;
273 }
274
prep_transhuge_page(struct page * page)275 static inline void prep_transhuge_page(struct page *page) {}
276
277 #define transparent_hugepage_flags 0UL
278
279 #define thp_get_unmapped_area NULL
280
281 static inline bool
can_split_huge_page(struct page * page,int * pextra_pins)282 can_split_huge_page(struct page *page, int *pextra_pins)
283 {
284 BUILD_BUG();
285 return false;
286 }
287 static inline int
split_huge_page_to_list(struct page * page,struct list_head * list)288 split_huge_page_to_list(struct page *page, struct list_head *list)
289 {
290 return 0;
291 }
split_huge_page(struct page * page)292 static inline int split_huge_page(struct page *page)
293 {
294 return 0;
295 }
deferred_split_huge_page(struct page * page)296 static inline void deferred_split_huge_page(struct page *page) {}
297 #define split_huge_pmd(__vma, __pmd, __address) \
298 do { } while (0)
299
__split_huge_pmd(struct vm_area_struct * vma,pmd_t * pmd,unsigned long address,bool freeze,struct page * page)300 static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
301 unsigned long address, bool freeze, struct page *page) {}
split_huge_pmd_address(struct vm_area_struct * vma,unsigned long address,bool freeze,struct page * page)302 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
303 unsigned long address, bool freeze, struct page *page) {}
304
305 #define split_huge_pud(__vma, __pmd, __address) \
306 do { } while (0)
307
hugepage_madvise(struct vm_area_struct * vma,unsigned long * vm_flags,int advice)308 static inline int hugepage_madvise(struct vm_area_struct *vma,
309 unsigned long *vm_flags, int advice)
310 {
311 BUG();
312 return 0;
313 }
vma_adjust_trans_huge(struct vm_area_struct * vma,unsigned long start,unsigned long end,long adjust_next)314 static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
315 unsigned long start,
316 unsigned long end,
317 long adjust_next)
318 {
319 }
is_swap_pmd(pmd_t pmd)320 static inline int is_swap_pmd(pmd_t pmd)
321 {
322 return 0;
323 }
pmd_trans_huge_lock(pmd_t * pmd,struct vm_area_struct * vma)324 static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
325 struct vm_area_struct *vma)
326 {
327 return NULL;
328 }
pud_trans_huge_lock(pud_t * pud,struct vm_area_struct * vma)329 static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
330 struct vm_area_struct *vma)
331 {
332 return NULL;
333 }
334
do_huge_pmd_numa_page(struct vm_fault * vmf,pmd_t orig_pmd)335 static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf,
336 pmd_t orig_pmd)
337 {
338 return 0;
339 }
340
is_huge_zero_page(struct page * page)341 static inline bool is_huge_zero_page(struct page *page)
342 {
343 return false;
344 }
345
is_huge_zero_pmd(pmd_t pmd)346 static inline bool is_huge_zero_pmd(pmd_t pmd)
347 {
348 return false;
349 }
350
is_huge_zero_pud(pud_t pud)351 static inline bool is_huge_zero_pud(pud_t pud)
352 {
353 return false;
354 }
355
mm_put_huge_zero_page(struct mm_struct * mm)356 static inline void mm_put_huge_zero_page(struct mm_struct *mm)
357 {
358 return;
359 }
360
follow_devmap_pmd(struct vm_area_struct * vma,unsigned long addr,pmd_t * pmd,int flags)361 static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
362 unsigned long addr, pmd_t *pmd, int flags)
363 {
364 return NULL;
365 }
366
follow_devmap_pud(struct vm_area_struct * vma,unsigned long addr,pud_t * pud,int flags)367 static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
368 unsigned long addr, pud_t *pud, int flags)
369 {
370 return NULL;
371 }
372
thp_migration_supported(void)373 static inline bool thp_migration_supported(void)
374 {
375 return false;
376 }
377 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
378
379 #endif /* _LINUX_HUGE_MM_H */
380