[PATCH v4 08/17] iommufd: Always setup MSI and anforce cc on kernel-managed domains

From: Yi Liu
Date: Thu Sep 21 2023 - 16:28:05 EST


From: Nicolin Chen <nicolinc@xxxxxxxxxx>

Now enforce_cache_coherency and msi_cookie are kernel-managed hwpt things.
So, they should be only setup on kernel-managed domains. If the attaching
domain is a user-managed domain, redirect the hwpt to hwpt->parent to do
it correctly.

Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
Co-developed-by: Yi Liu <yi.l.liu@xxxxxxxxx>
Signed-off-by: Yi Liu <yi.l.liu@xxxxxxxxx>
---
drivers/iommu/iommufd/device.c | 4 ++++
drivers/iommu/iommufd/hw_pagetable.c | 4 ++++
2 files changed, 8 insertions(+)

diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index eb120f70a3e3..104dd061a2a3 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -305,12 +305,16 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
* domain after request_irq(). If it is not done interrupts will not
* work on this domain.
*
+ * Note: always set up a msi_cookie on a kernel-manage hw_pagetable.
+ *
* FIXME: This is conceptually broken for iommufd since we want to allow
* userspace to change the domains, eg switch from an identity IOAS to a
* DMA IOAS. There is currently no way to create a MSI window that
* matches what the IRQ layer actually expects in a newly created
* domain.
*/
+ if (hwpt->user_managed)
+ hwpt = hwpt->parent;
if (sw_msi_start != PHYS_ADDR_MAX && !hwpt->msi_cookie) {
rc = iommu_get_msi_cookie(hwpt->domain, sw_msi_start);
if (rc)
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index dc3e11a23acf..90fd65859e28 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -152,6 +152,10 @@ iommufd_user_managed_hwpt_alloc(struct iommufd_ctx *ictx,

int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt)
{
+ /* Always enforce cache coherency on a kernel-managed hw_pagetable */
+ if (hwpt->user_managed)
+ hwpt = hwpt->parent;
+
if (hwpt->enforce_cache_coherency)
return 0;

--
2.34.1