Re: [PATCH v4 4/7] iommu: Switch __iommu_domain_alloc() to device ops

From: Jason Gunthorpe
Date: Mon Oct 02 2023 - 15:36:21 EST


On Mon, Oct 02, 2023 at 08:02:23PM +0100, Robin Murphy wrote:
> On 02/10/2023 3:16 pm, Jason Gunthorpe wrote:
> > On Mon, Oct 02, 2023 at 02:49:12PM +0100, Robin Murphy wrote:
> > > @@ -2120,20 +2120,30 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
> > > return domain;
> > > }
> > > -static struct iommu_domain *
> > > -__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
> > > +static int __iommu_domain_alloc_dev(struct device *dev, void *data)
> > > {
> >
> > Why?
>
> Because out of 3 callsites, two were in a context which now needed to
> make the iommu_group_first_dev() call itself anyway,

I don't see it. Why not just do this?

static int __iommu_domain_alloc_dev(struct device *dev, void *data)
{
/* FIXME: This is not correctly locked */
struct iommu_group *group = iommu_group_get(dev);
struct group **alloc_group = data;

if (!group)
return 0;

mutex_lock(&group->mutex);
/* Theoretically we could have raced against release */
if (list_empty(&group->devices)) {
mutex_unlock(&group->mutex);
iommu_group_put(group);
return 0;
}

*alloc_group = group;
return 1;
}

struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
{
struct iommu_group *alloc_group;
struct iommu_domain *dom;

/* Only one driver is supported with this interface */
if (WARN_ON(iommu_num_drivers > 1))
return NULL;

bus_for_each_dev(bus, NULL, &alloc_group, __iommu_domain_alloc_dev);
if (!alloc_group)
return NULL;
dom = __iommu_group_domain_alloc(alloc_group, IOMMU_DOMAIN_UNMANAGED);
mutex_unlock(&alloc_group->mutex);
iommu_group_put(alloc_group);
return dom;
}
EXPORT_SYMBOL_GPL(iommu_domain_alloc);

(and ++/-- iommu_num_drivers in iommu_device_register)

One patch, it's pretty easy???

> Um... Good? I mean in 3/4 cases it's literally the exact same code just
> factored out again, while the one I've added picks some arbitrary device
> in a different way.

Sure, but the whole point was to make it obvious that there was no
direct linkage from the various dev parameters we have in places and
what dev will be passed by the driver. Everything passes through
__iommu_group_domain_alloc() and at the end of the day that should be
the only allocation API.

Jason