From nobody Mon Nov 23 04:28:04 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=1794471355; cv=none; d=zohomail.com; s=zohoarc; b=bcuogFhzEowFxAdPWZ1Z79RA9qJvATAlhd/E+Rv3uzE1PrQ6QSk6s/cTWQZfDIY6IztIdOkaA16wo7fpuxsnNzjpsYDQIwTH7LPCifIBIDxm+y5C8wEDPefCcy32Au5WbsBDzohrkwiJ4n0qmLvzynWz1yCvMcD1KYcY+GPf3vg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1794471355; 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=MA8IHZJ/xTMXiE3wxbmAkcLAU3LIUSF0h/68Eek8r3E=; b=WborEA69lSxSaqhbxzW0bKwZ0m73kUa5PGACdWbFADrGI7QDwbs1P7mrRixXolsTlwm1/DbrTU3YZQs/Q8gXynquHnM+z+vDIQ2hn/hpG9L67oj0ZLn1ssWs7fqiZaE4bSjxH+rjQdu1E2xM9kgM48UMMXrE6YT7tp6UhXQLOq4= 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 1794471354658789.9107380989353; Thu, 12 Nov 2026 00:15:54 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xRPy3-0004Ez-Bk; Thu, 12 Nov 2026 03:15:35 -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 1xRPxw-0004BU-1o for qemu-devel@nongnu.org; Thu, 12 Nov 2026 03:15:31 -0500 Received: from mgamail.intel.com ([198.175.65.9]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xRPxs-0000Il-48 for qemu-devel@nongnu.org; Thu, 12 Nov 2026 03:15:26 -0500 Received: from fmviesa012.fm.intel.com ([10.60.135.152]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Nov 2026 00:15:20 -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; 12 Nov 2026 00:15:17 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1794471324; x=1826007324; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=kpCvyj78fixhRKyYwxCpHSzphQ0fJQ+Eb1uySvbXxIA=; b=b0ItJBi1SA/60eMl8l/e1SaX0vY+J57kCjg31on7EHuY+3gnACGZK5I1 Mu45sVg6M/jO3ONCdTpB3vMry8Pmwb4JNCwmcLv64tKVGIf4WvUSqtXOP 5xX6NsLbTGVWWbXzjy/9BI1xlDqdtCjJqF2TBbBTGCcfCCd6YLr7pa+V1 oa0980KtRwomHTvnMaX84qBhT69C20r60oH+ogT8kP0sNfPRyDoMVpcVn ZJXGU/BQopNGyeziWtBqo+rXK6GGegFo1ysp21ZgR/sF6rV3UyYXxP1Cw 7GUTC1/Q6bqOh0l5QuqUfgGQBWuhPClfis5WWZ8jfF10enI53u5fkN5Ib A==; X-CSE-ConnectionGUID: pCoJLL5rSvO8P0ZrbPj2sw== X-CSE-MsgGUID: GiRTmE4MTWGpGSfcToiDrw== X-IronPort-AV: E=McAfee;i="6800,10657,11963"; a="3114187" X-IronPort-AV: E=Sophos;i="6.27,209,1787036400"; d="scan'208";a="3114187" X-CSE-ConnectionGUID: ImbtJty3TiCdVShnnmzjXQ== X-CSE-MsgGUID: LEE2lRdaT3+hOA/sC9heQA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,209,1787036400"; d="scan'208";a="10326641" 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 v3] intel_iommu: Keep PASID cache consistent with PT fast path Date: Thu, 12 Nov 2026 16:15:09 +0800 Message-ID: <20261112081509.442872-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.9; 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: 1794471357453158500 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, pass vtd_as to vtd_try_enable_pt_fast_path() instead of pick it from PASID 0. Opportunistically clarify the invalidation modes in the fast-path comment and merge trace event vtd_translate_pt and vtd_pt_enable_fast_path into vtd_fast_path_translate_pt. 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 Reviewed-by: Clement Mathieu--Drif Reviewed-by: Yi Liu --- v3: - introduce a new helper vtd_try_enable_pt_fast_path (Clement) v2: - move "VTDPASIDCacheEntry *pc_entry" in the branch it is used (Clement) hw/i386/intel_iommu.c | 62 ++++++++++++++++++++++++------------------- hw/i386/trace-events | 3 +-- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index b7da5d086d..a5ff89a11a 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. */ @@ -1701,8 +1706,8 @@ static bool vtd_switch_address_space(VTDAddressSpace = *as) assert(as); =20 s =3D as->iommu_state; - use_iommu =3D s->dmar_enabled && !vtd_as_pt_enabled(as); pt =3D s->dmar_enabled && vtd_as_pt_enabled(as); + use_iommu =3D s->dmar_enabled && !pt; =20 /* * When guest enables scalable mode and sets up first stage page table, @@ -1887,23 +1892,34 @@ 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 bool vtd_try_enable_pt_fast_path(IntelIOMMUState *s, + VTDAddressSpace *vtd_as, + VTDContextEntry *ce, uint32_t pasi= d) { - VTDAddressSpace *vtd_as; - bool success =3D false; + VTDPASIDEntry pe; =20 - vtd_as =3D vtd_get_as_by_sid(s, source_id); - if (!vtd_as) { - goto out; + if (!vtd_dev_pt_enabled(s, ce, pasid, &pe)) { + return false; } =20 - if (vtd_switch_address_space(vtd_as) =3D=3D false) { - /* We switched off IOMMU region successfully. */ - success =3D true; + /* + * When this happens, it means firstly caching-mode is not + * enabled, and this is the first passthrough translation for + * the device. Let's enable the fast path for passthrough. + * + * When passthrough is disabled again for the device, we can + * capture it via context-cache invalidation in legacy mode or + * PASID-cache invalidation in scalable mode, then the IOMMU + * region can be swapped back. + */ + if (!vtd_switch_address_space(vtd_as) && 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: - trace_vtd_pt_enable_fast_path(source_id, success); + return true; } =20 /* @@ -2167,23 +2183,12 @@ 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_try_enable_pt_fast_path(s, vtd_as, &ce, pasid)) { entry->iova =3D addr & VTD_PAGE_MASK_4K; entry->translated_addr =3D entry->iova; entry->addr_mask =3D ~VTD_PAGE_MASK_4K; entry->perm =3D IOMMU_RW; - trace_vtd_translate_pt(source_id, entry->iova); - - /* - * When this happens, it means firstly caching-mode is not - * enabled, and this is the first passthrough translation for - * 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. - */ - vtd_pt_enable_fast_path(s, source_id); + trace_vtd_fast_path_translate_pt(source_id, entry->iova); vtd_iommu_unlock(s); return true; } @@ -3133,6 +3138,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 +3164,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); diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 7bf4cddd38..3337a7e19c 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -47,8 +47,7 @@ vtd_page_walk_skip_read(uint64_t iova, uint64_t next) "Pa= ge walk skip iova 0x%"P vtd_page_walk_skip_reserve(uint64_t iova, uint64_t next) "Page walk skip i= ova 0x%"PRIx64" - 0x%"PRIx64" due to rsrv set" vtd_switch_address_space(uint8_t bus, uint8_t slot, uint8_t fn, bool on) "= Device %02x:%02x.%x switching address space (iommu enabled=3D%d)" vtd_as_unmap_whole(uint8_t bus, uint8_t slot, uint8_t fn, uint64_t iova, u= int64_t size) "Device %02x:%02x.%x start 0x%"PRIx64" size 0x%"PRIx64 -vtd_translate_pt(uint16_t sid, uint64_t addr) "source id 0x%"PRIx16", iova= 0x%"PRIx64 -vtd_pt_enable_fast_path(uint16_t sid, bool success) "sid 0x%"PRIx16" %d" +vtd_fast_path_translate_pt(uint16_t sid, uint64_t addr) "source id 0x%"PRI= x16", iova 0x%"PRIx64 vtd_irq_generate(uint64_t addr, uint64_t data) "addr 0x%"PRIx64" data 0x%"= PRIx64 vtd_reg_read(uint64_t addr, uint64_t size) "addr 0x%"PRIx64" size 0x%"PRIx= 64 vtd_reg_write(uint64_t addr, uint64_t size, uint64_t val) "addr 0x%"PRIx64= " size 0x%"PRIx64" value 0x%"PRIx64 --=20 2.52.0