[PATCH v2 13/19] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on

Zhenzhong Duan posted 19 patches 4 months, 4 weeks ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowang@redhat.com>, Yi Liu <yi.l.liu@intel.com>, "Clément Mathieu--Drif" <clement.mathieu--drif@eviden.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Eduardo Habkost <eduardo@habkost.net>, Alex Williamson <alex.williamson@redhat.com>, "Cédric Le Goater" <clg@redhat.com>, Eric Auger <eric.auger@redhat.com>, Zhenzhong Duan <zhenzhong.duan@intel.com>
There is a newer version of this series
[PATCH v2 13/19] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on
Posted by Zhenzhong Duan 4 months, 4 weeks ago
When guest in scalable mode and x-flts=on, we stick to system MR for IOMMUFD
backed host device. Then its default hwpt contains GPA->HPA mappings which is
used directly if PGTT=PT and used as nested parent if PGTT=FLT. Otherwise
fallback to original processing.

Suggested-by: Yi Liu <yi.l.liu@intel.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
---
 hw/i386/intel_iommu.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index ed71bb8ec7..be01f8885f 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -1779,6 +1779,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
              */
             return false;
         }
+
         return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
     }
 
@@ -1790,10 +1791,33 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 {
     IntelIOMMUState *s;
     VTDContextEntry ce;
+    struct vtd_as_key key = {
+        .bus = as->bus,
+        .devfn = as->devfn,
+    };
 
     assert(as);
 
     s = as->iommu_state;
+
+    /*
+     * When guest in scalable mode and x-flts=on, we stick to system MR
+     * for IOMMUFD backed host device. Then its default hwpt contains
+     * GPA->HPA mappings which is used directly if PGTT=PT and used as
+     * nested parent if PGTT=FLT. Otherwise fallback to original
+     * processing.
+     */
+     if (s->root_scalable && s->flts) {
+        VTDHostIOMMUDevice *vtd_hiod;
+
+        vtd_hiod = g_hash_table_lookup(s->vtd_host_iommu_dev, &key);
+        if (vtd_hiod && vtd_hiod->hiod &&
+            object_dynamic_cast(OBJECT(vtd_hiod->hiod),
+                                TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
+            return true;
+        }
+    }
+
     if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
                                  &ce)) {
         /*
-- 
2.34.1
Re: [PATCH v2 13/19] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on
Posted by Eric Auger 4 months, 3 weeks ago

On 6/20/25 9:18 AM, Zhenzhong Duan wrote:
> When guest in scalable mode and x-flts=on, we stick to system MR for IOMMUFD
when guest works in scalable mode ?
> backed host device. Then its default hwpt contains GPA->HPA mappings which is
> used directly if PGTT=PT and used as nested parent if PGTT=FLT. Otherwise
> fallback to original processing.
>
> Suggested-by: Yi Liu <yi.l.liu@intel.com>
> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
> ---
>  hw/i386/intel_iommu.c | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
>
> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
> index ed71bb8ec7..be01f8885f 100644
> --- a/hw/i386/intel_iommu.c
> +++ b/hw/i386/intel_iommu.c
> @@ -1779,6 +1779,7 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
>               */
>              return false;
>          }
> +
spurious new line
>          return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
>      }
>  
> @@ -1790,10 +1791,33 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
>  {
>      IntelIOMMUState *s;
>      VTDContextEntry ce;
> +    struct vtd_as_key key = {
> +        .bus = as->bus,
> +        .devfn = as->devfn,
> +    };
>  
>      assert(as);
>  
>      s = as->iommu_state;
> +
> +    /*
> +     * When guest in scalable mode and x-flts=on, we stick to system MR
ditto
> +     * for IOMMUFD backed host device. Then its default hwpt contains
> +     * GPA->HPA mappings which is used directly if PGTT=PT and used as
> +     * nested parent if PGTT=FLT. Otherwise fallback to original
fall back here and above

This comment sounds a little bit unrelated to the below implementation
which does not refer to system MR. how does the search for the hiod
relate to that. I would refocus the comment.
> +     * processing.
> +     */
> +     if (s->root_scalable && s->flts) {
> +        VTDHostIOMMUDevice *vtd_hiod;
> +
> +        vtd_hiod = g_hash_table_lookup(s->vtd_host_iommu_dev, &key);
> +        if (vtd_hiod && vtd_hiod->hiod &&
> +            object_dynamic_cast(OBJECT(vtd_hiod->hiod),
> +                                TYPE_HOST_IOMMU_DEVICE_IOMMUFD)) {
> +            return true;
> +        }
> +    }
> +
>      if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn,
>                                   &ce)) {
>          /*
Eric
RE: [PATCH v2 13/19] intel_iommu: Stick to system MR for IOMMUFD backed host device when x-fls=on
Posted by Duan, Zhenzhong 4 months, 3 weeks ago

>-----Original Message-----
>From: Eric Auger <eric.auger@redhat.com>
>Subject: Re: [PATCH v2 13/19] intel_iommu: Stick to system MR for
>IOMMUFD backed host device when x-fls=on
>
>
>
>On 6/20/25 9:18 AM, Zhenzhong Duan wrote:
>> When guest in scalable mode and x-flts=on, we stick to system MR for
>IOMMUFD
>when guest works in scalable mode ?
>> backed host device. Then its default hwpt contains GPA->HPA mappings
>which is
>> used directly if PGTT=PT and used as nested parent if PGTT=FLT. Otherwise
>> fallback to original processing.
>>
>> Suggested-by: Yi Liu <yi.l.liu@intel.com>
>> Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
>> ---
>>  hw/i386/intel_iommu.c | 24 ++++++++++++++++++++++++
>>  1 file changed, 24 insertions(+)
>>
>> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
>> index ed71bb8ec7..be01f8885f 100644
>> --- a/hw/i386/intel_iommu.c
>> +++ b/hw/i386/intel_iommu.c
>> @@ -1779,6 +1779,7 @@ static bool
>vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
>>               */
>>              return false;
>>          }
>> +
>spurious new line

Will fix.

>>          return (VTD_PE_GET_TYPE(&pe) == VTD_SM_PASID_ENTRY_PT);
>>      }
>>
>> @@ -1790,10 +1791,33 @@ static bool
>vtd_as_pt_enabled(VTDAddressSpace *as)
>>  {
>>      IntelIOMMUState *s;
>>      VTDContextEntry ce;
>> +    struct vtd_as_key key = {
>> +        .bus = as->bus,
>> +        .devfn = as->devfn,
>> +    };
>>
>>      assert(as);
>>
>>      s = as->iommu_state;
>> +
>> +    /*
>> +     * When guest in scalable mode and x-flts=on, we stick to system MR
>ditto

That's strange, I didn't see spurious new line here.

>> +     * for IOMMUFD backed host device. Then its default hwpt contains
>> +     * GPA->HPA mappings which is used directly if PGTT=PT and used as
>> +     * nested parent if PGTT=FLT. Otherwise fallback to original
>fall back here and above
>
>This comment sounds a little bit unrelated to the below implementation
>which does not refer to system MR. how does the search for the hiod
>relate to that. I would refocus the comment.

vtd_as_pt_enabled()'s return value determines which MR to switch to.
See vtd_switch_address_space(). How about adding a comment:

+/*
+ * vtd_switch_address_space() calls vtd_as_pt_enabled() to determine which
+ * MR to switch to. Switch to system MR if return true, iommu MR otherwise.
+ */
 static bool vtd_as_pt_enabled(VTDAddressSpace *as)
 {

With this comment, we know the return value impact MR switch and this patch
deal with return value.

Thanks
Zhenzhong