]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - mm/vmalloc.c
tmpfs: convert mem_cgroup shmem to radix-swap
[linux-2.6.git] / mm / vmalloc.c
index 323513858c20057f3a56ec476a739f96092588e5..464621d18eb249fde1403741e0f75b193aa0c18a 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/highmem.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/interrupt.h>
 #include <linux/radix-tree.h>
 #include <linux/rcupdate.h>
 #include <linux/pfn.h>
-
-#include <asm/atomic.h>
+#include <linux/kmemleak.h>
+#include <linux/atomic.h>
 #include <asm/uaccess.h>
 #include <asm/tlbflush.h>
-
+#include <asm/shmparam.h>
 
 /*** Page table manipulation functions ***/
 
@@ -167,11 +168,9 @@ static int vmap_page_range_noflush(unsigned long start, unsigned long end,
                next = pgd_addr_end(addr, end);
                err = vmap_pud_range(pgd, addr, next, prot, pages, &nr);
                if (err)
-                       break;
+                       return err;
        } while (pgd++, addr = next, addr != end);
 
-       if (unlikely(err))
-               return err;
        return nr;
 }
 
@@ -185,7 +184,7 @@ static int vmap_page_range(unsigned long start, unsigned long end,
        return ret;
 }
 
-static inline int is_vmalloc_or_module_addr(const void *x)
+int is_vmalloc_or_module_addr(const void *x)
 {
        /*
         * ARM, x86-64 and sparc64 put modules in a special place,
@@ -262,8 +261,16 @@ struct vmap_area {
 };
 
 static DEFINE_SPINLOCK(vmap_area_lock);
-static struct rb_root vmap_area_root = RB_ROOT;
 static LIST_HEAD(vmap_area_list);
+static struct rb_root vmap_area_root = RB_ROOT;
+
+/* The vmap cache globals are protected by vmap_area_lock */
+static struct rb_node *free_vmap_cache;
+static unsigned long cached_hole_size;
+static unsigned long cached_vstart;
+static unsigned long cached_align;
+
+static unsigned long vmap_area_pcpu_hole;
 
 static struct vmap_area *__find_vmap_area(unsigned long addr)
 {
@@ -291,13 +298,13 @@ static void __insert_vmap_area(struct vmap_area *va)
        struct rb_node *tmp;
 
        while (*p) {
-               struct vmap_area *tmp;
+               struct vmap_area *tmp_va;
 
                parent = *p;
-               tmp = rb_entry(parent, struct vmap_area, rb_node);
-               if (va->va_start < tmp->va_end)
+               tmp_va = rb_entry(parent, struct vmap_area, rb_node);
+               if (va->va_start < tmp_va->va_end)
                        p = &(*p)->rb_left;
-               else if (va->va_end > tmp->va_start)
+               else if (va->va_end > tmp_va->va_start)
                        p = &(*p)->rb_right;
                else
                        BUG();
@@ -331,9 +338,11 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
        struct rb_node *n;
        unsigned long addr;
        int purged = 0;
+       struct vmap_area *first;
 
        BUG_ON(!size);
        BUG_ON(size & ~PAGE_MASK);
+       BUG_ON(!is_power_of_2(align));
 
        va = kmalloc_node(sizeof(struct vmap_area),
                        gfp_mask & GFP_RECLAIM_MASK, node);
@@ -341,96 +350,141 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
                return ERR_PTR(-ENOMEM);
 
 retry:
-       addr = ALIGN(vstart, align);
-
        spin_lock(&vmap_area_lock);
-       if (addr + size - 1 < addr)
-               goto overflow;
+       /*
+        * Invalidate cache if we have more permissive parameters.
+        * cached_hole_size notes the largest hole noticed _below_
+        * the vmap_area cached in free_vmap_cache: if size fits
+        * into that hole, we want to scan from vstart to reuse
+        * the hole instead of allocating above free_vmap_cache.
+        * Note that __free_vmap_area may update free_vmap_cache
+        * without updating cached_hole_size or cached_align.
+        */
+       if (!free_vmap_cache ||
+                       size < cached_hole_size ||
+                       vstart < cached_vstart ||
+                       align < cached_align) {
+nocache:
+               cached_hole_size = 0;
+               free_vmap_cache = NULL;
+       }
+       /* record if we encounter less permissive parameters */
+       cached_vstart = vstart;
+       cached_align = align;
+
+       /* find starting point for our search */
+       if (free_vmap_cache) {
+               first = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
+               addr = ALIGN(first->va_end, align);
+               if (addr < vstart)
+                       goto nocache;
+               if (addr + size - 1 < addr)
+                       goto overflow;
+
+       } else {
+               addr = ALIGN(vstart, align);
+               if (addr + size - 1 < addr)
+                       goto overflow;
 
-       /* XXX: could have a last_hole cache */
-       n = vmap_area_root.rb_node;
-       if (n) {
-               struct vmap_area *first = NULL;
+               n = vmap_area_root.rb_node;
+               first = NULL;
 
-               do {
+               while (n) {
                        struct vmap_area *tmp;
                        tmp = rb_entry(n, struct vmap_area, rb_node);
                        if (tmp->va_end >= addr) {
-                               if (!first && tmp->va_start < addr + size)
-                                       first = tmp;
-                               n = n->rb_left;
-                       } else {
                                first = tmp;
+                               if (tmp->va_start <= addr)
+                                       break;
+                               n = n->rb_left;
+                       } else
                                n = n->rb_right;
-                       }
-               } while (n);
+               }
 
                if (!first)
                        goto found;
-
-               if (first->va_end < addr) {
-                       n = rb_next(&first->rb_node);
-                       if (n)
-                               first = rb_entry(n, struct vmap_area, rb_node);
-                       else
-                               goto found;
-               }
-
-               while (addr + size > first->va_start && addr + size <= vend) {
-                       addr = ALIGN(first->va_end + PAGE_SIZE, align);
-                       if (addr + size - 1 < addr)
-                               goto overflow;
-
-                       n = rb_next(&first->rb_node);
-                       if (n)
-                               first = rb_entry(n, struct vmap_area, rb_node);
-                       else
-                               goto found;
-               }
        }
-found:
-       if (addr + size > vend) {
-overflow:
-               spin_unlock(&vmap_area_lock);
-               if (!purged) {
-                       purge_vmap_area_lazy();
-                       purged = 1;
-                       goto retry;
-               }
-               if (printk_ratelimit())
-                       printk(KERN_WARNING
-                               "vmap allocation for size %lu failed: "
-                               "use vmalloc=<size> to increase size.\n", size);
-               kfree(va);
-               return ERR_PTR(-EBUSY);
+
+       /* from the starting point, walk areas until a suitable hole is found */
+       while (addr + size > first->va_start && addr + size <= vend) {
+               if (addr + cached_hole_size < first->va_start)
+                       cached_hole_size = first->va_start - addr;
+               addr = ALIGN(first->va_end, align);
+               if (addr + size - 1 < addr)
+                       goto overflow;
+
+               n = rb_next(&first->rb_node);
+               if (n)
+                       first = rb_entry(n, struct vmap_area, rb_node);
+               else
+                       goto found;
        }
 
-       BUG_ON(addr & (align-1));
+found:
+       if (addr + size > vend)
+               goto overflow;
 
        va->va_start = addr;
        va->va_end = addr + size;
        va->flags = 0;
        __insert_vmap_area(va);
+       free_vmap_cache = &va->rb_node;
        spin_unlock(&vmap_area_lock);
 
-       return va;
-}
+       BUG_ON(va->va_start & (align-1));
+       BUG_ON(va->va_start < vstart);
+       BUG_ON(va->va_end > vend);
 
-static void rcu_free_va(struct rcu_head *head)
-{
-       struct vmap_area *va = container_of(head, struct vmap_area, rcu_head);
+       return va;
 
+overflow:
+       spin_unlock(&vmap_area_lock);
+       if (!purged) {
+               purge_vmap_area_lazy();
+               purged = 1;
+               goto retry;
+       }
+       if (printk_ratelimit())
+               printk(KERN_WARNING
+                       "vmap allocation for size %lu failed: "
+                       "use vmalloc=<size> to increase size.\n", size);
        kfree(va);
+       return ERR_PTR(-EBUSY);
 }
 
 static void __free_vmap_area(struct vmap_area *va)
 {
        BUG_ON(RB_EMPTY_NODE(&va->rb_node));
+
+       if (free_vmap_cache) {
+               if (va->va_end < cached_vstart) {
+                       free_vmap_cache = NULL;
+               } else {
+                       struct vmap_area *cache;
+                       cache = rb_entry(free_vmap_cache, struct vmap_area, rb_node);
+                       if (va->va_start <= cache->va_start) {
+                               free_vmap_cache = rb_prev(&va->rb_node);
+                               /*
+                                * We don't try to update cached_hole_size or
+                                * cached_align, but it won't go very wrong.
+                                */
+                       }
+               }
+       }
        rb_erase(&va->rb_node, &vmap_area_root);
        RB_CLEAR_NODE(&va->rb_node);
        list_del_rcu(&va->list);
 
-       call_rcu(&va->rcu_head, rcu_free_va);
+       /*
+        * Track the highest possible candidate for pcpu area
+        * allocation.  Areas outside of vmalloc area can be returned
+        * here too, consider only end addresses which fall inside
+        * vmalloc area proper.
+        */
+       if (va->va_end > VMALLOC_START && va->va_end <= VMALLOC_END)
+               vmap_area_pcpu_hole = max(vmap_area_pcpu_hole, va->va_end);
+
+       kfree_rcu(va, rcu_head);
 }
 
 /*
@@ -499,6 +553,18 @@ static unsigned long lazy_max_pages(void)
 
 static atomic_t vmap_lazy_nr = ATOMIC_INIT(0);
 
+/* for per-CPU blocks */
+static void purge_fragmented_blocks_allcpus(void);
+
+/*
+ * called before a call to iounmap() if the caller wants vm_area_struct's
+ * immediately freed.
+ */
+void set_iounmap_nonlazy(void)
+{
+       atomic_set(&vmap_lazy_nr, lazy_max_pages()+1);
+}
+
 /*
  * Purges all lazily-freed vmap areas.
  *
@@ -529,6 +595,9 @@ static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
        } else
                spin_lock(&purge_lock);
 
+       if (sync)
+               purge_fragmented_blocks_allcpus();
+
        rcu_read_lock();
        list_for_each_entry_rcu(va, &vmap_area_list, list) {
                if (va->flags & VM_LAZY_FREE) {
@@ -537,7 +606,6 @@ static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
                        if (va->va_end > *end)
                                *end = va->va_end;
                        nr += (va->va_end - va->va_start) >> PAGE_SHIFT;
-                       unmap_vmap_area(va);
                        list_add_tail(&va->purge_list, &valist);
                        va->flags |= VM_LAZY_FREEING;
                        va->flags &= ~VM_LAZY_FREE;
@@ -545,10 +613,8 @@ static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
        }
        rcu_read_unlock();
 
-       if (nr) {
-               BUG_ON(nr > atomic_read(&vmap_lazy_nr));
+       if (nr)
                atomic_sub(nr, &vmap_lazy_nr);
-       }
 
        if (nr || force_flush)
                flush_tlb_kernel_range(*start, *end);
@@ -584,10 +650,11 @@ static void purge_vmap_area_lazy(void)
 }
 
 /*
- * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
- * called for the correct range previously.
+ * Free a vmap area, caller ensuring that the area has been unmapped
+ * and flush_cache_vunmap had been called for the correct range
+ * previously.
  */
-static void free_unmap_vmap_area_noflush(struct vmap_area *va)
+static void free_vmap_area_noflush(struct vmap_area *va)
 {
        va->flags |= VM_LAZY_FREE;
        atomic_add((va->va_end - va->va_start) >> PAGE_SHIFT, &vmap_lazy_nr);
@@ -595,6 +662,16 @@ static void free_unmap_vmap_area_noflush(struct vmap_area *va)
                try_purge_vmap_area_lazy();
 }
 
+/*
+ * Free and unmap a vmap area, caller ensuring flush_cache_vunmap had been
+ * called for the correct range previously.
+ */
+static void free_unmap_vmap_area_noflush(struct vmap_area *va)
+{
+       unmap_vmap_area(va);
+       free_vmap_area_noflush(va);
+}
+
 /*
  * Free and unmap a vmap area
  */
@@ -659,8 +736,6 @@ static bool vmap_initialized __read_mostly = false;
 struct vmap_block_queue {
        spinlock_t lock;
        struct list_head free;
-       struct list_head dirty;
-       unsigned int nr_dirty;
 };
 
 struct vmap_block {
@@ -670,10 +745,9 @@ struct vmap_block {
        unsigned long free, dirty;
        DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
        DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
-       union {
-               struct list_head free_list;
-               struct rcu_head rcu_head;
-       };
+       struct list_head free_list;
+       struct rcu_head rcu_head;
+       struct list_head purge;
 };
 
 /* Queue of free and dirty vmap blocks, for allocation and flushing purposes */
@@ -719,9 +793,9 @@ static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
        va = alloc_vmap_area(VMAP_BLOCK_SIZE, VMAP_BLOCK_SIZE,
                                        VMALLOC_START, VMALLOC_END,
                                        node, gfp_mask);
-       if (unlikely(IS_ERR(va))) {
+       if (IS_ERR(va)) {
                kfree(vb);
-               return ERR_PTR(PTR_ERR(va));
+               return ERR_CAST(va);
        }
 
        err = radix_tree_preload(gfp_mask);
@@ -749,35 +823,74 @@ static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
        vbq = &get_cpu_var(vmap_block_queue);
        vb->vbq = vbq;
        spin_lock(&vbq->lock);
-       list_add(&vb->free_list, &vbq->free);
+       list_add_rcu(&vb->free_list, &vbq->free);
        spin_unlock(&vbq->lock);
-       put_cpu_var(vmap_cpu_blocks);
+       put_cpu_var(vmap_block_queue);
 
        return vb;
 }
 
-static void rcu_free_vb(struct rcu_head *head)
-{
-       struct vmap_block *vb = container_of(head, struct vmap_block, rcu_head);
-
-       kfree(vb);
-}
-
 static void free_vmap_block(struct vmap_block *vb)
 {
        struct vmap_block *tmp;
        unsigned long vb_idx;
 
-       BUG_ON(!list_empty(&vb->free_list));
-
        vb_idx = addr_to_vb_idx(vb->va->va_start);
        spin_lock(&vmap_block_tree_lock);
        tmp = radix_tree_delete(&vmap_block_tree, vb_idx);
        spin_unlock(&vmap_block_tree_lock);
        BUG_ON(tmp != vb);
 
-       free_unmap_vmap_area_noflush(vb->va);
-       call_rcu(&vb->rcu_head, rcu_free_vb);
+       free_vmap_area_noflush(vb->va);
+       kfree_rcu(vb, rcu_head);
+}
+
+static void purge_fragmented_blocks(int cpu)
+{
+       LIST_HEAD(purge);
+       struct vmap_block *vb;
+       struct vmap_block *n_vb;
+       struct vmap_block_queue *vbq = &per_cpu(vmap_block_queue, cpu);
+
+       rcu_read_lock();
+       list_for_each_entry_rcu(vb, &vbq->free, free_list) {
+
+               if (!(vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS))
+                       continue;
+
+               spin_lock(&vb->lock);
+               if (vb->free + vb->dirty == VMAP_BBMAP_BITS && vb->dirty != VMAP_BBMAP_BITS) {
+                       vb->free = 0; /* prevent further allocs after releasing lock */
+                       vb->dirty = VMAP_BBMAP_BITS; /* prevent purging it again */
+                       bitmap_fill(vb->alloc_map, VMAP_BBMAP_BITS);
+                       bitmap_fill(vb->dirty_map, VMAP_BBMAP_BITS);
+                       spin_lock(&vbq->lock);
+                       list_del_rcu(&vb->free_list);
+                       spin_unlock(&vbq->lock);
+                       spin_unlock(&vb->lock);
+                       list_add_tail(&vb->purge, &purge);
+               } else
+                       spin_unlock(&vb->lock);
+       }
+       rcu_read_unlock();
+
+       list_for_each_entry_safe(vb, n_vb, &purge, purge) {
+               list_del(&vb->purge);
+               free_vmap_block(vb);
+       }
+}
+
+static void purge_fragmented_blocks_thiscpu(void)
+{
+       purge_fragmented_blocks(smp_processor_id());
+}
+
+static void purge_fragmented_blocks_allcpus(void)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               purge_fragmented_blocks(cpu);
 }
 
 static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
@@ -786,6 +899,7 @@ static void *vb_alloc(unsigned long size, gfp_t gfp_mask)
        struct vmap_block *vb;
        unsigned long addr = 0;
        unsigned int order;
+       int purge = 0;
 
        BUG_ON(size & ~PAGE_MASK);
        BUG_ON(size > PAGE_SIZE*VMAP_MAX_ALLOC);
@@ -798,25 +912,39 @@ again:
                int i;
 
                spin_lock(&vb->lock);
+               if (vb->free < 1UL << order)
+                       goto next;
+
                i = bitmap_find_free_region(vb->alloc_map,
                                                VMAP_BBMAP_BITS, order);
 
-               if (i >= 0) {
-                       addr = vb->va->va_start + (i << PAGE_SHIFT);
-                       BUG_ON(addr_to_vb_idx(addr) !=
-                                       addr_to_vb_idx(vb->va->va_start));
-                       vb->free -= 1UL << order;
-                       if (vb->free == 0) {
-                               spin_lock(&vbq->lock);
-                               list_del_init(&vb->free_list);
-                               spin_unlock(&vbq->lock);
+               if (i < 0) {
+                       if (vb->free + vb->dirty == VMAP_BBMAP_BITS) {
+                               /* fragmented and no outstanding allocations */
+                               BUG_ON(vb->dirty != VMAP_BBMAP_BITS);
+                               purge = 1;
                        }
-                       spin_unlock(&vb->lock);
-                       break;
+                       goto next;
+               }
+               addr = vb->va->va_start + (i << PAGE_SHIFT);
+               BUG_ON(addr_to_vb_idx(addr) !=
+                               addr_to_vb_idx(vb->va->va_start));
+               vb->free -= 1UL << order;
+               if (vb->free == 0) {
+                       spin_lock(&vbq->lock);
+                       list_del_rcu(&vb->free_list);
+                       spin_unlock(&vbq->lock);
                }
                spin_unlock(&vb->lock);
+               break;
+next:
+               spin_unlock(&vb->lock);
        }
-       put_cpu_var(vmap_cpu_blocks);
+
+       if (purge)
+               purge_fragmented_blocks_thiscpu();
+
+       put_cpu_var(vmap_block_queue);
        rcu_read_unlock();
 
        if (!addr) {
@@ -851,12 +979,14 @@ static void vb_free(const void *addr, unsigned long size)
        rcu_read_unlock();
        BUG_ON(!vb);
 
+       vunmap_page_range((unsigned long)addr, (unsigned long)addr + size);
+
        spin_lock(&vb->lock);
-       bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order);
+       BUG_ON(bitmap_allocate_region(vb->dirty_map, offset >> PAGE_SHIFT, order));
 
        vb->dirty += 1UL << order;
        if (vb->dirty == VMAP_BBMAP_BITS) {
-               BUG_ON(vb->free || !list_empty(&vb->free_list));
+               BUG_ON(vb->free);
                spin_unlock(&vb->lock);
                free_vmap_block(vb);
        } else
@@ -903,7 +1033,6 @@ void vm_unmap_aliases(void)
 
                                s = vb->va->va_start + (i << PAGE_SHIFT);
                                e = vb->va->va_start + (j << PAGE_SHIFT);
-                               vunmap_page_range(s, e);
                                flush = 1;
 
                                if (s < start)
@@ -1025,8 +1154,6 @@ void __init vmalloc_init(void)
                vbq = &per_cpu(vmap_block_queue, i);
                spin_lock_init(&vbq->lock);
                INIT_LIST_HEAD(&vbq->free);
-               INIT_LIST_HEAD(&vbq->dirty);
-               vbq->nr_dirty = 0;
        }
 
        /* Import existing vmlist entries. */
@@ -1037,6 +1164,9 @@ void __init vmalloc_init(void)
                va->va_end = va->va_start + tmp->size;
                __insert_vmap_area(va);
        }
+
+       vmap_area_pcpu_hole = VMALLOC_END;
+
        vmap_initialized = true;
 }
 
@@ -1083,6 +1213,7 @@ void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
 {
        vunmap_page_range(addr, addr + size);
 }
+EXPORT_SYMBOL_GPL(unmap_kernel_range_noflush);
 
 /**
  * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
@@ -1121,14 +1252,34 @@ EXPORT_SYMBOL_GPL(map_vm_area);
 DEFINE_RWLOCK(vmlist_lock);
 struct vm_struct *vmlist;
 
+static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
+                             unsigned long flags, void *caller)
+{
+       struct vm_struct *tmp, **p;
+
+       vm->flags = flags;
+       vm->addr = (void *)va->va_start;
+       vm->size = va->va_end - va->va_start;
+       vm->caller = caller;
+       va->private = vm;
+       va->flags |= VM_VM_AREA;
+
+       write_lock(&vmlist_lock);
+       for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
+               if (tmp->addr >= vm->addr)
+                       break;
+       }
+       vm->next = *p;
+       *p = vm;
+       write_unlock(&vmlist_lock);
+}
+
 static struct vm_struct *__get_vm_area_node(unsigned long size,
-               unsigned long flags, unsigned long start, unsigned long end,
-               int node, gfp_t gfp_mask, void *caller)
+               unsigned long align, unsigned long flags, unsigned long start,
+               unsigned long end, int node, gfp_t gfp_mask, void *caller)
 {
        static struct vmap_area *va;
        struct vm_struct *area;
-       struct vm_struct *tmp, **p;
-       unsigned long align = 1;
 
        BUG_ON(in_interrupt());
        if (flags & VM_IOREMAP) {
@@ -1146,7 +1297,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
        if (unlikely(!size))
                return NULL;
 
-       area = kmalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
+       area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node);
        if (unlikely(!area))
                return NULL;
 
@@ -1161,32 +1312,14 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
                return NULL;
        }
 
-       area->flags = flags;
-       area->addr = (void *)va->va_start;
-       area->size = size;
-       area->pages = NULL;
-       area->nr_pages = 0;
-       area->phys_addr = 0;
-       area->caller = caller;
-       va->private = area;
-       va->flags |= VM_VM_AREA;
-
-       write_lock(&vmlist_lock);
-       for (p = &vmlist; (tmp = *p) != NULL; p = &tmp->next) {
-               if (tmp->addr >= area->addr)
-                       break;
-       }
-       area->next = *p;
-       *p = area;
-       write_unlock(&vmlist_lock);
-
+       insert_vmalloc_vm(area, va, flags, caller);
        return area;
 }
 
 struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
                                unsigned long start, unsigned long end)
 {
-       return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
+       return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
                                                __builtin_return_address(0));
 }
 EXPORT_SYMBOL_GPL(__get_vm_area);
@@ -1195,7 +1328,7 @@ struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
                                       unsigned long start, unsigned long end,
                                       void *caller)
 {
-       return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
+       return __get_vm_area_node(size, 1, flags, start, end, -1, GFP_KERNEL,
                                  caller);
 }
 
@@ -1210,24 +1343,17 @@ struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
  */
 struct vm_struct *get_vm_area(unsigned long size, unsigned long flags)
 {
-       return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
+       return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
                                -1, GFP_KERNEL, __builtin_return_address(0));
 }
 
 struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,
                                void *caller)
 {
-       return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END,
+       return __get_vm_area_node(size, 1, flags, VMALLOC_START, VMALLOC_END,
                                                -1, GFP_KERNEL, caller);
 }
 
-struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags,
-                                  int node, gfp_t gfp_mask)
-{
-       return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node,
-                                 gfp_mask, __builtin_return_address(0));
-}
-
 static struct vm_struct *find_vm_area(const void *addr)
 {
        struct vmap_area *va;
@@ -1255,17 +1381,21 @@ struct vm_struct *remove_vm_area(const void *addr)
        if (va && va->flags & VM_VM_AREA) {
                struct vm_struct *vm = va->private;
                struct vm_struct *tmp, **p;
-
-               vmap_debug_free_range(va->va_start, va->va_end);
-               free_unmap_vmap_area(va);
-               vm->size -= PAGE_SIZE;
-
+               /*
+                * remove from list and disallow access to this vm_struct
+                * before unmap. (address range confliction is maintained by
+                * vmap.)
+                */
                write_lock(&vmlist_lock);
                for (p = &vmlist; (tmp = *p) != vm; p = &tmp->next)
                        ;
                *p = tmp->next;
                write_unlock(&vmlist_lock);
 
+               vmap_debug_free_range(va->va_start, va->va_end);
+               free_unmap_vmap_area(va);
+               vm->size -= PAGE_SIZE;
+
                return vm;
        }
        return NULL;
@@ -1326,6 +1456,9 @@ static void __vunmap(const void *addr, int deallocate_pages)
 void vfree(const void *addr)
 {
        BUG_ON(in_interrupt());
+
+       kmemleak_free(addr);
+
        __vunmap(addr, 1);
 }
 EXPORT_SYMBOL(vfree);
@@ -1364,7 +1497,7 @@ void *vmap(struct page **pages, unsigned int count,
 
        might_sleep();
 
-       if (count > num_physpages)
+       if (count > totalram_pages)
                return NULL;
 
        area = get_vm_area_caller((count << PAGE_SHIFT), flags,
@@ -1381,13 +1514,16 @@ void *vmap(struct page **pages, unsigned int count,
 }
 EXPORT_SYMBOL(vmap);
 
-static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
+static void *__vmalloc_node(unsigned long size, unsigned long align,
+                           gfp_t gfp_mask, pgprot_t prot,
                            int node, void *caller);
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
                                 pgprot_t prot, int node, void *caller)
 {
+       const int order = 0;
        struct page **pages;
        unsigned int nr_pages, array_size, i;
+       gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
 
        nr_pages = (area->size - PAGE_SIZE) >> PAGE_SHIFT;
        array_size = (nr_pages * sizeof(struct page *));
@@ -1395,13 +1531,11 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        area->nr_pages = nr_pages;
        /* Please note that the recursion is strictly bounded. */
        if (array_size > PAGE_SIZE) {
-               pages = __vmalloc_node(array_size, gfp_mask | __GFP_ZERO,
+               pages = __vmalloc_node(array_size, 1, nested_gfp|__GFP_HIGHMEM,
                                PAGE_KERNEL, node, caller);
                area->flags |= VM_VPAGES;
        } else {
-               pages = kmalloc_node(array_size,
-                               (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO,
-                               node);
+               pages = kmalloc_node(array_size, nested_gfp, node);
        }
        area->pages = pages;
        area->caller = caller;
@@ -1413,11 +1547,12 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 
        for (i = 0; i < area->nr_pages; i++) {
                struct page *page;
+               gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
 
                if (node < 0)
-                       page = alloc_page(gfp_mask);
+                       page = alloc_page(tmp_mask);
                else
-                       page = alloc_pages_node(node, gfp_mask, 0);
+                       page = alloc_pages_node(node, tmp_mask, order);
 
                if (unlikely(!page)) {
                        /* Successfully allocated i pages, free them in __vunmap() */
@@ -1432,19 +1567,19 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
        return area->addr;
 
 fail:
+       warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+                         "allocated %ld of %ld bytes\n",
+                         (area->nr_pages*PAGE_SIZE), area->size);
        vfree(area->addr);
        return NULL;
 }
 
-void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
-{
-       return __vmalloc_area_node(area, gfp_mask, prot, -1,
-                                       __builtin_return_address(0));
-}
-
 /**
- *     __vmalloc_node  -  allocate virtually contiguous memory
+ *     __vmalloc_node_range  -  allocate virtually contiguous memory
  *     @size:          allocation size
+ *     @align:         desired alignment
+ *     @start:         vm area range start
+ *     @end:           vm area range end
  *     @gfp_mask:      flags for the page level allocator
  *     @prot:          protection mask for the allocated pages
  *     @node:          node to use for allocation or -1
@@ -1454,31 +1589,71 @@ void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask, pgprot_t prot)
  *     allocator with @gfp_mask flags.  Map them into contiguous
  *     kernel virtual space, using a pagetable protection of @prot.
  */
-static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,
-                                               int node, void *caller)
+void *__vmalloc_node_range(unsigned long size, unsigned long align,
+                       unsigned long start, unsigned long end, gfp_t gfp_mask,
+                       pgprot_t prot, int node, void *caller)
 {
        struct vm_struct *area;
+       void *addr;
+       unsigned long real_size = size;
 
        size = PAGE_ALIGN(size);
-       if (!size || (size >> PAGE_SHIFT) > num_physpages)
+       if (!size || (size >> PAGE_SHIFT) > totalram_pages)
                return NULL;
 
-       area = __get_vm_area_node(size, VM_ALLOC, VMALLOC_START, VMALLOC_END,
-                                               node, gfp_mask, caller);
+       area = __get_vm_area_node(size, align, VM_ALLOC, start, end, node,
+                                 gfp_mask, caller);
 
        if (!area)
                return NULL;
 
-       return __vmalloc_area_node(area, gfp_mask, prot, node, caller);
+       addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
+
+       /*
+        * A ref_count = 3 is needed because the vm_struct and vmap_area
+        * structures allocated in the __get_vm_area_node() function contain
+        * references to the virtual address of the vmalloc'ed block.
+        */
+       kmemleak_alloc(addr, real_size, 3, gfp_mask);
+
+       return addr;
+}
+
+/**
+ *     __vmalloc_node  -  allocate virtually contiguous memory
+ *     @size:          allocation size
+ *     @align:         desired alignment
+ *     @gfp_mask:      flags for the page level allocator
+ *     @prot:          protection mask for the allocated pages
+ *     @node:          node to use for allocation or -1
+ *     @caller:        caller's return address
+ *
+ *     Allocate enough pages to cover @size from the page level
+ *     allocator with @gfp_mask flags.  Map them into contiguous
+ *     kernel virtual space, using a pagetable protection of @prot.
+ */
+static void *__vmalloc_node(unsigned long size, unsigned long align,
+                           gfp_t gfp_mask, pgprot_t prot,
+                           int node, void *caller)
+{
+       return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
+                               gfp_mask, prot, node, caller);
 }
 
 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
 {
-       return __vmalloc_node(size, gfp_mask, prot, -1,
+       return __vmalloc_node(size, 1, gfp_mask, prot, -1,
                                __builtin_return_address(0));
 }
 EXPORT_SYMBOL(__vmalloc);
 
+static inline void *__vmalloc_node_flags(unsigned long size,
+                                       int node, gfp_t flags)
+{
+       return __vmalloc_node(size, 1, flags, PAGE_KERNEL,
+                                       node, __builtin_return_address(0));
+}
+
 /**
  *     vmalloc  -  allocate virtually contiguous memory
  *     @size:          allocation size
@@ -1490,11 +1665,27 @@ EXPORT_SYMBOL(__vmalloc);
  */
 void *vmalloc(unsigned long size)
 {
-       return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
-                                       -1, __builtin_return_address(0));
+       return __vmalloc_node_flags(size, -1, GFP_KERNEL | __GFP_HIGHMEM);
 }
 EXPORT_SYMBOL(vmalloc);
 
+/**
+ *     vzalloc - allocate virtually contiguous memory with zero fill
+ *     @size:  allocation size
+ *     Allocate enough pages to cover @size from the page level
+ *     allocator and map them into contiguous kernel virtual space.
+ *     The memory allocated is set to zero.
+ *
+ *     For tight control over page level allocator and protection flags
+ *     use __vmalloc() instead.
+ */
+void *vzalloc(unsigned long size)
+{
+       return __vmalloc_node_flags(size, -1,
+                               GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
+}
+EXPORT_SYMBOL(vzalloc);
+
 /**
  * vmalloc_user - allocate zeroed virtually contiguous memory for userspace
  * @size: allocation size
@@ -1507,7 +1698,8 @@ void *vmalloc_user(unsigned long size)
        struct vm_struct *area;
        void *ret;
 
-       ret = __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
+       ret = __vmalloc_node(size, SHMLBA,
+                            GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
                             PAGE_KERNEL, -1, __builtin_return_address(0));
        if (ret) {
                area = find_vm_area(ret);
@@ -1530,11 +1722,30 @@ EXPORT_SYMBOL(vmalloc_user);
  */
 void *vmalloc_node(unsigned long size, int node)
 {
-       return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
+       return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL,
                                        node, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(vmalloc_node);
 
+/**
+ * vzalloc_node - allocate memory on a specific node with zero fill
+ * @size:      allocation size
+ * @node:      numa node
+ *
+ * Allocate enough pages to cover @size from the page level
+ * allocator and map them into contiguous kernel virtual space.
+ * The memory allocated is set to zero.
+ *
+ * For tight control over page level allocator and protection flags
+ * use __vmalloc_node() instead.
+ */
+void *vzalloc_node(unsigned long size, int node)
+{
+       return __vmalloc_node_flags(size, node,
+                        GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
+}
+EXPORT_SYMBOL(vzalloc_node);
+
 #ifndef PAGE_KERNEL_EXEC
 # define PAGE_KERNEL_EXEC PAGE_KERNEL
 #endif
@@ -1553,7 +1764,7 @@ EXPORT_SYMBOL(vmalloc_node);
 
 void *vmalloc_exec(unsigned long size)
 {
-       return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
+       return __vmalloc_node(size, 1, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL_EXEC,
                              -1, __builtin_return_address(0));
 }
 
@@ -1574,7 +1785,7 @@ void *vmalloc_exec(unsigned long size)
  */
 void *vmalloc_32(unsigned long size)
 {
-       return __vmalloc_node(size, GFP_VMALLOC32, PAGE_KERNEL,
+       return __vmalloc_node(size, 1, GFP_VMALLOC32, PAGE_KERNEL,
                              -1, __builtin_return_address(0));
 }
 EXPORT_SYMBOL(vmalloc_32);
@@ -1591,7 +1802,7 @@ void *vmalloc_32_user(unsigned long size)
        struct vm_struct *area;
        void *ret;
 
-       ret = __vmalloc_node(size, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
+       ret = __vmalloc_node(size, 1, GFP_VMALLOC32 | __GFP_ZERO, PAGE_KERNEL,
                             -1, __builtin_return_address(0));
        if (ret) {
                area = find_vm_area(ret);
@@ -1601,10 +1812,120 @@ void *vmalloc_32_user(unsigned long size)
 }
 EXPORT_SYMBOL(vmalloc_32_user);
 
+/*
+ * small helper routine , copy contents to buf from addr.
+ * If the page is not present, fill zero.
+ */
+
+static int aligned_vread(char *buf, char *addr, unsigned long count)
+{
+       struct page *p;
+       int copied = 0;
+
+       while (count) {
+               unsigned long offset, length;
+
+               offset = (unsigned long)addr & ~PAGE_MASK;
+               length = PAGE_SIZE - offset;
+               if (length > count)
+                       length = count;
+               p = vmalloc_to_page(addr);
+               /*
+                * To do safe access to this _mapped_ area, we need
+                * lock. But adding lock here means that we need to add
+                * overhead of vmalloc()/vfree() calles for this _debug_
+                * interface, rarely used. Instead of that, we'll use
+                * kmap() and get small overhead in this access function.
+                */
+               if (p) {
+                       /*
+                        * we can expect USER0 is not used (see vread/vwrite's
+                        * function description)
+                        */
+                       void *map = kmap_atomic(p, KM_USER0);
+                       memcpy(buf, map + offset, length);
+                       kunmap_atomic(map, KM_USER0);
+               } else
+                       memset(buf, 0, length);
+
+               addr += length;
+               buf += length;
+               copied += length;
+               count -= length;
+       }
+       return copied;
+}
+
+static int aligned_vwrite(char *buf, char *addr, unsigned long count)
+{
+       struct page *p;
+       int copied = 0;
+
+       while (count) {
+               unsigned long offset, length;
+
+               offset = (unsigned long)addr & ~PAGE_MASK;
+               length = PAGE_SIZE - offset;
+               if (length > count)
+                       length = count;
+               p = vmalloc_to_page(addr);
+               /*
+                * To do safe access to this _mapped_ area, we need
+                * lock. But adding lock here means that we need to add
+                * overhead of vmalloc()/vfree() calles for this _debug_
+                * interface, rarely used. Instead of that, we'll use
+                * kmap() and get small overhead in this access function.
+                */
+               if (p) {
+                       /*
+                        * we can expect USER0 is not used (see vread/vwrite's
+                        * function description)
+                        */
+                       void *map = kmap_atomic(p, KM_USER0);
+                       memcpy(map + offset, buf, length);
+                       kunmap_atomic(map, KM_USER0);
+               }
+               addr += length;
+               buf += length;
+               copied += length;
+               count -= length;
+       }
+       return copied;
+}
+
+/**
+ *     vread() -  read vmalloc area in a safe way.
+ *     @buf:           buffer for reading data
+ *     @addr:          vm address.
+ *     @count:         number of bytes to be read.
+ *
+ *     Returns # of bytes which addr and buf should be increased.
+ *     (same number to @count). Returns 0 if [addr...addr+count) doesn't
+ *     includes any intersect with alive vmalloc area.
+ *
+ *     This function checks that addr is a valid vmalloc'ed area, and
+ *     copy data from that area to a given buffer. If the given memory range
+ *     of [addr...addr+count) includes some valid address, data is copied to
+ *     proper area of @buf. If there are memory holes, they'll be zero-filled.
+ *     IOREMAP area is treated as memory hole and no copy is done.
+ *
+ *     If [addr...addr+count) doesn't includes any intersects with alive
+ *     vm_struct area, returns 0.
+ *     @buf should be kernel's buffer. Because this function uses KM_USER0,
+ *     the caller should guarantee KM_USER0 is not used.
+ *
+ *     Note: In usual ops, vread() is never necessary because the caller
+ *     should know vmalloc() area is valid and can use memcpy().
+ *     This is for routines which have to access vmalloc area without
+ *     any informaion, as /dev/kmem.
+ *
+ */
+
 long vread(char *buf, char *addr, unsigned long count)
 {
        struct vm_struct *tmp;
        char *vaddr, *buf_start = buf;
+       unsigned long buflen = count;
        unsigned long n;
 
        /* Don't allow overflow */
@@ -1612,7 +1933,7 @@ long vread(char *buf, char *addr, unsigned long count)
                count = -(unsigned long) addr;
 
        read_lock(&vmlist_lock);
-       for (tmp = vmlist; tmp; tmp = tmp->next) {
+       for (tmp = vmlist; count && tmp; tmp = tmp->next) {
                vaddr = (char *) tmp->addr;
                if (addr >= vaddr + tmp->size - PAGE_SIZE)
                        continue;
@@ -1625,32 +1946,70 @@ long vread(char *buf, char *addr, unsigned long count)
                        count--;
                }
                n = vaddr + tmp->size - PAGE_SIZE - addr;
-               do {
-                       if (count == 0)
-                               goto finished;
-                       *buf = *addr;
-                       buf++;
-                       addr++;
-                       count--;
-               } while (--n > 0);
+               if (n > count)
+                       n = count;
+               if (!(tmp->flags & VM_IOREMAP))
+                       aligned_vread(buf, addr, n);
+               else /* IOREMAP area is treated as memory hole */
+                       memset(buf, 0, n);
+               buf += n;
+               addr += n;
+               count -= n;
        }
 finished:
        read_unlock(&vmlist_lock);
-       return buf - buf_start;
+
+       if (buf == buf_start)
+               return 0;
+       /* zero-fill memory holes */
+       if (buf != buf_start + buflen)
+               memset(buf, 0, buflen - (buf - buf_start));
+
+       return buflen;
 }
 
+/**
+ *     vwrite() -  write vmalloc area in a safe way.
+ *     @buf:           buffer for source data
+ *     @addr:          vm address.
+ *     @count:         number of bytes to be read.
+ *
+ *     Returns # of bytes which addr and buf should be incresed.
+ *     (same number to @count).
+ *     If [addr...addr+count) doesn't includes any intersect with valid
+ *     vmalloc area, returns 0.
+ *
+ *     This function checks that addr is a valid vmalloc'ed area, and
+ *     copy data from a buffer to the given addr. If specified range of
+ *     [addr...addr+count) includes some valid address, data is copied from
+ *     proper area of @buf. If there are memory holes, no copy to hole.
+ *     IOREMAP area is treated as memory hole and no copy is done.
+ *
+ *     If [addr...addr+count) doesn't includes any intersects with alive
+ *     vm_struct area, returns 0.
+ *     @buf should be kernel's buffer. Because this function uses KM_USER0,
+ *     the caller should guarantee KM_USER0 is not used.
+ *
+ *     Note: In usual ops, vwrite() is never necessary because the caller
+ *     should know vmalloc() area is valid and can use memcpy().
+ *     This is for routines which have to access vmalloc area without
+ *     any informaion, as /dev/kmem.
+ */
+
 long vwrite(char *buf, char *addr, unsigned long count)
 {
        struct vm_struct *tmp;
-       char *vaddr, *buf_start = buf;
-       unsigned long n;
+       char *vaddr;
+       unsigned long n, buflen;
+       int copied = 0;
 
        /* Don't allow overflow */
        if ((unsigned long) addr + count < count)
                count = -(unsigned long) addr;
+       buflen = count;
 
        read_lock(&vmlist_lock);
-       for (tmp = vmlist; tmp; tmp = tmp->next) {
+       for (tmp = vmlist; count && tmp; tmp = tmp->next) {
                vaddr = (char *) tmp->addr;
                if (addr >= vaddr + tmp->size - PAGE_SIZE)
                        continue;
@@ -1662,18 +2021,21 @@ long vwrite(char *buf, char *addr, unsigned long count)
                        count--;
                }
                n = vaddr + tmp->size - PAGE_SIZE - addr;
-               do {
-                       if (count == 0)
-                               goto finished;
-                       *addr = *buf;
-                       buf++;
-                       addr++;
-                       count--;
-               } while (--n > 0);
+               if (n > count)
+                       n = count;
+               if (!(tmp->flags & VM_IOREMAP)) {
+                       aligned_vwrite(buf, addr, n);
+                       copied++;
+               }
+               buf += n;
+               addr += n;
+               count -= n;
        }
 finished:
        read_unlock(&vmlist_lock);
-       return buf - buf_start;
+       if (!copied)
+               return 0;
+       return buflen;
 }
 
 /**
@@ -1777,10 +2139,6 @@ struct vm_struct *alloc_vm_area(size_t size)
                return NULL;
        }
 
-       /* Make sure the pagetables are constructed in process kernel
-          mappings */
-       vmalloc_sync_all();
-
        return area;
 }
 EXPORT_SYMBOL_GPL(alloc_vm_area);
@@ -1794,9 +2152,289 @@ void free_vm_area(struct vm_struct *area)
 }
 EXPORT_SYMBOL_GPL(free_vm_area);
 
+#ifdef CONFIG_SMP
+static struct vmap_area *node_to_va(struct rb_node *n)
+{
+       return n ? rb_entry(n, struct vmap_area, rb_node) : NULL;
+}
+
+/**
+ * pvm_find_next_prev - find the next and prev vmap_area surrounding @end
+ * @end: target address
+ * @pnext: out arg for the next vmap_area
+ * @pprev: out arg for the previous vmap_area
+ *
+ * Returns: %true if either or both of next and prev are found,
+ *         %false if no vmap_area exists
+ *
+ * Find vmap_areas end addresses of which enclose @end.  ie. if not
+ * NULL, *pnext->va_end > @end and *pprev->va_end <= @end.
+ */
+static bool pvm_find_next_prev(unsigned long end,
+                              struct vmap_area **pnext,
+                              struct vmap_area **pprev)
+{
+       struct rb_node *n = vmap_area_root.rb_node;
+       struct vmap_area *va = NULL;
+
+       while (n) {
+               va = rb_entry(n, struct vmap_area, rb_node);
+               if (end < va->va_end)
+                       n = n->rb_left;
+               else if (end > va->va_end)
+                       n = n->rb_right;
+               else
+                       break;
+       }
+
+       if (!va)
+               return false;
+
+       if (va->va_end > end) {
+               *pnext = va;
+               *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
+       } else {
+               *pprev = va;
+               *pnext = node_to_va(rb_next(&(*pprev)->rb_node));
+       }
+       return true;
+}
+
+/**
+ * pvm_determine_end - find the highest aligned address between two vmap_areas
+ * @pnext: in/out arg for the next vmap_area
+ * @pprev: in/out arg for the previous vmap_area
+ * @align: alignment
+ *
+ * Returns: determined end address
+ *
+ * Find the highest aligned address between *@pnext and *@pprev below
+ * VMALLOC_END.  *@pnext and *@pprev are adjusted so that the aligned
+ * down address is between the end addresses of the two vmap_areas.
+ *
+ * Please note that the address returned by this function may fall
+ * inside *@pnext vmap_area.  The caller is responsible for checking
+ * that.
+ */
+static unsigned long pvm_determine_end(struct vmap_area **pnext,
+                                      struct vmap_area **pprev,
+                                      unsigned long align)
+{
+       const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
+       unsigned long addr;
+
+       if (*pnext)
+               addr = min((*pnext)->va_start & ~(align - 1), vmalloc_end);
+       else
+               addr = vmalloc_end;
+
+       while (*pprev && (*pprev)->va_end > addr) {
+               *pnext = *pprev;
+               *pprev = node_to_va(rb_prev(&(*pnext)->rb_node));
+       }
+
+       return addr;
+}
+
+/**
+ * pcpu_get_vm_areas - allocate vmalloc areas for percpu allocator
+ * @offsets: array containing offset of each area
+ * @sizes: array containing size of each area
+ * @nr_vms: the number of areas to allocate
+ * @align: alignment, all entries in @offsets and @sizes must be aligned to this
+ *
+ * Returns: kmalloc'd vm_struct pointer array pointing to allocated
+ *         vm_structs on success, %NULL on failure
+ *
+ * Percpu allocator wants to use congruent vm areas so that it can
+ * maintain the offsets among percpu areas.  This function allocates
+ * congruent vmalloc areas for it with GFP_KERNEL.  These areas tend to
+ * be scattered pretty far, distance between two areas easily going up
+ * to gigabytes.  To avoid interacting with regular vmallocs, these
+ * areas are allocated from top.
+ *
+ * Despite its complicated look, this allocator is rather simple.  It
+ * does everything top-down and scans areas from the end looking for
+ * matching slot.  While scanning, if any of the areas overlaps with
+ * existing vmap_area, the base address is pulled down to fit the
+ * area.  Scanning is repeated till all the areas fit and then all
+ * necessary data structres are inserted and the result is returned.
+ */
+struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets,
+                                    const size_t *sizes, int nr_vms,
+                                    size_t align)
+{
+       const unsigned long vmalloc_start = ALIGN(VMALLOC_START, align);
+       const unsigned long vmalloc_end = VMALLOC_END & ~(align - 1);
+       struct vmap_area **vas, *prev, *next;
+       struct vm_struct **vms;
+       int area, area2, last_area, term_area;
+       unsigned long base, start, end, last_end;
+       bool purged = false;
+
+       /* verify parameters and allocate data structures */
+       BUG_ON(align & ~PAGE_MASK || !is_power_of_2(align));
+       for (last_area = 0, area = 0; area < nr_vms; area++) {
+               start = offsets[area];
+               end = start + sizes[area];
+
+               /* is everything aligned properly? */
+               BUG_ON(!IS_ALIGNED(offsets[area], align));
+               BUG_ON(!IS_ALIGNED(sizes[area], align));
+
+               /* detect the area with the highest address */
+               if (start > offsets[last_area])
+                       last_area = area;
+
+               for (area2 = 0; area2 < nr_vms; area2++) {
+                       unsigned long start2 = offsets[area2];
+                       unsigned long end2 = start2 + sizes[area2];
+
+                       if (area2 == area)
+                               continue;
+
+                       BUG_ON(start2 >= start && start2 < end);
+                       BUG_ON(end2 <= end && end2 > start);
+               }
+       }
+       last_end = offsets[last_area] + sizes[last_area];
+
+       if (vmalloc_end - vmalloc_start < last_end) {
+               WARN_ON(true);
+               return NULL;
+       }
+
+       vms = kzalloc(sizeof(vms[0]) * nr_vms, GFP_KERNEL);
+       vas = kzalloc(sizeof(vas[0]) * nr_vms, GFP_KERNEL);
+       if (!vas || !vms)
+               goto err_free;
+
+       for (area = 0; area < nr_vms; area++) {
+               vas[area] = kzalloc(sizeof(struct vmap_area), GFP_KERNEL);
+               vms[area] = kzalloc(sizeof(struct vm_struct), GFP_KERNEL);
+               if (!vas[area] || !vms[area])
+                       goto err_free;
+       }
+retry:
+       spin_lock(&vmap_area_lock);
+
+       /* start scanning - we scan from the top, begin with the last area */
+       area = term_area = last_area;
+       start = offsets[area];
+       end = start + sizes[area];
+
+       if (!pvm_find_next_prev(vmap_area_pcpu_hole, &next, &prev)) {
+               base = vmalloc_end - last_end;
+               goto found;
+       }
+       base = pvm_determine_end(&next, &prev, align) - end;
+
+       while (true) {
+               BUG_ON(next && next->va_end <= base + end);
+               BUG_ON(prev && prev->va_end > base + end);
+
+               /*
+                * base might have underflowed, add last_end before
+                * comparing.
+                */
+               if (base + last_end < vmalloc_start + last_end) {
+                       spin_unlock(&vmap_area_lock);
+                       if (!purged) {
+                               purge_vmap_area_lazy();
+                               purged = true;
+                               goto retry;
+                       }
+                       goto err_free;
+               }
+
+               /*
+                * If next overlaps, move base downwards so that it's
+                * right below next and then recheck.
+                */
+               if (next && next->va_start < base + end) {
+                       base = pvm_determine_end(&next, &prev, align) - end;
+                       term_area = area;
+                       continue;
+               }
+
+               /*
+                * If prev overlaps, shift down next and prev and move
+                * base so that it's right below new next and then
+                * recheck.
+                */
+               if (prev && prev->va_end > base + start)  {
+                       next = prev;
+                       prev = node_to_va(rb_prev(&next->rb_node));
+                       base = pvm_determine_end(&next, &prev, align) - end;
+                       term_area = area;
+                       continue;
+               }
+
+               /*
+                * This area fits, move on to the previous one.  If
+                * the previous one is the terminal one, we're done.
+                */
+               area = (area + nr_vms - 1) % nr_vms;
+               if (area == term_area)
+                       break;
+               start = offsets[area];
+               end = start + sizes[area];
+               pvm_find_next_prev(base + end, &next, &prev);
+       }
+found:
+       /* we've found a fitting base, insert all va's */
+       for (area = 0; area < nr_vms; area++) {
+               struct vmap_area *va = vas[area];
+
+               va->va_start = base + offsets[area];
+               va->va_end = va->va_start + sizes[area];
+               __insert_vmap_area(va);
+       }
+
+       vmap_area_pcpu_hole = base + offsets[last_area];
+
+       spin_unlock(&vmap_area_lock);
+
+       /* insert all vm's */
+       for (area = 0; area < nr_vms; area++)
+               insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
+                                 pcpu_get_vm_areas);
+
+       kfree(vas);
+       return vms;
+
+err_free:
+       for (area = 0; area < nr_vms; area++) {
+               if (vas)
+                       kfree(vas[area]);
+               if (vms)
+                       kfree(vms[area]);
+       }
+       kfree(vas);
+       kfree(vms);
+       return NULL;
+}
+
+/**
+ * pcpu_free_vm_areas - free vmalloc areas for percpu allocator
+ * @vms: vm_struct pointer array returned by pcpu_get_vm_areas()
+ * @nr_vms: the number of allocated areas
+ *
+ * Free vm_structs and the array allocated by pcpu_get_vm_areas().
+ */
+void pcpu_free_vm_areas(struct vm_struct **vms, int nr_vms)
+{
+       int i;
+
+       for (i = 0; i < nr_vms; i++)
+               free_vm_area(vms[i]);
+       kfree(vms);
+}
+#endif /* CONFIG_SMP */
 
 #ifdef CONFIG_PROC_FS
 static void *s_start(struct seq_file *m, loff_t *pos)
+       __acquires(&vmlist_lock)
 {
        loff_t n = *pos;
        struct vm_struct *v;
@@ -1823,6 +2461,7 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos)
 }
 
 static void s_stop(struct seq_file *m, void *p)
+       __releases(&vmlist_lock)
 {
        read_unlock(&vmlist_lock);
 }
@@ -1853,19 +2492,14 @@ static int s_show(struct seq_file *m, void *p)
        seq_printf(m, "0x%p-0x%p %7ld",
                v->addr, v->addr + v->size, v->size);
 
-       if (v->caller) {
-               char buff[KSYM_SYMBOL_LEN];
-
-               seq_putc(m, ' ');
-               sprint_symbol(buff, (unsigned long)v->caller);
-               seq_puts(m, buff);
-       }
+       if (v->caller)
+               seq_printf(m, " %pS", v->caller);
 
        if (v->nr_pages)
                seq_printf(m, " pages=%d", v->nr_pages);
 
        if (v->phys_addr)
-               seq_printf(m, " phys=%lx", v->phys_addr);
+               seq_printf(m, " phys=%llx", (unsigned long long)v->phys_addr);
 
        if (v->flags & VM_IOREMAP)
                seq_printf(m, " ioremap");
@@ -1899,8 +2533,11 @@ static int vmalloc_open(struct inode *inode, struct file *file)
        unsigned int *ptr = NULL;
        int ret;
 
-       if (NUMA_BUILD)
+       if (NUMA_BUILD) {
                ptr = kmalloc(nr_node_ids * sizeof(unsigned int), GFP_KERNEL);
+               if (ptr == NULL)
+                       return -ENOMEM;
+       }
        ret = seq_open(file, &vmalloc_op);
        if (!ret) {
                struct seq_file *m = file->private_data;