Merge tag 'drm-misc-fixes-2022-11-24' of git://anongit.freedesktop.org/drm/drm-misc...
[sfrench/cifs-2.6.git] / drivers / dma-buf / dma-heap.c
index 8f5848aa144fec35a2de2ea68558c27dc3083669..59d158873f4cb451b5a5db4f0691e25d6a56909c 100644 (file)
@@ -233,18 +233,6 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
                return ERR_PTR(-EINVAL);
        }
 
-       /* 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);
-               }
-       }
-       mutex_unlock(&heap_list_lock);
-
        heap = kzalloc(sizeof(*heap), GFP_KERNEL);
        if (!heap)
                return ERR_PTR(-ENOMEM);
@@ -283,13 +271,27 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
                err_ret = ERR_CAST(dev_ret);
                goto err2;
        }
-       /* Add heap to the list */
+
        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)) {
+                       mutex_unlock(&heap_list_lock);
+                       pr_err("dma_heap: Already registered heap named %s\n",
+                              exp_info->name);
+                       err_ret = ERR_PTR(-EINVAL);
+                       goto err3;
+               }
+       }
+
+       /* Add heap to the list */
        list_add(&heap->list, &heap_list);
        mutex_unlock(&heap_list_lock);
 
        return heap;
 
+err3:
+       device_destroy(dma_heap_class, heap->heap_devt);
 err2:
        cdev_del(&heap->heap_cdev);
 err1: