All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] swiotlb: sync buffer when mapping FROM_DEVICE
@ 2019-05-22  7:20 ` Horia Geantă
  0 siblings, 0 replies; 24+ messages in thread
From: Horia Geantă @ 2019-05-22  7:20 UTC (permalink / raw)
  To: Konrad Rzeszutek Wilk, Christoph Hellwig, Marek Szyprowski, Robin Murphy
  Cc: iommu, linux-kernel, linux-imx, Horia Geantă

From the very beginning, the swiotlb implementation (and even before that,
pci implementation if we look in full git history) did not sync
the bounced buffer in case of DMA mapping using DMA_FROM_DEVICE direction.

However, this is incorrect since the device might not write to that area
at all (or might partially write to it), which leads to data corruption
in the sense that data in original buffer is lost (overwritten with
uninitialized data in the bounced buffer at DMA unmap time).

In general, DMA mapping using DMA_FROM_DEVICE does not mean existing data
should be thrown away.

Fix this by sync-ing the bounced buffer at DMA mapping time
irrespective of DMA direction.

Link: https://lore.kernel.org/lkml/584b54f6-bd12-d036-35e6-23eb2dabe811@arm.com
Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
---

I haven't provided a Fixes tag since this approach goes way back in time.
If you agree with the fix, we'll have to decide if it should go
into -stable and what's the earliest LTS branch to get the backport.

Patch is based on konrad/swiotlb.git, devel/for-linus-5.2 branch.

 kernel/dma/swiotlb.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
index 38d57218809c..f330222f0eb5 100644
--- a/kernel/dma/swiotlb.c
+++ b/kernel/dma/swiotlb.c
@@ -545,13 +545,14 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev,
 
 	/*
 	 * Save away the mapping from the original address to the DMA address.
-	 * This is needed when we sync the memory.  Then we sync the buffer if
-	 * needed.
+	 * This is needed when we sync the memory.  Then we sync the buffer
+	 * irrespective of mapping direction - since for FROM_DEVICE we want to
+	 * make sure original data is not lost in the case of device not fully
+	 * overwriting the area mapped.
 	 */
 	for (i = 0; i < nslots; i++)
 		io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
-	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
-	    (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
+	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
 		swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE);
 
 	return tlb_addr;
-- 
2.17.1


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

end of thread, other threads:[~2019-05-23 18:05 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-22  7:20 [PATCH] swiotlb: sync buffer when mapping FROM_DEVICE Horia Geantă
2019-05-22  7:20 ` Horia Geantă
2019-05-22 12:32 ` Christoph Hellwig
2019-05-22 12:32   ` Christoph Hellwig
2019-05-22 12:50   ` Robin Murphy
2019-05-22 12:50     ` Robin Murphy
2019-05-22 13:09     ` Christoph Hellwig
2019-05-22 13:09       ` Christoph Hellwig
2019-05-22 13:25       ` Robin Murphy
2019-05-22 13:25         ` Robin Murphy
2019-05-22 13:34         ` Christoph Hellwig
2019-05-22 13:34           ` Christoph Hellwig
2019-05-22 13:55           ` Robin Murphy
2019-05-22 13:55             ` Robin Murphy
2019-05-23  5:35             ` Marek Szyprowski
2019-05-23  5:35               ` Marek Szyprowski
2019-05-23 16:25               ` Horia Geanta
2019-05-23 16:25                 ` Horia Geanta
2019-05-23 16:43               ` Christoph Hellwig
2019-05-23 16:43                 ` Christoph Hellwig
2019-05-23 17:53                 ` Horia Geanta
2019-05-23 17:53                   ` Horia Geanta
2019-05-23 18:05                 ` Robin Murphy
2019-05-23 18:05                   ` Robin Murphy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.