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