]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/vmalloc.h
KVM: arm64: Fix PMU probe ordering
[mirror_ubuntu-jammy-kernel.git] / include / linux / vmalloc.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_VMALLOC_H
3#define _LINUX_VMALLOC_H
4
5#include <linux/spinlock.h>
db64fe02 6#include <linux/init.h>
13ba3fcb 7#include <linux/list.h>
80c4bd7a 8#include <linux/llist.h>
1da177e4 9#include <asm/page.h> /* pgprot_t */
13ba3fcb 10#include <linux/rbtree.h>
3b3b1a29 11#include <linux/overflow.h>
1da177e4 12
1f059dfd
IM
13#include <asm/vmalloc.h>
14
605d9288 15struct vm_area_struct; /* vma defining user mapping in mm_types.h */
4da56b99 16struct notifier_block; /* in notifier.h */
83342314 17
605d9288 18/* bits in flags of vmalloc's vm_struct below */
20fc02b4
ZY
19#define VM_IOREMAP 0x00000001 /* ioremap() and friends */
20#define VM_ALLOC 0x00000002 /* vmalloc() */
21#define VM_MAP 0x00000004 /* vmap()ed pages */
22#define VM_USERMAP 0x00000008 /* suitable for remap_vmalloc_range */
fe9041c2 23#define VM_DMA_COHERENT 0x00000010 /* dma_alloc_coherent */
20fc02b4 24#define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */
71394fe5 25#define VM_NO_GUARD 0x00000040 /* don't add guard page */
a5af5aa8 26#define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */
4f6ec860
RE
27#define VM_FLUSH_RESET_PERMS 0x00000100 /* reset direct map and flush TLB on unmap, can't be freed in atomic context */
28#define VM_MAP_PUT_PAGES 0x00000200 /* put pages and free array in vfree */
121e6f32 29#define VM_NO_HUGE_VMAP 0x00000400 /* force PAGE_SIZE pte mapping */
3c5c3cfb
DA
30
31/*
06c88398 32 * VM_KASAN is used slightly differently depending on CONFIG_KASAN_VMALLOC.
3c5c3cfb
DA
33 *
34 * If IS_ENABLED(CONFIG_KASAN_VMALLOC), VM_KASAN is set on a vm_struct after
35 * shadow memory has been mapped. It's used to handle allocation errors so that
f0953a1b 36 * we don't try to poison shadow on free if it was never allocated.
3c5c3cfb
DA
37 *
38 * Otherwise, VM_KASAN is set for kasan_module_alloc() allocations and used to
39 * determine which allocations need the module shadow freed.
40 */
41
1da177e4
LT
42/* bits [20..32] reserved for arch specific ioremap internals */
43
fd195c49
DS
44/*
45 * Maximum alignment for ioremap() regions.
f0953a1b 46 * Can be overridden by arch-specific value.
fd195c49
DS
47 */
48#ifndef IOREMAP_MAX_ORDER
49#define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */
50#endif
51
1da177e4 52struct vm_struct {
2b4ac44e 53 struct vm_struct *next;
1da177e4
LT
54 void *addr;
55 unsigned long size;
56 unsigned long flags;
57 struct page **pages;
121e6f32
NP
58#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
59 unsigned int page_order;
60#endif
1da177e4 61 unsigned int nr_pages;
ffa71f33 62 phys_addr_t phys_addr;
5e6cafc8 63 const void *caller;
1da177e4
LT
64};
65
13ba3fcb
AK
66struct vmap_area {
67 unsigned long va_start;
68 unsigned long va_end;
68ad4a33 69
13ba3fcb
AK
70 struct rb_node rb_node; /* address sorted rbtree */
71 struct list_head list; /* address sorted list */
688fcbfc
PL
72
73 /*
96e2db45
URS
74 * The following two variables can be packed, because
75 * a vmap_area object can be either:
688fcbfc 76 * 1) in "free" tree (root is vmap_area_root)
96e2db45 77 * 2) or "busy" tree (root is free_vmap_area_root)
688fcbfc
PL
78 */
79 union {
80 unsigned long subtree_max_size; /* in "free" tree */
81 struct vm_struct *vm; /* in "busy" tree */
688fcbfc 82 };
13ba3fcb
AK
83};
84
6f680e70
NP
85/* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
86#ifndef arch_vmap_p4d_supported
87static inline bool arch_vmap_p4d_supported(pgprot_t prot)
88{
89 return false;
90}
91#endif
92
93#ifndef arch_vmap_pud_supported
94static inline bool arch_vmap_pud_supported(pgprot_t prot)
95{
96 return false;
97}
98#endif
99
100#ifndef arch_vmap_pmd_supported
101static inline bool arch_vmap_pmd_supported(pgprot_t prot)
102{
103 return false;
104}
bbc180a5
NP
105#endif
106
f7ee1f13
CL
107#ifndef arch_vmap_pte_range_map_size
108static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end,
109 u64 pfn, unsigned int max_page_shift)
110{
111 return PAGE_SIZE;
112}
113#endif
114
3382bbee
CL
115#ifndef arch_vmap_pte_supported_shift
116static inline int arch_vmap_pte_supported_shift(unsigned long size)
117{
118 return PAGE_SHIFT;
119}
120#endif
121
1da177e4
LT
122/*
123 * Highlevel APIs for driver use
124 */
db64fe02 125extern void vm_unmap_ram(const void *mem, unsigned int count);
d4efd79a 126extern void *vm_map_ram(struct page **pages, unsigned int count, int node);
db64fe02
NP
127extern void vm_unmap_aliases(void);
128
129#ifdef CONFIG_MMU
130extern void __init vmalloc_init(void);
97105f0a 131extern unsigned long vmalloc_nr_pages(void);
db64fe02
NP
132#else
133static inline void vmalloc_init(void)
134{
135}
97105f0a 136static inline unsigned long vmalloc_nr_pages(void) { return 0; }
db64fe02
NP
137#endif
138
1da177e4 139extern void *vmalloc(unsigned long size);
e1ca7788 140extern void *vzalloc(unsigned long size);
83342314 141extern void *vmalloc_user(unsigned long size);
930fc45a 142extern void *vmalloc_node(unsigned long size, int node);
e1ca7788 143extern void *vzalloc_node(unsigned long size, int node);
1da177e4 144extern void *vmalloc_32(unsigned long size);
83342314 145extern void *vmalloc_32_user(unsigned long size);
88dca4ca 146extern void *__vmalloc(unsigned long size, gfp_t gfp_mask);
d0a21265
DR
147extern void *__vmalloc_node_range(unsigned long size, unsigned long align,
148 unsigned long start, unsigned long end, gfp_t gfp_mask,
cb9e3c29
AR
149 pgprot_t prot, unsigned long vm_flags, int node,
150 const void *caller);
2b905948
CH
151void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
152 int node, const void *caller);
15a64f5a 153void *vmalloc_no_huge(unsigned long size);
cb9e3c29 154
b3bdda02 155extern void vfree(const void *addr);
bf22e37a 156extern void vfree_atomic(const void *addr);
1da177e4
LT
157
158extern void *vmap(struct page **pages, unsigned int count,
159 unsigned long flags, pgprot_t prot);
3e9a9e25 160void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot);
b3bdda02 161extern void vunmap(const void *addr);
83342314 162
e69e9d4a
HD
163extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,
164 unsigned long uaddr, void *kaddr,
bdebd6a2 165 unsigned long pgoff, unsigned long size);
e69e9d4a 166
83342314
NP
167extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
168 unsigned long pgoff);
763802b5 169
2ba3e694
JR
170/*
171 * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values
172 * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
173 * needs to be called.
174 */
175#ifndef ARCH_PAGE_TABLE_SYNC_MASK
176#define ARCH_PAGE_TABLE_SYNC_MASK 0
177#endif
178
179/*
180 * There is no default implementation for arch_sync_kernel_mappings(). It is
181 * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK
182 * is 0.
183 */
184void arch_sync_kernel_mappings(unsigned long start, unsigned long end);
185
1da177e4
LT
186/*
187 * Lowlevel-APIs (not for driver use!)
188 */
9585116b
JF
189
190static inline size_t get_vm_area_size(const struct vm_struct *area)
191{
71394fe5
AR
192 if (!(area->flags & VM_NO_GUARD))
193 /* return actual size without guard page */
194 return area->size - PAGE_SIZE;
195 else
196 return area->size;
197
9585116b
JF
198}
199
1da177e4 200extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags);
23016969 201extern struct vm_struct *get_vm_area_caller(unsigned long size,
5e6cafc8 202 unsigned long flags, const void *caller);
c2968612
BH
203extern struct vm_struct *__get_vm_area_caller(unsigned long size,
204 unsigned long flags,
205 unsigned long start, unsigned long end,
5e6cafc8 206 const void *caller);
301fa9f2 207void free_vm_area(struct vm_struct *area);
b3bdda02 208extern struct vm_struct *remove_vm_area(const void *addr);
e9da6e99 209extern struct vm_struct *find_vm_area(const void *addr);
c19c03fc 210
121e6f32
NP
211static inline bool is_vm_area_hugepages(const void *addr)
212{
213 /*
214 * This may not 100% tell if the area is mapped with > PAGE_SIZE
215 * page table entries, if for some reason the architecture indicates
216 * larger sizes are available but decides not to use them, nothing
217 * prevents that. This only indicates the size of the physical page
218 * allocated in the vmalloc layer.
219 */
220#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC
221 return find_vm_area(addr)->page_order > 0;
222#else
223 return false;
224#endif
225}
226
b554cb42 227#ifdef CONFIG_MMU
4ad0ae8c 228void vunmap_range(unsigned long addr, unsigned long end);
868b104d
RE
229static inline void set_vm_flush_reset_perms(void *addr)
230{
231 struct vm_struct *vm = find_vm_area(addr);
232
233 if (vm)
234 vm->flags |= VM_FLUSH_RESET_PERMS;
235}
121e6f32 236
b554cb42 237#else
868b104d
RE
238static inline void set_vm_flush_reset_perms(void *addr)
239{
240}
b554cb42 241#endif
1da177e4 242
bbcd53c9 243/* for /proc/kcore */
69beeb1d 244extern long vread(char *buf, char *addr, unsigned long count);
69beeb1d 245
1da177e4 246/*
06c88398 247 * Internals. Don't use..
1da177e4 248 */
f1c4069e 249extern struct list_head vmap_area_list;
be9b7335 250extern __init void vm_area_add_early(struct vm_struct *vm);
c0c0a293 251extern __init void vm_area_register_early(struct vm_struct *vm, size_t align);
1da177e4 252
4f8b02b4 253#ifdef CONFIG_SMP
b554cb42 254# ifdef CONFIG_MMU
ca23e405
TH
255struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
256 const size_t *sizes, int nr_vms,
ec3f64fc 257 size_t align);
ca23e405
TH
258
259void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms);
b554cb42
GY
260# else
261static inline struct vm_struct **
262pcpu_get_vm_areas(const unsigned long *offsets,
263 const size_t *sizes, int nr_vms,
264 size_t align)
265{
266 return NULL;
267}
268
269static inline void
270pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
271{
272}
273# endif
4f8b02b4 274#endif
ca23e405 275
db3808c1
JK
276#ifdef CONFIG_MMU
277#define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START)
db3808c1 278#else
db3808c1 279#define VMALLOC_TOTAL 0UL
db3808c1
JK
280#endif
281
4da56b99
CW
282int register_vmap_purge_notifier(struct notifier_block *nb);
283int unregister_vmap_purge_notifier(struct notifier_block *nb);
284
5bb1bb35 285#if defined(CONFIG_MMU) && defined(CONFIG_PRINTK)
98f18083
PM
286bool vmalloc_dump_obj(void *object);
287#else
288static inline bool vmalloc_dump_obj(void *object) { return false; }
289#endif
290
1da177e4 291#endif /* _LINUX_VMALLOC_H */