Linux ARM-MSM sub-architecture
 help / color / mirror / Atom feed
* Re: [PATCH v2] iommu: Hold group references across bus_iommu_probe()
       [not found] ` <179520803625.1164516.5819106673711321760.b4-review@b4>
@ 2026-11-24  7:09   ` Peter Chen
  2026-11-24 12:49     ` Jason Gunthorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Chen @ 2026-11-24  7:09 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Kevin Tian, Lu Baolu,
	Johan Hovold, Fenglin Wu, iommu, linux-kernel, stable,
	linux-arm-msm, Jack Pham, Wesley Cheng

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] iommu: Hold group references across bus_iommu_probe()
  2026-11-24  7:09   ` [PATCH v2] iommu: Hold group references across bus_iommu_probe() Peter Chen
@ 2026-11-24 12:49     ` Jason Gunthorpe
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Gunthorpe @ 2026-11-24 12:49 UTC (permalink / raw)
  To: Peter Chen
  Cc: Joerg Roedel, Will Deacon, Robin Murphy, Kevin Tian, Lu Baolu,
	Johan Hovold, Fenglin Wu, iommu, linux-kernel, stable,
	linux-arm-msm, Jack Pham, Wesley Cheng

On Mon, Nov 23, 2026 at 11:09:03PM -0800, Peter Chen wrote:
> 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() */
> 	}

That's not how EPROBE_DEFER is supposed to be handled :(

The driver core is already setup to reprobe the child driver later
inside of_platform_populate()

Your issue is it is quite wrong to try to synchronously reach into a
probed driver's like this from outside. Then when it fails tearing
down the device means you defeat the natural EPROBE_DEFER.

This should be asynchronous, then you won't have ordering problems.

But OK, it is clear how you get into this situation.

> > > @@ -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);

A small comment might help. The lock seems necessary

Jason

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 12:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261112110747.1903321-1-peter.chen@oss.qualcomm.com>
     [not found] ` <179520803625.1164516.5819106673711321760.b4-review@b4>
2026-11-24  7:09   ` [PATCH v2] iommu: Hold group references across bus_iommu_probe() Peter Chen
2026-11-24 12:49     ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox