Lynx framebuffers multidomain implementation.
[linux/elbrus.git] / mm / vmalloc.c
blob138d9ec997e163f1e233955a68539bc9ffc8d1dd
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/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>
35 #ifdef CONFIG_E2K
36 #include <asm/pgalloc.h> /* For pmd_alloc_kernel() and friends */
37 #endif
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);
52 while (llnode) {
53 void *p = llnode;
54 llnode = llist_next(llnode);
55 __vunmap(p, 1);
59 /*** Page table manipulation functions ***/
61 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
63 pte_t *pte;
65 pte = pte_offset_kernel(pmd, addr);
66 do {
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)
74 pmd_t *pmd;
75 unsigned long next;
77 #ifdef CONFIG_E2K
78 pmd = pmd_offset_kernel(pud, addr);
79 #else
80 pmd = pmd_offset(pud, addr);
81 #endif
82 do {
83 next = pmd_addr_end(addr, end);
84 #ifdef CONFIG_E2K
85 if (pmd_none_or_clear_bad_kernel(pmd))
86 #else
87 if (pmd_none_or_clear_bad(pmd))
88 #endif
89 continue;
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)
96 pud_t *pud;
97 unsigned long next;
99 #ifdef CONFIG_E2K
100 pud = pud_offset_kernel(pgd, addr);
101 #else
102 pud = pud_offset(pgd, addr);
103 #endif
104 do {
105 next = pud_addr_end(addr, end);
106 #ifdef CONFIG_E2K
107 if (pud_none_or_clear_bad_kernel(pud))
108 #else
109 if (pud_none_or_clear_bad(pud))
110 #endif
111 continue;
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)
118 pgd_t *pgd;
119 unsigned long next;
120 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
121 unsigned long start = addr;
122 #endif
124 BUG_ON(addr >= end);
125 pgd = pgd_offset_k(addr);
126 do {
127 next = pgd_addr_end(addr, end);
128 #ifdef CONFIG_E2K
129 if (pgd_none_or_clear_bad_kernel(pgd))
130 #else
131 if (pgd_none_or_clear_bad(pgd))
132 #endif
133 continue;
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);
139 #endif
142 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
143 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
145 pte_t *pte;
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);
153 if (!pte)
154 return -ENOMEM;
155 do {
156 struct page *page = pages[*nr];
158 if (WARN_ON(!pte_none(*pte)))
159 return -EBUSY;
160 if (WARN_ON(!page))
161 return -ENOMEM;
162 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
163 (*nr)++;
164 } while (pte++, addr += PAGE_SIZE, addr != end);
165 return 0;
168 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
169 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
171 pmd_t *pmd;
172 unsigned long next;
174 #ifdef CONFIG_E2K
175 pmd = pmd_alloc_kernel(&init_mm, pud, addr);
176 #else
177 pmd = pmd_alloc(&init_mm, pud, addr);
178 #endif
179 if (!pmd)
180 return -ENOMEM;
181 do {
182 next = pmd_addr_end(addr, end);
183 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
184 return -ENOMEM;
185 } while (pmd++, addr = next, addr != end);
186 return 0;
189 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
190 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
192 pud_t *pud;
193 unsigned long next;
195 #ifdef CONFIG_E2K
196 pud = pud_alloc_kernel(&init_mm, pgd, addr);
197 #else
198 pud = pud_alloc(&init_mm, pgd, addr);
199 #endif
200 if (!pud)
201 return -ENOMEM;
202 do {
203 next = pud_addr_end(addr, end);
204 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
205 return -ENOMEM;
206 } while (pud++, addr = next, addr != end);
207 return 0;
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)
219 pgd_t *pgd;
220 unsigned long next;
221 unsigned long addr = start;
222 int err = 0;
223 int nr = 0;
224 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
225 int nid = numa_node_id();
226 #endif
228 BUG_ON(addr >= end);
229 #if defined(CONFIG_E2K) && defined(CONFIG_NUMA)
230 pgd = node_pgd_offset_kernel(nid, addr);
231 #else
232 pgd = pgd_offset_k(addr);
233 #endif
234 do {
235 next = pgd_addr_end(addr, end);
236 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
237 if (err)
238 return err;
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",
243 start, end);
245 #endif
247 return nr;
250 static int vmap_page_range(unsigned long start, unsigned long end,
251 pgprot_t prot, struct page **pages)
253 int ret;
255 ret = vmap_page_range_noflush(start, end, prot, pages);
256 flush_cache_vmap(start, end);
257 return ret;
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)
270 return 1;
271 #endif
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)) {
295 pte_t *ptep, pte;
297 ptep = pte_offset_map(pmd, addr);
298 pte = *ptep;
299 if (pte_present(pte))
300 page = pte_page(pte);
301 pte_unmap(ptep);
305 return page;
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;
342 while (n) {
343 struct vmap_area *va;
345 va = rb_entry(n, struct vmap_area, rb_node);
346 if (addr < va->va_start)
347 n = n->rb_left;
348 else if (addr >= va->va_end)
349 n = n->rb_right;
350 else
351 return va;
354 return NULL;
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;
361 struct rb_node *tmp;
363 while (*p) {
364 struct vmap_area *tmp_va;
366 parent = *p;
367 tmp_va = rb_entry(parent, struct vmap_area, rb_node);
368 if (va->va_start < tmp_va->va_end)
369 p = &(*p)->rb_left;
370 else if (va->va_end > tmp_va->va_start)
371 p = &(*p)->rb_right;
372 else
373 BUG();
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);
381 if (tmp) {
382 struct vmap_area *prev;
383 prev = rb_entry(tmp, struct vmap_area, rb_node);
384 list_add_rcu(&va->list, &prev->list);
385 } else
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
393 * vstart and vend.
395 static struct vmap_area *alloc_vmap_area(unsigned long size,
396 unsigned long align,
397 unsigned long vstart, unsigned long vend,
398 int node, gfp_t gfp_mask)
400 struct vmap_area *va;
401 struct rb_node *n;
402 unsigned long addr;
403 int purged = 0;
404 struct vmap_area *first;
406 BUG_ON(!size);
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);
412 if (unlikely(!va))
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);
421 retry:
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) {
436 nocache:
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);
448 if (addr < vstart)
449 goto nocache;
450 if (addr + size < addr)
451 goto overflow;
453 } else {
454 addr = ALIGN(vstart, align);
455 if (addr + size < addr)
456 goto overflow;
458 n = vmap_area_root.rb_node;
459 first = NULL;
461 while (n) {
462 struct vmap_area *tmp;
463 tmp = rb_entry(n, struct vmap_area, rb_node);
464 if (tmp->va_end >= addr) {
465 first = tmp;
466 if (tmp->va_start <= addr)
467 break;
468 n = n->rb_left;
469 } else
470 n = n->rb_right;
473 if (!first)
474 goto found;
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)
483 goto overflow;
485 if (list_is_last(&first->list, &vmap_area_list))
486 goto found;
488 first = list_entry(first->list.next,
489 struct vmap_area, list);
492 found:
493 if (addr + size > vend)
494 goto overflow;
496 va->va_start = addr;
497 va->va_end = addr + size;
498 va->flags = 0;
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);
507 return va;
509 overflow:
510 spin_unlock(&vmap_area_lock);
511 if (!purged) {
512 purge_vmap_area_lazy();
513 purged = 1;
514 goto retry;
516 if (printk_ratelimit())
517 printk(KERN_WARNING
518 "vmap allocation for size %lu failed: "
519 "use vmalloc=<size> to increase size.\n", size);
520 kfree(va);
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;
531 } else {
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
590 * faster).
592 #ifdef CONFIG_DEBUG_PAGEALLOC
593 vunmap_page_range(start, end);
594 flush_tlb_kernel_range(start, end);
595 #endif
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)
616 unsigned int log;
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
630 * immediately freed.
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);
651 LIST_HEAD(valist);
652 struct vmap_area *va;
653 struct vmap_area *n_va;
654 int nr = 0;
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))
663 return;
664 } else
665 spin_lock(&purge_lock);
667 if (sync)
668 purge_fragmented_blocks_allcpus();
670 rcu_read_lock();
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)
676 *end = va->va_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;
683 rcu_read_unlock();
685 if (nr)
686 atomic_sub(nr, &vmap_lazy_nr);
688 if (nr || force_flush)
689 flush_tlb_kernel_range(*start, *end);
691 if (nr) {
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
724 * previously.
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)
740 unmap_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);
761 return va;
764 static void free_unmap_vmap_area_addr(unsigned long addr)
766 struct vmap_area *va;
768 va = find_vmap_area(addr);
769 BUG_ON(!va);
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)
787 #else
788 #define VMALLOC_SPACE (128UL*1024*1024*1024)
789 #endif
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 {
807 spinlock_t lock;
808 struct list_head free;
811 struct vmap_block {
812 spinlock_t lock;
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
836 * big problem.
839 static unsigned long addr_to_vb_idx(unsigned long addr)
841 addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
842 addr /= VMAP_BLOCK_SIZE;
843 return addr;
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;
852 int node, err, cpu;
854 node = numa_node_id();
856 vb = kmalloc_node(sizeof(struct vmap_block),
857 gfp_mask & GFP_RECLAIM_MASK, node);
858 if (unlikely(!vb))
859 return ERR_PTR(-ENOMEM);
861 va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
862 VMALLOC_START, VMALLOC_END,
863 node, gfp_mask);
864 if (IS_ERR(va)) {
865 kfree(vb);
866 return ERR_CAST(va);
869 err = radix_tree_preload(gfp_mask);
870 if (unlikely(err)) {
871 kfree(vb);
872 free_vmap_area(va);
873 return ERR_PTR(err);
876 spin_lock_init(&vb->lock);
877 vb->va = va;
878 vb->free = VMAP_BBMAP_BITS;
879 vb->dirty = 0;
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);
887 BUG_ON(err);
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);
895 put_cpu_light();
897 return vb;
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);
909 BUG_ON(tmp != vb);
911 free_vmap_area_noflush(vb->va);
912 kfree_rcu(vb, rcu_head);
915 static void purge_fragmented_blocks(int cpu)
917 LIST_HEAD(purge);
918 struct vmap_block *vb;
919 struct vmap_block *n_vb;
920 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
922 rcu_read_lock();
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))
926 continue;
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);
938 } else
939 spin_unlock(&vb->lock);
941 rcu_read_unlock();
943 list_for_each_entry_safe(vb, n_vb, &purge, purge) {
944 list_del(&vb->purge);
945 free_vmap_block(vb);
949 static void purge_fragmented_blocks_allcpus(void)
951 int cpu;
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;
962 unsigned int order;
963 int cpu = 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
971 * early.
973 return NULL;
975 order = get_order(size);
977 again:
978 rcu_read_lock();
979 cpu = get_cpu_light();
980 vbq = &__get_cpu_var(vmap_block_queue);
981 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
982 int i;
984 spin_lock(&vb->lock);
985 if (vb->free < 1UL << order)
986 goto next;
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;
993 if (vb->free == 0) {
994 spin_lock(&vbq->lock);
995 list_del_rcu(&vb->free_list);
996 spin_unlock(&vbq->lock);
998 spin_unlock(&vb->lock);
999 break;
1000 next:
1001 spin_unlock(&vb->lock);
1004 put_cpu_light();
1005 rcu_read_unlock();
1007 if (!addr) {
1008 vb = new_vmap_block(gfp_mask);
1009 if (IS_ERR(vb))
1010 return vb;
1011 goto again;
1014 return (void *)addr;
1017 static void vb_free(const void *addr, unsigned long size)
1019 unsigned long offset;
1020 unsigned long vb_idx;
1021 unsigned int order;
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);
1034 rcu_read_lock();
1035 vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
1036 rcu_read_unlock();
1037 BUG_ON(!vb);
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) {
1046 BUG_ON(vb->free);
1047 spin_unlock(&vb->lock);
1048 free_vmap_block(vb);
1049 } else
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;
1069 int cpu;
1070 int flush = 0;
1072 if (unlikely(!vmap_initialized))
1073 return;
1075 for_each_possible_cpu(cpu) {
1076 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
1077 struct vmap_block *vb;
1079 rcu_read_lock();
1080 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
1081 int i, j;
1083 spin_lock(&vb->lock);
1084 i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
1085 if (i < VMAP_BBMAP_BITS) {
1086 unsigned long s, e;
1088 j = find_last_bit(vb->dirty_map,
1089 VMAP_BBMAP_BITS);
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);
1094 flush = 1;
1096 if (s < start)
1097 start = s;
1098 if (e > end)
1099 end = e;
1101 spin_unlock(&vb->lock);
1103 rcu_read_unlock();
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;
1120 BUG_ON(!addr);
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))
1129 vb_free(mem, size);
1130 else
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;
1147 unsigned long addr;
1148 void *mem;
1150 if (likely(count <= VMAP_MAX_ALLOC)) {
1151 mem = vb_alloc(size, GFP_KERNEL);
1152 if (IS_ERR(mem))
1153 return NULL;
1154 addr = (unsigned long)mem;
1155 } else {
1156 struct vmap_area *va;
1157 va = alloc_vmap_area(size, PAGE_SIZE,
1158 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
1159 if (IS_ERR(va))
1160 return NULL;
1162 addr = va->va_start;
1163 mem = (void *)addr;
1165 if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
1166 vm_unmap_ram(mem, count);
1167 return NULL;
1169 return mem;
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);
1192 break;
1193 } else
1194 BUG_ON(tmp->addr + tmp->size > vm->addr);
1196 vm->next = *p;
1197 *p = vm;
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;
1215 unsigned long addr;
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;
1229 int i;
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;
1249 va->vm = tmp;
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
1267 * friends.
1269 * NOTE:
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.
1274 * RETURNS:
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
1290 * friends.
1292 * NOTE:
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);
1324 int err;
1326 err = vmap_page_range(addr, end, prot, *pages);
1327 if (err > 0) {
1328 *pages += err;
1329 err = 0;
1332 return err;
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);
1340 vm->flags = flags;
1341 vm->addr = (void *)va->va_start;
1342 vm->size = va->va_end - va->va_start;
1343 vm->caller = caller;
1344 va->vm = vm;
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().
1356 smp_wmb();
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))
1373 return NULL;
1375 area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1376 if (unlikely(!area))
1377 return NULL;
1380 * We always allocate a guard page.
1382 size += PAGE_SIZE;
1384 va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1385 if (IS_ERR(va)) {
1386 kfree(area);
1387 return NULL;
1390 setup_vmalloc_vm(area, va, flags, caller);
1392 return area;
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,
1405 const void *caller)
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,
1428 const void *caller)
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
1440 * pointer valid.
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)
1448 return va->vm;
1450 return NULL;
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);
1470 va->vm = NULL;
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;
1478 return vm;
1480 return NULL;
1483 static void __vunmap(const void *addr, int deallocate_pages)
1485 struct vm_struct *area;
1487 if (!addr)
1488 return;
1490 if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
1491 addr))
1492 return;
1494 area = remove_vm_area(addr);
1495 if (unlikely(!area)) {
1496 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1497 addr);
1498 return;
1501 debug_check_no_locks_freed(addr, area->size);
1502 debug_check_no_obj_freed(addr, area->size);
1504 if (deallocate_pages) {
1505 int i;
1507 for (i = 0; i < area->nr_pages; i++) {
1508 struct page *page = area->pages[i];
1510 BUG_ON(!page);
1511 __free_page(page);
1514 if (area->flags & VM_VPAGES)
1515 vfree(area->pages);
1516 else
1517 kfree(area->pages);
1520 kfree(area);
1521 return;
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)
1540 BUG_ON(in_nmi());
1542 kmemleak_free(addr);
1544 if (!addr)
1545 return;
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);
1550 } else
1551 __vunmap(addr, 1);
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());
1567 might_sleep();
1568 if (addr)
1569 __vunmap(addr, 0);
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
1581 * space.
1583 void *vmap(struct page **pages, unsigned int count,
1584 unsigned long flags, pgprot_t prot)
1586 struct vm_struct *area;
1588 might_sleep();
1590 if (count > totalram_pages)
1591 return NULL;
1593 area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1594 __builtin_return_address(0));
1595 if (!area)
1596 return NULL;
1598 if (map_vm_area(area, prot, &pages)) {
1599 vunmap(area->addr);
1600 return NULL;
1603 return area->addr;
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;
1627 } else {
1628 pages = kmalloc_node(array_size, nested_gfp, node);
1630 area->pages = pages;
1631 if (!area->pages) {
1632 remove_vm_area(area->addr);
1633 kfree(area);
1634 return NULL;
1637 for (i = 0; i < area->nr_pages; i++) {
1638 struct page *page;
1639 gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
1641 if (node == NUMA_NO_NODE)
1642 page = alloc_page(tmp_mask);
1643 else
1644 page = alloc_pages_node(node, tmp_mask, order);
1646 if (unlikely(!page)) {
1647 /* Successfully allocated i pages, free them in __vunmap() */
1648 area->nr_pages = i;
1649 goto fail;
1651 area->pages[i] = page;
1654 if (map_vm_area(area, prot, &pages))
1655 goto fail;
1656 return area->addr;
1658 fail:
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);
1662 vfree(area->addr);
1663 return NULL;
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;
1686 void *addr;
1687 unsigned long real_size = size;
1689 size = PAGE_ALIGN(size);
1690 if (!size || (size >> PAGE_SHIFT) > totalram_pages)
1691 goto fail;
1693 area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED,
1694 start, end, node, gfp_mask, caller);
1695 if (!area)
1696 goto fail;
1698 addr = __vmalloc_area_node(area, gfp_mask, prot, node);
1699 if (!addr)
1700 return NULL;
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);
1716 return addr;
1718 fail:
1719 warn_alloc_failed(gfp_mask, 0,
1720 "vmalloc: allocation failure: %lu bytes\n",
1721 real_size);
1722 return NULL;
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;
1803 void *ret;
1805 ret = __vmalloc_node(size, SHMLBA,
1806 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1807 PAGE_KERNEL, NUMA_NO_NODE,
1808 __builtin_return_address(0));
1809 if (ret) {
1810 area = find_vm_area(ret);
1811 area->flags |= VM_USERMAP;
1813 return ret;
1815 EXPORT_SYMBOL(vmalloc_user);
1818 * vmalloc_node - allocate memory on a specific node
1819 * @size: allocation size
1820 * @node: numa node
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
1838 * @node: numa node
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
1856 #endif
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
1880 #else
1881 #define GFP_VMALLOC32 GFP_KERNEL
1882 #endif
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;
1908 void *ret;
1910 ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1911 NUMA_NO_NODE, __builtin_return_address(0));
1912 if (ret) {
1913 area = find_vm_area(ret);
1914 area->flags |= VM_USERMAP;
1916 return ret;
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)
1927 struct page *p;
1928 int copied = 0;
1930 while (count) {
1931 unsigned long offset, length;
1933 offset = (unsigned long)addr & ~PAGE_MASK;
1934 length = PAGE_SIZE - offset;
1935 if (length > count)
1936 length = count;
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.
1945 if (p) {
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);
1952 kunmap_atomic(map);
1953 } else
1954 memset(buf, 0, length);
1956 addr += length;
1957 buf += length;
1958 copied += length;
1959 count -= length;
1961 return copied;
1964 static int aligned_vwrite(char *buf, char *addr, unsigned long count)
1966 struct page *p;
1967 int copied = 0;
1969 while (count) {
1970 unsigned long offset, length;
1972 offset = (unsigned long)addr & ~PAGE_MASK;
1973 length = PAGE_SIZE - offset;
1974 if (length > count)
1975 length = count;
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.
1984 if (p) {
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);
1991 kunmap_atomic(map);
1993 addr += length;
1994 buf += length;
1995 copied += length;
1996 count -= length;
1998 return copied;
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;
2033 unsigned long n;
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) {
2041 if (!count)
2042 break;
2044 if (!(va->flags & VM_VM_AREA))
2045 continue;
2047 vm = va->vm;
2048 vaddr = (char *) vm->addr;
2049 if (addr >= vaddr + get_vm_area_size(vm))
2050 continue;
2051 while (addr < vaddr) {
2052 if (count == 0)
2053 goto finished;
2054 *buf = '\0';
2055 buf++;
2056 addr++;
2057 count--;
2059 n = vaddr + get_vm_area_size(vm) - addr;
2060 if (n > count)
2061 n = count;
2062 if (!(vm->flags & VM_IOREMAP))
2063 aligned_vread(buf, addr, n);
2064 else /* IOREMAP area is treated as memory hole */
2065 memset(buf, 0, n);
2066 buf += n;
2067 addr += n;
2068 count -= n;
2070 finished:
2071 spin_unlock(&vmap_area_lock);
2073 if (buf == buf_start)
2074 return 0;
2075 /* zero-fill memory holes */
2076 if (buf != buf_start + buflen)
2077 memset(buf, 0, buflen - (buf - buf_start));
2079 return buflen;
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;
2112 char *vaddr;
2113 unsigned long n, buflen;
2114 int copied = 0;
2116 /* Don't allow overflow */
2117 if ((unsigned long) addr + count < count)
2118 count = -(unsigned long) addr;
2119 buflen = count;
2121 spin_lock(&vmap_area_lock);
2122 list_for_each_entry(va, &vmap_area_list, list) {
2123 if (!count)
2124 break;
2126 if (!(va->flags & VM_VM_AREA))
2127 continue;
2129 vm = va->vm;
2130 vaddr = (char *) vm->addr;
2131 if (addr >= vaddr + get_vm_area_size(vm))
2132 continue;
2133 while (addr < vaddr) {
2134 if (count == 0)
2135 goto finished;
2136 buf++;
2137 addr++;
2138 count--;
2140 n = vaddr + get_vm_area_size(vm) - addr;
2141 if (n > count)
2142 n = count;
2143 if (!(vm->flags & VM_IOREMAP)) {
2144 aligned_vwrite(buf, addr, n);
2145 copied++;
2147 buf += n;
2148 addr += n;
2149 count -= n;
2151 finished:
2152 spin_unlock(&vmap_area_lock);
2153 if (!copied)
2154 return 0;
2155 return buflen;
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
2170 * met.
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))
2182 return -EINVAL;
2184 area = find_vm_area(kaddr);
2185 if (!area)
2186 return -EINVAL;
2188 if (!(area->flags & VM_USERMAP))
2189 return -EINVAL;
2191 if (kaddr + size > area->addr + area->size)
2192 return -EINVAL;
2194 do {
2195 struct page *page = vmalloc_to_page(kaddr);
2196 int ret;
2198 ret = vm_insert_page(vma, uaddr, page);
2199 if (ret)
2200 return ret;
2202 uaddr += PAGE_SIZE;
2203 kaddr += PAGE_SIZE;
2204 size -= PAGE_SIZE;
2205 } while (size > 0);
2207 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2209 return 0;
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
2238 * have one.
2240 void __attribute__((weak)) vmalloc_sync_all(void)
2245 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
2247 pte_t ***p = data;
2249 if (p) {
2250 *(*p) = pte;
2251 (*p)++;
2253 return 0;
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
2265 * are created.
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));
2276 if (area == NULL)
2277 return NULL;
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)) {
2285 free_vm_area(area);
2286 return NULL;
2289 return area;
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);
2298 kfree(area);
2300 EXPORT_SYMBOL_GPL(free_vm_area);
2302 #ifdef CONFIG_SMP
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;
2327 while (n) {
2328 va = rb_entry(n, struct vmap_area, rb_node);
2329 if (end < va->va_end)
2330 n = n->rb_left;
2331 else if (end > va->va_end)
2332 n = n->rb_right;
2333 else
2334 break;
2337 if (!va)
2338 return false;
2340 if (va->va_end > end) {
2341 *pnext = va;
2342 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2343 } else {
2344 *pprev = va;
2345 *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
2347 return true;
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
2354 * @align: alignment
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
2364 * that.
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);
2371 unsigned long addr;
2373 if (*pnext)
2374 addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
2375 else
2376 addr = vmalloc_end;
2378 while (*pprev && (*pprev)->va_end > addr) {
2379 *pnext = *pprev;
2380 *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
2383 return addr;
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,
2412 size_t align)
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])
2434 last_area = area;
2436 for (area2 = 0; area2 < nr_vms; area2++) {
2437 unsigned long start2 = offsets[area2];
2438 unsigned long end2 = start2 + sizes[area2];
2440 if (area2 == area)
2441 continue;
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) {
2450 WARN_ON(true);
2451 return NULL;
2454 vms = kcalloc(nr_vms, sizeof(vms[0]), GFP_KERNEL);
2455 vas = kcalloc(nr_vms, sizeof(vas[0]), GFP_KERNEL);
2456 if (!vas || !vms)
2457 goto err_free2;
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])
2463 goto err_free;
2465 retry:
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;
2475 goto found;
2477 base = pvm_determine_end(&next, &prev, align) - end;
2479 while (true) {
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
2485 * comparing.
2487 if (base + last_end < vmalloc_start + last_end) {
2488 spin_unlock(&vmap_area_lock);
2489 if (!purged) {
2490 purge_vmap_area_lazy();
2491 purged = true;
2492 goto retry;
2494 goto err_free;
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;
2503 term_area = area;
2504 continue;
2508 * If prev overlaps, shift down next and prev and move
2509 * base so that it's right below new next and then
2510 * recheck.
2512 if (prev && prev->va_end > base + start) {
2513 next = prev;
2514 prev = node_to_va(rb_prev(&next->rb_node));
2515 base = pvm_determine_end(&next, &prev, align) - end;
2516 term_area = area;
2517 continue;
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)
2526 break;
2527 start = offsets[area];
2528 end = start + sizes[area];
2529 pvm_find_next_prev(base + end, &next, &prev);
2531 found:
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,
2548 pcpu_get_vm_areas);
2550 kfree(vas);
2551 return vms;
2553 err_free:
2554 for (area = 0; area < nr_vms; area++) {
2555 kfree(vas[area]);
2556 kfree(vms[area]);
2558 err_free2:
2559 kfree(vas);
2560 kfree(vms);
2561 return NULL;
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)
2573 int i;
2575 for (i = 0; i < nr_vms; i++)
2576 free_vm_area(vms[i]);
2577 kfree(vms);
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)
2585 loff_t n = *pos;
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) {
2591 n--;
2592 va = list_entry(va->list.next, typeof(*va), list);
2594 if (!n && &va->list != &vmap_area_list)
2595 return va;
2597 return NULL;
2601 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
2603 struct vmap_area *va = p, *next;
2605 ++*pos;
2606 next = list_entry(va->list.next, typeof(*va), list);
2607 if (&next->list != &vmap_area_list)
2608 return next;
2610 return NULL;
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;
2624 if (!counters)
2625 return;
2627 /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
2628 smp_rmb();
2629 if (v->flags & VM_UNINITIALIZED)
2630 return;
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)
2638 if (counters[nr])
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))
2653 return 0;
2655 v = va->vm;
2657 seq_printf(m, "0x%pK-0x%pK %7ld",
2658 v->addr, v->addr + v->size, v->size);
2660 if (v->caller)
2661 seq_printf(m, " %pS", v->caller);
2663 if (v->nr_pages)
2664 seq_printf(m, " pages=%d", v->nr_pages);
2666 if (v->phys_addr)
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);
2685 seq_putc(m, '\n');
2686 return 0;
2689 static const struct seq_operations vmalloc_op = {
2690 .start = s_start,
2691 .next = s_next,
2692 .stop = s_stop,
2693 .show = s_show,
2696 static int vmalloc_open(struct inode *inode, struct file *file)
2698 unsigned int *ptr = NULL;
2699 int ret;
2701 if (IS_ENABLED(CONFIG_NUMA)) {
2702 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
2703 if (ptr == NULL)
2704 return -ENOMEM;
2706 ret = seq_open(file, &vmalloc_op);
2707 if (!ret) {
2708 struct seq_file *m = file->private_data;
2709 m->private = ptr;
2710 } else
2711 kfree(ptr);
2712 return ret;
2715 static const struct file_operations proc_vmalloc_operations = {
2716 .open = vmalloc_open,
2717 .read = seq_read,
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);
2725 return 0;
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;
2735 vmi->used = 0;
2736 vmi->largest_chunk = 0;
2738 prev_end = VMALLOC_START;
2740 rcu_read_lock();
2742 if (list_empty(&vmap_area_list)) {
2743 vmi->largest_chunk = VMALLOC_TOTAL;
2744 goto out;
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)
2754 continue;
2755 if (addr >= VMALLOC_END)
2756 break;
2758 if (va->flags & (VM_LAZY_FREE | VM_LAZY_FREEING))
2759 continue;
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;
2773 out:
2774 rcu_read_unlock();
2776 #endif