2022-01-04 07:36:17

by Weizhao Ouyang

[permalink] [raw]
Subject: [PATCH] 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")
Signed-off-by: Weizhao Ouyang <[email protected]>
---
drivers/dma-buf/heaps/cma_heap.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
index 0c05b79870f9..83f02bd51dda 100644
--- a/drivers/dma-buf/heaps/cma_heap.c
+++ b/drivers/dma-buf/heaps/cma_heap.c
@@ -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;
--
2.32.0



2022-01-04 07:43:11

by John Stultz

[permalink] [raw]
Subject: Re: [PATCH] dma-buf: cma_heap: Fix mutex locking section

On Mon, Jan 3, 2022 at 11:36 PM Weizhao Ouyang <[email protected]> wrote:
>
> 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")
> Signed-off-by: Weizhao Ouyang <[email protected]>

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

Acked-by: John Stultz <[email protected]>

thanks again
-john

2022-01-13 11:08:04

by Sumit Semwal

[permalink] [raw]
Subject: Re: [PATCH] dma-buf: cma_heap: Fix mutex locking section

Hello Weizhao,

On Tue, 4 Jan 2022 at 13:13, John Stultz <[email protected]> wrote:
>
> On Mon, Jan 3, 2022 at 11:36 PM Weizhao Ouyang <[email protected]> wrote:
> >
> > 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")
> > Signed-off-by: Weizhao Ouyang <[email protected]>
>
> Looks good to me! Thanks so much for sending this in!
Thanks for the patch; it's merged to drm-misc-fixes.
>
> Acked-by: John Stultz <[email protected]>
>
> thanks again
> -john


Best,
Sumit.

2022-01-21 19:22:11

by Daniel Vetter

[permalink] [raw]
Subject: Re: [PATCH] dma-buf: cma_heap: Fix mutex locking section

On Tue, Jan 04, 2022 at 03:35:45PM +0800, Weizhao Ouyang wrote:
> 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")
> Signed-off-by: Weizhao Ouyang <[email protected]>
> ---
> drivers/dma-buf/heaps/cma_heap.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/dma-buf/heaps/cma_heap.c b/drivers/dma-buf/heaps/cma_heap.c
> index 0c05b79870f9..83f02bd51dda 100644
> --- a/drivers/dma-buf/heaps/cma_heap.c
> +++ b/drivers/dma-buf/heaps/cma_heap.c
> @@ -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


>
> - 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;
> --
> 2.32.0
>

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch