spi: bcm2835: Fix controller unregister order
[linux/fpc-iii.git] / mm / vmalloc.c
blob5d11aeceb7f852f03053fce054430e0d503e5516
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/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/notifier.h>
25 #include <linux/rbtree.h>
26 #include <linux/radix-tree.h>
27 #include <linux/rcupdate.h>
28 #include <linux/pfn.h>
29 #include <linux/kmemleak.h>
30 #include <linux/atomic.h>
31 #include <linux/compiler.h>
32 #include <linux/llist.h>
33 #include <linux/bitops.h>
34 #include <linux/overflow.h>
36 #include <asm/uaccess.h>
37 #include <asm/tlbflush.h>
38 #include <asm/shmparam.h>
40 #include "internal.h"
42 struct vfree_deferred {
43 struct llist_head list;
44 struct work_struct wq;
46 static DEFINE_PER_CPU(struct vfree_deferred, vfree_deferred);
48 static void __vunmap(const void *, int);
50 static void free_work(struct work_struct *w)
52 struct vfree_deferred *p = container_of(w, struct vfree_deferred, wq);
53 struct llist_node *llnode = llist_del_all(&p->list);
54 while (llnode) {
55 void *p = llnode;
56 llnode = llist_next(llnode);
57 __vunmap(p, 1);
61 /*** Page table manipulation functions ***/
63 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
65 pte_t *pte;
67 pte = pte_offset_kernel(pmd, addr);
68 do {
69 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
70 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
71 } while (pte++, addr += PAGE_SIZE, addr != end);
74 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
76 pmd_t *pmd;
77 unsigned long next;
79 pmd = pmd_offset(pud, addr);
80 do {
81 next = pmd_addr_end(addr, end);
82 if (pmd_clear_huge(pmd))
83 continue;
84 if (pmd_none_or_clear_bad(pmd))
85 continue;
86 vunmap_pte_range(pmd, addr, next);
87 } while (pmd++, addr = next, addr != end);
90 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
92 pud_t *pud;
93 unsigned long next;
95 pud = pud_offset(pgd, addr);
96 do {
97 next = pud_addr_end(addr, end);
98 if (pud_clear_huge(pud))
99 continue;
100 if (pud_none_or_clear_bad(pud))
101 continue;
102 vunmap_pmd_range(pud, addr, next);
103 } while (pud++, addr = next, addr != end);
106 static void vunmap_page_range(unsigned long addr, unsigned long end)
108 pgd_t *pgd;
109 unsigned long next;
111 BUG_ON(addr >= end);
112 pgd = pgd_offset_k(addr);
113 do {
114 next = pgd_addr_end(addr, end);
115 if (pgd_none_or_clear_bad(pgd))
116 continue;
117 vunmap_pud_range(pgd, addr, next);
118 } while (pgd++, addr = next, addr != end);
121 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
122 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
124 pte_t *pte;
127 * nr is a running index into the array which helps higher level
128 * callers keep track of where we're up to.
131 pte = pte_alloc_kernel(pmd, addr);
132 if (!pte)
133 return -ENOMEM;
134 do {
135 struct page *page = pages[*nr];
137 if (WARN_ON(!pte_none(*pte)))
138 return -EBUSY;
139 if (WARN_ON(!page))
140 return -ENOMEM;
141 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
142 (*nr)++;
143 } while (pte++, addr += PAGE_SIZE, addr != end);
144 return 0;
147 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
148 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
150 pmd_t *pmd;
151 unsigned long next;
153 pmd = pmd_alloc(&init_mm, pud, addr);
154 if (!pmd)
155 return -ENOMEM;
156 do {
157 next = pmd_addr_end(addr, end);
158 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
159 return -ENOMEM;
160 } while (pmd++, addr = next, addr != end);
161 return 0;
164 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
165 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
167 pud_t *pud;
168 unsigned long next;
170 pud = pud_alloc(&init_mm, pgd, addr);
171 if (!pud)
172 return -ENOMEM;
173 do {
174 next = pud_addr_end(addr, end);
175 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
176 return -ENOMEM;
177 } while (pud++, addr = next, addr != end);
178 return 0;
182 * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
183 * will have pfns corresponding to the "pages" array.
185 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
187 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
188 pgprot_t prot, struct page **pages)
190 pgd_t *pgd;
191 unsigned long next;
192 unsigned long addr = start;
193 int err = 0;
194 int nr = 0;
196 BUG_ON(addr >= end);
197 pgd = pgd_offset_k(addr);
198 do {
199 next = pgd_addr_end(addr, end);
200 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
201 if (err)
202 return err;
203 } while (pgd++, addr = next, addr != end);
205 return nr;
208 static int vmap_page_range(unsigned long start, unsigned long end,
209 pgprot_t prot, struct page **pages)
211 int ret;
213 ret = vmap_page_range_noflush(start, end, prot, pages);
214 flush_cache_vmap(start, end);
215 return ret;
218 int is_vmalloc_or_module_addr(const void *x)
221 * ARM, x86-64 and sparc64 put modules in a special place,
222 * and fall back on vmalloc() if that fails. Others
223 * just put it in the vmalloc space.
225 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
226 unsigned long addr = (unsigned long)x;
227 if (addr >= MODULES_VADDR && addr < MODULES_END)
228 return 1;
229 #endif
230 return is_vmalloc_addr(x);
234 * Walk a vmap address to the struct page it maps.
236 struct page *vmalloc_to_page(const void *vmalloc_addr)
238 unsigned long addr = (unsigned long) vmalloc_addr;
239 struct page *page = NULL;
240 pgd_t *pgd = pgd_offset_k(addr);
243 * XXX we might need to change this if we add VIRTUAL_BUG_ON for
244 * architectures that do not vmalloc module space
246 VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
249 * Don't dereference bad PUD or PMD (below) entries. This will also
250 * identify huge mappings, which we may encounter on architectures
251 * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be
252 * identified as vmalloc addresses by is_vmalloc_addr(), but are
253 * not [unambiguously] associated with a struct page, so there is
254 * no correct value to return for them.
256 if (!pgd_none(*pgd)) {
257 pud_t *pud = pud_offset(pgd, addr);
258 WARN_ON_ONCE(pud_bad(*pud));
259 if (!pud_none(*pud) && !pud_bad(*pud)) {
260 pmd_t *pmd = pmd_offset(pud, addr);
261 WARN_ON_ONCE(pmd_bad(*pmd));
262 if (!pmd_none(*pmd) && !pmd_bad(*pmd)) {
263 pte_t *ptep, pte;
265 ptep = pte_offset_map(pmd, addr);
266 pte = *ptep;
267 if (pte_present(pte))
268 page = pte_page(pte);
269 pte_unmap(ptep);
273 return page;
275 EXPORT_SYMBOL(vmalloc_to_page);
278 * Map a vmalloc()-space virtual address to the physical page frame number.
280 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
282 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
284 EXPORT_SYMBOL(vmalloc_to_pfn);
287 /*** Global kva allocator ***/
289 #define VM_VM_AREA 0x04
291 static DEFINE_SPINLOCK(vmap_area_lock);
292 /* Export for kexec only */
293 LIST_HEAD(vmap_area_list);
294 static LLIST_HEAD(vmap_purge_list);
295 static struct rb_root vmap_area_root = RB_ROOT;
297 /* The vmap cache globals are protected by vmap_area_lock */
298 static struct rb_node *free_vmap_cache;
299 static unsigned long cached_hole_size;
300 static unsigned long cached_vstart;
301 static unsigned long cached_align;
303 static unsigned long vmap_area_pcpu_hole;
305 static struct vmap_area *__find_vmap_area(unsigned long addr)
307 struct rb_node *n = vmap_area_root.rb_node;
309 while (n) {
310 struct vmap_area *va;
312 va = rb_entry(n, struct vmap_area, rb_node);
313 if (addr < va->va_start)
314 n = n->rb_left;
315 else if (addr >= va->va_end)
316 n = n->rb_right;
317 else
318 return va;
321 return NULL;
324 static void __insert_vmap_area(struct vmap_area *va)
326 struct rb_node **p = &vmap_area_root.rb_node;
327 struct rb_node *parent = NULL;
328 struct rb_node *tmp;
330 while (*p) {
331 struct vmap_area *tmp_va;
333 parent = *p;
334 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
335 if (va->va_start < tmp_va->va_end)
336 p = &(*p)->rb_left;
337 else if (va->va_end > tmp_va->va_start)
338 p = &(*p)->rb_right;
339 else
340 BUG();
343 rb_link_node(&va->rb_node, parent, p);
344 rb_insert_color(&va->rb_node, &vmap_area_root);
346 /* address-sort this list */
347 tmp = rb_prev(&va->rb_node);
348 if (tmp) {
349 struct vmap_area *prev;
350 prev = rb_entry(tmp, struct vmap_area, rb_node);
351 list_add_rcu(&va->list, &prev->list);
352 } else
353 list_add_rcu(&va->list, &vmap_area_list);
356 static void purge_vmap_area_lazy(void);
358 static BLOCKING_NOTIFIER_HEAD(vmap_notify_list);
361 * Allocate a region of KVA of the specified size and alignment, within the
362 * vstart and vend.
364 static struct vmap_area *alloc_vmap_area(unsigned long size,
365 unsigned long align,
366 unsigned long vstart, unsigned long vend,
367 int node, gfp_t gfp_mask)
369 struct vmap_area *va;
370 struct rb_node *n;
371 unsigned long addr;
372 int purged = 0;
373 struct vmap_area *first;
375 BUG_ON(!size);
376 BUG_ON(offset_in_page(size));
377 BUG_ON(!is_power_of_2(align));
379 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
381 va = kmalloc_node(sizeof(struct vmap_area),
382 gfp_mask & GFP_RECLAIM_MASK, node);
383 if (unlikely(!va))
384 return ERR_PTR(-ENOMEM);
387 * Only scan the relevant parts containing pointers to other objects
388 * to avoid false negatives.
390 kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK);
392 retry:
393 spin_lock(&vmap_area_lock);
395 * Invalidate cache if we have more permissive parameters.
396 * cached_hole_size notes the largest hole noticed _below_
397 * the vmap_area cached in free_vmap_cache: if size fits
398 * into that hole, we want to scan from vstart to reuse
399 * the hole instead of allocating above free_vmap_cache.
400 * Note that __free_vmap_area may update free_vmap_cache
401 * without updating cached_hole_size or cached_align.
403 if (!free_vmap_cache ||
404 size < cached_hole_size ||
405 vstart < cached_vstart ||
406 align < cached_align) {
407 nocache:
408 cached_hole_size = 0;
409 free_vmap_cache = NULL;
411 /* record if we encounter less permissive parameters */
412 cached_vstart = vstart;
413 cached_align = align;
415 /* find starting point for our search */
416 if (free_vmap_cache) {
417 first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
418 addr = ALIGN(first->va_end, align);
419 if (addr < vstart)
420 goto nocache;
421 if (addr + size < addr)
422 goto overflow;
424 } else {
425 addr = ALIGN(vstart, align);
426 if (addr + size < addr)
427 goto overflow;
429 n = vmap_area_root.rb_node;
430 first = NULL;
432 while (n) {
433 struct vmap_area *tmp;
434 tmp = rb_entry(n, struct vmap_area, rb_node);
435 if (tmp->va_end >= addr) {
436 first = tmp;
437 if (tmp->va_start <= addr)
438 break;
439 n = n->rb_left;
440 } else
441 n = n->rb_right;
444 if (!first)
445 goto found;
448 /* from the starting point, walk areas until a suitable hole is found */
449 while (addr + size > first->va_start && addr + size <= vend) {
450 if (addr + cached_hole_size < first->va_start)
451 cached_hole_size = first->va_start - addr;
452 addr = ALIGN(first->va_end, align);
453 if (addr + size < addr)
454 goto overflow;
456 if (list_is_last(&first->list, &vmap_area_list))
457 goto found;
459 first = list_next_entry(first, list);
462 found:
464 * Check also calculated address against the vstart,
465 * because it can be 0 because of big align request.
467 if (addr + size > vend || addr < vstart)
468 goto overflow;
470 va->va_start = addr;
471 va->va_end = addr + size;
472 va->flags = 0;
473 __insert_vmap_area(va);
474 free_vmap_cache = &va->rb_node;
475 spin_unlock(&vmap_area_lock);
477 BUG_ON(!IS_ALIGNED(va->va_start, align));
478 BUG_ON(va->va_start < vstart);
479 BUG_ON(va->va_end > vend);
481 return va;
483 overflow:
484 spin_unlock(&vmap_area_lock);
485 if (!purged) {
486 purge_vmap_area_lazy();
487 purged = 1;
488 goto retry;
491 if (gfpflags_allow_blocking(gfp_mask)) {
492 unsigned long freed = 0;
493 blocking_notifier_call_chain(&vmap_notify_list, 0, &freed);
494 if (freed > 0) {
495 purged = 0;
496 goto retry;
500 if (printk_ratelimit())
501 pr_warn("vmap allocation for size %lu failed: use vmalloc=<size> to increase size\n",
502 size);
503 kfree(va);
504 return ERR_PTR(-EBUSY);
507 int register_vmap_purge_notifier(struct notifier_block *nb)
509 return blocking_notifier_chain_register(&vmap_notify_list, nb);
511 EXPORT_SYMBOL_GPL(register_vmap_purge_notifier);
513 int unregister_vmap_purge_notifier(struct notifier_block *nb)
515 return blocking_notifier_chain_unregister(&vmap_notify_list, nb);
517 EXPORT_SYMBOL_GPL(unregister_vmap_purge_notifier);
519 static void __free_vmap_area(struct vmap_area *va)
521 BUG_ON(RB_EMPTY_NODE(&va->rb_node));
523 if (free_vmap_cache) {
524 if (va->va_end < cached_vstart) {
525 free_vmap_cache = NULL;
526 } else {
527 struct vmap_area *cache;
528 cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
529 if (va->va_start <= cache->va_start) {
530 free_vmap_cache = rb_prev(&va->rb_node);
532 * We don't try to update cached_hole_size or
533 * cached_align, but it won't go very wrong.
538 rb_erase(&va->rb_node, &vmap_area_root);
539 RB_CLEAR_NODE(&va->rb_node);
540 list_del_rcu(&va->list);
543 * Track the highest possible candidate for pcpu area
544 * allocation. Areas outside of vmalloc area can be returned
545 * here too, consider only end addresses which fall inside
546 * vmalloc area proper.
548 if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
549 vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
551 kfree_rcu(va, rcu_head);
555 * Free a region of KVA allocated by alloc_vmap_area
557 static void free_vmap_area(struct vmap_area *va)
559 spin_lock(&vmap_area_lock);
560 __free_vmap_area(va);
561 spin_unlock(&vmap_area_lock);
565 * Clear the pagetable entries of a given vmap_area
567 static void unmap_vmap_area(struct vmap_area *va)
569 vunmap_page_range(va->va_start, va->va_end);
572 static void vmap_debug_free_range(unsigned long start, unsigned long end)
575 * Unmap page tables and force a TLB flush immediately if pagealloc
576 * debugging is enabled. This catches use after free bugs similarly to
577 * those in linear kernel virtual address space after a page has been
578 * freed.
580 * All the lazy freeing logic is still retained, in order to minimise
581 * intrusiveness of this debugging feature.
583 * This is going to be *slow* (linear kernel virtual address debugging
584 * doesn't do a broadcast TLB flush so it is a lot faster).
586 if (debug_pagealloc_enabled()) {
587 vunmap_page_range(start, end);
588 flush_tlb_kernel_range(start, end);
593 * lazy_max_pages is the maximum amount of virtual address space we gather up
594 * before attempting to purge with a TLB flush.
596 * There is a tradeoff here: a larger number will cover more kernel page tables
597 * and take slightly longer to purge, but it will linearly reduce the number of
598 * global TLB flushes that must be performed. It would seem natural to scale
599 * this number up linearly with the number of CPUs (because vmapping activity
600 * could also scale linearly with the number of CPUs), however it is likely
601 * that in practice, workloads might be constrained in other ways that mean
602 * vmap activity will not scale linearly with CPUs. Also, I want to be
603 * conservative and not introduce a big latency on huge systems, so go with
604 * a less aggressive log scale. It will still be an improvement over the old
605 * code, and it will be simple to change the scale factor if we find that it
606 * becomes a problem on bigger systems.
608 static unsigned long lazy_max_pages(void)
610 unsigned int log;
612 log = fls(num_online_cpus());
614 return log * (32UL * 1024 * 1024 / PAGE_SIZE);
617 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
619 /* for per-CPU blocks */
620 static void purge_fragmented_blocks_allcpus(void);
623 * called before a call to iounmap() if the caller wants vm_area_struct's
624 * immediately freed.
626 void set_iounmap_nonlazy(void)
628 atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
632 * Purges all lazily-freed vmap areas.
634 * If sync is 0 then don't purge if there is already a purge in progress.
635 * If force_flush is 1, then flush kernel TLBs between *start and *end even
636 * if we found no lazy vmap areas to unmap (callers can use this to optimise
637 * their own TLB flushing).
638 * Returns with *start = min(*start, lowest purged address)
639 * *end = max(*end, highest purged address)
641 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
642 int sync, int force_flush)
644 static DEFINE_SPINLOCK(purge_lock);
645 struct llist_node *valist;
646 struct vmap_area *va;
647 struct vmap_area *n_va;
648 int nr = 0;
651 * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
652 * should not expect such behaviour. This just simplifies locking for
653 * the case that isn't actually used at the moment anyway.
655 if (!sync && !force_flush) {
656 if (!spin_trylock(&purge_lock))
657 return;
658 } else
659 spin_lock(&purge_lock);
661 if (sync)
662 purge_fragmented_blocks_allcpus();
664 valist = llist_del_all(&vmap_purge_list);
665 llist_for_each_entry(va, valist, purge_list) {
666 if (va->va_start < *start)
667 *start = va->va_start;
668 if (va->va_end > *end)
669 *end = va->va_end;
670 nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
673 if (nr)
674 atomic_sub(nr, &vmap_lazy_nr);
676 if (nr || force_flush)
677 flush_tlb_kernel_range(*start, *end);
679 if (nr) {
680 spin_lock(&vmap_area_lock);
681 llist_for_each_entry_safe(va, n_va, valist, purge_list)
682 __free_vmap_area(va);
683 spin_unlock(&vmap_area_lock);
685 spin_unlock(&purge_lock);
689 * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
690 * is already purging.
692 static void try_purge_vmap_area_lazy(void)
694 unsigned long start = ULONG_MAX, end = 0;
696 __purge_vmap_area_lazy(&start, &end, 0, 0);
700 * Kick off a purge of the outstanding lazy areas.
702 static void purge_vmap_area_lazy(void)
704 unsigned long start = ULONG_MAX, end = 0;
706 __purge_vmap_area_lazy(&start, &end, 1, 0);
710 * Free a vmap area, caller ensuring that the area has been unmapped
711 * and flush_cache_vunmap had been called for the correct range
712 * previously.
714 static void free_vmap_area_noflush(struct vmap_area *va)
716 int nr_lazy;
718 nr_lazy = atomic_add_return((va->va_end - va->va_start) >> PAGE_SHIFT,
719 &vmap_lazy_nr);
721 /* After this point, we may free va at any time */
722 llist_add(&va->purge_list, &vmap_purge_list);
724 if (unlikely(nr_lazy > lazy_max_pages()))
725 try_purge_vmap_area_lazy();
729 * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
730 * called for the correct range previously.
732 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
734 unmap_vmap_area(va);
735 free_vmap_area_noflush(va);
739 * Free and unmap a vmap area
741 static void free_unmap_vmap_area(struct vmap_area *va)
743 flush_cache_vunmap(va->va_start, va->va_end);
744 free_unmap_vmap_area_noflush(va);
747 static struct vmap_area *find_vmap_area(unsigned long addr)
749 struct vmap_area *va;
751 spin_lock(&vmap_area_lock);
752 va = __find_vmap_area(addr);
753 spin_unlock(&vmap_area_lock);
755 return va;
758 static void free_unmap_vmap_area_addr(unsigned long addr)
760 struct vmap_area *va;
762 va = find_vmap_area(addr);
763 BUG_ON(!va);
764 free_unmap_vmap_area(va);
768 /*** Per cpu kva allocator ***/
771 * vmap space is limited especially on 32 bit architectures. Ensure there is
772 * room for at least 16 percpu vmap blocks per CPU.
775 * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
776 * to #define VMALLOC_SPACE (VMALLOC_END-VMALLOC_START). Guess
777 * instead (we just need a rough idea)
779 #if BITS_PER_LONG == 32
780 #define VMALLOC_SPACE (128UL*1024*1024)
781 #else
782 #define VMALLOC_SPACE (128UL*1024*1024*1024)
783 #endif
785 #define VMALLOC_PAGES (VMALLOC_SPACE / PAGE_SIZE)
786 #define VMAP_MAX_ALLOC BITS_PER_LONG /* 256K with 4K pages */
787 #define VMAP_BBMAP_BITS_MAX 1024 /* 4MB with 4K pages */
788 #define VMAP_BBMAP_BITS_MIN (VMAP_MAX_ALLOC*2)
789 #define VMAP_MIN(x, y) ((x) < (y) ? (x) : (y)) /* can't use min() */
790 #define VMAP_MAX(x, y) ((x) > (y) ? (x) : (y)) /* can't use max() */
791 #define VMAP_BBMAP_BITS \
792 VMAP_MIN(VMAP_BBMAP_BITS_MAX, \
793 VMAP_MAX(VMAP_BBMAP_BITS_MIN, \
794 VMALLOC_PAGES / roundup_pow_of_two(NR_CPUS) / 16))
796 #define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)
798 static bool vmap_initialized __read_mostly = false;
800 struct vmap_block_queue {
801 spinlock_t lock;
802 struct list_head free;
805 struct vmap_block {
806 spinlock_t lock;
807 struct vmap_area *va;
808 unsigned long free, dirty;
809 unsigned long dirty_min, dirty_max; /*< dirty range */
810 struct list_head free_list;
811 struct rcu_head rcu_head;
812 struct list_head purge;
815 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
816 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
819 * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
820 * in the free path. Could get rid of this if we change the API to return a
821 * "cookie" from alloc, to be passed to free. But no big deal yet.
823 static DEFINE_SPINLOCK(vmap_block_tree_lock);
824 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
827 * We should probably have a fallback mechanism to allocate virtual memory
828 * out of partially filled vmap blocks. However vmap block sizing should be
829 * fairly reasonable according to the vmalloc size, so it shouldn't be a
830 * big problem.
833 static unsigned long addr_to_vb_idx(unsigned long addr)
835 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
836 addr /= VMAP_BLOCK_SIZE;
837 return addr;
840 static void *vmap_block_vaddr(unsigned long va_start, unsigned long pages_off)
842 unsigned long addr;
844 addr = va_start + (pages_off << PAGE_SHIFT);
845 BUG_ON(addr_to_vb_idx(addr) != addr_to_vb_idx(va_start));
846 return (void *)addr;
850 * new_vmap_block - allocates new vmap_block and occupies 2^order pages in this
851 * block. Of course pages number can't exceed VMAP_BBMAP_BITS
852 * @order: how many 2^order pages should be occupied in newly allocated block
853 * @gfp_mask: flags for the page level allocator
855 * Returns: virtual address in a newly allocated block or ERR_PTR(-errno)
857 static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
859 struct vmap_block_queue *vbq;
860 struct vmap_block *vb;
861 struct vmap_area *va;
862 unsigned long vb_idx;
863 int node, err;
864 void *vaddr;
866 node = numa_node_id();
868 vb = kmalloc_node(sizeof(struct vmap_block),
869 gfp_mask & GFP_RECLAIM_MASK, node);
870 if (unlikely(!vb))
871 return ERR_PTR(-ENOMEM);
873 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
874 VMALLOC_START, VMALLOC_END,
875 node, gfp_mask);
876 if (IS_ERR(va)) {
877 kfree(vb);
878 return ERR_CAST(va);
881 err = radix_tree_preload(gfp_mask);
882 if (unlikely(err)) {
883 kfree(vb);
884 free_vmap_area(va);
885 return ERR_PTR(err);
888 vaddr = vmap_block_vaddr(va->va_start, 0);
889 spin_lock_init(&vb->lock);
890 vb->va = va;
891 /* At least something should be left free */
892 BUG_ON(VMAP_BBMAP_BITS <= (1UL << order));
893 vb->free = VMAP_BBMAP_BITS - (1UL << order);
894 vb->dirty = 0;
895 vb->dirty_min = VMAP_BBMAP_BITS;
896 vb->dirty_max = 0;
897 INIT_LIST_HEAD(&vb->free_list);
899 vb_idx = addr_to_vb_idx(va->va_start);
900 spin_lock(&vmap_block_tree_lock);
901 err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
902 spin_unlock(&vmap_block_tree_lock);
903 BUG_ON(err);
904 radix_tree_preload_end();
906 vbq = &get_cpu_var(vmap_block_queue);
907 spin_lock(&vbq->lock);
908 list_add_tail_rcu(&vb->free_list, &vbq->free);
909 spin_unlock(&vbq->lock);
910 put_cpu_var(vmap_block_queue);
912 return vaddr;
915 static void free_vmap_block(struct vmap_block *vb)
917 struct vmap_block *tmp;
918 unsigned long vb_idx;
920 vb_idx = addr_to_vb_idx(vb->va->va_start);
921 spin_lock(&vmap_block_tree_lock);
922 tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
923 spin_unlock(&vmap_block_tree_lock);
924 BUG_ON(tmp != vb);
926 free_vmap_area_noflush(vb->va);
927 kfree_rcu(vb, rcu_head);
930 static void purge_fragmented_blocks(int cpu)
932 LIST_HEAD(purge);
933 struct vmap_block *vb;
934 struct vmap_block *n_vb;
935 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
937 rcu_read_lock();
938 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
940 if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
941 continue;
943 spin_lock(&vb->lock);
944 if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
945 vb->free = 0; /* prevent further allocs after releasing lock */
946 vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
947 vb->dirty_min = 0;
948 vb->dirty_max = VMAP_BBMAP_BITS;
949 spin_lock(&vbq->lock);
950 list_del_rcu(&vb->free_list);
951 spin_unlock(&vbq->lock);
952 spin_unlock(&vb->lock);
953 list_add_tail(&vb->purge, &purge);
954 } else
955 spin_unlock(&vb->lock);
957 rcu_read_unlock();
959 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
960 list_del(&vb->purge);
961 free_vmap_block(vb);
965 static void purge_fragmented_blocks_allcpus(void)
967 int cpu;
969 for_each_possible_cpu(cpu)
970 purge_fragmented_blocks(cpu);
973 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
975 struct vmap_block_queue *vbq;
976 struct vmap_block *vb;
977 void *vaddr = NULL;
978 unsigned int order;
980 BUG_ON(offset_in_page(size));
981 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
982 if (WARN_ON(size == 0)) {
984 * Allocating 0 bytes isn't what caller wants since
985 * get_order(0) returns funny result. Just warn and terminate
986 * early.
988 return NULL;
990 order = get_order(size);
992 rcu_read_lock();
993 vbq = &get_cpu_var(vmap_block_queue);
994 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
995 unsigned long pages_off;
997 spin_lock(&vb->lock);
998 if (vb->free < (1UL << order)) {
999 spin_unlock(&vb->lock);
1000 continue;
1003 pages_off = VMAP_BBMAP_BITS - vb->free;
1004 vaddr = vmap_block_vaddr(vb->va->va_start, pages_off);
1005 vb->free -= 1UL << order;
1006 if (vb->free == 0) {
1007 spin_lock(&vbq->lock);
1008 list_del_rcu(&vb->free_list);
1009 spin_unlock(&vbq->lock);
1012 spin_unlock(&vb->lock);
1013 break;
1016 put_cpu_var(vmap_block_queue);
1017 rcu_read_unlock();
1019 /* Allocate new block if nothing was found */
1020 if (!vaddr)
1021 vaddr = new_vmap_block(order, gfp_mask);
1023 return vaddr;
1026 static void vb_free(const void *addr, unsigned long size)
1028 unsigned long offset;
1029 unsigned long vb_idx;
1030 unsigned int order;
1031 struct vmap_block *vb;
1033 BUG_ON(offset_in_page(size));
1034 BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
1036 flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
1038 order = get_order(size);
1040 offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
1041 offset >>= PAGE_SHIFT;
1043 vb_idx = addr_to_vb_idx((unsigned long)addr);
1044 rcu_read_lock();
1045 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1046 rcu_read_unlock();
1047 BUG_ON(!vb);
1049 vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
1051 spin_lock(&vb->lock);
1053 /* Expand dirty range */
1054 vb->dirty_min = min(vb->dirty_min, offset);
1055 vb->dirty_max = max(vb->dirty_max, offset + (1UL << order));
1057 vb->dirty += 1UL << order;
1058 if (vb->dirty == VMAP_BBMAP_BITS) {
1059 BUG_ON(vb->free);
1060 spin_unlock(&vb->lock);
1061 free_vmap_block(vb);
1062 } else
1063 spin_unlock(&vb->lock);
1067 * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1069 * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1070 * to amortize TLB flushing overheads. What this means is that any page you
1071 * have now, may, in a former life, have been mapped into kernel virtual
1072 * address by the vmap layer and so there might be some CPUs with TLB entries
1073 * still referencing that page (additional to the regular 1:1 kernel mapping).
1075 * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1076 * be sure that none of the pages we have control over will have any aliases
1077 * from the vmap layer.
1079 void vm_unmap_aliases(void)
1081 unsigned long start = ULONG_MAX, end = 0;
1082 int cpu;
1083 int flush = 0;
1085 if (unlikely(!vmap_initialized))
1086 return;
1088 for_each_possible_cpu(cpu) {
1089 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1090 struct vmap_block *vb;
1092 rcu_read_lock();
1093 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1094 spin_lock(&vb->lock);
1095 if (vb->dirty) {
1096 unsigned long va_start = vb->va->va_start;
1097 unsigned long s, e;
1099 s = va_start + (vb->dirty_min << PAGE_SHIFT);
1100 e = va_start + (vb->dirty_max << PAGE_SHIFT);
1102 start = min(s, start);
1103 end = max(e, end);
1105 flush = 1;
1107 spin_unlock(&vb->lock);
1109 rcu_read_unlock();
1112 __purge_vmap_area_lazy(&start, &end, 1, flush);
1114 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
1117 * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
1118 * @mem: the pointer returned by vm_map_ram
1119 * @count: the count passed to that vm_map_ram call (cannot unmap partial)
1121 void vm_unmap_ram(const void *mem, unsigned int count)
1123 unsigned long size = (unsigned long)count << PAGE_SHIFT;
1124 unsigned long addr = (unsigned long)mem;
1126 BUG_ON(!addr);
1127 BUG_ON(addr < VMALLOC_START);
1128 BUG_ON(addr > VMALLOC_END);
1129 BUG_ON(!PAGE_ALIGNED(addr));
1131 debug_check_no_locks_freed(mem, size);
1132 vmap_debug_free_range(addr, addr+size);
1134 if (likely(count <= VMAP_MAX_ALLOC))
1135 vb_free(mem, size);
1136 else
1137 free_unmap_vmap_area_addr(addr);
1139 EXPORT_SYMBOL(vm_unmap_ram);
1142 * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
1143 * @pages: an array of pointers to the pages to be mapped
1144 * @count: number of pages
1145 * @node: prefer to allocate data structures on this node
1146 * @prot: memory protection to use. PAGE_KERNEL for regular RAM
1148 * If you use this function for less than VMAP_MAX_ALLOC pages, it could be
1149 * faster than vmap so it's good. But if you mix long-life and short-life
1150 * objects with vm_map_ram(), it could consume lots of address space through
1151 * fragmentation (especially on a 32bit machine). You could see failures in
1152 * the end. Please use this function for short-lived objects.
1154 * Returns: a pointer to the address that has been mapped, or %NULL on failure
1156 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
1158 unsigned long size = (unsigned long)count << PAGE_SHIFT;
1159 unsigned long addr;
1160 void *mem;
1162 if (likely(count <= VMAP_MAX_ALLOC)) {
1163 mem = vb_alloc(size, GFP_KERNEL);
1164 if (IS_ERR(mem))
1165 return NULL;
1166 addr = (unsigned long)mem;
1167 } else {
1168 struct vmap_area *va;
1169 va = alloc_vmap_area(size, PAGE_SIZE,
1170 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1171 if (IS_ERR(va))
1172 return NULL;
1174 addr = va->va_start;
1175 mem = (void *)addr;
1177 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1178 vm_unmap_ram(mem, count);
1179 return NULL;
1181 return mem;
1183 EXPORT_SYMBOL(vm_map_ram);
1185 static struct vm_struct *vmlist __initdata;
1187 * vm_area_add_early - add vmap area early during boot
1188 * @vm: vm_struct to add
1190 * This function is used to add fixed kernel vm area to vmlist before
1191 * vmalloc_init() is called. @vm->addr, @vm->size, and @vm->flags
1192 * should contain proper values and the other fields should be zero.
1194 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1196 void __init vm_area_add_early(struct vm_struct *vm)
1198 struct vm_struct *tmp, **p;
1200 BUG_ON(vmap_initialized);
1201 for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1202 if (tmp->addr >= vm->addr) {
1203 BUG_ON(tmp->addr < vm->addr + vm->size);
1204 break;
1205 } else
1206 BUG_ON(tmp->addr + tmp->size > vm->addr);
1208 vm->next = *p;
1209 *p = vm;
1213 * vm_area_register_early - register vmap area early during boot
1214 * @vm: vm_struct to register
1215 * @align: requested alignment
1217 * This function is used to register kernel vm area before
1218 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1219 * proper values on entry and other fields should be zero. On return,
1220 * vm->addr contains the allocated address.
1222 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1224 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1226 static size_t vm_init_off __initdata;
1227 unsigned long addr;
1229 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1230 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1232 vm->addr = (void *)addr;
1234 vm_area_add_early(vm);
1237 void __init vmalloc_init(void)
1239 struct vmap_area *va;
1240 struct vm_struct *tmp;
1241 int i;
1243 for_each_possible_cpu(i) {
1244 struct vmap_block_queue *vbq;
1245 struct vfree_deferred *p;
1247 vbq = &per_cpu(vmap_block_queue, i);
1248 spin_lock_init(&vbq->lock);
1249 INIT_LIST_HEAD(&vbq->free);
1250 p = &per_cpu(vfree_deferred, i);
1251 init_llist_head(&p->list);
1252 INIT_WORK(&p->wq, free_work);
1255 /* Import existing vmlist entries. */
1256 for (tmp = vmlist; tmp; tmp = tmp->next) {
1257 va = kzalloc(sizeof(struct vmap_area), GFP_NOWAIT);
1258 va->flags = VM_VM_AREA;
1259 va->va_start = (unsigned long)tmp->addr;
1260 va->va_end = va->va_start + tmp->size;
1261 va->vm = tmp;
1262 __insert_vmap_area(va);
1265 vmap_area_pcpu_hole = VMALLOC_END;
1267 vmap_initialized = true;
1271 * map_kernel_range_noflush - map kernel VM area with the specified pages
1272 * @addr: start of the VM area to map
1273 * @size: size of the VM area to map
1274 * @prot: page protection flags to use
1275 * @pages: pages to map
1277 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1278 * specify should have been allocated using get_vm_area() and its
1279 * friends.
1281 * NOTE:
1282 * This function does NOT do any cache flushing. The caller is
1283 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1284 * before calling this function.
1286 * RETURNS:
1287 * The number of pages mapped on success, -errno on failure.
1289 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1290 pgprot_t prot, struct page **pages)
1292 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1296 * unmap_kernel_range_noflush - unmap kernel VM area
1297 * @addr: start of the VM area to unmap
1298 * @size: size of the VM area to unmap
1300 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1301 * specify should have been allocated using get_vm_area() and its
1302 * friends.
1304 * NOTE:
1305 * This function does NOT do any cache flushing. The caller is
1306 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1307 * before calling this function and flush_tlb_kernel_range() after.
1309 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1311 vunmap_page_range(addr, addr + size);
1313 EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
1316 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1317 * @addr: start of the VM area to unmap
1318 * @size: size of the VM area to unmap
1320 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1321 * the unmapping and tlb after.
1323 void unmap_kernel_range(unsigned long addr, unsigned long size)
1325 unsigned long end = addr + size;
1327 flush_cache_vunmap(addr, end);
1328 vunmap_page_range(addr, end);
1329 flush_tlb_kernel_range(addr, end);
1331 EXPORT_SYMBOL_GPL(unmap_kernel_range);
1333 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page **pages)
1335 unsigned long addr = (unsigned long)area->addr;
1336 unsigned long end = addr + get_vm_area_size(area);
1337 int err;
1339 err = vmap_page_range(addr, end, prot, pages);
1341 return err > 0 ? 0 : err;
1343 EXPORT_SYMBOL_GPL(map_vm_area);
1345 static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
1346 unsigned long flags, const void *caller)
1348 spin_lock(&vmap_area_lock);
1349 vm->flags = flags;
1350 vm->addr = (void *)va->va_start;
1351 vm->size = va->va_end - va->va_start;
1352 vm->caller = caller;
1353 va->vm = vm;
1354 va->flags |= VM_VM_AREA;
1355 spin_unlock(&vmap_area_lock);
1358 static void clear_vm_uninitialized_flag(struct vm_struct *vm)
1361 * Before removing VM_UNINITIALIZED,
1362 * we should make sure that vm has proper values.
1363 * Pair with smp_rmb() in show_numa_info().
1365 smp_wmb();
1366 vm->flags &= ~VM_UNINITIALIZED;
1369 static struct vm_struct *__get_vm_area_node(unsigned long size,
1370 unsigned long align, unsigned long flags, unsigned long start,
1371 unsigned long end, int node, gfp_t gfp_mask, const void *caller)
1373 struct vmap_area *va;
1374 struct vm_struct *area;
1376 BUG_ON(in_interrupt());
1377 size = PAGE_ALIGN(size);
1378 if (unlikely(!size))
1379 return NULL;
1381 if (flags & VM_IOREMAP)
1382 align = 1ul << clamp_t(int, get_count_order_long(size),
1383 PAGE_SHIFT, IOREMAP_MAX_ORDER);
1385 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1386 if (unlikely(!area))
1387 return NULL;
1389 if (!(flags & VM_NO_GUARD))
1390 size += PAGE_SIZE;
1392 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1393 if (IS_ERR(va)) {
1394 kfree(area);
1395 return NULL;
1398 setup_vmalloc_vm(area, va, flags, caller);
1400 return area;
1403 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1404 unsigned long start, unsigned long end)
1406 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1407 GFP_KERNEL, __builtin_return_address(0));
1409 EXPORT_SYMBOL_GPL(__get_vm_area);
1411 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1412 unsigned long start, unsigned long end,
1413 const void *caller)
1415 return __get_vm_area_node(size, 1, flags, start, end, NUMA_NO_NODE,
1416 GFP_KERNEL, caller);
1420 * get_vm_area - reserve a contiguous kernel virtual area
1421 * @size: size of the area
1422 * @flags: %VM_IOREMAP for I/O mappings or VM_ALLOC
1424 * Search an area of @size in the kernel virtual mapping area,
1425 * and reserved it for out purposes. Returns the area descriptor
1426 * on success or %NULL on failure.
1428 struct vm_struct *get_vm_area(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,
1432 __builtin_return_address(0));
1435 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1436 const void *caller)
1438 return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
1439 NUMA_NO_NODE, GFP_KERNEL, caller);
1443 * find_vm_area - find a continuous kernel virtual area
1444 * @addr: base address
1446 * Search for the kernel VM area starting at @addr, and return it.
1447 * It is up to the caller to do all required locking to keep the returned
1448 * pointer valid.
1450 struct vm_struct *find_vm_area(const void *addr)
1452 struct vmap_area *va;
1454 va = find_vmap_area((unsigned long)addr);
1455 if (va && va->flags & VM_VM_AREA)
1456 return va->vm;
1458 return NULL;
1462 * remove_vm_area - find and remove a continuous kernel virtual area
1463 * @addr: base address
1465 * Search for the kernel VM area starting at @addr, and remove it.
1466 * This function returns the found VM area, but using it is NOT safe
1467 * on SMP machines, except for its size or flags.
1469 struct vm_struct *remove_vm_area(const void *addr)
1471 struct vmap_area *va;
1473 va = find_vmap_area((unsigned long)addr);
1474 if (va && va->flags & VM_VM_AREA) {
1475 struct vm_struct *vm = va->vm;
1477 spin_lock(&vmap_area_lock);
1478 va->vm = NULL;
1479 va->flags &= ~VM_VM_AREA;
1480 spin_unlock(&vmap_area_lock);
1482 vmap_debug_free_range(va->va_start, va->va_end);
1483 kasan_free_shadow(vm);
1484 free_unmap_vmap_area(va);
1486 return vm;
1488 return NULL;
1491 static void __vunmap(const void *addr, int deallocate_pages)
1493 struct vm_struct *area;
1495 if (!addr)
1496 return;
1498 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
1499 addr))
1500 return;
1502 area = find_vm_area(addr);
1503 if (unlikely(!area)) {
1504 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1505 addr);
1506 return;
1509 debug_check_no_locks_freed(addr, get_vm_area_size(area));
1510 debug_check_no_obj_freed(addr, get_vm_area_size(area));
1512 remove_vm_area(addr);
1513 if (deallocate_pages) {
1514 int i;
1516 for (i = 0; i < area->nr_pages; i++) {
1517 struct page *page = area->pages[i];
1519 BUG_ON(!page);
1520 __free_pages(page, 0);
1523 kvfree(area->pages);
1526 kfree(area);
1527 return;
1531 * vfree - release memory allocated by vmalloc()
1532 * @addr: memory base address
1534 * Free the virtually continuous memory area starting at @addr, as
1535 * obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1536 * NULL, no operation is performed.
1538 * Must not be called in NMI context (strictly speaking, only if we don't
1539 * have CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG, but making the calling
1540 * conventions for vfree() arch-depenedent would be a really bad idea)
1542 * NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
1544 void vfree(const void *addr)
1546 BUG_ON(in_nmi());
1548 kmemleak_free(addr);
1550 if (!addr)
1551 return;
1552 if (unlikely(in_interrupt())) {
1553 struct vfree_deferred *p = this_cpu_ptr(&vfree_deferred);
1554 if (llist_add((struct llist_node *)addr, &p->list))
1555 schedule_work(&p->wq);
1556 } else
1557 __vunmap(addr, 1);
1559 EXPORT_SYMBOL(vfree);
1562 * vunmap - release virtual mapping obtained by vmap()
1563 * @addr: memory base address
1565 * Free the virtually contiguous memory area starting at @addr,
1566 * which was created from the page array passed to vmap().
1568 * Must not be called in interrupt context.
1570 void vunmap(const void *addr)
1572 BUG_ON(in_interrupt());
1573 might_sleep();
1574 if (addr)
1575 __vunmap(addr, 0);
1577 EXPORT_SYMBOL(vunmap);
1580 * vmap - map an array of pages into virtually contiguous space
1581 * @pages: array of page pointers
1582 * @count: number of pages to map
1583 * @flags: vm_area->flags
1584 * @prot: page protection for the mapping
1586 * Maps @count pages from @pages into contiguous kernel virtual
1587 * space.
1589 void *vmap(struct page **pages, unsigned int count,
1590 unsigned long flags, pgprot_t prot)
1592 struct vm_struct *area;
1593 unsigned long size; /* In bytes */
1595 might_sleep();
1597 if (count > totalram_pages)
1598 return NULL;
1600 size = (unsigned long)count << PAGE_SHIFT;
1601 area = get_vm_area_caller(size, flags, __builtin_return_address(0));
1602 if (!area)
1603 return NULL;
1605 if (map_vm_area(area, prot, pages)) {
1606 vunmap(area->addr);
1607 return NULL;
1610 return area->addr;
1612 EXPORT_SYMBOL(vmap);
1614 static void *__vmalloc_node(unsigned long size, unsigned long align,
1615 gfp_t gfp_mask, pgprot_t prot,
1616 int node, const void *caller);
1617 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1618 pgprot_t prot, int node)
1620 struct page **pages;
1621 unsigned int nr_pages, array_size, i;
1622 const gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
1623 const gfp_t alloc_mask = gfp_mask | __GFP_NOWARN;
1625 nr_pages = get_vm_area_size(area) >> PAGE_SHIFT;
1626 array_size = (nr_pages * sizeof(struct page *));
1628 area->nr_pages = nr_pages;
1629 /* Please note that the recursion is strictly bounded. */
1630 if (array_size > PAGE_SIZE) {
1631 pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
1632 PAGE_KERNEL, node, area->caller);
1633 } else {
1634 pages = kmalloc_node(array_size, nested_gfp, node);
1636 area->pages = pages;
1637 if (!area->pages) {
1638 remove_vm_area(area->addr);
1639 kfree(area);
1640 return NULL;
1643 for (i = 0; i < area->nr_pages; i++) {
1644 struct page *page;
1646 if (node == NUMA_NO_NODE)
1647 page = alloc_page(alloc_mask);
1648 else
1649 page = alloc_pages_node(node, alloc_mask, 0);
1651 if (unlikely(!page)) {
1652 /* Successfully allocated i pages, free them in __vunmap() */
1653 area->nr_pages = i;
1654 goto fail;
1656 area->pages[i] = page;
1657 if (gfpflags_allow_blocking(gfp_mask))
1658 cond_resched();
1661 if (map_vm_area(area, prot, pages))
1662 goto fail;
1663 return area->addr;
1665 fail:
1666 warn_alloc(gfp_mask,
1667 "vmalloc: allocation failure, allocated %ld of %ld bytes",
1668 (area->nr_pages*PAGE_SIZE), area->size);
1669 vfree(area->addr);
1670 return NULL;
1674 * __vmalloc_node_range - allocate virtually contiguous memory
1675 * @size: allocation size
1676 * @align: desired alignment
1677 * @start: vm area range start
1678 * @end: vm area range end
1679 * @gfp_mask: flags for the page level allocator
1680 * @prot: protection mask for the allocated pages
1681 * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD)
1682 * @node: node to use for allocation or NUMA_NO_NODE
1683 * @caller: caller's return address
1685 * Allocate enough pages to cover @size from the page level
1686 * allocator with @gfp_mask flags. Map them into contiguous
1687 * kernel virtual space, using a pagetable protection of @prot.
1689 void *__vmalloc_node_range(unsigned long size, unsigned long align,
1690 unsigned long start, unsigned long end, gfp_t gfp_mask,
1691 pgprot_t prot, unsigned long vm_flags, int node,
1692 const void *caller)
1694 struct vm_struct *area;
1695 void *addr;
1696 unsigned long real_size = size;
1698 size = PAGE_ALIGN(size);
1699 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1700 goto fail;
1702 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED |
1703 vm_flags, start, end, node, gfp_mask, caller);
1704 if (!area)
1705 goto fail;
1707 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
1708 if (!addr)
1709 return NULL;
1712 * First make sure the mappings are removed from all page-tables
1713 * before they are freed.
1715 vmalloc_sync_unmappings();
1718 * In this function, newly allocated vm_struct has VM_UNINITIALIZED
1719 * flag. It means that vm_struct is not fully initialized.
1720 * Now, it is fully initialized, so remove this flag here.
1722 clear_vm_uninitialized_flag(area);
1725 * A ref_count = 2 is needed because vm_struct allocated in
1726 * __get_vm_area_node() contains a reference to the virtual address of
1727 * the vmalloc'ed block.
1729 kmemleak_alloc(addr, real_size, 2, gfp_mask);
1731 return addr;
1733 fail:
1734 warn_alloc(gfp_mask,
1735 "vmalloc: allocation failure: %lu bytes", real_size);
1736 return NULL;
1740 * __vmalloc_node - allocate virtually contiguous memory
1741 * @size: allocation size
1742 * @align: desired alignment
1743 * @gfp_mask: flags for the page level allocator
1744 * @prot: protection mask for the allocated pages
1745 * @node: node to use for allocation or NUMA_NO_NODE
1746 * @caller: caller's return address
1748 * Allocate enough pages to cover @size from the page level
1749 * allocator with @gfp_mask flags. Map them into contiguous
1750 * kernel virtual space, using a pagetable protection of @prot.
1752 static void *__vmalloc_node(unsigned long size, unsigned long align,
1753 gfp_t gfp_mask, pgprot_t prot,
1754 int node, const void *caller)
1756 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
1757 gfp_mask, prot, 0, node, caller);
1760 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1762 return __vmalloc_node(size, 1, gfp_mask, prot, NUMA_NO_NODE,
1763 __builtin_return_address(0));
1765 EXPORT_SYMBOL(__vmalloc);
1767 static inline void *__vmalloc_node_flags(unsigned long size,
1768 int node, gfp_t flags)
1770 return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
1771 node, __builtin_return_address(0));
1775 * vmalloc - allocate virtually contiguous memory
1776 * @size: allocation size
1777 * Allocate enough pages to cover @size from the page level
1778 * allocator and map them into contiguous kernel virtual space.
1780 * For tight control over page level allocator and protection flags
1781 * use __vmalloc() instead.
1783 void *vmalloc(unsigned long size)
1785 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1786 GFP_KERNEL | __GFP_HIGHMEM);
1788 EXPORT_SYMBOL(vmalloc);
1791 * vzalloc - allocate virtually contiguous memory with zero fill
1792 * @size: allocation size
1793 * Allocate enough pages to cover @size from the page level
1794 * allocator and map them into contiguous kernel virtual space.
1795 * The memory allocated is set to zero.
1797 * For tight control over page level allocator and protection flags
1798 * use __vmalloc() instead.
1800 void *vzalloc(unsigned long size)
1802 return __vmalloc_node_flags(size, NUMA_NO_NODE,
1803 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1805 EXPORT_SYMBOL(vzalloc);
1808 * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1809 * @size: allocation size
1811 * The resulting memory area is zeroed so it can be mapped to userspace
1812 * without leaking data.
1814 void *vmalloc_user(unsigned long size)
1816 struct vm_struct *area;
1817 void *ret;
1819 ret = __vmalloc_node(size, SHMLBA,
1820 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1821 PAGE_KERNEL, NUMA_NO_NODE,
1822 __builtin_return_address(0));
1823 if (ret) {
1824 area = find_vm_area(ret);
1825 area->flags |= VM_USERMAP;
1827 return ret;
1829 EXPORT_SYMBOL(vmalloc_user);
1832 * vmalloc_node - allocate memory on a specific node
1833 * @size: allocation size
1834 * @node: numa node
1836 * Allocate enough pages to cover @size from the page level
1837 * allocator and map them into contiguous kernel virtual space.
1839 * For tight control over page level allocator and protection flags
1840 * use __vmalloc() instead.
1842 void *vmalloc_node(unsigned long size, int node)
1844 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1845 node, __builtin_return_address(0));
1847 EXPORT_SYMBOL(vmalloc_node);
1850 * vzalloc_node - allocate memory on a specific node with zero fill
1851 * @size: allocation size
1852 * @node: numa node
1854 * Allocate enough pages to cover @size from the page level
1855 * allocator and map them into contiguous kernel virtual space.
1856 * The memory allocated is set to zero.
1858 * For tight control over page level allocator and protection flags
1859 * use __vmalloc_node() instead.
1861 void *vzalloc_node(unsigned long size, int node)
1863 return __vmalloc_node_flags(size, node,
1864 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
1866 EXPORT_SYMBOL(vzalloc_node);
1868 #ifndef PAGE_KERNEL_EXEC
1869 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1870 #endif
1873 * vmalloc_exec - allocate virtually contiguous, executable memory
1874 * @size: allocation size
1876 * Kernel-internal function to allocate enough pages to cover @size
1877 * the page level allocator and map them into contiguous and
1878 * executable kernel virtual space.
1880 * For tight control over page level allocator and protection flags
1881 * use __vmalloc() instead.
1884 void *vmalloc_exec(unsigned long size)
1886 return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1887 NUMA_NO_NODE, __builtin_return_address(0));
1890 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1891 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1892 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1893 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1894 #else
1895 #define GFP_VMALLOC32 GFP_KERNEL
1896 #endif
1899 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1900 * @size: allocation size
1902 * Allocate enough 32bit PA addressable pages to cover @size from the
1903 * page level allocator and map them into contiguous kernel virtual space.
1905 void *vmalloc_32(unsigned long size)
1907 return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
1908 NUMA_NO_NODE, __builtin_return_address(0));
1910 EXPORT_SYMBOL(vmalloc_32);
1913 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1914 * @size: allocation size
1916 * The resulting memory area is 32bit addressable and zeroed so it can be
1917 * mapped to userspace without leaking data.
1919 void *vmalloc_32_user(unsigned long size)
1921 struct vm_struct *area;
1922 void *ret;
1924 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1925 NUMA_NO_NODE, __builtin_return_address(0));
1926 if (ret) {
1927 area = find_vm_area(ret);
1928 area->flags |= VM_USERMAP;
1930 return ret;
1932 EXPORT_SYMBOL(vmalloc_32_user);
1935 * small helper routine , copy contents to buf from addr.
1936 * If the page is not present, fill zero.
1939 static int aligned_vread(char *buf, char *addr, unsigned long count)
1941 struct page *p;
1942 int copied = 0;
1944 while (count) {
1945 unsigned long offset, length;
1947 offset = offset_in_page(addr);
1948 length = PAGE_SIZE - offset;
1949 if (length > count)
1950 length = count;
1951 p = vmalloc_to_page(addr);
1953 * To do safe access to this _mapped_ area, we need
1954 * lock. But adding lock here means that we need to add
1955 * overhead of vmalloc()/vfree() calles for this _debug_
1956 * interface, rarely used. Instead of that, we'll use
1957 * kmap() and get small overhead in this access function.
1959 if (p) {
1961 * we can expect USER0 is not used (see vread/vwrite's
1962 * function description)
1964 void *map = kmap_atomic(p);
1965 memcpy(buf, map + offset, length);
1966 kunmap_atomic(map);
1967 } else
1968 memset(buf, 0, length);
1970 addr += length;
1971 buf += length;
1972 copied += length;
1973 count -= length;
1975 return copied;
1978 static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1980 struct page *p;
1981 int copied = 0;
1983 while (count) {
1984 unsigned long offset, length;
1986 offset = offset_in_page(addr);
1987 length = PAGE_SIZE - offset;
1988 if (length > count)
1989 length = count;
1990 p = vmalloc_to_page(addr);
1992 * To do safe access to this _mapped_ area, we need
1993 * lock. But adding lock here means that we need to add
1994 * overhead of vmalloc()/vfree() calles for this _debug_
1995 * interface, rarely used. Instead of that, we'll use
1996 * kmap() and get small overhead in this access function.
1998 if (p) {
2000 * we can expect USER0 is not used (see vread/vwrite's
2001 * function description)
2003 void *map = kmap_atomic(p);
2004 memcpy(map + offset, buf, length);
2005 kunmap_atomic(map);
2007 addr += length;
2008 buf += length;
2009 copied += length;
2010 count -= length;
2012 return copied;
2016 * vread() - read vmalloc area in a safe way.
2017 * @buf: buffer for reading data
2018 * @addr: vm address.
2019 * @count: number of bytes to be read.
2021 * Returns # of bytes which addr and buf should be increased.
2022 * (same number to @count). Returns 0 if [addr...addr+count) doesn't
2023 * includes any intersect with alive vmalloc area.
2025 * This function checks that addr is a valid vmalloc'ed area, and
2026 * copy data from that area to a given buffer. If the given memory range
2027 * of [addr...addr+count) includes some valid address, data is copied to
2028 * proper area of @buf. If there are memory holes, they'll be zero-filled.
2029 * IOREMAP area is treated as memory hole and no copy is done.
2031 * If [addr...addr+count) doesn't includes any intersects with alive
2032 * vm_struct area, returns 0. @buf should be kernel's buffer.
2034 * Note: In usual ops, vread() is never necessary because the caller
2035 * should know vmalloc() area is valid and can use memcpy().
2036 * This is for routines which have to access vmalloc area without
2037 * any informaion, as /dev/kmem.
2041 long vread(char *buf, char *addr, unsigned long count)
2043 struct vmap_area *va;
2044 struct vm_struct *vm;
2045 char *vaddr, *buf_start = buf;
2046 unsigned long buflen = count;
2047 unsigned long n;
2049 /* Don't allow overflow */
2050 if ((unsigned long) addr + count < count)
2051 count = -(unsigned long) addr;
2053 spin_lock(&vmap_area_lock);
2054 list_for_each_entry(va, &vmap_area_list, list) {
2055 if (!count)
2056 break;
2058 if (!(va->flags & VM_VM_AREA))
2059 continue;
2061 vm = va->vm;
2062 vaddr = (char *) vm->addr;
2063 if (addr >= vaddr + get_vm_area_size(vm))
2064 continue;
2065 while (addr < vaddr) {
2066 if (count == 0)
2067 goto finished;
2068 *buf = '\0';
2069 buf++;
2070 addr++;
2071 count--;
2073 n = vaddr + get_vm_area_size(vm) - addr;
2074 if (n > count)
2075 n = count;
2076 if (!(vm->flags & VM_IOREMAP))
2077 aligned_vread(buf, addr, n);
2078 else /* IOREMAP area is treated as memory hole */
2079 memset(buf, 0, n);
2080 buf += n;
2081 addr += n;
2082 count -= n;
2084 finished:
2085 spin_unlock(&vmap_area_lock);
2087 if (buf == buf_start)
2088 return 0;
2089 /* zero-fill memory holes */
2090 if (buf != buf_start + buflen)
2091 memset(buf, 0, buflen - (buf - buf_start));
2093 return buflen;
2097 * vwrite() - write vmalloc area in a safe way.
2098 * @buf: buffer for source data
2099 * @addr: vm address.
2100 * @count: number of bytes to be read.
2102 * Returns # of bytes which addr and buf should be incresed.
2103 * (same number to @count).
2104 * If [addr...addr+count) doesn't includes any intersect with valid
2105 * vmalloc area, returns 0.
2107 * This function checks that addr is a valid vmalloc'ed area, and
2108 * copy data from a buffer to the given addr. If specified range of
2109 * [addr...addr+count) includes some valid address, data is copied from
2110 * proper area of @buf. If there are memory holes, no copy to hole.
2111 * IOREMAP area is treated as memory hole and no copy is done.
2113 * If [addr...addr+count) doesn't includes any intersects with alive
2114 * vm_struct area, returns 0. @buf should be kernel's buffer.
2116 * Note: In usual ops, vwrite() is never necessary because the caller
2117 * should know vmalloc() area is valid and can use memcpy().
2118 * This is for routines which have to access vmalloc area without
2119 * any informaion, as /dev/kmem.
2122 long vwrite(char *buf, char *addr, unsigned long count)
2124 struct vmap_area *va;
2125 struct vm_struct *vm;
2126 char *vaddr;
2127 unsigned long n, buflen;
2128 int copied = 0;
2130 /* Don't allow overflow */
2131 if ((unsigned long) addr + count < count)
2132 count = -(unsigned long) addr;
2133 buflen = count;
2135 spin_lock(&vmap_area_lock);
2136 list_for_each_entry(va, &vmap_area_list, list) {
2137 if (!count)
2138 break;
2140 if (!(va->flags & VM_VM_AREA))
2141 continue;
2143 vm = va->vm;
2144 vaddr = (char *) vm->addr;
2145 if (addr >= vaddr + get_vm_area_size(vm))
2146 continue;
2147 while (addr < vaddr) {
2148 if (count == 0)
2149 goto finished;
2150 buf++;
2151 addr++;
2152 count--;
2154 n = vaddr + get_vm_area_size(vm) - addr;
2155 if (n > count)
2156 n = count;
2157 if (!(vm->flags & VM_IOREMAP)) {
2158 aligned_vwrite(buf, addr, n);
2159 copied++;
2161 buf += n;
2162 addr += n;
2163 count -= n;
2165 finished:
2166 spin_unlock(&vmap_area_lock);
2167 if (!copied)
2168 return 0;
2169 return buflen;
2173 * remap_vmalloc_range_partial - map vmalloc pages to userspace
2174 * @vma: vma to cover
2175 * @uaddr: target user address to start at
2176 * @kaddr: virtual address of vmalloc kernel memory
2177 * @pgoff: offset from @kaddr to start at
2178 * @size: size of map area
2180 * Returns: 0 for success, -Exxx on failure
2182 * This function checks that @kaddr is a valid vmalloc'ed area,
2183 * and that it is big enough to cover the range starting at
2184 * @uaddr in @vma. Will return failure if that criteria isn't
2185 * met.
2187 * Similar to remap_pfn_range() (see mm/memory.c)
2189 int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
2190 void *kaddr, unsigned long pgoff,
2191 unsigned long size)
2193 struct vm_struct *area;
2194 unsigned long off;
2195 unsigned long end_index;
2197 if (check_shl_overflow(pgoff, PAGE_SHIFT, &off))
2198 return -EINVAL;
2200 size = PAGE_ALIGN(size);
2202 if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
2203 return -EINVAL;
2205 area = find_vm_area(kaddr);
2206 if (!area)
2207 return -EINVAL;
2209 if (!(area->flags & VM_USERMAP))
2210 return -EINVAL;
2212 if (check_add_overflow(size, off, &end_index) ||
2213 end_index > get_vm_area_size(area))
2214 return -EINVAL;
2215 kaddr += off;
2217 do {
2218 struct page *page = vmalloc_to_page(kaddr);
2219 int ret;
2221 ret = vm_insert_page(vma, uaddr, page);
2222 if (ret)
2223 return ret;
2225 uaddr += PAGE_SIZE;
2226 kaddr += PAGE_SIZE;
2227 size -= PAGE_SIZE;
2228 } while (size > 0);
2230 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2232 return 0;
2234 EXPORT_SYMBOL(remap_vmalloc_range_partial);
2237 * remap_vmalloc_range - map vmalloc pages to userspace
2238 * @vma: vma to cover (map full range of vma)
2239 * @addr: vmalloc memory
2240 * @pgoff: number of pages into addr before first page to map
2242 * Returns: 0 for success, -Exxx on failure
2244 * This function checks that addr is a valid vmalloc'ed area, and
2245 * that it is big enough to cover the vma. Will return failure if
2246 * that criteria isn't met.
2248 * Similar to remap_pfn_range() (see mm/memory.c)
2250 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
2251 unsigned long pgoff)
2253 return remap_vmalloc_range_partial(vma, vma->vm_start,
2254 addr, pgoff,
2255 vma->vm_end - vma->vm_start);
2257 EXPORT_SYMBOL(remap_vmalloc_range);
2260 * Implement stubs for vmalloc_sync_[un]mappings () if the architecture chose
2261 * not to have one.
2263 * The purpose of this function is to make sure the vmalloc area
2264 * mappings are identical in all page-tables in the system.
2266 void __weak vmalloc_sync_mappings(void)
2270 void __weak vmalloc_sync_unmappings(void)
2274 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
2276 pte_t ***p = data;
2278 if (p) {
2279 *(*p) = pte;
2280 (*p)++;
2282 return 0;
2286 * alloc_vm_area - allocate a range of kernel address space
2287 * @size: size of the area
2288 * @ptes: returns the PTEs for the address space
2290 * Returns: NULL on failure, vm_struct on success
2292 * This function reserves a range of kernel address space, and
2293 * allocates pagetables to map that range. No actual mappings
2294 * are created.
2296 * If @ptes is non-NULL, pointers to the PTEs (in init_mm)
2297 * allocated for the VM area are returned.
2299 struct vm_struct *alloc_vm_area(size_t size, pte_t **ptes)
2301 struct vm_struct *area;
2303 area = get_vm_area_caller(size, VM_IOREMAP,
2304 __builtin_return_address(0));
2305 if (area == NULL)
2306 return NULL;
2309 * This ensures that page tables are constructed for this region
2310 * of kernel virtual address space and mapped into init_mm.
2312 if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
2313 size, f, ptes ? &ptes : NULL)) {
2314 free_vm_area(area);
2315 return NULL;
2318 return area;
2320 EXPORT_SYMBOL_GPL(alloc_vm_area);
2322 void free_vm_area(struct vm_struct *area)
2324 struct vm_struct *ret;
2325 ret = remove_vm_area(area->addr);
2326 BUG_ON(ret != area);
2327 kfree(area);
2329 EXPORT_SYMBOL_GPL(free_vm_area);
2331 #ifdef CONFIG_SMP
2332 static struct vmap_area *node_to_va(struct rb_node *n)
2334 return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
2338 * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
2339 * @end: target address
2340 * @pnext: out arg for the next vmap_area
2341 * @pprev: out arg for the previous vmap_area
2343 * Returns: %true if either or both of next and prev are found,
2344 * %false if no vmap_area exists
2346 * Find vmap_areas end addresses of which enclose @end. ie. if not
2347 * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
2349 static bool pvm_find_next_prev(unsigned long end,
2350 struct vmap_area **pnext,
2351 struct vmap_area **pprev)
2353 struct rb_node *n = vmap_area_root.rb_node;
2354 struct vmap_area *va = NULL;
2356 while (n) {
2357 va = rb_entry(n, struct vmap_area, rb_node);
2358 if (end < va->va_end)
2359 n = n->rb_left;
2360 else if (end > va->va_end)
2361 n = n->rb_right;
2362 else
2363 break;
2366 if (!va)
2367 return false;
2369 if (va->va_end > end) {
2370 *pnext = va;
2371 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2372 } else {
2373 *pprev = va;
2374 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2376 return true;
2380 * pvm_determine_end - find the highest aligned address between two vmap_areas
2381 * @pnext: in/out arg for the next vmap_area
2382 * @pprev: in/out arg for the previous vmap_area
2383 * @align: alignment
2385 * Returns: determined end address
2387 * Find the highest aligned address between *@pnext and *@pprev below
2388 * VMALLOC_END. *@pnext and *@pprev are adjusted so that the aligned
2389 * down address is between the end addresses of the two vmap_areas.
2391 * Please note that the address returned by this function may fall
2392 * inside *@pnext vmap_area. The caller is responsible for checking
2393 * that.
2395 static unsigned long pvm_determine_end(struct vmap_area **pnext,
2396 struct vmap_area **pprev,
2397 unsigned long align)
2399 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2400 unsigned long addr;
2402 if (*pnext)
2403 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2404 else
2405 addr = vmalloc_end;
2407 while (*pprev && (*pprev)->va_end > addr) {
2408 *pnext = *pprev;
2409 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2412 return addr;
2416 * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
2417 * @offsets: array containing offset of each area
2418 * @sizes: array containing size of each area
2419 * @nr_vms: the number of areas to allocate
2420 * @align: alignment, all entries in @offsets and @sizes must be aligned to this
2422 * Returns: kmalloc'd vm_struct pointer array pointing to allocated
2423 * vm_structs on success, %NULL on failure
2425 * Percpu allocator wants to use congruent vm areas so that it can
2426 * maintain the offsets among percpu areas. This function allocates
2427 * congruent vmalloc areas for it with GFP_KERNEL. These areas tend to
2428 * be scattered pretty far, distance between two areas easily going up
2429 * to gigabytes. To avoid interacting with regular vmallocs, these
2430 * areas are allocated from top.
2432 * Despite its complicated look, this allocator is rather simple. It
2433 * does everything top-down and scans areas from the end looking for
2434 * matching slot. While scanning, if any of the areas overlaps with
2435 * existing vmap_area, the base address is pulled down to fit the
2436 * area. Scanning is repeated till all the areas fit and then all
2437 * necessary data structres are inserted and the result is returned.
2439 struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
2440 const size_t *sizes, int nr_vms,
2441 size_t align)
2443 const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
2444 const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
2445 struct vmap_area **vas, *prev, *next;
2446 struct vm_struct **vms;
2447 int area, area2, last_area, term_area;
2448 unsigned long base, start, end, last_end;
2449 bool purged = false;
2451 /* verify parameters and allocate data structures */
2452 BUG_ON(offset_in_page(align) || !is_power_of_2(align));
2453 for (last_area = 0, area = 0; area < nr_vms; area++) {
2454 start = offsets[area];
2455 end = start + sizes[area];
2457 /* is everything aligned properly? */
2458 BUG_ON(!IS_ALIGNED(offsets[area], align));
2459 BUG_ON(!IS_ALIGNED(sizes[area], align));
2461 /* detect the area with the highest address */
2462 if (start > offsets[last_area])
2463 last_area = area;
2465 for (area2 = 0; area2 < nr_vms; area2++) {
2466 unsigned long start2 = offsets[area2];
2467 unsigned long end2 = start2 + sizes[area2];
2469 if (area2 == area)
2470 continue;
2472 BUG_ON(start2 >= start && start2 < end);
2473 BUG_ON(end2 <= end && end2 > start);
2476 last_end = offsets[last_area] + sizes[last_area];
2478 if (vmalloc_end - vmalloc_start < last_end) {
2479 WARN_ON(true);
2480 return NULL;
2483 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
2484 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2485 if (!vas || !vms)
2486 goto err_free2;
2488 for (area = 0; area < nr_vms; area++) {
2489 vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
2490 vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
2491 if (!vas[area] || !vms[area])
2492 goto err_free;
2494 retry:
2495 spin_lock(&vmap_area_lock);
2497 /* start scanning - we scan from the top, begin with the last area */
2498 area = term_area = last_area;
2499 start = offsets[area];
2500 end = start + sizes[area];
2502 if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
2503 base = vmalloc_end - last_end;
2504 goto found;
2506 base = pvm_determine_end(&next, &prev, align) - end;
2508 while (true) {
2509 BUG_ON(next && next->va_end <= base + end);
2510 BUG_ON(prev && prev->va_end > base + end);
2513 * base might have underflowed, add last_end before
2514 * comparing.
2516 if (base + last_end < vmalloc_start + last_end) {
2517 spin_unlock(&vmap_area_lock);
2518 if (!purged) {
2519 purge_vmap_area_lazy();
2520 purged = true;
2521 goto retry;
2523 goto err_free;
2527 * If next overlaps, move base downwards so that it's
2528 * right below next and then recheck.
2530 if (next && next->va_start < base + end) {
2531 base = pvm_determine_end(&next, &prev, align) - end;
2532 term_area = area;
2533 continue;
2537 * If prev overlaps, shift down next and prev and move
2538 * base so that it's right below new next and then
2539 * recheck.
2541 if (prev && prev->va_end > base + start) {
2542 next = prev;
2543 prev = node_to_va(rb_prev(&next->rb_node));
2544 base = pvm_determine_end(&next, &prev, align) - end;
2545 term_area = area;
2546 continue;
2550 * This area fits, move on to the previous one. If
2551 * the previous one is the terminal one, we're done.
2553 area = (area + nr_vms - 1) % nr_vms;
2554 if (area == term_area)
2555 break;
2556 start = offsets[area];
2557 end = start + sizes[area];
2558 pvm_find_next_prev(base + end, &next, &prev);
2560 found:
2561 /* we've found a fitting base, insert all va's */
2562 for (area = 0; area < nr_vms; area++) {
2563 struct vmap_area *va = vas[area];
2565 va->va_start = base + offsets[area];
2566 va->va_end = va->va_start + sizes[area];
2567 __insert_vmap_area(va);
2570 vmap_area_pcpu_hole = base + offsets[last_area];
2572 spin_unlock(&vmap_area_lock);
2574 /* insert all vm's */
2575 for (area = 0; area < nr_vms; area++)
2576 setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
2577 pcpu_get_vm_areas);
2579 kfree(vas);
2580 return vms;
2582 err_free:
2583 for (area = 0; area < nr_vms; area++) {
2584 kfree(vas[area]);
2585 kfree(vms[area]);
2587 err_free2:
2588 kfree(vas);
2589 kfree(vms);
2590 return NULL;
2594 * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
2595 * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
2596 * @nr_vms: the number of allocated areas
2598 * Free vm_structs and the array allocated by pcpu_get_vm_areas().
2600 void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
2602 int i;
2604 for (i = 0; i < nr_vms; i++)
2605 free_vm_area(vms[i]);
2606 kfree(vms);
2608 #endif /* CONFIG_SMP */
2610 #ifdef CONFIG_PROC_FS
2611 static void *s_start(struct seq_file *m, loff_t *pos)
2612 __acquires(&vmap_area_lock)
2614 loff_t n = *pos;
2615 struct vmap_area *va;
2617 spin_lock(&vmap_area_lock);
2618 va = list_first_entry(&vmap_area_list, typeof(*va), list);
2619 while (n > 0 && &va->list != &vmap_area_list) {
2620 n--;
2621 va = list_next_entry(va, list);
2623 if (!n && &va->list != &vmap_area_list)
2624 return va;
2626 return NULL;
2630 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2632 struct vmap_area *va = p, *next;
2634 ++*pos;
2635 next = list_next_entry(va, list);
2636 if (&next->list != &vmap_area_list)
2637 return next;
2639 return NULL;
2642 static void s_stop(struct seq_file *m, void *p)
2643 __releases(&vmap_area_lock)
2645 spin_unlock(&vmap_area_lock);
2648 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
2650 if (IS_ENABLED(CONFIG_NUMA)) {
2651 unsigned int nr, *counters = m->private;
2653 if (!counters)
2654 return;
2656 if (v->flags & VM_UNINITIALIZED)
2657 return;
2658 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
2659 smp_rmb();
2661 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
2663 for (nr = 0; nr < v->nr_pages; nr++)
2664 counters[page_to_nid(v->pages[nr])]++;
2666 for_each_node_state(nr, N_HIGH_MEMORY)
2667 if (counters[nr])
2668 seq_printf(m, " N%u=%u", nr, counters[nr]);
2672 static int s_show(struct seq_file *m, void *p)
2674 struct vmap_area *va = p;
2675 struct vm_struct *v;
2678 * s_show can encounter race with remove_vm_area, !VM_VM_AREA on
2679 * behalf of vmap area is being tear down or vm_map_ram allocation.
2681 if (!(va->flags & VM_VM_AREA))
2682 return 0;
2684 v = va->vm;
2686 seq_printf(m, "0x%pK-0x%pK %7ld",
2687 v->addr, v->addr + v->size, v->size);
2689 if (v->caller)
2690 seq_printf(m, " %pS", v->caller);
2692 if (v->nr_pages)
2693 seq_printf(m, " pages=%d", v->nr_pages);
2695 if (v->phys_addr)
2696 seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
2698 if (v->flags & VM_IOREMAP)
2699 seq_puts(m, " ioremap");
2701 if (v->flags & VM_ALLOC)
2702 seq_puts(m, " vmalloc");
2704 if (v->flags & VM_MAP)
2705 seq_puts(m, " vmap");
2707 if (v->flags & VM_USERMAP)
2708 seq_puts(m, " user");
2710 if (is_vmalloc_addr(v->pages))
2711 seq_puts(m, " vpages");
2713 show_numa_info(m, v);
2714 seq_putc(m, '\n');
2715 return 0;
2718 static const struct seq_operations vmalloc_op = {
2719 .start = s_start,
2720 .next = s_next,
2721 .stop = s_stop,
2722 .show = s_show,
2725 static int vmalloc_open(struct inode *inode, struct file *file)
2727 if (IS_ENABLED(CONFIG_NUMA))
2728 return seq_open_private(file, &vmalloc_op,
2729 nr_node_ids * sizeof(unsigned int));
2730 else
2731 return seq_open(file, &vmalloc_op);
2734 static const struct file_operations proc_vmalloc_operations = {
2735 .open = vmalloc_open,
2736 .read = seq_read,
2737 .llseek = seq_lseek,
2738 .release = seq_release_private,
2741 static int __init proc_vmalloc_init(void)
2743 proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
2744 return 0;
2746 module_init(proc_vmalloc_init);
2748 #endif