1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_GFP_H
3 #define __LINUX_GFP_H
4
5 #include <linux/gfp_types.h>
6
7 #include <linux/mmzone.h>
8 #include <linux/topology.h>
9 #include <linux/alloc_tag.h>
10 #include <linux/sched.h>
11
12 struct vm_area_struct;
13 struct mempolicy;
14
15 /* Convert GFP flags to their corresponding migrate type */
16 #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
17 #define GFP_MOVABLE_SHIFT 3
18
gfp_migratetype(const gfp_t gfp_flags)19 static inline int gfp_migratetype(const gfp_t gfp_flags)
20 {
21 VM_WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
22 BUILD_BUG_ON((1UL << GFP_MOVABLE_SHIFT) != ___GFP_MOVABLE);
23 BUILD_BUG_ON((___GFP_MOVABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_MOVABLE);
24 BUILD_BUG_ON((___GFP_RECLAIMABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_RECLAIMABLE);
25 BUILD_BUG_ON(((___GFP_MOVABLE | ___GFP_RECLAIMABLE) >>
26 GFP_MOVABLE_SHIFT) != MIGRATE_HIGHATOMIC);
27
28 if (unlikely(page_group_by_mobility_disabled))
29 return MIGRATE_UNMOVABLE;
30
31 /* Group based on mobility */
32 return (__force unsigned long)(gfp_flags & GFP_MOVABLE_MASK) >> GFP_MOVABLE_SHIFT;
33 }
34 #undef GFP_MOVABLE_MASK
35 #undef GFP_MOVABLE_SHIFT
36
gfpflags_allow_blocking(const gfp_t gfp_flags)37 static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags)
38 {
39 return !!(gfp_flags & __GFP_DIRECT_RECLAIM);
40 }
41
gfpflags_allow_spinning(const gfp_t gfp_flags)42 static inline bool gfpflags_allow_spinning(const gfp_t gfp_flags)
43 {
44 /*
45 * !__GFP_DIRECT_RECLAIM -> direct claim is not allowed.
46 * !__GFP_KSWAPD_RECLAIM -> it's not safe to wake up kswapd.
47 * All GFP_* flags including GFP_NOWAIT use one or both flags.
48 * alloc_pages_nolock() is the only API that doesn't specify either flag.
49 *
50 * This is stronger than GFP_NOWAIT or GFP_ATOMIC because
51 * those are guaranteed to never block on a sleeping lock.
52 * Here we are enforcing that the allocation doesn't ever spin
53 * on any locks (i.e. only trylocks). There is no high level
54 * GFP_$FOO flag for this use in alloc_pages_nolock() as the
55 * regular page allocator doesn't fully support this
56 * allocation mode.
57 */
58 return !!(gfp_flags & __GFP_RECLAIM);
59 }
60
61 #ifdef CONFIG_HIGHMEM
62 #define OPT_ZONE_HIGHMEM ZONE_HIGHMEM
63 #else
64 #define OPT_ZONE_HIGHMEM ZONE_NORMAL
65 #endif
66
67 #ifdef CONFIG_ZONE_DMA
68 #define OPT_ZONE_DMA ZONE_DMA
69 #else
70 #define OPT_ZONE_DMA ZONE_NORMAL
71 #endif
72
73 #ifdef CONFIG_ZONE_DMA32
74 #define OPT_ZONE_DMA32 ZONE_DMA32
75 #else
76 #define OPT_ZONE_DMA32 ZONE_NORMAL
77 #endif
78
79 /*
80 * GFP_ZONE_TABLE is a word size bitstring that is used for looking up the
81 * zone to use given the lowest 4 bits of gfp_t. Entries are GFP_ZONES_SHIFT
82 * bits long and there are 16 of them to cover all possible combinations of
83 * __GFP_DMA, __GFP_DMA32, __GFP_MOVABLE and __GFP_HIGHMEM.
84 *
85 * The zone fallback order is MOVABLE=>HIGHMEM=>NORMAL=>DMA32=>DMA.
86 * But GFP_MOVABLE is not only a zone specifier but also an allocation
87 * policy. Therefore __GFP_MOVABLE plus another zone selector is valid.
88 * Only 1 bit of the lowest 3 bits (DMA,DMA32,HIGHMEM) can be set to "1".
89 *
90 * bit result
91 * =================
92 * 0x0 => NORMAL
93 * 0x1 => DMA or NORMAL
94 * 0x2 => HIGHMEM or NORMAL
95 * 0x3 => BAD (DMA+HIGHMEM)
96 * 0x4 => DMA32 or NORMAL
97 * 0x5 => BAD (DMA+DMA32)
98 * 0x6 => BAD (HIGHMEM+DMA32)
99 * 0x7 => BAD (HIGHMEM+DMA32+DMA)
100 * 0x8 => NORMAL (MOVABLE+0)
101 * 0x9 => DMA or NORMAL (MOVABLE+DMA)
102 * 0xa => MOVABLE (Movable is valid only if HIGHMEM is set too)
103 * 0xb => BAD (MOVABLE+HIGHMEM+DMA)
104 * 0xc => DMA32 or NORMAL (MOVABLE+DMA32)
105 * 0xd => BAD (MOVABLE+DMA32+DMA)
106 * 0xe => BAD (MOVABLE+DMA32+HIGHMEM)
107 * 0xf => BAD (MOVABLE+DMA32+HIGHMEM+DMA)
108 *
109 * GFP_ZONES_SHIFT must be <= 2 on 32 bit platforms.
110 */
111
112 #if defined(CONFIG_ZONE_DEVICE) && (MAX_NR_ZONES-1) <= 4
113 /* ZONE_DEVICE is not a valid GFP zone specifier */
114 #define GFP_ZONES_SHIFT 2
115 #else
116 #define GFP_ZONES_SHIFT ZONES_SHIFT
117 #endif
118
119 #if 16 * GFP_ZONES_SHIFT > BITS_PER_LONG
120 #error GFP_ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
121 #endif
122
123 #define GFP_ZONE_TABLE ( \
124 (ZONE_NORMAL << 0 * GFP_ZONES_SHIFT) \
125 | (OPT_ZONE_DMA << ___GFP_DMA * GFP_ZONES_SHIFT) \
126 | (OPT_ZONE_HIGHMEM << ___GFP_HIGHMEM * GFP_ZONES_SHIFT) \
127 | (OPT_ZONE_DMA32 << ___GFP_DMA32 * GFP_ZONES_SHIFT) \
128 | (ZONE_NORMAL << ___GFP_MOVABLE * GFP_ZONES_SHIFT) \
129 | (OPT_ZONE_DMA << (___GFP_MOVABLE | ___GFP_DMA) * GFP_ZONES_SHIFT) \
130 | (ZONE_MOVABLE << (___GFP_MOVABLE | ___GFP_HIGHMEM) * GFP_ZONES_SHIFT)\
131 | (OPT_ZONE_DMA32 << (___GFP_MOVABLE | ___GFP_DMA32) * GFP_ZONES_SHIFT)\
132 )
133
134 /*
135 * GFP_ZONE_BAD is a bitmap for all combinations of __GFP_DMA, __GFP_DMA32
136 * __GFP_HIGHMEM and __GFP_MOVABLE that are not permitted. One flag per
137 * entry starting with bit 0. Bit is set if the combination is not
138 * allowed.
139 */
140 #define GFP_ZONE_BAD ( \
141 1 << (___GFP_DMA | ___GFP_HIGHMEM) \
142 | 1 << (___GFP_DMA | ___GFP_DMA32) \
143 | 1 << (___GFP_DMA32 | ___GFP_HIGHMEM) \
144 | 1 << (___GFP_DMA | ___GFP_DMA32 | ___GFP_HIGHMEM) \
145 | 1 << (___GFP_MOVABLE | ___GFP_HIGHMEM | ___GFP_DMA) \
146 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA) \
147 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_HIGHMEM) \
148 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA | ___GFP_HIGHMEM) \
149 )
150
gfp_zone(gfp_t flags)151 static inline enum zone_type gfp_zone(gfp_t flags)
152 {
153 enum zone_type z;
154 int bit = (__force int) (flags & GFP_ZONEMASK);
155
156 z = (GFP_ZONE_TABLE >> (bit * GFP_ZONES_SHIFT)) &
157 ((1 << GFP_ZONES_SHIFT) - 1);
158 VM_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
159 return z;
160 }
161
162 /*
163 * There is only one page-allocator function, and two main namespaces to
164 * it. The alloc_page*() variants return 'struct page *' and as such
165 * can allocate highmem pages, the *get*page*() variants return
166 * virtual kernel addresses to the allocated page(s).
167 */
168
gfp_zonelist(gfp_t flags)169 static inline int gfp_zonelist(gfp_t flags)
170 {
171 #ifdef CONFIG_NUMA
172 if (unlikely(flags & __GFP_THISNODE))
173 return ZONELIST_NOFALLBACK;
174 #endif
175 return ZONELIST_FALLBACK;
176 }
177
178 /*
179 * gfp flag masking for nested internal allocations.
180 *
181 * For code that needs to do allocations inside the public allocation API (e.g.
182 * memory allocation tracking code) the allocations need to obey the caller
183 * allocation context constrains to prevent allocation context mismatches (e.g.
184 * GFP_KERNEL allocations in GFP_NOFS contexts) from potential deadlock
185 * situations.
186 *
187 * It is also assumed that these nested allocations are for internal kernel
188 * object storage purposes only and are not going to be used for DMA, etc. Hence
189 * we strip out all the zone information and leave just the context information
190 * intact.
191 *
192 * Further, internal allocations must fail before the higher level allocation
193 * can fail, so we must make them fail faster and fail silently. We also don't
194 * want them to deplete emergency reserves. Hence nested allocations must be
195 * prepared for these allocations to fail.
196 */
gfp_nested_mask(gfp_t flags)197 static inline gfp_t gfp_nested_mask(gfp_t flags)
198 {
199 return ((flags & (GFP_KERNEL | GFP_ATOMIC | __GFP_NOLOCKDEP)) |
200 (__GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN));
201 }
202
203 /*
204 * We get the zone list from the current node and the gfp_mask.
205 * This zone list contains a maximum of MAX_NUMNODES*MAX_NR_ZONES zones.
206 * There are two zonelists per node, one for all zones with memory and
207 * one containing just zones from the node the zonelist belongs to.
208 *
209 * For the case of non-NUMA systems the NODE_DATA() gets optimized to
210 * &contig_page_data at compile-time.
211 */
node_zonelist(int nid,gfp_t flags)212 static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
213 {
214 return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
215 }
216
217 #ifndef HAVE_ARCH_FREE_PAGE
arch_free_page(struct page * page,int order)218 static inline void arch_free_page(struct page *page, int order) { }
219 #endif
220 #ifndef HAVE_ARCH_ALLOC_PAGE
arch_alloc_page(struct page * page,int order)221 static inline void arch_alloc_page(struct page *page, int order) { }
222 #endif
223
224 struct page *__alloc_pages_noprof(gfp_t gfp, unsigned int order, int preferred_nid,
225 nodemask_t *nodemask);
226 #define __alloc_pages(...) alloc_hooks(__alloc_pages_noprof(__VA_ARGS__))
227
228 struct folio *__folio_alloc_noprof(gfp_t gfp, unsigned int order, int preferred_nid,
229 nodemask_t *nodemask);
230 #define __folio_alloc(...) alloc_hooks(__folio_alloc_noprof(__VA_ARGS__))
231
232 unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
233 nodemask_t *nodemask, int nr_pages,
234 struct page **page_array);
235 #define __alloc_pages_bulk(...) alloc_hooks(alloc_pages_bulk_noprof(__VA_ARGS__))
236
237 unsigned long alloc_pages_bulk_mempolicy_noprof(gfp_t gfp,
238 unsigned long nr_pages,
239 struct page **page_array);
240 #define alloc_pages_bulk_mempolicy(...) \
241 alloc_hooks(alloc_pages_bulk_mempolicy_noprof(__VA_ARGS__))
242
243 /* Bulk allocate order-0 pages */
244 #define alloc_pages_bulk(_gfp, _nr_pages, _page_array) \
245 __alloc_pages_bulk(_gfp, numa_mem_id(), NULL, _nr_pages, _page_array)
246
247 static inline unsigned long
alloc_pages_bulk_node_noprof(gfp_t gfp,int nid,unsigned long nr_pages,struct page ** page_array)248 alloc_pages_bulk_node_noprof(gfp_t gfp, int nid, unsigned long nr_pages,
249 struct page **page_array)
250 {
251 if (nid == NUMA_NO_NODE)
252 nid = numa_mem_id();
253
254 return alloc_pages_bulk_noprof(gfp, nid, NULL, nr_pages, page_array);
255 }
256
257 #define alloc_pages_bulk_node(...) \
258 alloc_hooks(alloc_pages_bulk_node_noprof(__VA_ARGS__))
259
warn_if_node_offline(int this_node,gfp_t gfp_mask)260 static inline void warn_if_node_offline(int this_node, gfp_t gfp_mask)
261 {
262 gfp_t warn_gfp = gfp_mask & (__GFP_THISNODE|__GFP_NOWARN);
263
264 if (warn_gfp != (__GFP_THISNODE|__GFP_NOWARN))
265 return;
266
267 if (node_online(this_node))
268 return;
269
270 pr_warn("%pGg allocation from offline node %d\n", &gfp_mask, this_node);
271 dump_stack();
272 }
273
274 /*
275 * Allocate pages, preferring the node given as nid. The node must be valid and
276 * online. For more general interface, see alloc_pages_node().
277 */
278 static inline struct page *
__alloc_pages_node_noprof(int nid,gfp_t gfp_mask,unsigned int order)279 __alloc_pages_node_noprof(int nid, gfp_t gfp_mask, unsigned int order)
280 {
281 VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES);
282 warn_if_node_offline(nid, gfp_mask);
283
284 return __alloc_pages_noprof(gfp_mask, order, nid, NULL);
285 }
286
287 #define __alloc_pages_node(...) alloc_hooks(__alloc_pages_node_noprof(__VA_ARGS__))
288
289 static inline
__folio_alloc_node_noprof(gfp_t gfp,unsigned int order,int nid)290 struct folio *__folio_alloc_node_noprof(gfp_t gfp, unsigned int order, int nid)
291 {
292 VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES);
293 warn_if_node_offline(nid, gfp);
294
295 return __folio_alloc_noprof(gfp, order, nid, NULL);
296 }
297
298 #define __folio_alloc_node(...) alloc_hooks(__folio_alloc_node_noprof(__VA_ARGS__))
299
300 /*
301 * Allocate pages, preferring the node given as nid. When nid == NUMA_NO_NODE,
302 * prefer the current CPU's closest node. Otherwise node must be valid and
303 * online.
304 */
alloc_pages_node_noprof(int nid,gfp_t gfp_mask,unsigned int order)305 static inline struct page *alloc_pages_node_noprof(int nid, gfp_t gfp_mask,
306 unsigned int order)
307 {
308 if (nid == NUMA_NO_NODE)
309 nid = numa_mem_id();
310
311 return __alloc_pages_node_noprof(nid, gfp_mask, order);
312 }
313
314 #define alloc_pages_node(...) alloc_hooks(alloc_pages_node_noprof(__VA_ARGS__))
315
316 #ifdef CONFIG_NUMA
317 struct page *alloc_pages_noprof(gfp_t gfp, unsigned int order);
318 struct folio *folio_alloc_noprof(gfp_t gfp, unsigned int order);
319 struct folio *folio_alloc_mpol_noprof(gfp_t gfp, unsigned int order,
320 struct mempolicy *mpol, pgoff_t ilx, int nid);
321 struct folio *vma_alloc_folio_noprof(gfp_t gfp, int order, struct vm_area_struct *vma,
322 unsigned long addr);
323 #else
alloc_pages_noprof(gfp_t gfp_mask,unsigned int order)324 static inline struct page *alloc_pages_noprof(gfp_t gfp_mask, unsigned int order)
325 {
326 return alloc_pages_node_noprof(numa_node_id(), gfp_mask, order);
327 }
folio_alloc_noprof(gfp_t gfp,unsigned int order)328 static inline struct folio *folio_alloc_noprof(gfp_t gfp, unsigned int order)
329 {
330 return __folio_alloc_node_noprof(gfp, order, numa_node_id());
331 }
folio_alloc_mpol_noprof(gfp_t gfp,unsigned int order,struct mempolicy * mpol,pgoff_t ilx,int nid)332 static inline struct folio *folio_alloc_mpol_noprof(gfp_t gfp, unsigned int order,
333 struct mempolicy *mpol, pgoff_t ilx, int nid)
334 {
335 return folio_alloc_noprof(gfp, order);
336 }
337 #define vma_alloc_folio_noprof(gfp, order, vma, addr) \
338 folio_alloc_noprof(gfp, order)
339 #endif
340
341 #define alloc_pages(...) alloc_hooks(alloc_pages_noprof(__VA_ARGS__))
342 #define folio_alloc(...) alloc_hooks(folio_alloc_noprof(__VA_ARGS__))
343 #define folio_alloc_mpol(...) alloc_hooks(folio_alloc_mpol_noprof(__VA_ARGS__))
344 #define vma_alloc_folio(...) alloc_hooks(vma_alloc_folio_noprof(__VA_ARGS__))
345
346 #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
347
alloc_page_vma_noprof(gfp_t gfp,struct vm_area_struct * vma,unsigned long addr)348 static inline struct page *alloc_page_vma_noprof(gfp_t gfp,
349 struct vm_area_struct *vma, unsigned long addr)
350 {
351 struct folio *folio = vma_alloc_folio_noprof(gfp, 0, vma, addr);
352
353 return &folio->page;
354 }
355 #define alloc_page_vma(...) alloc_hooks(alloc_page_vma_noprof(__VA_ARGS__))
356
357 struct page *alloc_pages_nolock_noprof(int nid, unsigned int order);
358 #define alloc_pages_nolock(...) alloc_hooks(alloc_pages_nolock_noprof(__VA_ARGS__))
359
360 extern unsigned long get_free_pages_noprof(gfp_t gfp_mask, unsigned int order);
361 #define __get_free_pages(...) alloc_hooks(get_free_pages_noprof(__VA_ARGS__))
362
363 extern unsigned long get_zeroed_page_noprof(gfp_t gfp_mask);
364 #define get_zeroed_page(...) alloc_hooks(get_zeroed_page_noprof(__VA_ARGS__))
365
366 void *alloc_pages_exact_noprof(size_t size, gfp_t gfp_mask) __alloc_size(1);
367 #define alloc_pages_exact(...) alloc_hooks(alloc_pages_exact_noprof(__VA_ARGS__))
368
369 void free_pages_exact(void *virt, size_t size);
370
371 __meminit void *alloc_pages_exact_nid_noprof(int nid, size_t size, gfp_t gfp_mask) __alloc_size(2);
372 #define alloc_pages_exact_nid(...) \
373 alloc_hooks(alloc_pages_exact_nid_noprof(__VA_ARGS__))
374
375 #define __get_free_page(gfp_mask) \
376 __get_free_pages((gfp_mask), 0)
377
378 #define __get_dma_pages(gfp_mask, order) \
379 __get_free_pages((gfp_mask) | GFP_DMA, (order))
380
381 extern void __free_pages(struct page *page, unsigned int order);
382 extern void free_pages_nolock(struct page *page, unsigned int order);
383 extern void free_pages(unsigned long addr, unsigned int order);
384
385 #define __free_page(page) __free_pages((page), 0)
386 #define free_page(addr) free_pages((addr), 0)
387
388 void page_alloc_init_cpuhp(void);
389 int decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp);
390 void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
391 void drain_all_pages(struct zone *zone);
392 void drain_local_pages(struct zone *zone);
393
394 void page_alloc_init_late(void);
395 void setup_pcp_cacheinfo(unsigned int cpu);
396
397 /*
398 * gfp_allowed_mask is set to GFP_BOOT_MASK during early boot to restrict what
399 * GFP flags are used before interrupts are enabled. Once interrupts are
400 * enabled, it is set to __GFP_BITS_MASK while the system is running. During
401 * hibernation, it is used by PM to avoid I/O during memory allocation while
402 * devices are suspended.
403 */
404 extern gfp_t gfp_allowed_mask;
405
406 /* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */
407 bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
408
gfp_has_io_fs(gfp_t gfp)409 static inline bool gfp_has_io_fs(gfp_t gfp)
410 {
411 return (gfp & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS);
412 }
413
414 /*
415 * Check if the gfp flags allow compaction - GFP_NOIO is a really
416 * tricky context because the migration might require IO.
417 */
gfp_compaction_allowed(gfp_t gfp_mask)418 static inline bool gfp_compaction_allowed(gfp_t gfp_mask)
419 {
420 return IS_ENABLED(CONFIG_COMPACTION) && (gfp_mask & __GFP_IO);
421 }
422
423 extern gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma);
424
425 #ifdef CONFIG_CONTIG_ALLOC
426 /* The below functions must be run on a range from a single zone. */
427 extern int alloc_contig_range_noprof(unsigned long start, unsigned long end,
428 unsigned migratetype, gfp_t gfp_mask);
429 #define alloc_contig_range(...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__))
430
431 extern struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
432 int nid, nodemask_t *nodemask);
433 #define alloc_contig_pages(...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__))
434
435 #endif
436 void free_contig_range(unsigned long pfn, unsigned long nr_pages);
437
438 #ifdef CONFIG_CONTIG_ALLOC
folio_alloc_gigantic_noprof(int order,gfp_t gfp,int nid,nodemask_t * node)439 static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t gfp,
440 int nid, nodemask_t *node)
441 {
442 struct page *page;
443
444 if (WARN_ON(!order || !(gfp & __GFP_COMP)))
445 return NULL;
446
447 page = alloc_contig_pages_noprof(1 << order, gfp, nid, node);
448
449 return page ? page_folio(page) : NULL;
450 }
451 #else
folio_alloc_gigantic_noprof(int order,gfp_t gfp,int nid,nodemask_t * node)452 static inline struct folio *folio_alloc_gigantic_noprof(int order, gfp_t gfp,
453 int nid, nodemask_t *node)
454 {
455 return NULL;
456 }
457 #endif
458 /* This should be paired with folio_put() rather than free_contig_range(). */
459 #define folio_alloc_gigantic(...) alloc_hooks(folio_alloc_gigantic_noprof(__VA_ARGS__))
460
461 #endif /* __LINUX_GFP_H */
462