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