From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7EDE4C624D3 for ; Fri, 4 Sep 2026 10:19:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wZzk2gTe4yopE7nwhjxjLHyb9ULGgpT/4fu9Q1AhpHg=; b=KiqZy22CI/uCcHUeVCXqPaVk3r BIYA3eQx+KsmWVWx6fdWqN+zClKciiqJ3kECwhtBgzM7dQRqjL8uIjq4lWgZn9m5Raz+nsqnqFZEd fHWCgTXguCcXnBEJt2lk+pFTs2mdJ9VFDR3sPsDQtt4iV5DNNYnWqflDPb/ULuBFu5/1TTorW6VXf p5pBbsQPODgueNwBV+QzEZNYfTkpc3J5tMjudUFkNxybzSH+tue2LiM5moiHXTca4Pl2xKxwokBgu lOtO0anCGfNv59MHmwGc26M/pJ+q0oOnfY88ls2UEanwP3DHvcypfFOlf/3+T8LcO311NNenugDGx OZ+peOSw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1x2R0h-00000001e9D-0xlS; Fri, 04 Sep 2026 10:19:03 +0000 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1x2R0f-00000001e7k-0Eit; Fri, 04 Sep 2026 10:19:01 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 235F860211; Fri, 4 Sep 2026 10:19:00 +0000 (UTC) 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> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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