Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
71e3aac0 AA |
2 | #ifndef _LINUX_HUGE_MM_H |
3 | #define _LINUX_HUGE_MM_H | |
4 | ||
16981d76 | 5 | #include <linux/sched/coredump.h> |
226ab561 | 6 | #include <linux/mm_types.h> |
16981d76 | 7 | |
baabda26 DW |
8 | #include <linux/fs.h> /* only for vma_is_dax() */ |
9 | ||
ebfe1b8f RC |
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, | |
8f34f1ea | 13 | struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma); |
5db4f15c | 14 | void huge_pmd_set_accessed(struct vm_fault *vmf); |
ebfe1b8f RC |
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); | |
a00cc7d9 MW |
18 | |
19 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD | |
ebfe1b8f | 20 | void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud); |
a00cc7d9 MW |
21 | #else |
22 | static inline void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) | |
23 | { | |
24 | } | |
25 | #endif | |
26 | ||
5db4f15c | 27 | vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf); |
ebfe1b8f RC |
28 | struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, |
29 | unsigned long addr, pmd_t *pmd, | |
30 | unsigned int flags); | |
31 | bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, | |
32 | pmd_t *pmd, unsigned long addr, unsigned long next); | |
33 | int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, | |
34 | unsigned long addr); | |
35 | int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t *pud, | |
36 | unsigned long addr); | |
37 | bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, | |
38 | unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd); | |
4a18419f NA |
39 | int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
40 | pmd_t *pmd, unsigned long addr, pgprot_t newprot, | |
41 | unsigned long cp_flags); | |
9a9731b1 | 42 | |
7b806d22 LS |
43 | vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write); |
44 | vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write); | |
9a9731b1 | 45 | |
71e3aac0 | 46 | enum transparent_hugepage_flag { |
3c556d24 | 47 | TRANSPARENT_HUGEPAGE_UNSUPPORTED, |
71e3aac0 AA |
48 | TRANSPARENT_HUGEPAGE_FLAG, |
49 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
444eb2a4 MG |
50 | TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, |
51 | TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, | |
21440d7e | 52 | TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, |
71e3aac0 | 53 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, |
ba76149f | 54 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG, |
79da5407 | 55 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG, |
71e3aac0 AA |
56 | }; |
57 | ||
b46e756f KS |
58 | struct kobject; |
59 | struct kobj_attribute; | |
60 | ||
ebfe1b8f RC |
61 | ssize_t single_hugepage_flag_store(struct kobject *kobj, |
62 | struct kobj_attribute *attr, | |
63 | const char *buf, size_t count, | |
64 | enum transparent_hugepage_flag flag); | |
65 | ssize_t single_hugepage_flag_show(struct kobject *kobj, | |
66 | struct kobj_attribute *attr, char *buf, | |
67 | enum transparent_hugepage_flag flag); | |
5a6e75f8 KS |
68 | extern struct kobj_attribute shmem_enabled_attr; |
69 | ||
d8c37c48 NH |
70 | #define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT) |
71 | #define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER) | |
72 | ||
71e3aac0 | 73 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
fde52796 AK |
74 | #define HPAGE_PMD_SHIFT PMD_SHIFT |
75 | #define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT) | |
76 | #define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1)) | |
71e3aac0 | 77 | |
a00cc7d9 MW |
78 | #define HPAGE_PUD_SHIFT PUD_SHIFT |
79 | #define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT) | |
80 | #define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1)) | |
81 | ||
16981d76 DW |
82 | extern unsigned long transparent_hugepage_flags; |
83 | ||
1064026b YS |
84 | #define hugepage_flags_enabled() \ |
85 | (transparent_hugepage_flags & \ | |
86 | ((1<<TRANSPARENT_HUGEPAGE_FLAG) | \ | |
87 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))) | |
88 | #define hugepage_flags_always() \ | |
89 | (transparent_hugepage_flags & \ | |
90 | (1<<TRANSPARENT_HUGEPAGE_FLAG)) | |
91 | ||
4fa6893f YS |
92 | /* |
93 | * Do the below checks: | |
94 | * - For file vma, check if the linear page offset of vma is | |
95 | * HPAGE_PMD_NR aligned within the file. The hugepage is | |
96 | * guaranteed to be hugepage-aligned within the file, but we must | |
97 | * check that the PMD-aligned addresses in the VMA map to | |
98 | * PMD-aligned offsets within the file, else the hugepage will | |
99 | * not be PMD-mappable. | |
100 | * - For all vmas, check if the haddr is in an aligned HPAGE_PMD_SIZE | |
101 | * area. | |
102 | */ | |
e6be37b2 | 103 | static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, |
c453d8c7 | 104 | unsigned long addr) |
e6be37b2 | 105 | { |
c453d8c7 YS |
106 | unsigned long haddr; |
107 | ||
e6be37b2 ML |
108 | /* Don't have to check pgoff for anonymous vma */ |
109 | if (!vma_is_anonymous(vma)) { | |
110 | if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, | |
111 | HPAGE_PMD_NR)) | |
112 | return false; | |
113 | } | |
114 | ||
c453d8c7 | 115 | haddr = addr & HPAGE_PMD_MASK; |
e6be37b2 | 116 | |
e6be37b2 | 117 | if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) |
e6be37b2 ML |
118 | return false; |
119 | return true; | |
120 | } | |
121 | ||
78d12c19 YS |
122 | static inline bool file_thp_enabled(struct vm_area_struct *vma) |
123 | { | |
124 | struct inode *inode; | |
125 | ||
126 | if (!vma->vm_file) | |
127 | return false; | |
128 | ||
129 | inode = vma->vm_file->f_inode; | |
130 | ||
131 | return (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS)) && | |
132 | (vma->vm_flags & VM_EXEC) && | |
133 | !inode_is_open_for_write(inode) && S_ISREG(inode->i_mode); | |
134 | } | |
135 | ||
a7f4e6e4 ZK |
136 | bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags, |
137 | bool smaps, bool in_pf, bool enforce_sysfs); | |
43675e6f | 138 | |
79da5407 KS |
139 | #define transparent_hugepage_use_zero_page() \ |
140 | (transparent_hugepage_flags & \ | |
141 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG)) | |
71e3aac0 | 142 | |
ebfe1b8f RC |
143 | unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, |
144 | unsigned long len, unsigned long pgoff, unsigned long flags); | |
74d2fad1 | 145 | |
ebfe1b8f RC |
146 | void prep_transhuge_page(struct page *page); |
147 | void free_transhuge_page(struct page *page); | |
9a982250 | 148 | |
d4b4084a | 149 | bool can_split_folio(struct folio *folio, int *pextra_pins); |
e9b61f19 KS |
150 | int split_huge_page_to_list(struct page *page, struct list_head *list); |
151 | static inline int split_huge_page(struct page *page) | |
152 | { | |
153 | return split_huge_page_to_list(page, NULL); | |
154 | } | |
f158ed61 | 155 | void deferred_split_folio(struct folio *folio); |
eef1b3ba KS |
156 | |
157 | void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, | |
af28a988 | 158 | unsigned long address, bool freeze, struct folio *folio); |
eef1b3ba KS |
159 | |
160 | #define split_huge_pmd(__vma, __pmd, __address) \ | |
161 | do { \ | |
162 | pmd_t *____pmd = (__pmd); \ | |
84c3fc4e | 163 | if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \ |
5c7fb56e | 164 | || pmd_devmap(*____pmd)) \ |
fec89c10 | 165 | __split_huge_pmd(__vma, __pmd, __address, \ |
33f4751e | 166 | false, NULL); \ |
eef1b3ba | 167 | } while (0) |
ad0bed24 | 168 | |
2a52bcbc | 169 | |
fec89c10 | 170 | void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, |
af28a988 | 171 | bool freeze, struct folio *folio); |
2a52bcbc | 172 | |
a00cc7d9 MW |
173 | void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, |
174 | unsigned long address); | |
175 | ||
176 | #define split_huge_pud(__vma, __pud, __address) \ | |
177 | do { \ | |
178 | pud_t *____pud = (__pud); \ | |
179 | if (pud_trans_huge(*____pud) \ | |
180 | || pud_devmap(*____pud)) \ | |
181 | __split_huge_pud(__vma, __pud, __address); \ | |
182 | } while (0) | |
183 | ||
ebfe1b8f RC |
184 | int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags, |
185 | int advice); | |
7d8faaf1 ZK |
186 | int madvise_collapse(struct vm_area_struct *vma, |
187 | struct vm_area_struct **prev, | |
188 | unsigned long start, unsigned long end); | |
ebfe1b8f RC |
189 | void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start, |
190 | unsigned long end, long adjust_next); | |
191 | spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma); | |
192 | spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma); | |
84c3fc4e ZY |
193 | |
194 | static inline int is_swap_pmd(pmd_t pmd) | |
195 | { | |
196 | return !pmd_none(pmd) && !pmd_present(pmd); | |
197 | } | |
198 | ||
c1e8d7c6 | 199 | /* mmap_lock must be held on entry */ |
b6ec57f4 KS |
200 | static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd, |
201 | struct vm_area_struct *vma) | |
025c5b24 | 202 | { |
84c3fc4e | 203 | if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) |
b6ec57f4 | 204 | return __pmd_trans_huge_lock(pmd, vma); |
025c5b24 | 205 | else |
969e8d7e | 206 | return NULL; |
025c5b24 | 207 | } |
a00cc7d9 MW |
208 | static inline spinlock_t *pud_trans_huge_lock(pud_t *pud, |
209 | struct vm_area_struct *vma) | |
210 | { | |
a00cc7d9 MW |
211 | if (pud_trans_huge(*pud) || pud_devmap(*pud)) |
212 | return __pud_trans_huge_lock(pud, vma); | |
213 | else | |
214 | return NULL; | |
215 | } | |
6ffbb458 | 216 | |
5bf34d7c MWO |
217 | /** |
218 | * folio_test_pmd_mappable - Can we map this folio with a PMD? | |
219 | * @folio: The folio to test | |
220 | */ | |
221 | static inline bool folio_test_pmd_mappable(struct folio *folio) | |
222 | { | |
223 | return folio_order(folio) >= HPAGE_PMD_ORDER; | |
224 | } | |
225 | ||
a00cc7d9 | 226 | struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr, |
df06b37f | 227 | pmd_t *pmd, int flags, struct dev_pagemap **pgmap); |
a00cc7d9 | 228 | struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr, |
df06b37f | 229 | pud_t *pud, int flags, struct dev_pagemap **pgmap); |
a00cc7d9 | 230 | |
5db4f15c | 231 | vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); |
d10e63f2 | 232 | |
56873f43 | 233 | extern struct page *huge_zero_page; |
3b77e8c8 | 234 | extern unsigned long huge_zero_pfn; |
56873f43 WY |
235 | |
236 | static inline bool is_huge_zero_page(struct page *page) | |
237 | { | |
6aa7de05 | 238 | return READ_ONCE(huge_zero_page) == page; |
56873f43 WY |
239 | } |
240 | ||
fc437044 MW |
241 | static inline bool is_huge_zero_pmd(pmd_t pmd) |
242 | { | |
3ce4fee4 | 243 | return pmd_present(pmd) && READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd); |
fc437044 MW |
244 | } |
245 | ||
a00cc7d9 MW |
246 | static inline bool is_huge_zero_pud(pud_t pud) |
247 | { | |
248 | return false; | |
249 | } | |
250 | ||
6fcb52a5 AL |
251 | struct page *mm_get_huge_zero_page(struct mm_struct *mm); |
252 | void mm_put_huge_zero_page(struct mm_struct *mm); | |
fc437044 | 253 | |
10102459 KS |
254 | #define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot)) |
255 | ||
9c670ea3 NH |
256 | static inline bool thp_migration_supported(void) |
257 | { | |
258 | return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION); | |
259 | } | |
260 | ||
71e3aac0 | 261 | #else /* CONFIG_TRANSPARENT_HUGEPAGE */ |
d8c37c48 NH |
262 | #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; }) |
263 | #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; }) | |
264 | #define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; }) | |
71e3aac0 | 265 | |
a00cc7d9 MW |
266 | #define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; }) |
267 | #define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; }) | |
268 | #define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; }) | |
269 | ||
5bf34d7c MWO |
270 | static inline bool folio_test_pmd_mappable(struct folio *folio) |
271 | { | |
272 | return false; | |
273 | } | |
274 | ||
43675e6f | 275 | static inline bool transhuge_vma_suitable(struct vm_area_struct *vma, |
c453d8c7 | 276 | unsigned long addr) |
43675e6f YS |
277 | { |
278 | return false; | |
279 | } | |
280 | ||
9fec5168 | 281 | static inline bool hugepage_vma_check(struct vm_area_struct *vma, |
a7f4e6e4 ZK |
282 | unsigned long vm_flags, bool smaps, |
283 | bool in_pf, bool enforce_sysfs) | |
e6be37b2 ML |
284 | { |
285 | return false; | |
286 | } | |
287 | ||
800d8c63 KS |
288 | static inline void prep_transhuge_page(struct page *page) {} |
289 | ||
71e3aac0 | 290 | #define transparent_hugepage_flags 0UL |
74d2fad1 TK |
291 | |
292 | #define thp_get_unmapped_area NULL | |
293 | ||
b8f593cd | 294 | static inline bool |
d4b4084a | 295 | can_split_folio(struct folio *folio, int *pextra_pins) |
b8f593cd | 296 | { |
b8f593cd HY |
297 | return false; |
298 | } | |
5bc7b8ac SL |
299 | static inline int |
300 | split_huge_page_to_list(struct page *page, struct list_head *list) | |
301 | { | |
302 | return 0; | |
303 | } | |
71e3aac0 AA |
304 | static inline int split_huge_page(struct page *page) |
305 | { | |
306 | return 0; | |
307 | } | |
f158ed61 | 308 | static inline void deferred_split_folio(struct folio *folio) {} |
78ddc534 | 309 | #define split_huge_pmd(__vma, __pmd, __address) \ |
e180377f | 310 | do { } while (0) |
2a52bcbc | 311 | |
fd60775a | 312 | static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
af28a988 | 313 | unsigned long address, bool freeze, struct folio *folio) {} |
2a52bcbc | 314 | static inline void split_huge_pmd_address(struct vm_area_struct *vma, |
af28a988 | 315 | unsigned long address, bool freeze, struct folio *folio) {} |
2a52bcbc | 316 | |
a00cc7d9 MW |
317 | #define split_huge_pud(__vma, __pmd, __address) \ |
318 | do { } while (0) | |
319 | ||
60ab3244 AA |
320 | static inline int hugepage_madvise(struct vm_area_struct *vma, |
321 | unsigned long *vm_flags, int advice) | |
0af4e98b | 322 | { |
7d8faaf1 | 323 | return -EINVAL; |
0af4e98b | 324 | } |
7d8faaf1 ZK |
325 | |
326 | static inline int madvise_collapse(struct vm_area_struct *vma, | |
327 | struct vm_area_struct **prev, | |
328 | unsigned long start, unsigned long end) | |
329 | { | |
330 | return -EINVAL; | |
331 | } | |
332 | ||
94fcc585 AA |
333 | static inline void vma_adjust_trans_huge(struct vm_area_struct *vma, |
334 | unsigned long start, | |
335 | unsigned long end, | |
336 | long adjust_next) | |
337 | { | |
338 | } | |
84c3fc4e ZY |
339 | static inline int is_swap_pmd(pmd_t pmd) |
340 | { | |
341 | return 0; | |
342 | } | |
b6ec57f4 KS |
343 | static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd, |
344 | struct vm_area_struct *vma) | |
025c5b24 | 345 | { |
b6ec57f4 | 346 | return NULL; |
025c5b24 | 347 | } |
a00cc7d9 MW |
348 | static inline spinlock_t *pud_trans_huge_lock(pud_t *pud, |
349 | struct vm_area_struct *vma) | |
350 | { | |
351 | return NULL; | |
352 | } | |
d10e63f2 | 353 | |
5db4f15c | 354 | static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) |
d10e63f2 | 355 | { |
4daae3b4 | 356 | return 0; |
d10e63f2 MG |
357 | } |
358 | ||
56873f43 WY |
359 | static inline bool is_huge_zero_page(struct page *page) |
360 | { | |
361 | return false; | |
362 | } | |
363 | ||
3b77e8c8 HD |
364 | static inline bool is_huge_zero_pmd(pmd_t pmd) |
365 | { | |
366 | return false; | |
367 | } | |
368 | ||
a00cc7d9 MW |
369 | static inline bool is_huge_zero_pud(pud_t pud) |
370 | { | |
371 | return false; | |
372 | } | |
373 | ||
6fcb52a5 | 374 | static inline void mm_put_huge_zero_page(struct mm_struct *mm) |
aa88b68c | 375 | { |
6fcb52a5 | 376 | return; |
aa88b68c | 377 | } |
3565fce3 DW |
378 | |
379 | static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma, | |
df06b37f | 380 | unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap) |
3565fce3 DW |
381 | { |
382 | return NULL; | |
383 | } | |
a00cc7d9 MW |
384 | |
385 | static inline struct page *follow_devmap_pud(struct vm_area_struct *vma, | |
df06b37f | 386 | unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap) |
a00cc7d9 MW |
387 | { |
388 | return NULL; | |
389 | } | |
9c670ea3 NH |
390 | |
391 | static inline bool thp_migration_supported(void) | |
392 | { | |
393 | return false; | |
394 | } | |
71e3aac0 AA |
395 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
396 | ||
346cf613 MWO |
397 | static inline int split_folio_to_list(struct folio *folio, |
398 | struct list_head *list) | |
399 | { | |
400 | return split_huge_page_to_list(&folio->page, list); | |
401 | } | |
402 | ||
d788f5b3 MWO |
403 | static inline int split_folio(struct folio *folio) |
404 | { | |
405 | return split_folio_to_list(folio, NULL); | |
406 | } | |
407 | ||
d0637c50 BS |
408 | /* |
409 | * archs that select ARCH_WANTS_THP_SWAP but don't support THP_SWP due to | |
410 | * limitations in the implementation like arm64 MTE can override this to | |
411 | * false | |
412 | */ | |
413 | #ifndef arch_thp_swp_supported | |
414 | static inline bool arch_thp_swp_supported(void) | |
415 | { | |
416 | return true; | |
417 | } | |
418 | #endif | |
419 | ||
71e3aac0 | 420 | #endif /* _LINUX_HUGE_MM_H */ |