From nobody Mon Nov 23 04:28:06 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=1793699767; cv=none; d=zohomail.com; s=zohoarc; b=NE+Aa12dBssrJp4M1ZTEGKvjDBlLK/MdVcKHzXlclrg7BNRO0tRlCLAf6PVR9LUliMEZ01Lmyf+lCteBf6JkjfgDpjGz8RQqRNy7hYLUcyHyk3vlBEBgemurMJN1Kw2L6npOn940m8G8txRjw8ZKXN3DgE7hVwEZwCniLQh96cM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793699767; 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=vGnQxFA3ZmUtaSFcE26SvaiYRoxNpO5FB7uqZz/iE24=; b=avAdl9DOIXhj1jpj5Iw4hXs/yMXjWz/8fhS9HvB3SWKBrlRjyjW4ijYYRu6QxZx2GciBOWT/6ZVXVYUDYTbTLyYe8Eafbc9euWMmc2Fq6TyVuPyNv7TOCBevl/YeBmXnEFN+lAodrNQL9TONKbUs/S+CA9IBT1nT5VDb6l5xwHs= 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 1793699767386445.38914144952275; Tue, 3 Nov 2026 01:56:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xOBEf-0000yp-7U; Tue, 03 Nov 2026 04:55:21 -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 1xOBEc-0000yb-QD for qemu-devel@nongnu.org; Tue, 03 Nov 2026 04:55:18 -0500 Received: from mgamail.intel.com ([198.175.65.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xOBEa-0004K8-Ce for qemu-devel@nongnu.org; Tue, 03 Nov 2026 04:55:18 -0500 Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Nov 2026 01:55:12 -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 01:55:10 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1793699716; x=1825235716; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=x39JTdyv4F07kClYlzgbjDeJfENien04cz3ebIkRkWA=; b=VLNfVbvk6Qy8Jp7LYtMJUIudFgcypJWbOb5zO9TmiNqUvoGRwdIBPWek NOuB/mcA59kj9yj9BsvzDMLKNlAIczYIS/QVzPJsZJZIQ8++05S4tOVDA /e56JZoiSpLkSXOuqCRMfMGOAapgbKD6uL+dAOB5DVFMYZdR/j6f9yS6k i1fSZbPaM6UOL+MoDjNsNsEqHlXXh9jtomv+wg2+qXVjpHQcAgz1RJ9cn CQE+FjrS2eO5cfQTyzzfCjPm86lEPVTDKflya9D2duZPVUKKtMokMC1yO D7ZRaaGbbno8H9UVHpa5KxGmXcBuXnxwyLA4ZAHQImR7aE4Jf0m7RkDGY w==; X-CSE-ConnectionGUID: bvRta9QOR06AUfFoMoHJCQ== X-CSE-MsgGUID: BltNjXHFQb+nNSV396uhQQ== X-IronPort-AV: E=McAfee;i="6800,10657,11954"; a="2429986" X-IronPort-AV: E=Sophos;i="6.27,193,1787036400"; d="scan'208";a="2429986" X-CSE-ConnectionGUID: k3IAS8RlQRKMY7mbAydBTw== X-CSE-MsgGUID: 1Ksd3Oz3TVWopPKhoj1GQQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,193,1787036400"; d="scan'208";a="6283765" 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] intel_iommu: Keep PASID cache consistent with PT fast path Date: Tue, 3 Nov 2026 17:54:44 +0800 Message-ID: <20261103095444.1218225-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=198.175.65.18; 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: 1793699771522158500 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") Signed-off-by: Zhenzhong Duan Reported-by: Clement Mathieu--Drif 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..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 *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; @@ -1898,8 +1904,15 @@ static void vtd_pt_enable_fast_path(IntelIOMMUState = *s, uint16_t source_id) } =20 if (vtd_switch_address_space(vtd_as) =3D=3D false) { + VTDPASIDCacheEntry *pc_entry =3D &vtd_as->pasid_cache_entry; + /* We switched off IOMMU region successfully. */ success =3D true; + + if (s->root_scalable) { + 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