Linux Audio

Check our new training course

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