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 D5E6240D57B; Fri, 4 Sep 2026 10:19:00 +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=1788517143; cv=none; b=GNBMz+3ZbjNgj3kc5L14ih7BPegT/K9NyFFyFei5ssMqMy+cObborc8c3dT7Pdql4v18UYaB2Qu0OIpwDMsjEII22XdXJyWqwguyG85j/zDwsGcnlou/oCzZF62qD9ekygCqJiO/Ffz9aulIGwzetLElgnEzvJvh8UFFK6JqySU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788517143; c=relaxed/simple; bh=3yKkRSaVVBl4ZK+//zaXPd6AJLh4CsYenHmkG6VHFfM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PYtqsHBKMJQIq+9kb21iMLEF3f9rFfBw6ZPnWn3DW1b8OEmNZecBDrmm/u/IRw/RYN4TNW8Csk1Wb+So0d++NxQ5g3zwdJhL4joZTUWd3Cbtdz0HSphyRWqFTwowi7uuQfyMQo1gxxPW+heNl93ooVxTJhFVpZl6wuseVTo7iOw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hgnDgwtm; 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="hgnDgwtm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 924521F00A3D; Fri, 4 Sep 2026 10:18:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788517139; bh=wZzk2gTe4yopE7nwhjxjLHyb9ULGgpT/4fu9Q1AhpHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hgnDgwtmJBR4VQibi8Y3PH+fcWHKA3VdBX/ioaGUIUm2NI4B8jEmsyUh2IMKIxasD RpuXJlT71VriRm2nQWlR7xcpPrfp95m6N1j5OjeBpiQwNDREq5jsIkpIWqWrBGOoIm FYWmwH7QvgnVZdUtnv0K3lJAWzhUr0UpYhlzziYrR1AwWNIW2cVthYV8b0IU0uQ1EQ HI8WPSv1rIhdOn4Qpm/IEeur9GG/fP7ENXGlsI4wyj1YjqS+H33tF5kfPvEXNnv2+h ambldeLP5ADSEk45lgzDPyr05O3HSPByI5SI8YcDF3KamIzLg4kQhpeDGRSPYaGdtQ 775XovWEAxRVw== From: "Aneesh Kumar K.V (Arm)" To: iommu@lists.linux.dev Cc: "Aneesh Kumar K.V (Arm)" , Robin Murphy , Marek Szyprowski , Will Deacon , 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)" , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , Russell King , Huacai Chen , Thomas Bogendoerfer , Jiaxun Yang , Paul Walmsley , Palmer Dabbelt , Albert Ou , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.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 v3 2/4] dma: swiotlb: Centralize minimal pool sizing Date: Fri, 4 Sep 2026 15:48:08 +0530 Message-ID: <20260904101810.1193505-3-aneesh.kumar@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260904101810.1193505-1-aneesh.kumar@kernel.org> References: <20260904101810.1193505-1-aneesh.kumar@kernel.org> Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. NOTE: We lose the RISC-V SWIOTLB_ANY setting in this patch. It will be reinstated in a follow-up patch where we make SWIOTLB_ANY redundant. Signed-off-by: Aneesh Kumar K.V (Arm) --- arch/arm64/mm/init.c | 11 ----------- arch/riscv/mm/init.c | 20 -------------------- kernel/dma/swiotlb.c | 18 ++++++++++++++++-- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 7d51fb5f5a71..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 = 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; diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index fc1bec090db1..7921ca9c58cd 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -165,31 +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. - */ - 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; - } - if ((max_pfn > PFN_DOWN(dma32_phys_limit)) && (memblock_start_of_DRAM() < dma32_phys_limit)) swiotlb_flags |= SWIOTLB_INIT_ADDRESSING_LIMIT; diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index aa19e81ae544..90e0bfd0715c 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -304,6 +304,11 @@ unsigned long swiotlb_size_or_default(void) return default_nslabs << IO_TLB_SHIFT; } +static bool __init swiotlb_default_size_changed(void) +{ + return default_nslabs != IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT; +} + void __init swiotlb_adjust_size(unsigned long size) { /* @@ -311,7 +316,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); @@ -476,9 +481,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: -- 2.43.0