mm/rmap: Turn page_referenced() into folio_referenced()
[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);
39int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr,
40 pgprot_t newprot, unsigned long cp_flags);
9a9731b1
THV
41vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
42 pgprot_t pgprot, bool write);
43
44/**
45 * vmf_insert_pfn_pmd - insert a pmd size pfn
46 * @vmf: Structure describing the fault
47 * @pfn: pfn to insert
48 * @pgprot: page protection to use
49 * @write: whether it's a write fault
50 *
51 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
52 *
53 * Return: vm_fault_t value.
54 */
55static inline vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn,
56 bool write)
57{
58 return vmf_insert_pfn_pmd_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
59}
60vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
61 pgprot_t pgprot, bool write);
62
63/**
64 * vmf_insert_pfn_pud - insert a pud size pfn
65 * @vmf: Structure describing the fault
66 * @pfn: pfn to insert
67 * @pgprot: page protection to use
68 * @write: whether it's a write fault
69 *
70 * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
71 *
72 * Return: vm_fault_t value.
73 */
74static inline vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn,
75 bool write)
76{
77 return vmf_insert_pfn_pud_prot(vmf, pfn, vmf->vma->vm_page_prot, write);
78}
79
71e3aac0 80enum transparent_hugepage_flag {
bae84953 81 TRANSPARENT_HUGEPAGE_NEVER_DAX,
71e3aac0
AA
82 TRANSPARENT_HUGEPAGE_FLAG,
83 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
444eb2a4
MG
84 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
85 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
21440d7e 86 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
71e3aac0 87 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
ba76149f 88 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG,
79da5407 89 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG,
71e3aac0
AA
90};
91
b46e756f
KS
92struct kobject;
93struct kobj_attribute;
94
ebfe1b8f
RC
95ssize_t single_hugepage_flag_store(struct kobject *kobj,
96 struct kobj_attribute *attr,
97 const char *buf, size_t count,
98 enum transparent_hugepage_flag flag);
99ssize_t single_hugepage_flag_show(struct kobject *kobj,
100 struct kobj_attribute *attr, char *buf,
101 enum transparent_hugepage_flag flag);
5a6e75f8
KS
102extern struct kobj_attribute shmem_enabled_attr;
103
d8c37c48
NH
104#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT)
105#define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
106
71e3aac0 107#ifdef CONFIG_TRANSPARENT_HUGEPAGE
fde52796
AK
108#define HPAGE_PMD_SHIFT PMD_SHIFT
109#define HPAGE_PMD_SIZE ((1UL) << HPAGE_PMD_SHIFT)
110#define HPAGE_PMD_MASK (~(HPAGE_PMD_SIZE - 1))
71e3aac0 111
a00cc7d9
MW
112#define HPAGE_PUD_SHIFT PUD_SHIFT
113#define HPAGE_PUD_SIZE ((1UL) << HPAGE_PUD_SHIFT)
114#define HPAGE_PUD_MASK (~(HPAGE_PUD_SIZE - 1))
115
16981d76
DW
116extern unsigned long transparent_hugepage_flags;
117
e6be37b2
ML
118static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
119 unsigned long haddr)
120{
121 /* Don't have to check pgoff for anonymous vma */
122 if (!vma_is_anonymous(vma)) {
123 if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
124 HPAGE_PMD_NR))
125 return false;
126 }
127
128 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
129 return false;
130 return true;
131}
132
133static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
134 unsigned long vm_flags)
135{
136 /* Explicitly disabled through madvise. */
137 if ((vm_flags & VM_NOHUGEPAGE) ||
138 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
139 return false;
140 return true;
141}
142
7635d9cb
MH
143/*
144 * to be used on vmas which are known to support THP.
e6be37b2 145 * Use transparent_hugepage_active otherwise
7635d9cb
MH
146 */
147static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
16981d76 148{
bae84953
AK
149
150 /*
151 * If the hardware/firmware marked hugepage support disabled.
152 */
153 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
154 return false;
155
e6be37b2 156 if (!transhuge_vma_enabled(vma, vma->vm_flags))
16981d76
DW
157 return false;
158
222100ee 159 if (vma_is_temporary_stack(vma))
16981d76
DW
160 return false;
161
16981d76
DW
162 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
163 return true;
bae84953 164
baabda26
DW
165 if (vma_is_dax(vma))
166 return true;
167
16981d76
DW
168 if (transparent_hugepage_flags &
169 (1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
170 return !!(vma->vm_flags & VM_HUGEPAGE);
171
172 return false;
173}
174
e6be37b2 175bool transparent_hugepage_active(struct vm_area_struct *vma);
43675e6f 176
79da5407
KS
177#define transparent_hugepage_use_zero_page() \
178 (transparent_hugepage_flags & \
179 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG))
71e3aac0 180
ebfe1b8f
RC
181unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
182 unsigned long len, unsigned long pgoff, unsigned long flags);
74d2fad1 183
ebfe1b8f
RC
184void prep_transhuge_page(struct page *page);
185void free_transhuge_page(struct page *page);
005ba37c 186bool is_transparent_hugepage(struct page *page);
9a982250 187
b8f593cd 188bool can_split_huge_page(struct page *page, int *pextra_pins);
e9b61f19
KS
189int split_huge_page_to_list(struct page *page, struct list_head *list);
190static inline int split_huge_page(struct page *page)
191{
192 return split_huge_page_to_list(page, NULL);
193}
9a982250 194void deferred_split_huge_page(struct page *page);
eef1b3ba
KS
195
196void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
33f4751e 197 unsigned long address, bool freeze, struct page *page);
eef1b3ba
KS
198
199#define split_huge_pmd(__vma, __pmd, __address) \
200 do { \
201 pmd_t *____pmd = (__pmd); \
84c3fc4e 202 if (is_swap_pmd(*____pmd) || pmd_trans_huge(*____pmd) \
5c7fb56e 203 || pmd_devmap(*____pmd)) \
fec89c10 204 __split_huge_pmd(__vma, __pmd, __address, \
33f4751e 205 false, NULL); \
eef1b3ba 206 } while (0)
ad0bed24 207
2a52bcbc 208
fec89c10
KS
209void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
210 bool freeze, struct page *page);
2a52bcbc 211
a00cc7d9
MW
212void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
213 unsigned long address);
214
215#define split_huge_pud(__vma, __pud, __address) \
216 do { \
217 pud_t *____pud = (__pud); \
218 if (pud_trans_huge(*____pud) \
219 || pud_devmap(*____pud)) \
220 __split_huge_pud(__vma, __pud, __address); \
221 } while (0)
222
ebfe1b8f
RC
223int hugepage_madvise(struct vm_area_struct *vma, unsigned long *vm_flags,
224 int advice);
225void vma_adjust_trans_huge(struct vm_area_struct *vma, unsigned long start,
226 unsigned long end, long adjust_next);
227spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma);
228spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma);
84c3fc4e
ZY
229
230static inline int is_swap_pmd(pmd_t pmd)
231{
232 return !pmd_none(pmd) && !pmd_present(pmd);
233}
234
c1e8d7c6 235/* mmap_lock must be held on entry */
b6ec57f4
KS
236static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
237 struct vm_area_struct *vma)
025c5b24 238{
84c3fc4e 239 if (is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || pmd_devmap(*pmd))
b6ec57f4 240 return __pmd_trans_huge_lock(pmd, vma);
025c5b24 241 else
969e8d7e 242 return NULL;
025c5b24 243}
a00cc7d9
MW
244static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
245 struct vm_area_struct *vma)
246{
a00cc7d9
MW
247 if (pud_trans_huge(*pud) || pud_devmap(*pud))
248 return __pud_trans_huge_lock(pud, vma);
249 else
250 return NULL;
251}
6ffbb458
MWO
252
253/**
254 * thp_order - Order of a transparent huge page.
255 * @page: Head page of a transparent huge page.
256 */
257static inline unsigned int thp_order(struct page *page)
258{
259 VM_BUG_ON_PGFLAGS(PageTail(page), page);
260 if (PageHead(page))
261 return HPAGE_PMD_ORDER;
262 return 0;
263}
264
6c357848
MWO
265/**
266 * thp_nr_pages - The number of regular pages in this huge page.
267 * @page: The head page of a huge page.
268 */
269static inline int thp_nr_pages(struct page *page)
2c888cfb 270{
6c357848
MWO
271 VM_BUG_ON_PGFLAGS(PageTail(page), page);
272 if (PageHead(page))
2c888cfb
RR
273 return HPAGE_PMD_NR;
274 return 1;
275}
d10e63f2 276
5bf34d7c
MWO
277/**
278 * folio_test_pmd_mappable - Can we map this folio with a PMD?
279 * @folio: The folio to test
280 */
281static inline bool folio_test_pmd_mappable(struct folio *folio)
282{
283 return folio_order(folio) >= HPAGE_PMD_ORDER;
284}
285
a00cc7d9 286struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
df06b37f 287 pmd_t *pmd, int flags, struct dev_pagemap **pgmap);
a00cc7d9 288struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
df06b37f 289 pud_t *pud, int flags, struct dev_pagemap **pgmap);
a00cc7d9 290
5db4f15c 291vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf);
d10e63f2 292
56873f43 293extern struct page *huge_zero_page;
3b77e8c8 294extern unsigned long huge_zero_pfn;
56873f43
WY
295
296static inline bool is_huge_zero_page(struct page *page)
297{
6aa7de05 298 return READ_ONCE(huge_zero_page) == page;
56873f43
WY
299}
300
fc437044
MW
301static inline bool is_huge_zero_pmd(pmd_t pmd)
302{
3b77e8c8 303 return READ_ONCE(huge_zero_pfn) == pmd_pfn(pmd) && pmd_present(pmd);
fc437044
MW
304}
305
a00cc7d9
MW
306static inline bool is_huge_zero_pud(pud_t pud)
307{
308 return false;
309}
310
6fcb52a5
AL
311struct page *mm_get_huge_zero_page(struct mm_struct *mm);
312void mm_put_huge_zero_page(struct mm_struct *mm);
fc437044 313
10102459
KS
314#define mk_huge_pmd(page, prot) pmd_mkhuge(mk_pmd(page, prot))
315
9c670ea3
NH
316static inline bool thp_migration_supported(void)
317{
318 return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
319}
320
87eaceb3
YS
321static inline struct list_head *page_deferred_list(struct page *page)
322{
323 /*
324 * Global or memcg deferred list in the second tail pages is
325 * occupied by compound_head.
326 */
327 return &page[2].deferred_list;
328}
329
71e3aac0 330#else /* CONFIG_TRANSPARENT_HUGEPAGE */
d8c37c48
NH
331#define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
332#define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
333#define HPAGE_PMD_SIZE ({ BUILD_BUG(); 0; })
71e3aac0 334
a00cc7d9
MW
335#define HPAGE_PUD_SHIFT ({ BUILD_BUG(); 0; })
336#define HPAGE_PUD_MASK ({ BUILD_BUG(); 0; })
337#define HPAGE_PUD_SIZE ({ BUILD_BUG(); 0; })
338
6ffbb458
MWO
339static inline unsigned int thp_order(struct page *page)
340{
341 VM_BUG_ON_PGFLAGS(PageTail(page), page);
342 return 0;
343}
344
6c357848 345static inline int thp_nr_pages(struct page *page)
77d6b909 346{
6c357848 347 VM_BUG_ON_PGFLAGS(PageTail(page), page);
77d6b909
MWO
348 return 1;
349}
2c888cfb 350
5bf34d7c
MWO
351static inline bool folio_test_pmd_mappable(struct folio *folio)
352{
353 return false;
354}
355
7635d9cb
MH
356static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
357{
358 return false;
359}
360
e6be37b2 361static inline bool transparent_hugepage_active(struct vm_area_struct *vma)
16981d76
DW
362{
363 return false;
364}
71e3aac0 365
43675e6f
YS
366static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
367 unsigned long haddr)
368{
369 return false;
370}
371
e6be37b2
ML
372static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
373 unsigned long vm_flags)
374{
375 return false;
376}
377
800d8c63
KS
378static inline void prep_transhuge_page(struct page *page) {}
379
005ba37c
SC
380static inline bool is_transparent_hugepage(struct page *page)
381{
382 return false;
383}
384
71e3aac0 385#define transparent_hugepage_flags 0UL
74d2fad1
TK
386
387#define thp_get_unmapped_area NULL
388
b8f593cd
HY
389static inline bool
390can_split_huge_page(struct page *page, int *pextra_pins)
391{
392 BUILD_BUG();
393 return false;
394}
5bc7b8ac
SL
395static inline int
396split_huge_page_to_list(struct page *page, struct list_head *list)
397{
398 return 0;
399}
71e3aac0
AA
400static inline int split_huge_page(struct page *page)
401{
402 return 0;
403}
9a982250 404static inline void deferred_split_huge_page(struct page *page) {}
78ddc534 405#define split_huge_pmd(__vma, __pmd, __address) \
e180377f 406 do { } while (0)
2a52bcbc 407
fd60775a
DR
408static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
409 unsigned long address, bool freeze, struct page *page) {}
2a52bcbc 410static inline void split_huge_pmd_address(struct vm_area_struct *vma,
fec89c10 411 unsigned long address, bool freeze, struct page *page) {}
2a52bcbc 412
a00cc7d9
MW
413#define split_huge_pud(__vma, __pmd, __address) \
414 do { } while (0)
415
60ab3244
AA
416static inline int hugepage_madvise(struct vm_area_struct *vma,
417 unsigned long *vm_flags, int advice)
0af4e98b
AA
418{
419 BUG();
420 return 0;
421}
94fcc585
AA
422static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
423 unsigned long start,
424 unsigned long end,
425 long adjust_next)
426{
427}
84c3fc4e
ZY
428static inline int is_swap_pmd(pmd_t pmd)
429{
430 return 0;
431}
b6ec57f4
KS
432static inline spinlock_t *pmd_trans_huge_lock(pmd_t *pmd,
433 struct vm_area_struct *vma)
025c5b24 434{
b6ec57f4 435 return NULL;
025c5b24 436}
a00cc7d9
MW
437static inline spinlock_t *pud_trans_huge_lock(pud_t *pud,
438 struct vm_area_struct *vma)
439{
440 return NULL;
441}
d10e63f2 442
5db4f15c 443static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
d10e63f2 444{
4daae3b4 445 return 0;
d10e63f2
MG
446}
447
56873f43
WY
448static inline bool is_huge_zero_page(struct page *page)
449{
450 return false;
451}
452
3b77e8c8
HD
453static inline bool is_huge_zero_pmd(pmd_t pmd)
454{
455 return false;
456}
457
a00cc7d9
MW
458static inline bool is_huge_zero_pud(pud_t pud)
459{
460 return false;
461}
462
6fcb52a5 463static inline void mm_put_huge_zero_page(struct mm_struct *mm)
aa88b68c 464{
6fcb52a5 465 return;
aa88b68c 466}
3565fce3
DW
467
468static inline struct page *follow_devmap_pmd(struct vm_area_struct *vma,
df06b37f 469 unsigned long addr, pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
3565fce3
DW
470{
471 return NULL;
472}
a00cc7d9
MW
473
474static inline struct page *follow_devmap_pud(struct vm_area_struct *vma,
df06b37f 475 unsigned long addr, pud_t *pud, int flags, struct dev_pagemap **pgmap)
a00cc7d9
MW
476{
477 return NULL;
478}
9c670ea3
NH
479
480static inline bool thp_migration_supported(void)
481{
482 return false;
483}
71e3aac0
AA
484#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
485
346cf613
MWO
486static inline int split_folio_to_list(struct folio *folio,
487 struct list_head *list)
488{
489 return split_huge_page_to_list(&folio->page, list);
490}
491
af3bbc12
MWO
492/**
493 * thp_size - Size of a transparent huge page.
494 * @page: Head page of a transparent huge page.
495 *
496 * Return: Number of bytes in this page.
497 */
498static inline unsigned long thp_size(struct page *page)
499{
500 return PAGE_SIZE << thp_order(page);
501}
502
71e3aac0 503#endif /* _LINUX_HUGE_MM_H */