OSDN Git Service

Merge 4.4.152 into android-4.4-p
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / staging / android / ion / ion.c
index 374f840..0d8fd8d 100644 (file)
@@ -15,6 +15,7 @@
  *
  */
 
+#include <linux/atomic.h>
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/file.h>
@@ -387,6 +388,16 @@ static void ion_handle_get(struct ion_handle *handle)
        kref_get(&handle->ref);
 }
 
+/* Must hold the client lock */
+static struct ion_handle *ion_handle_get_check_overflow(
+                                       struct ion_handle *handle)
+{
+       if (atomic_read(&handle->ref.refcount) + 1 == 0)
+               return ERR_PTR(-EOVERFLOW);
+       ion_handle_get(handle);
+       return handle;
+}
+
 static int ion_handle_put_nolock(struct ion_handle *handle)
 {
        int ret;
@@ -433,9 +444,9 @@ static struct ion_handle *ion_handle_get_by_id_nolock(struct ion_client *client,
 
        handle = idr_find(&client->idr, id);
        if (handle)
-               ion_handle_get(handle);
+               return ion_handle_get_check_overflow(handle);
 
-       return handle ? handle : ERR_PTR(-EINVAL);
+       return ERR_PTR(-EINVAL);
 }
 
 struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
@@ -856,7 +867,6 @@ void ion_client_destroy(struct ion_client *client)
        struct ion_device *dev = client->dev;
        struct rb_node *n;
 
-       pr_debug("%s: %d\n", __func__, __LINE__);
        while ((n = rb_first(&client->handles))) {
                struct ion_handle *handle = rb_entry(n, struct ion_handle,
                                                     node);
@@ -947,9 +957,6 @@ static void ion_buffer_sync_for_device(struct ion_buffer *buffer,
        int pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
        int i;
 
-       pr_debug("%s: syncing for device %s\n", __func__,
-                dev ? dev_name(dev) : "null");
-
        if (!ion_buffer_fault_user_mappings(buffer))
                return;
 
@@ -1003,7 +1010,6 @@ static void ion_vm_open(struct vm_area_struct *vma)
        mutex_lock(&buffer->lock);
        list_add(&vma_list->list, &buffer->vmas);
        mutex_unlock(&buffer->lock);
-       pr_debug("%s: adding %p\n", __func__, vma);
 }
 
 static void ion_vm_close(struct vm_area_struct *vma)
@@ -1011,14 +1017,12 @@ static void ion_vm_close(struct vm_area_struct *vma)
        struct ion_buffer *buffer = vma->vm_private_data;
        struct ion_vma_list *vma_list, *tmp;
 
-       pr_debug("%s\n", __func__);
        mutex_lock(&buffer->lock);
        list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) {
                if (vma_list->vma != vma)
                        continue;
                list_del(&vma_list->list);
                kfree(vma_list);
-               pr_debug("%s: deleting %p\n", __func__, vma);
                break;
        }
        mutex_unlock(&buffer->lock);
@@ -1202,7 +1206,7 @@ struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
        /* if a handle exists for this buffer just take a reference to it */
        handle = ion_handle_lookup(client, buffer);
        if (!IS_ERR(handle)) {
-               ion_handle_get(handle);
+               handle = ion_handle_get_check_overflow(handle);
                mutex_unlock(&client->lock);
                goto end;
        }
@@ -1375,7 +1379,6 @@ static int ion_release(struct inode *inode, struct file *file)
 {
        struct ion_client *client = file->private_data;
 
-       pr_debug("%s: %d\n", __func__, __LINE__);
        ion_client_destroy(client);
        return 0;
 }
@@ -1387,7 +1390,6 @@ static int ion_open(struct inode *inode, struct file *file)
        struct ion_client *client;
        char debug_name[64];
 
-       pr_debug("%s: %d\n", __func__, __LINE__);
        snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
        client = ion_client_create(dev, debug_name);
        if (IS_ERR(client))