mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Chen <peter.chen@kernel.org>
To: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Lu Baolu <baolu.lu@linux.intel.com>,
	Johan Hovold <johan@kernel.org>,
	Fenglin Wu <fenglin.wu@oss.qualcomm.com>,
	Wesley Cheng <wesley.cheng@oss.qualcomm.com>,
	Jack Pham <jack.pham@oss.qualcomm.com>,
	Udipto Goswami <udipto.goswami@oss.qualcomm.com>,
	Pratham Pratap <pratham.pratap@oss.qualcomm.com>,
	Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-arm-msm@vger.kernel.org,
	Peter Chen <peter.chen@oss.qualcomm.com>,
	stable@vger.kernel.org
Subject: [PATCH v3] iommu: Hold group references across bus_iommu_probe()
Date: Sun, 29 Nov 2026 23:16:06 -0800	[thread overview]
Message-ID: <20261130071606.290149-1-peter.chen@kernel.org> (raw)

From: Peter Chen <peter.chen@oss.qualcomm.com>

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 <peter.chen@oss.qualcomm.com>
---
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


                 reply	other threads:[~2026-11-30  7:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130071606.290149-1-peter.chen@kernel.org \
    --to=peter.chen@kernel.org \
    --cc=baolu.lu@linux.intel.com \
    --cc=fenglin.wu@oss.qualcomm.com \
    --cc=iommu@lists.linux.dev \
    --cc=jack.pham@oss.qualcomm.com \
    --cc=jgg@nvidia.com \
    --cc=johan@kernel.org \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=krishna.kurapati@oss.qualcomm.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.chen@oss.qualcomm.com \
    --cc=pratham.pratap@oss.qualcomm.com \
    --cc=robin.murphy@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=udipto.goswami@oss.qualcomm.com \
    --cc=wesley.cheng@oss.qualcomm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®