From nobody Thu Sep 24 12:50:46 2026 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 391043F8EB9; Thu, 24 Sep 2026 06:08:36 +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=1790230117; cv=none; b=aKbuX4ARN4iq/TuCqamUJAITBDtBoBRQ39ZkjtP+04tkf9677gHD0OMSJ0YrntOvK3KkXDLd4beiq4ng022GooLLp7UOlLnWSEsWc1e2NAxShktL8L+/VFQee8pHjAASABYOiP/ltMWSmNLQ08pf6heJAbNd2fCvv9IL52t+Pcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230117; c=relaxed/simple; bh=qK0G9eKtoWA18oa8e+GB5V8ZCeoeRcPLvKHunDBhFB0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i4HILTQHVkP0Rl6Ga+BjAVRD9B5rqwUxeLfmV+DSgOjO7YwWBPMidp9WjMSBUKG5TL6gWrKOkTsp+ijzITl9E55q7IzuqZK2h84AJavZ/5yD4TYcoiOAHYD6zLY9gg8LPT7Nv9Omt8fkdkNAuWDI9Z/oDtN6l67ndkFZJO2A7xM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Dn1JBlRs; 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="Dn1JBlRs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE2F01F00893; Thu, 24 Sep 2026 06:08:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230115; bh=90/UBHgIVZHnWcX2iXkQQVtFO2UpIQ73195GKlJNVa4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Dn1JBlRsc1tzw1/Hp0ITWR1GzpUUhunYQCS0MQv1129xb7p54LHARo1KWvgJbKpYA WxRRH3LRfneK8Jz+4Zv9Nb+SqgCe1KZxIRAUYJeRoXALzqqfIJjYWLZY+Blfsk7etY 7cbkpIUhQT3NqW9yf+YPzFUS7wMLzRbUEOyRPMgBwXSEKJWbGdGk2DigF3ABrcabAJ IedCTNlJjkIg/pOURGkjX18mbmywzCKsTIosd2GinD2FpeTrKk+yD06Xa48WBpagr3 JXK0moBAIFefd37ROD/cGCVIBgOoINaxjXS2y3lSw4P+i6b2O/+LY7qU11vY+s3+Bz DRDSkaKIG1Blw== 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 1/8] dma: swiotlb: Rename swiotlb_size_or_default() Date: Thu, 24 Sep 2026 11:37:49 +0530 Message-ID: <20260924060756.1325156-2-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: quoted-printable Content-Type: text/plain; charset="utf-8" Rename swiotlb_size_or_default() to swiotlb_default_pool_size() to make clear that it returns the current size of the default SWIOTLB pool. This includes changes made by command-line options or sizing heuristics, not just the compiled-in default. No functional change. Cc: Stefano Stabellini Cc: Russell King Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Rutland Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Alexandre Ghiti Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- arch/arm/xen/mm.c | 2 +- arch/arm64/mm/init.c | 2 +- arch/riscv/mm/init.c | 2 +- arch/x86/include/asm/crash_reserve.h | 4 ++-- include/linux/swiotlb.h | 2 +- kernel/dma/swiotlb.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/arm/xen/mm.c b/arch/arm/xen/mm.c index 882cd70c7a2f..a6cf55c6fd55 100644 --- a/arch/arm/xen/mm.c +++ b/arch/arm/xen/mm.c @@ -125,7 +125,7 @@ static int __init xen_mm_init(void) return 0; =20 /* we can work with the default swiotlb */ - rc =3D swiotlb_init_late(swiotlb_size_or_default(), + rc =3D swiotlb_init_late(swiotlb_default_pool_size(), xen_swiotlb_gfp(), NULL); if (rc < 0) return rc; diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index fbf215ecc7d0..0842098a279b 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -348,7 +348,7 @@ void __init arch_mm_preinit(void) unsigned long size =3D DIV_ROUND_UP(memblock_phys_mem_size(), 1024); =20 - swiotlb_adjust_size(min(swiotlb_size_or_default(), size)); + swiotlb_adjust_size(min(swiotlb_default_pool_size(), size)); } =20 swiotlb_init(true, flags); diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index fb37b0b67efe..5fa392021717 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -185,7 +185,7 @@ void __init arch_mm_preinit(void) */ unsigned long size =3D DIV_ROUND_UP(memblock_phys_mem_size(), 1024); - swiotlb_adjust_size(min(swiotlb_size_or_default(), size)); + swiotlb_adjust_size(min(swiotlb_default_pool_size(), size)); swiotlb =3D true; swiotlb_flags |=3D SWIOTLB_ANY; } diff --git a/arch/x86/include/asm/crash_reserve.h b/arch/x86/include/asm/cr= ash_reserve.h index 7835b2cdff04..047f638a766d 100644 --- a/arch/x86/include/asm/crash_reserve.h +++ b/arch/x86/include/asm/crash_reserve.h @@ -18,7 +18,7 @@ * 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); +extern unsigned long swiotlb_default_pool_size(void); =20 #ifdef CONFIG_X86_32 # define CRASH_ADDR_LOW_MAX SZ_512M @@ -33,7 +33,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_default_pool_size() + (8UL << 20), 256UL << 20); #else return 0; #endif diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 2a421241b980..5f24edf47b68 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -38,7 +38,7 @@ struct scatterlist; #define IO_TLB_DEFAULT_SIZE (64UL << 20) #endif =20 -unsigned long swiotlb_size_or_default(void); +unsigned long swiotlb_default_pool_size(void); void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, int (*remap)(void *tlb, unsigned long nslabs)); int swiotlb_init_late(size_t size, gfp_t gfp_mask, diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index ded7016a46a7..42c51fb57e12 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -285,7 +285,7 @@ setup_io_tlb_npages(char *str) } early_param("swiotlb", setup_io_tlb_npages); =20 -unsigned long swiotlb_size_or_default(void) +unsigned long swiotlb_default_pool_size(void) { return default_nslabs << IO_TLB_SHIFT; } --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 01CBA3D25D2; Thu, 24 Sep 2026 06:08:52 +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=1790230134; cv=none; b=Ysu/Kt0Du05EMah97JHkiCp/2d2c+m6cOycdzCVVFlvGkuVak4ThMdTph+7Ootd6CGwzAvBga3mKa7mwRiIeerF+/4m9EALV1eIBSjyuVN8Kw64lGxjSoAyCJqx7mpYPTTPXc2qMeYFdHm4cmTRF8L0QNSdQpJLhTp6m+g27iZ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230134; c=relaxed/simple; bh=09mPp9QX3cV5CNigZJnjhYofuzJkVGyc6AFcGbQxlhw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c0y+G83DZs7O+3HE6umOmk2sh9xzKaZhKqsOTuC5cjEC60u0DlN/sT63QTqD0zINq1HOyh9S89B9NV4wdP72wp/h/NduA/Mylr95UMV2I4Md9KMTwwbE6RvGF8G3Rbj+p+shnp1A9uwRKWHl1dXXXpIet9v8LOVXgLjDtnwW78A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dNlQSxkP; 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="dNlQSxkP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14A3B1F00898; Thu, 24 Sep 2026 06:08:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230132; bh=bZdtkw5xik3/AxZvbApIZ+mvQYUki7df1BjpPFZ+vJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=dNlQSxkP4yr5E+y/jSc5A9piYh3waKMTK0o2RQ+KX4Px78mVUyWMVx9iaSjIaJGK5 gJ4g5j1TZLpkHcvb7fbxl2m79KMOHT/PLr8y8EE+3Xo74V9mRwOllpaIDtPQzMilZu hW/LtqZgULKhwFgn7FREEl00TZL1WXDzIg794JfVn8f8BXQ6TWKb89GOY6eGIOuQfB BjgoA8DlLQJaarw5tulyvD0To6GNn2KQSrFmUqCKcJq96LCUq7AOzKBJxwqJ4GQbx3 lg+P62RmSv1/u1y+wVlTSzM3VhJYReFL4IRgUYIyL3IZWk9iEoNixE5/tp+C4Ns+2B SxOHv5AMKOVww== 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 2/8] dma: swiotlb: Consolidate slab rounding Date: Thu, 24 Sep 2026 11:37:50 +0530 Message-ID: <20260924060756.1325156-3-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: quoted-printable Content-Type: text/plain; charset="utf-8" Pass the area count explicitly to swiotlb_calc_nslabs() and keep the calculation separate from updates to the default_nslabs . Move the default_nslabs update into swiotlb_adjust_nareas(). Round slabs to a power of two even before the area count is initialized. For early swiotlb_adjust_size() calls, this moves rounding from area initialization to size adjustment, so early queries see the rounded size. Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- kernel/dma/swiotlb.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 42c51fb57e12..b59dd2a5984e 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -117,27 +117,20 @@ struct io_tlb_area { spinlock_t lock; }; =20 -/* - * Round up number of slabs to the next power of 2. The last area is going - * be smaller than the rest if default_nslabs is not power of two. - * The number of slot in an area should be a multiple of IO_TLB_SEGSIZE, - * otherwise a segment may span two or more areas. It conflicts with free - * contiguous slots tracking: free slots are treated contiguous no matter - * whether they cross an area boundary. - * - * Return true if default_nslabs is rounded up. - */ -static bool round_up_default_nslabs(void) +/* Return a power-of-two number of slabs that can be split between areas. = */ +static unsigned long swiotlb_calc_nslabs(unsigned long size, + unsigned long nareas) { - if (!default_nareas) - return false; + unsigned long nslabs; =20 - if (default_nslabs < IO_TLB_SEGSIZE * default_nareas) - default_nslabs =3D IO_TLB_SEGSIZE * default_nareas; - else if (is_power_of_2(default_nslabs)) - return false; - default_nslabs =3D roundup_pow_of_two(default_nslabs); - return true; + nslabs =3D ALIGN(DIV_ROUND_UP(size, IO_TLB_SIZE), IO_TLB_SEGSIZE); + if (nareas && nslabs < IO_TLB_SEGSIZE * nareas) + nslabs =3D IO_TLB_SEGSIZE * nareas; + + if (!is_power_of_2(nslabs)) + nslabs =3D roundup_pow_of_two(nslabs); + + return nslabs; } =20 /** @@ -150,6 +143,8 @@ static bool round_up_default_nslabs(void) */ static void swiotlb_adjust_nareas(unsigned int nareas) { + unsigned long nslabs; + if (!nareas) nareas =3D 1; else if (!is_power_of_2(nareas)) @@ -158,9 +153,12 @@ static void swiotlb_adjust_nareas(unsigned int nareas) default_nareas =3D nareas; =20 pr_info("area num %d.\n", nareas); - if (round_up_default_nslabs()) + nslabs =3D swiotlb_calc_nslabs(default_nslabs << IO_TLB_SHIFT, nareas); + if (nslabs !=3D default_nslabs) { + default_nslabs =3D nslabs; pr_info("SWIOTLB bounce buffer size roundup to %luMB", (default_nslabs << IO_TLB_SHIFT) >> 20); + } } =20 /** @@ -300,10 +298,8 @@ void __init swiotlb_adjust_size(unsigned long size) if (default_nslabs !=3D IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT) return; =20 - size =3D ALIGN(size, IO_TLB_SIZE); - default_nslabs =3D ALIGN(size >> IO_TLB_SHIFT, IO_TLB_SEGSIZE); - if (round_up_default_nslabs()) - size =3D default_nslabs << IO_TLB_SHIFT; + default_nslabs =3D swiotlb_calc_nslabs(size, default_nareas); + size =3D default_nslabs << IO_TLB_SHIFT; pr_info("SWIOTLB bounce buffer size adjusted to %luMB", size >> 20); } =20 --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 3533A3F4DD2; Thu, 24 Sep 2026 06:09:09 +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=1790230151; cv=none; b=Eow9qAuEnKiQ0eiiIgqZyMKPn/PKGUlNnsWWjyIebtSfOq9Hv7lvzKT/3P098YZhVJLlUERXyO8bBNovobKcqunuelUtvZhN/fJ2s/UJFaL/ivYOVVnZ78CQSxti0zBs86AfCU2ykq3pl/5okwdZqzkzj85O4HWQvZ30c5Rf99M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230151; c=relaxed/simple; bh=4CJEyVcs1k79aNGKjlS207vQIHEMmJ4tXfjSXmeKvKc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=khm5VYKpKCejmtyoFy+6yAcaSk8qTo2F20jo4bHSNZ6ohwpYFEJEMNtEDyKTvWL6eM2dYa8QCpysD7A0Ql2dFFHEnl60RBVq9Bnu2Nva3hr1hAl1k1PU38fTBR3fhyQcKmUvMUiNwxnWQg57LXuXv1XWprINUYPley1Qqy4ay4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MPNqW2n3; 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="MPNqW2n3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F8231F000FF; Thu, 24 Sep 2026 06:08:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230149; bh=scK31GWR2h/dTmEm9pgXi3rZWUkl7hz/7+eEu9rpbOk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MPNqW2n3xQPMHb3zk4FZ6ncKB0hS41jziOySyXRP85orS2goMtQH2mm/0L9yaZxJ7 uyfxEYDfSKu8jV+fdC4+eGcFWr353MFkOwEd70Y7yLG+AbSEyBgtn2ckcyeoRzGBD+ I2tflAOZ4Q1fn7RbuaEgmuUo+/QGXFno6TJ+hS3dXjCIRl3cw4GGfgWF+2qH69vs2P MwZo6Xz45RCXfRm9huH6Y7sEvvHmd8YWAtVSH/kZU0DX+sGsu33lFAv7L2p9isA4MW 5TroSKQNx8yklWoYeiNZ44FrEEEF8XAaphtrhBdD4Jpm+EP38YK8c58oQMes/CsBtO qlrJqSzruzYKg== 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 3/8] dma: swiotlb: Track whether the pool size was explicitly set Date: Thu, 24 Sep 2026 11:37:51 +0530 Message-ID: <20260924060756.1325156-4-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: quoted-printable Content-Type: text/plain; charset="utf-8" swiotlb_adjust_size() must not override a pool size supplied through the swiotlb=3D 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=3D supplied a numeric size in swiotlb_cmdline_size_s= et and check that flag directly before adjusting the pool size. Reviewed-by: Catalin Marinas Reviewed-by: Robin Murphy Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- kernel/dma/swiotlb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index b59dd2a5984e..61efa3244daa 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -100,6 +100,7 @@ static struct io_tlb_mem io_tlb_default_mem; =20 static unsigned long default_nslabs =3D IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIF= T; static unsigned long default_nareas; +static bool swiotlb_cmdline_size_set __initdata; =20 /** * struct io_tlb_area - IO TLB memory area descriptor @@ -253,6 +254,7 @@ setup_io_tlb_npages(char *str) /* avoid tail segment of size < IO_TLB_SEGSIZE */ default_nslabs =3D ALIGN(simple_strtoul(str, &str, 0), IO_TLB_SEGSIZE); + swiotlb_cmdline_size_set =3D true; } if (*str =3D=3D ',') ++str; @@ -295,7 +297,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 !=3D IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT) + if (swiotlb_cmdline_size_set) return; =20 default_nslabs =3D swiotlb_calc_nslabs(size, default_nareas); --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 58468349CFF; Thu, 24 Sep 2026 06:09:27 +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=1790230169; cv=none; b=EPgIYHZxzZiCxV+JccEiuKoNUOvxU82r29E6GIbpJtQ4n9wllPV82ZXqNn8c4zq3C16dEOWriNFFan3vRvMLqRoYY7KcKYoDDlonDf0KQ0GIkhDzoYwSKdqgduvpHPjEO+NUdZJQ3E7/NqhRZUpnmkWwufZDR4PHxtyHWJufJqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230169; c=relaxed/simple; bh=dihbcKvFW699pQaPyqYSLLAFCYqBWIOZ5GkuBbOAeOc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l4GyTO5KqQR//kJnUZEMnRc/7aMz/0ye3VnA5Nxb0dHt+urd+OIRrsA1+1fr+lVRB+m6oUwuvhv3rTjfPkUpNld4sB4K16Ueg5Ta4SSefRlMO8tTOVlG+kUkaBZaOcsFLC2/j4E45DpkyTEuyx6lj4Zq4HB3ya8jXRrnM5+yknY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ItBK3aGb; 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="ItBK3aGb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E7E11F0089E; Thu, 24 Sep 2026 06:09:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230167; bh=fa42Z7sJn7eHCCjR0HSX1YHro4gkTrs6qZrrDCPffPA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ItBK3aGbHrt4wxvNLXSR46LDg1miA4vs/XlcsDuC253bPnZwWk2jQ7yy8TnL9VaOS oeNxIJrgppPEKVgxMYt3UOZm6t+SvqgmSRLFDyL5BOjo+vEX7Z+HgZCjg+dw2n/4F+ SGosF02od0BiOT+e38RoEe5rNPcRtz+MmIZ4xeJ5gBvptDWlET9BSDWIh9jARHFCfw WUN+s3njBjg/xw82BPVgi4XyKoTEu3pQ0RPOkDNore7vMawLL6P3Ig+6aLjIvchgTL KXh/WYWnRU5aEvPuJQjdr0jlAlEpj+vEV0D2iJDrdAWLQq4QPMnasu+2lN1uV8v7ej Kyy7s9L9xeKxg== 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 4/8] dma: swiotlb: Centralize default pool policy selection Date: Thu, 24 Sep 2026 11:37:52 +0530 Message-ID: <20260924060756.1325156-5-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3Dforce. 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. Reviewed-by: Robin Murphy Reviewed-by: Catalin Marinas Cc: Russell King Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Rutland Cc: Huacai Chen Cc: WANG Xuerui Cc: Thomas Bogendoerfer Cc: Jiaxun Yang Cc: Madhavan Srinivasan Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: "Ritesh Harjani (IBM)" Cc: Shrikanth Hegde Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Alexandre Ghiti Cc: Alexander Gordeev Cc: Gerald Schaefer Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- 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 | 90 ++++++++++++++++++++++------ 15 files changed, 128 insertions(+), 47 deletions(-) diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c index 0cc1bf04686d..aca97a4e5dcd 100644 --- a/arch/arm/mm/init.c +++ b/arch/arm/mm/init.c @@ -223,7 +223,11 @@ static inline void poison_init_mem(void *s, size_t cou= nt) void __init arch_mm_preinit(void) { #ifdef CONFIG_ARM_LPAE - swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE); + unsigned int flags =3D SWIOTLB_VERBOSE; + + if (max_pfn > arm_dma_pfn_limit) + flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + swiotlb_init(flags); #endif =20 #ifdef CONFIG_SA1111 diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 0842098a279b..f55ebe57b8e1 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -351,7 +351,10 @@ void __init arch_mm_preinit(void) swiotlb_adjust_size(min(swiotlb_default_pool_size(), size)); } =20 - swiotlb_init(true, flags); + if (max_pfn > PFN_DOWN(arm64_dma_phys_limit)) + flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + + swiotlb_init(flags); =20 /* * Check boundaries twice: Some fundamental inconsistencies can be diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c index 6fa4a22a58fd..02f52f6b2747 100644 --- a/arch/loongarch/kernel/setup.c +++ b/arch/loongarch/kernel/setup.c @@ -404,7 +404,7 @@ static void __init arch_mem_init(char **cmdline_p) =20 memblock_set_bottom_up(true); =20 - swiotlb_init(true, SWIOTLB_VERBOSE); + swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT); =20 dma_contiguous_reserve(PFN_PHYS(max_low_pfn)); =20 diff --git a/arch/mips/cavium-octeon/dma-octeon.c b/arch/mips/cavium-octeon= /dma-octeon.c index 9fbba6a8fa4c..ca7c37bc070f 100644 --- a/arch/mips/cavium-octeon/dma-octeon.c +++ b/arch/mips/cavium-octeon/dma-octeon.c @@ -235,5 +235,5 @@ void __init plat_swiotlb_setup(void) #endif =20 swiotlb_adjust_size(swiotlbsize); - swiotlb_init(true, SWIOTLB_VERBOSE); + swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT); } diff --git a/arch/mips/loongson64/dma.c b/arch/mips/loongson64/dma.c index 52801442ea86..5b8056e6c5a8 100644 --- a/arch/mips/loongson64/dma.c +++ b/arch/mips/loongson64/dma.c @@ -25,5 +25,5 @@ phys_addr_t dma_to_phys(struct device *dev, dma_addr_t da= ddr) =20 void __init plat_swiotlb_setup(void) { - swiotlb_init(true, SWIOTLB_VERBOSE); + swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT); } diff --git a/arch/mips/sibyte/common/dma.c b/arch/mips/sibyte/common/dma.c index c5c2c782aff6..3835fee21489 100644 --- a/arch/mips/sibyte/common/dma.c +++ b/arch/mips/sibyte/common/dma.c @@ -10,5 +10,5 @@ =20 void __init plat_swiotlb_setup(void) { - swiotlb_init(true, SWIOTLB_VERBOSE); + swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_INIT_ADDRESSING_LIMIT); } diff --git a/arch/powerpc/kernel/dma-swiotlb.c b/arch/powerpc/kernel/dma-sw= iotlb.c index ba256c37bcc0..97fffa46f05a 100644 --- a/arch/powerpc/kernel/dma-swiotlb.c +++ b/arch/powerpc/kernel/dma-swiotlb.c @@ -14,8 +14,10 @@ unsigned int ppc_swiotlb_flags; =20 void __init swiotlb_detect_4g(void) { - if ((memblock_end_of_DRAM() - 1) > 0xffffffff) + if ((memblock_end_of_DRAM() - 1) > 0xffffffff) { ppc_swiotlb_enable =3D 1; + ppc_swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + } } =20 static int __init check_swiotlb_enabled(void) diff --git a/arch/powerpc/mm/mem.c b/arch/powerpc/mm/mem.c index b617b69452cd..006ed41097e9 100644 --- a/arch/powerpc/mm/mem.c +++ b/arch/powerpc/mm/mem.c @@ -287,6 +287,19 @@ void __init arch_mm_preinit(void) BUILD_BUG_ON(MMU_PAGE_COUNT > 16); =20 #ifdef CONFIG_SWIOTLB + if (is_secure_guest()) { + + /* Don't release the SWIOTLB buffer. */ + ppc_swiotlb_enable =3D 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 |=3D 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 =20 kasan_late_init(); diff --git a/arch/powerpc/platforms/pseries/svm.c b/arch/powerpc/platforms/= pseries/svm.c index 7a403dbd35ee..4a0be631dc6d 100644 --- a/arch/powerpc/platforms/pseries/svm.c +++ b/arch/powerpc/platforms/pseries/svm.c @@ -21,16 +21,6 @@ static int __init init_svm(void) if (!is_secure_guest()) return 0; =20 - /* Don't release the SWIOTLB buffer. */ - ppc_swiotlb_enable =3D 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 |=3D SWIOTLB_ANY; - /* Share the SWIOTLB buffer with the host. */ swiotlb_update_mem_attributes(); =20 diff --git a/arch/powerpc/sysdev/fsl_pci.c b/arch/powerpc/sysdev/fsl_pci.c index 600f83cea1cd..49264e25108b 100644 --- a/arch/powerpc/sysdev/fsl_pci.c +++ b/arch/powerpc/sysdev/fsl_pci.c @@ -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 =3D 1; + ppc_swiotlb_flags |=3D 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", diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 5fa392021717..2562cf45a8f2 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -190,7 +190,11 @@ void __init arch_mm_preinit(void) swiotlb_flags |=3D SWIOTLB_ANY; } =20 - swiotlb_init(swiotlb, swiotlb_flags); + if ((max_pfn > PFN_DOWN(dma32_phys_limit)) && + (memblock_start_of_DRAM() < dma32_phys_limit)) + swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + + swiotlb_init(swiotlb_flags); =20 print_vm_layout(); } diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c index be7e009e7b59..78f507e24ceb 100644 --- a/arch/s390/mm/init.c +++ b/arch/s390/mm/init.c @@ -166,7 +166,7 @@ static void __init pv_init(void) virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); =20 /* make sure bounce buffers are shared */ - swiotlb_init(true, SWIOTLB_VERBOSE | SWIOTLB_ANY); + swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_ANY); swiotlb_update_mem_attributes(); } =20 diff --git a/arch/x86/kernel/pci-dma.c b/arch/x86/kernel/pci-dma.c index 75cf8f6ae8cd..0cff255827ba 100644 --- a/arch/x86/kernel/pci-dma.c +++ b/arch/x86/kernel/pci-dma.c @@ -44,8 +44,10 @@ static unsigned int x86_swiotlb_flags; static void __init pci_swiotlb_detect(void) { /* don't initialize swiotlb if iommu=3Doff (no_iommu=3D1) */ - if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) + if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) { x86_swiotlb_enable =3D true; + x86_swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + } =20 /* * 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 =3D true; - x86_swiotlb_flags |=3D 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 &=3D ~SWIOTLB_INIT_ADDRESSING_LIMIT; + x86_swiotlb_flags |=3D SWIOTLB_INIT_REMAP | SWIOTLB_ANY; + swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup); dma_ops =3D &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); } =20 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 =3D true; + x86_swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + } #endif if (!strncmp(p, "pt", 2)) iommu_set_default_passthrough(true); diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 5f24edf47b68..6386536a5ec6 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -16,6 +16,10 @@ struct scatterlist; =20 #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) =20 /* * Maximum allowable number of contiguous slabs to map, @@ -39,8 +43,8 @@ struct scatterlist; #endif =20 unsigned long swiotlb_default_pool_size(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 devic= e *dev) return mem && mem->force_bounce; } =20 -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 flag= s) +static inline void swiotlb_init(unsigned int flags) { } =20 diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 61efa3244daa..8ae315b2d0e7 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -81,6 +81,13 @@ struct io_tlb_slot { static bool swiotlb_force_bounce; static bool swiotlb_force_disable; =20 +enum swiotlb_pool_policy { + SWIOTLB_POOL_NONE, + SWIOTLB_POOL_MINIMAL, + SWIOTLB_POOL_DEFAULT, + SWIOTLB_POOL_CC_GUEST, +}; + #ifdef CONFIG_SWIOTLB_DYNAMIC =20 static void swiotlb_dyn_alloc(struct work_struct *work); @@ -354,24 +361,15 @@ static void swiotlb_mark_pool_used(struct io_tlb_pool= *pool) void __init swiotlb_update_mem_attributes(void) { struct io_tlb_pool *mem =3D &io_tlb_default_mem.defpool; - unsigned long bytes; - - /* - * if platform support memory encryption, swiotlb buffers are - * shared by default. - */ - if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) - io_tlb_default_mem.cc_shared =3D true; - else - io_tlb_default_mem.cc_shared =3D false; =20 if (!mem->nslabs || mem->late_alloc) return; - bytes =3D PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT); =20 if (io_tlb_default_mem.cc_shared) { int ret; + unsigned long bytes; =20 + bytes =3D PAGE_ALIGN(mem->nslabs << IO_TLB_SHIFT); ret =3D set_memory_decrypted((unsigned long)mem->vaddr, bytes >> PAGE_SHIFT); if (ret) { @@ -462,22 +460,72 @@ static void __init *swiotlb_memblock_alloc(unsigned l= ong nslabs, return tlb; } =20 +static bool __init swiotlb_kmalloc_needs_bounce(void) +{ + return IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && + (dma_get_cache_alignment() > 1); +} + +static void __init +swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy) +{ + switch (policy) { + case SWIOTLB_POOL_MINIMAL: + return; + case SWIOTLB_POOL_CC_GUEST: + return; + case SWIOTLB_POOL_NONE: + WARN(true, "Cannot adjust SWIOTLB size without a pool\n"); + return; + case SWIOTLB_POOL_DEFAULT: + default: + return; + } +} + +static enum swiotlb_pool_policy __init +swiotlb_select_pool_policy(unsigned int flags) +{ + if (swiotlb_force_disable) + return SWIOTLB_POOL_NONE; + + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) + return SWIOTLB_POOL_CC_GUEST; + + if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) + return SWIOTLB_POOL_DEFAULT; + + if (flags & SWIOTLB_INIT_REMAP) + return SWIOTLB_POOL_DEFAULT; + + if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT) + return SWIOTLB_POOL_DEFAULT; + + if (swiotlb_force_bounce) + return SWIOTLB_POOL_DEFAULT; + + if (swiotlb_kmalloc_needs_bounce()) + return SWIOTLB_POOL_MINIMAL; + + return SWIOTLB_POOL_NONE; +} + /* * Statically reserve bounce buffer space and initialize bounce buffer data * structures for the software IO TLB used to implement the DMA API. */ -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)) { struct io_tlb_pool *mem =3D &io_tlb_default_mem.defpool; + enum swiotlb_pool_policy policy; unsigned long nslabs; unsigned int nareas; size_t alloc_size; void *tlb; =20 - if (!addressing_limit && !swiotlb_force_bounce) - return; - if (swiotlb_force_disable) + policy =3D swiotlb_select_pool_policy(flags); + if (policy =3D=3D SWIOTLB_POOL_NONE) return; =20 io_tlb_default_mem.force_bounce =3D swiotlb_force_bounce; @@ -491,6 +539,12 @@ void __init swiotlb_init_remap(bool addressing_limit, = unsigned int flags, io_tlb_default_mem.phys_limit =3D ARCH_LOW_ADDRESS_LIMIT; #endif =20 + /* if we have host or guest memory encryption */ + if (cc_platform_has(CC_ATTR_MEM_ENCRYPT)) + io_tlb_default_mem.cc_shared =3D true; + + swiotlb_adjust_pool_size(policy); + if (!default_nareas) swiotlb_adjust_nareas(num_possible_cpus()); =20 @@ -531,9 +585,9 @@ void __init swiotlb_init_remap(bool addressing_limit, u= nsigned int flags, swiotlb_print_info(); } =20 -void __init swiotlb_init(bool addressing_limit, unsigned int flags) +void __init swiotlb_init(unsigned int flags) { - swiotlb_init_remap(addressing_limit, flags, NULL); + swiotlb_init_remap(flags, NULL); } =20 /* --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 BFEB0381E8B; Thu, 24 Sep 2026 06:09:44 +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=1790230186; cv=none; b=MWV0/Tchvs+pP+c5HXOZgbh50+Jy/gIfChBjXpnx4C5tOdKWll1gITIsFUT/S9JS7ElTMvbP4pSdGrgSl751Rw1WnS7RQdN67gS7T9FcwgyEOmh4sCM3AQw2t93ru7o7WEl5Ycq8i/qiS0jAiU06iumXjaMSEYmjn27HTWRdCmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230186; c=relaxed/simple; bh=B7u2gHOxSgbV1IVBu7Ya9S2XZ0SGh6Tfesrhi9DhxA8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GIDPCH2p+fL2fRxC9Y2mV8y50Mt+ZSNSHbMvcDqLaUZCHyozEp7y1yW178dfAdnvECvrG4R27Tqw9QDf/DwqcfRftWLdc6lpUJbHTbvdTZasNNyVBKA0Ro3P23Vnq9u96khQVe+qo+fS+bTZ0jGsSnJiP/hN/HClutG50xtVqm8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DCNOixZ1; 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="DCNOixZ1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E16F91F000FF; Thu, 24 Sep 2026 06:09:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230184; bh=tusr1yJ0MgOFWnYBFAQROVz75fdWB9waVMwRJ7a9kV0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=DCNOixZ1Uff5UkaFVScwFmBw6mXUZbMx0tgzEzPPRjLmGZVot10iFQ0ssXfLbeo52 z7fTCAC0mZk2tXAEkEnMTDNkATIn8sbA9AO8GsxYR4xGD7P90W2FNMjkQysI1pf47w LbD9PgYtrDtUxxVLrfl77xSmS8nUjRfRqlYI/5y+hUIgeSzBpFR3i8x1opZKu45s35 ezO3ufVmH5GDESRxdwtIbqlfidV30AS+NYPv2zC/MUFlnZR/aDK0wCzTU1HjyYV/Xe 9a8MCBjC42Bry8aSYtbpOkz3ae1A4V7BdqAyJoaZmvcPMeQ6W9CBkSe5SFYb4bt3g2 ho4cz+DiCI9Kw== 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 5/8] dma: swiotlb: Centralize minimal pool sizing Date: Thu, 24 Sep 2026 11:37:53 +0530 Message-ID: <20260924060756.1325156-6-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D sizing remains unchanged because swiotlb_adjust_size() preserves a user-configured size. Reviewed-by: Catalin Marinas Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Rutland Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Alexandre Ghiti Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- arch/arm64/mm/init.c | 11 ----------- arch/riscv/mm/init.c | 4 ---- kernel/dma/swiotlb.c | 9 ++++++++- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index f55ebe57b8e1..95075b5c8207 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -340,17 +340,6 @@ void __init arch_mm_preinit(void) { unsigned int flags =3D SWIOTLB_VERBOSE; =20 - if (max_pfn <=3D 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 =3D - DIV_ROUND_UP(memblock_phys_mem_size(), 1024); - - swiotlb_adjust_size(min(swiotlb_default_pool_size(), size)); - } - if (max_pfn > PFN_DOWN(arm64_dma_phys_limit)) flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; =20 diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 2562cf45a8f2..96856114b387 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -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 =3D - DIV_ROUND_UP(memblock_phys_mem_size(), 1024); - swiotlb_adjust_size(min(swiotlb_default_pool_size(), size)); - swiotlb =3D true; swiotlb_flags |=3D SWIOTLB_ANY; } =20 diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 8ae315b2d0e7..75ee63cd354e 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -469,9 +469,14 @@ static bool __init swiotlb_kmalloc_needs_bounce(void) static void __init swiotlb_adjust_pool_size(enum swiotlb_pool_policy policy) { + unsigned long size; + switch (policy) { case SWIOTLB_POOL_MINIMAL: - return; + /* Use 1MB per 1GB of RAM for kmalloc() bouncing. */ + size =3D DIV_ROUND_UP(memblock_phys_mem_size(), 1024); + size =3D min(swiotlb_default_pool_size(), size); + break; case SWIOTLB_POOL_CC_GUEST: return; case SWIOTLB_POOL_NONE: @@ -481,6 +486,8 @@ swiotlb_adjust_pool_size(enum swiotlb_pool_policy polic= y) default: return; } + + swiotlb_adjust_size(size); } =20 static enum swiotlb_pool_policy __init --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D 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) =20 void __init mem_encrypt_setup_arch(void) { - phys_addr_t total_mem =3D 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; =20 - /* - * 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 =3D total_mem * 6 / 100; - size =3D 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 { =20 static bool swiotlb_force_bounce; static bool swiotlb_force_disable; +static bool restricted_dma_pool_present __initdata; =20 enum swiotlb_pool_policy { SWIOTLB_POOL_NONE, @@ -478,7 +480,25 @@ swiotlb_adjust_pool_size(enum swiotlb_pool_policy poli= cy) size =3D 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 =3D div_u64((u64)memblock_phys_mem_size() * 6, 100); + size =3D 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; =20 - 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; =20 if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) @@ -2142,6 +2163,8 @@ static int __init rmem_swiotlb_setup(unsigned long no= de, of_get_flat_dt_prop(node, "no-map", NULL)) return -EINVAL; =20 + restricted_dma_pool_present =3D true; + pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld Mi= B\n", &rmem->base, (unsigned long)rmem->size / SZ_1M); return 0; --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 44E933DDDB0; Thu, 24 Sep 2026 06:10:18 +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=1790230220; cv=none; b=Hcc6roS3F4IUPCu5VdflbTcG6n4RAdAe8PbidjkaHZKFNmq8JIsBEbFxrcEfrQKATurLjrx6yznbEb8Vj5prF+jYYYt8I70qy01fOQjyyzIBQNh+ljJbb2GX65Cv/H5b62kxIlXq36UR00jhN7epgQi4gfyUoMqqxPilAiYCsrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230220; c=relaxed/simple; bh=p1uZ6m2QAos8fgm5OVJVppqz17Rk2IQHv1YVhSpdB4A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XbF0n0y0VCCKPmdBobsJck57jz/cjbVQARTfGND8TSiZ7c++Nq09LKmRic4iCLKfzXJo1fREJGLuhjf2mmwnPFE7TRB3ZGxd+iMAAdZ80/sw0kh3PSYjECjQ4CJSiCkQfBSckNZ+/AuqITuJlWeySkpHHqdYC9xvwCCELXjacyA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lfjpmzYr; 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="lfjpmzYr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F4561F000FF; Thu, 24 Sep 2026 06:10:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230218; bh=rNm+mOo8Qc346ouhd+MVcZZHr4pMSublo9/m/bzvlkE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lfjpmzYr2RJ/hwCqh4+Xa84oi7xdypaLrJm6fg+U3mWTgBN0yRfH3aGn1EEj3dGNt Kp8IckeRp2L30fKxhmpkmXWq7OXQzrIMlRFV5cq4/gsT1SVlUaePfDRHSSG0nQhPtr qZ9Swk606Qo4mAVLWNXtqH5LINBI8/SaVe5/E9BC7xGXnX64SBaDOWGIogT7WKp00G CTji3JPJk7iQKZo6TX6213hpDPapehVq3bDQPvXlV7MKjRai9u30EpJnhDlmr1I004 MY2ZKadu9+0hUOxFLnaMmc/JR0oU9ocYeXs6lWmKcVWqBN16sO/IkzkeR9Q51iM251 gkQnHaMRcshWw== 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 7/8] dma: swiotlb: Add an overridable architecture pool opt-out Date: Thu, 24 Sep 2026 11:37:55 +0530 Message-ID: <20260924060756.1325156-8-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: quoted-printable Content-Type: text/plain; charset="utf-8" On x86, iommu=3Doff 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=3Dforce 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_DEFAULT_OFF 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=3Dnoforce and unconditionally prevents pool initialization, the new flag is an architecture default. Explicit requirements such as memory encryption, architecture remapping, or swiotlb=3Dforce take precedence over it. iommu=3Dsoft 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=3Doff disables both hardware IOMMU translation and swiotlb bouncing. Direct DMA remains available; mappings outside a device's addressable range fail instead of being bounced. Reviewed-by: Catalin Marinas Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- arch/x86/kernel/pci-dma.c | 11 ++++++++--- include/linux/swiotlb.h | 2 ++ kernel/dma/swiotlb.c | 10 ++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/pci-dma.c b/arch/x86/kernel/pci-dma.c index 0cff255827ba..ce257b20ea9c 100644 --- a/arch/x86/kernel/pci-dma.c +++ b/arch/x86/kernel/pci-dma.c @@ -44,9 +44,13 @@ static unsigned int x86_swiotlb_flags; static void __init pci_swiotlb_detect(void) { /* don't initialize swiotlb if iommu=3Doff (no_iommu=3D1) */ - if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN) { - x86_swiotlb_enable =3D true; + if (no_iommu) + x86_swiotlb_flags |=3D SWIOTLB_INIT_DEFAULT_OFF; + + if (max_possible_pfn > MAX_DMA32_PFN) { x86_swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; + if (!no_iommu) + x86_swiotlb_enable =3D true; } =20 /* @@ -84,7 +88,8 @@ static void __init pci_xen_swiotlb_init(void) return; x86_swiotlb_enable =3D true; /* Xen can use a SWIOTLB pool anywhere in directly mapped memory. */ - x86_swiotlb_flags &=3D ~SWIOTLB_INIT_ADDRESSING_LIMIT; + x86_swiotlb_flags &=3D ~(SWIOTLB_INIT_ADDRESSING_LIMIT | + SWIOTLB_INIT_DEFAULT_OFF); x86_swiotlb_flags |=3D SWIOTLB_INIT_REMAP | SWIOTLB_ANY; swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup); dma_ops =3D &xen_swiotlb_dma_ops; diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 6386536a5ec6..f5ce45f2994b 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -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) =20 /* * Maximum allowable number of contiguous slabs to map, diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index e626cca63ad1..130a13eb85b0 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -526,10 +526,16 @@ swiotlb_select_pool_policy(unsigned int flags) if (flags & SWIOTLB_INIT_REMAP) return SWIOTLB_POOL_DEFAULT; =20 - if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT) + if (swiotlb_force_bounce) return SWIOTLB_POOL_DEFAULT; =20 - 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; =20 if (swiotlb_kmalloc_needs_bounce()) --=20 2.43.0 From nobody Thu Sep 24 12:50:46 2026 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 7CDCD3EDE43; Thu, 24 Sep 2026 06:10:36 +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=1790230238; cv=none; b=JuGHGuoGV3oZUi3qxaXIzevRoaCWbJslVr7XOmb4tA5k1J+qWu7YCJggGAQLLYQDcuBbMabgvRnc/gA2XANAA3ygLJRKd8PwF9wd+eTKMmhzFGChGNfUcHiEd38mJgul1fBUPt4q69oLWdbpFGHARWKN3mT8zfPswXIe37UE3ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790230238; c=relaxed/simple; bh=dD6ObIKSSqijbh+0yZUIR1NC8si7N+WSUYK2dWGGSqI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dUN2srr+8ThZkta0m9Eo8NxkC9scmopbKE7Ny6D3nlAWrHEWL09kAea+ovLQLQIdJD4UvIbXn/A50/vP+AeA141RacTMYNGo64UbghfcozOhlpCl1XDNob8/QMv3vpwG5g7xteUFOEov9cZZDNfmAZDKCem/nDu0NtvAA0J6DvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mv7QZcUK; 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="mv7QZcUK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 857421F00899; Thu, 24 Sep 2026 06:10:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1790230236; bh=jX6aIZrBXbttG8mDoIDDZezQu+BWHDQT5zvSSYON3kI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=mv7QZcUKekVi8xEIBYldkeMJ63fZXB3dnKscuE25jnwxzYrV7YpOlQV/JJ7nfqg3x SfDLbl+mDW1GI/N7TXv5KGeZNR8MKIL21WpefTE9MXcFmkl7CaqykwTkTliHDnqqvk 4Ym75PMMk7c3iX1NgY2p6jy3VU+TVpCMyMg1HNZpwafhoMM5GRvepJ1E6t1Wn57j6r mMLod8Y2tm4uoakNX4QCVAPga4nscd5K68nbDB46LddC7N68bEyQWZtWIn7HmZ8RE9 Iw+XQ3et9bhPrJLeaeGRWXA0GWgj6znxqt3YA7o5wCWlj+y1mYkFRwWC7aLuqtrqtN cXVDy/0hxqblA== 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 8/8] dma: swiotlb: Remove SWIOTLB_ANY Date: Thu, 24 Sep 2026 11:37:56 +0530 Message-ID: <20260924060756.1325156-9-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Reviewed-by: Robin Murphy Reviewed-by: Catalin Marinas Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Madhavan Srinivasan Cc: Michael Ellerman Cc: Nicholas Piggin Cc: "Christophe Leroy (CS GROUP)" Cc: "Ritesh Harjani (IBM)" Cc: Shrikanth Hegde Cc: Paul Walmsley Cc: Palmer Dabbelt Cc: Albert Ou Cc: Alexandre Ghiti Cc: Alexander Gordeev Cc: Gerald Schaefer Cc: Heiko Carstens Cc: Vasily Gorbik Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: x86@kernel.org Cc: "H. Peter Anvin" Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- 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(-) diff --git a/Documentation/core-api/swiotlb.rst b/Documentation/core-api/sw= iotlb.rst index 71b4e4c27eb5..06d9f88fdf7b 100644 --- a/Documentation/core-api/swiotlb.rst +++ b/Documentation/core-api/swiotlb.rst @@ -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 lin= e so -it works for devices that can only address 32-bits of physical memory (unl= ess -architecture-specific code provides the SWIOTLB_ANY flag). In a CoCo VM, t= he -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. =20 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 diff --git a/arch/powerpc/mm/mem.c b/arch/powerpc/mm/mem.c index 006ed41097e9..1bdea6c03848 100644 --- a/arch/powerpc/mm/mem.c +++ b/arch/powerpc/mm/mem.c @@ -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 |=3D SWIOTLB_ANY; + ppc_swiotlb_flags &=3D ~SWIOTLB_INIT_ADDRESSING_LIMIT; } =20 /* diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 96856114b387..734712230f35 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -165,27 +165,11 @@ static void print_vm_layout(void) { } =20 void __init arch_mm_preinit(void) { - bool swiotlb =3D max_pfn > PFN_DOWN(dma32_phys_limit) && - memblock_start_of_DRAM() < dma32_phys_limit; unsigned int swiotlb_flags =3D SWIOTLB_VERBOSE; #ifdef CONFIG_FLATMEM BUG_ON(!mem_map); #endif /* CONFIG_FLATMEM */ =20 - if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb && - dma_cache_alignment !=3D 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 |=3D SWIOTLB_ANY; - } - if ((max_pfn > PFN_DOWN(dma32_phys_limit)) && (memblock_start_of_DRAM() < dma32_phys_limit)) swiotlb_flags |=3D SWIOTLB_INIT_ADDRESSING_LIMIT; diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c index 78f507e24ceb..ce10292447f1 100644 --- a/arch/s390/mm/init.c +++ b/arch/s390/mm/init.c @@ -166,7 +166,7 @@ static void __init pv_init(void) virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); =20 /* make sure bounce buffers are shared */ - swiotlb_init(SWIOTLB_VERBOSE | SWIOTLB_ANY); + swiotlb_init(SWIOTLB_VERBOSE); swiotlb_update_mem_attributes(); } =20 diff --git a/arch/x86/kernel/pci-dma.c b/arch/x86/kernel/pci-dma.c index ce257b20ea9c..74e92b457758 100644 --- a/arch/x86/kernel/pci-dma.c +++ b/arch/x86/kernel/pci-dma.c @@ -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 &=3D ~(SWIOTLB_INIT_ADDRESSING_LIMIT | SWIOTLB_INIT_DEFAULT_OFF); - x86_swiotlb_flags |=3D SWIOTLB_INIT_REMAP | SWIOTLB_ANY; + x86_swiotlb_flags |=3D SWIOTLB_INIT_REMAP; swiotlb_init_remap(x86_swiotlb_flags, xen_swiotlb_fixup); dma_ops =3D &xen_swiotlb_dma_ops; if (IS_ENABLED(CONFIG_PCI)) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index f5ce45f2994b..3c3f9d93d400 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -15,13 +15,12 @@ struct page; struct scatterlist; =20 #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) =20 /* * Maximum allowable number of contiguous slabs to map, diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 130a13eb85b0..4d13421c3901 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -437,15 +437,10 @@ static void __init *swiotlb_memblock_alloc(unsigned l= ong nslabs, size_t bytes =3D PAGE_ALIGN(nslabs << IO_TLB_SHIFT); void *tlb; =20 - /* - * 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 =3D memblock_alloc(bytes, PAGE_SIZE); - else + if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT) tlb =3D memblock_alloc_low(bytes, PAGE_SIZE); + else + tlb =3D memblock_alloc(bytes, PAGE_SIZE); =20 if (!tlb) { pr_warn("%s: Failed to allocate %zu bytes tlb structure\n", @@ -567,10 +562,10 @@ void __init swiotlb_init_remap(unsigned int flags, #ifdef CONFIG_SWIOTLB_DYNAMIC if (!remap) io_tlb_default_mem.can_grow =3D true; - if (flags & SWIOTLB_ANY) - io_tlb_default_mem.phys_limit =3D virt_to_phys(high_memory - 1); - else + if (flags & SWIOTLB_INIT_ADDRESSING_LIMIT) io_tlb_default_mem.phys_limit =3D ARCH_LOW_ADDRESS_LIMIT; + else + io_tlb_default_mem.phys_limit =3D virt_to_phys(high_memory - 1); #endif =20 /* if we have host or guest memory encryption */ --=20 2.43.0