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