mm/ksm: use folio in write_protect_page
[linux-block.git] / mm / internal.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/* internal.h: mm/ internal definitions
3 *
4 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
1da177e4 6 */
0f8053a5
NP
7#ifndef __MM_INTERNAL_H
8#define __MM_INTERNAL_H
9
29f175d1 10#include <linux/fs.h>
0f8053a5 11#include <linux/mm.h>
e9b61f19 12#include <linux/pagemap.h>
2aff7a47 13#include <linux/rmap.h>
a62fb92a
RR
14#include <linux/swap.h>
15#include <linux/swapops.h>
edf14cdb 16#include <linux/tracepoint-defs.h>
1da177e4 17
0e499ed3
MWO
18struct folio_batch;
19
dd56b046
MG
20/*
21 * The set of flags that only affect watermark checking and reclaim
22 * behaviour. This is used by the MM to obey the caller constraints
23 * about IO, FS and watermark checking while ignoring placement
24 * hints such as HIGHMEM usage.
25 */
26#define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\
dcda9b04 27 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\
e838a45f 28 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\
2973d822 29 __GFP_NOLOCKDEP)
dd56b046
MG
30
31/* The GFP flags allowed during early boot */
32#define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS))
33
34/* Control allocation cpuset and node placement constraints */
35#define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
36
37/* Do not use these with a slab allocator */
38#define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
39
3f913fc5
QZ
40/*
41 * Different from WARN_ON_ONCE(), no warning will be issued
42 * when we specify __GFP_NOWARN.
43 */
44#define WARN_ON_ONCE_GFP(cond, gfp) ({ \
45 static bool __section(".data.once") __warned; \
46 int __ret_warn_once = !!(cond); \
47 \
48 if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \
49 __warned = true; \
50 WARN_ON(1); \
51 } \
52 unlikely(__ret_warn_once); \
53})
54
62906027
NP
55void page_writeback_init(void);
56
eec20426
MWO
57/*
58 * If a 16GB hugetlb folio were mapped by PTEs of all of its 4kB pages,
e78a13fd 59 * its nr_pages_mapped would be 0x400000: choose the ENTIRELY_MAPPED bit
eec20426
MWO
60 * above that range, instead of 2*(PMD_SIZE/PAGE_SIZE). Hugetlb currently
61 * leaves nr_pages_mapped at 0, but avoid surprise if it participates later.
62 */
e78a13fd
DH
63#define ENTIRELY_MAPPED 0x800000
64#define FOLIO_PAGES_MAPPED (ENTIRELY_MAPPED - 1)
eec20426 65
1279aa06
KW
66/*
67 * Flags passed to __show_mem() and show_free_areas() to suppress output in
68 * various contexts.
69 */
70#define SHOW_MEM_FILTER_NODES (0x0001u) /* disallowed nodes */
71
eec20426
MWO
72/*
73 * How many individual pages have an elevated _mapcount. Excludes
74 * the folio's entire_mapcount.
05c5323b
DH
75 *
76 * Don't use this function outside of debugging code.
eec20426 77 */
b84fd283 78static inline int folio_nr_pages_mapped(const struct folio *folio)
eec20426
MWO
79{
80 return atomic_read(&folio->_nr_pages_mapped) & FOLIO_PAGES_MAPPED;
81}
82
f238b8c3
BS
83/*
84 * Retrieve the first entry of a folio based on a provided entry within the
85 * folio. We cannot rely on folio->swap as there is no guarantee that it has
86 * been initialized. Used for calling arch_swap_restore()
87 */
b84fd283
MWO
88static inline swp_entry_t folio_swap(swp_entry_t entry,
89 const struct folio *folio)
f238b8c3
BS
90{
91 swp_entry_t swap = {
92 .val = ALIGN_DOWN(entry.val, folio_nr_pages(folio)),
93 };
94
95 return swap;
96}
97
b84fd283 98static inline void *folio_raw_mapping(const struct folio *folio)
64601000
MWO
99{
100 unsigned long mapping = (unsigned long)folio->mapping;
101
102 return (void *)(mapping & ~PAGE_MAPPING_FLAGS);
103}
104
ac96cc4d
BS
105#ifdef CONFIG_MMU
106
107/* Flags for folio_pte_batch(). */
108typedef int __bitwise fpb_t;
109
110/* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */
111#define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0))
112
113/* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty bit. */
114#define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1))
115
116static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags)
117{
118 if (flags & FPB_IGNORE_DIRTY)
119 pte = pte_mkclean(pte);
120 if (likely(flags & FPB_IGNORE_SOFT_DIRTY))
121 pte = pte_clear_soft_dirty(pte);
122 return pte_wrprotect(pte_mkold(pte));
123}
124
125/**
126 * folio_pte_batch - detect a PTE batch for a large folio
127 * @folio: The large folio to detect a PTE batch for.
128 * @addr: The user virtual address the first page is mapped at.
129 * @start_ptep: Page table pointer for the first entry.
130 * @pte: Page table entry for the first page.
131 * @max_nr: The maximum number of table entries to consider.
132 * @flags: Flags to modify the PTE batch semantics.
133 * @any_writable: Optional pointer to indicate whether any entry except the
134 * first one is writable.
3931b871
RR
135 * @any_young: Optional pointer to indicate whether any entry except the
136 * first one is young.
ac96cc4d
BS
137 *
138 * Detect a PTE batch: consecutive (present) PTEs that map consecutive
139 * pages of the same large folio.
140 *
141 * All PTEs inside a PTE batch have the same PTE bits set, excluding the PFN,
142 * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) and
143 * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY).
144 *
145 * start_ptep must map any page of the folio. max_nr must be at least one and
146 * must be limited by the caller so scanning cannot exceed a single page table.
147 *
148 * Return: the number of table entries in the batch.
149 */
150static inline int folio_pte_batch(struct folio *folio, unsigned long addr,
151 pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags,
3931b871 152 bool *any_writable, bool *any_young)
ac96cc4d
BS
153{
154 unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio);
155 const pte_t *end_ptep = start_ptep + max_nr;
156 pte_t expected_pte, *ptep;
3931b871 157 bool writable, young;
ac96cc4d
BS
158 int nr;
159
160 if (any_writable)
161 *any_writable = false;
3931b871
RR
162 if (any_young)
163 *any_young = false;
ac96cc4d
BS
164
165 VM_WARN_ON_FOLIO(!pte_present(pte), folio);
166 VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio);
167 VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) != folio, folio);
168
169 nr = pte_batch_hint(start_ptep, pte);
170 expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags);
171 ptep = start_ptep + nr;
172
173 while (ptep < end_ptep) {
174 pte = ptep_get(ptep);
175 if (any_writable)
176 writable = !!pte_write(pte);
3931b871
RR
177 if (any_young)
178 young = !!pte_young(pte);
ac96cc4d
BS
179 pte = __pte_batch_clear_ignored(pte, flags);
180
181 if (!pte_same(pte, expected_pte))
182 break;
183
184 /*
185 * Stop immediately once we reached the end of the folio. In
186 * corner cases the next PFN might fall into a different
187 * folio.
188 */
189 if (pte_pfn(pte) >= folio_end_pfn)
190 break;
191
192 if (any_writable)
193 *any_writable |= writable;
3931b871
RR
194 if (any_young)
195 *any_young |= young;
ac96cc4d
BS
196
197 nr = pte_batch_hint(ptep, pte);
198 expected_pte = pte_advance_pfn(expected_pte, nr);
199 ptep += nr;
200 }
201
202 return min(ptep - start_ptep, max_nr);
203}
a62fb92a
RR
204
205/**
206 * pte_next_swp_offset - Increment the swap entry offset field of a swap pte.
207 * @pte: The initial pte state; is_swap_pte(pte) must be true and
208 * non_swap_entry() must be false.
209 *
210 * Increments the swap offset, while maintaining all other fields, including
211 * swap type, and any swp pte bits. The resulting pte is returned.
212 */
213static inline pte_t pte_next_swp_offset(pte_t pte)
214{
215 swp_entry_t entry = pte_to_swp_entry(pte);
216 pte_t new = __swp_entry_to_pte(__swp_entry(swp_type(entry),
217 (swp_offset(entry) + 1)));
218
219 if (pte_swp_soft_dirty(pte))
220 new = pte_swp_mksoft_dirty(new);
221 if (pte_swp_exclusive(pte))
222 new = pte_swp_mkexclusive(new);
223 if (pte_swp_uffd_wp(pte))
224 new = pte_swp_mkuffd_wp(new);
225
226 return new;
227}
228
229/**
230 * swap_pte_batch - detect a PTE batch for a set of contiguous swap entries
231 * @start_ptep: Page table pointer for the first entry.
232 * @max_nr: The maximum number of table entries to consider.
233 * @pte: Page table entry for the first entry.
234 *
235 * Detect a batch of contiguous swap entries: consecutive (non-present) PTEs
236 * containing swap entries all with consecutive offsets and targeting the same
237 * swap type, all with matching swp pte bits.
238 *
239 * max_nr must be at least one and must be limited by the caller so scanning
240 * cannot exceed a single page table.
241 *
242 * Return: the number of table entries in the batch.
243 */
244static inline int swap_pte_batch(pte_t *start_ptep, int max_nr, pte_t pte)
245{
246 pte_t expected_pte = pte_next_swp_offset(pte);
247 const pte_t *end_ptep = start_ptep + max_nr;
248 pte_t *ptep = start_ptep + 1;
249
250 VM_WARN_ON(max_nr < 1);
251 VM_WARN_ON(!is_swap_pte(pte));
252 VM_WARN_ON(non_swap_entry(pte_to_swp_entry(pte)));
253
254 while (ptep < end_ptep) {
255 pte = ptep_get(ptep);
256
257 if (!pte_same(pte, expected_pte))
258 break;
259
260 expected_pte = pte_next_swp_offset(expected_pte);
261 ptep++;
262 }
263
264 return ptep - start_ptep;
265}
ac96cc4d
BS
266#endif /* CONFIG_MMU */
267
512b7931 268void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio,
8cd7c588 269 int nr_throttled);
512b7931 270static inline void acct_reclaim_writeback(struct folio *folio)
8cd7c588 271{
512b7931 272 pg_data_t *pgdat = folio_pgdat(folio);
8cd7c588
MG
273 int nr_throttled = atomic_read(&pgdat->nr_writeback_throttled);
274
275 if (nr_throttled)
512b7931 276 __acct_reclaim_writeback(pgdat, folio, nr_throttled);
8cd7c588
MG
277}
278
d818fca1
MG
279static inline void wake_throttle_isolated(pg_data_t *pgdat)
280{
281 wait_queue_head_t *wqh;
282
283 wqh = &pgdat->reclaim_wait[VMSCAN_THROTTLE_ISOLATED];
284 if (waitqueue_active(wqh))
285 wake_up(wqh);
286}
287
997f0ecb 288vm_fault_t vmf_anon_prepare(struct vm_fault *vmf);
2b740303 289vm_fault_t do_swap_page(struct vm_fault *vmf);
575ced1c 290void folio_rotate_reclaimable(struct folio *folio);
2580d554 291bool __folio_end_writeback(struct folio *folio);
261b6840 292void deactivate_file_folio(struct folio *folio);
018ee47f 293void folio_activate(struct folio *folio);
8a966ed7 294
fd892593 295void free_pgtables(struct mmu_gather *tlb, struct ma_state *mas,
763ecb03 296 struct vm_area_struct *start_vma, unsigned long floor,
98e51a22 297 unsigned long ceiling, bool mm_wr_locked);
03c4f204 298void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte);
42b77728 299
3506659e 300struct zap_details;
aac45363
MH
301void unmap_page_range(struct mmu_gather *tlb,
302 struct vm_area_struct *vma,
303 unsigned long addr, unsigned long end,
304 struct zap_details *details);
305
56a4d67c
MWO
306void page_cache_ra_order(struct readahead_control *, struct file_ra_state *,
307 unsigned int order);
fcd9ae4f 308void force_page_cache_ra(struct readahead_control *, unsigned long nr);
7b3df3b9
DH
309static inline void force_page_cache_readahead(struct address_space *mapping,
310 struct file *file, pgoff_t index, unsigned long nr_to_read)
311{
fcd9ae4f
MWO
312 DEFINE_READAHEAD(ractl, file, &file->f_ra, mapping, index);
313 force_page_cache_ra(&ractl, nr_to_read);
7b3df3b9 314}
29f175d1 315
3392ca12 316unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start,
51dcbdac 317 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
9fb6beea 318unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
0e499ed3 319 pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices);
78f42660 320void filemap_free_folio(struct address_space *mapping, struct folio *folio);
1e84a3d9 321int truncate_inode_folio(struct address_space *mapping, struct folio *folio);
b9a8a419
MWO
322bool truncate_inode_partial_folio(struct folio *folio, loff_t start,
323 loff_t end);
1e12cbb9 324long mapping_evict_folio(struct address_space *mapping, struct folio *folio);
1a0fc811
MWO
325unsigned long mapping_try_invalidate(struct address_space *mapping,
326 pgoff_t start, pgoff_t end, unsigned long *nr_failed);
5c211ba2 327
1eb6234e 328/**
3eed3ef5
MWO
329 * folio_evictable - Test whether a folio is evictable.
330 * @folio: The folio to test.
1eb6234e 331 *
3eed3ef5
MWO
332 * Test whether @folio is evictable -- i.e., should be placed on
333 * active/inactive lists vs unevictable list.
1eb6234e 334 *
3eed3ef5
MWO
335 * Reasons folio might not be evictable:
336 * 1. folio's mapping marked unevictable
337 * 2. One of the pages in the folio is part of an mlocked VMA
1eb6234e 338 */
3eed3ef5
MWO
339static inline bool folio_evictable(struct folio *folio)
340{
341 bool ret;
342
343 /* Prevent address_space of inode and swap cache from being freed */
344 rcu_read_lock();
345 ret = !mapping_unevictable(folio_mapping(folio)) &&
346 !folio_test_mlocked(folio);
347 rcu_read_unlock();
348 return ret;
349}
350
7835e98b 351/*
0139aa7b 352 * Turn a non-refcounted page (->_refcount == 0) into refcounted with
7835e98b
NP
353 * a count of one.
354 */
355static inline void set_page_refcounted(struct page *page)
356{
309381fe 357 VM_BUG_ON_PAGE(PageTail(page), page);
fe896d18 358 VM_BUG_ON_PAGE(page_ref_count(page), page);
77a8a788 359 set_page_count(page, 1);
77a8a788
NP
360}
361
0201ebf2
DH
362/*
363 * Return true if a folio needs ->release_folio() calling upon it.
364 */
365static inline bool folio_needs_release(struct folio *folio)
366{
b4fa966f
DH
367 struct address_space *mapping = folio_mapping(folio);
368
369 return folio_has_private(folio) ||
370 (mapping && mapping_release_always(mapping));
0201ebf2
DH
371}
372
03f6462a
HD
373extern unsigned long highest_memmap_pfn;
374
c73322d0
JW
375/*
376 * Maximum number of reclaim retries without progress before the OOM
377 * killer is consider the only way forward.
378 */
379#define MAX_RECLAIM_RETRIES 16
380
894bc310
LS
381/*
382 * in mm/vmscan.c:
383 */
f7f9c00d 384bool isolate_lru_page(struct page *page);
be2d5756 385bool folio_isolate_lru(struct folio *folio);
ca6d60f3
MWO
386void putback_lru_page(struct page *page);
387void folio_putback_lru(struct folio *folio);
c3f4a9a2 388extern void reclaim_throttle(pg_data_t *pgdat, enum vmscan_throttle_state reason);
62695a84 389
6219049a
BL
390/*
391 * in mm/rmap.c:
392 */
50722804 393pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
6219049a 394
894bc310
LS
395/*
396 * in mm/page_alloc.c
397 */
eb8589b4 398#define K(x) ((x) << (PAGE_SHIFT-10))
3c605096 399
9420f89d
MRI
400extern char * const zone_names[MAX_NR_ZONES];
401
f2fc4b44
MRI
402/* perform sanity checks on struct pages being allocated or freed */
403DECLARE_STATIC_KEY_MAYBE(CONFIG_DEBUG_VM, check_pages_enabled);
404
e95d372c
KW
405extern int min_free_kbytes;
406
407void setup_per_zone_wmarks(void);
408void calculate_min_free_kbytes(void);
409int __meminit init_per_zone_wmark_min(void);
410void page_alloc_sysctl_init(void);
f2fc4b44 411
1a6d53a1
VB
412/*
413 * Structure for holding the mostly immutable allocation parameters passed
414 * between functions involved in allocations, including the alloc_pages*
415 * family of functions.
416 *
97a225e6 417 * nodemask, migratetype and highest_zoneidx are initialized only once in
84172f4b 418 * __alloc_pages() and then never change.
1a6d53a1 419 *
97a225e6 420 * zonelist, preferred_zone and highest_zoneidx are set first in
84172f4b 421 * __alloc_pages() for the fast path, and might be later changed
68956ccb 422 * in __alloc_pages_slowpath(). All other functions pass the whole structure
1a6d53a1
VB
423 * by a const pointer.
424 */
425struct alloc_context {
426 struct zonelist *zonelist;
427 nodemask_t *nodemask;
c33d6c06 428 struct zoneref *preferred_zoneref;
1a6d53a1 429 int migratetype;
97a225e6
JK
430
431 /*
432 * highest_zoneidx represents highest usable zone index of
433 * the allocation request. Due to the nature of the zone,
434 * memory on lower zone than the highest_zoneidx will be
435 * protected by lowmem_reserve[highest_zoneidx].
436 *
437 * highest_zoneidx is also used by reclaim/compaction to limit
438 * the target zone since higher zone than this index cannot be
439 * usable for this allocation request.
440 */
441 enum zone_type highest_zoneidx;
c9ab0c4f 442 bool spread_dirty_pages;
1a6d53a1
VB
443};
444
8170ac47
ZY
445/*
446 * This function returns the order of a free page in the buddy system. In
447 * general, page_zone(page)->lock must be held by the caller to prevent the
448 * page from being allocated in parallel and returning garbage as the order.
449 * If a caller does not hold page_zone(page)->lock, it must guarantee that the
450 * page cannot be allocated or merged in parallel. Alternatively, it must
451 * handle invalid values gracefully, and use buddy_order_unsafe() below.
452 */
453static inline unsigned int buddy_order(struct page *page)
454{
455 /* PageBuddy() must be checked by the caller */
456 return page_private(page);
457}
458
459/*
460 * Like buddy_order(), but for callers who cannot afford to hold the zone lock.
461 * PageBuddy() should be checked first by the caller to minimize race window,
462 * and invalid values must be handled gracefully.
463 *
464 * READ_ONCE is used so that if the caller assigns the result into a local
465 * variable and e.g. tests it for valid range before using, the compiler cannot
466 * decide to remove the variable and inline the page_private(page) multiple
467 * times, potentially observing different values in the tests and the actual
468 * use of the result.
469 */
470#define buddy_order_unsafe(page) READ_ONCE(page_private(page))
471
472/*
473 * This function checks whether a page is free && is the buddy
474 * we can coalesce a page and its buddy if
475 * (a) the buddy is not in a hole (check before calling!) &&
476 * (b) the buddy is in the buddy system &&
477 * (c) a page and its buddy have the same order &&
478 * (d) a page and its buddy are in the same zone.
479 *
480 * For recording whether a page is in the buddy system, we set PageBuddy.
481 * Setting, clearing, and testing PageBuddy is serialized by zone->lock.
482 *
483 * For recording page's order, we use page_private(page).
484 */
485static inline bool page_is_buddy(struct page *page, struct page *buddy,
486 unsigned int order)
487{
488 if (!page_is_guard(buddy) && !PageBuddy(buddy))
489 return false;
490
491 if (buddy_order(buddy) != order)
492 return false;
493
494 /*
495 * zone check is done late to avoid uselessly calculating
496 * zone/node ids for pages that could never merge.
497 */
498 if (page_zone_id(page) != page_zone_id(buddy))
499 return false;
500
501 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
502
503 return true;
504}
505
3c605096
JK
506/*
507 * Locate the struct page for both the matching buddy in our
508 * pair (buddy1) and the combined O(n+1) page they form (page).
509 *
510 * 1) Any buddy B1 will have an order O twin B2 which satisfies
511 * the following equation:
512 * B2 = B1 ^ (1 << O)
513 * For example, if the starting buddy (buddy2) is #8 its order
514 * 1 buddy is #10:
515 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
516 *
517 * 2) Any buddy B will have an order O+1 parent P which
518 * satisfies the following equation:
519 * P = B & ~(1 << O)
520 *
5e0a760b 521 * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER
3c605096
JK
522 */
523static inline unsigned long
76741e77 524__find_buddy_pfn(unsigned long page_pfn, unsigned int order)
3c605096 525{
76741e77 526 return page_pfn ^ (1 << order);
3c605096
JK
527}
528
8170ac47
ZY
529/*
530 * Find the buddy of @page and validate it.
531 * @page: The input page
532 * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the
533 * function is used in the performance-critical __free_one_page().
534 * @order: The order of the page
535 * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to
536 * page_to_pfn().
537 *
538 * The found buddy can be a non PageBuddy, out of @page's zone, or its order is
539 * not the same as @page. The validation is necessary before use it.
540 *
541 * Return: the found buddy page or NULL if not found.
542 */
543static inline struct page *find_buddy_page_pfn(struct page *page,
544 unsigned long pfn, unsigned int order, unsigned long *buddy_pfn)
545{
546 unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order);
547 struct page *buddy;
548
549 buddy = page + (__buddy_pfn - pfn);
550 if (buddy_pfn)
551 *buddy_pfn = __buddy_pfn;
552
553 if (page_is_buddy(page, buddy, order))
554 return buddy;
555 return NULL;
556}
557
7cf91a98
JK
558extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
559 unsigned long end_pfn, struct zone *zone);
560
561static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
562 unsigned long end_pfn, struct zone *zone)
563{
564 if (zone->contiguous)
565 return pfn_to_page(start_pfn);
566
567 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
568}
569
904d5857
KW
570void set_zone_contiguous(struct zone *zone);
571
572static inline void clear_zone_contiguous(struct zone *zone)
573{
574 zone->contiguous = false;
575}
576
3c605096 577extern int __isolate_free_page(struct page *page, unsigned int order);
624f58d8
AD
578extern void __putback_isolated_page(struct page *page, unsigned int order,
579 int mt);
7c2ee349 580extern void memblock_free_pages(struct page *page, unsigned long pfn,
d70ddd7a 581 unsigned int order);
a9cd410a 582extern void __free_pages_core(struct page *page, unsigned int order);
ba42b524 583extern void kernel_init_pages(struct page *page, int numpages);
9420f89d 584
1e3be485
TS
585/*
586 * This will have no effect, other than possibly generating a warning, if the
587 * caller passes in a non-large folio.
588 */
589static inline void folio_set_order(struct folio *folio, unsigned int order)
590{
591 if (WARN_ON_ONCE(!order || !folio_test_large(folio)))
592 return;
593
ebc1baf5 594 folio->_flags_1 = (folio->_flags_1 & ~0xffUL) | order;
1e3be485
TS
595#ifdef CONFIG_64BIT
596 folio->_folio_nr_pages = 1U << order;
597#endif
598}
599
8dc4a8f1
MWO
600void folio_undo_large_rmappable(struct folio *folio);
601
23e48832
HD
602static inline struct folio *page_rmappable_folio(struct page *page)
603{
604 struct folio *folio = (struct folio *)page;
605
85edc15a
MWO
606 if (folio && folio_test_large(folio))
607 folio_set_large_rmappable(folio);
23e48832
HD
608 return folio;
609}
610
9420f89d
MRI
611static inline void prep_compound_head(struct page *page, unsigned int order)
612{
613 struct folio *folio = (struct folio *)page;
614
1e3be485 615 folio_set_order(folio, order);
05c5323b 616 atomic_set(&folio->_large_mapcount, -1);
9420f89d
MRI
617 atomic_set(&folio->_entire_mapcount, -1);
618 atomic_set(&folio->_nr_pages_mapped, 0);
619 atomic_set(&folio->_pincount, 0);
b7b098cf
MWO
620 if (order > 1)
621 INIT_LIST_HEAD(&folio->_deferred_list);
9420f89d
MRI
622}
623
624static inline void prep_compound_tail(struct page *head, int tail_idx)
625{
626 struct page *p = head + tail_idx;
627
628 p->mapping = TAIL_MAPPING;
629 set_compound_head(p, head);
630 set_page_private(p, 0);
631}
632
d00181b9 633extern void prep_compound_page(struct page *page, unsigned int order);
9420f89d 634
46f24fd8
JK
635extern void post_alloc_hook(struct page *page, unsigned int order,
636 gfp_t gfp_flags);
733aea0b
ZY
637extern bool free_pages_prepare(struct page *page, unsigned int order);
638
42aa83cb 639extern int user_min_free_kbytes;
20a0307c 640
90491d87
MWO
641void free_unref_page(struct page *page, unsigned int order);
642void free_unref_folios(struct folio_batch *fbatch);
0966aeb4 643
68265390 644extern void zone_pcp_reset(struct zone *zone);
ec6e8c7e
VB
645extern void zone_pcp_disable(struct zone *zone);
646extern void zone_pcp_enable(struct zone *zone);
9420f89d 647extern void zone_pcp_init(struct zone *zone);
68265390 648
c803b3c8
MR
649extern void *memmap_alloc(phys_addr_t size, phys_addr_t align,
650 phys_addr_t min_addr,
651 int nid, bool exact_nid);
652
e95d372c
KW
653void memmap_init_range(unsigned long, int, unsigned long, unsigned long,
654 unsigned long, enum meminit_context, struct vmem_altmap *, int);
b2c9e2fb 655
ff9543fd
MN
656#if defined CONFIG_COMPACTION || defined CONFIG_CMA
657
658/*
659 * in mm/compaction.c
660 */
661/*
662 * compact_control is used to track pages being migrated and the free pages
663 * they are being migrated to during memory compaction. The free_pfn starts
664 * at the end of a zone and migrate_pfn begins at the start. Movable pages
665 * are moved to the end of a zone during a compaction run and the run
666 * completes when free_pfn <= migrate_pfn
667 */
668struct compact_control {
733aea0b 669 struct list_head freepages[NR_PAGE_ORDERS]; /* List of free pages to migrate to */
ff9543fd 670 struct list_head migratepages; /* List of pages being migrated */
c5fbd937
MG
671 unsigned int nr_freepages; /* Number of isolated free pages */
672 unsigned int nr_migratepages; /* Number of pages to migrate */
ff9543fd 673 unsigned long free_pfn; /* isolate_freepages search base */
c2ad7a1f
OS
674 /*
675 * Acts as an in/out parameter to page isolation for migration.
676 * isolate_migratepages uses it as a search base.
677 * isolate_migratepages_block will update the value to the next pfn
678 * after the last isolated one.
679 */
680 unsigned long migrate_pfn;
70b44595 681 unsigned long fast_start_pfn; /* a pfn to start linear scan from */
c5943b9c
MG
682 struct zone *zone;
683 unsigned long total_migrate_scanned;
684 unsigned long total_free_scanned;
dbe2d4e4
MG
685 unsigned short fast_search_fail;/* failures to use free list searches */
686 short search_order; /* order to start a fast search at */
f25ba6dc
VB
687 const gfp_t gfp_mask; /* gfp mask of a direct compactor */
688 int order; /* order a direct compactor needs */
d39773a0 689 int migratetype; /* migratetype of direct compactor */
f25ba6dc 690 const unsigned int alloc_flags; /* alloc flags of a direct compactor */
97a225e6 691 const int highest_zoneidx; /* zone index of a direct compactor */
e0b9daeb 692 enum migrate_mode mode; /* Async or sync migration mode */
bb13ffeb 693 bool ignore_skip_hint; /* Scan blocks even if marked skip */
2583d671 694 bool no_set_skip_hint; /* Don't mark blocks for skipping */
9f7e3387 695 bool ignore_block_suitable; /* Scan blocks considered unsuitable */
accf6242 696 bool direct_compaction; /* False from kcompactd or /proc/... */
facdaa91 697 bool proactive_compaction; /* kcompactd proactive compaction */
06ed2998 698 bool whole_zone; /* Whole zone should/has been scanned */
d56c1584 699 bool contended; /* Signal lock contention */
48731c84
MG
700 bool finish_pageblock; /* Scan the remainder of a pageblock. Used
701 * when there are potentially transient
702 * isolation or migration failures to
703 * ensure forward progress.
704 */
b06eda09 705 bool alloc_contig; /* alloc_contig_range allocation */
ff9543fd
MN
706};
707
5e1f0f09
MG
708/*
709 * Used in direct compaction when a page should be taken from the freelists
710 * immediately when one is created during the free path.
711 */
712struct capture_control {
713 struct compact_control *cc;
714 struct page *page;
715};
716
ff9543fd 717unsigned long
bb13ffeb
MG
718isolate_freepages_range(struct compact_control *cc,
719 unsigned long start_pfn, unsigned long end_pfn);
c2ad7a1f 720int
edc2ca61
VB
721isolate_migratepages_range(struct compact_control *cc,
722 unsigned long low_pfn, unsigned long end_pfn);
b2c9e2fb
ZY
723
724int __alloc_contig_migrate_range(struct compact_control *cc,
c8b36003
RC
725 unsigned long start, unsigned long end,
726 int migratetype);
9420f89d
MRI
727
728/* Free whole pageblock and set its migration type to MIGRATE_CMA. */
729void init_cma_reserved_pageblock(struct page *page);
730
731#endif /* CONFIG_COMPACTION || CONFIG_CMA */
732
2149cdae
JK
733int find_suitable_fallback(struct free_area *area, unsigned int order,
734 int migratetype, bool only_stealable, bool *can_steal);
ff9543fd 735
62f31bd4
MRI
736static inline bool free_area_empty(struct free_area *area, int migratetype)
737{
738 return list_empty(&area->free_list[migratetype]);
739}
740
30bdbb78
KK
741/*
742 * These three helpers classifies VMAs for virtual memory accounting.
743 */
744
745/*
746 * Executable code area - executable, not writable, not stack
747 */
d977d56c
KK
748static inline bool is_exec_mapping(vm_flags_t flags)
749{
30bdbb78 750 return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC;
d977d56c
KK
751}
752
30bdbb78 753/*
00547ef7 754 * Stack area (including shadow stacks)
30bdbb78
KK
755 *
756 * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous:
757 * do_mmap() forbids all other combinations.
758 */
d977d56c
KK
759static inline bool is_stack_mapping(vm_flags_t flags)
760{
00547ef7 761 return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK);
d977d56c
KK
762}
763
30bdbb78
KK
764/*
765 * Data area - private, writable, not stack
766 */
d977d56c
KK
767static inline bool is_data_mapping(vm_flags_t flags)
768{
30bdbb78 769 return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE;
d977d56c
KK
770}
771
6038def0 772/* mm/util.c */
e05b3453 773struct anon_vma *folio_anon_vma(struct folio *folio);
6038def0 774
af8e3354 775#ifdef CONFIG_MMU
3506659e 776void unmap_mapping_folio(struct folio *folio);
fc05f566 777extern long populate_vma_page_range(struct vm_area_struct *vma,
a78f1ccd 778 unsigned long start, unsigned long end, int *locked);
631426ba
DH
779extern long faultin_page_range(struct mm_struct *mm, unsigned long start,
780 unsigned long end, bool write, int *locked);
b0cc5e89 781extern bool mlock_future_ok(struct mm_struct *mm, unsigned long flags,
3c54a298 782 unsigned long bytes);
28e56657
YF
783
784/*
785 * NOTE: This function can't tell whether the folio is "fully mapped" in the
786 * range.
787 * "fully mapped" means all the pages of folio is associated with the page
788 * table of range while this function just check whether the folio range is
be16dd76 789 * within the range [start, end). Function caller needs to do page table
28e56657
YF
790 * check if it cares about the page table association.
791 *
792 * Typical usage (like mlock or madvise) is:
793 * Caller knows at least 1 page of folio is associated with page table of VMA
794 * and the range [start, end) is intersect with the VMA range. Caller wants
795 * to know whether the folio is fully associated with the range. It calls
796 * this function to check whether the folio is in the range first. Then checks
797 * the page table to know whether the folio is fully mapped to the range.
798 */
799static inline bool
800folio_within_range(struct folio *folio, struct vm_area_struct *vma,
801 unsigned long start, unsigned long end)
802{
803 pgoff_t pgoff, addr;
dd05f5ec 804 unsigned long vma_pglen = vma_pages(vma);
28e56657
YF
805
806 VM_WARN_ON_FOLIO(folio_test_ksm(folio), folio);
807 if (start > end)
808 return false;
809
810 if (start < vma->vm_start)
811 start = vma->vm_start;
812
813 if (end > vma->vm_end)
814 end = vma->vm_end;
815
816 pgoff = folio_pgoff(folio);
817
818 /* if folio start address is not in vma range */
819 if (!in_range(pgoff, vma->vm_pgoff, vma_pglen))
820 return false;
821
822 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
823
824 return !(addr < start || end - addr < folio_size(folio));
825}
826
827static inline bool
828folio_within_vma(struct folio *folio, struct vm_area_struct *vma)
829{
830 return folio_within_range(folio, vma, vma->vm_start, vma->vm_end);
831}
832
b291f000 833/*
7efecffb 834 * mlock_vma_folio() and munlock_vma_folio():
cea86fe2
HD
835 * should be called with vma's mmap_lock held for read or write,
836 * under page table lock for the pte/pmd being added or removed.
b291f000 837 *
4a8ffab0 838 * mlock is usually called at the end of folio_add_*_rmap_*(), munlock at
4d8f7418 839 * the end of folio_remove_rmap_*(); but new anon folios are managed by
96f97c43 840 * folio_add_lru_vma() calling mlock_new_folio().
b291f000 841 */
dcc5d337
MWO
842void mlock_folio(struct folio *folio);
843static inline void mlock_vma_folio(struct folio *folio,
1acbc3f9 844 struct vm_area_struct *vma)
cea86fe2 845{
c8263bd6
HD
846 /*
847 * The VM_SPECIAL check here serves two purposes.
848 * 1) VM_IO check prevents migration from double-counting during mlock.
849 * 2) Although mmap_region() and mlock_fixup() take care that VM_LOCKED
850 * is never left set on a VM_SPECIAL vma, there is an interval while
851 * file->f_op->mmap() is using vm_insert_page(s), when VM_LOCKED may
852 * still be set while VM_SPECIAL bits are added: so ignore it then.
853 */
1acbc3f9 854 if (unlikely((vma->vm_flags & (VM_LOCKED|VM_SPECIAL)) == VM_LOCKED))
dcc5d337
MWO
855 mlock_folio(folio);
856}
857
96f97c43 858void munlock_folio(struct folio *folio);
96f97c43 859static inline void munlock_vma_folio(struct folio *folio,
1acbc3f9 860 struct vm_area_struct *vma)
cea86fe2 861{
1acbc3f9
YF
862 /*
863 * munlock if the function is called. Ideally, we should only
864 * do munlock if any page of folio is unmapped from VMA and
865 * cause folio not fully mapped to VMA.
866 *
867 * But it's not easy to confirm that's the situation. So we
868 * always munlock the folio and page reclaim will correct it
869 * if it's wrong.
870 */
871 if (unlikely(vma->vm_flags & VM_LOCKED))
96f97c43 872 munlock_folio(folio);
cea86fe2 873}
96f97c43 874
96f97c43
LS
875void mlock_new_folio(struct folio *folio);
876bool need_mlock_drain(int cpu);
877void mlock_drain_local(void);
878void mlock_drain_remote(int cpu);
b291f000 879
f55e1014 880extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
b32967ff 881
412ad5fb 882/**
e0abfbb6
MWO
883 * vma_address - Find the virtual address a page range is mapped at
884 * @vma: The vma which maps this object.
412ad5fb
MWO
885 * @pgoff: The page offset within its object.
886 * @nr_pages: The number of pages to consider.
412ad5fb
MWO
887 *
888 * If any page in this range is mapped by this VMA, return the first address
889 * where any of these pages appear. Otherwise, return -EFAULT.
e9b61f19 890 */
e0abfbb6
MWO
891static inline unsigned long vma_address(struct vm_area_struct *vma,
892 pgoff_t pgoff, unsigned long nr_pages)
e9b61f19 893{
494334e4
HD
894 unsigned long address;
895
494334e4
HD
896 if (pgoff >= vma->vm_pgoff) {
897 address = vma->vm_start +
898 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
899 /* Check for address beyond vma (or wrapped through 0?) */
900 if (address < vma->vm_start || address >= vma->vm_end)
901 address = -EFAULT;
6a8e0596 902 } else if (pgoff + nr_pages - 1 >= vma->vm_pgoff) {
494334e4
HD
903 /* Test above avoids possibility of wrap to 0 on 32-bit */
904 address = vma->vm_start;
905 } else {
906 address = -EFAULT;
907 }
908 return address;
6a8e0596
MS
909}
910
494334e4 911/*
2aff7a47 912 * Then at what user virtual address will none of the range be found in vma?
494334e4 913 * Assumes that vma_address() already returned a good starting address.
494334e4 914 */
2aff7a47 915static inline unsigned long vma_address_end(struct page_vma_mapped_walk *pvmw)
e9b61f19 916{
2aff7a47 917 struct vm_area_struct *vma = pvmw->vma;
494334e4
HD
918 pgoff_t pgoff;
919 unsigned long address;
920
2aff7a47
MWO
921 /* Common case, plus ->pgoff is invalid for KSM */
922 if (pvmw->nr_pages == 1)
923 return pvmw->address + PAGE_SIZE;
924
925 pgoff = pvmw->pgoff + pvmw->nr_pages;
494334e4
HD
926 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
927 /* Check for address beyond vma (or wrapped through 0?) */
928 if (address < vma->vm_start || address > vma->vm_end)
929 address = vma->vm_end;
930 return address;
e9b61f19
KS
931}
932
89b15332
JW
933static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
934 struct file *fpin)
935{
936 int flags = vmf->flags;
937
938 if (fpin)
939 return fpin;
940
941 /*
942 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
c1e8d7c6 943 * anything, so we only pin the file and drop the mmap_lock if only
4064b982 944 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt.
89b15332 945 */
4064b982
PX
946 if (fault_flag_allow_retry_first(flags) &&
947 !(flags & FAULT_FLAG_RETRY_NOWAIT)) {
89b15332 948 fpin = get_file(vmf->vma->vm_file);
0790e1e2 949 release_fault_lock(vmf);
89b15332
JW
950 }
951 return fpin;
952}
af8e3354 953#else /* !CONFIG_MMU */
3506659e 954static inline void unmap_mapping_folio(struct folio *folio) { }
96f97c43
LS
955static inline void mlock_new_folio(struct folio *folio) { }
956static inline bool need_mlock_drain(int cpu) { return false; }
957static inline void mlock_drain_local(void) { }
958static inline void mlock_drain_remote(int cpu) { }
4ad0ae8c
NP
959static inline void vunmap_range_noflush(unsigned long start, unsigned long end)
960{
961}
af8e3354 962#endif /* !CONFIG_MMU */
894bc310 963
6b74ab97 964/* Memory initialisation debug and verification */
9420f89d
MRI
965#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
966DECLARE_STATIC_KEY_TRUE(deferred_pages);
967
968bool __init deferred_grow_zone(struct zone *zone, unsigned int order);
969#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
970
6b74ab97
MG
971enum mminit_level {
972 MMINIT_WARNING,
973 MMINIT_VERIFY,
974 MMINIT_TRACE
975};
976
977#ifdef CONFIG_DEBUG_MEMORY_INIT
978
979extern int mminit_loglevel;
980
981#define mminit_dprintk(level, prefix, fmt, arg...) \
982do { \
983 if (level < mminit_loglevel) { \
fc5199d1 984 if (level <= MMINIT_WARNING) \
1170532b 985 pr_warn("mminit::" prefix " " fmt, ##arg); \
fc5199d1
RV
986 else \
987 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \
6b74ab97
MG
988 } \
989} while (0)
990
708614e6 991extern void mminit_verify_pageflags_layout(void);
68ad8df4 992extern void mminit_verify_zonelist(void);
6b74ab97
MG
993#else
994
995static inline void mminit_dprintk(enum mminit_level level,
996 const char *prefix, const char *fmt, ...)
997{
998}
999
708614e6
MG
1000static inline void mminit_verify_pageflags_layout(void)
1001{
1002}
1003
68ad8df4
MG
1004static inline void mminit_verify_zonelist(void)
1005{
1006}
6b74ab97 1007#endif /* CONFIG_DEBUG_MEMORY_INIT */
2dbb51c4 1008
a5f5f91d
MG
1009#define NODE_RECLAIM_NOSCAN -2
1010#define NODE_RECLAIM_FULL -1
1011#define NODE_RECLAIM_SOME 0
1012#define NODE_RECLAIM_SUCCESS 1
7c116f2b 1013
8b09549c
WY
1014#ifdef CONFIG_NUMA
1015extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int);
79c28a41 1016extern int find_next_best_node(int node, nodemask_t *used_node_mask);
8b09549c
WY
1017#else
1018static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask,
1019 unsigned int order)
1020{
1021 return NODE_RECLAIM_NOSCAN;
1022}
79c28a41
DH
1023static inline int find_next_best_node(int node, nodemask_t *used_node_mask)
1024{
1025 return NUMA_NO_NODE;
1026}
8b09549c
WY
1027#endif
1028
60f272f6 1029/*
1030 * mm/memory-failure.c
1031 */
31d3d348
WF
1032extern int hwpoison_filter(struct page *p);
1033
7c116f2b
WF
1034extern u32 hwpoison_filter_dev_major;
1035extern u32 hwpoison_filter_dev_minor;
478c5ffc
WF
1036extern u64 hwpoison_filter_flags_mask;
1037extern u64 hwpoison_filter_flags_value;
4fd466eb 1038extern u64 hwpoison_filter_memcg;
1bfe5feb 1039extern u32 hwpoison_filter_enable;
eb36c587 1040
dc0ef0df 1041extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long,
eb36c587 1042 unsigned long, unsigned long,
9fbeb5ab 1043 unsigned long, unsigned long);
ca57df79
XQ
1044
1045extern void set_pageblock_order(void);
2864f3d0 1046unsigned long reclaim_pages(struct list_head *folio_list, bool ignore_references);
730ec8c0 1047unsigned int reclaim_clean_pages_from_list(struct zone *zone,
4bf4f155 1048 struct list_head *folio_list);
d95ea5d1
BZ
1049/* The ALLOC_WMARK bits are used as an index to zone->watermark */
1050#define ALLOC_WMARK_MIN WMARK_MIN
1051#define ALLOC_WMARK_LOW WMARK_LOW
1052#define ALLOC_WMARK_HIGH WMARK_HIGH
1053#define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
1054
1055/* Mask to get the watermark bits */
1056#define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
1057
cd04ae1e
MH
1058/*
1059 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we
1060 * cannot assume a reduced access to memory reserves is sufficient for
1061 * !MMU
1062 */
1063#ifdef CONFIG_MMU
1064#define ALLOC_OOM 0x08
1065#else
1066#define ALLOC_OOM ALLOC_NO_WATERMARKS
1067#endif
1068
1ebbb218
MG
1069#define ALLOC_NON_BLOCK 0x10 /* Caller cannot block. Allow access
1070 * to 25% of the min watermark or
1071 * 62.5% if __GFP_HIGH is set.
1072 */
524c4807
MG
1073#define ALLOC_MIN_RESERVE 0x20 /* __GFP_HIGH set. Allow access to 50%
1074 * of the min watermark.
1075 */
6bb15450
MG
1076#define ALLOC_CPUSET 0x40 /* check for correct cpuset */
1077#define ALLOC_CMA 0x80 /* allow allocations from CMA areas */
1078#ifdef CONFIG_ZONE_DMA32
1079#define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */
1080#else
1081#define ALLOC_NOFRAGMENT 0x0
1082#endif
eb2e2b42 1083#define ALLOC_HIGHATOMIC 0x200 /* Allows access to MIGRATE_HIGHATOMIC */
736838e9 1084#define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */
d95ea5d1 1085
ab350885 1086/* Flags that allow allocations below the min watermark. */
1ebbb218 1087#define ALLOC_RESERVES (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM)
ab350885 1088
72b252ae
MG
1089enum ttu_flags;
1090struct tlbflush_unmap_batch;
1091
ce612879
MH
1092
1093/*
1094 * only for MM internal work items which do not depend on
1095 * any allocations or locks which might depend on allocations
1096 */
1097extern struct workqueue_struct *mm_percpu_wq;
1098
72b252ae
MG
1099#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
1100void try_to_unmap_flush(void);
d950c947 1101void try_to_unmap_flush_dirty(void);
3ea27719 1102void flush_tlb_batched_pending(struct mm_struct *mm);
72b252ae
MG
1103#else
1104static inline void try_to_unmap_flush(void)
1105{
1106}
d950c947
MG
1107static inline void try_to_unmap_flush_dirty(void)
1108{
1109}
3ea27719
MG
1110static inline void flush_tlb_batched_pending(struct mm_struct *mm)
1111{
1112}
72b252ae 1113#endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
edf14cdb
VB
1114
1115extern const struct trace_print_flags pageflag_names[];
4c85c0be 1116extern const struct trace_print_flags pagetype_names[];
edf14cdb
VB
1117extern const struct trace_print_flags vmaflag_names[];
1118extern const struct trace_print_flags gfpflag_names[];
1119
a6ffdc07
XQ
1120static inline bool is_migrate_highatomic(enum migratetype migratetype)
1121{
1122 return migratetype == MIGRATE_HIGHATOMIC;
1123}
1124
72675e13 1125void setup_zone_pageset(struct zone *zone);
19fc7bed
JK
1126
1127struct migration_target_control {
1128 int nid; /* preferred node id */
1129 nodemask_t *nmask;
1130 gfp_t gfp_mask;
e42dfe4e 1131 enum migrate_reason reason;
19fc7bed
JK
1132};
1133
07073eb0
DH
1134/*
1135 * mm/filemap.c
1136 */
1137size_t splice_folio_into_pipe(struct pipe_inode_info *pipe,
1138 struct folio *folio, loff_t fpos, size_t size);
1139
b67177ec
NP
1140/*
1141 * mm/vmalloc.c
1142 */
4ad0ae8c 1143#ifdef CONFIG_MMU
b6714911 1144void __init vmalloc_init(void);
d905ae2b 1145int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
b67177ec 1146 pgprot_t prot, struct page **pages, unsigned int page_shift);
4ad0ae8c 1147#else
b6714911
MRI
1148static inline void vmalloc_init(void)
1149{
1150}
1151
4ad0ae8c 1152static inline
d905ae2b 1153int __must_check vmap_pages_range_noflush(unsigned long addr, unsigned long end,
4ad0ae8c
NP
1154 pgprot_t prot, struct page **pages, unsigned int page_shift)
1155{
1156 return -EINVAL;
1157}
1158#endif
1159
d905ae2b
AP
1160int __must_check __vmap_pages_range_noflush(unsigned long addr,
1161 unsigned long end, pgprot_t prot,
1162 struct page **pages, unsigned int page_shift);
b073d7f8 1163
4ad0ae8c 1164void vunmap_range_noflush(unsigned long start, unsigned long end);
b67177ec 1165
b073d7f8
AP
1166void __vunmap_range_noflush(unsigned long start, unsigned long end);
1167
f8fd525b 1168int numa_migrate_prep(struct folio *folio, struct vm_fault *vmf,
f4c0d836
YS
1169 unsigned long addr, int page_nid, int *flags);
1170
9f100e3b 1171void free_zone_device_folio(struct folio *folio);
b05a79d4 1172int migrate_device_coherent_page(struct page *page);
27674ef6 1173
ece1ed7b
MWO
1174/*
1175 * mm/gup.c
1176 */
1177struct folio *try_grab_folio(struct page *page, int refs, unsigned int flags);
7ce154fe 1178int __must_check try_grab_page(struct page *page, unsigned int flags);
ece1ed7b 1179
8b9c1cc0
DH
1180/*
1181 * mm/huge_memory.c
1182 */
1b167618
PX
1183void touch_pud(struct vm_area_struct *vma, unsigned long addr,
1184 pud_t *pud, bool write);
4418c522
PX
1185void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1186 pmd_t *pmd, bool write);
8b9c1cc0 1187
adb20b0c
LS
1188/*
1189 * mm/mmap.c
1190 */
93bf5d4a
LS
1191struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
1192 struct vm_area_struct *vma,
1193 unsigned long delta);
adb20b0c 1194
2c224108
JG
1195enum {
1196 /* mark page accessed */
1197 FOLL_TOUCH = 1 << 16,
1198 /* a retry, previous pass started an IO */
1199 FOLL_TRIED = 1 << 17,
1200 /* we are working on non-current tsk/mm */
1201 FOLL_REMOTE = 1 << 18,
1202 /* pages must be released via unpin_user_page */
1203 FOLL_PIN = 1 << 19,
1204 /* gup_fast: prevent fall-back to slow gup */
1205 FOLL_FAST_ONLY = 1 << 20,
1206 /* allow unlocking the mmap lock */
1207 FOLL_UNLOCKABLE = 1 << 21,
631426ba
DH
1208 /* VMA lookup+checks compatible with MADV_POPULATE_(READ|WRITE) */
1209 FOLL_MADV_POPULATE = 1 << 22,
2c224108
JG
1210};
1211
0f20bba1 1212#define INTERNAL_GUP_FLAGS (FOLL_TOUCH | FOLL_TRIED | FOLL_REMOTE | FOLL_PIN | \
631426ba
DH
1213 FOLL_FAST_ONLY | FOLL_UNLOCKABLE | \
1214 FOLL_MADV_POPULATE)
0f20bba1 1215
63b60512
JG
1216/*
1217 * Indicates for which pages that are write-protected in the page table,
1218 * whether GUP has to trigger unsharing via FAULT_FLAG_UNSHARE such that the
1219 * GUP pin will remain consistent with the pages mapped into the page tables
1220 * of the MM.
1221 *
1222 * Temporary unmapping of PageAnonExclusive() pages or clearing of
1223 * PageAnonExclusive() has to protect against concurrent GUP:
1224 * * Ordinary GUP: Using the PT lock
1225 * * GUP-fast and fork(): mm->write_protect_seq
1226 * * GUP-fast and KSM or temporary unmapping (swap, migration): see
e3b4b137 1227 * folio_try_share_anon_rmap_*()
63b60512
JG
1228 *
1229 * Must be called with the (sub)page that's actually referenced via the
1230 * page table entry, which might not necessarily be the head page for a
1231 * PTE-mapped THP.
1232 *
1233 * If the vma is NULL, we're coming from the GUP-fast path and might have
1234 * to fallback to the slow path just to lookup the vma.
1235 */
1236static inline bool gup_must_unshare(struct vm_area_struct *vma,
1237 unsigned int flags, struct page *page)
1238{
1239 /*
1240 * FOLL_WRITE is implicitly handled correctly as the page table entry
1241 * has to be writable -- and if it references (part of) an anonymous
1242 * folio, that part is required to be marked exclusive.
1243 */
1244 if ((flags & (FOLL_WRITE | FOLL_PIN)) != FOLL_PIN)
1245 return false;
1246 /*
1247 * Note: PageAnon(page) is stable until the page is actually getting
1248 * freed.
1249 */
1250 if (!PageAnon(page)) {
1251 /*
1252 * We only care about R/O long-term pining: R/O short-term
1253 * pinning does not have the semantics to observe successive
1254 * changes through the process page tables.
1255 */
1256 if (!(flags & FOLL_LONGTERM))
1257 return false;
1258
1259 /* We really need the vma ... */
1260 if (!vma)
1261 return true;
1262
1263 /*
1264 * ... because we only care about writable private ("COW")
1265 * mappings where we have to break COW early.
1266 */
1267 return is_cow_mapping(vma->vm_flags);
1268 }
1269
e3b4b137 1270 /* Paired with a memory barrier in folio_try_share_anon_rmap_*(). */
25176ad0 1271 if (IS_ENABLED(CONFIG_HAVE_GUP_FAST))
63b60512
JG
1272 smp_rmb();
1273
1274 /*
1275 * Note that PageKsm() pages cannot be exclusive, and consequently,
1276 * cannot get pinned.
1277 */
1278 return !PageAnonExclusive(page);
1279}
ece1ed7b 1280
902c2d91 1281extern bool mirrored_kernelcore;
0db31d63 1282extern bool memblock_has_mirror(void);
902c2d91 1283
412c6ef9
YD
1284static __always_inline void vma_set_range(struct vm_area_struct *vma,
1285 unsigned long start, unsigned long end,
1286 pgoff_t pgoff)
1287{
1288 vma->vm_start = start;
1289 vma->vm_end = end;
1290 vma->vm_pgoff = pgoff;
1291}
1292
76aefad6
PX
1293static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma)
1294{
1295 /*
1296 * NOTE: we must check this before VM_SOFTDIRTY on soft-dirty
1297 * enablements, because when without soft-dirty being compiled in,
1298 * VM_SOFTDIRTY is defined as 0x0, then !(vm_flags & VM_SOFTDIRTY)
1299 * will be constantly true.
1300 */
1301 if (!IS_ENABLED(CONFIG_MEM_SOFT_DIRTY))
1302 return false;
1303
1304 /*
1305 * Soft-dirty is kind of special: its tracking is enabled when the
1306 * vma flags not set.
1307 */
1308 return !(vma->vm_flags & VM_SOFTDIRTY);
1309}
1310
53bee98d
LH
1311static inline void vma_iter_config(struct vma_iterator *vmi,
1312 unsigned long index, unsigned long last)
1313{
53bee98d
LH
1314 __mas_set_range(&vmi->mas, index, last - 1);
1315}
1316
d4e6b397
YD
1317static inline void vma_iter_reset(struct vma_iterator *vmi)
1318{
1319 mas_reset(&vmi->mas);
1320}
1321
1322static inline
1323struct vm_area_struct *vma_iter_prev_range_limit(struct vma_iterator *vmi, unsigned long min)
1324{
1325 return mas_prev_range(&vmi->mas, min);
1326}
1327
1328static inline
1329struct vm_area_struct *vma_iter_next_range_limit(struct vma_iterator *vmi, unsigned long max)
1330{
1331 return mas_next_range(&vmi->mas, max);
1332}
1333
1334static inline int vma_iter_area_lowest(struct vma_iterator *vmi, unsigned long min,
1335 unsigned long max, unsigned long size)
1336{
1337 return mas_empty_area(&vmi->mas, min, max - 1, size);
1338}
1339
1340static inline int vma_iter_area_highest(struct vma_iterator *vmi, unsigned long min,
1341 unsigned long max, unsigned long size)
1342{
1343 return mas_empty_area_rev(&vmi->mas, min, max - 1, size);
1344}
1345
b62b633e
LH
1346/*
1347 * VMA Iterator functions shared between nommu and mmap
1348 */
b5df0922
LH
1349static inline int vma_iter_prealloc(struct vma_iterator *vmi,
1350 struct vm_area_struct *vma)
b62b633e 1351{
b5df0922 1352 return mas_preallocate(&vmi->mas, vma, GFP_KERNEL);
b62b633e
LH
1353}
1354
b5df0922 1355static inline void vma_iter_clear(struct vma_iterator *vmi)
b62b633e 1356{
b62b633e
LH
1357 mas_store_prealloc(&vmi->mas, NULL);
1358}
1359
1360static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi)
1361{
1362 return mas_walk(&vmi->mas);
1363}
1364
1365/* Store a VMA with preallocated memory */
1366static inline void vma_iter_store(struct vma_iterator *vmi,
1367 struct vm_area_struct *vma)
1368{
1369
1370#if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
067311d3 1371 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&
36bd9310
LH
1372 vmi->mas.index > vma->vm_start)) {
1373 pr_warn("%lx > %lx\n store vma %lx-%lx\n into slot %lx-%lx\n",
1374 vmi->mas.index, vma->vm_start, vma->vm_start,
1375 vma->vm_end, vmi->mas.index, vmi->mas.last);
b62b633e 1376 }
067311d3 1377 if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&
36bd9310
LH
1378 vmi->mas.last < vma->vm_start)) {
1379 pr_warn("%lx < %lx\nstore vma %lx-%lx\ninto slot %lx-%lx\n",
1380 vmi->mas.last, vma->vm_start, vma->vm_start, vma->vm_end,
1381 vmi->mas.index, vmi->mas.last);
b62b633e
LH
1382 }
1383#endif
1384
067311d3 1385 if (vmi->mas.status != ma_start &&
b62b633e
LH
1386 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))
1387 vma_iter_invalidate(vmi);
1388
b5df0922 1389 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1);
b62b633e
LH
1390 mas_store_prealloc(&vmi->mas, vma);
1391}
1392
1393static inline int vma_iter_store_gfp(struct vma_iterator *vmi,
1394 struct vm_area_struct *vma, gfp_t gfp)
1395{
067311d3 1396 if (vmi->mas.status != ma_start &&
b62b633e
LH
1397 ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))
1398 vma_iter_invalidate(vmi);
1399
b5df0922 1400 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1);
b62b633e
LH
1401 mas_store_gfp(&vmi->mas, vma, gfp);
1402 if (unlikely(mas_is_err(&vmi->mas)))
1403 return -ENOMEM;
1404
1405 return 0;
1406}
440703e0
LH
1407
1408/*
1409 * VMA lock generalization
1410 */
1411struct vma_prepare {
1412 struct vm_area_struct *vma;
1413 struct vm_area_struct *adj_next;
1414 struct file *file;
1415 struct address_space *mapping;
1416 struct anon_vma *anon_vma;
1417 struct vm_area_struct *insert;
1418 struct vm_area_struct *remove;
1419 struct vm_area_struct *remove2;
1420};
3ee0aa9f 1421
fde1c4ec
UA
1422void __meminit __init_single_page(struct page *page, unsigned long pfn,
1423 unsigned long zone, int nid);
1424
3ee0aa9f 1425/* shrinker related functions */
96f7b2b9
QZ
1426unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,
1427 int priority);
3ee0aa9f
QZ
1428
1429#ifdef CONFIG_SHRINKER_DEBUG
f04eba13
LM
1430static inline __printf(2, 0) int shrinker_debugfs_name_alloc(
1431 struct shrinker *shrinker, const char *fmt, va_list ap)
c42d50ae
QZ
1432{
1433 shrinker->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
1434
1435 return shrinker->name ? 0 : -ENOMEM;
1436}
1437
1438static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
1439{
1440 kfree_const(shrinker->name);
1441 shrinker->name = NULL;
1442}
1443
3ee0aa9f
QZ
1444extern int shrinker_debugfs_add(struct shrinker *shrinker);
1445extern struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
1446 int *debugfs_id);
1447extern void shrinker_debugfs_remove(struct dentry *debugfs_entry,
1448 int debugfs_id);
1449#else /* CONFIG_SHRINKER_DEBUG */
1450static inline int shrinker_debugfs_add(struct shrinker *shrinker)
1451{
1452 return 0;
1453}
c42d50ae
QZ
1454static inline int shrinker_debugfs_name_alloc(struct shrinker *shrinker,
1455 const char *fmt, va_list ap)
1456{
1457 return 0;
1458}
1459static inline void shrinker_debugfs_name_free(struct shrinker *shrinker)
1460{
1461}
3ee0aa9f
QZ
1462static inline struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
1463 int *debugfs_id)
1464{
1465 *debugfs_id = -1;
1466 return NULL;
1467}
1468static inline void shrinker_debugfs_remove(struct dentry *debugfs_entry,
1469 int debugfs_id)
1470{
1471}
1472#endif /* CONFIG_SHRINKER_DEBUG */
1473
b64e74e9
CH
1474/* Only track the nodes of mappings with shadow entries */
1475void workingset_update_node(struct xa_node *node);
1476extern struct list_lru shadow_nodes;
1477
db971418 1478#endif /* __MM_INTERNAL_H */