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=1796022972; b=vgpxDsTRjYOH0XFIrh5kZoqXin897QdnxZ80LyaVXI43fpWcZUIMuuDpeqfQbb9bKtWU LJNjGUnBCE08BRxFNEMAPg1FWdemKg+UY8kCJGFoQIeIvqjj97wkQT9toF1ltUXmEpTkn ijjMKCZenI03i32+Gcl6QqPzjb5PSR6wIWpujOLnzskWpg97b29NpGAcoUv2hICJaopah LjbIeKf2e+5U0vMTT/DJNds7Ec9FCp1CekXu1vFu0LGDgAYNBwuI7A1k6MaeRzRV8l2Er mXo5pkD4WbM24e0krBcrGfkHSr17BfRQY/G10NcvCC+3VHcXGdMSzZV7EVJ8gTURR5Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796022972; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=Z1CvIlQ55BBEOufUhefNYk5yikq5dVV5Wk9qSjk+grs=; b=5Vmas2bIydRNch2MXPlkX8WataDUXcq22KE8TdFqUEarCQneUszh+oQ4UL+4EL1vp7QW Gw+gryyoAUz8aZiMnt6fALvUcf5nVTR039JXdVyjxMPmEFTIwezhDlF25f7utpwSJia9+ LJfM519WK00SuI+gn4ND6AAU1W0TpCxpjGTkus+r46ceXuqWGw6jGyG4b7K0gGhQOqioy AhTKK98Khghg5ZLFC6vf8OjnzJMHlx2+8fZeoJ73xWtxbPntNqaHOiMkt42dWmJWu1OiW H8ePOFJHqUcsOBxJ6G8tO+vV6acqwBM0EJ73Gpionicx0GIcd3hLI0a8+6Jj1hhqa+w== 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=cmtrgqO0; 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 51B47606A31C; Mon, 30 Nov 2026 07:16:12 +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=cmtrgqO0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55BCE1F00897; Mon, 30 Nov 2026 07:16:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796022971; bh=Z1CvIlQ55BBEOufUhefNYk5yikq5dVV5Wk9qSjk+grs=; h=From:To:Cc:Subject:Date; b=cmtrgqO0OQpX58LIQx3UzikjJZR1H6IIRguYmpBt32jSAUkPogBef3XFLHQDOZYaq VuPtAKMd8idew34lhzG7TEV9Iv20ovfiNNd1M6bKKh7aVny7KTKOMIcNI2a9wWtLo0 HE29gWRN4Ii0wXWvPRJemlIE/8DMaesH4h432LJi4x+3DY9zAKYnTPhEQTbFWpu6WZ broCWBTnKzWsdM8aHT5074EY/aMrvzNaGiam71aXQIOiEo84+LfAweayg5jHTY3vTk +ZMsP6sgTi3RmgifqMhW8nEXFPlXeyLIn0LDUdG4WjWTNbSc6zUOwJmqtr8lpQn+S2 HpFBSxMdWKikg== From: Peter Chen To: Joerg Roedel , Will Deacon Cc: Robin Murphy , Jason Gunthorpe , Kevin Tian , Lu Baolu , Johan Hovold , Fenglin Wu , Wesley Cheng , Jack Pham , Udipto Goswami , Pratham Pratap , Krishna Kurapati , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Peter Chen , stable@vger.kernel.org Subject: [PATCH v3] iommu: Hold group references across bus_iommu_probe() Date: Sun, 29 Nov 2026 23:16:06 -0800 Message-ID: <20261130071606.290149-1-peter.chen@kernel.org> X-Mailer: git-send-email 2.43.0 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Peter Chen bus_iommu_probe() collects the groups that are linked through group->entry, but the list does not hold a reference on them. Only the member devices do. A device of such a group may be released while the bus walk is still in progress. If the last iommu_group_put() then frees the group while it is still linked on the list, the second loop of bus_iommu_probe() walks and unlinks freed memory. This is the "iommu_bus_notifier() being triggered during bus_iommu_probe()" case that commit da33e87bd2bf ("iommu: Handle yet another race around registration") left open. It was hit with a downstream Qualcomm device driver. The core is a child node of the glue node, and the child references the SMMU: parent@ax00000 { /* glue */ ... child@ax00000 { /* core */ compatible = "xxx,xxx"; iommus = <&apps_smmu 0x40 0x0>; }; }; The parent populates the child, and if the child is not bound yet, it depopulates it again, the same as upstream dwc3_imx8mp_probe() does. The child's probe defers because the SMMU has not registered yet. At the same time the SMMU driver probes from the deferred probe work and registers, and its bus walk picks up the child that is about to be removed: glue SMMU (deferred_probe_work) of_platform_populate() device_add(T) T's probe -> -EPROBE_DEFER iommu_device_register() bus_iommu_probe() __iommu_probe_device(T) allocates group X, queues X on group_list of_platform_depopulate() platform_device_unregister(T) iommu_bus_notifier() __iommu_group_remove_device(T) iommu_group_put(X) -> kfree(X) list_del_init(&X->entry) <- use-after-free The kernel dump is like below: list_del corruption. next->prev should be ffffff8064a984e8, but was 0000000000000000. (next=ffffff8064a9a6e8) kernel BUG at lib/list_debug.c:67! Call trace: __list_del_entry_valid_or_report+0x148/0x14c (P) iommu_device_register+0x1d8/0x268 arm_smmu_device_probe+0x588/0x624 [arm_smmu] platform_probe+0x74/0xb8 ... deferred_probe_work_func+0xa4/0xf4 where next points at ->entry (offset 232) of a freed kmalloc-512 object, the released iommu_group of the depopulated device. That was seen on an Android 6.18-based kernel, but the code involved is unchanged in mainline. Take a group reference when a group is added to the list, and drop it once bus_iommu_probe() is done with the group. With the group kept alive, a group that has lost all of its devices in the meantime can simply be skipped: there is nothing left to attach a default domain to. Also drain the list on the error paths. Returning early used to leave the remaining groups linked to the dead on-stack list head, so list_empty(&group->entry) stays false for them and a later bus_iommu_probe() would never queue them for default domain setup again. Fixes: 41df6dcc0a3f ("iommu: Keep a list of allocated groups in __iommu_probe_device()") Cc: stable@vger.kernel.org Signed-off-by: Peter Chen --- Changes in v3: - Describe the real-world scenario (glue driver populating and depopulating a child whose probe defers on the SMMU) in the commit message. (Jason) - Add a comment in iommu_group_list_put() explaining why group->entry is reset under group->mutex. (Jason) - v2: https://lore.kernel.org/all/20261112110747.1903321-1-peter.chen@oss.qualcomm.com/ Changes in v2: - Replace the step list with a race diagram in the commit message. - Add Cc: stable. - Rebase on v7.4-rc1, no code change. - Delete the LLM Assisted-by tag. - v1: https://lore.kernel.org/all/20260929070737.3798358-1-peter.chen@oss.qualcomm.com/ --- drivers/iommu/iommu.c | 46 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index b486b8bbd1fc..b2e50b2c1cbe 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -682,10 +682,14 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list /* * With a group_list argument we defer the default_domain setup * to the caller by providing a de-duplicated list of groups - * that need further setup. + * that need further setup. The list holds a reference on each + * group, as its devices may be released before the caller + * gets to it. */ - if (list_empty(&group->entry)) + if (list_empty(&group->entry)) { + iommu_group_ref_get(group); list_add_tail(&group->entry, group_list); + } } if (group->default_domain) @@ -1951,6 +1955,23 @@ static void iommu_group_do_probe_finalize(struct device *dev) ops->probe_finalize(dev); } +static void iommu_group_list_put(struct list_head *group_list) +{ + struct iommu_group *group, *next; + + /* + * group->entry being empty is the de-dup flag tested and set by + * __iommu_probe_device() under group->mutex, and bus_iommu_probe() + * may run concurrently for other IOMMU instances. + */ + list_for_each_entry_safe(group, next, group_list, entry) { + mutex_lock(&group->mutex); + list_del_init(&group->entry); + mutex_unlock(&group->mutex); + iommu_group_put(group); + } +} + static int bus_iommu_probe(const struct bus_type *bus) { struct iommu_group *group, *next; @@ -1959,7 +1980,7 @@ static int bus_iommu_probe(const struct bus_type *bus) ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group); if (ret) - return ret; + goto err_put_groups; list_for_each_entry_safe(group, next, &group_list, entry) { struct group_device *gdev; @@ -1969,6 +1990,16 @@ static int bus_iommu_probe(const struct bus_type *bus) /* Remove item from the list */ list_del_init(&group->entry); + /* + * The bus notifier may have released every device of the group + * since it was added to the list; there is nothing to set up. + */ + if (list_empty(&group->devices)) { + mutex_unlock(&group->mutex); + iommu_group_put(group); + continue; + } + /* * We go to the trouble of deferred default domain creation so * that the cross-group default domain type and the setup of the @@ -1977,7 +2008,8 @@ static int bus_iommu_probe(const struct bus_type *bus) ret = iommu_setup_default_domain(group, 0); if (ret) { mutex_unlock(&group->mutex); - return ret; + iommu_group_put(group); + goto err_put_groups; } for_each_group_device(group, gdev) iommu_setup_dma_ops(gdev->dev, group->default_domain); @@ -1991,9 +2023,15 @@ static int bus_iommu_probe(const struct bus_type *bus) */ for_each_group_device(group, gdev) iommu_group_do_probe_finalize(gdev->dev); + + iommu_group_put(group); } return 0; + +err_put_groups: + iommu_group_list_put(&group_list); + return ret; } /** base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 -- 2.43.0