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> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/interrupt.h> |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 19 | #include <linux/proc_fs.h> |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 21 | #include <linux/debugobjects.h> |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 22 | #include <linux/kallsyms.h> |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 23 | #include <linux/list.h> |
| 24 | #include <linux/rbtree.h> |
| 25 | #include <linux/radix-tree.h> |
| 26 | #include <linux/rcupdate.h> |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 27 | #include <linux/pfn.h> |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 28 | #include <linux/kmemleak.h> |
Arun Sharma | 6006349 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 29 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/tlbflush.h> |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 32 | #include <asm/shmparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Figo.zhang | bf88c8c | 2009-09-21 17:01:47 -0700 | [diff] [blame] | 171 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } while (pgd++, addr = next, addr != end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 173 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 174 | return nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 177 | static int vmap_page_range(unsigned long start, unsigned long end, |
| 178 | pgprot_t prot, struct page **pages) |
| 179 | { |
| 180 | int ret; |
| 181 | |
| 182 | ret = vmap_page_range_noflush(start, end, prot, pages); |
| 183 | flush_cache_vmap(start, end); |
| 184 | return ret; |
| 185 | } |
| 186 | |
Laura Abbott | d326348 | 2013-08-22 13:46:07 -0700 | [diff] [blame] | 187 | #ifdef ENABLE_VMALLOC_SAVING |
| 188 | int is_vmalloc_addr(const void *x) |
| 189 | { |
| 190 | struct rb_node *n; |
| 191 | struct vmap_area *va; |
| 192 | int ret = 0; |
| 193 | |
| 194 | spin_lock(&vmap_area_lock); |
| 195 | |
| 196 | for (n = rb_first(vmap_area_root); n; rb_next(n)) { |
| 197 | va = rb_entry(n, struct vmap_area, rb_node); |
| 198 | if (x >= va->va_start && x < va->va_end) { |
| 199 | ret = 1; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | spin_unlock(&vmap_area_lock); |
| 205 | return ret; |
| 206 | } |
| 207 | #else |
| 208 | int is_vmalloc_addr(const void *x) |
| 209 | { |
| 210 | unsigned long addr = (unsigned long)x; |
| 211 | |
| 212 | return addr >= VMALLOC_START && addr < VMALLOC_END; |
| 213 | } |
| 214 | #endif |
| 215 | EXPORT_SYMBOL(is_vmalloc_addr); |
| 216 | |
KAMEZAWA Hiroyuki | 81ac3ad | 2009-09-22 16:45:49 -0700 | [diff] [blame] | 217 | int is_vmalloc_or_module_addr(const void *x) |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 218 | { |
| 219 | /* |
Russell King | ab4f2ee | 2008-11-06 17:11:07 +0000 | [diff] [blame] | 220 | * ARM, x86-64 and sparc64 put modules in a special place, |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 221 | * and fall back on vmalloc() if that fails. Others |
| 222 | * just put it in the vmalloc space. |
| 223 | */ |
| 224 | #if defined(CONFIG_MODULES) && defined(MODULES_VADDR) |
| 225 | unsigned long addr = (unsigned long)x; |
| 226 | if (addr >= MODULES_VADDR && addr < MODULES_END) |
| 227 | return 1; |
| 228 | #endif |
| 229 | return is_vmalloc_addr(x); |
| 230 | } |
| 231 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 232 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 233 | * Walk a vmap address to the struct page it maps. |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 234 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 235 | struct page *vmalloc_to_page(const void *vmalloc_addr) |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 236 | { |
| 237 | unsigned long addr = (unsigned long) vmalloc_addr; |
| 238 | struct page *page = NULL; |
| 239 | pgd_t *pgd = pgd_offset_k(addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 240 | |
Ingo Molnar | 7aa413d | 2008-06-19 13:28:11 +0200 | [diff] [blame] | 241 | /* |
| 242 | * XXX we might need to change this if we add VIRTUAL_BUG_ON for |
| 243 | * architectures that do not vmalloc module space |
| 244 | */ |
Linus Torvalds | 73bdf0a | 2008-10-15 08:35:12 -0700 | [diff] [blame] | 245 | VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr)); |
Jiri Slaby | 59ea746 | 2008-06-12 13:56:40 +0200 | [diff] [blame] | 246 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 247 | if (!pgd_none(*pgd)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 248 | pud_t *pud = pud_offset(pgd, addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 249 | if (!pud_none(*pud)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 250 | pmd_t *pmd = pmd_offset(pud, addr); |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 251 | if (!pmd_none(*pmd)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 252 | pte_t *ptep, pte; |
| 253 | |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 254 | ptep = pte_offset_map(pmd, addr); |
| 255 | pte = *ptep; |
| 256 | if (pte_present(pte)) |
| 257 | page = pte_page(pte); |
| 258 | pte_unmap(ptep); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | return page; |
| 263 | } |
| 264 | EXPORT_SYMBOL(vmalloc_to_page); |
| 265 | |
| 266 | /* |
| 267 | * Map a vmalloc()-space virtual address to the physical page frame number. |
| 268 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 269 | unsigned long vmalloc_to_pfn(const void *vmalloc_addr) |
Christoph Lameter | 48667e7 | 2008-02-04 22:28:31 -0800 | [diff] [blame] | 270 | { |
| 271 | return page_to_pfn(vmalloc_to_page(vmalloc_addr)); |
| 272 | } |
| 273 | EXPORT_SYMBOL(vmalloc_to_pfn); |
| 274 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 275 | |
| 276 | /*** Global kva allocator ***/ |
| 277 | |
| 278 | #define VM_LAZY_FREE 0x01 |
| 279 | #define VM_LAZY_FREEING 0x02 |
| 280 | #define VM_VM_AREA 0x04 |
| 281 | |
| 282 | struct vmap_area { |
| 283 | unsigned long va_start; |
| 284 | unsigned long va_end; |
| 285 | unsigned long flags; |
| 286 | struct rb_node rb_node; /* address sorted rbtree */ |
| 287 | struct list_head list; /* address sorted list */ |
| 288 | struct list_head purge_list; /* "lazy purge" list */ |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 289 | struct vm_struct *vm; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 290 | struct rcu_head rcu_head; |
| 291 | }; |
| 292 | |
| 293 | static DEFINE_SPINLOCK(vmap_area_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 294 | static LIST_HEAD(vmap_area_list); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 295 | static struct rb_root vmap_area_root = RB_ROOT; |
| 296 | |
| 297 | /* The vmap cache globals are protected by vmap_area_lock */ |
| 298 | static struct rb_node *free_vmap_cache; |
| 299 | static unsigned long cached_hole_size; |
| 300 | static unsigned long cached_vstart; |
| 301 | static unsigned long cached_align; |
| 302 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 303 | static unsigned long vmap_area_pcpu_hole; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 304 | |
| 305 | static struct vmap_area *__find_vmap_area(unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 307 | struct rb_node *n = vmap_area_root.rb_node; |
| 308 | |
| 309 | while (n) { |
| 310 | struct vmap_area *va; |
| 311 | |
| 312 | va = rb_entry(n, struct vmap_area, rb_node); |
| 313 | if (addr < va->va_start) |
| 314 | n = n->rb_left; |
| 315 | else if (addr > va->va_start) |
| 316 | n = n->rb_right; |
| 317 | else |
| 318 | return va; |
| 319 | } |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | static void __insert_vmap_area(struct vmap_area *va) |
| 325 | { |
| 326 | struct rb_node **p = &vmap_area_root.rb_node; |
| 327 | struct rb_node *parent = NULL; |
| 328 | struct rb_node *tmp; |
| 329 | |
| 330 | while (*p) { |
Namhyung Kim | 170168d | 2010-10-26 14:22:02 -0700 | [diff] [blame] | 331 | struct vmap_area *tmp_va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 332 | |
| 333 | parent = *p; |
Namhyung Kim | 170168d | 2010-10-26 14:22:02 -0700 | [diff] [blame] | 334 | tmp_va = rb_entry(parent, struct vmap_area, rb_node); |
| 335 | if (va->va_start < tmp_va->va_end) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 336 | p = &(*p)->rb_left; |
Namhyung Kim | 170168d | 2010-10-26 14:22:02 -0700 | [diff] [blame] | 337 | else if (va->va_end > tmp_va->va_start) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 338 | p = &(*p)->rb_right; |
| 339 | else |
| 340 | BUG(); |
| 341 | } |
| 342 | |
| 343 | rb_link_node(&va->rb_node, parent, p); |
| 344 | rb_insert_color(&va->rb_node, &vmap_area_root); |
| 345 | |
| 346 | /* address-sort this list so it is usable like the vmlist */ |
| 347 | tmp = rb_prev(&va->rb_node); |
| 348 | if (tmp) { |
| 349 | struct vmap_area *prev; |
| 350 | prev = rb_entry(tmp, struct vmap_area, rb_node); |
| 351 | list_add_rcu(&va->list, &prev->list); |
| 352 | } else |
| 353 | list_add_rcu(&va->list, &vmap_area_list); |
| 354 | } |
| 355 | |
| 356 | static void purge_vmap_area_lazy(void); |
| 357 | |
| 358 | /* |
| 359 | * Allocate a region of KVA of the specified size and alignment, within the |
| 360 | * vstart and vend. |
| 361 | */ |
| 362 | static struct vmap_area *alloc_vmap_area(unsigned long size, |
| 363 | unsigned long align, |
| 364 | unsigned long vstart, unsigned long vend, |
| 365 | int node, gfp_t gfp_mask) |
| 366 | { |
| 367 | struct vmap_area *va; |
| 368 | struct rb_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | unsigned long addr; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 370 | int purged = 0; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 371 | struct vmap_area *first; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 372 | |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 373 | BUG_ON(!size); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 374 | BUG_ON(size & ~PAGE_MASK); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 375 | BUG_ON(!is_power_of_2(align)); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 376 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 377 | va = kmalloc_node(sizeof(struct vmap_area), |
| 378 | gfp_mask & GFP_RECLAIM_MASK, node); |
| 379 | if (unlikely(!va)) |
| 380 | return ERR_PTR(-ENOMEM); |
| 381 | |
| 382 | retry: |
| 383 | spin_lock(&vmap_area_lock); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 384 | /* |
| 385 | * Invalidate cache if we have more permissive parameters. |
| 386 | * cached_hole_size notes the largest hole noticed _below_ |
| 387 | * the vmap_area cached in free_vmap_cache: if size fits |
| 388 | * into that hole, we want to scan from vstart to reuse |
| 389 | * the hole instead of allocating above free_vmap_cache. |
| 390 | * Note that __free_vmap_area may update free_vmap_cache |
| 391 | * without updating cached_hole_size or cached_align. |
| 392 | */ |
| 393 | if (!free_vmap_cache || |
| 394 | size < cached_hole_size || |
| 395 | vstart < cached_vstart || |
| 396 | align < cached_align) { |
| 397 | nocache: |
| 398 | cached_hole_size = 0; |
| 399 | free_vmap_cache = NULL; |
| 400 | } |
| 401 | /* record if we encounter less permissive parameters */ |
| 402 | cached_vstart = vstart; |
| 403 | cached_align = align; |
Nick Piggin | 7766970 | 2009-02-27 14:03:03 -0800 | [diff] [blame] | 404 | |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 405 | /* find starting point for our search */ |
| 406 | if (free_vmap_cache) { |
| 407 | first = rb_entry(free_vmap_cache, struct vmap_area, rb_node); |
Johannes Weiner | 248ac0e | 2011-05-24 17:11:43 -0700 | [diff] [blame] | 408 | addr = ALIGN(first->va_end, align); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 409 | if (addr < vstart) |
| 410 | goto nocache; |
| 411 | if (addr + size - 1 < addr) |
| 412 | goto overflow; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 413 | |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 414 | } else { |
| 415 | addr = ALIGN(vstart, align); |
| 416 | if (addr + size - 1 < addr) |
| 417 | goto overflow; |
| 418 | |
| 419 | n = vmap_area_root.rb_node; |
| 420 | first = NULL; |
| 421 | |
| 422 | while (n) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 423 | struct vmap_area *tmp; |
| 424 | tmp = rb_entry(n, struct vmap_area, rb_node); |
| 425 | if (tmp->va_end >= addr) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 426 | first = tmp; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 427 | if (tmp->va_start <= addr) |
| 428 | break; |
| 429 | n = n->rb_left; |
| 430 | } else |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 431 | n = n->rb_right; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 432 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 433 | |
| 434 | if (!first) |
| 435 | goto found; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 436 | } |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* from the starting point, walk areas until a suitable hole is found */ |
Johannes Weiner | 248ac0e | 2011-05-24 17:11:43 -0700 | [diff] [blame] | 439 | while (addr + size > first->va_start && addr + size <= vend) { |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 440 | if (addr + cached_hole_size < first->va_start) |
| 441 | cached_hole_size = first->va_start - addr; |
Johannes Weiner | 248ac0e | 2011-05-24 17:11:43 -0700 | [diff] [blame] | 442 | addr = ALIGN(first->va_end, align); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 443 | if (addr + size - 1 < addr) |
| 444 | goto overflow; |
| 445 | |
| 446 | n = rb_next(&first->rb_node); |
| 447 | if (n) |
| 448 | first = rb_entry(n, struct vmap_area, rb_node); |
| 449 | else |
| 450 | goto found; |
| 451 | } |
| 452 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 453 | found: |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 454 | if (addr + size > vend) |
| 455 | goto overflow; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 456 | |
| 457 | va->va_start = addr; |
| 458 | va->va_end = addr + size; |
| 459 | va->flags = 0; |
| 460 | __insert_vmap_area(va); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 461 | free_vmap_cache = &va->rb_node; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 462 | spin_unlock(&vmap_area_lock); |
| 463 | |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 464 | BUG_ON(va->va_start & (align-1)); |
| 465 | BUG_ON(va->va_start < vstart); |
| 466 | BUG_ON(va->va_end > vend); |
| 467 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 468 | return va; |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 469 | |
| 470 | overflow: |
| 471 | spin_unlock(&vmap_area_lock); |
| 472 | if (!purged) { |
| 473 | purge_vmap_area_lazy(); |
| 474 | purged = 1; |
| 475 | goto retry; |
| 476 | } |
| 477 | if (printk_ratelimit()) |
| 478 | printk(KERN_WARNING |
| 479 | "vmap allocation for size %lu failed: " |
| 480 | "use vmalloc=<size> to increase size.\n", size); |
| 481 | kfree(va); |
| 482 | return ERR_PTR(-EBUSY); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 485 | static void __free_vmap_area(struct vmap_area *va) |
| 486 | { |
| 487 | BUG_ON(RB_EMPTY_NODE(&va->rb_node)); |
Nick Piggin | 8969960 | 2011-03-22 16:30:36 -0700 | [diff] [blame] | 488 | |
| 489 | if (free_vmap_cache) { |
| 490 | if (va->va_end < cached_vstart) { |
| 491 | free_vmap_cache = NULL; |
| 492 | } else { |
| 493 | struct vmap_area *cache; |
| 494 | cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node); |
| 495 | if (va->va_start <= cache->va_start) { |
| 496 | free_vmap_cache = rb_prev(&va->rb_node); |
| 497 | /* |
| 498 | * We don't try to update cached_hole_size or |
| 499 | * cached_align, but it won't go very wrong. |
| 500 | */ |
| 501 | } |
| 502 | } |
| 503 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 504 | rb_erase(&va->rb_node, &vmap_area_root); |
| 505 | RB_CLEAR_NODE(&va->rb_node); |
| 506 | list_del_rcu(&va->list); |
| 507 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 508 | /* |
| 509 | * Track the highest possible candidate for pcpu area |
| 510 | * allocation. Areas outside of vmalloc area can be returned |
| 511 | * here too, consider only end addresses which fall inside |
| 512 | * vmalloc area proper. |
| 513 | */ |
| 514 | if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END) |
| 515 | vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end); |
| 516 | |
Lai Jiangshan | 14769de | 2011-03-18 12:12:19 +0800 | [diff] [blame] | 517 | kfree_rcu(va, rcu_head); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Free a region of KVA allocated by alloc_vmap_area |
| 522 | */ |
| 523 | static void free_vmap_area(struct vmap_area *va) |
| 524 | { |
| 525 | spin_lock(&vmap_area_lock); |
| 526 | __free_vmap_area(va); |
| 527 | spin_unlock(&vmap_area_lock); |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Clear the pagetable entries of a given vmap_area |
| 532 | */ |
| 533 | static void unmap_vmap_area(struct vmap_area *va) |
| 534 | { |
| 535 | vunmap_page_range(va->va_start, va->va_end); |
| 536 | } |
| 537 | |
Nick Piggin | cd52858 | 2009-01-06 14:39:20 -0800 | [diff] [blame] | 538 | static void vmap_debug_free_range(unsigned long start, unsigned long end) |
| 539 | { |
| 540 | /* |
| 541 | * Unmap page tables and force a TLB flush immediately if |
| 542 | * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free |
| 543 | * bugs similarly to those in linear kernel virtual address |
| 544 | * space after a page has been freed. |
| 545 | * |
| 546 | * All the lazy freeing logic is still retained, in order to |
| 547 | * minimise intrusiveness of this debugging feature. |
| 548 | * |
| 549 | * This is going to be *slow* (linear kernel virtual address |
| 550 | * debugging doesn't do a broadcast TLB flush so it is a lot |
| 551 | * faster). |
| 552 | */ |
| 553 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 554 | vunmap_page_range(start, end); |
| 555 | flush_tlb_kernel_range(start, end); |
| 556 | #endif |
| 557 | } |
| 558 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 559 | /* |
| 560 | * lazy_max_pages is the maximum amount of virtual address space we gather up |
| 561 | * before attempting to purge with a TLB flush. |
| 562 | * |
| 563 | * There is a tradeoff here: a larger number will cover more kernel page tables |
| 564 | * and take slightly longer to purge, but it will linearly reduce the number of |
| 565 | * global TLB flushes that must be performed. It would seem natural to scale |
| 566 | * this number up linearly with the number of CPUs (because vmapping activity |
| 567 | * could also scale linearly with the number of CPUs), however it is likely |
| 568 | * that in practice, workloads might be constrained in other ways that mean |
| 569 | * vmap activity will not scale linearly with CPUs. Also, I want to be |
| 570 | * conservative and not introduce a big latency on huge systems, so go with |
| 571 | * a less aggressive log scale. It will still be an improvement over the old |
| 572 | * code, and it will be simple to change the scale factor if we find that it |
| 573 | * becomes a problem on bigger systems. |
| 574 | */ |
| 575 | static unsigned long lazy_max_pages(void) |
| 576 | { |
| 577 | unsigned int log; |
| 578 | |
| 579 | log = fls(num_online_cpus()); |
| 580 | |
| 581 | return log * (32UL * 1024 * 1024 / PAGE_SIZE); |
| 582 | } |
| 583 | |
| 584 | static atomic_t vmap_lazy_nr = ATOMIC_INIT(0); |
| 585 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 586 | /* for per-CPU blocks */ |
| 587 | static void purge_fragmented_blocks_allcpus(void); |
| 588 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 589 | /* |
Cliff Wickman | 3ee48b6 | 2010-09-16 11:44:02 -0500 | [diff] [blame] | 590 | * called before a call to iounmap() if the caller wants vm_area_struct's |
| 591 | * immediately freed. |
| 592 | */ |
| 593 | void set_iounmap_nonlazy(void) |
| 594 | { |
| 595 | atomic_set(&vmap_lazy_nr, lazy_max_pages()+1); |
| 596 | } |
| 597 | |
| 598 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 599 | * Purges all lazily-freed vmap areas. |
| 600 | * |
| 601 | * If sync is 0 then don't purge if there is already a purge in progress. |
| 602 | * If force_flush is 1, then flush kernel TLBs between *start and *end even |
| 603 | * if we found no lazy vmap areas to unmap (callers can use this to optimise |
| 604 | * their own TLB flushing). |
| 605 | * Returns with *start = min(*start, lowest purged address) |
| 606 | * *end = max(*end, highest purged address) |
| 607 | */ |
| 608 | static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end, |
| 609 | int sync, int force_flush) |
| 610 | { |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 611 | static DEFINE_SPINLOCK(purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 612 | LIST_HEAD(valist); |
| 613 | struct vmap_area *va; |
Vegard Nossum | cbb7667 | 2009-02-27 14:03:04 -0800 | [diff] [blame] | 614 | struct vmap_area *n_va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 615 | int nr = 0; |
| 616 | |
| 617 | /* |
| 618 | * If sync is 0 but force_flush is 1, we'll go sync anyway but callers |
| 619 | * should not expect such behaviour. This just simplifies locking for |
| 620 | * the case that isn't actually used at the moment anyway. |
| 621 | */ |
| 622 | if (!sync && !force_flush) { |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 623 | if (!spin_trylock(&purge_lock)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 624 | return; |
| 625 | } else |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 626 | spin_lock(&purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 627 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 628 | if (sync) |
| 629 | purge_fragmented_blocks_allcpus(); |
| 630 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 631 | rcu_read_lock(); |
| 632 | list_for_each_entry_rcu(va, &vmap_area_list, list) { |
| 633 | if (va->flags & VM_LAZY_FREE) { |
| 634 | if (va->va_start < *start) |
| 635 | *start = va->va_start; |
| 636 | if (va->va_end > *end) |
| 637 | *end = va->va_end; |
| 638 | nr += (va->va_end - va->va_start) >> PAGE_SHIFT; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 639 | list_add_tail(&va->purge_list, &valist); |
| 640 | va->flags |= VM_LAZY_FREEING; |
| 641 | va->flags &= ~VM_LAZY_FREE; |
| 642 | } |
| 643 | } |
| 644 | rcu_read_unlock(); |
| 645 | |
Yongseok Koh | 88f5004 | 2010-01-19 17:33:49 +0900 | [diff] [blame] | 646 | if (nr) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 647 | atomic_sub(nr, &vmap_lazy_nr); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 648 | |
| 649 | if (nr || force_flush) |
| 650 | flush_tlb_kernel_range(*start, *end); |
| 651 | |
| 652 | if (nr) { |
| 653 | spin_lock(&vmap_area_lock); |
Vegard Nossum | cbb7667 | 2009-02-27 14:03:04 -0800 | [diff] [blame] | 654 | list_for_each_entry_safe(va, n_va, &valist, purge_list) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 655 | __free_vmap_area(va); |
| 656 | spin_unlock(&vmap_area_lock); |
| 657 | } |
Andrew Morton | 46666d8 | 2009-01-15 13:51:15 -0800 | [diff] [blame] | 658 | spin_unlock(&purge_lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 662 | * Kick off a purge of the outstanding lazy areas. Don't bother if somebody |
| 663 | * is already purging. |
| 664 | */ |
| 665 | static void try_purge_vmap_area_lazy(void) |
| 666 | { |
| 667 | unsigned long start = ULONG_MAX, end = 0; |
| 668 | |
| 669 | __purge_vmap_area_lazy(&start, &end, 0, 0); |
| 670 | } |
| 671 | |
| 672 | /* |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 673 | * Kick off a purge of the outstanding lazy areas. |
| 674 | */ |
| 675 | static void purge_vmap_area_lazy(void) |
| 676 | { |
| 677 | unsigned long start = ULONG_MAX, end = 0; |
| 678 | |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 679 | __purge_vmap_area_lazy(&start, &end, 1, 0); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 683 | * Free a vmap area, caller ensuring that the area has been unmapped |
| 684 | * and flush_cache_vunmap had been called for the correct range |
| 685 | * previously. |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 686 | */ |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 687 | static void free_vmap_area_noflush(struct vmap_area *va) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 688 | { |
| 689 | va->flags |= VM_LAZY_FREE; |
| 690 | atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr); |
| 691 | if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages())) |
Nick Piggin | 496850e | 2008-11-19 15:36:33 -0800 | [diff] [blame] | 692 | try_purge_vmap_area_lazy(); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 695 | /* |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 696 | * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been |
| 697 | * called for the correct range previously. |
| 698 | */ |
| 699 | static void free_unmap_vmap_area_noflush(struct vmap_area *va) |
| 700 | { |
| 701 | unmap_vmap_area(va); |
| 702 | free_vmap_area_noflush(va); |
| 703 | } |
| 704 | |
| 705 | /* |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 706 | * Free and unmap a vmap area |
| 707 | */ |
| 708 | static void free_unmap_vmap_area(struct vmap_area *va) |
| 709 | { |
| 710 | flush_cache_vunmap(va->va_start, va->va_end); |
| 711 | free_unmap_vmap_area_noflush(va); |
| 712 | } |
| 713 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 714 | static struct vmap_area *find_vmap_area(unsigned long addr) |
| 715 | { |
| 716 | struct vmap_area *va; |
| 717 | |
| 718 | spin_lock(&vmap_area_lock); |
| 719 | va = __find_vmap_area(addr); |
| 720 | spin_unlock(&vmap_area_lock); |
| 721 | |
| 722 | return va; |
| 723 | } |
| 724 | |
| 725 | static void free_unmap_vmap_area_addr(unsigned long addr) |
| 726 | { |
| 727 | struct vmap_area *va; |
| 728 | |
| 729 | va = find_vmap_area(addr); |
| 730 | BUG_ON(!va); |
| 731 | free_unmap_vmap_area(va); |
| 732 | } |
| 733 | |
| 734 | |
| 735 | /*** Per cpu kva allocator ***/ |
| 736 | |
| 737 | /* |
| 738 | * vmap space is limited especially on 32 bit architectures. Ensure there is |
| 739 | * room for at least 16 percpu vmap blocks per CPU. |
| 740 | */ |
| 741 | /* |
| 742 | * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able |
| 743 | * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess |
| 744 | * instead (we just need a rough idea) |
| 745 | */ |
| 746 | #if BITS_PER_LONG == 32 |
| 747 | #define VMALLOC_SPACE (128UL*1024*1024) |
| 748 | #else |
| 749 | #define VMALLOC_SPACE (128UL*1024*1024*1024) |
| 750 | #endif |
| 751 | |
| 752 | #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE) |
| 753 | #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */ |
| 754 | #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */ |
| 755 | #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2) |
| 756 | #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */ |
| 757 | #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */ |
Clemens Ladisch | f982f91 | 2011-06-21 22:09:50 +0200 | [diff] [blame] | 758 | #define VMAP_BBMAP_BITS \ |
| 759 | VMAP_MIN(VMAP_BBMAP_BITS_MAX, \ |
| 760 | VMAP_MAX(VMAP_BBMAP_BITS_MIN, \ |
| 761 | VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 762 | |
| 763 | #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE) |
| 764 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 765 | static bool vmap_initialized __read_mostly = false; |
| 766 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 767 | struct vmap_block_queue { |
| 768 | spinlock_t lock; |
| 769 | struct list_head free; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 770 | }; |
| 771 | |
| 772 | struct vmap_block { |
| 773 | spinlock_t lock; |
| 774 | struct vmap_area *va; |
| 775 | struct vmap_block_queue *vbq; |
| 776 | unsigned long free, dirty; |
| 777 | DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS); |
| 778 | DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS); |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 779 | struct list_head free_list; |
| 780 | struct rcu_head rcu_head; |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 781 | struct list_head purge; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
| 784 | /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */ |
| 785 | static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue); |
| 786 | |
| 787 | /* |
| 788 | * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block |
| 789 | * in the free path. Could get rid of this if we change the API to return a |
| 790 | * "cookie" from alloc, to be passed to free. But no big deal yet. |
| 791 | */ |
| 792 | static DEFINE_SPINLOCK(vmap_block_tree_lock); |
| 793 | static RADIX_TREE(vmap_block_tree, GFP_ATOMIC); |
| 794 | |
| 795 | /* |
| 796 | * We should probably have a fallback mechanism to allocate virtual memory |
| 797 | * out of partially filled vmap blocks. However vmap block sizing should be |
| 798 | * fairly reasonable according to the vmalloc size, so it shouldn't be a |
| 799 | * big problem. |
| 800 | */ |
| 801 | |
| 802 | static unsigned long addr_to_vb_idx(unsigned long addr) |
| 803 | { |
| 804 | addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1); |
| 805 | addr /= VMAP_BLOCK_SIZE; |
| 806 | return addr; |
| 807 | } |
| 808 | |
| 809 | static struct vmap_block *new_vmap_block(gfp_t gfp_mask) |
| 810 | { |
| 811 | struct vmap_block_queue *vbq; |
| 812 | struct vmap_block *vb; |
| 813 | struct vmap_area *va; |
| 814 | unsigned long vb_idx; |
| 815 | int node, err; |
| 816 | |
| 817 | node = numa_node_id(); |
| 818 | |
| 819 | vb = kmalloc_node(sizeof(struct vmap_block), |
| 820 | gfp_mask & GFP_RECLAIM_MASK, node); |
| 821 | if (unlikely(!vb)) |
| 822 | return ERR_PTR(-ENOMEM); |
| 823 | |
| 824 | va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE, |
| 825 | VMALLOC_START, VMALLOC_END, |
| 826 | node, gfp_mask); |
Tobias Klauser | ddf9c6d | 2011-01-13 15:46:15 -0800 | [diff] [blame] | 827 | if (IS_ERR(va)) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 828 | kfree(vb); |
Julia Lawall | e7d8634 | 2010-08-09 17:18:28 -0700 | [diff] [blame] | 829 | return ERR_CAST(va); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | err = radix_tree_preload(gfp_mask); |
| 833 | if (unlikely(err)) { |
| 834 | kfree(vb); |
| 835 | free_vmap_area(va); |
| 836 | return ERR_PTR(err); |
| 837 | } |
| 838 | |
| 839 | spin_lock_init(&vb->lock); |
| 840 | vb->va = va; |
| 841 | vb->free = VMAP_BBMAP_BITS; |
| 842 | vb->dirty = 0; |
| 843 | bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS); |
| 844 | bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS); |
| 845 | INIT_LIST_HEAD(&vb->free_list); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 846 | |
| 847 | vb_idx = addr_to_vb_idx(va->va_start); |
| 848 | spin_lock(&vmap_block_tree_lock); |
| 849 | err = radix_tree_insert(&vmap_block_tree, vb_idx, vb); |
| 850 | spin_unlock(&vmap_block_tree_lock); |
| 851 | BUG_ON(err); |
| 852 | radix_tree_preload_end(); |
| 853 | |
| 854 | vbq = &get_cpu_var(vmap_block_queue); |
| 855 | vb->vbq = vbq; |
| 856 | spin_lock(&vbq->lock); |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 857 | list_add_rcu(&vb->free_list, &vbq->free); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 858 | spin_unlock(&vbq->lock); |
Tejun Heo | 3f04ba8 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 859 | put_cpu_var(vmap_block_queue); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 860 | |
| 861 | return vb; |
| 862 | } |
| 863 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 864 | static void free_vmap_block(struct vmap_block *vb) |
| 865 | { |
| 866 | struct vmap_block *tmp; |
| 867 | unsigned long vb_idx; |
| 868 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 869 | vb_idx = addr_to_vb_idx(vb->va->va_start); |
| 870 | spin_lock(&vmap_block_tree_lock); |
| 871 | tmp = radix_tree_delete(&vmap_block_tree, vb_idx); |
| 872 | spin_unlock(&vmap_block_tree_lock); |
| 873 | BUG_ON(tmp != vb); |
| 874 | |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 875 | free_vmap_area_noflush(vb->va); |
Lai Jiangshan | 22a3c7d | 2011-03-18 12:13:08 +0800 | [diff] [blame] | 876 | kfree_rcu(vb, rcu_head); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 879 | static void purge_fragmented_blocks(int cpu) |
| 880 | { |
| 881 | LIST_HEAD(purge); |
| 882 | struct vmap_block *vb; |
| 883 | struct vmap_block *n_vb; |
| 884 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); |
| 885 | |
| 886 | rcu_read_lock(); |
| 887 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 888 | |
| 889 | if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS)) |
| 890 | continue; |
| 891 | |
| 892 | spin_lock(&vb->lock); |
| 893 | if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) { |
| 894 | vb->free = 0; /* prevent further allocs after releasing lock */ |
| 895 | vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */ |
| 896 | bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS); |
| 897 | bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS); |
| 898 | spin_lock(&vbq->lock); |
| 899 | list_del_rcu(&vb->free_list); |
| 900 | spin_unlock(&vbq->lock); |
| 901 | spin_unlock(&vb->lock); |
| 902 | list_add_tail(&vb->purge, &purge); |
| 903 | } else |
| 904 | spin_unlock(&vb->lock); |
| 905 | } |
| 906 | rcu_read_unlock(); |
| 907 | |
| 908 | list_for_each_entry_safe(vb, n_vb, &purge, purge) { |
| 909 | list_del(&vb->purge); |
| 910 | free_vmap_block(vb); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | static void purge_fragmented_blocks_thiscpu(void) |
| 915 | { |
| 916 | purge_fragmented_blocks(smp_processor_id()); |
| 917 | } |
| 918 | |
| 919 | static void purge_fragmented_blocks_allcpus(void) |
| 920 | { |
| 921 | int cpu; |
| 922 | |
| 923 | for_each_possible_cpu(cpu) |
| 924 | purge_fragmented_blocks(cpu); |
| 925 | } |
| 926 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 927 | static void *vb_alloc(unsigned long size, gfp_t gfp_mask) |
| 928 | { |
| 929 | struct vmap_block_queue *vbq; |
| 930 | struct vmap_block *vb; |
| 931 | unsigned long addr = 0; |
| 932 | unsigned int order; |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 933 | int purge = 0; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 934 | |
| 935 | BUG_ON(size & ~PAGE_MASK); |
| 936 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
| 937 | order = get_order(size); |
| 938 | |
| 939 | again: |
| 940 | rcu_read_lock(); |
| 941 | vbq = &get_cpu_var(vmap_block_queue); |
| 942 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 943 | int i; |
| 944 | |
| 945 | spin_lock(&vb->lock); |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 946 | if (vb->free < 1UL << order) |
| 947 | goto next; |
| 948 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 949 | i = bitmap_find_free_region(vb->alloc_map, |
| 950 | VMAP_BBMAP_BITS, order); |
| 951 | |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 952 | if (i < 0) { |
| 953 | if (vb->free + vb->dirty == VMAP_BBMAP_BITS) { |
| 954 | /* fragmented and no outstanding allocations */ |
| 955 | BUG_ON(vb->dirty != VMAP_BBMAP_BITS); |
| 956 | purge = 1; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 957 | } |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 958 | goto next; |
| 959 | } |
| 960 | addr = vb->va->va_start + (i << PAGE_SHIFT); |
| 961 | BUG_ON(addr_to_vb_idx(addr) != |
| 962 | addr_to_vb_idx(vb->va->va_start)); |
| 963 | vb->free -= 1UL << order; |
| 964 | if (vb->free == 0) { |
| 965 | spin_lock(&vbq->lock); |
| 966 | list_del_rcu(&vb->free_list); |
| 967 | spin_unlock(&vbq->lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 968 | } |
| 969 | spin_unlock(&vb->lock); |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 970 | break; |
| 971 | next: |
| 972 | spin_unlock(&vb->lock); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 973 | } |
Nick Piggin | 02b709d | 2010-02-01 22:25:57 +1100 | [diff] [blame] | 974 | |
| 975 | if (purge) |
| 976 | purge_fragmented_blocks_thiscpu(); |
| 977 | |
Tejun Heo | 3f04ba8 | 2009-10-29 22:34:12 +0900 | [diff] [blame] | 978 | put_cpu_var(vmap_block_queue); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 979 | rcu_read_unlock(); |
| 980 | |
| 981 | if (!addr) { |
| 982 | vb = new_vmap_block(gfp_mask); |
| 983 | if (IS_ERR(vb)) |
| 984 | return vb; |
| 985 | goto again; |
| 986 | } |
| 987 | |
| 988 | return (void *)addr; |
| 989 | } |
| 990 | |
| 991 | static void vb_free(const void *addr, unsigned long size) |
| 992 | { |
| 993 | unsigned long offset; |
| 994 | unsigned long vb_idx; |
| 995 | unsigned int order; |
| 996 | struct vmap_block *vb; |
| 997 | |
| 998 | BUG_ON(size & ~PAGE_MASK); |
| 999 | BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC); |
Nick Piggin | b29acbd | 2008-12-01 13:13:47 -0800 | [diff] [blame] | 1000 | |
| 1001 | flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size); |
| 1002 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1003 | order = get_order(size); |
| 1004 | |
| 1005 | offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1); |
| 1006 | |
| 1007 | vb_idx = addr_to_vb_idx((unsigned long)addr); |
| 1008 | rcu_read_lock(); |
| 1009 | vb = radix_tree_lookup(&vmap_block_tree, vb_idx); |
| 1010 | rcu_read_unlock(); |
| 1011 | BUG_ON(!vb); |
| 1012 | |
Jeremy Fitzhardinge | 64141da | 2010-12-02 14:31:18 -0800 | [diff] [blame] | 1013 | vunmap_page_range((unsigned long)addr, (unsigned long)addr + size); |
| 1014 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1015 | spin_lock(&vb->lock); |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 1016 | BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order)); |
MinChan Kim | d086817 | 2009-03-31 15:19:26 -0700 | [diff] [blame] | 1017 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1018 | vb->dirty += 1UL << order; |
| 1019 | if (vb->dirty == VMAP_BBMAP_BITS) { |
Nick Piggin | de56042 | 2010-02-01 22:24:18 +1100 | [diff] [blame] | 1020 | BUG_ON(vb->free); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1021 | spin_unlock(&vb->lock); |
| 1022 | free_vmap_block(vb); |
| 1023 | } else |
| 1024 | spin_unlock(&vb->lock); |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer |
| 1029 | * |
| 1030 | * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily |
| 1031 | * to amortize TLB flushing overheads. What this means is that any page you |
| 1032 | * have now, may, in a former life, have been mapped into kernel virtual |
| 1033 | * address by the vmap layer and so there might be some CPUs with TLB entries |
| 1034 | * still referencing that page (additional to the regular 1:1 kernel mapping). |
| 1035 | * |
| 1036 | * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can |
| 1037 | * be sure that none of the pages we have control over will have any aliases |
| 1038 | * from the vmap layer. |
| 1039 | */ |
| 1040 | void vm_unmap_aliases(void) |
| 1041 | { |
| 1042 | unsigned long start = ULONG_MAX, end = 0; |
| 1043 | int cpu; |
| 1044 | int flush = 0; |
| 1045 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1046 | if (unlikely(!vmap_initialized)) |
| 1047 | return; |
| 1048 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1049 | for_each_possible_cpu(cpu) { |
| 1050 | struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu); |
| 1051 | struct vmap_block *vb; |
| 1052 | |
| 1053 | rcu_read_lock(); |
| 1054 | list_for_each_entry_rcu(vb, &vbq->free, free_list) { |
| 1055 | int i; |
| 1056 | |
| 1057 | spin_lock(&vb->lock); |
| 1058 | i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS); |
| 1059 | while (i < VMAP_BBMAP_BITS) { |
| 1060 | unsigned long s, e; |
| 1061 | int j; |
| 1062 | j = find_next_zero_bit(vb->dirty_map, |
| 1063 | VMAP_BBMAP_BITS, i); |
| 1064 | |
| 1065 | s = vb->va->va_start + (i << PAGE_SHIFT); |
| 1066 | e = vb->va->va_start + (j << PAGE_SHIFT); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1067 | flush = 1; |
| 1068 | |
| 1069 | if (s < start) |
| 1070 | start = s; |
| 1071 | if (e > end) |
| 1072 | end = e; |
| 1073 | |
| 1074 | i = j; |
| 1075 | i = find_next_bit(vb->dirty_map, |
| 1076 | VMAP_BBMAP_BITS, i); |
| 1077 | } |
| 1078 | spin_unlock(&vb->lock); |
| 1079 | } |
| 1080 | rcu_read_unlock(); |
| 1081 | } |
| 1082 | |
| 1083 | __purge_vmap_area_lazy(&start, &end, 1, flush); |
| 1084 | } |
| 1085 | EXPORT_SYMBOL_GPL(vm_unmap_aliases); |
| 1086 | |
| 1087 | /** |
| 1088 | * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram |
| 1089 | * @mem: the pointer returned by vm_map_ram |
| 1090 | * @count: the count passed to that vm_map_ram call (cannot unmap partial) |
| 1091 | */ |
| 1092 | void vm_unmap_ram(const void *mem, unsigned int count) |
| 1093 | { |
| 1094 | unsigned long size = count << PAGE_SHIFT; |
| 1095 | unsigned long addr = (unsigned long)mem; |
| 1096 | |
| 1097 | BUG_ON(!addr); |
| 1098 | BUG_ON(addr < VMALLOC_START); |
| 1099 | BUG_ON(addr > VMALLOC_END); |
| 1100 | BUG_ON(addr & (PAGE_SIZE-1)); |
| 1101 | |
| 1102 | debug_check_no_locks_freed(mem, size); |
Nick Piggin | cd52858 | 2009-01-06 14:39:20 -0800 | [diff] [blame] | 1103 | vmap_debug_free_range(addr, addr+size); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1104 | |
| 1105 | if (likely(count <= VMAP_MAX_ALLOC)) |
| 1106 | vb_free(mem, size); |
| 1107 | else |
| 1108 | free_unmap_vmap_area_addr(addr); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(vm_unmap_ram); |
| 1111 | |
| 1112 | /** |
| 1113 | * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space) |
| 1114 | * @pages: an array of pointers to the pages to be mapped |
| 1115 | * @count: number of pages |
| 1116 | * @node: prefer to allocate data structures on this node |
| 1117 | * @prot: memory protection to use. PAGE_KERNEL for regular RAM |
Randy Dunlap | e99c97a | 2008-10-29 14:01:09 -0700 | [diff] [blame] | 1118 | * |
| 1119 | * 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] | 1120 | */ |
| 1121 | void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot) |
| 1122 | { |
| 1123 | unsigned long size = count << PAGE_SHIFT; |
| 1124 | unsigned long addr; |
| 1125 | void *mem; |
| 1126 | |
| 1127 | if (likely(count <= VMAP_MAX_ALLOC)) { |
| 1128 | mem = vb_alloc(size, GFP_KERNEL); |
| 1129 | if (IS_ERR(mem)) |
| 1130 | return NULL; |
| 1131 | addr = (unsigned long)mem; |
| 1132 | } else { |
| 1133 | struct vmap_area *va; |
| 1134 | va = alloc_vmap_area(size, PAGE_SIZE, |
| 1135 | VMALLOC_START, VMALLOC_END, node, GFP_KERNEL); |
| 1136 | if (IS_ERR(va)) |
| 1137 | return NULL; |
| 1138 | |
| 1139 | addr = va->va_start; |
| 1140 | mem = (void *)addr; |
| 1141 | } |
| 1142 | if (vmap_page_range(addr, addr + size, prot, pages) < 0) { |
| 1143 | vm_unmap_ram(mem, count); |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | return mem; |
| 1147 | } |
| 1148 | EXPORT_SYMBOL(vm_map_ram); |
Neeti Desai | c278c94 | 2013-06-10 17:14:21 -0700 | [diff] [blame] | 1149 | /** |
| 1150 | * vm_area_check_early - check if vmap area is already mapped |
| 1151 | * @vm: vm_struct to be checked |
| 1152 | * |
| 1153 | * This function is used to check if the vmap area has been |
| 1154 | * mapped already. @vm->addr, @vm->size and @vm->flags should |
| 1155 | * contain proper values. |
| 1156 | * |
| 1157 | */ |
| 1158 | int __init vm_area_check_early(struct vm_struct *vm) |
| 1159 | { |
| 1160 | struct vm_struct *tmp, **p; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1161 | |
Neeti Desai | c278c94 | 2013-06-10 17:14:21 -0700 | [diff] [blame] | 1162 | BUG_ON(vmap_initialized); |
| 1163 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { |
| 1164 | if (tmp->addr >= vm->addr) { |
| 1165 | if (tmp->addr < vm->addr + vm->size) |
| 1166 | return 1; |
| 1167 | } else { |
| 1168 | if (tmp->addr + tmp->size > vm->addr) |
| 1169 | return 1; |
| 1170 | } |
| 1171 | } |
| 1172 | return 0; |
| 1173 | } |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1174 | /** |
Nicolas Pitre | be9b733 | 2011-08-25 00:24:21 -0400 | [diff] [blame] | 1175 | * vm_area_add_early - add vmap area early during boot |
| 1176 | * @vm: vm_struct to add |
| 1177 | * |
| 1178 | * This function is used to add fixed kernel vm area to vmlist before |
| 1179 | * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags |
| 1180 | * should contain proper values and the other fields should be zero. |
| 1181 | * |
| 1182 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. |
| 1183 | */ |
| 1184 | void __init vm_area_add_early(struct vm_struct *vm) |
| 1185 | { |
| 1186 | struct vm_struct *tmp, **p; |
| 1187 | |
| 1188 | BUG_ON(vmap_initialized); |
| 1189 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { |
| 1190 | if (tmp->addr >= vm->addr) { |
| 1191 | BUG_ON(tmp->addr < vm->addr + vm->size); |
| 1192 | break; |
| 1193 | } else |
| 1194 | BUG_ON(tmp->addr + tmp->size > vm->addr); |
| 1195 | } |
| 1196 | vm->next = *p; |
| 1197 | *p = vm; |
| 1198 | } |
| 1199 | |
| 1200 | /** |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1201 | * vm_area_register_early - register vmap area early during boot |
| 1202 | * @vm: vm_struct to register |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1203 | * @align: requested alignment |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1204 | * |
| 1205 | * This function is used to register kernel vm area before |
| 1206 | * vmalloc_init() is called. @vm->size and @vm->flags should contain |
| 1207 | * proper values on entry and other fields should be zero. On return, |
| 1208 | * vm->addr contains the allocated address. |
| 1209 | * |
| 1210 | * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING. |
| 1211 | */ |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1212 | 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] | 1213 | { |
| 1214 | static size_t vm_init_off __initdata; |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1215 | unsigned long addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1216 | |
Tejun Heo | c0c0a29 | 2009-02-24 11:57:21 +0900 | [diff] [blame] | 1217 | addr = ALIGN(VMALLOC_START + vm_init_off, align); |
| 1218 | vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START; |
| 1219 | |
| 1220 | vm->addr = (void *)addr; |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1221 | |
Nicolas Pitre | be9b733 | 2011-08-25 00:24:21 -0400 | [diff] [blame] | 1222 | vm_area_add_early(vm); |
Tejun Heo | f0aa661 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1223 | } |
| 1224 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1225 | void __init vmalloc_init(void) |
| 1226 | { |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1227 | struct vmap_area *va; |
| 1228 | struct vm_struct *tmp; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1229 | int i; |
| 1230 | |
| 1231 | for_each_possible_cpu(i) { |
| 1232 | struct vmap_block_queue *vbq; |
| 1233 | |
| 1234 | vbq = &per_cpu(vmap_block_queue, i); |
| 1235 | spin_lock_init(&vbq->lock); |
| 1236 | INIT_LIST_HEAD(&vbq->free); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1237 | } |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1238 | |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1239 | /* Import existing vmlist entries. */ |
| 1240 | for (tmp = vmlist; tmp; tmp = tmp->next) { |
Pekka Enberg | 43ebdac | 2009-05-25 15:01:35 +0300 | [diff] [blame] | 1241 | va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT); |
KyongHo | fa00262 | 2012-05-29 15:06:49 -0700 | [diff] [blame] | 1242 | va->flags = VM_VM_AREA; |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1243 | va->va_start = (unsigned long)tmp->addr; |
| 1244 | va->va_end = va->va_start + tmp->size; |
KyongHo | fa00262 | 2012-05-29 15:06:49 -0700 | [diff] [blame] | 1245 | va->vm = tmp; |
Ivan Kokshaysky | 822c18f | 2009-01-15 13:50:48 -0800 | [diff] [blame] | 1246 | __insert_vmap_area(va); |
| 1247 | } |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1248 | |
| 1249 | vmap_area_pcpu_hole = VMALLOC_END; |
| 1250 | |
Jeremy Fitzhardinge | 9b46333 | 2008-10-28 19:22:34 +1100 | [diff] [blame] | 1251 | vmap_initialized = true; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1254 | /** |
| 1255 | * map_kernel_range_noflush - map kernel VM area with the specified pages |
| 1256 | * @addr: start of the VM area to map |
| 1257 | * @size: size of the VM area to map |
| 1258 | * @prot: page protection flags to use |
| 1259 | * @pages: pages to map |
| 1260 | * |
| 1261 | * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size |
| 1262 | * specify should have been allocated using get_vm_area() and its |
| 1263 | * friends. |
| 1264 | * |
| 1265 | * NOTE: |
| 1266 | * This function does NOT do any cache flushing. The caller is |
| 1267 | * responsible for calling flush_cache_vmap() on to-be-mapped areas |
| 1268 | * before calling this function. |
| 1269 | * |
| 1270 | * RETURNS: |
| 1271 | * The number of pages mapped on success, -errno on failure. |
| 1272 | */ |
| 1273 | int map_kernel_range_noflush(unsigned long addr, unsigned long size, |
| 1274 | pgprot_t prot, struct page **pages) |
| 1275 | { |
| 1276 | return vmap_page_range_noflush(addr, addr + size, prot, pages); |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * unmap_kernel_range_noflush - unmap kernel VM area |
| 1281 | * @addr: start of the VM area to unmap |
| 1282 | * @size: size of the VM area to unmap |
| 1283 | * |
| 1284 | * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size |
| 1285 | * specify should have been allocated using get_vm_area() and its |
| 1286 | * friends. |
| 1287 | * |
| 1288 | * NOTE: |
| 1289 | * This function does NOT do any cache flushing. The caller is |
| 1290 | * responsible for calling flush_cache_vunmap() on to-be-mapped areas |
| 1291 | * before calling this function and flush_tlb_kernel_range() after. |
| 1292 | */ |
| 1293 | void unmap_kernel_range_noflush(unsigned long addr, unsigned long size) |
| 1294 | { |
| 1295 | vunmap_page_range(addr, addr + size); |
| 1296 | } |
Huang Ying | 81e88fd | 2011-01-12 14:44:55 +0800 | [diff] [blame] | 1297 | EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush); |
Tejun Heo | 8fc4898 | 2009-02-20 16:29:08 +0900 | [diff] [blame] | 1298 | |
| 1299 | /** |
| 1300 | * unmap_kernel_range - unmap kernel VM area and flush cache and TLB |
| 1301 | * @addr: start of the VM area to unmap |
| 1302 | * @size: size of the VM area to unmap |
| 1303 | * |
| 1304 | * Similar to unmap_kernel_range_noflush() but flushes vcache before |
| 1305 | * the unmapping and tlb after. |
| 1306 | */ |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1307 | void unmap_kernel_range(unsigned long addr, unsigned long size) |
| 1308 | { |
| 1309 | unsigned long end = addr + size; |
Tejun Heo | f6fcba7 | 2009-02-20 15:38:48 -0800 | [diff] [blame] | 1310 | |
| 1311 | flush_cache_vunmap(addr, end); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1312 | vunmap_page_range(addr, end); |
| 1313 | flush_tlb_kernel_range(addr, end); |
| 1314 | } |
| 1315 | |
| 1316 | int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages) |
| 1317 | { |
| 1318 | unsigned long addr = (unsigned long)area->addr; |
| 1319 | unsigned long end = addr + area->size - PAGE_SIZE; |
| 1320 | int err; |
| 1321 | |
| 1322 | err = vmap_page_range(addr, end, prot, *pages); |
| 1323 | if (err > 0) { |
| 1324 | *pages += err; |
| 1325 | err = 0; |
| 1326 | } |
| 1327 | |
| 1328 | return err; |
| 1329 | } |
| 1330 | EXPORT_SYMBOL_GPL(map_vm_area); |
| 1331 | |
| 1332 | /*** Old vmalloc interfaces ***/ |
| 1333 | DEFINE_RWLOCK(vmlist_lock); |
| 1334 | struct vm_struct *vmlist; |
| 1335 | |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1336 | static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1337 | unsigned long flags, const void *caller) |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1338 | { |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1339 | vm->flags = flags; |
| 1340 | vm->addr = (void *)va->va_start; |
| 1341 | vm->size = va->va_end - va->va_start; |
| 1342 | vm->caller = caller; |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 1343 | va->vm = vm; |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1344 | va->flags |= VM_VM_AREA; |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1345 | } |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1346 | |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1347 | static void insert_vmalloc_vmlist(struct vm_struct *vm) |
| 1348 | { |
| 1349 | struct vm_struct *tmp, **p; |
| 1350 | |
| 1351 | vm->flags &= ~VM_UNLIST; |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1352 | write_lock(&vmlist_lock); |
| 1353 | for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) { |
| 1354 | if (tmp->addr >= vm->addr) |
| 1355 | break; |
| 1356 | } |
| 1357 | vm->next = *p; |
| 1358 | *p = vm; |
| 1359 | write_unlock(&vmlist_lock); |
| 1360 | } |
| 1361 | |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1362 | static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1363 | unsigned long flags, const void *caller) |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1364 | { |
| 1365 | setup_vmalloc_vm(vm, va, flags, caller); |
| 1366 | insert_vmalloc_vmlist(vm); |
| 1367 | } |
| 1368 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1369 | static struct vm_struct *__get_vm_area_node(unsigned long size, |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1370 | unsigned long align, unsigned long flags, unsigned long start, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1371 | unsigned long end, int node, gfp_t gfp_mask, const void *caller) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1372 | { |
Kautuk Consul | 0006526 | 2011-12-19 17:12:04 -0800 | [diff] [blame] | 1373 | struct vmap_area *va; |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1374 | struct vm_struct *area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Giridhar Pemmasani | 52fd24c | 2006-10-28 10:38:34 -0700 | [diff] [blame] | 1376 | BUG_ON(in_interrupt()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | if (flags & VM_IOREMAP) { |
| 1378 | int bit = fls(size); |
| 1379 | |
| 1380 | if (bit > IOREMAP_MAX_ORDER) |
| 1381 | bit = IOREMAP_MAX_ORDER; |
| 1382 | else if (bit < PAGE_SHIFT) |
| 1383 | bit = PAGE_SHIFT; |
| 1384 | |
| 1385 | align = 1ul << bit; |
| 1386 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | size = PAGE_ALIGN(size); |
OGAWA Hirofumi | 31be830 | 2006-11-16 01:19:29 -0800 | [diff] [blame] | 1389 | if (unlikely(!size)) |
| 1390 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
Tejun Heo | cf88c79 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 1392 | area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | if (unlikely(!area)) |
| 1394 | return NULL; |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * We always allocate a guard page. |
| 1398 | */ |
| 1399 | size += PAGE_SIZE; |
| 1400 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1401 | va = alloc_vmap_area(size, align, start, end, node, gfp_mask); |
| 1402 | if (IS_ERR(va)) { |
| 1403 | kfree(area); |
| 1404 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1407 | /* |
| 1408 | * When this function is called from __vmalloc_node_range, |
| 1409 | * we do not add vm_struct to vmlist here to avoid |
| 1410 | * accessing uninitialized members of vm_struct such as |
| 1411 | * pages and nr_pages fields. They will be set later. |
| 1412 | * To distinguish it from others, we use a VM_UNLIST flag. |
| 1413 | */ |
| 1414 | if (flags & VM_UNLIST) |
| 1415 | setup_vmalloc_vm(area, va, flags, caller); |
| 1416 | else |
| 1417 | insert_vmalloc_vm(area, va, flags, caller); |
| 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | return area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1422 | struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags, |
| 1423 | unsigned long start, unsigned long end) |
| 1424 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1425 | return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1426 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1427 | } |
Rusty Russell | 5992b6d | 2007-07-19 01:49:21 -0700 | [diff] [blame] | 1428 | EXPORT_SYMBOL_GPL(__get_vm_area); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1429 | |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 1430 | struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags, |
| 1431 | unsigned long start, unsigned long end, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1432 | const void *caller) |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 1433 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1434 | return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL, |
Benjamin Herrenschmidt | c296861 | 2009-02-18 14:48:12 -0800 | [diff] [blame] | 1435 | caller); |
| 1436 | } |
| 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | /** |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1439 | * get_vm_area - reserve a contiguous kernel virtual area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | * @size: size of the area |
| 1441 | * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC |
| 1442 | * |
| 1443 | * Search an area of @size in the kernel virtual mapping area, |
| 1444 | * and reserved it for out purposes. Returns the area descriptor |
| 1445 | * on success or %NULL on failure. |
| 1446 | */ |
| 1447 | struct vm_struct *get_vm_area(unsigned long size, unsigned long flags) |
| 1448 | { |
Neeti Desai | e6ccd03 | 2013-06-14 17:39:33 -0700 | [diff] [blame] | 1449 | #ifdef CONFIG_ENABLE_VMALLOC_SAVING |
| 1450 | return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END, |
| 1451 | -1, GFP_KERNEL, __builtin_return_address(0)); |
| 1452 | #else |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1453 | return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1454 | -1, GFP_KERNEL, __builtin_return_address(0)); |
Neeti Desai | e6ccd03 | 2013-06-14 17:39:33 -0700 | [diff] [blame] | 1455 | #endif |
| 1456 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1460 | const void *caller) |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1461 | { |
Neeti Desai | e6ccd03 | 2013-06-14 17:39:33 -0700 | [diff] [blame] | 1462 | #ifdef CONFIG_ENABLE_VMALLOC_SAVING |
| 1463 | return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1464 | -1, GFP_KERNEL, caller); |
Neeti Desai | e6ccd03 | 2013-06-14 17:39:33 -0700 | [diff] [blame] | 1465 | #else |
| 1466 | return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END, |
| 1467 | -1, GFP_KERNEL, __builtin_return_address(0)); |
| 1468 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Marek Szyprowski | cb4d7a6 | 2012-07-30 09:11:33 +0200 | [diff] [blame] | 1471 | /** |
| 1472 | * find_vm_area - find a continuous kernel virtual area |
| 1473 | * @addr: base address |
| 1474 | * |
| 1475 | * Search for the kernel VM area starting at @addr, and return it. |
| 1476 | * It is up to the caller to do all required locking to keep the returned |
| 1477 | * pointer valid. |
| 1478 | */ |
| 1479 | struct vm_struct *find_vm_area(const void *addr) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1480 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1481 | struct vmap_area *va; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1482 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1483 | va = find_vmap_area((unsigned long)addr); |
| 1484 | if (va && va->flags & VM_VM_AREA) |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 1485 | return va->vm; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1486 | |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 1487 | return NULL; |
Andi Kleen | 7856dfe | 2005-05-20 14:27:57 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | /** |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1491 | * remove_vm_area - find and remove a continuous kernel virtual area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | * @addr: base address |
| 1493 | * |
| 1494 | * Search for the kernel VM area starting at @addr, and remove it. |
| 1495 | * 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] | 1496 | * on SMP machines, except for its size or flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1498 | struct vm_struct *remove_vm_area(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1500 | struct vmap_area *va; |
| 1501 | |
| 1502 | va = find_vmap_area((unsigned long)addr); |
| 1503 | if (va && va->flags & VM_VM_AREA) { |
Minchan Kim | db1aeca | 2012-01-10 15:08:39 -0800 | [diff] [blame] | 1504 | struct vm_struct *vm = va->vm; |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1505 | |
| 1506 | if (!(vm->flags & VM_UNLIST)) { |
| 1507 | struct vm_struct *tmp, **p; |
| 1508 | /* |
| 1509 | * remove from list and disallow access to |
| 1510 | * this vm_struct before unmap. (address range |
| 1511 | * confliction is maintained by vmap.) |
| 1512 | */ |
| 1513 | write_lock(&vmlist_lock); |
| 1514 | for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next) |
| 1515 | ; |
| 1516 | *p = tmp->next; |
| 1517 | write_unlock(&vmlist_lock); |
| 1518 | } |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1519 | |
KAMEZAWA Hiroyuki | dd32c27 | 2009-09-21 17:02:32 -0700 | [diff] [blame] | 1520 | vmap_debug_free_range(va->va_start, va->va_end); |
| 1521 | free_unmap_vmap_area(va); |
| 1522 | vm->size -= PAGE_SIZE; |
| 1523 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1524 | return vm; |
| 1525 | } |
| 1526 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1529 | static void __vunmap(const void *addr, int deallocate_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
| 1531 | struct vm_struct *area; |
| 1532 | |
| 1533 | if (!addr) |
| 1534 | return; |
| 1535 | |
| 1536 | if ((PAGE_SIZE-1) & (unsigned long)addr) { |
Arjan van de Ven | 4c8573e | 2008-07-25 19:45:37 -0700 | [diff] [blame] | 1537 | WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | area = remove_vm_area(addr); |
| 1542 | if (unlikely(!area)) { |
Arjan van de Ven | 4c8573e | 2008-07-25 19:45:37 -0700 | [diff] [blame] | 1543 | WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | return; |
| 1546 | } |
| 1547 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1548 | debug_check_no_locks_freed(addr, area->size); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1549 | debug_check_no_obj_freed(addr, area->size); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 1550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | if (deallocate_pages) { |
| 1552 | int i; |
| 1553 | |
| 1554 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1555 | struct page *page = area->pages[i]; |
| 1556 | |
| 1557 | BUG_ON(!page); |
| 1558 | __free_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1561 | if (area->flags & VM_VPAGES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | vfree(area->pages); |
| 1563 | else |
| 1564 | kfree(area->pages); |
| 1565 | } |
| 1566 | |
| 1567 | kfree(area); |
| 1568 | return; |
| 1569 | } |
| 1570 | |
| 1571 | /** |
| 1572 | * vfree - release memory allocated by vmalloc() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | * @addr: memory base address |
| 1574 | * |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1575 | * Free the virtually continuous memory area starting at @addr, as |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1576 | * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is |
| 1577 | * NULL, no operation is performed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | * |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1579 | * Must not be called in interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1581 | void vfree(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | { |
| 1583 | BUG_ON(in_interrupt()); |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1584 | |
| 1585 | kmemleak_free(addr); |
| 1586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | __vunmap(addr, 1); |
| 1588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | EXPORT_SYMBOL(vfree); |
| 1590 | |
| 1591 | /** |
| 1592 | * vunmap - release virtual mapping obtained by vmap() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | * @addr: memory base address |
| 1594 | * |
| 1595 | * Free the virtually contiguous memory area starting at @addr, |
| 1596 | * which was created from the page array passed to vmap(). |
| 1597 | * |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 1598 | * Must not be called in interrupt context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | */ |
Christoph Lameter | b3bdda0 | 2008-02-04 22:28:32 -0800 | [diff] [blame] | 1600 | void vunmap(const void *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | { |
| 1602 | BUG_ON(in_interrupt()); |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 1603 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | __vunmap(addr, 0); |
| 1605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | EXPORT_SYMBOL(vunmap); |
| 1607 | |
| 1608 | /** |
| 1609 | * vmap - map an array of pages into virtually contiguous space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | * @pages: array of page pointers |
| 1611 | * @count: number of pages to map |
| 1612 | * @flags: vm_area->flags |
| 1613 | * @prot: page protection for the mapping |
| 1614 | * |
| 1615 | * Maps @count pages from @pages into contiguous kernel virtual |
| 1616 | * space. |
| 1617 | */ |
| 1618 | void *vmap(struct page **pages, unsigned int count, |
| 1619 | unsigned long flags, pgprot_t prot) |
| 1620 | { |
| 1621 | struct vm_struct *area; |
| 1622 | |
Peter Zijlstra | 34754b6 | 2009-02-25 16:04:03 +0100 | [diff] [blame] | 1623 | might_sleep(); |
| 1624 | |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 1625 | if (count > totalram_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | return NULL; |
| 1627 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1628 | area = get_vm_area_caller((count << PAGE_SHIFT), flags, |
| 1629 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | if (!area) |
| 1631 | return NULL; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | if (map_vm_area(area, prot, &pages)) { |
| 1634 | vunmap(area->addr); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | |
| 1638 | return area->addr; |
| 1639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | EXPORT_SYMBOL(vmap); |
| 1641 | |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1642 | static void *__vmalloc_node(unsigned long size, unsigned long align, |
| 1643 | gfp_t gfp_mask, pgprot_t prot, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1644 | int node, const void *caller); |
Adrian Bunk | e31d9eb | 2008-02-04 22:29:09 -0800 | [diff] [blame] | 1645 | static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1646 | pgprot_t prot, int node, const void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | { |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 1648 | const int order = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | struct page **pages; |
| 1650 | unsigned int nr_pages, array_size, i; |
Jan Beulich | 976d6df | 2009-12-14 17:58:39 -0800 | [diff] [blame] | 1651 | gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
| 1653 | nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT; |
| 1654 | array_size = (nr_pages * sizeof(struct page *)); |
| 1655 | |
| 1656 | area->nr_pages = nr_pages; |
| 1657 | /* Please note that the recursion is strictly bounded. */ |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1658 | if (array_size > PAGE_SIZE) { |
Jan Beulich | 976d6df | 2009-12-14 17:58:39 -0800 | [diff] [blame] | 1659 | pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1660 | PAGE_KERNEL, node, caller); |
Jan Kiszka | 8757d5f | 2006-07-14 00:23:56 -0700 | [diff] [blame] | 1661 | area->flags |= VM_VPAGES; |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 1662 | } else { |
Jan Beulich | 976d6df | 2009-12-14 17:58:39 -0800 | [diff] [blame] | 1663 | pages = kmalloc_node(array_size, nested_gfp, node); |
Andrew Morton | 286e1ea | 2006-10-17 00:09:57 -0700 | [diff] [blame] | 1664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | area->pages = pages; |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1666 | area->caller = caller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | if (!area->pages) { |
| 1668 | remove_vm_area(area->addr); |
| 1669 | kfree(area); |
| 1670 | return NULL; |
| 1671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
| 1673 | for (i = 0; i < area->nr_pages; i++) { |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1674 | struct page *page; |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 1675 | gfp_t tmp_mask = gfp_mask | __GFP_NOWARN; |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1676 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1677 | if (node < 0) |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 1678 | page = alloc_page(tmp_mask); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1679 | else |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 1680 | page = alloc_pages_node(node, tmp_mask, order); |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1681 | |
| 1682 | if (unlikely(!page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | /* Successfully allocated i pages, free them in __vunmap() */ |
| 1684 | area->nr_pages = i; |
| 1685 | goto fail; |
| 1686 | } |
Christoph Lameter | bf53d6f | 2008-02-04 22:28:34 -0800 | [diff] [blame] | 1687 | area->pages[i] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | if (map_vm_area(area, prot, &pages)) |
| 1691 | goto fail; |
| 1692 | return area->addr; |
| 1693 | |
| 1694 | fail: |
Joe Perches | 3ee9a4f | 2011-10-31 17:08:35 -0700 | [diff] [blame] | 1695 | warn_alloc_failed(gfp_mask, order, |
| 1696 | "vmalloc: allocation failure, allocated %ld of %ld bytes\n", |
Dave Hansen | 22943ab | 2011-05-24 17:12:18 -0700 | [diff] [blame] | 1697 | (area->nr_pages*PAGE_SIZE), area->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | vfree(area->addr); |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1702 | /** |
| 1703 | * __vmalloc_node_range - allocate virtually contiguous memory |
| 1704 | * @size: allocation size |
| 1705 | * @align: desired alignment |
| 1706 | * @start: vm area range start |
| 1707 | * @end: vm area range end |
| 1708 | * @gfp_mask: flags for the page level allocator |
| 1709 | * @prot: protection mask for the allocated pages |
| 1710 | * @node: node to use for allocation or -1 |
| 1711 | * @caller: caller's return address |
| 1712 | * |
| 1713 | * Allocate enough pages to cover @size from the page level |
| 1714 | * allocator with @gfp_mask flags. Map them into contiguous |
| 1715 | * kernel virtual space, using a pagetable protection of @prot. |
| 1716 | */ |
| 1717 | void *__vmalloc_node_range(unsigned long size, unsigned long align, |
| 1718 | unsigned long start, unsigned long end, gfp_t gfp_mask, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1719 | pgprot_t prot, int node, const void *caller) |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1720 | { |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1721 | struct vm_struct *area; |
| 1722 | void *addr; |
| 1723 | unsigned long real_size = size; |
Jack Cheung | 59f9f1c | 2011-11-29 16:52:49 -0800 | [diff] [blame] | 1724 | #ifdef CONFIG_FIX_MOVABLE_ZONE |
| 1725 | unsigned long total_pages = total_unmovable_pages; |
| 1726 | #else |
| 1727 | unsigned long total_pages = totalram_pages; |
| 1728 | #endif |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1729 | |
| 1730 | size = PAGE_ALIGN(size); |
Jack Cheung | 59f9f1c | 2011-11-29 16:52:49 -0800 | [diff] [blame] | 1731 | if (!size || (size >> PAGE_SHIFT) > total_pages) |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 1732 | goto fail; |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1733 | |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1734 | area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST, |
| 1735 | start, end, node, gfp_mask, caller); |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1736 | if (!area) |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 1737 | goto fail; |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1738 | |
| 1739 | addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller); |
Mel Gorman | 1368edf | 2011-12-08 14:34:30 -0800 | [diff] [blame] | 1740 | if (!addr) |
| 1741 | return NULL; |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1742 | |
| 1743 | /* |
Mitsuo Hayasaka | f5252e0 | 2011-10-31 17:08:13 -0700 | [diff] [blame] | 1744 | * In this function, newly allocated vm_struct is not added |
| 1745 | * to vmlist at __get_vm_area_node(). so, it is added here. |
| 1746 | */ |
| 1747 | insert_vmalloc_vmlist(area); |
| 1748 | |
| 1749 | /* |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1750 | * A ref_count = 3 is needed because the vm_struct and vmap_area |
| 1751 | * structures allocated in the __get_vm_area_node() function contain |
| 1752 | * references to the virtual address of the vmalloc'ed block. |
| 1753 | */ |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1754 | kmemleak_alloc(addr, real_size, 3, gfp_mask); |
Catalin Marinas | 89219d3 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 1755 | |
| 1756 | return addr; |
Joe Perches | de7d2b5 | 2011-10-31 17:08:48 -0700 | [diff] [blame] | 1757 | |
| 1758 | fail: |
| 1759 | warn_alloc_failed(gfp_mask, 0, |
| 1760 | "vmalloc: allocation failure: %lu bytes\n", |
| 1761 | real_size); |
| 1762 | return NULL; |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | /** |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1766 | * __vmalloc_node - allocate virtually contiguous memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | * @size: allocation size |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1768 | * @align: desired alignment |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | * @gfp_mask: flags for the page level allocator |
| 1770 | * @prot: protection mask for the allocated pages |
Randy Dunlap | d44e078 | 2005-11-07 01:01:10 -0800 | [diff] [blame] | 1771 | * @node: node to use for allocation or -1 |
Randy Dunlap | c85d194 | 2008-05-01 04:34:48 -0700 | [diff] [blame] | 1772 | * @caller: caller's return address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | * |
| 1774 | * Allocate enough pages to cover @size from the page level |
| 1775 | * allocator with @gfp_mask flags. Map them into contiguous |
| 1776 | * kernel virtual space, using a pagetable protection of @prot. |
| 1777 | */ |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1778 | static void *__vmalloc_node(unsigned long size, unsigned long align, |
| 1779 | gfp_t gfp_mask, pgprot_t prot, |
Marek Szyprowski | acf4e61 | 2012-04-13 12:32:09 +0200 | [diff] [blame] | 1780 | int node, const void *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | { |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 1782 | return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, |
| 1783 | gfp_mask, prot, node, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1786 | void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot) |
| 1787 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1788 | return __vmalloc_node(size, 1, gfp_mask, prot, -1, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1789 | __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | EXPORT_SYMBOL(__vmalloc); |
| 1792 | |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 1793 | static inline void *__vmalloc_node_flags(unsigned long size, |
| 1794 | int node, gfp_t flags) |
| 1795 | { |
| 1796 | return __vmalloc_node(size, 1, flags, PAGE_KERNEL, |
| 1797 | node, __builtin_return_address(0)); |
| 1798 | } |
| 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | /** |
| 1801 | * vmalloc - allocate virtually contiguous memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | * @size: allocation size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | * Allocate enough pages to cover @size from the page level |
| 1804 | * allocator and map them into contiguous kernel virtual space. |
| 1805 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1806 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | * use __vmalloc() instead. |
| 1808 | */ |
| 1809 | void *vmalloc(unsigned long size) |
| 1810 | { |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 1811 | return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | EXPORT_SYMBOL(vmalloc); |
| 1814 | |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1815 | /** |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 1816 | * vzalloc - allocate virtually contiguous memory with zero fill |
| 1817 | * @size: allocation size |
| 1818 | * Allocate enough pages to cover @size from the page level |
| 1819 | * allocator and map them into contiguous kernel virtual space. |
| 1820 | * The memory allocated is set to zero. |
| 1821 | * |
| 1822 | * For tight control over page level allocator and protection flags |
| 1823 | * use __vmalloc() instead. |
| 1824 | */ |
| 1825 | void *vzalloc(unsigned long size) |
| 1826 | { |
| 1827 | return __vmalloc_node_flags(size, -1, |
| 1828 | GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); |
| 1829 | } |
| 1830 | EXPORT_SYMBOL(vzalloc); |
| 1831 | |
| 1832 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1833 | * vmalloc_user - allocate zeroed virtually contiguous memory for userspace |
| 1834 | * @size: allocation size |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1835 | * |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1836 | * The resulting memory area is zeroed so it can be mapped to userspace |
| 1837 | * without leaking data. |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1838 | */ |
| 1839 | void *vmalloc_user(unsigned long size) |
| 1840 | { |
| 1841 | struct vm_struct *area; |
| 1842 | void *ret; |
| 1843 | |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1844 | ret = __vmalloc_node(size, SHMLBA, |
| 1845 | GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1846 | PAGE_KERNEL, -1, __builtin_return_address(0)); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1847 | if (ret) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1848 | area = find_vm_area(ret); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1849 | area->flags |= VM_USERMAP; |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1850 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1851 | return ret; |
| 1852 | } |
| 1853 | EXPORT_SYMBOL(vmalloc_user); |
| 1854 | |
| 1855 | /** |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1856 | * vmalloc_node - allocate memory on a specific node |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1857 | * @size: allocation size |
Randy Dunlap | d44e078 | 2005-11-07 01:01:10 -0800 | [diff] [blame] | 1858 | * @node: numa node |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1859 | * |
| 1860 | * Allocate enough pages to cover @size from the page level |
| 1861 | * allocator and map them into contiguous kernel virtual space. |
| 1862 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1863 | * For tight control over page level allocator and protection flags |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1864 | * use __vmalloc() instead. |
| 1865 | */ |
| 1866 | void *vmalloc_node(unsigned long size, int node) |
| 1867 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1868 | return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 1869 | node, __builtin_return_address(0)); |
Christoph Lameter | 930fc45 | 2005-10-29 18:15:41 -0700 | [diff] [blame] | 1870 | } |
| 1871 | EXPORT_SYMBOL(vmalloc_node); |
| 1872 | |
Dave Young | e1ca778 | 2010-10-26 14:22:06 -0700 | [diff] [blame] | 1873 | /** |
| 1874 | * vzalloc_node - allocate memory on a specific node with zero fill |
| 1875 | * @size: allocation size |
| 1876 | * @node: numa node |
| 1877 | * |
| 1878 | * Allocate enough pages to cover @size from the page level |
| 1879 | * allocator and map them into contiguous kernel virtual space. |
| 1880 | * The memory allocated is set to zero. |
| 1881 | * |
| 1882 | * For tight control over page level allocator and protection flags |
| 1883 | * use __vmalloc_node() instead. |
| 1884 | */ |
| 1885 | void *vzalloc_node(unsigned long size, int node) |
| 1886 | { |
| 1887 | return __vmalloc_node_flags(size, node, |
| 1888 | GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO); |
| 1889 | } |
| 1890 | EXPORT_SYMBOL(vzalloc_node); |
| 1891 | |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 1892 | #ifndef PAGE_KERNEL_EXEC |
| 1893 | # define PAGE_KERNEL_EXEC PAGE_KERNEL |
| 1894 | #endif |
| 1895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | /** |
| 1897 | * vmalloc_exec - allocate virtually contiguous, executable memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | * @size: allocation size |
| 1899 | * |
| 1900 | * Kernel-internal function to allocate enough pages to cover @size |
| 1901 | * the page level allocator and map them into contiguous and |
| 1902 | * executable kernel virtual space. |
| 1903 | * |
Michael Opdenacker | c1c8897 | 2006-10-03 23:21:02 +0200 | [diff] [blame] | 1904 | * For tight control over page level allocator and protection flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | * use __vmalloc() instead. |
| 1906 | */ |
| 1907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | void *vmalloc_exec(unsigned long size) |
| 1909 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1910 | return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC, |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1911 | -1, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } |
| 1913 | |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1914 | #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32) |
Benjamin Herrenschmidt | 7ac674f | 2007-07-19 01:49:10 -0700 | [diff] [blame] | 1915 | #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1916 | #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA) |
Benjamin Herrenschmidt | 7ac674f | 2007-07-19 01:49:10 -0700 | [diff] [blame] | 1917 | #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL |
Andi Kleen | 0d08e0d | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1918 | #else |
| 1919 | #define GFP_VMALLOC32 GFP_KERNEL |
| 1920 | #endif |
| 1921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | /** |
| 1923 | * vmalloc_32 - allocate virtually contiguous memory (32bit addressable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | * @size: allocation size |
| 1925 | * |
| 1926 | * Allocate enough 32bit PA addressable pages to cover @size from the |
| 1927 | * page level allocator and map them into contiguous kernel virtual space. |
| 1928 | */ |
| 1929 | void *vmalloc_32(unsigned long size) |
| 1930 | { |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1931 | return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL, |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1932 | -1, __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | EXPORT_SYMBOL(vmalloc_32); |
| 1935 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1936 | /** |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1937 | * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1938 | * @size: allocation size |
Rolf Eike Beer | ead0408 | 2006-09-27 01:50:13 -0700 | [diff] [blame] | 1939 | * |
| 1940 | * The resulting memory area is 32bit addressable and zeroed so it can be |
| 1941 | * mapped to userspace without leaking data. |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1942 | */ |
| 1943 | void *vmalloc_32_user(unsigned long size) |
| 1944 | { |
| 1945 | struct vm_struct *area; |
| 1946 | void *ret; |
| 1947 | |
David Miller | 2dca699 | 2009-09-21 12:22:34 -0700 | [diff] [blame] | 1948 | ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL, |
Glauber Costa | 8487784 | 2009-01-06 14:39:19 -0800 | [diff] [blame] | 1949 | -1, __builtin_return_address(0)); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1950 | if (ret) { |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 1951 | area = find_vm_area(ret); |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1952 | area->flags |= VM_USERMAP; |
Eric Dumazet | 2b4ac44 | 2006-11-10 12:27:48 -0800 | [diff] [blame] | 1953 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 1954 | return ret; |
| 1955 | } |
| 1956 | EXPORT_SYMBOL(vmalloc_32_user); |
| 1957 | |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 1958 | /* |
| 1959 | * small helper routine , copy contents to buf from addr. |
| 1960 | * If the page is not present, fill zero. |
| 1961 | */ |
| 1962 | |
| 1963 | static int aligned_vread(char *buf, char *addr, unsigned long count) |
| 1964 | { |
| 1965 | struct page *p; |
| 1966 | int copied = 0; |
| 1967 | |
| 1968 | while (count) { |
| 1969 | unsigned long offset, length; |
| 1970 | |
| 1971 | offset = (unsigned long)addr & ~PAGE_MASK; |
| 1972 | length = PAGE_SIZE - offset; |
| 1973 | if (length > count) |
| 1974 | length = count; |
| 1975 | p = vmalloc_to_page(addr); |
| 1976 | /* |
| 1977 | * To do safe access to this _mapped_ area, we need |
| 1978 | * lock. But adding lock here means that we need to add |
| 1979 | * overhead of vmalloc()/vfree() calles for this _debug_ |
| 1980 | * interface, rarely used. Instead of that, we'll use |
| 1981 | * kmap() and get small overhead in this access function. |
| 1982 | */ |
| 1983 | if (p) { |
| 1984 | /* |
| 1985 | * we can expect USER0 is not used (see vread/vwrite's |
| 1986 | * function description) |
| 1987 | */ |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 1988 | void *map = kmap_atomic(p); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 1989 | memcpy(buf, map + offset, length); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 1990 | kunmap_atomic(map); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 1991 | } else |
| 1992 | memset(buf, 0, length); |
| 1993 | |
| 1994 | addr += length; |
| 1995 | buf += length; |
| 1996 | copied += length; |
| 1997 | count -= length; |
| 1998 | } |
| 1999 | return copied; |
| 2000 | } |
| 2001 | |
| 2002 | static int aligned_vwrite(char *buf, char *addr, unsigned long count) |
| 2003 | { |
| 2004 | struct page *p; |
| 2005 | int copied = 0; |
| 2006 | |
| 2007 | while (count) { |
| 2008 | unsigned long offset, length; |
| 2009 | |
| 2010 | offset = (unsigned long)addr & ~PAGE_MASK; |
| 2011 | length = PAGE_SIZE - offset; |
| 2012 | if (length > count) |
| 2013 | length = count; |
| 2014 | p = vmalloc_to_page(addr); |
| 2015 | /* |
| 2016 | * To do safe access to this _mapped_ area, we need |
| 2017 | * lock. But adding lock here means that we need to add |
| 2018 | * overhead of vmalloc()/vfree() calles for this _debug_ |
| 2019 | * interface, rarely used. Instead of that, we'll use |
| 2020 | * kmap() and get small overhead in this access function. |
| 2021 | */ |
| 2022 | if (p) { |
| 2023 | /* |
| 2024 | * we can expect USER0 is not used (see vread/vwrite's |
| 2025 | * function description) |
| 2026 | */ |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2027 | void *map = kmap_atomic(p); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2028 | memcpy(map + offset, buf, length); |
Cong Wang | 9b04c5f | 2011-11-25 23:14:39 +0800 | [diff] [blame] | 2029 | kunmap_atomic(map); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2030 | } |
| 2031 | addr += length; |
| 2032 | buf += length; |
| 2033 | copied += length; |
| 2034 | count -= length; |
| 2035 | } |
| 2036 | return copied; |
| 2037 | } |
| 2038 | |
| 2039 | /** |
| 2040 | * vread() - read vmalloc area in a safe way. |
| 2041 | * @buf: buffer for reading data |
| 2042 | * @addr: vm address. |
| 2043 | * @count: number of bytes to be read. |
| 2044 | * |
| 2045 | * Returns # of bytes which addr and buf should be increased. |
| 2046 | * (same number to @count). Returns 0 if [addr...addr+count) doesn't |
| 2047 | * includes any intersect with alive vmalloc area. |
| 2048 | * |
| 2049 | * This function checks that addr is a valid vmalloc'ed area, and |
| 2050 | * copy data from that area to a given buffer. If the given memory range |
| 2051 | * of [addr...addr+count) includes some valid address, data is copied to |
| 2052 | * proper area of @buf. If there are memory holes, they'll be zero-filled. |
| 2053 | * IOREMAP area is treated as memory hole and no copy is done. |
| 2054 | * |
| 2055 | * If [addr...addr+count) doesn't includes any intersects with alive |
| 2056 | * vm_struct area, returns 0. |
| 2057 | * @buf should be kernel's buffer. Because this function uses KM_USER0, |
| 2058 | * the caller should guarantee KM_USER0 is not used. |
| 2059 | * |
| 2060 | * Note: In usual ops, vread() is never necessary because the caller |
| 2061 | * should know vmalloc() area is valid and can use memcpy(). |
| 2062 | * This is for routines which have to access vmalloc area without |
| 2063 | * any informaion, as /dev/kmem. |
| 2064 | * |
| 2065 | */ |
| 2066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | long vread(char *buf, char *addr, unsigned long count) |
| 2068 | { |
| 2069 | struct vm_struct *tmp; |
| 2070 | char *vaddr, *buf_start = buf; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2071 | unsigned long buflen = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | unsigned long n; |
| 2073 | |
| 2074 | /* Don't allow overflow */ |
| 2075 | if ((unsigned long) addr + count < count) |
| 2076 | count = -(unsigned long) addr; |
| 2077 | |
| 2078 | read_lock(&vmlist_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2079 | for (tmp = vmlist; count && tmp; tmp = tmp->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | vaddr = (char *) tmp->addr; |
| 2081 | if (addr >= vaddr + tmp->size - PAGE_SIZE) |
| 2082 | continue; |
| 2083 | while (addr < vaddr) { |
| 2084 | if (count == 0) |
| 2085 | goto finished; |
| 2086 | *buf = '\0'; |
| 2087 | buf++; |
| 2088 | addr++; |
| 2089 | count--; |
| 2090 | } |
| 2091 | n = vaddr + tmp->size - PAGE_SIZE - addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2092 | if (n > count) |
| 2093 | n = count; |
| 2094 | if (!(tmp->flags & VM_IOREMAP)) |
| 2095 | aligned_vread(buf, addr, n); |
| 2096 | else /* IOREMAP area is treated as memory hole */ |
| 2097 | memset(buf, 0, n); |
| 2098 | buf += n; |
| 2099 | addr += n; |
| 2100 | count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | } |
| 2102 | finished: |
| 2103 | read_unlock(&vmlist_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2104 | |
| 2105 | if (buf == buf_start) |
| 2106 | return 0; |
| 2107 | /* zero-fill memory holes */ |
| 2108 | if (buf != buf_start + buflen) |
| 2109 | memset(buf, 0, buflen - (buf - buf_start)); |
| 2110 | |
| 2111 | return buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2114 | /** |
| 2115 | * vwrite() - write vmalloc area in a safe way. |
| 2116 | * @buf: buffer for source data |
| 2117 | * @addr: vm address. |
| 2118 | * @count: number of bytes to be read. |
| 2119 | * |
| 2120 | * Returns # of bytes which addr and buf should be incresed. |
| 2121 | * (same number to @count). |
| 2122 | * If [addr...addr+count) doesn't includes any intersect with valid |
| 2123 | * vmalloc area, returns 0. |
| 2124 | * |
| 2125 | * This function checks that addr is a valid vmalloc'ed area, and |
| 2126 | * copy data from a buffer to the given addr. If specified range of |
| 2127 | * [addr...addr+count) includes some valid address, data is copied from |
| 2128 | * proper area of @buf. If there are memory holes, no copy to hole. |
| 2129 | * IOREMAP area is treated as memory hole and no copy is done. |
| 2130 | * |
| 2131 | * If [addr...addr+count) doesn't includes any intersects with alive |
| 2132 | * vm_struct area, returns 0. |
| 2133 | * @buf should be kernel's buffer. Because this function uses KM_USER0, |
| 2134 | * the caller should guarantee KM_USER0 is not used. |
| 2135 | * |
| 2136 | * Note: In usual ops, vwrite() is never necessary because the caller |
| 2137 | * should know vmalloc() area is valid and can use memcpy(). |
| 2138 | * This is for routines which have to access vmalloc area without |
| 2139 | * any informaion, as /dev/kmem. |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2140 | */ |
| 2141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | long vwrite(char *buf, char *addr, unsigned long count) |
| 2143 | { |
| 2144 | struct vm_struct *tmp; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2145 | char *vaddr; |
| 2146 | unsigned long n, buflen; |
| 2147 | int copied = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
| 2149 | /* Don't allow overflow */ |
| 2150 | if ((unsigned long) addr + count < count) |
| 2151 | count = -(unsigned long) addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2152 | buflen = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
| 2154 | read_lock(&vmlist_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2155 | for (tmp = vmlist; count && tmp; tmp = tmp->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | vaddr = (char *) tmp->addr; |
| 2157 | if (addr >= vaddr + tmp->size - PAGE_SIZE) |
| 2158 | continue; |
| 2159 | while (addr < vaddr) { |
| 2160 | if (count == 0) |
| 2161 | goto finished; |
| 2162 | buf++; |
| 2163 | addr++; |
| 2164 | count--; |
| 2165 | } |
| 2166 | n = vaddr + tmp->size - PAGE_SIZE - addr; |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2167 | if (n > count) |
| 2168 | n = count; |
| 2169 | if (!(tmp->flags & VM_IOREMAP)) { |
| 2170 | aligned_vwrite(buf, addr, n); |
| 2171 | copied++; |
| 2172 | } |
| 2173 | buf += n; |
| 2174 | addr += n; |
| 2175 | count -= n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | finished: |
| 2178 | read_unlock(&vmlist_lock); |
KAMEZAWA Hiroyuki | d0107eb | 2009-09-21 17:02:34 -0700 | [diff] [blame] | 2179 | if (!copied) |
| 2180 | return 0; |
| 2181 | return buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2183 | |
| 2184 | /** |
| 2185 | * remap_vmalloc_range - map vmalloc pages to userspace |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2186 | * @vma: vma to cover (map full range of vma) |
| 2187 | * @addr: vmalloc memory |
| 2188 | * @pgoff: number of pages into addr before first page to map |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 2189 | * |
| 2190 | * Returns: 0 for success, -Exxx on failure |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2191 | * |
| 2192 | * This function checks that addr is a valid vmalloc'ed area, and |
| 2193 | * that it is big enough to cover the vma. Will return failure if |
| 2194 | * that criteria isn't met. |
| 2195 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2196 | * Similar to remap_pfn_range() (see mm/memory.c) |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2197 | */ |
| 2198 | int remap_vmalloc_range(struct vm_area_struct *vma, void *addr, |
| 2199 | unsigned long pgoff) |
| 2200 | { |
| 2201 | struct vm_struct *area; |
| 2202 | unsigned long uaddr = vma->vm_start; |
| 2203 | unsigned long usize = vma->vm_end - vma->vm_start; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2204 | |
| 2205 | if ((PAGE_SIZE-1) & (unsigned long)addr) |
| 2206 | return -EINVAL; |
| 2207 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2208 | area = find_vm_area(addr); |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2209 | if (!area) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2210 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2211 | |
| 2212 | if (!(area->flags & VM_USERMAP)) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2213 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2214 | |
| 2215 | if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE) |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2216 | return -EINVAL; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2217 | |
| 2218 | addr += pgoff << PAGE_SHIFT; |
| 2219 | do { |
| 2220 | struct page *page = vmalloc_to_page(addr); |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2221 | int ret; |
| 2222 | |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2223 | ret = vm_insert_page(vma, uaddr, page); |
| 2224 | if (ret) |
| 2225 | return ret; |
| 2226 | |
| 2227 | uaddr += PAGE_SIZE; |
| 2228 | addr += PAGE_SIZE; |
| 2229 | usize -= PAGE_SIZE; |
| 2230 | } while (usize > 0); |
| 2231 | |
| 2232 | /* Prevent "things" like memory migration? VM_flags need a cleanup... */ |
| 2233 | vma->vm_flags |= VM_RESERVED; |
| 2234 | |
Nick Piggin | db64fe0 | 2008-10-18 20:27:03 -0700 | [diff] [blame] | 2235 | return 0; |
Nick Piggin | 8334231 | 2006-06-23 02:03:20 -0700 | [diff] [blame] | 2236 | } |
| 2237 | EXPORT_SYMBOL(remap_vmalloc_range); |
| 2238 | |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 2239 | /* |
| 2240 | * Implement a stub for vmalloc_sync_all() if the architecture chose not to |
| 2241 | * have one. |
| 2242 | */ |
| 2243 | void __attribute__((weak)) vmalloc_sync_all(void) |
| 2244 | { |
| 2245 | } |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2246 | |
| 2247 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame] | 2248 | 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] | 2249 | { |
David Vrabel | cd12909 | 2011-09-29 16:53:32 +0100 | [diff] [blame] | 2250 | pte_t ***p = data; |
| 2251 | |
| 2252 | if (p) { |
| 2253 | *(*p) = pte; |
| 2254 | (*p)++; |
| 2255 | } |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | /** |
| 2260 | * alloc_vm_area - allocate a range of kernel address space |
| 2261 | * @size: size of the area |
David Vrabel | cd12909 | 2011-09-29 16:53:32 +0100 | [diff] [blame] | 2262 | * @ptes: returns the PTEs for the address space |
Randy Dunlap | 7682486 | 2008-03-19 17:00:40 -0700 | [diff] [blame] | 2263 | * |
| 2264 | * Returns: NULL on failure, vm_struct on success |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2265 | * |
| 2266 | * This function reserves a range of kernel address space, and |
| 2267 | * allocates pagetables to map that range. No actual mappings |
David Vrabel | cd12909 | 2011-09-29 16:53:32 +0100 | [diff] [blame] | 2268 | * are created. |
| 2269 | * |
| 2270 | * If @ptes is non-NULL, pointers to the PTEs (in init_mm) |
| 2271 | * allocated for the VM area are returned. |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2272 | */ |
David Vrabel | cd12909 | 2011-09-29 16:53:32 +0100 | [diff] [blame] | 2273 | struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes) |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2274 | { |
| 2275 | struct vm_struct *area; |
| 2276 | |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2277 | area = get_vm_area_caller(size, VM_IOREMAP, |
| 2278 | __builtin_return_address(0)); |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2279 | if (area == NULL) |
| 2280 | return NULL; |
| 2281 | |
| 2282 | /* |
| 2283 | * This ensures that page tables are constructed for this region |
| 2284 | * of kernel virtual address space and mapped into init_mm. |
| 2285 | */ |
| 2286 | if (apply_to_page_range(&init_mm, (unsigned long)area->addr, |
David Vrabel | cd12909 | 2011-09-29 16:53:32 +0100 | [diff] [blame] | 2287 | size, f, ptes ? &ptes : NULL)) { |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2288 | free_vm_area(area); |
| 2289 | return NULL; |
| 2290 | } |
| 2291 | |
David Vrabel | d63c8a0 | 2011-09-14 16:22:02 -0700 | [diff] [blame] | 2292 | /* |
| 2293 | * If the allocated address space is passed to a hypercall |
| 2294 | * before being used then we cannot rely on a page fault to |
| 2295 | * trigger an update of the page tables. So sync all the page |
| 2296 | * tables here. |
| 2297 | */ |
| 2298 | vmalloc_sync_all(); |
| 2299 | |
Jeremy Fitzhardinge | 5f4352f | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 2300 | return area; |
| 2301 | } |
| 2302 | EXPORT_SYMBOL_GPL(alloc_vm_area); |
| 2303 | |
| 2304 | void free_vm_area(struct vm_struct *area) |
| 2305 | { |
| 2306 | struct vm_struct *ret; |
| 2307 | ret = remove_vm_area(area->addr); |
| 2308 | BUG_ON(ret != area); |
| 2309 | kfree(area); |
| 2310 | } |
| 2311 | EXPORT_SYMBOL_GPL(free_vm_area); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2312 | |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 2313 | #ifdef CONFIG_SMP |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2314 | static struct vmap_area *node_to_va(struct rb_node *n) |
| 2315 | { |
| 2316 | return n ? rb_entry(n, struct vmap_area, rb_node) : NULL; |
| 2317 | } |
| 2318 | |
| 2319 | /** |
| 2320 | * pvm_find_next_prev - find the next and prev vmap_area surrounding @end |
| 2321 | * @end: target address |
| 2322 | * @pnext: out arg for the next vmap_area |
| 2323 | * @pprev: out arg for the previous vmap_area |
| 2324 | * |
| 2325 | * Returns: %true if either or both of next and prev are found, |
| 2326 | * %false if no vmap_area exists |
| 2327 | * |
| 2328 | * Find vmap_areas end addresses of which enclose @end. ie. if not |
| 2329 | * NULL, *pnext->va_end > @end and *pprev->va_end <= @end. |
| 2330 | */ |
| 2331 | static bool pvm_find_next_prev(unsigned long end, |
| 2332 | struct vmap_area **pnext, |
| 2333 | struct vmap_area **pprev) |
| 2334 | { |
| 2335 | struct rb_node *n = vmap_area_root.rb_node; |
| 2336 | struct vmap_area *va = NULL; |
| 2337 | |
| 2338 | while (n) { |
| 2339 | va = rb_entry(n, struct vmap_area, rb_node); |
| 2340 | if (end < va->va_end) |
| 2341 | n = n->rb_left; |
| 2342 | else if (end > va->va_end) |
| 2343 | n = n->rb_right; |
| 2344 | else |
| 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | if (!va) |
| 2349 | return false; |
| 2350 | |
| 2351 | if (va->va_end > end) { |
| 2352 | *pnext = va; |
| 2353 | *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); |
| 2354 | } else { |
| 2355 | *pprev = va; |
| 2356 | *pnext = node_to_va(rb_next(&(*pprev)->rb_node)); |
| 2357 | } |
| 2358 | return true; |
| 2359 | } |
| 2360 | |
| 2361 | /** |
| 2362 | * pvm_determine_end - find the highest aligned address between two vmap_areas |
| 2363 | * @pnext: in/out arg for the next vmap_area |
| 2364 | * @pprev: in/out arg for the previous vmap_area |
| 2365 | * @align: alignment |
| 2366 | * |
| 2367 | * Returns: determined end address |
| 2368 | * |
| 2369 | * Find the highest aligned address between *@pnext and *@pprev below |
| 2370 | * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned |
| 2371 | * down address is between the end addresses of the two vmap_areas. |
| 2372 | * |
| 2373 | * Please note that the address returned by this function may fall |
| 2374 | * inside *@pnext vmap_area. The caller is responsible for checking |
| 2375 | * that. |
| 2376 | */ |
| 2377 | static unsigned long pvm_determine_end(struct vmap_area **pnext, |
| 2378 | struct vmap_area **pprev, |
| 2379 | unsigned long align) |
| 2380 | { |
| 2381 | const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
| 2382 | unsigned long addr; |
| 2383 | |
| 2384 | if (*pnext) |
| 2385 | addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end); |
| 2386 | else |
| 2387 | addr = vmalloc_end; |
| 2388 | |
| 2389 | while (*pprev && (*pprev)->va_end > addr) { |
| 2390 | *pnext = *pprev; |
| 2391 | *pprev = node_to_va(rb_prev(&(*pnext)->rb_node)); |
| 2392 | } |
| 2393 | |
| 2394 | return addr; |
| 2395 | } |
| 2396 | |
| 2397 | /** |
| 2398 | * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator |
| 2399 | * @offsets: array containing offset of each area |
| 2400 | * @sizes: array containing size of each area |
| 2401 | * @nr_vms: the number of areas to allocate |
| 2402 | * @align: alignment, all entries in @offsets and @sizes must be aligned to this |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2403 | * |
| 2404 | * Returns: kmalloc'd vm_struct pointer array pointing to allocated |
| 2405 | * vm_structs on success, %NULL on failure |
| 2406 | * |
| 2407 | * Percpu allocator wants to use congruent vm areas so that it can |
| 2408 | * maintain the offsets among percpu areas. This function allocates |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 2409 | * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to |
| 2410 | * be scattered pretty far, distance between two areas easily going up |
| 2411 | * to gigabytes. To avoid interacting with regular vmallocs, these |
| 2412 | * areas are allocated from top. |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2413 | * |
| 2414 | * Despite its complicated look, this allocator is rather simple. It |
| 2415 | * does everything top-down and scans areas from the end looking for |
| 2416 | * matching slot. While scanning, if any of the areas overlaps with |
| 2417 | * existing vmap_area, the base address is pulled down to fit the |
| 2418 | * area. Scanning is repeated till all the areas fit and then all |
| 2419 | * necessary data structres are inserted and the result is returned. |
| 2420 | */ |
| 2421 | struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, |
| 2422 | const size_t *sizes, int nr_vms, |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 2423 | size_t align) |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2424 | { |
| 2425 | const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align); |
| 2426 | const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1); |
| 2427 | struct vmap_area **vas, *prev, *next; |
| 2428 | struct vm_struct **vms; |
| 2429 | int area, area2, last_area, term_area; |
| 2430 | unsigned long base, start, end, last_end; |
| 2431 | bool purged = false; |
| 2432 | |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2433 | /* verify parameters and allocate data structures */ |
| 2434 | BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align)); |
| 2435 | for (last_area = 0, area = 0; area < nr_vms; area++) { |
| 2436 | start = offsets[area]; |
| 2437 | end = start + sizes[area]; |
| 2438 | |
| 2439 | /* is everything aligned properly? */ |
| 2440 | BUG_ON(!IS_ALIGNED(offsets[area], align)); |
| 2441 | BUG_ON(!IS_ALIGNED(sizes[area], align)); |
| 2442 | |
| 2443 | /* detect the area with the highest address */ |
| 2444 | if (start > offsets[last_area]) |
| 2445 | last_area = area; |
| 2446 | |
| 2447 | for (area2 = 0; area2 < nr_vms; area2++) { |
| 2448 | unsigned long start2 = offsets[area2]; |
| 2449 | unsigned long end2 = start2 + sizes[area2]; |
| 2450 | |
| 2451 | if (area2 == area) |
| 2452 | continue; |
| 2453 | |
| 2454 | BUG_ON(start2 >= start && start2 < end); |
| 2455 | BUG_ON(end2 <= end && end2 > start); |
| 2456 | } |
| 2457 | } |
| 2458 | last_end = offsets[last_area] + sizes[last_area]; |
| 2459 | |
| 2460 | if (vmalloc_end - vmalloc_start < last_end) { |
| 2461 | WARN_ON(true); |
| 2462 | return NULL; |
| 2463 | } |
| 2464 | |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 2465 | vms = kzalloc(sizeof(vms[0]) * nr_vms, GFP_KERNEL); |
| 2466 | vas = kzalloc(sizeof(vas[0]) * nr_vms, GFP_KERNEL); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2467 | if (!vas || !vms) |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 2468 | goto err_free2; |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2469 | |
| 2470 | for (area = 0; area < nr_vms; area++) { |
David Rientjes | ec3f64f | 2011-01-13 15:46:01 -0800 | [diff] [blame] | 2471 | vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL); |
| 2472 | vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2473 | if (!vas[area] || !vms[area]) |
| 2474 | goto err_free; |
| 2475 | } |
| 2476 | retry: |
| 2477 | spin_lock(&vmap_area_lock); |
| 2478 | |
| 2479 | /* start scanning - we scan from the top, begin with the last area */ |
| 2480 | area = term_area = last_area; |
| 2481 | start = offsets[area]; |
| 2482 | end = start + sizes[area]; |
| 2483 | |
| 2484 | if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) { |
| 2485 | base = vmalloc_end - last_end; |
| 2486 | goto found; |
| 2487 | } |
| 2488 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2489 | |
| 2490 | while (true) { |
| 2491 | BUG_ON(next && next->va_end <= base + end); |
| 2492 | BUG_ON(prev && prev->va_end > base + end); |
| 2493 | |
| 2494 | /* |
| 2495 | * base might have underflowed, add last_end before |
| 2496 | * comparing. |
| 2497 | */ |
| 2498 | if (base + last_end < vmalloc_start + last_end) { |
| 2499 | spin_unlock(&vmap_area_lock); |
| 2500 | if (!purged) { |
| 2501 | purge_vmap_area_lazy(); |
| 2502 | purged = true; |
| 2503 | goto retry; |
| 2504 | } |
| 2505 | goto err_free; |
| 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * If next overlaps, move base downwards so that it's |
| 2510 | * right below next and then recheck. |
| 2511 | */ |
| 2512 | if (next && next->va_start < base + end) { |
| 2513 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2514 | term_area = area; |
| 2515 | continue; |
| 2516 | } |
| 2517 | |
| 2518 | /* |
| 2519 | * If prev overlaps, shift down next and prev and move |
| 2520 | * base so that it's right below new next and then |
| 2521 | * recheck. |
| 2522 | */ |
| 2523 | if (prev && prev->va_end > base + start) { |
| 2524 | next = prev; |
| 2525 | prev = node_to_va(rb_prev(&next->rb_node)); |
| 2526 | base = pvm_determine_end(&next, &prev, align) - end; |
| 2527 | term_area = area; |
| 2528 | continue; |
| 2529 | } |
| 2530 | |
| 2531 | /* |
| 2532 | * This area fits, move on to the previous one. If |
| 2533 | * the previous one is the terminal one, we're done. |
| 2534 | */ |
| 2535 | area = (area + nr_vms - 1) % nr_vms; |
| 2536 | if (area == term_area) |
| 2537 | break; |
| 2538 | start = offsets[area]; |
| 2539 | end = start + sizes[area]; |
| 2540 | pvm_find_next_prev(base + end, &next, &prev); |
| 2541 | } |
| 2542 | found: |
| 2543 | /* we've found a fitting base, insert all va's */ |
| 2544 | for (area = 0; area < nr_vms; area++) { |
| 2545 | struct vmap_area *va = vas[area]; |
| 2546 | |
| 2547 | va->va_start = base + offsets[area]; |
| 2548 | va->va_end = va->va_start + sizes[area]; |
| 2549 | __insert_vmap_area(va); |
| 2550 | } |
| 2551 | |
| 2552 | vmap_area_pcpu_hole = base + offsets[last_area]; |
| 2553 | |
| 2554 | spin_unlock(&vmap_area_lock); |
| 2555 | |
| 2556 | /* insert all vm's */ |
| 2557 | for (area = 0; area < nr_vms; area++) |
| 2558 | insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC, |
| 2559 | pcpu_get_vm_areas); |
| 2560 | |
| 2561 | kfree(vas); |
| 2562 | return vms; |
| 2563 | |
| 2564 | err_free: |
| 2565 | for (area = 0; area < nr_vms; area++) { |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 2566 | kfree(vas[area]); |
| 2567 | kfree(vms[area]); |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2568 | } |
Kautuk Consul | f1db7af | 2012-01-12 17:20:08 -0800 | [diff] [blame] | 2569 | err_free2: |
Tejun Heo | ca23e40 | 2009-08-14 15:00:52 +0900 | [diff] [blame] | 2570 | kfree(vas); |
| 2571 | kfree(vms); |
| 2572 | return NULL; |
| 2573 | } |
| 2574 | |
| 2575 | /** |
| 2576 | * pcpu_free_vm_areas - free vmalloc areas for percpu allocator |
| 2577 | * @vms: vm_struct pointer array returned by pcpu_get_vm_areas() |
| 2578 | * @nr_vms: the number of allocated areas |
| 2579 | * |
| 2580 | * Free vm_structs and the array allocated by pcpu_get_vm_areas(). |
| 2581 | */ |
| 2582 | void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms) |
| 2583 | { |
| 2584 | int i; |
| 2585 | |
| 2586 | for (i = 0; i < nr_vms; i++) |
| 2587 | free_vm_area(vms[i]); |
| 2588 | kfree(vms); |
| 2589 | } |
Tejun Heo | 4f8b02b | 2010-09-03 18:22:47 +0200 | [diff] [blame] | 2590 | #endif /* CONFIG_SMP */ |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2591 | |
| 2592 | #ifdef CONFIG_PROC_FS |
| 2593 | static void *s_start(struct seq_file *m, loff_t *pos) |
Namhyung Kim | e199b5d | 2010-10-26 14:22:03 -0700 | [diff] [blame] | 2594 | __acquires(&vmlist_lock) |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2595 | { |
| 2596 | loff_t n = *pos; |
| 2597 | struct vm_struct *v; |
| 2598 | |
| 2599 | read_lock(&vmlist_lock); |
| 2600 | v = vmlist; |
| 2601 | while (n > 0 && v) { |
| 2602 | n--; |
| 2603 | v = v->next; |
| 2604 | } |
| 2605 | if (!n) |
| 2606 | return v; |
| 2607 | |
| 2608 | return NULL; |
| 2609 | |
| 2610 | } |
| 2611 | |
| 2612 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) |
| 2613 | { |
| 2614 | struct vm_struct *v = p; |
| 2615 | |
| 2616 | ++*pos; |
| 2617 | return v->next; |
| 2618 | } |
| 2619 | |
| 2620 | static void s_stop(struct seq_file *m, void *p) |
Namhyung Kim | e199b5d | 2010-10-26 14:22:03 -0700 | [diff] [blame] | 2621 | __releases(&vmlist_lock) |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2622 | { |
| 2623 | read_unlock(&vmlist_lock); |
| 2624 | } |
| 2625 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 2626 | static void show_numa_info(struct seq_file *m, struct vm_struct *v) |
| 2627 | { |
| 2628 | if (NUMA_BUILD) { |
| 2629 | unsigned int nr, *counters = m->private; |
| 2630 | |
| 2631 | if (!counters) |
| 2632 | return; |
| 2633 | |
| 2634 | memset(counters, 0, nr_node_ids * sizeof(unsigned int)); |
| 2635 | |
| 2636 | for (nr = 0; nr < v->nr_pages; nr++) |
| 2637 | counters[page_to_nid(v->pages[nr])]++; |
| 2638 | |
| 2639 | for_each_node_state(nr, N_HIGH_MEMORY) |
| 2640 | if (counters[nr]) |
| 2641 | seq_printf(m, " N%u=%u", nr, counters[nr]); |
| 2642 | } |
| 2643 | } |
| 2644 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2645 | static int s_show(struct seq_file *m, void *p) |
| 2646 | { |
| 2647 | struct vm_struct *v = p; |
| 2648 | |
| 2649 | seq_printf(m, "0x%p-0x%p %7ld", |
| 2650 | v->addr, v->addr + v->size, v->size); |
| 2651 | |
Joe Perches | 62c70bc | 2011-01-13 15:45:52 -0800 | [diff] [blame] | 2652 | if (v->caller) |
| 2653 | seq_printf(m, " %pS", v->caller); |
Christoph Lameter | 2301696 | 2008-04-28 02:12:42 -0700 | [diff] [blame] | 2654 | |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2655 | if (v->nr_pages) |
| 2656 | seq_printf(m, " pages=%d", v->nr_pages); |
| 2657 | |
| 2658 | if (v->phys_addr) |
Kenji Kaneshige | ffa71f3 | 2010-06-18 12:22:40 +0900 | [diff] [blame] | 2659 | seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2660 | |
| 2661 | if (v->flags & VM_IOREMAP) |
| 2662 | seq_printf(m, " ioremap"); |
| 2663 | |
| 2664 | if (v->flags & VM_ALLOC) |
| 2665 | seq_printf(m, " vmalloc"); |
| 2666 | |
| 2667 | if (v->flags & VM_MAP) |
| 2668 | seq_printf(m, " vmap"); |
| 2669 | |
| 2670 | if (v->flags & VM_USERMAP) |
| 2671 | seq_printf(m, " user"); |
| 2672 | |
| 2673 | if (v->flags & VM_VPAGES) |
| 2674 | seq_printf(m, " vpages"); |
| 2675 | |
Laura Abbott | d326348 | 2013-08-22 13:46:07 -0700 | [diff] [blame] | 2676 | if (v->flags & VM_LOWMEM) |
| 2677 | seq_printf(m, " lowmem"); |
| 2678 | |
Eric Dumazet | a47a126 | 2008-07-23 21:27:38 -0700 | [diff] [blame] | 2679 | show_numa_info(m, v); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2680 | seq_putc(m, '\n'); |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2684 | static const struct seq_operations vmalloc_op = { |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2685 | .start = s_start, |
| 2686 | .next = s_next, |
| 2687 | .stop = s_stop, |
| 2688 | .show = s_show, |
| 2689 | }; |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2690 | |
| 2691 | static int vmalloc_open(struct inode *inode, struct file *file) |
| 2692 | { |
| 2693 | unsigned int *ptr = NULL; |
| 2694 | int ret; |
| 2695 | |
Kulikov Vasiliy | 51980ac | 2010-08-09 17:19:58 -0700 | [diff] [blame] | 2696 | if (NUMA_BUILD) { |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2697 | ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL); |
Kulikov Vasiliy | 51980ac | 2010-08-09 17:19:58 -0700 | [diff] [blame] | 2698 | if (ptr == NULL) |
| 2699 | return -ENOMEM; |
| 2700 | } |
Alexey Dobriyan | 5f6a6a9 | 2008-10-06 03:50:47 +0400 | [diff] [blame] | 2701 | ret = seq_open(file, &vmalloc_op); |
| 2702 | if (!ret) { |
| 2703 | struct seq_file *m = file->private_data; |
| 2704 | m->private = ptr; |
| 2705 | } else |
| 2706 | kfree(ptr); |
| 2707 | return ret; |
| 2708 | } |
| 2709 | |
| 2710 | static const struct file_operations proc_vmalloc_operations = { |
| 2711 | .open = vmalloc_open, |
| 2712 | .read = seq_read, |
| 2713 | .llseek = seq_lseek, |
| 2714 | .release = seq_release_private, |
| 2715 | }; |
| 2716 | |
| 2717 | static int __init proc_vmalloc_init(void) |
| 2718 | { |
| 2719 | proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations); |
| 2720 | return 0; |
| 2721 | } |
| 2722 | module_init(proc_vmalloc_init); |
Christoph Lameter | a10aa57 | 2008-04-28 02:12:40 -0700 | [diff] [blame] | 2723 | #endif |
| 2724 | |