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