Skip to content

Commit a6016aa

Browse files
alobakinChristoph Hellwig
authored andcommitted
dma: fix DMA sync for drivers not calling dma_set_mask*()
There are several reports that the DMA sync shortcut broke non-coherent devices. dev->dma_need_sync is false after the &device allocation and if a driver didn't call dma_set_mask*(), it will still be false even if the device is not DMA-coherent and thus needs synchronizing. Due to historical reasons, there's still a lot of drivers not calling it. Invert the boolean, so that the sync will be performed by default and the shortcut will be enabled only when calling dma_set_mask*(). Reported-by: Steven Price <[email protected]> Closes: https://lore.kernel.org/lkml/[email protected] Reported-by: Marek Szyprowski <[email protected]> Closes: https://lore.kernel.org/lkml/[email protected] Fixes: f406c8e. ("dma: avoid redundant calls for sync operations") Signed-off-by: Alexander Lobakin <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]> Tested-by: Steven Price <[email protected]> Tested-by: Marek Szyprowski <[email protected]>
1 parent 163943a commit a6016aa

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

include/linux/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ struct device_physical_location {
691691
* and optionall (if the coherent mask is large enough) also
692692
* for dma allocations. This flag is managed by the dma ops
693693
* instance from ->dma_supported.
694-
* @dma_need_sync: The device needs performing DMA sync operations.
694+
* @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
695695
*
696696
* At the lowest level, every device in a Linux system is represented by an
697697
* instance of struct device. The device structure contains the information
@@ -805,7 +805,7 @@ struct device {
805805
bool dma_ops_bypass : 1;
806806
#endif
807807
#ifdef CONFIG_DMA_NEED_SYNC
808-
bool dma_need_sync:1;
808+
bool dma_skip_sync:1;
809809
#endif
810810
};
811811

include/linux/dma-map-ops.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ static inline void dma_reset_need_sync(struct device *dev)
280280
{
281281
#ifdef CONFIG_DMA_NEED_SYNC
282282
/* Reset it only once so that the function can be called on hotpath */
283-
if (unlikely(!dev->dma_need_sync))
284-
dev->dma_need_sync = true;
283+
if (unlikely(dev->dma_skip_sync))
284+
dev->dma_skip_sync = false;
285285
#endif
286286
}
287287

include/linux/dma-mapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr);
295295
static inline bool dma_dev_need_sync(const struct device *dev)
296296
{
297297
/* Always call DMA sync operations when debugging is enabled */
298-
return dev->dma_need_sync || IS_ENABLED(CONFIG_DMA_API_DEBUG);
298+
return !dev->dma_skip_sync || IS_ENABLED(CONFIG_DMA_API_DEBUG);
299299
}
300300

301301
static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,

kernel/dma/mapping.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool __dma_need_sync(struct device *dev, dma_addr_t dma_addr)
392392

393393
if (dma_map_direct(dev, ops))
394394
/*
395-
* dma_need_sync could've been reset on first SWIOTLB buffer
395+
* dma_skip_sync could've been reset on first SWIOTLB buffer
396396
* mapping, but @dma_addr is not necessary an SWIOTLB buffer.
397397
* In this case, fall back to more granular check.
398398
*/
@@ -407,20 +407,20 @@ static void dma_setup_need_sync(struct device *dev)
407407

408408
if (dma_map_direct(dev, ops) || (ops->flags & DMA_F_CAN_SKIP_SYNC))
409409
/*
410-
* dma_need_sync will be reset to %true on first SWIOTLB buffer
410+
* dma_skip_sync will be reset to %false on first SWIOTLB buffer
411411
* mapping, if any. During the device initialization, it's
412412
* enough to check only for the DMA coherence.
413413
*/
414-
dev->dma_need_sync = !dev_is_dma_coherent(dev);
414+
dev->dma_skip_sync = dev_is_dma_coherent(dev);
415415
else if (!ops->sync_single_for_device && !ops->sync_single_for_cpu &&
416416
!ops->sync_sg_for_device && !ops->sync_sg_for_cpu)
417417
/*
418418
* Synchronization is not possible when none of DMA sync ops
419419
* is set.
420420
*/
421-
dev->dma_need_sync = false;
421+
dev->dma_skip_sync = true;
422422
else
423-
dev->dma_need_sync = true;
423+
dev->dma_skip_sync = false;
424424
}
425425
#else /* !CONFIG_DMA_NEED_SYNC */
426426
static inline void dma_setup_need_sync(struct device *dev) { }

kernel/dma/swiotlb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
14091409
}
14101410

14111411
/*
1412-
* If dma_need_sync wasn't set, reset it on first SWIOTLB buffer
1412+
* If dma_skip_sync was set, reset it on first SWIOTLB buffer
14131413
* mapping to always sync SWIOTLB buffers.
14141414
*/
14151415
dma_reset_need_sync(dev);

0 commit comments

Comments
 (0)