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