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