iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device
@ 2019-07-19  9:26 fugang.duan
  2019-07-19 10:38 ` Robin Murphy
  2019-07-19 12:12 ` Christoph Hellwig
  0 siblings, 2 replies; 3+ messages in thread
From: fugang.duan @ 2019-07-19  9:26 UTC (permalink / raw)
  To: hch, m.szyprowski
  Cc: aisheng.dong, Fugang Duan, festevam, linux-kernel, iommu, robin.murphy

From: Fugang Duan <fugang.duan@nxp.com>

dma_map_sg() may use swiotlb buffer when kernel parameter include
"swiotlb=force" or the dma_addr is out of dev->dma_mask range. After
DMA complete the memory moving from device to memory, then user call
dma_sync_sg_for_cpu() to sync with DMA buffer, and copy the original
virtual buffer to other space.

So dma_direct_sync_sg_for_cpu() should use swiotlb physical addr, not
the original physical addr from sg_phys(sg).

dma_direct_sync_sg_for_device() also has the similar issue, correct it.

Fixes: 55897af63091("dma-direct: merge swiotlb_dma_ops into the dma_direct code")
Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
---
 kernel/dma/direct.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index b90e1ae..0e87f86 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -242,12 +242,14 @@ void dma_direct_sync_sg_for_device(struct device *dev,
 	int i;
 
 	for_each_sg(sgl, sg, nents, i) {
-		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
-			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
+		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
+
+		if (unlikely(is_swiotlb_buffer(paddr)))
+			swiotlb_tbl_sync_single(dev, paddr, sg->length,
 					dir, SYNC_FOR_DEVICE);
 
 		if (!dev_is_dma_coherent(dev))
-			arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
+			arch_sync_dma_for_device(dev, paddr, sg->length,
 					dir);
 	}
 }
@@ -279,11 +281,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
 	int i;
 
 	for_each_sg(sgl, sg, nents, i) {
+		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
+
 		if (!dev_is_dma_coherent(dev))
-			arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
-	
-		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
-			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir,
+			arch_sync_dma_for_cpu(dev, paddr, sg->length, dir);
+
+		if (unlikely(is_swiotlb_buffer(paddr)))
+			swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
 					SYNC_FOR_CPU);
 	}
 
-- 
2.7.4

_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device
  2019-07-19  9:26 [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device fugang.duan
@ 2019-07-19 10:38 ` Robin Murphy
  2019-07-19 12:12 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Robin Murphy @ 2019-07-19 10:38 UTC (permalink / raw)
  To: fugang.duan, hch, m.szyprowski
  Cc: aisheng.dong, Al Farleigh, iommu, festevam, linux-kernel

On 19/07/2019 10:26, fugang.duan@nxp.com wrote:
> From: Fugang Duan <fugang.duan@nxp.com>
> 
> dma_map_sg() may use swiotlb buffer when kernel parameter include
> "swiotlb=force" or the dma_addr is out of dev->dma_mask range. After
> DMA complete the memory moving from device to memory, then user call
> dma_sync_sg_for_cpu() to sync with DMA buffer, and copy the original
> virtual buffer to other space.
> 
> So dma_direct_sync_sg_for_cpu() should use swiotlb physical addr, not
> the original physical addr from sg_phys(sg).
> 
> dma_direct_sync_sg_for_device() also has the similar issue, correct it.

Yikes, that was unfortunate. There aren't too many users of 
dma_sync_sg*(), but I note that a few of them are in and around v4l2, so 
this could well explain the video brokenness that Al reported.

AFAICS the fix looks appropriate;

Reviewed-by: Robin Murphy <robin.murphy@arm.com>

> Fixes: 55897af63091("dma-direct: merge swiotlb_dma_ops into the dma_direct code")
> Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
> ---
>   kernel/dma/direct.c | 18 +++++++++++-------
>   1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> index b90e1ae..0e87f86 100644
> --- a/kernel/dma/direct.c
> +++ b/kernel/dma/direct.c
> @@ -242,12 +242,14 @@ void dma_direct_sync_sg_for_device(struct device *dev,
>   	int i;
>   
>   	for_each_sg(sgl, sg, nents, i) {
> -		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
> -			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
> +		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
> +
> +		if (unlikely(is_swiotlb_buffer(paddr)))
> +			swiotlb_tbl_sync_single(dev, paddr, sg->length,
>   					dir, SYNC_FOR_DEVICE);
>   
>   		if (!dev_is_dma_coherent(dev))
> -			arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
> +			arch_sync_dma_for_device(dev, paddr, sg->length,
>   					dir);
>   	}
>   }
> @@ -279,11 +281,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
>   	int i;
>   
>   	for_each_sg(sgl, sg, nents, i) {
> +		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
> +
>   		if (!dev_is_dma_coherent(dev))
> -			arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
> -	
> -		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
> -			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir,
> +			arch_sync_dma_for_cpu(dev, paddr, sg->length, dir);
> +
> +		if (unlikely(is_swiotlb_buffer(paddr)))
> +			swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
>   					SYNC_FOR_CPU);
>   	}
>   
> 
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH dma  1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device
  2019-07-19  9:26 [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device fugang.duan
  2019-07-19 10:38 ` Robin Murphy
@ 2019-07-19 12:12 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2019-07-19 12:12 UTC (permalink / raw)
  To: fugang.duan
  Cc: aisheng.dong, festevam, linux-kernel, iommu, robin.murphy, hch

Thanks,

applied to the dma-mapping tree and I'll sent it to Linus this
weekend.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-19 12:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-19  9:26 [PATCH dma 1/1] dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device fugang.duan
2019-07-19 10:38 ` Robin Murphy
2019-07-19 12:12 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).