Now it works.
[cbs-scheduler.git] / mm / vmalloc.c
blobaf58324c361addc715ee2eb9b839bada21cb2773
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/bootmem.h>
27 #include <linux/pfn.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);
269 static struct vmap_area *__find_vmap_area(unsigned long addr)
271 struct rb_node *n = vmap_area_root.rb_node;
273 while (n) {
274 struct vmap_area *va;
276 va = rb_entry(n, struct vmap_area, rb_node);
277 if (addr < va->va_start)
278 n = n->rb_left;
279 else if (addr > va->va_start)
280 n = n->rb_right;
281 else
282 return va;
285 return NULL;
288 static void __insert_vmap_area(struct vmap_area *va)
290 struct rb_node **p = &vmap_area_root.rb_node;
291 struct rb_node *parent = NULL;
292 struct rb_node *tmp;
294 while (*p) {
295 struct vmap_area *tmp;
297 parent = *p;
298 tmp = rb_entry(parent, struct vmap_area, rb_node);
299 if (va->va_start < tmp->va_end)
300 p = &(*p)->rb_left;
301 else if (va->va_end > tmp->va_start)
302 p = &(*p)->rb_right;
303 else
304 BUG();
307 rb_link_node(&va->rb_node, parent, p);
308 rb_insert_color(&va->rb_node, &vmap_area_root);
310 /* address-sort this list so it is usable like the vmlist */
311 tmp = rb_prev(&va->rb_node);
312 if (tmp) {
313 struct vmap_area *prev;
314 prev = rb_entry(tmp, struct vmap_area, rb_node);
315 list_add_rcu(&va->list, &prev->list);
316 } else
317 list_add_rcu(&va->list, &vmap_area_list);
320 static void purge_vmap_area_lazy(void);
323 * Allocate a region of KVA of the specified size and alignment, within the
324 * vstart and vend.
326 static struct vmap_area *alloc_vmap_area(unsigned long size,
327 unsigned long align,
328 unsigned long vstart, unsigned long vend,
329 int node, gfp_t gfp_mask)
331 struct vmap_area *va;
332 struct rb_node *n;
333 unsigned long addr;
334 int purged = 0;
336 BUG_ON(!size);
337 BUG_ON(size & ~PAGE_MASK);
339 va = kmalloc_node(sizeof(struct vmap_area),
340 gfp_mask & GFP_RECLAIM_MASK, node);
341 if (unlikely(!va))
342 return ERR_PTR(-ENOMEM);
344 retry:
345 addr = ALIGN(vstart, align);
347 spin_lock(&vmap_area_lock);
348 if (addr + size - 1 < addr)
349 goto overflow;
351 /* XXX: could have a last_hole cache */
352 n = vmap_area_root.rb_node;
353 if (n) {
354 struct vmap_area *first = NULL;
356 do {
357 struct vmap_area *tmp;
358 tmp = rb_entry(n, struct vmap_area, rb_node);
359 if (tmp->va_end >= addr) {
360 if (!first && tmp->va_start < addr + size)
361 first = tmp;
362 n = n->rb_left;
363 } else {
364 first = tmp;
365 n = n->rb_right;
367 } while (n);
369 if (!first)
370 goto found;
372 if (first->va_end < addr) {
373 n = rb_next(&first->rb_node);
374 if (n)
375 first = rb_entry(n, struct vmap_area, rb_node);
376 else
377 goto found;
380 while (addr + size > first->va_start && addr + size <= vend) {
381 addr = ALIGN(first->va_end + PAGE_SIZE, align);
382 if (addr + size - 1 < addr)
383 goto overflow;
385 n = rb_next(&first->rb_node);
386 if (n)
387 first = rb_entry(n, struct vmap_area, rb_node);
388 else
389 goto found;
392 found:
393 if (addr + size > vend) {
394 overflow:
395 spin_unlock(&vmap_area_lock);
396 if (!purged) {
397 purge_vmap_area_lazy();
398 purged = 1;
399 goto retry;
401 if (printk_ratelimit())
402 printk(KERN_WARNING
403 "vmap allocation for size %lu failed: "
404 "use vmalloc=<size> to increase size.\n", size);
405 return ERR_PTR(-EBUSY);
408 BUG_ON(addr & (align-1));
410 va->va_start = addr;
411 va->va_end = addr + size;
412 va->flags = 0;
413 __insert_vmap_area(va);
414 spin_unlock(&vmap_area_lock);
416 return va;
419 static void rcu_free_va(struct rcu_head *head)
421 struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
423 kfree(va);
426 static void __free_vmap_area(struct vmap_area *va)
428 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
429 rb_erase(&va->rb_node, &vmap_area_root);
430 RB_CLEAR_NODE(&va->rb_node);
431 list_del_rcu(&va->list);
433 call_rcu(&va->rcu_head, rcu_free_va);
437 * Free a region of KVA allocated by alloc_vmap_area
439 static void free_vmap_area(struct vmap_area *va)
441 spin_lock(&vmap_area_lock);
442 __free_vmap_area(va);
443 spin_unlock(&vmap_area_lock);
447 * Clear the pagetable entries of a given vmap_area
449 static void unmap_vmap_area(struct vmap_area *va)
451 vunmap_page_range(va->va_start, va->va_end);
454 static void vmap_debug_free_range(unsigned long start, unsigned long end)
457 * Unmap page tables and force a TLB flush immediately if
458 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
459 * bugs similarly to those in linear kernel virtual address
460 * space after a page has been freed.
462 * All the lazy freeing logic is still retained, in order to
463 * minimise intrusiveness of this debugging feature.
465 * This is going to be *slow* (linear kernel virtual address
466 * debugging doesn't do a broadcast TLB flush so it is a lot
467 * faster).
469 #ifdef CONFIG_DEBUG_PAGEALLOC
470 vunmap_page_range(start, end);
471 flush_tlb_kernel_range(start, end);
472 #endif
476 * lazy_max_pages is the maximum amount of virtual address space we gather up
477 * before attempting to purge with a TLB flush.
479 * There is a tradeoff here: a larger number will cover more kernel page tables
480 * and take slightly longer to purge, but it will linearly reduce the number of
481 * global TLB flushes that must be performed. It would seem natural to scale
482 * this number up linearly with the number of CPUs (because vmapping activity
483 * could also scale linearly with the number of CPUs), however it is likely
484 * that in practice, workloads might be constrained in other ways that mean
485 * vmap activity will not scale linearly with CPUs. Also, I want to be
486 * conservative and not introduce a big latency on huge systems, so go with
487 * a less aggressive log scale. It will still be an improvement over the old
488 * code, and it will be simple to change the scale factor if we find that it
489 * becomes a problem on bigger systems.
491 static unsigned long lazy_max_pages(void)
493 unsigned int log;
495 log = fls(num_online_cpus());
497 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
500 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
503 * Purges all lazily-freed vmap areas.
505 * If sync is 0 then don't purge if there is already a purge in progress.
506 * If force_flush is 1, then flush kernel TLBs between *start and *end even
507 * if we found no lazy vmap areas to unmap (callers can use this to optimise
508 * their own TLB flushing).
509 * Returns with *start = min(*start, lowest purged address)
510 * *end = max(*end, highest purged address)
512 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
513 int sync, int force_flush)
515 static DEFINE_SPINLOCK(purge_lock);
516 LIST_HEAD(valist);
517 struct vmap_area *va;
518 struct vmap_area *n_va;
519 int nr = 0;
522 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
523 * should not expect such behaviour. This just simplifies locking for
524 * the case that isn't actually used at the moment anyway.
526 if (!sync && !force_flush) {
527 if (!spin_trylock(&purge_lock))
528 return;
529 } else
530 spin_lock(&purge_lock);
532 rcu_read_lock();
533 list_for_each_entry_rcu(va, &vmap_area_list, list) {
534 if (va->flags & VM_LAZY_FREE) {
535 if (va->va_start < *start)
536 *start = va->va_start;
537 if (va->va_end > *end)
538 *end = va->va_end;
539 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
540 unmap_vmap_area(va);
541 list_add_tail(&va->purge_list, &valist);
542 va->flags |= VM_LAZY_FREEING;
543 va->flags &= ~VM_LAZY_FREE;
546 rcu_read_unlock();
548 if (nr) {
549 BUG_ON(nr > atomic_read(&vmap_lazy_nr));
550 atomic_sub(nr, &vmap_lazy_nr);
553 if (nr || force_flush)
554 flush_tlb_kernel_range(*start, *end);
556 if (nr) {
557 spin_lock(&vmap_area_lock);
558 list_for_each_entry_safe(va, n_va, &valist, purge_list)
559 __free_vmap_area(va);
560 spin_unlock(&vmap_area_lock);
562 spin_unlock(&purge_lock);
566 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
567 * is already purging.
569 static void try_purge_vmap_area_lazy(void)
571 unsigned long start = ULONG_MAX, end = 0;
573 __purge_vmap_area_lazy(&start, &end, 0, 0);
577 * Kick off a purge of the outstanding lazy areas.
579 static void purge_vmap_area_lazy(void)
581 unsigned long start = ULONG_MAX, end = 0;
583 __purge_vmap_area_lazy(&start, &end, 1, 0);
587 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
588 * called for the correct range previously.
590 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
592 va->flags |= VM_LAZY_FREE;
593 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
594 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
595 try_purge_vmap_area_lazy();
599 * Free and unmap a vmap area
601 static void free_unmap_vmap_area(struct vmap_area *va)
603 flush_cache_vunmap(va->va_start, va->va_end);
604 free_unmap_vmap_area_noflush(va);
607 static struct vmap_area *find_vmap_area(unsigned long addr)
609 struct vmap_area *va;
611 spin_lock(&vmap_area_lock);
612 va = __find_vmap_area(addr);
613 spin_unlock(&vmap_area_lock);
615 return va;
618 static void free_unmap_vmap_area_addr(unsigned long addr)
620 struct vmap_area *va;
622 va = find_vmap_area(addr);
623 BUG_ON(!va);
624 free_unmap_vmap_area(va);
628 /*** Per cpu kva allocator ***/
631 * vmap space is limited especially on 32 bit architectures. Ensure there is
632 * room for at least 16 percpu vmap blocks per CPU.
635 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
636 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
637 * instead (we just need a rough idea)
639 #if BITS_PER_LONG == 32
640 #define VMALLOC_SPACE (128UL*1024*1024)
641 #else
642 #define VMALLOC_SPACE (128UL*1024*1024*1024)
643 #endif
645 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
646 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
647 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
648 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
649 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
650 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
651 #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
652 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
653 VMALLOC_PAGES / NR_CPUS / 16))
655 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
657 static bool vmap_initialized __read_mostly = false;
659 struct vmap_block_queue {
660 spinlock_t lock;
661 struct list_head free;
662 struct list_head dirty;
663 unsigned int nr_dirty;
666 struct vmap_block {
667 spinlock_t lock;
668 struct vmap_area *va;
669 struct vmap_block_queue *vbq;
670 unsigned long free, dirty;
671 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
672 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
673 union {
674 struct {
675 struct list_head free_list;
676 struct list_head dirty_list;
678 struct rcu_head rcu_head;
682 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
683 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
686 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
687 * in the free path. Could get rid of this if we change the API to return a
688 * "cookie" from alloc, to be passed to free. But no big deal yet.
690 static DEFINE_SPINLOCK(vmap_block_tree_lock);
691 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
694 * We should probably have a fallback mechanism to allocate virtual memory
695 * out of partially filled vmap blocks. However vmap block sizing should be
696 * fairly reasonable according to the vmalloc size, so it shouldn't be a
697 * big problem.
700 static unsigned long addr_to_vb_idx(unsigned long addr)
702 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
703 addr /= VMAP_BLOCK_SIZE;
704 return addr;
707 static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
709 struct vmap_block_queue *vbq;
710 struct vmap_block *vb;
711 struct vmap_area *va;
712 unsigned long vb_idx;
713 int node, err;
715 node = numa_node_id();
717 vb = kmalloc_node(sizeof(struct vmap_block),
718 gfp_mask & GFP_RECLAIM_MASK, node);
719 if (unlikely(!vb))
720 return ERR_PTR(-ENOMEM);
722 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
723 VMALLOC_START, VMALLOC_END,
724 node, gfp_mask);
725 if (unlikely(IS_ERR(va))) {
726 kfree(vb);
727 return ERR_PTR(PTR_ERR(va));
730 err = radix_tree_preload(gfp_mask);
731 if (unlikely(err)) {
732 kfree(vb);
733 free_vmap_area(va);
734 return ERR_PTR(err);
737 spin_lock_init(&vb->lock);
738 vb->va = va;
739 vb->free = VMAP_BBMAP_BITS;
740 vb->dirty = 0;
741 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
742 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
743 INIT_LIST_HEAD(&vb->free_list);
744 INIT_LIST_HEAD(&vb->dirty_list);
746 vb_idx = addr_to_vb_idx(va->va_start);
747 spin_lock(&vmap_block_tree_lock);
748 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
749 spin_unlock(&vmap_block_tree_lock);
750 BUG_ON(err);
751 radix_tree_preload_end();
753 vbq = &get_cpu_var(vmap_block_queue);
754 vb->vbq = vbq;
755 spin_lock(&vbq->lock);
756 list_add(&vb->free_list, &vbq->free);
757 spin_unlock(&vbq->lock);
758 put_cpu_var(vmap_cpu_blocks);
760 return vb;
763 static void rcu_free_vb(struct rcu_head *head)
765 struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
767 kfree(vb);
770 static void free_vmap_block(struct vmap_block *vb)
772 struct vmap_block *tmp;
773 unsigned long vb_idx;
775 spin_lock(&vb->vbq->lock);
776 if (!list_empty(&vb->free_list))
777 list_del(&vb->free_list);
778 if (!list_empty(&vb->dirty_list))
779 list_del(&vb->dirty_list);
780 spin_unlock(&vb->vbq->lock);
782 vb_idx = addr_to_vb_idx(vb->va->va_start);
783 spin_lock(&vmap_block_tree_lock);
784 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
785 spin_unlock(&vmap_block_tree_lock);
786 BUG_ON(tmp != vb);
788 free_unmap_vmap_area_noflush(vb->va);
789 call_rcu(&vb->rcu_head, rcu_free_vb);
792 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
794 struct vmap_block_queue *vbq;
795 struct vmap_block *vb;
796 unsigned long addr = 0;
797 unsigned int order;
799 BUG_ON(size & ~PAGE_MASK);
800 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
801 order = get_order(size);
803 again:
804 rcu_read_lock();
805 vbq = &get_cpu_var(vmap_block_queue);
806 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
807 int i;
809 spin_lock(&vb->lock);
810 i = bitmap_find_free_region(vb->alloc_map,
811 VMAP_BBMAP_BITS, order);
813 if (i >= 0) {
814 addr = vb->va->va_start + (i << PAGE_SHIFT);
815 BUG_ON(addr_to_vb_idx(addr) !=
816 addr_to_vb_idx(vb->va->va_start));
817 vb->free -= 1UL << order;
818 if (vb->free == 0) {
819 spin_lock(&vbq->lock);
820 list_del_init(&vb->free_list);
821 spin_unlock(&vbq->lock);
823 spin_unlock(&vb->lock);
824 break;
826 spin_unlock(&vb->lock);
828 put_cpu_var(vmap_cpu_blocks);
829 rcu_read_unlock();
831 if (!addr) {
832 vb = new_vmap_block(gfp_mask);
833 if (IS_ERR(vb))
834 return vb;
835 goto again;
838 return (void *)addr;
841 static void vb_free(const void *addr, unsigned long size)
843 unsigned long offset;
844 unsigned long vb_idx;
845 unsigned int order;
846 struct vmap_block *vb;
848 BUG_ON(size & ~PAGE_MASK);
849 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
851 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
853 order = get_order(size);
855 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
857 vb_idx = addr_to_vb_idx((unsigned long)addr);
858 rcu_read_lock();
859 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
860 rcu_read_unlock();
861 BUG_ON(!vb);
863 spin_lock(&vb->lock);
864 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
865 if (!vb->dirty) {
866 spin_lock(&vb->vbq->lock);
867 list_add(&vb->dirty_list, &vb->vbq->dirty);
868 spin_unlock(&vb->vbq->lock);
870 vb->dirty += 1UL << order;
871 if (vb->dirty == VMAP_BBMAP_BITS) {
872 BUG_ON(vb->free || !list_empty(&vb->free_list));
873 spin_unlock(&vb->lock);
874 free_vmap_block(vb);
875 } else
876 spin_unlock(&vb->lock);
880 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
882 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
883 * to amortize TLB flushing overheads. What this means is that any page you
884 * have now, may, in a former life, have been mapped into kernel virtual
885 * address by the vmap layer and so there might be some CPUs with TLB entries
886 * still referencing that page (additional to the regular 1:1 kernel mapping).
888 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
889 * be sure that none of the pages we have control over will have any aliases
890 * from the vmap layer.
892 void vm_unmap_aliases(void)
894 unsigned long start = ULONG_MAX, end = 0;
895 int cpu;
896 int flush = 0;
898 if (unlikely(!vmap_initialized))
899 return;
901 for_each_possible_cpu(cpu) {
902 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
903 struct vmap_block *vb;
905 rcu_read_lock();
906 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
907 int i;
909 spin_lock(&vb->lock);
910 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
911 while (i < VMAP_BBMAP_BITS) {
912 unsigned long s, e;
913 int j;
914 j = find_next_zero_bit(vb->dirty_map,
915 VMAP_BBMAP_BITS, i);
917 s = vb->va->va_start + (i << PAGE_SHIFT);
918 e = vb->va->va_start + (j << PAGE_SHIFT);
919 vunmap_page_range(s, e);
920 flush = 1;
922 if (s < start)
923 start = s;
924 if (e > end)
925 end = e;
927 i = j;
928 i = find_next_bit(vb->dirty_map,
929 VMAP_BBMAP_BITS, i);
931 spin_unlock(&vb->lock);
933 rcu_read_unlock();
936 __purge_vmap_area_lazy(&start, &end, 1, flush);
938 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
941 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
942 * @mem: the pointer returned by vm_map_ram
943 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
945 void vm_unmap_ram(const void *mem, unsigned int count)
947 unsigned long size = count << PAGE_SHIFT;
948 unsigned long addr = (unsigned long)mem;
950 BUG_ON(!addr);
951 BUG_ON(addr < VMALLOC_START);
952 BUG_ON(addr > VMALLOC_END);
953 BUG_ON(addr & (PAGE_SIZE-1));
955 debug_check_no_locks_freed(mem, size);
956 vmap_debug_free_range(addr, addr+size);
958 if (likely(count <= VMAP_MAX_ALLOC))
959 vb_free(mem, size);
960 else
961 free_unmap_vmap_area_addr(addr);
963 EXPORT_SYMBOL(vm_unmap_ram);
966 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
967 * @pages: an array of pointers to the pages to be mapped
968 * @count: number of pages
969 * @node: prefer to allocate data structures on this node
970 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
972 * Returns: a pointer to the address that has been mapped, or %NULL on failure
974 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
976 unsigned long size = count << PAGE_SHIFT;
977 unsigned long addr;
978 void *mem;
980 if (likely(count <= VMAP_MAX_ALLOC)) {
981 mem = vb_alloc(size, GFP_KERNEL);
982 if (IS_ERR(mem))
983 return NULL;
984 addr = (unsigned long)mem;
985 } else {
986 struct vmap_area *va;
987 va = alloc_vmap_area(size, PAGE_SIZE,
988 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
989 if (IS_ERR(va))
990 return NULL;
992 addr = va->va_start;
993 mem = (void *)addr;
995 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
996 vm_unmap_ram(mem, count);
997 return NULL;
999 return mem;
1001 EXPORT_SYMBOL(vm_map_ram);
1004 * vm_area_register_early - register vmap area early during boot
1005 * @vm: vm_struct to register
1006 * @align: requested alignment
1008 * This function is used to register kernel vm area before
1009 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1010 * proper values on entry and other fields should be zero. On return,
1011 * vm->addr contains the allocated address.
1013 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1015 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1017 static size_t vm_init_off __initdata;
1018 unsigned long addr;
1020 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1021 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1023 vm->addr = (void *)addr;
1025 vm->next = vmlist;
1026 vmlist = vm;
1029 void __init vmalloc_init(void)
1031 struct vmap_area *va;
1032 struct vm_struct *tmp;
1033 int i;
1035 for_each_possible_cpu(i) {
1036 struct vmap_block_queue *vbq;
1038 vbq = &per_cpu(vmap_block_queue, i);
1039 spin_lock_init(&vbq->lock);
1040 INIT_LIST_HEAD(&vbq->free);
1041 INIT_LIST_HEAD(&vbq->dirty);
1042 vbq->nr_dirty = 0;
1045 /* Import existing vmlist entries. */
1046 for (tmp = vmlist; tmp; tmp = tmp->next) {
1047 va = alloc_bootmem(sizeof(struct vmap_area));
1048 va->flags = tmp->flags | VM_VM_AREA;
1049 va->va_start = (unsigned long)tmp->addr;
1050 va->va_end = va->va_start + tmp->size;
1051 __insert_vmap_area(va);
1053 vmap_initialized = true;
1057 * map_kernel_range_noflush - map kernel VM area with the specified pages
1058 * @addr: start of the VM area to map
1059 * @size: size of the VM area to map
1060 * @prot: page protection flags to use
1061 * @pages: pages to map
1063 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1064 * specify should have been allocated using get_vm_area() and its
1065 * friends.
1067 * NOTE:
1068 * This function does NOT do any cache flushing. The caller is
1069 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1070 * before calling this function.
1072 * RETURNS:
1073 * The number of pages mapped on success, -errno on failure.
1075 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1076 pgprot_t prot, struct page **pages)
1078 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1082 * unmap_kernel_range_noflush - unmap kernel VM area
1083 * @addr: start of the VM area to unmap
1084 * @size: size of the VM area to unmap
1086 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1087 * specify should have been allocated using get_vm_area() and its
1088 * friends.
1090 * NOTE:
1091 * This function does NOT do any cache flushing. The caller is
1092 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1093 * before calling this function and flush_tlb_kernel_range() after.
1095 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1097 vunmap_page_range(addr, addr + size);
1101 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1102 * @addr: start of the VM area to unmap
1103 * @size: size of the VM area to unmap
1105 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1106 * the unmapping and tlb after.
1108 void unmap_kernel_range(unsigned long addr, unsigned long size)
1110 unsigned long end = addr + size;
1112 flush_cache_vunmap(addr, end);
1113 vunmap_page_range(addr, end);
1114 flush_tlb_kernel_range(addr, end);
1117 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1119 unsigned long addr = (unsigned long)area->addr;
1120 unsigned long end = addr + area->size - PAGE_SIZE;
1121 int err;
1123 err = vmap_page_range(addr, end, prot, *pages);
1124 if (err > 0) {
1125 *pages += err;
1126 err = 0;
1129 return err;
1131 EXPORT_SYMBOL_GPL(map_vm_area);
1133 /*** Old vmalloc interfaces ***/
1134 DEFINE_RWLOCK(vmlist_lock);
1135 struct vm_struct *vmlist;
1137 static struct vm_struct *__get_vm_area_node(unsigned long size,
1138 unsigned long flags, unsigned long start, unsigned long end,
1139 int node, gfp_t gfp_mask, void *caller)
1141 static struct vmap_area *va;
1142 struct vm_struct *area;
1143 struct vm_struct *tmp, **p;
1144 unsigned long align = 1;
1146 BUG_ON(in_interrupt());
1147 if (flags & VM_IOREMAP) {
1148 int bit = fls(size);
1150 if (bit > IOREMAP_MAX_ORDER)
1151 bit = IOREMAP_MAX_ORDER;
1152 else if (bit < PAGE_SHIFT)
1153 bit = PAGE_SHIFT;
1155 align = 1ul << bit;
1158 size = PAGE_ALIGN(size);
1159 if (unlikely(!size))
1160 return NULL;
1162 area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1163 if (unlikely(!area))
1164 return NULL;
1167 * We always allocate a guard page.
1169 size += PAGE_SIZE;
1171 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1172 if (IS_ERR(va)) {
1173 kfree(area);
1174 return NULL;
1177 area->flags = flags;
1178 area->addr = (void *)va->va_start;
1179 area->size = size;
1180 area->pages = NULL;
1181 area->nr_pages = 0;
1182 area->phys_addr = 0;
1183 area->caller = caller;
1184 va->private = area;
1185 va->flags |= VM_VM_AREA;
1187 write_lock(&vmlist_lock);
1188 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1189 if (tmp->addr >= area->addr)
1190 break;
1192 area->next = *p;
1193 *p = area;
1194 write_unlock(&vmlist_lock);
1196 return area;
1199 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1200 unsigned long start, unsigned long end)
1202 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1203 __builtin_return_address(0));
1205 EXPORT_SYMBOL_GPL(__get_vm_area);
1207 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1208 unsigned long start, unsigned long end,
1209 void *caller)
1211 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1212 caller);
1216 * get_vm_area - reserve a contiguous kernel virtual area
1217 * @size: size of the area
1218 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1220 * Search an area of @size in the kernel virtual mapping area,
1221 * and reserved it for out purposes. Returns the area descriptor
1222 * on success or %NULL on failure.
1224 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1226 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1227 -1, GFP_KERNEL, __builtin_return_address(0));
1230 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1231 void *caller)
1233 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1234 -1, GFP_KERNEL, caller);
1237 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1238 int node, gfp_t gfp_mask)
1240 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
1241 gfp_mask, __builtin_return_address(0));
1244 static struct vm_struct *find_vm_area(const void *addr)
1246 struct vmap_area *va;
1248 va = find_vmap_area((unsigned long)addr);
1249 if (va && va->flags & VM_VM_AREA)
1250 return va->private;
1252 return NULL;
1256 * remove_vm_area - find and remove a continuous kernel virtual area
1257 * @addr: base address
1259 * Search for the kernel VM area starting at @addr, and remove it.
1260 * This function returns the found VM area, but using it is NOT safe
1261 * on SMP machines, except for its size or flags.
1263 struct vm_struct *remove_vm_area(const void *addr)
1265 struct vmap_area *va;
1267 va = find_vmap_area((unsigned long)addr);
1268 if (va && va->flags & VM_VM_AREA) {
1269 struct vm_struct *vm = va->private;
1270 struct vm_struct *tmp, **p;
1272 vmap_debug_free_range(va->va_start, va->va_end);
1273 free_unmap_vmap_area(va);
1274 vm->size -= PAGE_SIZE;
1276 write_lock(&vmlist_lock);
1277 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1279 *p = tmp->next;
1280 write_unlock(&vmlist_lock);
1282 return vm;
1284 return NULL;
1287 static void __vunmap(const void *addr, int deallocate_pages)
1289 struct vm_struct *area;
1291 if (!addr)
1292 return;
1294 if ((PAGE_SIZE-1) & (unsigned long)addr) {
1295 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1296 return;
1299 area = remove_vm_area(addr);
1300 if (unlikely(!area)) {
1301 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1302 addr);
1303 return;
1306 debug_check_no_locks_freed(addr, area->size);
1307 debug_check_no_obj_freed(addr, area->size);
1309 if (deallocate_pages) {
1310 int i;
1312 for (i = 0; i < area->nr_pages; i++) {
1313 struct page *page = area->pages[i];
1315 BUG_ON(!page);
1316 __free_page(page);
1319 if (area->flags & VM_VPAGES)
1320 vfree(area->pages);
1321 else
1322 kfree(area->pages);
1325 kfree(area);
1326 return;
1330 * vfree - release memory allocated by vmalloc()
1331 * @addr: memory base address
1333 * Free the virtually continuous memory area starting at @addr, as
1334 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1335 * NULL, no operation is performed.
1337 * Must not be called in interrupt context.
1339 void vfree(const void *addr)
1341 BUG_ON(in_interrupt());
1342 __vunmap(addr, 1);
1344 EXPORT_SYMBOL(vfree);
1347 * vunmap - release virtual mapping obtained by vmap()
1348 * @addr: memory base address
1350 * Free the virtually contiguous memory area starting at @addr,
1351 * which was created from the page array passed to vmap().
1353 * Must not be called in interrupt context.
1355 void vunmap(const void *addr)
1357 BUG_ON(in_interrupt());
1358 might_sleep();
1359 __vunmap(addr, 0);
1361 EXPORT_SYMBOL(vunmap);
1364 * vmap - map an array of pages into virtually contiguous space
1365 * @pages: array of page pointers
1366 * @count: number of pages to map
1367 * @flags: vm_area->flags
1368 * @prot: page protection for the mapping
1370 * Maps @count pages from @pages into contiguous kernel virtual
1371 * space.
1373 void *vmap(struct page **pages, unsigned int count,
1374 unsigned long flags, pgprot_t prot)
1376 struct vm_struct *area;
1378 might_sleep();
1380 if (count > num_physpages)
1381 return NULL;
1383 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1384 __builtin_return_address(0));
1385 if (!area)
1386 return NULL;
1388 if (map_vm_area(area, prot, &pages)) {
1389 vunmap(area->addr);
1390 return NULL;
1393 return area->addr;
1395 EXPORT_SYMBOL(vmap);
1397 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1398 int node, void *caller);
1399 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1400 pgprot_t prot, int node, void *caller)
1402 struct page **pages;
1403 unsigned int nr_pages, array_size, i;
1405 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1406 array_size = (nr_pages * sizeof(struct page *));
1408 area->nr_pages = nr_pages;
1409 /* Please note that the recursion is strictly bounded. */
1410 if (array_size > PAGE_SIZE) {
1411 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
1412 PAGE_KERNEL, node, caller);
1413 area->flags |= VM_VPAGES;
1414 } else {
1415 pages = kmalloc_node(array_size,
1416 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1417 node);
1419 area->pages = pages;
1420 area->caller = caller;
1421 if (!area->pages) {
1422 remove_vm_area(area->addr);
1423 kfree(area);
1424 return NULL;
1427 for (i = 0; i < area->nr_pages; i++) {
1428 struct page *page;
1430 if (node < 0)
1431 page = alloc_page(gfp_mask);
1432 else
1433 page = alloc_pages_node(node, gfp_mask, 0);
1435 if (unlikely(!page)) {
1436 /* Successfully allocated i pages, free them in __vunmap() */
1437 area->nr_pages = i;
1438 goto fail;
1440 area->pages[i] = page;
1443 if (map_vm_area(area, prot, &pages))
1444 goto fail;
1445 return area->addr;
1447 fail:
1448 vfree(area->addr);
1449 return NULL;
1452 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1454 return __vmalloc_area_node(area, gfp_mask, prot, -1,
1455 __builtin_return_address(0));
1459 * __vmalloc_node - allocate virtually contiguous memory
1460 * @size: allocation size
1461 * @gfp_mask: flags for the page level allocator
1462 * @prot: protection mask for the allocated pages
1463 * @node: node to use for allocation or -1
1464 * @caller: caller's return address
1466 * Allocate enough pages to cover @size from the page level
1467 * allocator with @gfp_mask flags. Map them into contiguous
1468 * kernel virtual space, using a pagetable protection of @prot.
1470 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1471 int node, void *caller)
1473 struct vm_struct *area;
1475 size = PAGE_ALIGN(size);
1476 if (!size || (size >> PAGE_SHIFT) > num_physpages)
1477 return NULL;
1479 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1480 node, gfp_mask, caller);
1482 if (!area)
1483 return NULL;
1485 return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1488 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1490 return __vmalloc_node(size, gfp_mask, prot, -1,
1491 __builtin_return_address(0));
1493 EXPORT_SYMBOL(__vmalloc);
1496 * vmalloc - allocate virtually contiguous memory
1497 * @size: allocation size
1498 * Allocate enough pages to cover @size from the page level
1499 * allocator and map them into contiguous kernel virtual space.
1501 * For tight control over page level allocator and protection flags
1502 * use __vmalloc() instead.
1504 void *vmalloc(unsigned long size)
1506 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1507 -1, __builtin_return_address(0));
1509 EXPORT_SYMBOL(vmalloc);
1512 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1513 * @size: allocation size
1515 * The resulting memory area is zeroed so it can be mapped to userspace
1516 * without leaking data.
1518 void *vmalloc_user(unsigned long size)
1520 struct vm_struct *area;
1521 void *ret;
1523 ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1524 PAGE_KERNEL, -1, __builtin_return_address(0));
1525 if (ret) {
1526 area = find_vm_area(ret);
1527 area->flags |= VM_USERMAP;
1529 return ret;
1531 EXPORT_SYMBOL(vmalloc_user);
1534 * vmalloc_node - allocate memory on a specific node
1535 * @size: allocation size
1536 * @node: numa node
1538 * Allocate enough pages to cover @size from the page level
1539 * allocator and map them into contiguous kernel virtual space.
1541 * For tight control over page level allocator and protection flags
1542 * use __vmalloc() instead.
1544 void *vmalloc_node(unsigned long size, int node)
1546 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1547 node, __builtin_return_address(0));
1549 EXPORT_SYMBOL(vmalloc_node);
1551 #ifndef PAGE_KERNEL_EXEC
1552 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1553 #endif
1556 * vmalloc_exec - allocate virtually contiguous, executable memory
1557 * @size: allocation size
1559 * Kernel-internal function to allocate enough pages to cover @size
1560 * the page level allocator and map them into contiguous and
1561 * executable kernel virtual space.
1563 * For tight control over page level allocator and protection flags
1564 * use __vmalloc() instead.
1567 void *vmalloc_exec(unsigned long size)
1569 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1570 -1, __builtin_return_address(0));
1573 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1574 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1575 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1576 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1577 #else
1578 #define GFP_VMALLOC32 GFP_KERNEL
1579 #endif
1582 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1583 * @size: allocation size
1585 * Allocate enough 32bit PA addressable pages to cover @size from the
1586 * page level allocator and map them into contiguous kernel virtual space.
1588 void *vmalloc_32(unsigned long size)
1590 return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1591 -1, __builtin_return_address(0));
1593 EXPORT_SYMBOL(vmalloc_32);
1596 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1597 * @size: allocation size
1599 * The resulting memory area is 32bit addressable and zeroed so it can be
1600 * mapped to userspace without leaking data.
1602 void *vmalloc_32_user(unsigned long size)
1604 struct vm_struct *area;
1605 void *ret;
1607 ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1608 -1, __builtin_return_address(0));
1609 if (ret) {
1610 area = find_vm_area(ret);
1611 area->flags |= VM_USERMAP;
1613 return ret;
1615 EXPORT_SYMBOL(vmalloc_32_user);
1617 long vread(char *buf, char *addr, unsigned long count)
1619 struct vm_struct *tmp;
1620 char *vaddr, *buf_start = buf;
1621 unsigned long n;
1623 /* Don't allow overflow */
1624 if ((unsigned long) addr + count < count)
1625 count = -(unsigned long) addr;
1627 read_lock(&vmlist_lock);
1628 for (tmp = vmlist; tmp; tmp = tmp->next) {
1629 vaddr = (char *) tmp->addr;
1630 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1631 continue;
1632 while (addr < vaddr) {
1633 if (count == 0)
1634 goto finished;
1635 *buf = '\0';
1636 buf++;
1637 addr++;
1638 count--;
1640 n = vaddr + tmp->size - PAGE_SIZE - addr;
1641 do {
1642 if (count == 0)
1643 goto finished;
1644 *buf = *addr;
1645 buf++;
1646 addr++;
1647 count--;
1648 } while (--n > 0);
1650 finished:
1651 read_unlock(&vmlist_lock);
1652 return buf - buf_start;
1655 long vwrite(char *buf, char *addr, unsigned long count)
1657 struct vm_struct *tmp;
1658 char *vaddr, *buf_start = buf;
1659 unsigned long n;
1661 /* Don't allow overflow */
1662 if ((unsigned long) addr + count < count)
1663 count = -(unsigned long) addr;
1665 read_lock(&vmlist_lock);
1666 for (tmp = vmlist; tmp; tmp = tmp->next) {
1667 vaddr = (char *) tmp->addr;
1668 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1669 continue;
1670 while (addr < vaddr) {
1671 if (count == 0)
1672 goto finished;
1673 buf++;
1674 addr++;
1675 count--;
1677 n = vaddr + tmp->size - PAGE_SIZE - addr;
1678 do {
1679 if (count == 0)
1680 goto finished;
1681 *addr = *buf;
1682 buf++;
1683 addr++;
1684 count--;
1685 } while (--n > 0);
1687 finished:
1688 read_unlock(&vmlist_lock);
1689 return buf - buf_start;
1693 * remap_vmalloc_range - map vmalloc pages to userspace
1694 * @vma: vma to cover (map full range of vma)
1695 * @addr: vmalloc memory
1696 * @pgoff: number of pages into addr before first page to map
1698 * Returns: 0 for success, -Exxx on failure
1700 * This function checks that addr is a valid vmalloc'ed area, and
1701 * that it is big enough to cover the vma. Will return failure if
1702 * that criteria isn't met.
1704 * Similar to remap_pfn_range() (see mm/memory.c)
1706 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1707 unsigned long pgoff)
1709 struct vm_struct *area;
1710 unsigned long uaddr = vma->vm_start;
1711 unsigned long usize = vma->vm_end - vma->vm_start;
1713 if ((PAGE_SIZE-1) & (unsigned long)addr)
1714 return -EINVAL;
1716 area = find_vm_area(addr);
1717 if (!area)
1718 return -EINVAL;
1720 if (!(area->flags & VM_USERMAP))
1721 return -EINVAL;
1723 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1724 return -EINVAL;
1726 addr += pgoff << PAGE_SHIFT;
1727 do {
1728 struct page *page = vmalloc_to_page(addr);
1729 int ret;
1731 ret = vm_insert_page(vma, uaddr, page);
1732 if (ret)
1733 return ret;
1735 uaddr += PAGE_SIZE;
1736 addr += PAGE_SIZE;
1737 usize -= PAGE_SIZE;
1738 } while (usize > 0);
1740 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
1741 vma->vm_flags |= VM_RESERVED;
1743 return 0;
1745 EXPORT_SYMBOL(remap_vmalloc_range);
1748 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1749 * have one.
1751 void __attribute__((weak)) vmalloc_sync_all(void)
1756 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
1758 /* apply_to_page_range() does all the hard work. */
1759 return 0;
1763 * alloc_vm_area - allocate a range of kernel address space
1764 * @size: size of the area
1766 * Returns: NULL on failure, vm_struct on success
1768 * This function reserves a range of kernel address space, and
1769 * allocates pagetables to map that range. No actual mappings
1770 * are created. If the kernel address space is not shared
1771 * between processes, it syncs the pagetable across all
1772 * processes.
1774 struct vm_struct *alloc_vm_area(size_t size)
1776 struct vm_struct *area;
1778 area = get_vm_area_caller(size, VM_IOREMAP,
1779 __builtin_return_address(0));
1780 if (area == NULL)
1781 return NULL;
1784 * This ensures that page tables are constructed for this region
1785 * of kernel virtual address space and mapped into init_mm.
1787 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1788 area->size, f, NULL)) {
1789 free_vm_area(area);
1790 return NULL;
1793 /* Make sure the pagetables are constructed in process kernel
1794 mappings */
1795 vmalloc_sync_all();
1797 return area;
1799 EXPORT_SYMBOL_GPL(alloc_vm_area);
1801 void free_vm_area(struct vm_struct *area)
1803 struct vm_struct *ret;
1804 ret = remove_vm_area(area->addr);
1805 BUG_ON(ret != area);
1806 kfree(area);
1808 EXPORT_SYMBOL_GPL(free_vm_area);
1811 #ifdef CONFIG_PROC_FS
1812 static void *s_start(struct seq_file *m, loff_t *pos)
1814 loff_t n = *pos;
1815 struct vm_struct *v;
1817 read_lock(&vmlist_lock);
1818 v = vmlist;
1819 while (n > 0 && v) {
1820 n--;
1821 v = v->next;
1823 if (!n)
1824 return v;
1826 return NULL;
1830 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
1832 struct vm_struct *v = p;
1834 ++*pos;
1835 return v->next;
1838 static void s_stop(struct seq_file *m, void *p)
1840 read_unlock(&vmlist_lock);
1843 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
1845 if (NUMA_BUILD) {
1846 unsigned int nr, *counters = m->private;
1848 if (!counters)
1849 return;
1851 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
1853 for (nr = 0; nr < v->nr_pages; nr++)
1854 counters[page_to_nid(v->pages[nr])]++;
1856 for_each_node_state(nr, N_HIGH_MEMORY)
1857 if (counters[nr])
1858 seq_printf(m, " N%u=%u", nr, counters[nr]);
1862 static int s_show(struct seq_file *m, void *p)
1864 struct vm_struct *v = p;
1866 seq_printf(m, "0x%p-0x%p %7ld",
1867 v->addr, v->addr + v->size, v->size);
1869 if (v->caller) {
1870 char buff[KSYM_SYMBOL_LEN];
1872 seq_putc(m, ' ');
1873 sprint_symbol(buff, (unsigned long)v->caller);
1874 seq_puts(m, buff);
1877 if (v->nr_pages)
1878 seq_printf(m, " pages=%d", v->nr_pages);
1880 if (v->phys_addr)
1881 seq_printf(m, " phys=%lx", v->phys_addr);
1883 if (v->flags & VM_IOREMAP)
1884 seq_printf(m, " ioremap");
1886 if (v->flags & VM_ALLOC)
1887 seq_printf(m, " vmalloc");
1889 if (v->flags & VM_MAP)
1890 seq_printf(m, " vmap");
1892 if (v->flags & VM_USERMAP)
1893 seq_printf(m, " user");
1895 if (v->flags & VM_VPAGES)
1896 seq_printf(m, " vpages");
1898 show_numa_info(m, v);
1899 seq_putc(m, '\n');
1900 return 0;
1903 static const struct seq_operations vmalloc_op = {
1904 .start = s_start,
1905 .next = s_next,
1906 .stop = s_stop,
1907 .show = s_show,
1910 static int vmalloc_open(struct inode *inode, struct file *file)
1912 unsigned int *ptr = NULL;
1913 int ret;
1915 if (NUMA_BUILD)
1916 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
1917 ret = seq_open(file, &vmalloc_op);
1918 if (!ret) {
1919 struct seq_file *m = file->private_data;
1920 m->private = ptr;
1921 } else
1922 kfree(ptr);
1923 return ret;
1926 static const struct file_operations proc_vmalloc_operations = {
1927 .open = vmalloc_open,
1928 .read = seq_read,
1929 .llseek = seq_lseek,
1930 .release = seq_release_private,
1933 static int __init proc_vmalloc_init(void)
1935 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
1936 return 0;
1938 module_init(proc_vmalloc_init);
1939 #endif