blob: c260b5e35c2171f7288cbfa46f777b1807c31744 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Lameter930fc452005-10-29 18:15:41 -07008 * Numa awareness, Christoph Lameter, SGI, June 2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Nick Piggindb64fe02008-10-18 20:27:03 -070011#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/highmem.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +040019#include <linux/proc_fs.h>
Christoph Lametera10aa572008-04-28 02:12:40 -070020#include <linux/seq_file.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070021#include <linux/debugobjects.h>
Christoph Lameter23016962008-04-28 02:12:42 -070022#include <linux/kallsyms.h>
Nick Piggindb64fe02008-10-18 20:27:03 -070023#include <linux/list.h>
24#include <linux/rbtree.h>
25#include <linux/radix-tree.h>
26#include <linux/rcupdate.h>
Tejun Heof0aa6612009-02-20 16:29:08 +090027#include <linux/pfn.h>
Catalin Marinas89219d32009-06-11 13:23:19 +010028#include <linux/kmemleak.h>
Arun Sharma60063492011-07-26 16:09:06 -070029#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/tlbflush.h>
David Miller2dca6992009-09-21 12:22:34 -070032#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Nick Piggindb64fe02008-10-18 20:27:03 -070034/*** Page table manipulation functions ***/
Adrian Bunkb2213852006-09-25 23:31:02 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static 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 Piggindb64fe02008-10-18 20:27:03 -070047static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
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 Piggindb64fe02008-10-18 20:27:03 -070061static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
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 Piggindb64fe02008-10-18 20:27:03 -070075static void vunmap_page_range(unsigned long addr, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 pgd_t *pgd;
78 unsigned long next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 BUG_ON(addr >= end);
81 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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 Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
Nick Piggindb64fe02008-10-18 20:27:03 -070091 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 pte_t *pte;
94
Nick Piggindb64fe02008-10-18 20:27:03 -070095 /*
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 Dickins872fec12005-10-29 18:16:21 -0700100 pte = pte_alloc_kernel(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!pte)
102 return -ENOMEM;
103 do {
Nick Piggindb64fe02008-10-18 20:27:03 -0700104 struct page *page = pages[*nr];
105
106 if (WARN_ON(!pte_none(*pte)))
107 return -EBUSY;
108 if (WARN_ON(!page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENOMEM;
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
Nick Piggindb64fe02008-10-18 20:27:03 -0700111 (*nr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } while (pte++, addr += PAGE_SIZE, addr != end);
113 return 0;
114}
115
Nick Piggindb64fe02008-10-18 20:27:03 -0700116static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
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 Piggindb64fe02008-10-18 20:27:03 -0700127 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -ENOMEM;
129 } while (pmd++, addr = next, addr != end);
130 return 0;
131}
132
Nick Piggindb64fe02008-10-18 20:27:03 -0700133static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
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 Piggindb64fe02008-10-18 20:27:03 -0700144 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -ENOMEM;
146 } while (pud++, addr = next, addr != end);
147 return 0;
148}
149
Nick Piggindb64fe02008-10-18 20:27:03 -0700150/*
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 Heo8fc48982009-02-20 16:29:08 +0900156static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157 pgprot_t prot, struct page **pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 pgd_t *pgd;
160 unsigned long next;
Adam Lackorzynski2e4e27c2009-01-04 12:00:46 -0800161 unsigned long addr = start;
Nick Piggindb64fe02008-10-18 20:27:03 -0700162 int err = 0;
163 int nr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 BUG_ON(addr >= end);
166 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 do {
168 next = pgd_addr_end(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700169 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (err)
Figo.zhangbf88c8c2009-09-21 17:01:47 -0700171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } while (pgd++, addr = next, addr != end);
Nick Piggindb64fe02008-10-18 20:27:03 -0700173
Nick Piggindb64fe02008-10-18 20:27:03 -0700174 return nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Tejun Heo8fc48982009-02-20 16:29:08 +0900177static 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
KAMEZAWA Hiroyuki81ac3ad2009-09-22 16:45:49 -0700187int is_vmalloc_or_module_addr(const void *x)
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700188{
189 /*
Russell Kingab4f2ee2008-11-06 17:11:07 +0000190 * ARM, x86-64 and sparc64 put modules in a special place,
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700191 * and fall back on vmalloc() if that fails. Others
192 * just put it in the vmalloc space.
193 */
194#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
195 unsigned long addr = (unsigned long)x;
196 if (addr >= MODULES_VADDR && addr < MODULES_END)
197 return 1;
198#endif
199 return is_vmalloc_addr(x);
200}
201
Christoph Lameter48667e72008-02-04 22:28:31 -0800202/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700203 * Walk a vmap address to the struct page it maps.
Christoph Lameter48667e72008-02-04 22:28:31 -0800204 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800205struct page *vmalloc_to_page(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800206{
207 unsigned long addr = (unsigned long) vmalloc_addr;
208 struct page *page = NULL;
209 pgd_t *pgd = pgd_offset_k(addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800210
Ingo Molnar7aa413d2008-06-19 13:28:11 +0200211 /*
212 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
213 * architectures that do not vmalloc module space
214 */
Linus Torvalds73bdf0a2008-10-15 08:35:12 -0700215 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
Jiri Slaby59ea7462008-06-12 13:56:40 +0200216
Christoph Lameter48667e72008-02-04 22:28:31 -0800217 if (!pgd_none(*pgd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700218 pud_t *pud = pud_offset(pgd, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800219 if (!pud_none(*pud)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700220 pmd_t *pmd = pmd_offset(pud, addr);
Christoph Lameter48667e72008-02-04 22:28:31 -0800221 if (!pmd_none(*pmd)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700222 pte_t *ptep, pte;
223
Christoph Lameter48667e72008-02-04 22:28:31 -0800224 ptep = pte_offset_map(pmd, addr);
225 pte = *ptep;
226 if (pte_present(pte))
227 page = pte_page(pte);
228 pte_unmap(ptep);
229 }
230 }
231 }
232 return page;
233}
234EXPORT_SYMBOL(vmalloc_to_page);
235
236/*
237 * Map a vmalloc()-space virtual address to the physical page frame number.
238 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -0800239unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
Christoph Lameter48667e72008-02-04 22:28:31 -0800240{
241 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
242}
243EXPORT_SYMBOL(vmalloc_to_pfn);
244
Nick Piggindb64fe02008-10-18 20:27:03 -0700245
246/*** Global kva allocator ***/
247
248#define VM_LAZY_FREE 0x01
249#define VM_LAZY_FREEING 0x02
250#define VM_VM_AREA 0x04
251
252struct vmap_area {
253 unsigned long va_start;
254 unsigned long va_end;
255 unsigned long flags;
256 struct rb_node rb_node; /* address sorted rbtree */
257 struct list_head list; /* address sorted list */
258 struct list_head purge_list; /* "lazy purge" list */
Minchan Kimdb1aeca2012-01-10 15:08:39 -0800259 struct vm_struct *vm;
Nick Piggindb64fe02008-10-18 20:27:03 -0700260 struct rcu_head rcu_head;
261};
262
263static DEFINE_SPINLOCK(vmap_area_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700264static LIST_HEAD(vmap_area_list);
Nick Piggin89699602011-03-22 16:30:36 -0700265static struct rb_root vmap_area_root = RB_ROOT;
266
267/* The vmap cache globals are protected by vmap_area_lock */
268static struct rb_node *free_vmap_cache;
269static unsigned long cached_hole_size;
270static unsigned long cached_vstart;
271static unsigned long cached_align;
272
Tejun Heoca23e402009-08-14 15:00:52 +0900273static unsigned long vmap_area_pcpu_hole;
Nick Piggindb64fe02008-10-18 20:27:03 -0700274
Laura Abbottf2da5eb2013-12-20 13:17:19 -0800275#ifdef CONFIG_ENABLE_VMALLOC_SAVING
276#define POSSIBLE_VMALLOC_START PAGE_OFFSET
277
278#define VMALLOC_BITMAP_SIZE ((VMALLOC_END - PAGE_OFFSET) >> \
279 PAGE_SHIFT)
280#define VMALLOC_TO_BIT(addr) ((addr - PAGE_OFFSET) >> PAGE_SHIFT)
281#define BIT_TO_VMALLOC(i) (PAGE_OFFSET + i * PAGE_SIZE)
282
283DECLARE_BITMAP(possible_areas, VMALLOC_BITMAP_SIZE);
284
285void mark_vmalloc_reserved_area(void *x, unsigned long size)
286{
287 unsigned long addr = (unsigned long)x;
288
289 bitmap_set(possible_areas, VMALLOC_TO_BIT(addr), size >> PAGE_SHIFT);
290}
291
292int is_vmalloc_addr(const void *x)
293{
294 unsigned long addr = (unsigned long)x;
295
296 if (addr < POSSIBLE_VMALLOC_START || addr >= VMALLOC_END)
297 return 0;
298
299 if (test_bit(VMALLOC_TO_BIT(addr), possible_areas))
300 return 0;
301
302 return 1;
303}
304#else
305int is_vmalloc_addr(const void *x)
306{
307 unsigned long addr = (unsigned long)x;
308
309 return addr >= VMALLOC_START && addr < VMALLOC_END;
310}
311#endif
312EXPORT_SYMBOL(is_vmalloc_addr);
313
314
315
Nick Piggindb64fe02008-10-18 20:27:03 -0700316static struct vmap_area *__find_vmap_area(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Nick Piggindb64fe02008-10-18 20:27:03 -0700318 struct rb_node *n = vmap_area_root.rb_node;
319
320 while (n) {
321 struct vmap_area *va;
322
323 va = rb_entry(n, struct vmap_area, rb_node);
324 if (addr < va->va_start)
325 n = n->rb_left;
326 else if (addr > va->va_start)
327 n = n->rb_right;
328 else
329 return va;
330 }
331
332 return NULL;
333}
334
335static void __insert_vmap_area(struct vmap_area *va)
336{
337 struct rb_node **p = &vmap_area_root.rb_node;
338 struct rb_node *parent = NULL;
339 struct rb_node *tmp;
340
341 while (*p) {
Namhyung Kim170168d2010-10-26 14:22:02 -0700342 struct vmap_area *tmp_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700343
344 parent = *p;
Namhyung Kim170168d2010-10-26 14:22:02 -0700345 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
346 if (va->va_start < tmp_va->va_end)
Nick Piggindb64fe02008-10-18 20:27:03 -0700347 p = &(*p)->rb_left;
Namhyung Kim170168d2010-10-26 14:22:02 -0700348 else if (va->va_end > tmp_va->va_start)
Nick Piggindb64fe02008-10-18 20:27:03 -0700349 p = &(*p)->rb_right;
350 else
351 BUG();
352 }
353
354 rb_link_node(&va->rb_node, parent, p);
355 rb_insert_color(&va->rb_node, &vmap_area_root);
356
357 /* address-sort this list so it is usable like the vmlist */
358 tmp = rb_prev(&va->rb_node);
359 if (tmp) {
360 struct vmap_area *prev;
361 prev = rb_entry(tmp, struct vmap_area, rb_node);
362 list_add_rcu(&va->list, &prev->list);
363 } else
364 list_add_rcu(&va->list, &vmap_area_list);
365}
366
367static void purge_vmap_area_lazy(void);
368
369/*
370 * Allocate a region of KVA of the specified size and alignment, within the
371 * vstart and vend.
372 */
373static struct vmap_area *alloc_vmap_area(unsigned long size,
374 unsigned long align,
375 unsigned long vstart, unsigned long vend,
376 int node, gfp_t gfp_mask)
377{
378 struct vmap_area *va;
379 struct rb_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned long addr;
Nick Piggindb64fe02008-10-18 20:27:03 -0700381 int purged = 0;
Nick Piggin89699602011-03-22 16:30:36 -0700382 struct vmap_area *first;
Nick Piggindb64fe02008-10-18 20:27:03 -0700383
Nick Piggin77669702009-02-27 14:03:03 -0800384 BUG_ON(!size);
Nick Piggindb64fe02008-10-18 20:27:03 -0700385 BUG_ON(size & ~PAGE_MASK);
Nick Piggin89699602011-03-22 16:30:36 -0700386 BUG_ON(!is_power_of_2(align));
Nick Piggindb64fe02008-10-18 20:27:03 -0700387
Nick Piggindb64fe02008-10-18 20:27:03 -0700388 va = kmalloc_node(sizeof(struct vmap_area),
389 gfp_mask & GFP_RECLAIM_MASK, node);
390 if (unlikely(!va))
391 return ERR_PTR(-ENOMEM);
392
Catalin Marinas9504eeb2013-11-12 15:07:45 -0800393 /*
394 * Only scan the relevant parts containing pointers to other objects
395 * to avoid false negatives.
396 */
397 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
398
Nick Piggindb64fe02008-10-18 20:27:03 -0700399retry:
400 spin_lock(&vmap_area_lock);
Nick Piggin89699602011-03-22 16:30:36 -0700401 /*
402 * Invalidate cache if we have more permissive parameters.
403 * cached_hole_size notes the largest hole noticed _below_
404 * the vmap_area cached in free_vmap_cache: if size fits
405 * into that hole, we want to scan from vstart to reuse
406 * the hole instead of allocating above free_vmap_cache.
407 * Note that __free_vmap_area may update free_vmap_cache
408 * without updating cached_hole_size or cached_align.
409 */
410 if (!free_vmap_cache ||
411 size < cached_hole_size ||
412 vstart < cached_vstart ||
413 align < cached_align) {
414nocache:
415 cached_hole_size = 0;
416 free_vmap_cache = NULL;
417 }
418 /* record if we encounter less permissive parameters */
419 cached_vstart = vstart;
420 cached_align = align;
Nick Piggin77669702009-02-27 14:03:03 -0800421
Nick Piggin89699602011-03-22 16:30:36 -0700422 /* find starting point for our search */
423 if (free_vmap_cache) {
424 first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700425 addr = ALIGN(first->va_end, align);
Nick Piggin89699602011-03-22 16:30:36 -0700426 if (addr < vstart)
427 goto nocache;
428 if (addr + size - 1 < addr)
429 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700430
Nick Piggin89699602011-03-22 16:30:36 -0700431 } else {
432 addr = ALIGN(vstart, align);
433 if (addr + size - 1 < addr)
434 goto overflow;
435
436 n = vmap_area_root.rb_node;
437 first = NULL;
438
439 while (n) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700440 struct vmap_area *tmp;
441 tmp = rb_entry(n, struct vmap_area, rb_node);
442 if (tmp->va_end >= addr) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700443 first = tmp;
Nick Piggin89699602011-03-22 16:30:36 -0700444 if (tmp->va_start <= addr)
445 break;
446 n = n->rb_left;
447 } else
Nick Piggindb64fe02008-10-18 20:27:03 -0700448 n = n->rb_right;
Nick Piggin89699602011-03-22 16:30:36 -0700449 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700450
451 if (!first)
452 goto found;
Nick Piggindb64fe02008-10-18 20:27:03 -0700453 }
Nick Piggin89699602011-03-22 16:30:36 -0700454
455 /* from the starting point, walk areas until a suitable hole is found */
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700456 while (addr + size > first->va_start && addr + size <= vend) {
Nick Piggin89699602011-03-22 16:30:36 -0700457 if (addr + cached_hole_size < first->va_start)
458 cached_hole_size = first->va_start - addr;
Johannes Weiner248ac0e2011-05-24 17:11:43 -0700459 addr = ALIGN(first->va_end, align);
Nick Piggin89699602011-03-22 16:30:36 -0700460 if (addr + size - 1 < addr)
461 goto overflow;
462
463 n = rb_next(&first->rb_node);
464 if (n)
465 first = rb_entry(n, struct vmap_area, rb_node);
466 else
467 goto found;
468 }
469
Nick Piggindb64fe02008-10-18 20:27:03 -0700470found:
Nick Piggin89699602011-03-22 16:30:36 -0700471 if (addr + size > vend)
472 goto overflow;
Nick Piggindb64fe02008-10-18 20:27:03 -0700473
474 va->va_start = addr;
475 va->va_end = addr + size;
476 va->flags = 0;
477 __insert_vmap_area(va);
Nick Piggin89699602011-03-22 16:30:36 -0700478 free_vmap_cache = &va->rb_node;
Nick Piggindb64fe02008-10-18 20:27:03 -0700479 spin_unlock(&vmap_area_lock);
480
Nick Piggin89699602011-03-22 16:30:36 -0700481 BUG_ON(va->va_start & (align-1));
482 BUG_ON(va->va_start < vstart);
483 BUG_ON(va->va_end > vend);
484
Nick Piggindb64fe02008-10-18 20:27:03 -0700485 return va;
Nick Piggin89699602011-03-22 16:30:36 -0700486
487overflow:
488 spin_unlock(&vmap_area_lock);
489 if (!purged) {
490 purge_vmap_area_lazy();
491 purged = 1;
492 goto retry;
493 }
494 if (printk_ratelimit())
495 printk(KERN_WARNING
496 "vmap allocation for size %lu failed: "
497 "use vmalloc=<size> to increase size.\n", size);
498 kfree(va);
499 return ERR_PTR(-EBUSY);
Nick Piggindb64fe02008-10-18 20:27:03 -0700500}
501
Nick Piggindb64fe02008-10-18 20:27:03 -0700502static void __free_vmap_area(struct vmap_area *va)
503{
504 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
Nick Piggin89699602011-03-22 16:30:36 -0700505
506 if (free_vmap_cache) {
507 if (va->va_end < cached_vstart) {
508 free_vmap_cache = NULL;
509 } else {
510 struct vmap_area *cache;
511 cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
512 if (va->va_start <= cache->va_start) {
513 free_vmap_cache = rb_prev(&va->rb_node);
514 /*
515 * We don't try to update cached_hole_size or
516 * cached_align, but it won't go very wrong.
517 */
518 }
519 }
520 }
Nick Piggindb64fe02008-10-18 20:27:03 -0700521 rb_erase(&va->rb_node, &vmap_area_root);
522 RB_CLEAR_NODE(&va->rb_node);
523 list_del_rcu(&va->list);
524
Tejun Heoca23e402009-08-14 15:00:52 +0900525 /*
526 * Track the highest possible candidate for pcpu area
527 * allocation. Areas outside of vmalloc area can be returned
528 * here too, consider only end addresses which fall inside
529 * vmalloc area proper.
530 */
531 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
532 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
533
Lai Jiangshan14769de2011-03-18 12:12:19 +0800534 kfree_rcu(va, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700535}
536
537/*
538 * Free a region of KVA allocated by alloc_vmap_area
539 */
540static void free_vmap_area(struct vmap_area *va)
541{
542 spin_lock(&vmap_area_lock);
543 __free_vmap_area(va);
544 spin_unlock(&vmap_area_lock);
545}
546
547/*
548 * Clear the pagetable entries of a given vmap_area
549 */
550static void unmap_vmap_area(struct vmap_area *va)
551{
552 vunmap_page_range(va->va_start, va->va_end);
553}
554
Nick Piggincd528582009-01-06 14:39:20 -0800555static void vmap_debug_free_range(unsigned long start, unsigned long end)
556{
557 /*
558 * Unmap page tables and force a TLB flush immediately if
559 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
560 * bugs similarly to those in linear kernel virtual address
561 * space after a page has been freed.
562 *
563 * All the lazy freeing logic is still retained, in order to
564 * minimise intrusiveness of this debugging feature.
565 *
566 * This is going to be *slow* (linear kernel virtual address
567 * debugging doesn't do a broadcast TLB flush so it is a lot
568 * faster).
569 */
570#ifdef CONFIG_DEBUG_PAGEALLOC
571 vunmap_page_range(start, end);
572 flush_tlb_kernel_range(start, end);
573#endif
574}
575
Nick Piggindb64fe02008-10-18 20:27:03 -0700576/*
577 * lazy_max_pages is the maximum amount of virtual address space we gather up
578 * before attempting to purge with a TLB flush.
579 *
580 * There is a tradeoff here: a larger number will cover more kernel page tables
581 * and take slightly longer to purge, but it will linearly reduce the number of
582 * global TLB flushes that must be performed. It would seem natural to scale
583 * this number up linearly with the number of CPUs (because vmapping activity
584 * could also scale linearly with the number of CPUs), however it is likely
585 * that in practice, workloads might be constrained in other ways that mean
586 * vmap activity will not scale linearly with CPUs. Also, I want to be
587 * conservative and not introduce a big latency on huge systems, so go with
588 * a less aggressive log scale. It will still be an improvement over the old
589 * code, and it will be simple to change the scale factor if we find that it
590 * becomes a problem on bigger systems.
591 */
592static unsigned long lazy_max_pages(void)
593{
594 unsigned int log;
595
596 log = fls(num_online_cpus());
597
598 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
599}
600
601static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
602
Nick Piggin02b709d2010-02-01 22:25:57 +1100603/* for per-CPU blocks */
604static void purge_fragmented_blocks_allcpus(void);
605
Nick Piggindb64fe02008-10-18 20:27:03 -0700606/*
Cliff Wickman3ee48b62010-09-16 11:44:02 -0500607 * called before a call to iounmap() if the caller wants vm_area_struct's
608 * immediately freed.
609 */
610void set_iounmap_nonlazy(void)
611{
612 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
613}
614
615/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700616 * Purges all lazily-freed vmap areas.
617 *
618 * If sync is 0 then don't purge if there is already a purge in progress.
619 * If force_flush is 1, then flush kernel TLBs between *start and *end even
620 * if we found no lazy vmap areas to unmap (callers can use this to optimise
621 * their own TLB flushing).
622 * Returns with *start = min(*start, lowest purged address)
623 * *end = max(*end, highest purged address)
624 */
625static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
626 int sync, int force_flush)
627{
Andrew Morton46666d82009-01-15 13:51:15 -0800628 static DEFINE_SPINLOCK(purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700629 LIST_HEAD(valist);
630 struct vmap_area *va;
Vegard Nossumcbb76672009-02-27 14:03:04 -0800631 struct vmap_area *n_va;
Nick Piggindb64fe02008-10-18 20:27:03 -0700632 int nr = 0;
633
634 /*
635 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
636 * should not expect such behaviour. This just simplifies locking for
637 * the case that isn't actually used at the moment anyway.
638 */
639 if (!sync && !force_flush) {
Andrew Morton46666d82009-01-15 13:51:15 -0800640 if (!spin_trylock(&purge_lock))
Nick Piggindb64fe02008-10-18 20:27:03 -0700641 return;
642 } else
Andrew Morton46666d82009-01-15 13:51:15 -0800643 spin_lock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700644
Nick Piggin02b709d2010-02-01 22:25:57 +1100645 if (sync)
646 purge_fragmented_blocks_allcpus();
647
Nick Piggindb64fe02008-10-18 20:27:03 -0700648 rcu_read_lock();
649 list_for_each_entry_rcu(va, &vmap_area_list, list) {
650 if (va->flags & VM_LAZY_FREE) {
651 if (va->va_start < *start)
652 *start = va->va_start;
653 if (va->va_end > *end)
654 *end = va->va_end;
655 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
Nick Piggindb64fe02008-10-18 20:27:03 -0700656 list_add_tail(&va->purge_list, &valist);
657 va->flags |= VM_LAZY_FREEING;
658 va->flags &= ~VM_LAZY_FREE;
659 }
660 }
661 rcu_read_unlock();
662
Yongseok Koh88f50042010-01-19 17:33:49 +0900663 if (nr)
Nick Piggindb64fe02008-10-18 20:27:03 -0700664 atomic_sub(nr, &vmap_lazy_nr);
Nick Piggindb64fe02008-10-18 20:27:03 -0700665
666 if (nr || force_flush)
667 flush_tlb_kernel_range(*start, *end);
668
669 if (nr) {
670 spin_lock(&vmap_area_lock);
Vegard Nossumcbb76672009-02-27 14:03:04 -0800671 list_for_each_entry_safe(va, n_va, &valist, purge_list)
Nick Piggindb64fe02008-10-18 20:27:03 -0700672 __free_vmap_area(va);
673 spin_unlock(&vmap_area_lock);
674 }
Andrew Morton46666d82009-01-15 13:51:15 -0800675 spin_unlock(&purge_lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700676}
677
678/*
Nick Piggin496850e2008-11-19 15:36:33 -0800679 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
680 * is already purging.
681 */
682static void try_purge_vmap_area_lazy(void)
683{
684 unsigned long start = ULONG_MAX, end = 0;
685
686 __purge_vmap_area_lazy(&start, &end, 0, 0);
687}
688
689/*
Nick Piggindb64fe02008-10-18 20:27:03 -0700690 * Kick off a purge of the outstanding lazy areas.
691 */
692static void purge_vmap_area_lazy(void)
693{
694 unsigned long start = ULONG_MAX, end = 0;
695
Nick Piggin496850e2008-11-19 15:36:33 -0800696 __purge_vmap_area_lazy(&start, &end, 1, 0);
Nick Piggindb64fe02008-10-18 20:27:03 -0700697}
698
699/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800700 * Free a vmap area, caller ensuring that the area has been unmapped
701 * and flush_cache_vunmap had been called for the correct range
702 * previously.
Nick Piggindb64fe02008-10-18 20:27:03 -0700703 */
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800704static void free_vmap_area_noflush(struct vmap_area *va)
Nick Piggindb64fe02008-10-18 20:27:03 -0700705{
706 va->flags |= VM_LAZY_FREE;
707 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
708 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
Nick Piggin496850e2008-11-19 15:36:33 -0800709 try_purge_vmap_area_lazy();
Nick Piggindb64fe02008-10-18 20:27:03 -0700710}
711
Nick Pigginb29acbd2008-12-01 13:13:47 -0800712/*
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800713 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
714 * called for the correct range previously.
715 */
716static void free_unmap_vmap_area_noflush(struct vmap_area *va)
717{
718 unmap_vmap_area(va);
719 free_vmap_area_noflush(va);
720}
721
722/*
Nick Pigginb29acbd2008-12-01 13:13:47 -0800723 * Free and unmap a vmap area
724 */
725static void free_unmap_vmap_area(struct vmap_area *va)
726{
727 flush_cache_vunmap(va->va_start, va->va_end);
728 free_unmap_vmap_area_noflush(va);
729}
730
Nick Piggindb64fe02008-10-18 20:27:03 -0700731static struct vmap_area *find_vmap_area(unsigned long addr)
732{
733 struct vmap_area *va;
734
735 spin_lock(&vmap_area_lock);
736 va = __find_vmap_area(addr);
737 spin_unlock(&vmap_area_lock);
738
739 return va;
740}
741
742static void free_unmap_vmap_area_addr(unsigned long addr)
743{
744 struct vmap_area *va;
745
746 va = find_vmap_area(addr);
747 BUG_ON(!va);
748 free_unmap_vmap_area(va);
749}
750
751
752/*** Per cpu kva allocator ***/
753
754/*
755 * vmap space is limited especially on 32 bit architectures. Ensure there is
756 * room for at least 16 percpu vmap blocks per CPU.
757 */
758/*
759 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
760 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
761 * instead (we just need a rough idea)
762 */
763#if BITS_PER_LONG == 32
764#define VMALLOC_SPACE (128UL*1024*1024)
765#else
766#define VMALLOC_SPACE (128UL*1024*1024*1024)
767#endif
768
769#define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
770#define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
771#define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
772#define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
773#define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
774#define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
Clemens Ladischf982f912011-06-21 22:09:50 +0200775#define VMAP_BBMAP_BITS \
776 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
777 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
778 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
Nick Piggindb64fe02008-10-18 20:27:03 -0700779
780#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
781
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +1100782static bool vmap_initialized __read_mostly = false;
783
Nick Piggindb64fe02008-10-18 20:27:03 -0700784struct vmap_block_queue {
785 spinlock_t lock;
786 struct list_head free;
Nick Piggindb64fe02008-10-18 20:27:03 -0700787};
788
789struct vmap_block {
790 spinlock_t lock;
791 struct vmap_area *va;
792 struct vmap_block_queue *vbq;
793 unsigned long free, dirty;
794 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
795 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
Nick Pigginde560422010-02-01 22:24:18 +1100796 struct list_head free_list;
797 struct rcu_head rcu_head;
Nick Piggin02b709d2010-02-01 22:25:57 +1100798 struct list_head purge;
Nick Piggindb64fe02008-10-18 20:27:03 -0700799};
800
801/* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
802static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
803
804/*
805 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
806 * in the free path. Could get rid of this if we change the API to return a
807 * "cookie" from alloc, to be passed to free. But no big deal yet.
808 */
809static DEFINE_SPINLOCK(vmap_block_tree_lock);
810static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
811
812/*
813 * We should probably have a fallback mechanism to allocate virtual memory
814 * out of partially filled vmap blocks. However vmap block sizing should be
815 * fairly reasonable according to the vmalloc size, so it shouldn't be a
816 * big problem.
817 */
818
819static unsigned long addr_to_vb_idx(unsigned long addr)
820{
821 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
822 addr /= VMAP_BLOCK_SIZE;
823 return addr;
824}
825
826static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
827{
828 struct vmap_block_queue *vbq;
829 struct vmap_block *vb;
830 struct vmap_area *va;
831 unsigned long vb_idx;
832 int node, err;
833
834 node = numa_node_id();
835
836 vb = kmalloc_node(sizeof(struct vmap_block),
837 gfp_mask & GFP_RECLAIM_MASK, node);
838 if (unlikely(!vb))
839 return ERR_PTR(-ENOMEM);
840
841 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
842 VMALLOC_START, VMALLOC_END,
843 node, gfp_mask);
Tobias Klauserddf9c6d2011-01-13 15:46:15 -0800844 if (IS_ERR(va)) {
Nick Piggindb64fe02008-10-18 20:27:03 -0700845 kfree(vb);
Julia Lawalle7d86342010-08-09 17:18:28 -0700846 return ERR_CAST(va);
Nick Piggindb64fe02008-10-18 20:27:03 -0700847 }
848
849 err = radix_tree_preload(gfp_mask);
850 if (unlikely(err)) {
851 kfree(vb);
852 free_vmap_area(va);
853 return ERR_PTR(err);
854 }
855
856 spin_lock_init(&vb->lock);
857 vb->va = va;
858 vb->free = VMAP_BBMAP_BITS;
859 vb->dirty = 0;
860 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
861 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
862 INIT_LIST_HEAD(&vb->free_list);
Nick Piggindb64fe02008-10-18 20:27:03 -0700863
864 vb_idx = addr_to_vb_idx(va->va_start);
865 spin_lock(&vmap_block_tree_lock);
866 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
867 spin_unlock(&vmap_block_tree_lock);
868 BUG_ON(err);
869 radix_tree_preload_end();
870
871 vbq = &get_cpu_var(vmap_block_queue);
872 vb->vbq = vbq;
873 spin_lock(&vbq->lock);
Nick Pigginde560422010-02-01 22:24:18 +1100874 list_add_rcu(&vb->free_list, &vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -0700875 spin_unlock(&vbq->lock);
Tejun Heo3f04ba82009-10-29 22:34:12 +0900876 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700877
878 return vb;
879}
880
Nick Piggindb64fe02008-10-18 20:27:03 -0700881static void free_vmap_block(struct vmap_block *vb)
882{
883 struct vmap_block *tmp;
884 unsigned long vb_idx;
885
Nick Piggindb64fe02008-10-18 20:27:03 -0700886 vb_idx = addr_to_vb_idx(vb->va->va_start);
887 spin_lock(&vmap_block_tree_lock);
888 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
889 spin_unlock(&vmap_block_tree_lock);
890 BUG_ON(tmp != vb);
891
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -0800892 free_vmap_area_noflush(vb->va);
Lai Jiangshan22a3c7d2011-03-18 12:13:08 +0800893 kfree_rcu(vb, rcu_head);
Nick Piggindb64fe02008-10-18 20:27:03 -0700894}
895
Nick Piggin02b709d2010-02-01 22:25:57 +1100896static void purge_fragmented_blocks(int cpu)
897{
898 LIST_HEAD(purge);
899 struct vmap_block *vb;
900 struct vmap_block *n_vb;
901 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
902
903 rcu_read_lock();
904 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
905
906 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
907 continue;
908
909 spin_lock(&vb->lock);
910 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
911 vb->free = 0; /* prevent further allocs after releasing lock */
912 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
913 bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
914 bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
915 spin_lock(&vbq->lock);
916 list_del_rcu(&vb->free_list);
917 spin_unlock(&vbq->lock);
918 spin_unlock(&vb->lock);
919 list_add_tail(&vb->purge, &purge);
920 } else
921 spin_unlock(&vb->lock);
922 }
923 rcu_read_unlock();
924
925 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
926 list_del(&vb->purge);
927 free_vmap_block(vb);
928 }
929}
930
931static void purge_fragmented_blocks_thiscpu(void)
932{
933 purge_fragmented_blocks(smp_processor_id());
934}
935
936static void purge_fragmented_blocks_allcpus(void)
937{
938 int cpu;
939
940 for_each_possible_cpu(cpu)
941 purge_fragmented_blocks(cpu);
942}
943
Nick Piggindb64fe02008-10-18 20:27:03 -0700944static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
945{
946 struct vmap_block_queue *vbq;
947 struct vmap_block *vb;
948 unsigned long addr = 0;
949 unsigned int order;
Nick Piggin02b709d2010-02-01 22:25:57 +1100950 int purge = 0;
Nick Piggindb64fe02008-10-18 20:27:03 -0700951
952 BUG_ON(size & ~PAGE_MASK);
953 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
954 order = get_order(size);
955
956again:
957 rcu_read_lock();
958 vbq = &get_cpu_var(vmap_block_queue);
959 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
960 int i;
961
962 spin_lock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +1100963 if (vb->free < 1UL << order)
964 goto next;
965
Nick Piggindb64fe02008-10-18 20:27:03 -0700966 i = bitmap_find_free_region(vb->alloc_map,
967 VMAP_BBMAP_BITS, order);
968
Nick Piggin02b709d2010-02-01 22:25:57 +1100969 if (i < 0) {
970 if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
971 /* fragmented and no outstanding allocations */
972 BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
973 purge = 1;
Nick Piggindb64fe02008-10-18 20:27:03 -0700974 }
Nick Piggin02b709d2010-02-01 22:25:57 +1100975 goto next;
976 }
977 addr = vb->va->va_start + (i << PAGE_SHIFT);
978 BUG_ON(addr_to_vb_idx(addr) !=
979 addr_to_vb_idx(vb->va->va_start));
980 vb->free -= 1UL << order;
981 if (vb->free == 0) {
982 spin_lock(&vbq->lock);
983 list_del_rcu(&vb->free_list);
984 spin_unlock(&vbq->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700985 }
986 spin_unlock(&vb->lock);
Nick Piggin02b709d2010-02-01 22:25:57 +1100987 break;
988next:
989 spin_unlock(&vb->lock);
Nick Piggindb64fe02008-10-18 20:27:03 -0700990 }
Nick Piggin02b709d2010-02-01 22:25:57 +1100991
992 if (purge)
993 purge_fragmented_blocks_thiscpu();
994
Tejun Heo3f04ba82009-10-29 22:34:12 +0900995 put_cpu_var(vmap_block_queue);
Nick Piggindb64fe02008-10-18 20:27:03 -0700996 rcu_read_unlock();
997
998 if (!addr) {
999 vb = new_vmap_block(gfp_mask);
1000 if (IS_ERR(vb))
1001 return vb;
1002 goto again;
1003 }
1004
1005 return (void *)addr;
1006}
1007
1008static void vb_free(const void *addr, unsigned long size)
1009{
1010 unsigned long offset;
1011 unsigned long vb_idx;
1012 unsigned int order;
1013 struct vmap_block *vb;
1014
1015 BUG_ON(size & ~PAGE_MASK);
1016 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
Nick Pigginb29acbd2008-12-01 13:13:47 -08001017
1018 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1019
Nick Piggindb64fe02008-10-18 20:27:03 -07001020 order = get_order(size);
1021
1022 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
1023
1024 vb_idx = addr_to_vb_idx((unsigned long)addr);
1025 rcu_read_lock();
1026 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1027 rcu_read_unlock();
1028 BUG_ON(!vb);
1029
Jeremy Fitzhardinge64141da2010-12-02 14:31:18 -08001030 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1031
Nick Piggindb64fe02008-10-18 20:27:03 -07001032 spin_lock(&vb->lock);
Nick Pigginde560422010-02-01 22:24:18 +11001033 BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
MinChan Kimd0868172009-03-31 15:19:26 -07001034
Nick Piggindb64fe02008-10-18 20:27:03 -07001035 vb->dirty += 1UL << order;
1036 if (vb->dirty == VMAP_BBMAP_BITS) {
Nick Pigginde560422010-02-01 22:24:18 +11001037 BUG_ON(vb->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001038 spin_unlock(&vb->lock);
1039 free_vmap_block(vb);
1040 } else
1041 spin_unlock(&vb->lock);
1042}
1043
1044/**
1045 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1046 *
1047 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1048 * to amortize TLB flushing overheads. What this means is that any page you
1049 * have now, may, in a former life, have been mapped into kernel virtual
1050 * address by the vmap layer and so there might be some CPUs with TLB entries
1051 * still referencing that page (additional to the regular 1:1 kernel mapping).
1052 *
1053 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1054 * be sure that none of the pages we have control over will have any aliases
1055 * from the vmap layer.
1056 */
1057void vm_unmap_aliases(void)
1058{
1059 unsigned long start = ULONG_MAX, end = 0;
1060 int cpu;
1061 int flush = 0;
1062
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001063 if (unlikely(!vmap_initialized))
1064 return;
1065
Nick Piggindb64fe02008-10-18 20:27:03 -07001066 for_each_possible_cpu(cpu) {
1067 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1068 struct vmap_block *vb;
1069
1070 rcu_read_lock();
1071 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1072 int i;
1073
1074 spin_lock(&vb->lock);
1075 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1076 while (i < VMAP_BBMAP_BITS) {
1077 unsigned long s, e;
1078 int j;
1079 j = find_next_zero_bit(vb->dirty_map,
1080 VMAP_BBMAP_BITS, i);
1081
1082 s = vb->va->va_start + (i << PAGE_SHIFT);
1083 e = vb->va->va_start + (j << PAGE_SHIFT);
Nick Piggindb64fe02008-10-18 20:27:03 -07001084 flush = 1;
1085
1086 if (s < start)
1087 start = s;
1088 if (e > end)
1089 end = e;
1090
1091 i = j;
1092 i = find_next_bit(vb->dirty_map,
1093 VMAP_BBMAP_BITS, i);
1094 }
1095 spin_unlock(&vb->lock);
1096 }
1097 rcu_read_unlock();
1098 }
1099
1100 __purge_vmap_area_lazy(&start, &end, 1, flush);
1101}
1102EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1103
1104/**
1105 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1106 * @mem: the pointer returned by vm_map_ram
1107 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1108 */
1109void vm_unmap_ram(const void *mem, unsigned int count)
1110{
1111 unsigned long size = count << PAGE_SHIFT;
1112 unsigned long addr = (unsigned long)mem;
1113
1114 BUG_ON(!addr);
1115 BUG_ON(addr < VMALLOC_START);
1116 BUG_ON(addr > VMALLOC_END);
1117 BUG_ON(addr & (PAGE_SIZE-1));
1118
1119 debug_check_no_locks_freed(mem, size);
Nick Piggincd528582009-01-06 14:39:20 -08001120 vmap_debug_free_range(addr, addr+size);
Nick Piggindb64fe02008-10-18 20:27:03 -07001121
1122 if (likely(count <= VMAP_MAX_ALLOC))
1123 vb_free(mem, size);
1124 else
1125 free_unmap_vmap_area_addr(addr);
1126}
1127EXPORT_SYMBOL(vm_unmap_ram);
1128
1129/**
1130 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1131 * @pages: an array of pointers to the pages to be mapped
1132 * @count: number of pages
1133 * @node: prefer to allocate data structures on this node
1134 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
Randy Dunlape99c97a2008-10-29 14:01:09 -07001135 *
1136 * Returns: a pointer to the address that has been mapped, or %NULL on failure
Nick Piggindb64fe02008-10-18 20:27:03 -07001137 */
1138void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1139{
1140 unsigned long size = count << PAGE_SHIFT;
1141 unsigned long addr;
1142 void *mem;
1143
1144 if (likely(count <= VMAP_MAX_ALLOC)) {
1145 mem = vb_alloc(size, GFP_KERNEL);
1146 if (IS_ERR(mem))
1147 return NULL;
1148 addr = (unsigned long)mem;
1149 } else {
1150 struct vmap_area *va;
1151 va = alloc_vmap_area(size, PAGE_SIZE,
1152 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1153 if (IS_ERR(va))
1154 return NULL;
1155
1156 addr = va->va_start;
1157 mem = (void *)addr;
1158 }
1159 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1160 vm_unmap_ram(mem, count);
1161 return NULL;
1162 }
1163 return mem;
1164}
1165EXPORT_SYMBOL(vm_map_ram);
Neeti Desaic278c942013-06-10 17:14:21 -07001166/**
1167 * vm_area_check_early - check if vmap area is already mapped
1168 * @vm: vm_struct to be checked
1169 *
1170 * This function is used to check if the vmap area has been
1171 * mapped already. @vm->addr, @vm->size and @vm->flags should
1172 * contain proper values.
1173 *
1174 */
1175int __init vm_area_check_early(struct vm_struct *vm)
1176{
1177 struct vm_struct *tmp, **p;
Nick Piggindb64fe02008-10-18 20:27:03 -07001178
Neeti Desaic278c942013-06-10 17:14:21 -07001179 BUG_ON(vmap_initialized);
1180 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1181 if (tmp->addr >= vm->addr) {
1182 if (tmp->addr < vm->addr + vm->size)
1183 return 1;
1184 } else {
1185 if (tmp->addr + tmp->size > vm->addr)
1186 return 1;
1187 }
1188 }
1189 return 0;
1190}
Tejun Heof0aa6612009-02-20 16:29:08 +09001191/**
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001192 * vm_area_add_early - add vmap area early during boot
1193 * @vm: vm_struct to add
1194 *
1195 * This function is used to add fixed kernel vm area to vmlist before
1196 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1197 * should contain proper values and the other fields should be zero.
1198 *
1199 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1200 */
1201void __init vm_area_add_early(struct vm_struct *vm)
1202{
1203 struct vm_struct *tmp, **p;
1204
1205 BUG_ON(vmap_initialized);
1206 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1207 if (tmp->addr >= vm->addr) {
1208 BUG_ON(tmp->addr < vm->addr + vm->size);
1209 break;
1210 } else
1211 BUG_ON(tmp->addr + tmp->size > vm->addr);
1212 }
1213 vm->next = *p;
1214 *p = vm;
1215}
1216
1217/**
Tejun Heof0aa6612009-02-20 16:29:08 +09001218 * vm_area_register_early - register vmap area early during boot
1219 * @vm: vm_struct to register
Tejun Heoc0c0a292009-02-24 11:57:21 +09001220 * @align: requested alignment
Tejun Heof0aa6612009-02-20 16:29:08 +09001221 *
1222 * This function is used to register kernel vm area before
1223 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1224 * proper values on entry and other fields should be zero. On return,
1225 * vm->addr contains the allocated address.
1226 *
1227 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1228 */
Tejun Heoc0c0a292009-02-24 11:57:21 +09001229void __init vm_area_register_early(struct vm_struct *vm, size_t align)
Tejun Heof0aa6612009-02-20 16:29:08 +09001230{
1231 static size_t vm_init_off __initdata;
Tejun Heoc0c0a292009-02-24 11:57:21 +09001232 unsigned long addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001233
Tejun Heoc0c0a292009-02-24 11:57:21 +09001234 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1235 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1236
1237 vm->addr = (void *)addr;
Tejun Heof0aa6612009-02-20 16:29:08 +09001238
Nicolas Pitrebe9b7332011-08-25 00:24:21 -04001239 vm_area_add_early(vm);
Tejun Heof0aa6612009-02-20 16:29:08 +09001240}
1241
Nick Piggindb64fe02008-10-18 20:27:03 -07001242void __init vmalloc_init(void)
1243{
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001244 struct vmap_area *va;
1245 struct vm_struct *tmp;
Nick Piggindb64fe02008-10-18 20:27:03 -07001246 int i;
1247
1248 for_each_possible_cpu(i) {
1249 struct vmap_block_queue *vbq;
1250
1251 vbq = &per_cpu(vmap_block_queue, i);
1252 spin_lock_init(&vbq->lock);
1253 INIT_LIST_HEAD(&vbq->free);
Nick Piggindb64fe02008-10-18 20:27:03 -07001254 }
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001255
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001256 /* Import existing vmlist entries. */
1257 for (tmp = vmlist; tmp; tmp = tmp->next) {
Pekka Enberg43ebdac2009-05-25 15:01:35 +03001258 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
KyongHofa002622012-05-29 15:06:49 -07001259 va->flags = VM_VM_AREA;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001260 va->va_start = (unsigned long)tmp->addr;
1261 va->va_end = va->va_start + tmp->size;
KyongHofa002622012-05-29 15:06:49 -07001262 va->vm = tmp;
Ivan Kokshaysky822c18f2009-01-15 13:50:48 -08001263 __insert_vmap_area(va);
1264 }
Tejun Heoca23e402009-08-14 15:00:52 +09001265
1266 vmap_area_pcpu_hole = VMALLOC_END;
1267
Jeremy Fitzhardinge9b463332008-10-28 19:22:34 +11001268 vmap_initialized = true;
Nick Piggindb64fe02008-10-18 20:27:03 -07001269}
1270
Tejun Heo8fc48982009-02-20 16:29:08 +09001271/**
1272 * map_kernel_range_noflush - map kernel VM area with the specified pages
1273 * @addr: start of the VM area to map
1274 * @size: size of the VM area to map
1275 * @prot: page protection flags to use
1276 * @pages: pages to map
1277 *
1278 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1279 * specify should have been allocated using get_vm_area() and its
1280 * friends.
1281 *
1282 * NOTE:
1283 * This function does NOT do any cache flushing. The caller is
1284 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1285 * before calling this function.
1286 *
1287 * RETURNS:
1288 * The number of pages mapped on success, -errno on failure.
1289 */
1290int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1291 pgprot_t prot, struct page **pages)
1292{
1293 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1294}
1295
1296/**
1297 * unmap_kernel_range_noflush - unmap kernel VM area
1298 * @addr: start of the VM area to unmap
1299 * @size: size of the VM area to unmap
1300 *
1301 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1302 * specify should have been allocated using get_vm_area() and its
1303 * friends.
1304 *
1305 * NOTE:
1306 * This function does NOT do any cache flushing. The caller is
1307 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1308 * before calling this function and flush_tlb_kernel_range() after.
1309 */
1310void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1311{
1312 vunmap_page_range(addr, addr + size);
1313}
Huang Ying81e88fd2011-01-12 14:44:55 +08001314EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
Tejun Heo8fc48982009-02-20 16:29:08 +09001315
1316/**
1317 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1318 * @addr: start of the VM area to unmap
1319 * @size: size of the VM area to unmap
1320 *
1321 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1322 * the unmapping and tlb after.
1323 */
Nick Piggindb64fe02008-10-18 20:27:03 -07001324void unmap_kernel_range(unsigned long addr, unsigned long size)
1325{
1326 unsigned long end = addr + size;
Tejun Heof6fcba72009-02-20 15:38:48 -08001327
1328 flush_cache_vunmap(addr, end);
Nick Piggindb64fe02008-10-18 20:27:03 -07001329 vunmap_page_range(addr, end);
1330 flush_tlb_kernel_range(addr, end);
1331}
1332
1333int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1334{
1335 unsigned long addr = (unsigned long)area->addr;
1336 unsigned long end = addr + area->size - PAGE_SIZE;
1337 int err;
1338
1339 err = vmap_page_range(addr, end, prot, *pages);
1340 if (err > 0) {
1341 *pages += err;
1342 err = 0;
1343 }
1344
1345 return err;
1346}
1347EXPORT_SYMBOL_GPL(map_vm_area);
1348
1349/*** Old vmalloc interfaces ***/
1350DEFINE_RWLOCK(vmlist_lock);
1351struct vm_struct *vmlist;
1352
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001353static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001354 unsigned long flags, const void *caller)
Tejun Heocf88c792009-08-14 15:00:52 +09001355{
Tejun Heocf88c792009-08-14 15:00:52 +09001356 vm->flags = flags;
1357 vm->addr = (void *)va->va_start;
1358 vm->size = va->va_end - va->va_start;
1359 vm->caller = caller;
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001360 va->vm = vm;
Tejun Heocf88c792009-08-14 15:00:52 +09001361 va->flags |= VM_VM_AREA;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001362}
Tejun Heocf88c792009-08-14 15:00:52 +09001363
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001364static void insert_vmalloc_vmlist(struct vm_struct *vm)
1365{
1366 struct vm_struct *tmp, **p;
1367
1368 vm->flags &= ~VM_UNLIST;
Tejun Heocf88c792009-08-14 15:00:52 +09001369 write_lock(&vmlist_lock);
1370 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1371 if (tmp->addr >= vm->addr)
1372 break;
1373 }
1374 vm->next = *p;
1375 *p = vm;
1376 write_unlock(&vmlist_lock);
1377}
1378
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001379static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001380 unsigned long flags, const void *caller)
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001381{
1382 setup_vmalloc_vm(vm, va, flags, caller);
1383 insert_vmalloc_vmlist(vm);
1384}
1385
Nick Piggindb64fe02008-10-18 20:27:03 -07001386static struct vm_struct *__get_vm_area_node(unsigned long size,
David Miller2dca6992009-09-21 12:22:34 -07001387 unsigned long align, unsigned long flags, unsigned long start,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001388 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
Nick Piggindb64fe02008-10-18 20:27:03 -07001389{
Kautuk Consul00065262011-12-19 17:12:04 -08001390 struct vmap_area *va;
Nick Piggindb64fe02008-10-18 20:27:03 -07001391 struct vm_struct *area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Giridhar Pemmasani52fd24c2006-10-28 10:38:34 -07001393 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (flags & VM_IOREMAP) {
1395 int bit = fls(size);
1396
1397 if (bit > IOREMAP_MAX_ORDER)
1398 bit = IOREMAP_MAX_ORDER;
1399 else if (bit < PAGE_SHIFT)
1400 bit = PAGE_SHIFT;
1401
1402 align = 1ul << bit;
1403 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 size = PAGE_ALIGN(size);
OGAWA Hirofumi31be8302006-11-16 01:19:29 -08001406 if (unlikely(!size))
1407 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Tejun Heocf88c792009-08-14 15:00:52 +09001409 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (unlikely(!area))
1411 return NULL;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * We always allocate a guard page.
1415 */
1416 size += PAGE_SIZE;
1417
Nick Piggindb64fe02008-10-18 20:27:03 -07001418 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1419 if (IS_ERR(va)) {
1420 kfree(area);
1421 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001424 /*
1425 * When this function is called from __vmalloc_node_range,
1426 * we do not add vm_struct to vmlist here to avoid
1427 * accessing uninitialized members of vm_struct such as
1428 * pages and nr_pages fields. They will be set later.
1429 * To distinguish it from others, we use a VM_UNLIST flag.
1430 */
1431 if (flags & VM_UNLIST)
1432 setup_vmalloc_vm(area, va, flags, caller);
1433 else
1434 insert_vmalloc_vm(area, va, flags, caller);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return area;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Christoph Lameter930fc452005-10-29 18:15:41 -07001439struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1440 unsigned long start, unsigned long end)
1441{
David Miller2dca6992009-09-21 12:22:34 -07001442 return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07001443 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001444}
Rusty Russell5992b6d2007-07-19 01:49:21 -07001445EXPORT_SYMBOL_GPL(__get_vm_area);
Christoph Lameter930fc452005-10-29 18:15:41 -07001446
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001447struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1448 unsigned long start, unsigned long end,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001449 const void *caller)
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001450{
David Miller2dca6992009-09-21 12:22:34 -07001451 return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
Benjamin Herrenschmidtc2968612009-02-18 14:48:12 -08001452 caller);
1453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455/**
Simon Arlott183ff222007-10-20 01:27:18 +02001456 * get_vm_area - reserve a contiguous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 * @size: size of the area
1458 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1459 *
1460 * Search an area of @size in the kernel virtual mapping area,
1461 * and reserved it for out purposes. Returns the area descriptor
1462 * on success or %NULL on failure.
1463 */
1464struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1465{
Neeti Desaie6ccd032013-06-14 17:39:33 -07001466#ifdef CONFIG_ENABLE_VMALLOC_SAVING
1467 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
1468 -1, GFP_KERNEL, __builtin_return_address(0));
1469#else
David Miller2dca6992009-09-21 12:22:34 -07001470 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
Christoph Lameter23016962008-04-28 02:12:42 -07001471 -1, GFP_KERNEL, __builtin_return_address(0));
Neeti Desaie6ccd032013-06-14 17:39:33 -07001472#endif
1473
Christoph Lameter23016962008-04-28 02:12:42 -07001474}
1475
1476struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001477 const void *caller)
Christoph Lameter23016962008-04-28 02:12:42 -07001478{
Neeti Desaie6ccd032013-06-14 17:39:33 -07001479#ifdef CONFIG_ENABLE_VMALLOC_SAVING
1480 return __get_vm_area_node(size, 1, flags, PAGE_OFFSET, VMALLOC_END,
Christoph Lameter23016962008-04-28 02:12:42 -07001481 -1, GFP_KERNEL, caller);
Neeti Desaie6ccd032013-06-14 17:39:33 -07001482#else
1483 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
1484 -1, GFP_KERNEL, __builtin_return_address(0));
1485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Marek Szyprowskicb4d7a62012-07-30 09:11:33 +02001488/**
1489 * find_vm_area - find a continuous kernel virtual area
1490 * @addr: base address
1491 *
1492 * Search for the kernel VM area starting at @addr, and return it.
1493 * It is up to the caller to do all required locking to keep the returned
1494 * pointer valid.
1495 */
1496struct vm_struct *find_vm_area(const void *addr)
Nick Piggin83342312006-06-23 02:03:20 -07001497{
Nick Piggindb64fe02008-10-18 20:27:03 -07001498 struct vmap_area *va;
Nick Piggin83342312006-06-23 02:03:20 -07001499
Nick Piggindb64fe02008-10-18 20:27:03 -07001500 va = find_vmap_area((unsigned long)addr);
1501 if (va && va->flags & VM_VM_AREA)
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001502 return va->vm;
Nick Piggin83342312006-06-23 02:03:20 -07001503
Andi Kleen7856dfe2005-05-20 14:27:57 -07001504 return NULL;
Andi Kleen7856dfe2005-05-20 14:27:57 -07001505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/**
Simon Arlott183ff222007-10-20 01:27:18 +02001508 * remove_vm_area - find and remove a continuous kernel virtual area
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 * @addr: base address
1510 *
1511 * Search for the kernel VM area starting at @addr, and remove it.
1512 * This function returns the found VM area, but using it is NOT safe
Andi Kleen7856dfe2005-05-20 14:27:57 -07001513 * on SMP machines, except for its size or flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001515struct vm_struct *remove_vm_area(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Nick Piggindb64fe02008-10-18 20:27:03 -07001517 struct vmap_area *va;
1518
1519 va = find_vmap_area((unsigned long)addr);
1520 if (va && va->flags & VM_VM_AREA) {
Minchan Kimdb1aeca2012-01-10 15:08:39 -08001521 struct vm_struct *vm = va->vm;
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001522
1523 if (!(vm->flags & VM_UNLIST)) {
1524 struct vm_struct *tmp, **p;
1525 /*
1526 * remove from list and disallow access to
1527 * this vm_struct before unmap. (address range
1528 * confliction is maintained by vmap.)
1529 */
1530 write_lock(&vmlist_lock);
1531 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1532 ;
1533 *p = tmp->next;
1534 write_unlock(&vmlist_lock);
1535 }
Nick Piggindb64fe02008-10-18 20:27:03 -07001536
KAMEZAWA Hiroyukidd32c272009-09-21 17:02:32 -07001537 vmap_debug_free_range(va->va_start, va->va_end);
1538 free_unmap_vmap_area(va);
1539 vm->size -= PAGE_SIZE;
1540
Nick Piggindb64fe02008-10-18 20:27:03 -07001541 return vm;
1542 }
1543 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001546static void __vunmap(const void *addr, int deallocate_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct vm_struct *area;
1549
1550 if (!addr)
1551 return;
1552
1553 if ((PAGE_SIZE-1) & (unsigned long)addr) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001554 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return;
1556 }
1557
1558 area = remove_vm_area(addr);
1559 if (unlikely(!area)) {
Arjan van de Ven4c8573e2008-07-25 19:45:37 -07001560 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return;
1563 }
1564
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001565 debug_check_no_locks_freed(addr, area->size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001566 debug_check_no_obj_freed(addr, area->size);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (deallocate_pages) {
1569 int i;
1570
1571 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001572 struct page *page = area->pages[i];
1573
1574 BUG_ON(!page);
1575 __free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001578 if (area->flags & VM_VPAGES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 vfree(area->pages);
1580 else
1581 kfree(area->pages);
1582 }
1583
1584 kfree(area);
1585 return;
1586}
1587
1588/**
1589 * vfree - release memory allocated by vmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * @addr: memory base address
1591 *
Simon Arlott183ff222007-10-20 01:27:18 +02001592 * Free the virtually continuous memory area starting at @addr, as
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001593 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1594 * NULL, no operation is performed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001596 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001598void vfree(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 BUG_ON(in_interrupt());
Catalin Marinas89219d32009-06-11 13:23:19 +01001601
1602 kmemleak_free(addr);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 __vunmap(addr, 1);
1605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606EXPORT_SYMBOL(vfree);
1607
1608/**
1609 * vunmap - release virtual mapping obtained by vmap()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * @addr: memory base address
1611 *
1612 * Free the virtually contiguous memory area starting at @addr,
1613 * which was created from the page array passed to vmap().
1614 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07001615 * Must not be called in interrupt context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Christoph Lameterb3bdda02008-02-04 22:28:32 -08001617void vunmap(const void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 BUG_ON(in_interrupt());
Peter Zijlstra34754b62009-02-25 16:04:03 +01001620 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 __vunmap(addr, 0);
1622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623EXPORT_SYMBOL(vunmap);
1624
1625/**
1626 * vmap - map an array of pages into virtually contiguous space
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * @pages: array of page pointers
1628 * @count: number of pages to map
1629 * @flags: vm_area->flags
1630 * @prot: page protection for the mapping
1631 *
1632 * Maps @count pages from @pages into contiguous kernel virtual
1633 * space.
1634 */
1635void *vmap(struct page **pages, unsigned int count,
1636 unsigned long flags, pgprot_t prot)
1637{
1638 struct vm_struct *area;
1639
Peter Zijlstra34754b62009-02-25 16:04:03 +01001640 might_sleep();
1641
Jan Beulich44813742009-09-21 17:03:05 -07001642 if (count > totalram_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return NULL;
1644
Christoph Lameter23016962008-04-28 02:12:42 -07001645 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1646 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (!area)
1648 return NULL;
Christoph Lameter23016962008-04-28 02:12:42 -07001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (map_vm_area(area, prot, &pages)) {
1651 vunmap(area->addr);
1652 return NULL;
1653 }
1654
1655 return area->addr;
1656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657EXPORT_SYMBOL(vmap);
1658
David Miller2dca6992009-09-21 12:22:34 -07001659static void *__vmalloc_node(unsigned long size, unsigned long align,
1660 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001661 int node, const void *caller);
Adrian Bunke31d9eb2008-02-04 22:29:09 -08001662static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001663 pgprot_t prot, int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Dave Hansen22943ab2011-05-24 17:12:18 -07001665 const int order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct page **pages;
1667 unsigned int nr_pages, array_size, i;
Jan Beulich976d6df2009-12-14 17:58:39 -08001668 gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1671 array_size = (nr_pages * sizeof(struct page *));
1672
1673 area->nr_pages = nr_pages;
1674 /* Please note that the recursion is strictly bounded. */
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001675 if (array_size > PAGE_SIZE) {
Jan Beulich976d6df2009-12-14 17:58:39 -08001676 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
Christoph Lameter23016962008-04-28 02:12:42 -07001677 PAGE_KERNEL, node, caller);
Jan Kiszka8757d5f2006-07-14 00:23:56 -07001678 area->flags |= VM_VPAGES;
Andrew Morton286e1ea2006-10-17 00:09:57 -07001679 } else {
Jan Beulich976d6df2009-12-14 17:58:39 -08001680 pages = kmalloc_node(array_size, nested_gfp, node);
Andrew Morton286e1ea2006-10-17 00:09:57 -07001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 area->pages = pages;
Christoph Lameter23016962008-04-28 02:12:42 -07001683 area->caller = caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (!area->pages) {
1685 remove_vm_area(area->addr);
1686 kfree(area);
1687 return NULL;
1688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 for (i = 0; i < area->nr_pages; i++) {
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001691 struct page *page;
Dave Hansen22943ab2011-05-24 17:12:18 -07001692 gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001693
Christoph Lameter930fc452005-10-29 18:15:41 -07001694 if (node < 0)
Dave Hansen22943ab2011-05-24 17:12:18 -07001695 page = alloc_page(tmp_mask);
Christoph Lameter930fc452005-10-29 18:15:41 -07001696 else
Dave Hansen22943ab2011-05-24 17:12:18 -07001697 page = alloc_pages_node(node, tmp_mask, order);
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001698
1699 if (unlikely(!page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Successfully allocated i pages, free them in __vunmap() */
1701 area->nr_pages = i;
1702 goto fail;
1703 }
Christoph Lameterbf53d6f2008-02-04 22:28:34 -08001704 area->pages[i] = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
1707 if (map_vm_area(area, prot, &pages))
1708 goto fail;
1709 return area->addr;
1710
1711fail:
Joe Perches3ee9a4f2011-10-31 17:08:35 -07001712 warn_alloc_failed(gfp_mask, order,
1713 "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
Dave Hansen22943ab2011-05-24 17:12:18 -07001714 (area->nr_pages*PAGE_SIZE), area->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 vfree(area->addr);
1716 return NULL;
1717}
1718
David Rientjesd0a21262011-01-13 15:46:02 -08001719/**
1720 * __vmalloc_node_range - allocate virtually contiguous memory
1721 * @size: allocation size
1722 * @align: desired alignment
1723 * @start: vm area range start
1724 * @end: vm area range end
1725 * @gfp_mask: flags for the page level allocator
1726 * @prot: protection mask for the allocated pages
1727 * @node: node to use for allocation or -1
1728 * @caller: caller's return address
1729 *
1730 * Allocate enough pages to cover @size from the page level
1731 * allocator with @gfp_mask flags. Map them into contiguous
1732 * kernel virtual space, using a pagetable protection of @prot.
1733 */
1734void *__vmalloc_node_range(unsigned long size, unsigned long align,
1735 unsigned long start, unsigned long end, gfp_t gfp_mask,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001736 pgprot_t prot, int node, const void *caller)
Christoph Lameter930fc452005-10-29 18:15:41 -07001737{
David Rientjesd0a21262011-01-13 15:46:02 -08001738 struct vm_struct *area;
1739 void *addr;
1740 unsigned long real_size = size;
Jack Cheung59f9f1c2011-11-29 16:52:49 -08001741#ifdef CONFIG_FIX_MOVABLE_ZONE
1742 unsigned long total_pages = total_unmovable_pages;
1743#else
1744 unsigned long total_pages = totalram_pages;
1745#endif
David Rientjesd0a21262011-01-13 15:46:02 -08001746
1747 size = PAGE_ALIGN(size);
Jack Cheung59f9f1c2011-11-29 16:52:49 -08001748 if (!size || (size >> PAGE_SHIFT) > total_pages)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001749 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001750
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001751 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
1752 start, end, node, gfp_mask, caller);
David Rientjesd0a21262011-01-13 15:46:02 -08001753 if (!area)
Joe Perchesde7d2b52011-10-31 17:08:48 -07001754 goto fail;
David Rientjesd0a21262011-01-13 15:46:02 -08001755
1756 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
Mel Gorman1368edf2011-12-08 14:34:30 -08001757 if (!addr)
1758 return NULL;
Catalin Marinas89219d32009-06-11 13:23:19 +01001759
1760 /*
Mitsuo Hayasakaf5252e02011-10-31 17:08:13 -07001761 * In this function, newly allocated vm_struct is not added
1762 * to vmlist at __get_vm_area_node(). so, it is added here.
1763 */
1764 insert_vmalloc_vmlist(area);
1765
1766 /*
Catalin Marinas9504eeb2013-11-12 15:07:45 -08001767 * A ref_count = 2 is needed because vm_struct allocated in
1768 * __get_vm_area_node() contains a reference to the virtual address of
1769 * the vmalloc'ed block.
Catalin Marinas89219d32009-06-11 13:23:19 +01001770 */
Catalin Marinas9504eeb2013-11-12 15:07:45 -08001771 kmemleak_alloc(addr, real_size, 2, gfp_mask);
Catalin Marinas89219d32009-06-11 13:23:19 +01001772
1773 return addr;
Joe Perchesde7d2b52011-10-31 17:08:48 -07001774
1775fail:
1776 warn_alloc_failed(gfp_mask, 0,
1777 "vmalloc: allocation failure: %lu bytes\n",
1778 real_size);
1779 return NULL;
Christoph Lameter930fc452005-10-29 18:15:41 -07001780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001783 * __vmalloc_node - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * @size: allocation size
David Miller2dca6992009-09-21 12:22:34 -07001785 * @align: desired alignment
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * @gfp_mask: flags for the page level allocator
1787 * @prot: protection mask for the allocated pages
Randy Dunlapd44e0782005-11-07 01:01:10 -08001788 * @node: node to use for allocation or -1
Randy Dunlapc85d1942008-05-01 04:34:48 -07001789 * @caller: caller's return address
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 *
1791 * Allocate enough pages to cover @size from the page level
1792 * allocator with @gfp_mask flags. Map them into contiguous
1793 * kernel virtual space, using a pagetable protection of @prot.
1794 */
David Miller2dca6992009-09-21 12:22:34 -07001795static void *__vmalloc_node(unsigned long size, unsigned long align,
1796 gfp_t gfp_mask, pgprot_t prot,
Marek Szyprowskiacf4e612012-04-13 12:32:09 +02001797 int node, const void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
David Rientjesd0a21262011-01-13 15:46:02 -08001799 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1800 gfp_mask, prot, node, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Christoph Lameter930fc452005-10-29 18:15:41 -07001803void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1804{
David Miller2dca6992009-09-21 12:22:34 -07001805 return __vmalloc_node(size, 1, gfp_mask, prot, -1,
Christoph Lameter23016962008-04-28 02:12:42 -07001806 __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808EXPORT_SYMBOL(__vmalloc);
1809
Dave Younge1ca7782010-10-26 14:22:06 -07001810static inline void *__vmalloc_node_flags(unsigned long size,
1811 int node, gfp_t flags)
1812{
1813 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1814 node, __builtin_return_address(0));
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/**
1818 * vmalloc - allocate virtually contiguous memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * @size: allocation size
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * Allocate enough pages to cover @size from the page level
1821 * allocator and map them into contiguous kernel virtual space.
1822 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001823 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 * use __vmalloc() instead.
1825 */
1826void *vmalloc(unsigned long size)
1827{
Dave Younge1ca7782010-10-26 14:22:06 -07001828 return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830EXPORT_SYMBOL(vmalloc);
1831
Christoph Lameter930fc452005-10-29 18:15:41 -07001832/**
Dave Younge1ca7782010-10-26 14:22:06 -07001833 * vzalloc - allocate virtually contiguous memory with zero fill
1834 * @size: allocation size
1835 * Allocate enough pages to cover @size from the page level
1836 * allocator and map them into contiguous kernel virtual space.
1837 * The memory allocated is set to zero.
1838 *
1839 * For tight control over page level allocator and protection flags
1840 * use __vmalloc() instead.
1841 */
1842void *vzalloc(unsigned long size)
1843{
1844 return __vmalloc_node_flags(size, -1,
1845 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1846}
1847EXPORT_SYMBOL(vzalloc);
1848
1849/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001850 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1851 * @size: allocation size
Nick Piggin83342312006-06-23 02:03:20 -07001852 *
Rolf Eike Beeread04082006-09-27 01:50:13 -07001853 * The resulting memory area is zeroed so it can be mapped to userspace
1854 * without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001855 */
1856void *vmalloc_user(unsigned long size)
1857{
1858 struct vm_struct *area;
1859 void *ret;
1860
David Miller2dca6992009-09-21 12:22:34 -07001861 ret = __vmalloc_node(size, SHMLBA,
1862 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
Glauber Costa84877842009-01-06 14:39:19 -08001863 PAGE_KERNEL, -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001864 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001865 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001866 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001867 }
Nick Piggin83342312006-06-23 02:03:20 -07001868 return ret;
1869}
1870EXPORT_SYMBOL(vmalloc_user);
1871
1872/**
Christoph Lameter930fc452005-10-29 18:15:41 -07001873 * vmalloc_node - allocate memory on a specific node
Christoph Lameter930fc452005-10-29 18:15:41 -07001874 * @size: allocation size
Randy Dunlapd44e0782005-11-07 01:01:10 -08001875 * @node: numa node
Christoph Lameter930fc452005-10-29 18:15:41 -07001876 *
1877 * Allocate enough pages to cover @size from the page level
1878 * allocator and map them into contiguous kernel virtual space.
1879 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001880 * For tight control over page level allocator and protection flags
Christoph Lameter930fc452005-10-29 18:15:41 -07001881 * use __vmalloc() instead.
1882 */
1883void *vmalloc_node(unsigned long size, int node)
1884{
David Miller2dca6992009-09-21 12:22:34 -07001885 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
Christoph Lameter23016962008-04-28 02:12:42 -07001886 node, __builtin_return_address(0));
Christoph Lameter930fc452005-10-29 18:15:41 -07001887}
1888EXPORT_SYMBOL(vmalloc_node);
1889
Dave Younge1ca7782010-10-26 14:22:06 -07001890/**
1891 * vzalloc_node - allocate memory on a specific node with zero fill
1892 * @size: allocation size
1893 * @node: numa node
1894 *
1895 * Allocate enough pages to cover @size from the page level
1896 * allocator and map them into contiguous kernel virtual space.
1897 * The memory allocated is set to zero.
1898 *
1899 * For tight control over page level allocator and protection flags
1900 * use __vmalloc_node() instead.
1901 */
1902void *vzalloc_node(unsigned long size, int node)
1903{
1904 return __vmalloc_node_flags(size, node,
1905 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1906}
1907EXPORT_SYMBOL(vzalloc_node);
1908
Pavel Pisa4dc3b162005-05-01 08:59:25 -07001909#ifndef PAGE_KERNEL_EXEC
1910# define PAGE_KERNEL_EXEC PAGE_KERNEL
1911#endif
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913/**
1914 * vmalloc_exec - allocate virtually contiguous, executable memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * @size: allocation size
1916 *
1917 * Kernel-internal function to allocate enough pages to cover @size
1918 * the page level allocator and map them into contiguous and
1919 * executable kernel virtual space.
1920 *
Michael Opdenackerc1c88972006-10-03 23:21:02 +02001921 * For tight control over page level allocator and protection flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * use __vmalloc() instead.
1923 */
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925void *vmalloc_exec(unsigned long size)
1926{
David Miller2dca6992009-09-21 12:22:34 -07001927 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
Glauber Costa84877842009-01-06 14:39:19 -08001928 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001931#if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001932#define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001933#elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
Benjamin Herrenschmidt7ac674f2007-07-19 01:49:10 -07001934#define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
Andi Kleen0d08e0d2007-05-02 19:27:12 +02001935#else
1936#define GFP_VMALLOC32 GFP_KERNEL
1937#endif
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/**
1940 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 * @size: allocation size
1942 *
1943 * Allocate enough 32bit PA addressable pages to cover @size from the
1944 * page level allocator and map them into contiguous kernel virtual space.
1945 */
1946void *vmalloc_32(unsigned long size)
1947{
David Miller2dca6992009-09-21 12:22:34 -07001948 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
Glauber Costa84877842009-01-06 14:39:19 -08001949 -1, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951EXPORT_SYMBOL(vmalloc_32);
1952
Nick Piggin83342312006-06-23 02:03:20 -07001953/**
Rolf Eike Beeread04082006-09-27 01:50:13 -07001954 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
Nick Piggin83342312006-06-23 02:03:20 -07001955 * @size: allocation size
Rolf Eike Beeread04082006-09-27 01:50:13 -07001956 *
1957 * The resulting memory area is 32bit addressable and zeroed so it can be
1958 * mapped to userspace without leaking data.
Nick Piggin83342312006-06-23 02:03:20 -07001959 */
1960void *vmalloc_32_user(unsigned long size)
1961{
1962 struct vm_struct *area;
1963 void *ret;
1964
David Miller2dca6992009-09-21 12:22:34 -07001965 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
Glauber Costa84877842009-01-06 14:39:19 -08001966 -1, __builtin_return_address(0));
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001967 if (ret) {
Nick Piggindb64fe02008-10-18 20:27:03 -07001968 area = find_vm_area(ret);
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001969 area->flags |= VM_USERMAP;
Eric Dumazet2b4ac442006-11-10 12:27:48 -08001970 }
Nick Piggin83342312006-06-23 02:03:20 -07001971 return ret;
1972}
1973EXPORT_SYMBOL(vmalloc_32_user);
1974
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07001975/*
1976 * small helper routine , copy contents to buf from addr.
1977 * If the page is not present, fill zero.
1978 */
1979
1980static int aligned_vread(char *buf, char *addr, unsigned long count)
1981{
1982 struct page *p;
1983 int copied = 0;
1984
1985 while (count) {
1986 unsigned long offset, length;
1987
1988 offset = (unsigned long)addr & ~PAGE_MASK;
1989 length = PAGE_SIZE - offset;
1990 if (length > count)
1991 length = count;
1992 p = vmalloc_to_page(addr);
1993 /*
1994 * To do safe access to this _mapped_ area, we need
1995 * lock. But adding lock here means that we need to add
1996 * overhead of vmalloc()/vfree() calles for this _debug_
1997 * interface, rarely used. Instead of that, we'll use
1998 * kmap() and get small overhead in this access function.
1999 */
2000 if (p) {
2001 /*
2002 * we can expect USER0 is not used (see vread/vwrite's
2003 * function description)
2004 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002005 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002006 memcpy(buf, map + offset, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002007 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002008 } else
2009 memset(buf, 0, length);
2010
2011 addr += length;
2012 buf += length;
2013 copied += length;
2014 count -= length;
2015 }
2016 return copied;
2017}
2018
2019static int aligned_vwrite(char *buf, char *addr, unsigned long count)
2020{
2021 struct page *p;
2022 int copied = 0;
2023
2024 while (count) {
2025 unsigned long offset, length;
2026
2027 offset = (unsigned long)addr & ~PAGE_MASK;
2028 length = PAGE_SIZE - offset;
2029 if (length > count)
2030 length = count;
2031 p = vmalloc_to_page(addr);
2032 /*
2033 * To do safe access to this _mapped_ area, we need
2034 * lock. But adding lock here means that we need to add
2035 * overhead of vmalloc()/vfree() calles for this _debug_
2036 * interface, rarely used. Instead of that, we'll use
2037 * kmap() and get small overhead in this access function.
2038 */
2039 if (p) {
2040 /*
2041 * we can expect USER0 is not used (see vread/vwrite's
2042 * function description)
2043 */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002044 void *map = kmap_atomic(p);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002045 memcpy(map + offset, buf, length);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002046 kunmap_atomic(map);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002047 }
2048 addr += length;
2049 buf += length;
2050 copied += length;
2051 count -= length;
2052 }
2053 return copied;
2054}
2055
2056/**
2057 * vread() - read vmalloc area in a safe way.
2058 * @buf: buffer for reading data
2059 * @addr: vm address.
2060 * @count: number of bytes to be read.
2061 *
2062 * Returns # of bytes which addr and buf should be increased.
2063 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2064 * includes any intersect with alive vmalloc area.
2065 *
2066 * This function checks that addr is a valid vmalloc'ed area, and
2067 * copy data from that area to a given buffer. If the given memory range
2068 * of [addr...addr+count) includes some valid address, data is copied to
2069 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2070 * IOREMAP area is treated as memory hole and no copy is done.
2071 *
2072 * If [addr...addr+count) doesn't includes any intersects with alive
2073 * vm_struct area, returns 0.
2074 * @buf should be kernel's buffer. Because this function uses KM_USER0,
2075 * the caller should guarantee KM_USER0 is not used.
2076 *
2077 * Note: In usual ops, vread() is never necessary because the caller
2078 * should know vmalloc() area is valid and can use memcpy().
2079 * This is for routines which have to access vmalloc area without
2080 * any informaion, as /dev/kmem.
2081 *
2082 */
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084long vread(char *buf, char *addr, unsigned long count)
2085{
2086 struct vm_struct *tmp;
2087 char *vaddr, *buf_start = buf;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002088 unsigned long buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned long n;
2090
2091 /* Don't allow overflow */
2092 if ((unsigned long) addr + count < count)
2093 count = -(unsigned long) addr;
2094
2095 read_lock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002096 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 vaddr = (char *) tmp->addr;
2098 if (addr >= vaddr + tmp->size - PAGE_SIZE)
2099 continue;
2100 while (addr < vaddr) {
2101 if (count == 0)
2102 goto finished;
2103 *buf = '\0';
2104 buf++;
2105 addr++;
2106 count--;
2107 }
2108 n = vaddr + tmp->size - PAGE_SIZE - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002109 if (n > count)
2110 n = count;
2111 if (!(tmp->flags & VM_IOREMAP))
2112 aligned_vread(buf, addr, n);
2113 else /* IOREMAP area is treated as memory hole */
2114 memset(buf, 0, n);
2115 buf += n;
2116 addr += n;
2117 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119finished:
2120 read_unlock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002121
2122 if (buf == buf_start)
2123 return 0;
2124 /* zero-fill memory holes */
2125 if (buf != buf_start + buflen)
2126 memset(buf, 0, buflen - (buf - buf_start));
2127
2128 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002131/**
2132 * vwrite() - write vmalloc area in a safe way.
2133 * @buf: buffer for source data
2134 * @addr: vm address.
2135 * @count: number of bytes to be read.
2136 *
2137 * Returns # of bytes which addr and buf should be incresed.
2138 * (same number to @count).
2139 * If [addr...addr+count) doesn't includes any intersect with valid
2140 * vmalloc area, returns 0.
2141 *
2142 * This function checks that addr is a valid vmalloc'ed area, and
2143 * copy data from a buffer to the given addr. If specified range of
2144 * [addr...addr+count) includes some valid address, data is copied from
2145 * proper area of @buf. If there are memory holes, no copy to hole.
2146 * IOREMAP area is treated as memory hole and no copy is done.
2147 *
2148 * If [addr...addr+count) doesn't includes any intersects with alive
2149 * vm_struct area, returns 0.
2150 * @buf should be kernel's buffer. Because this function uses KM_USER0,
2151 * the caller should guarantee KM_USER0 is not used.
2152 *
2153 * Note: In usual ops, vwrite() is never necessary because the caller
2154 * should know vmalloc() area is valid and can use memcpy().
2155 * This is for routines which have to access vmalloc area without
2156 * any informaion, as /dev/kmem.
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002157 */
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159long vwrite(char *buf, char *addr, unsigned long count)
2160{
2161 struct vm_struct *tmp;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002162 char *vaddr;
2163 unsigned long n, buflen;
2164 int copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /* Don't allow overflow */
2167 if ((unsigned long) addr + count < count)
2168 count = -(unsigned long) addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002169 buflen = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 read_lock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002172 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 vaddr = (char *) tmp->addr;
2174 if (addr >= vaddr + tmp->size - PAGE_SIZE)
2175 continue;
2176 while (addr < vaddr) {
2177 if (count == 0)
2178 goto finished;
2179 buf++;
2180 addr++;
2181 count--;
2182 }
2183 n = vaddr + tmp->size - PAGE_SIZE - addr;
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002184 if (n > count)
2185 n = count;
2186 if (!(tmp->flags & VM_IOREMAP)) {
2187 aligned_vwrite(buf, addr, n);
2188 copied++;
2189 }
2190 buf += n;
2191 addr += n;
2192 count -= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194finished:
2195 read_unlock(&vmlist_lock);
KAMEZAWA Hiroyukid0107eb2009-09-21 17:02:34 -07002196 if (!copied)
2197 return 0;
2198 return buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
Nick Piggin83342312006-06-23 02:03:20 -07002200
2201/**
2202 * remap_vmalloc_range - map vmalloc pages to userspace
Nick Piggin83342312006-06-23 02:03:20 -07002203 * @vma: vma to cover (map full range of vma)
2204 * @addr: vmalloc memory
2205 * @pgoff: number of pages into addr before first page to map
Randy Dunlap76824862008-03-19 17:00:40 -07002206 *
2207 * Returns: 0 for success, -Exxx on failure
Nick Piggin83342312006-06-23 02:03:20 -07002208 *
2209 * This function checks that addr is a valid vmalloc'ed area, and
2210 * that it is big enough to cover the vma. Will return failure if
2211 * that criteria isn't met.
2212 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002213 * Similar to remap_pfn_range() (see mm/memory.c)
Nick Piggin83342312006-06-23 02:03:20 -07002214 */
2215int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2216 unsigned long pgoff)
2217{
2218 struct vm_struct *area;
2219 unsigned long uaddr = vma->vm_start;
2220 unsigned long usize = vma->vm_end - vma->vm_start;
Nick Piggin83342312006-06-23 02:03:20 -07002221
2222 if ((PAGE_SIZE-1) & (unsigned long)addr)
2223 return -EINVAL;
2224
Nick Piggindb64fe02008-10-18 20:27:03 -07002225 area = find_vm_area(addr);
Nick Piggin83342312006-06-23 02:03:20 -07002226 if (!area)
Nick Piggindb64fe02008-10-18 20:27:03 -07002227 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002228
2229 if (!(area->flags & VM_USERMAP))
Nick Piggindb64fe02008-10-18 20:27:03 -07002230 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002231
2232 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
Nick Piggindb64fe02008-10-18 20:27:03 -07002233 return -EINVAL;
Nick Piggin83342312006-06-23 02:03:20 -07002234
2235 addr += pgoff << PAGE_SHIFT;
2236 do {
2237 struct page *page = vmalloc_to_page(addr);
Nick Piggindb64fe02008-10-18 20:27:03 -07002238 int ret;
2239
Nick Piggin83342312006-06-23 02:03:20 -07002240 ret = vm_insert_page(vma, uaddr, page);
2241 if (ret)
2242 return ret;
2243
2244 uaddr += PAGE_SIZE;
2245 addr += PAGE_SIZE;
2246 usize -= PAGE_SIZE;
2247 } while (usize > 0);
2248
2249 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
2250 vma->vm_flags |= VM_RESERVED;
2251
Nick Piggindb64fe02008-10-18 20:27:03 -07002252 return 0;
Nick Piggin83342312006-06-23 02:03:20 -07002253}
2254EXPORT_SYMBOL(remap_vmalloc_range);
2255
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -07002256/*
2257 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2258 * have one.
2259 */
2260void __attribute__((weak)) vmalloc_sync_all(void)
2261{
2262}
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002263
2264
Martin Schwidefsky2f569af2008-02-08 04:22:04 -08002265static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002266{
David Vrabelcd129092011-09-29 16:53:32 +01002267 pte_t ***p = data;
2268
2269 if (p) {
2270 *(*p) = pte;
2271 (*p)++;
2272 }
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002273 return 0;
2274}
2275
2276/**
2277 * alloc_vm_area - allocate a range of kernel address space
2278 * @size: size of the area
David Vrabelcd129092011-09-29 16:53:32 +01002279 * @ptes: returns the PTEs for the address space
Randy Dunlap76824862008-03-19 17:00:40 -07002280 *
2281 * Returns: NULL on failure, vm_struct on success
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002282 *
2283 * This function reserves a range of kernel address space, and
2284 * allocates pagetables to map that range. No actual mappings
David Vrabelcd129092011-09-29 16:53:32 +01002285 * are created.
2286 *
2287 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2288 * allocated for the VM area are returned.
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002289 */
David Vrabelcd129092011-09-29 16:53:32 +01002290struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002291{
2292 struct vm_struct *area;
2293
Christoph Lameter23016962008-04-28 02:12:42 -07002294 area = get_vm_area_caller(size, VM_IOREMAP,
2295 __builtin_return_address(0));
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002296 if (area == NULL)
2297 return NULL;
2298
2299 /*
2300 * This ensures that page tables are constructed for this region
2301 * of kernel virtual address space and mapped into init_mm.
2302 */
2303 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
David Vrabelcd129092011-09-29 16:53:32 +01002304 size, f, ptes ? &ptes : NULL)) {
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002305 free_vm_area(area);
2306 return NULL;
2307 }
2308
David Vrabeld63c8a02011-09-14 16:22:02 -07002309 /*
2310 * If the allocated address space is passed to a hypercall
2311 * before being used then we cannot rely on a page fault to
2312 * trigger an update of the page tables. So sync all the page
2313 * tables here.
2314 */
2315 vmalloc_sync_all();
2316
Jeremy Fitzhardinge5f4352f2007-07-17 18:37:04 -07002317 return area;
2318}
2319EXPORT_SYMBOL_GPL(alloc_vm_area);
2320
2321void free_vm_area(struct vm_struct *area)
2322{
2323 struct vm_struct *ret;
2324 ret = remove_vm_area(area->addr);
2325 BUG_ON(ret != area);
2326 kfree(area);
2327}
2328EXPORT_SYMBOL_GPL(free_vm_area);
Christoph Lametera10aa572008-04-28 02:12:40 -07002329
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002330#ifdef CONFIG_SMP
Tejun Heoca23e402009-08-14 15:00:52 +09002331static struct vmap_area *node_to_va(struct rb_node *n)
2332{
2333 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2334}
2335
2336/**
2337 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2338 * @end: target address
2339 * @pnext: out arg for the next vmap_area
2340 * @pprev: out arg for the previous vmap_area
2341 *
2342 * Returns: %true if either or both of next and prev are found,
2343 * %false if no vmap_area exists
2344 *
2345 * Find vmap_areas end addresses of which enclose @end. ie. if not
2346 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2347 */
2348static bool pvm_find_next_prev(unsigned long end,
2349 struct vmap_area **pnext,
2350 struct vmap_area **pprev)
2351{
2352 struct rb_node *n = vmap_area_root.rb_node;
2353 struct vmap_area *va = NULL;
2354
2355 while (n) {
2356 va = rb_entry(n, struct vmap_area, rb_node);
2357 if (end < va->va_end)
2358 n = n->rb_left;
2359 else if (end > va->va_end)
2360 n = n->rb_right;
2361 else
2362 break;
2363 }
2364
2365 if (!va)
2366 return false;
2367
2368 if (va->va_end > end) {
2369 *pnext = va;
2370 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2371 } else {
2372 *pprev = va;
2373 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2374 }
2375 return true;
2376}
2377
2378/**
2379 * pvm_determine_end - find the highest aligned address between two vmap_areas
2380 * @pnext: in/out arg for the next vmap_area
2381 * @pprev: in/out arg for the previous vmap_area
2382 * @align: alignment
2383 *
2384 * Returns: determined end address
2385 *
2386 * Find the highest aligned address between *@pnext and *@pprev below
2387 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2388 * down address is between the end addresses of the two vmap_areas.
2389 *
2390 * Please note that the address returned by this function may fall
2391 * inside *@pnext vmap_area. The caller is responsible for checking
2392 * that.
2393 */
2394static unsigned long pvm_determine_end(struct vmap_area **pnext,
2395 struct vmap_area **pprev,
2396 unsigned long align)
2397{
2398 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2399 unsigned long addr;
2400
2401 if (*pnext)
2402 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2403 else
2404 addr = vmalloc_end;
2405
2406 while (*pprev && (*pprev)->va_end > addr) {
2407 *pnext = *pprev;
2408 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2409 }
2410
2411 return addr;
2412}
2413
2414/**
2415 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2416 * @offsets: array containing offset of each area
2417 * @sizes: array containing size of each area
2418 * @nr_vms: the number of areas to allocate
2419 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
Tejun Heoca23e402009-08-14 15:00:52 +09002420 *
2421 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2422 * vm_structs on success, %NULL on failure
2423 *
2424 * Percpu allocator wants to use congruent vm areas so that it can
2425 * maintain the offsets among percpu areas. This function allocates
David Rientjesec3f64f2011-01-13 15:46:01 -08002426 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2427 * be scattered pretty far, distance between two areas easily going up
2428 * to gigabytes. To avoid interacting with regular vmallocs, these
2429 * areas are allocated from top.
Tejun Heoca23e402009-08-14 15:00:52 +09002430 *
2431 * Despite its complicated look, this allocator is rather simple. It
2432 * does everything top-down and scans areas from the end looking for
2433 * matching slot. While scanning, if any of the areas overlaps with
2434 * existing vmap_area, the base address is pulled down to fit the
2435 * area. Scanning is repeated till all the areas fit and then all
2436 * necessary data structres are inserted and the result is returned.
2437 */
2438struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2439 const size_t *sizes, int nr_vms,
David Rientjesec3f64f2011-01-13 15:46:01 -08002440 size_t align)
Tejun Heoca23e402009-08-14 15:00:52 +09002441{
2442 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2443 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2444 struct vmap_area **vas, *prev, *next;
2445 struct vm_struct **vms;
2446 int area, area2, last_area, term_area;
2447 unsigned long base, start, end, last_end;
2448 bool purged = false;
2449
Tejun Heoca23e402009-08-14 15:00:52 +09002450 /* verify parameters and allocate data structures */
2451 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2452 for (last_area = 0, area = 0; area < nr_vms; area++) {
2453 start = offsets[area];
2454 end = start + sizes[area];
2455
2456 /* is everything aligned properly? */
2457 BUG_ON(!IS_ALIGNED(offsets[area], align));
2458 BUG_ON(!IS_ALIGNED(sizes[area], align));
2459
2460 /* detect the area with the highest address */
2461 if (start > offsets[last_area])
2462 last_area = area;
2463
2464 for (area2 = 0; area2 < nr_vms; area2++) {
2465 unsigned long start2 = offsets[area2];
2466 unsigned long end2 = start2 + sizes[area2];
2467
2468 if (area2 == area)
2469 continue;
2470
2471 BUG_ON(start2 >= start && start2 < end);
2472 BUG_ON(end2 <= end && end2 > start);
2473 }
2474 }
2475 last_end = offsets[last_area] + sizes[last_area];
2476
2477 if (vmalloc_end - vmalloc_start < last_end) {
2478 WARN_ON(true);
2479 return NULL;
2480 }
2481
David Rientjesec3f64f2011-01-13 15:46:01 -08002482 vms = kzalloc(sizeof(vms[0]) * nr_vms, GFP_KERNEL);
2483 vas = kzalloc(sizeof(vas[0]) * nr_vms, GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002484 if (!vas || !vms)
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002485 goto err_free2;
Tejun Heoca23e402009-08-14 15:00:52 +09002486
2487 for (area = 0; area < nr_vms; area++) {
David Rientjesec3f64f2011-01-13 15:46:01 -08002488 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2489 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
Tejun Heoca23e402009-08-14 15:00:52 +09002490 if (!vas[area] || !vms[area])
2491 goto err_free;
2492 }
2493retry:
2494 spin_lock(&vmap_area_lock);
2495
2496 /* start scanning - we scan from the top, begin with the last area */
2497 area = term_area = last_area;
2498 start = offsets[area];
2499 end = start + sizes[area];
2500
2501 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2502 base = vmalloc_end - last_end;
2503 goto found;
2504 }
2505 base = pvm_determine_end(&next, &prev, align) - end;
2506
2507 while (true) {
2508 BUG_ON(next && next->va_end <= base + end);
2509 BUG_ON(prev && prev->va_end > base + end);
2510
2511 /*
2512 * base might have underflowed, add last_end before
2513 * comparing.
2514 */
2515 if (base + last_end < vmalloc_start + last_end) {
2516 spin_unlock(&vmap_area_lock);
2517 if (!purged) {
2518 purge_vmap_area_lazy();
2519 purged = true;
2520 goto retry;
2521 }
2522 goto err_free;
2523 }
2524
2525 /*
2526 * If next overlaps, move base downwards so that it's
2527 * right below next and then recheck.
2528 */
2529 if (next && next->va_start < base + end) {
2530 base = pvm_determine_end(&next, &prev, align) - end;
2531 term_area = area;
2532 continue;
2533 }
2534
2535 /*
2536 * If prev overlaps, shift down next and prev and move
2537 * base so that it's right below new next and then
2538 * recheck.
2539 */
2540 if (prev && prev->va_end > base + start) {
2541 next = prev;
2542 prev = node_to_va(rb_prev(&next->rb_node));
2543 base = pvm_determine_end(&next, &prev, align) - end;
2544 term_area = area;
2545 continue;
2546 }
2547
2548 /*
2549 * This area fits, move on to the previous one. If
2550 * the previous one is the terminal one, we're done.
2551 */
2552 area = (area + nr_vms - 1) % nr_vms;
2553 if (area == term_area)
2554 break;
2555 start = offsets[area];
2556 end = start + sizes[area];
2557 pvm_find_next_prev(base + end, &next, &prev);
2558 }
2559found:
2560 /* we've found a fitting base, insert all va's */
2561 for (area = 0; area < nr_vms; area++) {
2562 struct vmap_area *va = vas[area];
2563
2564 va->va_start = base + offsets[area];
2565 va->va_end = va->va_start + sizes[area];
2566 __insert_vmap_area(va);
2567 }
2568
2569 vmap_area_pcpu_hole = base + offsets[last_area];
2570
2571 spin_unlock(&vmap_area_lock);
2572
2573 /* insert all vm's */
2574 for (area = 0; area < nr_vms; area++)
2575 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2576 pcpu_get_vm_areas);
2577
2578 kfree(vas);
2579 return vms;
2580
2581err_free:
2582 for (area = 0; area < nr_vms; area++) {
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002583 kfree(vas[area]);
2584 kfree(vms[area]);
Tejun Heoca23e402009-08-14 15:00:52 +09002585 }
Kautuk Consulf1db7af2012-01-12 17:20:08 -08002586err_free2:
Tejun Heoca23e402009-08-14 15:00:52 +09002587 kfree(vas);
2588 kfree(vms);
2589 return NULL;
2590}
2591
2592/**
2593 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2594 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2595 * @nr_vms: the number of allocated areas
2596 *
2597 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2598 */
2599void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2600{
2601 int i;
2602
2603 for (i = 0; i < nr_vms; i++)
2604 free_vm_area(vms[i]);
2605 kfree(vms);
2606}
Tejun Heo4f8b02b2010-09-03 18:22:47 +02002607#endif /* CONFIG_SMP */
Christoph Lametera10aa572008-04-28 02:12:40 -07002608
2609#ifdef CONFIG_PROC_FS
2610static void *s_start(struct seq_file *m, loff_t *pos)
Namhyung Kime199b5d2010-10-26 14:22:03 -07002611 __acquires(&vmlist_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002612{
2613 loff_t n = *pos;
2614 struct vm_struct *v;
2615
2616 read_lock(&vmlist_lock);
2617 v = vmlist;
2618 while (n > 0 && v) {
2619 n--;
2620 v = v->next;
2621 }
2622 if (!n)
2623 return v;
2624
2625 return NULL;
2626
2627}
2628
2629static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2630{
2631 struct vm_struct *v = p;
2632
2633 ++*pos;
2634 return v->next;
2635}
2636
2637static void s_stop(struct seq_file *m, void *p)
Namhyung Kime199b5d2010-10-26 14:22:03 -07002638 __releases(&vmlist_lock)
Christoph Lametera10aa572008-04-28 02:12:40 -07002639{
2640 read_unlock(&vmlist_lock);
2641}
2642
Eric Dumazeta47a1262008-07-23 21:27:38 -07002643static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2644{
2645 if (NUMA_BUILD) {
2646 unsigned int nr, *counters = m->private;
2647
2648 if (!counters)
2649 return;
2650
2651 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2652
2653 for (nr = 0; nr < v->nr_pages; nr++)
2654 counters[page_to_nid(v->pages[nr])]++;
2655
2656 for_each_node_state(nr, N_HIGH_MEMORY)
2657 if (counters[nr])
2658 seq_printf(m, " N%u=%u", nr, counters[nr]);
2659 }
2660}
2661
Christoph Lametera10aa572008-04-28 02:12:40 -07002662static int s_show(struct seq_file *m, void *p)
2663{
2664 struct vm_struct *v = p;
2665
2666 seq_printf(m, "0x%p-0x%p %7ld",
2667 v->addr, v->addr + v->size, v->size);
2668
Joe Perches62c70bc2011-01-13 15:45:52 -08002669 if (v->caller)
2670 seq_printf(m, " %pS", v->caller);
Christoph Lameter23016962008-04-28 02:12:42 -07002671
Christoph Lametera10aa572008-04-28 02:12:40 -07002672 if (v->nr_pages)
2673 seq_printf(m, " pages=%d", v->nr_pages);
2674
2675 if (v->phys_addr)
Kenji Kaneshigeffa71f32010-06-18 12:22:40 +09002676 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
Christoph Lametera10aa572008-04-28 02:12:40 -07002677
2678 if (v->flags & VM_IOREMAP)
2679 seq_printf(m, " ioremap");
2680
2681 if (v->flags & VM_ALLOC)
2682 seq_printf(m, " vmalloc");
2683
2684 if (v->flags & VM_MAP)
2685 seq_printf(m, " vmap");
2686
2687 if (v->flags & VM_USERMAP)
2688 seq_printf(m, " user");
2689
2690 if (v->flags & VM_VPAGES)
2691 seq_printf(m, " vpages");
2692
Laura Abbottd3263482013-08-22 13:46:07 -07002693 if (v->flags & VM_LOWMEM)
2694 seq_printf(m, " lowmem");
2695
Eric Dumazeta47a1262008-07-23 21:27:38 -07002696 show_numa_info(m, v);
Christoph Lametera10aa572008-04-28 02:12:40 -07002697 seq_putc(m, '\n');
2698 return 0;
2699}
2700
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002701static const struct seq_operations vmalloc_op = {
Christoph Lametera10aa572008-04-28 02:12:40 -07002702 .start = s_start,
2703 .next = s_next,
2704 .stop = s_stop,
2705 .show = s_show,
2706};
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002707
2708static int vmalloc_open(struct inode *inode, struct file *file)
2709{
2710 unsigned int *ptr = NULL;
2711 int ret;
2712
Kulikov Vasiliy51980ac2010-08-09 17:19:58 -07002713 if (NUMA_BUILD) {
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002714 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
Kulikov Vasiliy51980ac2010-08-09 17:19:58 -07002715 if (ptr == NULL)
2716 return -ENOMEM;
2717 }
Alexey Dobriyan5f6a6a92008-10-06 03:50:47 +04002718 ret = seq_open(file, &vmalloc_op);
2719 if (!ret) {
2720 struct seq_file *m = file->private_data;
2721 m->private = ptr;
2722 } else
2723 kfree(ptr);
2724 return ret;
2725}
2726
2727static const struct file_operations proc_vmalloc_operations = {
2728 .open = vmalloc_open,
2729 .read = seq_read,
2730 .llseek = seq_lseek,
2731 .release = seq_release_private,
2732};
2733
2734static int __init proc_vmalloc_init(void)
2735{
2736 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2737 return 0;
2738}
2739module_init(proc_vmalloc_init);
Christoph Lametera10aa572008-04-28 02:12:40 -07002740#endif
2741