Skip to content

Commit 432e259

Browse files
Dawei Lisumitsemwal
authored andcommitted
dma-buf: fix racing conflict of dma_heap_add()
Racing conflict could be: task A task B list_for_each_entry strcmp(h->name)) list_for_each_entry strcmp(h->name) kzalloc kzalloc ...... ..... device_create device_create list_add list_add The root cause is that task B has no idea about the fact someone else(A) has inserted heap with same name when it calls list_add, so a potential collision occurs. Fixes: c02a81f ("dma-buf: Add dma-buf heaps framework") Signed-off-by: Dawei Li <[email protected]> Acked-by: Andrew Davis <[email protected]> Acked-by: Christian König <[email protected]> Signed-off-by: Sumit Semwal <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/TYCP286MB2323873BBDF88020781FB986CA3B9@TYCP286MB2323.JPNP286.PROD.OUTLOOK.COM
1 parent c19083c commit 432e259

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

drivers/dma-buf/dma-heap.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,6 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
233233
return ERR_PTR(-EINVAL);
234234
}
235235

236-
/* check the name is unique */
237-
mutex_lock(&heap_list_lock);
238-
list_for_each_entry(h, &heap_list, list) {
239-
if (!strcmp(h->name, exp_info->name)) {
240-
mutex_unlock(&heap_list_lock);
241-
pr_err("dma_heap: Already registered heap named %s\n",
242-
exp_info->name);
243-
return ERR_PTR(-EINVAL);
244-
}
245-
}
246-
mutex_unlock(&heap_list_lock);
247-
248236
heap = kzalloc(sizeof(*heap), GFP_KERNEL);
249237
if (!heap)
250238
return ERR_PTR(-ENOMEM);
@@ -283,13 +271,27 @@ struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
283271
err_ret = ERR_CAST(dev_ret);
284272
goto err2;
285273
}
286-
/* Add heap to the list */
274+
287275
mutex_lock(&heap_list_lock);
276+
/* check the name is unique */
277+
list_for_each_entry(h, &heap_list, list) {
278+
if (!strcmp(h->name, exp_info->name)) {
279+
mutex_unlock(&heap_list_lock);
280+
pr_err("dma_heap: Already registered heap named %s\n",
281+
exp_info->name);
282+
err_ret = ERR_PTR(-EINVAL);
283+
goto err3;
284+
}
285+
}
286+
287+
/* Add heap to the list */
288288
list_add(&heap->list, &heap_list);
289289
mutex_unlock(&heap_list_lock);
290290

291291
return heap;
292292

293+
err3:
294+
device_destroy(dma_heap_class, heap->heap_devt);
293295
err2:
294296
cdev_del(&heap->heap_cdev);
295297
err1:

0 commit comments

Comments
 (0)