From: "Horia Geantă" <horia.geanta@nxp.com> To: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>, Christoph Hellwig <hch@lst.de>, Marek Szyprowski <m.szyprowski@samsung.com>, Robin Murphy <robin.murphy@arm.com> Cc: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, linux-imx@nxp.com, "Horia Geantă" <horia.geanta@nxp.com> Subject: [PATCH] swiotlb: sync buffer when mapping FROM_DEVICE Date: Wed, 22 May 2019 10:20:18 +0300 [thread overview] Message-ID: <20190522072018.10660-1-horia.geanta@nxp.com> (raw) 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
WARNING: multiple messages have this Message-ID (diff)
From: "Horia Geantă" <horia.geanta@nxp.com> To: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>, Christoph Hellwig <hch@lst.de>, Marek Szyprowski <m.szyprowski@samsung.com>, Robin Murphy <robin.murphy@arm.com> Cc: iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, "Horia Geantă" <horia.geanta@nxp.com>, linux-imx@nxp.com Subject: [PATCH] swiotlb: sync buffer when mapping FROM_DEVICE Date: Wed, 22 May 2019 10:20:18 +0300 [thread overview] Message-ID: <20190522072018.10660-1-horia.geanta@nxp.com> (raw) 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 _______________________________________________ iommu mailing list iommu@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/iommu
next reply other threads:[~2019-05-22 7:20 UTC|newest] Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-05-22 7:20 Horia Geantă [this message] 2019-05-22 7:20 ` [PATCH] swiotlb: sync buffer when mapping FROM_DEVICE 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20190522072018.10660-1-horia.geanta@nxp.com \ --to=horia.geanta@nxp.com \ --cc=hch@lst.de \ --cc=iommu@lists.linux-foundation.org \ --cc=konrad.wilk@oracle.com \ --cc=linux-imx@nxp.com \ --cc=linux-kernel@vger.kernel.org \ --cc=m.szyprowski@samsung.com \ --cc=robin.murphy@arm.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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.