Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/vmalloc.c |
| 3 | * |
| 4 | * Copyright (C) 1993 Linus Torvalds |
| 5 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| 6 | * SMP-safe vmalloc/vfree/ioremap, Tigran Aivazian <tigran@veritas.com>, May 2000 |
| 7 | * Major rework to support vmap/vunmap, Christoph Hellwig, SGI, August 2002 |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 8 | * Numa awareness, Christoph Lameter, SGI, June 2005 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 11 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/highmem.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/interrupt.h> |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 18 | #include <linux/proc_fs.h> |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 19 | #include <linux/seq_file.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 20 | #include <linux/debugobjects.h> |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 21 | #include <linux/kallsyms.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/rbtree.h> |
| 24 | #include <linux/radix-tree.h> |
| 25 | #include <linux/rcupdate.h> |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 26 | #include <linux/pfn.h> |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 27 | #include <linux/kmemleak.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 29 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/tlbflush.h> |
| 32 | |
| 33 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 34 | /*** Page table manipulation functions ***/ |
Adrian Bunk | b221385 | 2006-09-25 23:31:02 -0700 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end) |
| 37 | { |
| 38 | pte_t *pte; |
| 39 | |
| 40 | pte = pte_offset_kernel(pmd, addr); |
| 41 | do { |
| 42 | pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte); |
| 43 | WARN_ON(!pte_none(ptent) && !pte_present(ptent)); |
| 44 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 45 | } |
| 46 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 47 | static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | pmd_t *pmd; |
| 50 | unsigned long next; |
| 51 | |
| 52 | pmd = pmd_offset(pud, addr); |
| 53 | do { |
| 54 | next = pmd_addr_end(addr, end); |
| 55 | if (pmd_none_or_clear_bad(pmd)) |
| 56 | continue; |
| 57 | vunmap_pte_range(pmd, addr, next); |
| 58 | } while (pmd++, addr = next, addr != end); |
| 59 | } |
| 60 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 61 | static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
| 63 | pud_t *pud; |
| 64 | unsigned long next; |
| 65 | |
| 66 | pud = pud_offset(pgd, addr); |
| 67 | do { |
| 68 | next = pud_addr_end(addr, end); |
| 69 | if (pud_none_or_clear_bad(pud)) |
| 70 | continue; |
| 71 | vunmap_pmd_range(pud, addr, next); |
| 72 | } while (pud++, addr = next, addr != end); |
| 73 | } |
| 74 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 75 | static void vunmap_page_range(unsigned long addr, unsigned long end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | pgd_t *pgd; |
| 78 | unsigned long next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | BUG_ON(addr >= end); |
| 81 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | do { |
| 83 | next = pgd_addr_end(addr, end); |
| 84 | if (pgd_none_or_clear_bad(pgd)) |
| 85 | continue; |
| 86 | vunmap_pud_range(pgd, addr, next); |
| 87 | } while (pgd++, addr = next, addr != end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int vmap_pte_range(pmd_t *pmd, unsigned long addr, |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 91 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | pte_t *pte; |
| 94 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 95 | /* |
| 96 | * nr is a running index into the array which helps higher level |
| 97 | * callers keep track of where we're up to. |
| 98 | */ |
| 99 | |
Hugh Dickins | 872fec1 | 2005-10-29 18:16:21 -0700 | [diff] [blame] | 100 | pte = pte_alloc_kernel(pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (!pte) |
| 102 | return -ENOMEM; |
| 103 | do { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 104 | struct page *page = pages[*nr]; |
| 105 | |
| 106 | if (WARN_ON(!pte_none(*pte))) |
| 107 | return -EBUSY; |
| 108 | if (WARN_ON(!page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return -ENOMEM; |
| 110 | set_pte_at(&init_mm, addr, pte, mk_pte(page, prot)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 111 | (*nr)++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } while (pte++, addr += PAGE_SIZE, addr != end); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 116 | static int vmap_pmd_range(pud_t *pud, unsigned long addr, |
| 117 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
| 119 | pmd_t *pmd; |
| 120 | unsigned long next; |
| 121 | |
| 122 | pmd = pmd_alloc(&init_mm, pud, addr); |
| 123 | if (!pmd) |
| 124 | return -ENOMEM; |
| 125 | do { |
| 126 | next = pmd_addr_end(addr, end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 127 | if (vmap_pte_range(pmd, addr, next, prot, pages, nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return -ENOMEM; |
| 129 | } while (pmd++, addr = next, addr != end); |
| 130 | return 0; |
| 131 | } |
| 132 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 133 | static int vmap_pud_range(pgd_t *pgd, unsigned long addr, |
| 134 | unsigned long end, pgprot_t prot, struct page **pages, int *nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
| 136 | pud_t *pud; |
| 137 | unsigned long next; |
| 138 | |
| 139 | pud = pud_alloc(&init_mm, pgd, addr); |
| 140 | if (!pud) |
| 141 | return -ENOMEM; |
| 142 | do { |
| 143 | next = pud_addr_end(addr, end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 144 | if (vmap_pmd_range(pud, addr, next, prot, pages, nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -ENOMEM; |
| 146 | } while (pud++, addr = next, addr != end); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and |
| 152 | * will have pfns corresponding to the "pages" array. |
| 153 | * |
| 154 | * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] |
| 155 | */ |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 156 | static int vmap_page_range_noflush(unsigned long start, unsigned long end, |
| 157 | pgprot_t prot, struct page **pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | pgd_t *pgd; |
| 160 | unsigned long next; |
Adam Lackorzynski | 2e4e27c | 2009-01-04 12:00:46 -0800 | [diff] [blame] | 161 | unsigned long addr = start; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 162 | int err = 0; |
| 163 | int nr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | BUG_ON(addr >= end); |
| 166 | pgd = pgd_offset_k(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | do { |
| 168 | next = pgd_addr_end(addr, end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 169 | err = vmap_pud_range(pgd, addr, next, prot, pages, &nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (err) |
| 171 | break; |
| 172 | } while (pgd++, addr = next, addr != end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 173 | |
| 174 | if (unlikely(err)) |
| 175 | return err; |
| 176 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 179 | static int vmap_page_range(unsigned long start, unsigned long end, |
| 180 | pgprot_t prot, struct page **pages) |
| 181 | { |
| 182 | int ret; |
| 183 | |
| 184 | ret = vmap_page_range_noflush(start, end, prot, pages); |
| 185 | flush_cache_vmap(start, end); |
| 186 | return ret; |
| 187 | } |
| 188 | |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 189 | static inline int is_vmalloc_or_module_addr(const void *x) |
| 190 | { |
| 191 | /* |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 192 | * ARM, x86-64 and sparc64 put modules in a special place, |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 193 | * and fall back on vmalloc() if that fails. Others |
| 194 | * just put it in the vmalloc space. |
| 195 | */ |
| 196 | #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) |
| 197 | unsigned long addr = (unsigned long)x; |
| 198 | if (addr >= MODULES_VADDR && addr < MODULES_END) |
| 199 | return 1; |
| 200 | #endif |
| 201 | return is_vmalloc_addr(x); |
| 202 | } |
| 203 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 204 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 205 | * Walk a vmap address to the struct page it maps. |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 206 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 207 | struct page *vmalloc_to_page(const void *vmalloc_addr) |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 208 | { |
| 209 | unsigned long addr = (unsigned long) vmalloc_addr; |
| 210 | struct page *page = NULL; |
| 211 | pgd_t *pgd = pgd_offset_k(addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 212 | |
Ingo Molnar | 7aa413d | 2008-06-19 13:28:11 +0200 | [diff] [blame] | 213 | /* |
| 214 | * XXX we might need to change this if we add VIRTUAL_BUG_ON for |
| 215 | * architectures that do not vmalloc module space |
| 216 | */ |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 217 | VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); |
Jiri Slaby | 59ea746 | 2008-06-12 13:56:40 +0200 | [diff] [blame] | 218 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 219 | if (!pgd_none(*pgd)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 220 | pud_t *pud = pud_offset(pgd, addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 221 | if (!pud_none(*pud)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 222 | pmd_t *pmd = pmd_offset(pud, addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 223 | if (!pmd_none(*pmd)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 224 | pte_t *ptep, pte; |
| 225 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 226 | ptep = pte_offset_map(pmd, addr); |
| 227 | pte = *ptep; |
| 228 | if (pte_present(pte)) |
| 229 | page = pte_page(pte); |
| 230 | pte_unmap(ptep); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return page; |
| 235 | } |
| 236 | EXPORT_SYMBOL(vmalloc_to_page); |
| 237 | |
| 238 | /* |
| 239 | * Map a vmalloc()-space virtual address to the physical page frame number. |
| 240 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 241 | unsigned long vmalloc_to_pfn(const void *vmalloc_addr) |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 242 | { |
| 243 | return page_to_pfn(vmalloc_to_page(vmalloc_addr)); |
| 244 | } |
| 245 | EXPORT_SYMBOL(vmalloc_to_pfn); |
| 246 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 247 | |
| 248 | /*** Global kva allocator ***/ |
| 249 | |
| 250 | #define VM_LAZY_FREE 0x01 |
| 251 | #define VM_LAZY_FREEING 0x02 |
| 252 | #define VM_VM_AREA 0x04 |
| 253 | |
| 254 | struct vmap_area { |
| 255 | unsigned long va_start; |
| 256 | unsigned long va_end; |
| 257 | unsigned long flags; |
| 258 | struct rb_node rb_node; /* address sorted rbtree */ |
| 259 | struct list_head list; /* address sorted list */ |
| 260 | struct list_head purge_list; /* "lazy purge" list */ |
| 261 | void *private; |
| 262 | struct rcu_head rcu_head; |
| 263 | }; |
| 264 | |
| 265 | static DEFINE_SPINLOCK(vmap_area_lock); |
| 266 | static struct rb_root vmap_area_root = RB_ROOT; |
| 267 | static LIST_HEAD(vmap_area_list); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 268 | static unsigned long vmap_area_pcpu_hole; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 269 | |
| 270 | static struct vmap_area *__find_vmap_area(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 272 | struct rb_node *n = vmap_area_root.rb_node; |
| 273 | |
| 274 | while (n) { |
| 275 | struct vmap_area *va; |
| 276 | |
| 277 | va = rb_entry(n, struct vmap_area, rb_node); |
| 278 | if (addr < va->va_start) |
| 279 | n = n->rb_left; |
| 280 | else if (addr > va->va_start) |
| 281 | n = n->rb_right; |
| 282 | else |
| 283 | return va; |
| 284 | } |
| 285 | |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | static void __insert_vmap_area(struct vmap_area *va) |
| 290 | { |
| 291 | struct rb_node **p = &vmap_area_root.rb_node; |
| 292 | struct rb_node *parent = NULL; |
| 293 | struct rb_node *tmp; |
| 294 | |
| 295 | while (*p) { |
| 296 | struct vmap_area *tmp; |
| 297 | |
| 298 | parent = *p; |
| 299 | tmp = rb_entry(parent, struct vmap_area, rb_node); |
| 300 | if (va->va_start < tmp->va_end) |
| 301 | p = &(*p)->rb_left; |
| 302 | else if (va->va_end > tmp->va_start) |
| 303 | p = &(*p)->rb_right; |
| 304 | else |
| 305 | BUG(); |
| 306 | } |
| 307 | |
| 308 | rb_link_node(&va->rb_node, parent, p); |
| 309 | rb_insert_color(&va->rb_node, &vmap_area_root); |
| 310 | |
| 311 | /* address-sort this list so it is usable like the vmlist */ |
| 312 | tmp = rb_prev(&va->rb_node); |
| 313 | if (tmp) { |
| 314 | struct vmap_area *prev; |
| 315 | prev = rb_entry(tmp, struct vmap_area, rb_node); |
| 316 | list_add_rcu(&va->list, &prev->list); |
| 317 | } else |
| 318 | list_add_rcu(&va->list, &vmap_area_list); |
| 319 | } |
| 320 | |
| 321 | static void purge_vmap_area_lazy(void); |
| 322 | |
| 323 | /* |
| 324 | * Allocate a region of KVA of the specified size and alignment, within the |
| 325 | * vstart and vend. |
| 326 | */ |
| 327 | static struct vmap_area *alloc_vmap_area(unsigned long size, |
| 328 | unsigned long align, |
| 329 | unsigned long vstart, unsigned long vend, |
| 330 | int node, gfp_t gfp_mask) |
| 331 | { |
| 332 | struct vmap_area *va; |
| 333 | struct rb_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | unsigned long addr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 335 | int purged = 0; |
| 336 | |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 337 | BUG_ON(!size); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 338 | BUG_ON(size & ~PAGE_MASK); |
| 339 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 340 | va = kmalloc_node(sizeof(struct vmap_area), |
| 341 | gfp_mask & GFP_RECLAIM_MASK, node); |
| 342 | if (unlikely(!va)) |
| 343 | return ERR_PTR(-ENOMEM); |
| 344 | |
| 345 | retry: |
Glauber Costa | 0ae1513 | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 346 | addr = ALIGN(vstart, align); |
| 347 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 348 | spin_lock(&vmap_area_lock); |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 349 | if (addr + size - 1 < addr) |
| 350 | goto overflow; |
| 351 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 352 | /* XXX: could have a last_hole cache */ |
| 353 | n = vmap_area_root.rb_node; |
| 354 | if (n) { |
| 355 | struct vmap_area *first = NULL; |
| 356 | |
| 357 | do { |
| 358 | struct vmap_area *tmp; |
| 359 | tmp = rb_entry(n, struct vmap_area, rb_node); |
| 360 | if (tmp->va_end >= addr) { |
| 361 | if (!first && tmp->va_start < addr + size) |
| 362 | first = tmp; |
| 363 | n = n->rb_left; |
| 364 | } else { |
| 365 | first = tmp; |
| 366 | n = n->rb_right; |
| 367 | } |
| 368 | } while (n); |
| 369 | |
| 370 | if (!first) |
| 371 | goto found; |
| 372 | |
| 373 | if (first->va_end < addr) { |
| 374 | n = rb_next(&first->rb_node); |
| 375 | if (n) |
| 376 | first = rb_entry(n, struct vmap_area, rb_node); |
| 377 | else |
| 378 | goto found; |
| 379 | } |
| 380 | |
Nick Piggin | f011c2d | 2008-11-19 15:36:32 -0800 | [diff] [blame] | 381 | while (addr + size > first->va_start && addr + size <= vend) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 382 | addr = ALIGN(first->va_end + PAGE_SIZE, align); |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 383 | if (addr + size - 1 < addr) |
| 384 | goto overflow; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 385 | |
| 386 | n = rb_next(&first->rb_node); |
| 387 | if (n) |
| 388 | first = rb_entry(n, struct vmap_area, rb_node); |
| 389 | else |
| 390 | goto found; |
| 391 | } |
| 392 | } |
| 393 | found: |
| 394 | if (addr + size > vend) { |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 395 | overflow: |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 396 | spin_unlock(&vmap_area_lock); |
| 397 | if (!purged) { |
| 398 | purge_vmap_area_lazy(); |
| 399 | purged = 1; |
| 400 | goto retry; |
| 401 | } |
| 402 | if (printk_ratelimit()) |
Glauber Costa | c1279c4 | 2009-01-06 14:39:18 -0800 | [diff] [blame] | 403 | printk(KERN_WARNING |
| 404 | "vmap allocation for size %lu failed: " |
| 405 | "use vmalloc=<size> to increase size.\n", size); |
Ralph Wuerthner | 2498ce4 | 2009-05-06 16:02:59 -0700 | [diff] [blame] | 406 | kfree(va); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 407 | return ERR_PTR(-EBUSY); |
| 408 | } |
| 409 | |
| 410 | BUG_ON(addr & (align-1)); |
| 411 | |
| 412 | va->va_start = addr; |
| 413 | va->va_end = addr + size; |
| 414 | va->flags = 0; |
| 415 | __insert_vmap_area(va); |
| 416 | spin_unlock(&vmap_area_lock); |
| 417 | |
| 418 | return va; |
| 419 | } |
| 420 | |
| 421 | static void rcu_free_va(struct rcu_head *head) |
| 422 | { |
| 423 | struct vmap_area *va = container_of(head, struct vmap_area, rcu_head); |
| 424 | |
| 425 | kfree(va); |
| 426 | } |
| 427 | |
| 428 | static void __free_vmap_area(struct vmap_area *va) |
| 429 | { |
| 430 | BUG_ON(RB_EMPTY_NODE(&va->rb_node)); |
| 431 | rb_erase(&va->rb_node, &vmap_area_root); |
| 432 | RB_CLEAR_NODE(&va->rb_node); |
| 433 | list_del_rcu(&va->list); |
| 434 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 435 | /* |
| 436 | * Track the highest possible candidate for pcpu area |
| 437 | * allocation. Areas outside of vmalloc area can be returned |
| 438 | * here too, consider only end addresses which fall inside |
| 439 | * vmalloc area proper. |
| 440 | */ |
| 441 | if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END) |
| 442 | vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end); |
| 443 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 444 | call_rcu(&va->rcu_head, rcu_free_va); |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Free a region of KVA allocated by alloc_vmap_area |
| 449 | */ |
| 450 | static void free_vmap_area(struct vmap_area *va) |
| 451 | { |
| 452 | spin_lock(&vmap_area_lock); |
| 453 | __free_vmap_area(va); |
| 454 | spin_unlock(&vmap_area_lock); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Clear the pagetable entries of a given vmap_area |
| 459 | */ |
| 460 | static void unmap_vmap_area(struct vmap_area *va) |
| 461 | { |
| 462 | vunmap_page_range(va->va_start, va->va_end); |
| 463 | } |
| 464 | |
Nick Piggin | cd52858 | 2009-01-06 14:39:20 -0800 | [diff] [blame] | 465 | static void vmap_debug_free_range(unsigned long start, unsigned long end) |
| 466 | { |
| 467 | /* |
| 468 | * Unmap page tables and force a TLB flush immediately if |
| 469 | * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free |
| 470 | * bugs similarly to those in linear kernel virtual address |
| 471 | * space after a page has been freed. |
| 472 | * |
| 473 | * All the lazy freeing logic is still retained, in order to |
| 474 | * minimise intrusiveness of this debugging feature. |
| 475 | * |
| 476 | * This is going to be *slow* (linear kernel virtual address |
| 477 | * debugging doesn't do a broadcast TLB flush so it is a lot |
| 478 | * faster). |
| 479 | */ |
| 480 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 481 | vunmap_page_range(start, end); |
| 482 | flush_tlb_kernel_range(start, end); |
| 483 | #endif |
| 484 | } |
| 485 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 486 | /* |
| 487 | * lazy_max_pages is the maximum amount of virtual address space we gather up |
| 488 | * before attempting to purge with a TLB flush. |
| 489 | * |
| 490 | * There is a tradeoff here: a larger number will cover more kernel page tables |
| 491 | * and take slightly longer to purge, but it will linearly reduce the number of |
| 492 | * global TLB flushes that must be performed. It would seem natural to scale |
| 493 | * this number up linearly with the number of CPUs (because vmapping activity |
| 494 | * could also scale linearly with the number of CPUs), however it is likely |
| 495 | * that in practice, workloads might be constrained in other ways that mean |
| 496 | * vmap activity will not scale linearly with CPUs. Also, I want to be |
| 497 | * conservative and not introduce a big latency on huge systems, so go with |
| 498 | * a less aggressive log scale. It will still be an improvement over the old |
| 499 | * code, and it will be simple to change the scale factor if we find that it |
| 500 | * becomes a problem on bigger systems. |
| 501 | */ |
| 502 | static unsigned long lazy_max_pages(void) |
| 503 | { |
| 504 | unsigned int log; |
| 505 | |
| 506 | log = fls(num_online_cpus()); |
| 507 | |
| 508 | return log * (32UL * 1024 * 1024 / PAGE_SIZE); |
| 509 | } |
| 510 | |
| 511 | static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); |
| 512 | |
| 513 | /* |
| 514 | * Purges all lazily-freed vmap areas. |
| 515 | * |
| 516 | * If sync is 0 then don't purge if there is already a purge in progress. |
| 517 | * If force_flush is 1, then flush kernel TLBs between *start and *end even |
| 518 | * if we found no lazy vmap areas to unmap (callers can use this to optimise |
| 519 | * their own TLB flushing). |
| 520 | * Returns with *start = min(*start, lowest purged address) |
| 521 | * *end = max(*end, highest purged address) |
| 522 | */ |
| 523 | static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end, |
| 524 | int sync, int force_flush) |
| 525 | { |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 526 | static DEFINE_SPINLOCK(purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 527 | LIST_HEAD(valist); |
| 528 | struct vmap_area *va; |
Vegard Nossum | cbb7667 | 2009-02-27 14:03:04 -0800 | [diff] [blame] | 529 | struct vmap_area *n_va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 530 | int nr = 0; |
| 531 | |
| 532 | /* |
| 533 | * If sync is 0 but force_flush is 1, we'll go sync anyway but callers |
| 534 | * should not expect such behaviour. This just simplifies locking for |
| 535 | * the case that isn't actually used at the moment anyway. |
| 536 | */ |
| 537 | if (!sync && !force_flush) { |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 538 | if (!spin_trylock(&purge_lock)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 539 | return; |
| 540 | } else |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 541 | spin_lock(&purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 542 | |
| 543 | rcu_read_lock(); |
| 544 | list_for_each_entry_rcu(va, &vmap_area_list, list) { |
| 545 | if (va->flags & VM_LAZY_FREE) { |
| 546 | if (va->va_start < *start) |
| 547 | *start = va->va_start; |
| 548 | if (va->va_end > *end) |
| 549 | *end = va->va_end; |
| 550 | nr += (va->va_end - va->va_start) >> PAGE_SHIFT; |
| 551 | unmap_vmap_area(va); |
| 552 | list_add_tail(&va->purge_list, &valist); |
| 553 | va->flags |= VM_LAZY_FREEING; |
| 554 | va->flags &= ~VM_LAZY_FREE; |
| 555 | } |
| 556 | } |
| 557 | rcu_read_unlock(); |
| 558 | |
| 559 | if (nr) { |
| 560 | BUG_ON(nr > atomic_read(&vmap_lazy_nr)); |
| 561 | atomic_sub(nr, &vmap_lazy_nr); |
| 562 | } |
| 563 | |
| 564 | if (nr || force_flush) |
| 565 | flush_tlb_kernel_range(*start, *end); |
| 566 | |
| 567 | if (nr) { |
| 568 | spin_lock(&vmap_area_lock); |
Vegard Nossum | cbb7667 | 2009-02-27 14:03:04 -0800 | [diff] [blame] | 569 | list_for_each_entry_safe(va, n_va, &valist, purge_list) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 570 | __free_vmap_area(va); |
| 571 | spin_unlock(&vmap_area_lock); |
| 572 | } |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 573 | spin_unlock(&purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 577 | * Kick off a purge of the outstanding lazy areas. Don't bother if somebody |
| 578 | * is already purging. |
| 579 | */ |
| 580 | static void try_purge_vmap_area_lazy(void) |
| 581 | { |
| 582 | unsigned long start = ULONG_MAX, end = 0; |
| 583 | |
| 584 | __purge_vmap_area_lazy(&start, &end, 0, 0); |
| 585 | } |
| 586 | |
| 587 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 588 | * Kick off a purge of the outstanding lazy areas. |
| 589 | */ |
| 590 | static void purge_vmap_area_lazy(void) |
| 591 | { |
| 592 | unsigned long start = ULONG_MAX, end = 0; |
| 593 | |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 594 | __purge_vmap_area_lazy(&start, &end, 1, 0); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 598 | * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been |
| 599 | * called for the correct range previously. |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 600 | */ |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 601 | static void free_unmap_vmap_area_noflush(struct vmap_area *va) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 602 | { |
| 603 | va->flags |= VM_LAZY_FREE; |
| 604 | atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr); |
| 605 | if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages())) |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 606 | try_purge_vmap_area_lazy(); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 609 | /* |
| 610 | * Free and unmap a vmap area |
| 611 | */ |
| 612 | static void free_unmap_vmap_area(struct vmap_area *va) |
| 613 | { |
| 614 | flush_cache_vunmap(va->va_start, va->va_end); |
| 615 | free_unmap_vmap_area_noflush(va); |
| 616 | } |
| 617 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 618 | static struct vmap_area *find_vmap_area(unsigned long addr) |
| 619 | { |
| 620 | struct vmap_area *va; |
| 621 | |
| 622 | spin_lock(&vmap_area_lock); |
| 623 | va = __find_vmap_area(addr); |
| 624 | spin_unlock(&vmap_area_lock); |
| 625 | |
| 626 | return va; |
| 627 | } |
| 628 | |
| 629 | static void free_unmap_vmap_area_addr(unsigned long addr) |
| 630 | { |
| 631 | struct vmap_area *va; |
| 632 | |
| 633 | va = find_vmap_area(addr); |
| 634 | BUG_ON(!va); |
| 635 | free_unmap_vmap_area(va); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /*** Per cpu kva allocator ***/ |
| 640 | |
| 641 | /* |
| 642 | * vmap space is limited especially on 32 bit architectures. Ensure there is |
| 643 | * room for at least 16 percpu vmap blocks per CPU. |
| 644 | */ |
| 645 | /* |
| 646 | * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able |
| 647 | * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess |
| 648 | * instead (we just need a rough idea) |
| 649 | */ |
| 650 | #if BITS_PER_LONG == 32 |
| 651 | #define VMALLOC_SPACE (128UL*1024*1024) |
| 652 | #else |
| 653 | #define VMALLOC_SPACE (128UL*1024*1024*1024) |
| 654 | #endif |
| 655 | |
| 656 | #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) |
| 657 | #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ |
| 658 | #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ |
| 659 | #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) |
| 660 | #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ |
| 661 | #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ |
| 662 | #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ |
| 663 | VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ |
| 664 | VMALLOC_PAGES / NR_CPUS / 16)) |
| 665 | |
| 666 | #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) |
| 667 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 668 | static bool vmap_initialized __read_mostly = false; |
| 669 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 670 | struct vmap_block_queue { |
| 671 | spinlock_t lock; |
| 672 | struct list_head free; |
| 673 | struct list_head dirty; |
| 674 | unsigned int nr_dirty; |
| 675 | }; |
| 676 | |
| 677 | struct vmap_block { |
| 678 | spinlock_t lock; |
| 679 | struct vmap_area *va; |
| 680 | struct vmap_block_queue *vbq; |
| 681 | unsigned long free, dirty; |
| 682 | DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS); |
| 683 | DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); |
| 684 | union { |
MinChan Kim | d086817 | 2009-03-31 15:19:26 -0700 | [diff] [blame] | 685 | struct list_head free_list; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 686 | struct rcu_head rcu_head; |
| 687 | }; |
| 688 | }; |
| 689 | |
| 690 | /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ |
| 691 | static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); |
| 692 | |
| 693 | /* |
| 694 | * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block |
| 695 | * in the free path. Could get rid of this if we change the API to return a |
| 696 | * "cookie" from alloc, to be passed to free. But no big deal yet. |
| 697 | */ |
| 698 | static DEFINE_SPINLOCK(vmap_block_tree_lock); |
| 699 | static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); |
| 700 | |
| 701 | /* |
| 702 | * We should probably have a fallback mechanism to allocate virtual memory |
| 703 | * out of partially filled vmap blocks. However vmap block sizing should be |
| 704 | * fairly reasonable according to the vmalloc size, so it shouldn't be a |
| 705 | * big problem. |
| 706 | */ |
| 707 | |
| 708 | static unsigned long addr_to_vb_idx(unsigned long addr) |
| 709 | { |
| 710 | addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); |
| 711 | addr /= VMAP_BLOCK_SIZE; |
| 712 | return addr; |
| 713 | } |
| 714 | |
| 715 | static struct vmap_block *new_vmap_block(gfp_t gfp_mask) |
| 716 | { |
| 717 | struct vmap_block_queue *vbq; |
| 718 | struct vmap_block *vb; |
| 719 | struct vmap_area *va; |
| 720 | unsigned long vb_idx; |
| 721 | int node, err; |
| 722 | |
| 723 | node = numa_node_id(); |
| 724 | |
| 725 | vb = kmalloc_node(sizeof(struct vmap_block), |
| 726 | gfp_mask & GFP_RECLAIM_MASK, node); |
| 727 | if (unlikely(!vb)) |
| 728 | return ERR_PTR(-ENOMEM); |
| 729 | |
| 730 | va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, |
| 731 | VMALLOC_START, VMALLOC_END, |
| 732 | node, gfp_mask); |
| 733 | if (unlikely(IS_ERR(va))) { |
| 734 | kfree(vb); |
| 735 | return ERR_PTR(PTR_ERR(va)); |
| 736 | } |
| 737 | |
| 738 | err = radix_tree_preload(gfp_mask); |
| 739 | if (unlikely(err)) { |
| 740 | kfree(vb); |
| 741 | free_vmap_area(va); |
| 742 | return ERR_PTR(err); |
| 743 | } |
| 744 | |
| 745 | spin_lock_init(&vb->lock); |
| 746 | vb->va = va; |
| 747 | vb->free = VMAP_BBMAP_BITS; |
| 748 | vb->dirty = 0; |
| 749 | bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS); |
| 750 | bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); |
| 751 | INIT_LIST_HEAD(&vb->free_list); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 752 | |
| 753 | vb_idx = addr_to_vb_idx(va->va_start); |
| 754 | spin_lock(&vmap_block_tree_lock); |
| 755 | err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); |
| 756 | spin_unlock(&vmap_block_tree_lock); |
| 757 | BUG_ON(err); |
| 758 | radix_tree_preload_end(); |
| 759 | |
| 760 | vbq = &get_cpu_var(vmap_block_queue); |
| 761 | vb->vbq = vbq; |
| 762 | spin_lock(&vbq->lock); |
| 763 | list_add(&vb->free_list, &vbq->free); |
| 764 | spin_unlock(&vbq->lock); |
| 765 | put_cpu_var(vmap_cpu_blocks); |
| 766 | |
| 767 | return vb; |
| 768 | } |
| 769 | |
| 770 | static void rcu_free_vb(struct rcu_head *head) |
| 771 | { |
| 772 | struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head); |
| 773 | |
| 774 | kfree(vb); |
| 775 | } |
| 776 | |
| 777 | static void free_vmap_block(struct vmap_block *vb) |
| 778 | { |
| 779 | struct vmap_block *tmp; |
| 780 | unsigned long vb_idx; |
| 781 | |
MinChan Kim | d086817 | 2009-03-31 15:19:26 -0700 | [diff] [blame] | 782 | BUG_ON(!list_empty(&vb->free_list)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 783 | |
| 784 | vb_idx = addr_to_vb_idx(vb->va->va_start); |
| 785 | spin_lock(&vmap_block_tree_lock); |
| 786 | tmp = radix_tree_delete(&vmap_block_tree, vb_idx); |
| 787 | spin_unlock(&vmap_block_tree_lock); |
| 788 | BUG_ON(tmp != vb); |
| 789 | |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 790 | free_unmap_vmap_area_noflush(vb->va); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 791 | call_rcu(&vb->rcu_head, rcu_free_vb); |
| 792 | } |
| 793 | |
| 794 | static void *vb_alloc(unsigned long size, gfp_t gfp_mask) |
| 795 | { |
| 796 | struct vmap_block_queue *vbq; |
| 797 | struct vmap_block *vb; |
| 798 | unsigned long addr = 0; |
| 799 | unsigned int order; |
| 800 | |
| 801 | BUG_ON(size & ~PAGE_MASK); |
| 802 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
| 803 | order = get_order(size); |
| 804 | |
| 805 | again: |
| 806 | rcu_read_lock(); |
| 807 | vbq = &get_cpu_var(vmap_block_queue); |
| 808 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 809 | int i; |
| 810 | |
| 811 | spin_lock(&vb->lock); |
| 812 | i = bitmap_find_free_region(vb->alloc_map, |
| 813 | VMAP_BBMAP_BITS, order); |
| 814 | |
| 815 | if (i >= 0) { |
| 816 | addr = vb->va->va_start + (i << PAGE_SHIFT); |
| 817 | BUG_ON(addr_to_vb_idx(addr) != |
| 818 | addr_to_vb_idx(vb->va->va_start)); |
| 819 | vb->free -= 1UL << order; |
| 820 | if (vb->free == 0) { |
| 821 | spin_lock(&vbq->lock); |
| 822 | list_del_init(&vb->free_list); |
| 823 | spin_unlock(&vbq->lock); |
| 824 | } |
| 825 | spin_unlock(&vb->lock); |
| 826 | break; |
| 827 | } |
| 828 | spin_unlock(&vb->lock); |
| 829 | } |
| 830 | put_cpu_var(vmap_cpu_blocks); |
| 831 | rcu_read_unlock(); |
| 832 | |
| 833 | if (!addr) { |
| 834 | vb = new_vmap_block(gfp_mask); |
| 835 | if (IS_ERR(vb)) |
| 836 | return vb; |
| 837 | goto again; |
| 838 | } |
| 839 | |
| 840 | return (void *)addr; |
| 841 | } |
| 842 | |
| 843 | static void vb_free(const void *addr, unsigned long size) |
| 844 | { |
| 845 | unsigned long offset; |
| 846 | unsigned long vb_idx; |
| 847 | unsigned int order; |
| 848 | struct vmap_block *vb; |
| 849 | |
| 850 | BUG_ON(size & ~PAGE_MASK); |
| 851 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 852 | |
| 853 | flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); |
| 854 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 855 | order = get_order(size); |
| 856 | |
| 857 | offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); |
| 858 | |
| 859 | vb_idx = addr_to_vb_idx((unsigned long)addr); |
| 860 | rcu_read_lock(); |
| 861 | vb = radix_tree_lookup(&vmap_block_tree, vb_idx); |
| 862 | rcu_read_unlock(); |
| 863 | BUG_ON(!vb); |
| 864 | |
| 865 | spin_lock(&vb->lock); |
| 866 | bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order); |
MinChan Kim | d086817 | 2009-03-31 15:19:26 -0700 | [diff] [blame] | 867 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 868 | vb->dirty += 1UL << order; |
| 869 | if (vb->dirty == VMAP_BBMAP_BITS) { |
| 870 | BUG_ON(vb->free || !list_empty(&vb->free_list)); |
| 871 | spin_unlock(&vb->lock); |
| 872 | free_vmap_block(vb); |
| 873 | } else |
| 874 | spin_unlock(&vb->lock); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer |
| 879 | * |
| 880 | * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily |
| 881 | * to amortize TLB flushing overheads. What this means is that any page you |
| 882 | * have now, may, in a former life, have been mapped into kernel virtual |
| 883 | * address by the vmap layer and so there might be some CPUs with TLB entries |
| 884 | * still referencing that page (additional to the regular 1:1 kernel mapping). |
| 885 | * |
| 886 | * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can |
| 887 | * be sure that none of the pages we have control over will have any aliases |
| 888 | * from the vmap layer. |
| 889 | */ |
| 890 | void vm_unmap_aliases(void) |
| 891 | { |
| 892 | unsigned long start = ULONG_MAX, end = 0; |
| 893 | int cpu; |
| 894 | int flush = 0; |
| 895 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 896 | if (unlikely(!vmap_initialized)) |
| 897 | return; |
| 898 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 899 | for_each_possible_cpu(cpu) { |
| 900 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); |
| 901 | struct vmap_block *vb; |
| 902 | |
| 903 | rcu_read_lock(); |
| 904 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 905 | int i; |
| 906 | |
| 907 | spin_lock(&vb->lock); |
| 908 | i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS); |
| 909 | while (i < VMAP_BBMAP_BITS) { |
| 910 | unsigned long s, e; |
| 911 | int j; |
| 912 | j = find_next_zero_bit(vb->dirty_map, |
| 913 | VMAP_BBMAP_BITS, i); |
| 914 | |
| 915 | s = vb->va->va_start + (i << PAGE_SHIFT); |
| 916 | e = vb->va->va_start + (j << PAGE_SHIFT); |
| 917 | vunmap_page_range(s, e); |
| 918 | flush = 1; |
| 919 | |
| 920 | if (s < start) |
| 921 | start = s; |
| 922 | if (e > end) |
| 923 | end = e; |
| 924 | |
| 925 | i = j; |
| 926 | i = find_next_bit(vb->dirty_map, |
| 927 | VMAP_BBMAP_BITS, i); |
| 928 | } |
| 929 | spin_unlock(&vb->lock); |
| 930 | } |
| 931 | rcu_read_unlock(); |
| 932 | } |
| 933 | |
| 934 | __purge_vmap_area_lazy(&start, &end, 1, flush); |
| 935 | } |
| 936 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); |
| 937 | |
| 938 | /** |
| 939 | * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram |
| 940 | * @mem: the pointer returned by vm_map_ram |
| 941 | * @count: the count passed to that vm_map_ram call (cannot unmap partial) |
| 942 | */ |
| 943 | void vm_unmap_ram(const void *mem, unsigned int count) |
| 944 | { |
| 945 | unsigned long size = count << PAGE_SHIFT; |
| 946 | unsigned long addr = (unsigned long)mem; |
| 947 | |
| 948 | BUG_ON(!addr); |
| 949 | BUG_ON(addr < VMALLOC_START); |
| 950 | BUG_ON(addr > VMALLOC_END); |
| 951 | BUG_ON(addr & (PAGE_SIZE-1)); |
| 952 | |
| 953 | debug_check_no_locks_freed(mem, size); |
Nick Piggin | cd52858 | 2009-01-06 14:39:20 -0800 | [diff] [blame] | 954 | vmap_debug_free_range(addr, addr+size); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 955 | |
| 956 | if (likely(count <= VMAP_MAX_ALLOC)) |
| 957 | vb_free(mem, size); |
| 958 | else |
| 959 | free_unmap_vmap_area_addr(addr); |
| 960 | } |
| 961 | EXPORT_SYMBOL(vm_unmap_ram); |
| 962 | |
| 963 | /** |
| 964 | * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) |
| 965 | * @pages: an array of pointers to the pages to be mapped |
| 966 | * @count: number of pages |
| 967 | * @node: prefer to allocate data structures on this node |
| 968 | * @prot: memory protection to use. PAGE_KERNEL for regular RAM |
Randy Dunlap | e99c97a | 2008-10-29 14:01:09 -0700 | [diff] [blame] | 969 | * |
| 970 | * Returns: a pointer to the address that has been mapped, or %NULL on failure |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 971 | */ |
| 972 | void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) |
| 973 | { |
| 974 | unsigned long size = count << PAGE_SHIFT; |
| 975 | unsigned long addr; |
| 976 | void *mem; |
| 977 | |
| 978 | if (likely(count <= VMAP_MAX_ALLOC)) { |
| 979 | mem = vb_alloc(size, GFP_KERNEL); |
| 980 | if (IS_ERR(mem)) |
| 981 | return NULL; |
| 982 | addr = (unsigned long)mem; |
| 983 | } else { |
| 984 | struct vmap_area *va; |
| 985 | va = alloc_vmap_area(size, PAGE_SIZE, |
| 986 | VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); |
| 987 | if (IS_ERR(va)) |
| 988 | return NULL; |
| 989 | |
| 990 | addr = va->va_start; |
| 991 | mem = (void *)addr; |
| 992 | } |
| 993 | if (vmap_page_range(addr, addr + size, prot, pages) < 0) { |
| 994 | vm_unmap_ram(mem, count); |
| 995 | return NULL; |
| 996 | } |
| 997 | return mem; |
| 998 | } |
| 999 | EXPORT_SYMBOL(vm_map_ram); |
| 1000 | |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1001 | /** |
| 1002 | * vm_area_register_early - register vmap area early during boot |
| 1003 | * @vm: vm_struct to register |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1004 | * @align: requested alignment |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1005 | * |
| 1006 | * This function is used to register kernel vm area before |
| 1007 | * vmalloc_init() is called. @vm->size and @vm->flags should contain |
| 1008 | * proper values on entry and other fields should be zero. On return, |
| 1009 | * vm->addr contains the allocated address. |
| 1010 | * |
| 1011 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. |
| 1012 | */ |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1013 | void __init vm_area_register_early(struct vm_struct *vm, size_t align) |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1014 | { |
| 1015 | static size_t vm_init_off __initdata; |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1016 | unsigned long addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1017 | |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1018 | addr = ALIGN(VMALLOC_START + vm_init_off, align); |
| 1019 | vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; |
| 1020 | |
| 1021 | vm->addr = (void *)addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1022 | |
| 1023 | vm->next = vmlist; |
| 1024 | vmlist = vm; |
| 1025 | } |
| 1026 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1027 | void __init vmalloc_init(void) |
| 1028 | { |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1029 | struct vmap_area *va; |
| 1030 | struct vm_struct *tmp; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1031 | int i; |
| 1032 | |
| 1033 | for_each_possible_cpu(i) { |
| 1034 | struct vmap_block_queue *vbq; |
| 1035 | |
| 1036 | vbq = &per_cpu(vmap_block_queue, i); |
| 1037 | spin_lock_init(&vbq->lock); |
| 1038 | INIT_LIST_HEAD(&vbq->free); |
| 1039 | INIT_LIST_HEAD(&vbq->dirty); |
| 1040 | vbq->nr_dirty = 0; |
| 1041 | } |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1042 | |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1043 | /* Import existing vmlist entries. */ |
| 1044 | for (tmp = vmlist; tmp; tmp = tmp->next) { |
Pekka Enberg | 43ebdac | 2009-05-25 15:01:35 +0300 | [diff] [blame] | 1045 | va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT); |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1046 | va->flags = tmp->flags | VM_VM_AREA; |
| 1047 | va->va_start = (unsigned long)tmp->addr; |
| 1048 | va->va_end = va->va_start + tmp->size; |
| 1049 | __insert_vmap_area(va); |
| 1050 | } |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1051 | |
| 1052 | vmap_area_pcpu_hole = VMALLOC_END; |
| 1053 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1054 | vmap_initialized = true; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1057 | /** |
| 1058 | * map_kernel_range_noflush - map kernel VM area with the specified pages |
| 1059 | * @addr: start of the VM area to map |
| 1060 | * @size: size of the VM area to map |
| 1061 | * @prot: page protection flags to use |
| 1062 | * @pages: pages to map |
| 1063 | * |
| 1064 | * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size |
| 1065 | * specify should have been allocated using get_vm_area() and its |
| 1066 | * friends. |
| 1067 | * |
| 1068 | * NOTE: |
| 1069 | * This function does NOT do any cache flushing. The caller is |
| 1070 | * responsible for calling flush_cache_vmap() on to-be-mapped areas |
| 1071 | * before calling this function. |
| 1072 | * |
| 1073 | * RETURNS: |
| 1074 | * The number of pages mapped on success, -errno on failure. |
| 1075 | */ |
| 1076 | int map_kernel_range_noflush(unsigned long addr, unsigned long size, |
| 1077 | pgprot_t prot, struct page **pages) |
| 1078 | { |
| 1079 | return vmap_page_range_noflush(addr, addr + size, prot, pages); |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * unmap_kernel_range_noflush - unmap kernel VM area |
| 1084 | * @addr: start of the VM area to unmap |
| 1085 | * @size: size of the VM area to unmap |
| 1086 | * |
| 1087 | * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size |
| 1088 | * specify should have been allocated using get_vm_area() and its |
| 1089 | * friends. |
| 1090 | * |
| 1091 | * NOTE: |
| 1092 | * This function does NOT do any cache flushing. The caller is |
| 1093 | * responsible for calling flush_cache_vunmap() on to-be-mapped areas |
| 1094 | * before calling this function and flush_tlb_kernel_range() after. |
| 1095 | */ |
| 1096 | void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) |
| 1097 | { |
| 1098 | vunmap_page_range(addr, addr + size); |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * unmap_kernel_range - unmap kernel VM area and flush cache and TLB |
| 1103 | * @addr: start of the VM area to unmap |
| 1104 | * @size: size of the VM area to unmap |
| 1105 | * |
| 1106 | * Similar to unmap_kernel_range_noflush() but flushes vcache before |
| 1107 | * the unmapping and tlb after. |
| 1108 | */ |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1109 | void unmap_kernel_range(unsigned long addr, unsigned long size) |
| 1110 | { |
| 1111 | unsigned long end = addr + size; |
Tejun Heo | f6fcba7 | 2009-02-20 15:38:48 -0800 | [diff] [blame] | 1112 | |
| 1113 | flush_cache_vunmap(addr, end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1114 | vunmap_page_range(addr, end); |
| 1115 | flush_tlb_kernel_range(addr, end); |
| 1116 | } |
| 1117 | |
| 1118 | int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages) |
| 1119 | { |
| 1120 | unsigned long addr = (unsigned long)area->addr; |
| 1121 | unsigned long end = addr + area->size - PAGE_SIZE; |
| 1122 | int err; |
| 1123 | |
| 1124 | err = vmap_page_range(addr, end, prot, *pages); |
| 1125 | if (err > 0) { |
| 1126 | *pages += err; |
| 1127 | err = 0; |
| 1128 | } |
| 1129 | |
| 1130 | return err; |
| 1131 | } |
| 1132 | EXPORT_SYMBOL_GPL(map_vm_area); |
| 1133 | |
| 1134 | /*** Old vmalloc interfaces ***/ |
| 1135 | DEFINE_RWLOCK(vmlist_lock); |
| 1136 | struct vm_struct *vmlist; |
| 1137 | |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1138 | static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, |
| 1139 | unsigned long flags, void *caller) |
| 1140 | { |
| 1141 | struct vm_struct *tmp, **p; |
| 1142 | |
| 1143 | vm->flags = flags; |
| 1144 | vm->addr = (void *)va->va_start; |
| 1145 | vm->size = va->va_end - va->va_start; |
| 1146 | vm->caller = caller; |
| 1147 | va->private = vm; |
| 1148 | va->flags |= VM_VM_AREA; |
| 1149 | |
| 1150 | write_lock(&vmlist_lock); |
| 1151 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { |
| 1152 | if (tmp->addr >= vm->addr) |
| 1153 | break; |
| 1154 | } |
| 1155 | vm->next = *p; |
| 1156 | *p = vm; |
| 1157 | write_unlock(&vmlist_lock); |
| 1158 | } |
| 1159 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1160 | static struct vm_struct *__get_vm_area_node(unsigned long size, |
| 1161 | unsigned long flags, unsigned long start, unsigned long end, |
| 1162 | int node, gfp_t gfp_mask, void *caller) |
| 1163 | { |
| 1164 | static struct vmap_area *va; |
| 1165 | struct vm_struct *area; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1166 | unsigned long align = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | |
Giridhar Pemmasani | 52fd24c | 2006-10-28 10:38:34 -0700 | [diff] [blame] | 1168 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (flags & VM_IOREMAP) { |
| 1170 | int bit = fls(size); |
| 1171 | |
| 1172 | if (bit > IOREMAP_MAX_ORDER) |
| 1173 | bit = IOREMAP_MAX_ORDER; |
| 1174 | else if (bit < PAGE_SHIFT) |
| 1175 | bit = PAGE_SHIFT; |
| 1176 | |
| 1177 | align = 1ul << bit; |
| 1178 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | size = PAGE_ALIGN(size); |
OGAWA Hirofumi | 31be830 | 2006-11-16 01:19:29 -0800 | [diff] [blame] | 1181 | if (unlikely(!size)) |
| 1182 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1184 | area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | if (unlikely(!area)) |
| 1186 | return NULL; |
| 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | /* |
| 1189 | * We always allocate a guard page. |
| 1190 | */ |
| 1191 | size += PAGE_SIZE; |
| 1192 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1193 | va = alloc_vmap_area(size, align, start, end, node, gfp_mask); |
| 1194 | if (IS_ERR(va)) { |
| 1195 | kfree(area); |
| 1196 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1199 | insert_vmalloc_vm(area, va, flags, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | return area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1203 | struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, |
| 1204 | unsigned long start, unsigned long end) |
| 1205 | { |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1206 | return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL, |
| 1207 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1208 | } |
Rusty Russell | 5992b6d | 2007-07-19 01:49:21 -0700 | [diff] [blame] | 1209 | EXPORT_SYMBOL_GPL(__get_vm_area); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1210 | |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 1211 | struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, |
| 1212 | unsigned long start, unsigned long end, |
| 1213 | void *caller) |
| 1214 | { |
| 1215 | return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL, |
| 1216 | caller); |
| 1217 | } |
| 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | /** |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1220 | * get_vm_area - reserve a contiguous kernel virtual area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * @size: size of the area |
| 1222 | * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC |
| 1223 | * |
| 1224 | * Search an area of @size in the kernel virtual mapping area, |
| 1225 | * and reserved it for out purposes. Returns the area descriptor |
| 1226 | * on success or %NULL on failure. |
| 1227 | */ |
| 1228 | struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) |
| 1229 | { |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1230 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, |
| 1231 | -1, GFP_KERNEL, __builtin_return_address(0)); |
| 1232 | } |
| 1233 | |
| 1234 | struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, |
| 1235 | void *caller) |
| 1236 | { |
| 1237 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, |
| 1238 | -1, GFP_KERNEL, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Giridhar Pemmasani | 52fd24c | 2006-10-28 10:38:34 -0700 | [diff] [blame] | 1241 | struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags, |
| 1242 | int node, gfp_t gfp_mask) |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1243 | { |
Giridhar Pemmasani | 52fd24c | 2006-10-28 10:38:34 -0700 | [diff] [blame] | 1244 | return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1245 | gfp_mask, __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1248 | static struct vm_struct *find_vm_area(const void *addr) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1249 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1250 | struct vmap_area *va; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1251 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1252 | va = find_vmap_area((unsigned long)addr); |
| 1253 | if (va && va->flags & VM_VM_AREA) |
| 1254 | return va->private; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1255 | |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 1256 | return NULL; |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | /** |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1260 | * remove_vm_area - find and remove a continuous kernel virtual area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | * @addr: base address |
| 1262 | * |
| 1263 | * Search for the kernel VM area starting at @addr, and remove it. |
| 1264 | * This function returns the found VM area, but using it is NOT safe |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 1265 | * on SMP machines, except for its size or flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1267 | struct vm_struct *remove_vm_area(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1269 | struct vmap_area *va; |
| 1270 | |
| 1271 | va = find_vmap_area((unsigned long)addr); |
| 1272 | if (va && va->flags & VM_VM_AREA) { |
| 1273 | struct vm_struct *vm = va->private; |
| 1274 | struct vm_struct *tmp, **p; |
Nick Piggin | cd52858 | 2009-01-06 14:39:20 -0800 | [diff] [blame] | 1275 | |
| 1276 | vmap_debug_free_range(va->va_start, va->va_end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1277 | free_unmap_vmap_area(va); |
| 1278 | vm->size -= PAGE_SIZE; |
| 1279 | |
| 1280 | write_lock(&vmlist_lock); |
| 1281 | for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next) |
| 1282 | ; |
| 1283 | *p = tmp->next; |
| 1284 | write_unlock(&vmlist_lock); |
| 1285 | |
| 1286 | return vm; |
| 1287 | } |
| 1288 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1291 | static void __vunmap(const void *addr, int deallocate_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | { |
| 1293 | struct vm_struct *area; |
| 1294 | |
| 1295 | if (!addr) |
| 1296 | return; |
| 1297 | |
| 1298 | if ((PAGE_SIZE-1) & (unsigned long)addr) { |
Arjan van de Ven | 4c8573e | 2008-07-25 19:45:37 -0700 | [diff] [blame] | 1299 | WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | return; |
| 1301 | } |
| 1302 | |
| 1303 | area = remove_vm_area(addr); |
| 1304 | if (unlikely(!area)) { |
Arjan van de Ven | 4c8573e | 2008-07-25 19:45:37 -0700 | [diff] [blame] | 1305 | WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | return; |
| 1308 | } |
| 1309 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1310 | debug_check_no_locks_freed(addr, area->size); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1311 | debug_check_no_obj_freed(addr, area->size); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (deallocate_pages) { |
| 1314 | int i; |
| 1315 | |
| 1316 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1317 | struct page *page = area->pages[i]; |
| 1318 | |
| 1319 | BUG_ON(!page); |
| 1320 | __free_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1323 | if (area->flags & VM_VPAGES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | vfree(area->pages); |
| 1325 | else |
| 1326 | kfree(area->pages); |
| 1327 | } |
| 1328 | |
| 1329 | kfree(area); |
| 1330 | return; |
| 1331 | } |
| 1332 | |
| 1333 | /** |
| 1334 | * vfree - release memory allocated by vmalloc() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | * @addr: memory base address |
| 1336 | * |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1337 | * Free the virtually continuous memory area starting at @addr, as |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1338 | * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is |
| 1339 | * NULL, no operation is performed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | * |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1341 | * Must not be called in interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1343 | void vfree(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | { |
| 1345 | BUG_ON(in_interrupt()); |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1346 | |
| 1347 | kmemleak_free(addr); |
| 1348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | __vunmap(addr, 1); |
| 1350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | EXPORT_SYMBOL(vfree); |
| 1352 | |
| 1353 | /** |
| 1354 | * vunmap - release virtual mapping obtained by vmap() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | * @addr: memory base address |
| 1356 | * |
| 1357 | * Free the virtually contiguous memory area starting at @addr, |
| 1358 | * which was created from the page array passed to vmap(). |
| 1359 | * |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1360 | * Must not be called in interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1362 | void vunmap(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | { |
| 1364 | BUG_ON(in_interrupt()); |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 1365 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | __vunmap(addr, 0); |
| 1367 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | EXPORT_SYMBOL(vunmap); |
| 1369 | |
| 1370 | /** |
| 1371 | * vmap - map an array of pages into virtually contiguous space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | * @pages: array of page pointers |
| 1373 | * @count: number of pages to map |
| 1374 | * @flags: vm_area->flags |
| 1375 | * @prot: page protection for the mapping |
| 1376 | * |
| 1377 | * Maps @count pages from @pages into contiguous kernel virtual |
| 1378 | * space. |
| 1379 | */ |
| 1380 | void *vmap(struct page **pages, unsigned int count, |
| 1381 | unsigned long flags, pgprot_t prot) |
| 1382 | { |
| 1383 | struct vm_struct *area; |
| 1384 | |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 1385 | might_sleep(); |
| 1386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | if (count > num_physpages) |
| 1388 | return NULL; |
| 1389 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1390 | area = get_vm_area_caller((count << PAGE_SHIFT), flags, |
| 1391 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | if (!area) |
| 1393 | return NULL; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | if (map_vm_area(area, prot, &pages)) { |
| 1396 | vunmap(area->addr); |
| 1397 | return NULL; |
| 1398 | } |
| 1399 | |
| 1400 | return area->addr; |
| 1401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | EXPORT_SYMBOL(vmap); |
| 1403 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1404 | static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot, |
| 1405 | int node, void *caller); |
Adrian Bunk | e31d9eb | 2008-02-04 22:29:09 -0800 | [diff] [blame] | 1406 | static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1407 | pgprot_t prot, int node, void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | { |
| 1409 | struct page **pages; |
| 1410 | unsigned int nr_pages, array_size, i; |
| 1411 | |
| 1412 | nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT; |
| 1413 | array_size = (nr_pages * sizeof(struct page *)); |
| 1414 | |
| 1415 | area->nr_pages = nr_pages; |
| 1416 | /* Please note that the recursion is strictly bounded. */ |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1417 | if (array_size > PAGE_SIZE) { |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 1418 | pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1419 | PAGE_KERNEL, node, caller); |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1420 | area->flags |= VM_VPAGES; |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 1421 | } else { |
| 1422 | pages = kmalloc_node(array_size, |
Christoph Lameter | 6cb0622 | 2007-10-16 01:25:41 -0700 | [diff] [blame] | 1423 | (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO, |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 1424 | node); |
| 1425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | area->pages = pages; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1427 | area->caller = caller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | if (!area->pages) { |
| 1429 | remove_vm_area(area->addr); |
| 1430 | kfree(area); |
| 1431 | return NULL; |
| 1432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1435 | struct page *page; |
| 1436 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1437 | if (node < 0) |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1438 | page = alloc_page(gfp_mask); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1439 | else |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1440 | page = alloc_pages_node(node, gfp_mask, 0); |
| 1441 | |
| 1442 | if (unlikely(!page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | /* Successfully allocated i pages, free them in __vunmap() */ |
| 1444 | area->nr_pages = i; |
| 1445 | goto fail; |
| 1446 | } |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1447 | area->pages[i] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | if (map_vm_area(area, prot, &pages)) |
| 1451 | goto fail; |
| 1452 | return area->addr; |
| 1453 | |
| 1454 | fail: |
| 1455 | vfree(area->addr); |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1459 | void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot) |
| 1460 | { |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1461 | void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1, |
| 1462 | __builtin_return_address(0)); |
| 1463 | |
| 1464 | /* |
| 1465 | * A ref_count = 3 is needed because the vm_struct and vmap_area |
| 1466 | * structures allocated in the __get_vm_area_node() function contain |
| 1467 | * references to the virtual address of the vmalloc'ed block. |
| 1468 | */ |
| 1469 | kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask); |
| 1470 | |
| 1471 | return addr; |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | /** |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1475 | * __vmalloc_node - allocate virtually contiguous memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | * @size: allocation size |
| 1477 | * @gfp_mask: flags for the page level allocator |
| 1478 | * @prot: protection mask for the allocated pages |
Randy Dunlap | d44e078 | 2005-11-07 01:01:10 -0800 | [diff] [blame] | 1479 | * @node: node to use for allocation or -1 |
Randy Dunlap | c85d194 | 2008-05-01 04:34:48 -0700 | [diff] [blame] | 1480 | * @caller: caller's return address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | * |
| 1482 | * Allocate enough pages to cover @size from the page level |
| 1483 | * allocator with @gfp_mask flags. Map them into contiguous |
| 1484 | * kernel virtual space, using a pagetable protection of @prot. |
| 1485 | */ |
Adrian Bunk | b221385 | 2006-09-25 23:31:02 -0700 | [diff] [blame] | 1486 | static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1487 | int node, void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | { |
| 1489 | struct vm_struct *area; |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1490 | void *addr; |
| 1491 | unsigned long real_size = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
| 1493 | size = PAGE_ALIGN(size); |
| 1494 | if (!size || (size >> PAGE_SHIFT) > num_physpages) |
| 1495 | return NULL; |
| 1496 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1497 | area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END, |
| 1498 | node, gfp_mask, caller); |
| 1499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | if (!area) |
| 1501 | return NULL; |
| 1502 | |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1503 | addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller); |
| 1504 | |
| 1505 | /* |
| 1506 | * A ref_count = 3 is needed because the vm_struct and vmap_area |
| 1507 | * structures allocated in the __get_vm_area_node() function contain |
| 1508 | * references to the virtual address of the vmalloc'ed block. |
| 1509 | */ |
| 1510 | kmemleak_alloc(addr, real_size, 3, gfp_mask); |
| 1511 | |
| 1512 | return addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1515 | void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) |
| 1516 | { |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1517 | return __vmalloc_node(size, gfp_mask, prot, -1, |
| 1518 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1519 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | EXPORT_SYMBOL(__vmalloc); |
| 1521 | |
| 1522 | /** |
| 1523 | * vmalloc - allocate virtually contiguous memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | * @size: allocation size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | * Allocate enough pages to cover @size from the page level |
| 1526 | * allocator and map them into contiguous kernel virtual space. |
| 1527 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1528 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | * use __vmalloc() instead. |
| 1530 | */ |
| 1531 | void *vmalloc(unsigned long size) |
| 1532 | { |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1533 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, |
| 1534 | -1, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | EXPORT_SYMBOL(vmalloc); |
| 1537 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1538 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1539 | * vmalloc_user - allocate zeroed virtually contiguous memory for userspace |
| 1540 | * @size: allocation size |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1541 | * |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1542 | * The resulting memory area is zeroed so it can be mapped to userspace |
| 1543 | * without leaking data. |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1544 | */ |
| 1545 | void *vmalloc_user(unsigned long size) |
| 1546 | { |
| 1547 | struct vm_struct *area; |
| 1548 | void *ret; |
| 1549 | |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1550 | ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
| 1551 | PAGE_KERNEL, -1, __builtin_return_address(0)); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1552 | if (ret) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1553 | area = find_vm_area(ret); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1554 | area->flags |= VM_USERMAP; |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1555 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1556 | return ret; |
| 1557 | } |
| 1558 | EXPORT_SYMBOL(vmalloc_user); |
| 1559 | |
| 1560 | /** |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1561 | * vmalloc_node - allocate memory on a specific node |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1562 | * @size: allocation size |
Randy Dunlap | d44e078 | 2005-11-07 01:01:10 -0800 | [diff] [blame] | 1563 | * @node: numa node |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1564 | * |
| 1565 | * Allocate enough pages to cover @size from the page level |
| 1566 | * allocator and map them into contiguous kernel virtual space. |
| 1567 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1568 | * For tight control over page level allocator and protection flags |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1569 | * use __vmalloc() instead. |
| 1570 | */ |
| 1571 | void *vmalloc_node(unsigned long size, int node) |
| 1572 | { |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1573 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, |
| 1574 | node, __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1575 | } |
| 1576 | EXPORT_SYMBOL(vmalloc_node); |
| 1577 | |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 1578 | #ifndef PAGE_KERNEL_EXEC |
| 1579 | # define PAGE_KERNEL_EXEC PAGE_KERNEL |
| 1580 | #endif |
| 1581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | /** |
| 1583 | * vmalloc_exec - allocate virtually contiguous, executable memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | * @size: allocation size |
| 1585 | * |
| 1586 | * Kernel-internal function to allocate enough pages to cover @size |
| 1587 | * the page level allocator and map them into contiguous and |
| 1588 | * executable kernel virtual space. |
| 1589 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1590 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | * use __vmalloc() instead. |
| 1592 | */ |
| 1593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | void *vmalloc_exec(unsigned long size) |
| 1595 | { |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1596 | return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC, |
| 1597 | -1, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1600 | #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) |
Benjamin Herrenschmidt | 7ac674f | 2007-07-19 01:49:10 -0700 | [diff] [blame] | 1601 | #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1602 | #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) |
Benjamin Herrenschmidt | 7ac674f | 2007-07-19 01:49:10 -0700 | [diff] [blame] | 1603 | #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1604 | #else |
| 1605 | #define GFP_VMALLOC32 GFP_KERNEL |
| 1606 | #endif |
| 1607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | /** |
| 1609 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | * @size: allocation size |
| 1611 | * |
| 1612 | * Allocate enough 32bit PA addressable pages to cover @size from the |
| 1613 | * page level allocator and map them into contiguous kernel virtual space. |
| 1614 | */ |
| 1615 | void *vmalloc_32(unsigned long size) |
| 1616 | { |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1617 | return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL, |
| 1618 | -1, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | EXPORT_SYMBOL(vmalloc_32); |
| 1621 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1622 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1623 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1624 | * @size: allocation size |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1625 | * |
| 1626 | * The resulting memory area is 32bit addressable and zeroed so it can be |
| 1627 | * mapped to userspace without leaking data. |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1628 | */ |
| 1629 | void *vmalloc_32_user(unsigned long size) |
| 1630 | { |
| 1631 | struct vm_struct *area; |
| 1632 | void *ret; |
| 1633 | |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1634 | ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, |
| 1635 | -1, __builtin_return_address(0)); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1636 | if (ret) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1637 | area = find_vm_area(ret); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1638 | area->flags |= VM_USERMAP; |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1639 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1640 | return ret; |
| 1641 | } |
| 1642 | EXPORT_SYMBOL(vmalloc_32_user); |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | long vread(char *buf, char *addr, unsigned long count) |
| 1645 | { |
| 1646 | struct vm_struct *tmp; |
| 1647 | char *vaddr, *buf_start = buf; |
| 1648 | unsigned long n; |
| 1649 | |
| 1650 | /* Don't allow overflow */ |
| 1651 | if ((unsigned long) addr + count < count) |
| 1652 | count = -(unsigned long) addr; |
| 1653 | |
| 1654 | read_lock(&vmlist_lock); |
| 1655 | for (tmp = vmlist; tmp; tmp = tmp->next) { |
| 1656 | vaddr = (char *) tmp->addr; |
| 1657 | if (addr >= vaddr + tmp->size - PAGE_SIZE) |
| 1658 | continue; |
| 1659 | while (addr < vaddr) { |
| 1660 | if (count == 0) |
| 1661 | goto finished; |
| 1662 | *buf = '\0'; |
| 1663 | buf++; |
| 1664 | addr++; |
| 1665 | count--; |
| 1666 | } |
| 1667 | n = vaddr + tmp->size - PAGE_SIZE - addr; |
| 1668 | do { |
| 1669 | if (count == 0) |
| 1670 | goto finished; |
| 1671 | *buf = *addr; |
| 1672 | buf++; |
| 1673 | addr++; |
| 1674 | count--; |
| 1675 | } while (--n > 0); |
| 1676 | } |
| 1677 | finished: |
| 1678 | read_unlock(&vmlist_lock); |
| 1679 | return buf - buf_start; |
| 1680 | } |
| 1681 | |
| 1682 | long vwrite(char *buf, char *addr, unsigned long count) |
| 1683 | { |
| 1684 | struct vm_struct *tmp; |
| 1685 | char *vaddr, *buf_start = buf; |
| 1686 | unsigned long n; |
| 1687 | |
| 1688 | /* Don't allow overflow */ |
| 1689 | if ((unsigned long) addr + count < count) |
| 1690 | count = -(unsigned long) addr; |
| 1691 | |
| 1692 | read_lock(&vmlist_lock); |
| 1693 | for (tmp = vmlist; tmp; tmp = tmp->next) { |
| 1694 | vaddr = (char *) tmp->addr; |
| 1695 | if (addr >= vaddr + tmp->size - PAGE_SIZE) |
| 1696 | continue; |
| 1697 | while (addr < vaddr) { |
| 1698 | if (count == 0) |
| 1699 | goto finished; |
| 1700 | buf++; |
| 1701 | addr++; |
| 1702 | count--; |
| 1703 | } |
| 1704 | n = vaddr + tmp->size - PAGE_SIZE - addr; |
| 1705 | do { |
| 1706 | if (count == 0) |
| 1707 | goto finished; |
| 1708 | *addr = *buf; |
| 1709 | buf++; |
| 1710 | addr++; |
| 1711 | count--; |
| 1712 | } while (--n > 0); |
| 1713 | } |
| 1714 | finished: |
| 1715 | read_unlock(&vmlist_lock); |
| 1716 | return buf - buf_start; |
| 1717 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1718 | |
| 1719 | /** |
| 1720 | * remap_vmalloc_range - map vmalloc pages to userspace |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1721 | * @vma: vma to cover (map full range of vma) |
| 1722 | * @addr: vmalloc memory |
| 1723 | * @pgoff: number of pages into addr before first page to map |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 1724 | * |
| 1725 | * Returns: 0 for success, -Exxx on failure |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1726 | * |
| 1727 | * This function checks that addr is a valid vmalloc'ed area, and |
| 1728 | * that it is big enough to cover the vma. Will return failure if |
| 1729 | * that criteria isn't met. |
| 1730 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 1731 | * Similar to remap_pfn_range() (see mm/memory.c) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1732 | */ |
| 1733 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 1734 | unsigned long pgoff) |
| 1735 | { |
| 1736 | struct vm_struct *area; |
| 1737 | unsigned long uaddr = vma->vm_start; |
| 1738 | unsigned long usize = vma->vm_end - vma->vm_start; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1739 | |
| 1740 | if ((PAGE_SIZE-1) & (unsigned long)addr) |
| 1741 | return -EINVAL; |
| 1742 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1743 | area = find_vm_area(addr); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1744 | if (!area) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1745 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1746 | |
| 1747 | if (!(area->flags & VM_USERMAP)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1748 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1749 | |
| 1750 | if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1751 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1752 | |
| 1753 | addr += pgoff << PAGE_SHIFT; |
| 1754 | do { |
| 1755 | struct page *page = vmalloc_to_page(addr); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1756 | int ret; |
| 1757 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1758 | ret = vm_insert_page(vma, uaddr, page); |
| 1759 | if (ret) |
| 1760 | return ret; |
| 1761 | |
| 1762 | uaddr += PAGE_SIZE; |
| 1763 | addr += PAGE_SIZE; |
| 1764 | usize -= PAGE_SIZE; |
| 1765 | } while (usize > 0); |
| 1766 | |
| 1767 | /* Prevent "things" like memory migration? VM_flags need a cleanup... */ |
| 1768 | vma->vm_flags |= VM_RESERVED; |
| 1769 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1770 | return 0; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1771 | } |
| 1772 | EXPORT_SYMBOL(remap_vmalloc_range); |
| 1773 | |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 1774 | /* |
| 1775 | * Implement a stub for vmalloc_sync_all() if the architecture chose not to |
| 1776 | * have one. |
| 1777 | */ |
| 1778 | void __attribute__((weak)) vmalloc_sync_all(void) |
| 1779 | { |
| 1780 | } |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1781 | |
| 1782 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 1783 | static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data) |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1784 | { |
| 1785 | /* apply_to_page_range() does all the hard work. */ |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | /** |
| 1790 | * alloc_vm_area - allocate a range of kernel address space |
| 1791 | * @size: size of the area |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 1792 | * |
| 1793 | * Returns: NULL on failure, vm_struct on success |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1794 | * |
| 1795 | * This function reserves a range of kernel address space, and |
| 1796 | * allocates pagetables to map that range. No actual mappings |
| 1797 | * are created. If the kernel address space is not shared |
| 1798 | * between processes, it syncs the pagetable across all |
| 1799 | * processes. |
| 1800 | */ |
| 1801 | struct vm_struct *alloc_vm_area(size_t size) |
| 1802 | { |
| 1803 | struct vm_struct *area; |
| 1804 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1805 | area = get_vm_area_caller(size, VM_IOREMAP, |
| 1806 | __builtin_return_address(0)); |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 1807 | if (area == NULL) |
| 1808 | return NULL; |
| 1809 | |
| 1810 | /* |
| 1811 | * This ensures that page tables are constructed for this region |
| 1812 | * of kernel virtual address space and mapped into init_mm. |
| 1813 | */ |
| 1814 | if (apply_to_page_range(&init_mm, (unsigned long)area->addr, |
| 1815 | area->size, f, NULL)) { |
| 1816 | free_vm_area(area); |
| 1817 | return NULL; |
| 1818 | } |
| 1819 | |
| 1820 | /* Make sure the pagetables are constructed in process kernel |
| 1821 | mappings */ |
| 1822 | vmalloc_sync_all(); |
| 1823 | |
| 1824 | return area; |
| 1825 | } |
| 1826 | EXPORT_SYMBOL_GPL(alloc_vm_area); |
| 1827 | |
| 1828 | void free_vm_area(struct vm_struct *area) |
| 1829 | { |
| 1830 | struct vm_struct *ret; |
| 1831 | ret = remove_vm_area(area->addr); |
| 1832 | BUG_ON(ret != area); |
| 1833 | kfree(area); |
| 1834 | } |
| 1835 | EXPORT_SYMBOL_GPL(free_vm_area); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 1836 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1837 | static struct vmap_area *node_to_va(struct rb_node *n) |
| 1838 | { |
| 1839 | return n ? rb_entry(n, struct vmap_area, rb_node) : NULL; |
| 1840 | } |
| 1841 | |
| 1842 | /** |
| 1843 | * pvm_find_next_prev - find the next and prev vmap_area surrounding @end |
| 1844 | * @end: target address |
| 1845 | * @pnext: out arg for the next vmap_area |
| 1846 | * @pprev: out arg for the previous vmap_area |
| 1847 | * |
| 1848 | * Returns: %true if either or both of next and prev are found, |
| 1849 | * %false if no vmap_area exists |
| 1850 | * |
| 1851 | * Find vmap_areas end addresses of which enclose @end. ie. if not |
| 1852 | * NULL, *pnext->va_end > @end and *pprev->va_end <= @end. |
| 1853 | */ |
| 1854 | static bool pvm_find_next_prev(unsigned long end, |
| 1855 | struct vmap_area **pnext, |
| 1856 | struct vmap_area **pprev) |
| 1857 | { |
| 1858 | struct rb_node *n = vmap_area_root.rb_node; |
| 1859 | struct vmap_area *va = NULL; |
| 1860 | |
| 1861 | while (n) { |
| 1862 | va = rb_entry(n, struct vmap_area, rb_node); |
| 1863 | if (end < va->va_end) |
| 1864 | n = n->rb_left; |
| 1865 | else if (end > va->va_end) |
| 1866 | n = n->rb_right; |
| 1867 | else |
| 1868 | break; |
| 1869 | } |
| 1870 | |
| 1871 | if (!va) |
| 1872 | return false; |
| 1873 | |
| 1874 | if (va->va_end > end) { |
| 1875 | *pnext = va; |
| 1876 | *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); |
| 1877 | } else { |
| 1878 | *pprev = va; |
| 1879 | *pnext = node_to_va(rb_next(&(*pprev)->rb_node)); |
| 1880 | } |
| 1881 | return true; |
| 1882 | } |
| 1883 | |
| 1884 | /** |
| 1885 | * pvm_determine_end - find the highest aligned address between two vmap_areas |
| 1886 | * @pnext: in/out arg for the next vmap_area |
| 1887 | * @pprev: in/out arg for the previous vmap_area |
| 1888 | * @align: alignment |
| 1889 | * |
| 1890 | * Returns: determined end address |
| 1891 | * |
| 1892 | * Find the highest aligned address between *@pnext and *@pprev below |
| 1893 | * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned |
| 1894 | * down address is between the end addresses of the two vmap_areas. |
| 1895 | * |
| 1896 | * Please note that the address returned by this function may fall |
| 1897 | * inside *@pnext vmap_area. The caller is responsible for checking |
| 1898 | * that. |
| 1899 | */ |
| 1900 | static unsigned long pvm_determine_end(struct vmap_area **pnext, |
| 1901 | struct vmap_area **pprev, |
| 1902 | unsigned long align) |
| 1903 | { |
| 1904 | const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
| 1905 | unsigned long addr; |
| 1906 | |
| 1907 | if (*pnext) |
| 1908 | addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end); |
| 1909 | else |
| 1910 | addr = vmalloc_end; |
| 1911 | |
| 1912 | while (*pprev && (*pprev)->va_end > addr) { |
| 1913 | *pnext = *pprev; |
| 1914 | *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); |
| 1915 | } |
| 1916 | |
| 1917 | return addr; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator |
| 1922 | * @offsets: array containing offset of each area |
| 1923 | * @sizes: array containing size of each area |
| 1924 | * @nr_vms: the number of areas to allocate |
| 1925 | * @align: alignment, all entries in @offsets and @sizes must be aligned to this |
| 1926 | * @gfp_mask: allocation mask |
| 1927 | * |
| 1928 | * Returns: kmalloc'd vm_struct pointer array pointing to allocated |
| 1929 | * vm_structs on success, %NULL on failure |
| 1930 | * |
| 1931 | * Percpu allocator wants to use congruent vm areas so that it can |
| 1932 | * maintain the offsets among percpu areas. This function allocates |
| 1933 | * congruent vmalloc areas for it. These areas tend to be scattered |
| 1934 | * pretty far, distance between two areas easily going up to |
| 1935 | * gigabytes. To avoid interacting with regular vmallocs, these areas |
| 1936 | * are allocated from top. |
| 1937 | * |
| 1938 | * Despite its complicated look, this allocator is rather simple. It |
| 1939 | * does everything top-down and scans areas from the end looking for |
| 1940 | * matching slot. While scanning, if any of the areas overlaps with |
| 1941 | * existing vmap_area, the base address is pulled down to fit the |
| 1942 | * area. Scanning is repeated till all the areas fit and then all |
| 1943 | * necessary data structres are inserted and the result is returned. |
| 1944 | */ |
| 1945 | struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, |
| 1946 | const size_t *sizes, int nr_vms, |
| 1947 | size_t align, gfp_t gfp_mask) |
| 1948 | { |
| 1949 | const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); |
| 1950 | const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
| 1951 | struct vmap_area **vas, *prev, *next; |
| 1952 | struct vm_struct **vms; |
| 1953 | int area, area2, last_area, term_area; |
| 1954 | unsigned long base, start, end, last_end; |
| 1955 | bool purged = false; |
| 1956 | |
| 1957 | gfp_mask &= GFP_RECLAIM_MASK; |
| 1958 | |
| 1959 | /* verify parameters and allocate data structures */ |
| 1960 | BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align)); |
| 1961 | for (last_area = 0, area = 0; area < nr_vms; area++) { |
| 1962 | start = offsets[area]; |
| 1963 | end = start + sizes[area]; |
| 1964 | |
| 1965 | /* is everything aligned properly? */ |
| 1966 | BUG_ON(!IS_ALIGNED(offsets[area], align)); |
| 1967 | BUG_ON(!IS_ALIGNED(sizes[area], align)); |
| 1968 | |
| 1969 | /* detect the area with the highest address */ |
| 1970 | if (start > offsets[last_area]) |
| 1971 | last_area = area; |
| 1972 | |
| 1973 | for (area2 = 0; area2 < nr_vms; area2++) { |
| 1974 | unsigned long start2 = offsets[area2]; |
| 1975 | unsigned long end2 = start2 + sizes[area2]; |
| 1976 | |
| 1977 | if (area2 == area) |
| 1978 | continue; |
| 1979 | |
| 1980 | BUG_ON(start2 >= start && start2 < end); |
| 1981 | BUG_ON(end2 <= end && end2 > start); |
| 1982 | } |
| 1983 | } |
| 1984 | last_end = offsets[last_area] + sizes[last_area]; |
| 1985 | |
| 1986 | if (vmalloc_end - vmalloc_start < last_end) { |
| 1987 | WARN_ON(true); |
| 1988 | return NULL; |
| 1989 | } |
| 1990 | |
| 1991 | vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask); |
| 1992 | vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask); |
| 1993 | if (!vas || !vms) |
| 1994 | goto err_free; |
| 1995 | |
| 1996 | for (area = 0; area < nr_vms; area++) { |
| 1997 | vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask); |
| 1998 | vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask); |
| 1999 | if (!vas[area] || !vms[area]) |
| 2000 | goto err_free; |
| 2001 | } |
| 2002 | retry: |
| 2003 | spin_lock(&vmap_area_lock); |
| 2004 | |
| 2005 | /* start scanning - we scan from the top, begin with the last area */ |
| 2006 | area = term_area = last_area; |
| 2007 | start = offsets[area]; |
| 2008 | end = start + sizes[area]; |
| 2009 | |
| 2010 | if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) { |
| 2011 | base = vmalloc_end - last_end; |
| 2012 | goto found; |
| 2013 | } |
| 2014 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2015 | |
| 2016 | while (true) { |
| 2017 | BUG_ON(next && next->va_end <= base + end); |
| 2018 | BUG_ON(prev && prev->va_end > base + end); |
| 2019 | |
| 2020 | /* |
| 2021 | * base might have underflowed, add last_end before |
| 2022 | * comparing. |
| 2023 | */ |
| 2024 | if (base + last_end < vmalloc_start + last_end) { |
| 2025 | spin_unlock(&vmap_area_lock); |
| 2026 | if (!purged) { |
| 2027 | purge_vmap_area_lazy(); |
| 2028 | purged = true; |
| 2029 | goto retry; |
| 2030 | } |
| 2031 | goto err_free; |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * If next overlaps, move base downwards so that it's |
| 2036 | * right below next and then recheck. |
| 2037 | */ |
| 2038 | if (next && next->va_start < base + end) { |
| 2039 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2040 | term_area = area; |
| 2041 | continue; |
| 2042 | } |
| 2043 | |
| 2044 | /* |
| 2045 | * If prev overlaps, shift down next and prev and move |
| 2046 | * base so that it's right below new next and then |
| 2047 | * recheck. |
| 2048 | */ |
| 2049 | if (prev && prev->va_end > base + start) { |
| 2050 | next = prev; |
| 2051 | prev = node_to_va(rb_prev(&next->rb_node)); |
| 2052 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2053 | term_area = area; |
| 2054 | continue; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * This area fits, move on to the previous one. If |
| 2059 | * the previous one is the terminal one, we're done. |
| 2060 | */ |
| 2061 | area = (area + nr_vms - 1) % nr_vms; |
| 2062 | if (area == term_area) |
| 2063 | break; |
| 2064 | start = offsets[area]; |
| 2065 | end = start + sizes[area]; |
| 2066 | pvm_find_next_prev(base + end, &next, &prev); |
| 2067 | } |
| 2068 | found: |
| 2069 | /* we've found a fitting base, insert all va's */ |
| 2070 | for (area = 0; area < nr_vms; area++) { |
| 2071 | struct vmap_area *va = vas[area]; |
| 2072 | |
| 2073 | va->va_start = base + offsets[area]; |
| 2074 | va->va_end = va->va_start + sizes[area]; |
| 2075 | __insert_vmap_area(va); |
| 2076 | } |
| 2077 | |
| 2078 | vmap_area_pcpu_hole = base + offsets[last_area]; |
| 2079 | |
| 2080 | spin_unlock(&vmap_area_lock); |
| 2081 | |
| 2082 | /* insert all vm's */ |
| 2083 | for (area = 0; area < nr_vms; area++) |
| 2084 | insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC, |
| 2085 | pcpu_get_vm_areas); |
| 2086 | |
| 2087 | kfree(vas); |
| 2088 | return vms; |
| 2089 | |
| 2090 | err_free: |
| 2091 | for (area = 0; area < nr_vms; area++) { |
| 2092 | if (vas) |
| 2093 | kfree(vas[area]); |
| 2094 | if (vms) |
| 2095 | kfree(vms[area]); |
| 2096 | } |
| 2097 | kfree(vas); |
| 2098 | kfree(vms); |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | |
| 2102 | /** |
| 2103 | * pcpu_free_vm_areas - free vmalloc areas for percpu allocator |
| 2104 | * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() |
| 2105 | * @nr_vms: the number of allocated areas |
| 2106 | * |
| 2107 | * Free vm_structs and the array allocated by pcpu_get_vm_areas(). |
| 2108 | */ |
| 2109 | void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) |
| 2110 | { |
| 2111 | int i; |
| 2112 | |
| 2113 | for (i = 0; i < nr_vms; i++) |
| 2114 | free_vm_area(vms[i]); |
| 2115 | kfree(vms); |
| 2116 | } |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2117 | |
| 2118 | #ifdef CONFIG_PROC_FS |
| 2119 | static void *s_start(struct seq_file *m, loff_t *pos) |
| 2120 | { |
| 2121 | loff_t n = *pos; |
| 2122 | struct vm_struct *v; |
| 2123 | |
| 2124 | read_lock(&vmlist_lock); |
| 2125 | v = vmlist; |
| 2126 | while (n > 0 && v) { |
| 2127 | n--; |
| 2128 | v = v->next; |
| 2129 | } |
| 2130 | if (!n) |
| 2131 | return v; |
| 2132 | |
| 2133 | return NULL; |
| 2134 | |
| 2135 | } |
| 2136 | |
| 2137 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) |
| 2138 | { |
| 2139 | struct vm_struct *v = p; |
| 2140 | |
| 2141 | ++*pos; |
| 2142 | return v->next; |
| 2143 | } |
| 2144 | |
| 2145 | static void s_stop(struct seq_file *m, void *p) |
| 2146 | { |
| 2147 | read_unlock(&vmlist_lock); |
| 2148 | } |
| 2149 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 2150 | static void show_numa_info(struct seq_file *m, struct vm_struct *v) |
| 2151 | { |
| 2152 | if (NUMA_BUILD) { |
| 2153 | unsigned int nr, *counters = m->private; |
| 2154 | |
| 2155 | if (!counters) |
| 2156 | return; |
| 2157 | |
| 2158 | memset(counters, 0, nr_node_ids * sizeof(unsigned int)); |
| 2159 | |
| 2160 | for (nr = 0; nr < v->nr_pages; nr++) |
| 2161 | counters[page_to_nid(v->pages[nr])]++; |
| 2162 | |
| 2163 | for_each_node_state(nr, N_HIGH_MEMORY) |
| 2164 | if (counters[nr]) |
| 2165 | seq_printf(m, " N%u=%u", nr, counters[nr]); |
| 2166 | } |
| 2167 | } |
| 2168 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2169 | static int s_show(struct seq_file *m, void *p) |
| 2170 | { |
| 2171 | struct vm_struct *v = p; |
| 2172 | |
| 2173 | seq_printf(m, "0x%p-0x%p %7ld", |
| 2174 | v->addr, v->addr + v->size, v->size); |
| 2175 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2176 | if (v->caller) { |
Hugh Dickins | 9c24624 | 2008-12-09 13:14:27 -0800 | [diff] [blame] | 2177 | char buff[KSYM_SYMBOL_LEN]; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2178 | |
| 2179 | seq_putc(m, ' '); |
| 2180 | sprint_symbol(buff, (unsigned long)v->caller); |
| 2181 | seq_puts(m, buff); |
| 2182 | } |
| 2183 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2184 | if (v->nr_pages) |
| 2185 | seq_printf(m, " pages=%d", v->nr_pages); |
| 2186 | |
| 2187 | if (v->phys_addr) |
| 2188 | seq_printf(m, " phys=%lx", v->phys_addr); |
| 2189 | |
| 2190 | if (v->flags & VM_IOREMAP) |
| 2191 | seq_printf(m, " ioremap"); |
| 2192 | |
| 2193 | if (v->flags & VM_ALLOC) |
| 2194 | seq_printf(m, " vmalloc"); |
| 2195 | |
| 2196 | if (v->flags & VM_MAP) |
| 2197 | seq_printf(m, " vmap"); |
| 2198 | |
| 2199 | if (v->flags & VM_USERMAP) |
| 2200 | seq_printf(m, " user"); |
| 2201 | |
| 2202 | if (v->flags & VM_VPAGES) |
| 2203 | seq_printf(m, " vpages"); |
| 2204 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 2205 | show_numa_info(m, v); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2206 | seq_putc(m, '\n'); |
| 2207 | return 0; |
| 2208 | } |
| 2209 | |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2210 | static const struct seq_operations vmalloc_op = { |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2211 | .start = s_start, |
| 2212 | .next = s_next, |
| 2213 | .stop = s_stop, |
| 2214 | .show = s_show, |
| 2215 | }; |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2216 | |
| 2217 | static int vmalloc_open(struct inode *inode, struct file *file) |
| 2218 | { |
| 2219 | unsigned int *ptr = NULL; |
| 2220 | int ret; |
| 2221 | |
| 2222 | if (NUMA_BUILD) |
| 2223 | ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); |
| 2224 | ret = seq_open(file, &vmalloc_op); |
| 2225 | if (!ret) { |
| 2226 | struct seq_file *m = file->private_data; |
| 2227 | m->private = ptr; |
| 2228 | } else |
| 2229 | kfree(ptr); |
| 2230 | return ret; |
| 2231 | } |
| 2232 | |
| 2233 | static const struct file_operations proc_vmalloc_operations = { |
| 2234 | .open = vmalloc_open, |
| 2235 | .read = seq_read, |
| 2236 | .llseek = seq_lseek, |
| 2237 | .release = seq_release_private, |
| 2238 | }; |
| 2239 | |
| 2240 | static int __init proc_vmalloc_init(void) |
| 2241 | { |
| 2242 | proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations); |
| 2243 | return 0; |
| 2244 | } |
| 2245 | module_init(proc_vmalloc_init); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2246 | #endif |
| 2247 | |