kmemleak: Add the vmalloc memory allocation/freeing hooks
[safe/jmp/linux-2.6] / mm / vmalloc.c
1 /*
2  *  linux/mm/vmalloc.c
3  *
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  */
10
11 #include <linux/vmalloc.h>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/highmem.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/interrupt.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugobjects.h>
21 #include <linux/kallsyms.h>
22 #include <linux/list.h>
23 #include <linux/rbtree.h>
24 #include <linux/radix-tree.h>
25 #include <linux/rcupdate.h>
26 #include <linux/bootmem.h>
27 #include <linux/pfn.h>
28 #include <linux/kmemleak.h>
29
30 #include <asm/atomic.h>
31 #include <asm/uaccess.h>
32 #include <asm/tlbflush.h>
33
34
35 /*** Page table manipulation functions ***/
36
37 static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end)
38 {
39         pte_t *pte;
40
41         pte = pte_offset_kernel(pmd, addr);
42         do {
43                 pte_t ptent = ptep_get_and_clear(&init_mm, addr, pte);
44                 WARN_ON(!pte_none(ptent) && !pte_present(ptent));
45         } while (pte++, addr += PAGE_SIZE, addr != end);
46 }
47
48 static void vunmap_pmd_range(pud_t *pud, unsigned long addr, unsigned long end)
49 {
50         pmd_t *pmd;
51         unsigned long next;
52
53         pmd = pmd_offset(pud, addr);
54         do {
55                 next = pmd_addr_end(addr, end);
56                 if (pmd_none_or_clear_bad(pmd))
57                         continue;
58                 vunmap_pte_range(pmd, addr, next);
59         } while (pmd++, addr = next, addr != end);
60 }
61
62 static void vunmap_pud_range(pgd_t *pgd, unsigned long addr, unsigned long end)
63 {
64         pud_t *pud;
65         unsigned long next;
66
67         pud = pud_offset(pgd, addr);
68         do {
69                 next = pud_addr_end(addr, end);
70                 if (pud_none_or_clear_bad(pud))
71                         continue;
72                 vunmap_pmd_range(pud, addr, next);
73         } while (pud++, addr = next, addr != end);
74 }
75
76 static void vunmap_page_range(unsigned long addr, unsigned long end)
77 {
78         pgd_t *pgd;
79         unsigned long next;
80
81         BUG_ON(addr >= end);
82         pgd = pgd_offset_k(addr);
83         do {
84                 next = pgd_addr_end(addr, end);
85                 if (pgd_none_or_clear_bad(pgd))
86                         continue;
87                 vunmap_pud_range(pgd, addr, next);
88         } while (pgd++, addr = next, addr != end);
89 }
90
91 static int vmap_pte_range(pmd_t *pmd, unsigned long addr,
92                 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
93 {
94         pte_t *pte;
95
96         /*
97          * nr is a running index into the array which helps higher level
98          * callers keep track of where we're up to.
99          */
100
101         pte = pte_alloc_kernel(pmd, addr);
102         if (!pte)
103                 return -ENOMEM;
104         do {
105                 struct page *page = pages[*nr];
106
107                 if (WARN_ON(!pte_none(*pte)))
108                         return -EBUSY;
109                 if (WARN_ON(!page))
110                         return -ENOMEM;
111                 set_pte_at(&init_mm, addr, pte, mk_pte(page, prot));
112                 (*nr)++;
113         } while (pte++, addr += PAGE_SIZE, addr != end);
114         return 0;
115 }
116
117 static int vmap_pmd_range(pud_t *pud, unsigned long addr,
118                 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
119 {
120         pmd_t *pmd;
121         unsigned long next;
122
123         pmd = pmd_alloc(&init_mm, pud, addr);
124         if (!pmd)
125                 return -ENOMEM;
126         do {
127                 next = pmd_addr_end(addr, end);
128                 if (vmap_pte_range(pmd, addr, next, prot, pages, nr))
129                         return -ENOMEM;
130         } while (pmd++, addr = next, addr != end);
131         return 0;
132 }
133
134 static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
135                 unsigned long end, pgprot_t prot, struct page **pages, int *nr)
136 {
137         pud_t *pud;
138         unsigned long next;
139
140         pud = pud_alloc(&init_mm, pgd, addr);
141         if (!pud)
142                 return -ENOMEM;
143         do {
144                 next = pud_addr_end(addr, end);
145                 if (vmap_pmd_range(pud, addr, next, prot, pages, nr))
146                         return -ENOMEM;
147         } while (pud++, addr = next, addr != end);
148         return 0;
149 }
150
151 /*
152  * Set up page tables in kva (addr, end). The ptes shall have prot "prot", and
153  * will have pfns corresponding to the "pages" array.
154  *
155  * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
156  */
157 static int vmap_page_range_noflush(unsigned long start, unsigned long end,
158                                    pgprot_t prot, struct page **pages)
159 {
160         pgd_t *pgd;
161         unsigned long next;
162         unsigned long addr = start;
163         int err = 0;
164         int nr = 0;
165
166         BUG_ON(addr >= end);
167         pgd = pgd_offset_k(addr);
168         do {
169                 next = pgd_addr_end(addr, end);
170                 err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
171                 if (err)
172                         break;
173         } while (pgd++, addr = next, addr != end);
174
175         if (unlikely(err))
176                 return err;
177         return nr;
178 }
179
180 static int vmap_page_range(unsigned long start, unsigned long end,
181                            pgprot_t prot, struct page **pages)
182 {
183         int ret;
184
185         ret = vmap_page_range_noflush(start, end, prot, pages);
186         flush_cache_vmap(start, end);
187         return ret;
188 }
189
190 static inline int is_vmalloc_or_module_addr(const void *x)
191 {
192         /*
193          * ARM, x86-64 and sparc64 put modules in a special place,
194          * and fall back on vmalloc() if that fails. Others
195          * just put it in the vmalloc space.
196          */
197 #if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
198         unsigned long addr = (unsigned long)x;
199         if (addr >= MODULES_VADDR && addr < MODULES_END)
200                 return 1;
201 #endif
202         return is_vmalloc_addr(x);
203 }
204
205 /*
206  * Walk a vmap address to the struct page it maps.
207  */
208 struct page *vmalloc_to_page(const void *vmalloc_addr)
209 {
210         unsigned long addr = (unsigned long) vmalloc_addr;
211         struct page *page = NULL;
212         pgd_t *pgd = pgd_offset_k(addr);
213
214         /*
215          * XXX we might need to change this if we add VIRTUAL_BUG_ON for
216          * architectures that do not vmalloc module space
217          */
218         VIRTUAL_BUG_ON(!is_vmalloc_or_module_addr(vmalloc_addr));
219
220         if (!pgd_none(*pgd)) {
221                 pud_t *pud = pud_offset(pgd, addr);
222                 if (!pud_none(*pud)) {
223                         pmd_t *pmd = pmd_offset(pud, addr);
224                         if (!pmd_none(*pmd)) {
225                                 pte_t *ptep, pte;
226
227                                 ptep = pte_offset_map(pmd, addr);
228                                 pte = *ptep;
229                                 if (pte_present(pte))
230                                         page = pte_page(pte);
231                                 pte_unmap(ptep);
232                         }
233                 }
234         }
235         return page;
236 }
237 EXPORT_SYMBOL(vmalloc_to_page);
238
239 /*
240  * Map a vmalloc()-space virtual address to the physical page frame number.
241  */
242 unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
243 {
244         return page_to_pfn(vmalloc_to_page(vmalloc_addr));
245 }
246 EXPORT_SYMBOL(vmalloc_to_pfn);
247
248
249 /*** Global kva allocator ***/
250
251 #define VM_LAZY_FREE    0x01
252 #define VM_LAZY_FREEING 0x02
253 #define VM_VM_AREA      0x04
254
255 struct vmap_area {
256         unsigned long va_start;
257         unsigned long va_end;
258         unsigned long flags;
259         struct rb_node rb_node;         /* address sorted rbtree */
260         struct list_head list;          /* address sorted list */
261         struct list_head purge_list;    /* "lazy purge" list */
262         void *private;
263         struct rcu_head rcu_head;
264 };
265
266 static DEFINE_SPINLOCK(vmap_area_lock);
267 static struct rb_root vmap_area_root = RB_ROOT;
268 static LIST_HEAD(vmap_area_list);
269
270 static struct vmap_area *__find_vmap_area(unsigned long addr)
271 {
272         struct rb_node *n = vmap_area_root.rb_node;
273
274         while (n) {
275                 struct vmap_area *va;
276
277                 va = rb_entry(n, struct vmap_area, rb_node);
278                 if (addr < va->va_start)
279                         n = n->rb_left;
280                 else if (addr > va->va_start)
281                         n = n->rb_right;
282                 else
283                         return va;
284         }
285
286         return NULL;
287 }
288
289 static void __insert_vmap_area(struct vmap_area *va)
290 {
291         struct rb_node **p = &vmap_area_root.rb_node;
292         struct rb_node *parent = NULL;
293         struct rb_node *tmp;
294
295         while (*p) {
296                 struct vmap_area *tmp;
297
298                 parent = *p;
299                 tmp = rb_entry(parent, struct vmap_area, rb_node);
300                 if (va->va_start < tmp->va_end)
301                         p = &(*p)->rb_left;
302                 else if (va->va_end > tmp->va_start)
303                         p = &(*p)->rb_right;
304                 else
305                         BUG();
306         }
307
308         rb_link_node(&va->rb_node, parent, p);
309         rb_insert_color(&va->rb_node, &vmap_area_root);
310
311         /* address-sort this list so it is usable like the vmlist */
312         tmp = rb_prev(&va->rb_node);
313         if (tmp) {
314                 struct vmap_area *prev;
315                 prev = rb_entry(tmp, struct vmap_area, rb_node);
316                 list_add_rcu(&va->list, &prev->list);
317         } else
318                 list_add_rcu(&va->list, &vmap_area_list);
319 }
320
321 static void purge_vmap_area_lazy(void);
322
323 /*
324  * Allocate a region of KVA of the specified size and alignment, within the
325  * vstart and vend.
326  */
327 static struct vmap_area *alloc_vmap_area(unsigned long size,
328                                 unsigned long align,
329                                 unsigned long vstart, unsigned long vend,
330                                 int node, gfp_t gfp_mask)
331 {
332         struct vmap_area *va;
333         struct rb_node *n;
334         unsigned long addr;
335         int purged = 0;
336
337         BUG_ON(!size);
338         BUG_ON(size & ~PAGE_MASK);
339
340         va = kmalloc_node(sizeof(struct vmap_area),
341                         gfp_mask & GFP_RECLAIM_MASK, node);
342         if (unlikely(!va))
343                 return ERR_PTR(-ENOMEM);
344
345 retry:
346         addr = ALIGN(vstart, align);
347
348         spin_lock(&vmap_area_lock);
349         if (addr + size - 1 < addr)
350                 goto overflow;
351
352         /* XXX: could have a last_hole cache */
353         n = vmap_area_root.rb_node;
354         if (n) {
355                 struct vmap_area *first = NULL;
356
357                 do {
358                         struct vmap_area *tmp;
359                         tmp = rb_entry(n, struct vmap_area, rb_node);
360                         if (tmp->va_end >= addr) {
361                                 if (!first && tmp->va_start < addr + size)
362                                         first = tmp;
363                                 n = n->rb_left;
364                         } else {
365                                 first = tmp;
366                                 n = n->rb_right;
367                         }
368                 } while (n);
369
370                 if (!first)
371                         goto found;
372
373                 if (first->va_end < addr) {
374                         n = rb_next(&first->rb_node);
375                         if (n)
376                                 first = rb_entry(n, struct vmap_area, rb_node);
377                         else
378                                 goto found;
379                 }
380
381                 while (addr + size > first->va_start && addr + size <= vend) {
382                         addr = ALIGN(first->va_end + PAGE_SIZE, align);
383                         if (addr + size - 1 < addr)
384                                 goto overflow;
385
386                         n = rb_next(&first->rb_node);
387                         if (n)
388                                 first = rb_entry(n, struct vmap_area, rb_node);
389                         else
390                                 goto found;
391                 }
392         }
393 found:
394         if (addr + size > vend) {
395 overflow:
396                 spin_unlock(&vmap_area_lock);
397                 if (!purged) {
398                         purge_vmap_area_lazy();
399                         purged = 1;
400                         goto retry;
401                 }
402                 if (printk_ratelimit())
403                         printk(KERN_WARNING
404                                 "vmap allocation for size %lu failed: "
405                                 "use vmalloc=<size> to increase size.\n", size);
406                 kfree(va);
407                 return ERR_PTR(-EBUSY);
408         }
409
410         BUG_ON(addr & (align-1));
411
412         va->va_start = addr;
413         va->va_end = addr + size;
414         va->flags = 0;
415         __insert_vmap_area(va);
416         spin_unlock(&vmap_area_lock);
417
418         return va;
419 }
420
421 static void rcu_free_va(struct rcu_head *head)
422 {
423         struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
424
425         kfree(va);
426 }
427
428 static void __free_vmap_area(struct vmap_area *va)
429 {
430         BUG_ON(RB_EMPTY_NODE(&va->rb_node));
431         rb_erase(&va->rb_node, &vmap_area_root);
432         RB_CLEAR_NODE(&va->rb_node);
433         list_del_rcu(&va->list);
434
435         call_rcu(&va->rcu_head, rcu_free_va);
436 }
437
438 /*
439  * Free a region of KVA allocated by alloc_vmap_area
440  */
441 static void free_vmap_area(struct vmap_area *va)
442 {
443         spin_lock(&vmap_area_lock);
444         __free_vmap_area(va);
445         spin_unlock(&vmap_area_lock);
446 }
447
448 /*
449  * Clear the pagetable entries of a given vmap_area
450  */
451 static void unmap_vmap_area(struct vmap_area *va)
452 {
453         vunmap_page_range(va->va_start, va->va_end);
454 }
455
456 static void vmap_debug_free_range(unsigned long start, unsigned long end)
457 {
458         /*
459          * Unmap page tables and force a TLB flush immediately if
460          * CONFIG_DEBUG_PAGEALLOC is set. This catches use after free
461          * bugs similarly to those in linear kernel virtual address
462          * space after a page has been freed.
463          *
464          * All the lazy freeing logic is still retained, in order to
465          * minimise intrusiveness of this debugging feature.
466          *
467          * This is going to be *slow* (linear kernel virtual address
468          * debugging doesn't do a broadcast TLB flush so it is a lot
469          * faster).
470          */
471 #ifdef CONFIG_DEBUG_PAGEALLOC
472         vunmap_page_range(start, end);
473         flush_tlb_kernel_range(start, end);
474 #endif
475 }
476
477 /*
478  * lazy_max_pages is the maximum amount of virtual address space we gather up
479  * before attempting to purge with a TLB flush.
480  *
481  * There is a tradeoff here: a larger number will cover more kernel page tables
482  * and take slightly longer to purge, but it will linearly reduce the number of
483  * global TLB flushes that must be performed. It would seem natural to scale
484  * this number up linearly with the number of CPUs (because vmapping activity
485  * could also scale linearly with the number of CPUs), however it is likely
486  * that in practice, workloads might be constrained in other ways that mean
487  * vmap activity will not scale linearly with CPUs. Also, I want to be
488  * conservative and not introduce a big latency on huge systems, so go with
489  * a less aggressive log scale. It will still be an improvement over the old
490  * code, and it will be simple to change the scale factor if we find that it
491  * becomes a problem on bigger systems.
492  */
493 static unsigned long lazy_max_pages(void)
494 {
495         unsigned int log;
496
497         log = fls(num_online_cpus());
498
499         return log * (32UL * 1024 * 1024 / PAGE_SIZE);
500 }
501
502 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
503
504 /*
505  * Purges all lazily-freed vmap areas.
506  *
507  * If sync is 0 then don't purge if there is already a purge in progress.
508  * If force_flush is 1, then flush kernel TLBs between *start and *end even
509  * if we found no lazy vmap areas to unmap (callers can use this to optimise
510  * their own TLB flushing).
511  * Returns with *start = min(*start, lowest purged address)
512  *              *end = max(*end, highest purged address)
513  */
514 static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
515                                         int sync, int force_flush)
516 {
517         static DEFINE_SPINLOCK(purge_lock);
518         LIST_HEAD(valist);
519         struct vmap_area *va;
520         struct vmap_area *n_va;
521         int nr = 0;
522
523         /*
524          * If sync is 0 but force_flush is 1, we'll go sync anyway but callers
525          * should not expect such behaviour. This just simplifies locking for
526          * the case that isn't actually used at the moment anyway.
527          */
528         if (!sync && !force_flush) {
529                 if (!spin_trylock(&purge_lock))
530                         return;
531         } else
532                 spin_lock(&purge_lock);
533
534         rcu_read_lock();
535         list_for_each_entry_rcu(va, &vmap_area_list, list) {
536                 if (va->flags & VM_LAZY_FREE) {
537                         if (va->va_start < *start)
538                                 *start = va->va_start;
539                         if (va->va_end > *end)
540                                 *end = va->va_end;
541                         nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
542                         unmap_vmap_area(va);
543                         list_add_tail(&va->purge_list, &valist);
544                         va->flags |= VM_LAZY_FREEING;
545                         va->flags &= ~VM_LAZY_FREE;
546                 }
547         }
548         rcu_read_unlock();
549
550         if (nr) {
551                 BUG_ON(nr > atomic_read(&vmap_lazy_nr));
552                 atomic_sub(nr, &vmap_lazy_nr);
553         }
554
555         if (nr || force_flush)
556                 flush_tlb_kernel_range(*start, *end);
557
558         if (nr) {
559                 spin_lock(&vmap_area_lock);
560                 list_for_each_entry_safe(va, n_va, &valist, purge_list)
561                         __free_vmap_area(va);
562                 spin_unlock(&vmap_area_lock);
563         }
564         spin_unlock(&purge_lock);
565 }
566
567 /*
568  * Kick off a purge of the outstanding lazy areas. Don't bother if somebody
569  * is already purging.
570  */
571 static void try_purge_vmap_area_lazy(void)
572 {
573         unsigned long start = ULONG_MAX, end = 0;
574
575         __purge_vmap_area_lazy(&start, &end, 0, 0);
576 }
577
578 /*
579  * Kick off a purge of the outstanding lazy areas.
580  */
581 static void purge_vmap_area_lazy(void)
582 {
583         unsigned long start = ULONG_MAX, end = 0;
584
585         __purge_vmap_area_lazy(&start, &end, 1, 0);
586 }
587
588 /*
589  * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
590  * called for the correct range previously.
591  */
592 static void free_unmap_vmap_area_noflush(struct vmap_area *va)
593 {
594         va->flags |= VM_LAZY_FREE;
595         atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
596         if (unlikely(atomic_read(&vmap_lazy_nr) > lazy_max_pages()))
597                 try_purge_vmap_area_lazy();
598 }
599
600 /*
601  * Free and unmap a vmap area
602  */
603 static void free_unmap_vmap_area(struct vmap_area *va)
604 {
605         flush_cache_vunmap(va->va_start, va->va_end);
606         free_unmap_vmap_area_noflush(va);
607 }
608
609 static struct vmap_area *find_vmap_area(unsigned long addr)
610 {
611         struct vmap_area *va;
612
613         spin_lock(&vmap_area_lock);
614         va = __find_vmap_area(addr);
615         spin_unlock(&vmap_area_lock);
616
617         return va;
618 }
619
620 static void free_unmap_vmap_area_addr(unsigned long addr)
621 {
622         struct vmap_area *va;
623
624         va = find_vmap_area(addr);
625         BUG_ON(!va);
626         free_unmap_vmap_area(va);
627 }
628
629
630 /*** Per cpu kva allocator ***/
631
632 /*
633  * vmap space is limited especially on 32 bit architectures. Ensure there is
634  * room for at least 16 percpu vmap blocks per CPU.
635  */
636 /*
637  * If we had a constant VMALLOC_START and VMALLOC_END, we'd like to be able
638  * to #define VMALLOC_SPACE             (VMALLOC_END-VMALLOC_START). Guess
639  * instead (we just need a rough idea)
640  */
641 #if BITS_PER_LONG == 32
642 #define VMALLOC_SPACE           (128UL*1024*1024)
643 #else
644 #define VMALLOC_SPACE           (128UL*1024*1024*1024)
645 #endif
646
647 #define VMALLOC_PAGES           (VMALLOC_SPACE / PAGE_SIZE)
648 #define VMAP_MAX_ALLOC          BITS_PER_LONG   /* 256K with 4K pages */
649 #define VMAP_BBMAP_BITS_MAX     1024    /* 4MB with 4K pages */
650 #define VMAP_BBMAP_BITS_MIN     (VMAP_MAX_ALLOC*2)
651 #define VMAP_MIN(x, y)          ((x) < (y) ? (x) : (y)) /* can't use min() */
652 #define VMAP_MAX(x, y)          ((x) > (y) ? (x) : (y)) /* can't use max() */
653 #define VMAP_BBMAP_BITS         VMAP_MIN(VMAP_BBMAP_BITS_MAX,           \
654                                         VMAP_MAX(VMAP_BBMAP_BITS_MIN,   \
655                                                 VMALLOC_PAGES / NR_CPUS / 16))
656
657 #define VMAP_BLOCK_SIZE         (VMAP_BBMAP_BITS * PAGE_SIZE)
658
659 static bool vmap_initialized __read_mostly = false;
660
661 struct vmap_block_queue {
662         spinlock_t lock;
663         struct list_head free;
664         struct list_head dirty;
665         unsigned int nr_dirty;
666 };
667
668 struct vmap_block {
669         spinlock_t lock;
670         struct vmap_area *va;
671         struct vmap_block_queue *vbq;
672         unsigned long free, dirty;
673         DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
674         DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
675         union {
676                 struct list_head free_list;
677                 struct rcu_head rcu_head;
678         };
679 };
680
681 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
682 static DEFINE_PER_CPU(struct vmap_block_queue, vmap_block_queue);
683
684 /*
685  * Radix tree of vmap blocks, indexed by address, to quickly find a vmap block
686  * in the free path. Could get rid of this if we change the API to return a
687  * "cookie" from alloc, to be passed to free. But no big deal yet.
688  */
689 static DEFINE_SPINLOCK(vmap_block_tree_lock);
690 static RADIX_TREE(vmap_block_tree, GFP_ATOMIC);
691
692 /*
693  * We should probably have a fallback mechanism to allocate virtual memory
694  * out of partially filled vmap blocks. However vmap block sizing should be
695  * fairly reasonable according to the vmalloc size, so it shouldn't be a
696  * big problem.
697  */
698
699 static unsigned long addr_to_vb_idx(unsigned long addr)
700 {
701         addr -= VMALLOC_START & ~(VMAP_BLOCK_SIZE-1);
702         addr /= VMAP_BLOCK_SIZE;
703         return addr;
704 }
705
706 static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
707 {
708         struct vmap_block_queue *vbq;
709         struct vmap_block *vb;
710         struct vmap_area *va;
711         unsigned long vb_idx;
712         int node, err;
713
714         node = numa_node_id();
715
716         vb = kmalloc_node(sizeof(struct vmap_block),
717                         gfp_mask & GFP_RECLAIM_MASK, node);
718         if (unlikely(!vb))
719                 return ERR_PTR(-ENOMEM);
720
721         va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
722                                         VMALLOC_START, VMALLOC_END,
723                                         node, gfp_mask);
724         if (unlikely(IS_ERR(va))) {
725                 kfree(vb);
726                 return ERR_PTR(PTR_ERR(va));
727         }
728
729         err = radix_tree_preload(gfp_mask);
730         if (unlikely(err)) {
731                 kfree(vb);
732                 free_vmap_area(va);
733                 return ERR_PTR(err);
734         }
735
736         spin_lock_init(&vb->lock);
737         vb->va = va;
738         vb->free = VMAP_BBMAP_BITS;
739         vb->dirty = 0;
740         bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
741         bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
742         INIT_LIST_HEAD(&vb->free_list);
743
744         vb_idx = addr_to_vb_idx(va->va_start);
745         spin_lock(&vmap_block_tree_lock);
746         err = radix_tree_insert(&vmap_block_tree, vb_idx, vb);
747         spin_unlock(&vmap_block_tree_lock);
748         BUG_ON(err);
749         radix_tree_preload_end();
750
751         vbq = &get_cpu_var(vmap_block_queue);
752         vb->vbq = vbq;
753         spin_lock(&vbq->lock);
754         list_add(&vb->free_list, &vbq->free);
755         spin_unlock(&vbq->lock);
756         put_cpu_var(vmap_cpu_blocks);
757
758         return vb;
759 }
760
761 static void rcu_free_vb(struct rcu_head *head)
762 {
763         struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
764
765         kfree(vb);
766 }
767
768 static void free_vmap_block(struct vmap_block *vb)
769 {
770         struct vmap_block *tmp;
771         unsigned long vb_idx;
772
773         BUG_ON(!list_empty(&vb->free_list));
774
775         vb_idx = addr_to_vb_idx(vb->va->va_start);
776         spin_lock(&vmap_block_tree_lock);
777         tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
778         spin_unlock(&vmap_block_tree_lock);
779         BUG_ON(tmp != vb);
780
781         free_unmap_vmap_area_noflush(vb->va);
782         call_rcu(&vb->rcu_head, rcu_free_vb);
783 }
784
785 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
786 {
787         struct vmap_block_queue *vbq;
788         struct vmap_block *vb;
789         unsigned long addr = 0;
790         unsigned int order;
791
792         BUG_ON(size & ~PAGE_MASK);
793         BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
794         order = get_order(size);
795
796 again:
797         rcu_read_lock();
798         vbq = &get_cpu_var(vmap_block_queue);
799         list_for_each_entry_rcu(vb, &vbq->free, free_list) {
800                 int i;
801
802                 spin_lock(&vb->lock);
803                 i = bitmap_find_free_region(vb->alloc_map,
804                                                 VMAP_BBMAP_BITS, order);
805
806                 if (i >= 0) {
807                         addr = vb->va->va_start + (i << PAGE_SHIFT);
808                         BUG_ON(addr_to_vb_idx(addr) !=
809                                         addr_to_vb_idx(vb->va->va_start));
810                         vb->free -= 1UL << order;
811                         if (vb->free == 0) {
812                                 spin_lock(&vbq->lock);
813                                 list_del_init(&vb->free_list);
814                                 spin_unlock(&vbq->lock);
815                         }
816                         spin_unlock(&vb->lock);
817                         break;
818                 }
819                 spin_unlock(&vb->lock);
820         }
821         put_cpu_var(vmap_cpu_blocks);
822         rcu_read_unlock();
823
824         if (!addr) {
825                 vb = new_vmap_block(gfp_mask);
826                 if (IS_ERR(vb))
827                         return vb;
828                 goto again;
829         }
830
831         return (void *)addr;
832 }
833
834 static void vb_free(const void *addr, unsigned long size)
835 {
836         unsigned long offset;
837         unsigned long vb_idx;
838         unsigned int order;
839         struct vmap_block *vb;
840
841         BUG_ON(size & ~PAGE_MASK);
842         BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
843
844         flush_cache_vunmap((unsigned long)addr, (unsigned long)addr + size);
845
846         order = get_order(size);
847
848         offset = (unsigned long)addr & (VMAP_BLOCK_SIZE - 1);
849
850         vb_idx = addr_to_vb_idx((unsigned long)addr);
851         rcu_read_lock();
852         vb = radix_tree_lookup(&vmap_block_tree, vb_idx);
853         rcu_read_unlock();
854         BUG_ON(!vb);
855
856         spin_lock(&vb->lock);
857         bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
858
859         vb->dirty += 1UL << order;
860         if (vb->dirty == VMAP_BBMAP_BITS) {
861                 BUG_ON(vb->free || !list_empty(&vb->free_list));
862                 spin_unlock(&vb->lock);
863                 free_vmap_block(vb);
864         } else
865                 spin_unlock(&vb->lock);
866 }
867
868 /**
869  * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
870  *
871  * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
872  * to amortize TLB flushing overheads. What this means is that any page you
873  * have now, may, in a former life, have been mapped into kernel virtual
874  * address by the vmap layer and so there might be some CPUs with TLB entries
875  * still referencing that page (additional to the regular 1:1 kernel mapping).
876  *
877  * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
878  * be sure that none of the pages we have control over will have any aliases
879  * from the vmap layer.
880  */
881 void vm_unmap_aliases(void)
882 {
883         unsigned long start = ULONG_MAX, end = 0;
884         int cpu;
885         int flush = 0;
886
887         if (unlikely(!vmap_initialized))
888                 return;
889
890         for_each_possible_cpu(cpu) {
891                 struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
892                 struct vmap_block *vb;
893
894                 rcu_read_lock();
895                 list_for_each_entry_rcu(vb, &vbq->free, free_list) {
896                         int i;
897
898                         spin_lock(&vb->lock);
899                         i = find_first_bit(vb->dirty_map, VMAP_BBMAP_BITS);
900                         while (i < VMAP_BBMAP_BITS) {
901                                 unsigned long s, e;
902                                 int j;
903                                 j = find_next_zero_bit(vb->dirty_map,
904                                         VMAP_BBMAP_BITS, i);
905
906                                 s = vb->va->va_start + (i << PAGE_SHIFT);
907                                 e = vb->va->va_start + (j << PAGE_SHIFT);
908                                 vunmap_page_range(s, e);
909                                 flush = 1;
910
911                                 if (s < start)
912                                         start = s;
913                                 if (e > end)
914                                         end = e;
915
916                                 i = j;
917                                 i = find_next_bit(vb->dirty_map,
918                                                         VMAP_BBMAP_BITS, i);
919                         }
920                         spin_unlock(&vb->lock);
921                 }
922                 rcu_read_unlock();
923         }
924
925         __purge_vmap_area_lazy(&start, &end, 1, flush);
926 }
927 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
928
929 /**
930  * vm_unmap_ram - unmap linear kernel address space set up by vm_map_ram
931  * @mem: the pointer returned by vm_map_ram
932  * @count: the count passed to that vm_map_ram call (cannot unmap partial)
933  */
934 void vm_unmap_ram(const void *mem, unsigned int count)
935 {
936         unsigned long size = count << PAGE_SHIFT;
937         unsigned long addr = (unsigned long)mem;
938
939         BUG_ON(!addr);
940         BUG_ON(addr < VMALLOC_START);
941         BUG_ON(addr > VMALLOC_END);
942         BUG_ON(addr & (PAGE_SIZE-1));
943
944         debug_check_no_locks_freed(mem, size);
945         vmap_debug_free_range(addr, addr+size);
946
947         if (likely(count <= VMAP_MAX_ALLOC))
948                 vb_free(mem, size);
949         else
950                 free_unmap_vmap_area_addr(addr);
951 }
952 EXPORT_SYMBOL(vm_unmap_ram);
953
954 /**
955  * vm_map_ram - map pages linearly into kernel virtual address (vmalloc space)
956  * @pages: an array of pointers to the pages to be mapped
957  * @count: number of pages
958  * @node: prefer to allocate data structures on this node
959  * @prot: memory protection to use. PAGE_KERNEL for regular RAM
960  *
961  * Returns: a pointer to the address that has been mapped, or %NULL on failure
962  */
963 void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t prot)
964 {
965         unsigned long size = count << PAGE_SHIFT;
966         unsigned long addr;
967         void *mem;
968
969         if (likely(count <= VMAP_MAX_ALLOC)) {
970                 mem = vb_alloc(size, GFP_KERNEL);
971                 if (IS_ERR(mem))
972                         return NULL;
973                 addr = (unsigned long)mem;
974         } else {
975                 struct vmap_area *va;
976                 va = alloc_vmap_area(size, PAGE_SIZE,
977                                 VMALLOC_START, VMALLOC_END, node, GFP_KERNEL);
978                 if (IS_ERR(va))
979                         return NULL;
980
981                 addr = va->va_start;
982                 mem = (void *)addr;
983         }
984         if (vmap_page_range(addr, addr + size, prot, pages) < 0) {
985                 vm_unmap_ram(mem, count);
986                 return NULL;
987         }
988         return mem;
989 }
990 EXPORT_SYMBOL(vm_map_ram);
991
992 /**
993  * vm_area_register_early - register vmap area early during boot
994  * @vm: vm_struct to register
995  * @align: requested alignment
996  *
997  * This function is used to register kernel vm area before
998  * vmalloc_init() is called.  @vm->size and @vm->flags should contain
999  * proper values on entry and other fields should be zero.  On return,
1000  * vm->addr contains the allocated address.
1001  *
1002  * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1003  */
1004 void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1005 {
1006         static size_t vm_init_off __initdata;
1007         unsigned long addr;
1008
1009         addr = ALIGN(VMALLOC_START + vm_init_off, align);
1010         vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1011
1012         vm->addr = (void *)addr;
1013
1014         vm->next = vmlist;
1015         vmlist = vm;
1016 }
1017
1018 void __init vmalloc_init(void)
1019 {
1020         struct vmap_area *va;
1021         struct vm_struct *tmp;
1022         int i;
1023
1024         for_each_possible_cpu(i) {
1025                 struct vmap_block_queue *vbq;
1026
1027                 vbq = &per_cpu(vmap_block_queue, i);
1028                 spin_lock_init(&vbq->lock);
1029                 INIT_LIST_HEAD(&vbq->free);
1030                 INIT_LIST_HEAD(&vbq->dirty);
1031                 vbq->nr_dirty = 0;
1032         }
1033
1034         /* Import existing vmlist entries. */
1035         for (tmp = vmlist; tmp; tmp = tmp->next) {
1036                 va = alloc_bootmem(sizeof(struct vmap_area));
1037                 va->flags = tmp->flags | VM_VM_AREA;
1038                 va->va_start = (unsigned long)tmp->addr;
1039                 va->va_end = va->va_start + tmp->size;
1040                 __insert_vmap_area(va);
1041         }
1042         vmap_initialized = true;
1043 }
1044
1045 /**
1046  * map_kernel_range_noflush - map kernel VM area with the specified pages
1047  * @addr: start of the VM area to map
1048  * @size: size of the VM area to map
1049  * @prot: page protection flags to use
1050  * @pages: pages to map
1051  *
1052  * Map PFN_UP(@size) pages at @addr.  The VM area @addr and @size
1053  * specify should have been allocated using get_vm_area() and its
1054  * friends.
1055  *
1056  * NOTE:
1057  * This function does NOT do any cache flushing.  The caller is
1058  * responsible for calling flush_cache_vmap() on to-be-mapped areas
1059  * before calling this function.
1060  *
1061  * RETURNS:
1062  * The number of pages mapped on success, -errno on failure.
1063  */
1064 int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1065                              pgprot_t prot, struct page **pages)
1066 {
1067         return vmap_page_range_noflush(addr, addr + size, prot, pages);
1068 }
1069
1070 /**
1071  * unmap_kernel_range_noflush - unmap kernel VM area
1072  * @addr: start of the VM area to unmap
1073  * @size: size of the VM area to unmap
1074  *
1075  * Unmap PFN_UP(@size) pages at @addr.  The VM area @addr and @size
1076  * specify should have been allocated using get_vm_area() and its
1077  * friends.
1078  *
1079  * NOTE:
1080  * This function does NOT do any cache flushing.  The caller is
1081  * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1082  * before calling this function and flush_tlb_kernel_range() after.
1083  */
1084 void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1085 {
1086         vunmap_page_range(addr, addr + size);
1087 }
1088
1089 /**
1090  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1091  * @addr: start of the VM area to unmap
1092  * @size: size of the VM area to unmap
1093  *
1094  * Similar to unmap_kernel_range_noflush() but flushes vcache before
1095  * the unmapping and tlb after.
1096  */
1097 void unmap_kernel_range(unsigned long addr, unsigned long size)
1098 {
1099         unsigned long end = addr + size;
1100
1101         flush_cache_vunmap(addr, end);
1102         vunmap_page_range(addr, end);
1103         flush_tlb_kernel_range(addr, end);
1104 }
1105
1106 int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
1107 {
1108         unsigned long addr = (unsigned long)area->addr;
1109         unsigned long end = addr + area->size - PAGE_SIZE;
1110         int err;
1111
1112         err = vmap_page_range(addr, end, prot, *pages);
1113         if (err > 0) {
1114                 *pages += err;
1115                 err = 0;
1116         }
1117
1118         return err;
1119 }
1120 EXPORT_SYMBOL_GPL(map_vm_area);
1121
1122 /*** Old vmalloc interfaces ***/
1123 DEFINE_RWLOCK(vmlist_lock);
1124 struct vm_struct *vmlist;
1125
1126 static struct vm_struct *__get_vm_area_node(unsigned long size,
1127                 unsigned long flags, unsigned long start, unsigned long end,
1128                 int node, gfp_t gfp_mask, void *caller)
1129 {
1130         static struct vmap_area *va;
1131         struct vm_struct *area;
1132         struct vm_struct *tmp, **p;
1133         unsigned long align = 1;
1134
1135         BUG_ON(in_interrupt());
1136         if (flags & VM_IOREMAP) {
1137                 int bit = fls(size);
1138
1139                 if (bit > IOREMAP_MAX_ORDER)
1140                         bit = IOREMAP_MAX_ORDER;
1141                 else if (bit < PAGE_SHIFT)
1142                         bit = PAGE_SHIFT;
1143
1144                 align = 1ul << bit;
1145         }
1146
1147         size = PAGE_ALIGN(size);
1148         if (unlikely(!size))
1149                 return NULL;
1150
1151         area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
1152         if (unlikely(!area))
1153                 return NULL;
1154
1155         /*
1156          * We always allocate a guard page.
1157          */
1158         size += PAGE_SIZE;
1159
1160         va = alloc_vmap_area(size, align, start, end, node, gfp_mask);
1161         if (IS_ERR(va)) {
1162                 kfree(area);
1163                 return NULL;
1164         }
1165
1166         area->flags = flags;
1167         area->addr = (void *)va->va_start;
1168         area->size = size;
1169         area->pages = NULL;
1170         area->nr_pages = 0;
1171         area->phys_addr = 0;
1172         area->caller = caller;
1173         va->private = area;
1174         va->flags |= VM_VM_AREA;
1175
1176         write_lock(&vmlist_lock);
1177         for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
1178                 if (tmp->addr >= area->addr)
1179                         break;
1180         }
1181         area->next = *p;
1182         *p = area;
1183         write_unlock(&vmlist_lock);
1184
1185         return area;
1186 }
1187
1188 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1189                                 unsigned long start, unsigned long end)
1190 {
1191         return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1192                                                 __builtin_return_address(0));
1193 }
1194 EXPORT_SYMBOL_GPL(__get_vm_area);
1195
1196 struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1197                                        unsigned long start, unsigned long end,
1198                                        void *caller)
1199 {
1200         return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1201                                   caller);
1202 }
1203
1204 /**
1205  *      get_vm_area  -  reserve a contiguous kernel virtual area
1206  *      @size:          size of the area
1207  *      @flags:         %VM_IOREMAP for I/O mappings or VM_ALLOC
1208  *
1209  *      Search an area of @size in the kernel virtual mapping area,
1210  *      and reserved it for out purposes.  Returns the area descriptor
1211  *      on success or %NULL on failure.
1212  */
1213 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
1214 {
1215         return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1216                                 -1, GFP_KERNEL, __builtin_return_address(0));
1217 }
1218
1219 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
1220                                 void *caller)
1221 {
1222         return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
1223                                                 -1, GFP_KERNEL, caller);
1224 }
1225
1226 struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
1227                                    int node, gfp_t gfp_mask)
1228 {
1229         return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
1230                                   gfp_mask, __builtin_return_address(0));
1231 }
1232
1233 static struct vm_struct *find_vm_area(const void *addr)
1234 {
1235         struct vmap_area *va;
1236
1237         va = find_vmap_area((unsigned long)addr);
1238         if (va && va->flags & VM_VM_AREA)
1239                 return va->private;
1240
1241         return NULL;
1242 }
1243
1244 /**
1245  *      remove_vm_area  -  find and remove a continuous kernel virtual area
1246  *      @addr:          base address
1247  *
1248  *      Search for the kernel VM area starting at @addr, and remove it.
1249  *      This function returns the found VM area, but using it is NOT safe
1250  *      on SMP machines, except for its size or flags.
1251  */
1252 struct vm_struct *remove_vm_area(const void *addr)
1253 {
1254         struct vmap_area *va;
1255
1256         va = find_vmap_area((unsigned long)addr);
1257         if (va && va->flags & VM_VM_AREA) {
1258                 struct vm_struct *vm = va->private;
1259                 struct vm_struct *tmp, **p;
1260
1261                 vmap_debug_free_range(va->va_start, va->va_end);
1262                 free_unmap_vmap_area(va);
1263                 vm->size -= PAGE_SIZE;
1264
1265                 write_lock(&vmlist_lock);
1266                 for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
1267                         ;
1268                 *p = tmp->next;
1269                 write_unlock(&vmlist_lock);
1270
1271                 return vm;
1272         }
1273         return NULL;
1274 }
1275
1276 static void __vunmap(const void *addr, int deallocate_pages)
1277 {
1278         struct vm_struct *area;
1279
1280         if (!addr)
1281                 return;
1282
1283         if ((PAGE_SIZE-1) & (unsigned long)addr) {
1284                 WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
1285                 return;
1286         }
1287
1288         area = remove_vm_area(addr);
1289         if (unlikely(!area)) {
1290                 WARN(1, KERN_ERR "Trying to vfree() nonexistent vm area (%p)\n",
1291                                 addr);
1292                 return;
1293         }
1294
1295         debug_check_no_locks_freed(addr, area->size);
1296         debug_check_no_obj_freed(addr, area->size);
1297
1298         if (deallocate_pages) {
1299                 int i;
1300
1301                 for (i = 0; i < area->nr_pages; i++) {
1302                         struct page *page = area->pages[i];
1303
1304                         BUG_ON(!page);
1305                         __free_page(page);
1306                 }
1307
1308                 if (area->flags & VM_VPAGES)
1309                         vfree(area->pages);
1310                 else
1311                         kfree(area->pages);
1312         }
1313
1314         kfree(area);
1315         return;
1316 }
1317
1318 /**
1319  *      vfree  -  release memory allocated by vmalloc()
1320  *      @addr:          memory base address
1321  *
1322  *      Free the virtually continuous memory area starting at @addr, as
1323  *      obtained from vmalloc(), vmalloc_32() or __vmalloc(). If @addr is
1324  *      NULL, no operation is performed.
1325  *
1326  *      Must not be called in interrupt context.
1327  */
1328 void vfree(const void *addr)
1329 {
1330         BUG_ON(in_interrupt());
1331
1332         kmemleak_free(addr);
1333
1334         __vunmap(addr, 1);
1335 }
1336 EXPORT_SYMBOL(vfree);
1337
1338 /**
1339  *      vunmap  -  release virtual mapping obtained by vmap()
1340  *      @addr:          memory base address
1341  *
1342  *      Free the virtually contiguous memory area starting at @addr,
1343  *      which was created from the page array passed to vmap().
1344  *
1345  *      Must not be called in interrupt context.
1346  */
1347 void vunmap(const void *addr)
1348 {
1349         BUG_ON(in_interrupt());
1350         might_sleep();
1351         __vunmap(addr, 0);
1352 }
1353 EXPORT_SYMBOL(vunmap);
1354
1355 /**
1356  *      vmap  -  map an array of pages into virtually contiguous space
1357  *      @pages:         array of page pointers
1358  *      @count:         number of pages to map
1359  *      @flags:         vm_area->flags
1360  *      @prot:          page protection for the mapping
1361  *
1362  *      Maps @count pages from @pages into contiguous kernel virtual
1363  *      space.
1364  */
1365 void *vmap(struct page **pages, unsigned int count,
1366                 unsigned long flags, pgprot_t prot)
1367 {
1368         struct vm_struct *area;
1369
1370         might_sleep();
1371
1372         if (count > num_physpages)
1373                 return NULL;
1374
1375         area = get_vm_area_caller((count << PAGE_SHIFT), flags,
1376                                         __builtin_return_address(0));
1377         if (!area)
1378                 return NULL;
1379
1380         if (map_vm_area(area, prot, &pages)) {
1381                 vunmap(area->addr);
1382                 return NULL;
1383         }
1384
1385         return area->addr;
1386 }
1387 EXPORT_SYMBOL(vmap);
1388
1389 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1390                             int node, void *caller);
1391 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
1392                                  pgprot_t prot, int node, void *caller)
1393 {
1394         struct page **pages;
1395         unsigned int nr_pages, array_size, i;
1396
1397         nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
1398         array_size = (nr_pages * sizeof(struct page *));
1399
1400         area->nr_pages = nr_pages;
1401         /* Please note that the recursion is strictly bounded. */
1402         if (array_size > PAGE_SIZE) {
1403                 pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
1404                                 PAGE_KERNEL, node, caller);
1405                 area->flags |= VM_VPAGES;
1406         } else {
1407                 pages = kmalloc_node(array_size,
1408                                 (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
1409                                 node);
1410         }
1411         area->pages = pages;
1412         area->caller = caller;
1413         if (!area->pages) {
1414                 remove_vm_area(area->addr);
1415                 kfree(area);
1416                 return NULL;
1417         }
1418
1419         for (i = 0; i < area->nr_pages; i++) {
1420                 struct page *page;
1421
1422                 if (node < 0)
1423                         page = alloc_page(gfp_mask);
1424                 else
1425                         page = alloc_pages_node(node, gfp_mask, 0);
1426
1427                 if (unlikely(!page)) {
1428                         /* Successfully allocated i pages, free them in __vunmap() */
1429                         area->nr_pages = i;
1430                         goto fail;
1431                 }
1432                 area->pages[i] = page;
1433         }
1434
1435         if (map_vm_area(area, prot, &pages))
1436                 goto fail;
1437         return area->addr;
1438
1439 fail:
1440         vfree(area->addr);
1441         return NULL;
1442 }
1443
1444 void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
1445 {
1446         void *addr = __vmalloc_area_node(area, gfp_mask, prot, -1,
1447                                          __builtin_return_address(0));
1448
1449         /*
1450          * A ref_count = 3 is needed because the vm_struct and vmap_area
1451          * structures allocated in the __get_vm_area_node() function contain
1452          * references to the virtual address of the vmalloc'ed block.
1453          */
1454         kmemleak_alloc(addr, area->size - PAGE_SIZE, 3, gfp_mask);
1455
1456         return addr;
1457 }
1458
1459 /**
1460  *      __vmalloc_node  -  allocate virtually contiguous memory
1461  *      @size:          allocation size
1462  *      @gfp_mask:      flags for the page level allocator
1463  *      @prot:          protection mask for the allocated pages
1464  *      @node:          node to use for allocation or -1
1465  *      @caller:        caller's return address
1466  *
1467  *      Allocate enough pages to cover @size from the page level
1468  *      allocator with @gfp_mask flags.  Map them into contiguous
1469  *      kernel virtual space, using a pagetable protection of @prot.
1470  */
1471 static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
1472                                                 int node, void *caller)
1473 {
1474         struct vm_struct *area;
1475         void *addr;
1476         unsigned long real_size = size;
1477
1478         size = PAGE_ALIGN(size);
1479         if (!size || (size >> PAGE_SHIFT) > num_physpages)
1480                 return NULL;
1481
1482         area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
1483                                                 node, gfp_mask, caller);
1484
1485         if (!area)
1486                 return NULL;
1487
1488         addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
1489
1490         /*
1491          * A ref_count = 3 is needed because the vm_struct and vmap_area
1492          * structures allocated in the __get_vm_area_node() function contain
1493          * references to the virtual address of the vmalloc'ed block.
1494          */
1495         kmemleak_alloc(addr, real_size, 3, gfp_mask);
1496
1497         return addr;
1498 }
1499
1500 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
1501 {
1502         return __vmalloc_node(size, gfp_mask, prot, -1,
1503                                 __builtin_return_address(0));
1504 }
1505 EXPORT_SYMBOL(__vmalloc);
1506
1507 /**
1508  *      vmalloc  -  allocate virtually contiguous memory
1509  *      @size:          allocation size
1510  *      Allocate enough pages to cover @size from the page level
1511  *      allocator and map them into contiguous kernel virtual space.
1512  *
1513  *      For tight control over page level allocator and protection flags
1514  *      use __vmalloc() instead.
1515  */
1516 void *vmalloc(unsigned long size)
1517 {
1518         return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1519                                         -1, __builtin_return_address(0));
1520 }
1521 EXPORT_SYMBOL(vmalloc);
1522
1523 /**
1524  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
1525  * @size: allocation size
1526  *
1527  * The resulting memory area is zeroed so it can be mapped to userspace
1528  * without leaking data.
1529  */
1530 void *vmalloc_user(unsigned long size)
1531 {
1532         struct vm_struct *area;
1533         void *ret;
1534
1535         ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
1536                              PAGE_KERNEL, -1, __builtin_return_address(0));
1537         if (ret) {
1538                 area = find_vm_area(ret);
1539                 area->flags |= VM_USERMAP;
1540         }
1541         return ret;
1542 }
1543 EXPORT_SYMBOL(vmalloc_user);
1544
1545 /**
1546  *      vmalloc_node  -  allocate memory on a specific node
1547  *      @size:          allocation size
1548  *      @node:          numa node
1549  *
1550  *      Allocate enough pages to cover @size from the page level
1551  *      allocator and map them into contiguous kernel virtual space.
1552  *
1553  *      For tight control over page level allocator and protection flags
1554  *      use __vmalloc() instead.
1555  */
1556 void *vmalloc_node(unsigned long size, int node)
1557 {
1558         return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
1559                                         node, __builtin_return_address(0));
1560 }
1561 EXPORT_SYMBOL(vmalloc_node);
1562
1563 #ifndef PAGE_KERNEL_EXEC
1564 # define PAGE_KERNEL_EXEC PAGE_KERNEL
1565 #endif
1566
1567 /**
1568  *      vmalloc_exec  -  allocate virtually contiguous, executable memory
1569  *      @size:          allocation size
1570  *
1571  *      Kernel-internal function to allocate enough pages to cover @size
1572  *      the page level allocator and map them into contiguous and
1573  *      executable kernel virtual space.
1574  *
1575  *      For tight control over page level allocator and protection flags
1576  *      use __vmalloc() instead.
1577  */
1578
1579 void *vmalloc_exec(unsigned long size)
1580 {
1581         return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
1582                               -1, __builtin_return_address(0));
1583 }
1584
1585 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)
1586 #define GFP_VMALLOC32 GFP_DMA32 | GFP_KERNEL
1587 #elif defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA)
1588 #define GFP_VMALLOC32 GFP_DMA | GFP_KERNEL
1589 #else
1590 #define GFP_VMALLOC32 GFP_KERNEL
1591 #endif
1592
1593 /**
1594  *      vmalloc_32  -  allocate virtually contiguous memory (32bit addressable)
1595  *      @size:          allocation size
1596  *
1597  *      Allocate enough 32bit PA addressable pages to cover @size from the
1598  *      page level allocator and map them into contiguous kernel virtual space.
1599  */
1600 void *vmalloc_32(unsigned long size)
1601 {
1602         return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
1603                               -1, __builtin_return_address(0));
1604 }
1605 EXPORT_SYMBOL(vmalloc_32);
1606
1607 /**
1608  * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
1609  *      @size:          allocation size
1610  *
1611  * The resulting memory area is 32bit addressable and zeroed so it can be
1612  * mapped to userspace without leaking data.
1613  */
1614 void *vmalloc_32_user(unsigned long size)
1615 {
1616         struct vm_struct *area;
1617         void *ret;
1618
1619         ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
1620                              -1, __builtin_return_address(0));
1621         if (ret) {
1622                 area = find_vm_area(ret);
1623                 area->flags |= VM_USERMAP;
1624         }
1625         return ret;
1626 }
1627 EXPORT_SYMBOL(vmalloc_32_user);
1628
1629 long vread(char *buf, char *addr, unsigned long count)
1630 {
1631         struct vm_struct *tmp;
1632         char *vaddr, *buf_start = buf;
1633         unsigned long n;
1634
1635         /* Don't allow overflow */
1636         if ((unsigned long) addr + count < count)
1637                 count = -(unsigned long) addr;
1638
1639         read_lock(&vmlist_lock);
1640         for (tmp = vmlist; tmp; tmp = tmp->next) {
1641                 vaddr = (char *) tmp->addr;
1642                 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1643                         continue;
1644                 while (addr < vaddr) {
1645                         if (count == 0)
1646                                 goto finished;
1647                         *buf = '\0';
1648                         buf++;
1649                         addr++;
1650                         count--;
1651                 }
1652                 n = vaddr + tmp->size - PAGE_SIZE - addr;
1653                 do {
1654                         if (count == 0)
1655                                 goto finished;
1656                         *buf = *addr;
1657                         buf++;
1658                         addr++;
1659                         count--;
1660                 } while (--n > 0);
1661         }
1662 finished:
1663         read_unlock(&vmlist_lock);
1664         return buf - buf_start;
1665 }
1666
1667 long vwrite(char *buf, char *addr, unsigned long count)
1668 {
1669         struct vm_struct *tmp;
1670         char *vaddr, *buf_start = buf;
1671         unsigned long n;
1672
1673         /* Don't allow overflow */
1674         if ((unsigned long) addr + count < count)
1675                 count = -(unsigned long) addr;
1676
1677         read_lock(&vmlist_lock);
1678         for (tmp = vmlist; tmp; tmp = tmp->next) {
1679                 vaddr = (char *) tmp->addr;
1680                 if (addr >= vaddr + tmp->size - PAGE_SIZE)
1681                         continue;
1682                 while (addr < vaddr) {
1683                         if (count == 0)
1684                                 goto finished;
1685                         buf++;
1686                         addr++;
1687                         count--;
1688                 }
1689                 n = vaddr + tmp->size - PAGE_SIZE - addr;
1690                 do {
1691                         if (count == 0)
1692                                 goto finished;
1693                         *addr = *buf;
1694                         buf++;
1695                         addr++;
1696                         count--;
1697                 } while (--n > 0);
1698         }
1699 finished:
1700         read_unlock(&vmlist_lock);
1701         return buf - buf_start;
1702 }
1703
1704 /**
1705  *      remap_vmalloc_range  -  map vmalloc pages to userspace
1706  *      @vma:           vma to cover (map full range of vma)
1707  *      @addr:          vmalloc memory
1708  *      @pgoff:         number of pages into addr before first page to map
1709  *
1710  *      Returns:        0 for success, -Exxx on failure
1711  *
1712  *      This function checks that addr is a valid vmalloc'ed area, and
1713  *      that it is big enough to cover the vma. Will return failure if
1714  *      that criteria isn't met.
1715  *
1716  *      Similar to remap_pfn_range() (see mm/memory.c)
1717  */
1718 int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1719                                                 unsigned long pgoff)
1720 {
1721         struct vm_struct *area;
1722         unsigned long uaddr = vma->vm_start;
1723         unsigned long usize = vma->vm_end - vma->vm_start;
1724
1725         if ((PAGE_SIZE-1) & (unsigned long)addr)
1726                 return -EINVAL;
1727
1728         area = find_vm_area(addr);
1729         if (!area)
1730                 return -EINVAL;
1731
1732         if (!(area->flags & VM_USERMAP))
1733                 return -EINVAL;
1734
1735         if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
1736                 return -EINVAL;
1737
1738         addr += pgoff << PAGE_SHIFT;
1739         do {
1740                 struct page *page = vmalloc_to_page(addr);
1741                 int ret;
1742
1743                 ret = vm_insert_page(vma, uaddr, page);
1744                 if (ret)
1745                         return ret;
1746
1747                 uaddr += PAGE_SIZE;
1748                 addr += PAGE_SIZE;
1749                 usize -= PAGE_SIZE;
1750         } while (usize > 0);
1751
1752         /* Prevent "things" like memory migration? VM_flags need a cleanup... */
1753         vma->vm_flags |= VM_RESERVED;
1754
1755         return 0;
1756 }
1757 EXPORT_SYMBOL(remap_vmalloc_range);
1758
1759 /*
1760  * Implement a stub for vmalloc_sync_all() if the architecture chose not to
1761  * have one.
1762  */
1763 void  __attribute__((weak)) vmalloc_sync_all(void)
1764 {
1765 }
1766
1767
1768 static int f(pte_t *pte, pgtable_t table, unsigned long addr, void *data)
1769 {
1770         /* apply_to_page_range() does all the hard work. */
1771         return 0;
1772 }
1773
1774 /**
1775  *      alloc_vm_area - allocate a range of kernel address space
1776  *      @size:          size of the area
1777  *
1778  *      Returns:        NULL on failure, vm_struct on success
1779  *
1780  *      This function reserves a range of kernel address space, and
1781  *      allocates pagetables to map that range.  No actual mappings
1782  *      are created.  If the kernel address space is not shared
1783  *      between processes, it syncs the pagetable across all
1784  *      processes.
1785  */
1786 struct vm_struct *alloc_vm_area(size_t size)
1787 {
1788         struct vm_struct *area;
1789
1790         area = get_vm_area_caller(size, VM_IOREMAP,
1791                                 __builtin_return_address(0));
1792         if (area == NULL)
1793                 return NULL;
1794
1795         /*
1796          * This ensures that page tables are constructed for this region
1797          * of kernel virtual address space and mapped into init_mm.
1798          */
1799         if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
1800                                 area->size, f, NULL)) {
1801                 free_vm_area(area);
1802                 return NULL;
1803         }
1804
1805         /* Make sure the pagetables are constructed in process kernel
1806            mappings */
1807         vmalloc_sync_all();
1808
1809         return area;
1810 }
1811 EXPORT_SYMBOL_GPL(alloc_vm_area);
1812
1813 void free_vm_area(struct vm_struct *area)
1814 {
1815         struct vm_struct *ret;
1816         ret = remove_vm_area(area->addr);
1817         BUG_ON(ret != area);
1818         kfree(area);
1819 }
1820 EXPORT_SYMBOL_GPL(free_vm_area);
1821
1822
1823 #ifdef CONFIG_PROC_FS
1824 static void *s_start(struct seq_file *m, loff_t *pos)
1825 {
1826         loff_t n = *pos;
1827         struct vm_struct *v;
1828
1829         read_lock(&vmlist_lock);
1830         v = vmlist;
1831         while (n > 0 && v) {
1832                 n--;
1833                 v = v->next;
1834         }
1835         if (!n)
1836                 return v;
1837
1838         return NULL;
1839
1840 }
1841
1842 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
1843 {
1844         struct vm_struct *v = p;
1845
1846         ++*pos;
1847         return v->next;
1848 }
1849
1850 static void s_stop(struct seq_file *m, void *p)
1851 {
1852         read_unlock(&vmlist_lock);
1853 }
1854
1855 static void show_numa_info(struct seq_file *m, struct vm_struct *v)
1856 {
1857         if (NUMA_BUILD) {
1858                 unsigned int nr, *counters = m->private;
1859
1860                 if (!counters)
1861                         return;
1862
1863                 memset(counters, 0, nr_node_ids * sizeof(unsigned int));
1864
1865                 for (nr = 0; nr < v->nr_pages; nr++)
1866                         counters[page_to_nid(v->pages[nr])]++;
1867
1868                 for_each_node_state(nr, N_HIGH_MEMORY)
1869                         if (counters[nr])
1870                                 seq_printf(m, " N%u=%u", nr, counters[nr]);
1871         }
1872 }
1873
1874 static int s_show(struct seq_file *m, void *p)
1875 {
1876         struct vm_struct *v = p;
1877
1878         seq_printf(m, "0x%p-0x%p %7ld",
1879                 v->addr, v->addr + v->size, v->size);
1880
1881         if (v->caller) {
1882                 char buff[KSYM_SYMBOL_LEN];
1883
1884                 seq_putc(m, ' ');
1885                 sprint_symbol(buff, (unsigned long)v->caller);
1886                 seq_puts(m, buff);
1887         }
1888
1889         if (v->nr_pages)
1890                 seq_printf(m, " pages=%d", v->nr_pages);
1891
1892         if (v->phys_addr)
1893                 seq_printf(m, " phys=%lx", v->phys_addr);
1894
1895         if (v->flags & VM_IOREMAP)
1896                 seq_printf(m, " ioremap");
1897
1898         if (v->flags & VM_ALLOC)
1899                 seq_printf(m, " vmalloc");
1900
1901         if (v->flags & VM_MAP)
1902                 seq_printf(m, " vmap");
1903
1904         if (v->flags & VM_USERMAP)
1905                 seq_printf(m, " user");
1906
1907         if (v->flags & VM_VPAGES)
1908                 seq_printf(m, " vpages");
1909
1910         show_numa_info(m, v);
1911         seq_putc(m, '\n');
1912         return 0;
1913 }
1914
1915 static const struct seq_operations vmalloc_op = {
1916         .start = s_start,
1917         .next = s_next,
1918         .stop = s_stop,
1919         .show = s_show,
1920 };
1921
1922 static int vmalloc_open(struct inode *inode, struct file *file)
1923 {
1924         unsigned int *ptr = NULL;
1925         int ret;
1926
1927         if (NUMA_BUILD)
1928                 ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
1929         ret = seq_open(file, &vmalloc_op);
1930         if (!ret) {
1931                 struct seq_file *m = file->private_data;
1932                 m->private = ptr;
1933         } else
1934                 kfree(ptr);
1935         return ret;
1936 }
1937
1938 static const struct file_operations proc_vmalloc_operations = {
1939         .open           = vmalloc_open,
1940         .read           = seq_read,
1941         .llseek         = seq_lseek,
1942         .release        = seq_release_private,
1943 };
1944
1945 static int __init proc_vmalloc_init(void)
1946 {
1947         proc_create("vmallocinfo", S_IRUSR, NULL, &proc_vmalloc_operations);
1948         return 0;
1949 }
1950 module_init(proc_vmalloc_init);
1951 #endif
1952