Staging: cpc-usb: convert comment style in cpcusb.h
[linux-2.6/kvm.git] / mm / vmalloc.c
blob204b8243d8abb11cb9abd2f37dd8864ad0a346aa
1 /*
2 * linux/mm/vmalloc.c
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
8 * Numa awareness, Christoph Lameter, SGI, June 2005
9 */
11 #include <linux/vmalloc.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugobjects.h>
21 #include <linux/kallsyms.h>
22 #include <linux/list.h>
23 #include <linux/rbtree.h>
24 #include <linux/radix-tree.h>
25 #include <linux/rcupdate.h>
26 #include <linux/pfn.h>
27 #include <linux/kmemleak.h>
29 #include <asm/atomic.h>
30 #include <asm/uaccess.h>
31 #include <asm/tlbflush.h>
34 /*** Page table manipulation functions ***/
36 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
38 pte_t *pte;
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);
47 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
49 pmd_t *pmd;
50 unsigned long next;
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);
61 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
63 pud_t *pud;
64 unsigned long next;
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);
75 static void vunmap_page_range(unsigned long addr, unsigned long end)
77 pgd_t *pgd;
78 unsigned long next;
80 BUG_ON(addr >= end);
81 pgd = pgd_offset_k(addr);
82 do {
83 next = pgd_addr_end(addr, end);
84 if (pgd_none_or_clear_bad(pgd))
85 continue;
86 vunmap_pud_range(pgd, addr, next);
87 } while (pgd++, addr = next, addr != end);
90 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
91 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
93 pte_t *pte;
96 * nr is a running index into the array which helps higher level
97 * callers keep track of where we're up to.
100 pte = pte_alloc_kernel(pmd, addr);
101 if (!pte)
102 return -ENOMEM;
103 do {
104 struct page *page = pages[*nr];
106 if (WARN_ON(!pte_none(*pte)))
107 return -EBUSY;
108 if (WARN_ON(!page))
109 return -ENOMEM;
110 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
111 (*nr)++;
112 } while (pte++, addr += PAGE_SIZE, addr != end);
113 return 0;
116 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
117 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
119 pmd_t *pmd;
120 unsigned long next;
122 pmd = pmd_alloc(&init_mm, pud, addr);
123 if (!pmd)
124 return -ENOMEM;
125 do {
126 next = pmd_addr_end(addr, end);
127 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
128 return -ENOMEM;
129 } while (pmd++, addr = next, addr != end);
130 return 0;
133 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
134 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
136 pud_t *pud;
137 unsigned long next;
139 pud = pud_alloc(&init_mm, pgd, addr);
140 if (!pud)
141 return -ENOMEM;
142 do {
143 next = pud_addr_end(addr, end);
144 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
145 return -ENOMEM;
146 } while (pud++, addr = next, addr != end);
147 return 0;
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.
154 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
156 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
157 pgprot_t prot, struct page **pages)
159 pgd_t *pgd;
160 unsigned long next;
161 unsigned long addr = start;
162 int err = 0;
163 int nr = 0;
165 BUG_ON(addr >= end);
166 pgd = pgd_offset_k(addr);
167 do {
168 next = pgd_addr_end(addr, end);
169 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
170 if (err)
171 break;
172 } while (pgd++, addr = next, addr != end);
174 if (unlikely(err))
175 return err;
176 return nr;
179 static int vmap_page_range(unsigned long start, unsigned long end,
180 pgprot_t prot, struct page **pages)
182 int ret;
184 ret = vmap_page_range_noflush(start, end, prot, pages);
185 flush_cache_vmap(start, end);
186 return ret;
189 static inline int is_vmalloc_or_module_addr(const void *x)
192 * ARM, x86-64 and sparc64 put modules in a special place,
193 * and fall back on vmalloc() if that fails. Others
194 * just put it in the vmalloc space.
196 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
197 unsigned long addr = (unsigned long)x;
198 if (addr >= MODULES_VADDR && addr < MODULES_END)
199 return 1;
200 #endif
201 return is_vmalloc_addr(x);
205 * Walk a vmap address to the struct page it maps.
207 struct page *vmalloc_to_page(const void *vmalloc_addr)
209 unsigned long addr = (unsigned long) vmalloc_addr;
210 struct page *page = NULL;
211 pgd_t *pgd = pgd_offset_k(addr);
214 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
215 * architectures that do not vmalloc module space
217 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
219 if (!pgd_none(*pgd)) {
220 pud_t *pud = pud_offset(pgd, addr);
221 if (!pud_none(*pud)) {
222 pmd_t *pmd = pmd_offset(pud, addr);
223 if (!pmd_none(*pmd)) {
224 pte_t *ptep, pte;
226 ptep = pte_offset_map(pmd, addr);
227 pte = *ptep;
228 if (pte_present(pte))
229 page = pte_page(pte);
230 pte_unmap(ptep);
234 return page;
236 EXPORT_SYMBOL(vmalloc_to_page);
239 * Map a vmalloc()-space virtual address to the physical page frame number.
241 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
243 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
245 EXPORT_SYMBOL(vmalloc_to_pfn);
248 /*** Global kva allocator ***/
250 #define VM_LAZY_FREE 0x01
251 #define VM_LAZY_FREEING 0x02
252 #define VM_VM_AREA 0x04
254 struct vmap_area {
255 unsigned long va_start;
256 unsigned long va_end;
257 unsigned long flags;
258 struct rb_node rb_node; /* address sorted rbtree */
259 struct list_head list; /* address sorted list */
260 struct list_head purge_list; /* "lazy purge" list */
261 void *private;
262 struct rcu_head rcu_head;
265 static DEFINE_SPINLOCK(vmap_area_lock);
266 static struct rb_root vmap_area_root = RB_ROOT;
267 static LIST_HEAD(vmap_area_list);
268 static unsigned long vmap_area_pcpu_hole;
270 static struct vmap_area *__find_vmap_area(unsigned long addr)
272 struct rb_node *n = vmap_area_root.rb_node;
274 while (n) {
275 struct vmap_area *va;
277 va = rb_entry(n, struct vmap_area, rb_node);
278 if (addr < va->va_start)
279 n = n->rb_left;
280 else if (addr > va->va_start)
281 n = n->rb_right;
282 else
283 return va;
286 return NULL;
289 static void __insert_vmap_area(struct vmap_area *va)
291 struct rb_node **p = &vmap_area_root.rb_node;
292 struct rb_node *parent = NULL;
293 struct rb_node *tmp;
295 while (*p) {
296 struct vmap_area *tmp;
298 parent = *p;
299 tmp = rb_entry(parent, struct vmap_area, rb_node);
300 if (va->va_start < tmp->va_end)
301 p = &(*p)->rb_left;
302 else if (va->va_end > tmp->va_start)
303 p = &(*p)->rb_right;
304 else
305 BUG();
308 rb_link_node(&va->rb_node, parent, p);
309 rb_insert_color(&va->rb_node, &vmap_area_root);
311 /* address-sort this list so it is usable like the vmlist */
312 tmp = rb_prev(&va->rb_node);
313 if (tmp) {
314 struct vmap_area *prev;
315 prev = rb_entry(tmp, struct vmap_area, rb_node);
316 list_add_rcu(&va->list, &prev->list);
317 } else
318 list_add_rcu(&va->list, &vmap_area_list);
321 static void purge_vmap_area_lazy(void);
324 * Allocate a region of KVA of the specified size and alignment, within the
325 * vstart and vend.
327 static struct vmap_area *alloc_vmap_area(unsigned long size,
328 unsigned long align,
329 unsigned long vstart, unsigned long vend,
330 int node, gfp_t gfp_mask)
332 struct vmap_area *va;
333 struct rb_node *n;
334 unsigned long addr;
335 int purged = 0;
337 BUG_ON(!size);
338 BUG_ON(size & ~PAGE_MASK);
340 va = kmalloc_node(sizeof(struct vmap_area),
341 gfp_mask & GFP_RECLAIM_MASK, node);
342 if (unlikely(!va))
343 return ERR_PTR(-ENOMEM);
345 retry:
346 addr = ALIGN(vstart, align);
348 spin_lock(&vmap_area_lock);
349 if (addr + size - 1 < addr)
350 goto overflow;
352 /* XXX: could have a last_hole cache */
353 n = vmap_area_root.rb_node;
354 if (n) {
355 struct vmap_area *first = NULL;
357 do {
358 struct vmap_area *tmp;
359 tmp = rb_entry(n, struct vmap_area, rb_node);
360 if (tmp->va_end >= addr) {
361 if (!first && tmp->va_start < addr + size)
362 first = tmp;
363 n = n->rb_left;
364 } else {
365 first = tmp;
366 n = n->rb_right;
368 } while (n);
370 if (!first)
371 goto found;
373 if (first->va_end < addr) {
374 n = rb_next(&first->rb_node);
375 if (n)
376 first = rb_entry(n, struct vmap_area, rb_node);
377 else
378 goto found;
381 while (addr + size > first->va_start && addr + size <= vend) {
382 addr = ALIGN(first->va_end + PAGE_SIZE, align);
383 if (addr + size - 1 < addr)
384 goto overflow;
386 n = rb_next(&first->rb_node);
387 if (n)
388 first = rb_entry(n, struct vmap_area, rb_node);
389 else
390 goto found;
393 found:
394 if (addr + size > vend) {
395 overflow:
396 spin_unlock(&vmap_area_lock);
397 if (!purged) {
398 purge_vmap_area_lazy();
399 purged = 1;
400 goto retry;
402 if (printk_ratelimit())
403 printk(KERN_WARNING
404 "vmap allocation for size %lu failed: "
405 "use vmalloc=<size> to increase size.\n", size);
406 kfree(va);
407 return ERR_PTR(-EBUSY);
410 BUG_ON(addr & (align-1));
412 va->va_start = addr;
413 va->va_end = addr + size;
414 va->flags = 0;
415 __insert_vmap_area(va);
416 spin_unlock(&vmap_area_lock);
418 return va;
421 static void rcu_free_va(struct rcu_head *head)
423 struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
425 kfree(va);
428 static void __free_vmap_area(struct vmap_area *va)
430 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
431 rb_erase(&va->rb_node, &vmap_area_root);
432 RB_CLEAR_NODE(&va->rb_node);
433 list_del_rcu(&va->list);
436 * Track the highest possible candidate for pcpu area
437 * allocation. Areas outside of vmalloc area can be returned
438 * here too, consider only end addresses which fall inside
439 * vmalloc area proper.
441 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
442 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
444 call_rcu(&va->rcu_head, rcu_free_va);
448 * Free a region of KVA allocated by alloc_vmap_area
450 static void free_vmap_area(struct vmap_area *va)
452 spin_lock(&vmap_area_lock);
453 __free_vmap_area(va);
454 spin_unlock(&vmap_area_lock);
458 * Clear the pagetable entries of a given vmap_area
460 static void unmap_vmap_area(struct vmap_area *va)
462 vunmap_page_range(va->va_start, va->va_end);
465 static void vmap_debug_free_range(unsigned long start, unsigned long end)
468 * Unmap page tables and force a TLB flush immediately if
469 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
470 * bugs similarly to those in linear kernel virtual address
471 * space after a page has been freed.
473 * All the lazy freeing logic is still retained, in order to
474 * minimise intrusiveness of this debugging feature.
476 * This is going to be *slow* (linear kernel virtual address
477 * debugging doesn't do a broadcast TLB flush so it is a lot
478 * faster).
480 #ifdef CONFIG_DEBUG_PAGEALLOC
481 vunmap_page_range(start, end);
482 flush_tlb_kernel_range(start, end);
483 #endif
487 * lazy_max_pages is the maximum amount of virtual address space we gather up
488 * before attempting to purge with a TLB flush.
490 * There is a tradeoff here: a larger number will cover more kernel page tables
491 * and take slightly longer to purge, but it will linearly reduce the number of
492 * global TLB flushes that must be performed. It would seem natural to scale
493 * this number up linearly with the number of CPUs (because vmapping activity
494 * could also scale linearly with the number of CPUs), however it is likely
495 * that in practice, workloads might be constrained in other ways that mean
496 * vmap activity will not scale linearly with CPUs. Also, I want to be
497 * conservative and not introduce a big latency on huge systems, so go with
498 * a less aggressive log scale. It will still be an improvement over the old
499 * code, and it will be simple to change the scale factor if we find that it
500 * becomes a problem on bigger systems.
502 static unsigned long lazy_max_pages(void)
504 unsigned int log;
506 log = fls(num_online_cpus());
508 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
511 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
514 * Purges all lazily-freed vmap areas.
516 * If sync is 0 then don't purge if there is already a purge in progress.
517 * If force_flush is 1, then flush kernel TLBs between *start and *end even
518 * if we found no lazy vmap areas to unmap (callers can use this to optimise
519 * their own TLB flushing).
520 * Returns with *start = min(*start, lowest purged address)
521 * *end = max(*end, highest purged address)
523 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
524 int sync, int force_flush)
526 static DEFINE_SPINLOCK(purge_lock);
527 LIST_HEAD(valist);
528 struct vmap_area *va;
529 struct vmap_area *n_va;
530 int nr = 0;
533 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
534 * should not expect such behaviour. This just simplifies locking for
535 * the case that isn't actually used at the moment anyway.
537 if (!sync && !force_flush) {
538 if (!spin_trylock(&purge_lock))
539 return;
540 } else
541 spin_lock(&purge_lock);
543 rcu_read_lock();
544 list_for_each_entry_rcu(va, &vmap_area_list, list) {
545 if (va->flags & VM_LAZY_FREE) {
546 if (va->va_start < *start)
547 *start = va->va_start;
548 if (va->va_end > *end)
549 *end = va->va_end;
550 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
551 unmap_vmap_area(va);
552 list_add_tail(&va->purge_list, &valist);
553 va->flags |= VM_LAZY_FREEING;
554 va->flags &= ~VM_LAZY_FREE;
557 rcu_read_unlock();
559 if (nr) {
560 BUG_ON(nr > atomic_read(&vmap_lazy_nr));
561 atomic_sub(nr, &vmap_lazy_nr);
564 if (nr || force_flush)
565 flush_tlb_kernel_range(*start, *end);
567 if (nr) {
568 spin_lock(&vmap_area_lock);
569 list_for_each_entry_safe(va, n_va, &valist, purge_list)
570 __free_vmap_area(va);
571 spin_unlock(&vmap_area_lock);
573 spin_unlock(&purge_lock);
577 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
578 * is already purging.
580 static void try_purge_vmap_area_lazy(void)
582 unsigned long start = ULONG_MAX, end = 0;
584 __purge_vmap_area_lazy(&start, &end, 0, 0);
588 * Kick off a purge of the outstanding lazy areas.
590 static void purge_vmap_area_lazy(void)
592 unsigned long start = ULONG_MAX, end = 0;
594 __purge_vmap_area_lazy(&start, &end, 1, 0);
598 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
599 * called for the correct range previously.
601 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
603 va->flags |= VM_LAZY_FREE;
604 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
605 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
606 try_purge_vmap_area_lazy();
610 * Free and unmap a vmap area
612 static void free_unmap_vmap_area(struct vmap_area *va)
614 flush_cache_vunmap(va->va_start, va->va_end);
615 free_unmap_vmap_area_noflush(va);
618 static struct vmap_area *find_vmap_area(unsigned long addr)
620 struct vmap_area *va;
622 spin_lock(&vmap_area_lock);
623 va = __find_vmap_area(addr);
624 spin_unlock(&vmap_area_lock);
626 return va;
629 static void free_unmap_vmap_area_addr(unsigned long addr)
631 struct vmap_area *va;
633 va = find_vmap_area(addr);
634 BUG_ON(!va);
635 free_unmap_vmap_area(va);
639 /*** Per cpu kva allocator ***/
642 * vmap space is limited especially on 32 bit architectures. Ensure there is
643 * room for at least 16 percpu vmap blocks per CPU.
646 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
647 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
648 * instead (we just need a rough idea)
650 #if BITS_PER_LONG == 32
651 #define VMALLOC_SPACE (128UL*1024*1024)
652 #else
653 #define VMALLOC_SPACE (128UL*1024*1024*1024)
654 #endif
656 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
657 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
658 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
659 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
660 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
661 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
662 #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
663 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
664 VMALLOC_PAGES / NR_CPUS / 16))
666 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
668 static bool vmap_initialized __read_mostly = false;
670 struct vmap_block_queue {
671 spinlock_t lock;
672 struct list_head free;
673 struct list_head dirty;
674 unsigned int nr_dirty;
677 struct vmap_block {
678 spinlock_t lock;
679 struct vmap_area *va;
680 struct vmap_block_queue *vbq;
681 unsigned long free, dirty;
682 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
683 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
684 union {
685 struct list_head free_list;
686 struct rcu_head rcu_head;
690 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
691 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
694 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
695 * in the free path. Could get rid of this if we change the API to return a
696 * "cookie" from alloc, to be passed to free. But no big deal yet.
698 static DEFINE_SPINLOCK(vmap_block_tree_lock);
699 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
702 * We should probably have a fallback mechanism to allocate virtual memory
703 * out of partially filled vmap blocks. However vmap block sizing should be
704 * fairly reasonable according to the vmalloc size, so it shouldn't be a
705 * big problem.
708 static unsigned long addr_to_vb_idx(unsigned long addr)
710 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
711 addr /= VMAP_BLOCK_SIZE;
712 return addr;
715 static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
717 struct vmap_block_queue *vbq;
718 struct vmap_block *vb;
719 struct vmap_area *va;
720 unsigned long vb_idx;
721 int node, err;
723 node = numa_node_id();
725 vb = kmalloc_node(sizeof(struct vmap_block),
726 gfp_mask & GFP_RECLAIM_MASK, node);
727 if (unlikely(!vb))
728 return ERR_PTR(-ENOMEM);
730 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
731 VMALLOC_START, VMALLOC_END,
732 node, gfp_mask);
733 if (unlikely(IS_ERR(va))) {
734 kfree(vb);
735 return ERR_PTR(PTR_ERR(va));
738 err = radix_tree_preload(gfp_mask);
739 if (unlikely(err)) {
740 kfree(vb);
741 free_vmap_area(va);
742 return ERR_PTR(err);
745 spin_lock_init(&vb->lock);
746 vb->va = va;
747 vb->free = VMAP_BBMAP_BITS;
748 vb->dirty = 0;
749 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
750 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
751 INIT_LIST_HEAD(&vb->free_list);
753 vb_idx = addr_to_vb_idx(va->va_start);
754 spin_lock(&vmap_block_tree_lock);
755 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
756 spin_unlock(&vmap_block_tree_lock);
757 BUG_ON(err);
758 radix_tree_preload_end();
760 vbq = &get_cpu_var(vmap_block_queue);
761 vb->vbq = vbq;
762 spin_lock(&vbq->lock);
763 list_add(&vb->free_list, &vbq->free);
764 spin_unlock(&vbq->lock);
765 put_cpu_var(vmap_cpu_blocks);
767 return vb;
770 static void rcu_free_vb(struct rcu_head *head)
772 struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
774 kfree(vb);
777 static void free_vmap_block(struct vmap_block *vb)
779 struct vmap_block *tmp;
780 unsigned long vb_idx;
782 BUG_ON(!list_empty(&vb->free_list));
784 vb_idx = addr_to_vb_idx(vb->va->va_start);
785 spin_lock(&vmap_block_tree_lock);
786 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
787 spin_unlock(&vmap_block_tree_lock);
788 BUG_ON(tmp != vb);
790 free_unmap_vmap_area_noflush(vb->va);
791 call_rcu(&vb->rcu_head, rcu_free_vb);
794 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
796 struct vmap_block_queue *vbq;
797 struct vmap_block *vb;
798 unsigned long addr = 0;
799 unsigned int order;
801 BUG_ON(size & ~PAGE_MASK);
802 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
803 order = get_order(size);
805 again:
806 rcu_read_lock();
807 vbq = &get_cpu_var(vmap_block_queue);
808 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
809 int i;
811 spin_lock(&vb->lock);
812 i = bitmap_find_free_region(vb->alloc_map,
813 VMAP_BBMAP_BITS, order);
815 if (i >= 0) {
816 addr = vb->va->va_start + (i << PAGE_SHIFT);
817 BUG_ON(addr_to_vb_idx(addr) !=
818 addr_to_vb_idx(vb->va->va_start));
819 vb->free -= 1UL << order;
820 if (vb->free == 0) {
821 spin_lock(&vbq->lock);
822 list_del_init(&vb->free_list);
823 spin_unlock(&vbq->lock);
825 spin_unlock(&vb->lock);
826 break;
828 spin_unlock(&vb->lock);
830 put_cpu_var(vmap_cpu_blocks);
831 rcu_read_unlock();
833 if (!addr) {
834 vb = new_vmap_block(gfp_mask);
835 if (IS_ERR(vb))
836 return vb;
837 goto again;
840 return (void *)addr;
843 static void vb_free(const void *addr, unsigned long size)
845 unsigned long offset;
846 unsigned long vb_idx;
847 unsigned int order;
848 struct vmap_block *vb;
850 BUG_ON(size & ~PAGE_MASK);
851 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
853 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
855 order = get_order(size);
857 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
859 vb_idx = addr_to_vb_idx((unsigned long)addr);
860 rcu_read_lock();
861 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
862 rcu_read_unlock();
863 BUG_ON(!vb);
865 spin_lock(&vb->lock);
866 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
868 vb->dirty += 1UL << order;
869 if (vb->dirty == VMAP_BBMAP_BITS) {
870 BUG_ON(vb->free || !list_empty(&vb->free_list));
871 spin_unlock(&vb->lock);
872 free_vmap_block(vb);
873 } else
874 spin_unlock(&vb->lock);
878 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
880 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
881 * to amortize TLB flushing overheads. What this means is that any page you
882 * have now, may, in a former life, have been mapped into kernel virtual
883 * address by the vmap layer and so there might be some CPUs with TLB entries
884 * still referencing that page (additional to the regular 1:1 kernel mapping).
886 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
887 * be sure that none of the pages we have control over will have any aliases
888 * from the vmap layer.
890 void vm_unmap_aliases(void)
892 unsigned long start = ULONG_MAX, end = 0;
893 int cpu;
894 int flush = 0;
896 if (unlikely(!vmap_initialized))
897 return;
899 for_each_possible_cpu(cpu) {
900 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
901 struct vmap_block *vb;
903 rcu_read_lock();
904 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
905 int i;
907 spin_lock(&vb->lock);
908 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
909 while (i < VMAP_BBMAP_BITS) {
910 unsigned long s, e;
911 int j;
912 j = find_next_zero_bit(vb->dirty_map,
913 VMAP_BBMAP_BITS, i);
915 s = vb->va->va_start + (i << PAGE_SHIFT);
916 e = vb->va->va_start + (j << PAGE_SHIFT);
917 vunmap_page_range(s, e);
918 flush = 1;
920 if (s < start)
921 start = s;
922 if (e > end)
923 end = e;
925 i = j;
926 i = find_next_bit(vb->dirty_map,
927 VMAP_BBMAP_BITS, i);
929 spin_unlock(&vb->lock);
931 rcu_read_unlock();
934 __purge_vmap_area_lazy(&start, &end, 1, flush);
936 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
939 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
940 * @mem: the pointer returned by vm_map_ram
941 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
943 void vm_unmap_ram(const void *mem, unsigned int count)
945 unsigned long size = count << PAGE_SHIFT;
946 unsigned long addr = (unsigned long)mem;
948 BUG_ON(!addr);
949 BUG_ON(addr < VMALLOC_START);
950 BUG_ON(addr > VMALLOC_END);
951 BUG_ON(addr & (PAGE_SIZE-1));
953 debug_check_no_locks_freed(mem, size);
954 vmap_debug_free_range(addr, addr+size);
956 if (likely(count <= VMAP_MAX_ALLOC))
957 vb_free(mem, size);
958 else
959 free_unmap_vmap_area_addr(addr);
961 EXPORT_SYMBOL(vm_unmap_ram);
964 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
965 * @pages: an array of pointers to the pages to be mapped
966 * @count: number of pages
967 * @node: prefer to allocate data structures on this node
968 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
970 * Returns: a pointer to the address that has been mapped, or %NULL on failure
972 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
974 unsigned long size = count << PAGE_SHIFT;
975 unsigned long addr;
976 void *mem;
978 if (likely(count <= VMAP_MAX_ALLOC)) {
979 mem = vb_alloc(size, GFP_KERNEL);
980 if (IS_ERR(mem))
981 return NULL;
982 addr = (unsigned long)mem;
983 } else {
984 struct vmap_area *va;
985 va = alloc_vmap_area(size, PAGE_SIZE,
986 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
987 if (IS_ERR(va))
988 return NULL;
990 addr = va->va_start;
991 mem = (void *)addr;
993 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
994 vm_unmap_ram(mem, count);
995 return NULL;
997 return mem;
999 EXPORT_SYMBOL(vm_map_ram);
1002 * vm_area_register_early - register vmap area early during boot
1003 * @vm: vm_struct to register
1004 * @align: requested alignment
1006 * This function is used to register kernel vm area before
1007 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1008 * proper values on entry and other fields should be zero. On return,
1009 * vm->addr contains the allocated address.
1011 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1013 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1015 static size_t vm_init_off __initdata;
1016 unsigned long addr;
1018 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1019 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1021 vm->addr = (void *)addr;
1023 vm->next = vmlist;
1024 vmlist = vm;
1027 void __init vmalloc_init(void)
1029 struct vmap_area *va;
1030 struct vm_struct *tmp;
1031 int i;
1033 for_each_possible_cpu(i) {
1034 struct vmap_block_queue *vbq;
1036 vbq = &per_cpu(vmap_block_queue, i);
1037 spin_lock_init(&vbq->lock);
1038 INIT_LIST_HEAD(&vbq->free);
1039 INIT_LIST_HEAD(&vbq->dirty);
1040 vbq->nr_dirty = 0;
1043 /* Import existing vmlist entries. */
1044 for (tmp = vmlist; tmp; tmp = tmp->next) {
1045 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1046 va->flags = tmp->flags | VM_VM_AREA;
1047 va->va_start = (unsigned long)tmp->addr;
1048 va->va_end = va->va_start + tmp->size;
1049 __insert_vmap_area(va);
1052 vmap_area_pcpu_hole = VMALLOC_END;
1054 vmap_initialized = true;
1058 * map_kernel_range_noflush - map kernel VM area with the specified pages
1059 * @addr: start of the VM area to map
1060 * @size: size of the VM area to map
1061 * @prot: page protection flags to use
1062 * @pages: pages to map
1064 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1065 * specify should have been allocated using get_vm_area() and its
1066 * friends.
1068 * NOTE:
1069 * This function does NOT do any cache flushing. The caller is
1070 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1071 * before calling this function.
1073 * RETURNS:
1074 * The number of pages mapped on success, -errno on failure.
1076 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1077 pgprot_t prot, struct page **pages)
1079 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1083 * unmap_kernel_range_noflush - unmap kernel VM area
1084 * @addr: start of the VM area to unmap
1085 * @size: size of the VM area to unmap
1087 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1088 * specify should have been allocated using get_vm_area() and its
1089 * friends.
1091 * NOTE:
1092 * This function does NOT do any cache flushing. The caller is
1093 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1094 * before calling this function and flush_tlb_kernel_range() after.
1096 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1098 vunmap_page_range(addr, addr + size);
1102 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1103 * @addr: start of the VM area to unmap
1104 * @size: size of the VM area to unmap
1106 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1107 * the unmapping and tlb after.
1109 void unmap_kernel_range(unsigned long addr, unsigned long size)
1111 unsigned long end = addr + size;
1113 flush_cache_vunmap(addr, end);
1114 vunmap_page_range(addr, end);
1115 flush_tlb_kernel_range(addr, end);
1118 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1120 unsigned long addr = (unsigned long)area->addr;
1121 unsigned long end = addr + area->size - PAGE_SIZE;
1122 int err;
1124 err = vmap_page_range(addr, end, prot, *pages);
1125 if (err > 0) {
1126 *pages += err;
1127 err = 0;
1130 return err;
1132 EXPORT_SYMBOL_GPL(map_vm_area);
1134 /*** Old vmalloc interfaces ***/
1135 DEFINE_RWLOCK(vmlist_lock);
1136 struct vm_struct *vmlist;
1138 static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1139 unsigned long flags, void *caller)
1141 struct vm_struct *tmp, **p;
1143 vm->flags = flags;
1144 vm->addr = (void *)va->va_start;
1145 vm->size = va->va_end - va->va_start;
1146 vm->caller = caller;
1147 va->private = vm;
1148 va->flags |= VM_VM_AREA;
1150 write_lock(&vmlist_lock);
1151 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1152 if (tmp->addr >= vm->addr)
1153 break;
1155 vm->next = *p;
1156 *p = vm;
1157 write_unlock(&vmlist_lock);
1160 static struct vm_struct *__get_vm_area_node(unsigned long size,
1161 unsigned long flags, unsigned long start, unsigned long end,
1162 int node, gfp_t gfp_mask, void *caller)
1164 static struct vmap_area *va;
1165 struct vm_struct *area;
1166 unsigned long align = 1;
1168 BUG_ON(in_interrupt());
1169 if (flags & VM_IOREMAP) {
1170 int bit = fls(size);
1172 if (bit > IOREMAP_MAX_ORDER)
1173 bit = IOREMAP_MAX_ORDER;
1174 else if (bit < PAGE_SHIFT)
1175 bit = PAGE_SHIFT;
1177 align = 1ul << bit;
1180 size = PAGE_ALIGN(size);
1181 if (unlikely(!size))
1182 return NULL;
1184 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1185 if (unlikely(!area))
1186 return NULL;
1189 * We always allocate a guard page.
1191 size += PAGE_SIZE;
1193 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1194 if (IS_ERR(va)) {
1195 kfree(area);
1196 return NULL;
1199 insert_vmalloc_vm(area, va, flags, caller);
1200 return area;
1203 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1204 unsigned long start, unsigned long end)
1206 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1207 __builtin_return_address(0));
1209 EXPORT_SYMBOL_GPL(__get_vm_area);
1211 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1212 unsigned long start, unsigned long end,
1213 void *caller)
1215 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1216 caller);
1220 * get_vm_area - reserve a contiguous kernel virtual area
1221 * @size: size of the area
1222 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1224 * Search an area of @size in the kernel virtual mapping area,
1225 * and reserved it for out purposes. Returns the area descriptor
1226 * on success or %NULL on failure.
1228 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1230 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1231 -1, GFP_KERNEL, __builtin_return_address(0));
1234 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1235 void *caller)
1237 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1238 -1, GFP_KERNEL, caller);
1241 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1242 int node, gfp_t gfp_mask)
1244 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
1245 gfp_mask, __builtin_return_address(0));
1248 static struct vm_struct *find_vm_area(const void *addr)
1250 struct vmap_area *va;
1252 va = find_vmap_area((unsigned long)addr);
1253 if (va && va->flags & VM_VM_AREA)
1254 return va->private;
1256 return NULL;
1260 * remove_vm_area - find and remove a continuous kernel virtual area
1261 * @addr: base address
1263 * Search for the kernel VM area starting at @addr, and remove it.
1264 * This function returns the found VM area, but using it is NOT safe
1265 * on SMP machines, except for its size or flags.
1267 struct vm_struct *remove_vm_area(const void *addr)
1269 struct vmap_area *va;
1271 va = find_vmap_area((unsigned long)addr);
1272 if (va && va->flags & VM_VM_AREA) {
1273 struct vm_struct *vm = va->private;
1274 struct vm_struct *tmp, **p;
1276 vmap_debug_free_range(va->va_start, va->va_end);
1277 free_unmap_vmap_area(va);
1278 vm->size -= PAGE_SIZE;
1280 write_lock(&vmlist_lock);
1281 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1283 *p = tmp->next;
1284 write_unlock(&vmlist_lock);
1286 return vm;
1288 return NULL;
1291 static void __vunmap(const void *addr, int deallocate_pages)
1293 struct vm_struct *area;
1295 if (!addr)
1296 return;
1298 if ((PAGE_SIZE-1) & (unsigned long)addr) {
1299 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1300 return;
1303 area = remove_vm_area(addr);
1304 if (unlikely(!area)) {
1305 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1306 addr);
1307 return;
1310 debug_check_no_locks_freed(addr, area->size);
1311 debug_check_no_obj_freed(addr, area->size);
1313 if (deallocate_pages) {
1314 int i;
1316 for (i = 0; i < area->nr_pages; i++) {
1317 struct page *page = area->pages[i];
1319 BUG_ON(!page);
1320 __free_page(page);
1323 if (area->flags & VM_VPAGES)
1324 vfree(area->pages);
1325 else
1326 kfree(area->pages);
1329 kfree(area);
1330 return;
1334 * vfree - release memory allocated by vmalloc()
1335 * @addr: memory base address
1337 * Free the virtually continuous memory area starting at @addr, as
1338 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1339 * NULL, no operation is performed.
1341 * Must not be called in interrupt context.
1343 void vfree(const void *addr)
1345 BUG_ON(in_interrupt());
1347 kmemleak_free(addr);
1349 __vunmap(addr, 1);
1351 EXPORT_SYMBOL(vfree);
1354 * vunmap - release virtual mapping obtained by vmap()
1355 * @addr: memory base address
1357 * Free the virtually contiguous memory area starting at @addr,
1358 * which was created from the page array passed to vmap().
1360 * Must not be called in interrupt context.
1362 void vunmap(const void *addr)
1364 BUG_ON(in_interrupt());
1365 might_sleep();
1366 __vunmap(addr, 0);
1368 EXPORT_SYMBOL(vunmap);
1371 * vmap - map an array of pages into virtually contiguous space
1372 * @pages: array of page pointers
1373 * @count: number of pages to map
1374 * @flags: vm_area->flags
1375 * @prot: page protection for the mapping
1377 * Maps @count pages from @pages into contiguous kernel virtual
1378 * space.
1380 void *vmap(struct page **pages, unsigned int count,
1381 unsigned long flags, pgprot_t prot)
1383 struct vm_struct *area;
1385 might_sleep();
1387 if (count > num_physpages)
1388 return NULL;
1390 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1391 __builtin_return_address(0));
1392 if (!area)
1393 return NULL;
1395 if (map_vm_area(area, prot, &pages)) {
1396 vunmap(area->addr);
1397 return NULL;
1400 return area->addr;
1402 EXPORT_SYMBOL(vmap);
1404 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1405 int node, void *caller);
1406 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1407 pgprot_t prot, int node, void *caller)
1409 struct page **pages;
1410 unsigned int nr_pages, array_size, i;
1412 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1413 array_size = (nr_pages * sizeof(struct page *));
1415 area->nr_pages = nr_pages;
1416 /* Please note that the recursion is strictly bounded. */
1417 if (array_size > PAGE_SIZE) {
1418 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
1419 PAGE_KERNEL, node, caller);
1420 area->flags |= VM_VPAGES;
1421 } else {
1422 pages = kmalloc_node(array_size,
1423 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1424 node);
1426 area->pages = pages;
1427 area->caller = caller;
1428 if (!area->pages) {
1429 remove_vm_area(area->addr);
1430 kfree(area);
1431 return NULL;
1434 for (i = 0; i < area->nr_pages; i++) {
1435 struct page *page;
1437 if (node < 0)
1438 page = alloc_page(gfp_mask);
1439 else
1440 page = alloc_pages_node(node, gfp_mask, 0);
1442 if (unlikely(!page)) {
1443 /* Successfully allocated i pages, free them in __vunmap() */
1444 area->nr_pages = i;
1445 goto fail;
1447 area->pages[i] = page;
1450 if (map_vm_area(area, prot, &pages))
1451 goto fail;
1452 return area->addr;
1454 fail:
1455 vfree(area->addr);
1456 return NULL;
1459 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1461 void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1462 __builtin_return_address(0));
1465 * A ref_count = 3 is needed because the vm_struct and vmap_area
1466 * structures allocated in the __get_vm_area_node() function contain
1467 * references to the virtual address of the vmalloc'ed block.
1469 kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1471 return addr;
1475 * __vmalloc_node - allocate virtually contiguous memory
1476 * @size: allocation size
1477 * @gfp_mask: flags for the page level allocator
1478 * @prot: protection mask for the allocated pages
1479 * @node: node to use for allocation or -1
1480 * @caller: caller's return address
1482 * Allocate enough pages to cover @size from the page level
1483 * allocator with @gfp_mask flags. Map them into contiguous
1484 * kernel virtual space, using a pagetable protection of @prot.
1486 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1487 int node, void *caller)
1489 struct vm_struct *area;
1490 void *addr;
1491 unsigned long real_size = size;
1493 size = PAGE_ALIGN(size);
1494 if (!size || (size >> PAGE_SHIFT) > num_physpages)
1495 return NULL;
1497 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1498 node, gfp_mask, caller);
1500 if (!area)
1501 return NULL;
1503 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1506 * A ref_count = 3 is needed because the vm_struct and vmap_area
1507 * structures allocated in the __get_vm_area_node() function contain
1508 * references to the virtual address of the vmalloc'ed block.
1510 kmemleak_alloc(addr, real_size, 3, gfp_mask);
1512 return addr;
1515 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1517 return __vmalloc_node(size, gfp_mask, prot, -1,
1518 __builtin_return_address(0));
1520 EXPORT_SYMBOL(__vmalloc);
1523 * vmalloc - allocate virtually contiguous memory
1524 * @size: allocation size
1525 * Allocate enough pages to cover @size from the page level
1526 * allocator and map them into contiguous kernel virtual space.
1528 * For tight control over page level allocator and protection flags
1529 * use __vmalloc() instead.
1531 void *vmalloc(unsigned long size)
1533 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1534 -1, __builtin_return_address(0));
1536 EXPORT_SYMBOL(vmalloc);
1539 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1540 * @size: allocation size
1542 * The resulting memory area is zeroed so it can be mapped to userspace
1543 * without leaking data.
1545 void *vmalloc_user(unsigned long size)
1547 struct vm_struct *area;
1548 void *ret;
1550 ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1551 PAGE_KERNEL, -1, __builtin_return_address(0));
1552 if (ret) {
1553 area = find_vm_area(ret);
1554 area->flags |= VM_USERMAP;
1556 return ret;
1558 EXPORT_SYMBOL(vmalloc_user);
1561 * vmalloc_node - allocate memory on a specific node
1562 * @size: allocation size
1563 * @node: numa node
1565 * Allocate enough pages to cover @size from the page level
1566 * allocator and map them into contiguous kernel virtual space.
1568 * For tight control over page level allocator and protection flags
1569 * use __vmalloc() instead.
1571 void *vmalloc_node(unsigned long size, int node)
1573 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1574 node, __builtin_return_address(0));
1576 EXPORT_SYMBOL(vmalloc_node);
1578 #ifndef PAGE_KERNEL_EXEC
1579 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1580 #endif
1583 * vmalloc_exec - allocate virtually contiguous, executable memory
1584 * @size: allocation size
1586 * Kernel-internal function to allocate enough pages to cover @size
1587 * the page level allocator and map them into contiguous and
1588 * executable kernel virtual space.
1590 * For tight control over page level allocator and protection flags
1591 * use __vmalloc() instead.
1594 void *vmalloc_exec(unsigned long size)
1596 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1597 -1, __builtin_return_address(0));
1600 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1601 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1602 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1603 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1604 #else
1605 #define GFP_VMALLOC32 GFP_KERNEL
1606 #endif
1609 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1610 * @size: allocation size
1612 * Allocate enough 32bit PA addressable pages to cover @size from the
1613 * page level allocator and map them into contiguous kernel virtual space.
1615 void *vmalloc_32(unsigned long size)
1617 return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1618 -1, __builtin_return_address(0));
1620 EXPORT_SYMBOL(vmalloc_32);
1623 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1624 * @size: allocation size
1626 * The resulting memory area is 32bit addressable and zeroed so it can be
1627 * mapped to userspace without leaking data.
1629 void *vmalloc_32_user(unsigned long size)
1631 struct vm_struct *area;
1632 void *ret;
1634 ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1635 -1, __builtin_return_address(0));
1636 if (ret) {
1637 area = find_vm_area(ret);
1638 area->flags |= VM_USERMAP;
1640 return ret;
1642 EXPORT_SYMBOL(vmalloc_32_user);
1644 long vread(char *buf, char *addr, unsigned long count)
1646 struct vm_struct *tmp;
1647 char *vaddr, *buf_start = buf;
1648 unsigned long n;
1650 /* Don't allow overflow */
1651 if ((unsigned long) addr + count < count)
1652 count = -(unsigned long) addr;
1654 read_lock(&vmlist_lock);
1655 for (tmp = vmlist; tmp; tmp = tmp->next) {
1656 vaddr = (char *) tmp->addr;
1657 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1658 continue;
1659 while (addr < vaddr) {
1660 if (count == 0)
1661 goto finished;
1662 *buf = '\0';
1663 buf++;
1664 addr++;
1665 count--;
1667 n = vaddr + tmp->size - PAGE_SIZE - addr;
1668 do {
1669 if (count == 0)
1670 goto finished;
1671 *buf = *addr;
1672 buf++;
1673 addr++;
1674 count--;
1675 } while (--n > 0);
1677 finished:
1678 read_unlock(&vmlist_lock);
1679 return buf - buf_start;
1682 long vwrite(char *buf, char *addr, unsigned long count)
1684 struct vm_struct *tmp;
1685 char *vaddr, *buf_start = buf;
1686 unsigned long n;
1688 /* Don't allow overflow */
1689 if ((unsigned long) addr + count < count)
1690 count = -(unsigned long) addr;
1692 read_lock(&vmlist_lock);
1693 for (tmp = vmlist; tmp; tmp = tmp->next) {
1694 vaddr = (char *) tmp->addr;
1695 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1696 continue;
1697 while (addr < vaddr) {
1698 if (count == 0)
1699 goto finished;
1700 buf++;
1701 addr++;
1702 count--;
1704 n = vaddr + tmp->size - PAGE_SIZE - addr;
1705 do {
1706 if (count == 0)
1707 goto finished;
1708 *addr = *buf;
1709 buf++;
1710 addr++;
1711 count--;
1712 } while (--n > 0);
1714 finished:
1715 read_unlock(&vmlist_lock);
1716 return buf - buf_start;
1720 * remap_vmalloc_range - map vmalloc pages to userspace
1721 * @vma: vma to cover (map full range of vma)
1722 * @addr: vmalloc memory
1723 * @pgoff: number of pages into addr before first page to map
1725 * Returns: 0 for success, -Exxx on failure
1727 * This function checks that addr is a valid vmalloc'ed area, and
1728 * that it is big enough to cover the vma. Will return failure if
1729 * that criteria isn't met.
1731 * Similar to remap_pfn_range() (see mm/memory.c)
1733 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1734 unsigned long pgoff)
1736 struct vm_struct *area;
1737 unsigned long uaddr = vma->vm_start;
1738 unsigned long usize = vma->vm_end - vma->vm_start;
1740 if ((PAGE_SIZE-1) & (unsigned long)addr)
1741 return -EINVAL;
1743 area = find_vm_area(addr);
1744 if (!area)
1745 return -EINVAL;
1747 if (!(area->flags & VM_USERMAP))
1748 return -EINVAL;
1750 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1751 return -EINVAL;
1753 addr += pgoff << PAGE_SHIFT;
1754 do {
1755 struct page *page = vmalloc_to_page(addr);
1756 int ret;
1758 ret = vm_insert_page(vma, uaddr, page);
1759 if (ret)
1760 return ret;
1762 uaddr += PAGE_SIZE;
1763 addr += PAGE_SIZE;
1764 usize -= PAGE_SIZE;
1765 } while (usize > 0);
1767 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
1768 vma->vm_flags |= VM_RESERVED;
1770 return 0;
1772 EXPORT_SYMBOL(remap_vmalloc_range);
1775 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1776 * have one.
1778 void __attribute__((weak)) vmalloc_sync_all(void)
1783 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
1785 /* apply_to_page_range() does all the hard work. */
1786 return 0;
1790 * alloc_vm_area - allocate a range of kernel address space
1791 * @size: size of the area
1793 * Returns: NULL on failure, vm_struct on success
1795 * This function reserves a range of kernel address space, and
1796 * allocates pagetables to map that range. No actual mappings
1797 * are created. If the kernel address space is not shared
1798 * between processes, it syncs the pagetable across all
1799 * processes.
1801 struct vm_struct *alloc_vm_area(size_t size)
1803 struct vm_struct *area;
1805 area = get_vm_area_caller(size, VM_IOREMAP,
1806 __builtin_return_address(0));
1807 if (area == NULL)
1808 return NULL;
1811 * This ensures that page tables are constructed for this region
1812 * of kernel virtual address space and mapped into init_mm.
1814 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1815 area->size, f, NULL)) {
1816 free_vm_area(area);
1817 return NULL;
1820 /* Make sure the pagetables are constructed in process kernel
1821 mappings */
1822 vmalloc_sync_all();
1824 return area;
1826 EXPORT_SYMBOL_GPL(alloc_vm_area);
1828 void free_vm_area(struct vm_struct *area)
1830 struct vm_struct *ret;
1831 ret = remove_vm_area(area->addr);
1832 BUG_ON(ret != area);
1833 kfree(area);
1835 EXPORT_SYMBOL_GPL(free_vm_area);
1837 static struct vmap_area *node_to_va(struct rb_node *n)
1839 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
1843 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
1844 * @end: target address
1845 * @pnext: out arg for the next vmap_area
1846 * @pprev: out arg for the previous vmap_area
1848 * Returns: %true if either or both of next and prev are found,
1849 * %false if no vmap_area exists
1851 * Find vmap_areas end addresses of which enclose @end. ie. if not
1852 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
1854 static bool pvm_find_next_prev(unsigned long end,
1855 struct vmap_area **pnext,
1856 struct vmap_area **pprev)
1858 struct rb_node *n = vmap_area_root.rb_node;
1859 struct vmap_area *va = NULL;
1861 while (n) {
1862 va = rb_entry(n, struct vmap_area, rb_node);
1863 if (end < va->va_end)
1864 n = n->rb_left;
1865 else if (end > va->va_end)
1866 n = n->rb_right;
1867 else
1868 break;
1871 if (!va)
1872 return false;
1874 if (va->va_end > end) {
1875 *pnext = va;
1876 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1877 } else {
1878 *pprev = va;
1879 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
1881 return true;
1885 * pvm_determine_end - find the highest aligned address between two vmap_areas
1886 * @pnext: in/out arg for the next vmap_area
1887 * @pprev: in/out arg for the previous vmap_area
1888 * @align: alignment
1890 * Returns: determined end address
1892 * Find the highest aligned address between *@pnext and *@pprev below
1893 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
1894 * down address is between the end addresses of the two vmap_areas.
1896 * Please note that the address returned by this function may fall
1897 * inside *@pnext vmap_area. The caller is responsible for checking
1898 * that.
1900 static unsigned long pvm_determine_end(struct vmap_area **pnext,
1901 struct vmap_area **pprev,
1902 unsigned long align)
1904 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1905 unsigned long addr;
1907 if (*pnext)
1908 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
1909 else
1910 addr = vmalloc_end;
1912 while (*pprev && (*pprev)->va_end > addr) {
1913 *pnext = *pprev;
1914 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
1917 return addr;
1921 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
1922 * @offsets: array containing offset of each area
1923 * @sizes: array containing size of each area
1924 * @nr_vms: the number of areas to allocate
1925 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
1926 * @gfp_mask: allocation mask
1928 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
1929 * vm_structs on success, %NULL on failure
1931 * Percpu allocator wants to use congruent vm areas so that it can
1932 * maintain the offsets among percpu areas. This function allocates
1933 * congruent vmalloc areas for it. These areas tend to be scattered
1934 * pretty far, distance between two areas easily going up to
1935 * gigabytes. To avoid interacting with regular vmallocs, these areas
1936 * are allocated from top.
1938 * Despite its complicated look, this allocator is rather simple. It
1939 * does everything top-down and scans areas from the end looking for
1940 * matching slot. While scanning, if any of the areas overlaps with
1941 * existing vmap_area, the base address is pulled down to fit the
1942 * area. Scanning is repeated till all the areas fit and then all
1943 * necessary data structres are inserted and the result is returned.
1945 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
1946 const size_t *sizes, int nr_vms,
1947 size_t align, gfp_t gfp_mask)
1949 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
1950 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
1951 struct vmap_area **vas, *prev, *next;
1952 struct vm_struct **vms;
1953 int area, area2, last_area, term_area;
1954 unsigned long base, start, end, last_end;
1955 bool purged = false;
1957 gfp_mask &= GFP_RECLAIM_MASK;
1959 /* verify parameters and allocate data structures */
1960 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
1961 for (last_area = 0, area = 0; area < nr_vms; area++) {
1962 start = offsets[area];
1963 end = start + sizes[area];
1965 /* is everything aligned properly? */
1966 BUG_ON(!IS_ALIGNED(offsets[area], align));
1967 BUG_ON(!IS_ALIGNED(sizes[area], align));
1969 /* detect the area with the highest address */
1970 if (start > offsets[last_area])
1971 last_area = area;
1973 for (area2 = 0; area2 < nr_vms; area2++) {
1974 unsigned long start2 = offsets[area2];
1975 unsigned long end2 = start2 + sizes[area2];
1977 if (area2 == area)
1978 continue;
1980 BUG_ON(start2 >= start && start2 < end);
1981 BUG_ON(end2 <= end && end2 > start);
1984 last_end = offsets[last_area] + sizes[last_area];
1986 if (vmalloc_end - vmalloc_start < last_end) {
1987 WARN_ON(true);
1988 return NULL;
1991 vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask);
1992 vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask);
1993 if (!vas || !vms)
1994 goto err_free;
1996 for (area = 0; area < nr_vms; area++) {
1997 vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask);
1998 vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask);
1999 if (!vas[area] || !vms[area])
2000 goto err_free;
2002 retry:
2003 spin_lock(&vmap_area_lock);
2005 /* start scanning - we scan from the top, begin with the last area */
2006 area = term_area = last_area;
2007 start = offsets[area];
2008 end = start + sizes[area];
2010 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2011 base = vmalloc_end - last_end;
2012 goto found;
2014 base = pvm_determine_end(&next, &prev, align) - end;
2016 while (true) {
2017 BUG_ON(next && next->va_end <= base + end);
2018 BUG_ON(prev && prev->va_end > base + end);
2021 * base might have underflowed, add last_end before
2022 * comparing.
2024 if (base + last_end < vmalloc_start + last_end) {
2025 spin_unlock(&vmap_area_lock);
2026 if (!purged) {
2027 purge_vmap_area_lazy();
2028 purged = true;
2029 goto retry;
2031 goto err_free;
2035 * If next overlaps, move base downwards so that it's
2036 * right below next and then recheck.
2038 if (next && next->va_start < base + end) {
2039 base = pvm_determine_end(&next, &prev, align) - end;
2040 term_area = area;
2041 continue;
2045 * If prev overlaps, shift down next and prev and move
2046 * base so that it's right below new next and then
2047 * recheck.
2049 if (prev && prev->va_end > base + start) {
2050 next = prev;
2051 prev = node_to_va(rb_prev(&next->rb_node));
2052 base = pvm_determine_end(&next, &prev, align) - end;
2053 term_area = area;
2054 continue;
2058 * This area fits, move on to the previous one. If
2059 * the previous one is the terminal one, we're done.
2061 area = (area + nr_vms - 1) % nr_vms;
2062 if (area == term_area)
2063 break;
2064 start = offsets[area];
2065 end = start + sizes[area];
2066 pvm_find_next_prev(base + end, &next, &prev);
2068 found:
2069 /* we've found a fitting base, insert all va's */
2070 for (area = 0; area < nr_vms; area++) {
2071 struct vmap_area *va = vas[area];
2073 va->va_start = base + offsets[area];
2074 va->va_end = va->va_start + sizes[area];
2075 __insert_vmap_area(va);
2078 vmap_area_pcpu_hole = base + offsets[last_area];
2080 spin_unlock(&vmap_area_lock);
2082 /* insert all vm's */
2083 for (area = 0; area < nr_vms; area++)
2084 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2085 pcpu_get_vm_areas);
2087 kfree(vas);
2088 return vms;
2090 err_free:
2091 for (area = 0; area < nr_vms; area++) {
2092 if (vas)
2093 kfree(vas[area]);
2094 if (vms)
2095 kfree(vms[area]);
2097 kfree(vas);
2098 kfree(vms);
2099 return NULL;
2103 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2104 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2105 * @nr_vms: the number of allocated areas
2107 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2109 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2111 int i;
2113 for (i = 0; i < nr_vms; i++)
2114 free_vm_area(vms[i]);
2115 kfree(vms);
2118 #ifdef CONFIG_PROC_FS
2119 static void *s_start(struct seq_file *m, loff_t *pos)
2121 loff_t n = *pos;
2122 struct vm_struct *v;
2124 read_lock(&vmlist_lock);
2125 v = vmlist;
2126 while (n > 0 && v) {
2127 n--;
2128 v = v->next;
2130 if (!n)
2131 return v;
2133 return NULL;
2137 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2139 struct vm_struct *v = p;
2141 ++*pos;
2142 return v->next;
2145 static void s_stop(struct seq_file *m, void *p)
2147 read_unlock(&vmlist_lock);
2150 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2152 if (NUMA_BUILD) {
2153 unsigned int nr, *counters = m->private;
2155 if (!counters)
2156 return;
2158 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2160 for (nr = 0; nr < v->nr_pages; nr++)
2161 counters[page_to_nid(v->pages[nr])]++;
2163 for_each_node_state(nr, N_HIGH_MEMORY)
2164 if (counters[nr])
2165 seq_printf(m, " N%u=%u", nr, counters[nr]);
2169 static int s_show(struct seq_file *m, void *p)
2171 struct vm_struct *v = p;
2173 seq_printf(m, "0x%p-0x%p %7ld",
2174 v->addr, v->addr + v->size, v->size);
2176 if (v->caller) {
2177 char buff[KSYM_SYMBOL_LEN];
2179 seq_putc(m, ' ');
2180 sprint_symbol(buff, (unsigned long)v->caller);
2181 seq_puts(m, buff);
2184 if (v->nr_pages)
2185 seq_printf(m, " pages=%d", v->nr_pages);
2187 if (v->phys_addr)
2188 seq_printf(m, " phys=%lx", v->phys_addr);
2190 if (v->flags & VM_IOREMAP)
2191 seq_printf(m, " ioremap");
2193 if (v->flags & VM_ALLOC)
2194 seq_printf(m, " vmalloc");
2196 if (v->flags & VM_MAP)
2197 seq_printf(m, " vmap");
2199 if (v->flags & VM_USERMAP)
2200 seq_printf(m, " user");
2202 if (v->flags & VM_VPAGES)
2203 seq_printf(m, " vpages");
2205 show_numa_info(m, v);
2206 seq_putc(m, '\n');
2207 return 0;
2210 static const struct seq_operations vmalloc_op = {
2211 .start = s_start,
2212 .next = s_next,
2213 .stop = s_stop,
2214 .show = s_show,
2217 static int vmalloc_open(struct inode *inode, struct file *file)
2219 unsigned int *ptr = NULL;
2220 int ret;
2222 if (NUMA_BUILD)
2223 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2224 ret = seq_open(file, &vmalloc_op);
2225 if (!ret) {
2226 struct seq_file *m = file->private_data;
2227 m->private = ptr;
2228 } else
2229 kfree(ptr);
2230 return ret;
2233 static const struct file_operations proc_vmalloc_operations = {
2234 .open = vmalloc_open,
2235 .read = seq_read,
2236 .llseek = seq_lseek,
2237 .release = seq_release_private,
2240 static int __init proc_vmalloc_init(void)
2242 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2243 return 0;
2245 module_init(proc_vmalloc_init);
2246 #endif