[PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path

Zhenzhong Duan posted 1 patch 2 weeks, 4 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261104070740.1577884-1-zhenzhong.duan@intel.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowangio@gmail.com>, Yi Liu <yi.l.liu@intel.com>, "Clément Mathieu--Drif" <clement.mathieu--drif@bull.com>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>
There is a newer version of this series
hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
[PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Zhenzhong Duan 2 weeks, 4 days ago
With caching mode disabled, a guest can install a scalable-mode
pass-through PASID entry without issuing a PASID-cache invalidation. The
first DMA enables the PT fast path, but leaves QEMU's PASID cache invalid.

When the guest tears down that entry to switch back to a DMA domain, the
invalidation handler sees both a non-present guest entry and an invalid
cached entry and returns without re-enabling the IOMMU memory region.
Subsequent DMA therefore continues to bypass translation.

The issue can be reproduced with an emulated device in scalable mode
with caching-mode=off:

1. Unbind the guest device driver and change the group's type to
   identity through /sys/kernel/iommu_groups/<group>/type.
2. Rebind the driver and issue a DMA write.
3. Unbind the driver, change the group's type back to DMA-FQ, and
   rebind the driver.
4. Issue another DMA write using an IOVA mapped to a different GPA.

The second write incorrectly bypasses translation instead of reaching
the mapped GPA.

Save the PASID entry when enabling the scalable-mode PT fast path so
teardown invalidation can restore the IOMMU address space. Return after
handling a non-present entry, and refresh the cached entry when a present
entry changes.

Opportunistically clarify the invalidation modes in the fast-path comment.

Fixes: c9cf33075a4c ("intel_iommu: Fix DMA failure when guest switches IOMMU domain")
Reported-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Tested-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>
---
 hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index b7da5d086d..2c5aa04f69 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -1649,7 +1649,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
  * or second stage translation, it depends on PGTT setting.
  */
 static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
-                               uint32_t pasid)
+                               uint32_t pasid, VTDPASIDEntry *pasid_entry)
 {
     VTDPASIDEntry pe;
     int ret;
@@ -1663,6 +1663,11 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
              */
             return false;
         }
+
+        if (pasid_entry) {
+            *pasid_entry = pe;
+        }
+
         return vtd_pe_pgtt_is_pt(&pe);
     }
 
@@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
         return false;
     }
 
-    return vtd_dev_pt_enabled(s, &ce, as->pasid);
+    return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL);
 }
 
 /* Return whether the device is using IOMMU translation. */
@@ -1887,7 +1892,8 @@ VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)
     return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
 }
 
-static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
+static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id,
+                                    VTDPASIDEntry *pe)
 {
     VTDAddressSpace *vtd_as;
     bool success = false;
@@ -1900,6 +1906,13 @@ static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
     if (vtd_switch_address_space(vtd_as) == false) {
         /* We switched off IOMMU region successfully. */
         success = true;
+
+        if (s->root_scalable) {
+            VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+
+            pc_entry->pasid_entry = *pe;
+            pc_entry->valid = true;
+        }
     }
 
 out:
@@ -2091,6 +2104,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
 {
     IntelIOMMUState *s = vtd_as->iommu_state;
     VTDContextEntry ce;
+    VTDPASIDEntry pe;
     uint8_t bus_num = pci_bus_num(bus);
     VTDContextCacheEntry *cc_entry;
     uint64_t pte, page_mask;
@@ -2167,7 +2181,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      * We don't need to translate for pass-through context entries.
      * Also, let's ignore IOTLB caching as well for PT devices.
      */
-    if (vtd_dev_pt_enabled(s, &ce, pasid)) {
+    if (vtd_dev_pt_enabled(s, &ce, pasid, &pe)) {
         entry->iova = addr & VTD_PAGE_MASK_4K;
         entry->translated_addr = entry->iova;
         entry->addr_mask = ~VTD_PAGE_MASK_4K;
@@ -2180,10 +2194,11 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
          * the device. Let's enable the fast path for passthrough.
          *
          * When passthrough is disabled again for the device, we can
-         * capture it via the context entry invalidation, then the
-         * IOMMU region can be swapped back.
+         * capture it via context-cache invalidation in legacy mode or
+         * PASID-cache invalidation in scalable mode, then the IOMMU
+         * region can be swapped back.
          */
-        vtd_pt_enable_fast_path(s, source_id);
+        vtd_pt_enable_fast_path(s, source_id, &pe);
         vtd_iommu_unlock(s);
         return true;
     }
@@ -3133,6 +3148,7 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
             vtd_address_space_unmap(vtd_as, n);
         }
         vtd_switch_address_space(vtd_as);
+        return;
     }
 
     /*
@@ -3158,11 +3174,11 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,
     }
 
     if (!pc_entry->valid) {
-        pc_entry->pasid_entry = pe;
         pc_entry->valid = true;
     } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
         return;
     }
+    pc_entry->pasid_entry = pe;
 
     vtd_switch_address_space(vtd_as);
     vtd_address_space_sync(vtd_as);
-- 
2.52.0
Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Clément MATHIEU--DRIF 2 weeks, 4 days ago
Hi Zhenzhong,  

I traced the calls we have and I fear the flow is becoming a bit spaghetti.

```
vtd_do_iommu_translate
	|
	 --> if (vtd_dev_pt_enabled) vtd_pt_enable_fast_path
						|
						v
				    vtd_switch_address_space
					(updates the pasid
					entry based on the
					parameter it has
					been given)
						|
						v
					vtd_as_pt_enabled
					  (drops the
					  entry returned
					  by vtd_dev_pt_enabled)
						|
						v
					vtd_dev_pt_enabled
```

It looks like we are checking things twice, ignoring the result on the second call and re-using the first one.

I think we are trying to gate the call to vtd_pt_enable_fast_path from vtd_do_iommu_translate whereas the callee could do the thing on its own.

What is your opinion about this?



On Wed, 2026-11-04 at 15:07 +0800, Zhenzhong Duan wrote:
> Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.
> 
> 
> With caching mode disabled, a guest can install a scalable-mode  
> pass-through PASID entry without issuing a PASID-cache invalidation. The  
> first DMA enables the PT fast path, but leaves QEMU's PASID cache invalid.
> 
> When the guest tears down that entry to switch back to a DMA domain, the  
> invalidation handler sees both a non-present guest entry and an invalid  
> cached entry and returns without re-enabling the IOMMU memory region.  
> Subsequent DMA therefore continues to bypass translation.
> 
> The issue can be reproduced with an emulated device in scalable mode  
> with caching-mode=off:
> 
> 1. Unbind the guest device driver and change the group's type to  
>    identity through /sys/kernel/iommu_groups/<group>/type.  
> 2. Rebind the driver and issue a DMA write.  
> 3. Unbind the driver, change the group's type back to DMA-FQ, and  
>    rebind the driver.  
> 4. Issue another DMA write using an IOVA mapped to a different GPA.
> 
> The second write incorrectly bypasses translation instead of reaching  
> the mapped GPA.
> 
> Save the PASID entry when enabling the scalable-mode PT fast path so  
> teardown invalidation can restore the IOMMU address space. Return after  
> handling a non-present entry, and refresh the cached entry when a present  
> entry changes.
> 
> Opportunistically clarify the invalidation modes in the fast-path comment.
> 
> Fixes: c9cf33075a4c ("intel_iommu: Fix DMA failure when guest switches IOMMU domain")  
> Reported-by: Clement Mathieu--Drif <[clement.mathieu--drif@bull.com](mailto:clement.mathieu--drif@bull.com)>  
> Signed-off-by: Zhenzhong Duan <[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.com)>  
> Tested-by: Clement Mathieu--Drif <[clement.mathieu--drif@bull.com](mailto:clement.mathieu--drif@bull.com)>  
> ---  
>  hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------  
>  1 file changed, 24 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c  
> index b7da5d086d..2c5aa04f69 100644  
> --- a/hw/i386/intel_iommu.c  
> +++ b/hw/i386/intel_iommu.c  
> @@ -1649,7 +1649,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vtd_as)  
>   * or second stage translation, it depends on PGTT setting.  
>   */  
>  static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,  
> -                               uint32_t pasid)  
> +                               uint32_t pasid, VTDPASIDEntry *pasid_entry)  
>  {  
>      VTDPASIDEntry pe;  
>      int ret;  
> @@ -1663,6 +1663,11 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,  
>               */  
>              return false;  
>          }  
> +  
> +        if (pasid_entry) {  
> +            *pasid_entry = pe;  
> +        }  
> +  
>          return vtd_pe_pgtt_is_pt(&pe);  
>      }
> 
> @@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)  
>          return false;  
>      }
> 
> -    return vtd_dev_pt_enabled(s, &ce, as->pasid);  
> +    return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL);  
>  }
> 
>  /* Return whether the device is using IOMMU translation. */  
> @@ -1887,7 +1892,8 @@ VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState *s, uint16_t sid)  
>      return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);  
>  }
> 
> -static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)  
> +static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id,  
> +                                    VTDPASIDEntry *pe)  
>  {  
>      VTDAddressSpace *vtd_as;  
>      bool success = false;  
> @@ -1900,6 +1906,13 @@ static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)  
>      if (vtd_switch_address_space(vtd_as) == false) {  
>          /* We switched off IOMMU region successfully. */  
>          success = true;  
> +  
> +        if (s->root_scalable) {  
> +            VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;  
> +  
> +            pc_entry->pasid_entry = *pe;  
> +            pc_entry->valid = true;  
> +        }  
>      }
> 
>  out:  
> @@ -2091,6 +2104,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  
>  {  
>      IntelIOMMUState *s = vtd_as->iommu_state;  
>      VTDContextEntry ce;  
> +    VTDPASIDEntry pe;  
>      uint8_t bus_num = pci_bus_num(bus);  
>      VTDContextCacheEntry *cc_entry;  
>      uint64_t pte, page_mask;  
> @@ -2167,7 +2181,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  
>       * We don't need to translate for pass-through context entries.  
>       * Also, let's ignore IOTLB caching as well for PT devices.  
>       */  
> -    if (vtd_dev_pt_enabled(s, &ce, pasid)) {  
> +    if (vtd_dev_pt_enabled(s, &ce, pasid, &pe)) {  
>          entry->iova = addr & VTD_PAGE_MASK_4K;  
>          entry->translated_addr = entry->iova;  
>          entry->addr_mask = ~VTD_PAGE_MASK_4K;  
> @@ -2180,10 +2194,11 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,  
>           * the device. Let's enable the fast path for passthrough.  
>           *  
>           * When passthrough is disabled again for the device, we can  
> -         * capture it via the context entry invalidation, then the  
> -         * IOMMU region can be swapped back.  
> +         * capture it via context-cache invalidation in legacy mode or  
> +         * PASID-cache invalidation in scalable mode, then the IOMMU  
> +         * region can be swapped back.  
>           */  
> -        vtd_pt_enable_fast_path(s, source_id);  
> +        vtd_pt_enable_fast_path(s, source_id, &pe);  
>          vtd_iommu_unlock(s);  
>          return true;  
>      }  
> @@ -3133,6 +3148,7 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,  
>              vtd_address_space_unmap(vtd_as, n);  
>          }  
>          vtd_switch_address_space(vtd_as);  
> +        return;  
>      }
> 
>      /*  
> @@ -3158,11 +3174,11 @@ static void vtd_pasid_cache_sync_locked(gpointer key, gpointer value,  
>      }
> 
>      if (!pc_entry->valid) {  
> -        pc_entry->pasid_entry = pe;  
>          pc_entry->valid = true;  
>      } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {  
>          return;  
>      }  
> +    pc_entry->pasid_entry = pe;
> 
>      vtd_switch_address_space(vtd_as);  
>      vtd_address_space_sync(vtd_as);  
> --  
> 2.52.0
> 
RE: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Duan, Zhenzhong 2 weeks, 3 days ago
Hi Clement,

Good finding!

It looks not easy to refactor all these as
vtd_switch_address_space() is called in many callsites,
not only fast path.

I see in vtd_switch_address_space() vtd_as_pt_enabled()
is called twice, that's obviously unnecessary.

I'm not sure if it's possible to cache CE and PE before
all vtd_switch_address_space() so it only needs to check
cache for PT.

Thanks
Zhenzhong

>-----Original Message-----
>From: Clément MATHIEU--DRIF <clement.mathieu--drif@bull.com>
>Subject: Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT
>fast path
>
>Hi Zhenzhong,
>
>I traced the calls we have and I fear the flow is becoming a bit spaghetti.
>
>```
>vtd_do_iommu_translate
>	|
>	 --> if (vtd_dev_pt_enabled) vtd_pt_enable_fast_path
>						|
>						v
>				    vtd_switch_address_space
>					(updates the pasid
>					entry based on the
>					parameter it has
>					been given)
>						|
>						v
>					vtd_as_pt_enabled
>					  (drops the
>					  entry returned
>					  by vtd_dev_pt_enabled)
>						|
>						v
>					vtd_dev_pt_enabled
>```
>
>It looks like we are checking things twice, ignoring the result on the
>second call and re-using the first one.
>
>I think we are trying to gate the call to vtd_pt_enable_fast_path from
>vtd_do_iommu_translate whereas the callee could do the thing on its own.
>
>What is your opinion about this?
>
>
>
>On Wed, 2026-11-04 at 15:07 +0800, Zhenzhong Duan wrote:
>> Caution: External email. Do not open attachments or click links, unless
>this email comes from a known sender and you know the content is safe.
>>
>>
>> With caching mode disabled, a guest can install a scalable-mode
>> pass-through PASID entry without issuing a PASID-cache invalidation. The
>> first DMA enables the PT fast path, but leaves QEMU's PASID cache
>invalid.
>>
>> When the guest tears down that entry to switch back to a DMA domain, the
>> invalidation handler sees both a non-present guest entry and an invalid
>> cached entry and returns without re-enabling the IOMMU memory region.
>> Subsequent DMA therefore continues to bypass translation.
>>
>> The issue can be reproduced with an emulated device in scalable mode
>> with caching-mode=off:
>>
>> 1. Unbind the guest device driver and change the group's type to
>>    identity through /sys/kernel/iommu_groups/<group>/type.
>> 2. Rebind the driver and issue a DMA write.
>> 3. Unbind the driver, change the group's type back to DMA-FQ, and
>>    rebind the driver.
>> 4. Issue another DMA write using an IOVA mapped to a different GPA.
>>
>> The second write incorrectly bypasses translation instead of reaching
>> the mapped GPA.
>>
>> Save the PASID entry when enabling the scalable-mode PT fast path so
>> teardown invalidation can restore the IOMMU address space. Return after
>> handling a non-present entry, and refresh the cached entry when a present
>> entry changes.
>>
>> Opportunistically clarify the invalidation modes in the fast-path
>comment.
>>
>> Fixes: c9cf33075a4c ("intel_iommu: Fix DMA failure when guest switches
>IOMMU domain")
>> Reported-by: Clement Mathieu--Drif <[clement.mathieu--
>drif@bull.com](mailto:clement.mathieu--drif@bull.com)>
>> Signed-off-by: Zhenzhong Duan
><[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.com)>
>> Tested-by: Clement Mathieu--Drif <[clement.mathieu--
>drif@bull.com](mailto:clement.mathieu--drif@bull.com)>
>> ---
>>  hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------
>>  1 file changed, 24 insertions(+), 8 deletions(-)
>>
>> diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
>> index b7da5d086d..2c5aa04f69 100644
>> --- a/hw/i386/intel_iommu.c
>> +++ b/hw/i386/intel_iommu.c
>> @@ -1649,7 +1649,7 @@ static int vtd_address_space_sync(VTDAddressSpace
>*vtd_as)
>>   * or second stage translation, it depends on PGTT setting.
>>   */
>>  static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
>> -                               uint32_t pasid)
>> +                               uint32_t pasid, VTDPASIDEntry
>*pasid_entry)
>>  {
>>      VTDPASIDEntry pe;
>>      int ret;
>> @@ -1663,6 +1663,11 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s,
>VTDContextEntry *ce,
>>               */
>>              return false;
>>          }
>> +
>> +        if (pasid_entry) {
>> +            *pasid_entry = pe;
>> +        }
>> +
>>          return vtd_pe_pgtt_is_pt(&pe);
>>      }
>>
>> @@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
>>          return false;
>>      }
>>
>> -    return vtd_dev_pt_enabled(s, &ce, as->pasid);
>> +    return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL);
>>  }
>>
>>  /* Return whether the device is using IOMMU translation. */
>> @@ -1887,7 +1892,8 @@ VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState
>*s, uint16_t sid)
>>      return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
>>  }
>>
>> -static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
>source_id)
>> +static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
>source_id,
>> +                                    VTDPASIDEntry *pe)
>>  {
>>      VTDAddressSpace *vtd_as;
>>      bool success = false;
>> @@ -1900,6 +1906,13 @@ static void
>vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
>>      if (vtd_switch_address_space(vtd_as) == false) {
>>          /* We switched off IOMMU region successfully. */
>>          success = true;
>> +
>> +        if (s->root_scalable) {
>> +            VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
>> +
>> +            pc_entry->pasid_entry = *pe;
>> +            pc_entry->valid = true;
>> +        }
>>      }
>>
>>  out:
>> @@ -2091,6 +2104,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace
>*vtd_as, PCIBus *bus,
>>  {
>>      IntelIOMMUState *s = vtd_as->iommu_state;
>>      VTDContextEntry ce;
>> +    VTDPASIDEntry pe;
>>      uint8_t bus_num = pci_bus_num(bus);
>>      VTDContextCacheEntry *cc_entry;
>>      uint64_t pte, page_mask;
>> @@ -2167,7 +2181,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace
>*vtd_as, PCIBus *bus,
>>       * We don't need to translate for pass-through context entries.
>>       * Also, let's ignore IOTLB caching as well for PT devices.
>>       */
>> -    if (vtd_dev_pt_enabled(s, &ce, pasid)) {
>> +    if (vtd_dev_pt_enabled(s, &ce, pasid, &pe)) {
>>          entry->iova = addr & VTD_PAGE_MASK_4K;
>>          entry->translated_addr = entry->iova;
>>          entry->addr_mask = ~VTD_PAGE_MASK_4K;
>> @@ -2180,10 +2194,11 @@ static bool
>vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
>>           * the device. Let's enable the fast path for passthrough.
>>           *
>>           * When passthrough is disabled again for the device, we can
>> -         * capture it via the context entry invalidation, then the
>> -         * IOMMU region can be swapped back.
>> +         * capture it via context-cache invalidation in legacy mode or
>> +         * PASID-cache invalidation in scalable mode, then the IOMMU
>> +         * region can be swapped back.
>>           */
>> -        vtd_pt_enable_fast_path(s, source_id);
>> +        vtd_pt_enable_fast_path(s, source_id, &pe);
>>          vtd_iommu_unlock(s);
>>          return true;
>>      }
>> @@ -3133,6 +3148,7 @@ static void vtd_pasid_cache_sync_locked(gpointer
>key, gpointer value,
>>              vtd_address_space_unmap(vtd_as, n);
>>          }
>>          vtd_switch_address_space(vtd_as);
>> +        return;
>>      }
>>
>>      /*
>> @@ -3158,11 +3174,11 @@ static void vtd_pasid_cache_sync_locked(gpointer
>key, gpointer value,
>>      }
>>
>>      if (!pc_entry->valid) {
>> -        pc_entry->pasid_entry = pe;
>>          pc_entry->valid = true;
>>      } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
>>          return;
>>      }
>> +    pc_entry->pasid_entry = pe;
>>
>>      vtd_switch_address_space(vtd_as);
>>      vtd_address_space_sync(vtd_as);
>> --
>> 2.52.0
>>
Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Yi Liu 1 week, 3 days ago
On 11/5/26 17:07, Duan, Zhenzhong wrote:
> Hi Clement,
> 
> Good finding!
> 
> It looks not easy to refactor all these as
> vtd_switch_address_space() is called in many callsites,
> not only fast path.
> 
> I see in vtd_switch_address_space() vtd_as_pt_enabled()
> is called twice, that's obviously unnecessary.
> 
> I'm not sure if it's possible to cache CE and PE before
> all vtd_switch_address_space() so it only needs to check
> cache for PT.

I had considered it as well during reviewing the prior patches. It might
be worth thinking about caching both CE and PE in the AddressSpace and
gating their life circle by the CC/PC invalidation request from guest.
In other paths, we can just refer to the cached CE and PE. But this
means we rely on guest flushes cache timely when there is CE/PE
modification. This is spec defined behavior, so might be fine?

Regards,
Yi Liu
RE: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Duan, Zhenzhong 5 days, 18 hours ago

>-----Original Message-----
>From: Liu, Yi L <yi.l.liu@intel.com>
>Subject: Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT
>fast path
>
>On 11/5/26 17:07, Duan, Zhenzhong wrote:
>> Hi Clement,
>>
>> Good finding!
>>
>> It looks not easy to refactor all these as
>> vtd_switch_address_space() is called in many callsites,
>> not only fast path.
>>
>> I see in vtd_switch_address_space() vtd_as_pt_enabled()
>> is called twice, that's obviously unnecessary.
>>
>> I'm not sure if it's possible to cache CE and PE before
>> all vtd_switch_address_space() so it only needs to check
>> cache for PT.
>
>I had considered it as well during reviewing the prior patches. It might
>be worth thinking about caching both CE and PE in the AddressSpace and
>gating their life circle by the CC/PC invalidation request from guest.
>In other paths, we can just refer to the cached CE and PE. But this
>means we rely on guest flushes cache timely when there is CE/PE
>modification. This is spec defined behavior, so might be fine?

Yes, it should work for linux guest, I'm not clear if it breaks OS
that doesn't follow spec in that path:)

Thanks
Zhenzhong
Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Clément MATHIEU--DRIF 2 weeks, 1 day ago
What about converting vtd_pt_enable_fast_path into vtd_try_enable_pt_fast_path?

It would return bool instead of being void.

The checks would be done inside instead of being the responsibility of the caller (a bit like vtd_switch_address_space).

It would also make sure that the cache is correctly updated of invalidated after the call.

Maybe at some point vtd_pt_enable_fast_path and vtd_switch_address_space will be so close that they can become a single function?

On Thu, 2026-11-05 at 09:07 +0000, Duan, Zhenzhong wrote:
> Caution: External email. Do not open attachments or click links, unless this email comes from a known sender and you know the content is safe.
> 
> 
> Hi Clement,
> 
> Good finding!
> 
> It looks not easy to refactor all these as  
> vtd_switch_address_space() is called in many callsites,  
> not only fast path.
> 
> I see in vtd_switch_address_space() vtd_as_pt_enabled()  
> is called twice, that's obviously unnecessary.
> 
> I'm not sure if it's possible to cache CE and PE before  
> all vtd_switch_address_space() so it only needs to check  
> cache for PT.
> 
> Thanks  
> Zhenzhong
> 
> 
> > -----Original Message-----
> > From: Clément MATHIEU--DRIF <[clement.mathieu--drif@bull.com](mailto:clement.mathieu--drif@bull.com)>
> > Subject: Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT
> > fast path
> > 
> > Hi Zhenzhong,
> > 
> > I traced the calls we have and I fear the flow is becoming a bit spaghetti.
> > 
> > ```
> > vtd_do_iommu_translate
> >       |
> >        --> if (vtd_dev_pt_enabled) vtd_pt_enable_fast_path
> >                                               |
> >                                               v
> >                                   vtd_switch_address_space
> >                                       (updates the pasid
> >                                       entry based on the
> >                                       parameter it has
> >                                       been given)
> >                                               |
> >                                               v
> >                                       vtd_as_pt_enabled
> >                                         (drops the
> >                                         entry returned
> >                                         by vtd_dev_pt_enabled)
> >                                               |
> >                                               v
> >                                       vtd_dev_pt_enabled
> > ```
> > 
> > It looks like we are checking things twice, ignoring the result on the
> > second call and re-using the first one.
> > 
> > I think we are trying to gate the call to vtd_pt_enable_fast_path from
> > vtd_do_iommu_translate whereas the callee could do the thing on its own.
> > 
> > What is your opinion about this?
> > 
> > 
> > 
> > On Wed, 2026-11-04 at 15:07 +0800, Zhenzhong Duan wrote:
> > 
> > > Caution: External email. Do not open attachments or click links, unless
> > 
> > this email comes from a known sender and you know the content is safe.
> > 
> > > 
> > > 
> > > With caching mode disabled, a guest can install a scalable-mode
> > > pass-through PASID entry without issuing a PASID-cache invalidation. The
> > > first DMA enables the PT fast path, but leaves QEMU's PASID cache
> > 
> > invalid.
> > 
> > > 
> > > When the guest tears down that entry to switch back to a DMA domain, the
> > > invalidation handler sees both a non-present guest entry and an invalid
> > > cached entry and returns without re-enabling the IOMMU memory region.
> > > Subsequent DMA therefore continues to bypass translation.
> > > 
> > > The issue can be reproduced with an emulated device in scalable mode
> > > with caching-mode=off:
> > > 
> > > 1. Unbind the guest device driver and change the group's type to
> > >    identity through /sys/kernel/iommu_groups/<group>/type.
> > > 2. Rebind the driver and issue a DMA write.
> > > 3. Unbind the driver, change the group's type back to DMA-FQ, and
> > >    rebind the driver.
> > > 4. Issue another DMA write using an IOVA mapped to a different GPA.
> > > 
> > > The second write incorrectly bypasses translation instead of reaching
> > > the mapped GPA.
> > > 
> > > Save the PASID entry when enabling the scalable-mode PT fast path so
> > > teardown invalidation can restore the IOMMU address space. Return after
> > > handling a non-present entry, and refresh the cached entry when a present
> > > entry changes.
> > > 
> > > Opportunistically clarify the invalidation modes in the fast-path
> > 
> > comment.
> > 
> > > 
> > > Fixes: c9cf33075a4c ("intel_iommu: Fix DMA failure when guest switches
> > 
> > IOMMU domain")
> > 
> > > Reported-by: Clement Mathieu--Drif <[clement.mathieu--
> > 
> > [drif@bull.com](mailto:drif@bull.com)](mailto:[clement.mathieu--drif@bull.com](mailto:clement.mathieu--drif@bull.com))>
> > 
> > > Signed-off-by: Zhenzhong Duan
> > 
> > <[[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.com)](mailto:[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.com))>
> > 
> > > Tested-by: Clement Mathieu--Drif <[clement.mathieu--
> > 
> > [drif@bull.com](mailto:drif@bull.com)](mailto:[clement.mathieu--drif@bull.com](mailto:clement.mathieu--drif@bull.com))>
> > 
> > > ---
> > >  hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------
> > >  1 file changed, 24 insertions(+), 8 deletions(-)
> > > 
> > > diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
> > > index b7da5d086d..2c5aa04f69 100644
> > > --- a/hw/i386/intel_iommu.c
> > > +++ b/hw/i386/intel_iommu.c
> > > @@ -1649,7 +1649,7 @@ static int vtd_address_space_sync(VTDAddressSpace
> > 
> > *vtd_as)
> > 
> > >   * or second stage translation, it depends on PGTT setting.
> > >   */
> > >  static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce,
> > > -                               uint32_t pasid)
> > > +                               uint32_t pasid, VTDPASIDEntry
> > 
> > *pasid_entry)
> > 
> > >  {
> > >      VTDPASIDEntry pe;
> > >      int ret;
> > > @@ -1663,6 +1663,11 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s,
> > 
> > VTDContextEntry *ce,
> > 
> > >               */
> > >              return false;
> > >          }
> > > +
> > > +        if (pasid_entry) {
> > > +            *pasid_entry = pe;
> > > +        }
> > > +
> > >          return vtd_pe_pgtt_is_pt(&pe);
> > >      }
> > > 
> > > @@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as)
> > >          return false;
> > >      }
> > > 
> > > -    return vtd_dev_pt_enabled(s, &ce, as->pasid);
> > > +    return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL);
> > >  }
> > > 
> > >  /* Return whether the device is using IOMMU translation. */
> > > @@ -1887,7 +1892,8 @@ VTDAddressSpace *vtd_get_as_by_sid(IntelIOMMUState
> > 
> > *s, uint16_t sid)
> > 
> > >      return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
> > >  }
> > > 
> > > -static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
> > 
> > source_id)
> > 
> > > +static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
> > 
> > source_id,
> > 
> > > +                                    VTDPASIDEntry *pe)
> > >  {
> > >      VTDAddressSpace *vtd_as;
> > >      bool success = false;
> > > @@ -1900,6 +1906,13 @@ static void
> > 
> > vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
> > 
> > >      if (vtd_switch_address_space(vtd_as) == false) {
> > >          /* We switched off IOMMU region successfully. */
> > >          success = true;
> > > +
> > > +        if (s->root_scalable) {
> > > +            VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
> > > +
> > > +            pc_entry->pasid_entry = *pe;
> > > +            pc_entry->valid = true;
> > > +        }
> > >      }
> > > 
> > >  out:
> > > @@ -2091,6 +2104,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace
> > 
> > *vtd_as, PCIBus *bus,
> > 
> > >  {
> > >      IntelIOMMUState *s = vtd_as->iommu_state;
> > >      VTDContextEntry ce;
> > > +    VTDPASIDEntry pe;
> > >      uint8_t bus_num = pci_bus_num(bus);
> > >      VTDContextCacheEntry *cc_entry;
> > >      uint64_t pte, page_mask;
> > > @@ -2167,7 +2181,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace
> > 
> > *vtd_as, PCIBus *bus,
> > 
> > >       * We don't need to translate for pass-through context entries.
> > >       * Also, let's ignore IOTLB caching as well for PT devices.
> > >       */
> > > -    if (vtd_dev_pt_enabled(s, &ce, pasid)) {
> > > +    if (vtd_dev_pt_enabled(s, &ce, pasid, &pe)) {
> > >          entry->iova = addr & VTD_PAGE_MASK_4K;
> > >          entry->translated_addr = entry->iova;
> > >          entry->addr_mask = ~VTD_PAGE_MASK_4K;
> > > @@ -2180,10 +2194,11 @@ static bool
> > 
> > vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
> > 
> > >           * the device. Let's enable the fast path for passthrough.
> > >           *
> > >           * When passthrough is disabled again for the device, we can
> > > -         * capture it via the context entry invalidation, then the
> > > -         * IOMMU region can be swapped back.
> > > +         * capture it via context-cache invalidation in legacy mode or
> > > +         * PASID-cache invalidation in scalable mode, then the IOMMU
> > > +         * region can be swapped back.
> > >           */
> > > -        vtd_pt_enable_fast_path(s, source_id);
> > > +        vtd_pt_enable_fast_path(s, source_id, &pe);
> > >          vtd_iommu_unlock(s);
> > >          return true;
> > >      }
> > > @@ -3133,6 +3148,7 @@ static void vtd_pasid_cache_sync_locked(gpointer
> > 
> > key, gpointer value,
> > 
> > >              vtd_address_space_unmap(vtd_as, n);
> > >          }
> > >          vtd_switch_address_space(vtd_as);
> > > +        return;
> > >      }
> > > 
> > >      /*
> > > @@ -3158,11 +3174,11 @@ static void vtd_pasid_cache_sync_locked(gpointer
> > 
> > key, gpointer value,
> > 
> > >      }
> > > 
> > >      if (!pc_entry->valid) {
> > > -        pc_entry->pasid_entry = pe;
> > >          pc_entry->valid = true;
> > >      } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) {
> > >          return;
> > >      }
> > > +    pc_entry->pasid_entry = pe;
> > > 
> > >      vtd_switch_address_space(vtd_as);
> > >      vtd_address_space_sync(vtd_as);
> > > --
> > > 2.52.0
> > > 
> >
> 
RE: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Duan, Zhenzhong 1 week, 5 days ago
Hi Clement,

Yea, good idea, will do.

Zhenzhong

>-----Original Message-----
>From: Clément MATHIEU--DRIF <clement.mathieu--drif@bull.com>
>Subject: Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT
>fast path
>
>What about converting vtd_pt_enable_fast_path into
>vtd_try_enable_pt_fast_path?
>
>It would return bool instead of being void.
>
>The checks would be done inside instead of being the responsibility of the
>caller (a bit like vtd_switch_address_space).
>
>It would also make sure that the cache is correctly updated of invalidated
>after the call.
>
>Maybe at some point vtd_pt_enable_fast_path and vtd_switch_address_space
>will be so close that they can become a single function?
>
>On Thu, 2026-11-05 at 09:07 +0000, Duan, Zhenzhong wrote:
>> Caution: External email. Do not open attachments or click links, unless
>this email comes from a known sender and you know the content is safe.
>>
>>
>> Hi Clement,
>>
>> Good finding!
>>
>> It looks not easy to refactor all these as
>> vtd_switch_address_space() is called in many callsites,
>> not only fast path.
>>
>> I see in vtd_switch_address_space() vtd_as_pt_enabled()
>> is called twice, that's obviously unnecessary.
>>
>> I'm not sure if it's possible to cache CE and PE before
>> all vtd_switch_address_space() so it only needs to check
>> cache for PT.
>>
>> Thanks
>> Zhenzhong
>>
>>
>> > -----Original Message-----
>> > From: Clément MATHIEU--DRIF <[clement.mathieu--
>drif@bull.com](mailto:clement.mathieu--drif@bull.com)>
>> > Subject: Re: [PATCH v2] intel_iommu: Keep PASID cache consistent with
>PT
>> > fast path
>> >
>> > Hi Zhenzhong,
>> >
>> > I traced the calls we have and I fear the flow is becoming a bit
>spaghetti.
>> >
>> > ```
>> > vtd_do_iommu_translate
>> >       |
>> >        --> if (vtd_dev_pt_enabled) vtd_pt_enable_fast_path
>> >                                               |
>> >                                               v
>> >                                   vtd_switch_address_space
>> >                                       (updates the pasid
>> >                                       entry based on the
>> >                                       parameter it has
>> >                                       been given)
>> >                                               |
>> >                                               v
>> >                                       vtd_as_pt_enabled
>> >                                         (drops the
>> >                                         entry returned
>> >                                         by vtd_dev_pt_enabled)
>> >                                               |
>> >                                               v
>> >                                       vtd_dev_pt_enabled
>> > ```
>> >
>> > It looks like we are checking things twice, ignoring the result on the
>> > second call and re-using the first one.
>> >
>> > I think we are trying to gate the call to vtd_pt_enable_fast_path from
>> > vtd_do_iommu_translate whereas the callee could do the thing on its
>own.
>> >
>> > What is your opinion about this?
>> >
>> >
>> >
>> > On Wed, 2026-11-04 at 15:07 +0800, Zhenzhong Duan wrote:
>> >
>> > > Caution: External email. Do not open attachments or click links,
>unless
>> >
>> > this email comes from a known sender and you know the content is safe.
>> >
>> > >
>> > >
>> > > With caching mode disabled, a guest can install a scalable-mode
>> > > pass-through PASID entry without issuing a PASID-cache invalidation.
>The
>> > > first DMA enables the PT fast path, but leaves QEMU's PASID cache
>> >
>> > invalid.
>> >
>> > >
>> > > When the guest tears down that entry to switch back to a DMA domain,
>the
>> > > invalidation handler sees both a non-present guest entry and an
>invalid
>> > > cached entry and returns without re-enabling the IOMMU memory region.
>> > > Subsequent DMA therefore continues to bypass translation.
>> > >
>> > > The issue can be reproduced with an emulated device in scalable mode
>> > > with caching-mode=off:
>> > >
>> > > 1. Unbind the guest device driver and change the group's type to
>> > >    identity through /sys/kernel/iommu_groups/<group>/type.
>> > > 2. Rebind the driver and issue a DMA write.
>> > > 3. Unbind the driver, change the group's type back to DMA-FQ, and
>> > >    rebind the driver.
>> > > 4. Issue another DMA write using an IOVA mapped to a different GPA.
>> > >
>> > > The second write incorrectly bypasses translation instead of reaching
>> > > the mapped GPA.
>> > >
>> > > Save the PASID entry when enabling the scalable-mode PT fast path so
>> > > teardown invalidation can restore the IOMMU address space. Return
>after
>> > > handling a non-present entry, and refresh the cached entry when a
>present
>> > > entry changes.
>> > >
>> > > Opportunistically clarify the invalidation modes in the fast-path
>> >
>> > comment.
>> >
>> > >
>> > > Fixes: c9cf33075a4c ("intel_iommu: Fix DMA failure when guest
>switches
>> >
>> > IOMMU domain")
>> >
>> > > Reported-by: Clement Mathieu--Drif <[clement.mathieu--
>> >
>> > [drif@bull.com](mailto:drif@bull.com)](mailto:[clement.mathieu--
>drif@bull.com](mailto:clement.mathieu--drif@bull.com))>
>> >
>> > > Signed-off-by: Zhenzhong Duan
>> >
>> >
><[[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.com)](mailto:[zhen
>zhong.duan@intel.com](mailto:zhenzhong.duan@intel.com))>
>> >
>> > > Tested-by: Clement Mathieu--Drif <[clement.mathieu--
>> >
>> > [drif@bull.com](mailto:drif@bull.com)](mailto:[clement.mathieu--
>drif@bull.com](mailto:clement.mathieu--drif@bull.com))>
>> >
>> > > ---
>> > >  hw/i386/intel_iommu.c | 32 ++++++++++++++++++++++++--------
>> > >  1 file changed, 24 insertions(+), 8 deletions(-)
>> > >
>> > > diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
>> > > index b7da5d086d..2c5aa04f69 100644
>> > > --- a/hw/i386/intel_iommu.c
>> > > +++ b/hw/i386/intel_iommu.c
>> > > @@ -1649,7 +1649,7 @@ static int
>vtd_address_space_sync(VTDAddressSpace
>> >
>> > *vtd_as)
>> >
>> > >   * or second stage translation, it depends on PGTT setting.
>> > >   */
>> > >  static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry
>*ce,
>> > > -                               uint32_t pasid)
>> > > +                               uint32_t pasid, VTDPASIDEntry
>> >
>> > *pasid_entry)
>> >
>> > >  {
>> > >      VTDPASIDEntry pe;
>> > >      int ret;
>> > > @@ -1663,6 +1663,11 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState
>*s,
>> >
>> > VTDContextEntry *ce,
>> >
>> > >               */
>> > >              return false;
>> > >          }
>> > > +
>> > > +        if (pasid_entry) {
>> > > +            *pasid_entry = pe;
>> > > +        }
>> > > +
>> > >          return vtd_pe_pgtt_is_pt(&pe);
>> > >      }
>> > >
>> > > @@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace
>*as)
>> > >          return false;
>> > >      }
>> > >
>> > > -    return vtd_dev_pt_enabled(s, &ce, as->pasid);
>> > > +    return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL);
>> > >  }
>> > >
>> > >  /* Return whether the device is using IOMMU translation. */
>> > > @@ -1887,7 +1892,8 @@ VTDAddressSpace
>*vtd_get_as_by_sid(IntelIOMMUState
>> >
>> > *s, uint16_t sid)
>> >
>> > >      return vtd_get_as_by_sid_and_pasid(s, sid, IOMMU_NO_PASID);
>> > >  }
>> > >
>> > > -static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
>> >
>> > source_id)
>> >
>> > > +static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t
>> >
>> > source_id,
>> >
>> > > +                                    VTDPASIDEntry *pe)
>> > >  {
>> > >      VTDAddressSpace *vtd_as;
>> > >      bool success = false;
>> > > @@ -1900,6 +1906,13 @@ static void
>> >
>> > vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
>> >
>> > >      if (vtd_switch_address_space(vtd_as) == false) {
>> > >          /* We switched off IOMMU region successfully. */
>> > >          success = true;
>> > > +
>> > > +        if (s->root_scalable) {
>> > > +            VTDPASIDCacheEntry *pc_entry = &vtd_as-
>>pasid_cache_entry;
>> > > +
>> > > +            pc_entry->pasid_entry = *pe;
>> > > +            pc_entry->valid = true;
>> > > +        }
>> > >      }
>> > >
>> > >  out:
>> > > @@ -2091,6 +2104,7 @@ static bool
>vtd_do_iommu_translate(VTDAddressSpace
>> >
>> > *vtd_as, PCIBus *bus,
>> >
>> > >  {
>> > >      IntelIOMMUState *s = vtd_as->iommu_state;
>> > >      VTDContextEntry ce;
>> > > +    VTDPASIDEntry pe;
>> > >      uint8_t bus_num = pci_bus_num(bus);
>> > >      VTDContextCacheEntry *cc_entry;
>> > >      uint64_t pte, page_mask;
>> > > @@ -2167,7 +2181,7 @@ static bool
>vtd_do_iommu_translate(VTDAddressSpace
>> >
>> > *vtd_as, PCIBus *bus,
>> >
>> > >       * We don't need to translate for pass-through context entries.
>> > >       * Also, let's ignore IOTLB caching as well for PT devices.
>> > >       */
>> > > -    if (vtd_dev_pt_enabled(s, &ce, pasid)) {
>> > > +    if (vtd_dev_pt_enabled(s, &ce, pasid, &pe)) {
>> > >          entry->iova = addr & VTD_PAGE_MASK_4K;
>> > >          entry->translated_addr = entry->iova;
>> > >          entry->addr_mask = ~VTD_PAGE_MASK_4K;
>> > > @@ -2180,10 +2194,11 @@ static bool
>> >
>> > vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
>> >
>> > >           * the device. Let's enable the fast path for passthrough.
>> > >           *
>> > >           * When passthrough is disabled again for the device, we can
>> > > -         * capture it via the context entry invalidation, then the
>> > > -         * IOMMU region can be swapped back.
>> > > +         * capture it via context-cache invalidation in legacy mode
>or
>> > > +         * PASID-cache invalidation in scalable mode, then the IOMMU
>> > > +         * region can be swapped back.
>> > >           */
>> > > -        vtd_pt_enable_fast_path(s, source_id);
>> > > +        vtd_pt_enable_fast_path(s, source_id, &pe);
>> > >          vtd_iommu_unlock(s);
>> > >          return true;
>> > >      }
>> > > @@ -3133,6 +3148,7 @@ static void
>vtd_pasid_cache_sync_locked(gpointer
>> >
>> > key, gpointer value,
>> >
>> > >              vtd_address_space_unmap(vtd_as, n);
>> > >          }
>> > >          vtd_switch_address_space(vtd_as);
>> > > +        return;
>> > >      }
>> > >
>> > >      /*
>> > > @@ -3158,11 +3174,11 @@ static void
>vtd_pasid_cache_sync_locked(gpointer
>> >
>> > key, gpointer value,
>> >
>> > >      }
>> > >
>> > >      if (!pc_entry->valid) {
>> > > -        pc_entry->pasid_entry = pe;
>> > >          pc_entry->valid = true;
>> > >      } else if (!vtd_pasid_entry_compare(&pe, &pc_entry-
>>pasid_entry)) {
>> > >          return;
>> > >      }
>> > > +    pc_entry->pasid_entry = pe;
>> > >
>> > >      vtd_switch_address_space(vtd_as);
>> > >      vtd_address_space_sync(vtd_as);
>> > > --
>> > > 2.52.0
>> > >
>> >
>>