xref: /linux/include/linux/vmalloc.h (revision 254e7a129e47084ba5710d85d605dc5bc56a06bf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VMALLOC_H
3 #define _LINUX_VMALLOC_H
4 
5 #include <linux/alloc_tag.h>
6 #include <linux/cleanup.h>
7 #include <linux/sched.h>
8 #include <linux/spinlock.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/llist.h>
12 #include <asm/page.h>		/* pgprot_t */
13 #include <linux/rbtree.h>
14 #include <linux/overflow.h>
15 
16 #include <asm/vmalloc.h>
17 
18 struct vm_area_struct;		/* vma defining user mapping in mm_types.h */
19 struct notifier_block;		/* in notifier.h */
20 struct iov_iter;		/* in uio.h */
21 
22 /* bits in flags of vmalloc's vm_struct below */
23 #define VM_IOREMAP		0x00000001	/* ioremap() and friends */
24 #define VM_ALLOC		0x00000002	/* vmalloc() */
25 #define VM_MAP			0x00000004	/* vmap()ed pages */
26 #define VM_USERMAP		0x00000008	/* suitable for remap_vmalloc_range */
27 #define VM_DMA_COHERENT		0x00000010	/* dma_alloc_coherent */
28 #define VM_UNINITIALIZED	0x00000020	/* vm_struct is not fully initialized */
29 #define VM_NO_GUARD		0x00000040      /* ***DANGEROUS*** don't add guard page */
30 #define VM_KASAN		0x00000080      /* has allocated kasan shadow memory */
31 #define VM_FLUSH_RESET_PERMS	0x00000100	/* reset direct map and flush TLB on unmap, can't be freed in atomic context */
32 #define VM_MAP_PUT_PAGES	0x00000200	/* put pages and free array in vfree */
33 #define VM_ALLOW_HUGE_VMAP	0x00000400      /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */
34 
35 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
36 	!defined(CONFIG_KASAN_VMALLOC)
37 #define VM_DEFER_KMEMLEAK	0x00000800	/* defer kmemleak object creation */
38 #else
39 #define VM_DEFER_KMEMLEAK	0
40 #endif
41 #define VM_SPARSE		0x00001000	/* sparse vm_area. not all pages are present. */
42 #define VM_REQUIRE_HUGE_VMAP	0x00002000	/* huge page mapping or nothing */
43 
44 /* bits [20..32] reserved for arch specific ioremap internals */
45 
46 /*
47  * Maximum alignment for ioremap() regions.
48  * Can be overridden by arch-specific value.
49  */
50 #ifndef IOREMAP_MAX_ORDER
51 #define IOREMAP_MAX_ORDER	(7 + PAGE_SHIFT)	/* 128 pages */
52 #endif
53 
54 struct vm_struct {
55 	union {
56 		struct vm_struct *next;	  /* Early registration of vm_areas. */
57 		struct llist_node llnode; /* Asynchronous freeing on error paths. */
58 	};
59 
60 	void			*addr;
61 	unsigned long		size;
62 	unsigned long		flags;
63 	struct page		**pages;
64 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
65 	unsigned int		page_order;
66 #endif
67 	unsigned long		nr_pages;
68 	phys_addr_t		phys_addr;
69 	const void		*caller;
70 	unsigned long		requested_size;
71 };
72 
73 struct vmap_area {
74 	unsigned long va_start;
75 	unsigned long va_end;
76 
77 	struct rb_node rb_node;         /* address sorted rbtree */
78 	struct list_head list;          /* address sorted list */
79 
80 	/*
81 	 * The following two variables can be packed, because
82 	 * a vmap_area object can be either:
83 	 *    1) in "free" tree (root is free_vmap_area_root)
84 	 *    2) or "busy" tree (root is vmap_area_root)
85 	 */
86 	union {
87 		unsigned long subtree_max_size; /* in "free" tree */
88 		struct vm_struct *vm;           /* in "busy" tree */
89 	};
90 	unsigned long flags; /* mark type of vm_map_ram area */
91 };
92 
93 /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
94 #ifndef arch_vmap_p4d_supported
95 static inline bool arch_vmap_p4d_supported(pgprot_t prot)
96 {
97 	return false;
98 }
99 #endif
100 
101 #ifndef arch_vmap_pud_supported
102 static inline bool arch_vmap_pud_supported(pgprot_t prot)
103 {
104 	return false;
105 }
106 #endif
107 
108 #ifndef arch_vmap_pmd_supported
109 static inline bool arch_vmap_pmd_supported(pgprot_t prot)
110 {
111 	return false;
112 }
113 #endif
114 
115 #ifndef arch_vmap_pte_range_map_size
116 static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end,
117 							 u64 pfn, unsigned int max_page_shift)
118 {
119 	return PAGE_SIZE;
120 }
121 #endif
122 
123 #ifndef arch_vmap_pte_range_unmap_size
124 static inline unsigned long arch_vmap_pte_range_unmap_size(unsigned long addr,
125 							   pte_t *ptep)
126 {
127 	return PAGE_SIZE;
128 }
129 #endif
130 
131 #ifndef arch_vmap_pte_supported_shift
132 static inline int arch_vmap_pte_supported_shift(unsigned long size)
133 {
134 	return PAGE_SHIFT;
135 }
136 #endif
137 
138 #ifndef arch_vmap_pgprot_tagged
139 static inline pgprot_t arch_vmap_pgprot_tagged(pgprot_t prot)
140 {
141 	return prot;
142 }
143 #endif
144 
145 /*
146  *	Highlevel APIs for driver use
147  */
148 extern void vm_unmap_ram(const void *mem, unsigned int count);
149 extern void *vm_map_ram(struct page **pages, unsigned int count, int node);
150 extern void vm_unmap_aliases(void);
151 
152 extern void *vmalloc_noprof(unsigned long size) __alloc_size(1);
153 #define vmalloc(...)		alloc_hooks(vmalloc_noprof(__VA_ARGS__))
154 
155 extern void *vzalloc_noprof(unsigned long size) __alloc_size(1);
156 #define vzalloc(...)		alloc_hooks(vzalloc_noprof(__VA_ARGS__))
157 
158 extern void *vmalloc_user_noprof(unsigned long size) __alloc_size(1);
159 #define vmalloc_user(...)	alloc_hooks(vmalloc_user_noprof(__VA_ARGS__))
160 
161 extern void *vmalloc_node_noprof(unsigned long size, int node) __alloc_size(1);
162 #define vmalloc_node(...)	alloc_hooks(vmalloc_node_noprof(__VA_ARGS__))
163 
164 extern void *vzalloc_node_noprof(unsigned long size, int node) __alloc_size(1);
165 #define vzalloc_node(...)	alloc_hooks(vzalloc_node_noprof(__VA_ARGS__))
166 
167 extern void *vmalloc_32_noprof(unsigned long size) __alloc_size(1);
168 #define vmalloc_32(...)		alloc_hooks(vmalloc_32_noprof(__VA_ARGS__))
169 
170 extern void *vmalloc_32_user_noprof(unsigned long size) __alloc_size(1);
171 #define vmalloc_32_user(...)	alloc_hooks(vmalloc_32_user_noprof(__VA_ARGS__))
172 
173 extern void *__vmalloc_noprof(unsigned long size, gfp_t gfp_mask) __alloc_size(1);
174 #define __vmalloc(...)		alloc_hooks(__vmalloc_noprof(__VA_ARGS__))
175 
176 extern void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,
177 			unsigned long start, unsigned long end, gfp_t gfp_mask,
178 			pgprot_t prot, unsigned long vm_flags, int node,
179 			const void *caller) __alloc_size(1);
180 #define __vmalloc_node_range(...)	alloc_hooks(__vmalloc_node_range_noprof(__VA_ARGS__))
181 
182 void *__vmalloc_node_noprof(unsigned long size, unsigned long align, gfp_t gfp_mask,
183 		int node, const void *caller) __alloc_size(1);
184 #define __vmalloc_node(...)	alloc_hooks(__vmalloc_node_noprof(__VA_ARGS__))
185 
186 void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int node) __alloc_size(1);
187 #define vmalloc_huge_node(...)	alloc_hooks(vmalloc_huge_node_noprof(__VA_ARGS__))
188 
189 static inline void *vmalloc_huge(unsigned long size, gfp_t gfp_mask)
190 {
191 	return vmalloc_huge_node(size, gfp_mask, NUMA_NO_NODE);
192 }
193 
194 extern void *__vmalloc_array_noprof(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
195 #define __vmalloc_array(...)	alloc_hooks(__vmalloc_array_noprof(__VA_ARGS__))
196 
197 extern void *vmalloc_array_noprof(size_t n, size_t size) __alloc_size(1, 2);
198 #define vmalloc_array(...)	alloc_hooks(vmalloc_array_noprof(__VA_ARGS__))
199 
200 extern void *__vcalloc_noprof(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2);
201 #define __vcalloc(...)		alloc_hooks(__vcalloc_noprof(__VA_ARGS__))
202 
203 extern void *vcalloc_noprof(size_t n, size_t size) __alloc_size(1, 2);
204 #define vcalloc(...)		alloc_hooks(vcalloc_noprof(__VA_ARGS__))
205 
206 void *__must_check vrealloc_node_align_noprof(const void *p, size_t size,
207 		unsigned long align, gfp_t flags, int nid) __realloc_size(2);
208 #define vrealloc_node_noprof(_p, _s, _f, _nid)	\
209 	vrealloc_node_align_noprof(_p, _s, 1, _f, _nid)
210 #define vrealloc_noprof(_p, _s, _f)		\
211 	vrealloc_node_align_noprof(_p, _s, 1, _f, NUMA_NO_NODE)
212 #define vrealloc_node_align(...)		alloc_hooks(vrealloc_node_align_noprof(__VA_ARGS__))
213 #define vrealloc_node(...)			alloc_hooks(vrealloc_node_noprof(__VA_ARGS__))
214 #define vrealloc(...)				alloc_hooks(vrealloc_noprof(__VA_ARGS__))
215 
216 extern void vfree(const void *addr);
217 extern void vfree_atomic(const void *addr);
218 
219 DEFINE_FREE(vfree, void *, if (!IS_ERR_OR_NULL(_T)) vfree(_T))
220 
221 extern void *vmap(struct page **pages, unsigned int count,
222 			unsigned long flags, pgprot_t prot);
223 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
224 extern void vunmap(const void *addr);
225 
226 extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
227 				       unsigned long uaddr, void *kaddr,
228 				       unsigned long pgoff, unsigned long size);
229 
230 extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
231 							unsigned long pgoff);
232 
233 int vmap_pages_range(unsigned long addr, unsigned long end, pgprot_t prot,
234 		     struct page **pages, unsigned int page_shift);
235 
236 /*
237  *	Lowlevel-APIs (not for driver use!)
238  */
239 
240 static inline size_t get_vm_area_size(const struct vm_struct *area)
241 {
242 	if (!(area->flags & VM_NO_GUARD))
243 		/* return actual size without guard page */
244 		return area->size - PAGE_SIZE;
245 	else
246 		return area->size;
247 
248 }
249 
250 extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags);
251 extern struct vm_struct *get_vm_area_caller(unsigned long size,
252 					unsigned long flags, const void *caller);
253 extern struct vm_struct *__get_vm_area_caller(unsigned long size,
254 					unsigned long flags,
255 					unsigned long start, unsigned long end,
256 					const void *caller);
257 void free_vm_area(struct vm_struct *area);
258 extern struct vm_struct *remove_vm_area(const void *addr);
259 extern struct vm_struct *find_vm_area(const void *addr);
260 struct vmap_area *find_vmap_area(unsigned long addr);
261 
262 static inline bool is_vm_area_hugepages(const void *addr)
263 {
264 	/*
265 	 * This may not 100% tell if the area is mapped with > PAGE_SIZE
266 	 * page table entries, if for some reason the architecture indicates
267 	 * larger sizes are available but decides not to use them, nothing
268 	 * prevents that. This only indicates the size of the physical page
269 	 * allocated in the vmalloc layer.
270 	 */
271 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
272 	struct vm_struct *area = find_vm_area(addr);
273 
274 	return area && area->page_order > 0;
275 #else
276 	return false;
277 #endif
278 }
279 
280 /* for /proc/kcore */
281 long vread_iter(struct iov_iter *iter, const char *addr, size_t count);
282 
283 /*
284  *	Internals.  Don't use..
285  */
286 __init void vm_area_add_early(struct vm_struct *vm);
287 __init void vm_area_register_early(struct vm_struct *vm, size_t align);
288 
289 int register_vmap_purge_notifier(struct notifier_block *nb);
290 int unregister_vmap_purge_notifier(struct notifier_block *nb);
291 
292 #ifdef CONFIG_MMU
293 #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
294 
295 int vm_area_map_pages(struct vm_struct *area, unsigned long start,
296 		      unsigned long end, struct page **pages);
297 void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
298 			 unsigned long end);
299 void vunmap_range(unsigned long addr, unsigned long end);
300 
301 static inline void set_vm_flush_reset_perms(void *addr)
302 {
303 	struct vm_struct *vm = find_vm_area(addr);
304 
305 	if (vm)
306 		vm->flags |= VM_FLUSH_RESET_PERMS;
307 }
308 #else  /* !CONFIG_MMU */
309 #define VMALLOC_TOTAL 0UL
310 
311 static inline void set_vm_flush_reset_perms(void *addr) {}
312 #endif /* CONFIG_MMU */
313 
314 #if defined(CONFIG_MMU) && defined(CONFIG_SMP)
315 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
316 				     const size_t *sizes, int nr_vms,
317 				     size_t align, gfp_t gfp);
318 
319 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
320 # else
321 static inline struct vm_struct **
322 pcpu_get_vm_areas(const unsigned long *offsets,
323 		const size_t *sizes, int nr_vms,
324 		size_t align, gfp_t gfp)
325 {
326 	return NULL;
327 }
328 
329 static inline void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) {}
330 #endif
331 
332 #if defined(CONFIG_MMU) && defined(CONFIG_PRINTK)
333 bool vmalloc_dump_obj(void *object);
334 #else
335 static inline bool vmalloc_dump_obj(void *object) { return false; }
336 #endif
337 
338 unsigned int memalloc_apply_gfp_scope(gfp_t gfp_mask);
339 void memalloc_restore_scope(unsigned int flags);
340 #endif /* _LINUX_VMALLOC_H */
341