From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18475349CFF; Thu, 24 Sep 2026 06:10:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230203; cv=none; b=Nfgei8VQUCQnCZKhH9lUBeMG9VqrKf+d23WGBEwOdDNjXAhcT99VG4AHBLCSjWSpcYCTPuf+vcDUd83eFvpqL+JT/lRpx7zkfmgOuoVqv3YHSTDAtFI8nu6pfiaMYe7Kf0TCsuCH0Qcgolrn1X+C1P/6g3MGmNe0nDWinybDUew= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230203; c=relaxed/simple; bh=oPkJ7I4m81bREd/9YR0WWkqRm9bqwo35PiQcvWTzNFU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I7Nt29hTaAucEYW0K71uGHbKolMqdcy9cxycZ1haT2kxtjbWXD3yARbUE5ms6oFYnnE48cWdFqVHCu2u2rowO/facJL0Eq5XppNEM1KJ6RNu2NXzbbegQnXlQSPCJa8YPaXDeD05342WKwRKSqbmpNV7i8hOyuSWu/qmsOAzo5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JH592GnK; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JH592GnK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C1171F00893; Thu, 24 Sep 2026 06:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230201; bh=Fa0KQ3fgRkDVGiDssz/j9qXG8YHMD9kKAj2GPSYE3Uo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=JH592GnKcE2iGAD07ASXMeBURt6QXUxEDyZ1fIFQ0oGthXjgqzKxtqRghMZfV1SDX KjJCxF8wcsi0g3KU9Ll7drA0AED0KkzETF78nCFEaWQ7DCGbNiDY9pAKt3CISBLgxN ByVHf4gXBDkom9D/wNER72wL+d5o/rzfaA8lNRA6OZohytw6ckxnTgM58h5UUmx0oI QZxwJQoZCfqoiQUriDiBnPqaMeSVQgkY4vpC+PYkcwBmnmgSD/0GybQM/AP391pGdc dpQTnoZxeQ5tW8Wygzal5/6KEoXFAcjH/muCPqK8a6yKzU5SdpruNHq7BWA6AsmqPm rBHuXRBApBJFQ== From: "Aneesh Kumar K.V (Arm)" To: iommu@lists.linux.dev, linux-kernel@vger.kernel.org Cc: "Aneesh Kumar K.V (Arm)" , Robin Murphy , Marek Szyprowski , Jonathan Corbet , Shuah Khan , Randy Dunlap , Will Deacon , Mark Rutland , Marc Zyngier , Steven Price , Suzuki K Poulose , Catalin Marinas , Jiri Pirko , Jason Gunthorpe , Mostafa Saleh , Petr Tesarik , Alexey Kardashevskiy , Dan Williams , Xu Yilun , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , "Christophe Leroy (CS GROUP)" , "Ritesh Harjani (IBM)" , Shrikanth Hegde , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , Stefano Stabellini , Russell King , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , Jiaxun Yang , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , linux-arm-kernel@lists.infradead.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, x86@kernel.org Subject: [PATCH v6 6/8] dma: swiotlb: Centralize memory-encryption pool sizing Date: Thu, 24 Sep 2026 11:37:54 +0530 Message-ID: <20260924060756.1325156-7-aneesh.kumar@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260924060756.1325156-1-aneesh.kumar@kernel.org> References: <20260924060756.1325156-1-aneesh.kumar@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. 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. Reviewed-by: Catalin Marinas Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- arch/x86/mm/mem_encrypt.c | 24 ------------------------ kernel/dma/swiotlb.c | 27 +++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 95bae74fdab2..912f22ca838f 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -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); } diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 75ee63cd354e..e626cca63ad1 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,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, @@ -478,7 +480,25 @@ swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy) size = min(swiotlb_default_pool_size(), size); break; case SWIOTLB_POOL_CC_GUEST: - return; + /* + * 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); + break; case SWIOTLB_POOL_NONE: WARN(true, "Cannot adjust SWIOTLB size without a pool\n"); return; @@ -496,7 +516,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)) @@ -2142,6 +2163,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; -- 2.43.0