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/tracepoint-defs.h>
14 #include <linux/swap.h>
15 #include <linux/rmap.h>
16 #include <linux/types.h>
17 #include <linux/reclaim_acct.h>
18
19 /*
20 * The set of flags that only affect watermark checking and reclaim
21 * behaviour. This is used by the MM to obey the caller constraints
22 * about IO, FS and watermark checking while ignoring placement
23 * hints such as HIGHMEM usage.
24 */
25 #define GFP_RECLAIM_MASK (__GFP_RECLAIM|__GFP_HIGH|__GFP_IO|__GFP_FS|\
26 __GFP_NOWARN|__GFP_RETRY_MAYFAIL|__GFP_NOFAIL|\
27 __GFP_NORETRY|__GFP_MEMALLOC|__GFP_NOMEMALLOC|\
28 __GFP_ATOMIC)
29
30 /* The GFP flags allowed during early boot */
31 #define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_RECLAIM|__GFP_IO|__GFP_FS))
32
33 /* Control allocation cpuset and node placement constraints */
34 #define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
35
36 /* Do not use these with a slab allocator */
37 #define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
38
39 enum reclaim_invoker {
40 ALL,
41 KSWAPD,
42 ZSWAPD,
43 DIRECT_RECLAIM,
44 NODE_RECLAIM,
45 SOFT_LIMIT,
46 RCC_RECLAIM,
47 FILE_RECLAIM,
48 ANON_RECLAIM
49 };
50
51 struct scan_control {
52 /* How many pages shrink_list() should reclaim */
53 unsigned long nr_to_reclaim;
54
55 /*
56 * Nodemask of nodes allowed by the caller. If NULL, all nodes
57 * are scanned.
58 */
59 nodemask_t *nodemask;
60
61 /*
62 * The memory cgroup that hit its limit and as a result is the
63 * primary target of this reclaim invocation.
64 */
65 struct mem_cgroup *target_mem_cgroup;
66
67 /*
68 * Scan pressure balancing between anon and file LRUs
69 */
70 unsigned long anon_cost;
71 unsigned long file_cost;
72
73 /* Can active pages be deactivated as part of reclaim? */
74 #define DEACTIVATE_ANON 1
75 #define DEACTIVATE_FILE 2
76 unsigned int may_deactivate:2;
77 unsigned int force_deactivate:1;
78 unsigned int skipped_deactivate:1;
79
80 /* Writepage batching in laptop mode; RECLAIM_WRITE */
81 unsigned int may_writepage:1;
82
83 /* Can mapped pages be reclaimed? */
84 unsigned int may_unmap:1;
85
86 /* Can pages be swapped as part of reclaim? */
87 unsigned int may_swap:1;
88
89 /*
90 * Cgroups are not reclaimed below their configured memory.low,
91 * unless we threaten to OOM. If any cgroups are skipped due to
92 * memory.low and nothing was reclaimed, go back for memory.low.
93 */
94 unsigned int memcg_low_reclaim:1;
95 unsigned int memcg_low_skipped:1;
96
97 unsigned int hibernation_mode:1;
98
99 /* One of the zones is ready for compaction */
100 unsigned int compaction_ready:1;
101
102 /* There is easily reclaimable cold cache in the current node */
103 unsigned int cache_trim_mode:1;
104
105 /* The file pages on the current node are dangerously low */
106 unsigned int file_is_tiny:1;
107
108 /* Allocation order */
109 s8 order;
110
111 /* Scan (total_size >> priority) pages at once */
112 s8 priority;
113
114 /* The highest zone to isolate pages for reclaim from */
115 s8 reclaim_idx;
116
117 /* This context's GFP mask */
118 gfp_t gfp_mask;
119
120 /* Incremented by the number of inactive pages that were scanned */
121 unsigned long nr_scanned;
122
123 /* Number of pages freed so far during a call to shrink_zones() */
124 unsigned long nr_reclaimed;
125
126 struct {
127 unsigned int dirty;
128 unsigned int unqueued_dirty;
129 unsigned int congested;
130 unsigned int writeback;
131 unsigned int immediate;
132 unsigned int file_taken;
133 unsigned int taken;
134 } nr;
135
136 enum reclaim_invoker invoker;
137 u32 isolate_count;
138 unsigned long nr_scanned_anon;
139 unsigned long nr_scanned_file;
140 unsigned long nr_reclaimed_anon;
141 unsigned long nr_reclaimed_file;
142
143 /* for recording the reclaimed slab by now */
144 struct reclaim_state reclaim_state;
145 };
146
147 enum scan_balance {
148 SCAN_EQUAL,
149 SCAN_FRACT,
150 SCAN_ANON,
151 SCAN_FILE,
152 };
153
154 void page_writeback_init(void);
155
156 vm_fault_t do_swap_page(struct vm_fault *vmf);
157
158 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma,
159 unsigned long floor, unsigned long ceiling);
160
can_madv_lru_vma(struct vm_area_struct * vma)161 static inline bool can_madv_lru_vma(struct vm_area_struct *vma)
162 {
163 return !(vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP));
164 }
165
166 void unmap_page_range(struct mmu_gather *tlb,
167 struct vm_area_struct *vma,
168 unsigned long addr, unsigned long end,
169 struct zap_details *details);
170
171 void do_page_cache_ra(struct readahead_control *, unsigned long nr_to_read,
172 unsigned long lookahead_size);
173 void force_page_cache_ra(struct readahead_control *, struct file_ra_state *,
174 unsigned long nr);
force_page_cache_readahead(struct address_space * mapping,struct file * file,pgoff_t index,unsigned long nr_to_read)175 static inline void force_page_cache_readahead(struct address_space *mapping,
176 struct file *file, pgoff_t index, unsigned long nr_to_read)
177 {
178 DEFINE_READAHEAD(ractl, file, mapping, index);
179 force_page_cache_ra(&ractl, &file->f_ra, nr_to_read);
180 }
181
182 struct page *find_get_entry(struct address_space *mapping, pgoff_t index);
183 struct page *find_lock_entry(struct address_space *mapping, pgoff_t index);
184
185 /**
186 * page_evictable - test whether a page is evictable
187 * @page: the page to test
188 *
189 * Test whether page is evictable--i.e., should be placed on active/inactive
190 * lists vs unevictable list.
191 *
192 * Reasons page might not be evictable:
193 * (1) page's mapping marked unevictable
194 * (2) page is part of an mlocked VMA
195 *
196 */
page_evictable(struct page * page)197 static inline bool page_evictable(struct page *page)
198 {
199 bool ret;
200
201 /* Prevent address_space of inode and swap cache from being freed */
202 rcu_read_lock();
203 ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
204 rcu_read_unlock();
205 return ret;
206 }
207
208 /*
209 * Turn a non-refcounted page (->_refcount == 0) into refcounted with
210 * a count of one.
211 */
set_page_refcounted(struct page * page)212 static inline void set_page_refcounted(struct page *page)
213 {
214 VM_BUG_ON_PAGE(PageTail(page), page);
215 VM_BUG_ON_PAGE(page_ref_count(page), page);
216 set_page_count(page, 1);
217 }
218
219 extern unsigned long highest_memmap_pfn;
220
221 /*
222 * Maximum number of reclaim retries without progress before the OOM
223 * killer is consider the only way forward.
224 */
225 #define MAX_RECLAIM_RETRIES 16
226
227 /*
228 * in mm/vmscan.c:
229 */
230 #ifdef CONFIG_MEMORY_MONITOR
231 extern void kswapd_monitor_wake_up_queue(void);
232 #endif
233 extern int isolate_lru_page(struct page *page);
234 extern void putback_lru_page(struct page *page);
235 extern unsigned int shrink_page_list(struct list_head *page_list, struct pglist_data *pgdat,
236 struct scan_control *sc, struct reclaim_stat *stat, bool ignore_references);
237 extern unsigned long isolate_lru_pages(unsigned long nr_to_scan, struct lruvec *lruvec,
238 struct list_head *dst, unsigned long *nr_scanned, struct scan_control *sc,
239 enum lru_list lru);
240 extern unsigned move_pages_to_lru(struct lruvec *lruvec, struct list_head *list);
241 extern void shrink_active_list(unsigned long nr_to_scan, struct lruvec *lruvec,
242 struct scan_control *sc, enum lru_list lru);
243 extern unsigned long shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
244 struct scan_control *sc, enum lru_list lru);
245 extern void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc);
246
247 /*
248 * in mm/rmap.c:
249 */
250 extern pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
251
252 /*
253 * in mm/page_alloc.c
254 */
255
256 /*
257 * Structure for holding the mostly immutable allocation parameters passed
258 * between functions involved in allocations, including the alloc_pages*
259 * family of functions.
260 *
261 * nodemask, migratetype and highest_zoneidx are initialized only once in
262 * __alloc_pages_nodemask() and then never change.
263 *
264 * zonelist, preferred_zone and highest_zoneidx are set first in
265 * __alloc_pages_nodemask() for the fast path, and might be later changed
266 * in __alloc_pages_slowpath(). All other functions pass the whole structure
267 * by a const pointer.
268 */
269 struct alloc_context {
270 struct zonelist *zonelist;
271 nodemask_t *nodemask;
272 struct zoneref *preferred_zoneref;
273 int migratetype;
274
275 /*
276 * highest_zoneidx represents highest usable zone index of
277 * the allocation request. Due to the nature of the zone,
278 * memory on lower zone than the highest_zoneidx will be
279 * protected by lowmem_reserve[highest_zoneidx].
280 *
281 * highest_zoneidx is also used by reclaim/compaction to limit
282 * the target zone since higher zone than this index cannot be
283 * usable for this allocation request.
284 */
285 enum zone_type highest_zoneidx;
286 bool spread_dirty_pages;
287 };
288
289 /*
290 * Locate the struct page for both the matching buddy in our
291 * pair (buddy1) and the combined O(n+1) page they form (page).
292 *
293 * 1) Any buddy B1 will have an order O twin B2 which satisfies
294 * the following equation:
295 * B2 = B1 ^ (1 << O)
296 * For example, if the starting buddy (buddy2) is #8 its order
297 * 1 buddy is #10:
298 * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
299 *
300 * 2) Any buddy B will have an order O+1 parent P which
301 * satisfies the following equation:
302 * P = B & ~(1 << O)
303 *
304 * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
305 */
306 static inline unsigned long
__find_buddy_pfn(unsigned long page_pfn,unsigned int order)307 __find_buddy_pfn(unsigned long page_pfn, unsigned int order)
308 {
309 return page_pfn ^ (1 << order);
310 }
311
312 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
313 unsigned long end_pfn, struct zone *zone);
314
pageblock_pfn_to_page(unsigned long start_pfn,unsigned long end_pfn,struct zone * zone)315 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
316 unsigned long end_pfn, struct zone *zone)
317 {
318 if (zone->contiguous)
319 return pfn_to_page(start_pfn);
320
321 return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
322 }
323
324 extern int __isolate_free_page(struct page *page, unsigned int order);
325 extern void __putback_isolated_page(struct page *page, unsigned int order,
326 int mt);
327 extern void memblock_free_pages(struct page *page, unsigned long pfn,
328 unsigned int order);
329 extern void __free_pages_core(struct page *page, unsigned int order);
330 extern void prep_compound_page(struct page *page, unsigned int order);
331 extern void post_alloc_hook(struct page *page, unsigned int order,
332 gfp_t gfp_flags);
333 extern int user_min_free_kbytes;
334
335 extern void zone_pcp_update(struct zone *zone);
336 extern void zone_pcp_reset(struct zone *zone);
337
338 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
339
340 /*
341 * in mm/compaction.c
342 */
343 /*
344 * compact_control is used to track pages being migrated and the free pages
345 * they are being migrated to during memory compaction. The free_pfn starts
346 * at the end of a zone and migrate_pfn begins at the start. Movable pages
347 * are moved to the end of a zone during a compaction run and the run
348 * completes when free_pfn <= migrate_pfn
349 */
350 struct compact_control {
351 struct list_head freepages; /* List of free pages to migrate to */
352 struct list_head migratepages; /* List of pages being migrated */
353 unsigned int nr_freepages; /* Number of isolated free pages */
354 unsigned int nr_migratepages; /* Number of pages to migrate */
355 unsigned long free_pfn; /* isolate_freepages search base */
356 unsigned long migrate_pfn; /* isolate_migratepages search base */
357 unsigned long fast_start_pfn; /* a pfn to start linear scan from */
358 struct zone *zone;
359 unsigned long total_migrate_scanned;
360 unsigned long total_free_scanned;
361 unsigned short fast_search_fail;/* failures to use free list searches */
362 short search_order; /* order to start a fast search at */
363 const gfp_t gfp_mask; /* gfp mask of a direct compactor */
364 int order; /* order a direct compactor needs */
365 int migratetype; /* migratetype of direct compactor */
366 const unsigned int alloc_flags; /* alloc flags of a direct compactor */
367 const int highest_zoneidx; /* zone index of a direct compactor */
368 enum migrate_mode mode; /* Async or sync migration mode */
369 bool ignore_skip_hint; /* Scan blocks even if marked skip */
370 bool no_set_skip_hint; /* Don't mark blocks for skipping */
371 bool ignore_block_suitable; /* Scan blocks considered unsuitable */
372 bool direct_compaction; /* False from kcompactd or /proc/... */
373 bool proactive_compaction; /* kcompactd proactive compaction */
374 bool whole_zone; /* Whole zone should/has been scanned */
375 bool contended; /* Signal lock or sched contention */
376 bool rescan; /* Rescanning the same pageblock */
377 bool alloc_contig; /* alloc_contig_range allocation */
378 };
379
380 /*
381 * Used in direct compaction when a page should be taken from the freelists
382 * immediately when one is created during the free path.
383 */
384 struct capture_control {
385 struct compact_control *cc;
386 struct page *page;
387 };
388
389 unsigned long
390 isolate_freepages_range(struct compact_control *cc,
391 unsigned long start_pfn, unsigned long end_pfn);
392 unsigned long
393 isolate_migratepages_range(struct compact_control *cc,
394 unsigned long low_pfn, unsigned long end_pfn);
395 int find_suitable_fallback(struct free_area *area, unsigned int order,
396 int migratetype, bool only_stealable, bool *can_steal);
397
398 #endif
399
400 /*
401 * This function returns the order of a free page in the buddy system. In
402 * general, page_zone(page)->lock must be held by the caller to prevent the
403 * page from being allocated in parallel and returning garbage as the order.
404 * If a caller does not hold page_zone(page)->lock, it must guarantee that the
405 * page cannot be allocated or merged in parallel. Alternatively, it must
406 * handle invalid values gracefully, and use buddy_order_unsafe() below.
407 */
buddy_order(struct page * page)408 static inline unsigned int buddy_order(struct page *page)
409 {
410 /* PageBuddy() must be checked by the caller */
411 return page_private(page);
412 }
413
414 /*
415 * Like buddy_order(), but for callers who cannot afford to hold the zone lock.
416 * PageBuddy() should be checked first by the caller to minimize race window,
417 * and invalid values must be handled gracefully.
418 *
419 * READ_ONCE is used so that if the caller assigns the result into a local
420 * variable and e.g. tests it for valid range before using, the compiler cannot
421 * decide to remove the variable and inline the page_private(page) multiple
422 * times, potentially observing different values in the tests and the actual
423 * use of the result.
424 */
425 #define buddy_order_unsafe(page) READ_ONCE(page_private(page))
426
is_cow_mapping(vm_flags_t flags)427 static inline bool is_cow_mapping(vm_flags_t flags)
428 {
429 return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
430 }
431
432 /*
433 * These three helpers classifies VMAs for virtual memory accounting.
434 */
435
436 /*
437 * Executable code area - executable, not writable, not stack
438 */
is_exec_mapping(vm_flags_t flags)439 static inline bool is_exec_mapping(vm_flags_t flags)
440 {
441 return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC;
442 }
443
444 /*
445 * Stack area - atomatically grows in one direction
446 *
447 * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous:
448 * do_mmap() forbids all other combinations.
449 */
is_stack_mapping(vm_flags_t flags)450 static inline bool is_stack_mapping(vm_flags_t flags)
451 {
452 return (flags & VM_STACK) == VM_STACK;
453 }
454
455 /*
456 * Data area - private, writable, not stack
457 */
is_data_mapping(vm_flags_t flags)458 static inline bool is_data_mapping(vm_flags_t flags)
459 {
460 return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE;
461 }
462
463 /* mm/util.c */
464 void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
465 struct vm_area_struct *prev);
466 void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma);
467
468 #ifdef CONFIG_MMU
469 extern long populate_vma_page_range(struct vm_area_struct *vma,
470 unsigned long start, unsigned long end, int *nonblocking);
471 extern void munlock_vma_pages_range(struct vm_area_struct *vma,
472 unsigned long start, unsigned long end);
munlock_vma_pages_all(struct vm_area_struct * vma)473 static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
474 {
475 munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
476 }
477
478 /*
479 * must be called with vma's mmap_lock held for read or write, and page locked.
480 */
481 extern void mlock_vma_page(struct page *page);
482 extern unsigned int munlock_vma_page(struct page *page);
483
484 /*
485 * Clear the page's PageMlocked(). This can be useful in a situation where
486 * we want to unconditionally remove a page from the pagecache -- e.g.,
487 * on truncation or freeing.
488 *
489 * It is legal to call this function for any page, mlocked or not.
490 * If called for a page that is still mapped by mlocked vmas, all we do
491 * is revert to lazy LRU behaviour -- semantics are not broken.
492 */
493 extern void clear_page_mlock(struct page *page);
494
495 /*
496 * mlock_migrate_page - called only from migrate_misplaced_transhuge_page()
497 * (because that does not go through the full procedure of migration ptes):
498 * to migrate the Mlocked page flag; update statistics.
499 */
mlock_migrate_page(struct page * newpage,struct page * page)500 static inline void mlock_migrate_page(struct page *newpage, struct page *page)
501 {
502 if (TestClearPageMlocked(page)) {
503 int nr_pages = thp_nr_pages(page);
504
505 /* Holding pmd lock, no change in irq context: __mod is safe */
506 __mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
507 SetPageMlocked(newpage);
508 __mod_zone_page_state(page_zone(newpage), NR_MLOCK, nr_pages);
509 }
510 }
511
512 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
513
514 /*
515 * At what user virtual address is page expected in vma?
516 * Returns -EFAULT if all of the page is outside the range of vma.
517 * If page is a compound head, the entire compound page is considered.
518 */
519 static inline unsigned long
vma_address(struct page * page,struct vm_area_struct * vma)520 vma_address(struct page *page, struct vm_area_struct *vma)
521 {
522 pgoff_t pgoff;
523 unsigned long address;
524
525 VM_BUG_ON_PAGE(PageKsm(page), page); /* KSM page->index unusable */
526 pgoff = page_to_pgoff(page);
527 if (pgoff >= vma->vm_pgoff) {
528 address = vma->vm_start +
529 ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
530 /* Check for address beyond vma (or wrapped through 0?) */
531 if (address < vma->vm_start || address >= vma->vm_end)
532 address = -EFAULT;
533 } else if (PageHead(page) &&
534 pgoff + compound_nr(page) - 1 >= vma->vm_pgoff) {
535 /* Test above avoids possibility of wrap to 0 on 32-bit */
536 address = vma->vm_start;
537 } else {
538 address = -EFAULT;
539 }
540 return address;
541 }
542
543 /*
544 * Then at what user virtual address will none of the page be found in vma?
545 * Assumes that vma_address() already returned a good starting address.
546 * If page is a compound head, the entire compound page is considered.
547 */
548 static inline unsigned long
vma_address_end(struct page * page,struct vm_area_struct * vma)549 vma_address_end(struct page *page, struct vm_area_struct *vma)
550 {
551 pgoff_t pgoff;
552 unsigned long address;
553
554 VM_BUG_ON_PAGE(PageKsm(page), page); /* KSM page->index unusable */
555 pgoff = page_to_pgoff(page) + compound_nr(page);
556 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
557 /* Check for address beyond vma (or wrapped through 0?) */
558 if (address < vma->vm_start || address > vma->vm_end)
559 address = vma->vm_end;
560 return address;
561 }
562
maybe_unlock_mmap_for_io(struct vm_fault * vmf,struct file * fpin)563 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
564 struct file *fpin)
565 {
566 int flags = vmf->flags;
567
568 if (fpin)
569 return fpin;
570
571 /*
572 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
573 * anything, so we only pin the file and drop the mmap_lock if only
574 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt.
575 */
576 if (fault_flag_allow_retry_first(flags) &&
577 !(flags & FAULT_FLAG_RETRY_NOWAIT)) {
578 fpin = get_file(vmf->vma->vm_file);
579 mmap_read_unlock(vmf->vma->vm_mm);
580 }
581 return fpin;
582 }
583
584 #else /* !CONFIG_MMU */
clear_page_mlock(struct page * page)585 static inline void clear_page_mlock(struct page *page) { }
mlock_vma_page(struct page * page)586 static inline void mlock_vma_page(struct page *page) { }
mlock_migrate_page(struct page * new,struct page * old)587 static inline void mlock_migrate_page(struct page *new, struct page *old) { }
588
589 #endif /* !CONFIG_MMU */
590
591 /*
592 * Return the mem_map entry representing the 'offset' subpage within
593 * the maximally aligned gigantic page 'base'. Handle any discontiguity
594 * in the mem_map at MAX_ORDER_NR_PAGES boundaries.
595 */
mem_map_offset(struct page * base,int offset)596 static inline struct page *mem_map_offset(struct page *base, int offset)
597 {
598 if (unlikely(offset >= MAX_ORDER_NR_PAGES))
599 return nth_page(base, offset);
600 return base + offset;
601 }
602
603 /*
604 * Iterator over all subpages within the maximally aligned gigantic
605 * page 'base'. Handle any discontiguity in the mem_map.
606 */
mem_map_next(struct page * iter,struct page * base,int offset)607 static inline struct page *mem_map_next(struct page *iter,
608 struct page *base, int offset)
609 {
610 if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
611 unsigned long pfn = page_to_pfn(base) + offset;
612 if (!pfn_valid(pfn))
613 return NULL;
614 return pfn_to_page(pfn);
615 }
616 return iter + 1;
617 }
618
619 /* Memory initialisation debug and verification */
620 enum mminit_level {
621 MMINIT_WARNING,
622 MMINIT_VERIFY,
623 MMINIT_TRACE
624 };
625
626 #ifdef CONFIG_DEBUG_MEMORY_INIT
627
628 extern int mminit_loglevel;
629
630 #define mminit_dprintk(level, prefix, fmt, arg...) \
631 do { \
632 if (level < mminit_loglevel) { \
633 if (level <= MMINIT_WARNING) \
634 pr_warn("mminit::" prefix " " fmt, ##arg); \
635 else \
636 printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \
637 } \
638 } while (0)
639
640 extern void mminit_verify_pageflags_layout(void);
641 extern void mminit_verify_zonelist(void);
642 #else
643
mminit_dprintk(enum mminit_level level,const char * prefix,const char * fmt,...)644 static inline void mminit_dprintk(enum mminit_level level,
645 const char *prefix, const char *fmt, ...)
646 {
647 }
648
mminit_verify_pageflags_layout(void)649 static inline void mminit_verify_pageflags_layout(void)
650 {
651 }
652
mminit_verify_zonelist(void)653 static inline void mminit_verify_zonelist(void)
654 {
655 }
656 #endif /* CONFIG_DEBUG_MEMORY_INIT */
657
658 /* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
659 #if defined(CONFIG_SPARSEMEM)
660 extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
661 unsigned long *end_pfn);
662 #else
mminit_validate_memmodel_limits(unsigned long * start_pfn,unsigned long * end_pfn)663 static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
664 unsigned long *end_pfn)
665 {
666 }
667 #endif /* CONFIG_SPARSEMEM */
668
669 #define NODE_RECLAIM_NOSCAN -2
670 #define NODE_RECLAIM_FULL -1
671 #define NODE_RECLAIM_SOME 0
672 #define NODE_RECLAIM_SUCCESS 1
673
674 #ifdef CONFIG_NUMA
675 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int);
676 #else
node_reclaim(struct pglist_data * pgdat,gfp_t mask,unsigned int order)677 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask,
678 unsigned int order)
679 {
680 return NODE_RECLAIM_NOSCAN;
681 }
682 #endif
683
684 extern int hwpoison_filter(struct page *p);
685
686 extern u32 hwpoison_filter_dev_major;
687 extern u32 hwpoison_filter_dev_minor;
688 extern u64 hwpoison_filter_flags_mask;
689 extern u64 hwpoison_filter_flags_value;
690 extern u64 hwpoison_filter_memcg;
691 extern u32 hwpoison_filter_enable;
692
693 extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long,
694 unsigned long, unsigned long,
695 unsigned long, unsigned long);
696
697 extern void set_pageblock_order(void);
698 unsigned int reclaim_clean_pages_from_list(struct zone *zone,
699 struct list_head *page_list);
700 /* The ALLOC_WMARK bits are used as an index to zone->watermark */
701 #define ALLOC_WMARK_MIN WMARK_MIN
702 #define ALLOC_WMARK_LOW WMARK_LOW
703 #define ALLOC_WMARK_HIGH WMARK_HIGH
704 #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
705
706 /* Mask to get the watermark bits */
707 #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
708
709 /*
710 * Only MMU archs have async oom victim reclaim - aka oom_reaper so we
711 * cannot assume a reduced access to memory reserves is sufficient for
712 * !MMU
713 */
714 #ifdef CONFIG_MMU
715 #define ALLOC_OOM 0x08
716 #else
717 #define ALLOC_OOM ALLOC_NO_WATERMARKS
718 #endif
719
720 #define ALLOC_HARDER 0x10 /* try to alloc harder */
721 #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
722 #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
723 #define ALLOC_CMA 0x80 /* allow allocations from CMA areas */
724 #ifdef CONFIG_ZONE_DMA32
725 #define ALLOC_NOFRAGMENT 0x100 /* avoid mixing pageblock types */
726 #else
727 #define ALLOC_NOFRAGMENT 0x0
728 #endif
729 #define ALLOC_KSWAPD 0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */
730
731 enum ttu_flags;
732 struct tlbflush_unmap_batch;
733
734
735 /*
736 * only for MM internal work items which do not depend on
737 * any allocations or locks which might depend on allocations
738 */
739 extern struct workqueue_struct *mm_percpu_wq;
740
741 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
742 void try_to_unmap_flush(void);
743 void try_to_unmap_flush_dirty(void);
744 void flush_tlb_batched_pending(struct mm_struct *mm);
745 #else
try_to_unmap_flush(void)746 static inline void try_to_unmap_flush(void)
747 {
748 }
try_to_unmap_flush_dirty(void)749 static inline void try_to_unmap_flush_dirty(void)
750 {
751 }
flush_tlb_batched_pending(struct mm_struct * mm)752 static inline void flush_tlb_batched_pending(struct mm_struct *mm)
753 {
754 }
755 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
756
757 extern const struct trace_print_flags pageflag_names[];
758 extern const struct trace_print_flags vmaflag_names[];
759 extern const struct trace_print_flags gfpflag_names[];
760
is_migrate_highatomic(enum migratetype migratetype)761 static inline bool is_migrate_highatomic(enum migratetype migratetype)
762 {
763 return migratetype == MIGRATE_HIGHATOMIC;
764 }
765
is_migrate_highatomic_page(struct page * page)766 static inline bool is_migrate_highatomic_page(struct page *page)
767 {
768 return get_pageblock_migratetype(page) == MIGRATE_HIGHATOMIC;
769 }
770
771 void setup_zone_pageset(struct zone *zone);
772
773 struct migration_target_control {
774 int nid; /* preferred node id */
775 nodemask_t *nmask;
776 gfp_t gfp_mask;
777 };
778
779 #ifdef CONFIG_RECLAIM_ACCT
780 #define DELAY_LV0 5000000 /* 5ms */
781 #define DELAY_LV1 10000000 /* 10ms */
782 #define DELAY_LV2 50000000 /* 50ms */
783 #define DELAY_LV3 100000000 /* 100ms */
784 #define DELAY_LV4 2000000000 /* 2000ms */
785 #define DELAY_LV5 50000000000 /* 50000ms */
786 #define NR_DELAY_LV 6
787
788 struct reclaim_acct {
789 u64 start[NR_RA_STUBS];
790 u64 delay[NR_RA_STUBS];
791 u64 count[NR_RA_STUBS];
792 u64 freed[NR_RA_STUBS];
793 unsigned int reclaim_type;
794 };
795
796 bool reclaimacct_initialize_show_data(void);
797 void reclaimacct_reinitialize_show_data(void);
798 void reclaimacct_destroy_show_data(void);
799
800 void reclaimacct_collect_data(void);
801 void reclaimacct_collect_reclaim_efficiency(void);
802 #endif
803
804 #endif /* __MM_INTERNAL_H */
805