xref: /linux/mm/page_alloc.h (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * mm-internal API for the page (buddy) allocator. Public API lives in
4  * include/linux/gfp.h.
5  */
6 #ifndef __MM_PAGE_ALLOC_H
7 #define __MM_PAGE_ALLOC_H
8 
9 #include <linux/mm.h>
10 #include <linux/mmzone.h>
11 #include <linux/nodemask.h>
12 #include <linux/seqlock.h>
13 #include <linux/types.h>
14 
15 #define ALLOC_DEFAULT		0
16 /* The ALLOC_WMARK bits are used as an index to zone->watermark */
17 #define ALLOC_WMARK_MIN		WMARK_MIN
18 #define ALLOC_WMARK_LOW		WMARK_LOW
19 #define ALLOC_WMARK_HIGH	WMARK_HIGH
20 #define ALLOC_NO_WATERMARKS	0x04 /* don't check watermarks at all */
21 
22 /* Mask to get the watermark bits */
23 #define ALLOC_WMARK_MASK	(ALLOC_NO_WATERMARKS-1)
24 
25 /*
26  * Only MMU archs have async oom victim reclaim - aka oom_reaper so we
27  * cannot assume a reduced access to memory reserves is sufficient for
28  * !MMU
29  */
30 #ifdef CONFIG_MMU
31 #define ALLOC_OOM		0x08
32 #else
33 #define ALLOC_OOM		ALLOC_NO_WATERMARKS
34 #endif
35 
36 #define ALLOC_NON_BLOCK		 0x10 /* Caller cannot block. */
37 #define ALLOC_MIN_RESERVE	 0x20 /* __GFP_HIGH set. Allow access to 50%
38 				       * of the min watermark, or 62.5% if
39 				       * the caller cannot block either
40 				       * (ALLOC_NON_BLOCK).
41 				       */
42 #define ALLOC_CPUSET		 0x40 /* check for correct cpuset */
43 #define ALLOC_CMA		 0x80 /* allow allocations from CMA areas */
44 #ifdef CONFIG_ZONE_DMA32
45 #define ALLOC_NOFRAGMENT	0x100 /* avoid mixing pageblock types */
46 #else
47 #define ALLOC_NOFRAGMENT	  0x0
48 #endif
49 #define ALLOC_HIGHATOMIC	0x200 /* Allows access to MIGRATE_HIGHATOMIC */
50 #define ALLOC_NOLOCK		0x400 /* Only use spin_trylock in allocation path */
51 #define ALLOC_KSWAPD		0x800 /* allow waking of kswapd, __GFP_KSWAPD_RECLAIM set */
52 /*
53  * Avoid alloc_tag recursion for internal allocations.
54  *
55  * Callers must clear_page_tag_ref() before freeing to avoid warnings from
56  * alloc_tag_sub_check().
57  */
58 #define ALLOC_NO_CODETAG       0x1000
59 
60 /* Flags that allow allocations below the min watermark. */
61 #define ALLOC_RESERVES (ALLOC_MIN_RESERVE|ALLOC_HIGHATOMIC|ALLOC_OOM)
62 
63 /* Flags that mean GFP_ATOMIC */
64 #define ALLOC_MASK_ATOMIC (ALLOC_NON_BLOCK|ALLOC_MIN_RESERVE)
65 
66 /*
67  * Structure for holding the mostly immutable allocation parameters passed
68  * between functions involved in allocations, including the alloc_pages*
69  * family of functions.
70  *
71  * nodemask, migratetype and highest_zoneidx are initialized only once in
72  * __alloc_pages() and then never change.
73  *
74  * zonelist, preferred_zone and highest_zoneidx are set first in
75  * __alloc_pages() for the fast path, and might be later changed
76  * in __alloc_pages_slowpath(). All other functions pass the whole structure
77  * by a const pointer.
78  */
79 struct alloc_context {
80 	struct zonelist *zonelist;
81 	const nodemask_t *nodemask;
82 	struct zoneref *preferred_zoneref;
83 	int migratetype;
84 
85 	/*
86 	 * highest_zoneidx represents highest usable zone index of
87 	 * the allocation request. Due to the nature of the zone,
88 	 * memory on lower zone than the highest_zoneidx will be
89 	 * protected by lowmem_reserve[highest_zoneidx].
90 	 *
91 	 * highest_zoneidx is also used by reclaim/compaction to limit
92 	 * the target zone since higher zone than this index cannot be
93 	 * usable for this allocation request.
94 	 */
95 	enum zone_type highest_zoneidx;
96 	bool spread_dirty_pages;
97 	/* Only flags that are global to the whole allocation go here. */
98 	unsigned int alloc_flags;
99 	/* Set by __alloc_pages_slowpath() before reclaim and compaction */
100 	unsigned int reclaim_compact_order;
101 };
102 
103 /*
104  * This function returns the order of a free page in the buddy system. In
105  * general, page_zone(page)->lock must be held by the caller to prevent the
106  * page from being allocated in parallel and returning garbage as the order.
107  * If a caller does not hold page_zone(page)->lock, it must guarantee that the
108  * page cannot be allocated or merged in parallel. Alternatively, it must
109  * handle invalid values gracefully, and use buddy_order_unsafe() below.
110  */
111 static inline unsigned int buddy_order(struct page *page)
112 {
113 	/* PageBuddy() must be checked by the caller */
114 	return page_private(page);
115 }
116 
117 /*
118  * Like buddy_order(), but for callers who cannot afford to hold the zone lock.
119  * PageBuddy() should be checked first by the caller to minimize race window,
120  * and invalid values must be handled gracefully.
121  *
122  * READ_ONCE is used so that if the caller assigns the result into a local
123  * variable and e.g. tests it for valid range before using, the compiler cannot
124  * decide to remove the variable and inline the page_private(page) multiple
125  * times, potentially observing different values in the tests and the actual
126  * use of the result.
127  */
128 #define buddy_order_unsafe(page)	READ_ONCE(page_private(page))
129 
130 /*
131  * This function checks whether a page is free && is the buddy
132  * we can coalesce a page and its buddy if
133  * (a) the buddy is not in a hole (check before calling!) &&
134  * (b) the buddy is in the buddy system &&
135  * (c) a page and its buddy have the same order &&
136  * (d) a page and its buddy are in the same zone.
137  *
138  * For recording whether a page is in the buddy system, we set PageBuddy.
139  * Setting, clearing, and testing PageBuddy is serialized by zone->lock.
140  *
141  * For recording page's order, we use page_private(page).
142  */
143 static inline bool page_is_buddy(struct page *page, struct page *buddy,
144 				 unsigned int order)
145 {
146 	if (!page_is_guard(buddy) && !PageBuddy(buddy))
147 		return false;
148 
149 	if (buddy_order(buddy) != order)
150 		return false;
151 
152 	/*
153 	 * zone check is done late to avoid uselessly calculating
154 	 * zone/node ids for pages that could never merge.
155 	 */
156 	if (page_zone_id(page) != page_zone_id(buddy))
157 		return false;
158 
159 	VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
160 
161 	return true;
162 }
163 
164 /*
165  * Locate the struct page for both the matching buddy in our
166  * pair (buddy1) and the combined O(n+1) page they form (page).
167  *
168  * 1) Any buddy B1 will have an order O twin B2 which satisfies
169  * the following equation:
170  *     B2 = B1 ^ (1 << O)
171  * For example, if the starting buddy (buddy2) is #8 its order
172  * 1 buddy is #10:
173  *     B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
174  *
175  * 2) Any buddy B will have an order O+1 parent P which
176  * satisfies the following equation:
177  *     P = B & ~(1 << O)
178  *
179  * Assumption: *_mem_map is contiguous at least up to MAX_PAGE_ORDER
180  */
181 static inline unsigned long
182 __find_buddy_pfn(unsigned long page_pfn, unsigned int order)
183 {
184 	return page_pfn ^ (1 << order);
185 }
186 
187 /*
188  * Find the buddy of @page and validate it.
189  * @page: The input page
190  * @pfn: The pfn of the page, it saves a call to page_to_pfn() when the
191  *       function is used in the performance-critical __free_one_page().
192  * @order: The order of the page
193  * @buddy_pfn: The output pointer to the buddy pfn, it also saves a call to
194  *             page_to_pfn().
195  *
196  * The found buddy can be a non PageBuddy, out of @page's zone, or its order is
197  * not the same as @page. The validation is necessary before use it.
198  *
199  * Return: the found buddy page or NULL if not found.
200  */
201 static inline struct page *find_buddy_page_pfn(struct page *page,
202 			unsigned long pfn, unsigned int order, unsigned long *buddy_pfn)
203 {
204 	unsigned long __buddy_pfn = __find_buddy_pfn(pfn, order);
205 	struct page *buddy;
206 
207 	buddy = page + (__buddy_pfn - pfn);
208 	if (buddy_pfn)
209 		*buddy_pfn = __buddy_pfn;
210 
211 	if (page_is_buddy(page, buddy, order))
212 		return buddy;
213 	return NULL;
214 }
215 
216 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
217 				unsigned long end_pfn, struct zone *zone);
218 
219 static inline struct page *pageblock_pfn_to_page(unsigned long start_pfn,
220 				unsigned long end_pfn, struct zone *zone)
221 {
222 	if (zone_is_contiguous(zone))
223 		return pfn_to_page(start_pfn);
224 
225 	return __pageblock_pfn_to_page(start_pfn, end_pfn, zone);
226 }
227 
228 extern void __free_pages_core(struct page *page, unsigned int order,
229 		enum meminit_context context);
230 
231 void post_alloc_hook(struct page *page, unsigned int order, gfp_t gfp_flags,
232 		     unsigned int alloc_flags);
233 extern bool free_pages_prepare(struct page *page, unsigned int order);
234 
235 extern int user_min_free_kbytes;
236 
237 struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order, int nid,
238 		nodemask_t *nodemask, unsigned int alloc_flags);
239 #define __alloc_frozen_pages(...) \
240 	alloc_hooks(__alloc_frozen_pages_noprof(__VA_ARGS__))
241 void free_frozen_pages(struct page *page, unsigned int order);
242 void free_unref_folios(struct folio_batch *fbatch);
243 
244 #ifdef CONFIG_NUMA
245 struct page *alloc_frozen_pages_noprof(gfp_t, unsigned int order);
246 #else
247 static inline struct page *alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order)
248 {
249 	return __alloc_frozen_pages_noprof(gfp, order, numa_node_id(), NULL,
250 					   ALLOC_DEFAULT);
251 }
252 #endif
253 
254 #define alloc_frozen_pages(...) \
255 	alloc_hooks(alloc_frozen_pages_noprof(__VA_ARGS__))
256 
257 struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order);
258 #define alloc_frozen_pages_nolock(...) \
259 	alloc_hooks(alloc_frozen_pages_nolock_noprof(__VA_ARGS__))
260 void free_frozen_pages_nolock(struct page *page, unsigned int order);
261 
262 struct page *__alloc_pages_noprof(gfp_t gfp, unsigned int order, int preferred_nid,
263 		nodemask_t *nodemask, unsigned int alloc_flags);
264 #define __alloc_pages(...)			alloc_hooks(__alloc_pages_noprof(__VA_ARGS__))
265 
266 extern void zone_pcp_reset(struct zone *zone);
267 extern void zone_pcp_disable(struct zone *zone);
268 extern void zone_pcp_enable(struct zone *zone);
269 extern void zone_pcp_init(struct zone *zone);
270 
271 extern seqlock_t zonelist_update_seq;
272 
273 static inline unsigned int zonelist_iter_begin(void)
274 {
275 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
276 		return read_seqbegin(&zonelist_update_seq);
277 
278 	return 0;
279 }
280 
281 static inline unsigned int check_retry_zonelist(unsigned int seq)
282 {
283 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
284 		return read_seqretry(&zonelist_update_seq, seq);
285 
286 	return 0;
287 }
288 
289 enum fallback_result {
290 	/* Found suitable migratetype, *mt_out is valid. */
291 	FALLBACK_FOUND,
292 	/* No fallback found in requested order. */
293 	FALLBACK_EMPTY,
294 	/* Passed @claimable, but claiming whole block is a bad idea. */
295 	FALLBACK_NOCLAIM,
296 };
297 enum fallback_result
298 find_suitable_fallback(struct free_area *area, unsigned int order,
299 		       int migratetype, bool claimable, int *mt_out);
300 
301 static inline bool free_area_empty(struct free_area *area, int migratetype)
302 {
303 	return list_empty(&area->free_list[migratetype]);
304 }
305 
306 /* Convert GFP flags to their corresponding migrate type */
307 #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
308 #define GFP_MOVABLE_SHIFT 3
309 
310 static inline int gfp_migratetype(const gfp_t gfp_flags)
311 {
312 	VM_WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
313 	BUILD_BUG_ON((1UL << GFP_MOVABLE_SHIFT) != ___GFP_MOVABLE);
314 	BUILD_BUG_ON((___GFP_MOVABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_MOVABLE);
315 	BUILD_BUG_ON((___GFP_RECLAIMABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_RECLAIMABLE);
316 	BUILD_BUG_ON(((___GFP_MOVABLE | ___GFP_RECLAIMABLE) >>
317 		      GFP_MOVABLE_SHIFT) != MIGRATE_HIGHATOMIC);
318 
319 	if (unlikely(page_group_by_mobility_disabled))
320 		return MIGRATE_UNMOVABLE;
321 
322 	/* Group based on mobility */
323 	return (__force unsigned long)(gfp_flags & GFP_MOVABLE_MASK) >> GFP_MOVABLE_SHIFT;
324 }
325 #undef GFP_MOVABLE_MASK
326 #undef GFP_MOVABLE_SHIFT
327 
328 bool decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp);
329 void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
330 void drain_all_pages(struct zone *zone);
331 
332 void page_alloc_init_cpuhp(void);
333 void page_alloc_sysctl_init(void);
334 
335 #endif /* __MM_PAGE_ALLOC_H */
336