1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_VMALLOC_H 3 #define _LINUX_VMALLOC_H 4 5 #include <linux/spinlock.h> 6 #include <linux/init.h> 7 #include <linux/list.h> 8 #include <linux/llist.h> 9 #include <asm/page.h> /* pgprot_t */ 10 #include <linux/rbtree.h> 11 #include <linux/overflow.h> 12 13 #include <asm/vmalloc.h> 14 15 struct vm_area_struct; /* vma defining user mapping in mm_types.h */ 16 struct notifier_block; /* in notifier.h */ 17 18 /* bits in flags of vmalloc's vm_struct below */ 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 */ 23 #define VM_DMA_COHERENT 0x00000010 /* dma_alloc_coherent */ 24 #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */ 25 #define VM_NO_GUARD 0x00000040 /* ***DANGEROUS*** don't add guard page */ 26 #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */ 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 */ 29 #define VM_NO_HUGE_VMAP 0x00000400 /* force PAGE_SIZE pte mapping */ 30 31 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 32 !defined(CONFIG_KASAN_VMALLOC) 33 #define VM_DEFER_KMEMLEAK 0x00000800 /* defer kmemleak object creation */ 34 #else 35 #define VM_DEFER_KMEMLEAK 0 36 #endif 37 38 /* 39 * VM_KASAN is used slightly differently depending on CONFIG_KASAN_VMALLOC. 40 * 41 * If IS_ENABLED(CONFIG_KASAN_VMALLOC), VM_KASAN is set on a vm_struct after 42 * shadow memory has been mapped. It's used to handle allocation errors so that 43 * we don't try to poison shadow on free if it was never allocated. 44 * 45 * Otherwise, VM_KASAN is set for kasan_module_alloc() allocations and used to 46 * determine which allocations need the module shadow freed. 47 */ 48 49 /* bits [20..32] reserved for arch specific ioremap internals */ 50 51 /* 52 * Maximum alignment for ioremap() regions. 53 * Can be overridden by arch-specific value. 54 */ 55 #ifndef IOREMAP_MAX_ORDER 56 #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */ 57 #endif 58 59 struct vm_struct { 60 struct vm_struct *next; 61 void *addr; 62 unsigned long size; 63 unsigned long flags; 64 struct page **pages; 65 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 66 unsigned int page_order; 67 #endif 68 unsigned int nr_pages; 69 phys_addr_t phys_addr; 70 const void *caller; 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 vmap_area_root) 84 * 2) or "busy" tree (root is free_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 }; 91 92 /* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */ 93 #ifndef arch_vmap_p4d_supported 94 static inline bool arch_vmap_p4d_supported(pgprot_t prot) 95 { 96 return false; 97 } 98 #endif 99 100 #ifndef arch_vmap_pud_supported 101 static inline bool arch_vmap_pud_supported(pgprot_t prot) 102 { 103 return false; 104 } 105 #endif 106 107 #ifndef arch_vmap_pmd_supported 108 static inline bool arch_vmap_pmd_supported(pgprot_t prot) 109 { 110 return false; 111 } 112 #endif 113 114 #ifndef arch_vmap_pte_range_map_size 115 static inline unsigned long arch_vmap_pte_range_map_size(unsigned long addr, unsigned long end, 116 u64 pfn, unsigned int max_page_shift) 117 { 118 return PAGE_SIZE; 119 } 120 #endif 121 122 #ifndef arch_vmap_pte_supported_shift 123 static inline int arch_vmap_pte_supported_shift(unsigned long size) 124 { 125 return PAGE_SHIFT; 126 } 127 #endif 128 129 /* 130 * Highlevel APIs for driver use 131 */ 132 extern void vm_unmap_ram(const void *mem, unsigned int count); 133 extern void *vm_map_ram(struct page **pages, unsigned int count, int node); 134 extern void vm_unmap_aliases(void); 135 136 #ifdef CONFIG_MMU 137 extern void __init vmalloc_init(void); 138 extern unsigned long vmalloc_nr_pages(void); 139 #else 140 static inline void vmalloc_init(void) 141 { 142 } 143 static inline unsigned long vmalloc_nr_pages(void) { return 0; } 144 #endif 145 146 extern void *vmalloc(unsigned long size) __alloc_size(1); 147 extern void *vzalloc(unsigned long size) __alloc_size(1); 148 extern void *vmalloc_user(unsigned long size) __alloc_size(1); 149 extern void *vmalloc_node(unsigned long size, int node) __alloc_size(1); 150 extern void *vzalloc_node(unsigned long size, int node) __alloc_size(1); 151 extern void *vmalloc_32(unsigned long size) __alloc_size(1); 152 extern void *vmalloc_32_user(unsigned long size) __alloc_size(1); 153 extern void *__vmalloc(unsigned long size, gfp_t gfp_mask) __alloc_size(1); 154 extern void *__vmalloc_node_range(unsigned long size, unsigned long align, 155 unsigned long start, unsigned long end, gfp_t gfp_mask, 156 pgprot_t prot, unsigned long vm_flags, int node, 157 const void *caller) __alloc_size(1); 158 void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, 159 int node, const void *caller) __alloc_size(1); 160 void *vmalloc_no_huge(unsigned long size) __alloc_size(1); 161 162 extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2); 163 extern void *vmalloc_array(size_t n, size_t size) __alloc_size(1, 2); 164 extern void *__vcalloc(size_t n, size_t size, gfp_t flags) __alloc_size(1, 2); 165 extern void *vcalloc(size_t n, size_t size) __alloc_size(1, 2); 166 167 extern void vfree(const void *addr); 168 extern void vfree_atomic(const void *addr); 169 170 extern void *vmap(struct page **pages, unsigned int count, 171 unsigned long flags, pgprot_t prot); 172 void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot); 173 extern void vunmap(const void *addr); 174 175 extern int remap_vmalloc_range_partial(struct vm_area_struct *vma, 176 unsigned long uaddr, void *kaddr, 177 unsigned long pgoff, unsigned long size); 178 179 extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, 180 unsigned long pgoff); 181 182 /* 183 * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values 184 * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings() 185 * needs to be called. 186 */ 187 #ifndef ARCH_PAGE_TABLE_SYNC_MASK 188 #define ARCH_PAGE_TABLE_SYNC_MASK 0 189 #endif 190 191 /* 192 * There is no default implementation for arch_sync_kernel_mappings(). It is 193 * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK 194 * is 0. 195 */ 196 void arch_sync_kernel_mappings(unsigned long start, unsigned long end); 197 198 /* 199 * Lowlevel-APIs (not for driver use!) 200 */ 201 202 static inline size_t get_vm_area_size(const struct vm_struct *area) 203 { 204 if (!(area->flags & VM_NO_GUARD)) 205 /* return actual size without guard page */ 206 return area->size - PAGE_SIZE; 207 else 208 return area->size; 209 210 } 211 212 extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags); 213 extern struct vm_struct *get_vm_area_caller(unsigned long size, 214 unsigned long flags, const void *caller); 215 extern struct vm_struct *__get_vm_area_caller(unsigned long size, 216 unsigned long flags, 217 unsigned long start, unsigned long end, 218 const void *caller); 219 void free_vm_area(struct vm_struct *area); 220 extern struct vm_struct *remove_vm_area(const void *addr); 221 extern struct vm_struct *find_vm_area(const void *addr); 222 223 static inline bool is_vm_area_hugepages(const void *addr) 224 { 225 /* 226 * This may not 100% tell if the area is mapped with > PAGE_SIZE 227 * page table entries, if for some reason the architecture indicates 228 * larger sizes are available but decides not to use them, nothing 229 * prevents that. This only indicates the size of the physical page 230 * allocated in the vmalloc layer. 231 */ 232 #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC 233 return find_vm_area(addr)->page_order > 0; 234 #else 235 return false; 236 #endif 237 } 238 239 #ifdef CONFIG_MMU 240 void vunmap_range(unsigned long addr, unsigned long end); 241 static inline void set_vm_flush_reset_perms(void *addr) 242 { 243 struct vm_struct *vm = find_vm_area(addr); 244 245 if (vm) 246 vm->flags |= VM_FLUSH_RESET_PERMS; 247 } 248 249 #else 250 static inline void set_vm_flush_reset_perms(void *addr) 251 { 252 } 253 #endif 254 255 /* for /proc/kcore */ 256 extern long vread(char *buf, char *addr, unsigned long count); 257 258 /* 259 * Internals. Don't use.. 260 */ 261 extern struct list_head vmap_area_list; 262 extern __init void vm_area_add_early(struct vm_struct *vm); 263 extern __init void vm_area_register_early(struct vm_struct *vm, size_t align); 264 265 #ifdef CONFIG_SMP 266 # ifdef CONFIG_MMU 267 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, 268 const size_t *sizes, int nr_vms, 269 size_t align); 270 271 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms); 272 # else 273 static inline struct vm_struct ** 274 pcpu_get_vm_areas(const unsigned long *offsets, 275 const size_t *sizes, int nr_vms, 276 size_t align) 277 { 278 return NULL; 279 } 280 281 static inline void 282 pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) 283 { 284 } 285 # endif 286 #endif 287 288 #ifdef CONFIG_MMU 289 #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START) 290 #else 291 #define VMALLOC_TOTAL 0UL 292 #endif 293 294 int register_vmap_purge_notifier(struct notifier_block *nb); 295 int unregister_vmap_purge_notifier(struct notifier_block *nb); 296 297 #if defined(CONFIG_MMU) && defined(CONFIG_PRINTK) 298 bool vmalloc_dump_obj(void *object); 299 #else 300 static inline bool vmalloc_dump_obj(void *object) { return false; } 301 #endif 302 303 #endif /* _LINUX_VMALLOC_H */ 304