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