ipc,msg: make msgctl_nolock lockless
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / vmalloc.c
index d365724..13a5495 100644 (file)
@@ -292,7 +292,7 @@ static struct vmap_area *__find_vmap_area(unsigned long addr)
                va = rb_entry(n, struct vmap_area, rb_node);
                if (addr < va->va_start)
                        n = n->rb_left;
-               else if (addr > va->va_start)
+               else if (addr >= va->va_end)
                        n = n->rb_right;
                else
                        return va;
@@ -388,12 +388,12 @@ nocache:
                addr = ALIGN(first->va_end, align);
                if (addr < vstart)
                        goto nocache;
-               if (addr + size - 1 < addr)
+               if (addr + size < addr)
                        goto overflow;
 
        } else {
                addr = ALIGN(vstart, align);
-               if (addr + size - 1 < addr)
+               if (addr + size < addr)
                        goto overflow;
 
                n = vmap_area_root.rb_node;
@@ -420,7 +420,7 @@ nocache:
                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)
+               if (addr + size < addr)
                        goto overflow;
 
                if (list_is_last(&first->list, &vmap_area_list))
@@ -754,7 +754,6 @@ struct vmap_block {
        struct vmap_area *va;
        struct vmap_block_queue *vbq;
        unsigned long free, dirty;
-       DECLARE_BITMAP(alloc_map, VMAP_BBMAP_BITS);
        DECLARE_BITMAP(dirty_map, VMAP_BBMAP_BITS);
        struct list_head free_list;
        struct rcu_head rcu_head;
@@ -820,7 +819,6 @@ static struct vmap_block *new_vmap_block(gfp_t gfp_mask)
        vb->va = va;
        vb->free = VMAP_BBMAP_BITS;
        vb->dirty = 0;
-       bitmap_zero(vb->alloc_map, VMAP_BBMAP_BITS);
        bitmap_zero(vb->dirty_map, VMAP_BBMAP_BITS);
        INIT_LIST_HEAD(&vb->free_list);
 
@@ -873,7 +871,6 @@ static void purge_fragmented_blocks(int cpu)
                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);
@@ -891,11 +888,6 @@ static void purge_fragmented_blocks(int cpu)
        }
 }
 
-static void purge_fragmented_blocks_thiscpu(void)
-{
-       purge_fragmented_blocks(smp_processor_id());
-}
-
 static void purge_fragmented_blocks_allcpus(void)
 {
        int cpu;
@@ -910,7 +902,6 @@ 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);
@@ -934,17 +925,7 @@ again:
                if (vb->free < 1UL << order)
                        goto next;
 
-               i = bitmap_find_free_region(vb->alloc_map,
-                                               VMAP_BBMAP_BITS, order);
-
-               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;
-                       }
-                       goto next;
-               }
+               i = VMAP_BBMAP_BITS - vb->free;
                addr = vb->va->va_start + (i << PAGE_SHIFT);
                BUG_ON(addr_to_vb_idx(addr) !=
                                addr_to_vb_idx(vb->va->va_start));
@@ -960,9 +941,6 @@ next:
                spin_unlock(&vb->lock);
        }
 
-       if (purge)
-               purge_fragmented_blocks_thiscpu();
-
        put_cpu_var(vmap_block_queue);
        rcu_read_unlock();
 
@@ -1311,22 +1289,15 @@ static void setup_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
        spin_unlock(&vmap_area_lock);
 }
 
-static void clear_vm_unlist(struct vm_struct *vm)
+static void clear_vm_uninitialized_flag(struct vm_struct *vm)
 {
        /*
-        * Before removing VM_UNLIST,
+        * Before removing VM_UNINITIALIZED,
         * we should make sure that vm has proper values.
         * Pair with smp_rmb() in show_numa_info().
         */
        smp_wmb();
-       vm->flags &= ~VM_UNLIST;
-}
-
-static void insert_vmalloc_vm(struct vm_struct *vm, struct vmap_area *va,
-                             unsigned long flags, const void *caller)
-{
-       setup_vmalloc_vm(vm, va, flags, caller);
-       clear_vm_unlist(vm);
+       vm->flags &= ~VM_UNINITIALIZED;
 }
 
 static struct vm_struct *__get_vm_area_node(unsigned long size,
@@ -1337,16 +1308,8 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
        struct vm_struct *area;
 
        BUG_ON(in_interrupt());
-       if (flags & VM_IOREMAP) {
-               int bit = fls(size);
-
-               if (bit > IOREMAP_MAX_ORDER)
-                       bit = IOREMAP_MAX_ORDER;
-               else if (bit < PAGE_SHIFT)
-                       bit = PAGE_SHIFT;
-
-               align = 1ul << bit;
-       }
+       if (flags & VM_IOREMAP)
+               align = 1ul << clamp(fls(size), PAGE_SHIFT, IOREMAP_MAX_ORDER);
 
        size = PAGE_ALIGN(size);
        if (unlikely(!size))
@@ -1367,16 +1330,7 @@ static struct vm_struct *__get_vm_area_node(unsigned long size,
                return NULL;
        }
 
-       /*
-        * When this function is called from __vmalloc_node_range,
-        * we add VM_UNLIST flag to avoid accessing uninitialized
-        * members of vm_struct such as pages and nr_pages fields.
-        * They will be set later.
-        */
-       if (flags & VM_UNLIST)
-               setup_vmalloc_vm(area, va, flags, caller);
-       else
-               insert_vmalloc_vm(area, va, flags, caller);
+       setup_vmalloc_vm(area, va, flags, caller);
 
        return area;
 }
@@ -1476,10 +1430,9 @@ static void __vunmap(const void *addr, int deallocate_pages)
        if (!addr)
                return;
 
-       if ((PAGE_SIZE-1) & (unsigned long)addr) {
-               WARN(1, KERN_ERR "Trying to vfree() bad address (%p)\n", addr);
+       if (WARN(!PAGE_ALIGNED(addr), "Trying to vfree() bad address (%p)\n",
+                       addr))
                return;
-       }
 
        area = remove_vm_area(addr);
        if (unlikely(!area)) {
@@ -1524,7 +1477,6 @@ static void __vunmap(const void *addr, int deallocate_pages)
  *     conventions for vfree() arch-depenedent would be a really bad idea)
  *
  *     NOTE: assumes that the object at *addr has a size >= sizeof(llist_node)
- *     
  */
 void vfree(const void *addr)
 {
@@ -1536,8 +1488,8 @@ void vfree(const void *addr)
                return;
        if (unlikely(in_interrupt())) {
                struct vfree_deferred *p = &__get_cpu_var(vfree_deferred);
-               llist_add((struct llist_node *)addr, &p->list);
-               schedule_work(&p->wq);
+               if (llist_add((struct llist_node *)addr, &p->list))
+                       schedule_work(&p->wq);
        } else
                __vunmap(addr, 1);
 }
@@ -1682,21 +1634,21 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,
        if (!size || (size >> PAGE_SHIFT) > totalram_pages)
                goto fail;
 
-       area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNLIST,
+       area = __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED,
                                  start, end, node, gfp_mask, caller);
        if (!area)
                goto fail;
 
        addr = __vmalloc_area_node(area, gfp_mask, prot, node, caller);
        if (!addr)
-               return NULL;
+               goto fail;
 
        /*
-        * In this function, newly allocated vm_struct has VM_UNLIST flag.
-        * It means that vm_struct is not fully initialized.
+        * In this function, newly allocated vm_struct has VM_UNINITIALIZED
+        * flag. It means that vm_struct is not fully initialized.
         * Now, it is fully initialized, so remove this flag here.
         */
-       clear_vm_unlist(area);
+       clear_vm_uninitialized_flag(area);
 
        /*
         * A ref_count = 3 is needed because the vm_struct and vmap_area
@@ -2148,42 +2100,43 @@ finished:
 }
 
 /**
- *     remap_vmalloc_range  -  map vmalloc pages to userspace
- *     @vma:           vma to cover (map full range of vma)
- *     @addr:          vmalloc memory
- *     @pgoff:         number of pages into addr before first page to map
+ *     remap_vmalloc_range_partial  -  map vmalloc pages to userspace
+ *     @vma:           vma to cover
+ *     @uaddr:         target user address to start at
+ *     @kaddr:         virtual address of vmalloc kernel memory
+ *     @size:          size of map area
  *
  *     Returns:        0 for success, -Exxx on failure
  *
- *     This function checks that addr is a valid vmalloc'ed area, and
- *     that it is big enough to cover the vma. Will return failure if
- *     that criteria isn't met.
+ *     This function checks that @kaddr is a valid vmalloc'ed area,
+ *     and that it is big enough to cover the range starting at
+ *     @uaddr in @vma. Will return failure if that criteria isn't
+ *     met.
  *
  *     Similar to remap_pfn_range() (see mm/memory.c)
  */
-int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
-                                               unsigned long pgoff)
+int remap_vmalloc_range_partial(struct vm_area_struct *vma, unsigned long uaddr,
+                               void *kaddr, unsigned long size)
 {
        struct vm_struct *area;
-       unsigned long uaddr = vma->vm_start;
-       unsigned long usize = vma->vm_end - vma->vm_start;
 
-       if ((PAGE_SIZE-1) & (unsigned long)addr)
+       size = PAGE_ALIGN(size);
+
+       if (!PAGE_ALIGNED(uaddr) || !PAGE_ALIGNED(kaddr))
                return -EINVAL;
 
-       area = find_vm_area(addr);
+       area = find_vm_area(kaddr);
        if (!area)
                return -EINVAL;
 
        if (!(area->flags & VM_USERMAP))
                return -EINVAL;
 
-       if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
+       if (kaddr + size > area->addr + area->size)
                return -EINVAL;
 
-       addr += pgoff << PAGE_SHIFT;
        do {
-               struct page *page = vmalloc_to_page(addr);
+               struct page *page = vmalloc_to_page(kaddr);
                int ret;
 
                ret = vm_insert_page(vma, uaddr, page);
@@ -2191,14 +2144,37 @@ int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
                        return ret;
 
                uaddr += PAGE_SIZE;
-               addr += PAGE_SIZE;
-               usize -= PAGE_SIZE;
-       } while (usize > 0);
+               kaddr += PAGE_SIZE;
+               size -= PAGE_SIZE;
+       } while (size > 0);
 
        vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
 
        return 0;
 }
+EXPORT_SYMBOL(remap_vmalloc_range_partial);
+
+/**
+ *     remap_vmalloc_range  -  map vmalloc pages to userspace
+ *     @vma:           vma to cover (map full range of vma)
+ *     @addr:          vmalloc memory
+ *     @pgoff:         number of pages into addr before first page to map
+ *
+ *     Returns:        0 for success, -Exxx on failure
+ *
+ *     This function checks that addr is a valid vmalloc'ed area, and
+ *     that it is big enough to cover the vma. Will return failure if
+ *     that criteria isn't met.
+ *
+ *     Similar to remap_pfn_range() (see mm/memory.c)
+ */
+int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
+                                               unsigned long pgoff)
+{
+       return remap_vmalloc_range_partial(vma, vma->vm_start,
+                                          addr + (pgoff << PAGE_SHIFT),
+                                          vma->vm_end - vma->vm_start);
+}
 EXPORT_SYMBOL(remap_vmalloc_range);
 
 /*
@@ -2512,8 +2488,8 @@ found:
 
        /* insert all vm's */
        for (area = 0; area < nr_vms; area++)
-               insert_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
-                                 pcpu_get_vm_areas);
+               setup_vmalloc_vm(vms[area], vas[area], VM_ALLOC,
+                                pcpu_get_vm_areas);
 
        kfree(vas);
        return vms;
@@ -2592,11 +2568,6 @@ static void show_numa_info(struct seq_file *m, struct vm_struct *v)
                if (!counters)
                        return;
 
-               /* Pair with smp_wmb() in clear_vm_unlist() */
-               smp_rmb();
-               if (v->flags & VM_UNLIST)
-                       return;
-
                memset(counters, 0, nr_node_ids * sizeof(unsigned int));
 
                for (nr = 0; nr < v->nr_pages; nr++)
@@ -2625,6 +2596,11 @@ static int s_show(struct seq_file *m, void *p)
 
        v = va->vm;
 
+       /* Pair with smp_wmb() in clear_vm_uninitialized_flag() */
+       smp_rmb();
+       if (v->flags & VM_UNINITIALIZED)
+               return 0;
+
        seq_printf(m, "0x%pK-0x%pK %7ld",
                v->addr, v->addr + v->size, v->size);