Split dma_heap_add() into creation and registration
phases while preserving the ordering between
cdev_add() and device_add(), and ensuring all
device fields are initialised.

This lets callers build a heap and its device,
bind reserved memory, and cleanly unwind on failure
before the heap is registered. It also avoids a window
where userspace can see a heap that exists but isn’t
fully functional. The coherent heap will need this to
bind rmem to the heap device prior to registration.

Signed-off-by: Albert Esteve <[email protected]>
---
 drivers/dma-buf/dma-heap.c | 126 +++++++++++++++++++++++++++++++++++----------
 include/linux/dma-heap.h   |   3 ++
 2 files changed, 103 insertions(+), 26 deletions(-)

diff --git a/drivers/dma-buf/dma-heap.c b/drivers/dma-buf/dma-heap.c
index 1124d63eb1398..ba87e5ac16ae2 100644
--- a/drivers/dma-buf/dma-heap.c
+++ b/drivers/dma-buf/dma-heap.c
@@ -238,15 +238,30 @@ struct device *dma_heap_get_dev(struct dma_heap *heap)
 }
 EXPORT_SYMBOL_NS_GPL(dma_heap_get_dev, "DMA_BUF_HEAP");
 
+static void dma_heap_dev_release(struct device *dev)
+{
+       struct dma_heap *heap;
+
+       pr_debug("heap device: '%s': %s\n", dev_name(dev), __func__);
+       heap = dev_get_drvdata(dev);
+       kfree(heap->name);
+       kfree(heap);
+       kfree(dev);
+}
+
 /**
- * dma_heap_add - adds a heap to dmabuf heaps
- * @exp_info: information needed to register this heap
+ * dma_heap_create() - allocate and initialize a heap object
+ * @exp_info: information needed to create a heap
+ *
+ * Creates a heap instance but does not register it or create device nodes.
+ * Use dma_heap_register() to make it visible to userspace, or
+ * dma_heap_destroy() to release it.
+ *
+ * Returns a heap on success or ERR_PTR(-errno) on failure.
  */
-struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
+struct dma_heap *dma_heap_create(const struct dma_heap_export_info *exp_info)
 {
-       struct dma_heap *heap, *h, *err_ret;
-       unsigned int minor;
-       int ret;
+       struct dma_heap *heap;
 
        if (!exp_info->name || !strcmp(exp_info->name, "")) {
                pr_err("dma_heap: Cannot add heap without a name\n");
@@ -265,13 +280,41 @@ struct dma_heap *dma_heap_add(const struct 
dma_heap_export_info *exp_info)
        heap->name = exp_info->name;
        heap->ops = exp_info->ops;
        heap->priv = exp_info->priv;
+       heap->heap_dev = kzalloc_obj(*heap->heap_dev);
+       if (!heap->heap_dev) {
+               kfree(heap);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       device_initialize(heap->heap_dev);
+       dev_set_drvdata(heap->heap_dev, heap);
+
+       dev_set_name(heap->heap_dev, heap->name);
+       heap->heap_dev->class = dma_heap_class;
+       heap->heap_dev->release = dma_heap_dev_release;
+
+       return heap;
+}
+EXPORT_SYMBOL_NS_GPL(dma_heap_create, "DMA_BUF_HEAP");
+
+/**
+ * dma_heap_register() - register a heap with the dma-heap framework
+ * @heap: heap instance created with dma_heap_create()
+ *
+ * Registers the heap, creating its device node and adding it to the heap
+ * list. Returns 0 on success or a negative error code on failure.
+ */
+int dma_heap_register(struct dma_heap *heap)
+{
+       struct dma_heap *h;
+       unsigned int minor;
+       int ret;
 
        /* Find unused minor number */
        ret = xa_alloc(&dma_heap_minors, &minor, heap,
                       XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
        if (ret < 0) {
                pr_err("dma_heap: Unable to get minor number for heap\n");
-               err_ret = ERR_PTR(ret);
                goto err0;
        }
 
@@ -282,42 +325,34 @@ struct dma_heap *dma_heap_add(const struct 
dma_heap_export_info *exp_info)
        ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
        if (ret < 0) {
                pr_err("dma_heap: Unable to add char device\n");
-               err_ret = ERR_PTR(ret);
                goto err1;
        }
 
-       heap->heap_dev = device_create(dma_heap_class,
-                                      NULL,
-                                      heap->heap_devt,
-                                      NULL,
-                                      heap->name);
-       if (IS_ERR(heap->heap_dev)) {
-               pr_err("dma_heap: Unable to create device\n");
-               err_ret = ERR_CAST(heap->heap_dev);
+       heap->heap_dev->devt = heap->heap_devt;
+
+       ret = device_add(heap->heap_dev);
+       if (ret) {
+               pr_err("dma_heap: Unable to add device\n");
                goto err2;
        }
 
        mutex_lock(&heap_list_lock);
        /* check the name is unique */
        list_for_each_entry(h, &heap_list, list) {
-               if (!strcmp(h->name, exp_info->name)) {
+               if (!strcmp(h->name, heap->name)) {
                        mutex_unlock(&heap_list_lock);
                        pr_err("dma_heap: Already registered heap named %s\n",
-                              exp_info->name);
-                       err_ret = ERR_PTR(-EINVAL);
+                              heap->name);
+                       ret = -EINVAL;
                        goto err3;
                }
        }
 
-       /* Make sure it doesn't disappear on us */
-       heap->heap_dev = get_device(heap->heap_dev);
-
-
        /* Add heap to the list */
        list_add(&heap->list, &heap_list);
        mutex_unlock(&heap_list_lock);
 
-       return heap;
+       return 0;
 
 err3:
        device_destroy(dma_heap_class, heap->heap_devt);
@@ -326,8 +361,47 @@ struct dma_heap *dma_heap_add(const struct 
dma_heap_export_info *exp_info)
 err1:
        xa_erase(&dma_heap_minors, minor);
 err0:
-       kfree(heap);
-       return err_ret;
+       dma_heap_destroy(heap);
+       return ret;
+}
+EXPORT_SYMBOL_NS_GPL(dma_heap_register, "DMA_BUF_HEAP");
+
+/**
+ * dma_heap_destroy() - release a heap created by dma_heap_create()
+ * @heap: heap instance to release
+ *
+ * Drops the heap device reference; the heap and its device are freed in the
+ * device release path when the last reference is gone.
+ */
+void dma_heap_destroy(struct dma_heap *heap)
+{
+       put_device(heap->heap_dev);
+}
+EXPORT_SYMBOL_NS_GPL(dma_heap_destroy, "DMA_BUF_HEAP");
+
+/**
+ * dma_heap_add - adds a heap to dmabuf heaps
+ * @exp_info: information needed to register this heap
+ */
+struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
+{
+       struct dma_heap *heap;
+       int ret;
+
+       heap = dma_heap_create(exp_info);
+       if (IS_ERR(heap)) {
+               pr_err("dma_heap: failed to create heap (%ld)\n", 
PTR_ERR(heap));
+               return ERR_CAST(heap);
+       }
+
+       ret = dma_heap_register(heap);
+       if (ret) {
+               pr_err("dma_heap: failed to register heap (%d)\n", ret);
+               dma_heap_destroy(heap);
+               return ERR_PTR(ret);
+       }
+
+       return heap;
 }
 EXPORT_SYMBOL_NS_GPL(dma_heap_add, "DMA_BUF_HEAP");
 
diff --git a/include/linux/dma-heap.h b/include/linux/dma-heap.h
index 493085e69b70e..1b0ea43ba66c3 100644
--- a/include/linux/dma-heap.h
+++ b/include/linux/dma-heap.h
@@ -46,6 +46,9 @@ void *dma_heap_get_drvdata(struct dma_heap *heap);
 const char *dma_heap_get_name(struct dma_heap *heap);
 struct device *dma_heap_get_dev(struct dma_heap *heap);
 
+struct dma_heap *dma_heap_create(const struct dma_heap_export_info *exp_info);
+int dma_heap_register(struct dma_heap *heap);
+void dma_heap_destroy(struct dma_heap *heap);
 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
 
 extern bool mem_accounting;

-- 
2.52.0

Reply via email to