Skip to contentSkip to conversation

dma-buf: cma_heap: Fix mutex locking section

PATCH In review Weizhao Ouyang 4 participants 4 messages Jan 4, 2022 – Jan 19, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-buf: cma_heap: Fix mutex locking section

Fix cma_heap_buffer mutex locking critical section to protect vmap_cnt and vaddr.

Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")
---
 drivers/dma-buf/heaps/cma_heap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/dma-buf/heaps/cma_heap.c +4 -2
index 0c05b79870f9..83f02bd51dda 100644
@@ -124,10 +124,11 @@ static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
 	struct cma_heap_buffer *buffer = dmabuf->priv;
 	struct dma_heap_attachment *a;
 
+	mutex_lock(&buffer->lock);
+
 	if (buffer->vmap_cnt)
 		invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);
 
-	mutex_lock(&buffer->lock);
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
@@ -144,10 +145,11 @@ static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
 	struct cma_heap_buffer *buffer = dmabuf->priv;
 	struct dma_heap_attachment *a;
 
+	mutex_lock(&buffer->lock);
+
 	if (buffer->vmap_cnt)
 		flush_kernel_vmap_range(buffer->vaddr, buffer->len);
 
-	mutex_lock(&buffer->lock);
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
Signature · 1 line
2.32.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Weizhao Ouyang

Fix cma_heap_buffer mutex locking critical section to protect vmap_cnt and vaddr.

Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

Looks good to me! Thanks so much for sending this in!

thanks again
-john

maintainer
#2 lore raw

Hello Weizhao,

Quoted 10 lines · John Stultz
Quoted 6 lines · Weizhao Ouyang

Fix cma_heap_buffer mutex locking critical section to protect vmap_cnt and vaddr.

Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")

Looks good to me! Thanks so much for sending this in!

Thanks for the patch; it's merged to drm-misc-fixes.

Quoted 5 lines

thanks again
-john

Best,
Sumit.

#3 lore raw
In reply to Weizhao Ouyang
Quoted 14 lines · Weizhao Ouyang

Fix cma_heap_buffer mutex locking critical section to protect vmap_cnt and vaddr.

Fixes a5d2d29e24be ("dma-buf: heaps: Move heap-helper logic into the cma_heap implementation")
---
 drivers/dma-buf/heaps/cma_heap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/dma-buf/heaps/cma_heap.c +0 -0
index 0c05b79870f9..83f02bd51dda 100644

drivers/dma-buf/heaps/cma_heap.c static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, · line 130

Quoted 7 lines
drivers/dma-buf/heaps/cma_heap.c +2 -0
@@ -124,10 +124,11 @@ static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
 	struct cma_heap_buffer *buffer = dmabuf->priv;
 	struct dma_heap_attachment *a;
 
+	mutex_lock(&buffer->lock);
+
 	if (buffer->vmap_cnt)
 		invalidate_kernel_vmap_range(buffer->vaddr, buffer->len);

Since this creates nesting with mm/, but optionally I think it'd be good to prime lockdep so it knows about this. See e.g. dma_resv_lockdep() in dma-resv.c, except I don't know offhand what the right lock for invalidate_kernel_vmap_range is.
-Daniel

Quoted 21 lines
drivers/dma-buf/heaps/cma_heap.c +2 -2
-	mutex_lock(&buffer->lock);
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
@@ -144,10 +145,11 @@ static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
 	struct cma_heap_buffer *buffer = dmabuf->priv;
 	struct dma_heap_attachment *a;
 
+	mutex_lock(&buffer->lock);
+
 	if (buffer->vmap_cnt)
 		flush_kernel_vmap_range(buffer->vaddr, buffer->len);
 
-	mutex_lock(&buffer->lock);
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
Signature · 1 line
2.32.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch