blob: 8b0cc08958d9a56687e91b5d6ab76a750219f2f1 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef __LINUX_GFP_H
2#define __LINUX_GFP_H
3
4#include <linux/mmzone.h>
5#include <linux/stddef.h>
6#include <linux/linkage.h>
7#include <linux/topology.h>
8#include <linux/mmdebug.h>
9#ifdef CONFIG_MEM_CHECK
10#include <linux/sched.h>
11#include <asm/current.h>
12#endif
13
14struct vm_area_struct;
15
16/* Plain integer GFP bitmasks. Do not use this directly. */
17#define ___GFP_DMA 0x01u
18#define ___GFP_HIGHMEM 0x02u
19#define ___GFP_DMA32 0x04u
20#define ___GFP_MOVABLE 0x08u
21#define ___GFP_WAIT 0x10u
22#define ___GFP_HIGH 0x20u
23#define ___GFP_IO 0x40u
24#define ___GFP_FS 0x80u
25#define ___GFP_COLD 0x100u
26#define ___GFP_NOWARN 0x200u
27#define ___GFP_REPEAT 0x400u
28#define ___GFP_NOFAIL 0x800u
29#define ___GFP_NORETRY 0x1000u
30#define ___GFP_COMP 0x4000u
31#define ___GFP_ZERO 0x8000u
32#define ___GFP_NOMEMALLOC 0x10000u
33#define ___GFP_HARDWALL 0x20000u
34#define ___GFP_THISNODE 0x40000u
35#define ___GFP_RECLAIMABLE 0x80000u
36#define ___GFP_PAGEMODEM 0x100000u /* Only Allocate for modem , failed to return NULL */
37#ifdef CONFIG_KMEMCHECK
38#define ___GFP_NOTRACK 0x200000u
39#else
40#define ___GFP_NOTRACK 0
41#endif
42#define ___GFP_NO_KSWAPD 0x400000u
43#define ___GFP_OTHER_NODE 0x800000u
44#define ___GFP_WRITE 0x1000000u
45#define ___GFP_PAGECACHE 0x2000000u
46#define ___GFP_PAGERAMFS 0x4000000u
47#define ___GFP_PAGETMPFS 0x8000000u
48
49
50/*
51 * GFP bitmasks..
52 *
53 * Zone modifiers (see linux/mmzone.h - low three bits)
54 *
55 * Do not put any conditional on these. If necessary modify the definitions
56 * without the underscores and use them consistently. The definitions here may
57 * be used in bit comparisons.
58 */
59#define __GFP_DMA ((__force gfp_t)___GFP_DMA)
60#define __GFP_HIGHMEM ((__force gfp_t)___GFP_HIGHMEM)
61#define __GFP_DMA32 ((__force gfp_t)___GFP_DMA32)
62#define __GFP_MOVABLE ((__force gfp_t)___GFP_MOVABLE) /* Page is movable */
63#define GFP_ZONEMASK (__GFP_DMA|__GFP_HIGHMEM|__GFP_DMA32|__GFP_MOVABLE)
64/*
65 * Action modifiers - doesn't change the zoning
66 *
67 * __GFP_REPEAT: Try hard to allocate the memory, but the allocation attempt
68 * _might_ fail. This depends upon the particular VM implementation.
69 *
70 * __GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
71 * cannot handle allocation failures. This modifier is deprecated and no new
72 * users should be added.
73 *
74 * __GFP_NORETRY: The VM implementation must not retry indefinitely.
75 *
76 * __GFP_MOVABLE: Flag that this page will be movable by the page migration
77 * mechanism or reclaimed
78 */
79#define __GFP_WAIT ((__force gfp_t)___GFP_WAIT) /* Can wait and reschedule? */
80#define __GFP_HIGH ((__force gfp_t)___GFP_HIGH) /* Should access emergency pools? */
81#define __GFP_IO ((__force gfp_t)___GFP_IO) /* Can start physical IO? */
82#define __GFP_FS ((__force gfp_t)___GFP_FS) /* Can call down to low-level FS? */
83#define __GFP_COLD ((__force gfp_t)___GFP_COLD) /* Cache-cold page required */
84#define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN) /* Suppress page allocation failure warning */
85#define __GFP_REPEAT ((__force gfp_t)___GFP_REPEAT) /* See above */
86#define __GFP_NOFAIL ((__force gfp_t)___GFP_NOFAIL) /* See above */
87#define __GFP_NORETRY ((__force gfp_t)___GFP_NORETRY) /* See above */
88#define __GFP_COMP ((__force gfp_t)___GFP_COMP) /* Add compound page metadata */
89#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO) /* Return zeroed page on success */
90#define __GFP_NOMEMALLOC ((__force gfp_t)___GFP_NOMEMALLOC) /* Don't use emergency reserves */
91#define __GFP_HARDWALL ((__force gfp_t)___GFP_HARDWALL) /* Enforce hardwall cpuset memory allocs */
92#define __GFP_THISNODE ((__force gfp_t)___GFP_THISNODE)/* No fallback, no policies */
93#define __GFP_RECLAIMABLE ((__force gfp_t)___GFP_RECLAIMABLE) /* Page is reclaimable */
94#define __GFP_NOTRACK ((__force gfp_t)___GFP_NOTRACK) /* Don't track with kmemcheck */
95
96#define __GFP_NO_KSWAPD ((__force gfp_t)___GFP_NO_KSWAPD)
97#define __GFP_OTHER_NODE ((__force gfp_t)___GFP_OTHER_NODE) /* On behalf of other node */
98#define __GFP_WRITE ((__force gfp_t)___GFP_WRITE) /* Allocator intends to dirty page */
99
100#define __GFP_PAGECACHE ((__force gfp_t)___GFP_PAGECACHE) /* Allocate as page cache */
101#define __GFP_PAGERAMFS ((__force gfp_t)___GFP_PAGERAMFS) /* Allocate for ramfs */
102#define __GFP_PAGETMPFS ((__force gfp_t)___GFP_PAGETMPFS) /* Allocate for tmpfs */
103#define __GFP_PAGEMODEM ((__force gfp_t)___GFP_PAGEMODEM) /* Only Allocate for modem only */
104/*
105 * This may seem redundant, but it's a way of annotating false positives vs.
106 * allocations that simply cannot be supported (e.g. page tables).
107 */
108#define __GFP_NOTRACK_FALSE_POSITIVE (__GFP_NOTRACK)
109
110#define __GFP_BITS_SHIFT 28 /* Room for N __GFP_FOO bits */
111#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
112
113/* This equals 0, but use constants in case they ever change */
114#define GFP_NOWAIT (GFP_ATOMIC & ~__GFP_HIGH)
115/* GFP_ATOMIC means both !wait (__GFP_WAIT not set) and use emergency pool */
116#define GFP_ATOMIC (__GFP_HIGH)
117#define GFP_NOIO (__GFP_WAIT)
118#define GFP_NOFS (__GFP_WAIT | __GFP_IO)
119#define GFP_KERNEL (__GFP_WAIT | __GFP_IO | __GFP_FS)
120#define GFP_TEMPORARY (__GFP_WAIT | __GFP_IO | __GFP_FS | \
121 __GFP_RECLAIMABLE)
122#define GFP_USER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
123#define GFP_HIGHUSER (__GFP_WAIT | __GFP_IO | __GFP_FS | __GFP_HARDWALL | \
124 __GFP_HIGHMEM)
125#define GFP_HIGHUSER_MOVABLE (__GFP_WAIT | __GFP_IO | __GFP_FS | \
126 __GFP_HARDWALL | __GFP_HIGHMEM | \
127 __GFP_MOVABLE)
128#define GFP_IOFS (__GFP_IO | __GFP_FS)
129#define GFP_TRANSHUGE (GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
130 __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | \
131 __GFP_NO_KSWAPD)
132
133#ifdef CONFIG_NUMA
134#define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)
135#else
136#define GFP_THISNODE ((__force gfp_t)0)
137#endif
138
139/* This mask makes up all the page movable related flags */
140#define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
141
142/* Control page allocator reclaim behavior */
143#define GFP_RECLAIM_MASK (__GFP_WAIT|__GFP_HIGH|__GFP_IO|__GFP_FS|\
144 __GFP_NOWARN|__GFP_REPEAT|__GFP_NOFAIL|\
145 __GFP_NORETRY|__GFP_NOMEMALLOC)
146
147/* Control slab gfp mask during early boot */
148#define GFP_BOOT_MASK (__GFP_BITS_MASK & ~(__GFP_WAIT|__GFP_IO|__GFP_FS))
149
150/* Control allocation constraints */
151#define GFP_CONSTRAINT_MASK (__GFP_HARDWALL|__GFP_THISNODE)
152
153/* Do not use these with a slab allocator */
154#define GFP_SLAB_BUG_MASK (__GFP_DMA32|__GFP_HIGHMEM|~__GFP_BITS_MASK)
155
156/* Flag - indicates that the buffer will be suitable for DMA. Ignored on some
157 platforms, used as appropriate on others */
158
159#define GFP_DMA __GFP_DMA
160
161/* 4GB DMA on some platforms */
162#define GFP_DMA32 __GFP_DMA32
163
164/* Convert GFP flags to their corresponding migrate type */
165static inline int allocflags_to_migratetype(gfp_t gfp_flags)
166{
167 WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
168
169 if (unlikely(page_group_by_mobility_disabled))
170 return MIGRATE_UNMOVABLE;
171
172 /* Group based on mobility */
173 return (((gfp_flags & __GFP_MOVABLE) != 0) << 1) |
174 ((gfp_flags & __GFP_RECLAIMABLE) != 0);
175}
176
177#ifdef CONFIG_HIGHMEM
178#define OPT_ZONE_HIGHMEM ZONE_HIGHMEM
179#else
180#define OPT_ZONE_HIGHMEM ZONE_NORMAL
181#endif
182
183#ifdef CONFIG_ZONE_DMA
184#define OPT_ZONE_DMA ZONE_DMA
185#else
186#define OPT_ZONE_DMA ZONE_NORMAL
187#endif
188
189#ifdef CONFIG_ZONE_DMA32
190#define OPT_ZONE_DMA32 ZONE_DMA32
191#else
192#define OPT_ZONE_DMA32 ZONE_NORMAL
193#endif
194
195/*
196 * GFP_ZONE_TABLE is a word size bitstring that is used for looking up the
197 * zone to use given the lowest 4 bits of gfp_t. Entries are ZONE_SHIFT long
198 * and there are 16 of them to cover all possible combinations of
199 * __GFP_DMA, __GFP_DMA32, __GFP_MOVABLE and __GFP_HIGHMEM.
200 *
201 * The zone fallback order is MOVABLE=>HIGHMEM=>NORMAL=>DMA32=>DMA.
202 * But GFP_MOVABLE is not only a zone specifier but also an allocation
203 * policy. Therefore __GFP_MOVABLE plus another zone selector is valid.
204 * Only 1 bit of the lowest 3 bits (DMA,DMA32,HIGHMEM) can be set to "1".
205 *
206 * bit result
207 * =================
208 * 0x0 => NORMAL
209 * 0x1 => DMA or NORMAL
210 * 0x2 => HIGHMEM or NORMAL
211 * 0x3 => BAD (DMA+HIGHMEM)
212 * 0x4 => DMA32 or DMA or NORMAL
213 * 0x5 => BAD (DMA+DMA32)
214 * 0x6 => BAD (HIGHMEM+DMA32)
215 * 0x7 => BAD (HIGHMEM+DMA32+DMA)
216 * 0x8 => NORMAL (MOVABLE+0)
217 * 0x9 => DMA or NORMAL (MOVABLE+DMA)
218 * 0xa => MOVABLE (Movable is valid only if HIGHMEM is set too)
219 * 0xb => BAD (MOVABLE+HIGHMEM+DMA)
220 * 0xc => DMA32 (MOVABLE+HIGHMEM+DMA32)
221 * 0xd => BAD (MOVABLE+DMA32+DMA)
222 * 0xe => BAD (MOVABLE+DMA32+HIGHMEM)
223 * 0xf => BAD (MOVABLE+DMA32+HIGHMEM+DMA)
224 *
225 * ZONES_SHIFT must be <= 2 on 32 bit platforms.
226 */
227
228#if 16 * ZONES_SHIFT > BITS_PER_LONG
229#error ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
230#endif
231
232#define GFP_ZONE_TABLE ( \
233 (ZONE_NORMAL << 0 * ZONES_SHIFT) \
234 | (OPT_ZONE_DMA << ___GFP_DMA * ZONES_SHIFT) \
235 | (OPT_ZONE_HIGHMEM << ___GFP_HIGHMEM * ZONES_SHIFT) \
236 | (OPT_ZONE_DMA32 << ___GFP_DMA32 * ZONES_SHIFT) \
237 | (ZONE_NORMAL << ___GFP_MOVABLE * ZONES_SHIFT) \
238 | (OPT_ZONE_DMA << (___GFP_MOVABLE | ___GFP_DMA) * ZONES_SHIFT) \
239 | (ZONE_MOVABLE << (___GFP_MOVABLE | ___GFP_HIGHMEM) * ZONES_SHIFT) \
240 | (OPT_ZONE_DMA32 << (___GFP_MOVABLE | ___GFP_DMA32) * ZONES_SHIFT) \
241)
242
243/*
244 * GFP_ZONE_BAD is a bitmap for all combinations of __GFP_DMA, __GFP_DMA32
245 * __GFP_HIGHMEM and __GFP_MOVABLE that are not permitted. One flag per
246 * entry starting with bit 0. Bit is set if the combination is not
247 * allowed.
248 */
249#define GFP_ZONE_BAD ( \
250 1 << (___GFP_DMA | ___GFP_HIGHMEM) \
251 | 1 << (___GFP_DMA | ___GFP_DMA32) \
252 | 1 << (___GFP_DMA32 | ___GFP_HIGHMEM) \
253 | 1 << (___GFP_DMA | ___GFP_DMA32 | ___GFP_HIGHMEM) \
254 | 1 << (___GFP_MOVABLE | ___GFP_HIGHMEM | ___GFP_DMA) \
255 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA) \
256 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_HIGHMEM) \
257 | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA | ___GFP_HIGHMEM) \
258)
259
260static inline enum zone_type gfp_zone(gfp_t flags)
261{
262 enum zone_type z;
263 int bit = (__force int) (flags & GFP_ZONEMASK);
264
265 z = (GFP_ZONE_TABLE >> (bit * ZONES_SHIFT)) &
266 ((1 << ZONES_SHIFT) - 1);
267 VM_BUG_ON((GFP_ZONE_BAD >> bit) & 1);
268 return z;
269}
270
271/*
272 * There is only one page-allocator function, and two main namespaces to
273 * it. The alloc_page*() variants return 'struct page *' and as such
274 * can allocate highmem pages, the *get*page*() variants return
275 * virtual kernel addresses to the allocated page(s).
276 */
277
278static inline int gfp_zonelist(gfp_t flags)
279{
280 if (NUMA_BUILD && unlikely(flags & __GFP_THISNODE))
281 return 1;
282
283 return 0;
284}
285
286/*
287 * We get the zone list from the current node and the gfp_mask.
288 * This zone list contains a maximum of MAXNODES*MAX_NR_ZONES zones.
289 * There are two zonelists per node, one for all zones with memory and
290 * one containing just zones from the node the zonelist belongs to.
291 *
292 * For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets
293 * optimized to &contig_page_data at compile-time.
294 */
295static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
296{
297 return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
298}
299
300#ifndef HAVE_ARCH_FREE_PAGE
301static inline void arch_free_page(struct page *page, int order) { }
302#endif
303#ifndef HAVE_ARCH_ALLOC_PAGE
304static inline void arch_alloc_page(struct page *page, int order) { }
305#endif
306
307struct page *
308__alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
309 struct zonelist *zonelist, nodemask_t *nodemask);
310
311static inline struct page *
312__alloc_pages(gfp_t gfp_mask, unsigned int order,
313 struct zonelist *zonelist)
314{
315 return __alloc_pages_nodemask(gfp_mask, order, zonelist, NULL);
316}
317
318static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
319 unsigned int order)
320{
321#ifdef CONFIG_MEM_CHECK
322 if ( (PAGE_SIZE << order) > CONFIG_MEM_CHECK_SIZE)
323 printk(KERN_ALERT"memcheck_pages %d %s (%pS)\n", order, current->comm, __builtin_return_address(0));
324#endif
325
326 /* Unknown node is current node */
327 if (nid < 0)
328 nid = numa_node_id();
329
330 return __alloc_pages(gfp_mask, order, node_zonelist(nid, gfp_mask));
331}
332
333static inline struct page *alloc_pages_exact_node(int nid, gfp_t gfp_mask,
334 unsigned int order)
335{
336 VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES || !node_online(nid));
337
338 return __alloc_pages(gfp_mask, order, node_zonelist(nid, gfp_mask));
339}
340
341#ifdef CONFIG_NUMA
342extern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);
343
344static inline struct page *
345alloc_pages(gfp_t gfp_mask, unsigned int order)
346{
347 return alloc_pages_current(gfp_mask, order);
348}
349extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order,
350 struct vm_area_struct *vma, unsigned long addr,
351 int node);
352#else
353#define alloc_pages(gfp_mask, order) \
354 alloc_pages_node(numa_node_id(), gfp_mask, order)
355#define alloc_pages_vma(gfp_mask, order, vma, addr, node) \
356 alloc_pages(gfp_mask, order)
357#endif
358#define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
359#define alloc_page_vma(gfp_mask, vma, addr) \
360 alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id())
361#define alloc_page_vma_node(gfp_mask, vma, addr, node) \
362 alloc_pages_vma(gfp_mask, 0, vma, addr, node)
363
364extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
365extern unsigned long get_zeroed_page(gfp_t gfp_mask);
366
367void *alloc_pages_exact(size_t size, gfp_t gfp_mask);
368void free_pages_exact(void *virt, size_t size);
369/* This is different from alloc_pages_exact_node !!! */
370void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask);
371
372#define __get_free_page(gfp_mask) \
373 __get_free_pages((gfp_mask), 0)
374
375#define __get_dma_pages(gfp_mask, order) \
376 __get_free_pages((gfp_mask) | GFP_DMA, (order))
377
378extern void __free_pages(struct page *page, unsigned int order);
379extern void free_pages(unsigned long addr, unsigned int order);
380extern void free_hot_cold_page(struct page *page, int cold);
381extern void free_hot_cold_page_list(struct list_head *list, int cold);
382
383#define __free_page(page) __free_pages((page), 0)
384#define free_page(addr) free_pages((addr), 0)
385
386void page_alloc_init(void);
387void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
388void drain_all_pages(void);
389void drain_local_pages(void *dummy);
390
391/*
392 * gfp_allowed_mask is set to GFP_BOOT_MASK during early boot to restrict what
393 * GFP flags are used before interrupts are enabled. Once interrupts are
394 * enabled, it is set to __GFP_BITS_MASK while the system is running. During
395 * hibernation, it is used by PM to avoid I/O during memory allocation while
396 * devices are suspended.
397 */
398extern gfp_t gfp_allowed_mask;
399
400extern void pm_restrict_gfp_mask(void);
401extern void pm_restore_gfp_mask(void);
402
403#ifdef CONFIG_PM_SLEEP
404extern bool pm_suspended_storage(void);
405#else
406static inline bool pm_suspended_storage(void)
407{
408 return false;
409}
410#endif /* CONFIG_PM_SLEEP */
411
412#endif /* __LINUX_GFP_H */