Re: [PATCH v3 1/3] iommu: Sort out domain user data

From: Nicolin Chen
Date: Thu Mar 06 2025 - 15:10:44 EST


On Thu, Mar 06, 2025 at 05:59:03AM +0000, Tian, Kevin wrote:
> > From: Nicolin Chen <nicolinc@xxxxxxxxxx>
> > Sent: Tuesday, March 4, 2025 4:53 AM
> >
> > From: Robin Murphy <robin.murphy@xxxxxxx>
> >
> > When DMA/MSI cookies were made first-class citizens back in commit
> > 46983fcd67ac ("iommu: Pull IOVA cookie management into the core"), there
> > was no real need to further expose the two different cookie types.
> > However, now that IOMMUFD wants to add a third type of MSI-mapping
> > cookie, we do have a nicely compelling reason to properly dismabiguate
> > things at the domain level beyond just vaguely guessing from the domain
> > type.
> >
> > Meanwhile, we also effectively have another "cookie" in the form of the
> > anonymous union for other user data, which isn't much better in terms of
> > being vague and unenforced. The fact is that all these cookie types are
> > mutually exclusive, in the sense that combining them makes zero sense
> > and/or would be catastrophic (iommu_set_fault_handler() on an SVA
> > domain, anyone?) - the only combination which *might* be reasonable is
> > perhaps a fault handler and an MSI cookie, but nobody's doing that at
> > the moment, so let's rule it out as well for the sake of being clear and
> > robust. To that end, we pull DMA and MSI cookies apart a little more,
> > mostly to clear up the ambiguity at domain teardown, then for clarity
> > (and to save a little space), move them into the union, whose ownership
> > we can then properly describe and enforce entirely unambiguously.
> >
> > Signed-off-by: Robin Murphy <robin.murphy@xxxxxxx>
> > [nicolinc: rebase on latest tree; use prefix IOMMU_COOKIE_; merge unions
> > in iommu_domain; add IOMMU_COOKIE_IOMMUFD for
> > iommufd_hwpt]
> > Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
>
> Reviewed-by: Kevin Tian <kevin.tian@xxxxxxxxx>
>
> with a nit:
>
> > msi_addr &= ~(phys_addr_t)(size - 1);
> > - list_for_each_entry(msi_page, &cookie->msi_page_list, list)
> > + list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
> > if (msi_page->phys == msi_addr)
> > return msi_page;
> >
>
> Above checks cookie type in every iteration. Better save the list
> pointer to a local variable.

Ack.

--------------------------------------------------------------
@@ -1793,2 +1793,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
{
+ struct list_head *msi_page_list = cookie_msi_pages(domain);
struct iommu_dma_msi_page *msi_page;
@@ -1799,3 +1800,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
msi_addr &= ~(phys_addr_t)(size - 1);
- list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
+ list_for_each_entry(msi_page, msi_page_list, list)
if (msi_page->phys == msi_addr)
@@ -1817,3 +1818,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
msi_page->iova = iova;
- list_add(&msi_page->list, cookie_msi_pages(domain));
+ list_add(&msi_page->list, msi_page_list);
return msi_page;
--------------------------------------------------------------

Thanks
Nicolin