From nobody Mon Nov 23 04:28:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1793776138; cv=none; d=zohomail.com; s=zohoarc; b=RsJaXK6M1NPx/WWMqRTGvjTwfWEHQfTVZigKyolc4c8K3DoD/87z9vttVzYhP2vori7fNe3ndyDddul2wlg0nJWCXuUivVfyDi/oM8pWW7ppe+vUWlv/IwZRoHIjTQ0UeOwgVUMs5md36xQh3IerHUBtna15kqDmSYGyHH0rLJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793776138; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=bctTTRBRuvDiXzsFdMHbfhAF9QOAy8lNFGVuJxLyDhQ=; b=gWPh6iDD7P1EUfK/aN9g3Kqn/DfBXIueWukjhtSFRFZVYniqdkwT9kbM4GLPvpH1SVFTFXHRgoSZ26qPaCy+e0J2w4eC+fhHWr6szSemVfWXIkqYj5C8zZU19zZyIuryoMlQ3jJ88mrEhpnxeYSzcVq4MU2i/qok6bDfjlb7a7U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793776137998446.9233540080155; Tue, 3 Nov 2026 23:08:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOV6o-0004Fb-Cu; Wed, 04 Nov 2026 02:08:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOV6h-0004FJ-UP for qemu-devel@nongnu.org; Wed, 04 Nov 2026 02:08:28 -0500 Received: from mgamail.intel.com ([192.198.163.12]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOV6e-0004Kc-Ur for qemu-devel@nongnu.org; Wed, 04 Nov 2026 02:08:26 -0500 Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Nov 2026 23:08:23 -0800 Received: from unknown (HELO gnr-sp-2s-612.sh.intel.com) ([10.112.229.148]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Nov 2026 23:08:21 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1793776106; x=1825312106; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=lPATge5r9IgGhVzV7C6KyrQriD2ITHYVnHo2zzy0PBI=; b=APoAh0kBc7QBcV2UE7I9F15rPJid5aVxnazMvisMAeGOSFEkmS1o+IOT yQN+lXEEytKCcXCKFp23mfNOKWo4SsuH3us1hcgHo9xGKJoD296eI+zzv ZtMkTq9ofxsYz45LW0hfr7202jY7lJIFPUDqG9zs8LQvxgbgdTeq4NSPa XYk9RC4QIox9knwyH0rPAX9PcP1fFbvld5fYAzhwyr+Jb5oPzC4wQlaHL lQKxKaVKochvf0Qx08lDoEgoX2+NI9bgR+hGhcyydN06I0ZzZLxenE1Hs DPhiXOE5q6qOHNT36VqDltKSXM9CHkjNzmvbmmRR/qf59zIquMDjd4H3m g==; X-CSE-ConnectionGUID: Q+eiIKYBRk2jQwuo7FMKbA== X-CSE-MsgGUID: Ye7PEKl2Q6aNMlN+2YpBwQ== X-IronPort-AV: E=McAfee;i="6800,10657,11955"; a="2416180" X-IronPort-AV: E=Sophos;i="6.27,194,1787036400"; d="scan'208";a="2416180" X-CSE-ConnectionGUID: VRTfBTvjSz6ar3pDj9iKTw== X-CSE-MsgGUID: DFrtheQCT3eSqMYf2SyAHg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,194,1787036400"; d="scan'208";a="6988021" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: clement.mathieu--drif@bull.com, mst@redhat.com, jasowangio@gmail.com, yi.l.liu@intel.com, Zhenzhong Duan Subject: [PATCH v2] intel_iommu: Keep PASID cache consistent with PT fast path Date: Wed, 4 Nov 2026 15:07:39 +0800 Message-ID: <20261104070740.1577884-1-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.52.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=192.198.163.12; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1793776138863158500 Content-Type: text/plain; charset="utf-8" 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=3Doff: 1. Unbind the guest device driver and change the group's type to identity through /sys/kernel/iommu_groups//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 IOMM= U domain") Reported-by: Clement Mathieu--Drif Signed-off-by: Zhenzhong Duan Tested-by: Clement Mathieu--Drif --- 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 *vt= d_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, V= TDContextEntry *ce, */ return false; } + + if (pasid_entry) { + *pasid_entry =3D pe; + } + return vtd_pe_pgtt_is_pt(&pe); } =20 @@ -1689,7 +1694,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as) return false; } =20 - return vtd_dev_pt_enabled(s, &ce, as->pasid); + return vtd_dev_pt_enabled(s, &ce, as->pasid, NULL); } =20 /* 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); } =20 -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 =3D 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) =3D=3D false) { /* We switched off IOMMU region successfully. */ success =3D true; + + if (s->root_scalable) { + VTDPASIDCacheEntry *pc_entry =3D &vtd_as->pasid_cache_entry; + + pc_entry->pasid_entry =3D *pe; + pc_entry->valid =3D true; + } } =20 out: @@ -2091,6 +2104,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, { IntelIOMMUState *s =3D vtd_as->iommu_state; VTDContextEntry ce; + VTDPASIDEntry pe; uint8_t bus_num =3D pci_bus_num(bus); VTDContextCacheEntry *cc_entry; uint64_t pte, page_mask; @@ -2167,7 +2181,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_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 =3D addr & VTD_PAGE_MASK_4K; entry->translated_addr =3D entry->iova; entry->addr_mask =3D ~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; } =20 /* @@ -3158,11 +3174,11 @@ static void vtd_pasid_cache_sync_locked(gpointer ke= y, gpointer value, } =20 if (!pc_entry->valid) { - pc_entry->pasid_entry =3D pe; pc_entry->valid =3D true; } else if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry)) { return; } + pc_entry->pasid_entry =3D pe; =20 vtd_switch_address_space(vtd_as); vtd_address_space_sync(vtd_as); --=20 2.52.0