dma-mapping: arm64: support batched cache sync
Subsystems (4)
cover dma-mapping: arm64: support batched cache sync
From: Barry Song <[email protected]>
Many embedded ARM64 SoCs still lack hardware cache coherency support, which causes DMA mapping operations to appear as hotspots in on-CPU flame graphs.
For an SG list with *nents* entries, the current dma_map/unmap_sg() and DMA sync APIs perform cache maintenance one entry at a time. After each entry, the implementation synchronously waits for the corresponding region’s D-cache operations to complete. On architectures like arm64, efficiency can be improved by issuing all entries’ operations first and then performing a single batched wait for completion.
Tangquan's initial results show that batched synchronization can reduce dma_map_sg() time by 64.61% and dma_unmap_sg() time by 66.60% on an MTK phone platform (MediaTek Dimensity 9500). The tests were performed by pinning the task to CPU7 and fixing the CPU frequency at 2.6 GHz, running dma_map_sg() and dma_unmap_sg() on 10 MB buffers (10 MB / 4 KB sg entries per buffer) for 200 iterations and then averaging the results.
Barry Song (5):
arm64: Provide dcache_by_myline_op_nosync helper
arm64: Provide dcache_clean_poc_nosync helper
arm64: Provide dcache_inval_poc_nosync helper
arm64: Provide arch_sync_dma_ batched helpers
dma-mapping: Allow batched DMA sync operations if supported by the
arch
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/assembler.h | 79 +++++++++++++++++++------- arch/arm64/include/asm/cacheflush.h | 2 + arch/arm64/mm/cache.S | 58 +++++++++++++++---- arch/arm64/mm/dma-mapping.c | 24 ++++++++ include/linux/dma-map-ops.h | 8 +++ kernel/dma/Kconfig | 3 + kernel/dma/direct.c | 53 ++++++++++++++++-- kernel/dma/direct.h | 86 +++++++++++++++++++++++++---- 9 files changed, 267 insertions(+), 47 deletions(-)
Signature · 1 line
2.39.3 (Apple Git-146)
RFC PATCH 1/5 arm64: Provide dcache_by_myline_op_nosync helper
From: Barry Song <[email protected]>
dcache_by_myline_op ensures completion of the data cache operations for a region, while dcache_by_myline_op_nosync only issues them without waiting. This enables deferred synchronization so completion for multiple regions can be handled together later.
--- arch/arm64/include/asm/assembler.h | 79 ++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 20 deletions(-)
arch/arm64/include/asm/assembler.h +59 -20
@@ -366,22 +366,7 @@ alternative_else alternative_endif .endm -/* -* Macro to perform a data cache maintenance for the interval -* [start, end) with dcache line size explicitly provided. -* -* op: operation passed to dc instruction -* domain: domain used in dsb instruciton -* start: starting virtual address of the region -* end: end virtual address of the region -* linesz: dcache line size -* fixup: optional label to branch to on user fault -* Corrupts: start, end, tmp -*/ - .macro dcache_by_myline_op op, domain, start, end, linesz, tmp, fixup - sub \tmp, \linesz, #1 - bic \start, \start, \tmp -.Ldcache_op\@: + .macro __dcache_op_line op, start .ifc \op, cvau __dcache_op_workaround_clean_cache \op, \start .else
@@ -399,14 +384,54 @@ alternative_endif .endif .endif .endif - add \start, \start, \linesz - cmp \start, \end - b.lo .Ldcache_op\@ - dsb \domain + .endm + +/* +* Macro to perform a data cache maintenance for the interval +* [start, end) with dcache line size explicitly provided. +* +* op: operation passed to dc instruction +* domain: domain used in dsb instruciton +* start: starting virtual address of the region +* end: end virtual address of the region +* linesz: dcache line size +* fixup: optional label to branch to on user fault +* Corrupts: start, end, tmp +*/ + .macro dcache_by_myline_op op, domain, start, end, linesz, tmp, fixup + sub \tmp, \linesz, #1 + bic \start, \start, \tmp +.Ldcache_op\@: + __dcache_op_line \op, \start + add \start, \start, \linesz + cmp \start, \end + b.lo .Ldcache_op\@ + dsb \domain _cond_uaccess_extable .Ldcache_op\@, \fixup .endm +/* +* Macro to perform a data cache maintenance for the interval +* [start, end) with dcache line size explicitly provided. +* It won't wait for the completion of the dc operation. +* +* op: operation passed to dc instruction +* start: starting virtual address of the region +* end: end virtual address of the region +* linesz: dcache line size +* Corrupts: start, end, tmp +*/ + .macro dcache_by_myline_op_nosync op, start, end, linesz, tmp + sub \tmp, \linesz, #1 + bic \start, \start, \tmp +.Ldcache_op\@: + __dcache_op_line \op, \start + add \start, \start, \linesz + cmp \start, \end + b.lo .Ldcache_op\@ + .endm +/* * Macro to perform a data cache maintenance for the interval * [start, end)
@@ -423,6 +448,20 @@ alternative_endif dcache_by_myline_op \op, \domain, \start, \end, \tmp1, \tmp2, \fixup .endm +/* +* Macro to perform a data cache maintenance for the interval +* [start, end). It won’t wait for the dc operation to complete. +* +* op: operation passed to dc instruction +* start: starting virtual address of the region +* end: end virtual address of the region +* Corrupts: start, end, tmp1, tmp2 +*/ + .macro dcache_by_line_op_nosync op, start, end, tmp1, tmp2 + dcache_line_size \tmp1, \tmp2 + dcache_by_myline_op_nosync \op, \start, \end, \tmp1, \tmp2 + .endm +/* * Macro to perform an instruction cache maintenance for the interval * [start, end)
Signature · 1 line
2.39.3 (Apple Git-146)
RFC PATCH 2/5 arm64: Provide dcache_clean_poc_nosync helper
In reply to Barry SongFrom: Barry Song <[email protected]>
dcache_clean_poc_nosync does not wait for the data cache clean to complete. Later, we wait for completion of all scatter-gather entries together.
--- arch/arm64/include/asm/cacheflush.h | 1 + arch/arm64/mm/cache.S | 15 +++++++++++++++ 2 files changed, 16 insertions(+)
arch/arm64/include/asm/cacheflush.h +1 -0
@@ -74,6 +74,7 @@ extern void icache_inval_pou(unsigned long start, unsigned long end);extern void dcache_clean_inval_poc (unsigned long start,unsigned long end);extern void dcache_inval_poc (unsigned long start,unsigned long end);extern void dcache_clean_poc (unsigned long start,unsigned long end); +extern void dcache_clean_poc_nosync (unsigned long start,unsigned long end);extern void dcache_clean_pop (unsigned long start,unsigned long end);extern void dcache_clean_pou (unsigned long start,unsigned long end);extern long caches_clean_inval_user_pou (unsigned long start,unsigned long end);
arch/arm64/mm/cache.S +15 -0
@@ -178,6 +178,21 @@ SYM_FUNC_START(__pi_dcache_clean_poc) SYM_FUNC_END(__pi_dcache_clean_poc) SYM_FUNC_ALIAS(dcache_clean_poc, __pi_dcache_clean_poc) +/* + * dcache_clean_poc_nosync(start, end) + * + * Issue the instructions of D-cache lines for the interval [start, end). + * not necessarily cleaned to the PoC till an explicit dsb sy afterward. + * + * - start - virtual start address of region + * - end - virtual end address of region + */ +SYM_FUNC_START(__pi_dcache_clean_poc_nosync) + dcache_by_line_op_nosync cvac, x0, x1, x2, x3 + ret +SYM_FUNC_END(__pi_dcache_clean_poc_nosync) +SYM_FUNC_ALIAS(dcache_clean_poc_nosync, __pi_dcache_clean_poc_nosync) + /* * dcache_clean_pop(start, end) *
Signature · 1 line
2.39.3 (Apple Git-146)
RFC PATCH 3/5 arm64: Provide dcache_inval_poc_nosync helper
In reply to Barry SongFrom: Barry Song <[email protected]>
dcache_inval_poc_nosync does not wait for the data cache invalidation to complete. Later, we defer the synchronization so we can wait for all SG entries together.
--- arch/arm64/include/asm/cacheflush.h | 1 + arch/arm64/mm/cache.S | 43 +++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 11 deletions(-)
arch/arm64/include/asm/cacheflush.h +1 -0
@@ -74,6 +74,7 @@ extern void icache_inval_pou(unsigned long start, unsigned long end);extern void dcache_clean_inval_poc (unsigned long start,unsigned long end);extern void dcache_inval_poc (unsigned long start,unsigned long end);extern void dcache_clean_poc (unsigned long start,unsigned long end); +extern void dcache_inval_poc_nosync (unsigned long start,unsigned long end);extern void dcache_clean_poc_nosync (unsigned long start,unsigned long end);extern void dcache_clean_pop (unsigned long start,unsigned long end);extern void dcache_clean_pou (unsigned long start,unsigned long end);
arch/arm64/mm/cache.S +32 -11
60 lines not on this page · raw message
Signature · 1 line
2.39.3 (Apple Git-146)
RFC PATCH 4/5 arm64: Provide arch_sync_dma_ batched helpers
In reply to Barry SongFrom: Barry Song <[email protected]>
arch_sync_dma_for_device_batch_add() and arch_sync_dma_for_cpu_batch_add() batch the DMA sync operations, and arch_sync_dma_batch_flush() waits for their completion all together.
--- arch/arm64/Kconfig | 1 + arch/arm64/mm/dma-mapping.c | 24 ++++++++++++++++++++++++ include/linux/dma-map-ops.h | 8 ++++++++ kernel/dma/Kconfig | 3 +++ 4 files changed, 36 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -112,6 +112,7 @@ config ARM64 select ARCH_SUPPORTS_SCHED_CLUSTER select ARCH_SUPPORTS_SCHED_MC select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH + select ARCH_WANT_BATCHED_DMA_SYNC select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT select ARCH_WANT_DEFAULT_BPF_JIT select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
arch/arm64/mm/dma-mapping.c +24 -0
@@ -31,6 +31,30 @@ void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,dcache_inval_poc (start, start + size); } +void arch_sync_dma_for_device_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +unsigned long start = (unsigned long )phys_to_virt (paddr); + +dcache_clean_poc_nosync (start, start + size); +} + +void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +unsigned long start = (unsigned long )phys_to_virt (paddr); + +if (dir ==DMA_TO_DEVICE ) +return ; + +dcache_inval_poc_nosync (start, start + size); +} + +void arch_sync_dma_batch_flush (void ) +{ +dsb (sy); +} +void arch_dma_prep_coherent (struct page *page,size_t size) {unsigned long start = (unsigned long )page_address (page);
include/linux/dma-map-ops.h +8 -0
@@ -367,6 +367,14 @@ static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, }#endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */ +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +void arch_sync_dma_for_device_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir); +void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir); +void arch_sync_dma_batch_flush (void ); +#endif +#ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL void arch_sync_dma_for_cpu_all (void );#else
kernel/dma/Kconfig +3 -0
@@ -78,6 +78,9 @@ config ARCH_HAS_DMA_PREP_COHERENT config ARCH_HAS_FORCE_DMA_UNENCRYPTED bool +config ARCH_WANT_BATCHED_DMA_SYNC + bool + # # Select this option if the architecture assumes DMA devices are coherent # by default.
Signature · 1 line
2.39.3 (Apple Git-146)
RFC PATCH 5/5 dma-mapping: Allow batched DMA sync operations if supported by the arch
In reply to Barry SongFrom: Barry Song <[email protected]>
This enables dma_direct_sync_sg_for_device, dma_direct_sync_sg_for_cpu, dma_direct_map_sg, and dma_direct_unmap_sg to use batched DMA sync operations when possible. This significantly improves performance on devices without hardware cache coherence.
Tangquan's initial results show that batched synchronization can reduce dma_map_sg() time by 64.61% and dma_unmap_sg() time by 66.60% on an MTK phone platform (MediaTek Dimensity 9500). The tests were performed by pinning the task to CPU7 and fixing the CPU frequency at 2.6 GHz, running dma_map_sg() and dma_unmap_sg() on 10 MB buffers (10 MB / 4 KB sg entries per buffer) for 200 iterations and then averaging the results.
--- kernel/dma/direct.c | 53 +++++++++++++++++++++++++--- kernel/dma/direct.h | 86 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 123 insertions(+), 16 deletions(-)
kernel/dma/direct.c +48 -5
@@ -403,9 +403,16 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir); if (!dev_is_dma_coherent(dev)) -arch_sync_dma_for_device (paddr, sg->length, - dir); +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_for_device_batch_add (paddr, sg->length, dir); +#else +arch_sync_dma_for_device (paddr, sg->length, dir); +#endif } +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif }#endif
@@ -422,7 +429,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,phys_addr_t paddr =dma_to_phys (dev,sg_dma_address (sg)); if (!dev_is_dma_coherent(dev)) +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_for_cpu_batch_add (paddr, sg->length, dir); +#else arch_sync_dma_for_cpu (paddr, sg->length, dir); +#endif swiotlb_sync_single_for_cpu (dev, paddr, sg->length, dir);
@@ -430,8 +441,12 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,arch_dma_mark_clean (paddr, sg->length); } -if (!dev_is_dma_coherent (dev)) +if (!dev_is_dma_coherent (dev)) {arch_sync_dma_for_cpu_all (); +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_batch_flush (); +#endif + } }/*
@@ -443,14 +458,29 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, {struct scatterlist *sg;int i; +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +bool need_sync = false; +#endif for_each_sg (sgl, sg, nents, i) { -if (sg_dma_is_bus_address (sg)) +if (sg_dma_is_bus_address (sg)) {sg_dma_unmark_bus_address (sg); -else + }else { +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC + need_sync = true; +dma_direct_unmap_phys_batch_add (dev, sg->dma_address, +sg_dma_len (sg), dir, attrs); + +#else dma_direct_unmap_phys (dev, sg->dma_address,sg_dma_len (sg), dir, attrs); +#endif + } } +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +if (need_sync && !dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif }#endif
@@ -460,6 +490,9 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,struct pci_p2pdma_map_state p2pdma_state = {};struct scatterlist *sg;int i, ret; +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +bool need_sync = false; +#endif for_each_sg (sgl, sg, nents, i) {switch (pci_p2pdma_state (&p2pdma_state, dev,sg_page (sg))) {
@@ -471,8 +504,14 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,*/ break ;case PCI_P2PDMA_MAP_NONE : +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC + need_sync = true; + sg->dma_address =dma_direct_map_phys_batch_add (dev,sg_phys (sg), + sg->length, dir, attrs); +#else sg->dma_address =dma_direct_map_phys (dev,sg_phys (sg), sg->length, dir, attrs); +#endif if (sg->dma_address ==DMA_MAPPING_ERROR ) { ret = -EIO ; goto out_unmap;
@@ -490,6 +529,10 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,sg_dma_len (sg) = sg->length; } +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +if (need_sync && !dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif return nents; out_unmap:
kernel/dma/direct.h +75 -11
112 lines not on this page · raw message
Signature · 1 line
2.39.3 (Apple Git-146)
Catalin Marinas
maintainerRe: [RFC PATCH 5/5] dma-mapping: Allow batched DMA sync operations if supported by the arch
Quoted 12 lines · Barry Song
kernel/dma/direct.c +2
-2@@ -403,9 +403,16 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir); if (!dev_is_dma_coherent(dev)) -arch_sync_dma_for_device (paddr, sg->length, - dir); +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_for_device_batch_add (paddr, sg->length, dir);
kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 417
Quoted 10 lines
kernel/dma/direct.c +7
-0+#else +arch_sync_dma_for_device (paddr, sg->length, dir); +#endif } +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif }#endif
Just a high-level comment for now. I'm not opposed to the idea of batching the DSB barriers, we do this for ptes. However, the way it's implemented in the generic files, with lots of #ifdefs, makes the code pretty unreadable.
Can we have something like arch_sync_dma_begin/end() and let the arch code handle the barriers as they see fit?
Signature · 1 line
Catalin
Re: [RFC PATCH 5/5] dma-mapping: Allow batched DMA sync operations if supported by the arch
Quoted 19 lines · Catalin Marinas
Quoted 17 lines · Barry Song
kernel/dma/direct.c +6
-2@@ -403,9 +403,16 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir); if (!dev_is_dma_coherent(dev)) -arch_sync_dma_for_device (paddr, sg->length, - dir); +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_for_device_batch_add (paddr, sg->length, dir); +#else +arch_sync_dma_for_device (paddr, sg->length, dir); +#endif } +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC
Quoted 8 lines
kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 417
Quoted 5 lines
kernel/dma/direct.c +3
-0+if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif }#endif Just a high-level comment for now. I'm not opposed to the idea of batching the DSB barriers, we do this for ptes. However, the way it's
Thanks, Catalin. I agree we need batching, as phones and embedded systems could use many DMA buffers while some chips lack DMA-coherency.
Quoted 5 lines
implemented in the generic files, with lots of #ifdefs, makes the code pretty unreadable.
Can we have something like arch_sync_dma_begin/end() and let the arch code handle the barriers as they see fit?
I guess I can refactor it as below and then remove the #ifdef/#else/#endif blocks.
include/linux/dma-map-ops.h +14 -0
@@ -373,6 +373,20 @@ void arch_sync_dma_for_device_batch_add(phys_addr_t paddr, size_t size,void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size,enum dma_data_direction dir);void arch_sync_dma_batch_flush (void ); +#else +static inline void arch_sync_dma_for_device_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +arch_sync_dma_for_device (paddr, size, dir); +} +static inline void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +arch_sync_dma_for_cpu (paddr, size, dir); +} +static inline void arch_sync_dma_batch_flush (void ) +{ +}#endif #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
kernel/dma/direct.c +0 -31
@@ -403,16 +403,10 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir);if (!dev_is_dma_coherent (dev)) -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_for_device_batch_add(paddr, sg->length, dir); -#else -arch_sync_dma_for_device (paddr, sg->length, dir); -#endif } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (!dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif }#endif
@@ -429,11 +423,7 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,phys_addr_t paddr =dma_to_phys (dev,sg_dma_address (sg));if (!dev_is_dma_coherent (dev)) -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_for_cpu_batch_add (paddr, sg->length, dir); -#else -arch_sync_dma_for_cpu (paddr, sg->length, dir); -#endif swiotlb_sync_single_for_cpu (dev, paddr, sg->length, dir);
@@ -443,9 +433,7 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,if (!dev_is_dma_coherent (dev)) {arch_sync_dma_for_cpu_all (); -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_batch_flush (); -#endif } }
@@ -458,29 +446,19 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, {struct scatterlist *sg;int i; -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC bool need_sync = false; -#endif for_each_sg (sgl, sg, nents, i) {if (sg_dma_is_bus_address (sg)) {sg_dma_unmark_bus_address (sg); }else { -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC need_sync = true;dma_direct_unmap_phys_batch_add (dev, sg->dma_address,sg_dma_len (sg), dir, attrs); - -#else -dma_direct_unmap_phys (dev, sg->dma_address, -sg_dma_len (sg), dir, attrs); -#endif } } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (need_sync && !dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif }#endif
@@ -490,9 +468,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,struct pci_p2pdma_map_state p2pdma_state = {};struct scatterlist *sg;int i, ret; -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC bool need_sync = false; -#endif for_each_sg (sgl, sg, nents, i) {switch (pci_p2pdma_state (&p2pdma_state, dev,sg_page (sg))) {
@@ -504,14 +480,9 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,*/ break ;case PCI_P2PDMA_MAP_NONE : -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC need_sync = true; sg->dma_address =dma_direct_map_phys_batch_add (dev,sg_phys (sg), sg->length, dir, attrs); -#else - sg->dma_address =dma_direct_map_phys (dev,sg_phys (sg), - sg->length, dir, attrs); -#endif if (sg->dma_address ==DMA_MAPPING_ERROR ) { ret = -EIO ; goto out_unmap;
@@ -529,10 +500,8 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,sg_dma_len (sg) = sg->length; } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (need_sync && !dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif return nents; out_unmap:
Thanks
Barry
Marek Szyprowski
maintainerRe: [RFC PATCH 5/5] dma-mapping: Allow batched DMA sync operations if supported by the arch
Hi Barry,
Quoted 165 lines · Barry Song
Quoted 17 lines · Catalin Marinas
Quoted 16 lines · Barry Song
kernel/dma/direct.c +5
-2@@ -403,9 +403,16 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir); if (!dev_is_dma_coherent(dev)) -arch_sync_dma_for_device (paddr, sg->length, - dir); +#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +arch_sync_dma_for_device_batch_add (paddr, sg->length, dir); +#else +arch_sync_dma_for_device (paddr, sg->length, dir); +#endif }Quoted 8 lines
kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 417
Quoted 6 lines
kernel/dma/direct.c +4
-0+#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC +if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush (); +#endif }#endif Just a high-level comment for now. I'm not opposed to the idea of batching the DSB barriers, we do this for ptes. However, the way it's
Thanks, Catalin. I agree we need batching, as phones and embedded systems could use many DMA buffers while some chips lack DMA-coherency.
Quoted 5 lines
implemented in the generic files, with lots of #ifdefs, makes the code pretty unreadable.
Can we have something like arch_sync_dma_begin/end() and let the arch code handle the barriers as they see fit?
I guess I can refactor it as below and then remove the #ifdef/#else/#endif blocks.
include/linux/dma-map-ops.h +14
-0@@ -373,6 +373,20 @@ void arch_sync_dma_for_device_batch_add(phys_addr_t paddr, size_t size,void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size,enum dma_data_direction dir);void arch_sync_dma_batch_flush (void ); +#else +static inline void arch_sync_dma_for_device_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +arch_sync_dma_for_device (paddr, size, dir); +} +static inline void arch_sync_dma_for_cpu_batch_add (phys_addr_t paddr,size_t size, +enum dma_data_direction dir) +{ +arch_sync_dma_for_cpu (paddr, size, dir); +} +static inline void arch_sync_dma_batch_flush (void ) +{ +}#endif #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL kernel/dma/direct.c +0
-29@@ -403,16 +403,10 @@ void dma_direct_sync_sg_for_device(struct device *dev,swiotlb_sync_single_for_device (dev, paddr, sg->length, dir);if (!dev_is_dma_coherent (dev)) -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_for_device_batch_add(paddr, sg->length, dir); -#else -arch_sync_dma_for_device (paddr, sg->length, dir); -#endif } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (!dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif }#endif @@ -429,11 +423,7 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,phys_addr_t paddr =dma_to_phys (dev,sg_dma_address (sg));if (!dev_is_dma_coherent (dev)) -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_for_cpu_batch_add (paddr, sg->length, dir); -#else -arch_sync_dma_for_cpu (paddr, sg->length, dir); -#endif swiotlb_sync_single_for_cpu (dev, paddr, sg->length, dir);@@ -443,9 +433,7 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,if (!dev_is_dma_coherent (dev)) {arch_sync_dma_for_cpu_all (); -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC arch_sync_dma_batch_flush (); -#endif } }@@ -458,29 +446,19 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl, {struct scatterlist *sg;int i; -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC bool need_sync = false; -#endif for_each_sg (sgl, sg, nents, i) {if (sg_dma_is_bus_address (sg)) {sg_dma_unmark_bus_address (sg); }else { -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC need_sync = true;dma_direct_unmap_phys_batch_add (dev, sg->dma_address,sg_dma_len (sg), dir, attrs); - -#else -dma_direct_unmap_phys (dev, sg->dma_address, -sg_dma_len (sg), dir, attrs); -#endif } } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (need_sync && !dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif }#endif @@ -490,9 +468,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,struct pci_p2pdma_map_state p2pdma_state = {};struct scatterlist *sg;int i, ret; -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC bool need_sync = false; -#endif for_each_sg (sgl, sg, nents, i) {switch (pci_p2pdma_state (&p2pdma_state, dev,sg_page (sg))) {@@ -504,14 +480,9 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,*/ break ;case PCI_P2PDMA_MAP_NONE : -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC need_sync = true; sg->dma_address =dma_direct_map_phys_batch_add (dev,sg_phys (sg), sg->length, dir, attrs); -#else - sg->dma_address =dma_direct_map_phys (dev,sg_phys (sg), - sg->length, dir, attrs); -#endif if (sg->dma_address ==DMA_MAPPING_ERROR ) { ret = -EIO ; goto out_unmap;
kernel/dma/direct.c int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents, · line 507
Quoted 10 lines
kernel/dma/direct.c +0
-2@@ -529,10 +500,8 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,sg_dma_len (sg) = sg->length; } -#ifdef CONFIG_ARCH_WANT_BATCHED_DMA_SYNC if (need_sync && !dev_is_dma_coherent (dev))arch_sync_dma_batch_flush (); -#endif return nents; out_unmap:
This version looks a bit better to me. Similar batching could be added also to dma_iova_link()/dma_iova_sync() paths.
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Re: [RFC PATCH 5/5] dma-mapping: Allow batched DMA sync operations if supported by the arch
Quoted 3 lines · Marek Szyprowski
Hi Barry,
[...]
Quoted 2 lines
This version looks a bit better to me. Similar batching could be added also to dma_iova_link()/dma_iova_sync() paths.
Thanks, Marek. I will respin a new version. For dma_iova, I assume you meant something like the following?
drivers/iommu/dma-iommu.c +3 -1
@@ -1837,7 +1837,7 @@ static int __dma_iova_link(struct device *dev, dma_addr_t addr,int prot =dma_info_to_prot (dir, coherent, attrs);if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC |DMA_ATTR_MMIO ))) -arch_sync_dma_for_device (phys, size, dir); +arch_sync_dma_for_device_batch_add (phys, size, dir);return iommu_map_nosync (iommu_get_dma_domain (dev), addr, phys, size, prot,GFP_ATOMIC );
@@ -1980,6 +1980,8 @@ int dma_iova_sync(struct device *dev, struct dma_iova_state *state,dma_addr_t addr = state->addr + offset;size_t iova_start_pad =iova_offset (iovad, addr); +if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush ();return iommu_sync_map (domain, addr - iova_start_pad,iova_align (iovad, size + iova_start_pad)); }
If so, I don't really have such hardware to test. I wonder if I can make it as patch 6/6 when respinning, and still mark it as RFC v2. Or should I leave it as is and expect someone with the hardware to test and send it?
Thanks
Barry
Marek Szyprowski
maintainerRe: [RFC PATCH 5/5] dma-mapping: Allow batched DMA sync operations if supported by the arch
Quoted 30 lines · Barry Song
On Sat, Nov 22, 2025 at 12:09 AM Marek Szyprowski <[email protected]> wrote:
[...]Quoted 2 lines
This version looks a bit better to me. Similar batching could be added also to dma_iova_link()/dma_iova_sync() paths.
Thanks, Marek. I will respin a new version. For
dma_iova, I assume you meant something like the following?drivers/iommu/dma-iommu.c +3
-1@@ -1837,7 +1837,7 @@ static int __dma_iova_link(struct device *dev, dma_addr_t addr,int prot =dma_info_to_prot (dir, coherent, attrs);if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC |DMA_ATTR_MMIO ))) -arch_sync_dma_for_device (phys, size, dir); +arch_sync_dma_for_device_batch_add (phys, size, dir);return iommu_map_nosync (iommu_get_dma_domain (dev), addr, phys, size, prot,GFP_ATOMIC );@@ -1980,6 +1980,8 @@ int dma_iova_sync(struct device *dev, struct dma_iova_state *state,dma_addr_t addr = state->addr + offset;size_t iova_start_pad =iova_offset (iovad, addr); +if (!dev_is_dma_coherent (dev)) +arch_sync_dma_batch_flush ();return iommu_sync_map (domain, addr - iova_start_pad,iova_align (iovad, size + iova_start_pad)); }
Quoted 3 lines
If so, I don't really have such hardware to test. I wonder if I can make it as patch 6/6 when respinning, and still mark it as RFC v2. Or should I leave it as is and expect someone with the hardware to test and send it?
Yes, I meant something like the above diff, also for dma_iova_unlink(). It can be an additional, 6th patch with RFC tag, assuming You have no way to test it.
Please notice that I've just sent a patch touching the similar code paths:
https://lore.kernel.org/all/[email protected]/
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Quoted 21 lines · Barry Song
From: Barry Song <[email protected]>
Many embedded ARM64 SoCs still lack hardware cache coherency support, which causes DMA mapping operations to appear as hotspots in on-CPU flame graphs.
For an SG list with *nents* entries, the current dma_map/unmap_sg() and DMA sync APIs perform cache maintenance one entry at a time. After each entry, the implementation synchronously waits for the corresponding region’s D-cache operations to complete. On architectures like arm64, efficiency can be improved by issuing all entries’ operations first and then performing a single batched wait for completion.
Tangquan's initial results show that batched synchronization can reduce
dma_map_sg()time by 64.61% anddma_unmap_sg()time by 66.60% on an MTK phone platform (MediaTek Dimensity 9500). The tests were performed by pinning the task to CPU7 and fixing the CPU frequency at 2.6 GHz, runningdma_map_sg()anddma_unmap_sg()on 10 MB buffers (10 MB / 4 KB sg entries per buffer) for 200 iterations and then averaging the results.
Quoted 8 lines
Barry Song (5):
arm64: Providedcache_by_myline_op_nosynchelper
arm64: Providedcache_clean_poc_nosynchelper
arm64: Providedcache_inval_poc_nosynchelper
arm64: Providearch_sync_dma_batched helpers
dma-mapping: Allow batched DMA sync operations if supported by the
arch
Hi Catalin, Will, Marek, Robin, and all,
Do you have any feedback on this before I send the formal patchset (dropping the RFC tag)?
Thanks
Barry