android: ion: remove the list of attachments
authorCho KyongHo <pullip.cho@samsung.com>
Mon, 12 Feb 2018 00:45:03 +0000 (09:45 +0900)
committerSangwook Ju <sw.ju@samsung.com>
Mon, 14 May 2018 10:45:23 +0000 (19:45 +0900)
dma_buf also has the list of attachments. ion_buffer.attachments is
redundant. So, let's remove it.
dma_buf.attachment should be accessed with dma_buf.lock held. Holding
dma_buf.lock in begin_cpu_access() and end_cpu_access() is okay. But
it should be handled with care to prevent possible deadlock.

Change-Id: I6a4a5c74c1030874fc08684c6c84485373b47919
Signed-off-by: Cho KyongHo <pullip.cho@samsung.com>
drivers/staging/android/ion/ion.c
drivers/staging/android/ion/ion.h

index 49e32f8d13c3fa26ecb949bf3ac6c9a9f9e184c5..dd6ec2648d676374b4e90c890adcd2b537d79cab 100644 (file)
@@ -115,7 +115,6 @@ static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
 
        buffer->dev = dev;
        buffer->size = len;
-       INIT_LIST_HEAD(&buffer->attachments);
        mutex_init(&buffer->lock);
        mutex_lock(&dev->buffer_lock);
        ion_buffer_add(dev, buffer);
@@ -212,38 +211,17 @@ static void free_duped_table(struct sg_table *table)
        kfree(table);
 }
 
-struct ion_dma_buf_attachment {
-       struct device *dev;
-       struct sg_table *table;
-       struct list_head list;
-};
-
 static int ion_dma_buf_attach(struct dma_buf *dmabuf, struct device *dev,
                              struct dma_buf_attachment *attachment)
 {
-       struct ion_dma_buf_attachment *a;
        struct sg_table *table;
        struct ion_buffer *buffer = dmabuf->priv;
 
-       a = kzalloc(sizeof(*a), GFP_KERNEL);
-       if (!a)
-               return -ENOMEM;
-
        table = dup_sg_table(buffer->sg_table);
-       if (IS_ERR(table)) {
-               kfree(a);
+       if (IS_ERR(table))
                return -ENOMEM;
-       }
 
-       a->table = table;
-       a->dev = dev;
-       INIT_LIST_HEAD(&a->list);
-
-       attachment->priv = a;
-
-       mutex_lock(&buffer->lock);
-       list_add(&a->list, &buffer->attachments);
-       mutex_unlock(&buffer->lock);
+       attachment->priv = table;
 
        return 0;
 }
@@ -251,24 +229,13 @@ static int ion_dma_buf_attach(struct dma_buf *dmabuf, struct device *dev,
 static void ion_dma_buf_detatch(struct dma_buf *dmabuf,
                                struct dma_buf_attachment *attachment)
 {
-       struct ion_dma_buf_attachment *a = attachment->priv;
-       struct ion_buffer *buffer = dmabuf->priv;
-
-       free_duped_table(a->table);
-       mutex_lock(&buffer->lock);
-       list_del(&a->list);
-       mutex_unlock(&buffer->lock);
-
-       kfree(a);
+       free_duped_table(attachment->priv);
 }
 
 static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
                                        enum dma_data_direction direction)
 {
-       struct ion_dma_buf_attachment *a = attachment->priv;
-       struct sg_table *table;
-
-       table = a->table;
+       struct sg_table *table = attachment->priv;
 
        if (!dma_map_sg(attachment->dev, table->sgl, table->nents,
                        direction))
@@ -370,7 +337,7 @@ static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
 {
        struct ion_buffer *buffer = dmabuf->priv;
        void *vaddr;
-       struct ion_dma_buf_attachment *a;
+       struct dma_buf_attachment *att;
 
        /*
         * TODO: Move this elsewhere because we don't always need a vaddr
@@ -381,12 +348,14 @@ static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
                mutex_unlock(&buffer->lock);
        }
 
-       mutex_lock(&buffer->lock);
-       list_for_each_entry(a, &buffer->attachments, list) {
-               dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents,
+       mutex_lock(&dmabuf->lock);
+       list_for_each_entry(att, &dmabuf->attachments, node) {
+               struct sg_table *table = att->priv;
+
+               dma_sync_sg_for_cpu(att->dev, table->sgl, table->nents,
                                    direction);
        }
-       mutex_unlock(&buffer->lock);
+       mutex_unlock(&dmabuf->lock);
 
        return 0;
 }
@@ -395,7 +364,7 @@ static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
                                      enum dma_data_direction direction)
 {
        struct ion_buffer *buffer = dmabuf->priv;
-       struct ion_dma_buf_attachment *a;
+       struct dma_buf_attachment *att;
 
        if (buffer->heap->ops->map_kernel) {
                mutex_lock(&buffer->lock);
@@ -403,12 +372,14 @@ static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
                mutex_unlock(&buffer->lock);
        }
 
-       mutex_lock(&buffer->lock);
-       list_for_each_entry(a, &buffer->attachments, list) {
-               dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents,
+       mutex_lock(&dmabuf->lock);
+       list_for_each_entry(att, &dmabuf->attachments, node) {
+               struct sg_table *table = att->priv;
+
+               dma_sync_sg_for_device(att->dev, table->sgl, table->nents,
                                       direction);
        }
-       mutex_unlock(&buffer->lock);
+       mutex_unlock(&dmabuf->lock);
 
        return 0;
 }
index 7c4011fc2cfa6957c76efd091cdafe210039bf84..9f5599b5e3bb269019ea6128e22280e48c123dd7 100644 (file)
@@ -85,7 +85,6 @@ struct ion_buffer {
        int kmap_cnt;
        void *vaddr;
        struct sg_table *sg_table;
-       struct list_head attachments;
 };
 void ion_buffer_destroy(struct ion_buffer *buffer);