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