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