Skip to contentSkip to conversation

dma-mapping: arm64: support batched cache sync

PATCH Superseded Barry Song 6 participants 32 messages Dec 26, 2025 – Jan 26

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

v2 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 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.

I also ran this patch set on an RK3588 Rock5B+ board and observed that millions of DMA sync operations were batched.

v2:
* Refine a large amount of arm64 asm code based on feedback from Robin, thanks!
* Drop batch_add APIs and always use arch_sync_dma_for_* + flush, even for a single buffer, based on Leon’s suggestion, thanks!
* Refine a large amount of code based on feedback from Leon, thanks!
* Also add batch support for iommu_dma_sync_sg_for_{cpu,device}
v1 link:
https://lore.kernel.org/lkml/[email protected]/

v1, diff with RFC:
* Drop a large number of #ifdef/#else/#endif blocks based on feedback from Catalin and Marek, thanks!
* Also add batched iova link/unlink support, marked as RFC since I lack the required hardware. This was suggested by Marek, thanks!
RFC link:
https://lore.kernel.org/lkml/[email protected]/

Barry Song (8):
arm64: Provide dcache_by_myline_op_nosync helper
arm64: Provide dcache_clean_poc_nosync helper
arm64: Provide dcache_inval_poc_nosync helper
dma-mapping: Separate DMA sync issuing and completion waiting
dma-mapping: Support batch mode for dma_direct_sync_sg_for_*
dma-mapping: Support batch mode for dma_direct_{map,unmap}_sg
dma-iommu: Support DMA sync batch mode for IOVA link and unlink
dma-iommu: Support DMA sync batch mode for iommu_dma_sync_sg_for_{cpu,

    device}
 arch/arm64/include/asm/assembler.h  | 24 +++++++++---
 arch/arm64/include/asm/cache.h      |  6 +++
 arch/arm64/include/asm/cacheflush.h |  2 +
 arch/arm64/kernel/relocate_kernel.S |  3 +-
 arch/arm64/mm/cache.S               | 57 +++++++++++++++++++++++------
 arch/arm64/mm/dma-mapping.c         |  4 +-
 drivers/iommu/dma-iommu.c           | 35 ++++++++++++++----
 drivers/xen/swiotlb-xen.c           | 24 ++++++++----
 include/linux/dma-map-ops.h         |  6 +++
 kernel/dma/direct.c                 | 23 +++++++++---
 kernel/dma/direct.h                 | 21 ++++++++---
 kernel/dma/mapping.c                |  6 +--
 kernel/dma/swiotlb.c                |  4 +-
 13 files changed, 165 insertions(+), 50 deletions(-)

Cc: Leon Romanovsky <[email protected]>
Cc: Marek Szyprowski <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Ada Couprie Diaz <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Marc Zyngier <[email protected]>
Cc: Anshuman Khandual <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Suren Baghdasaryan <[email protected]>
Cc: Robin Murphy <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: Juergen Gross <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Oleksandr Tyshchenko <[email protected]>
Cc: Tangquan Zheng <[email protected]>
Cc: Huacai Zhou <[email protected]>
--
2.43.0

author
#1 lore raw

PATCH v2 1/8 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.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/assembler.h  | 24 +++++++++++++++++++-----
 arch/arm64/kernel/relocate_kernel.S |  3 ++-
 2 files changed, 21 insertions(+), 6 deletions(-)
arch/arm64/include/asm/assembler.h +19 -5
index f0ca7196f6fa..b408ed61866f 100644
@@ -371,14 +371,13 @@ alternative_endif
  * [start, end) with dcache line size explicitly provided.
  *
  * 	op:		operation passed to dc instruction
- * 	domain:		domain used in dsb instruction
  * 	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
+	.macro raw_dcache_by_myline_op op, start, end, linesz, tmp, fixup
 	sub	\tmp, \linesz, #1
 	bic	\start, \start, \tmp
 .Ldcache_op\@:
@@ -402,14 +401,13 @@ alternative_endif
 	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)
+ * [start, end) and wait for completion
  *
  * 	op:		operation passed to dc instruction
  * 	domain:		domain used in dsb instruction
@@ -420,7 +418,23 @@ alternative_endif
  */
 	.macro dcache_by_line_op op, domain, start, end, tmp1, tmp2, fixup
 	dcache_line_size \tmp1, \tmp2
-	dcache_by_myline_op \op, \domain, \start, \end, \tmp1, \tmp2, \fixup
+	raw_dcache_by_myline_op \op, \start, \end, \tmp1, \tmp2, \fixup
+	dsb \domain
+	.endm
+
+/*
+ * Macro to perform a data cache maintenance for the interval
+ * [start, end) without waiting for completion
+ *
+ * 	op:		operation passed to dc instruction
+ * 	start:          starting virtual address of the region
+ * 	end:            end virtual address of the region
+ * 	fixup:		optional label to branch to on user fault
+ * 	Corrupts:       start, end, tmp1, tmp2
+ */
+	.macro dcache_by_line_op_nosync op, start, end, tmp1, tmp2, fixup
+	dcache_line_size \tmp1, \tmp2
+	raw_dcache_by_myline_op \op, \start, \end, \tmp1, \tmp2, \fixup
 	.endm
 
 /*
arch/arm64/kernel/relocate_kernel.S +2 -1
index 413f899e4ac6..71938eb3a3a3 100644
@@ -64,7 +64,8 @@ SYM_CODE_START(arm64_relocate_new_kernel)
 	mov	x19, x13
 	copy_page x13, x12, x1, x2, x3, x4, x5, x6, x7, x8
 	add	x1, x19, #PAGE_SIZE
-	dcache_by_myline_op civac, sy, x19, x1, x15, x20
+	raw_dcache_by_myline_op civac, x19, x1, x15, x20
+	dsb	sy
 	b	.Lnext
 .Ltest_indirection:
 	tbz	x16, IND_INDIRECTION_BIT, .Ltest_destination
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v2 1/8] arm64: Provide dcache_by_myline_op_nosync helper

Quoted 46 lines · Barry Song

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.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/assembler.h  | 24 +++++++++++++++++++-----
 arch/arm64/kernel/relocate_kernel.S |  3 ++-
 2 files changed, 21 insertions(+), 6 deletions(-)
arch/arm64/include/asm/assembler.h +1 -2
index f0ca7196f6fa..b408ed61866f 100644
@@ -371,14 +371,13 @@ alternative_endif
  * [start, end) with dcache line size explicitly provided.
  *
  * 	op:		operation passed to dc instruction
- * 	domain:		domain used in dsb instruction
  * 	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
+	.macro raw_dcache_by_myline_op op, start, end, linesz, tmp, fixup
 	sub	\tmp, \linesz, #1
 	bic	\start, \start, \tmp
 .Ldcache_op\@:

arch/arm64/include/asm/assembler.h alternative_endif · line 405

Quoted 4 lines
arch/arm64/include/asm/assembler.h +0 -1
@@ -402,14 +401,13 @@ alternative_endif
 	add	\start, \start, \linesz
 	cmp	\start, \end
 	b.lo	.Ldcache_op\@
-	dsb	\domain

Naming nit, but I'd prefer this to be dcache_by_myline_op_nosync() for consistency with the other macros that you're adding. The 'raw' prefix is used by raw_dcache_line_size() to indicate that we're getting the value from the underlying hardware register.

Quoted 12 lines
arch/arm64/include/asm/assembler.h +1 -1
 	_cond_uaccess_extable .Ldcache_op\@, \fixup
 	.endm
 
 /*
  * Macro to perform a data cache maintenance for the interval
- * [start, end)
+ * [start, end) and wait for completion
  *
  * 	op:		operation passed to dc instruction
  * 	domain:		domain used in dsb instruction

arch/arm64/include/asm/assembler.h alternative_endif · line 423

Quoted 7 lines
arch/arm64/include/asm/assembler.h +3 -1
@@ -420,7 +418,23 @@ alternative_endif
  */
 	.macro dcache_by_line_op op, domain, start, end, tmp1, tmp2, fixup
 	dcache_line_size \tmp1, \tmp2
-	dcache_by_myline_op \op, \domain, \start, \end, \tmp1, \tmp2, \fixup
+	raw_dcache_by_myline_op \op, \start, \end, \tmp1, \tmp2, \fixup
+	dsb \domain
+	.endm

This could just be dcache_by_line_op_nosync() + dsb.

Will

author
#3 lore raw

Re: [PATCH v2 1/8] arm64: Provide dcache_by_myline_op_nosync helper

[...]

Quoted 8 lines
Quoted 8 lines

*/

arch/arm64/include/asm/assembler.h +1 -1
-     .macro dcache_by_myline_op op, domain, start, end, linesz, tmp, fixup
+     .macro raw_dcache_by_myline_op op, start, end, linesz, tmp, fixup
      sub     \tmp, \linesz, #1
      bic     \start, \start, \tmp
 .Ldcache_op\@:
@@ -402,14 +401,13 @@ alternative_endif
      add     \start, \start, \linesz
Quoted 8 lines

arch/arm64/include/asm/assembler.h alternative_endif · line 405

Quoted 3 lines
arch/arm64/include/asm/assembler.h +0 -1
      cmp     \start, \end
      b.lo    .Ldcache_op\@
-     dsb     \domain

Naming nit, but I'd prefer this to be dcache_by_myline_op_nosync() for consistency with the other macros that you're adding. The 'raw' prefix is used by raw_dcache_line_size() to indicate that we're getting the value from the underlying hardware register.

Ok. thanks!

Quoted 14 lines
Quoted 14 lines
arch/arm64/include/asm/assembler.h +1 -1
      _cond_uaccess_extable .Ldcache_op\@, \fixup
      .endm
 
 /*
  * Macro to perform a data cache maintenance for the interval
- * [start, end)
+ * [start, end) and wait for completion
  *
  *   op:             operation passed to dc instruction
  *   domain:         domain used in dsb instruction
@@ -420,7 +418,23 @@ alternative_endif
  */
Quoted 8 lines

arch/arm64/include/asm/assembler.h alternative_endif · line 423

Quoted 6 lines
arch/arm64/include/asm/assembler.h +3 -1
      .macro dcache_by_line_op op, domain, start, end, tmp1, tmp2, fixup
      dcache_line_size \tmp1, \tmp2
-     dcache_by_myline_op \op, \domain, \start, \end, \tmp1, \tmp2, \fixup
+     raw_dcache_by_myline_op \op, \start, \end, \tmp1, \tmp2, \fixup
+     dsb \domain
+     .endm

This could just be dcache_by_line_op_nosync() + dsb.

Ok. thanks!

Best Regards
Barry

author
#4 lore raw

PATCH v2 2/8 arm64: Provide dcache_clean_poc_nosync helper

In reply to Barry Song

From: 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.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 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
index 28ab96e808ef..9b6d0a62cf3d 100644
@@ -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
index 503567c864fd..4a7c7e03785d 100644
@@ -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.43.0
author
#5 lore raw

PATCH v2 3/8 arm64: Provide dcache_inval_poc_nosync helper

In reply to Barry Song

From: 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.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cacheflush.h |  1 +
 arch/arm64/mm/cache.S               | 42 +++++++++++++++++++++--------
 2 files changed, 32 insertions(+), 11 deletions(-)
arch/arm64/include/asm/cacheflush.h +1 -0
index 9b6d0a62cf3d..382b4ac3734d 100644
@@ -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 +31 -11

59 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#6 lore raw

Re: [PATCH v2 3/8] arm64: Provide dcache_inval_poc_nosync helper

Quoted 85 lines · Barry Song

From: 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.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cacheflush.h |  1 +
 arch/arm64/mm/cache.S               | 42 +++++++++++++++++++++--------
 2 files changed, 32 insertions(+), 11 deletions(-)
arch/arm64/include/asm/cacheflush.h +1 -0
index 9b6d0a62cf3d..382b4ac3734d 100644
@@ -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 +18 -11
index 4a7c7e03785d..99a093d3aecb 100644
@@ -132,17 +132,7 @@ alternative_else_nop_endif
 	ret
 SYM_FUNC_END(dcache_clean_pou)
 
-/*
- *	dcache_inval_poc(start, end)
- *
- * 	Ensure that any D-cache lines for the interval [start, end)
- * 	are invalidated. Any partial lines at the ends of the interval are
- *	also cleaned to PoC to prevent data loss.
- *
- *	- start   - kernel start address of region
- *	- end     - kernel end address of region
- */
-SYM_FUNC_START(__pi_dcache_inval_poc)
+.macro raw_dcache_inval_poc_macro
 	dcache_line_size x2, x3
 	sub	x3, x2, #1
 	tst	x1, x3				// end cache line aligned?
@@ -158,11 +148,41 @@ SYM_FUNC_START(__pi_dcache_inval_poc)
 3:	add	x0, x0, x2
 	cmp	x0, x1
 	b.lo	2b
+.endm
+
+/*
+ *	dcache_inval_poc(start, end)
+ *
+ * 	Ensure that any D-cache lines for the interval [start, end)
+ * 	are invalidated. Any partial lines at the ends of the interval are
+ *	also cleaned to PoC to prevent data loss.
+ *
+ *	- start   - kernel start address of region
+ *	- end     - kernel end address of region
+ */
+SYM_FUNC_START(__pi_dcache_inval_poc)
+	raw_dcache_inval_poc_macro
 	dsb	sy
 	ret
 SYM_FUNC_END(__pi_dcache_inval_poc)
 SYM_FUNC_ALIAS(dcache_inval_poc, __pi_dcache_inval_poc)
 
+/*
+ *	dcache_inval_poc_nosync(start, end)
+ *

arch/arm64/mm/cache.S SYM_FUNC_START(__pi_dcache_inval_poc) · line 182

Quoted 10 lines
arch/arm64/mm/cache.S +10 -0
+ * 	Issue the instructions of D-cache lines for the interval [start, end)
+ * 	for invalidation. Not necessarily cleaned to PoC till an explicit dsb
+ *	sy is issued later
+ *
+ *	- start   - kernel start address of region
+ *	- end     - kernel end address of region
+ */
+SYM_FUNC_START(__pi_dcache_inval_poc_nosync)
+	raw_dcache_inval_poc_macro
+	ret

Sorry, similar naming nit to the other patch. Let's have the macro use the 'nosync' suffix instead of the 'raw' prefix. You can chuck some underscores at it if you want to keep the name of this function the same.

Will

author
#7 lore raw

PATCH v2 4/8 dma-mapping: Separate DMA sync issuing and completion waiting

In reply to Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)
arch/arm64/include/asm/cache.h +6 -0
index dd2c8586a725..487fb7c355ed 100644
@@ -87,6 +87,12 @@ int cache_line_size(void);
 
 #define dma_get_cache_alignment	cache_line_size
 
+static inline void arch_sync_dma_flush(void)
+{
+	dsb(sy);
+}
+#define arch_sync_dma_flush arch_sync_dma_flush
+
 /* Compress a u64 MPIDR value into 32 bits. */
 static inline u64 arch_compact_of_hwid(u64 id)
 {
arch/arm64/mm/dma-mapping.c +2 -2
index b2b5792b2caa..ae1ae0280eef 100644
@@ -17,7 +17,7 @@ void arch_sync_dma_for_device(phys_addr_t paddr, size_t size,
 {
 	unsigned long start = (unsigned long)phys_to_virt(paddr);
 
-	dcache_clean_poc(start, start + size);
+	dcache_clean_poc_nosync(start, start + size);
 }
 
 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
@@ -28,7 +28,7 @@ void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
 	if (dir == DMA_TO_DEVICE)
 		return;
 
-	dcache_inval_poc(start, start + size);
+	dcache_inval_poc_nosync(start, start + size);
 }
 
 void arch_dma_prep_coherent(struct page *page, size_t size)
drivers/iommu/dma-iommu.c +27 -10
index c92088855450..6827763a3877 100644
@@ -1095,8 +1095,10 @@ void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
 		return;
 
 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
-	if (!dev_is_dma_coherent(dev))
+	if (!dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_cpu(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 
 	swiotlb_sync_single_for_cpu(dev, phys, size, dir);
 }
@@ -1112,8 +1114,10 @@ void iommu_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
 	swiotlb_sync_single_for_device(dev, phys, size, dir);
 
-	if (!dev_is_dma_coherent(dev))
+	if (!dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_device(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 }
 
 void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
@@ -1122,13 +1126,16 @@ void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
 	struct scatterlist *sg;
 	int i;
 
-	if (sg_dma_is_swiotlb(sgl))
+	if (sg_dma_is_swiotlb(sgl)) {
 		for_each_sg(sgl, sg, nelems, i)
 			iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
 						      sg->length, dir);
-	else if (!dev_is_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i)
+	} else if (!dev_is_dma_coherent(dev)) {
+		for_each_sg(sgl, sg, nelems, i) {
 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
+			arch_sync_dma_flush();
+		}
+	}
 }
 
 void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
@@ -1143,8 +1150,10 @@ void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
 							 sg_dma_address(sg),
 							 sg->length, dir);
 	else if (!dev_is_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i)
+		for_each_sg(sgl, sg, nelems, i) {
 			arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
+			arch_sync_dma_flush();
+		}
 }
 
 static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,
@@ -1219,8 +1228,10 @@ dma_addr_t iommu_dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
 			return DMA_MAPPING_ERROR;
 	}
 
-	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
+	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
 		arch_sync_dma_for_device(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 
 	iova = __iommu_dma_map(dev, phys, size, prot, dma_mask);
 	if (iova == DMA_MAPPING_ERROR && !(attrs & DMA_ATTR_MMIO))
@@ -1242,8 +1253,10 @@ void iommu_dma_unmap_phys(struct device *dev, dma_addr_t dma_handle,
 	if (WARN_ON(!phys))
 		return;
 
-	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev))
+	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_cpu(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 
 	__iommu_dma_unmap(dev, dma_handle, size);
 
@@ -1836,8 +1849,10 @@ static int __dma_iova_link(struct device *dev, dma_addr_t addr,
 	bool coherent = dev_is_dma_coherent(dev);
 	int prot = dma_info_to_prot(dir, coherent, attrs);
 
-	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
+	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
 		arch_sync_dma_for_device(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 
 	return iommu_map_nosync(iommu_get_dma_domain(dev), addr, phys, size,
 			prot, GFP_ATOMIC);
@@ -2008,8 +2023,10 @@ static void iommu_dma_iova_unlink_range_slow(struct device *dev,
 			end - addr, iovad->granule - iova_start_pad);
 
 		if (!dev_is_dma_coherent(dev) &&
-		    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
+		    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
 			arch_sync_dma_for_cpu(phys, len, dir);
+			arch_sync_dma_flush();
+		}
 
 		swiotlb_tbl_unmap_single(dev, phys, len, dir, attrs);
 
drivers/xen/swiotlb-xen.c +16 -8
index ccf25027bec1..b79917e785a5 100644
@@ -262,10 +262,12 @@ static dma_addr_t xen_swiotlb_map_phys(struct device *dev, phys_addr_t phys,
 
 done:
 	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dev_addr))))
+		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dev_addr)))) {
 			arch_sync_dma_for_device(phys, size, dir);
-		else
+			arch_sync_dma_flush();
+		} else {
 			xen_dma_sync_for_device(dev, dev_addr, size, dir);
+		}
 	}
 	return dev_addr;
 }
@@ -287,10 +289,12 @@ static void xen_swiotlb_unmap_phys(struct device *hwdev, dma_addr_t dev_addr,
 	BUG_ON(dir == DMA_NONE);
 
 	if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(hwdev, dev_addr))))
+		if (pfn_valid(PFN_DOWN(dma_to_phys(hwdev, dev_addr)))) {
 			arch_sync_dma_for_cpu(paddr, size, dir);
-		else
+			arch_sync_dma_flush();
+		} else {
 			xen_dma_sync_for_cpu(hwdev, dev_addr, size, dir);
+		}
 	}
 
 	/* NOTE: We use dev_addr here, not paddr! */
@@ -308,10 +312,12 @@ xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr,
 	struct io_tlb_pool *pool;
 
 	if (!dev_is_dma_coherent(dev)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
+		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr)))) {
 			arch_sync_dma_for_cpu(paddr, size, dir);
-		else
+			arch_sync_dma_flush();
+		} else {
 			xen_dma_sync_for_cpu(dev, dma_addr, size, dir);
+		}
 	}
 
 	pool = xen_swiotlb_find_pool(dev, dma_addr);
@@ -331,10 +337,12 @@ xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr,
 		__swiotlb_sync_single_for_device(dev, paddr, size, dir, pool);
 
 	if (!dev_is_dma_coherent(dev)) {
-		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
+		if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr)))) {
 			arch_sync_dma_for_device(paddr, size, dir);
-		else
+			arch_sync_dma_flush();
+		} else {
 			xen_dma_sync_for_device(dev, dma_addr, size, dir);
+		}
 	}
 }
 
include/linux/dma-map-ops.h +6 -0
index 4809204c674c..e7dd8a63b40e 100644
@@ -361,6 +361,12 @@ static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
 }
 #endif /* ARCH_HAS_SYNC_DMA_FOR_CPU */
 
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif
+
 #ifdef CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL
 void arch_sync_dma_for_cpu_all(void);
 #else
kernel/dma/direct.c +6 -2
index 50c3fe2a1d55..a219911c7b90 100644
@@ -402,9 +402,11 @@ 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))
+		if (!dev_is_dma_coherent(dev)) {
 			arch_sync_dma_for_device(paddr, sg->length,
 					dir);
+			arch_sync_dma_flush();
+		}
 	}
 }
 #endif
@@ -421,8 +423,10 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
 	for_each_sg(sgl, sg, nents, i) {
 		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-		if (!dev_is_dma_coherent(dev))
+		if (!dev_is_dma_coherent(dev)) {
 			arch_sync_dma_for_cpu(paddr, sg->length, dir);
+			arch_sync_dma_flush();
+		}
 
 		swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 
kernel/dma/direct.h +7 -2
index da2fadf45bcd..a69326eed266 100644
@@ -60,8 +60,10 @@ static inline void dma_direct_sync_single_for_device(struct device *dev,
 
 	swiotlb_sync_single_for_device(dev, paddr, size, dir);
 
-	if (!dev_is_dma_coherent(dev))
+	if (!dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_device(paddr, size, dir);
+		arch_sync_dma_flush();
+	}
 }
 
 static inline void dma_direct_sync_single_for_cpu(struct device *dev,
@@ -71,6 +73,7 @@ static inline void dma_direct_sync_single_for_cpu(struct device *dev,
 
 	if (!dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_cpu(paddr, size, dir);
+		arch_sync_dma_flush();
 		arch_sync_dma_for_cpu_all();
 	}
 
@@ -109,8 +112,10 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev,
 	}
 
 	if (!dev_is_dma_coherent(dev) &&
-	    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
+	    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
 		arch_sync_dma_for_device(phys, size, dir);
+		arch_sync_dma_flush();
+	}
 	return dma_addr;
 
 err_overflow:
kernel/dma/swiotlb.c +3 -1
index a547c7693135..7cdbfcdfef86 100644
@@ -1595,8 +1595,10 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
 		return DMA_MAPPING_ERROR;
 	}
 
-	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
+	if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
 		arch_sync_dma_for_device(swiotlb_addr, size, dir);
+		arch_sync_dma_flush();
+	}
 	return dma_addr;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v2 4/8
#8 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)

<...>

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

Thanks,

author
#9 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

Quoted 55 lines · Leon Romanovsky
Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)

<...>

Quoted 9 lines

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

I am not quite sure how much this benefits users, as the same information could also be obtained by grepping for #define arch_sync_dma_flush in the source code.

Quoted 3 lines

Thanks,

Thanks very much, Leon, for reviewing this over the weekend. One thing you might have missed is that I place arch_sync_dma_flush() after all arch_sync_dma_for_*() calls, for both single and sg cases. I also used a Python script to scan the code and verify that every arch_sync_dma_for_*() is followed by arch_sync_dma_flush(), to ensure that no call is left out.

In the subsequent patches, for sg cases, the per-entry flush is replaced by a single flush of the entire sg. Each sg case has different characteristics: some are straightforward, while others can be tricky and involve additional contexts.

Thanks
Barry

#10 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

Quoted 66 lines · Barry Song
Quoted 55 lines · Leon Romanovsky
Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)

<...>

Quoted 9 lines

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

Quoted 3 lines

I am not quite sure how much this benefits users, as the same information could also be obtained by grepping for #define arch_sync_dma_flush in the source code.

It differs slightly. Users no longer need to grep around or guess whether this platform used the arch_sync_dma_flush path. A simple grep for ARCH_HAVE_ in /proc/config.gz provides the answer.

Quoted 12 lines
Quoted 4 lines

Thanks,

Thanks very much, Leon, for reviewing this over the weekend. One thing you might have missed is that I place arch_sync_dma_flush() after all arch_sync_dma_for_*() calls, for both single and sg cases. I also used a Python script to scan the code and verify that every arch_sync_dma_for_*() is followed by arch_sync_dma_flush(), to ensure that no call is left out.

Quoted 4 lines

In the subsequent patches, for sg cases, the per-entry flush is replaced by a single flush of the entire sg. Each sg case has different characteristics: some are straightforward, while others can be tricky and involve additional contexts.

I didn't overlook it, and I understand your rationale. However, this is not how kernel patches should be structured. You should not introduce code in patch X and then move it elsewhere in patch X + Y.

Place the code in the correct location from the start. Your patches are small enough to review as is.

Thanks"

Quoted 3 lines

Thanks
Barry

author
#11 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

Quoted 68 lines · Leon Romanovsky
Quoted 66 lines · Barry Song
Quoted 64 lines · Leon Romanovsky
Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)

<...>

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

Quoted 7 lines
Quoted 3 lines

I am not quite sure how much this benefits users, as the same information could also be obtained by grepping for #define arch_sync_dma_flush in the source code.

It differs slightly. Users no longer need to grep around or guess whether this platform used the arch_sync_dma_flush path. A simple grep for ARCH_HAVE_ in /proc/config.gz provides the answer.

In any case, it is only two or three lines of code, so I am fine with either approach. Perhaps Marek, Robin, and others have a point here?

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines

Thanks,

Thanks very much, Leon, for reviewing this over the weekend. One thing you might have missed is that I place arch_sync_dma_flush() after all arch_sync_dma_for_*() calls, for both single and sg cases. I also used a Python script to scan the code and verify that every arch_sync_dma_for_*() is followed by arch_sync_dma_flush(), to ensure that no call is left out.

Quoted 8 lines
Quoted 4 lines

In the subsequent patches, for sg cases, the per-entry flush is replaced by a single flush of the entire sg. Each sg case has different characteristics: some are straightforward, while others can be tricky and involve additional contexts.

I didn't overlook it, and I understand your rationale. However, this is not how kernel patches should be structured. You should not introduce code in patch X and then move it elsewhere in patch X + Y.

I am not quite convinced by this concern. This patch only separates DMA sync issuing from completion waiting, and it reflects that the development is done step by step.

Quoted 3 lines

Place the code in the correct location from the start. Your patches are small enough to review as is.

My point is that this patch places the code in the correct locations from the start. It splits arch_sync_dma_for_*() into arch_sync_dma_for_*() plus arch_sync_dma_flush() everywhere, without introducing any functional changes from the outset.
The subsequent patches clearly show which parts are truly batched.

In the meantime, I do not have a strong preference here. If you think it is better to move some of the straightforward batching code here, I can follow that approach. Perhaps I could move patch 5, patch 8, and the iommu_dma_iova_unlink_range_slow change from patch 7 here, while keeping

[PATCH 6] dma-mapping: Support batch mode for
dma_direct_{map,unmap}_sg

and the IOVA link part from patch 7 as separate patches, since that part is not straightforward. The IOVA link changes affect both __dma_iova_link() and dma_iova_sync(), which are two separate functions and require a deeper understanding of the contexts to determine correctness. That part also lacks testing.

Would that be okay with you?

Thanks
Barry

#12 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

Quoted 122 lines · Barry Song
Quoted 68 lines · Leon Romanovsky
Quoted 66 lines · Barry Song
Quoted 64 lines · Leon Romanovsky
Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 arch/arm64/include/asm/cache.h |  6 ++++++
 arch/arm64/mm/dma-mapping.c    |  4 ++--
 drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
 drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
 include/linux/dma-map-ops.h    |  6 ++++++
 kernel/dma/direct.c            |  8 ++++++--
 kernel/dma/direct.h            |  9 +++++++--
 kernel/dma/swiotlb.c           |  4 +++-
 8 files changed, 73 insertions(+), 25 deletions(-)

<...>

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

Quoted 7 lines
Quoted 3 lines

I am not quite sure how much this benefits users, as the same information could also be obtained by grepping for #define arch_sync_dma_flush in the source code.

It differs slightly. Users no longer need to grep around or guess whether this platform used the arch_sync_dma_flush path. A simple grep for ARCH_HAVE_ in /proc/config.gz provides the answer.

In any case, it is only two or three lines of code, so I am fine with either approach. Perhaps Marek, Robin, and others have a point here?

Quoted 13 lines
Quoted 13 lines
Quoted 5 lines

Thanks,

Thanks very much, Leon, for reviewing this over the weekend. One thing you might have missed is that I place arch_sync_dma_flush() after all arch_sync_dma_for_*() calls, for both single and sg cases. I also used a Python script to scan the code and verify that every arch_sync_dma_for_*() is followed by arch_sync_dma_flush(), to ensure that no call is left out.

Quoted 8 lines
Quoted 4 lines

In the subsequent patches, for sg cases, the per-entry flush is replaced by a single flush of the entire sg. Each sg case has different characteristics: some are straightforward, while others can be tricky and involve additional contexts.

I didn't overlook it, and I understand your rationale. However, this is not how kernel patches should be structured. You should not introduce code in patch X and then move it elsewhere in patch X + Y.

I am not quite convinced by this concern. This patch only separates DMA sync issuing from completion waiting, and it reflects that the development is done step by step.

Quoted 3 lines

Place the code in the correct location from the start. Your patches are small enough to review as is.

My point is that this patch places the code in the correct locations from the start. It splits arch_sync_dma_for_*() into arch_sync_dma_for_*() plus arch_sync_dma_flush() everywhere, without introducing any functional changes from the outset.
The subsequent patches clearly show which parts are truly batched.

In the meantime, I do not have a strong preference here. If you think it is better to move some of the straightforward batching code here, I can follow that approach. Perhaps I could move patch 5, patch 8, and the iommu_dma_iova_unlink_range_slow change from patch 7 here, while keeping

Quoted 8 lines

[PATCH 6] dma-mapping: Support batch mode for
dma_direct_{map,unmap}_sg

and the IOVA link part from patch 7 as separate patches, since that part is not straightforward. The IOVA link changes affect both __dma_iova_link() and dma_iova_sync(), which are two separate functions and require a deeper understanding of the contexts to determine correctness. That part also lacks testing.

Don't worry about testing. NVME, RDMA and GPU are using this path and someone will test it.

Quoted 2 lines

Would that be okay with you?

I don't know, need to see the code.

Thanks

Quoted 3 lines

Thanks
Barry

maintainer
#13 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

In reply to Barry Song
Quoted 64 lines · Barry Song
Quoted 63 lines · Leon Romanovsky
Quoted 62 lines · Barry Song
Quoted 61 lines · Leon Romanovsky
Quoted 50 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
  arch/arm64/include/asm/cache.h |  6 ++++++
  arch/arm64/mm/dma-mapping.c    |  4 ++--
  drivers/iommu/dma-iommu.c      | 37 +++++++++++++++++++++++++---------
  drivers/xen/swiotlb-xen.c      | 24 ++++++++++++++--------
  include/linux/dma-map-ops.h    |  6 ++++++
  kernel/dma/direct.c            |  8 ++++++--
  kernel/dma/direct.h            |  9 +++++++--
  kernel/dma/swiotlb.c           |  4 +++-
  8 files changed, 73 insertions(+), 25 deletions(-)

<...>

include/linux/dma-map-ops.h static inline void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, · line 368

Quoted 5 lines
include/linux/dma-map-ops.h +5 -0
+#ifndef arch_sync_dma_flush
+static inline void arch_sync_dma_flush(void)
+{
+}
+#endif

Over the weekend I realized a useful advantage of the ARCH_HAVE_* config
options: they make it straightforward to inspect the entire DMA path simply by looking at the .config.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I am not quite sure how much this benefits users, as the same information could also be obtained by grepping for #define arch_sync_dma_flush in the source code.

It differs slightly. Users no longer need to grep around or guess whether this platform used the arch_sync_dma_flush path. A simple grep for ARCH_HAVE_ in /proc/config.gz provides the answer.

In any case, it is only two or three lines of code, so I am fine with either approach. Perhaps Marek, Robin, and others have a point here?

If possible I would suggest to follow the already used style in the given code even if it means a bit larger patch.

Quoted 37 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines

Thanks,

Thanks very much, Leon, for reviewing this over the weekend. One thing you might have missed is that I place arch_sync_dma_flush() after all arch_sync_dma_for_*() calls, for both single and sg cases. I also used a Python script to scan the code and verify that every arch_sync_dma_for_*() is followed by arch_sync_dma_flush(), to ensure that no call is left out.

Quoted 7 lines
Quoted 4 lines

In the subsequent patches, for sg cases, the per-entry flush is replaced by a single flush of the entire sg. Each sg case has different characteristics: some are straightforward, while others can be tricky and involve additional contexts.

I didn't overlook it, and I understand your rationale. However, this is not how kernel patches should be structured. You should not introduce code in patch X and then move it elsewhere in patch X + Y.

I am not quite convinced by this concern. This patch only separates DMA sync issuing from completion waiting, and it reflects that the development is done step by step.

Quoted 2 lines

Place the code in the correct location from the start. Your patches are small enough to review as is.

My point is that this patch places the code in the correct locations from the start. It splits arch_sync_dma_for_*() into arch_sync_dma_for_*() plus arch_sync_dma_flush() everywhere, without introducing any functional changes from the outset.
The subsequent patches clearly show which parts are truly batched.

In the meantime, I do not have a strong preference here. If you think it is better to move some of the straightforward batching code here, I can follow that approach. Perhaps I could move patch 5, patch 8, and the iommu_dma_iova_unlink_range_slow change from patch 7 here, while keeping

[PATCH 6] dma-mapping: Support batch mode for
dma_direct_{map,unmap}_sg

Quoted 7 lines

and the IOVA link part from patch 7 as separate patches, since that part is not straightforward. The IOVA link changes affect both __dma_iova_link() and dma_iova_sync(), which are two separate functions and require a deeper understanding of the contexts to determine correctness. That part also lacks testing.

Would that be okay with you?

Yes, this will be okay. The changes are easy to understand, so we don't need to go there with such very small steps.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
maintainer Reviewed-by on v2 4/8
#14 lore raw

Re: [PATCH v2 4/8] dma-mapping: Separate DMA sync issuing and completion waiting

In reply to Barry Song
Quoted 32 lines · Barry Song

From: Barry Song <[email protected]>

Currently, arch_sync_dma_for_cpu and arch_sync_dma_for_device always wait for the completion of each DMA buffer. That is, issuing the DMA sync and waiting for completion is done in a single API call.

For scatter-gather lists with multiple entries, this means issuing and waiting is repeated for each entry, which can hurt performance. Architectures like ARM64 may be able to issue all DMA sync operations for all entries first and then wait for completion together.

To address this, arch_sync_dma_for_* now issues DMA operations in batch, followed by a flush. On ARM64, the flush is implemented using a dsb instruction within arch_sync_dma_flush().

For now, add arch_sync_dma_flush() after each arch_sync_dma_for_*() call. arch_sync_dma_flush() is defined as a no-op on all architectures except arm64, so this patch does not change existing behavior. Subsequent patches will introduce true batching for SG DMA buffers.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Quoted 8 lines
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Oleksandr Tyshchenko <[email protected]>
Cc Tangquan Zheng <[email protected]>

Juergen

author
#15 lore raw

PATCH v2 5/8 dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

In reply to Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/dma/direct.c +7 -7
index a219911c7b90..98bacf562ca1 100644
@@ -402,12 +402,12 @@ 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)) {
+		if (!dev_is_dma_coherent(dev))
 			arch_sync_dma_for_device(paddr, sg->length,
 					dir);
-			arch_sync_dma_flush();
-		}
 	}
+	if (!dev_is_dma_coherent(dev))
+		arch_sync_dma_flush();
 }
 #endif
 
@@ -423,10 +423,8 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
 	for_each_sg(sgl, sg, nents, i) {
 		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-		if (!dev_is_dma_coherent(dev)) {
+		if (!dev_is_dma_coherent(dev))
 			arch_sync_dma_for_cpu(paddr, sg->length, dir);
-			arch_sync_dma_flush();
-		}
 
 		swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 
@@ -434,8 +432,10 @@ 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_flush();
 		arch_sync_dma_for_cpu_all();
+	}
 }
 
 /*
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

<...>

kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 410

Quoted 9 lines
kernel/dma/direct.c +3 -3
-		if (!dev_is_dma_coherent(dev)) {
+		if (!dev_is_dma_coherent(dev))
 			arch_sync_dma_for_device(paddr, sg->length,
 					dir);
-			arch_sync_dma_flush();
-		}
 	}
+	if (!dev_is_dma_coherent(dev))
+		arch_sync_dma_flush();

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Thanks

author
#17 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

<...>

kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 410

Quoted 9 lines
kernel/dma/direct.c +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                      arch_sync_dma_for_device(paddr, sg->length,
                                      dir);
-                     arch_sync_dma_flush();
-             }
      }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Thanks
Barry

#18 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 41 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

<...>

kernel/dma/direct.c void dma_direct_sync_sg_for_device(struct device *dev, · line 410

Quoted 9 lines
kernel/dma/direct.c +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                      arch_sync_dma_for_device(paddr, sg->length,
                                      dir);
-                     arch_sync_dma_flush();
-             }
      }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Quoted 8 lines

Hi Leon,

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

Quoted 3 lines

Thanks
Barry

author
#19 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 45 lines · Leon Romanovsky
Quoted 43 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Review

Quoted 9 lines
(quoted diff) +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                      arch_sync_dma_for_device(paddr, sg->length,
                                      dir);
-                     arch_sync_dma_flush();
-             }
      }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
        struct scatterlist *sg;
        int i;

        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                if (!dev_is_dma_coherent(dev))
                        arch_sync_dma_for_cpu(paddr, sg->length, dir);

                swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                if (dir == DMA_FROM_DEVICE)
                        arch_dma_mark_clean(paddr, sg->length);
        }

        if (!dev_is_dma_coherent(dev)) {
                arch_sync_dma_flush();
                arch_sync_dma_for_cpu_all();
        }
}

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
        struct scatterlist *sg;
        int i;

        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                if (!dev_is_dma_coherent(dev))
                        arch_sync_dma_for_cpu(paddr, sg->length, dir);
        }

        if (!dev_is_dma_coherent(dev)) {
                arch_sync_dma_flush();
                arch_sync_dma_for_cpu_all();
        }

        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                if (dir == DMA_FROM_DEVICE)
                        arch_dma_mark_clean(paddr, sg->length);
        }
}

Could this be the same issue for dma_direct_unmap_sg()?

Another option is to not support batched synchronization for the bounce buffer case, since it is rare. In that case, it could be:

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                        arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 

What’s your view on this, Leon?

Thanks
Barry

maintainer
#20 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 84 lines · Barry Song
Quoted 45 lines · Leon Romanovsky
Quoted 43 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
  kernel/dma/direct.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Review

Quoted 9 lines
(quoted diff) +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                       arch_sync_dma_for_device(paddr, sg->length,
                                       dir);
-                     arch_sync_dma_flush();
-             }
       }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
         struct scatterlist *sg;
         int i;

         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                 if (!dev_is_dma_coherent(dev))
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);

                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                 if (dir == DMA_FROM_DEVICE)
                         arch_dma_mark_clean(paddr, sg->length);
         }

         if (!dev_is_dma_coherent(dev)) {
                 arch_sync_dma_flush();
                 arch_sync_dma_for_cpu_all();
         }
}
Quoted 3 lines

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

Yes, this and the equivalents in the later patches are broken for all the sync_for_cpu and unmap paths which may end up bouncing (beware some of them get a bit fiddly) - any cache maintenance *must* be completed before calling SWIOTLB. As for mark_clean, IIRC that was an IA-64 thing, and appears to be entirely dead now.

Thanks,
Robin.

Quoted 54 lines

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
         struct scatterlist *sg;
         int i;

         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                 if (!dev_is_dma_coherent(dev))
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);
         }

         if (!dev_is_dma_coherent(dev)) {
                 arch_sync_dma_flush();
                 arch_sync_dma_for_cpu_all();
         }

         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                 if (dir == DMA_FROM_DEVICE)
                         arch_dma_mark_clean(paddr, sg->length);
         }
}

Could this be the same issue for dma_direct_unmap_sg()?

Another option is to not support batched synchronization for the bounce buffer case, since it is rare. In that case, it could be:

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 

What’s your view on this, Leon?

Thanks
Barry

author
#21 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 90 lines · Robin Murphy
Quoted 84 lines · Barry Song
Quoted 45 lines · Leon Romanovsky
Quoted 43 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
  kernel/dma/direct.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Review

Quoted 9 lines
(quoted diff) +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                       arch_sync_dma_for_device(paddr, sg->length,
                                       dir);
-                     arch_sync_dma_flush();
-             }
       }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
         struct scatterlist *sg;
         int i;

         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                 if (!dev_is_dma_coherent(dev))
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);

                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                 if (dir == DMA_FROM_DEVICE)
                         arch_dma_mark_clean(paddr, sg->length);
         }

         if (!dev_is_dma_coherent(dev)) {
                 arch_sync_dma_flush();
                 arch_sync_dma_for_cpu_all();
         }
}
Quoted 3 lines

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

Quoted 5 lines

Yes, this and the equivalents in the later patches are broken for all the sync_for_cpu and unmap paths which may end up bouncing (beware some of them get a bit fiddly) - any cache maintenance *must* be completed before calling SWIOTLB. As for mark_clean, IIRC that was an IA-64 thing, and appears to be entirely dead now.

Thanks, Robin. Personally, I would prefer an approach like the one below— that is, not optimizing the bounce buffer cases, as they are already slow due to hardware limitations with memcpy, and optimizing them would make the code quite messy.

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                        arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 

I’d like to check with you, Leon, and Marek on your views about this.

Thanks
Barry

#22 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 119 lines · Barry Song
Quoted 90 lines · Robin Murphy
Quoted 84 lines · Barry Song
Quoted 45 lines · Leon Romanovsky
Quoted 43 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
  kernel/dma/direct.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Review

Quoted 9 lines
(quoted diff) +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                       arch_sync_dma_for_device(paddr, sg->length,
                                       dir);
-                     arch_sync_dma_flush();
-             }
       }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
         struct scatterlist *sg;
         int i;

         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                 if (!dev_is_dma_coherent(dev))
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);

                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                 if (dir == DMA_FROM_DEVICE)
                         arch_dma_mark_clean(paddr, sg->length);
         }

         if (!dev_is_dma_coherent(dev)) {
                 arch_sync_dma_flush();
                 arch_sync_dma_for_cpu_all();
         }
}
Quoted 3 lines

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

Quoted 5 lines

Yes, this and the equivalents in the later patches are broken for all the sync_for_cpu and unmap paths which may end up bouncing (beware some of them get a bit fiddly) - any cache maintenance *must* be completed before calling SWIOTLB. As for mark_clean, IIRC that was an IA-64 thing, and appears to be entirely dead now.

Thanks, Robin. Personally, I would prefer an approach like the one below— that is, not optimizing the bounce buffer cases, as they are already slow due to hardware limitations with memcpy, and optimizing them would make the code quite messy.

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
        for_each_sg(sgl, sg, nents, i) {
                phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                        arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 
Quoted 1 line

I’d like to check with you, Leon, and Marek on your views about this.

I agree with your point that the non‑SWIOTLB path is the performant one and should be preferred. My concern is that you are accessing the dma_io_tlb_mem variable directly from direct.c, which looks like a layer violation.

You likely need to introduce an is_swiotlb_something() helper for this.

BTW, please send a v3 instead of posting incremental follow‑ups. It's hard to track the changes across multiple small additions.

Thanks.

Quoted 3 lines

Thanks
Barry

maintainer
#23 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

In reply to Barry Song
Quoted 119 lines · Barry Song
Quoted 90 lines · Robin Murphy
Quoted 84 lines · Barry Song
Quoted 45 lines · Leon Romanovsky
Quoted 43 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
   kernel/dma/direct.c | 14 +++++++-------
   1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Review

Quoted 9 lines
(quoted diff) +3 -3
-             if (!dev_is_dma_coherent(dev)) {
+             if (!dev_is_dma_coherent(dev))
                        arch_sync_dma_for_device(paddr, sg->length,
                                        dir);
-                     arch_sync_dma_flush();
-             }
        }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();
Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sgl, int nents, enum dma_data_direction dir)

{
          struct scatterlist *sg;
          int i;

          for_each_sg(sgl, sg, nents, i) {
                  phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                  if (!dev_is_dma_coherent(dev))
                          arch_sync_dma_for_cpu(paddr, sg->length, dir);

                  swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                  if (dir == DMA_FROM_DEVICE)
                          arch_dma_mark_clean(paddr, sg->length);
          }

          if (!dev_is_dma_coherent(dev)) {
                  arch_sync_dma_flush();
                  arch_sync_dma_for_cpu_all();
          }
}
Quoted 3 lines

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

Quoted 5 lines

Yes, this and the equivalents in the later patches are broken for all the sync_for_cpu and unmap paths which may end up bouncing (beware some of them get a bit fiddly) - any cache maintenance *must* be completed before calling SWIOTLB. As for mark_clean, IIRC that was an IA-64 thing, and appears to be entirely dead now.

Thanks, Robin. Personally, I would prefer an approach like the one below— that is, not optimizing the bounce buffer cases, as they are already slow due to hardware limitations with memcpy, and optimizing them would make the code quite messy.

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
         for_each_sg(sgl, sg, nents, i) {
                 phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                         arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                 swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);
 
Quoted 1 line

I’d like to check with you, Leon, and Marek on your views about this.

That doesn't work, since dma_io_tlb_mem is always initialised if a SWIOTLB buffer exists at all. Similarly I think the existing dma_need_sync tracking is also too coarse, as that's also always going to be true for a non-coherent device.

Really this flush wants to be after the swiotlb_find_pool() check in the swiotlb_tbl_unmap_single()/__swiotlb_sync_single_for_cpu() paths, as that's the only point we know for sure it's definitely needed for the given address. It would then be rather fiddly to avoid potentially-redundant flushes for the non-sg cases (and the final segment of an sg), but as you already mentioned, if it's limited to cases when we *are* already paying the cost of bouncing anyway, perhaps one extra DSB isn't *too* bad if it means zero impact to the non-bouncing paths.

Thanks,
Robin.

maintainer
#24 lore raw

Re: [PATCH v2 5/8] dma-mapping: Support batch mode for dma_direct_sync_sg_for_*

Quoted 139 lines · Robin Murphy
Quoted 131 lines · Barry Song
Quoted 96 lines · Robin Murphy
Quoted 90 lines · Barry Song
Quoted 46 lines · Leon Romanovsky
Quoted 44 lines · Barry Song
Quoted 37 lines · Leon Romanovsky
Quoted 22 lines · Barry Song

From: Barry Song <[email protected]>

Instead of performing a flush per SG entry, issue all cache operations first and then flush once. This ultimately benefits __dma_sync_sg_for_cpu() and __dma_sync_sg_for_device().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>

---
   kernel/dma/direct.c | 14 +++++++-------

   1 file changed, 7 insertions(+), 7 deletions(-)

<...>

Quoted 9 lines

-             if (!dev_is_dma_coherent(dev)) { +             if (!dev_is_dma_coherent(dev)) arch_sync_dma_for_device(paddr, sg->length, dir); -                     arch_sync_dma_flush(); -             }
        }
+     if (!dev_is_dma_coherent(dev))
+             arch_sync_dma_flush();

Quoted 2 lines

This patch should be squashed into the previous one. You introduced arch_sync_dma_flush() there, and now you are placing it elsewhere.

Hi Leon,

Quoted 8 lines
Quoted 6 lines

The previous patch replaces all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(), without any functional change. The subsequent patches then implement the actual batching. I feel this is a better approach for reviewing each change independently. Otherwise, the previous patch would be too large.

Don't worry about it. Your patches are small enough.

My hardware does not require a bounce buffer, but I am concerned that this patch may be incorrect for systems that do require one.

Now it is:

void dma_direct_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, int nents, enum dma_data_direction dir)
{
          struct scatterlist *sg;
          int i;

          for_each_sg(sgl, sg, nents, i) { phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));

                  if (!dev_is_dma_coherent(dev)) arch_sync_dma_for_cpu(paddr, sg->length, dir);

                  swiotlb_sync_single_for_cpu(dev, paddr, sg->length, dir);

                  if (dir == DMA_FROM_DEVICE) arch_dma_mark_clean(paddr, sg->length); }

          if (!dev_is_dma_coherent(dev)) {
                  arch_sync_dma_flush();
                  arch_sync_dma_for_cpu_all();
          }
}

Quoted 3 lines

Should we call swiotlb_sync_single_for_cpu() and arch_dma_mark_clean() after the flush to ensure the CPU sees the latest data and that the memcpy is correct? I mean:

Quoted 6 lines

Yes, this and the equivalents in the later patches are broken for all the sync_for_cpu and unmap paths which may end up bouncing (beware some of them get a bit fiddly) - any cache maintenance *must* be completed before calling SWIOTLB. As for mark_clean, IIRC that was an IA-64 thing,
and appears to be entirely dead now.

Thanks, Robin. Personally, I would prefer an approach like the one below—
that is, not optimizing the bounce buffer cases, as they are already slow
due to hardware limitations with memcpy, and optimizing them would make the code quite messy.

kernel/dma/direct.c +4 -1
index 550a1a13148d..a4840f7e8722 100644
@@ -423,8 +423,11 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
          for_each_sg(sgl, sg, nents, i) {
                  phys_addr_t paddr = dma_to_phys(dev, 
 sg_dma_address(sg));
 
-               if (!dev_is_dma_coherent(dev))
+               if (!dev_is_dma_coherent(dev)) {
                          arch_sync_dma_for_cpu(paddr, sg->length, dir);
+                       if (unlikely(dev->dma_io_tlb_mem))
+                               arch_sync_dma_flush();
+               }
 
                  swiotlb_sync_single_for_cpu(dev, paddr, sg->length, 

dir);

Quoted 1 line

I’d like to check with you, Leon, and Marek on your views about this.

That doesn't work, since dma_io_tlb_mem is always initialised if a SWIOTLB buffer exists at all. Similarly I think the existing dma_need_sync tracking is also too coarse, as that's also always going to be true for a non-coherent device.

Quoted 9 lines

Really this flush wants to be after the swiotlb_find_pool() check in the swiotlb_tbl_unmap_single()/__swiotlb_sync_single_for_cpu() paths, as that's the only point we know for sure it's definitely needed for the given address. It would then be rather fiddly to avoid potentially-redundant flushes for the non-sg cases (and the final segment of an sg), but as you already mentioned, if it's limited to cases when we *are* already paying the cost of bouncing anyway, perhaps one extra DSB isn't *too* bad if it means zero impact to the non-bouncing paths.

I agree with Robin, optimizing the swiotlb path doesn't make much sense.

Best regards

Signature · 2 lines
Marek Szyprowski, PhD
Samsung R&D Institute Poland
author
#25 lore raw

PATCH v2 6/8 dma-mapping: Support batch mode for dma_direct_{map,unmap}_sg

In reply to Barry Song

From: Barry Song <[email protected]>

Leon suggested extending a flush argument to dma_direct_unmap_phys(), dma_direct_map_phys(), and dma_direct_sync_single_for_cpu(). For single-buffer cases, this would use flush=true, while for SG cases flush=false would be used, followed by a single flush after all cache operations are issued in dma_direct_{map,unmap}_sg().

This ultimately benefits dma_map_sg() and dma_unmap_sg().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 kernel/dma/direct.c  | 17 +++++++++++++----
 kernel/dma/direct.h  | 16 ++++++++++------
 kernel/dma/mapping.c |  6 +++---
 3 files changed, 26 insertions(+), 13 deletions(-)
kernel/dma/direct.c +13 -4
index 98bacf562ca1..550a1a13148d 100644
@@ -447,14 +447,19 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
 {
 	struct scatterlist *sg;
 	int i;
+	bool need_sync = false;
 
 	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 {
+			need_sync = true;
 			dma_direct_unmap_phys(dev, sg->dma_address,
-					      sg_dma_len(sg), dir, attrs);
+					      sg_dma_len(sg), dir, attrs, false);
+		}
 	}
+	if (need_sync && !dev_is_dma_coherent(dev))
+		arch_sync_dma_flush();
 }
 #endif
 
@@ -464,6 +469,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;
+	bool need_sync = false;
 
 	for_each_sg(sgl, sg, nents, i) {
 		switch (pci_p2pdma_state(&p2pdma_state, dev, sg_page(sg))) {
@@ -475,8 +481,9 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
 			 */
 			break;
 		case PCI_P2PDMA_MAP_NONE:
+			need_sync = true;
 			sg->dma_address = dma_direct_map_phys(dev, sg_phys(sg),
-					sg->length, dir, attrs);
+					sg->length, dir, attrs, false);
 			if (sg->dma_address == DMA_MAPPING_ERROR) {
 				ret = -EIO;
 				goto out_unmap;
@@ -495,6 +502,8 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
 		sg_dma_len(sg) = sg->length;
 	}
 
+	if (need_sync && !dev_is_dma_coherent(dev))
+		arch_sync_dma_flush();
 	return nents;
 
 out_unmap:
kernel/dma/direct.h +10 -6
index a69326eed266..d4ad79828090 100644
@@ -67,13 +67,15 @@ static inline void dma_direct_sync_single_for_device(struct device *dev,
 }
 
 static inline void dma_direct_sync_single_for_cpu(struct device *dev,
-		dma_addr_t addr, size_t size, enum dma_data_direction dir)
+		dma_addr_t addr, size_t size, enum dma_data_direction dir,
+		bool flush)
 {
 	phys_addr_t paddr = dma_to_phys(dev, addr);
 
 	if (!dev_is_dma_coherent(dev)) {
 		arch_sync_dma_for_cpu(paddr, size, dir);
-		arch_sync_dma_flush();
+		if (flush)
+			arch_sync_dma_flush();
 		arch_sync_dma_for_cpu_all();
 	}
 
@@ -85,7 +87,7 @@ static inline void dma_direct_sync_single_for_cpu(struct device *dev,
 
 static inline dma_addr_t dma_direct_map_phys(struct device *dev,
 		phys_addr_t phys, size_t size, enum dma_data_direction dir,
-		unsigned long attrs)
+		unsigned long attrs, bool flush)
 {
 	dma_addr_t dma_addr;
 
@@ -114,7 +116,8 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev,
 	if (!dev_is_dma_coherent(dev) &&
 	    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
 		arch_sync_dma_for_device(phys, size, dir);
-		arch_sync_dma_flush();
+		if (flush)
+			arch_sync_dma_flush();
 	}
 	return dma_addr;
 
@@ -127,7 +130,8 @@ static inline dma_addr_t dma_direct_map_phys(struct device *dev,
 }
 
 static inline void dma_direct_unmap_phys(struct device *dev, dma_addr_t addr,
-		size_t size, enum dma_data_direction dir, unsigned long attrs)
+		size_t size, enum dma_data_direction dir, unsigned long attrs,
+		bool flush)
 {
 	phys_addr_t phys;
 
@@ -137,7 +141,7 @@ static inline void dma_direct_unmap_phys(struct device *dev, dma_addr_t addr,
 
 	phys = dma_to_phys(dev, addr);
 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
-		dma_direct_sync_single_for_cpu(dev, addr, size, dir);
+		dma_direct_sync_single_for_cpu(dev, addr, size, dir, flush);
 
 	swiotlb_tbl_unmap_single(dev, phys, size, dir,
 					 attrs | DMA_ATTR_SKIP_CPU_SYNC);
kernel/dma/mapping.c +3 -3
index 37163eb49f9f..d8cfa56a3cbb 100644
@@ -166,7 +166,7 @@ dma_addr_t dma_map_phys(struct device *dev, phys_addr_t phys, size_t size,
 
 	if (dma_map_direct(dev, ops) ||
 	    (!is_mmio && arch_dma_map_phys_direct(dev, phys + size)))
-		addr = dma_direct_map_phys(dev, phys, size, dir, attrs);
+		addr = dma_direct_map_phys(dev, phys, size, dir, attrs, true);
 	else if (use_dma_iommu(dev))
 		addr = iommu_dma_map_phys(dev, phys, size, dir, attrs);
 	else if (ops->map_phys)
@@ -207,7 +207,7 @@ void dma_unmap_phys(struct device *dev, dma_addr_t addr, size_t size,
 	BUG_ON(!valid_dma_direction(dir));
 	if (dma_map_direct(dev, ops) ||
 	    (!is_mmio && arch_dma_unmap_phys_direct(dev, addr + size)))
-		dma_direct_unmap_phys(dev, addr, size, dir, attrs);
+		dma_direct_unmap_phys(dev, addr, size, dir, attrs, true);
 	else if (use_dma_iommu(dev))
 		iommu_dma_unmap_phys(dev, addr, size, dir, attrs);
 	else if (ops->unmap_phys)
@@ -373,7 +373,7 @@ void __dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
 
 	BUG_ON(!valid_dma_direction(dir));
 	if (dma_map_direct(dev, ops))
-		dma_direct_sync_single_for_cpu(dev, addr, size, dir);
+		dma_direct_sync_single_for_cpu(dev, addr, size, dir, true);
 	else if (use_dma_iommu(dev))
 		iommu_dma_sync_single_for_cpu(dev, addr, size, dir);
 	else if (ops->sync_single_for_cpu)
Signature · 1 line
2.43.0
Reviewed-by on v2 6/8
#26 lore raw

Re: [PATCH v2 6/8] dma-mapping: Support batch mode for dma_direct_{map,unmap}_sg

Quoted 3 lines · Barry Song

From: Barry Song <[email protected]>

Leon suggested extending a flush argument to

Let's move this sentence out of the commit message and place it in the changelog instead.

Quoted 21 lines

dma_direct_unmap_phys(), dma_direct_map_phys(), and dma_direct_sync_single_for_cpu(). For single-buffer cases, this would use flush=true, while for SG cases flush=false would be used, followed by a single flush after all cache operations are issued in dma_direct_{map,unmap}_sg().

This ultimately benefits dma_map_sg() and dma_unmap_sg().

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Tangquan Zheng <[email protected]>
Quoted 5 lines
---
 kernel/dma/direct.c  | 17 +++++++++++++----
 kernel/dma/direct.h  | 16 ++++++++++------
 kernel/dma/mapping.c |  6 +++---
 3 files changed, 26 insertions(+), 13 deletions(-)

Thanks,

author
#27 lore raw

RFC PATCH v2 7/8 dma-iommu: Support DMA sync batch mode for IOVA link and unlink

In reply to Barry Song

From: Barry Song <[email protected]>

Apply batched DMA synchronization to __dma_iova_link() and iommu_dma_iova_unlink_range_slow(). For multiple sync_dma_for_device() and sync_dma_for_cpu() calls, we only need to wait once for the completion of all sync operations, rather than waiting for each one individually.

I do not have the hardware to test this, so it is marked as RFC. I would greatly appreciate it if someone could test it.

Cc Leon Romanovsky <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 drivers/iommu/dma-iommu.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iommu/dma-iommu.c +9 -6
index 6827763a3877..ffa940bdbbaf 100644
@@ -1849,10 +1849,8 @@ static int __dma_iova_link(struct device *dev, dma_addr_t addr,
 	bool coherent = dev_is_dma_coherent(dev);
 	int prot = dma_info_to_prot(dir, coherent, attrs);
 
-	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
+	if (!coherent && !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
 		arch_sync_dma_for_device(phys, size, dir);
-		arch_sync_dma_flush();
-	}
 
 	return iommu_map_nosync(iommu_get_dma_domain(dev), addr, phys, size,
 			prot, GFP_ATOMIC);
@@ -1995,6 +1993,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_flush();
 	return iommu_sync_map(domain, addr - iova_start_pad,
 		      iova_align(iovad, size + iova_start_pad));
 }
@@ -2008,6 +2008,8 @@ static void iommu_dma_iova_unlink_range_slow(struct device *dev,
 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
 	struct iova_domain *iovad = &cookie->iovad;
 	size_t iova_start_pad = iova_offset(iovad, addr);
+	bool need_sync_dma = !dev_is_dma_coherent(dev) &&
+			!(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO));
 	dma_addr_t end = addr + size;
 
 	do {
@@ -2023,16 +2025,17 @@ static void iommu_dma_iova_unlink_range_slow(struct device *dev,
 			end - addr, iovad->granule - iova_start_pad);
 
 		if (!dev_is_dma_coherent(dev) &&
-		    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO))) {
+		    !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
 			arch_sync_dma_for_cpu(phys, len, dir);
-			arch_sync_dma_flush();
-		}
 
 		swiotlb_tbl_unmap_single(dev, phys, len, dir, attrs);
 
 		addr += len;
 		iova_start_pad = 0;
 	} while (addr < end);
+
+	if (need_sync_dma)
+		arch_sync_dma_flush();
 }
 
 static void __iommu_dma_iova_unlink(struct device *dev,
Signature · 1 line
2.43.0
author
#28 lore raw

RFC PATCH v2 8/8 dma-iommu: Support DMA sync batch mode for iommu_dma_sync_sg_for_{cpu, device}

In reply to Barry Song

From: Barry Song <[email protected]>

Apply batched DMA synchronization to iommu_dma_sync_sg_for_cpu() and iommu_dma_sync_sg_for_device(). For all buffers in an SG list, only a single flush operation is needed.

I do not have the hardware to test this, so the patch is marked as RFC. I would greatly appreciate any testing feedback.

Cc Leon Romanovsky <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 drivers/iommu/dma-iommu.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/dma-iommu.c +7 -8
index ffa940bdbbaf..b68dbfcb7846 100644
@@ -1131,10 +1131,9 @@ void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
 			iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
 						      sg->length, dir);
 	} else if (!dev_is_dma_coherent(dev)) {
-		for_each_sg(sgl, sg, nelems, i) {
+		for_each_sg(sgl, sg, nelems, i)
 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
-			arch_sync_dma_flush();
-		}
+		arch_sync_dma_flush();
 	}
 }
 
@@ -1144,16 +1143,16 @@ void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
 	struct scatterlist *sg;
 	int i;
 
-	if (sg_dma_is_swiotlb(sgl))
+	if (sg_dma_is_swiotlb(sgl)) {
 		for_each_sg(sgl, sg, nelems, i)
 			iommu_dma_sync_single_for_device(dev,
 							 sg_dma_address(sg),
 							 sg->length, dir);
-	else if (!dev_is_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i) {
+	} else if (!dev_is_dma_coherent(dev)) {
+		for_each_sg(sgl, sg, nelems, i)
 			arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
-			arch_sync_dma_flush();
-		}
+		arch_sync_dma_flush();
+	}
 }
 
 static phys_addr_t iommu_dma_map_swiotlb(struct device *dev, phys_addr_t phys,
Signature · 1 line
2.43.0
#29 lore raw

Re: [PATCH RFC v2 8/8] dma-iommu: Support DMA sync batch mode for iommu_dma_sync_sg_for_{cpu, device}

Quoted 32 lines · Barry Song

From: Barry Song <[email protected]>

Apply batched DMA synchronization to iommu_dma_sync_sg_for_cpu() and iommu_dma_sync_sg_for_device(). For all buffers in an SG list, only a single flush operation is needed.

I do not have the hardware to test this, so the patch is marked as RFC. I would greatly appreciate any testing feedback.

Cc Leon Romanovsky <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 drivers/iommu/dma-iommu.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/dma-iommu.c +0 -0
index ffa940bdbbaf..b68dbfcb7846 100644

drivers/iommu/dma-iommu.c void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, · line 1136

Quoted 9 lines
drivers/iommu/dma-iommu.c +2 -3
@@ -1131,10 +1131,9 @@ void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
 			iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
 						      sg->length, dir);
 	} else if (!dev_is_dma_coherent(dev)) {
-		for_each_sg(sgl, sg, nelems, i) {
+		for_each_sg(sgl, sg, nelems, i)
 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
-			arch_sync_dma_flush();
-		}
+		arch_sync_dma_flush();

This and previous patches should be squashed into the one which introduced arch_sync_dma_flush().

Thanks

author
#30 lore raw

Re: [PATCH RFC v2 8/8] dma-iommu: Support DMA sync batch mode for iommu_dma_sync_sg_for_{cpu, device}

Quoted 38 lines · Leon Romanovsky
Quoted 36 lines · Barry Song

From: Barry Song <[email protected]>

Apply batched DMA synchronization to iommu_dma_sync_sg_for_cpu() and iommu_dma_sync_sg_for_device(). For all buffers in an SG list, only a single flush operation is needed.

I do not have the hardware to test this, so the patch is marked as RFC. I would greatly appreciate any testing feedback.

Cc Leon Romanovsky <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
 drivers/iommu/dma-iommu.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/dma-iommu.c +0 -1
index ffa940bdbbaf..b68dbfcb7846 100644
@@ -1131,10 +1131,9 @@ void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
                      iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
                                                    sg->length, dir);
      } else if (!dev_is_dma_coherent(dev)) {
-             for_each_sg(sgl, sg, nelems, i) {
Quoted 8 lines

drivers/iommu/dma-iommu.c void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, · line 1136

Quoted 5 lines
drivers/iommu/dma-iommu.c +2 -2
+             for_each_sg(sgl, sg, nelems, i)
                      arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
-                     arch_sync_dma_flush();
-             }
+             arch_sync_dma_flush();

This and previous patches should be squashed into the one which introduced arch_sync_dma_flush().

Hi Leon,

The series is structured to first introduce no functional change by replacing all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(). Subsequent patches then add batching for different scenarios as separate changes.

Another issue is that I was unable to find a board that both runs mainline and exercises the IOMMU paths affected by these changes. As a result, patches 7 and 8 are marked as RFC, while the other patches have been tested on a real board running mainline + changes.

Thanks
Barry

maintainer
#31 lore raw

Re: [PATCH RFC v2 8/8] dma-iommu: Support DMA sync batch mode for iommu_dma_sync_sg_for_{cpu, device}

Quoted 55 lines · Barry Song
Quoted 38 lines · Leon Romanovsky
Quoted 36 lines · Barry Song

From: Barry Song <[email protected]>

Apply batched DMA synchronization to iommu_dma_sync_sg_for_cpu() and iommu_dma_sync_sg_for_device(). For all buffers in an SG list, only a single flush operation is needed.

I do not have the hardware to test this, so the patch is marked as RFC. I would greatly appreciate any testing feedback.

Cc Leon Romanovsky <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ada Couprie Diaz <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Anshuman Khandual <[email protected]>
Cc Ryan Roberts <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Robin Murphy <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Tangquan Zheng <[email protected]>
---
  drivers/iommu/dma-iommu.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
drivers/iommu/dma-iommu.c +0 -1
index ffa940bdbbaf..b68dbfcb7846 100644
@@ -1131,10 +1131,9 @@ void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
                       iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
                                                     sg->length, dir);
       } else if (!dev_is_dma_coherent(dev)) {
-             for_each_sg(sgl, sg, nelems, i) {
Quoted 8 lines

drivers/iommu/dma-iommu.c void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, · line 1136

Quoted 5 lines
drivers/iommu/dma-iommu.c +2 -2
+             for_each_sg(sgl, sg, nelems, i)
                       arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
-                     arch_sync_dma_flush();
-             }
+             arch_sync_dma_flush();

This and previous patches should be squashed into the one which introduced arch_sync_dma_flush().

Hi Leon,

The series is structured to first introduce no functional change by replacing all arch_sync_dma_for_* calls with arch_sync_dma_for_* plus arch_sync_dma_flush(). Subsequent patches then add batching for different scenarios as separate changes.

Quoted 4 lines

Another issue is that I was unable to find a board that both runs mainline and exercises the IOMMU paths affected by these changes. As a result, patches 7 and 8 are marked as RFC, while the other patches have been tested on a real board running mainline + changes.

FWIW if you can get your hands on an M.2 NVMe for the Rock5 then that has an SMMU in front of PCIe (and could also work to test non-coherent SWIOTLB, with the SMMU in bypass and either some fake restrictive dma-ranges in the DT or a hack to reduce the DMA mask in the NVMe driver.)

Cheers,
Robin.