1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_VMALLOC_H 2 #ifndef _LINUX_VMALLOC_H 3 #define _LINUX_VMALLOC_H 3 #define _LINUX_VMALLOC_H 4 4 5 #include <linux/alloc_tag.h> << 6 #include <linux/sched.h> << 7 #include <linux/spinlock.h> 5 #include <linux/spinlock.h> 8 #include <linux/init.h> 6 #include <linux/init.h> 9 #include <linux/list.h> 7 #include <linux/list.h> 10 #include <linux/llist.h> 8 #include <linux/llist.h> 11 #include <asm/page.h> /* pgprot_t */ 9 #include <asm/page.h> /* pgprot_t */ 12 #include <linux/rbtree.h> 10 #include <linux/rbtree.h> 13 #include <linux/overflow.h> 11 #include <linux/overflow.h> 14 12 15 #include <asm/vmalloc.h> 13 #include <asm/vmalloc.h> 16 14 17 struct vm_area_struct; /* vma definin 15 struct vm_area_struct; /* vma defining user mapping in mm_types.h */ 18 struct notifier_block; /* in notifier 16 struct notifier_block; /* in notifier.h */ 19 struct iov_iter; /* in uio.h */ << 20 17 21 /* bits in flags of vmalloc's vm_struct below 18 /* bits in flags of vmalloc's vm_struct below */ 22 #define VM_IOREMAP 0x00000001 19 #define VM_IOREMAP 0x00000001 /* ioremap() and friends */ 23 #define VM_ALLOC 0x00000002 20 #define VM_ALLOC 0x00000002 /* vmalloc() */ 24 #define VM_MAP 0x00000004 21 #define VM_MAP 0x00000004 /* vmap()ed pages */ 25 #define VM_USERMAP 0x00000008 22 #define VM_USERMAP 0x00000008 /* suitable for remap_vmalloc_range */ 26 #define VM_DMA_COHERENT 0x00000010 23 #define VM_DMA_COHERENT 0x00000010 /* dma_alloc_coherent */ 27 #define VM_UNINITIALIZED 0x00000020 24 #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */ 28 #define VM_NO_GUARD 0x00000040 !! 25 #define VM_NO_GUARD 0x00000040 /* don't add guard page */ 29 #define VM_KASAN 0x00000080 26 #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */ 30 #define VM_FLUSH_RESET_PERMS 0x00000100 !! 27 31 #define VM_MAP_PUT_PAGES 0x00000200 !! 28 /* 32 #define VM_ALLOW_HUGE_VMAP 0x00000400 !! 29 * VM_KASAN is used slighly differently depending on CONFIG_KASAN_VMALLOC. 33 !! 30 * 34 #if (defined(CONFIG_KASAN_GENERIC) || defined( !! 31 * If IS_ENABLED(CONFIG_KASAN_VMALLOC), VM_KASAN is set on a vm_struct after 35 !defined(CONFIG_KASAN_VMALLOC) !! 32 * shadow memory has been mapped. It's used to handle allocation errors so that 36 #define VM_DEFER_KMEMLEAK 0x00000800 !! 33 * we don't try to poision shadow on free if it was never allocated. 37 #else !! 34 * 38 #define VM_DEFER_KMEMLEAK 0 !! 35 * Otherwise, VM_KASAN is set for kasan_module_alloc() allocations and used to 39 #endif !! 36 * determine which allocations need the module shadow freed. 40 #define VM_SPARSE 0x00001000 !! 37 */ >> 38 >> 39 /* >> 40 * Memory with VM_FLUSH_RESET_PERMS cannot be freed in an interrupt or with >> 41 * vfree_atomic(). >> 42 */ >> 43 #define VM_FLUSH_RESET_PERMS 0x00000100 /* Reset direct map and flush TLB on unmap */ 41 44 42 /* bits [20..32] reserved for arch specific io 45 /* bits [20..32] reserved for arch specific ioremap internals */ 43 46 44 /* 47 /* 45 * Maximum alignment for ioremap() regions. 48 * Maximum alignment for ioremap() regions. 46 * Can be overridden by arch-specific value. !! 49 * Can be overriden by arch-specific value. 47 */ 50 */ 48 #ifndef IOREMAP_MAX_ORDER 51 #ifndef IOREMAP_MAX_ORDER 49 #define IOREMAP_MAX_ORDER (7 + PAGE_SHIF 52 #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */ 50 #endif 53 #endif 51 54 52 struct vm_struct { 55 struct vm_struct { 53 struct vm_struct *next; 56 struct vm_struct *next; 54 void *addr; 57 void *addr; 55 unsigned long size; 58 unsigned long size; 56 unsigned long flags; 59 unsigned long flags; 57 struct page **pages; 60 struct page **pages; 58 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC << 59 unsigned int page_order; << 60 #endif << 61 unsigned int nr_pages; 61 unsigned int nr_pages; 62 phys_addr_t phys_addr; 62 phys_addr_t phys_addr; 63 const void *caller; 63 const void *caller; 64 }; 64 }; 65 65 66 struct vmap_area { 66 struct vmap_area { 67 unsigned long va_start; 67 unsigned long va_start; 68 unsigned long va_end; 68 unsigned long va_end; 69 69 70 struct rb_node rb_node; /* add 70 struct rb_node rb_node; /* address sorted rbtree */ 71 struct list_head list; /* add 71 struct list_head list; /* address sorted list */ 72 72 73 /* 73 /* 74 * The following two variables can be !! 74 * The following three variables can be packed, because 75 * a vmap_area object can be either: !! 75 * a vmap_area object is always one of the three states: 76 * 1) in "free" tree (root is free_ !! 76 * 1) in "free" tree (root is vmap_area_root) 77 * 2) or "busy" tree (root is vmap_ !! 77 * 2) in "busy" tree (root is free_vmap_area_root) >> 78 * 3) in purge list (head is vmap_purge_list) 78 */ 79 */ 79 union { 80 union { 80 unsigned long subtree_max_size 81 unsigned long subtree_max_size; /* in "free" tree */ 81 struct vm_struct *vm; 82 struct vm_struct *vm; /* in "busy" tree */ >> 83 struct llist_node purge_list; /* in purge list */ 82 }; 84 }; 83 unsigned long flags; /* mark type of v << 84 }; 85 }; 85 86 86 /* archs that select HAVE_ARCH_HUGE_VMAP shoul << 87 #ifndef arch_vmap_p4d_supported << 88 static inline bool arch_vmap_p4d_supported(pgp << 89 { << 90 return false; << 91 } << 92 #endif << 93 << 94 #ifndef arch_vmap_pud_supported << 95 static inline bool arch_vmap_pud_supported(pgp << 96 { << 97 return false; << 98 } << 99 #endif << 100 << 101 #ifndef arch_vmap_pmd_supported << 102 static inline bool arch_vmap_pmd_supported(pgp << 103 { << 104 return false; << 105 } << 106 #endif << 107 << 108 #ifndef arch_vmap_pte_range_map_size << 109 static inline unsigned long arch_vmap_pte_rang << 110 << 111 { << 112 return PAGE_SIZE; << 113 } << 114 #endif << 115 << 116 #ifndef arch_vmap_pte_supported_shift << 117 static inline int arch_vmap_pte_supported_shif << 118 { << 119 return PAGE_SHIFT; << 120 } << 121 #endif << 122 << 123 #ifndef arch_vmap_pgprot_tagged << 124 static inline pgprot_t arch_vmap_pgprot_tagged << 125 { << 126 return prot; << 127 } << 128 #endif << 129 << 130 /* 87 /* 131 * Highlevel APIs for driver use 88 * Highlevel APIs for driver use 132 */ 89 */ 133 extern void vm_unmap_ram(const void *mem, unsi 90 extern void vm_unmap_ram(const void *mem, unsigned int count); 134 extern void *vm_map_ram(struct page **pages, u 91 extern void *vm_map_ram(struct page **pages, unsigned int count, int node); 135 extern void vm_unmap_aliases(void); 92 extern void vm_unmap_aliases(void); 136 93 137 #ifdef CONFIG_MMU 94 #ifdef CONFIG_MMU >> 95 extern void __init vmalloc_init(void); 138 extern unsigned long vmalloc_nr_pages(void); 96 extern unsigned long vmalloc_nr_pages(void); 139 #else 97 #else >> 98 static inline void vmalloc_init(void) >> 99 { >> 100 } 140 static inline unsigned long vmalloc_nr_pages(v 101 static inline unsigned long vmalloc_nr_pages(void) { return 0; } 141 #endif 102 #endif 142 103 143 extern void *vmalloc_noprof(unsigned long size !! 104 extern void *vmalloc(unsigned long size); 144 #define vmalloc(...) alloc_hooks(vm !! 105 extern void *vzalloc(unsigned long size); 145 !! 106 extern void *vmalloc_user(unsigned long size); 146 extern void *vzalloc_noprof(unsigned long size !! 107 extern void *vmalloc_node(unsigned long size, int node); 147 #define vzalloc(...) alloc_hooks(vz !! 108 extern void *vzalloc_node(unsigned long size, int node); 148 !! 109 extern void *vmalloc_32(unsigned long size); 149 extern void *vmalloc_user_noprof(unsigned long !! 110 extern void *vmalloc_32_user(unsigned long size); 150 #define vmalloc_user(...) alloc_hooks(vm !! 111 extern void *__vmalloc(unsigned long size, gfp_t gfp_mask); 151 !! 112 extern void *__vmalloc_node_range(unsigned long size, unsigned long align, 152 extern void *vmalloc_node_noprof(unsigned long << 153 #define vmalloc_node(...) alloc_hooks(vm << 154 << 155 extern void *vzalloc_node_noprof(unsigned long << 156 #define vzalloc_node(...) alloc_hooks(vz << 157 << 158 extern void *vmalloc_32_noprof(unsigned long s << 159 #define vmalloc_32(...) alloc_hooks(vm << 160 << 161 extern void *vmalloc_32_user_noprof(unsigned l << 162 #define vmalloc_32_user(...) alloc_hooks(vm << 163 << 164 extern void *__vmalloc_noprof(unsigned long si << 165 #define __vmalloc(...) alloc_hooks(__ << 166 << 167 extern void *__vmalloc_node_range_noprof(unsig << 168 unsigned long start, u 113 unsigned long start, unsigned long end, gfp_t gfp_mask, 169 pgprot_t prot, unsigne 114 pgprot_t prot, unsigned long vm_flags, int node, 170 const void *caller) __ !! 115 const void *caller); 171 #define __vmalloc_node_range(...) alloc_ !! 116 void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, 172 !! 117 int node, const void *caller); 173 void *__vmalloc_node_noprof(unsigned long size << 174 int node, const void *caller) << 175 #define __vmalloc_node(...) alloc_hooks(__ << 176 << 177 void *vmalloc_huge_noprof(unsigned long size, << 178 #define vmalloc_huge(...) alloc_hooks(vm << 179 << 180 extern void *__vmalloc_array_noprof(size_t n, << 181 #define __vmalloc_array(...) alloc_hooks(__ << 182 << 183 extern void *vmalloc_array_noprof(size_t n, si << 184 #define vmalloc_array(...) alloc_hooks(vm << 185 << 186 extern void *__vcalloc_noprof(size_t n, size_t << 187 #define __vcalloc(...) alloc_hooks(__ << 188 << 189 extern void *vcalloc_noprof(size_t n, size_t s << 190 #define vcalloc(...) alloc_hooks(vc << 191 << 192 void * __must_check vrealloc_noprof(const void << 193 __realloc_size(2); << 194 #define vrealloc(...) alloc_hooks(vr << 195 118 196 extern void vfree(const void *addr); 119 extern void vfree(const void *addr); 197 extern void vfree_atomic(const void *addr); 120 extern void vfree_atomic(const void *addr); 198 121 199 extern void *vmap(struct page **pages, unsigne 122 extern void *vmap(struct page **pages, unsigned int count, 200 unsigned long flags, p 123 unsigned long flags, pgprot_t prot); 201 void *vmap_pfn(unsigned long *pfns, unsigned i << 202 extern void vunmap(const void *addr); 124 extern void vunmap(const void *addr); 203 125 204 extern int remap_vmalloc_range_partial(struct 126 extern int remap_vmalloc_range_partial(struct vm_area_struct *vma, 205 unsigne 127 unsigned long uaddr, void *kaddr, 206 unsigne 128 unsigned long pgoff, unsigned long size); 207 129 208 extern int remap_vmalloc_range(struct vm_area_ 130 extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 209 131 unsigned long pgoff); 210 132 211 /* 133 /* 212 * Architectures can set this mask to a combin 134 * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values 213 * and let generic vmalloc and ioremap code kn 135 * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings() 214 * needs to be called. 136 * needs to be called. 215 */ 137 */ 216 #ifndef ARCH_PAGE_TABLE_SYNC_MASK 138 #ifndef ARCH_PAGE_TABLE_SYNC_MASK 217 #define ARCH_PAGE_TABLE_SYNC_MASK 0 139 #define ARCH_PAGE_TABLE_SYNC_MASK 0 218 #endif 140 #endif 219 141 220 /* 142 /* 221 * There is no default implementation for arch 143 * There is no default implementation for arch_sync_kernel_mappings(). It is 222 * relied upon the compiler to optimize calls 144 * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK 223 * is 0. 145 * is 0. 224 */ 146 */ 225 void arch_sync_kernel_mappings(unsigned long s 147 void arch_sync_kernel_mappings(unsigned long start, unsigned long end); 226 148 227 /* 149 /* 228 * Lowlevel-APIs (not for driver use!) 150 * Lowlevel-APIs (not for driver use!) 229 */ 151 */ 230 152 231 static inline size_t get_vm_area_size(const st 153 static inline size_t get_vm_area_size(const struct vm_struct *area) 232 { 154 { 233 if (!(area->flags & VM_NO_GUARD)) 155 if (!(area->flags & VM_NO_GUARD)) 234 /* return actual size without 156 /* return actual size without guard page */ 235 return area->size - PAGE_SIZE; 157 return area->size - PAGE_SIZE; 236 else 158 else 237 return area->size; 159 return area->size; 238 160 239 } 161 } 240 162 241 extern struct vm_struct *get_vm_area(unsigned 163 extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags); 242 extern struct vm_struct *get_vm_area_caller(un 164 extern struct vm_struct *get_vm_area_caller(unsigned long size, 243 unsign 165 unsigned long flags, const void *caller); 244 extern struct vm_struct *__get_vm_area_caller( 166 extern struct vm_struct *__get_vm_area_caller(unsigned long size, 245 unsign 167 unsigned long flags, 246 unsign 168 unsigned long start, unsigned long end, 247 const 169 const void *caller); 248 void free_vm_area(struct vm_struct *area); << 249 extern struct vm_struct *remove_vm_area(const 170 extern struct vm_struct *remove_vm_area(const void *addr); 250 extern struct vm_struct *find_vm_area(const vo 171 extern struct vm_struct *find_vm_area(const void *addr); 251 struct vmap_area *find_vmap_area(unsigned long << 252 << 253 static inline bool is_vm_area_hugepages(const << 254 { << 255 /* << 256 * This may not 100% tell if the area << 257 * page table entries, if for some rea << 258 * larger sizes are available but deci << 259 * prevents that. This only indicates << 260 * allocated in the vmalloc layer. << 261 */ << 262 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC << 263 return find_vm_area(addr)->page_order << 264 #else << 265 return false; << 266 #endif << 267 } << 268 172 269 #ifdef CONFIG_MMU 173 #ifdef CONFIG_MMU 270 int vm_area_map_pages(struct vm_struct *area, !! 174 extern int map_kernel_range_noflush(unsigned long start, unsigned long size, 271 unsigned long end, struc !! 175 pgprot_t prot, struct page **pages); 272 void vm_area_unmap_pages(struct vm_struct *are !! 176 int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot, 273 unsigned long end); !! 177 struct page **pages); 274 void vunmap_range(unsigned long addr, unsigned !! 178 extern void unmap_kernel_range_noflush(unsigned long addr, unsigned long size); >> 179 extern void unmap_kernel_range(unsigned long addr, unsigned long size); 275 static inline void set_vm_flush_reset_perms(vo 180 static inline void set_vm_flush_reset_perms(void *addr) 276 { 181 { 277 struct vm_struct *vm = find_vm_area(ad 182 struct vm_struct *vm = find_vm_area(addr); 278 183 279 if (vm) 184 if (vm) 280 vm->flags |= VM_FLUSH_RESET_PE 185 vm->flags |= VM_FLUSH_RESET_PERMS; 281 } 186 } 282 << 283 #else 187 #else >> 188 static inline int >> 189 map_kernel_range_noflush(unsigned long start, unsigned long size, >> 190 pgprot_t prot, struct page **pages) >> 191 { >> 192 return size >> PAGE_SHIFT; >> 193 } >> 194 #define map_kernel_range map_kernel_range_noflush >> 195 static inline void >> 196 unmap_kernel_range_noflush(unsigned long addr, unsigned long size) >> 197 { >> 198 } >> 199 #define unmap_kernel_range unmap_kernel_range_noflush 284 static inline void set_vm_flush_reset_perms(vo 200 static inline void set_vm_flush_reset_perms(void *addr) 285 { 201 { 286 } 202 } 287 #endif 203 #endif 288 204 289 /* for /proc/kcore */ !! 205 /* Allocate/destroy a 'vmalloc' VM area. */ 290 extern long vread_iter(struct iov_iter *iter, !! 206 extern struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes); >> 207 extern void free_vm_area(struct vm_struct *area); >> 208 >> 209 /* for /dev/kmem */ >> 210 extern long vread(char *buf, char *addr, unsigned long count); >> 211 extern long vwrite(char *buf, char *addr, unsigned long count); 291 212 292 /* 213 /* 293 * Internals. Don't use.. !! 214 * Internals. Dont't use.. 294 */ 215 */ >> 216 extern struct list_head vmap_area_list; 295 extern __init void vm_area_add_early(struct vm 217 extern __init void vm_area_add_early(struct vm_struct *vm); 296 extern __init void vm_area_register_early(stru 218 extern __init void vm_area_register_early(struct vm_struct *vm, size_t align); 297 219 298 #ifdef CONFIG_SMP 220 #ifdef CONFIG_SMP 299 # ifdef CONFIG_MMU 221 # ifdef CONFIG_MMU 300 struct vm_struct **pcpu_get_vm_areas(const uns 222 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 301 const siz 223 const size_t *sizes, int nr_vms, 302 size_t al 224 size_t align); 303 225 304 void pcpu_free_vm_areas(struct vm_struct **vms 226 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms); 305 # else 227 # else 306 static inline struct vm_struct ** 228 static inline struct vm_struct ** 307 pcpu_get_vm_areas(const unsigned long *offsets 229 pcpu_get_vm_areas(const unsigned long *offsets, 308 const size_t *sizes, int nr_vm 230 const size_t *sizes, int nr_vms, 309 size_t align) 231 size_t align) 310 { 232 { 311 return NULL; 233 return NULL; 312 } 234 } 313 235 314 static inline void 236 static inline void 315 pcpu_free_vm_areas(struct vm_struct **vms, int 237 pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 316 { 238 { 317 } 239 } 318 # endif 240 # endif 319 #endif 241 #endif 320 242 321 #ifdef CONFIG_MMU 243 #ifdef CONFIG_MMU 322 #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_S 244 #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START) 323 #else 245 #else 324 #define VMALLOC_TOTAL 0UL 246 #define VMALLOC_TOTAL 0UL 325 #endif 247 #endif 326 248 327 int register_vmap_purge_notifier(struct notifi 249 int register_vmap_purge_notifier(struct notifier_block *nb); 328 int unregister_vmap_purge_notifier(struct noti 250 int unregister_vmap_purge_notifier(struct notifier_block *nb); 329 << 330 #if defined(CONFIG_MMU) && defined(CONFIG_PRIN << 331 bool vmalloc_dump_obj(void *object); << 332 #else << 333 static inline bool vmalloc_dump_obj(void *obje << 334 #endif << 335 251 336 #endif /* _LINUX_VMALLOC_H */ 252 #endif /* _LINUX_VMALLOC_H */ 337 253
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.