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
11 #include <linux/vmalloc.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/interrupt.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/debugobjects.h>
22 #include <linux/kallsyms.h>
23 #include <linux/list.h>
24 #include <linux/rbtree.h>
25 #include <linux/radix-tree.h>
26 #include <linux/rcupdate.h>
27 #include <linux/pfn.h>
28 #include <linux/kmemleak.h>
29 #include <linux/atomic.h>
30 #include <linux/llist.h>
31 #include <asm/uaccess.h>
32 #include <asm/tlbflush.h>
33 #include <asm/shmparam.h>
36 #include <asm/pgalloc.h> /* For pmd_alloc_kernel() and friends */
40 struct vfree_deferred
{
41 struct llist_head list
;
42 struct work_struct wq
;
44 static DEFINE_PER_CPU(struct vfree_deferred
, vfree_deferred
);
46 static void __vunmap(const void *, int);
48 static void free_work(struct work_struct
*w
)
50 struct vfree_deferred
*p
= container_of(w
, struct vfree_deferred
, wq
);
51 struct llist_node
*llnode
= llist_del_all(&p
->list
);
54 llnode
= llist_next(llnode
);
59 /*** Page table manipulation functions ***/
61 static void vunmap_pte_range(pmd_t
*pmd
, unsigned long addr
, unsigned long end
)
65 pte
= pte_offset_kernel(pmd
, addr
);
67 pte_t ptent
= ptep_get_and_clear(&init_mm
, addr
, pte
);
68 WARN_ON(!pte_none(ptent
) && !pte_present(ptent
));
69 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
72 static void vunmap_pmd_range(pud_t
*pud
, unsigned long addr
, unsigned long end
)
78 pmd
= pmd_offset_kernel(pud
, addr
);
80 pmd
= pmd_offset(pud
, addr
);
83 next
= pmd_addr_end(addr
, end
);
85 if (pmd_none_or_clear_bad_kernel(pmd
))
87 if (pmd_none_or_clear_bad(pmd
))
90 vunmap_pte_range(pmd
, addr
, next
);
91 } while (pmd
++, addr
= next
, addr
!= end
);
94 static void vunmap_pud_range(pgd_t
*pgd
, unsigned long addr
, unsigned long end
)
100 pud
= pud_offset_kernel(pgd
, addr
);
102 pud
= pud_offset(pgd
, addr
);
105 next
= pud_addr_end(addr
, end
);
107 if (pud_none_or_clear_bad_kernel(pud
))
109 if (pud_none_or_clear_bad(pud
))
112 vunmap_pmd_range(pud
, addr
, next
);
113 } while (pud
++, addr
= next
, addr
!= end
);
116 static void vunmap_page_range(unsigned long addr
, unsigned long end
)
120 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
121 unsigned long start
= addr
;
125 pgd
= pgd_offset_k(addr
);
127 next
= pgd_addr_end(addr
, end
);
129 if (pgd_none_or_clear_bad_kernel(pgd
))
131 if (pgd_none_or_clear_bad(pgd
))
134 vunmap_pud_range(pgd
, addr
, next
);
135 } while (pgd
++, addr
= next
, addr
!= end
);
137 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
138 all_nodes_unmap_kernel_vm_area_noflush(start
, end
);
142 static int vmap_pte_range(pmd_t
*pmd
, unsigned long addr
,
143 unsigned long end
, pgprot_t prot
, struct page
**pages
, int *nr
)
148 * nr is a running index into the array which helps higher level
149 * callers keep track of where we're up to.
152 pte
= pte_alloc_kernel(pmd
, addr
);
156 struct page
*page
= pages
[*nr
];
158 if (WARN_ON(!pte_none(*pte
)))
162 set_pte_at(&init_mm
, addr
, pte
, mk_pte(page
, prot
));
164 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
168 static int vmap_pmd_range(pud_t
*pud
, unsigned long addr
,
169 unsigned long end
, pgprot_t prot
, struct page
**pages
, int *nr
)
175 pmd
= pmd_alloc_kernel(&init_mm
, pud
, addr
);
177 pmd
= pmd_alloc(&init_mm
, pud
, addr
);
182 next
= pmd_addr_end(addr
, end
);
183 if (vmap_pte_range(pmd
, addr
, next
, prot
, pages
, nr
))
185 } while (pmd
++, addr
= next
, addr
!= end
);
189 static int vmap_pud_range(pgd_t
*pgd
, unsigned long addr
,
190 unsigned long end
, pgprot_t prot
, struct page
**pages
, int *nr
)
196 pud
= pud_alloc_kernel(&init_mm
, pgd
, addr
);
198 pud
= pud_alloc(&init_mm
, pgd
, addr
);
203 next
= pud_addr_end(addr
, end
);
204 if (vmap_pmd_range(pud
, addr
, next
, prot
, pages
, nr
))
206 } while (pud
++, addr
= next
, addr
!= end
);
211 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
212 * will have pfns corresponding to the "pages" array.
214 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
216 static int vmap_page_range_noflush(unsigned long start
, unsigned long end
,
217 pgprot_t prot
, struct page
**pages
)
221 unsigned long addr
= start
;
224 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
225 int nid
= numa_node_id();
229 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
230 pgd
= node_pgd_offset_kernel(nid
, addr
);
232 pgd
= pgd_offset_k(addr
);
235 next
= pgd_addr_end(addr
, end
);
236 err
= vmap_pud_range(pgd
, addr
, next
, prot
, pages
, &nr
);
239 } while (pgd
++, addr
= next
, addr
!= end
);
240 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
241 if (all_other_nodes_map_vm_area(nid
, start
, end
- start
)) {
242 panic("Could not map VM area from addr 0x%lx to 0x%lx on all numa nodes\n",
250 static int vmap_page_range(unsigned long start
, unsigned long end
,
251 pgprot_t prot
, struct page
**pages
)
255 ret
= vmap_page_range_noflush(start
, end
, prot
, pages
);
256 flush_cache_vmap(start
, end
);
260 int is_vmalloc_or_module_addr(const void *x
)
263 * ARM, x86-64 and sparc64 put modules in a special place,
264 * and fall back on vmalloc() if that fails. Others
265 * just put it in the vmalloc space.
267 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
268 unsigned long addr
= (unsigned long)x
;
269 if (addr
>= MODULES_VADDR
&& addr
< MODULES_END
)
272 return is_vmalloc_addr(x
);
276 * Walk a vmap address to the struct page it maps.
278 struct page
*vmalloc_to_page(const void *vmalloc_addr
)
280 unsigned long addr
= (unsigned long) vmalloc_addr
;
281 struct page
*page
= NULL
;
282 pgd_t
*pgd
= pgd_offset_k(addr
);
285 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
286 * architectures that do not vmalloc module space
288 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr
));
290 if (!pgd_none(*pgd
)) {
291 pud_t
*pud
= pud_offset(pgd
, addr
);
292 if (!pud_none(*pud
)) {
293 pmd_t
*pmd
= pmd_offset(pud
, addr
);
294 if (!pmd_none(*pmd
)) {
297 ptep
= pte_offset_map(pmd
, addr
);
299 if (pte_present(pte
))
300 page
= pte_page(pte
);
307 EXPORT_SYMBOL(vmalloc_to_page
);
310 * Map a vmalloc()-space virtual address to the physical page frame number.
312 unsigned long vmalloc_to_pfn(const void *vmalloc_addr
)
314 return page_to_pfn(vmalloc_to_page(vmalloc_addr
));
316 EXPORT_SYMBOL(vmalloc_to_pfn
);
319 /*** Global kva allocator ***/
321 #define VM_LAZY_FREE 0x01
322 #define VM_LAZY_FREEING 0x02
323 #define VM_VM_AREA 0x04
325 static DEFINE_SPINLOCK(vmap_area_lock
);
326 /* Export for kexec only */
327 LIST_HEAD(vmap_area_list
);
328 static struct rb_root vmap_area_root
= RB_ROOT
;
330 /* The vmap cache globals are protected by vmap_area_lock */
331 static struct rb_node
*free_vmap_cache
;
332 static unsigned long cached_hole_size
;
333 static unsigned long cached_vstart
;
334 static unsigned long cached_align
;
336 static unsigned long vmap_area_pcpu_hole
;
338 static struct vmap_area
*__find_vmap_area(unsigned long addr
)
340 struct rb_node
*n
= vmap_area_root
.rb_node
;
343 struct vmap_area
*va
;
345 va
= rb_entry(n
, struct vmap_area
, rb_node
);
346 if (addr
< va
->va_start
)
348 else if (addr
>= va
->va_end
)
357 static void __insert_vmap_area(struct vmap_area
*va
)
359 struct rb_node
**p
= &vmap_area_root
.rb_node
;
360 struct rb_node
*parent
= NULL
;
364 struct vmap_area
*tmp_va
;
367 tmp_va
= rb_entry(parent
, struct vmap_area
, rb_node
);
368 if (va
->va_start
< tmp_va
->va_end
)
370 else if (va
->va_end
> tmp_va
->va_start
)
376 rb_link_node(&va
->rb_node
, parent
, p
);
377 rb_insert_color(&va
->rb_node
, &vmap_area_root
);
379 /* address-sort this list */
380 tmp
= rb_prev(&va
->rb_node
);
382 struct vmap_area
*prev
;
383 prev
= rb_entry(tmp
, struct vmap_area
, rb_node
);
384 list_add_rcu(&va
->list
, &prev
->list
);
386 list_add_rcu(&va
->list
, &vmap_area_list
);
389 static void purge_vmap_area_lazy(void);
392 * Allocate a region of KVA of the specified size and alignment, within the
395 static struct vmap_area
*alloc_vmap_area(unsigned long size
,
397 unsigned long vstart
, unsigned long vend
,
398 int node
, gfp_t gfp_mask
)
400 struct vmap_area
*va
;
404 struct vmap_area
*first
;
407 BUG_ON(size
& ~PAGE_MASK
);
408 BUG_ON(!is_power_of_2(align
));
410 va
= kmalloc_node(sizeof(struct vmap_area
),
411 gfp_mask
& GFP_RECLAIM_MASK
, node
);
413 return ERR_PTR(-ENOMEM
);
416 * Only scan the relevant parts containing pointers to other objects
417 * to avoid false negatives.
419 kmemleak_scan_area(&va
->rb_node
, SIZE_MAX
, gfp_mask
& GFP_RECLAIM_MASK
);
422 spin_lock(&vmap_area_lock
);
424 * Invalidate cache if we have more permissive parameters.
425 * cached_hole_size notes the largest hole noticed _below_
426 * the vmap_area cached in free_vmap_cache: if size fits
427 * into that hole, we want to scan from vstart to reuse
428 * the hole instead of allocating above free_vmap_cache.
429 * Note that __free_vmap_area may update free_vmap_cache
430 * without updating cached_hole_size or cached_align.
432 if (!free_vmap_cache
||
433 size
< cached_hole_size
||
434 vstart
< cached_vstart
||
435 align
< cached_align
) {
437 cached_hole_size
= 0;
438 free_vmap_cache
= NULL
;
440 /* record if we encounter less permissive parameters */
441 cached_vstart
= vstart
;
442 cached_align
= align
;
444 /* find starting point for our search */
445 if (free_vmap_cache
) {
446 first
= rb_entry(free_vmap_cache
, struct vmap_area
, rb_node
);
447 addr
= ALIGN(first
->va_end
, align
);
450 if (addr
+ size
< addr
)
454 addr
= ALIGN(vstart
, align
);
455 if (addr
+ size
< addr
)
458 n
= vmap_area_root
.rb_node
;
462 struct vmap_area
*tmp
;
463 tmp
= rb_entry(n
, struct vmap_area
, rb_node
);
464 if (tmp
->va_end
>= addr
) {
466 if (tmp
->va_start
<= addr
)
477 /* from the starting point, walk areas until a suitable hole is found */
478 while (addr
+ size
> first
->va_start
&& addr
+ size
<= vend
) {
479 if (addr
+ cached_hole_size
< first
->va_start
)
480 cached_hole_size
= first
->va_start
- addr
;
481 addr
= ALIGN(first
->va_end
, align
);
482 if (addr
+ size
< addr
)
485 if (list_is_last(&first
->list
, &vmap_area_list
))
488 first
= list_entry(first
->list
.next
,
489 struct vmap_area
, list
);
493 if (addr
+ size
> vend
)
497 va
->va_end
= addr
+ size
;
499 __insert_vmap_area(va
);
500 free_vmap_cache
= &va
->rb_node
;
501 spin_unlock(&vmap_area_lock
);
503 BUG_ON(va
->va_start
& (align
-1));
504 BUG_ON(va
->va_start
< vstart
);
505 BUG_ON(va
->va_end
> vend
);
510 spin_unlock(&vmap_area_lock
);
512 purge_vmap_area_lazy();
516 if (printk_ratelimit())
518 "vmap allocation for size %lu failed: "
519 "use vmalloc=<size> to increase size.\n", size
);
521 return ERR_PTR(-EBUSY
);
524 static void __free_vmap_area(struct vmap_area
*va
)
526 BUG_ON(RB_EMPTY_NODE(&va
->rb_node
));
528 if (free_vmap_cache
) {
529 if (va
->va_end
< cached_vstart
) {
530 free_vmap_cache
= NULL
;
532 struct vmap_area
*cache
;
533 cache
= rb_entry(free_vmap_cache
, struct vmap_area
, rb_node
);
534 if (va
->va_start
<= cache
->va_start
) {
535 free_vmap_cache
= rb_prev(&va
->rb_node
);
537 * We don't try to update cached_hole_size or
538 * cached_align, but it won't go very wrong.
543 rb_erase(&va
->rb_node
, &vmap_area_root
);
544 RB_CLEAR_NODE(&va
->rb_node
);
545 list_del_rcu(&va
->list
);
548 * Track the highest possible candidate for pcpu area
549 * allocation. Areas outside of vmalloc area can be returned
550 * here too, consider only end addresses which fall inside
551 * vmalloc area proper.
553 if (va
->va_end
> VMALLOC_START
&& va
->va_end
<= VMALLOC_END
)
554 vmap_area_pcpu_hole
= max(vmap_area_pcpu_hole
, va
->va_end
);
556 kfree_rcu(va
, rcu_head
);
560 * Free a region of KVA allocated by alloc_vmap_area
562 static void free_vmap_area(struct vmap_area
*va
)
564 spin_lock(&vmap_area_lock
);
565 __free_vmap_area(va
);
566 spin_unlock(&vmap_area_lock
);
570 * Clear the pagetable entries of a given vmap_area
572 static void unmap_vmap_area(struct vmap_area
*va
)
574 vunmap_page_range(va
->va_start
, va
->va_end
);
577 static void vmap_debug_free_range(unsigned long start
, unsigned long end
)
580 * Unmap page tables and force a TLB flush immediately if
581 * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
582 * bugs similarly to those in linear kernel virtual address
583 * space after a page has been freed.
585 * All the lazy freeing logic is still retained, in order to
586 * minimise intrusiveness of this debugging feature.
588 * This is going to be *slow* (linear kernel virtual address
589 * debugging doesn't do a broadcast TLB flush so it is a lot
592 #ifdef CONFIG_DEBUG_PAGEALLOC
593 vunmap_page_range(start
, end
);
594 flush_tlb_kernel_range(start
, end
);
599 * lazy_max_pages is the maximum amount of virtual address space we gather up
600 * before attempting to purge with a TLB flush.
602 * There is a tradeoff here: a larger number will cover more kernel page tables
603 * and take slightly longer to purge, but it will linearly reduce the number of
604 * global TLB flushes that must be performed. It would seem natural to scale
605 * this number up linearly with the number of CPUs (because vmapping activity
606 * could also scale linearly with the number of CPUs), however it is likely
607 * that in practice, workloads might be constrained in other ways that mean
608 * vmap activity will not scale linearly with CPUs. Also, I want to be
609 * conservative and not introduce a big latency on huge systems, so go with
610 * a less aggressive log scale. It will still be an improvement over the old
611 * code, and it will be simple to change the scale factor if we find that it
612 * becomes a problem on bigger systems.
614 static unsigned long lazy_max_pages(void)
618 log
= fls(num_online_cpus());
620 return log
* (32UL * 1024 * 1024 / PAGE_SIZE
);
623 static atomic_t vmap_lazy_nr
= ATOMIC_INIT(0);
625 /* for per-CPU blocks */
626 static void purge_fragmented_blocks_allcpus(void);
629 * called before a call to iounmap() if the caller wants vm_area_struct's
632 void set_iounmap_nonlazy(void)
634 atomic_set(&vmap_lazy_nr
, lazy_max_pages()+1);
638 * Purges all lazily-freed vmap areas.
640 * If sync is 0 then don't purge if there is already a purge in progress.
641 * If force_flush is 1, then flush kernel TLBs between *start and *end even
642 * if we found no lazy vmap areas to unmap (callers can use this to optimise
643 * their own TLB flushing).
644 * Returns with *start = min(*start, lowest purged address)
645 * *end = max(*end, highest purged address)
647 static void __purge_vmap_area_lazy(unsigned long *start
, unsigned long *end
,
648 int sync
, int force_flush
)
650 static DEFINE_SPINLOCK(purge_lock
);
652 struct vmap_area
*va
;
653 struct vmap_area
*n_va
;
657 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
658 * should not expect such behaviour. This just simplifies locking for
659 * the case that isn't actually used at the moment anyway.
661 if (!sync
&& !force_flush
) {
662 if (!spin_trylock(&purge_lock
))
665 spin_lock(&purge_lock
);
668 purge_fragmented_blocks_allcpus();
671 list_for_each_entry_rcu(va
, &vmap_area_list
, list
) {
672 if (va
->flags
& VM_LAZY_FREE
) {
673 if (va
->va_start
< *start
)
674 *start
= va
->va_start
;
675 if (va
->va_end
> *end
)
677 nr
+= (va
->va_end
- va
->va_start
) >> PAGE_SHIFT
;
678 list_add_tail(&va
->purge_list
, &valist
);
679 va
->flags
|= VM_LAZY_FREEING
;
680 va
->flags
&= ~VM_LAZY_FREE
;
686 atomic_sub(nr
, &vmap_lazy_nr
);
688 if (nr
|| force_flush
)
689 flush_tlb_kernel_range(*start
, *end
);
692 spin_lock(&vmap_area_lock
);
693 list_for_each_entry_safe(va
, n_va
, &valist
, purge_list
)
694 __free_vmap_area(va
);
695 spin_unlock(&vmap_area_lock
);
697 spin_unlock(&purge_lock
);
701 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
702 * is already purging.
704 static void try_purge_vmap_area_lazy(void)
706 unsigned long start
= ULONG_MAX
, end
= 0;
708 __purge_vmap_area_lazy(&start
, &end
, 0, 0);
712 * Kick off a purge of the outstanding lazy areas.
714 static void purge_vmap_area_lazy(void)
716 unsigned long start
= ULONG_MAX
, end
= 0;
718 __purge_vmap_area_lazy(&start
, &end
, 1, 0);
722 * Free a vmap area, caller ensuring that the area has been unmapped
723 * and flush_cache_vunmap had been called for the correct range
726 static void free_vmap_area_noflush(struct vmap_area
*va
)
728 va
->flags
|= VM_LAZY_FREE
;
729 atomic_add((va
->va_end
- va
->va_start
) >> PAGE_SHIFT
, &vmap_lazy_nr
);
730 if (unlikely(atomic_read(&vmap_lazy_nr
) > lazy_max_pages()))
731 try_purge_vmap_area_lazy();
735 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
736 * called for the correct range previously.
738 static void free_unmap_vmap_area_noflush(struct vmap_area
*va
)
741 free_vmap_area_noflush(va
);
745 * Free and unmap a vmap area
747 static void free_unmap_vmap_area(struct vmap_area
*va
)
749 flush_cache_vunmap(va
->va_start
, va
->va_end
);
750 free_unmap_vmap_area_noflush(va
);
753 static struct vmap_area
*find_vmap_area(unsigned long addr
)
755 struct vmap_area
*va
;
757 spin_lock(&vmap_area_lock
);
758 va
= __find_vmap_area(addr
);
759 spin_unlock(&vmap_area_lock
);
764 static void free_unmap_vmap_area_addr(unsigned long addr
)
766 struct vmap_area
*va
;
768 va
= find_vmap_area(addr
);
770 free_unmap_vmap_area(va
);
774 /*** Per cpu kva allocator ***/
777 * vmap space is limited especially on 32 bit architectures. Ensure there is
778 * room for at least 16 percpu vmap blocks per CPU.
781 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
782 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
783 * instead (we just need a rough idea)
785 #if BITS_PER_LONG == 32
786 #define VMALLOC_SPACE (128UL*1024*1024)
788 #define VMALLOC_SPACE (128UL*1024*1024*1024)
791 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
792 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
793 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
794 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
795 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
796 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
797 #define VMAP_BBMAP_BITS \
798 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
799 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
800 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
802 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
804 static bool vmap_initialized __read_mostly
= false;
806 struct vmap_block_queue
{
808 struct list_head free
;
813 struct vmap_area
*va
;
814 unsigned long free
, dirty
;
815 DECLARE_BITMAP(dirty_map
, VMAP_BBMAP_BITS
);
816 struct list_head free_list
;
817 struct rcu_head rcu_head
;
818 struct list_head purge
;
821 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
822 static DEFINE_PER_CPU(struct vmap_block_queue
, vmap_block_queue
);
825 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
826 * in the free path. Could get rid of this if we change the API to return a
827 * "cookie" from alloc, to be passed to free. But no big deal yet.
829 static DEFINE_SPINLOCK(vmap_block_tree_lock
);
830 static RADIX_TREE(vmap_block_tree
, GFP_ATOMIC
);
833 * We should probably have a fallback mechanism to allocate virtual memory
834 * out of partially filled vmap blocks. However vmap block sizing should be
835 * fairly reasonable according to the vmalloc size, so it shouldn't be a
839 static unsigned long addr_to_vb_idx(unsigned long addr
)
841 addr
-= VMALLOC_START
& ~(VMAP_BLOCK_SIZE
-1);
842 addr
/= VMAP_BLOCK_SIZE
;
846 static struct vmap_block
*new_vmap_block(gfp_t gfp_mask
)
848 struct vmap_block_queue
*vbq
;
849 struct vmap_block
*vb
;
850 struct vmap_area
*va
;
851 unsigned long vb_idx
;
854 node
= numa_node_id();
856 vb
= kmalloc_node(sizeof(struct vmap_block
),
857 gfp_mask
& GFP_RECLAIM_MASK
, node
);
859 return ERR_PTR(-ENOMEM
);
861 va
= alloc_vmap_area(VMAP_BLOCK_SIZE
, VMAP_BLOCK_SIZE
,
862 VMALLOC_START
, VMALLOC_END
,
869 err
= radix_tree_preload(gfp_mask
);
876 spin_lock_init(&vb
->lock
);
878 vb
->free
= VMAP_BBMAP_BITS
;
880 bitmap_zero(vb
->dirty_map
, VMAP_BBMAP_BITS
);
881 INIT_LIST_HEAD(&vb
->free_list
);
883 vb_idx
= addr_to_vb_idx(va
->va_start
);
884 spin_lock(&vmap_block_tree_lock
);
885 err
= radix_tree_insert(&vmap_block_tree
, vb_idx
, vb
);
886 spin_unlock(&vmap_block_tree_lock
);
888 radix_tree_preload_end();
890 cpu
= get_cpu_light();
891 vbq
= &__get_cpu_var(vmap_block_queue
);
892 spin_lock(&vbq
->lock
);
893 list_add_rcu(&vb
->free_list
, &vbq
->free
);
894 spin_unlock(&vbq
->lock
);
900 static void free_vmap_block(struct vmap_block
*vb
)
902 struct vmap_block
*tmp
;
903 unsigned long vb_idx
;
905 vb_idx
= addr_to_vb_idx(vb
->va
->va_start
);
906 spin_lock(&vmap_block_tree_lock
);
907 tmp
= radix_tree_delete(&vmap_block_tree
, vb_idx
);
908 spin_unlock(&vmap_block_tree_lock
);
911 free_vmap_area_noflush(vb
->va
);
912 kfree_rcu(vb
, rcu_head
);
915 static void purge_fragmented_blocks(int cpu
)
918 struct vmap_block
*vb
;
919 struct vmap_block
*n_vb
;
920 struct vmap_block_queue
*vbq
= &per_cpu(vmap_block_queue
, cpu
);
923 list_for_each_entry_rcu(vb
, &vbq
->free
, free_list
) {
925 if (!(vb
->free
+ vb
->dirty
== VMAP_BBMAP_BITS
&& vb
->dirty
!= VMAP_BBMAP_BITS
))
928 spin_lock(&vb
->lock
);
929 if (vb
->free
+ vb
->dirty
== VMAP_BBMAP_BITS
&& vb
->dirty
!= VMAP_BBMAP_BITS
) {
930 vb
->free
= 0; /* prevent further allocs after releasing lock */
931 vb
->dirty
= VMAP_BBMAP_BITS
; /* prevent purging it again */
932 bitmap_fill(vb
->dirty_map
, VMAP_BBMAP_BITS
);
933 spin_lock(&vbq
->lock
);
934 list_del_rcu(&vb
->free_list
);
935 spin_unlock(&vbq
->lock
);
936 spin_unlock(&vb
->lock
);
937 list_add_tail(&vb
->purge
, &purge
);
939 spin_unlock(&vb
->lock
);
943 list_for_each_entry_safe(vb
, n_vb
, &purge
, purge
) {
944 list_del(&vb
->purge
);
949 static void purge_fragmented_blocks_allcpus(void)
953 for_each_possible_cpu(cpu
)
954 purge_fragmented_blocks(cpu
);
957 static void *vb_alloc(unsigned long size
, gfp_t gfp_mask
)
959 struct vmap_block_queue
*vbq
;
960 struct vmap_block
*vb
;
961 unsigned long addr
= 0;
965 BUG_ON(size
& ~PAGE_MASK
);
966 BUG_ON(size
> PAGE_SIZE
*VMAP_MAX_ALLOC
);
967 if (WARN_ON(size
== 0)) {
969 * Allocating 0 bytes isn't what caller wants since
970 * get_order(0) returns funny result. Just warn and terminate
975 order
= get_order(size
);
979 cpu
= get_cpu_light();
980 vbq
= &__get_cpu_var(vmap_block_queue
);
981 list_for_each_entry_rcu(vb
, &vbq
->free
, free_list
) {
984 spin_lock(&vb
->lock
);
985 if (vb
->free
< 1UL << order
)
988 i
= VMAP_BBMAP_BITS
- vb
->free
;
989 addr
= vb
->va
->va_start
+ (i
<< PAGE_SHIFT
);
990 BUG_ON(addr_to_vb_idx(addr
) !=
991 addr_to_vb_idx(vb
->va
->va_start
));
992 vb
->free
-= 1UL << order
;
994 spin_lock(&vbq
->lock
);
995 list_del_rcu(&vb
->free_list
);
996 spin_unlock(&vbq
->lock
);
998 spin_unlock(&vb
->lock
);
1001 spin_unlock(&vb
->lock
);
1008 vb
= new_vmap_block(gfp_mask
);
1014 return (void *)addr
;
1017 static void vb_free(const void *addr
, unsigned long size
)
1019 unsigned long offset
;
1020 unsigned long vb_idx
;
1022 struct vmap_block
*vb
;
1024 BUG_ON(size
& ~PAGE_MASK
);
1025 BUG_ON(size
> PAGE_SIZE
*VMAP_MAX_ALLOC
);
1027 flush_cache_vunmap((unsigned long)addr
, (unsigned long)addr
+ size
);
1029 order
= get_order(size
);
1031 offset
= (unsigned long)addr
& (VMAP_BLOCK_SIZE
- 1);
1033 vb_idx
= addr_to_vb_idx((unsigned long)addr
);
1035 vb
= radix_tree_lookup(&vmap_block_tree
, vb_idx
);
1039 vunmap_page_range((unsigned long)addr
, (unsigned long)addr
+ size
);
1041 spin_lock(&vb
->lock
);
1042 BUG_ON(bitmap_allocate_region(vb
->dirty_map
, offset
>> PAGE_SHIFT
, order
));
1044 vb
->dirty
+= 1UL << order
;
1045 if (vb
->dirty
== VMAP_BBMAP_BITS
) {
1047 spin_unlock(&vb
->lock
);
1048 free_vmap_block(vb
);
1050 spin_unlock(&vb
->lock
);
1054 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1056 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1057 * to amortize TLB flushing overheads. What this means is that any page you
1058 * have now, may, in a former life, have been mapped into kernel virtual
1059 * address by the vmap layer and so there might be some CPUs with TLB entries
1060 * still referencing that page (additional to the regular 1:1 kernel mapping).
1062 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1063 * be sure that none of the pages we have control over will have any aliases
1064 * from the vmap layer.
1066 void vm_unmap_aliases(void)
1068 unsigned long start
= ULONG_MAX
, end
= 0;
1072 if (unlikely(!vmap_initialized
))
1075 for_each_possible_cpu(cpu
) {
1076 struct vmap_block_queue
*vbq
= &per_cpu(vmap_block_queue
, cpu
);
1077 struct vmap_block
*vb
;
1080 list_for_each_entry_rcu(vb
, &vbq
->free
, free_list
) {
1083 spin_lock(&vb
->lock
);
1084 i
= find_first_bit(vb
->dirty_map
, VMAP_BBMAP_BITS
);
1085 if (i
< VMAP_BBMAP_BITS
) {
1088 j
= find_last_bit(vb
->dirty_map
,
1090 j
= j
+ 1; /* need exclusive index */
1092 s
= vb
->va
->va_start
+ (i
<< PAGE_SHIFT
);
1093 e
= vb
->va
->va_start
+ (j
<< PAGE_SHIFT
);
1101 spin_unlock(&vb
->lock
);
1106 __purge_vmap_area_lazy(&start
, &end
, 1, flush
);
1108 EXPORT_SYMBOL_GPL(vm_unmap_aliases
);
1111 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1112 * @mem: the pointer returned by vm_map_ram
1113 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1115 void vm_unmap_ram(const void *mem
, unsigned int count
)
1117 unsigned long size
= count
<< PAGE_SHIFT
;
1118 unsigned long addr
= (unsigned long)mem
;
1121 BUG_ON(addr
< VMALLOC_START
);
1122 BUG_ON(addr
> VMALLOC_END
);
1123 BUG_ON(addr
& (PAGE_SIZE
-1));
1125 debug_check_no_locks_freed(mem
, size
);
1126 vmap_debug_free_range(addr
, addr
+size
);
1128 if (likely(count
<= VMAP_MAX_ALLOC
))
1131 free_unmap_vmap_area_addr(addr
);
1133 EXPORT_SYMBOL(vm_unmap_ram
);
1136 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1137 * @pages: an array of pointers to the pages to be mapped
1138 * @count: number of pages
1139 * @node: prefer to allocate data structures on this node
1140 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1142 * Returns: a pointer to the address that has been mapped, or %NULL on failure
1144 void *vm_map_ram(struct page
**pages
, unsigned int count
, int node
, pgprot_t prot
)
1146 unsigned long size
= count
<< PAGE_SHIFT
;
1150 if (likely(count
<= VMAP_MAX_ALLOC
)) {
1151 mem
= vb_alloc(size
, GFP_KERNEL
);
1154 addr
= (unsigned long)mem
;
1156 struct vmap_area
*va
;
1157 va
= alloc_vmap_area(size
, PAGE_SIZE
,
1158 VMALLOC_START
, VMALLOC_END
, node
, GFP_KERNEL
);
1162 addr
= va
->va_start
;
1165 if (vmap_page_range(addr
, addr
+ size
, prot
, pages
) < 0) {
1166 vm_unmap_ram(mem
, count
);
1171 EXPORT_SYMBOL(vm_map_ram
);
1173 static struct vm_struct
*vmlist __initdata
;
1175 * vm_area_add_early - add vmap area early during boot
1176 * @vm: vm_struct to add
1178 * This function is used to add fixed kernel vm area to vmlist before
1179 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1180 * should contain proper values and the other fields should be zero.
1182 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1184 void __init
vm_area_add_early(struct vm_struct
*vm
)
1186 struct vm_struct
*tmp
, **p
;
1188 BUG_ON(vmap_initialized
);
1189 for (p
= &vmlist
; (tmp
= *p
) != NULL
; p
= &tmp
->next
) {
1190 if (tmp
->addr
>= vm
->addr
) {
1191 BUG_ON(tmp
->addr
< vm
->addr
+ vm
->size
);
1194 BUG_ON(tmp
->addr
+ tmp
->size
> vm
->addr
);
1201 * vm_area_register_early - register vmap area early during boot
1202 * @vm: vm_struct to register
1203 * @align: requested alignment
1205 * This function is used to register kernel vm area before
1206 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1207 * proper values on entry and other fields should be zero. On return,
1208 * vm->addr contains the allocated address.
1210 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1212 void __init
vm_area_register_early(struct vm_struct
*vm
, size_t align
)
1214 static size_t vm_init_off __initdata
;
1217 addr
= ALIGN(VMALLOC_START
+ vm_init_off
, align
);
1218 vm_init_off
= PFN_ALIGN(addr
+ vm
->size
) - VMALLOC_START
;
1220 vm
->addr
= (void *)addr
;
1222 vm_area_add_early(vm
);
1225 void __init
vmalloc_init(void)
1227 struct vmap_area
*va
;
1228 struct vm_struct
*tmp
;
1231 for_each_possible_cpu(i
) {
1232 struct vmap_block_queue
*vbq
;
1233 struct vfree_deferred
*p
;
1235 vbq
= &per_cpu(vmap_block_queue
, i
);
1236 spin_lock_init(&vbq
->lock
);
1237 INIT_LIST_HEAD(&vbq
->free
);
1238 p
= &per_cpu(vfree_deferred
, i
);
1239 init_llist_head(&p
->list
);
1240 INIT_WORK(&p
->wq
, free_work
);
1243 /* Import existing vmlist entries. */
1244 for (tmp
= vmlist
; tmp
; tmp
= tmp
->next
) {
1245 va
= kzalloc(sizeof(struct vmap_area
), GFP_NOWAIT
);
1246 va
->flags
= VM_VM_AREA
;
1247 va
->va_start
= (unsigned long)tmp
->addr
;
1248 va
->va_end
= va
->va_start
+ tmp
->size
;
1250 __insert_vmap_area(va
);
1253 vmap_area_pcpu_hole
= VMALLOC_END
;
1255 vmap_initialized
= true;
1259 * map_kernel_range_noflush - map kernel VM area with the specified pages
1260 * @addr: start of the VM area to map
1261 * @size: size of the VM area to map
1262 * @prot: page protection flags to use
1263 * @pages: pages to map
1265 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1266 * specify should have been allocated using get_vm_area() and its
1270 * This function does NOT do any cache flushing. The caller is
1271 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1272 * before calling this function.
1275 * The number of pages mapped on success, -errno on failure.
1277 int map_kernel_range_noflush(unsigned long addr
, unsigned long size
,
1278 pgprot_t prot
, struct page
**pages
)
1280 return vmap_page_range_noflush(addr
, addr
+ size
, prot
, pages
);
1284 * unmap_kernel_range_noflush - unmap kernel VM area
1285 * @addr: start of the VM area to unmap
1286 * @size: size of the VM area to unmap
1288 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1289 * specify should have been allocated using get_vm_area() and its
1293 * This function does NOT do any cache flushing. The caller is
1294 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1295 * before calling this function and flush_tlb_kernel_range() after.
1297 void unmap_kernel_range_noflush(unsigned long addr
, unsigned long size
)
1299 vunmap_page_range(addr
, addr
+ size
);
1301 EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush
);
1304 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1305 * @addr: start of the VM area to unmap
1306 * @size: size of the VM area to unmap
1308 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1309 * the unmapping and tlb after.
1311 void unmap_kernel_range(unsigned long addr
, unsigned long size
)
1313 unsigned long end
= addr
+ size
;
1315 flush_cache_vunmap(addr
, end
);
1316 vunmap_page_range(addr
, end
);
1317 flush_tlb_kernel_range(addr
, end
);
1320 int map_vm_area(struct vm_struct
*area
, pgprot_t prot
, struct page
***pages
)
1322 unsigned long addr
= (unsigned long)area
->addr
;
1323 unsigned long end
= addr
+ get_vm_area_size(area
);
1326 err
= vmap_page_range(addr
, end
, prot
, *pages
);
1334 EXPORT_SYMBOL_GPL(map_vm_area
);
1336 static void setup_vmalloc_vm(struct vm_struct
*vm
, struct vmap_area
*va
,
1337 unsigned long flags
, const void *caller
)
1339 spin_lock(&vmap_area_lock
);
1341 vm
->addr
= (void *)va
->va_start
;
1342 vm
->size
= va
->va_end
- va
->va_start
;
1343 vm
->caller
= caller
;
1345 va
->flags
|= VM_VM_AREA
;
1346 spin_unlock(&vmap_area_lock
);
1349 static void clear_vm_uninitialized_flag(struct vm_struct
*vm
)
1352 * Before removing VM_UNINITIALIZED,
1353 * we should make sure that vm has proper values.
1354 * Pair with smp_rmb() in show_numa_info().
1357 vm
->flags
&= ~VM_UNINITIALIZED
;
1360 static struct vm_struct
*__get_vm_area_node(unsigned long size
,
1361 unsigned long align
, unsigned long flags
, unsigned long start
,
1362 unsigned long end
, int node
, gfp_t gfp_mask
, const void *caller
)
1364 struct vmap_area
*va
;
1365 struct vm_struct
*area
;
1367 BUG_ON(in_interrupt());
1368 if (flags
& VM_IOREMAP
)
1369 align
= 1ul << clamp(fls(size
), PAGE_SHIFT
, IOREMAP_MAX_ORDER
);
1371 size
= PAGE_ALIGN(size
);
1372 if (unlikely(!size
))
1375 area
= kzalloc_node(sizeof(*area
), gfp_mask
& GFP_RECLAIM_MASK
, node
);
1376 if (unlikely(!area
))
1380 * We always allocate a guard page.
1384 va
= alloc_vmap_area(size
, align
, start
, end
, node
, gfp_mask
);
1390 setup_vmalloc_vm(area
, va
, flags
, caller
);
1395 struct vm_struct
*__get_vm_area(unsigned long size
, unsigned long flags
,
1396 unsigned long start
, unsigned long end
)
1398 return __get_vm_area_node(size
, 1, flags
, start
, end
, NUMA_NO_NODE
,
1399 GFP_KERNEL
, __builtin_return_address(0));
1401 EXPORT_SYMBOL_GPL(__get_vm_area
);
1403 struct vm_struct
*__get_vm_area_caller(unsigned long size
, unsigned long flags
,
1404 unsigned long start
, unsigned long end
,
1407 return __get_vm_area_node(size
, 1, flags
, start
, end
, NUMA_NO_NODE
,
1408 GFP_KERNEL
, caller
);
1412 * get_vm_area - reserve a contiguous kernel virtual area
1413 * @size: size of the area
1414 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1416 * Search an area of @size in the kernel virtual mapping area,
1417 * and reserved it for out purposes. Returns the area descriptor
1418 * on success or %NULL on failure.
1420 struct vm_struct
*get_vm_area(unsigned long size
, unsigned long flags
)
1422 return __get_vm_area_node(size
, 1, flags
, VMALLOC_START
, VMALLOC_END
,
1423 NUMA_NO_NODE
, GFP_KERNEL
,
1424 __builtin_return_address(0));
1427 struct vm_struct
*get_vm_area_caller(unsigned long size
, unsigned long flags
,
1430 return __get_vm_area_node(size
, 1, flags
, VMALLOC_START
, VMALLOC_END
,
1431 NUMA_NO_NODE
, GFP_KERNEL
, caller
);
1435 * find_vm_area - find a continuous kernel virtual area
1436 * @addr: base address
1438 * Search for the kernel VM area starting at @addr, and return it.
1439 * It is up to the caller to do all required locking to keep the returned
1442 struct vm_struct
*find_vm_area(const void *addr
)
1444 struct vmap_area
*va
;
1446 va
= find_vmap_area((unsigned long)addr
);
1447 if (va
&& va
->flags
& VM_VM_AREA
)
1454 * remove_vm_area - find and remove a continuous kernel virtual area
1455 * @addr: base address
1457 * Search for the kernel VM area starting at @addr, and remove it.
1458 * This function returns the found VM area, but using it is NOT safe
1459 * on SMP machines, except for its size or flags.
1461 struct vm_struct
*remove_vm_area(const void *addr
)
1463 struct vmap_area
*va
;
1465 va
= find_vmap_area((unsigned long)addr
);
1466 if (va
&& va
->flags
& VM_VM_AREA
) {
1467 struct vm_struct
*vm
= va
->vm
;
1469 spin_lock(&vmap_area_lock
);
1471 va
->flags
&= ~VM_VM_AREA
;
1472 spin_unlock(&vmap_area_lock
);
1474 vmap_debug_free_range(va
->va_start
, va
->va_end
);
1475 free_unmap_vmap_area(va
);
1476 vm
->size
-= PAGE_SIZE
;
1483 static void __vunmap(const void *addr
, int deallocate_pages
)
1485 struct vm_struct
*area
;
1490 if (WARN(!PAGE_ALIGNED(addr
), "Trying to vfree() bad address (%p)\n",
1494 area
= remove_vm_area(addr
);
1495 if (unlikely(!area
)) {
1496 WARN(1, KERN_ERR
"Trying to vfree() nonexistent vm area (%p)\n",
1501 debug_check_no_locks_freed(addr
, area
->size
);
1502 debug_check_no_obj_freed(addr
, area
->size
);
1504 if (deallocate_pages
) {
1507 for (i
= 0; i
< area
->nr_pages
; i
++) {
1508 struct page
*page
= area
->pages
[i
];
1514 if (area
->flags
& VM_VPAGES
)
1525 * vfree - release memory allocated by vmalloc()
1526 * @addr: memory base address
1528 * Free the virtually continuous memory area starting at @addr, as
1529 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1530 * NULL, no operation is performed.
1532 * Must not be called in NMI context (strictly speaking, only if we don't
1533 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
1534 * conventions for vfree() arch-depenedent would be a really bad idea)
1536 * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
1538 void vfree(const void *addr
)
1542 kmemleak_free(addr
);
1546 if (unlikely(in_interrupt())) {
1547 struct vfree_deferred
*p
= &__get_cpu_var(vfree_deferred
);
1548 if (llist_add((struct llist_node
*)addr
, &p
->list
))
1549 schedule_work(&p
->wq
);
1553 EXPORT_SYMBOL(vfree
);
1556 * vunmap - release virtual mapping obtained by vmap()
1557 * @addr: memory base address
1559 * Free the virtually contiguous memory area starting at @addr,
1560 * which was created from the page array passed to vmap().
1562 * Must not be called in interrupt context.
1564 void vunmap(const void *addr
)
1566 BUG_ON(in_interrupt());
1571 EXPORT_SYMBOL(vunmap
);
1574 * vmap - map an array of pages into virtually contiguous space
1575 * @pages: array of page pointers
1576 * @count: number of pages to map
1577 * @flags: vm_area->flags
1578 * @prot: page protection for the mapping
1580 * Maps @count pages from @pages into contiguous kernel virtual
1583 void *vmap(struct page
**pages
, unsigned int count
,
1584 unsigned long flags
, pgprot_t prot
)
1586 struct vm_struct
*area
;
1590 if (count
> totalram_pages
)
1593 area
= get_vm_area_caller((count
<< PAGE_SHIFT
), flags
,
1594 __builtin_return_address(0));
1598 if (map_vm_area(area
, prot
, &pages
)) {
1605 EXPORT_SYMBOL(vmap
);
1607 static void *__vmalloc_node(unsigned long size
, unsigned long align
,
1608 gfp_t gfp_mask
, pgprot_t prot
,
1609 int node
, const void *caller
);
1610 static void *__vmalloc_area_node(struct vm_struct
*area
, gfp_t gfp_mask
,
1611 pgprot_t prot
, int node
)
1613 const int order
= 0;
1614 struct page
**pages
;
1615 unsigned int nr_pages
, array_size
, i
;
1616 gfp_t nested_gfp
= (gfp_mask
& GFP_RECLAIM_MASK
) | __GFP_ZERO
;
1618 nr_pages
= get_vm_area_size(area
) >> PAGE_SHIFT
;
1619 array_size
= (nr_pages
* sizeof(struct page
*));
1621 area
->nr_pages
= nr_pages
;
1622 /* Please note that the recursion is strictly bounded. */
1623 if (array_size
> PAGE_SIZE
) {
1624 pages
= __vmalloc_node(array_size
, 1, nested_gfp
|__GFP_HIGHMEM
,
1625 PAGE_KERNEL
, node
, area
->caller
);
1626 area
->flags
|= VM_VPAGES
;
1628 pages
= kmalloc_node(array_size
, nested_gfp
, node
);
1630 area
->pages
= pages
;
1632 remove_vm_area(area
->addr
);
1637 for (i
= 0; i
< area
->nr_pages
; i
++) {
1639 gfp_t tmp_mask
= gfp_mask
| __GFP_NOWARN
;
1641 if (node
== NUMA_NO_NODE
)
1642 page
= alloc_page(tmp_mask
);
1644 page
= alloc_pages_node(node
, tmp_mask
, order
);
1646 if (unlikely(!page
)) {
1647 /* Successfully allocated i pages, free them in __vunmap() */
1651 area
->pages
[i
] = page
;
1654 if (map_vm_area(area
, prot
, &pages
))
1659 warn_alloc_failed(gfp_mask
, order
,
1660 "vmalloc: allocation failure, allocated %ld of %ld bytes\n",
1661 (area
->nr_pages
*PAGE_SIZE
), area
->size
);
1667 * __vmalloc_node_range - allocate virtually contiguous memory
1668 * @size: allocation size
1669 * @align: desired alignment
1670 * @start: vm area range start
1671 * @end: vm area range end
1672 * @gfp_mask: flags for the page level allocator
1673 * @prot: protection mask for the allocated pages
1674 * @node: node to use for allocation or NUMA_NO_NODE
1675 * @caller: caller's return address
1677 * Allocate enough pages to cover @size from the page level
1678 * allocator with @gfp_mask flags. Map them into contiguous
1679 * kernel virtual space, using a pagetable protection of @prot.
1681 void *__vmalloc_node_range(unsigned long size
, unsigned long align
,
1682 unsigned long start
, unsigned long end
, gfp_t gfp_mask
,
1683 pgprot_t prot
, int node
, const void *caller
)
1685 struct vm_struct
*area
;
1687 unsigned long real_size
= size
;
1689 size
= PAGE_ALIGN(size
);
1690 if (!size
|| (size
>> PAGE_SHIFT
) > totalram_pages
)
1693 area
= __get_vm_area_node(size
, align
, VM_ALLOC
| VM_UNINITIALIZED
,
1694 start
, end
, node
, gfp_mask
, caller
);
1698 addr
= __vmalloc_area_node(area
, gfp_mask
, prot
, node
);
1703 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
1704 * flag. It means that vm_struct is not fully initialized.
1705 * Now, it is fully initialized, so remove this flag here.
1707 clear_vm_uninitialized_flag(area
);
1710 * A ref_count = 2 is needed because vm_struct allocated in
1711 * __get_vm_area_node() contains a reference to the virtual address of
1712 * the vmalloc'ed block.
1714 kmemleak_alloc(addr
, real_size
, 2, gfp_mask
);
1719 warn_alloc_failed(gfp_mask
, 0,
1720 "vmalloc: allocation failure: %lu bytes\n",
1726 * __vmalloc_node - allocate virtually contiguous memory
1727 * @size: allocation size
1728 * @align: desired alignment
1729 * @gfp_mask: flags for the page level allocator
1730 * @prot: protection mask for the allocated pages
1731 * @node: node to use for allocation or NUMA_NO_NODE
1732 * @caller: caller's return address
1734 * Allocate enough pages to cover @size from the page level
1735 * allocator with @gfp_mask flags. Map them into contiguous
1736 * kernel virtual space, using a pagetable protection of @prot.
1738 static void *__vmalloc_node(unsigned long size
, unsigned long align
,
1739 gfp_t gfp_mask
, pgprot_t prot
,
1740 int node
, const void *caller
)
1742 return __vmalloc_node_range(size
, align
, VMALLOC_START
, VMALLOC_END
,
1743 gfp_mask
, prot
, node
, caller
);
1746 void *__vmalloc(unsigned long size
, gfp_t gfp_mask
, pgprot_t prot
)
1748 return __vmalloc_node(size
, 1, gfp_mask
, prot
, NUMA_NO_NODE
,
1749 __builtin_return_address(0));
1751 EXPORT_SYMBOL(__vmalloc
);
1753 static inline void *__vmalloc_node_flags(unsigned long size
,
1754 int node
, gfp_t flags
)
1756 return __vmalloc_node(size
, 1, flags
, PAGE_KERNEL
,
1757 node
, __builtin_return_address(0));
1761 * vmalloc - allocate virtually contiguous memory
1762 * @size: allocation size
1763 * Allocate enough pages to cover @size from the page level
1764 * allocator and map them into contiguous kernel virtual space.
1766 * For tight control over page level allocator and protection flags
1767 * use __vmalloc() instead.
1769 void *vmalloc(unsigned long size
)
1771 return __vmalloc_node_flags(size
, NUMA_NO_NODE
,
1772 GFP_KERNEL
| __GFP_HIGHMEM
);
1774 EXPORT_SYMBOL(vmalloc
);
1777 * vzalloc - allocate virtually contiguous memory with zero fill
1778 * @size: allocation size
1779 * Allocate enough pages to cover @size from the page level
1780 * allocator and map them into contiguous kernel virtual space.
1781 * The memory allocated is set to zero.
1783 * For tight control over page level allocator and protection flags
1784 * use __vmalloc() instead.
1786 void *vzalloc(unsigned long size
)
1788 return __vmalloc_node_flags(size
, NUMA_NO_NODE
,
1789 GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
);
1791 EXPORT_SYMBOL(vzalloc
);
1794 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1795 * @size: allocation size
1797 * The resulting memory area is zeroed so it can be mapped to userspace
1798 * without leaking data.
1800 void *vmalloc_user(unsigned long size
)
1802 struct vm_struct
*area
;
1805 ret
= __vmalloc_node(size
, SHMLBA
,
1806 GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
1807 PAGE_KERNEL
, NUMA_NO_NODE
,
1808 __builtin_return_address(0));
1810 area
= find_vm_area(ret
);
1811 area
->flags
|= VM_USERMAP
;
1815 EXPORT_SYMBOL(vmalloc_user
);
1818 * vmalloc_node - allocate memory on a specific node
1819 * @size: allocation size
1822 * Allocate enough pages to cover @size from the page level
1823 * allocator and map them into contiguous kernel virtual space.
1825 * For tight control over page level allocator and protection flags
1826 * use __vmalloc() instead.
1828 void *vmalloc_node(unsigned long size
, int node
)
1830 return __vmalloc_node(size
, 1, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
,
1831 node
, __builtin_return_address(0));
1833 EXPORT_SYMBOL(vmalloc_node
);
1836 * vzalloc_node - allocate memory on a specific node with zero fill
1837 * @size: allocation size
1840 * Allocate enough pages to cover @size from the page level
1841 * allocator and map them into contiguous kernel virtual space.
1842 * The memory allocated is set to zero.
1844 * For tight control over page level allocator and protection flags
1845 * use __vmalloc_node() instead.
1847 void *vzalloc_node(unsigned long size
, int node
)
1849 return __vmalloc_node_flags(size
, node
,
1850 GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
);
1852 EXPORT_SYMBOL(vzalloc_node
);
1854 #ifndef PAGE_KERNEL_EXEC
1855 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1859 * vmalloc_exec - allocate virtually contiguous, executable memory
1860 * @size: allocation size
1862 * Kernel-internal function to allocate enough pages to cover @size
1863 * the page level allocator and map them into contiguous and
1864 * executable kernel virtual space.
1866 * For tight control over page level allocator and protection flags
1867 * use __vmalloc() instead.
1870 void *vmalloc_exec(unsigned long size
)
1872 return __vmalloc_node(size
, 1, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL_EXEC
,
1873 NUMA_NO_NODE
, __builtin_return_address(0));
1876 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1877 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1878 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1879 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1881 #define GFP_VMALLOC32 GFP_KERNEL
1885 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1886 * @size: allocation size
1888 * Allocate enough 32bit PA addressable pages to cover @size from the
1889 * page level allocator and map them into contiguous kernel virtual space.
1891 void *vmalloc_32(unsigned long size
)
1893 return __vmalloc_node(size
, 1, GFP_VMALLOC32
, PAGE_KERNEL
,
1894 NUMA_NO_NODE
, __builtin_return_address(0));
1896 EXPORT_SYMBOL(vmalloc_32
);
1899 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1900 * @size: allocation size
1902 * The resulting memory area is 32bit addressable and zeroed so it can be
1903 * mapped to userspace without leaking data.
1905 void *vmalloc_32_user(unsigned long size
)
1907 struct vm_struct
*area
;
1910 ret
= __vmalloc_node(size
, 1, GFP_VMALLOC32
| __GFP_ZERO
, PAGE_KERNEL
,
1911 NUMA_NO_NODE
, __builtin_return_address(0));
1913 area
= find_vm_area(ret
);
1914 area
->flags
|= VM_USERMAP
;
1918 EXPORT_SYMBOL(vmalloc_32_user
);
1921 * small helper routine , copy contents to buf from addr.
1922 * If the page is not present, fill zero.
1925 static int aligned_vread(char *buf
, char *addr
, unsigned long count
)
1931 unsigned long offset
, length
;
1933 offset
= (unsigned long)addr
& ~PAGE_MASK
;
1934 length
= PAGE_SIZE
- offset
;
1937 p
= vmalloc_to_page(addr
);
1939 * To do safe access to this _mapped_ area, we need
1940 * lock. But adding lock here means that we need to add
1941 * overhead of vmalloc()/vfree() calles for this _debug_
1942 * interface, rarely used. Instead of that, we'll use
1943 * kmap() and get small overhead in this access function.
1947 * we can expect USER0 is not used (see vread/vwrite's
1948 * function description)
1950 void *map
= kmap_atomic(p
);
1951 memcpy(buf
, map
+ offset
, length
);
1954 memset(buf
, 0, length
);
1964 static int aligned_vwrite(char *buf
, char *addr
, unsigned long count
)
1970 unsigned long offset
, length
;
1972 offset
= (unsigned long)addr
& ~PAGE_MASK
;
1973 length
= PAGE_SIZE
- offset
;
1976 p
= vmalloc_to_page(addr
);
1978 * To do safe access to this _mapped_ area, we need
1979 * lock. But adding lock here means that we need to add
1980 * overhead of vmalloc()/vfree() calles for this _debug_
1981 * interface, rarely used. Instead of that, we'll use
1982 * kmap() and get small overhead in this access function.
1986 * we can expect USER0 is not used (see vread/vwrite's
1987 * function description)
1989 void *map
= kmap_atomic(p
);
1990 memcpy(map
+ offset
, buf
, length
);
2002 * vread() - read vmalloc area in a safe way.
2003 * @buf: buffer for reading data
2004 * @addr: vm address.
2005 * @count: number of bytes to be read.
2007 * Returns # of bytes which addr and buf should be increased.
2008 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2009 * includes any intersect with alive vmalloc area.
2011 * This function checks that addr is a valid vmalloc'ed area, and
2012 * copy data from that area to a given buffer. If the given memory range
2013 * of [addr...addr+count) includes some valid address, data is copied to
2014 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2015 * IOREMAP area is treated as memory hole and no copy is done.
2017 * If [addr...addr+count) doesn't includes any intersects with alive
2018 * vm_struct area, returns 0. @buf should be kernel's buffer.
2020 * Note: In usual ops, vread() is never necessary because the caller
2021 * should know vmalloc() area is valid and can use memcpy().
2022 * This is for routines which have to access vmalloc area without
2023 * any informaion, as /dev/kmem.
2027 long vread(char *buf
, char *addr
, unsigned long count
)
2029 struct vmap_area
*va
;
2030 struct vm_struct
*vm
;
2031 char *vaddr
, *buf_start
= buf
;
2032 unsigned long buflen
= count
;
2035 /* Don't allow overflow */
2036 if ((unsigned long) addr
+ count
< count
)
2037 count
= -(unsigned long) addr
;
2039 spin_lock(&vmap_area_lock
);
2040 list_for_each_entry(va
, &vmap_area_list
, list
) {
2044 if (!(va
->flags
& VM_VM_AREA
))
2048 vaddr
= (char *) vm
->addr
;
2049 if (addr
>= vaddr
+ get_vm_area_size(vm
))
2051 while (addr
< vaddr
) {
2059 n
= vaddr
+ get_vm_area_size(vm
) - addr
;
2062 if (!(vm
->flags
& VM_IOREMAP
))
2063 aligned_vread(buf
, addr
, n
);
2064 else /* IOREMAP area is treated as memory hole */
2071 spin_unlock(&vmap_area_lock
);
2073 if (buf
== buf_start
)
2075 /* zero-fill memory holes */
2076 if (buf
!= buf_start
+ buflen
)
2077 memset(buf
, 0, buflen
- (buf
- buf_start
));
2083 * vwrite() - write vmalloc area in a safe way.
2084 * @buf: buffer for source data
2085 * @addr: vm address.
2086 * @count: number of bytes to be read.
2088 * Returns # of bytes which addr and buf should be incresed.
2089 * (same number to @count).
2090 * If [addr...addr+count) doesn't includes any intersect with valid
2091 * vmalloc area, returns 0.
2093 * This function checks that addr is a valid vmalloc'ed area, and
2094 * copy data from a buffer to the given addr. If specified range of
2095 * [addr...addr+count) includes some valid address, data is copied from
2096 * proper area of @buf. If there are memory holes, no copy to hole.
2097 * IOREMAP area is treated as memory hole and no copy is done.
2099 * If [addr...addr+count) doesn't includes any intersects with alive
2100 * vm_struct area, returns 0. @buf should be kernel's buffer.
2102 * Note: In usual ops, vwrite() is never necessary because the caller
2103 * should know vmalloc() area is valid and can use memcpy().
2104 * This is for routines which have to access vmalloc area without
2105 * any informaion, as /dev/kmem.
2108 long vwrite(char *buf
, char *addr
, unsigned long count
)
2110 struct vmap_area
*va
;
2111 struct vm_struct
*vm
;
2113 unsigned long n
, buflen
;
2116 /* Don't allow overflow */
2117 if ((unsigned long) addr
+ count
< count
)
2118 count
= -(unsigned long) addr
;
2121 spin_lock(&vmap_area_lock
);
2122 list_for_each_entry(va
, &vmap_area_list
, list
) {
2126 if (!(va
->flags
& VM_VM_AREA
))
2130 vaddr
= (char *) vm
->addr
;
2131 if (addr
>= vaddr
+ get_vm_area_size(vm
))
2133 while (addr
< vaddr
) {
2140 n
= vaddr
+ get_vm_area_size(vm
) - addr
;
2143 if (!(vm
->flags
& VM_IOREMAP
)) {
2144 aligned_vwrite(buf
, addr
, n
);
2152 spin_unlock(&vmap_area_lock
);
2159 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2160 * @vma: vma to cover
2161 * @uaddr: target user address to start at
2162 * @kaddr: virtual address of vmalloc kernel memory
2163 * @size: size of map area
2165 * Returns: 0 for success, -Exxx on failure
2167 * This function checks that @kaddr is a valid vmalloc'ed area,
2168 * and that it is big enough to cover the range starting at
2169 * @uaddr in @vma. Will return failure if that criteria isn't
2172 * Similar to remap_pfn_range() (see mm/memory.c)
2174 int remap_vmalloc_range_partial(struct vm_area_struct
*vma
, unsigned long uaddr
,
2175 void *kaddr
, unsigned long size
)
2177 struct vm_struct
*area
;
2179 size
= PAGE_ALIGN(size
);
2181 if (!PAGE_ALIGNED(uaddr
) || !PAGE_ALIGNED(kaddr
))
2184 area
= find_vm_area(kaddr
);
2188 if (!(area
->flags
& VM_USERMAP
))
2191 if (kaddr
+ size
> area
->addr
+ area
->size
)
2195 struct page
*page
= vmalloc_to_page(kaddr
);
2198 ret
= vm_insert_page(vma
, uaddr
, page
);
2207 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
2211 EXPORT_SYMBOL(remap_vmalloc_range_partial
);
2214 * remap_vmalloc_range - map vmalloc pages to userspace
2215 * @vma: vma to cover (map full range of vma)
2216 * @addr: vmalloc memory
2217 * @pgoff: number of pages into addr before first page to map
2219 * Returns: 0 for success, -Exxx on failure
2221 * This function checks that addr is a valid vmalloc'ed area, and
2222 * that it is big enough to cover the vma. Will return failure if
2223 * that criteria isn't met.
2225 * Similar to remap_pfn_range() (see mm/memory.c)
2227 int remap_vmalloc_range(struct vm_area_struct
*vma
, void *addr
,
2228 unsigned long pgoff
)
2230 return remap_vmalloc_range_partial(vma
, vma
->vm_start
,
2231 addr
+ (pgoff
<< PAGE_SHIFT
),
2232 vma
->vm_end
- vma
->vm_start
);
2234 EXPORT_SYMBOL(remap_vmalloc_range
);
2237 * Implement a stub for vmalloc_sync_all() if the architecture chose not to
2240 void __attribute__((weak
)) vmalloc_sync_all(void)
2245 static int f(pte_t
*pte
, pgtable_t table
, unsigned long addr
, void *data
)
2257 * alloc_vm_area - allocate a range of kernel address space
2258 * @size: size of the area
2259 * @ptes: returns the PTEs for the address space
2261 * Returns: NULL on failure, vm_struct on success
2263 * This function reserves a range of kernel address space, and
2264 * allocates pagetables to map that range. No actual mappings
2267 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2268 * allocated for the VM area are returned.
2270 struct vm_struct
*alloc_vm_area(size_t size
, pte_t
**ptes
)
2272 struct vm_struct
*area
;
2274 area
= get_vm_area_caller(size
, VM_IOREMAP
,
2275 __builtin_return_address(0));
2280 * This ensures that page tables are constructed for this region
2281 * of kernel virtual address space and mapped into init_mm.
2283 if (apply_to_page_range(&init_mm
, (unsigned long)area
->addr
,
2284 size
, f
, ptes
? &ptes
: NULL
)) {
2291 EXPORT_SYMBOL_GPL(alloc_vm_area
);
2293 void free_vm_area(struct vm_struct
*area
)
2295 struct vm_struct
*ret
;
2296 ret
= remove_vm_area(area
->addr
);
2297 BUG_ON(ret
!= area
);
2300 EXPORT_SYMBOL_GPL(free_vm_area
);
2303 static struct vmap_area
*node_to_va(struct rb_node
*n
)
2305 return n
? rb_entry(n
, struct vmap_area
, rb_node
) : NULL
;
2309 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2310 * @end: target address
2311 * @pnext: out arg for the next vmap_area
2312 * @pprev: out arg for the previous vmap_area
2314 * Returns: %true if either or both of next and prev are found,
2315 * %false if no vmap_area exists
2317 * Find vmap_areas end addresses of which enclose @end. ie. if not
2318 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2320 static bool pvm_find_next_prev(unsigned long end
,
2321 struct vmap_area
**pnext
,
2322 struct vmap_area
**pprev
)
2324 struct rb_node
*n
= vmap_area_root
.rb_node
;
2325 struct vmap_area
*va
= NULL
;
2328 va
= rb_entry(n
, struct vmap_area
, rb_node
);
2329 if (end
< va
->va_end
)
2331 else if (end
> va
->va_end
)
2340 if (va
->va_end
> end
) {
2342 *pprev
= node_to_va(rb_prev(&(*pnext
)->rb_node
));
2345 *pnext
= node_to_va(rb_next(&(*pprev
)->rb_node
));
2351 * pvm_determine_end - find the highest aligned address between two vmap_areas
2352 * @pnext: in/out arg for the next vmap_area
2353 * @pprev: in/out arg for the previous vmap_area
2356 * Returns: determined end address
2358 * Find the highest aligned address between *@pnext and *@pprev below
2359 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2360 * down address is between the end addresses of the two vmap_areas.
2362 * Please note that the address returned by this function may fall
2363 * inside *@pnext vmap_area. The caller is responsible for checking
2366 static unsigned long pvm_determine_end(struct vmap_area
**pnext
,
2367 struct vmap_area
**pprev
,
2368 unsigned long align
)
2370 const unsigned long vmalloc_end
= VMALLOC_END
& ~(align
- 1);
2374 addr
= min((*pnext
)->va_start
& ~(align
- 1), vmalloc_end
);
2378 while (*pprev
&& (*pprev
)->va_end
> addr
) {
2380 *pprev
= node_to_va(rb_prev(&(*pnext
)->rb_node
));
2387 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2388 * @offsets: array containing offset of each area
2389 * @sizes: array containing size of each area
2390 * @nr_vms: the number of areas to allocate
2391 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2393 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2394 * vm_structs on success, %NULL on failure
2396 * Percpu allocator wants to use congruent vm areas so that it can
2397 * maintain the offsets among percpu areas. This function allocates
2398 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2399 * be scattered pretty far, distance between two areas easily going up
2400 * to gigabytes. To avoid interacting with regular vmallocs, these
2401 * areas are allocated from top.
2403 * Despite its complicated look, this allocator is rather simple. It
2404 * does everything top-down and scans areas from the end looking for
2405 * matching slot. While scanning, if any of the areas overlaps with
2406 * existing vmap_area, the base address is pulled down to fit the
2407 * area. Scanning is repeated till all the areas fit and then all
2408 * necessary data structres are inserted and the result is returned.
2410 struct vm_struct
**pcpu_get_vm_areas(const unsigned long *offsets
,
2411 const size_t *sizes
, int nr_vms
,
2414 const unsigned long vmalloc_start
= ALIGN(VMALLOC_START
, align
);
2415 const unsigned long vmalloc_end
= VMALLOC_END
& ~(align
- 1);
2416 struct vmap_area
**vas
, *prev
, *next
;
2417 struct vm_struct
**vms
;
2418 int area
, area2
, last_area
, term_area
;
2419 unsigned long base
, start
, end
, last_end
;
2420 bool purged
= false;
2422 /* verify parameters and allocate data structures */
2423 BUG_ON(align
& ~PAGE_MASK
|| !is_power_of_2(align
));
2424 for (last_area
= 0, area
= 0; area
< nr_vms
; area
++) {
2425 start
= offsets
[area
];
2426 end
= start
+ sizes
[area
];
2428 /* is everything aligned properly? */
2429 BUG_ON(!IS_ALIGNED(offsets
[area
], align
));
2430 BUG_ON(!IS_ALIGNED(sizes
[area
], align
));
2432 /* detect the area with the highest address */
2433 if (start
> offsets
[last_area
])
2436 for (area2
= 0; area2
< nr_vms
; area2
++) {
2437 unsigned long start2
= offsets
[area2
];
2438 unsigned long end2
= start2
+ sizes
[area2
];
2443 BUG_ON(start2
>= start
&& start2
< end
);
2444 BUG_ON(end2
<= end
&& end2
> start
);
2447 last_end
= offsets
[last_area
] + sizes
[last_area
];
2449 if (vmalloc_end
- vmalloc_start
< last_end
) {
2454 vms
= kcalloc(nr_vms
, sizeof(vms
[0]), GFP_KERNEL
);
2455 vas
= kcalloc(nr_vms
, sizeof(vas
[0]), GFP_KERNEL
);
2459 for (area
= 0; area
< nr_vms
; area
++) {
2460 vas
[area
] = kzalloc(sizeof(struct vmap_area
), GFP_KERNEL
);
2461 vms
[area
] = kzalloc(sizeof(struct vm_struct
), GFP_KERNEL
);
2462 if (!vas
[area
] || !vms
[area
])
2466 spin_lock(&vmap_area_lock
);
2468 /* start scanning - we scan from the top, begin with the last area */
2469 area
= term_area
= last_area
;
2470 start
= offsets
[area
];
2471 end
= start
+ sizes
[area
];
2473 if (!pvm_find_next_prev(vmap_area_pcpu_hole
, &next
, &prev
)) {
2474 base
= vmalloc_end
- last_end
;
2477 base
= pvm_determine_end(&next
, &prev
, align
) - end
;
2480 BUG_ON(next
&& next
->va_end
<= base
+ end
);
2481 BUG_ON(prev
&& prev
->va_end
> base
+ end
);
2484 * base might have underflowed, add last_end before
2487 if (base
+ last_end
< vmalloc_start
+ last_end
) {
2488 spin_unlock(&vmap_area_lock
);
2490 purge_vmap_area_lazy();
2498 * If next overlaps, move base downwards so that it's
2499 * right below next and then recheck.
2501 if (next
&& next
->va_start
< base
+ end
) {
2502 base
= pvm_determine_end(&next
, &prev
, align
) - end
;
2508 * If prev overlaps, shift down next and prev and move
2509 * base so that it's right below new next and then
2512 if (prev
&& prev
->va_end
> base
+ start
) {
2514 prev
= node_to_va(rb_prev(&next
->rb_node
));
2515 base
= pvm_determine_end(&next
, &prev
, align
) - end
;
2521 * This area fits, move on to the previous one. If
2522 * the previous one is the terminal one, we're done.
2524 area
= (area
+ nr_vms
- 1) % nr_vms
;
2525 if (area
== term_area
)
2527 start
= offsets
[area
];
2528 end
= start
+ sizes
[area
];
2529 pvm_find_next_prev(base
+ end
, &next
, &prev
);
2532 /* we've found a fitting base, insert all va's */
2533 for (area
= 0; area
< nr_vms
; area
++) {
2534 struct vmap_area
*va
= vas
[area
];
2536 va
->va_start
= base
+ offsets
[area
];
2537 va
->va_end
= va
->va_start
+ sizes
[area
];
2538 __insert_vmap_area(va
);
2541 vmap_area_pcpu_hole
= base
+ offsets
[last_area
];
2543 spin_unlock(&vmap_area_lock
);
2545 /* insert all vm's */
2546 for (area
= 0; area
< nr_vms
; area
++)
2547 setup_vmalloc_vm(vms
[area
], vas
[area
], VM_ALLOC
,
2554 for (area
= 0; area
< nr_vms
; area
++) {
2565 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2566 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2567 * @nr_vms: the number of allocated areas
2569 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2571 void pcpu_free_vm_areas(struct vm_struct
**vms
, int nr_vms
)
2575 for (i
= 0; i
< nr_vms
; i
++)
2576 free_vm_area(vms
[i
]);
2579 #endif /* CONFIG_SMP */
2581 #ifdef CONFIG_PROC_FS
2582 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
2583 __acquires(&vmap_area_lock
)
2586 struct vmap_area
*va
;
2588 spin_lock(&vmap_area_lock
);
2589 va
= list_entry((&vmap_area_list
)->next
, typeof(*va
), list
);
2590 while (n
> 0 && &va
->list
!= &vmap_area_list
) {
2592 va
= list_entry(va
->list
.next
, typeof(*va
), list
);
2594 if (!n
&& &va
->list
!= &vmap_area_list
)
2601 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2603 struct vmap_area
*va
= p
, *next
;
2606 next
= list_entry(va
->list
.next
, typeof(*va
), list
);
2607 if (&next
->list
!= &vmap_area_list
)
2613 static void s_stop(struct seq_file
*m
, void *p
)
2614 __releases(&vmap_area_lock
)
2616 spin_unlock(&vmap_area_lock
);
2619 static void show_numa_info(struct seq_file
*m
, struct vm_struct
*v
)
2621 if (IS_ENABLED(CONFIG_NUMA
)) {
2622 unsigned int nr
, *counters
= m
->private;
2627 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
2629 if (v
->flags
& VM_UNINITIALIZED
)
2632 memset(counters
, 0, nr_node_ids
* sizeof(unsigned int));
2634 for (nr
= 0; nr
< v
->nr_pages
; nr
++)
2635 counters
[page_to_nid(v
->pages
[nr
])]++;
2637 for_each_node_state(nr
, N_HIGH_MEMORY
)
2639 seq_printf(m
, " N%u=%u", nr
, counters
[nr
]);
2643 static int s_show(struct seq_file
*m
, void *p
)
2645 struct vmap_area
*va
= p
;
2646 struct vm_struct
*v
;
2649 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
2650 * behalf of vmap area is being tear down or vm_map_ram allocation.
2652 if (!(va
->flags
& VM_VM_AREA
))
2657 seq_printf(m
, "0x%pK-0x%pK %7ld",
2658 v
->addr
, v
->addr
+ v
->size
, v
->size
);
2661 seq_printf(m
, " %pS", v
->caller
);
2664 seq_printf(m
, " pages=%d", v
->nr_pages
);
2667 seq_printf(m
, " phys=%llx", (unsigned long long)v
->phys_addr
);
2669 if (v
->flags
& VM_IOREMAP
)
2670 seq_printf(m
, " ioremap");
2672 if (v
->flags
& VM_ALLOC
)
2673 seq_printf(m
, " vmalloc");
2675 if (v
->flags
& VM_MAP
)
2676 seq_printf(m
, " vmap");
2678 if (v
->flags
& VM_USERMAP
)
2679 seq_printf(m
, " user");
2681 if (v
->flags
& VM_VPAGES
)
2682 seq_printf(m
, " vpages");
2684 show_numa_info(m
, v
);
2689 static const struct seq_operations vmalloc_op
= {
2696 static int vmalloc_open(struct inode
*inode
, struct file
*file
)
2698 unsigned int *ptr
= NULL
;
2701 if (IS_ENABLED(CONFIG_NUMA
)) {
2702 ptr
= kmalloc(nr_node_ids
* sizeof(unsigned int), GFP_KERNEL
);
2706 ret
= seq_open(file
, &vmalloc_op
);
2708 struct seq_file
*m
= file
->private_data
;
2715 static const struct file_operations proc_vmalloc_operations
= {
2716 .open
= vmalloc_open
,
2718 .llseek
= seq_lseek
,
2719 .release
= seq_release_private
,
2722 static int __init
proc_vmalloc_init(void)
2724 proc_create("vmallocinfo", S_IRUSR
, NULL
, &proc_vmalloc_operations
);
2727 module_init(proc_vmalloc_init
);
2729 void get_vmalloc_info(struct vmalloc_info
*vmi
)
2731 struct vmap_area
*va
;
2732 unsigned long free_area_size
;
2733 unsigned long prev_end
;
2736 vmi
->largest_chunk
= 0;
2738 prev_end
= VMALLOC_START
;
2742 if (list_empty(&vmap_area_list
)) {
2743 vmi
->largest_chunk
= VMALLOC_TOTAL
;
2747 list_for_each_entry_rcu(va
, &vmap_area_list
, list
) {
2748 unsigned long addr
= va
->va_start
;
2751 * Some archs keep another range for modules in vmalloc space
2753 if (addr
< VMALLOC_START
)
2755 if (addr
>= VMALLOC_END
)
2758 if (va
->flags
& (VM_LAZY_FREE
| VM_LAZY_FREEING
))
2761 vmi
->used
+= (va
->va_end
- va
->va_start
);
2763 free_area_size
= addr
- prev_end
;
2764 if (vmi
->largest_chunk
< free_area_size
)
2765 vmi
->largest_chunk
= free_area_size
;
2767 prev_end
= va
->va_end
;
2770 if (VMALLOC_END
- prev_end
> vmi
->largest_chunk
)
2771 vmi
->largest_chunk
= VMALLOC_END
- prev_end
;