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 54501CD5BAF for ; Fri, 22 May 2026 04:32:21 +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=JY/iEQ1zR5tB3W7tvOEg18U1UPZnJP5nA9qclDJhKPc=; b=xGqDNG/E9BwAsUlCrK7hpHq3Zi CYIDlqyFVVxAjytz6vkbMY5oeMPj28WTUMYEvtWJ2AKlkGkwuz4A4Fb50AymQ8KueC4I/RP8ADjxE Cgx4puVl+o7Aj+AJb98Hrb7qH5kjK+lpyEpj10s8SyygS5kfsP6OcCMC9EJRCyUukPovXo6CwREeb lu0FgsRmTntIRfM2qt6m1v0+jlKxbGNEGJtHNOVnOBCp5/99nerivaXQz4n+WWS5wqcBtPVYEIwyL JXgEKy+ffspdwZF6nXQZJCjmWJ/Qw6xKzWgrmEcATFknB/7O38f8TtRWTb43MzBYzhBn8OoR8IcWH yiefEOnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wQHYV-00000009l4Z-0mcO; Fri, 22 May 2026 04:32:15 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wQHYS-00000009l2f-1j7G for linux-arm-kernel@lists.infradead.org; Fri, 22 May 2026 04:32:13 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 2141E43358; Fri, 22 May 2026 04:32:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7FC141F000E9; Fri, 22 May 2026 04:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779424332; bh=JY/iEQ1zR5tB3W7tvOEg18U1UPZnJP5nA9qclDJhKPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FP9J5cBMtIrWF+YanfLJVtaal/L7De0g7NNTpiXBYsS3spBW3aKd/DrwT3ygheJCW /YjQagBXSYIQ2v3g7014Y5s6f2Ab4hJy19x6UcGGNgWnX8NCgZo/CmaMMZ5Y1ms06i kCPF5b37pZm+8cRgKLiWjp660ZTGDDyyHNSiRNcn9uzFTsXM8ukykKmrSnOyBckOvV YpcjVw6+BU47iM8oxd9+3xBNwjZsHoccsURGcqTaFAfAD0UudDFa8YuFiIV4dedX59 CfuuECchxVGDG+aPmlgg/Yu7u8rmQDsiMuPZ3WwI5iC2s9JERoajJ7bkP4n4ZHagT6 fNwVIfT1zfP+g== From: "Aneesh Kumar K.V (Arm)" To: iommu@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-coco@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 , linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , "Christophe Leroy (CS GROUP)" , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , x86@kernel.org Subject: [PATCH v5 19/20] dma: free atomic pool pages by physical address Date: Fri, 22 May 2026 09:58:14 +0530 Message-ID: <20260522042815.370873-20-aneesh.kumar@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260522042815.370873-1-aneesh.kumar@kernel.org> References: <20260522042815.370873-1-aneesh.kumar@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260521_213212_492258_8B56973A X-CRM114-Status: GOOD ( 16.60 ) 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 dma_direct_alloc_pages() may satisfy atomic allocations from the coherent atomic pools. The pool allocation is keyed by the virtual address stored in the gen_pool, but the pages API returns only the backing struct page. On architectures with CONFIG_DMA_DIRECT_REMAP, atomic pool chunks are added to the gen_pool using their remapped virtual address. dma_direct_free_pages() reconstructs a linear-map address with page_address(page) and passes that to dma_free_from_pool(). That address does not match the gen_pool virtual range, so the pool lookup can fail and the code can fall through to freeing a pool-owned page through the normal page allocator path. Add a page-based pool free helper that looks up the owning pool chunk by physical address, translates it back to the gen_pool virtual address, and frees that address to the pool. Use it from dma_direct_free_pages() while keeping the existing virtual-address helper for coherent allocation frees. Signed-off-by: Aneesh Kumar K.V (Arm) --- include/linux/dma-map-ops.h | 1 + kernel/dma/direct.c | 4 +-- kernel/dma/pool.c | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 696b2c3a2305..8be059e69935 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -215,6 +215,7 @@ struct page *dma_alloc_from_pool(struct device *dev, size_t size, void **cpu_addr, gfp_t flags, unsigned long attrs, bool (*phys_addr_ok)(struct device *, phys_addr_t, size_t)); bool dma_free_from_pool(struct device *dev, void *start, size_t size); +bool dma_free_from_pool_page(struct device *dev, struct page *page, size_t size); int dma_direct_set_offset(struct device *dev, phys_addr_t cpu_start, dma_addr_t dma_start, u64 size); diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 907c6084c616..488d53ed21f3 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -488,9 +488,9 @@ void dma_direct_free_pages(struct device *dev, size_t size, */ bool mark_mem_encrypted = force_dma_unencrypted(dev); - /* If cpu_addr is not from an atomic pool, dma_free_from_pool() fails */ + /* If page is not from an atomic pool, dma_free_from_pool_page() fails */ if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) && - dma_free_from_pool(dev, vaddr, size)) + dma_free_from_pool_page(dev, page, size)) return; phys = page_to_phys(page); diff --git a/kernel/dma/pool.c b/kernel/dma/pool.c index e7df8d279e75..43b8101d860f 100644 --- a/kernel/dma/pool.c +++ b/kernel/dma/pool.c @@ -356,3 +356,57 @@ bool dma_free_from_pool(struct device *dev, void *start, size_t size) return false; } + +struct dma_pool_phys_match { + phys_addr_t phys; + size_t size; + unsigned long addr; + bool found; +}; + +static void dma_pool_find_phys(struct gen_pool *pool, struct gen_pool_chunk *chunk, + void *data) +{ + struct dma_pool_phys_match *match = data; + phys_addr_t end = match->phys + match->size - 1; + phys_addr_t chunk_end; + + if (match->found) + return; + + chunk_end = chunk->phys_addr + (chunk->end_addr - chunk->start_addr); + if (match->phys < chunk->phys_addr || end > chunk_end) + return; + + match->addr = chunk->start_addr + (match->phys - chunk->phys_addr); + match->found = true; +} + +static bool dma_free_from_pool_phys(struct dma_gen_pool *dma_pool, phys_addr_t phys, + size_t size) +{ + struct dma_pool_phys_match match = { + .phys = phys, + .size = size, + }; + + gen_pool_for_each_chunk(dma_pool->pool, dma_pool_find_phys, &match); + if (!match.found) + return false; + + gen_pool_free(dma_pool->pool, match.addr, size); + return true; +} + +bool dma_free_from_pool_page(struct device *dev, struct page *page, size_t size) +{ + struct dma_gen_pool *dma_pool = NULL; + phys_addr_t phys = page_to_phys(page); + + while ((dma_pool = dma_guess_pool(dma_pool, 0))) { + if (dma_free_from_pool_phys(dma_pool, phys, size)) + return true; + } + + return false; +} -- 2.43.0