[PATCH] dma-buf: insert memory barrier before updating num_fences

Hyejeong Choi posted 1 patch 9 months ago
There is a newer version of this series
drivers/dma-buf/dma-resv.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
[PATCH] dma-buf: insert memory barrier before updating num_fences
Posted by Hyejeong Choi 9 months ago
smp_store_mb() inserts memory barrier after storing operation.
It is different with what the comment is originally aiming so Null
pointer dereference can be happened if memory update is reordered.

Signed-off-by: Hyejeong Choi <hjeong.choi@samsung.com>
---
 drivers/dma-buf/dma-resv.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
index 5f8d010516f0..52af5c7430da 100644
--- a/drivers/dma-buf/dma-resv.c
+++ b/drivers/dma-buf/dma-resv.c
@@ -320,8 +320,9 @@ void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
 	count++;
 
 	dma_resv_list_set(fobj, i, fence, usage);
-	/* pointer update must be visible before we extend the num_fences */
-	smp_store_mb(fobj->num_fences, count);
+	/* fence update must be visible before we extend the num_fences */
+	smp_wmb();
+	WRITE_ONCE(fobj->num_fences, count);
 }
 EXPORT_SYMBOL(dma_resv_add_fence);
 
-- 
2.31.1

Re: [PATCH] dma-buf: insert memory barrier before updating num_fences
Posted by Christian König 9 months ago
On 5/12/25 13:12, Hyejeong Choi wrote:
> smp_store_mb() inserts memory barrier after storing operation.
> It is different with what the comment is originally aiming so Null
> pointer dereference can be happened if memory update is reordered.
> 
> Signed-off-by: Hyejeong Choi <hjeong.choi@samsung.com>
> ---
>  drivers/dma-buf/dma-resv.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
> index 5f8d010516f0..52af5c7430da 100644
> --- a/drivers/dma-buf/dma-resv.c
> +++ b/drivers/dma-buf/dma-resv.c
> @@ -320,8 +320,9 @@ void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
>  	count++;
>  
>  	dma_resv_list_set(fobj, i, fence, usage);
> -	/* pointer update must be visible before we extend the num_fences */
> -	smp_store_mb(fobj->num_fences, count);
> +	/* fence update must be visible before we extend the num_fences */
> +	smp_wmb();
> +	WRITE_ONCE(fobj->num_fences, count);

The WRITE_ONCE isn't necessary since smp_wmb() implies a compiler barrier, but apart from that really good catch.

Can you modify the patch and re-send? I will be pushing it to -fixes ASAP.

Regards,
Christian.

>  }
>  EXPORT_SYMBOL(dma_resv_add_fence);
>  
> 
>
Re: [PATCH] dma-buf: insert memory barrier before updating num_fences
Posted by Hyejeong Choi 9 months ago
Thank you for the review.
I re-sent updated patch, please take a look.

Best regards,
Hyejeong


On Mon, May 12, 2025 at 03:16:33PM +0200, Christian König wrote:
> On 5/12/25 13:12, Hyejeong Choi wrote:
> > smp_store_mb() inserts memory barrier after storing operation.
> > It is different with what the comment is originally aiming so Null
> > pointer dereference can be happened if memory update is reordered.
> > 
> > Signed-off-by: Hyejeong Choi <hjeong.choi@samsung.com>
> > ---
> >  drivers/dma-buf/dma-resv.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/dma-buf/dma-resv.c b/drivers/dma-buf/dma-resv.c
> > index 5f8d010516f0..52af5c7430da 100644
> > --- a/drivers/dma-buf/dma-resv.c
> > +++ b/drivers/dma-buf/dma-resv.c
> > @@ -320,8 +320,9 @@ void dma_resv_add_fence(struct dma_resv *obj, struct dma_fence *fence,
> >  	count++;
> >  
> >  	dma_resv_list_set(fobj, i, fence, usage);
> > -	/* pointer update must be visible before we extend the num_fences */
> > -	smp_store_mb(fobj->num_fences, count);
> > +	/* fence update must be visible before we extend the num_fences */
> > +	smp_wmb();
> > +	WRITE_ONCE(fobj->num_fences, count);
> 
> The WRITE_ONCE isn't necessary since smp_wmb() implies a compiler barrier, but apart from that really good catch.
> 
> Can you modify the patch and re-send? I will be pushing it to -fixes ASAP.
> 
> Regards,
> Christian.
> 
> >  }
> >  EXPORT_SYMBOL(dma_resv_add_fence);
> >  
> > 
> > 
> 
>