• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern int isolate_lru_page(struct page *page);
231 extern void putback_lru_page(struct page *page);
232 extern unsigned int shrink_page_list(struct list_head *page_list, struct pglist_data *pgdat,
233 		struct scan_control *sc, struct reclaim_stat *stat, bool ignore_references);
234 extern unsigned long isolate_lru_pages(unsigned long nr_to_scan, struct lruvec *lruvec,
235 		struct list_head *dst, unsigned long *nr_scanned, struct scan_control *sc,
236 		enum lru_list lru);
237 extern unsigned move_pages_to_lru(struct lruvec *lruvec, struct list_head *list);
238 extern void shrink_active_list(unsigned long nr_to_scan, struct lruvec *lruvec,
239 		struct scan_control *sc, enum lru_list lru);
240 extern unsigned long shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
241 		struct scan_control *sc, enum lru_list lru);
242 extern void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc);
243 
244 /*
245  * in mm/rmap.c:
246  */
247 extern pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address);
248 
249 /*
250  * in mm/page_alloc.c
251  */
252 
253 /*
254  * Structure for holding the mostly immutable allocation parameters passed
255  * between functions involved in allocations, including the alloc_pages*
256  * family of functions.
257  *
258  * nodemask, migratetype and highest_zoneidx are initialized only once in
259  * __alloc_pages_nodemask() and then never change.
260  *
261  * zonelist, preferred_zone and highest_zoneidx are set first in
262  * __alloc_pages_nodemask() for the fast path, and might be later changed
263  * in __alloc_pages_slowpath(). All other functions pass the whole structure
264  * by a const pointer.
265  */
266 struct alloc_context {
267 	struct zonelist *zonelist;
268 	nodemask_t *nodemask;
269 	struct zoneref *preferred_zoneref;
270 	int migratetype;
271 
272 	/*
273 	 * highest_zoneidx represents highest usable zone index of
274 	 * the allocation request. Due to the nature of the zone,
275 	 * memory on lower zone than the highest_zoneidx will be
276 	 * protected by lowmem_reserve[highest_zoneidx].
277 	 *
278 	 * highest_zoneidx is also used by reclaim/compaction to limit
279 	 * the target zone since higher zone than this index cannot be
280 	 * usable for this allocation request.
281 	 */
282 	enum zone_type highest_zoneidx;
283 	bool spread_dirty_pages;
284 };
285 
286 /*
287  * Locate the struct page for both the matching buddy in our
288  * pair (buddy1) and the combined O(n+1) page they form (page).
289  *
290  * 1) Any buddy B1 will have an order O twin B2 which satisfies
291  * the following equation:
292  *     B2 = B1 ^ (1 << O)
293  * For example, if the starting buddy (buddy2) is #8 its order
294  * 1 buddy is #10:
295  *     B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
296  *
297  * 2) Any buddy B will have an order O+1 parent P which
298  * satisfies the following equation:
299  *     P = B & ~(1 << O)
300  *
301  * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
302  */
303 static inline unsigned long
__find_buddy_pfn(unsigned long page_pfn,unsigned int order)304 __find_buddy_pfn(unsigned long page_pfn, unsigned int order)
305 {
306 	return page_pfn ^ (1 << order);
307 }
308 
309 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
310 				unsigned long end_pfn, struct zone *zone);
311 
pageblock_pfn_to_page(unsigned long start_pfn,unsigned long end_pfn,struct zone * zone)312 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
313 				unsigned long end_pfn, struct zone *zone)
314 {
315 	if (zone->contiguous)
316 		return pfn_to_page(start_pfn);
317 
318 	return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
319 }
320 
321 extern int __isolate_free_page(struct page *page, unsigned int order);
322 extern void __putback_isolated_page(struct page *page, unsigned int order,
323 				    int mt);
324 extern void memblock_free_pages(struct page *page, unsigned long pfn,
325 					unsigned int order);
326 extern void __free_pages_core(struct page *page, unsigned int order);
327 extern void prep_compound_page(struct page *page, unsigned int order);
328 extern void post_alloc_hook(struct page *page, unsigned int order,
329 					gfp_t gfp_flags);
330 extern int user_min_free_kbytes;
331 
332 extern void zone_pcp_update(struct zone *zone);
333 extern void zone_pcp_reset(struct zone *zone);
334 
335 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
336 
337 /*
338  * in mm/compaction.c
339  */
340 /*
341  * compact_control is used to track pages being migrated and the free pages
342  * they are being migrated to during memory compaction. The free_pfn starts
343  * at the end of a zone and migrate_pfn begins at the start. Movable pages
344  * are moved to the end of a zone during a compaction run and the run
345  * completes when free_pfn <= migrate_pfn
346  */
347 struct compact_control {
348 	struct list_head freepages;	/* List of free pages to migrate to */
349 	struct list_head migratepages;	/* List of pages being migrated */
350 	unsigned int nr_freepages;	/* Number of isolated free pages */
351 	unsigned int nr_migratepages;	/* Number of pages to migrate */
352 	unsigned long free_pfn;		/* isolate_freepages search base */
353 	unsigned long migrate_pfn;	/* isolate_migratepages search base */
354 	unsigned long fast_start_pfn;	/* a pfn to start linear scan from */
355 	struct zone *zone;
356 	unsigned long total_migrate_scanned;
357 	unsigned long total_free_scanned;
358 	unsigned short fast_search_fail;/* failures to use free list searches */
359 	short search_order;		/* order to start a fast search at */
360 	const gfp_t gfp_mask;		/* gfp mask of a direct compactor */
361 	int order;			/* order a direct compactor needs */
362 	int migratetype;		/* migratetype of direct compactor */
363 	const unsigned int alloc_flags;	/* alloc flags of a direct compactor */
364 	const int highest_zoneidx;	/* zone index of a direct compactor */
365 	enum migrate_mode mode;		/* Async or sync migration mode */
366 	bool ignore_skip_hint;		/* Scan blocks even if marked skip */
367 	bool no_set_skip_hint;		/* Don't mark blocks for skipping */
368 	bool ignore_block_suitable;	/* Scan blocks considered unsuitable */
369 	bool direct_compaction;		/* False from kcompactd or /proc/... */
370 	bool proactive_compaction;	/* kcompactd proactive compaction */
371 	bool whole_zone;		/* Whole zone should/has been scanned */
372 	bool contended;			/* Signal lock or sched contention */
373 	bool rescan;			/* Rescanning the same pageblock */
374 	bool alloc_contig;		/* alloc_contig_range allocation */
375 };
376 
377 /*
378  * Used in direct compaction when a page should be taken from the freelists
379  * immediately when one is created during the free path.
380  */
381 struct capture_control {
382 	struct compact_control *cc;
383 	struct page *page;
384 };
385 
386 unsigned long
387 isolate_freepages_range(struct compact_control *cc,
388 			unsigned long start_pfn, unsigned long end_pfn);
389 unsigned long
390 isolate_migratepages_range(struct compact_control *cc,
391 			   unsigned long low_pfn, unsigned long end_pfn);
392 int find_suitable_fallback(struct free_area *area, unsigned int order,
393 			int migratetype, bool only_stealable, bool *can_steal);
394 
395 #endif
396 
397 /*
398  * This function returns the order of a free page in the buddy system. In
399  * general, page_zone(page)->lock must be held by the caller to prevent the
400  * page from being allocated in parallel and returning garbage as the order.
401  * If a caller does not hold page_zone(page)->lock, it must guarantee that the
402  * page cannot be allocated or merged in parallel. Alternatively, it must
403  * handle invalid values gracefully, and use buddy_order_unsafe() below.
404  */
buddy_order(struct page * page)405 static inline unsigned int buddy_order(struct page *page)
406 {
407 	/* PageBuddy() must be checked by the caller */
408 	return page_private(page);
409 }
410 
411 /*
412  * Like buddy_order(), but for callers who cannot afford to hold the zone lock.
413  * PageBuddy() should be checked first by the caller to minimize race window,
414  * and invalid values must be handled gracefully.
415  *
416  * READ_ONCE is used so that if the caller assigns the result into a local
417  * variable and e.g. tests it for valid range before using, the compiler cannot
418  * decide to remove the variable and inline the page_private(page) multiple
419  * times, potentially observing different values in the tests and the actual
420  * use of the result.
421  */
422 #define buddy_order_unsafe(page)	READ_ONCE(page_private(page))
423 
is_cow_mapping(vm_flags_t flags)424 static inline bool is_cow_mapping(vm_flags_t flags)
425 {
426 	return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
427 }
428 
429 /*
430  * These three helpers classifies VMAs for virtual memory accounting.
431  */
432 
433 /*
434  * Executable code area - executable, not writable, not stack
435  */
is_exec_mapping(vm_flags_t flags)436 static inline bool is_exec_mapping(vm_flags_t flags)
437 {
438 	return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC;
439 }
440 
441 /*
442  * Stack area - atomatically grows in one direction
443  *
444  * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous:
445  * do_mmap() forbids all other combinations.
446  */
is_stack_mapping(vm_flags_t flags)447 static inline bool is_stack_mapping(vm_flags_t flags)
448 {
449 	return (flags & VM_STACK) == VM_STACK;
450 }
451 
452 /*
453  * Data area - private, writable, not stack
454  */
is_data_mapping(vm_flags_t flags)455 static inline bool is_data_mapping(vm_flags_t flags)
456 {
457 	return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE;
458 }
459 
460 /* mm/util.c */
461 void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
462 		struct vm_area_struct *prev);
463 void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma);
464 
465 #ifdef CONFIG_MMU
466 extern long populate_vma_page_range(struct vm_area_struct *vma,
467 		unsigned long start, unsigned long end, int *nonblocking);
468 extern void munlock_vma_pages_range(struct vm_area_struct *vma,
469 			unsigned long start, unsigned long end);
munlock_vma_pages_all(struct vm_area_struct * vma)470 static inline void munlock_vma_pages_all(struct vm_area_struct *vma)
471 {
472 	munlock_vma_pages_range(vma, vma->vm_start, vma->vm_end);
473 }
474 
475 /*
476  * must be called with vma's mmap_lock held for read or write, and page locked.
477  */
478 extern void mlock_vma_page(struct page *page);
479 extern unsigned int munlock_vma_page(struct page *page);
480 
481 /*
482  * Clear the page's PageMlocked().  This can be useful in a situation where
483  * we want to unconditionally remove a page from the pagecache -- e.g.,
484  * on truncation or freeing.
485  *
486  * It is legal to call this function for any page, mlocked or not.
487  * If called for a page that is still mapped by mlocked vmas, all we do
488  * is revert to lazy LRU behaviour -- semantics are not broken.
489  */
490 extern void clear_page_mlock(struct page *page);
491 
492 /*
493  * mlock_migrate_page - called only from migrate_misplaced_transhuge_page()
494  * (because that does not go through the full procedure of migration ptes):
495  * to migrate the Mlocked page flag; update statistics.
496  */
mlock_migrate_page(struct page * newpage,struct page * page)497 static inline void mlock_migrate_page(struct page *newpage, struct page *page)
498 {
499 	if (TestClearPageMlocked(page)) {
500 		int nr_pages = thp_nr_pages(page);
501 
502 		/* Holding pmd lock, no change in irq context: __mod is safe */
503 		__mod_zone_page_state(page_zone(page), NR_MLOCK, -nr_pages);
504 		SetPageMlocked(newpage);
505 		__mod_zone_page_state(page_zone(newpage), NR_MLOCK, nr_pages);
506 	}
507 }
508 
509 extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma);
510 
511 /*
512  * At what user virtual address is page expected in vma?
513  * Returns -EFAULT if all of the page is outside the range of vma.
514  * If page is a compound head, the entire compound page is considered.
515  */
516 static inline unsigned long
vma_address(struct page * page,struct vm_area_struct * vma)517 vma_address(struct page *page, struct vm_area_struct *vma)
518 {
519 	pgoff_t pgoff;
520 	unsigned long address;
521 
522 	VM_BUG_ON_PAGE(PageKsm(page), page);	/* KSM page->index unusable */
523 	pgoff = page_to_pgoff(page);
524 	if (pgoff >= vma->vm_pgoff) {
525 		address = vma->vm_start +
526 			((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
527 		/* Check for address beyond vma (or wrapped through 0?) */
528 		if (address < vma->vm_start || address >= vma->vm_end)
529 			address = -EFAULT;
530 	} else if (PageHead(page) &&
531 		   pgoff + compound_nr(page) - 1 >= vma->vm_pgoff) {
532 		/* Test above avoids possibility of wrap to 0 on 32-bit */
533 		address = vma->vm_start;
534 	} else {
535 		address = -EFAULT;
536 	}
537 	return address;
538 }
539 
540 /*
541  * Then at what user virtual address will none of the page be found in vma?
542  * Assumes that vma_address() already returned a good starting address.
543  * If page is a compound head, the entire compound page is considered.
544  */
545 static inline unsigned long
vma_address_end(struct page * page,struct vm_area_struct * vma)546 vma_address_end(struct page *page, struct vm_area_struct *vma)
547 {
548 	pgoff_t pgoff;
549 	unsigned long address;
550 
551 	VM_BUG_ON_PAGE(PageKsm(page), page);	/* KSM page->index unusable */
552 	pgoff = page_to_pgoff(page) + compound_nr(page);
553 	address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
554 	/* Check for address beyond vma (or wrapped through 0?) */
555 	if (address < vma->vm_start || address > vma->vm_end)
556 		address = vma->vm_end;
557 	return address;
558 }
559 
maybe_unlock_mmap_for_io(struct vm_fault * vmf,struct file * fpin)560 static inline struct file *maybe_unlock_mmap_for_io(struct vm_fault *vmf,
561 						    struct file *fpin)
562 {
563 	int flags = vmf->flags;
564 
565 	if (fpin)
566 		return fpin;
567 
568 	/*
569 	 * FAULT_FLAG_RETRY_NOWAIT means we don't want to wait on page locks or
570 	 * anything, so we only pin the file and drop the mmap_lock if only
571 	 * FAULT_FLAG_ALLOW_RETRY is set, while this is the first attempt.
572 	 */
573 	if (fault_flag_allow_retry_first(flags) &&
574 	    !(flags & FAULT_FLAG_RETRY_NOWAIT)) {
575 		fpin = get_file(vmf->vma->vm_file);
576 		mmap_read_unlock(vmf->vma->vm_mm);
577 	}
578 	return fpin;
579 }
580 
581 #else /* !CONFIG_MMU */
clear_page_mlock(struct page * page)582 static inline void clear_page_mlock(struct page *page) { }
mlock_vma_page(struct page * page)583 static inline void mlock_vma_page(struct page *page) { }
mlock_migrate_page(struct page * new,struct page * old)584 static inline void mlock_migrate_page(struct page *new, struct page *old) { }
585 
586 #endif /* !CONFIG_MMU */
587 
588 /*
589  * Return the mem_map entry representing the 'offset' subpage within
590  * the maximally aligned gigantic page 'base'.  Handle any discontiguity
591  * in the mem_map at MAX_ORDER_NR_PAGES boundaries.
592  */
mem_map_offset(struct page * base,int offset)593 static inline struct page *mem_map_offset(struct page *base, int offset)
594 {
595 	if (unlikely(offset >= MAX_ORDER_NR_PAGES))
596 		return nth_page(base, offset);
597 	return base + offset;
598 }
599 
600 /*
601  * Iterator over all subpages within the maximally aligned gigantic
602  * page 'base'.  Handle any discontiguity in the mem_map.
603  */
mem_map_next(struct page * iter,struct page * base,int offset)604 static inline struct page *mem_map_next(struct page *iter,
605 						struct page *base, int offset)
606 {
607 	if (unlikely((offset & (MAX_ORDER_NR_PAGES - 1)) == 0)) {
608 		unsigned long pfn = page_to_pfn(base) + offset;
609 		if (!pfn_valid(pfn))
610 			return NULL;
611 		return pfn_to_page(pfn);
612 	}
613 	return iter + 1;
614 }
615 
616 /* Memory initialisation debug and verification */
617 enum mminit_level {
618 	MMINIT_WARNING,
619 	MMINIT_VERIFY,
620 	MMINIT_TRACE
621 };
622 
623 #ifdef CONFIG_DEBUG_MEMORY_INIT
624 
625 extern int mminit_loglevel;
626 
627 #define mminit_dprintk(level, prefix, fmt, arg...) \
628 do { \
629 	if (level < mminit_loglevel) { \
630 		if (level <= MMINIT_WARNING) \
631 			pr_warn("mminit::" prefix " " fmt, ##arg);	\
632 		else \
633 			printk(KERN_DEBUG "mminit::" prefix " " fmt, ##arg); \
634 	} \
635 } while (0)
636 
637 extern void mminit_verify_pageflags_layout(void);
638 extern void mminit_verify_zonelist(void);
639 #else
640 
mminit_dprintk(enum mminit_level level,const char * prefix,const char * fmt,...)641 static inline void mminit_dprintk(enum mminit_level level,
642 				const char *prefix, const char *fmt, ...)
643 {
644 }
645 
mminit_verify_pageflags_layout(void)646 static inline void mminit_verify_pageflags_layout(void)
647 {
648 }
649 
mminit_verify_zonelist(void)650 static inline void mminit_verify_zonelist(void)
651 {
652 }
653 #endif /* CONFIG_DEBUG_MEMORY_INIT */
654 
655 /* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
656 #if defined(CONFIG_SPARSEMEM)
657 extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
658 				unsigned long *end_pfn);
659 #else
mminit_validate_memmodel_limits(unsigned long * start_pfn,unsigned long * end_pfn)660 static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
661 				unsigned long *end_pfn)
662 {
663 }
664 #endif /* CONFIG_SPARSEMEM */
665 
666 #define NODE_RECLAIM_NOSCAN	-2
667 #define NODE_RECLAIM_FULL	-1
668 #define NODE_RECLAIM_SOME	0
669 #define NODE_RECLAIM_SUCCESS	1
670 
671 #ifdef CONFIG_NUMA
672 extern int node_reclaim(struct pglist_data *, gfp_t, unsigned int);
673 #else
node_reclaim(struct pglist_data * pgdat,gfp_t mask,unsigned int order)674 static inline int node_reclaim(struct pglist_data *pgdat, gfp_t mask,
675 				unsigned int order)
676 {
677 	return NODE_RECLAIM_NOSCAN;
678 }
679 #endif
680 
681 extern int hwpoison_filter(struct page *p);
682 
683 extern u32 hwpoison_filter_dev_major;
684 extern u32 hwpoison_filter_dev_minor;
685 extern u64 hwpoison_filter_flags_mask;
686 extern u64 hwpoison_filter_flags_value;
687 extern u64 hwpoison_filter_memcg;
688 extern u32 hwpoison_filter_enable;
689 
690 extern unsigned long  __must_check vm_mmap_pgoff(struct file *, unsigned long,
691         unsigned long, unsigned long,
692         unsigned long, unsigned long);
693 
694 extern void set_pageblock_order(void);
695 unsigned int reclaim_clean_pages_from_list(struct zone *zone,
696 					    struct list_head *page_list);
697 /* The ALLOC_WMARK bits are used as an index to zone->watermark */
698 #define ALLOC_WMARK_MIN		WMARK_MIN
699 #define ALLOC_WMARK_LOW		WMARK_LOW
700 #define ALLOC_WMARK_HIGH	WMARK_HIGH
701 #define ALLOC_NO_WATERMARKS	0x04 /* don't check watermarks at all */
702 
703 /* Mask to get the watermark bits */
704 #define ALLOC_WMARK_MASK	(ALLOC_NO_WATERMARKS-1)
705 
706 /*
707  * Only MMU archs have async oom victim reclaim - aka oom_reaper so we
708  * cannot assume a reduced access to memory reserves is sufficient for
709  * !MMU
710  */
711 #ifdef CONFIG_MMU
712 #define ALLOC_OOM		0x08
713 #else
714 #define ALLOC_OOM		ALLOC_NO_WATERMARKS
715 #endif
716 
717 #define ALLOC_HARDER		 0x10 /* try to alloc harder */
718 #define ALLOC_HIGH		 0x20 /* __GFP_HIGH set */
719 #define ALLOC_CPUSET		 0x40 /* check for correct cpuset */
720 #define ALLOC_CMA		 0x80 /* allow allocations from CMA areas */
721 #ifdef CONFIG_ZONE_DMA32
722 #define ALLOC_NOFRAGMENT	0x100 /* avoid mixing pageblock types */
723 #else
724 #define ALLOC_NOFRAGMENT	  0x0
725 #endif
726 #define ALLOC_KSWAPD		0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */
727 
728 enum ttu_flags;
729 struct tlbflush_unmap_batch;
730 
731 
732 /*
733  * only for MM internal work items which do not depend on
734  * any allocations or locks which might depend on allocations
735  */
736 extern struct workqueue_struct *mm_percpu_wq;
737 
738 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
739 void try_to_unmap_flush(void);
740 void try_to_unmap_flush_dirty(void);
741 void flush_tlb_batched_pending(struct mm_struct *mm);
742 #else
try_to_unmap_flush(void)743 static inline void try_to_unmap_flush(void)
744 {
745 }
try_to_unmap_flush_dirty(void)746 static inline void try_to_unmap_flush_dirty(void)
747 {
748 }
flush_tlb_batched_pending(struct mm_struct * mm)749 static inline void flush_tlb_batched_pending(struct mm_struct *mm)
750 {
751 }
752 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
753 
754 extern const struct trace_print_flags pageflag_names[];
755 extern const struct trace_print_flags vmaflag_names[];
756 extern const struct trace_print_flags gfpflag_names[];
757 
is_migrate_highatomic(enum migratetype migratetype)758 static inline bool is_migrate_highatomic(enum migratetype migratetype)
759 {
760 	return migratetype == MIGRATE_HIGHATOMIC;
761 }
762 
is_migrate_highatomic_page(struct page * page)763 static inline bool is_migrate_highatomic_page(struct page *page)
764 {
765 	return get_pageblock_migratetype(page) == MIGRATE_HIGHATOMIC;
766 }
767 
768 void setup_zone_pageset(struct zone *zone);
769 
770 struct migration_target_control {
771 	int nid;		/* preferred node id */
772 	nodemask_t *nmask;
773 	gfp_t gfp_mask;
774 };
775 
776 #ifdef CONFIG_RECLAIM_ACCT
777 #define DELAY_LV0 5000000 /* 5ms */
778 #define DELAY_LV1 10000000 /* 10ms */
779 #define DELAY_LV2 50000000 /* 50ms */
780 #define DELAY_LV3 100000000 /* 100ms */
781 #define DELAY_LV4 2000000000 /* 2000ms */
782 #define DELAY_LV5 50000000000 /* 50000ms */
783 #define NR_DELAY_LV 6
784 
785 struct reclaim_acct {
786 	u64 start[NR_RA_STUBS];
787 	u64 delay[NR_RA_STUBS];
788 	u64 count[NR_RA_STUBS];
789 	u64 freed[NR_RA_STUBS];
790 	unsigned int reclaim_type;
791 };
792 
793 bool reclaimacct_initialize_show_data(void);
794 void reclaimacct_reinitialize_show_data(void);
795 void reclaimacct_destroy_show_data(void);
796 
797 void reclaimacct_collect_data(void);
798 void reclaimacct_collect_reclaim_efficiency(void);
799 #endif
800 
801 #endif	/* __MM_INTERNAL_H */
802