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
10struct vm_area_struct;
11struct mempolicy;
12
13/* Convert GFP flags to their corresponding migrate type */
14#define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
15#define GFP_MOVABLE_SHIFT 3
16
17static inline int gfp_migratetype(const gfp_t gfp_flags)
18{
19 VM_WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
20 BUILD_BUG_ON((1UL << GFP_MOVABLE_SHIFT) != ___GFP_MOVABLE);
21 BUILD_BUG_ON((___GFP_MOVABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_MOVABLE);
22 BUILD_BUG_ON((___GFP_RECLAIMABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_RECLAIMABLE);
23 BUILD_BUG_ON(((___GFP_MOVABLE | ___GFP_RECLAIMABLE) >>
24 GFP_MOVABLE_SHIFT) != MIGRATE_HIGHATOMIC);
25
26 if (unlikely(page_group_by_mobility_disabled))
27 return MIGRATE_UNMOVABLE;
28
29 /* Group based on mobility */
30 return (__force unsigned long)(gfp_flags & GFP_MOVABLE_MASK) >> GFP_MOVABLE_SHIFT;
31}
32#undef GFP_MOVABLE_MASK
33#undef GFP_MOVABLE_SHIFT
34
35static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags)
36{
37 return !!(gfp_flags & __GFP_DIRECT_RECLAIM);
38}
39
40#ifdef CONFIG_HIGHMEM
41#define OPT_ZONE_HIGHMEM ZONE_HIGHMEM
42#else
43#define OPT_ZONE_HIGHMEM ZONE_NORMAL
44#endif
45
46#ifdef CONFIG_ZONE_DMA
47#define OPT_ZONE_DMA ZONE_DMA
48#else
49#define OPT_ZONE_DMA ZONE_NORMAL
50#endif
51
52#ifdef CONFIG_ZONE_DMA32
53#define OPT_ZONE_DMA32 ZONE_DMA32
54#else
55#define OPT_ZONE_DMA32 ZONE_NORMAL
56#endif
57
58/*
59 * GFP_ZONE_TABLE is a word size bitstring that is used for looking up the
60 * zone to use given the lowest 4 bits of gfp_t. Entries are GFP_ZONES_SHIFT
61 * bits long and there are 16 of them to cover all possible combinations of
62 * __GFP_DMA, __GFP_DMA32, __GFP_MOVABLE and __GFP_HIGHMEM.
63 *
64 * The zone fallback order is MOVABLE=>HIGHMEM=>NORMAL=>DMA32=>DMA.
65 * But GFP_MOVABLE is not only a zone specifier but also an allocation
66 * policy. Therefore __GFP_MOVABLE plus another zone selector is valid.
67 * Only 1 bit of the lowest 3 bits (DMA,DMA32,HIGHMEM) can be set to "1".
68 *
69 * bit result
70 * =================
71 * 0x0 => NORMAL
72 * 0x1 => DMA or NORMAL
73 * 0x2 => HIGHMEM or NORMAL
74 * 0x3 => BAD (DMA+HIGHMEM)
75 * 0x4 => DMA32 or NORMAL
76 * 0x5 => BAD (DMA+DMA32)
77 * 0x6 => BAD (HIGHMEM+DMA32)
78 * 0x7 => BAD (HIGHMEM+DMA32+DMA)
79 * 0x8 => NORMAL (MOVABLE+0)
80 * 0x9 => DMA or NORMAL (MOVABLE+DMA)
81 * 0xa => MOVABLE (Movable is valid only if HIGHMEM is set too)
82 * 0xb => BAD (MOVABLE+HIGHMEM+DMA)
83 * 0xc => DMA32 or NORMAL (MOVABLE+DMA32)
84 * 0xd => BAD (MOVABLE+DMA32+DMA)
85 * 0xe => BAD (MOVABLE+DMA32+HIGHMEM)
86 * 0xf => BAD (MOVABLE+DMA32+HIGHMEM+DMA)
87 *
88 * GFP_ZONES_SHIFT must be <= 2 on 32 bit platforms.
89 */
90
91#if defined(CONFIG_ZONE_DEVICE) && (MAX_NR_ZONES-1) <= 4
92/* ZONE_DEVICE is not a valid GFP zone specifier */
93#define GFP_ZONES_SHIFT 2
94#else
95#define GFP_ZONES_SHIFT ZONES_SHIFT
96#endif
97
98#if 16 * GFP_ZONES_SHIFT > BITS_PER_LONG
99#error GFP_ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
100#endif
101
102#define GFP_ZONE_TABLE ( \
103 (ZONE_NORMAL << 0 * GFP_ZONES_SHIFT) \
104 | (OPT_ZONE_DMA << ___GFP_DMA * GFP_ZONES_SHIFT) \
105 | (OPT_ZONE_HIGHMEM << ___GFP_HIGHMEM * GFP_ZONES_SHIFT) \
106 | (OPT_ZONE_DMA32 << ___GFP_DMA32 * GFP_ZONES_SHIFT) \
107 | (ZONE_NORMAL << ___GFP_MOVABLE * GFP_ZONES_SHIFT) \
108 | (OPT_ZONE_DMA << (___GFP_MOVABLE | ___GFP_DMA) * GFP_ZONES_SHIFT) \
109 | (ZONE_MOVABLE << (___GFP_MOVABLE | ___GFP_HIGHMEM) * GFP_ZONES_SHIFT)\
110 | (OPT_ZONE_DMA32 << (___GFP_MOVABLE | ___GFP_DMA32) * GFP_ZONES_SHIFT)\
111)
112
113/*
114 * GFP_ZONE_BAD is a bitmap for all combinations of __GFP_DMA, __GFP_DMA32
115 * __GFP_HIGHMEM and __GFP_MOVABLE that are not permitted. One flag per
116 * entry starting with bit 0. Bit is set if the combination is not
117 * allowed.
118 */
119#define GFP_ZONE_BAD ( \
120 1 << (___GFP_DMA | ___GFP_HIGHMEM) \
121 | 1 << (___GFP_DMA | ___GFP_DMA32) \
122 | 1 << (___GFP_DMA32 | ___GFP_HIGHMEM) \
123 | 1 << (___GFP_DMA | ___GFP_DMA32 | ___GFP_HIGHMEM) \
124 | 1 << (___GFP_MOVABLE | ___GFP_HIGHMEM | ___GFP_DMA) \
125 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA) \
126 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_HIGHMEM) \
127 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA | ___GFP_HIGHMEM) \
128)
129
130static inline enum zone_type gfp_zone(gfp_t flags)
131{
132 enum zone_type z;
133 int bit = (__force int) (flags & GFP_ZONEMASK);
134
135 z = (GFP_ZONE_TABLE >> (bit * GFP_ZONES_SHIFT)) &
136 ((1 << GFP_ZONES_SHIFT) - 1);
137 VM_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
138 return z;
139}
140
141/*
142 * There is only one page-allocator function, and two main namespaces to
143 * it. The alloc_page*() variants return 'struct page *' and as such
144 * can allocate highmem pages, the *get*page*() variants return
145 * virtual kernel addresses to the allocated page(s).
146 */
147
148static inline int gfp_zonelist(gfp_t flags)
149{
150#ifdef CONFIG_NUMA
151 if (unlikely(flags & __GFP_THISNODE))
152 return ZONELIST_NOFALLBACK;
153#endif
154 return ZONELIST_FALLBACK;
155}
156
157/*
158 * We get the zone list from the current node and the gfp_mask.
159 * This zone list contains a maximum of MAX_NUMNODES*MAX_NR_ZONES zones.
160 * There are two zonelists per node, one for all zones with memory and
161 * one containing just zones from the node the zonelist belongs to.
162 *
163 * For the case of non-NUMA systems the NODE_DATA() gets optimized to
164 * &contig_page_data at compile-time.
165 */
166static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
167{
168 return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
169}
170
171#ifndef HAVE_ARCH_FREE_PAGE
172static inline void arch_free_page(struct page *page, int order) { }
173#endif
174#ifndef HAVE_ARCH_ALLOC_PAGE
175static inline void arch_alloc_page(struct page *page, int order) { }
176#endif
177
178struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid,
179 nodemask_t *nodemask);
180struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid,
181 nodemask_t *nodemask);
182
183unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
184 nodemask_t *nodemask, int nr_pages,
185 struct list_head *page_list,
186 struct page **page_array);
187
188unsigned long alloc_pages_bulk_array_mempolicy(gfp_t gfp,
189 unsigned long nr_pages,
190 struct page **page_array);
191
192/* Bulk allocate order-0 pages */
193static inline unsigned long
194alloc_pages_bulk_list(gfp_t gfp, unsigned long nr_pages, struct list_head *list)
195{
196 return __alloc_pages_bulk(gfp, preferred_nid: numa_mem_id(), NULL, nr_pages, page_list: list, NULL);
197}
198
199static inline unsigned long
200alloc_pages_bulk_array(gfp_t gfp, unsigned long nr_pages, struct page **page_array)
201{
202 return __alloc_pages_bulk(gfp, preferred_nid: numa_mem_id(), NULL, nr_pages, NULL, page_array);
203}
204
205static inline unsigned long
206alloc_pages_bulk_array_node(gfp_t gfp, int nid, unsigned long nr_pages, struct page **page_array)
207{
208 if (nid == NUMA_NO_NODE)
209 nid = numa_mem_id();
210
211 return __alloc_pages_bulk(gfp, preferred_nid: nid, NULL, nr_pages, NULL, page_array);
212}
213
214static inline void warn_if_node_offline(int this_node, gfp_t gfp_mask)
215{
216 gfp_t warn_gfp = gfp_mask & (__GFP_THISNODE|__GFP_NOWARN);
217
218 if (warn_gfp != (__GFP_THISNODE|__GFP_NOWARN))
219 return;
220
221 if (node_online(this_node))
222 return;
223
224 pr_warn("%pGg allocation from offline node %d\n", &gfp_mask, this_node);
225 dump_stack();
226}
227
228/*
229 * Allocate pages, preferring the node given as nid. The node must be valid and
230 * online. For more general interface, see alloc_pages_node().
231 */
232static inline struct page *
233__alloc_pages_node(int nid, gfp_t gfp_mask, unsigned int order)
234{
235 VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES);
236 warn_if_node_offline(this_node: nid, gfp_mask);
237
238 return __alloc_pages(gfp: gfp_mask, order, preferred_nid: nid, NULL);
239}
240
241static inline
242struct folio *__folio_alloc_node(gfp_t gfp, unsigned int order, int nid)
243{
244 VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES);
245 warn_if_node_offline(this_node: nid, gfp_mask: gfp);
246
247 return __folio_alloc(gfp, order, preferred_nid: nid, NULL);
248}
249
250/*
251 * Allocate pages, preferring the node given as nid. When nid == NUMA_NO_NODE,
252 * prefer the current CPU's closest node. Otherwise node must be valid and
253 * online.
254 */
255static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
256 unsigned int order)
257{
258 if (nid == NUMA_NO_NODE)
259 nid = numa_mem_id();
260
261 return __alloc_pages_node(nid, gfp_mask, order);
262}
263
264#ifdef CONFIG_NUMA
265struct page *alloc_pages(gfp_t gfp, unsigned int order);
266struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order,
267 struct mempolicy *mpol, pgoff_t ilx, int nid);
268struct folio *folio_alloc(gfp_t gfp, unsigned int order);
269struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma,
270 unsigned long addr, bool hugepage);
271#else
272static inline struct page *alloc_pages(gfp_t gfp_mask, unsigned int order)
273{
274 return alloc_pages_node(numa_node_id(), gfp_mask, order);
275}
276static inline struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order,
277 struct mempolicy *mpol, pgoff_t ilx, int nid)
278{
279 return alloc_pages(gfp, order);
280}
281static inline struct folio *folio_alloc(gfp_t gfp, unsigned int order)
282{
283 return __folio_alloc_node(gfp, order, numa_node_id());
284}
285#define vma_alloc_folio(gfp, order, vma, addr, hugepage) \
286 folio_alloc(gfp, order)
287#endif
288#define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
289static inline struct page *alloc_page_vma(gfp_t gfp,
290 struct vm_area_struct *vma, unsigned long addr)
291{
292 struct folio *folio = vma_alloc_folio(gfp, order: 0, vma, addr, hugepage: false);
293
294 return &folio->page;
295}
296
297extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
298extern unsigned long get_zeroed_page(gfp_t gfp_mask);
299
300void *alloc_pages_exact(size_t size, gfp_t gfp_mask) __alloc_size(1);
301void free_pages_exact(void *virt, size_t size);
302__meminit void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask) __alloc_size(2);
303
304#define __get_free_page(gfp_mask) \
305 __get_free_pages((gfp_mask), 0)
306
307#define __get_dma_pages(gfp_mask, order) \
308 __get_free_pages((gfp_mask) | GFP_DMA, (order))
309
310extern void __free_pages(struct page *page, unsigned int order);
311extern void free_pages(unsigned long addr, unsigned int order);
312
313struct page_frag_cache;
314void page_frag_cache_drain(struct page_frag_cache *nc);
315extern void __page_frag_cache_drain(struct page *page, unsigned int count);
316void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz,
317 gfp_t gfp_mask, unsigned int align_mask);
318
319static inline void *page_frag_alloc_align(struct page_frag_cache *nc,
320 unsigned int fragsz, gfp_t gfp_mask,
321 unsigned int align)
322{
323 WARN_ON_ONCE(!is_power_of_2(align));
324 return __page_frag_alloc_align(nc, fragsz, gfp_mask, align_mask: -align);
325}
326
327static inline void *page_frag_alloc(struct page_frag_cache *nc,
328 unsigned int fragsz, gfp_t gfp_mask)
329{
330 return __page_frag_alloc_align(nc, fragsz, gfp_mask, align_mask: ~0u);
331}
332
333extern void page_frag_free(void *addr);
334
335#define __free_page(page) __free_pages((page), 0)
336#define free_page(addr) free_pages((addr), 0)
337
338void page_alloc_init_cpuhp(void);
339int decay_pcp_high(struct zone *zone, struct per_cpu_pages *pcp);
340void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
341void drain_all_pages(struct zone *zone);
342void drain_local_pages(struct zone *zone);
343
344void page_alloc_init_late(void);
345void setup_pcp_cacheinfo(unsigned int cpu);
346
347/*
348 * gfp_allowed_mask is set to GFP_BOOT_MASK during early boot to restrict what
349 * GFP flags are used before interrupts are enabled. Once interrupts are
350 * enabled, it is set to __GFP_BITS_MASK while the system is running. During
351 * hibernation, it is used by PM to avoid I/O during memory allocation while
352 * devices are suspended.
353 */
354extern gfp_t gfp_allowed_mask;
355
356/* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */
357bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
358
359static inline bool gfp_has_io_fs(gfp_t gfp)
360{
361 return (gfp & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS);
362}
363
364/*
365 * Check if the gfp flags allow compaction - GFP_NOIO is a really
366 * tricky context because the migration might require IO.
367 */
368static inline bool gfp_compaction_allowed(gfp_t gfp_mask)
369{
370 return IS_ENABLED(CONFIG_COMPACTION) && (gfp_mask & __GFP_IO);
371}
372
373extern gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma);
374
375#ifdef CONFIG_CONTIG_ALLOC
376/* The below functions must be run on a range from a single zone. */
377extern int alloc_contig_range(unsigned long start, unsigned long end,
378 unsigned migratetype, gfp_t gfp_mask);
379extern struct page *alloc_contig_pages(unsigned long nr_pages, gfp_t gfp_mask,
380 int nid, nodemask_t *nodemask);
381#endif
382void free_contig_range(unsigned long pfn, unsigned long nr_pages);
383
384#endif /* __LINUX_GFP_H */
385

source code of linux/include/linux/gfp.h