blob: c762bd3b999207c625f3a51e9fddbe1921af85d5 [file] [log] [blame]
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: John Stultz <john.stultz@linaro.org>
Date: Tue, 5 May 2020 18:31:37 +0000
Subject: ANDROID: dma-heap: Refactor code to allow for future in-kernel users
Refactor the code to make in-kernel use a bit more clean.
[CPNOTE: 06/07/21] Lee: Asked vendor for an update via the bug
Signed-off-by: John Stultz <john.stultz@linaro.org>
Change-Id: I909a36ea6f2351415decbd489fd71ea03b2c2171
Bug: 154341375
---
drivers/dma-buf/dma-heap.c | 45 +++++++++++++++++++++++---------------
1 file changed, 27 insertions(+), 18 deletions(-)
diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -51,13 +51,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)
{
- struct dma_buf *dmabuf;
- int fd;
+ 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.
@@ -104,12 +122,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);
@@ -257,7 +269,7 @@ const char *dma_heap_get_name(struct dma_heap *heap)
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;
unsigned int minor;
int ret;
@@ -272,16 +284,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)