mmc: sdio: don't require CISTPL_VERS_1 to contain 4 strings
[linux-2.6/linux-2.6-openrd.git] / mm / vmalloc.c
blob2f7c9d75c55224583209a799416971f75d4bc62b
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>
28 #include <asm/atomic.h>
29 #include <asm/uaccess.h>
30 #include <asm/tlbflush.h>
33 /*** Page table manipulation functions ***/
35 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
37 pte_t *pte;
39 pte = pte_offset_kernel(pmd, addr);
40 do {
41 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
42 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
43 } while (pte++, addr += PAGE_SIZE, addr != end);
46 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
48 pmd_t *pmd;
49 unsigned long next;
51 pmd = pmd_offset(pud, addr);
52 do {
53 next = pmd_addr_end(addr, end);
54 if (pmd_none_or_clear_bad(pmd))
55 continue;
56 vunmap_pte_range(pmd, addr, next);
57 } while (pmd++, addr = next, addr != end);
60 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
62 pud_t *pud;
63 unsigned long next;
65 pud = pud_offset(pgd, addr);
66 do {
67 next = pud_addr_end(addr, end);
68 if (pud_none_or_clear_bad(pud))
69 continue;
70 vunmap_pmd_range(pud, addr, next);
71 } while (pud++, addr = next, addr != end);
74 static void vunmap_page_range(unsigned long addr, unsigned long end)
76 pgd_t *pgd;
77 unsigned long next;
79 BUG_ON(addr >= end);
80 pgd = pgd_offset_k(addr);
81 do {
82 next = pgd_addr_end(addr, end);
83 if (pgd_none_or_clear_bad(pgd))
84 continue;
85 vunmap_pud_range(pgd, addr, next);
86 } while (pgd++, addr = next, addr != end);
89 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
90 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
92 pte_t *pte;
95 * nr is a running index into the array which helps higher level
96 * callers keep track of where we're up to.
99 pte = pte_alloc_kernel(pmd, addr);
100 if (!pte)
101 return -ENOMEM;
102 do {
103 struct page *page = pages[*nr];
105 if (WARN_ON(!pte_none(*pte)))
106 return -EBUSY;
107 if (WARN_ON(!page))
108 return -ENOMEM;
109 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
110 (*nr)++;
111 } while (pte++, addr += PAGE_SIZE, addr != end);
112 return 0;
115 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
116 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
118 pmd_t *pmd;
119 unsigned long next;
121 pmd = pmd_alloc(&init_mm, pud, addr);
122 if (!pmd)
123 return -ENOMEM;
124 do {
125 next = pmd_addr_end(addr, end);
126 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
127 return -ENOMEM;
128 } while (pmd++, addr = next, addr != end);
129 return 0;
132 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
133 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
135 pud_t *pud;
136 unsigned long next;
138 pud = pud_alloc(&init_mm, pgd, addr);
139 if (!pud)
140 return -ENOMEM;
141 do {
142 next = pud_addr_end(addr, end);
143 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
144 return -ENOMEM;
145 } while (pud++, addr = next, addr != end);
146 return 0;
150 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
151 * will have pfns corresponding to the "pages" array.
153 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
155 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
156 pgprot_t prot, struct page **pages)
158 pgd_t *pgd;
159 unsigned long next;
160 unsigned long addr = start;
161 int err = 0;
162 int nr = 0;
164 BUG_ON(addr >= end);
165 pgd = pgd_offset_k(addr);
166 do {
167 next = pgd_addr_end(addr, end);
168 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
169 if (err)
170 return err;
171 } while (pgd++, addr = next, addr != end);
173 return nr;
176 static int vmap_page_range(unsigned long start, unsigned long end,
177 pgprot_t prot, struct page **pages)
179 int ret;
181 ret = vmap_page_range_noflush(start, end, prot, pages);
182 flush_cache_vmap(start, end);
183 return ret;
186 int is_vmalloc_or_module_addr(const void *x)
189 * ARM, x86-64 and sparc64 put modules in a special place,
190 * and fall back on vmalloc() if that fails. Others
191 * just put it in the vmalloc space.
193 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
194 unsigned long addr = (unsigned long)x;
195 if (addr >= MODULES_VADDR && addr < MODULES_END)
196 return 1;
197 #endif
198 return is_vmalloc_addr(x);
202 * Walk a vmap address to the struct page it maps.
204 struct page *vmalloc_to_page(const void *vmalloc_addr)
206 unsigned long addr = (unsigned long) vmalloc_addr;
207 struct page *page = NULL;
208 pgd_t *pgd = pgd_offset_k(addr);
211 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
212 * architectures that do not vmalloc module space
214 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
216 if (!pgd_none(*pgd)) {
217 pud_t *pud = pud_offset(pgd, addr);
218 if (!pud_none(*pud)) {
219 pmd_t *pmd = pmd_offset(pud, addr);
220 if (!pmd_none(*pmd)) {
221 pte_t *ptep, pte;
223 ptep = pte_offset_map(pmd, addr);
224 pte = *ptep;
225 if (pte_present(pte))
226 page = pte_page(pte);
227 pte_unmap(ptep);
231 return page;
233 EXPORT_SYMBOL(vmalloc_to_page);
236 * Map a vmalloc()-space virtual address to the physical page frame number.
238 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
240 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
242 EXPORT_SYMBOL(vmalloc_to_pfn);
245 /*** Global kva allocator ***/
247 #define VM_LAZY_FREE 0x01
248 #define VM_LAZY_FREEING 0x02
249 #define VM_VM_AREA 0x04
251 struct vmap_area {
252 unsigned long va_start;
253 unsigned long va_end;
254 unsigned long flags;
255 struct rb_node rb_node; /* address sorted rbtree */
256 struct list_head list; /* address sorted list */
257 struct list_head purge_list; /* "lazy purge" list */
258 void *private;
259 struct rcu_head rcu_head;
262 static DEFINE_SPINLOCK(vmap_area_lock);
263 static struct rb_root vmap_area_root = RB_ROOT;
264 static LIST_HEAD(vmap_area_list);
265 static unsigned long vmap_area_pcpu_hole;
267 static struct vmap_area *__find_vmap_area(unsigned long addr)
269 struct rb_node *n = vmap_area_root.rb_node;
271 while (n) {
272 struct vmap_area *va;
274 va = rb_entry(n, struct vmap_area, rb_node);
275 if (addr < va->va_start)
276 n = n->rb_left;
277 else if (addr > va->va_start)
278 n = n->rb_right;
279 else
280 return va;
283 return NULL;
286 static void __insert_vmap_area(struct vmap_area *va)
288 struct rb_node **p = &vmap_area_root.rb_node;
289 struct rb_node *parent = NULL;
290 struct rb_node *tmp;
292 while (*p) {
293 struct vmap_area *tmp;
295 parent = *p;
296 tmp = rb_entry(parent, struct vmap_area, rb_node);
297 if (va->va_start < tmp->va_end)
298 p = &(*p)->rb_left;
299 else if (va->va_end > tmp->va_start)
300 p = &(*p)->rb_right;
301 else
302 BUG();
305 rb_link_node(&va->rb_node, parent, p);
306 rb_insert_color(&va->rb_node, &vmap_area_root);
308 /* address-sort this list so it is usable like the vmlist */
309 tmp = rb_prev(&va->rb_node);
310 if (tmp) {
311 struct vmap_area *prev;
312 prev = rb_entry(tmp, struct vmap_area, rb_node);
313 list_add_rcu(&va->list, &prev->list);
314 } else
315 list_add_rcu(&va->list, &vmap_area_list);
318 static void purge_vmap_area_lazy(void);
321 * Allocate a region of KVA of the specified size and alignment, within the
322 * vstart and vend.
324 static struct vmap_area *alloc_vmap_area(unsigned long size,
325 unsigned long align,
326 unsigned long vstart, unsigned long vend,
327 int node, gfp_t gfp_mask)
329 struct vmap_area *va;
330 struct rb_node *n;
331 unsigned long addr;
332 int purged = 0;
334 BUG_ON(!size);
335 BUG_ON(size & ~PAGE_MASK);
337 va = kmalloc_node(sizeof(struct vmap_area),
338 gfp_mask & GFP_RECLAIM_MASK, node);
339 if (unlikely(!va))
340 return ERR_PTR(-ENOMEM);
342 retry:
343 addr = ALIGN(vstart, align);
345 spin_lock(&vmap_area_lock);
346 if (addr + size - 1 < addr)
347 goto overflow;
349 /* XXX: could have a last_hole cache */
350 n = vmap_area_root.rb_node;
351 if (n) {
352 struct vmap_area *first = NULL;
354 do {
355 struct vmap_area *tmp;
356 tmp = rb_entry(n, struct vmap_area, rb_node);
357 if (tmp->va_end >= addr) {
358 if (!first && tmp->va_start < addr + size)
359 first = tmp;
360 n = n->rb_left;
361 } else {
362 first = tmp;
363 n = n->rb_right;
365 } while (n);
367 if (!first)
368 goto found;
370 if (first->va_end < addr) {
371 n = rb_next(&first->rb_node);
372 if (n)
373 first = rb_entry(n, struct vmap_area, rb_node);
374 else
375 goto found;
378 while (addr + size > first->va_start && addr + size <= vend) {
379 addr = ALIGN(first->va_end + PAGE_SIZE, align);
380 if (addr + size - 1 < addr)
381 goto overflow;
383 n = rb_next(&first->rb_node);
384 if (n)
385 first = rb_entry(n, struct vmap_area, rb_node);
386 else
387 goto found;
390 found:
391 if (addr + size > vend) {
392 overflow:
393 spin_unlock(&vmap_area_lock);
394 if (!purged) {
395 purge_vmap_area_lazy();
396 purged = 1;
397 goto retry;
399 if (printk_ratelimit())
400 printk(KERN_WARNING
401 "vmap allocation for size %lu failed: "
402 "use vmalloc=<size> to increase size.\n", size);
403 kfree(va);
404 return ERR_PTR(-EBUSY);
407 BUG_ON(addr & (align-1));
409 va->va_start = addr;
410 va->va_end = addr + size;
411 va->flags = 0;
412 __insert_vmap_area(va);
413 spin_unlock(&vmap_area_lock);
415 return va;
418 static void rcu_free_va(struct rcu_head *head)
420 struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
422 kfree(va);
425 static void __free_vmap_area(struct vmap_area *va)
427 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
428 rb_erase(&va->rb_node, &vmap_area_root);
429 RB_CLEAR_NODE(&va->rb_node);
430 list_del_rcu(&va->list);
433 * Track the highest possible candidate for pcpu area
434 * allocation. Areas outside of vmalloc area can be returned
435 * here too, consider only end addresses which fall inside
436 * vmalloc area proper.
438 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
439 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
441 call_rcu(&va->rcu_head, rcu_free_va);
445 * Free a region of KVA allocated by alloc_vmap_area
447 static void free_vmap_area(struct vmap_area *va)
449 spin_lock(&vmap_area_lock);
450 __free_vmap_area(va);
451 spin_unlock(&vmap_area_lock);
455 * Clear the pagetable entries of a given vmap_area
457 static void unmap_vmap_area(struct vmap_area *va)
459 vunmap_page_range(va->va_start, va->va_end);
462 static void vmap_debug_free_range(unsigned long start, unsigned long end)
465 * Unmap page tables and force a TLB flush immediately if
466 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
467 * bugs similarly to those in linear kernel virtual address
468 * space after a page has been freed.
470 * All the lazy freeing logic is still retained, in order to
471 * minimise intrusiveness of this debugging feature.
473 * This is going to be *slow* (linear kernel virtual address
474 * debugging doesn't do a broadcast TLB flush so it is a lot
475 * faster).
477 #ifdef CONFIG_DEBUG_PAGEALLOC
478 vunmap_page_range(start, end);
479 flush_tlb_kernel_range(start, end);
480 #endif
484 * lazy_max_pages is the maximum amount of virtual address space we gather up
485 * before attempting to purge with a TLB flush.
487 * There is a tradeoff here: a larger number will cover more kernel page tables
488 * and take slightly longer to purge, but it will linearly reduce the number of
489 * global TLB flushes that must be performed. It would seem natural to scale
490 * this number up linearly with the number of CPUs (because vmapping activity
491 * could also scale linearly with the number of CPUs), however it is likely
492 * that in practice, workloads might be constrained in other ways that mean
493 * vmap activity will not scale linearly with CPUs. Also, I want to be
494 * conservative and not introduce a big latency on huge systems, so go with
495 * a less aggressive log scale. It will still be an improvement over the old
496 * code, and it will be simple to change the scale factor if we find that it
497 * becomes a problem on bigger systems.
499 static unsigned long lazy_max_pages(void)
501 unsigned int log;
503 log = fls(num_online_cpus());
505 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
508 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
511 * Purges all lazily-freed vmap areas.
513 * If sync is 0 then don't purge if there is already a purge in progress.
514 * If force_flush is 1, then flush kernel TLBs between *start and *end even
515 * if we found no lazy vmap areas to unmap (callers can use this to optimise
516 * their own TLB flushing).
517 * Returns with *start = min(*start, lowest purged address)
518 * *end = max(*end, highest purged address)
520 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
521 int sync, int force_flush)
523 static DEFINE_SPINLOCK(purge_lock);
524 LIST_HEAD(valist);
525 struct vmap_area *va;
526 struct vmap_area *n_va;
527 int nr = 0;
530 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
531 * should not expect such behaviour. This just simplifies locking for
532 * the case that isn't actually used at the moment anyway.
534 if (!sync && !force_flush) {
535 if (!spin_trylock(&purge_lock))
536 return;
537 } else
538 spin_lock(&purge_lock);
540 rcu_read_lock();
541 list_for_each_entry_rcu(va, &vmap_area_list, list) {
542 if (va->flags & VM_LAZY_FREE) {
543 if (va->va_start < *start)
544 *start = va->va_start;
545 if (va->va_end > *end)
546 *end = va->va_end;
547 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
548 unmap_vmap_area(va);
549 list_add_tail(&va->purge_list, &valist);
550 va->flags |= VM_LAZY_FREEING;
551 va->flags &= ~VM_LAZY_FREE;
554 rcu_read_unlock();
556 if (nr) {
557 BUG_ON(nr > atomic_read(&vmap_lazy_nr));
558 atomic_sub(nr, &vmap_lazy_nr);
561 if (nr || force_flush)
562 flush_tlb_kernel_range(*start, *end);
564 if (nr) {
565 spin_lock(&vmap_area_lock);
566 list_for_each_entry_safe(va, n_va, &valist, purge_list)
567 __free_vmap_area(va);
568 spin_unlock(&vmap_area_lock);
570 spin_unlock(&purge_lock);
574 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
575 * is already purging.
577 static void try_purge_vmap_area_lazy(void)
579 unsigned long start = ULONG_MAX, end = 0;
581 __purge_vmap_area_lazy(&start, &end, 0, 0);
585 * Kick off a purge of the outstanding lazy areas.
587 static void purge_vmap_area_lazy(void)
589 unsigned long start = ULONG_MAX, end = 0;
591 __purge_vmap_area_lazy(&start, &end, 1, 0);
595 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
596 * called for the correct range previously.
598 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
600 va->flags |= VM_LAZY_FREE;
601 atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
602 if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
603 try_purge_vmap_area_lazy();
607 * Free and unmap a vmap area
609 static void free_unmap_vmap_area(struct vmap_area *va)
611 flush_cache_vunmap(va->va_start, va->va_end);
612 free_unmap_vmap_area_noflush(va);
615 static struct vmap_area *find_vmap_area(unsigned long addr)
617 struct vmap_area *va;
619 spin_lock(&vmap_area_lock);
620 va = __find_vmap_area(addr);
621 spin_unlock(&vmap_area_lock);
623 return va;
626 static void free_unmap_vmap_area_addr(unsigned long addr)
628 struct vmap_area *va;
630 va = find_vmap_area(addr);
631 BUG_ON(!va);
632 free_unmap_vmap_area(va);
636 /*** Per cpu kva allocator ***/
639 * vmap space is limited especially on 32 bit architectures. Ensure there is
640 * room for at least 16 percpu vmap blocks per CPU.
643 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
644 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
645 * instead (we just need a rough idea)
647 #if BITS_PER_LONG == 32
648 #define VMALLOC_SPACE (128UL*1024*1024)
649 #else
650 #define VMALLOC_SPACE (128UL*1024*1024*1024)
651 #endif
653 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
654 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
655 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
656 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
657 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
658 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
659 #define VMAP_BBMAP_BITS VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
660 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
661 VMALLOC_PAGES / NR_CPUS / 16))
663 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
665 static bool vmap_initialized __read_mostly = false;
667 struct vmap_block_queue {
668 spinlock_t lock;
669 struct list_head free;
670 struct list_head dirty;
671 unsigned int nr_dirty;
674 struct vmap_block {
675 spinlock_t lock;
676 struct vmap_area *va;
677 struct vmap_block_queue *vbq;
678 unsigned long free, dirty;
679 DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
680 DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
681 union {
682 struct list_head free_list;
683 struct rcu_head rcu_head;
687 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
688 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
691 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
692 * in the free path. Could get rid of this if we change the API to return a
693 * "cookie" from alloc, to be passed to free. But no big deal yet.
695 static DEFINE_SPINLOCK(vmap_block_tree_lock);
696 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
699 * We should probably have a fallback mechanism to allocate virtual memory
700 * out of partially filled vmap blocks. However vmap block sizing should be
701 * fairly reasonable according to the vmalloc size, so it shouldn't be a
702 * big problem.
705 static unsigned long addr_to_vb_idx(unsigned long addr)
707 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
708 addr /= VMAP_BLOCK_SIZE;
709 return addr;
712 static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
714 struct vmap_block_queue *vbq;
715 struct vmap_block *vb;
716 struct vmap_area *va;
717 unsigned long vb_idx;
718 int node, err;
720 node = numa_node_id();
722 vb = kmalloc_node(sizeof(struct vmap_block),
723 gfp_mask & GFP_RECLAIM_MASK, node);
724 if (unlikely(!vb))
725 return ERR_PTR(-ENOMEM);
727 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
728 VMALLOC_START, VMALLOC_END,
729 node, gfp_mask);
730 if (unlikely(IS_ERR(va))) {
731 kfree(vb);
732 return ERR_PTR(PTR_ERR(va));
735 err = radix_tree_preload(gfp_mask);
736 if (unlikely(err)) {
737 kfree(vb);
738 free_vmap_area(va);
739 return ERR_PTR(err);
742 spin_lock_init(&vb->lock);
743 vb->va = va;
744 vb->free = VMAP_BBMAP_BITS;
745 vb->dirty = 0;
746 bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
747 bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
748 INIT_LIST_HEAD(&vb->free_list);
750 vb_idx = addr_to_vb_idx(va->va_start);
751 spin_lock(&vmap_block_tree_lock);
752 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
753 spin_unlock(&vmap_block_tree_lock);
754 BUG_ON(err);
755 radix_tree_preload_end();
757 vbq = &get_cpu_var(vmap_block_queue);
758 vb->vbq = vbq;
759 spin_lock(&vbq->lock);
760 list_add(&vb->free_list, &vbq->free);
761 spin_unlock(&vbq->lock);
762 put_cpu_var(vmap_cpu_blocks);
764 return vb;
767 static void rcu_free_vb(struct rcu_head *head)
769 struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
771 kfree(vb);
774 static void free_vmap_block(struct vmap_block *vb)
776 struct vmap_block *tmp;
777 unsigned long vb_idx;
779 BUG_ON(!list_empty(&vb->free_list));
781 vb_idx = addr_to_vb_idx(vb->va->va_start);
782 spin_lock(&vmap_block_tree_lock);
783 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
784 spin_unlock(&vmap_block_tree_lock);
785 BUG_ON(tmp != vb);
787 free_unmap_vmap_area_noflush(vb->va);
788 call_rcu(&vb->rcu_head, rcu_free_vb);
791 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
793 struct vmap_block_queue *vbq;
794 struct vmap_block *vb;
795 unsigned long addr = 0;
796 unsigned int order;
798 BUG_ON(size & ~PAGE_MASK);
799 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
800 order = get_order(size);
802 again:
803 rcu_read_lock();
804 vbq = &get_cpu_var(vmap_block_queue);
805 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
806 int i;
808 spin_lock(&vb->lock);
809 i = bitmap_find_free_region(vb->alloc_map,
810 VMAP_BBMAP_BITS, order);
812 if (i >= 0) {
813 addr = vb->va->va_start + (i << PAGE_SHIFT);
814 BUG_ON(addr_to_vb_idx(addr) !=
815 addr_to_vb_idx(vb->va->va_start));
816 vb->free -= 1UL << order;
817 if (vb->free == 0) {
818 spin_lock(&vbq->lock);
819 list_del_init(&vb->free_list);
820 spin_unlock(&vbq->lock);
822 spin_unlock(&vb->lock);
823 break;
825 spin_unlock(&vb->lock);
827 put_cpu_var(vmap_cpu_blocks);
828 rcu_read_unlock();
830 if (!addr) {
831 vb = new_vmap_block(gfp_mask);
832 if (IS_ERR(vb))
833 return vb;
834 goto again;
837 return (void *)addr;
840 static void vb_free(const void *addr, unsigned long size)
842 unsigned long offset;
843 unsigned long vb_idx;
844 unsigned int order;
845 struct vmap_block *vb;
847 BUG_ON(size & ~PAGE_MASK);
848 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
850 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
852 order = get_order(size);
854 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
856 vb_idx = addr_to_vb_idx((unsigned long)addr);
857 rcu_read_lock();
858 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
859 rcu_read_unlock();
860 BUG_ON(!vb);
862 spin_lock(&vb->lock);
863 bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
865 vb->dirty += 1UL << order;
866 if (vb->dirty == VMAP_BBMAP_BITS) {
867 BUG_ON(vb->free || !list_empty(&vb->free_list));
868 spin_unlock(&vb->lock);
869 free_vmap_block(vb);
870 } else
871 spin_unlock(&vb->lock);
875 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
877 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
878 * to amortize TLB flushing overheads. What this means is that any page you
879 * have now, may, in a former life, have been mapped into kernel virtual
880 * address by the vmap layer and so there might be some CPUs with TLB entries
881 * still referencing that page (additional to the regular 1:1 kernel mapping).
883 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
884 * be sure that none of the pages we have control over will have any aliases
885 * from the vmap layer.
887 void vm_unmap_aliases(void)
889 unsigned long start = ULONG_MAX, end = 0;
890 int cpu;
891 int flush = 0;
893 if (unlikely(!vmap_initialized))
894 return;
896 for_each_possible_cpu(cpu) {
897 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
898 struct vmap_block *vb;
900 rcu_read_lock();
901 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
902 int i;
904 spin_lock(&vb->lock);
905 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
906 while (i < VMAP_BBMAP_BITS) {
907 unsigned long s, e;
908 int j;
909 j = find_next_zero_bit(vb->dirty_map,
910 VMAP_BBMAP_BITS, i);
912 s = vb->va->va_start + (i << PAGE_SHIFT);
913 e = vb->va->va_start + (j << PAGE_SHIFT);
914 vunmap_page_range(s, e);
915 flush = 1;
917 if (s < start)
918 start = s;
919 if (e > end)
920 end = e;
922 i = j;
923 i = find_next_bit(vb->dirty_map,
924 VMAP_BBMAP_BITS, i);
926 spin_unlock(&vb->lock);
928 rcu_read_unlock();
931 __purge_vmap_area_lazy(&start, &end, 1, flush);
933 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
936 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
937 * @mem: the pointer returned by vm_map_ram
938 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
940 void vm_unmap_ram(const void *mem, unsigned int count)
942 unsigned long size = count << PAGE_SHIFT;
943 unsigned long addr = (unsigned long)mem;
945 BUG_ON(!addr);
946 BUG_ON(addr < VMALLOC_START);
947 BUG_ON(addr > VMALLOC_END);
948 BUG_ON(addr & (PAGE_SIZE-1));
950 debug_check_no_locks_freed(mem, size);
951 vmap_debug_free_range(addr, addr+size);
953 if (likely(count <= VMAP_MAX_ALLOC))
954 vb_free(mem, size);
955 else
956 free_unmap_vmap_area_addr(addr);
958 EXPORT_SYMBOL(vm_unmap_ram);
961 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
962 * @pages: an array of pointers to the pages to be mapped
963 * @count: number of pages
964 * @node: prefer to allocate data structures on this node
965 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
967 * Returns: a pointer to the address that has been mapped, or %NULL on failure
969 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
971 unsigned long size = count << PAGE_SHIFT;
972 unsigned long addr;
973 void *mem;
975 if (likely(count <= VMAP_MAX_ALLOC)) {
976 mem = vb_alloc(size, GFP_KERNEL);
977 if (IS_ERR(mem))
978 return NULL;
979 addr = (unsigned long)mem;
980 } else {
981 struct vmap_area *va;
982 va = alloc_vmap_area(size, PAGE_SIZE,
983 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
984 if (IS_ERR(va))
985 return NULL;
987 addr = va->va_start;
988 mem = (void *)addr;
990 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
991 vm_unmap_ram(mem, count);
992 return NULL;
994 return mem;
996 EXPORT_SYMBOL(vm_map_ram);
999 * vm_area_register_early - register vmap area early during boot
1000 * @vm: vm_struct to register
1001 * @align: requested alignment
1003 * This function is used to register kernel vm area before
1004 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1005 * proper values on entry and other fields should be zero. On return,
1006 * vm->addr contains the allocated address.
1008 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1010 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1012 static size_t vm_init_off __initdata;
1013 unsigned long addr;
1015 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1016 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1018 vm->addr = (void *)addr;
1020 vm->next = vmlist;
1021 vmlist = vm;
1024 void __init vmalloc_init(void)
1026 struct vmap_area *va;
1027 struct vm_struct *tmp;
1028 int i;
1030 for_each_possible_cpu(i) {
1031 struct vmap_block_queue *vbq;
1033 vbq = &per_cpu(vmap_block_queue, i);
1034 spin_lock_init(&vbq->lock);
1035 INIT_LIST_HEAD(&vbq->free);
1036 INIT_LIST_HEAD(&vbq->dirty);
1037 vbq->nr_dirty = 0;
1040 /* Import existing vmlist entries. */
1041 for (tmp = vmlist; tmp; tmp = tmp->next) {
1042 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1043 va->flags = tmp->flags | VM_VM_AREA;
1044 va->va_start = (unsigned long)tmp->addr;
1045 va->va_end = va->va_start + tmp->size;
1046 __insert_vmap_area(va);
1049 vmap_area_pcpu_hole = VMALLOC_END;
1051 vmap_initialized = true;
1055 * map_kernel_range_noflush - map kernel VM area with the specified pages
1056 * @addr: start of the VM area to map
1057 * @size: size of the VM area to map
1058 * @prot: page protection flags to use
1059 * @pages: pages to map
1061 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1062 * specify should have been allocated using get_vm_area() and its
1063 * friends.
1065 * NOTE:
1066 * This function does NOT do any cache flushing. The caller is
1067 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1068 * before calling this function.
1070 * RETURNS:
1071 * The number of pages mapped on success, -errno on failure.
1073 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1074 pgprot_t prot, struct page **pages)
1076 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1080 * unmap_kernel_range_noflush - unmap kernel VM area
1081 * @addr: start of the VM area to unmap
1082 * @size: size of the VM area to unmap
1084 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1085 * specify should have been allocated using get_vm_area() and its
1086 * friends.
1088 * NOTE:
1089 * This function does NOT do any cache flushing. The caller is
1090 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1091 * before calling this function and flush_tlb_kernel_range() after.
1093 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1095 vunmap_page_range(addr, addr + size);
1099 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1100 * @addr: start of the VM area to unmap
1101 * @size: size of the VM area to unmap
1103 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1104 * the unmapping and tlb after.
1106 void unmap_kernel_range(unsigned long addr, unsigned long size)
1108 unsigned long end = addr + size;
1110 flush_cache_vunmap(addr, end);
1111 vunmap_page_range(addr, end);
1112 flush_tlb_kernel_range(addr, end);
1115 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1117 unsigned long addr = (unsigned long)area->addr;
1118 unsigned long end = addr + area->size - PAGE_SIZE;
1119 int err;
1121 err = vmap_page_range(addr, end, prot, *pages);
1122 if (err > 0) {
1123 *pages += err;
1124 err = 0;
1127 return err;
1129 EXPORT_SYMBOL_GPL(map_vm_area);
1131 /*** Old vmalloc interfaces ***/
1132 DEFINE_RWLOCK(vmlist_lock);
1133 struct vm_struct *vmlist;
1135 static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1136 unsigned long flags, void *caller)
1138 struct vm_struct *tmp, **p;
1140 vm->flags = flags;
1141 vm->addr = (void *)va->va_start;
1142 vm->size = va->va_end - va->va_start;
1143 vm->caller = caller;
1144 va->private = vm;
1145 va->flags |= VM_VM_AREA;
1147 write_lock(&vmlist_lock);
1148 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1149 if (tmp->addr >= vm->addr)
1150 break;
1152 vm->next = *p;
1153 *p = vm;
1154 write_unlock(&vmlist_lock);
1157 static struct vm_struct *__get_vm_area_node(unsigned long size,
1158 unsigned long flags, unsigned long start, unsigned long end,
1159 int node, gfp_t gfp_mask, void *caller)
1161 static struct vmap_area *va;
1162 struct vm_struct *area;
1163 unsigned long align = 1;
1165 BUG_ON(in_interrupt());
1166 if (flags & VM_IOREMAP) {
1167 int bit = fls(size);
1169 if (bit > IOREMAP_MAX_ORDER)
1170 bit = IOREMAP_MAX_ORDER;
1171 else if (bit < PAGE_SHIFT)
1172 bit = PAGE_SHIFT;
1174 align = 1ul << bit;
1177 size = PAGE_ALIGN(size);
1178 if (unlikely(!size))
1179 return NULL;
1181 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1182 if (unlikely(!area))
1183 return NULL;
1186 * We always allocate a guard page.
1188 size += PAGE_SIZE;
1190 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1191 if (IS_ERR(va)) {
1192 kfree(area);
1193 return NULL;
1196 insert_vmalloc_vm(area, va, flags, caller);
1197 return area;
1200 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1201 unsigned long start, unsigned long end)
1203 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1204 __builtin_return_address(0));
1206 EXPORT_SYMBOL_GPL(__get_vm_area);
1208 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1209 unsigned long start, unsigned long end,
1210 void *caller)
1212 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1213 caller);
1217 * get_vm_area - reserve a contiguous kernel virtual area
1218 * @size: size of the area
1219 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1221 * Search an area of @size in the kernel virtual mapping area,
1222 * and reserved it for out purposes. Returns the area descriptor
1223 * on success or %NULL on failure.
1225 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1227 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1228 -1, GFP_KERNEL, __builtin_return_address(0));
1231 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1232 void *caller)
1234 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1235 -1, GFP_KERNEL, caller);
1238 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1239 int node, gfp_t gfp_mask)
1241 return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
1242 gfp_mask, __builtin_return_address(0));
1245 static struct vm_struct *find_vm_area(const void *addr)
1247 struct vmap_area *va;
1249 va = find_vmap_area((unsigned long)addr);
1250 if (va && va->flags & VM_VM_AREA)
1251 return va->private;
1253 return NULL;
1257 * remove_vm_area - find and remove a continuous kernel virtual area
1258 * @addr: base address
1260 * Search for the kernel VM area starting at @addr, and remove it.
1261 * This function returns the found VM area, but using it is NOT safe
1262 * on SMP machines, except for its size or flags.
1264 struct vm_struct *remove_vm_area(const void *addr)
1266 struct vmap_area *va;
1268 va = find_vmap_area((unsigned long)addr);
1269 if (va && va->flags & VM_VM_AREA) {
1270 struct vm_struct *vm = va->private;
1271 struct vm_struct *tmp, **p;
1273 * remove from list and disallow access to this vm_struct
1274 * before unmap. (address range confliction is maintained by
1275 * vmap.)
1277 write_lock(&vmlist_lock);
1278 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1280 *p = tmp->next;
1281 write_unlock(&vmlist_lock);
1283 vmap_debug_free_range(va->va_start, va->va_end);
1284 free_unmap_vmap_area(va);
1285 vm->size -= PAGE_SIZE;
1287 return vm;
1289 return NULL;
1292 static void __vunmap(const void *addr, int deallocate_pages)
1294 struct vm_struct *area;
1296 if (!addr)
1297 return;
1299 if ((PAGE_SIZE-1) & (unsigned long)addr) {
1300 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1301 return;
1304 area = remove_vm_area(addr);
1305 if (unlikely(!area)) {
1306 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1307 addr);
1308 return;
1311 debug_check_no_locks_freed(addr, area->size);
1312 debug_check_no_obj_freed(addr, area->size);
1314 if (deallocate_pages) {
1315 int i;
1317 for (i = 0; i < area->nr_pages; i++) {
1318 struct page *page = area->pages[i];
1320 BUG_ON(!page);
1321 __free_page(page);
1324 if (area->flags & VM_VPAGES)
1325 vfree(area->pages);
1326 else
1327 kfree(area->pages);
1330 kfree(area);
1331 return;
1335 * vfree - release memory allocated by vmalloc()
1336 * @addr: memory base address
1338 * Free the virtually continuous memory area starting at @addr, as
1339 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1340 * NULL, no operation is performed.
1342 * Must not be called in interrupt context.
1344 void vfree(const void *addr)
1346 BUG_ON(in_interrupt());
1348 kmemleak_free(addr);
1350 __vunmap(addr, 1);
1352 EXPORT_SYMBOL(vfree);
1355 * vunmap - release virtual mapping obtained by vmap()
1356 * @addr: memory base address
1358 * Free the virtually contiguous memory area starting at @addr,
1359 * which was created from the page array passed to vmap().
1361 * Must not be called in interrupt context.
1363 void vunmap(const void *addr)
1365 BUG_ON(in_interrupt());
1366 might_sleep();
1367 __vunmap(addr, 0);
1369 EXPORT_SYMBOL(vunmap);
1372 * vmap - map an array of pages into virtually contiguous space
1373 * @pages: array of page pointers
1374 * @count: number of pages to map
1375 * @flags: vm_area->flags
1376 * @prot: page protection for the mapping
1378 * Maps @count pages from @pages into contiguous kernel virtual
1379 * space.
1381 void *vmap(struct page **pages, unsigned int count,
1382 unsigned long flags, pgprot_t prot)
1384 struct vm_struct *area;
1386 might_sleep();
1388 if (count > totalram_pages)
1389 return NULL;
1391 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1392 __builtin_return_address(0));
1393 if (!area)
1394 return NULL;
1396 if (map_vm_area(area, prot, &pages)) {
1397 vunmap(area->addr);
1398 return NULL;
1401 return area->addr;
1403 EXPORT_SYMBOL(vmap);
1405 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1406 int node, void *caller);
1407 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1408 pgprot_t prot, int node, void *caller)
1410 struct page **pages;
1411 unsigned int nr_pages, array_size, i;
1413 nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1414 array_size = (nr_pages * sizeof(struct page *));
1416 area->nr_pages = nr_pages;
1417 /* Please note that the recursion is strictly bounded. */
1418 if (array_size > PAGE_SIZE) {
1419 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
1420 PAGE_KERNEL, node, caller);
1421 area->flags |= VM_VPAGES;
1422 } else {
1423 pages = kmalloc_node(array_size,
1424 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1425 node);
1427 area->pages = pages;
1428 area->caller = caller;
1429 if (!area->pages) {
1430 remove_vm_area(area->addr);
1431 kfree(area);
1432 return NULL;
1435 for (i = 0; i < area->nr_pages; i++) {
1436 struct page *page;
1438 if (node < 0)
1439 page = alloc_page(gfp_mask);
1440 else
1441 page = alloc_pages_node(node, gfp_mask, 0);
1443 if (unlikely(!page)) {
1444 /* Successfully allocated i pages, free them in __vunmap() */
1445 area->nr_pages = i;
1446 goto fail;
1448 area->pages[i] = page;
1451 if (map_vm_area(area, prot, &pages))
1452 goto fail;
1453 return area->addr;
1455 fail:
1456 vfree(area->addr);
1457 return NULL;
1460 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1462 void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1463 __builtin_return_address(0));
1466 * A ref_count = 3 is needed because the vm_struct and vmap_area
1467 * structures allocated in the __get_vm_area_node() function contain
1468 * references to the virtual address of the vmalloc'ed block.
1470 kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1472 return addr;
1476 * __vmalloc_node - allocate virtually contiguous memory
1477 * @size: allocation size
1478 * @gfp_mask: flags for the page level allocator
1479 * @prot: protection mask for the allocated pages
1480 * @node: node to use for allocation or -1
1481 * @caller: caller's return address
1483 * Allocate enough pages to cover @size from the page level
1484 * allocator with @gfp_mask flags. Map them into contiguous
1485 * kernel virtual space, using a pagetable protection of @prot.
1487 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1488 int node, void *caller)
1490 struct vm_struct *area;
1491 void *addr;
1492 unsigned long real_size = size;
1494 size = PAGE_ALIGN(size);
1495 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1496 return NULL;
1498 area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1499 node, gfp_mask, caller);
1501 if (!area)
1502 return NULL;
1504 addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1507 * A ref_count = 3 is needed because the vm_struct and vmap_area
1508 * structures allocated in the __get_vm_area_node() function contain
1509 * references to the virtual address of the vmalloc'ed block.
1511 kmemleak_alloc(addr, real_size, 3, gfp_mask);
1513 return addr;
1516 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1518 return __vmalloc_node(size, gfp_mask, prot, -1,
1519 __builtin_return_address(0));
1521 EXPORT_SYMBOL(__vmalloc);
1524 * vmalloc - allocate virtually contiguous memory
1525 * @size: allocation size
1526 * Allocate enough pages to cover @size from the page level
1527 * allocator and map them into contiguous kernel virtual space.
1529 * For tight control over page level allocator and protection flags
1530 * use __vmalloc() instead.
1532 void *vmalloc(unsigned long size)
1534 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1535 -1, __builtin_return_address(0));
1537 EXPORT_SYMBOL(vmalloc);
1540 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1541 * @size: allocation size
1543 * The resulting memory area is zeroed so it can be mapped to userspace
1544 * without leaking data.
1546 void *vmalloc_user(unsigned long size)
1548 struct vm_struct *area;
1549 void *ret;
1551 ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1552 PAGE_KERNEL, -1, __builtin_return_address(0));
1553 if (ret) {
1554 area = find_vm_area(ret);
1555 area->flags |= VM_USERMAP;
1557 return ret;
1559 EXPORT_SYMBOL(vmalloc_user);
1562 * vmalloc_node - allocate memory on a specific node
1563 * @size: allocation size
1564 * @node: numa node
1566 * Allocate enough pages to cover @size from the page level
1567 * allocator and map them into contiguous kernel virtual space.
1569 * For tight control over page level allocator and protection flags
1570 * use __vmalloc() instead.
1572 void *vmalloc_node(unsigned long size, int node)
1574 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1575 node, __builtin_return_address(0));
1577 EXPORT_SYMBOL(vmalloc_node);
1579 #ifndef PAGE_KERNEL_EXEC
1580 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1581 #endif
1584 * vmalloc_exec - allocate virtually contiguous, executable memory
1585 * @size: allocation size
1587 * Kernel-internal function to allocate enough pages to cover @size
1588 * the page level allocator and map them into contiguous and
1589 * executable kernel virtual space.
1591 * For tight control over page level allocator and protection flags
1592 * use __vmalloc() instead.
1595 void *vmalloc_exec(unsigned long size)
1597 return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1598 -1, __builtin_return_address(0));
1601 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1602 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1603 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1604 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1605 #else
1606 #define GFP_VMALLOC32 GFP_KERNEL
1607 #endif
1610 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1611 * @size: allocation size
1613 * Allocate enough 32bit PA addressable pages to cover @size from the
1614 * page level allocator and map them into contiguous kernel virtual space.
1616 void *vmalloc_32(unsigned long size)
1618 return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1619 -1, __builtin_return_address(0));
1621 EXPORT_SYMBOL(vmalloc_32);
1624 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1625 * @size: allocation size
1627 * The resulting memory area is 32bit addressable and zeroed so it can be
1628 * mapped to userspace without leaking data.
1630 void *vmalloc_32_user(unsigned long size)
1632 struct vm_struct *area;
1633 void *ret;
1635 ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1636 -1, __builtin_return_address(0));
1637 if (ret) {
1638 area = find_vm_area(ret);
1639 area->flags |= VM_USERMAP;
1641 return ret;
1643 EXPORT_SYMBOL(vmalloc_32_user);
1646 * small helper routine , copy contents to buf from addr.
1647 * If the page is not present, fill zero.
1650 static int aligned_vread(char *buf, char *addr, unsigned long count)
1652 struct page *p;
1653 int copied = 0;
1655 while (count) {
1656 unsigned long offset, length;
1658 offset = (unsigned long)addr & ~PAGE_MASK;
1659 length = PAGE_SIZE - offset;
1660 if (length > count)
1661 length = count;
1662 p = vmalloc_to_page(addr);
1664 * To do safe access to this _mapped_ area, we need
1665 * lock. But adding lock here means that we need to add
1666 * overhead of vmalloc()/vfree() calles for this _debug_
1667 * interface, rarely used. Instead of that, we'll use
1668 * kmap() and get small overhead in this access function.
1670 if (p) {
1672 * we can expect USER0 is not used (see vread/vwrite's
1673 * function description)
1675 void *map = kmap_atomic(p, KM_USER0);
1676 memcpy(buf, map + offset, length);
1677 kunmap_atomic(map, KM_USER0);
1678 } else
1679 memset(buf, 0, length);
1681 addr += length;
1682 buf += length;
1683 copied += length;
1684 count -= length;
1686 return copied;
1689 static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1691 struct page *p;
1692 int copied = 0;
1694 while (count) {
1695 unsigned long offset, length;
1697 offset = (unsigned long)addr & ~PAGE_MASK;
1698 length = PAGE_SIZE - offset;
1699 if (length > count)
1700 length = count;
1701 p = vmalloc_to_page(addr);
1703 * To do safe access to this _mapped_ area, we need
1704 * lock. But adding lock here means that we need to add
1705 * overhead of vmalloc()/vfree() calles for this _debug_
1706 * interface, rarely used. Instead of that, we'll use
1707 * kmap() and get small overhead in this access function.
1709 if (p) {
1711 * we can expect USER0 is not used (see vread/vwrite's
1712 * function description)
1714 void *map = kmap_atomic(p, KM_USER0);
1715 memcpy(map + offset, buf, length);
1716 kunmap_atomic(map, KM_USER0);
1718 addr += length;
1719 buf += length;
1720 copied += length;
1721 count -= length;
1723 return copied;
1727 * vread() - read vmalloc area in a safe way.
1728 * @buf: buffer for reading data
1729 * @addr: vm address.
1730 * @count: number of bytes to be read.
1732 * Returns # of bytes which addr and buf should be increased.
1733 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
1734 * includes any intersect with alive vmalloc area.
1736 * This function checks that addr is a valid vmalloc'ed area, and
1737 * copy data from that area to a given buffer. If the given memory range
1738 * of [addr...addr+count) includes some valid address, data is copied to
1739 * proper area of @buf. If there are memory holes, they'll be zero-filled.
1740 * IOREMAP area is treated as memory hole and no copy is done.
1742 * If [addr...addr+count) doesn't includes any intersects with alive
1743 * vm_struct area, returns 0.
1744 * @buf should be kernel's buffer. Because this function uses KM_USER0,
1745 * the caller should guarantee KM_USER0 is not used.
1747 * Note: In usual ops, vread() is never necessary because the caller
1748 * should know vmalloc() area is valid and can use memcpy().
1749 * This is for routines which have to access vmalloc area without
1750 * any informaion, as /dev/kmem.
1754 long vread(char *buf, char *addr, unsigned long count)
1756 struct vm_struct *tmp;
1757 char *vaddr, *buf_start = buf;
1758 unsigned long buflen = count;
1759 unsigned long n;
1761 /* Don't allow overflow */
1762 if ((unsigned long) addr + count < count)
1763 count = -(unsigned long) addr;
1765 read_lock(&vmlist_lock);
1766 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
1767 vaddr = (char *) tmp->addr;
1768 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1769 continue;
1770 while (addr < vaddr) {
1771 if (count == 0)
1772 goto finished;
1773 *buf = '\0';
1774 buf++;
1775 addr++;
1776 count--;
1778 n = vaddr + tmp->size - PAGE_SIZE - addr;
1779 if (n > count)
1780 n = count;
1781 if (!(tmp->flags & VM_IOREMAP))
1782 aligned_vread(buf, addr, n);
1783 else /* IOREMAP area is treated as memory hole */
1784 memset(buf, 0, n);
1785 buf += n;
1786 addr += n;
1787 count -= n;
1789 finished:
1790 read_unlock(&vmlist_lock);
1792 if (buf == buf_start)
1793 return 0;
1794 /* zero-fill memory holes */
1795 if (buf != buf_start + buflen)
1796 memset(buf, 0, buflen - (buf - buf_start));
1798 return buflen;
1802 * vwrite() - write vmalloc area in a safe way.
1803 * @buf: buffer for source data
1804 * @addr: vm address.
1805 * @count: number of bytes to be read.
1807 * Returns # of bytes which addr and buf should be incresed.
1808 * (same number to @count).
1809 * If [addr...addr+count) doesn't includes any intersect with valid
1810 * vmalloc area, returns 0.
1812 * This function checks that addr is a valid vmalloc'ed area, and
1813 * copy data from a buffer to the given addr. If specified range of
1814 * [addr...addr+count) includes some valid address, data is copied from
1815 * proper area of @buf. If there are memory holes, no copy to hole.
1816 * IOREMAP area is treated as memory hole and no copy is done.
1818 * If [addr...addr+count) doesn't includes any intersects with alive
1819 * vm_struct area, returns 0.
1820 * @buf should be kernel's buffer. Because this function uses KM_USER0,
1821 * the caller should guarantee KM_USER0 is not used.
1823 * Note: In usual ops, vwrite() is never necessary because the caller
1824 * should know vmalloc() area is valid and can use memcpy().
1825 * This is for routines which have to access vmalloc area without
1826 * any informaion, as /dev/kmem.
1828 * The caller should guarantee KM_USER1 is not used.
1831 long vwrite(char *buf, char *addr, unsigned long count)
1833 struct vm_struct *tmp;
1834 char *vaddr;
1835 unsigned long n, buflen;
1836 int copied = 0;
1838 /* Don't allow overflow */
1839 if ((unsigned long) addr + count < count)
1840 count = -(unsigned long) addr;
1841 buflen = count;
1843 read_lock(&vmlist_lock);
1844 for (tmp = vmlist; count && tmp; tmp = tmp->next) {
1845 vaddr = (char *) tmp->addr;
1846 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1847 continue;
1848 while (addr < vaddr) {
1849 if (count == 0)
1850 goto finished;
1851 buf++;
1852 addr++;
1853 count--;
1855 n = vaddr + tmp->size - PAGE_SIZE - addr;
1856 if (n > count)
1857 n = count;
1858 if (!(tmp->flags & VM_IOREMAP)) {
1859 aligned_vwrite(buf, addr, n);
1860 copied++;
1862 buf += n;
1863 addr += n;
1864 count -= n;
1866 finished:
1867 read_unlock(&vmlist_lock);
1868 if (!copied)
1869 return 0;
1870 return buflen;
1874 * remap_vmalloc_range - map vmalloc pages to userspace
1875 * @vma: vma to cover (map full range of vma)
1876 * @addr: vmalloc memory
1877 * @pgoff: number of pages into addr before first page to map
1879 * Returns: 0 for success, -Exxx on failure
1881 * This function checks that addr is a valid vmalloc'ed area, and
1882 * that it is big enough to cover the vma. Will return failure if
1883 * that criteria isn't met.
1885 * Similar to remap_pfn_range() (see mm/memory.c)
1887 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1888 unsigned long pgoff)
1890 struct vm_struct *area;
1891 unsigned long uaddr = vma->vm_start;
1892 unsigned long usize = vma->vm_end - vma->vm_start;
1894 if ((PAGE_SIZE-1) & (unsigned long)addr)
1895 return -EINVAL;
1897 area = find_vm_area(addr);
1898 if (!area)
1899 return -EINVAL;
1901 if (!(area->flags & VM_USERMAP))
1902 return -EINVAL;
1904 if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1905 return -EINVAL;
1907 addr += pgoff << PAGE_SHIFT;
1908 do {
1909 struct page *page = vmalloc_to_page(addr);
1910 int ret;
1912 ret = vm_insert_page(vma, uaddr, page);
1913 if (ret)
1914 return ret;
1916 uaddr += PAGE_SIZE;
1917 addr += PAGE_SIZE;
1918 usize -= PAGE_SIZE;
1919 } while (usize > 0);
1921 /* Prevent "things" like memory migration? VM_flags need a cleanup... */
1922 vma->vm_flags |= VM_RESERVED;
1924 return 0;
1926 EXPORT_SYMBOL(remap_vmalloc_range);
1929 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1930 * have one.
1932 void __attribute__((weak)) vmalloc_sync_all(void)
1937 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
1939 /* apply_to_page_range() does all the hard work. */
1940 return 0;
1944 * alloc_vm_area - allocate a range of kernel address space
1945 * @size: size of the area
1947 * Returns: NULL on failure, vm_struct on success
1949 * This function reserves a range of kernel address space, and
1950 * allocates pagetables to map that range. No actual mappings
1951 * are created. If the kernel address space is not shared
1952 * between processes, it syncs the pagetable across all
1953 * processes.
1955 struct vm_struct *alloc_vm_area(size_t size)
1957 struct vm_struct *area;
1959 area = get_vm_area_caller(size, VM_IOREMAP,
1960 __builtin_return_address(0));
1961 if (area == NULL)
1962 return NULL;
1965 * This ensures that page tables are constructed for this region
1966 * of kernel virtual address space and mapped into init_mm.
1968 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1969 area->size, f, NULL)) {
1970 free_vm_area(area);
1971 return NULL;
1974 /* Make sure the pagetables are constructed in process kernel
1975 mappings */
1976 vmalloc_sync_all();
1978 return area;
1980 EXPORT_SYMBOL_GPL(alloc_vm_area);
1982 void free_vm_area(struct vm_struct *area)
1984 struct vm_struct *ret;
1985 ret = remove_vm_area(area->addr);
1986 BUG_ON(ret != area);
1987 kfree(area);
1989 EXPORT_SYMBOL_GPL(free_vm_area);
1991 static struct vmap_area *node_to_va(struct rb_node *n)
1993 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
1997 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
1998 * @end: target address
1999 * @pnext: out arg for the next vmap_area
2000 * @pprev: out arg for the previous vmap_area
2002 * Returns: %true if either or both of next and prev are found,
2003 * %false if no vmap_area exists
2005 * Find vmap_areas end addresses of which enclose @end. ie. if not
2006 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2008 static bool pvm_find_next_prev(unsigned long end,
2009 struct vmap_area **pnext,
2010 struct vmap_area **pprev)
2012 struct rb_node *n = vmap_area_root.rb_node;
2013 struct vmap_area *va = NULL;
2015 while (n) {
2016 va = rb_entry(n, struct vmap_area, rb_node);
2017 if (end < va->va_end)
2018 n = n->rb_left;
2019 else if (end > va->va_end)
2020 n = n->rb_right;
2021 else
2022 break;
2025 if (!va)
2026 return false;
2028 if (va->va_end > end) {
2029 *pnext = va;
2030 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2031 } else {
2032 *pprev = va;
2033 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2035 return true;
2039 * pvm_determine_end - find the highest aligned address between two vmap_areas
2040 * @pnext: in/out arg for the next vmap_area
2041 * @pprev: in/out arg for the previous vmap_area
2042 * @align: alignment
2044 * Returns: determined end address
2046 * Find the highest aligned address between *@pnext and *@pprev below
2047 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2048 * down address is between the end addresses of the two vmap_areas.
2050 * Please note that the address returned by this function may fall
2051 * inside *@pnext vmap_area. The caller is responsible for checking
2052 * that.
2054 static unsigned long pvm_determine_end(struct vmap_area **pnext,
2055 struct vmap_area **pprev,
2056 unsigned long align)
2058 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2059 unsigned long addr;
2061 if (*pnext)
2062 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2063 else
2064 addr = vmalloc_end;
2066 while (*pprev && (*pprev)->va_end > addr) {
2067 *pnext = *pprev;
2068 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2071 return addr;
2075 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2076 * @offsets: array containing offset of each area
2077 * @sizes: array containing size of each area
2078 * @nr_vms: the number of areas to allocate
2079 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2080 * @gfp_mask: allocation mask
2082 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2083 * vm_structs on success, %NULL on failure
2085 * Percpu allocator wants to use congruent vm areas so that it can
2086 * maintain the offsets among percpu areas. This function allocates
2087 * congruent vmalloc areas for it. These areas tend to be scattered
2088 * pretty far, distance between two areas easily going up to
2089 * gigabytes. To avoid interacting with regular vmallocs, these areas
2090 * are allocated from top.
2092 * Despite its complicated look, this allocator is rather simple. It
2093 * does everything top-down and scans areas from the end looking for
2094 * matching slot. While scanning, if any of the areas overlaps with
2095 * existing vmap_area, the base address is pulled down to fit the
2096 * area. Scanning is repeated till all the areas fit and then all
2097 * necessary data structres are inserted and the result is returned.
2099 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2100 const size_t *sizes, int nr_vms,
2101 size_t align, gfp_t gfp_mask)
2103 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2104 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2105 struct vmap_area **vas, *prev, *next;
2106 struct vm_struct **vms;
2107 int area, area2, last_area, term_area;
2108 unsigned long base, start, end, last_end;
2109 bool purged = false;
2111 gfp_mask &= GFP_RECLAIM_MASK;
2113 /* verify parameters and allocate data structures */
2114 BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
2115 for (last_area = 0, area = 0; area < nr_vms; area++) {
2116 start = offsets[area];
2117 end = start + sizes[area];
2119 /* is everything aligned properly? */
2120 BUG_ON(!IS_ALIGNED(offsets[area], align));
2121 BUG_ON(!IS_ALIGNED(sizes[area], align));
2123 /* detect the area with the highest address */
2124 if (start > offsets[last_area])
2125 last_area = area;
2127 for (area2 = 0; area2 < nr_vms; area2++) {
2128 unsigned long start2 = offsets[area2];
2129 unsigned long end2 = start2 + sizes[area2];
2131 if (area2 == area)
2132 continue;
2134 BUG_ON(start2 >= start && start2 < end);
2135 BUG_ON(end2 <= end && end2 > start);
2138 last_end = offsets[last_area] + sizes[last_area];
2140 if (vmalloc_end - vmalloc_start < last_end) {
2141 WARN_ON(true);
2142 return NULL;
2145 vms = kzalloc(sizeof(vms[0]) * nr_vms, gfp_mask);
2146 vas = kzalloc(sizeof(vas[0]) * nr_vms, gfp_mask);
2147 if (!vas || !vms)
2148 goto err_free;
2150 for (area = 0; area < nr_vms; area++) {
2151 vas[area] = kzalloc(sizeof(struct vmap_area), gfp_mask);
2152 vms[area] = kzalloc(sizeof(struct vm_struct), gfp_mask);
2153 if (!vas[area] || !vms[area])
2154 goto err_free;
2156 retry:
2157 spin_lock(&vmap_area_lock);
2159 /* start scanning - we scan from the top, begin with the last area */
2160 area = term_area = last_area;
2161 start = offsets[area];
2162 end = start + sizes[area];
2164 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2165 base = vmalloc_end - last_end;
2166 goto found;
2168 base = pvm_determine_end(&next, &prev, align) - end;
2170 while (true) {
2171 BUG_ON(next && next->va_end <= base + end);
2172 BUG_ON(prev && prev->va_end > base + end);
2175 * base might have underflowed, add last_end before
2176 * comparing.
2178 if (base + last_end < vmalloc_start + last_end) {
2179 spin_unlock(&vmap_area_lock);
2180 if (!purged) {
2181 purge_vmap_area_lazy();
2182 purged = true;
2183 goto retry;
2185 goto err_free;
2189 * If next overlaps, move base downwards so that it's
2190 * right below next and then recheck.
2192 if (next && next->va_start < base + end) {
2193 base = pvm_determine_end(&next, &prev, align) - end;
2194 term_area = area;
2195 continue;
2199 * If prev overlaps, shift down next and prev and move
2200 * base so that it's right below new next and then
2201 * recheck.
2203 if (prev && prev->va_end > base + start) {
2204 next = prev;
2205 prev = node_to_va(rb_prev(&next->rb_node));
2206 base = pvm_determine_end(&next, &prev, align) - end;
2207 term_area = area;
2208 continue;
2212 * This area fits, move on to the previous one. If
2213 * the previous one is the terminal one, we're done.
2215 area = (area + nr_vms - 1) % nr_vms;
2216 if (area == term_area)
2217 break;
2218 start = offsets[area];
2219 end = start + sizes[area];
2220 pvm_find_next_prev(base + end, &next, &prev);
2222 found:
2223 /* we've found a fitting base, insert all va's */
2224 for (area = 0; area < nr_vms; area++) {
2225 struct vmap_area *va = vas[area];
2227 va->va_start = base + offsets[area];
2228 va->va_end = va->va_start + sizes[area];
2229 __insert_vmap_area(va);
2232 vmap_area_pcpu_hole = base + offsets[last_area];
2234 spin_unlock(&vmap_area_lock);
2236 /* insert all vm's */
2237 for (area = 0; area < nr_vms; area++)
2238 insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2239 pcpu_get_vm_areas);
2241 kfree(vas);
2242 return vms;
2244 err_free:
2245 for (area = 0; area < nr_vms; area++) {
2246 if (vas)
2247 kfree(vas[area]);
2248 if (vms)
2249 kfree(vms[area]);
2251 kfree(vas);
2252 kfree(vms);
2253 return NULL;
2257 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2258 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2259 * @nr_vms: the number of allocated areas
2261 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2263 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2265 int i;
2267 for (i = 0; i < nr_vms; i++)
2268 free_vm_area(vms[i]);
2269 kfree(vms);
2272 #ifdef CONFIG_PROC_FS
2273 static void *s_start(struct seq_file *m, loff_t *pos)
2275 loff_t n = *pos;
2276 struct vm_struct *v;
2278 read_lock(&vmlist_lock);
2279 v = vmlist;
2280 while (n > 0 && v) {
2281 n--;
2282 v = v->next;
2284 if (!n)
2285 return v;
2287 return NULL;
2291 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2293 struct vm_struct *v = p;
2295 ++*pos;
2296 return v->next;
2299 static void s_stop(struct seq_file *m, void *p)
2301 read_unlock(&vmlist_lock);
2304 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2306 if (NUMA_BUILD) {
2307 unsigned int nr, *counters = m->private;
2309 if (!counters)
2310 return;
2312 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2314 for (nr = 0; nr < v->nr_pages; nr++)
2315 counters[page_to_nid(v->pages[nr])]++;
2317 for_each_node_state(nr, N_HIGH_MEMORY)
2318 if (counters[nr])
2319 seq_printf(m, " N%u=%u", nr, counters[nr]);
2323 static int s_show(struct seq_file *m, void *p)
2325 struct vm_struct *v = p;
2327 seq_printf(m, "0x%p-0x%p %7ld",
2328 v->addr, v->addr + v->size, v->size);
2330 if (v->caller) {
2331 char buff[KSYM_SYMBOL_LEN];
2333 seq_putc(m, ' ');
2334 sprint_symbol(buff, (unsigned long)v->caller);
2335 seq_puts(m, buff);
2338 if (v->nr_pages)
2339 seq_printf(m, " pages=%d", v->nr_pages);
2341 if (v->phys_addr)
2342 seq_printf(m, " phys=%lx", v->phys_addr);
2344 if (v->flags & VM_IOREMAP)
2345 seq_printf(m, " ioremap");
2347 if (v->flags & VM_ALLOC)
2348 seq_printf(m, " vmalloc");
2350 if (v->flags & VM_MAP)
2351 seq_printf(m, " vmap");
2353 if (v->flags & VM_USERMAP)
2354 seq_printf(m, " user");
2356 if (v->flags & VM_VPAGES)
2357 seq_printf(m, " vpages");
2359 show_numa_info(m, v);
2360 seq_putc(m, '\n');
2361 return 0;
2364 static const struct seq_operations vmalloc_op = {
2365 .start = s_start,
2366 .next = s_next,
2367 .stop = s_stop,
2368 .show = s_show,
2371 static int vmalloc_open(struct inode *inode, struct file *file)
2373 unsigned int *ptr = NULL;
2374 int ret;
2376 if (NUMA_BUILD)
2377 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2378 ret = seq_open(file, &vmalloc_op);
2379 if (!ret) {
2380 struct seq_file *m = file->private_data;
2381 m->private = ptr;
2382 } else
2383 kfree(ptr);
2384 return ret;
2387 static const struct file_operations proc_vmalloc_operations = {
2388 .open = vmalloc_open,
2389 .read = seq_read,
2390 .llseek = seq_lseek,
2391 .release = seq_release_private,
2394 static int __init proc_vmalloc_init(void)
2396 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2397 return 0;
2399 module_init(proc_vmalloc_init);
2400 #endif