From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796205623; b=0XBtgcaDyItXsFJyGpAlZNyqT95+Pw50heFc2Z1ys5cBNuBdD20kjU3VI7QYulsdXe5U s/F/gU9WSaBKyFV4Sol0GnZ4otQtiI/qfva/b5BgjKPHLBbjl3PI3Cr98d9NMi6OB6/Il GQT8qajmaSuLrmvL/aN80/zCPccESgbHGGF6u2GQL86UPgbKkZ0L7A16RVgz/fqxLYT7i DgSZWyNktCokuDYyJ2a1xK/B775hFokuwKc1biouN6EuGHzlFhouAPKCpnaFQwg2/TlMf ytVTj3gCmoqqABCQ+Zw0rz9iTeecRad22cLX6kgLdS8n3iEfjuyYXrcDZ64QiJD6a7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796205623; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=xO/mPMmKondO7aGSg6/2Lwkwym4bwHABYUqhvlFKOlk=; b=AedT++0QRVTVn5k4e415VCCB4aUEzyMNMk+HCxcsNF63gQMFVDSnHIsZAqF59XMVP4F4 2uohdk2tnpO5ksCIb3a7NYGhRhwsOCTPi2doTnBp4l8W226WkNydJE9ke8r2FvyJ9bqCA dz7wnVaNJP5YCEv2Ou8aQtytBH7T+rar3zYxvt/XVhH9IXhkJ7/1jNFIVlcBNnbjgWO2b +6ccXNAdwDTqI+RQSEg0AIY+TFuLTVAMi9Eq2WaZzZwGO9L7/iRsJwfujOaWwcVzJQwnV qc8wB7ZOU56c322/bGJWdwfeTAy8CYMlskuE5kF2ZyL/gFy2iaK0rAL8guvBigBg1ZQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lfvoCO5F; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33BC0606A322; Wed, 2 Dec 2026 10:00:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lfvoCO5F Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1ABEC1F00A16; Wed, 2 Dec 2026 10:00:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796205622; bh=xO/mPMmKondO7aGSg6/2Lwkwym4bwHABYUqhvlFKOlk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lfvoCO5FneJcbA8ppr2bzaqQx3dzqbd2Jbh1JYj/GuYHfGNNHh6HNd1vh11afXtO5 eVSZRvC8Lef+Tlec3ARLth2tZRNcp2JJb5ty+b7RTVgHOKtJjrINtFjs/WVtXfMd29 UrayS2qkwj/7rOizoSPv9aBVfNsH2I1M7uPQa/KlcATXwgkQ6bm1TnnEX46bsqhEoI XOjhN+o8UNR/rYhvhtnQlNSU5D6vEMfNjNgJXwdzek4DIh3j2zSpmpoaBHTR2Xyg5X p6hAVswcn4eL6cod8Jvp7A491IfZDSlVHV0f82tqVpJzKJziH41R3A5MV/bNF87TKs hDEcJfoAmPJgA== From: "Aneesh Kumar K.V (Arm)" To: linux-coco@lists.linux.dev Cc: "Aneesh Kumar K.V (Arm)" , Andrew Morton , Catalin Marinas , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jason Gunthorpe , Joerg Roedel , Marc Zyngier , Marek Szyprowski , Robin Murphy , Steven Price , Sumit Semwal , Suzuki K Poulose , Will Deacon , "T.J. Mercier" , Madhavan Srinivasan , Heiko Carstens , Christian Borntraeger , Sven Schnelle , Dave Hansen , Kiryl Shutsemau , Rick Edgecombe , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Haiyang Zhang , Wei Liu , Dexuan Cui , Andy Lutomirski , Peter Zijlstra , Alexander Gordeev , Gerald Schaefer , Vasily Gorbik , x86@kernel.org, dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, linaro-mm-sig@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-media@vger.kernel.org, linux-mm@kvack.org, kvmarm@lists.linux.dev, patches@lists.linux.dev Subject: [PATCH v11 23/23] swiotlb: Make rounded shared pool capacity allocatable Date: Wed, 2 Dec 2026 15:24:46 +0530 Message-ID: <20261202095446.2029567-24-aneesh.kumar@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261202095446.2029567-1-aneesh.kumar@kernel.org> References: <20261202095446.2029567-1-aneesh.kumar@kernel.org> X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit CoCo shared memory may need to be allocated and transitioned in units larger than the requested object. Before this change, users handled the resulting capacity as follows: User Rounded capacity reused dma-buf system heap no DMA-direct no regular GIC tables no small GIC ITTs yes, through a gen_pool early SWIOTLB pool no late SWIOTLB pool yes persistent dynamic SWIOTLB no transient dynamic SWIOTLB no, one mapping only atomic DMA pools yes, through a gen_pool restricted SWIOTLB pool no additional padding Improve the early and persistent dynamic SWIOTLB pools. They already own and transition backing rounded to the shared granule size, and SWIOTLB is itself a suballocator. Advertise the rounded extent as slots, size the slot metadata to match. This makes the extra capacity available without reserving more backing memory. Record the rounded capacity for buddy-backed shared transient pools too. Keep the original suballocation size for non-blocking shared transient pools, which allocate from the atomic DMA pools. With pool start and end recording the rounded backing extent, use the recorded size for memory transitions and freeing without aligning it again. Keep the atomic-pool return first in swiotlb_free_tlb(), so those suballocations are returned using their original size. Do not attempt the same optimization for dma-buf, DMA-direct or regular GIC objects. Those allocations have independent caller-visible sizes and lifetimes. Reusing their padding requires a shared-granule suballocator with reference counting, per-object mappings and accounting. Note: For the current 64 KiB CCA shared granule size, SWIOTLB pool sizes are already multiples of the 256 KiB IO_TLB segment size. Consequently, the rounding does not change any runtime values on current CCA systems. It instead makes the code express the intended invariant that pool metadata describes the complete shared-granule-aligned backing allocation. Cc: Marek Szyprowski Cc: Robin Murphy Signed-off-by: Aneesh Kumar K.V (Arm) --- kernel/dma/swiotlb.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index dbeb600cbf24..2ef9cdda4d7b 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -329,6 +329,15 @@ static inline unsigned long nr_slots(u64 val) return DIV_ROUND_UP(val, IO_TLB_SIZE); } +static unsigned long swiotlb_align_nslabs(unsigned long nslabs) +{ + unsigned long bytes = nslabs << IO_TLB_SHIFT; + + bytes = ALIGN(bytes, cc_shared_granule_size()); + + return bytes >> IO_TLB_SHIFT; +} + static void swiotlb_mark_pool_used(struct io_tlb_pool *pool) { unsigned long i; @@ -369,7 +378,7 @@ void __init swiotlb_update_mem_attributes(void) if (!mem->nslabs || mem->late_alloc) return; - bytes = ALIGN(mem->nslabs << IO_TLB_SHIFT, cc_shared_granule_size()); + bytes = mem->nslabs << IO_TLB_SHIFT; if (io_tlb_default_mem.cc_shared) { int ret; @@ -432,11 +441,12 @@ static void add_mem_pool(struct io_tlb_mem *mem, struct io_tlb_pool *pool) } static void __init *swiotlb_memblock_alloc(unsigned long nslabs, - unsigned int flags, + unsigned long *alloc_nslabs, unsigned int flags, int (*remap)(void *tlb, unsigned long nslabs)) { + unsigned long aligned_nslabs = swiotlb_align_nslabs(nslabs); + size_t bytes = aligned_nslabs << IO_TLB_SHIFT; void *tlb; - size_t bytes = ALIGN(nslabs << IO_TLB_SHIFT, cc_shared_granule_size()); /* * By default allocate the bounce buffer memory from low memory, but @@ -454,12 +464,13 @@ static void __init *swiotlb_memblock_alloc(unsigned long nslabs, return NULL; } - if (remap && remap(tlb, nslabs) < 0) { + if (remap && remap(tlb, aligned_nslabs) < 0) { memblock_free(tlb, bytes); pr_warn("%s: Failed to remap %zu bytes\n", __func__, bytes); return NULL; } + *alloc_nslabs = aligned_nslabs; return tlb; } @@ -472,6 +483,7 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, { struct io_tlb_pool *mem = &io_tlb_default_mem.defpool; unsigned long nslabs; + unsigned long alloc_nslabs; unsigned int nareas; size_t alloc_size; void *tlb; @@ -496,14 +508,16 @@ void __init swiotlb_init_remap(bool addressing_limit, unsigned int flags, swiotlb_adjust_nareas(num_possible_cpus()); nslabs = default_nslabs; - nareas = limit_nareas(default_nareas, nslabs); - while ((tlb = swiotlb_memblock_alloc(nslabs, flags, remap)) == NULL) { + while ((tlb = swiotlb_memblock_alloc(nslabs, &alloc_nslabs, flags, + remap)) == NULL) { if (nslabs <= IO_TLB_MIN_SLABS) return; nslabs = ALIGN(nslabs >> 1, IO_TLB_SEGSIZE); - nareas = limit_nareas(nareas, nslabs); } + nslabs = alloc_nslabs; + nareas = limit_nareas(default_nareas, nslabs); + if (default_nslabs != nslabs) { pr_info("SWIOTLB bounce buffer size adjusted %lu -> %lu slabs", default_nslabs, nslabs); @@ -684,7 +698,7 @@ void __init swiotlb_exit(void) io_tlb_default_mem.nslabs = 0; #endif tbl_vaddr = (unsigned long)phys_to_virt(mem->start); - tbl_size = ALIGN(mem->end - mem->start, cc_shared_granule_size()); + tbl_size = mem->end - mem->start; slots_size = PAGE_ALIGN(array_size(sizeof(*mem->slots), mem->nslabs)); if (io_tlb_default_mem.cc_shared) { @@ -824,9 +838,6 @@ static void swiotlb_free_tlb(void *vaddr, size_t bytes, bool cc_shared) dma_free_from_pool(NULL, vaddr, bytes)) return; - if (cc_shared) - bytes = ALIGN(bytes, cc_shared_granule_size()); - /* Intentional leak if pages cannot be encrypted again. */ if (!cc_shared || !cc_make_private(vaddr, bytes)) @@ -883,6 +894,13 @@ static struct io_tlb_pool *swiotlb_alloc_pool(struct device *dev, tlb_size = nslabs << IO_TLB_SHIFT; } + /* Account for granule rounding in buddy-backed shared allocations. */ + if (mem->cc_shared && + (!dev || gfpflags_allow_blocking(gfp))) { + nslabs = swiotlb_align_nslabs(nslabs); + tlb_size = nslabs << IO_TLB_SHIFT; + } + slot_order = get_order(array_size(sizeof(*pool->slots), nslabs)); pool->slots = (struct io_tlb_slot *) __get_free_pages(gfp, slot_order); -- 2.43.0