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 <[email protected]> >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-- >[email protected]](mailto:[email protected])> >> Signed-off-by: Zhenzhong Duan ><[[email protected]](mailto:[email protected])> >> Tested-by: Clement Mathieu--Drif <[clement.mathieu-- >[email protected]](mailto:[email protected])> >> --- >> 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 >>
