1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __LINUX_GFP_H
3*4882a593Smuzhiyun #define __LINUX_GFP_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/mmdebug.h>
6*4882a593Smuzhiyun #include <linux/mmzone.h>
7*4882a593Smuzhiyun #include <linux/stddef.h>
8*4882a593Smuzhiyun #include <linux/linkage.h>
9*4882a593Smuzhiyun #include <linux/topology.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun struct vm_area_struct;
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun * In case of changes, please don't forget to update
15*4882a593Smuzhiyun * include/trace/events/mmflags.h and tools/perf/builtin-kmem.c
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* Plain integer GFP bitmasks. Do not use this directly. */
19*4882a593Smuzhiyun #define ___GFP_DMA 0x01u
20*4882a593Smuzhiyun #define ___GFP_HIGHMEM 0x02u
21*4882a593Smuzhiyun #define ___GFP_DMA32 0x04u
22*4882a593Smuzhiyun #define ___GFP_MOVABLE 0x08u
23*4882a593Smuzhiyun #define ___GFP_RECLAIMABLE 0x10u
24*4882a593Smuzhiyun #define ___GFP_HIGH 0x20u
25*4882a593Smuzhiyun #define ___GFP_IO 0x40u
26*4882a593Smuzhiyun #define ___GFP_FS 0x80u
27*4882a593Smuzhiyun #define ___GFP_ZERO 0x100u
28*4882a593Smuzhiyun #define ___GFP_ATOMIC 0x200u
29*4882a593Smuzhiyun #define ___GFP_DIRECT_RECLAIM 0x400u
30*4882a593Smuzhiyun #define ___GFP_KSWAPD_RECLAIM 0x800u
31*4882a593Smuzhiyun #define ___GFP_WRITE 0x1000u
32*4882a593Smuzhiyun #define ___GFP_NOWARN 0x2000u
33*4882a593Smuzhiyun #define ___GFP_RETRY_MAYFAIL 0x4000u
34*4882a593Smuzhiyun #define ___GFP_NOFAIL 0x8000u
35*4882a593Smuzhiyun #define ___GFP_NORETRY 0x10000u
36*4882a593Smuzhiyun #define ___GFP_MEMALLOC 0x20000u
37*4882a593Smuzhiyun #define ___GFP_COMP 0x40000u
38*4882a593Smuzhiyun #define ___GFP_NOMEMALLOC 0x80000u
39*4882a593Smuzhiyun #define ___GFP_HARDWALL 0x100000u
40*4882a593Smuzhiyun #define ___GFP_THISNODE 0x200000u
41*4882a593Smuzhiyun #define ___GFP_ACCOUNT 0x400000u
42*4882a593Smuzhiyun #define ___GFP_ZEROTAGS 0x800000u
43*4882a593Smuzhiyun #define ___GFP_SKIP_KASAN_POISON 0x1000000u
44*4882a593Smuzhiyun #ifdef CONFIG_CMA
45*4882a593Smuzhiyun #define ___GFP_CMA 0x2000000u
46*4882a593Smuzhiyun #else
47*4882a593Smuzhiyun #define ___GFP_CMA 0
48*4882a593Smuzhiyun #endif
49*4882a593Smuzhiyun #ifdef CONFIG_LOCKDEP
50*4882a593Smuzhiyun #ifdef CONFIG_CMA
51*4882a593Smuzhiyun #define ___GFP_NOLOCKDEP 0x4000000u
52*4882a593Smuzhiyun #else
53*4882a593Smuzhiyun #define ___GFP_NOLOCKDEP 0x2000000u
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun #else
56*4882a593Smuzhiyun #define ___GFP_NOLOCKDEP 0
57*4882a593Smuzhiyun #endif
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* If the above are modified, __GFP_BITS_SHIFT may need updating */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * Physical address zone modifiers (see linux/mmzone.h - low four bits)
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Do not put any conditional on these. If necessary modify the definitions
65*4882a593Smuzhiyun * without the underscores and use them consistently. The definitions here may
66*4882a593Smuzhiyun * be used in bit comparisons.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun #define __GFP_DMA ((__force gfp_t)___GFP_DMA)
69*4882a593Smuzhiyun #define __GFP_HIGHMEM ((__force gfp_t)___GFP_HIGHMEM)
70*4882a593Smuzhiyun #define __GFP_DMA32 ((__force gfp_t)___GFP_DMA32)
71*4882a593Smuzhiyun #define __GFP_MOVABLE ((__force gfp_t)___GFP_MOVABLE) /* ZONE_MOVABLE allowed */
72*4882a593Smuzhiyun #define __GFP_CMA ((__force gfp_t)___GFP_CMA)
73*4882a593Smuzhiyun #define GFP_ZONEMASK (__GFP_DMA|__GFP_HIGHMEM|__GFP_DMA32|__GFP_MOVABLE)
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /**
76*4882a593Smuzhiyun * DOC: Page mobility and placement hints
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * Page mobility and placement hints
79*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * These flags provide hints about how mobile the page is. Pages with similar
82*4882a593Smuzhiyun * mobility are placed within the same pageblocks to minimise problems due
83*4882a593Smuzhiyun * to external fragmentation.
84*4882a593Smuzhiyun *
85*4882a593Smuzhiyun * %__GFP_MOVABLE (also a zone modifier) indicates that the page can be
86*4882a593Smuzhiyun * moved by page migration during memory compaction or can be reclaimed.
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * %__GFP_RECLAIMABLE is used for slab allocations that specify
89*4882a593Smuzhiyun * SLAB_RECLAIM_ACCOUNT and whose pages can be freed via shrinkers.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * %__GFP_WRITE indicates the caller intends to dirty the page. Where possible,
92*4882a593Smuzhiyun * these pages will be spread between local zones to avoid all the dirty
93*4882a593Smuzhiyun * pages being in one zone (fair zone allocation policy).
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * %__GFP_HARDWALL enforces the cpuset memory allocation policy.
96*4882a593Smuzhiyun *
97*4882a593Smuzhiyun * %__GFP_THISNODE forces the allocation to be satisfied from the requested
98*4882a593Smuzhiyun * node with no fallbacks or placement policy enforcements.
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * %__GFP_ACCOUNT causes the allocation to be accounted to kmemcg.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun #define __GFP_RECLAIMABLE ((__force gfp_t)___GFP_RECLAIMABLE)
103*4882a593Smuzhiyun #define __GFP_WRITE ((__force gfp_t)___GFP_WRITE)
104*4882a593Smuzhiyun #define __GFP_HARDWALL ((__force gfp_t)___GFP_HARDWALL)
105*4882a593Smuzhiyun #define __GFP_THISNODE ((__force gfp_t)___GFP_THISNODE)
106*4882a593Smuzhiyun #define __GFP_ACCOUNT ((__force gfp_t)___GFP_ACCOUNT)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * DOC: Watermark modifiers
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * Watermark modifiers -- controls access to emergency reserves
112*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * %__GFP_HIGH indicates that the caller is high-priority and that granting
115*4882a593Smuzhiyun * the request is necessary before the system can make forward progress.
116*4882a593Smuzhiyun * For example, creating an IO context to clean pages.
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * %__GFP_ATOMIC indicates that the caller cannot reclaim or sleep and is
119*4882a593Smuzhiyun * high priority. Users are typically interrupt handlers. This may be
120*4882a593Smuzhiyun * used in conjunction with %__GFP_HIGH
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * %__GFP_MEMALLOC allows access to all memory. This should only be used when
123*4882a593Smuzhiyun * the caller guarantees the allocation will allow more memory to be freed
124*4882a593Smuzhiyun * very shortly e.g. process exiting or swapping. Users either should
125*4882a593Smuzhiyun * be the MM or co-ordinating closely with the VM (e.g. swap over NFS).
126*4882a593Smuzhiyun * Users of this flag have to be extremely careful to not deplete the reserve
127*4882a593Smuzhiyun * completely and implement a throttling mechanism which controls the
128*4882a593Smuzhiyun * consumption of the reserve based on the amount of freed memory.
129*4882a593Smuzhiyun * Usage of a pre-allocated pool (e.g. mempool) should be always considered
130*4882a593Smuzhiyun * before using this flag.
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * %__GFP_NOMEMALLOC is used to explicitly forbid access to emergency reserves.
133*4882a593Smuzhiyun * This takes precedence over the %__GFP_MEMALLOC flag if both are set.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun #define __GFP_ATOMIC ((__force gfp_t)___GFP_ATOMIC)
136*4882a593Smuzhiyun #define __GFP_HIGH ((__force gfp_t)___GFP_HIGH)
137*4882a593Smuzhiyun #define __GFP_MEMALLOC ((__force gfp_t)___GFP_MEMALLOC)
138*4882a593Smuzhiyun #define __GFP_NOMEMALLOC ((__force gfp_t)___GFP_NOMEMALLOC)
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * DOC: Reclaim modifiers
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * Reclaim modifiers
144*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~
145*4882a593Smuzhiyun * Please note that all the following flags are only applicable to sleepable
146*4882a593Smuzhiyun * allocations (e.g. %GFP_NOWAIT and %GFP_ATOMIC will ignore them).
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * %__GFP_IO can start physical IO.
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * %__GFP_FS can call down to the low-level FS. Clearing the flag avoids the
151*4882a593Smuzhiyun * allocator recursing into the filesystem which might already be holding
152*4882a593Smuzhiyun * locks.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * %__GFP_DIRECT_RECLAIM indicates that the caller may enter direct reclaim.
155*4882a593Smuzhiyun * This flag can be cleared to avoid unnecessary delays when a fallback
156*4882a593Smuzhiyun * option is available.
157*4882a593Smuzhiyun *
158*4882a593Smuzhiyun * %__GFP_KSWAPD_RECLAIM indicates that the caller wants to wake kswapd when
159*4882a593Smuzhiyun * the low watermark is reached and have it reclaim pages until the high
160*4882a593Smuzhiyun * watermark is reached. A caller may wish to clear this flag when fallback
161*4882a593Smuzhiyun * options are available and the reclaim is likely to disrupt the system. The
162*4882a593Smuzhiyun * canonical example is THP allocation where a fallback is cheap but
163*4882a593Smuzhiyun * reclaim/compaction may cause indirect stalls.
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * %__GFP_RECLAIM is shorthand to allow/forbid both direct and kswapd reclaim.
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * The default allocator behavior depends on the request size. We have a concept
168*4882a593Smuzhiyun * of so called costly allocations (with order > %PAGE_ALLOC_COSTLY_ORDER).
169*4882a593Smuzhiyun * !costly allocations are too essential to fail so they are implicitly
170*4882a593Smuzhiyun * non-failing by default (with some exceptions like OOM victims might fail so
171*4882a593Smuzhiyun * the caller still has to check for failures) while costly requests try to be
172*4882a593Smuzhiyun * not disruptive and back off even without invoking the OOM killer.
173*4882a593Smuzhiyun * The following three modifiers might be used to override some of these
174*4882a593Smuzhiyun * implicit rules
175*4882a593Smuzhiyun *
176*4882a593Smuzhiyun * %__GFP_NORETRY: The VM implementation will try only very lightweight
177*4882a593Smuzhiyun * memory direct reclaim to get some memory under memory pressure (thus
178*4882a593Smuzhiyun * it can sleep). It will avoid disruptive actions like OOM killer. The
179*4882a593Smuzhiyun * caller must handle the failure which is quite likely to happen under
180*4882a593Smuzhiyun * heavy memory pressure. The flag is suitable when failure can easily be
181*4882a593Smuzhiyun * handled at small cost, such as reduced throughput
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * %__GFP_RETRY_MAYFAIL: The VM implementation will retry memory reclaim
184*4882a593Smuzhiyun * procedures that have previously failed if there is some indication
185*4882a593Smuzhiyun * that progress has been made else where. It can wait for other
186*4882a593Smuzhiyun * tasks to attempt high level approaches to freeing memory such as
187*4882a593Smuzhiyun * compaction (which removes fragmentation) and page-out.
188*4882a593Smuzhiyun * There is still a definite limit to the number of retries, but it is
189*4882a593Smuzhiyun * a larger limit than with %__GFP_NORETRY.
190*4882a593Smuzhiyun * Allocations with this flag may fail, but only when there is
191*4882a593Smuzhiyun * genuinely little unused memory. While these allocations do not
192*4882a593Smuzhiyun * directly trigger the OOM killer, their failure indicates that
193*4882a593Smuzhiyun * the system is likely to need to use the OOM killer soon. The
194*4882a593Smuzhiyun * caller must handle failure, but can reasonably do so by failing
195*4882a593Smuzhiyun * a higher-level request, or completing it only in a much less
196*4882a593Smuzhiyun * efficient manner.
197*4882a593Smuzhiyun * If the allocation does fail, and the caller is in a position to
198*4882a593Smuzhiyun * free some non-essential memory, doing so could benefit the system
199*4882a593Smuzhiyun * as a whole.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * %__GFP_NOFAIL: The VM implementation _must_ retry infinitely: the caller
202*4882a593Smuzhiyun * cannot handle allocation failures. The allocation could block
203*4882a593Smuzhiyun * indefinitely but will never return with failure. Testing for
204*4882a593Smuzhiyun * failure is pointless.
205*4882a593Smuzhiyun * New users should be evaluated carefully (and the flag should be
206*4882a593Smuzhiyun * used only when there is no reasonable failure policy) but it is
207*4882a593Smuzhiyun * definitely preferable to use the flag rather than opencode endless
208*4882a593Smuzhiyun * loop around allocator.
209*4882a593Smuzhiyun * Using this flag for costly allocations is _highly_ discouraged.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun #define __GFP_IO ((__force gfp_t)___GFP_IO)
212*4882a593Smuzhiyun #define __GFP_FS ((__force gfp_t)___GFP_FS)
213*4882a593Smuzhiyun #define __GFP_DIRECT_RECLAIM ((__force gfp_t)___GFP_DIRECT_RECLAIM) /* Caller can reclaim */
214*4882a593Smuzhiyun #define __GFP_KSWAPD_RECLAIM ((__force gfp_t)___GFP_KSWAPD_RECLAIM) /* kswapd can wake */
215*4882a593Smuzhiyun #define __GFP_RECLAIM ((__force gfp_t)(___GFP_DIRECT_RECLAIM|___GFP_KSWAPD_RECLAIM))
216*4882a593Smuzhiyun #define __GFP_RETRY_MAYFAIL ((__force gfp_t)___GFP_RETRY_MAYFAIL)
217*4882a593Smuzhiyun #define __GFP_NOFAIL ((__force gfp_t)___GFP_NOFAIL)
218*4882a593Smuzhiyun #define __GFP_NORETRY ((__force gfp_t)___GFP_NORETRY)
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun * DOC: Action modifiers
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * Action modifiers
224*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * %__GFP_NOWARN suppresses allocation failure reports.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * %__GFP_COMP address compound page metadata.
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * %__GFP_ZERO returns a zeroed page on success.
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * %__GFP_ZEROTAGS returns a page with zeroed memory tags on success, if
233*4882a593Smuzhiyun * __GFP_ZERO is set.
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * %__GFP_SKIP_KASAN_POISON returns a page which does not need to be poisoned
236*4882a593Smuzhiyun * on deallocation. Typically used for userspace pages. Currently only has an
237*4882a593Smuzhiyun * effect in HW tags mode.
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun #define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)
240*4882a593Smuzhiyun #define __GFP_COMP ((__force gfp_t)___GFP_COMP)
241*4882a593Smuzhiyun #define __GFP_ZERO ((__force gfp_t)___GFP_ZERO)
242*4882a593Smuzhiyun #define __GFP_ZEROTAGS ((__force gfp_t)___GFP_ZEROTAGS)
243*4882a593Smuzhiyun #define __GFP_SKIP_KASAN_POISON ((__force gfp_t)___GFP_SKIP_KASAN_POISON)
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* Disable lockdep for GFP context tracking */
246*4882a593Smuzhiyun #define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /* Room for N __GFP_FOO bits */
249*4882a593Smuzhiyun #ifdef CONFIG_CMA
250*4882a593Smuzhiyun #define __GFP_BITS_SHIFT (26 + IS_ENABLED(CONFIG_LOCKDEP))
251*4882a593Smuzhiyun #else
252*4882a593Smuzhiyun #define __GFP_BITS_SHIFT (25 + IS_ENABLED(CONFIG_LOCKDEP))
253*4882a593Smuzhiyun #endif
254*4882a593Smuzhiyun #define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun * DOC: Useful GFP flag combinations
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Useful GFP flag combinations
260*4882a593Smuzhiyun * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261*4882a593Smuzhiyun *
262*4882a593Smuzhiyun * Useful GFP flag combinations that are commonly used. It is recommended
263*4882a593Smuzhiyun * that subsystems start with one of these combinations and then set/clear
264*4882a593Smuzhiyun * %__GFP_FOO flags as necessary.
265*4882a593Smuzhiyun *
266*4882a593Smuzhiyun * %GFP_ATOMIC users can not sleep and need the allocation to succeed. A lower
267*4882a593Smuzhiyun * watermark is applied to allow access to "atomic reserves".
268*4882a593Smuzhiyun * The current implementation doesn't support NMI and few other strict
269*4882a593Smuzhiyun * non-preemptive contexts (e.g. raw_spin_lock). The same applies to %GFP_NOWAIT.
270*4882a593Smuzhiyun *
271*4882a593Smuzhiyun * %GFP_KERNEL is typical for kernel-internal allocations. The caller requires
272*4882a593Smuzhiyun * %ZONE_NORMAL or a lower zone for direct access but can direct reclaim.
273*4882a593Smuzhiyun *
274*4882a593Smuzhiyun * %GFP_KERNEL_ACCOUNT is the same as GFP_KERNEL, except the allocation is
275*4882a593Smuzhiyun * accounted to kmemcg.
276*4882a593Smuzhiyun *
277*4882a593Smuzhiyun * %GFP_NOWAIT is for kernel allocations that should not stall for direct
278*4882a593Smuzhiyun * reclaim, start physical IO or use any filesystem callback.
279*4882a593Smuzhiyun *
280*4882a593Smuzhiyun * %GFP_NOIO will use direct reclaim to discard clean pages or slab pages
281*4882a593Smuzhiyun * that do not require the starting of any physical IO.
282*4882a593Smuzhiyun * Please try to avoid using this flag directly and instead use
283*4882a593Smuzhiyun * memalloc_noio_{save,restore} to mark the whole scope which cannot
284*4882a593Smuzhiyun * perform any IO with a short explanation why. All allocation requests
285*4882a593Smuzhiyun * will inherit GFP_NOIO implicitly.
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * %GFP_NOFS will use direct reclaim but will not use any filesystem interfaces.
288*4882a593Smuzhiyun * Please try to avoid using this flag directly and instead use
289*4882a593Smuzhiyun * memalloc_nofs_{save,restore} to mark the whole scope which cannot/shouldn't
290*4882a593Smuzhiyun * recurse into the FS layer with a short explanation why. All allocation
291*4882a593Smuzhiyun * requests will inherit GFP_NOFS implicitly.
292*4882a593Smuzhiyun *
293*4882a593Smuzhiyun * %GFP_USER is for userspace allocations that also need to be directly
294*4882a593Smuzhiyun * accessibly by the kernel or hardware. It is typically used by hardware
295*4882a593Smuzhiyun * for buffers that are mapped to userspace (e.g. graphics) that hardware
296*4882a593Smuzhiyun * still must DMA to. cpuset limits are enforced for these allocations.
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * %GFP_DMA exists for historical reasons and should be avoided where possible.
299*4882a593Smuzhiyun * The flags indicates that the caller requires that the lowest zone be
300*4882a593Smuzhiyun * used (%ZONE_DMA or 16M on x86-64). Ideally, this would be removed but
301*4882a593Smuzhiyun * it would require careful auditing as some users really require it and
302*4882a593Smuzhiyun * others use the flag to avoid lowmem reserves in %ZONE_DMA and treat the
303*4882a593Smuzhiyun * lowest zone as a type of emergency reserve.
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * %GFP_DMA32 is similar to %GFP_DMA except that the caller requires a 32-bit
306*4882a593Smuzhiyun * address.
307*4882a593Smuzhiyun *
308*4882a593Smuzhiyun * %GFP_HIGHUSER is for userspace allocations that may be mapped to userspace,
309*4882a593Smuzhiyun * do not need to be directly accessible by the kernel but that cannot
310*4882a593Smuzhiyun * move once in use. An example may be a hardware allocation that maps
311*4882a593Smuzhiyun * data directly into userspace but has no addressing limitations.
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * %GFP_HIGHUSER_MOVABLE is for userspace allocations that the kernel does not
314*4882a593Smuzhiyun * need direct access to but can use kmap() when access is required. They
315*4882a593Smuzhiyun * are expected to be movable via page reclaim or page migration. Typically,
316*4882a593Smuzhiyun * pages on the LRU would also be allocated with %GFP_HIGHUSER_MOVABLE.
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * %GFP_TRANSHUGE and %GFP_TRANSHUGE_LIGHT are used for THP allocations. They
319*4882a593Smuzhiyun * are compound allocations that will generally fail quickly if memory is not
320*4882a593Smuzhiyun * available and will not wake kswapd/kcompactd on failure. The _LIGHT
321*4882a593Smuzhiyun * version does not attempt reclaim/compaction at all and is by default used
322*4882a593Smuzhiyun * in page fault path, while the non-light is used by khugepaged.
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun #define GFP_ATOMIC (__GFP_HIGH|__GFP_ATOMIC|__GFP_KSWAPD_RECLAIM)
325*4882a593Smuzhiyun #define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS)
326*4882a593Smuzhiyun #define GFP_KERNEL_ACCOUNT (GFP_KERNEL | __GFP_ACCOUNT)
327*4882a593Smuzhiyun #define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM)
328*4882a593Smuzhiyun #define GFP_NOIO (__GFP_RECLAIM)
329*4882a593Smuzhiyun #define GFP_NOFS (__GFP_RECLAIM | __GFP_IO)
330*4882a593Smuzhiyun #define GFP_USER (__GFP_RECLAIM | __GFP_IO | __GFP_FS | __GFP_HARDWALL)
331*4882a593Smuzhiyun #define GFP_DMA __GFP_DMA
332*4882a593Smuzhiyun #define GFP_DMA32 __GFP_DMA32
333*4882a593Smuzhiyun #define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM)
334*4882a593Smuzhiyun #define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | \
335*4882a593Smuzhiyun __GFP_SKIP_KASAN_POISON)
336*4882a593Smuzhiyun #define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
337*4882a593Smuzhiyun __GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
338*4882a593Smuzhiyun #define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /* Convert GFP flags to their corresponding migrate type */
341*4882a593Smuzhiyun #define GFP_MOVABLE_MASK (__GFP_RECLAIMABLE|__GFP_MOVABLE)
342*4882a593Smuzhiyun #define GFP_MOVABLE_SHIFT 3
343*4882a593Smuzhiyun
gfp_migratetype(const gfp_t gfp_flags)344*4882a593Smuzhiyun static inline int gfp_migratetype(const gfp_t gfp_flags)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun VM_WARN_ON((gfp_flags & GFP_MOVABLE_MASK) == GFP_MOVABLE_MASK);
347*4882a593Smuzhiyun BUILD_BUG_ON((1UL << GFP_MOVABLE_SHIFT) != ___GFP_MOVABLE);
348*4882a593Smuzhiyun BUILD_BUG_ON((___GFP_MOVABLE >> GFP_MOVABLE_SHIFT) != MIGRATE_MOVABLE);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (unlikely(page_group_by_mobility_disabled))
351*4882a593Smuzhiyun return MIGRATE_UNMOVABLE;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* Group based on mobility */
354*4882a593Smuzhiyun return (gfp_flags & GFP_MOVABLE_MASK) >> GFP_MOVABLE_SHIFT;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun #undef GFP_MOVABLE_MASK
357*4882a593Smuzhiyun #undef GFP_MOVABLE_SHIFT
358*4882a593Smuzhiyun
gfpflags_allow_blocking(const gfp_t gfp_flags)359*4882a593Smuzhiyun static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun return !!(gfp_flags & __GFP_DIRECT_RECLAIM);
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun * gfpflags_normal_context - is gfp_flags a normal sleepable context?
366*4882a593Smuzhiyun * @gfp_flags: gfp_flags to test
367*4882a593Smuzhiyun *
368*4882a593Smuzhiyun * Test whether @gfp_flags indicates that the allocation is from the
369*4882a593Smuzhiyun * %current context and allowed to sleep.
370*4882a593Smuzhiyun *
371*4882a593Smuzhiyun * An allocation being allowed to block doesn't mean it owns the %current
372*4882a593Smuzhiyun * context. When direct reclaim path tries to allocate memory, the
373*4882a593Smuzhiyun * allocation context is nested inside whatever %current was doing at the
374*4882a593Smuzhiyun * time of the original allocation. The nested allocation may be allowed
375*4882a593Smuzhiyun * to block but modifying anything %current owns can corrupt the outer
376*4882a593Smuzhiyun * context's expectations.
377*4882a593Smuzhiyun *
378*4882a593Smuzhiyun * %true result from this function indicates that the allocation context
379*4882a593Smuzhiyun * can sleep and use anything that's associated with %current.
380*4882a593Smuzhiyun */
gfpflags_normal_context(const gfp_t gfp_flags)381*4882a593Smuzhiyun static inline bool gfpflags_normal_context(const gfp_t gfp_flags)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun return (gfp_flags & (__GFP_DIRECT_RECLAIM | __GFP_MEMALLOC)) ==
384*4882a593Smuzhiyun __GFP_DIRECT_RECLAIM;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun #ifdef CONFIG_HIGHMEM
388*4882a593Smuzhiyun #define OPT_ZONE_HIGHMEM ZONE_HIGHMEM
389*4882a593Smuzhiyun #else
390*4882a593Smuzhiyun #define OPT_ZONE_HIGHMEM ZONE_NORMAL
391*4882a593Smuzhiyun #endif
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun #ifdef CONFIG_ZONE_DMA
394*4882a593Smuzhiyun #define OPT_ZONE_DMA ZONE_DMA
395*4882a593Smuzhiyun #else
396*4882a593Smuzhiyun #define OPT_ZONE_DMA ZONE_NORMAL
397*4882a593Smuzhiyun #endif
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun #ifdef CONFIG_ZONE_DMA32
400*4882a593Smuzhiyun #define OPT_ZONE_DMA32 ZONE_DMA32
401*4882a593Smuzhiyun #else
402*4882a593Smuzhiyun #define OPT_ZONE_DMA32 ZONE_NORMAL
403*4882a593Smuzhiyun #endif
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun * GFP_ZONE_TABLE is a word size bitstring that is used for looking up the
407*4882a593Smuzhiyun * zone to use given the lowest 4 bits of gfp_t. Entries are GFP_ZONES_SHIFT
408*4882a593Smuzhiyun * bits long and there are 16 of them to cover all possible combinations of
409*4882a593Smuzhiyun * __GFP_DMA, __GFP_DMA32, __GFP_MOVABLE and __GFP_HIGHMEM.
410*4882a593Smuzhiyun *
411*4882a593Smuzhiyun * The zone fallback order is MOVABLE=>HIGHMEM=>NORMAL=>DMA32=>DMA.
412*4882a593Smuzhiyun * But GFP_MOVABLE is not only a zone specifier but also an allocation
413*4882a593Smuzhiyun * policy. Therefore __GFP_MOVABLE plus another zone selector is valid.
414*4882a593Smuzhiyun * Only 1 bit of the lowest 3 bits (DMA,DMA32,HIGHMEM) can be set to "1".
415*4882a593Smuzhiyun *
416*4882a593Smuzhiyun * bit result
417*4882a593Smuzhiyun * =================
418*4882a593Smuzhiyun * 0x0 => NORMAL
419*4882a593Smuzhiyun * 0x1 => DMA or NORMAL
420*4882a593Smuzhiyun * 0x2 => HIGHMEM or NORMAL
421*4882a593Smuzhiyun * 0x3 => BAD (DMA+HIGHMEM)
422*4882a593Smuzhiyun * 0x4 => DMA32 or NORMAL
423*4882a593Smuzhiyun * 0x5 => BAD (DMA+DMA32)
424*4882a593Smuzhiyun * 0x6 => BAD (HIGHMEM+DMA32)
425*4882a593Smuzhiyun * 0x7 => BAD (HIGHMEM+DMA32+DMA)
426*4882a593Smuzhiyun * 0x8 => NORMAL (MOVABLE+0)
427*4882a593Smuzhiyun * 0x9 => DMA or NORMAL (MOVABLE+DMA)
428*4882a593Smuzhiyun * 0xa => MOVABLE (Movable is valid only if HIGHMEM is set too)
429*4882a593Smuzhiyun * 0xb => BAD (MOVABLE+HIGHMEM+DMA)
430*4882a593Smuzhiyun * 0xc => DMA32 or NORMAL (MOVABLE+DMA32)
431*4882a593Smuzhiyun * 0xd => BAD (MOVABLE+DMA32+DMA)
432*4882a593Smuzhiyun * 0xe => BAD (MOVABLE+DMA32+HIGHMEM)
433*4882a593Smuzhiyun * 0xf => BAD (MOVABLE+DMA32+HIGHMEM+DMA)
434*4882a593Smuzhiyun *
435*4882a593Smuzhiyun * GFP_ZONES_SHIFT must be <= 2 on 32 bit platforms.
436*4882a593Smuzhiyun */
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun #if defined(CONFIG_ZONE_DEVICE) && (MAX_NR_ZONES-1) <= 4
439*4882a593Smuzhiyun /* ZONE_DEVICE is not a valid GFP zone specifier */
440*4882a593Smuzhiyun #define GFP_ZONES_SHIFT 2
441*4882a593Smuzhiyun #else
442*4882a593Smuzhiyun #define GFP_ZONES_SHIFT ZONES_SHIFT
443*4882a593Smuzhiyun #endif
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun #if 16 * GFP_ZONES_SHIFT > BITS_PER_LONG
446*4882a593Smuzhiyun #error GFP_ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
447*4882a593Smuzhiyun #endif
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun #define GFP_ZONE_TABLE ( \
450*4882a593Smuzhiyun (ZONE_NORMAL << 0 * GFP_ZONES_SHIFT) \
451*4882a593Smuzhiyun | (OPT_ZONE_DMA << ___GFP_DMA * GFP_ZONES_SHIFT) \
452*4882a593Smuzhiyun | (OPT_ZONE_HIGHMEM << ___GFP_HIGHMEM * GFP_ZONES_SHIFT) \
453*4882a593Smuzhiyun | (OPT_ZONE_DMA32 << ___GFP_DMA32 * GFP_ZONES_SHIFT) \
454*4882a593Smuzhiyun | (ZONE_NORMAL << ___GFP_MOVABLE * GFP_ZONES_SHIFT) \
455*4882a593Smuzhiyun | (OPT_ZONE_DMA << (___GFP_MOVABLE | ___GFP_DMA) * GFP_ZONES_SHIFT) \
456*4882a593Smuzhiyun | (ZONE_MOVABLE << (___GFP_MOVABLE | ___GFP_HIGHMEM) * GFP_ZONES_SHIFT)\
457*4882a593Smuzhiyun | (OPT_ZONE_DMA32 << (___GFP_MOVABLE | ___GFP_DMA32) * GFP_ZONES_SHIFT)\
458*4882a593Smuzhiyun )
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /*
461*4882a593Smuzhiyun * GFP_ZONE_BAD is a bitmap for all combinations of __GFP_DMA, __GFP_DMA32
462*4882a593Smuzhiyun * __GFP_HIGHMEM and __GFP_MOVABLE that are not permitted. One flag per
463*4882a593Smuzhiyun * entry starting with bit 0. Bit is set if the combination is not
464*4882a593Smuzhiyun * allowed.
465*4882a593Smuzhiyun */
466*4882a593Smuzhiyun #define GFP_ZONE_BAD ( \
467*4882a593Smuzhiyun 1 << (___GFP_DMA | ___GFP_HIGHMEM) \
468*4882a593Smuzhiyun | 1 << (___GFP_DMA | ___GFP_DMA32) \
469*4882a593Smuzhiyun | 1 << (___GFP_DMA32 | ___GFP_HIGHMEM) \
470*4882a593Smuzhiyun | 1 << (___GFP_DMA | ___GFP_DMA32 | ___GFP_HIGHMEM) \
471*4882a593Smuzhiyun | 1 << (___GFP_MOVABLE | ___GFP_HIGHMEM | ___GFP_DMA) \
472*4882a593Smuzhiyun | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA) \
473*4882a593Smuzhiyun | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_HIGHMEM) \
474*4882a593Smuzhiyun | 1 << (___GFP_MOVABLE | ___GFP_DMA32 | ___GFP_DMA | ___GFP_HIGHMEM) \
475*4882a593Smuzhiyun )
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun enum zone_type gfp_zone(gfp_t flags);
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /*
480*4882a593Smuzhiyun * There is only one page-allocator function, and two main namespaces to
481*4882a593Smuzhiyun * it. The alloc_page*() variants return 'struct page *' and as such
482*4882a593Smuzhiyun * can allocate highmem pages, the *get*page*() variants return
483*4882a593Smuzhiyun * virtual kernel addresses to the allocated page(s).
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun
gfp_zonelist(gfp_t flags)486*4882a593Smuzhiyun static inline int gfp_zonelist(gfp_t flags)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun #ifdef CONFIG_NUMA
489*4882a593Smuzhiyun if (unlikely(flags & __GFP_THISNODE))
490*4882a593Smuzhiyun return ZONELIST_NOFALLBACK;
491*4882a593Smuzhiyun #endif
492*4882a593Smuzhiyun return ZONELIST_FALLBACK;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun * We get the zone list from the current node and the gfp_mask.
497*4882a593Smuzhiyun * This zone list contains a maximum of MAXNODES*MAX_NR_ZONES zones.
498*4882a593Smuzhiyun * There are two zonelists per node, one for all zones with memory and
499*4882a593Smuzhiyun * one containing just zones from the node the zonelist belongs to.
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * For the normal case of non-DISCONTIGMEM systems the NODE_DATA() gets
502*4882a593Smuzhiyun * optimized to &contig_page_data at compile-time.
503*4882a593Smuzhiyun */
node_zonelist(int nid,gfp_t flags)504*4882a593Smuzhiyun static inline struct zonelist *node_zonelist(int nid, gfp_t flags)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun return NODE_DATA(nid)->node_zonelists + gfp_zonelist(flags);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun #ifndef HAVE_ARCH_FREE_PAGE
arch_free_page(struct page * page,int order)510*4882a593Smuzhiyun static inline void arch_free_page(struct page *page, int order) { }
511*4882a593Smuzhiyun #endif
512*4882a593Smuzhiyun #ifndef HAVE_ARCH_ALLOC_PAGE
arch_alloc_page(struct page * page,int order)513*4882a593Smuzhiyun static inline void arch_alloc_page(struct page *page, int order) { }
514*4882a593Smuzhiyun #endif
515*4882a593Smuzhiyun #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
arch_make_page_accessible(struct page * page)516*4882a593Smuzhiyun static inline int arch_make_page_accessible(struct page *page)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun return 0;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun #endif
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun struct page *
523*4882a593Smuzhiyun __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
524*4882a593Smuzhiyun nodemask_t *nodemask);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun static inline struct page *
__alloc_pages(gfp_t gfp_mask,unsigned int order,int preferred_nid)527*4882a593Smuzhiyun __alloc_pages(gfp_t gfp_mask, unsigned int order, int preferred_nid)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun return __alloc_pages_nodemask(gfp_mask, order, preferred_nid, NULL);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun /*
533*4882a593Smuzhiyun * Allocate pages, preferring the node given as nid. The node must be valid and
534*4882a593Smuzhiyun * online. For more general interface, see alloc_pages_node().
535*4882a593Smuzhiyun */
536*4882a593Smuzhiyun static inline struct page *
__alloc_pages_node(int nid,gfp_t gfp_mask,unsigned int order)537*4882a593Smuzhiyun __alloc_pages_node(int nid, gfp_t gfp_mask, unsigned int order)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun VM_BUG_ON(nid < 0 || nid >= MAX_NUMNODES);
540*4882a593Smuzhiyun VM_WARN_ON((gfp_mask & __GFP_THISNODE) && !node_online(nid));
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun return __alloc_pages(gfp_mask, order, nid);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /*
546*4882a593Smuzhiyun * Allocate pages, preferring the node given as nid. When nid == NUMA_NO_NODE,
547*4882a593Smuzhiyun * prefer the current CPU's closest node. Otherwise node must be valid and
548*4882a593Smuzhiyun * online.
549*4882a593Smuzhiyun */
alloc_pages_node(int nid,gfp_t gfp_mask,unsigned int order)550*4882a593Smuzhiyun static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,
551*4882a593Smuzhiyun unsigned int order)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun if (nid == NUMA_NO_NODE)
554*4882a593Smuzhiyun nid = numa_mem_id();
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun return __alloc_pages_node(nid, gfp_mask, order);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun #ifdef CONFIG_NUMA
560*4882a593Smuzhiyun extern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun static inline struct page *
alloc_pages(gfp_t gfp_mask,unsigned int order)563*4882a593Smuzhiyun alloc_pages(gfp_t gfp_mask, unsigned int order)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun return alloc_pages_current(gfp_mask, order);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun extern struct page *alloc_pages_vma(gfp_t gfp_mask, int order,
568*4882a593Smuzhiyun struct vm_area_struct *vma, unsigned long addr,
569*4882a593Smuzhiyun int node, bool hugepage);
570*4882a593Smuzhiyun #define alloc_hugepage_vma(gfp_mask, vma, addr, order) \
571*4882a593Smuzhiyun alloc_pages_vma(gfp_mask, order, vma, addr, numa_node_id(), true)
572*4882a593Smuzhiyun #else
alloc_pages(gfp_t gfp_mask,unsigned int order)573*4882a593Smuzhiyun static inline struct page *alloc_pages(gfp_t gfp_mask, unsigned int order)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun return alloc_pages_node(numa_node_id(), gfp_mask, order);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun #define alloc_pages_vma(gfp_mask, order, vma, addr, node, false)\
578*4882a593Smuzhiyun alloc_pages(gfp_mask, order)
579*4882a593Smuzhiyun #define alloc_hugepage_vma(gfp_mask, vma, addr, order) \
580*4882a593Smuzhiyun alloc_pages(gfp_mask, order)
581*4882a593Smuzhiyun #endif
582*4882a593Smuzhiyun #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
583*4882a593Smuzhiyun #define alloc_page_vma(gfp_mask, vma, addr) \
584*4882a593Smuzhiyun alloc_pages_vma(gfp_mask, 0, vma, addr, numa_node_id(), false)
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun extern unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order);
587*4882a593Smuzhiyun extern unsigned long get_zeroed_page(gfp_t gfp_mask);
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun void *alloc_pages_exact(size_t size, gfp_t gfp_mask);
590*4882a593Smuzhiyun void free_pages_exact(void *virt, size_t size);
591*4882a593Smuzhiyun void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask);
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun #define __get_free_page(gfp_mask) \
594*4882a593Smuzhiyun __get_free_pages((gfp_mask), 0)
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun #define __get_dma_pages(gfp_mask, order) \
597*4882a593Smuzhiyun __get_free_pages((gfp_mask) | GFP_DMA, (order))
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun extern void __free_pages(struct page *page, unsigned int order);
600*4882a593Smuzhiyun extern void free_pages(unsigned long addr, unsigned int order);
601*4882a593Smuzhiyun extern void free_unref_page(struct page *page);
602*4882a593Smuzhiyun extern void free_unref_page_list(struct list_head *list);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun struct page_frag_cache;
605*4882a593Smuzhiyun extern void __page_frag_cache_drain(struct page *page, unsigned int count);
606*4882a593Smuzhiyun extern void *page_frag_alloc(struct page_frag_cache *nc,
607*4882a593Smuzhiyun unsigned int fragsz, gfp_t gfp_mask);
608*4882a593Smuzhiyun extern void page_frag_free(void *addr);
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun #define __free_page(page) __free_pages((page), 0)
611*4882a593Smuzhiyun #define free_page(addr) free_pages((addr), 0)
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun void page_alloc_init(void);
614*4882a593Smuzhiyun void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp);
615*4882a593Smuzhiyun void drain_all_pages(struct zone *zone);
616*4882a593Smuzhiyun void drain_local_pages(struct zone *zone);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun void page_alloc_init_late(void);
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun /*
621*4882a593Smuzhiyun * gfp_allowed_mask is set to GFP_BOOT_MASK during early boot to restrict what
622*4882a593Smuzhiyun * GFP flags are used before interrupts are enabled. Once interrupts are
623*4882a593Smuzhiyun * enabled, it is set to __GFP_BITS_MASK while the system is running. During
624*4882a593Smuzhiyun * hibernation, it is used by PM to avoid I/O during memory allocation while
625*4882a593Smuzhiyun * devices are suspended.
626*4882a593Smuzhiyun */
627*4882a593Smuzhiyun extern gfp_t gfp_allowed_mask;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /* Returns true if the gfp_mask allows use of ALLOC_NO_WATERMARK */
630*4882a593Smuzhiyun bool gfp_pfmemalloc_allowed(gfp_t gfp_mask);
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun extern void pm_restrict_gfp_mask(void);
633*4882a593Smuzhiyun extern void pm_restore_gfp_mask(void);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
636*4882a593Smuzhiyun extern bool pm_suspended_storage(void);
637*4882a593Smuzhiyun #else
pm_suspended_storage(void)638*4882a593Smuzhiyun static inline bool pm_suspended_storage(void)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun return false;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun #ifdef CONFIG_CONTIG_ALLOC
645*4882a593Smuzhiyun extern unsigned long pfn_max_align_up(unsigned long pfn);
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun #define ACR_ERR_ISOLATE (1 << 0)
648*4882a593Smuzhiyun #define ACR_ERR_MIGRATE (1 << 1)
649*4882a593Smuzhiyun #define ACR_ERR_TEST (1 << 2)
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun struct acr_info {
652*4882a593Smuzhiyun unsigned long nr_mapped;
653*4882a593Smuzhiyun unsigned long nr_migrated;
654*4882a593Smuzhiyun unsigned long nr_reclaimed;
655*4882a593Smuzhiyun unsigned int err;
656*4882a593Smuzhiyun unsigned long failed_pfn;
657*4882a593Smuzhiyun };
658*4882a593Smuzhiyun
659*4882a593Smuzhiyun /* The below functions must be run on a range from a single zone. */
660*4882a593Smuzhiyun extern int alloc_contig_range(unsigned long start, unsigned long end,
661*4882a593Smuzhiyun unsigned migratetype, gfp_t gfp_mask,
662*4882a593Smuzhiyun struct acr_info *info);
663*4882a593Smuzhiyun extern struct page *alloc_contig_pages(unsigned long nr_pages, gfp_t gfp_mask,
664*4882a593Smuzhiyun int nid, nodemask_t *nodemask);
665*4882a593Smuzhiyun #endif
666*4882a593Smuzhiyun void free_contig_range(unsigned long pfn, unsigned int nr_pages);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun #ifdef CONFIG_CMA
669*4882a593Smuzhiyun /* CMA stuff */
670*4882a593Smuzhiyun extern void init_cma_reserved_pageblock(struct page *page);
671*4882a593Smuzhiyun #endif
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun #endif /* __LINUX_GFP_H */
674