Skip to content

Commit 449fa54

Browse files
fugangduanChristoph Hellwig
authored andcommitted
dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device
dma_map_sg() may use swiotlb buffer when the kernel command line includes "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 same issue, correct it as well. Fixes: 55897af("dma-direct: merge swiotlb_dma_ops into the dma_direct code") Signed-off-by: Fugang Duan <[email protected]> Reviewed-by: Robin Murphy <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent a5008b5 commit 449fa54

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

kernel/dma/direct.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,14 @@ void dma_direct_sync_sg_for_device(struct device *dev,
234234
int i;
235235

236236
for_each_sg(sgl, sg, nents, i) {
237-
if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
238-
swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
237+
phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
238+
239+
if (unlikely(is_swiotlb_buffer(paddr)))
240+
swiotlb_tbl_sync_single(dev, paddr, sg->length,
239241
dir, SYNC_FOR_DEVICE);
240242

241243
if (!dev_is_dma_coherent(dev))
242-
arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
244+
arch_sync_dma_for_device(dev, paddr, sg->length,
243245
dir);
244246
}
245247
}
@@ -271,11 +273,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
271273
int i;
272274

273275
for_each_sg(sgl, sg, nents, i) {
276+
phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
277+
274278
if (!dev_is_dma_coherent(dev))
275-
arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
276-
277-
if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
278-
swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir,
279+
arch_sync_dma_for_cpu(dev, paddr, sg->length, dir);
280+
281+
if (unlikely(is_swiotlb_buffer(paddr)))
282+
swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
279283
SYNC_FOR_CPU);
280284
}
281285

0 commit comments

Comments
 (0)