From fc1310ebf8fe25ea7b983400e6fa41f5a6d11966 Mon Sep 17 00:00:00 2001 From: John Stultz Date: Tue, 5 May 2020 18:31:37 +0000 Subject: [PATCH] ANDROID: dma-heap: Refactor code to allow for future in-kernel users Refactor the code to make in-kernel use a bit more clean. Signed-off-by: John Stultz Change-Id: I909a36ea6f2351415decbd489fd71ea03b2c2171 Bug: 154341375 --- drivers/dma-buf/dma-heap.c | 44 ++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c index 6b6cefa7f597..a64a802003a8 100644 --- a/drivers/dma-buf/dma-heap.c +++ b/drivers/dma-buf/dma-heap.c @@ -50,10 +50,31 @@ static dev_t dma_heap_devt; static struct class *dma_heap_class; static DEFINE_XARRAY_ALLOC(dma_heap_minors); +static struct dma_heap *dma_heap_find(const char *name) +{ + struct dma_heap *h; + + mutex_lock(&heap_list_lock); + list_for_each_entry(h, &heap_list, list) { + if (!strcmp(h->name, name)) { + kref_get(&h->refcount); + mutex_unlock(&heap_list_lock); + return h; + } + } + mutex_unlock(&heap_list_lock); + return NULL; +} + static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, unsigned int fd_flags, unsigned int heap_flags) { + if (fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) + return -EINVAL; + + if (heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) + return -EINVAL; /* * Allocations from all heaps have to begin * and end on page boundaries. @@ -91,12 +112,6 @@ static long dma_heap_ioctl_allocate(struct file *file, void *data) if (heap_allocation->fd) return -EINVAL; - if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS) - return -EINVAL; - - if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS) - return -EINVAL; - fd = dma_heap_buffer_alloc(heap, heap_allocation->len, heap_allocation->fd_flags, heap_allocation->heap_flags); @@ -219,7 +234,7 @@ void dma_heap_put(struct dma_heap *h) struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) { - struct dma_heap *heap, *h, *err_ret; + struct dma_heap *heap, *err_ret; struct device *dev_ret; int ret; @@ -234,16 +249,13 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info) } /* check the name is unique */ - mutex_lock(&heap_list_lock); - list_for_each_entry(h, &heap_list, list) { - if (!strcmp(h->name, exp_info->name)) { - mutex_unlock(&heap_list_lock); - pr_err("dma_heap: Already registered heap named %s\n", - exp_info->name); - return ERR_PTR(-EINVAL); - } + heap = dma_heap_find(exp_info->name); + if (heap) { + pr_err("dma_heap: Already registered heap named %s\n", + exp_info->name); + dma_heap_put(heap); + return ERR_PTR(-EINVAL); } - mutex_unlock(&heap_list_lock); heap = kzalloc(sizeof(*heap), GFP_KERNEL); if (!heap)