diff mbox series

[v3,15/25] drm: vmwgfx: fix common struct sg_table related issues

Message ID 20200505084614.30424-15-m.szyprowski@samsung.com
State Superseded
Headers show
Series DRM: fix struct sg_table nents vs. orig_nents misuse | expand

Commit Message

Marek Szyprowski May 5, 2020, 8:46 a.m. UTC
The Documentation/DMA-API-HOWTO.txt states that dma_map_sg returns the
numer of the created entries in the DMA address space. However the
subsequent calls to dma_sync_sg_for_{device,cpu} and dma_unmap_sg must be
called with the original number of the entries passed to dma_map_sg. The
sg_table->nents in turn holds the result of the dma_map_sg call as stated
in include/linux/scatterlist.h. A common mistake was to ignore a result
of the dma_map_sg function and don't use the sg_table->orig_nents at all.

To avoid such issues, lets use common dma-mapping wrappers operating
directly on the struct sg_table objects and adjust references to the
nents and orig_nents respectively.

Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>

---
For more information, see '[PATCH v3 00/25] DRM: fix struct sg_table nents
vs. orig_nents misuse' thread: https://lkml.org/lkml/2020/5/5/187
---
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

-- 
1.9.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Comments

Roland Scheidegger May 12, 2020, 12:19 a.m. UTC | #1
I'm not exactly an expert on this, but looks alright to me.
Acked-by: Roland Scheidegger <sroland@vmware.com>


Am 05.05.20 um 10:46 schrieb Marek Szyprowski:
> The Documentation/DMA-API-HOWTO.txt states that dma_map_sg returns the

> numer of the created entries in the DMA address space. However the

> subsequent calls to dma_sync_sg_for_{device,cpu} and dma_unmap_sg must be

> called with the original number of the entries passed to dma_map_sg. The

> sg_table->nents in turn holds the result of the dma_map_sg call as stated

> in include/linux/scatterlist.h. A common mistake was to ignore a result

> of the dma_map_sg function and don't use the sg_table->orig_nents at all.

> 

> To avoid such issues, lets use common dma-mapping wrappers operating

> directly on the struct sg_table objects and adjust references to the

> nents and orig_nents respectively.

> 

> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>

> ---

> For more information, see '[PATCH v3 00/25] DRM: fix struct sg_table nents

> vs. orig_nents misuse' thread: https://lkml.org/lkml/2020/5/5/187

> ---

>  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c | 17 ++++-------------

>  1 file changed, 4 insertions(+), 13 deletions(-)

> 

> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c

> index bf0bc46..e50ae8b 100644

> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c

> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c

> @@ -362,8 +362,7 @@ static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)

>  {

>  	struct device *dev = vmw_tt->dev_priv->dev->dev;

>  

> -	dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,

> -		DMA_BIDIRECTIONAL);

> +	dma_unmap_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL);

>  	vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;

>  }

>  

> @@ -383,16 +382,8 @@ static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)

>  static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)

>  {

>  	struct device *dev = vmw_tt->dev_priv->dev->dev;

> -	int ret;

> -

> -	ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,

> -			 DMA_BIDIRECTIONAL);

> -	if (unlikely(ret == 0))

> -		return -ENOMEM;

>  

> -	vmw_tt->sgt.nents = ret;

> -

> -	return 0;

> +	return dma_map_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL);

>  }

>  

>  /**

> @@ -449,10 +440,10 @@ static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)

>  		if (unlikely(ret != 0))

>  			goto out_sg_alloc_fail;

>  

> -		if (vsgt->num_pages > vmw_tt->sgt.nents) {

> +		if (vsgt->num_pages > vmw_tt->sgt.orig_nents) {

>  			uint64_t over_alloc =

>  				sgl_size * (vsgt->num_pages -

> -					    vmw_tt->sgt.nents);

> +					    vmw_tt->sgt.orig_nents);

>  

>  			ttm_mem_global_free(glob, over_alloc);

>  			vmw_tt->sg_alloc_size -= over_alloc;

> 


_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
diff mbox series

Patch

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
index bf0bc46..e50ae8b 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
@@ -362,8 +362,7 @@  static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
 {
 	struct device *dev = vmw_tt->dev_priv->dev->dev;
 
-	dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
-		DMA_BIDIRECTIONAL);
+	dma_unmap_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL);
 	vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
 }
 
@@ -383,16 +382,8 @@  static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
 static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
 {
 	struct device *dev = vmw_tt->dev_priv->dev->dev;
-	int ret;
-
-	ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
-			 DMA_BIDIRECTIONAL);
-	if (unlikely(ret == 0))
-		return -ENOMEM;
 
-	vmw_tt->sgt.nents = ret;
-
-	return 0;
+	return dma_map_sgtable(dev, vmw_tt->sgt, DMA_BIDIRECTIONAL);
 }
 
 /**
@@ -449,10 +440,10 @@  static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
 		if (unlikely(ret != 0))
 			goto out_sg_alloc_fail;
 
-		if (vsgt->num_pages > vmw_tt->sgt.nents) {
+		if (vsgt->num_pages > vmw_tt->sgt.orig_nents) {
 			uint64_t over_alloc =
 				sgl_size * (vsgt->num_pages -
-					    vmw_tt->sgt.nents);
+					    vmw_tt->sgt.orig_nents);
 
 			ttm_mem_global_free(glob, over_alloc);
 			vmw_tt->sg_alloc_size -= over_alloc;