Skip to contentSkip to conversation

dma: swiotlb: Centralize default pool policy and sizing

PATCH Superseded Aneesh Kumar K.V (Arm) 4 participants 28 messages Sep 21 – Sep 23

Subsystems (13)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover dma: swiotlb: Centralize default pool policy and sizing

Hi,

This series centralizes swiotlb default-pool selection and sizing, and simplifies the initialization flags used to describe pool placement.

The first patch replaces the addressing-limit argument with initialization flags and introduces explicit policies for the different reasons a default pool may be needed. Architectures now report DMA addressing constraints with SWIOTLB_INIT_ADDRESSING_LIMIT, while the core selects the corresponding pool policy.

The second patch moves the reduced sizing used when the pool is needed only for unaligned kmalloc bouncing from arm64 and RISC-V into the swiotlb core.

The third patch centralizes swiotlb sizing for memory-encrypted systems. It moves the existing x86 guest-sizing policy into the core. Host memory encryption retains the normal pool size, and a shared default pool is not selected solely for guest memory encryption when a restricted DMA pool is present.

The fourth patch removes SWIOTLB_ANY. Pool placement is derived directly from SWIOTLB_INIT_ADDRESSING_LIMIT: pools required for limited DMA addressing are allocated below the architecture's low address limit, while other pools may be allocated anywhere in directly mapped memory. Xen continues to request a pool explicitly with SWIOTLB_INIT_REMAP without imposing a low-address constraint.

Changes from v4:
https://lore.kernel.org/all/[email protected]
* Collect Reviewed-by tags.
* Add a new patch, "dma: swiotlb: Track whether the pool size was explicitly set".
* Retain SWIOTLB_ANY usage for RISC-V until patch 6.
* Rename SWIOTLB_INIT_FORCE_DISABLE to SWIOTLB_INIT_DEFAULT_OFF.

Changes from v3:
https://lore.kernel.org/all/[email protected]
* Add a new patch, "dma: swiotlb: Add an overridable architecture pool opt-out".
* Clarify the change in default behavior with the removal of SWIOTLB_ANY.

Changes from v2:
https://lore.kernel.org/all/[email protected]
* Rebase to latest tree.
* Drop SWIOTLB_ANY and derive that based on SWIOTLB_INIT_ADDRESSING_LIMIT
* Add SWIOTLB_INIT_REMAP.

v1:
https://lore.kernel.org/all/[email protected]

Cc Robin Murphy <[email protected]>
Cc Marek Szyprowski <[email protected]>
Cc Will Deacon <[email protected]>
Cc Marc Zyngier <[email protected]>
Cc Steven Price <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Jiri Pirko <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Mostafa Saleh <[email protected]>
Cc Petr Tesarik <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Dan Williams <[email protected]>
Cc Xu Yilun <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Nicholas Piggin <[email protected]>
Cc "Christophe Leroy (CS GROUP)" <[email protected]>
Cc Alexander Gordeev <[email protected]>
Cc Gerald Schaefer <[email protected]>
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Christian Borntraeger <[email protected]>
Cc Sven Schnelle <[email protected]>
Cc Russell King <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Thomas Bogendoerfer <[email protected]>
Cc Jiaxun Yang <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>

Aneesh Kumar K.V (Arm) (6):
dma: swiotlb: Centralize default pool policy selection
dma: swiotlb: Track whether the pool size was explicitly set
dma: swiotlb: Centralize minimal pool sizing
dma: swiotlb: Centralize memory-encryption pool sizing
dma: swiotlb: Add an overridable architecture pool opt-out
dma: swiotlb: Remove SWIOTLB_ANY

 Documentation/core-api/swiotlb.rst   |   8 +-
 arch/arm/mm/init.c                   |   6 +-
 arch/arm64/mm/init.c                 |  14 +-
 arch/loongarch/kernel/setup.c        |   2 +-
 arch/mips/cavium-octeon/dma-octeon.c |   2 +-
 arch/mips/loongson64/dma.c           |   2 +-
 arch/mips/sibyte/common/dma.c        |   2 +-
 arch/powerpc/kernel/dma-swiotlb.c    |   4 +-
 arch/powerpc/mm/mem.c                |  17 ++-
 arch/powerpc/platforms/pseries/svm.c |  10 --
 arch/powerpc/sysdev/fsl_pci.c        |   1 +
 arch/riscv/mm/init.c                 |  24 +--
 arch/s390/mm/init.c                  |   2 +-
 arch/x86/include/asm/crash_reserve.h |   6 +-
 arch/x86/kernel/pci-dma.c            |  23 ++-
 arch/x86/mm/mem_encrypt.c            |  24 ---
 include/linux/swiotlb.h              |  21 ++-
 kernel/dma/swiotlb.c                 | 220 +++++++++++++++++++++------
 18 files changed, 248 insertions(+), 140 deletions(-)

base-commit: 704340f1cd0dcef829eb62f5b48ae95a2ce17bdf

Signature · 1 line
2.43.0
#1 lore raw

PATCH v5 1/6 dma: swiotlb: Centralize default pool policy selection

The addressing_limited argument to swiotlb_init() describes only one reason for allocating a default SWIOTLB pool. A pool may also be needed for memory encryption, unaligned kmalloc bouncing, or swiotlb=force.

Replace the boolean argument with the SWIOTLB_INIT_ADDRESSING_LIMIT flag and have architectures report their DMA addressing constraints through the initialization flags.

Introduce SWIOTLB pool policies and select the policy in the core before allocating the default pool. This separates the decision to allocate a pool from the policy used to size it, allowing subsequent changes to centralize minimal and confidential-guest sizing.

Set the shared-pool state before memory attributes are updated. Also move the pseries secure-guest setup before swiotlb_init() so its initialization flags are available when the policy is selected.

---
 arch/arm/mm/init.c                   |   6 +-
 arch/arm64/mm/init.c                 |   5 +-
 arch/loongarch/kernel/setup.c        |   2 +-
 arch/mips/cavium-octeon/dma-octeon.c |   2 +-
 arch/mips/loongson64/dma.c           |   2 +-
 arch/mips/sibyte/common/dma.c        |   2 +-
 arch/powerpc/kernel/dma-swiotlb.c    |   4 +-
 arch/powerpc/mm/mem.c                |  15 ++-
 arch/powerpc/platforms/pseries/svm.c |  10 --
 arch/powerpc/sysdev/fsl_pci.c        |   1 +
 arch/riscv/mm/init.c                 |   6 +-
 arch/s390/mm/init.c                  |   2 +-
 arch/x86/kernel/pci-dma.c            |  16 +++-
 include/linux/swiotlb.h              |  12 ++-
 kernel/dma/swiotlb.c                 | 134 +++++++++++++++++++--------
 15 files changed, 152 insertions(+), 67 deletions(-)
arch/arm/mm/init.c +5 -1
index 0cc1bf04686d..aca97a4e5dcd 100644
@@ -223,7 +223,11 @@ static inline void poison_init_mem(void *s, size_t count)
 void __init arch_mm_preinit(void)
 {
 #ifdef CONFIG_ARM_LPAE
-	swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE);
+	unsigned int flags = SWIOTLB_VERBOSE;
+
+	if (max_pfn > arm_dma_pfn_limit)
+		flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	swiotlb_init(flags);
 #endif
 
 #ifdef CONFIG_SA1111
arch/arm64/mm/init.c +4 -1
index fbf215ecc7d0..7d51fb5f5a71 100644
@@ -351,7 +351,10 @@ void __init arch_mm_preinit(void)
 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
 	}
 
-	swiotlb_init(true, flags);
+	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))
+		flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+
+	swiotlb_init(flags);
 
 	/*
 	 * Check boundaries twice: Some fundamental inconsistencies can be
arch/loongarch/kernel/setup.c +1 -1
index 6fa4a22a58fd..02f52f6b2747 100644
@@ -404,7 +404,7 @@ static void __init arch_mem_init(char **cmdline_p)
 
 	memblock_set_bottom_up(true);
 
-	swiotlb_init(true, SWIOTLB_VERBOSE);
+	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT);
 
 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
 
arch/mips/cavium-octeon/dma-octeon.c +1 -1
index 9fbba6a8fa4c..ca7c37bc070f 100644
@@ -235,5 +235,5 @@ void __init plat_swiotlb_setup(void)
 #endif
 
 	swiotlb_adjust_size(swiotlbsize);
-	swiotlb_init(true, SWIOTLB_VERBOSE);
+	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT);
 }
arch/mips/loongson64/dma.c +1 -1
index 52801442ea86..5b8056e6c5a8 100644
@@ -25,5 +25,5 @@ phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 
 void __init plat_swiotlb_setup(void)
 {
-	swiotlb_init(true, SWIOTLB_VERBOSE);
+	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT);
 }
arch/mips/sibyte/common/dma.c +1 -1
index c5c2c782aff6..3835fee21489 100644
@@ -10,5 +10,5 @@
 
 void __init plat_swiotlb_setup(void)
 {
-	swiotlb_init(true, SWIOTLB_VERBOSE);
+	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT);
 }
arch/powerpc/kernel/dma-swiotlb.c +3 -1
index ba256c37bcc0..97fffa46f05a 100644
@@ -14,8 +14,10 @@ unsigned int ppc_swiotlb_flags;
 
 void __init swiotlb_detect_4g(void)
 {
-	if ((memblock_end_of_DRAM() - 1) > 0xffffffff)
+	if ((memblock_end_of_DRAM() - 1) > 0xffffffff) {
 		ppc_swiotlb_enable = 1;
+		ppc_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
 }
 
 static int __init check_swiotlb_enabled(void)
arch/powerpc/mm/mem.c +14 -1
index b617b69452cd..006ed41097e9 100644
@@ -287,6 +287,19 @@ void __init arch_mm_preinit(void)
 	BUILD_BUG_ON(MMU_PAGE_COUNT > 16);
 
 #ifdef CONFIG_SWIOTLB
+	if (is_secure_guest()) {
+
+		/* Don't release the SWIOTLB buffer. */
+		ppc_swiotlb_enable = 1;
+
+		/*
+		 * Since the guest memory is inaccessible to the host,
+		 * devices always need to use the SWIOTLB buffer for DMA
+		 * even if dma_capable() says otherwise.
+		 */
+		ppc_swiotlb_flags |= SWIOTLB_ANY;
+	}
+
 	/*
 	 * Some platforms (e.g. 85xx) limit DMA-able memory way below
 	 * 4G. We force memblock to bottom-up mode to ensure that the
@@ -295,7 +308,7 @@ void __init arch_mm_preinit(void)
 	 * back to to-down.
 	 */
 	memblock_set_bottom_up(true);
-	swiotlb_init(ppc_swiotlb_enable, ppc_swiotlb_flags);
+	swiotlb_init(ppc_swiotlb_flags);
 #endif
 
 	kasan_late_init();
arch/powerpc/platforms/pseries/svm.c +0 -10
index 7a403dbd35ee..4a0be631dc6d 100644
@@ -21,16 +21,6 @@ static int __init init_svm(void)
 	if (!is_secure_guest())
 		return 0;
 
-	/* Don't release the SWIOTLB buffer. */
-	ppc_swiotlb_enable = 1;
-
-	/*
-	 * Since the guest memory is inaccessible to the host, devices always
-	 * need to use the SWIOTLB buffer for DMA even if dma_capable() says
-	 * otherwise.
-	 */
-	ppc_swiotlb_flags |= SWIOTLB_ANY;
-
 	/* Share the SWIOTLB buffer with the host. */
 	swiotlb_update_mem_attributes();
 
arch/powerpc/sysdev/fsl_pci.c +1 -0
index 600f83cea1cd..49264e25108b 100644
@@ -444,6 +444,7 @@ static void setup_pci_atmu(struct pci_controller *hose)
 	if (hose->dma_window_size < mem) {
 #ifdef CONFIG_SWIOTLB
 		ppc_swiotlb_enable = 1;
+		ppc_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
 #else
 		pr_err("%pOF: ERROR: Memory size exceeds PCI ATMU ability to "
 			"map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
arch/riscv/mm/init.c +5 -1
index fb37b0b67efe..e0e4c103d603 100644
@@ -190,7 +190,11 @@ void __init arch_mm_preinit(void)
 		swiotlb_flags |= SWIOTLB_ANY;
 	}
 
-	swiotlb_init(swiotlb, swiotlb_flags);
+	if ((max_pfn > PFN_DOWN(dma32_phys_limit)) &&
+	     (memblock_start_of_DRAM() < dma32_phys_limit))
+		swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+
+	swiotlb_init(swiotlb_flags);
 
 	print_vm_layout();
 }
arch/s390/mm/init.c +1 -1
index be7e009e7b59..78f507e24ceb 100644
@@ -166,7 +166,7 @@ static void __init pv_init(void)
 	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
 
 	/* make sure bounce buffers are shared */
-	swiotlb_init(true, SWIOTLB_VERBOSE | SWIOTLB_ANY);
+	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_ANY);
 	swiotlb_update_mem_attributes();
 }
 
arch/x86/kernel/pci-dma.c +11 -5
index 75cf8f6ae8cd..0cff255827ba 100644
@@ -44,8 +44,10 @@ static unsigned int x86_swiotlb_flags;
 static void __init pci_swiotlb_detect(void)
 {
 	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
+	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
 		x86_swiotlb_enable = true;
+		x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
 
 	/*
 	 * Set swiotlb to 1 so that bounce buffers are allocated and used for
@@ -81,8 +83,10 @@ static void __init pci_xen_swiotlb_init(void)
 	if (!xen_swiotlb_enabled())
 		return;
 	x86_swiotlb_enable = true;
-	x86_swiotlb_flags |= SWIOTLB_ANY;
-	swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup);
+	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
+	x86_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
+	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;
+	swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup);
 	dma_ops = &xen_swiotlb_dma_ops;
 	if (IS_ENABLED(CONFIG_PCI))
 		pci_request_acs();
@@ -103,7 +107,7 @@ void __init pci_iommu_alloc(void)
 	gart_iommu_hole_init();
 	amd_iommu_detect();
 	detect_intel_iommu();
-	swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
+	swiotlb_init(x86_swiotlb_flags);
 }
 
 static __init int iommu_setup(char *p)
@@ -149,8 +153,10 @@ static __init int iommu_setup(char *p)
 			return 1;
 		}
 #ifdef CONFIG_SWIOTLB
-		if (!strncmp(p, "soft", 4))
+		if (!strncmp(p, "soft", 4)) {
 			x86_swiotlb_enable = true;
+			x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+		}
 #endif
 		if (!strncmp(p, "pt", 2))
 			iommu_set_default_passthrough(true);
include/linux/swiotlb.h +8 -4
index 2a421241b980..d69370c08094 100644
@@ -16,6 +16,10 @@ struct scatterlist;
 
 #define SWIOTLB_VERBOSE	(1 << 0) /* verbose initialization */
 #define SWIOTLB_ANY	(1 << 1) /* allow any memory for the buffer */
+/* Initialize a default-sized pool for devices with limited DMA addressing. */
+#define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 2)
+/* Initialize a default-sized pool that requires architecture remapping. */
+#define SWIOTLB_INIT_REMAP		(1 << 3)
 
 /*
  * Maximum allowable number of contiguous slabs to map,
@@ -39,8 +43,8 @@ struct scatterlist;
 #endif
 
 unsigned long swiotlb_size_or_default(void);
-void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags,
-	int (*remap)(void *tlb, unsigned long nslabs));
+void __init swiotlb_init_remap(unsigned int flags,
+			       int (*remap)(void *tlb, unsigned long nslabs));
 int swiotlb_init_late(size_t size, gfp_t gfp_mask,
 	int (*remap)(void *tlb, unsigned long nslabs));
 extern void __init swiotlb_update_mem_attributes(void);
@@ -183,7 +187,7 @@ static inline bool is_swiotlb_force_bounce(struct device *dev)
 	return mem && mem->force_bounce;
 }
 
-void swiotlb_init(bool addressing_limited, unsigned int flags);
+void swiotlb_init(unsigned int flags);
 void __init swiotlb_exit(void);
 void swiotlb_dev_init(struct device *dev);
 size_t swiotlb_max_mapping_size(struct device *dev);
@@ -193,7 +197,7 @@ void __init swiotlb_adjust_size(unsigned long size);
 phys_addr_t default_swiotlb_base(void);
 phys_addr_t default_swiotlb_limit(void);
 #else
-static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
+static inline void swiotlb_init(unsigned int flags)
 {
 }
 
kernel/dma/swiotlb.c +96 -38

199 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

PATCH v5 2/6 dma: swiotlb: Track whether the pool size was explicitly set

In reply to Aneesh Kumar K.V (Arm)

swiotlb_adjust_size() must not override a pool size supplied through the swiotlb= command-line parameter.

Currently, it determines whether a size was supplied by comparing default_nslabs with IO_TLB_DEFAULT_SIZE. This fails when the user explicitly requests the compiled-in default size, allowing the request to be overwritten by automatic pool sizing.

Record whether swiotlb= supplied a numeric size and add swiotlb_default_size_changed() to query that state.

---
 kernel/dma/swiotlb.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/dma/swiotlb.c +8 -1
index aa19e81ae544..8f86deb25be2 100644
@@ -107,6 +107,7 @@ static struct io_tlb_mem io_tlb_default_mem;
 
 static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
 static unsigned long default_nareas;
+static bool swiotlb_size_set __initdata;
 
 /**
  * struct io_tlb_area - IO TLB memory area descriptor
@@ -269,6 +270,7 @@ setup_io_tlb_npages(char *str)
 		/* avoid tail segment of size < IO_TLB_SEGSIZE */
 		default_nslabs =
 			ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
+		swiotlb_size_set = true;
 	}
 	if (*str == ',')
 		++str;
@@ -304,6 +306,11 @@ unsigned long swiotlb_size_or_default(void)
 	return default_nslabs << IO_TLB_SHIFT;
 }
 
+static bool __init swiotlb_default_size_changed(void)
+{
+	return swiotlb_size_set;
+}
+
 void __init swiotlb_adjust_size(unsigned long size)
 {
 	/*
@@ -311,7 +318,7 @@ void __init swiotlb_adjust_size(unsigned long size)
 	 * architectures such as those supporting memory encryption to
 	 * adjust/expand SWIOTLB size for their use.
 	 */
-	if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
+	if (swiotlb_default_size_changed())
 		return;
 
 	default_nslabs = swiotlb_aligned_nslabs(size);
Signature · 1 line
2.43.0
#10 lore raw

PATCH v5 3/6 dma: swiotlb: Centralize minimal pool sizing

In reply to Aneesh Kumar K.V (Arm)

A default SWIOTLB pool used only for unaligned kmalloc bouncing can be smaller than one required for limited DMA addressing. Arm64 and RISC-V currently implement this sizing independently, using 1 MiB of SWIOTLB space per GiB of RAM.

The SWIOTLB core now identifies this case with SWIOTLB_POOL_MINIMAL. Size that policy in swiotlb_adjust_pool_size() and remove the architecture-specific adjustments.

Explicit swiotlb= sizing remains unchanged because swiotlb_adjust_size() preserves a user-configured size.

---
 arch/arm64/mm/init.c | 11 -----------
 arch/riscv/mm/init.c |  4 ----
 kernel/dma/swiotlb.c | 11 ++++++++++-
 3 files changed, 10 insertions(+), 16 deletions(-)
arch/arm64/mm/init.c +0 -11
index 7d51fb5f5a71..95075b5c8207 100644
@@ -340,17 +340,6 @@ void __init arch_mm_preinit(void)
 {
 	unsigned int flags = SWIOTLB_VERBOSE;
 
-	if (max_pfn <= PFN_DOWN(arm64_dma_phys_limit)) {
-		/*
-		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
-		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
-		 */
-		unsigned long size =
-			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
-
-		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-	}
-
 	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))
 		flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
 
arch/riscv/mm/init.c +0 -4
index e0e4c103d603..96856114b387 100644
@@ -183,10 +183,6 @@ void __init arch_mm_preinit(void)
 		 * memory when DRAM starts above dma32_phys_limit.  Allocate
 		 * ~1 MB per 1 GB of RAM.
 		 */
-		unsigned long size =
-			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
-		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
 		swiotlb_flags |= SWIOTLB_ANY;
 	}
 
kernel/dma/swiotlb.c +10 -1
index 8f86deb25be2..f368a73f4ed0 100644
@@ -483,9 +483,18 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
 static void __init
 swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)
 {
+	if (swiotlb_default_size_changed())
+		return;
+
 	switch (policy) {
-	case SWIOTLB_POOL_MINIMAL:
+	case SWIOTLB_POOL_MINIMAL: {
+		unsigned long size;
+
+		/* Use 1MB per 1GB of RAM for kmalloc() bouncing. */
+		size = DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
 		break;
+	}
 	case SWIOTLB_POOL_CC_GUEST:
 		break;
 	case SWIOTLB_POOL_NONE:
Signature · 1 line
2.43.0
#15 lore raw

PATCH v5 4/6 dma: swiotlb: Centralize memory-encryption pool sizing

In reply to Aneesh Kumar K.V (Arm)

Memory-encrypted guests use shared or unencrypted memory for DMA and may route all DMA through SWIOTLB. The default pool can therefore be too small for I/O-intensive workloads.

Move the existing x86 guest-sizing policy into the SWIOTLB core. For SWIOTLB_POOL_CC_GUEST, size the pool to 6% of guest memory, clamped between the normal default and 1 GiB. Preserve an explicit swiotlb= size.

Provide swiotlb_adjusted_size() so early users, including the x86 crash kernel reservation, can account for the prospective guest pool size before SWIOTLB initialization. Use the same area-aware alignment for both the prospective and allocated sizes.

Host memory encryption still selects a normal-sized shared pool and does not use the guest-sizing policy.

A restricted DMA pool already provides shared bounce buffers for its devices. Record its presence during reserved-memory initialization and do not select the confidential-guest default-pool policy solely because guest memory encryption is active.

---
 arch/x86/include/asm/crash_reserve.h |  6 ++--
 arch/x86/mm/mem_encrypt.c            | 24 --------------
 include/linux/swiotlb.h              |  6 ++++
 kernel/dma/swiotlb.c                 | 47 +++++++++++++++++++++++++++-
 4 files changed, 56 insertions(+), 27 deletions(-)
arch/x86/include/asm/crash_reserve.h +4 -2
index 7835b2cdff04..c1f5bf5d4182 100644
@@ -18,7 +18,9 @@
  * no good way to detect the paging mode of the target kernel which will be
  * loaded for dumping.
  */
-extern unsigned long swiotlb_size_or_default(void);
+#ifdef CONFIG_X86_64
+unsigned long __init swiotlb_adjusted_size(void);
+#endif
 
 #ifdef CONFIG_X86_32
 # define CRASH_ADDR_LOW_MAX     SZ_512M
@@ -33,7 +35,7 @@ extern unsigned long swiotlb_size_or_default(void);
 static inline unsigned long crash_low_size_default(void)
 {
 #ifdef CONFIG_X86_64
-	return max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
+	return max(swiotlb_adjusted_size() + (8UL << 20), 256UL << 20);
 #else
 	return 0;
 #endif
arch/x86/mm/mem_encrypt.c +0 -24
index 95bae74fdab2..912f22ca838f 100644
@@ -101,9 +101,6 @@ void __init mem_encrypt_init(void)
 
 void __init mem_encrypt_setup_arch(void)
 {
-	phys_addr_t total_mem = memblock_phys_mem_size();
-	unsigned long size;
-
 	/*
 	 * Do RMP table fixups after the e820 tables have been setup by
 	 * e820__memory_setup().
@@ -114,27 +111,6 @@ void __init mem_encrypt_setup_arch(void)
 	if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
 		return;
 
-	/*
-	 * For SEV and TDX, all DMA has to occur via shared/unencrypted pages.
-	 * Kernel uses SWIOTLB to make this happen without changing device
-	 * drivers. However, depending on the workload being run, the
-	 * default 64MB of SWIOTLB may not be enough and SWIOTLB may
-	 * run out of buffers for DMA, resulting in I/O errors and/or
-	 * performance degradation especially with high I/O workloads.
-	 *
-	 * Adjust the default size of SWIOTLB using a percentage of guest
-	 * memory for SWIOTLB buffers. Also, as the SWIOTLB bounce buffer
-	 * memory is allocated from low memory, ensure that the adjusted size
-	 * is within the limits of low available memory.
-	 *
-	 * The percentage of guest memory used here for SWIOTLB buffers
-	 * is more of an approximation of the static adjustment which
-	 * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
-	 */
-	size = total_mem * 6 / 100;
-	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
-	swiotlb_adjust_size(size);
-
 	/* Set restricted memory access for virtio. */
 	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
 }
include/linux/swiotlb.h +6 -0
index d69370c08094..be3962a33fc6 100644
@@ -193,6 +193,7 @@ void swiotlb_dev_init(struct device *dev);
 size_t swiotlb_max_mapping_size(struct device *dev);
 bool is_swiotlb_allocated(void);
 bool is_swiotlb_active(struct device *dev);
+unsigned long __init swiotlb_adjusted_size(void);
 void __init swiotlb_adjust_size(unsigned long size);
 phys_addr_t default_swiotlb_base(void);
 phys_addr_t default_swiotlb_limit(void);
@@ -232,6 +233,11 @@ static inline bool is_swiotlb_active(struct device *dev)
 	return false;
 }
 
+static inline unsigned long __init swiotlb_adjusted_size(void)
+{
+	return 0;
+}
+
 static inline void swiotlb_adjust_size(unsigned long size)
 {
 }
kernel/dma/swiotlb.c +46 -1

77 lines not on this page · raw message

Signature · 1 line
2.43.0
#20 lore raw

PATCH v5 5/6 dma: swiotlb: Add an overridable architecture pool opt-out

In reply to Aneesh Kumar K.V (Arm)

On x86, iommu=off historically prevented SWIOTLB initialization by not setting SWIOTLB_INIT_ADDRESSING_LIMIT. This overloads that flag: it controls both whether a pool is selected and where the pool is allocated. If another requirement, swiotlb=force selects a pool independently, omitting SWIOTLB_INIT_ADDRESSING_LIMIT may place it above the addressable range of devices which need bounce buffering.

Add SWIOTLB_INIT_FORCE_DISABLE to let an architecture opt out of normal pool initialization without discarding the pool placement constraints.

Unlike swiotlb_force_disable, which is set by swiotlb=noforce and unconditionally prevents pool initialization, the new flag is an architecture default. Explicit requirements such as memory encryption, architecture remapping, or swiotlb=force take precedence over it.

iommu=soft retains SWIOTLB_INIT_ADDRESSING_LIMIT because it requests software bounce buffering for devices whose DMA masks cannot address all system memory. Such devices must be able to reach the bounce pool.

Clear both flags for Xen because Xen explicitly requires a remapped SWIOTLB pool and may allocate it anywhere in directly mapped memory.

With no override, iommu=off disables both hardware IOMMU translation and SWIOTLB bouncing. Direct DMA remains available; mappings outside a device's addressable range fail instead of being bounced.

---
 arch/x86/kernel/pci-dma.c | 11 ++++++++---
 include/linux/swiotlb.h   |  2 ++
 kernel/dma/swiotlb.c      | 10 ++++++++--
 3 files changed, 18 insertions(+), 5 deletions(-)
arch/x86/kernel/pci-dma.c +8 -3
index 0cff255827ba..ce257b20ea9c 100644
@@ -44,9 +44,13 @@ static unsigned int x86_swiotlb_flags;
 static void __init pci_swiotlb_detect(void)
 {
 	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
-		x86_swiotlb_enable = true;
+	if (no_iommu)
+		x86_swiotlb_flags |= SWIOTLB_INIT_DEFAULT_OFF;
+
+	if (max_possible_pfn > MAX_DMA32_PFN) {
 		x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+		if (!no_iommu)
+			x86_swiotlb_enable = true;
 	}
 
 	/*
@@ -84,7 +88,8 @@ static void __init pci_xen_swiotlb_init(void)
 		return;
 	x86_swiotlb_enable = true;
 	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
-	x86_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
+	x86_swiotlb_flags &= ~(SWIOTLB_INIT_ADDRESSING_LIMIT |
+			       SWIOTLB_INIT_DEFAULT_OFF);
 	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;
 	swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup);
 	dma_ops = &xen_swiotlb_dma_ops;
include/linux/swiotlb.h +2 -0
index be3962a33fc6..466c33795db0 100644
@@ -20,6 +20,8 @@ struct scatterlist;
 #define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 2)
 /* Initialize a default-sized pool that requires architecture remapping. */
 #define SWIOTLB_INIT_REMAP		(1 << 3)
+/* Do not initialize a pool unless SWIOTLB is explicitly required. */
+#define SWIOTLB_INIT_DEFAULT_OFF		(1 << 4)
 
 /*
  * Maximum allowable number of contiguous slabs to map,
kernel/dma/swiotlb.c +8 -2
index c19675846fc1..46b789eb0806 100644
@@ -563,10 +563,16 @@ swiotlb_select_pool_policy(unsigned int flags)
 	if (flags & SWIOTLB_INIT_REMAP)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
+	if (swiotlb_force_bounce)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (swiotlb_force_bounce)
+	/*
+	 * Explicit requirements above override an architecture's default opt-out.
+	 */
+	if (flags & SWIOTLB_INIT_DEFAULT_OFF)
+		return SWIOTLB_POOL_NONE;
+
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
 		return SWIOTLB_POOL_DEFAULT;
 
 	if (swiotlb_kmalloc_needs_bounce())
Signature · 1 line
2.43.0
#25 lore raw

PATCH v5 6/6 dma: swiotlb: Remove SWIOTLB_ANY

In reply to Aneesh Kumar K.V (Arm)

swiotlb pools are currently allocated below the architecture's low address limit by default. SWIOTLB_ANY overrides this and permits allocation anywhere in directly mapped memory.

SWIOTLB_INIT_ADDRESSING_LIMIT now explicitly identifies callers which need a pool that is reachable by devices with limited DMA addressing. Use it as the placement constraint and remove SWIOTLB_ANY.

This changes the default behavior for callers which do not set either flag. Previously, omitting SWIOTLB_ANY placed the default pool below the architecture's low address limit and also constrained dynamically allocated pools to the same range. After this change, omitting SWIOTLB_INIT_ADDRESSING_LIMIT permits both static and dynamic pools to use any directly mapped memory.

---
 Documentation/core-api/swiotlb.rst |  8 ++++----
 arch/powerpc/mm/mem.c              |  6 ++++--
 arch/riscv/mm/init.c               | 16 ----------------
 arch/s390/mm/init.c                |  2 +-
 arch/x86/kernel/pci-dma.c          |  2 +-
 include/linux/swiotlb.h            |  7 +++----
 kernel/dma/swiotlb.c               | 17 ++++++-----------
 7 files changed, 19 insertions(+), 39 deletions(-)
Documentation/core-api/swiotlb.rst +4 -4
index 71b4e4c27eb5..06d9f88fdf7b 100644
@@ -148,10 +148,10 @@ may also be adjusted
 due to other conditions, such as running in a CoCo VM, as described above. If
 CONFIG_SWIOTLB_DYNAMIC is enabled, additional pools may be allocated later in
 the life of the system. Each pool must be a contiguous range of physical
-memory. The default pool is allocated below the 4 GiB physical address line so
-it works for devices that can only address 32-bits of physical memory (unless
-architecture-specific code provides the SWIOTLB_ANY flag). In a CoCo VM, the
-pool memory must be decrypted before swiotlb is used.
+memory. The default pool is allocated below the architecture's low address
+limit when it is needed for devices with limited DMA addressing. Otherwise,
+it may be allocated anywhere in directly mapped memory. In a CoCo VM, the pool
+memory must be decrypted before swiotlb is used.
 
 Each pool is divided into "slots" of size IO_TLB_SIZE, which is 2 KiB with
 current definitions. IO_TLB_SEGSIZE contiguous slots (128 slots) constitute
arch/powerpc/mm/mem.c +4 -2
index 006ed41097e9..1bdea6c03848 100644
@@ -295,9 +295,11 @@ void __init arch_mm_preinit(void)
 		/*
 		 * Since the guest memory is inaccessible to the host,
 		 * devices always need to use the SWIOTLB buffer for DMA
-		 * even if dma_capable() says otherwise.
+		 * even if dma_capable() says otherwise. The hypervisor has
+		 * no addressing limitation, so the buffer may be allocated
+		 * anywhere.
 		 */
-		ppc_swiotlb_flags |= SWIOTLB_ANY;
+		ppc_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
 	}
 
 	/*
arch/riscv/mm/init.c +0 -16
index 96856114b387..734712230f35 100644
@@ -165,27 +165,11 @@ static void print_vm_layout(void) { }
 
 void __init arch_mm_preinit(void)
 {
-	bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit) &&
-		       memblock_start_of_DRAM() < dma32_phys_limit;
 	unsigned int swiotlb_flags = SWIOTLB_VERBOSE;
 #ifdef CONFIG_FLATMEM
 	BUG_ON(!mem_map);
 #endif /* CONFIG_FLATMEM */
 
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
-	    dma_cache_alignment != 1) {
-		/*
-		 * No 32-bit DMA bouncing needed (either all DRAM is within
-		 * the 32-bit limit, or it all starts above it), but
-		 * kmalloc() buffers whose sizes are not cache-line-aligned
-		 * still require bouncing for non-coherent DMA.  Use
-		 * SWIOTLB_ANY so that the buffer can be allocated from high
-		 * memory when DRAM starts above dma32_phys_limit.  Allocate
-		 * ~1 MB per 1 GB of RAM.
-		 */
-		swiotlb_flags |= SWIOTLB_ANY;
-	}
-
 	if ((max_pfn > PFN_DOWN(dma32_phys_limit)) &&
 	     (memblock_start_of_DRAM() < dma32_phys_limit))
 		swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
arch/s390/mm/init.c +1 -1
index 78f507e24ceb..ce10292447f1 100644
@@ -166,7 +166,7 @@ static void __init pv_init(void)
 	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
 
 	/* make sure bounce buffers are shared */
-	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_ANY);
+	swiotlb_init(SWIOTLB_VERBOSE);
 	swiotlb_update_mem_attributes();
 }
 
arch/x86/kernel/pci-dma.c +1 -1
index ce257b20ea9c..74e92b457758 100644
@@ -90,7 +90,7 @@ static void __init pci_xen_swiotlb_init(void)
 	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
 	x86_swiotlb_flags &= ~(SWIOTLB_INIT_ADDRESSING_LIMIT |
 			       SWIOTLB_INIT_DEFAULT_OFF);
-	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;
+	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP;
 	swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup);
 	dma_ops = &xen_swiotlb_dma_ops;
 	if (IS_ENABLED(CONFIG_PCI))
include/linux/swiotlb.h +3 -4
index 466c33795db0..503e1446e0d8 100644
@@ -15,13 +15,12 @@ struct page;
 struct scatterlist;
 
 #define SWIOTLB_VERBOSE	(1 << 0) /* verbose initialization */
-#define SWIOTLB_ANY	(1 << 1) /* allow any memory for the buffer */
 /* Initialize a default-sized pool for devices with limited DMA addressing. */
-#define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 2)
+#define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 1)
 /* Initialize a default-sized pool that requires architecture remapping. */
-#define SWIOTLB_INIT_REMAP		(1 << 3)
+#define SWIOTLB_INIT_REMAP		(1 << 2)
 /* Do not initialize a pool unless SWIOTLB is explicitly required. */
-#define SWIOTLB_INIT_DEFAULT_OFF		(1 << 4)
+#define SWIOTLB_INIT_DEFAULT_OFF	(1 << 3)
 
 /*
  * Maximum allowable number of contiguous slabs to map,
kernel/dma/swiotlb.c +6 -11
index 46b789eb0806..77d7c320de28 100644
@@ -450,15 +450,10 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
 	size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
 	void *tlb;
 
-	/*
-	 * By default allocate the bounce buffer memory from low memory, but
-	 * allow to pick a location everywhere for hypervisors with guest
-	 * memory encryption.
-	 */
-	if (flags & SWIOTLB_ANY)
-		tlb = memblock_alloc(bytes, PAGE_SIZE);
-	else
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
 		tlb = memblock_alloc_low(bytes, PAGE_SIZE);
+	else
+		tlb = memblock_alloc(bytes, PAGE_SIZE);
 
 	if (!tlb) {
 		pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
@@ -604,10 +599,10 @@ void __init swiotlb_init_remap(unsigned int flags,
 #ifdef CONFIG_SWIOTLB_DYNAMIC
 	if (!remap)
 		io_tlb_default_mem.can_grow = true;
-	if (flags & SWIOTLB_ANY)
-		io_tlb_default_mem.phys_limit = virt_to_phys(high_memory - 1);
-	else
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
 		io_tlb_default_mem.phys_limit = ARCH_LOW_ADDRESS_LIMIT;
+	else
+		io_tlb_default_mem.phys_limit = virt_to_phys(high_memory - 1);
 #endif
 
 	/* if we have host or guest memory encryption */
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v5 2/6] dma: swiotlb: Track whether the pool size was explicitly set

In reply to Aneesh Kumar K.V (Arm)
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

swiotlb_adjust_size() must not override a pool size supplied through the swiotlb= command-line parameter.

Currently, it determines whether a size was supplied by comparing default_nslabs with IO_TLB_DEFAULT_SIZE. This fails when the user explicitly requests the compiled-in default size, allowing the request to be overwritten by automatic pool sizing.

Quoted 4 lines

Record whether swiotlb= supplied a numeric size and add swiotlb_default_size_changed() to query that state.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#11 lore raw

Re: [PATCH v5 3/6] dma: swiotlb: Centralize minimal pool sizing

In reply to Aneesh Kumar K.V (Arm)
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

A default SWIOTLB pool used only for unaligned kmalloc bouncing can be smaller than one required for limited DMA addressing. Arm64 and RISC-V currently implement this sizing independently, using 1 MiB of SWIOTLB space per GiB of RAM.

Quoted 8 lines

The SWIOTLB core now identifies this case with SWIOTLB_POOL_MINIMAL. Size that policy in swiotlb_adjust_pool_size() and remove the architecture-specific adjustments.

Explicit swiotlb= sizing remains unchanged because swiotlb_adjust_size() preserves a user-configured size.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#26 lore raw

Re: [PATCH v5 6/6] dma: swiotlb: Remove SWIOTLB_ANY

In reply to Aneesh Kumar K.V (Arm)
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

swiotlb pools are currently allocated below the architecture's low address limit by default. SWIOTLB_ANY overrides this and permits allocation anywhere in directly mapped memory.

SWIOTLB_INIT_ADDRESSING_LIMIT now explicitly identifies callers which need a pool that is reachable by devices with limited DMA addressing. Use it as the placement constraint and remove SWIOTLB_ANY.

This changes the default behavior for callers which do not set either flag. Previously, omitting SWIOTLB_ANY placed the default pool below the architecture's low address limit and also constrained dynamically allocated pools to the same range. After this change, omitting SWIOTLB_INIT_ADDRESSING_LIMIT permits both static and dynamic pools to use any directly mapped memory.

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#21 lore raw

Re: [PATCH v5 5/6] dma: swiotlb: Add an overridable architecture pool opt-out

In reply to Aneesh Kumar K.V (Arm)
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] dma: swiotlb: default opt-out incorrectly overrides iommu=soft --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dma: swiotlb: default opt-out incorrectly overrides iommu=soft
--

commit 8a081ec4054b9ec450c0c293130d0ceb6b8d20fa
Author: Aneesh Kumar K.V (Arm) <[email protected]>

dma: swiotlb: Add an overridable architecture pool opt-out

This commit introduces SWIOTLB_INIT_DEFAULT_OFF to allow an architecture to opt out of SWIOTLB pool initialization without losing placement constraints.

arch/x86/kernel/pci-dma.c static unsigned int x86_swiotlb_flags; · line 48

Quoted 12 lines
arch/x86/kernel/pci-dma.c +2 -2
index 0cff255827baf..ce257b20ea9cf 100644
@@ -44,9 +44,13 @@ static unsigned int x86_swiotlb_flags;
 static void __init pci_swiotlb_detect(void)
 {
 	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
-		x86_swiotlb_enable = true;
+	if (no_iommu)
+		x86_swiotlb_flags |= SWIOTLB_INIT_DEFAULT_OFF;

[Severity: High]
When this is combined with iommu=soft (which sets SWIOTLB_INIT_ADDRESSING_LIMIT), doesn't this flag overwrite the user's explicit fallback request?

[ ... ]

Quoted 13 lines
kernel/dma/swiotlb.c +1 -2
index c19675846fc1d..46b789eb08063 100644
@@ -563,10 +563,16 @@ swiotlb_select_pool_policy(unsigned int flags)
 	if (flags & SWIOTLB_INIT_REMAP)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
+	if (swiotlb_force_bounce)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (swiotlb_force_bounce)

kernel/dma/swiotlb.c swiotlb_select_pool_policy(unsigned int flags) · line 578

Quoted 10 lines
kernel/dma/swiotlb.c +7 -0
+	/*
+	 * Explicit requirements above override an architecture's default opt-out.
+	 */
+	if (flags & SWIOTLB_INIT_DEFAULT_OFF)
+		return SWIOTLB_POOL_NONE;
+
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
 		return SWIOTLB_POOL_DEFAULT;
 
 	if (swiotlb_kmalloc_needs_bounce())

[Severity: High]
By evaluating SWIOTLB_INIT_DEFAULT_OFF before SWIOTLB_INIT_ADDRESSING_LIMIT and swiotlb_kmalloc_needs_bounce(), does the default opt-out incorrectly override explicit requests for a software bounce pool and unaligned kmalloc bouncing?

For instance, if a user boots an x86 system with iommu=off iommu=soft, pci_swiotlb_detect() sets SWIOTLB_INIT_DEFAULT_OFF because of no_iommu=1. The iommu=soft parameter also sets SWIOTLB_INIT_ADDRESSING_LIMIT.

Because the opt-out is checked first here in swiotlb_select_pool_policy(), it returns SWIOTLB_POOL_NONE and prevents the pool from being allocated. Could this lead to DMA mapping failures for devices that cannot address all system memory, contradicting the commit message which states that iommu=soft retains the addressing limit?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#16 lore raw

Re: [PATCH v5 4/6] dma: swiotlb: Centralize memory-encryption pool sizing

In reply to Aneesh Kumar K.V (Arm)
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] swiotlb: 64-bit division build error and 32-bit multiplication overflow --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] swiotlb: 64-bit division build error and 32-bit multiplication overflow --

commit 7f27b828f817aa2b9787b04060d6febbcd685bcf
Author: Aneesh Kumar K.V (Arm) <[email protected]>

dma: swiotlb: Centralize memory-encryption pool sizing

The patch moves the x86 guest-sizing policy into the SWIOTLB core, sizing the SWIOTLB_POOL_CC_GUEST pool to 6% of guest memory. It also provides a way for early users like the crash kernel to view the adjusted pool size before SWIOTLB initialization.

Quoted 8 lines
kernel/dma/swiotlb.c +3 -0
@@ -480,6 +481,46 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
 
+/**
+ * swiotlb_adjusted_size() - get the prospective adjusted SWIOTLB size
+ *

[ ... ]

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 500

Quoted 8 lines

+ */

kernel/dma/swiotlb.c +7 -0
+unsigned long __init swiotlb_adjusted_size(void)
+{
+	unsigned long nslabs, size = swiotlb_size_or_default();
+
+	if (swiotlb_default_size_changed() ||
+	    !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+		return size;

[Severity: High]
This is a pre-existing issue, but returning size directly here bypasses area-aware alignment for explicitly configured sizes and non-CC_GUEST setups.

Could this discrepancy, along with the behavior of swiotlb_aligned_nslabs() during early boot, cause the crash kernel to under-reserve memory?

The crash kernel reserves low memory based on the prospective size from swiotlb_adjusted_size() (or explicit swiotlb= sizes). However, early in boot, default_nareas is 0, so swiotlb_aligned_nslabs() merely aligns to segments and skips the area-aware power-of-two rounding:

kernel/dma/swiotlb.c:swiotlb_aligned_nslabs() {
	...
	if (!default_nareas)
		return nslabs;

	if (nslabs < IO_TLB_SEGSIZE * default_nareas)
		nslabs = IO_TLB_SEGSIZE * default_nareas;
	else if (!is_power_of_2(nslabs))
		nslabs = roundup_pow_of_two(nslabs);
	...
}

Later, swiotlb_init_remap() initializes default_nareas and calls swiotlb_adjust_nareas(), triggering round_up_default_nslabs() to round the pool up to the next power of two.

As a result, the allocated SWIOTLB pool can be up to ~50% larger than the reserved size (e.g., 600MB rounded to 1024MB). When the crash kernel attempts to allocate this rounded-up size, will it exceed its reserved memory and OOM?

Quoted 2 lines
kernel/dma/swiotlb.c +2 -0
+	/*
+	 * For SEV and TDX and CCA, all DMA has to occur via

[ ... ]

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 518

Quoted 6 lines
kernel/dma/swiotlb.c +6 -0
+	 * The percentage of guest memory used here for SWIOTLB buffers is
+	 * more of an approximation of the static adjustment which 64MB for
+	 * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+	 */
+	size = memblock_phys_mem_size() * 6 / 100;
+	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);

[Severity: High]
Does this calculation cause an undefined reference to __udivdi3 on 32-bit PAE builds, and overflow on 32-bit non-PAE builds?

The guest sizing logic has been relocated into kernel/dma/swiotlb.c, which is compiled for all architectures. On 32-bit kernels with PAE, phys_addr_t is 64-bit, so memblock_phys_mem_size() * 6 / 100 performs a 64-bit division by 100. GCC will emit a call to __udivdi3, which is missing from the kernel's libgcc, causing a fatal build link error.

On 32-bit kernels without PAE, phys_addr_t is 32-bit. If physical memory exceeds ~715MB (UINT_MAX / 6), the multiplication `memblock_phys_mem_size()
* 6` overflows the 32-bit integer, resulting in a silently truncated and incorrect pool size.

Should this be refactored to cast the operand to u64 and use div_u64() to be safe across all platforms?

Quoted 4 lines
kernel/dma/swiotlb.c +4 -0
+	nslabs = swiotlb_aligned_nslabs(size);
+
+	return nslabs << IO_TLB_SHIFT;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#2 lore raw

Re: [PATCH v5 1/6] dma: swiotlb: Centralize default pool policy selection

In reply to Aneesh Kumar K.V (Arm)
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

The addressing_limited argument to swiotlb_init() describes only one reason for allocating a default SWIOTLB pool. A pool may also be needed for memory encryption, unaligned kmalloc bouncing, or swiotlb=force.

Replace the boolean argument with the SWIOTLB_INIT_ADDRESSING_LIMIT flag and have architectures report their DMA addressing constraints through the initialization flags.

Introduce SWIOTLB pool policies and select the policy in the core before allocating the default pool. This separates the decision to allocate a pool from the policy used to size it, allowing subsequent changes to centralize minimal and confidential-guest sizing.

Quoted 6 lines

Set the shared-pool state before memory attributes are updated. Also move the pseries secure-guest setup before swiotlb_init() so its initialization flags are available when the policy is selected.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v5 2/6
#7 lore raw

Re: [PATCH v5 2/6] dma: swiotlb: Track whether the pool size was explicitly set

In reply to Aneesh Kumar K.V (Arm)
Quoted 8 lines · Aneesh Kumar K.V (Arm)

swiotlb_adjust_size() must not override a pool size supplied through the swiotlb= command-line parameter.

Currently, it determines whether a size was supplied by comparing default_nslabs with IO_TLB_DEFAULT_SIZE. This fails when the user explicitly requests the compiled-in default size, allowing the request to be overwritten by automatic pool sizing.

Quoted 4 lines

Record whether swiotlb= supplied a numeric size and add swiotlb_default_size_changed() to query that state.

maintainer Reviewed-by on v5 3/6
#12 lore raw

Re: [PATCH v5 3/6] dma: swiotlb: Centralize minimal pool sizing

In reply to Aneesh Kumar K.V (Arm)
Quoted 5 lines · Aneesh Kumar K.V (Arm)

A default SWIOTLB pool used only for unaligned kmalloc bouncing can be smaller than one required for limited DMA addressing. Arm64 and RISC-V currently implement this sizing independently, using 1 MiB of SWIOTLB space per GiB of RAM.

Quoted 8 lines

The SWIOTLB core now identifies this case with SWIOTLB_POOL_MINIMAL. Size that policy in swiotlb_adjust_pool_size() and remove the architecture-specific adjustments.

Explicit swiotlb= sizing remains unchanged because swiotlb_adjust_size() preserves a user-configured size.

reviewer Reviewed-by on v5 1/6
#3 lore raw

Re: [PATCH v5 1/6] dma: swiotlb: Centralize default pool policy selection

In reply to Aneesh Kumar K.V (Arm)

On 21/09/2026 7:36 am, Aneesh Kumar K.V (Arm) wrote:
[...]

arch/powerpc/kernel/dma-swiotlb.c unsigned int ppc_swiotlb_flags; · line 18

Quoted 11 lines
arch/powerpc/kernel/dma-swiotlb.c +1 -1
index ba256c37bcc0..97fffa46f05a 100644
@@ -14,8 +14,10 @@ unsigned int ppc_swiotlb_flags;
 
  void __init swiotlb_detect_4g(void)
  {
-	if ((memblock_end_of_DRAM() - 1) > 0xffffffff)
+	if ((memblock_end_of_DRAM() - 1) > 0xffffffff) {
  		ppc_swiotlb_enable = 1;

Nit: Perhaps it's beyond the functional scope of this patch, but I do wonder if these <arch>_swiotlb_enable conditions become redundant and could just be replaced with "if (<arch>_swiotlb_flags != 0)" too.

Quoted 5 lines
arch/powerpc/kernel/dma-swiotlb.c +2 -0
+		ppc_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
  }
 
  static int __init check_swiotlb_enabled(void)

[...]

Quoted 17 lines
arch/x86/kernel/pci-dma.c +3 -1
index 75cf8f6ae8cd..0cff255827ba 100644
@@ -44,8 +44,10 @@ static unsigned int x86_swiotlb_flags;
  static void __init pci_swiotlb_detect(void)
  {
  	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
+	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
  		x86_swiotlb_enable = true;
+		x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
 
  	/*
  	 * Set swiotlb to 1 so that bounce buffers are allocated and used for

arch/x86/kernel/pci-dma.c static void __init pci_xen_swiotlb_init(void) · line 88

Quoted 8 lines
arch/x86/kernel/pci-dma.c +3 -2
@@ -81,8 +83,10 @@ static void __init pci_xen_swiotlb_init(void)
  	if (!xen_swiotlb_enabled())
  		return;
  	x86_swiotlb_enable = true;
-	x86_swiotlb_flags |= SWIOTLB_ANY;
-	swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup);
+	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
+	x86_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
+	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;

Similarly, I see how having a SWIOTLB_INIT_REMAP flag keeps swiotlb_select_pool_policy() neat, but couldn't we technically already infer that from the fact that a non-NULL remap function was passed?

Nits aside, the overall change seems reasonable to me;

reviewer Reviewed-by on v5 2/6
#8 lore raw

Re: [PATCH v5 2/6] dma: swiotlb: Track whether the pool size was explicitly set

In reply to Aneesh Kumar K.V (Arm)
Quoted 21 lines · Aneesh Kumar K.V (Arm)

swiotlb_adjust_size() must not override a pool size supplied through the swiotlb= command-line parameter.

Currently, it determines whether a size was supplied by comparing default_nslabs with IO_TLB_DEFAULT_SIZE. This fails when the user explicitly requests the compiled-in default size, allowing the request to be overwritten by automatic pool sizing.

Record whether swiotlb= supplied a numeric size and add swiotlb_default_size_changed() to query that state.

---
  kernel/dma/swiotlb.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
kernel/dma/swiotlb.c +0 -0
index aa19e81ae544..8f86deb25be2 100644

kernel/dma/swiotlb.c static struct io_tlb_mem io_tlb_default_mem; · line 110

Quoted 4 lines
kernel/dma/swiotlb.c +1 -0
@@ -107,6 +107,7 @@ static struct io_tlb_mem io_tlb_default_mem;
 
  static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
  static unsigned long default_nareas;
+static bool swiotlb_size_set __initdata;

Nit: there are various ways and reasons that the size is set in general; could this please be named something clearer, e.g. "swiotlb_cmdline_size_set"?

Quoted 11 lines
kernel/dma/swiotlb.c +1 -0
  /**
   * struct io_tlb_area - IO TLB memory area descriptor
@@ -269,6 +270,7 @@ setup_io_tlb_npages(char *str)
  		/* avoid tail segment of size < IO_TLB_SEGSIZE */
  		default_nslabs =
  			ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
+		swiotlb_size_set = true;
  	}
  	if (*str == ',')
  		++str;

kernel/dma/swiotlb.c unsigned long swiotlb_size_or_default(void) · line 309

Quoted 4 lines
kernel/dma/swiotlb.c +1 -0
@@ -304,6 +306,11 @@ unsigned long swiotlb_size_or_default(void)
  	return default_nslabs << IO_TLB_SHIFT;
  }
 
+static bool __init swiotlb_default_size_changed(void)

...and then do we really need a trivial wrapper function with the exact same scope as the variable itself? (Plus the name is similarly confusing as the places this is used are themselves potentially changing the default size, if you consider "default" to mean "not explicitly specified"...)

With the naming improved,

Quoted 16 lines
kernel/dma/swiotlb.c +5 -1
+{
+	return swiotlb_size_set;
+}
+
  void __init swiotlb_adjust_size(unsigned long size)
  {
  	/*
@@ -311,7 +318,7 @@ void __init swiotlb_adjust_size(unsigned long size)
  	 * architectures such as those supporting memory encryption to
  	 * adjust/expand SWIOTLB size for their use.
  	 */
-	if (default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT)
+	if (swiotlb_default_size_changed())
  		return;
 
  	default_nslabs = swiotlb_aligned_nslabs(size);
reviewer
#13 lore raw

Re: [PATCH v5 3/6] dma: swiotlb: Centralize minimal pool sizing

In reply to Aneesh Kumar K.V (Arm)
Quoted 61 lines · Aneesh Kumar K.V (Arm)

A default SWIOTLB pool used only for unaligned kmalloc bouncing can be smaller than one required for limited DMA addressing. Arm64 and RISC-V currently implement this sizing independently, using 1 MiB of SWIOTLB space per GiB of RAM.

The SWIOTLB core now identifies this case with SWIOTLB_POOL_MINIMAL. Size that policy in swiotlb_adjust_pool_size() and remove the architecture-specific adjustments.

Explicit swiotlb= sizing remains unchanged because swiotlb_adjust_size() preserves a user-configured size.

---
  arch/arm64/mm/init.c | 11 -----------
  arch/riscv/mm/init.c |  4 ----
  kernel/dma/swiotlb.c | 11 ++++++++++-
  3 files changed, 10 insertions(+), 16 deletions(-)
arch/arm64/mm/init.c +0 -11
index 7d51fb5f5a71..95075b5c8207 100644
@@ -340,17 +340,6 @@ void __init arch_mm_preinit(void)
  {
  	unsigned int flags = SWIOTLB_VERBOSE;
 
-	if (max_pfn <= PFN_DOWN(arm64_dma_phys_limit)) {
-		/*
-		 * If no bouncing needed for ZONE_DMA, reduce the swiotlb
-		 * buffer for kmalloc() bouncing to 1MB per 1GB of RAM.
-		 */
-		unsigned long size =
-			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
-
-		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-	}
-
  	if (max_pfn > PFN_DOWN(arm64_dma_phys_limit))
  		flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
 
arch/riscv/mm/init.c +0 -4
index e0e4c103d603..96856114b387 100644
@@ -183,10 +183,6 @@ void __init arch_mm_preinit(void)
  		 * memory when DRAM starts above dma32_phys_limit.  Allocate
  		 * ~1 MB per 1 GB of RAM.
  		 */
-		unsigned long size =
-			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
-		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
-		swiotlb = true;
  		swiotlb_flags |= SWIOTLB_ANY;
  	}
 
kernel/dma/swiotlb.c +0 -0
index 8f86deb25be2..f368a73f4ed0 100644

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 488

Quoted 6 lines
kernel/dma/swiotlb.c +3 -0
@@ -483,9 +483,18 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
  static void __init
  swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)
  {
+	if (swiotlb_default_size_changed())
+		return;
+

This appears to be entirely redundant, as ultimately the point of this function is to call swiotlb_adjust_size() (if it does anything at all), and the first thing that does is this same exact check. We hardly need to micro-optimise short-circuiting a handful of arithmetic in a one-off setup path, and it's convoluted enough as it is, so please try to avoid redundancy that makes it even more confusing to follow.

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 496

Quoted 9 lines
kernel/dma/swiotlb.c +6 -1
  	switch (policy) {
-	case SWIOTLB_POOL_MINIMAL:
+	case SWIOTLB_POOL_MINIMAL: {
+		unsigned long size;
+
+		/* Use 1MB per 1GB of RAM for kmalloc() bouncing. */
+		size = DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
  		break;

Similarly I think it would be clearer if we had a common swiotlb_adjust_size() call at the end of the function, and then either calculate a size or return early in each switch case as appropriate.

Furthermore, swiotlb_size_or_default() is awful IMO - and in fact after this series we could perhaps clean it up entirely by making the size implicit in swiotlb_init_late() - not to mention misleadingly redundant. I'd say just open-code "default_nslabs << IO_TLB_SHIFT" like elsewhere in the file, but in fact it may as well just be IO_TLB_DEFAULT_SIZE (think about it...)

Thanks,
Robin.

Quoted 4 lines
kernel/dma/swiotlb.c +1 -0
+	}
  	case SWIOTLB_POOL_CC_GUEST:
  		break;
  	case SWIOTLB_POOL_NONE:
#4 lore raw

Re: [PATCH v5 1/6] dma: swiotlb: Centralize default pool policy selection

In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

On 21/09/2026 7:36 am, Aneesh Kumar K.V (Arm) wrote:
[...]

Quoted 6 lines
arch/powerpc/kernel/dma-swiotlb.c +0 -0
index ba256c37bcc0..97fffa46f05a 100644
@@ -14,8 +14,10 @@ unsigned int ppc_swiotlb_flags;
 
Quoted 10 lines

arch/powerpc/kernel/dma-swiotlb.c unsigned int ppc_swiotlb_flags; · line 18

Quoted 5 lines
arch/powerpc/kernel/dma-swiotlb.c +1 -1
  void __init swiotlb_detect_4g(void)
  {
-	if ((memblock_end_of_DRAM() - 1) > 0xffffffff)
+	if ((memblock_end_of_DRAM() - 1) > 0xffffffff) {
  		ppc_swiotlb_enable = 1;

Nit: Perhaps it's beyond the functional scope of this patch, but I do wonder if these <arch>_swiotlb_enable conditions become redundant and could just be replaced with "if (<arch>_swiotlb_flags != 0)" too.

Possibly. However, x86 has a case where x86_swiotlb_enable is cleared and used to free the swiotlb pool during initialization.

pci_iommu_init() {
	if (x86_swiotlb_enable) {
		pr_info("PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n");
		swiotlb_print_info();
	} else {
		swiotlb_exit();
	}
}
Quoted 21 lines
Quoted 6 lines
arch/powerpc/kernel/dma-swiotlb.c +2 -0
+		ppc_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
  }
 
  static int __init check_swiotlb_enabled(void)

[...]

Quoted 14 lines
arch/x86/kernel/pci-dma.c +3 -1
index 75cf8f6ae8cd..0cff255827ba 100644
@@ -44,8 +44,10 @@ static unsigned int x86_swiotlb_flags;
  static void __init pci_swiotlb_detect(void)
  {
  	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
+	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
  		x86_swiotlb_enable = true;
+		x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+	}
 
Quoted 16 lines

arch/x86/kernel/pci-dma.c static void __init pci_xen_swiotlb_init(void) · line 88

Quoted 11 lines
arch/x86/kernel/pci-dma.c +3 -2
  	/*
  	 * Set swiotlb to 1 so that bounce buffers are allocated and used for
@@ -81,8 +83,10 @@ static void __init pci_xen_swiotlb_init(void)
  	if (!xen_swiotlb_enabled())
  		return;
  	x86_swiotlb_enable = true;
-	x86_swiotlb_flags |= SWIOTLB_ANY;
-	swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup);
+	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
+	x86_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
+	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;

Similarly, I see how having a SWIOTLB_INIT_REMAP flag keeps swiotlb_select_pool_policy() neat, but couldn't we technically already infer that from the fact that a non-NULL remap function was passed?

I thought deriving the entire pool policy from the flags made the code easier to follow. For example, if flags == 0 and remap != NULL, we would also need to pass remap to swiotlb_select_pool_policy() to determine the policy.

Quoted 4 lines

Nits aside, the overall change seems reasonable to me;

Thanks
-aneesh

#9 lore raw

Re: [PATCH v5 2/6] dma: swiotlb: Track whether the pool size was explicitly set

In reply to Robin Murphy
Quoted 53 lines · Robin Murphy
Quoted 21 lines · Aneesh Kumar K.V (Arm)

swiotlb_adjust_size() must not override a pool size supplied through the swiotlb= command-line parameter.

Currently, it determines whether a size was supplied by comparing default_nslabs with IO_TLB_DEFAULT_SIZE. This fails when the user explicitly requests the compiled-in default size, allowing the request to be overwritten by automatic pool sizing.

Record whether swiotlb= supplied a numeric size and add swiotlb_default_size_changed() to query that state.

---
  kernel/dma/swiotlb.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
kernel/dma/swiotlb.c +0 -0
index aa19e81ae544..8f86deb25be2 100644

kernel/dma/swiotlb.c static struct io_tlb_mem io_tlb_default_mem; · line 110

Quoted 4 lines
kernel/dma/swiotlb.c +1 -0
@@ -107,6 +107,7 @@ static struct io_tlb_mem io_tlb_default_mem;
 
  static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
  static unsigned long default_nareas;
+static bool swiotlb_size_set __initdata;

Nit: there are various ways and reasons that the size is set in general; could this please be named something clearer, e.g. "swiotlb_cmdline_size_set"?

Quoted 11 lines
kernel/dma/swiotlb.c +1 -0
  /**
   * struct io_tlb_area - IO TLB memory area descriptor
@@ -269,6 +270,7 @@ setup_io_tlb_npages(char *str)
  		/* avoid tail segment of size < IO_TLB_SEGSIZE */
  		default_nslabs =
  			ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE);
+		swiotlb_size_set = true;
  	}
  	if (*str == ',')
  		++str;

kernel/dma/swiotlb.c unsigned long swiotlb_size_or_default(void) · line 309

Quoted 4 lines
kernel/dma/swiotlb.c +1 -0
@@ -304,6 +306,11 @@ unsigned long swiotlb_size_or_default(void)
  	return default_nslabs << IO_TLB_SHIFT;
  }
 
+static bool __init swiotlb_default_size_changed(void)

...and then do we really need a trivial wrapper function with the exact same scope as the variable itself? (Plus the name is similarly confusing as the places this is used are themselves potentially changing the default size, if you consider "default" to mean "not explicitly specified"...)

Quoted 2 lines

With the naming improved,

Updated and dropped the helper.

Quoted 3 lines

-aneesh

#14 lore raw

Re: [PATCH v5 3/6] dma: swiotlb: Centralize minimal pool sizing

In reply to Robin Murphy
Quoted 2 lines · Robin Murphy
Quoted 1 line · Aneesh Kumar K.V (Arm)

A default SWIOTLB pool used only for unaligned kmalloc bouncing can be

[ ... 54 lines skipped ... ]

Quoted 11 lines
Quoted 11 lines
kernel/dma/swiotlb.c +2 -0
index 8f86deb25be2..f368a73f4ed0 100644
@@ -483,9 +483,18 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
  static void __init
  swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)
  {
+	if (swiotlb_default_size_changed())
+		return;
Quoted 9 lines

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 488

Quoted 1 line
kernel/dma/swiotlb.c +1 -0
+

This appears to be entirely redundant, as ultimately the point of this function is to call swiotlb_adjust_size() (if it does anything at all), and the first thing that does is this same exact check. We hardly need to micro-optimise short-circuiting a handful of arithmetic in a one-off setup path, and it's convoluted enough as it is, so please try to avoid redundancy that makes it even more confusing to follow.

OK, I'll drop this.

Quoted 15 lines

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 496

Quoted 10 lines
kernel/dma/swiotlb.c +6 -1
  	switch (policy) {
-	case SWIOTLB_POOL_MINIMAL:
+	case SWIOTLB_POOL_MINIMAL: {
+		unsigned long size;
+
+		/* Use 1MB per 1GB of RAM for kmalloc() bouncing. */
+		size = DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
+		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
  		break;

Similarly I think it would be clearer if we had a common swiotlb_adjust_size() call at the end of the function, and then either calculate a size or return early in each switch case as appropriate.

Quoted 7 lines

Furthermore, swiotlb_size_or_default() is awful IMO - and in fact after this series we could perhaps clean it up entirely by making the size implicit in swiotlb_init_late() - not to mention misleadingly redundant. I'd say just open-code "default_nslabs << IO_TLB_SHIFT" like elsewhere in the file, but in fact it may as well just be IO_TLB_DEFAULT_SIZE (think about it...)

How about we rename swiotlb_size_or_default to

unsigned long swiotlb_default_pool_size(void)

{
	return default_nslabs << IO_TLB_SHIFT;
}

We still need a helper because arch/arm/xen/mm.c also uses it. I also updated swiotlb_adjust_pool_size() as suggested.

static void __init
swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)

{
	unsigned long size;

	switch (policy) {
	case SWIOTLB_POOL_MINIMAL:
		/* Use 1MB per 1GB of RAM for kmalloc() bouncing. */
		size = DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
		size = min(swiotlb_size_or_default(), size);
		break;
	case SWIOTLB_POOL_CC_GUEST:
		size = swiotlb_adjusted_size();
		break;
	case SWIOTLB_POOL_NONE:
		WARN(true, "Cannot adjust SWIOTLB size without a pool\n");
		return;
	case SWIOTLB_POOL_DEFAULT:
	default:
		return;
	}

	swiotlb_adjust_size(size);
}

-aneesh

#17 lore raw

Re: [PATCH v5 4/6] dma: swiotlb: Centralize memory-encryption pool sizing

In reply to [email protected]
Quoted 56 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] swiotlb: 64-bit division build error and 32-bit multiplication overflow --

commit 7f27b828f817aa2b9787b04060d6febbcd685bcf
Author: Aneesh Kumar K.V (Arm) <[email protected]>

dma: swiotlb: Centralize memory-encryption pool sizing

The patch moves the x86 guest-sizing policy into the SWIOTLB core, sizing the SWIOTLB_POOL_CC_GUEST pool to 6% of guest memory. It also provides a way for early users like the crash kernel to view the adjusted pool size before SWIOTLB initialization.

Quoted 8 lines
kernel/dma/swiotlb.c +3 -0
@@ -480,6 +481,46 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
 
+/**
+ * swiotlb_adjusted_size() - get the prospective adjusted SWIOTLB size
+ *

[ ... ]

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 500

Quoted 8 lines

+ */

kernel/dma/swiotlb.c +7 -0
+unsigned long __init swiotlb_adjusted_size(void)
+{
+	unsigned long nslabs, size = swiotlb_size_or_default();
+
+	if (swiotlb_default_size_changed() ||
+	    !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+		return size;

[Severity: High]
This is a pre-existing issue, but returning size directly here bypasses area-aware alignment for explicitly configured sizes and non-CC_GUEST setups.

Could this discrepancy, along with the behavior of swiotlb_aligned_nslabs() during early boot, cause the crash kernel to under-reserve memory?

The crash kernel reserves low memory based on the prospective size from swiotlb_adjusted_size() (or explicit swiotlb= sizes). However, early in boot, default_nareas is 0, so swiotlb_aligned_nslabs() merely aligns to segments and skips the area-aware power-of-two rounding:

kernel/dma/swiotlb.c:swiotlb_aligned_nslabs() {
	...
	if (!default_nareas)
		return nslabs;

	if (nslabs < IO_TLB_SEGSIZE * default_nareas)
		nslabs = IO_TLB_SEGSIZE * default_nareas;
	else if (!is_power_of_2(nslabs))
		nslabs = roundup_pow_of_two(nslabs);
	...
}
Quoted 9 lines

Later, swiotlb_init_remap() initializes default_nareas and calls swiotlb_adjust_nareas(), triggering round_up_default_nslabs() to round the pool up to the next power of two.

As a result, the allocated SWIOTLB pool can be up to ~50% larger than the reserved size (e.g., 600MB rounded to 1024MB). When the crash kernel attempts to allocate this rounded-up size, will it exceed its reserved memory and OOM?

I added a new patch that consolidates the slab rounding and addresses this issue.

Quoted 21 lines
Quoted 3 lines
kernel/dma/swiotlb.c +2 -0
+	/*
+	 * For SEV and TDX and CCA, all DMA has to occur via

[ ... ]

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 518

Quoted 6 lines
kernel/dma/swiotlb.c +6 -0
+	 * The percentage of guest memory used here for SWIOTLB buffers is
+	 * more of an approximation of the static adjustment which 64MB for
+	 * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+	 */
+	size = memblock_phys_mem_size() * 6 / 100;
+	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);

[Severity: High]
Does this calculation cause an undefined reference to __udivdi3 on 32-bit PAE builds, and overflow on 32-bit non-PAE builds?

The guest sizing logic has been relocated into kernel/dma/swiotlb.c, which is compiled for all architectures. On 32-bit kernels with PAE, phys_addr_t is 64-bit, so memblock_phys_mem_size() * 6 / 100 performs a 64-bit division by 100. GCC will emit a call to __udivdi3, which is missing from the kernel's libgcc, causing a fatal build link error.

Quoted 8 lines

On 32-bit kernels without PAE, phys_addr_t is 32-bit. If physical memory exceeds ~715MB (UINT_MAX / 6), the multiplication `memblock_phys_mem_size()
* 6` overflows the 32-bit integer, resulting in a silently truncated and incorrect pool size.

Should this be refactored to cast the operand to u64 and use div_u64() to be safe across all platforms?

Switched to div_u64() as shown below.

unsigned long __init swiotlb_adjusted_size(void)

{
	unsigned long nslabs;
	u64 size = swiotlb_size_or_default();

	if (!swiotlb_cmdline_size_set &&
	    cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
		/*
		 * For SEV and TDX and CCA, all DMA has to occur via
		 * shared/unencrypted pages. Kernel uses SWIOTLB to make this
		 * happen without changing device drivers. However, depending on
		 * the workload being run, the default 64MB of SWIOTLB may not be
		 * enough and SWIOTLB may run out of buffers for DMA, resulting in
		 * I/O errors and/or performance degradation especially with high
		 * I/O workloads.
		 *
		 * Adjust the default size of SWIOTLB using a percentage of guest
		 * memory for SWIOTLB buffers.
		 *
		 * The percentage of guest memory used here for SWIOTLB buffers is
		 * more of an approximation of the static adjustment which 64MB for
		 * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
		 */
		size = div_u64((u64)memblock_phys_mem_size() * 6, 100);
		size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
	}

	nslabs = swiotlb_calc_nslabs(size, default_nareas);

	return nslabs << IO_TLB_SHIFT;
}

-aneesh

#22 lore raw

Re: [PATCH v5 5/6] dma: swiotlb: Add an overridable architecture pool opt-out

In reply to [email protected]
Quoted 63 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dma: swiotlb: default opt-out incorrectly overrides iommu=soft
--

commit 8a081ec4054b9ec450c0c293130d0ceb6b8d20fa
Author: Aneesh Kumar K.V (Arm) <[email protected]>

dma: swiotlb: Add an overridable architecture pool opt-out

This commit introduces SWIOTLB_INIT_DEFAULT_OFF to allow an architecture to opt out of SWIOTLB pool initialization without losing placement constraints.

arch/x86/kernel/pci-dma.c static unsigned int x86_swiotlb_flags; · line 48

Quoted 12 lines
arch/x86/kernel/pci-dma.c +2 -2
index 0cff255827baf..ce257b20ea9cf 100644
@@ -44,9 +44,13 @@ static unsigned int x86_swiotlb_flags;
 static void __init pci_swiotlb_detect(void)
 {
 	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
-		x86_swiotlb_enable = true;
+	if (no_iommu)
+		x86_swiotlb_flags |= SWIOTLB_INIT_DEFAULT_OFF;

[Severity: High]
When this is combined with iommu=soft (which sets SWIOTLB_INIT_ADDRESSING_LIMIT), doesn't this flag overwrite the user's explicit fallback request?

[ ... ]

Quoted 13 lines
kernel/dma/swiotlb.c +1 -2
index c19675846fc1d..46b789eb08063 100644
@@ -563,10 +563,16 @@ swiotlb_select_pool_policy(unsigned int flags)
 	if (flags & SWIOTLB_INIT_REMAP)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
+	if (swiotlb_force_bounce)
 		return SWIOTLB_POOL_DEFAULT;
 
-	if (swiotlb_force_bounce)

kernel/dma/swiotlb.c swiotlb_select_pool_policy(unsigned int flags) · line 578

Quoted 10 lines
kernel/dma/swiotlb.c +7 -0
+	/*
+	 * Explicit requirements above override an architecture's default opt-out.
+	 */
+	if (flags & SWIOTLB_INIT_DEFAULT_OFF)
+		return SWIOTLB_POOL_NONE;
+
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
 		return SWIOTLB_POOL_DEFAULT;
 
 	if (swiotlb_kmalloc_needs_bounce())

[Severity: High]
By evaluating SWIOTLB_INIT_DEFAULT_OFF before SWIOTLB_INIT_ADDRESSING_LIMIT and swiotlb_kmalloc_needs_bounce(), does the default opt-out incorrectly override explicit requests for a software bounce pool and unaligned kmalloc bouncing?

Quoted 4 lines

For instance, if a user boots an x86 system with iommu=off iommu=soft, pci_swiotlb_detect() sets SWIOTLB_INIT_DEFAULT_OFF because of no_iommu=1. The iommu=soft parameter also sets SWIOTLB_INIT_ADDRESSING_LIMIT.

I don't think combining iommu=off with iommu=soft is valid.

Quoted 7 lines

Because the opt-out is checked first here in swiotlb_select_pool_policy(), it returns SWIOTLB_POOL_NONE and prevents the pool from being allocated. Could this lead to DMA mapping failures for devices that cannot address all system memory, contradicting the commit message which states that iommu=soft retains the addressing limit?

-aneesh

reviewer
#18 lore raw

Re: [PATCH v5 4/6] dma: swiotlb: Centralize memory-encryption pool sizing

In reply to Aneesh Kumar K.V (Arm)
Quoted 138 lines · Aneesh Kumar K.V (Arm)

Memory-encrypted guests use shared or unencrypted memory for DMA and may route all DMA through SWIOTLB. The default pool can therefore be too small for I/O-intensive workloads.

Move the existing x86 guest-sizing policy into the SWIOTLB core. For SWIOTLB_POOL_CC_GUEST, size the pool to 6% of guest memory, clamped between the normal default and 1 GiB. Preserve an explicit swiotlb= size.

Provide swiotlb_adjusted_size() so early users, including the x86 crash kernel reservation, can account for the prospective guest pool size before SWIOTLB initialization. Use the same area-aware alignment for both the prospective and allocated sizes.

Host memory encryption still selects a normal-sized shared pool and does not use the guest-sizing policy.

A restricted DMA pool already provides shared bounce buffers for its devices. Record its presence during reserved-memory initialization and do not select the confidential-guest default-pool policy solely because guest memory encryption is active.

---
  arch/x86/include/asm/crash_reserve.h |  6 ++--
  arch/x86/mm/mem_encrypt.c            | 24 --------------
  include/linux/swiotlb.h              |  6 ++++
  kernel/dma/swiotlb.c                 | 47 +++++++++++++++++++++++++++-
  4 files changed, 56 insertions(+), 27 deletions(-)
arch/x86/include/asm/crash_reserve.h +4 -2
index 7835b2cdff04..c1f5bf5d4182 100644
@@ -18,7 +18,9 @@
   * no good way to detect the paging mode of the target kernel which will be
   * loaded for dumping.
   */
-extern unsigned long swiotlb_size_or_default(void);
+#ifdef CONFIG_X86_64
+unsigned long __init swiotlb_adjusted_size(void);
+#endif
 
  #ifdef CONFIG_X86_32
  # define CRASH_ADDR_LOW_MAX     SZ_512M
@@ -33,7 +35,7 @@ extern unsigned long swiotlb_size_or_default(void);
  static inline unsigned long crash_low_size_default(void)
  {
  #ifdef CONFIG_X86_64
-	return max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
+	return max(swiotlb_adjusted_size() + (8UL << 20), 256UL << 20);
  #else
  	return 0;
  #endif
arch/x86/mm/mem_encrypt.c +0 -24
index 95bae74fdab2..912f22ca838f 100644
@@ -101,9 +101,6 @@ void __init mem_encrypt_init(void)
 
  void __init mem_encrypt_setup_arch(void)
  {
-	phys_addr_t total_mem = memblock_phys_mem_size();
-	unsigned long size;
-
  	/*
  	 * Do RMP table fixups after the e820 tables have been setup by
  	 * e820__memory_setup().
@@ -114,27 +111,6 @@ void __init mem_encrypt_setup_arch(void)
  	if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
  		return;
 
-	/*
-	 * For SEV and TDX, all DMA has to occur via shared/unencrypted pages.
-	 * Kernel uses SWIOTLB to make this happen without changing device
-	 * drivers. However, depending on the workload being run, the
-	 * default 64MB of SWIOTLB may not be enough and SWIOTLB may
-	 * run out of buffers for DMA, resulting in I/O errors and/or
-	 * performance degradation especially with high I/O workloads.
-	 *
-	 * Adjust the default size of SWIOTLB using a percentage of guest
-	 * memory for SWIOTLB buffers. Also, as the SWIOTLB bounce buffer
-	 * memory is allocated from low memory, ensure that the adjusted size
-	 * is within the limits of low available memory.
-	 *
-	 * The percentage of guest memory used here for SWIOTLB buffers
-	 * is more of an approximation of the static adjustment which
-	 * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
-	 */
-	size = total_mem * 6 / 100;
-	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
-	swiotlb_adjust_size(size);
-
  	/* Set restricted memory access for virtio. */
  	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
  }
include/linux/swiotlb.h +6 -0
index d69370c08094..be3962a33fc6 100644
@@ -193,6 +193,7 @@ void swiotlb_dev_init(struct device *dev);
  size_t swiotlb_max_mapping_size(struct device *dev);
  bool is_swiotlb_allocated(void);
  bool is_swiotlb_active(struct device *dev);
+unsigned long __init swiotlb_adjusted_size(void);
  void __init swiotlb_adjust_size(unsigned long size);
  phys_addr_t default_swiotlb_base(void);
  phys_addr_t default_swiotlb_limit(void);
@@ -232,6 +233,11 @@ static inline bool is_swiotlb_active(struct device *dev)
  	return false;
  }
 
+static inline unsigned long __init swiotlb_adjusted_size(void)
+{
+	return 0;
+}
+
  static inline void swiotlb_adjust_size(unsigned long size)
  {
  }
kernel/dma/swiotlb.c +2 -0
index f368a73f4ed0..c19675846fc1 100644
@@ -80,6 +80,7 @@ struct io_tlb_slot {
 
  static bool swiotlb_force_bounce;
  static bool swiotlb_force_disable;
+static bool restricted_dma_pool_present __initdata;
 
  enum swiotlb_pool_policy {
  	SWIOTLB_POOL_NONE,
@@ -480,6 +481,46 @@ static bool __init swiotlb_kmalloc_needs_bounce(void)
  	       (dma_get_cache_alignment() > 1);
  }
 
+/**

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 494

Quoted 10 lines
kernel/dma/swiotlb.c +10 -0
+ * swiotlb_adjusted_size() - get the prospective adjusted SWIOTLB size
+ *
+ * Return the size that confidential-computing guest sizing would select for
+ * the default pool, without changing the configured SWIOTLB size. An
+ * explicit swiotlb= size is always preserved. An explicit area count is
+ * included in the size calculation. Automatic area sizing is initialized
+ * later from the running kernel's possible CPU map and any resulting size
+ * adjustment is therefore not reflected in the returned size.
+ */
+unsigned long __init swiotlb_adjusted_size(void)

This is yet another misleadingly ambiguous name.

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 500

Quoted 6 lines
kernel/dma/swiotlb.c +6 -0
+{
+	unsigned long nslabs, size = swiotlb_size_or_default();
+
+	if (swiotlb_default_size_changed() ||
+	    !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+		return size;

And this makes for another needlessly convoluted calling convention.

Quoted 7 lines
kernel/dma/swiotlb.c +7 -0
+	/*
+	 * For SEV and TDX and CCA, all DMA has to occur via
+	 * shared/unencrypted pages. Kernel uses SWIOTLB to make this
+	 * happen without changing device drivers. However, depending on
+	 * the workload being run, the default 64MB of SWIOTLB may not be
+	 * enough and SWIOTLB may run out of buffers for DMA, resulting in
+	 * I/O errors and/or performance degradation especially with high

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 517

Quoted 10 lines
kernel/dma/swiotlb.c +10 -0
+	 * I/O workloads.
+	 *
+	 * Adjust the default size of SWIOTLB using a percentage of guest
+	 * memory for SWIOTLB buffers.
+	 *
+	 * The percentage of guest memory used here for SWIOTLB buffers is
+	 * more of an approximation of the static adjustment which 64MB for
+	 * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+	 */
+	size = memblock_phys_mem_size() * 6 / 100;

But mostly I fail to see how this makes any sense for the x86 crash_low_size_default() case anyway. This calculation is based on the *total* system memory, of which 6% is likely comparable to (or perhaps even more than) the *entire* amount of memory reserved for the crash kernel itself. What's more, if the low memory and size restrictions are lifted for regular CoCo SWIOTLB as people want, then it becomes even more utterly nonsensical to tie crashkernel_low to this.

Yes, this happens to be the behaviour that falls out of how two different parts of the existing code interact, but I highly doubt it was ever intentional, so I'm not convinced that complicating SWIOTLB interfaces to blindly preserve it is the right thing to do.

Thanks,
Robin.

Quoted 36 lines
kernel/dma/swiotlb.c +11 -1
+	size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
+	nslabs = swiotlb_aligned_nslabs(size);
+
+	return nslabs << IO_TLB_SHIFT;
+}
+
  static void __init
  swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)
  {
@@ -496,6 +537,7 @@ swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy)
  		break;
  	}
  	case SWIOTLB_POOL_CC_GUEST:
+		swiotlb_adjust_size(swiotlb_adjusted_size());
  		break;
  	case SWIOTLB_POOL_NONE:
  		WARN(true, "Cannot adjust SWIOTLB size without a pool\n");
@@ -511,7 +553,8 @@ swiotlb_select_pool_policy(unsigned int flags)
  	if (swiotlb_force_disable)
  		return SWIOTLB_POOL_NONE;
 
-	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+	if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT) &&
+	    !restricted_dma_pool_present)
  		return SWIOTLB_POOL_CC_GUEST;
 
  	if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
@@ -2157,6 +2200,8 @@ static int __init rmem_swiotlb_setup(unsigned long node,
  	    of_get_flat_dt_prop(node, "no-map", NULL))
  		return -EINVAL;
 
+	restricted_dma_pool_present = true;
+
  	pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n",
  		&rmem->base, (unsigned long)rmem->size / SZ_1M);
  	return 0;
reviewer
#23 lore raw

Re: [PATCH v5 5/6] dma: swiotlb: Add an overridable architecture pool opt-out

In reply to Aneesh Kumar K.V (Arm)
Quoted 6 lines · Aneesh Kumar K.V (Arm)

On x86, iommu=off historically prevented SWIOTLB initialization by not setting SWIOTLB_INIT_ADDRESSING_LIMIT. This overloads that flag: it controls both whether a pool is selected and where the pool is allocated. If another requirement, swiotlb=force selects a pool independently, omitting SWIOTLB_INIT_ADDRESSING_LIMIT may place it above the addressable range of devices which need bounce buffering.

Well then surely something is wrong with the design? If SWIOTLB_INIT_ADDRESSING_LIMIT is set then SWIOTLB still needs to respect it irrespective of any *additional* reasons for activating (either from arch code or internally). And conversely, if devices have addressing limits but SWIOTLB_INIT_ADDRESSING_LIMIT is *not* set, then the arch code has clearly gone wrong or made a bad assumption, but that doesn't seem like SWIOTLB's problem to worry about.

Quoted 7 lines

Add SWIOTLB_INIT_FORCE_DISABLE to let an architecture opt out of normal pool initialization without discarding the pool placement constraints.

Unlike swiotlb_force_disable, which is set by swiotlb=noforce and unconditionally prevents pool initialization, the new flag is an architecture default. Explicit requirements such as memory encryption, architecture remapping, or swiotlb=force take precedence over it.

Logically, that should already be implied by the architecture passing no SWIOTLB_INIT flags.

Thanks,
Robin.

Quoted 85 lines

iommu=soft retains SWIOTLB_INIT_ADDRESSING_LIMIT because it requests software bounce buffering for devices whose DMA masks cannot address all system memory. Such devices must be able to reach the bounce pool.

Clear both flags for Xen because Xen explicitly requires a remapped SWIOTLB pool and may allocate it anywhere in directly mapped memory.

With no override, iommu=off disables both hardware IOMMU translation and SWIOTLB bouncing. Direct DMA remains available; mappings outside a device's addressable range fail instead of being bounced.

---
  arch/x86/kernel/pci-dma.c | 11 ++++++++---
  include/linux/swiotlb.h   |  2 ++
  kernel/dma/swiotlb.c      | 10 ++++++++--
  3 files changed, 18 insertions(+), 5 deletions(-)
arch/x86/kernel/pci-dma.c +8 -3
index 0cff255827ba..ce257b20ea9c 100644
@@ -44,9 +44,13 @@ static unsigned int x86_swiotlb_flags;
  static void __init pci_swiotlb_detect(void)
  {
  	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
-	if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) {
-		x86_swiotlb_enable = true;
+	if (no_iommu)
+		x86_swiotlb_flags |= SWIOTLB_INIT_DEFAULT_OFF;
+
+	if (max_possible_pfn > MAX_DMA32_PFN) {
  		x86_swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
+		if (!no_iommu)
+			x86_swiotlb_enable = true;
  	}
 
  	/*
@@ -84,7 +88,8 @@ static void __init pci_xen_swiotlb_init(void)
  		return;
  	x86_swiotlb_enable = true;
  	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
-	x86_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
+	x86_swiotlb_flags &= ~(SWIOTLB_INIT_ADDRESSING_LIMIT |
+			       SWIOTLB_INIT_DEFAULT_OFF);
  	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;
  	swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup);
  	dma_ops = &xen_swiotlb_dma_ops;
include/linux/swiotlb.h +2 -0
index be3962a33fc6..466c33795db0 100644
@@ -20,6 +20,8 @@ struct scatterlist;
  #define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 2)
  /* Initialize a default-sized pool that requires architecture remapping. */
  #define SWIOTLB_INIT_REMAP		(1 << 3)
+/* Do not initialize a pool unless SWIOTLB is explicitly required. */
+#define SWIOTLB_INIT_DEFAULT_OFF		(1 << 4)
 
  /*
   * Maximum allowable number of contiguous slabs to map,
kernel/dma/swiotlb.c +8 -2
index c19675846fc1..46b789eb0806 100644
@@ -563,10 +563,16 @@ swiotlb_select_pool_policy(unsigned int flags)
  	if (flags & SWIOTLB_INIT_REMAP)
  		return SWIOTLB_POOL_DEFAULT;
 
-	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
+	if (swiotlb_force_bounce)
  		return SWIOTLB_POOL_DEFAULT;
 
-	if (swiotlb_force_bounce)
+	/*
+	 * Explicit requirements above override an architecture's default opt-out.
+	 */
+	if (flags & SWIOTLB_INIT_DEFAULT_OFF)
+		return SWIOTLB_POOL_NONE;
+
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
  		return SWIOTLB_POOL_DEFAULT;
 
  	if (swiotlb_kmalloc_needs_bounce())
reviewer Reviewed-by on v5 6/6
#27 lore raw

Re: [PATCH v5 6/6] dma: swiotlb: Remove SWIOTLB_ANY

In reply to Aneesh Kumar K.V (Arm)
Quoted 8 lines · Aneesh Kumar K.V (Arm)

swiotlb pools are currently allocated below the architecture's low address limit by default. SWIOTLB_ANY overrides this and permits allocation anywhere in directly mapped memory.

SWIOTLB_INIT_ADDRESSING_LIMIT now explicitly identifies callers which need a pool that is reachable by devices with limited DMA addressing. Use it as the placement constraint and remove SWIOTLB_ANY.

Quoted 6 lines

This changes the default behavior for callers which do not set either flag. Previously, omitting SWIOTLB_ANY placed the default pool below the architecture's low address limit and also constrained dynamically allocated pools to the same range. After this change, omitting SWIOTLB_INIT_ADDRESSING_LIMIT permits both static and dynamic pools to use any directly mapped memory.

While it's rather dependent on some of the other aspects wanting a bit more work still, this part I do entirely agree with :)

Quoted 165 lines
---
  Documentation/core-api/swiotlb.rst |  8 ++++----
  arch/powerpc/mm/mem.c              |  6 ++++--
  arch/riscv/mm/init.c               | 16 ----------------
  arch/s390/mm/init.c                |  2 +-
  arch/x86/kernel/pci-dma.c          |  2 +-
  include/linux/swiotlb.h            |  7 +++----
  kernel/dma/swiotlb.c               | 17 ++++++-----------
  7 files changed, 19 insertions(+), 39 deletions(-)
Documentation/core-api/swiotlb.rst +4 -4
index 71b4e4c27eb5..06d9f88fdf7b 100644
@@ -148,10 +148,10 @@ may also be adjusted
  due to other conditions, such as running in a CoCo VM, as described above. If
  CONFIG_SWIOTLB_DYNAMIC is enabled, additional pools may be allocated later in
  the life of the system. Each pool must be a contiguous range of physical
-memory. The default pool is allocated below the 4 GiB physical address line so
-it works for devices that can only address 32-bits of physical memory (unless
-architecture-specific code provides the SWIOTLB_ANY flag). In a CoCo VM, the
-pool memory must be decrypted before swiotlb is used.
+memory. The default pool is allocated below the architecture's low address
+limit when it is needed for devices with limited DMA addressing. Otherwise,
+it may be allocated anywhere in directly mapped memory. In a CoCo VM, the pool
+memory must be decrypted before swiotlb is used.
 
  Each pool is divided into "slots" of size IO_TLB_SIZE, which is 2 KiB with
  current definitions. IO_TLB_SEGSIZE contiguous slots (128 slots) constitute
arch/powerpc/mm/mem.c +4 -2
index 006ed41097e9..1bdea6c03848 100644
@@ -295,9 +295,11 @@ void __init arch_mm_preinit(void)
  		/*
  		 * Since the guest memory is inaccessible to the host,
  		 * devices always need to use the SWIOTLB buffer for DMA
-		 * even if dma_capable() says otherwise.
+		 * even if dma_capable() says otherwise. The hypervisor has
+		 * no addressing limitation, so the buffer may be allocated
+		 * anywhere.
  		 */
-		ppc_swiotlb_flags |= SWIOTLB_ANY;
+		ppc_swiotlb_flags &= ~SWIOTLB_INIT_ADDRESSING_LIMIT;
  	}
 
  	/*
arch/riscv/mm/init.c +0 -16
index 96856114b387..734712230f35 100644
@@ -165,27 +165,11 @@ static void print_vm_layout(void) { }
 
  void __init arch_mm_preinit(void)
  {
-	bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit) &&
-		       memblock_start_of_DRAM() < dma32_phys_limit;
  	unsigned int swiotlb_flags = SWIOTLB_VERBOSE;
  #ifdef CONFIG_FLATMEM
  	BUG_ON(!mem_map);
  #endif /* CONFIG_FLATMEM */
 
-	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
-	    dma_cache_alignment != 1) {
-		/*
-		 * No 32-bit DMA bouncing needed (either all DRAM is within
-		 * the 32-bit limit, or it all starts above it), but
-		 * kmalloc() buffers whose sizes are not cache-line-aligned
-		 * still require bouncing for non-coherent DMA.  Use
-		 * SWIOTLB_ANY so that the buffer can be allocated from high
-		 * memory when DRAM starts above dma32_phys_limit.  Allocate
-		 * ~1 MB per 1 GB of RAM.
-		 */
-		swiotlb_flags |= SWIOTLB_ANY;
-	}
-
  	if ((max_pfn > PFN_DOWN(dma32_phys_limit)) &&
  	     (memblock_start_of_DRAM() < dma32_phys_limit))
  		swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT;
arch/s390/mm/init.c +1 -1
index 78f507e24ceb..ce10292447f1 100644
@@ -166,7 +166,7 @@ static void __init pv_init(void)
  	virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
 
  	/* make sure bounce buffers are shared */
-	swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_ANY);
+	swiotlb_init(SWIOTLB_VERBOSE);
  	swiotlb_update_mem_attributes();
  }
 
arch/x86/kernel/pci-dma.c +1 -1
index ce257b20ea9c..74e92b457758 100644
@@ -90,7 +90,7 @@ static void __init pci_xen_swiotlb_init(void)
  	/* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */
  	x86_swiotlb_flags &= ~(SWIOTLB_INIT_ADDRESSING_LIMIT |
  			       SWIOTLB_INIT_DEFAULT_OFF);
-	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP | SWIOTLB_ANY;
+	x86_swiotlb_flags |= SWIOTLB_INIT_REMAP;
  	swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup);
  	dma_ops = &xen_swiotlb_dma_ops;
  	if (IS_ENABLED(CONFIG_PCI))
include/linux/swiotlb.h +3 -4
index 466c33795db0..503e1446e0d8 100644
@@ -15,13 +15,12 @@ struct page;
  struct scatterlist;
 
  #define SWIOTLB_VERBOSE	(1 << 0) /* verbose initialization */
-#define SWIOTLB_ANY	(1 << 1) /* allow any memory for the buffer */
  /* Initialize a default-sized pool for devices with limited DMA addressing. */
-#define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 2)
+#define SWIOTLB_INIT_ADDRESSING_LIMIT	(1 << 1)
  /* Initialize a default-sized pool that requires architecture remapping. */
-#define SWIOTLB_INIT_REMAP		(1 << 3)
+#define SWIOTLB_INIT_REMAP		(1 << 2)
  /* Do not initialize a pool unless SWIOTLB is explicitly required. */
-#define SWIOTLB_INIT_DEFAULT_OFF		(1 << 4)
+#define SWIOTLB_INIT_DEFAULT_OFF	(1 << 3)
 
  /*
   * Maximum allowable number of contiguous slabs to map,
kernel/dma/swiotlb.c +6 -11
index 46b789eb0806..77d7c320de28 100644
@@ -450,15 +450,10 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs,
  	size_t bytes = PAGE_ALIGN(nslabs << IO_TLB_SHIFT);
  	void *tlb;
 
-	/*
-	 * By default allocate the bounce buffer memory from low memory, but
-	 * allow to pick a location everywhere for hypervisors with guest
-	 * memory encryption.
-	 */
-	if (flags & SWIOTLB_ANY)
-		tlb = memblock_alloc(bytes, PAGE_SIZE);
-	else
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
  		tlb = memblock_alloc_low(bytes, PAGE_SIZE);
+	else
+		tlb = memblock_alloc(bytes, PAGE_SIZE);
 
  	if (!tlb) {
  		pr_warn("%s: Failed to allocate %zu bytes tlb structure\n",
@@ -604,10 +599,10 @@ void __init swiotlb_init_remap(unsigned int flags,
  #ifdef CONFIG_SWIOTLB_DYNAMIC
  	if (!remap)
  		io_tlb_default_mem.can_grow = true;
-	if (flags & SWIOTLB_ANY)
-		io_tlb_default_mem.phys_limit = virt_to_phys(high_memory - 1);
-	else
+	if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT)
  		io_tlb_default_mem.phys_limit = ARCH_LOW_ADDRESS_LIMIT;
+	else
+		io_tlb_default_mem.phys_limit = virt_to_phys(high_memory - 1);
  #endif
 
  	/* if we have host or guest memory encryption */
#24 lore raw

Re: [PATCH v5 5/6] dma: swiotlb: Add an overridable architecture pool opt-out

In reply to Robin Murphy
Quoted 8 lines · Robin Murphy
Quoted 6 lines · Aneesh Kumar K.V (Arm)

On x86, iommu=off historically prevented SWIOTLB initialization by not setting SWIOTLB_INIT_ADDRESSING_LIMIT. This overloads that flag: it controls both whether a pool is selected and where the pool is allocated. If another requirement, swiotlb=force selects a pool independently, omitting SWIOTLB_INIT_ADDRESSING_LIMIT may place it above the addressable range of devices which need bounce buffering.

Quoted 8 lines

Well then surely something is wrong with the design? If SWIOTLB_INIT_ADDRESSING_LIMIT is set then SWIOTLB still needs to respect it irrespective of any *additional* reasons for activating (either from arch code or internally). And conversely, if devices have addressing limits but SWIOTLB_INIT_ADDRESSING_LIMIT is *not* set, then the arch code has clearly gone wrong or made a bad assumption, but that doesn't seem like SWIOTLB's problem to worry about.

That is what this patch does. It sets SWIOTLB_INIT_ADDRESSING_LIMIT based on max_possible_pfn, while SWIOTLB_INIT_FORCE_DISABLE allows the architecture to opt out of SWIOTLB initialization independently.

Quoted 4 lines
Quoted 4 lines

Add SWIOTLB_INIT_FORCE_DISABLE to let an architecture opt out of normal pool initialization without discarding the pool placement constraints.

Quoted 8 lines
Quoted 4 lines

Unlike swiotlb_force_disable, which is set by swiotlb=noforce and unconditionally prevents pool initialization, the new flag is an architecture default. Explicit requirements such as memory encryption, architecture remapping, or swiotlb=force take precedence over it.

Logically, that should already be implied by the architecture passing no SWIOTLB_INIT flags.

We now have a case where max_possible_pfn causes SWIOTLB_INIT_ADDRESSING_LIMIT to be set, while the kernel command line specifies iommu=off. We want to disable SWIOTLB in this case even though the SWIOTLB_INIT flags are nonzero.

-aneesh

#19 lore raw

Re: [PATCH v5 4/6] dma: swiotlb: Centralize memory-encryption pool sizing

In reply to Robin Murphy
Quoted 5 lines · Robin Murphy
Quoted 4 lines · Aneesh Kumar K.V (Arm)

Memory-encrypted guests use shared or unencrypted memory for DMA and may route all DMA through SWIOTLB. The default pool can therefore be too small for I/O-intensive workloads.

[ ... 133 lines skipped ... ]

Quoted 5 lines
Quoted 5 lines

+/**
+ * swiotlb_adjusted_size() - get the prospective adjusted SWIOTLB size
+ *
+ * Return the size that confidential-computing guest sizing would select for
+ * the default pool, without changing the configured SWIOTLB size. An

Quoted 9 lines
Quoted 6 lines

+ * explicit swiotlb= size is always preserved. An explicit area count is
+ * included in the size calculation. Automatic area sizing is initialized
+ * later from the running kernel's possible CPU map and any resulting size
+ * adjustment is therefore not reflected in the returned size.
+ */
+unsigned long __init swiotlb_adjusted_size(void)

This is yet another misleadingly ambiguous name.

Do you have any suggestions for a better approach? This returns the swiotlb size adjusted according to the existing heuristics.

Quoted 9 lines

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 500

Quoted 6 lines
kernel/dma/swiotlb.c +6 -0
+{
+	unsigned long nslabs, size = swiotlb_size_or_default();
+
+	if (swiotlb_default_size_changed() ||
+	    !cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
+		return size;

And this makes for another needlessly convoluted calling convention.

I have updated this to

+static unsigned long __init swiotlb_adjusted_size(void) +{
+ unsigned long nslabs;
+ u64 size = swiotlb_default_pool_size();
+
+ if (!swiotlb_cmdline_size_set &&
+ cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
+ /*
+ * For SEV and TDX and CCA, all DMA has to occur via
+ * shared/unencrypted pages. Kernel uses SWIOTLB to make this
+ * happen without changing device drivers. However, depending on
+ * the workload being run, the default 64MB of SWIOTLB may not be
+ * enough and SWIOTLB may run out of buffers for DMA, resulting in
+ * I/O errors and/or performance degradation especially with high
+ * I/O workloads.
+ *
+ * Adjust the default size of SWIOTLB using a percentage of guest
+ * memory for SWIOTLB buffers.
+ *
+ * The percentage of guest memory used here for SWIOTLB buffers is
+ * more of an approximation of the static adjustment which 64MB for
+ * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+ */
+ size = div_u64((u64)memblock_phys_mem_size() * 6, 100);
+ size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
+ }
+
+ nslabs = swiotlb_calc_nslabs(size, default_nareas);
+
+ return nslabs << IO_TLB_SHIFT;
+}
+

But as shown above, we apply the CoCo sizing heuristic only when no explicit swiotlb= size was specified and guest memory encryption is enabled.

Quoted 27 lines
Quoted 8 lines
kernel/dma/swiotlb.c +7 -0
+	/*
+	 * For SEV and TDX and CCA, all DMA has to occur via
+	 * shared/unencrypted pages. Kernel uses SWIOTLB to make this
+	 * happen without changing device drivers. However, depending on
+	 * the workload being run, the default 64MB of SWIOTLB may not be
+	 * enough and SWIOTLB may run out of buffers for DMA, resulting in
+	 * I/O errors and/or performance degradation especially with high

kernel/dma/swiotlb.c static bool __init swiotlb_kmalloc_needs_bounce(void) · line 517

Quoted 10 lines
kernel/dma/swiotlb.c +10 -0
+	 * I/O workloads.
+	 *
+	 * Adjust the default size of SWIOTLB using a percentage of guest
+	 * memory for SWIOTLB buffers.
+	 *
+	 * The percentage of guest memory used here for SWIOTLB buffers is
+	 * more of an approximation of the static adjustment which 64MB for
+	 * <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
+	 */
+	size = memblock_phys_mem_size() * 6 / 100;

But mostly I fail to see how this makes any sense for the x86 crash_low_size_default() case anyway. This calculation is based on the *total* system memory, of which 6% is likely comparable to (or perhaps even more than) the *entire* amount of memory reserved for the crash kernel itself. What's more, if the low memory and size restrictions are lifted for regular CoCo SWIOTLB as people want, then it becomes even more utterly nonsensical to tie crashkernel_low to this.

Quoted 5 lines

Yes, this happens to be the behaviour that falls out of how two different parts of the existing code interact, but I highly doubt it was ever intentional, so I'm not convinced that complicating SWIOTLB interfaces to blindly preserve it is the right thing to do.

I agree with the concern, but what would be the right solution? When the crash kernel boots, it will try to allocate the SWIOTLB pool according to this sizing heuristic, and the allocation may fail if insufficient memory was reserved.

I have split the x86 crash-kernel changes into a separate patch, which we can drop depending on the conclusion here. I must admit that I do not understand the crash-kernel memory restrictions and allocation details well enough to propose a solution.

-aneesh