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 lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 D4250CD4F5B for ; Fri, 22 May 2026 04:31:53 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4gMC6J44Szz3bsm; Fri, 22 May 2026 14:31:52 +1000 (AEST) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2600:3c0a:e001:78e:0:1991:8:25" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1779424312; cv=none; b=B70qgoHGIxrlIFJRpeJUxvlFFZrPFTBXQYit6i6TsnQSFVDk3ovCk8YG3sF2kDxoHpyASXsKUs0z32xMLbbO+AOxjVrlOeoWHJ0U22aSvrIIwOwPgvcWreYO58Bwx8Y+dSBiiAhjE6Aj2Lxh7C3XQi8y+3ooiGBqzF7c8aJXaEGLyq4VXBWXLXBqzTHskOIYxr5GTm6YNLQ5mlkRm8ldDc8ynUBH6mU2m+tLQsPGfLFWa9lFdwYGHXd3UYdbmE7rToAVUao4MAnS71x/nM/BAD8dRiWizrl7pSCsOPDR6//W36YDaF1jTV9BXral0WzKY2fvOYZpvy7hP7VUZ46SGw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1779424312; c=relaxed/relaxed; bh=dcmTXB2lgA7G/MmVc6Lgh462v2J6ii5qK8aSjHsJld8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EaACWRxloP4GGSeifQWCFr5M2wPkGAkf0kb3VbKYyVsNq/Q72y+Siv4fjHlUi+BfhtaAB8Nh35mNuqBn4rvtzU6M4s36K4Aa5l/NVN5eIt2bs/9RM0DCJLiLb45EsRku5YQKltYZ8zni/4seESiP/2IsXha3DDkU6fo6TxJsiLUC5o5gihM3EO9Ul+O+VXMTLMJM6oOEs8fhxk+qkQgC69KUmr0f4gldyFMog9kQF8Jf0jXnL16Al1622uPDndc6+LdUHRkZwAPpqdWk6z/P7jhxWzkJzzsT8GvVgLZ/EpHT2FSzycWJETlUy1zW384VX6eyuQmtlhVVzJKe6RS0PQ== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=j/xCchvP; dkim-atps=neutral; spf=pass (client-ip=2600:3c0a:e001:78e:0:1991:8:25; helo=sea.source.kernel.org; envelope-from=aneesh.kumar@kernel.org; receiver=lists.ozlabs.org) smtp.mailfrom=kernel.org Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=j/xCchvP; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=kernel.org (client-ip=2600:3c0a:e001:78e:0:1991:8:25; helo=sea.source.kernel.org; envelope-from=aneesh.kumar@kernel.org; receiver=lists.ozlabs.org) Received: from sea.source.kernel.org (sea.source.kernel.org [IPv6:2600:3c0a:e001:78e:0:1991:8:25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4gMC6H5Sxqz2yS4 for ; Fri, 22 May 2026 14:31:51 +1000 (AEST) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id BA30243272; Fri, 22 May 2026 04:31:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 490C41F000E9; Fri, 22 May 2026 04:31:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1779424309; bh=dcmTXB2lgA7G/MmVc6Lgh462v2J6ii5qK8aSjHsJld8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=j/xCchvPe4MfoXPQoR0SqW+SV/OSZQxiq1dm+OogpuMy4jIINMhXlPkIKXBsWNG4c chVAiBxy6kb/TGkLAS9BamKfkaYocokS55O+vAgutoYZIqTQcH4oXDc8Jwgmdtjma3 N1npDpLRGqks29WjG463px4akkaCHmNVVYHX+BHd3d9NsapQWMTAamL8ojxeT6jVWH m9y7yi/fPo6zoTg5kevEsUm+EVB6qCusnExZ6C+3mcLrAXFbYDNtuThai//rRZLtiK Ohv8iSA61InNcTuKe4hQ6rhYHHCEnX2We4GjLcNMOFfdLGd1+7zAwTnAZOW49XJhsy zu/mI96MORqvA== 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 17/20] dma: swiotlb: free dynamic pools from process context Date: Fri, 22 May 2026 09:58:12 +0530 Message-ID: <20260522042815.370873-18-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> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit swiotlb_dyn_free() is used after removing a dynamic swiotlb pool from RCU-protected lists. It can call swiotlb_free_tlb(), which may need to restore the encryption state of an unencrypted pool with set_memory_encrypted() before freeing the pages. RCU callbacks run in atomic context, but set_memory_encrypted() is not guaranteed to be atomic-safe on all architectures. For example, page attribute updates may allocate page tables or take sleeping locks. Use queue_rcu_work() for dynamic pool freeing instead. This keeps the RCU grace period before freeing a published pool, while running the actual pool teardown from workqueue context. Use the same helper for the transient-pool error path, since that path may also be reached from atomic DMA mapping context. Signed-off-by: Aneesh Kumar K.V (Arm) --- include/linux/swiotlb.h | 4 ++-- kernel/dma/swiotlb.c | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index 4dcbf3931be1..526f82e9da45 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -64,7 +64,7 @@ extern void __init swiotlb_update_mem_attributes(void); * @areas: Array of memory area descriptors. * @slots: Array of slot descriptors. * @node: Member of the IO TLB memory pool list. - * @rcu: RCU head for swiotlb_dyn_free(). + * @dyn_free: RCU work item used to free the pool from process context. * @transient: %true if transient memory pool. */ struct io_tlb_pool { @@ -79,7 +79,7 @@ struct io_tlb_pool { struct io_tlb_slot *slots; #ifdef CONFIG_SWIOTLB_DYNAMIC struct list_head node; - struct rcu_head rcu; + struct rcu_work dyn_free; bool transient; bool unencrypted; #endif diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index f4e8b241a1c4..4c56f64602ea 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -774,13 +774,10 @@ static void swiotlb_dyn_alloc(struct work_struct *work) add_mem_pool(mem, pool); } -/** - * swiotlb_dyn_free() - RCU callback to free a memory pool - * @rcu: RCU head in the corresponding struct io_tlb_pool. - */ -static void swiotlb_dyn_free(struct rcu_head *rcu) +static void swiotlb_dyn_free_work(struct work_struct *work) { - struct io_tlb_pool *pool = container_of(rcu, struct io_tlb_pool, rcu); + struct io_tlb_pool *pool = + container_of(to_rcu_work(work), struct io_tlb_pool, dyn_free); size_t slots_size = array_size(sizeof(*pool->slots), pool->nslabs); size_t tlb_size = pool->end - pool->start; @@ -789,6 +786,12 @@ static void swiotlb_dyn_free(struct rcu_head *rcu) kfree(pool); } +static void swiotlb_schedule_dyn_free(struct io_tlb_pool *pool) +{ + INIT_RCU_WORK(&pool->dyn_free, swiotlb_dyn_free_work); + queue_rcu_work(system_wq, &pool->dyn_free); +} + /** * __swiotlb_find_pool() - find the IO TLB pool for a physical address * @dev: Device which has mapped the DMA buffer. @@ -835,7 +838,7 @@ static void swiotlb_del_pool(struct device *dev, struct io_tlb_pool *pool) list_del_rcu(&pool->node); spin_unlock_irqrestore(&dev->dma_io_tlb_lock, flags); - call_rcu(&pool->rcu, swiotlb_dyn_free); + swiotlb_schedule_dyn_free(pool); } #endif /* CONFIG_SWIOTLB_DYNAMIC */ @@ -1276,7 +1279,7 @@ static int swiotlb_find_slots(struct device *dev, phys_addr_t orig_addr, index = swiotlb_search_pool_area(dev, pool, 0, orig_addr, tbl_dma_addr, alloc_size, alloc_align_mask); if (index < 0) { - swiotlb_dyn_free(&pool->rcu); + swiotlb_schedule_dyn_free(pool); return -1; } -- 2.43.0