Re: [PATCH v2 7/7] iommu/riscv: Paging domain support

From: Jason Gunthorpe
Date: Mon Apr 22 2024 - 15:31:16 EST


On Mon, Apr 22, 2024 at 01:21:05PM +0800, Baolu Lu wrote:
> > + /* Track domain to devices mapping. */
> > + if (bond)
> > + list_add_rcu(&bond->list, &domain->bonds);
> > +
> > + /* Remove tracking from previous domain, if needed. */
> > + iommu_domain = iommu_get_domain_for_dev(dev);
>
> Calling iommu_get_domain_for_dev() in the domain attaching path is very
> fragile because it heavily depends on the order of calling the attach
> callback and setting the domain pointer in the core.

We have a couple places doing this already, the core code accomodates
it well enough for deleting from a list.. So I think it is OK to keep
doing.

> Perhaps the driver can use dev_iommu_priv_set/get() to keep the active
> domain in the per-device private data?
>
> > + if (iommu_domain && !!(iommu_domain->type & __IOMMU_DOMAIN_PAGING)) {
> > + domain = iommu_domain_to_riscv(iommu_domain);
> > + bond = NULL;
> > + rcu_read_lock();
> > + list_for_each_entry_rcu(b, &domain->bonds, list) {
> > + if (b->dev == dev) {
> > + bond = b;
> > + break;
> > + }
> > + }
> > + rcu_read_unlock();

But now that I look again, this is not safe, you have to hold some
kind of per-domain lock to mutate the list. rcu_*read*_lock() cannot
be used for write.

Jason