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

Zhenzhong Duan posted 1 patch 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261103095444.1218225-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] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Zhenzhong Duan 2 weeks, 5 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")
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.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..ec8d056735 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;
@@ -1898,8 +1904,15 @@ static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
     }
 
     if (vtd_switch_address_space(vtd_as) == false) {
+        VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
+
         /* We switched off IOMMU region successfully. */
         success = true;
+
+        if (s->root_scalable) {
+            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] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Clément MATHIEU--DRIF 2 weeks, 5 days ago
Hi Zhenzhong,

On Tue, 2026-11-03 at 17:54 +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")  
> Signed-off-by: Zhenzhong Duan <[zhenzhong.duan@intel.com](mailto:zhenzhong.duan@intel.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..ec8d056735 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;  
> @@ -1898,8 +1904,15 @@ static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)  
>      }
> 
>      if (vtd_switch_address_space(vtd_as) == false) {  
> +        VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;  

Is there a reason why you don't declare this in the second if?  
If not at the top of the function, why this intermediary level?

Reported-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>
Tested-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>

> +  
>          /* We switched off IOMMU region successfully. */  
>          success = true;  
> +  
> +        if (s->root_scalable) {  
> +            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] intel_iommu: Keep PASID cache consistent with PT fast path
Posted by Duan, Zhenzhong 2 weeks, 4 days ago
>vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
>>      }
>>
>>      if (vtd_switch_address_space(vtd_as) == false) {
>> +        VTDPASIDCacheEntry *pc_entry = &vtd_as->pasid_cache_entry;
>
>Is there a reason why you don't declare this in the second if?
>If not at the top of the function, why this intermediary level?

Ah. Thanks, I just missed that, I'll send a v2.

>
>Reported-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>
>Tested-by: Clement Mathieu--Drif <clement.mathieu--drif@bull.com>
>
>> +
>>          /* We switched off IOMMU region successfully. */
>>          success = true;
>> +
>> +        if (s->root_scalable) {
>> +            pc_entry->pasid_entry = *pe;
>> +            pc_entry->valid = true;
>> +        }
>>      }
>>