From: Peter Chen <peter.chen@oss.qualcomm.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Robin Murphy <robin.murphy@arm.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>,
iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, linux-arm-msm@vger.kernel.org,
Jack Pham <jack.pham@oss.qualcomm.com>,
Wesley Cheng <wesley.cheng@oss.qualcomm.com>
Subject: Re: [PATCH v2] iommu: Hold group references across bus_iommu_probe()
Date: Mon, 23 Nov 2026 23:09:03 -0800 [thread overview]
Message-ID: <awU4DyS7irT1a-nu@hu-petche-lv.qualcomm.com> (raw)
In-Reply-To: <179520803625.1164516.5819106673711321760.b4-review@b4>
On 26-11-20 16:53:56, Jason Gunthorpe wrote:
> > [ ... 9 lines skipped ... ]
> > 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. The sequence is:
> >
> > CPU0 (driver's of_platform_populate / of_platform_depopulate() path)
> > CPU1 (arm_smmu_device_probe() -> iommu_device_register())
> >
> > CPU0 CPU1
> > of_platform_device_create(T)
> > bus_iommu_probe()
> > __iommu_probe_device(T)
> > allocates group X,
> > queues X on group_list
> > 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
>
> This seems odd, like shouldn't something outside the iommu subsystem
> be serializing these two things? If some driver is calling
> of_platform_device_create() and racing it with
> platform_device_unregister() it is broken, IMHO.
>
> Can you explain more how this arises in the first place?
Hi Jason,
Thanks for reviewing.
It is not one driver racing create against unregister. It is the usual
glue/core pattern: the parent populates the child, and if the child is
not bound (due to IOMMU is not ready at this case) yet, it depopulates
and retries later. That runs at the same time as an unrelated IOMMU
instance registering.
We hit it on a Qualcomm SoC (Android 6.18 kernel), with a DT like this:
usb@a600000 { /* glue */
compatible = "qcom,...";
...
usb@a600000 { /* dwc3 core */
compatible = "snps,dwc3";
iommus = <&apps_smmu 0x40 0x0>;
};
};
The glue driver does what upstream dwc3-imx8mp.c does in
dwc3_imx8mp_probe():
of_platform_populate(node, NULL, NULL, dev);
pdev = of_find_device_by_node(dwc3_np);
dwc = platform_get_drvdata(pdev);
if (!dwc) {
/* core probe deferred, e.g. its SMMU is not there yet */
ret = -EPROBE_DEFER;
goto depopulate; /* -> platform_device_unregister() */
}
The dwc3 core probe defers because apps_smmu has not registered yet,
so the glue depopulates the child. Meanwhile the SMMU driver probes
from the deferred probe work and calls iommu_device_register():
glue (kworker) SMMU probe (deferred_probe_work)
-------------- --------------------------------
of_platform_populate()
device_add(T) /* dwc3 core */
T's probe -> -EPROBE_DEFER
iommu_device_register()
bus_iommu_probe(platform)
__iommu_probe_device(T)
dma_configure replay finds
the SMMU, allocates group G,
adds G to group_list
platform_get_drvdata(T) == NULL
of_platform_depopulate()
platform_device_unregister(T)
BUS_NOTIFY_REMOVED_DEVICE
iommu_release_device(T)
last ref on G dropped, G freed
list_for_each_entry_safe(G...)
mutex_lock(&G->mutex) <- UAF
The two sides only share the platform bus. Neither driver knows about
the other, so I don't see a lock outside the IOMMU core that could
order them. The glue does not know the SMMU is in the middle of
registering, and the SMMU driver has no relationship to the USB
device. Device add/remove can happen at any time while
bus_for_each_dev() runs, and that is why the core has
iommu_bus_notifier(). Commit da33e87bd2bf already handles this
notifier running during bus_iommu_probe(). This patch closes the
remaining hole, where a group on the on-stack list loses its last
device and gets freed.
I will put this real-world scenario into the commit message for v3.
>
> > @@ -1951,6 +1955,18 @@ 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;
> > +
> > + 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);
>
> What is the mutex for? The group_list is a stack head so what are we
> protecting list del against?
>
Yes, the list head is on the stack, but group->entry is in the
shared group. "Is group->entry empty" is the de-dup flag that
__iommu_probe_device() tests and sets under group->mutex:
if (list_empty(&group->entry))
list_add_tail(&group->entry, group_list);
bus_iommu_probe() can run concurrently for different IOMMU instances,
since only the per-device probe is serialised by
iommu_probe_device_lock. So the unlink and re-init is done under
group->mutex to make it atomic with respect to that check-and-add,
the same as the existing loop in bus_iommu_probe().
It is only on the error path, the cost is little, so I'd prefer to
keep it. If you think that concurrency can't happen in practice,
I'm fine dropping it in v3.
--
Thanks,
Peter Chen
prev parent reply other threads:[~2026-11-24 7:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 11:07 Peter Chen
2026-11-20 20:53 ` Jason Gunthorpe
2026-11-24 7:09 ` Peter Chen [this message]
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=awU4DyS7irT1a-nu@hu-petche-lv.qualcomm.com \
--to=peter.chen@oss.qualcomm.com \
--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=linux-arm-msm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=robin.murphy@arm.com \
--cc=stable@vger.kernel.org \
--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®