Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_VMALLOC_H |
| 3 | #define _LINUX_VMALLOC_H |
| 4 | |
| 5 | #include <linux/spinlock.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 6 | #include <linux/init.h> |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 7 | #include <linux/list.h> |
Chris Wilson | 80c4bd7 | 2016-05-20 16:57:38 -0700 | [diff] [blame] | 8 | #include <linux/llist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <asm/page.h> /* pgprot_t */ |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 10 | #include <linux/rbtree.h> |
Kees Cook | 3b3b1a29e | 2018-05-08 12:55:26 -0700 | [diff] [blame] | 11 | #include <linux/overflow.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Ingo Molnar | 1f059df | 2019-11-28 08:19:36 +0100 | [diff] [blame] | 13 | #include <asm/vmalloc.h> |
| 14 | |
Hugh Dickins | 605d928 | 2008-08-16 11:07:21 +0100 | [diff] [blame] | 15 | struct vm_area_struct; /* vma defining user mapping in mm_types.h */ |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 16 | struct notifier_block; /* in notifier.h */ |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 17 | |
Hugh Dickins | 605d928 | 2008-08-16 11:07:21 +0100 | [diff] [blame] | 18 | /* bits in flags of vmalloc's vm_struct below */ |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 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 */ |
Christoph Hellwig | fe9041c | 2019-06-03 08:55:13 +0200 | [diff] [blame] | 23 | #define VM_DMA_COHERENT 0x00000010 /* dma_alloc_coherent */ |
Zhang Yanfei | 20fc02b | 2013-07-08 15:59:58 -0700 | [diff] [blame] | 24 | #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */ |
Andrey Ryabinin | 71394fe | 2015-02-13 14:40:03 -0800 | [diff] [blame] | 25 | #define VM_NO_GUARD 0x00000040 /* don't add guard page */ |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 26 | #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */ |
Rick Edgecombe | 9abdd2c | 2021-02-04 18:32:24 -0800 | [diff] [blame] | 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 */ |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * VM_KASAN is used slighly differently depending on CONFIG_KASAN_VMALLOC. |
| 32 | * |
| 33 | * If IS_ENABLED(CONFIG_KASAN_VMALLOC), VM_KASAN is set on a vm_struct after |
| 34 | * shadow memory has been mapped. It's used to handle allocation errors so that |
| 35 | * we don't try to poision shadow on free if it was never allocated. |
| 36 | * |
| 37 | * Otherwise, VM_KASAN is set for kasan_module_alloc() allocations and used to |
| 38 | * determine which allocations need the module shadow freed. |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* bits [20..32] reserved for arch specific ioremap internals */ |
| 42 | |
Deepak Saxena | fd195c4 | 2005-09-03 15:54:58 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Maximum alignment for ioremap() regions. |
| 45 | * Can be overriden by arch-specific value. |
| 46 | */ |
| 47 | #ifndef IOREMAP_MAX_ORDER |
| 48 | #define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */ |
| 49 | #endif |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | struct vm_struct { |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 52 | struct vm_struct *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | void *addr; |
| 54 | unsigned long size; |
| 55 | unsigned long flags; |
| 56 | struct page **pages; |
| 57 | unsigned int nr_pages; |
Kenji Kaneshige | ffa71f3 | 2010-06-18 12:22:40 +0900 | [diff] [blame] | 58 | phys_addr_t phys_addr; |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 59 | const void *caller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 62 | struct vmap_area { |
| 63 | unsigned long va_start; |
| 64 | unsigned long va_end; |
Uladzislau Rezki (Sony) | 68ad4a3 | 2019-05-17 14:31:31 -0700 | [diff] [blame] | 65 | |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 66 | struct rb_node rb_node; /* address sorted rbtree */ |
| 67 | struct list_head list; /* address sorted list */ |
Pengfei Li | 688fcbf | 2019-09-23 15:36:39 -0700 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * The following three variables can be packed, because |
| 71 | * a vmap_area object is always one of the three states: |
| 72 | * 1) in "free" tree (root is vmap_area_root) |
| 73 | * 2) in "busy" tree (root is free_vmap_area_root) |
| 74 | * 3) in purge list (head is vmap_purge_list) |
| 75 | */ |
| 76 | union { |
| 77 | unsigned long subtree_max_size; /* in "free" tree */ |
| 78 | struct vm_struct *vm; /* in "busy" tree */ |
| 79 | struct llist_node purge_list; /* in purge list */ |
| 80 | }; |
Atsushi Kumagai | 13ba3fc | 2013-04-29 15:07:40 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Highlevel APIs for driver use |
| 85 | */ |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 86 | extern void vm_unmap_ram(const void *mem, unsigned int count); |
Christoph Hellwig | d4efd79 | 2020-06-01 21:51:27 -0700 | [diff] [blame] | 87 | extern void *vm_map_ram(struct page **pages, unsigned int count, int node); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 88 | extern void vm_unmap_aliases(void); |
| 89 | |
| 90 | #ifdef CONFIG_MMU |
| 91 | extern void __init vmalloc_init(void); |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 92 | extern unsigned long vmalloc_nr_pages(void); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 93 | #else |
| 94 | static inline void vmalloc_init(void) |
| 95 | { |
| 96 | } |
Roman Gushchin | 97105f0 | 2019-07-11 21:00:13 -0700 | [diff] [blame] | 97 | static inline unsigned long vmalloc_nr_pages(void) { return 0; } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 98 | #endif |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | extern void *vmalloc(unsigned long size); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 101 | extern void *vzalloc(unsigned long size); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 102 | extern void *vmalloc_user(unsigned long size); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 103 | extern void *vmalloc_node(unsigned long size, int node); |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 104 | extern void *vzalloc_node(unsigned long size, int node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | extern void *vmalloc_32(unsigned long size); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 106 | extern void *vmalloc_32_user(unsigned long size); |
Christoph Hellwig | 88dca4c | 2020-06-01 21:51:40 -0700 | [diff] [blame] | 107 | extern void *__vmalloc(unsigned long size, gfp_t gfp_mask); |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 108 | extern void *__vmalloc_node_range(unsigned long size, unsigned long align, |
| 109 | unsigned long start, unsigned long end, gfp_t gfp_mask, |
Andrey Ryabinin | cb9e3c2 | 2015-02-13 14:40:07 -0800 | [diff] [blame] | 110 | pgprot_t prot, unsigned long vm_flags, int node, |
| 111 | const void *caller); |
Christoph Hellwig | 2b90594 | 2020-06-01 21:51:53 -0700 | [diff] [blame] | 112 | void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, |
| 113 | int node, const void *caller); |
Andrey Ryabinin | cb9e3c2 | 2015-02-13 14:40:07 -0800 | [diff] [blame] | 114 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 115 | extern void vfree(const void *addr); |
Andrey Ryabinin | bf22e37 | 2016-12-12 16:44:10 -0800 | [diff] [blame] | 116 | extern void vfree_atomic(const void *addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | extern void *vmap(struct page **pages, unsigned int count, |
| 119 | unsigned long flags, pgprot_t prot); |
Christoph Hellwig | 3e9a9e2 | 2020-10-17 16:15:10 -0700 | [diff] [blame] | 120 | void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot); |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 121 | extern void vunmap(const void *addr); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 122 | |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 123 | extern int remap_vmalloc_range_partial(struct vm_area_struct *vma, |
| 124 | unsigned long uaddr, void *kaddr, |
Jann Horn | bdebd6a2 | 2020-04-20 18:14:11 -0700 | [diff] [blame] | 125 | unsigned long pgoff, unsigned long size); |
HATAYAMA Daisuke | e69e9d4a | 2013-07-03 15:02:18 -0700 | [diff] [blame] | 126 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 127 | extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 128 | unsigned long pgoff); |
Joerg Roedel | 763802b5 | 2020-03-21 18:22:41 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
Joerg Roedel | 2ba3e69 | 2020-06-01 21:52:22 -0700 | [diff] [blame] | 131 | * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values |
| 132 | * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings() |
| 133 | * needs to be called. |
| 134 | */ |
| 135 | #ifndef ARCH_PAGE_TABLE_SYNC_MASK |
| 136 | #define ARCH_PAGE_TABLE_SYNC_MASK 0 |
| 137 | #endif |
| 138 | |
| 139 | /* |
| 140 | * There is no default implementation for arch_sync_kernel_mappings(). It is |
| 141 | * relied upon the compiler to optimize calls out if ARCH_PAGE_TABLE_SYNC_MASK |
| 142 | * is 0. |
| 143 | */ |
| 144 | void arch_sync_kernel_mappings(unsigned long start, unsigned long end); |
| 145 | |
| 146 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | * Lowlevel-APIs (not for driver use!) |
| 148 | */ |
Jeremy Fitzhardinge | 9585116 | 2007-07-21 17:11:35 +0200 | [diff] [blame] | 149 | |
| 150 | static inline size_t get_vm_area_size(const struct vm_struct *area) |
| 151 | { |
Andrey Ryabinin | 71394fe | 2015-02-13 14:40:03 -0800 | [diff] [blame] | 152 | if (!(area->flags & VM_NO_GUARD)) |
| 153 | /* return actual size without guard page */ |
| 154 | return area->size - PAGE_SIZE; |
| 155 | else |
| 156 | return area->size; |
| 157 | |
Jeremy Fitzhardinge | 9585116 | 2007-07-21 17:11:35 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | extern struct vm_struct *get_vm_area(unsigned long size, unsigned long flags); |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 161 | extern struct vm_struct *get_vm_area_caller(unsigned long size, |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 162 | unsigned long flags, const void *caller); |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 163 | extern struct vm_struct *__get_vm_area_caller(unsigned long size, |
| 164 | unsigned long flags, |
| 165 | unsigned long start, unsigned long end, |
Marek Szyprowski | 5e6cafc | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 166 | const void *caller); |
Christoph Hellwig | 301fa9f | 2020-10-17 16:15:39 -0700 | [diff] [blame] | 167 | void free_vm_area(struct vm_struct *area); |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 168 | extern struct vm_struct *remove_vm_area(const void *addr); |
Marek Szyprowski | e9da6e9 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 169 | extern struct vm_struct *find_vm_area(const void *addr); |
Benjamin Herrenschmidt | c19c03f | 2007-06-04 15:15:35 +1000 | [diff] [blame] | 170 | |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 171 | #ifdef CONFIG_MMU |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 172 | extern int map_kernel_range_noflush(unsigned long start, unsigned long size, |
| 173 | pgprot_t prot, struct page **pages); |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 174 | int map_kernel_range(unsigned long start, unsigned long size, pgprot_t prot, |
| 175 | struct page **pages); |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 176 | extern void unmap_kernel_range_noflush(unsigned long addr, unsigned long size); |
Benjamin Herrenschmidt | c19c03f | 2007-06-04 15:15:35 +1000 | [diff] [blame] | 177 | extern void unmap_kernel_range(unsigned long addr, unsigned long size); |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 178 | static inline void set_vm_flush_reset_perms(void *addr) |
| 179 | { |
| 180 | struct vm_struct *vm = find_vm_area(addr); |
| 181 | |
| 182 | if (vm) |
| 183 | vm->flags |= VM_FLUSH_RESET_PERMS; |
| 184 | } |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 185 | #else |
| 186 | static inline int |
| 187 | map_kernel_range_noflush(unsigned long start, unsigned long size, |
| 188 | pgprot_t prot, struct page **pages) |
| 189 | { |
| 190 | return size >> PAGE_SHIFT; |
| 191 | } |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 192 | #define map_kernel_range map_kernel_range_noflush |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 193 | static inline void |
| 194 | unmap_kernel_range_noflush(unsigned long addr, unsigned long size) |
| 195 | { |
| 196 | } |
Christoph Hellwig | ed1f324 | 2020-06-01 21:51:19 -0700 | [diff] [blame] | 197 | #define unmap_kernel_range unmap_kernel_range_noflush |
Rick Edgecombe | 868b104 | 2019-04-25 17:11:36 -0700 | [diff] [blame] | 198 | static inline void set_vm_flush_reset_perms(void *addr) |
| 199 | { |
| 200 | } |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 201 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
KOSAKI Motohiro | 69beeb1 | 2009-01-06 14:39:46 -0800 | [diff] [blame] | 203 | /* for /dev/kmem */ |
| 204 | extern long vread(char *buf, char *addr, unsigned long count); |
| 205 | extern long vwrite(char *buf, char *addr, unsigned long count); |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /* |
| 208 | * Internals. Dont't use.. |
| 209 | */ |
Joonsoo Kim | f1c4069 | 2013-04-29 15:07:37 -0700 | [diff] [blame] | 210 | extern struct list_head vmap_area_list; |
Nicolas Pitre | be9b733 | 2011-08-25 00:24:21 -0400 | [diff] [blame] | 211 | extern __init void vm_area_add_early(struct vm_struct *vm); |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 212 | extern __init void vm_area_register_early(struct vm_struct *vm, size_t align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 214 | #ifdef CONFIG_SMP |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 215 | # ifdef CONFIG_MMU |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 216 | struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, |
| 217 | const size_t *sizes, int nr_vms, |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 218 | size_t align); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 219 | |
| 220 | void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms); |
Graf Yang | b554cb4 | 2011-03-28 12:53:29 +0100 | [diff] [blame] | 221 | # else |
| 222 | static inline struct vm_struct ** |
| 223 | pcpu_get_vm_areas(const unsigned long *offsets, |
| 224 | const size_t *sizes, int nr_vms, |
| 225 | size_t align) |
| 226 | { |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | static inline void |
| 231 | pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) |
| 232 | { |
| 233 | } |
| 234 | # endif |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 235 | #endif |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 236 | |
Joonsoo Kim | db3808c | 2013-04-29 15:07:28 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_MMU |
| 238 | #define VMALLOC_TOTAL (VMALLOC_END - VMALLOC_START) |
Joonsoo Kim | db3808c | 2013-04-29 15:07:28 -0700 | [diff] [blame] | 239 | #else |
Joonsoo Kim | db3808c | 2013-04-29 15:07:28 -0700 | [diff] [blame] | 240 | #define VMALLOC_TOTAL 0UL |
Joonsoo Kim | db3808c | 2013-04-29 15:07:28 -0700 | [diff] [blame] | 241 | #endif |
| 242 | |
Chris Wilson | 4da56b9 | 2016-04-04 14:46:42 +0100 | [diff] [blame] | 243 | int register_vmap_purge_notifier(struct notifier_block *nb); |
| 244 | int unregister_vmap_purge_notifier(struct notifier_block *nb); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | #endif /* _LINUX_VMALLOC_H */ |