From nobody Sat Nov 15 22:33:44 2025 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=1747826556; cv=none; d=zohomail.com; s=zohoarc; b=nkDG/xiWL88LOjj0Kc47Lwh1Z2vc95dyoD8fKe3q1DhZ3lCidU9Zr30qYe3HVqtXoyeuJypqmCto0GsZnFxBV5mYGrUidNOu1/PuBYP27dyF0+R7LgGwSw96yaHDLRCgkgyTAdZVZze3SpN3tSslZPYsQ5VivyK1Oc8fi+ManT8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747826556; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kq23I6OO6Qr3dZ9pgyWgD0+oiHDLRnTY62eB56/T9rw=; b=Al/HmS0vtsMIRKvVqr+spLXD0D98yy1FD0HoBWaW7zuhj6OtHH6SEctObi0k1V/gFp3plU6OshtaspgprRN0yEJ1nop2j1rQqyCKJsqBuhaGASjGTrk7ivAhi48Jn5F14Pv6E1VW3KKEAkS1ob0v48SWe8vaG8rRunjvQewKY7c= 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747826556637445.4334447421271; Wed, 21 May 2025 04:22:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uHhUA-0005wF-ST; Wed, 21 May 2025 07:19:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHhTj-0005Vo-4R for qemu-devel@nongnu.org; Wed, 21 May 2025 07:19:22 -0400 Received: from mgamail.intel.com ([192.198.163.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uHhTg-0006RC-27 for qemu-devel@nongnu.org; Wed, 21 May 2025 07:19:18 -0400 Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa108.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2025 04:19:15 -0700 Received: from spr-s2600bt.bj.intel.com ([10.240.192.127]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 May 2025 04:19:10 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1747826356; x=1779362356; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=KLjRZ+Hv72Y3pA0KOcUTZCjBQvdG45vZ8JtjLhnvJ+A=; b=SL3G64zuqtyDxtF7QWYY9hhSmYK+HEQAAbduTbVhSUFCMyS0WK9+pcEf PXr8bc+Er8X2uA6to/mHC2HwkYY5VV4kmIX4GZMamIAvI1RTMoDsMQEEu eroGUuOg8Rl+khNErHs4lr3MArZdMn9FpXM7AYk/mEmi51Eb9EQ6doWzw 6KaHhQjr2BWMtK+gFd5DbnV0mpXl6XV4KEWzRBWyi+/GYcuwoIqNx7Num 5nuT/jjYAsNBJ8r22H2EcEcVkhNLnzwoBBoikIUaaOBl6NVj8VhSL7Udj 6WkPq0Rs9bzaGIgljdN4gXzasx3tkootpWfr0YeZJNtzU6oosYHXL/9Nf g==; X-CSE-ConnectionGUID: B+cjEv68RF+NB+I0c1gecw== X-CSE-MsgGUID: JFRHimOYRqawviazBIrk6w== X-IronPort-AV: E=McAfee;i="6700,10204,11439"; a="49894991" X-IronPort-AV: E=Sophos;i="6.15,303,1739865600"; d="scan'208";a="49894991" X-CSE-ConnectionGUID: xYBNWGo1SZOU1uNKeJCF7Q== X-CSE-MsgGUID: 5oi/zXR/SDCKo/LYSEVakw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,303,1739865600"; d="scan'208";a="145158336" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, mst@redhat.com, jasowang@redhat.com, peterx@redhat.com, ddutile@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, shameerali.kolothum.thodi@huawei.com, joao.m.martins@oracle.com, clement.mathieu--drif@eviden.com, kevin.tian@intel.com, yi.l.liu@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost Subject: [PATCH rfcv3 08/21] intel_iommu: Optimize context entry cache utilization Date: Wed, 21 May 2025 19:14:38 +0800 Message-Id: <20250521111452.3316354-9-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250521111452.3316354-1-zhenzhong.duan@intel.com> References: <20250521111452.3316354-1-zhenzhong.duan@intel.com> 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=lists.gnu.org; Received-SPF: pass client-ip=192.198.163.14; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -45 X-Spam_score: -4.6 X-Spam_bar: ---- X-Spam_report: (-4.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.184, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-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: 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: 1747826557161116600 Content-Type: text/plain; charset="utf-8" There are many call sites referencing context entry by calling vtd_dev_to_context_entry() which will traverse the DMAR table. In most cases we can use cached context entry in vtd_as->context_cache_entry except when its entry is stale. Currently only global and domain context invalidation stale it. So introduce a helper function vtd_as_to_context_entry() to fetch from cache before trying with vtd_dev_to_context_entry(). Signed-off-by: Zhenzhong Duan --- hw/i386/intel_iommu.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index f0b1f90eff..a2f3250724 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1597,6 +1597,22 @@ static int vtd_dev_to_context_entry(IntelIOMMUState = *s, uint8_t bus_num, return 0; } =20 +static int vtd_as_to_context_entry(VTDAddressSpace *vtd_as, VTDContextEntr= y *ce) +{ + IntelIOMMUState *s =3D vtd_as->iommu_state; + uint8_t bus_num =3D pci_bus_num(vtd_as->bus); + uint8_t devfn =3D vtd_as->devfn; + VTDContextCacheEntry *cc_entry =3D &vtd_as->context_cache_entry; + + /* Try to fetch context-entry from cache first */ + if (cc_entry->context_cache_gen =3D=3D s->context_cache_gen) { + *ce =3D cc_entry->context_entry; + return 0; + } else { + return vtd_dev_to_context_entry(s, bus_num, devfn, ce); + } +} + static int vtd_sync_shadow_page_hook(const IOMMUTLBEvent *event, void *private) { @@ -1649,9 +1665,7 @@ static int vtd_address_space_sync(VTDAddressSpace *vt= d_as) return 0; } =20 - ret =3D vtd_dev_to_context_entry(vtd_as->iommu_state, - pci_bus_num(vtd_as->bus), - vtd_as->devfn, &ce); + ret =3D vtd_as_to_context_entry(vtd_as, &ce); if (ret) { if (ret =3D=3D -VTD_FR_CONTEXT_ENTRY_P) { /* @@ -1710,8 +1724,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as) assert(as); =20 s =3D as->iommu_state; - if (vtd_dev_to_context_entry(s, pci_bus_num(as->bus), as->devfn, - &ce)) { + if (vtd_as_to_context_entry(as, &ce)) { /* * Possibly failed to parse the context entry for some reason * (e.g., during init, or any guest configuration errors on @@ -2435,8 +2448,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUSta= te *s, uint16_t domain_id) vtd_iommu_unlock(s); =20 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { - if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), - vtd_as->devfn, &ce) && + if (!vtd_as_to_context_entry(vtd_as, &ce) && domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pasid)) { vtd_address_space_sync(vtd_as); } @@ -2458,8 +2470,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOM= MUState *s, hwaddr size =3D (1 << am) * VTD_PAGE_SIZE; =20 QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { - ret =3D vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), - vtd_as->devfn, &ce); + ret =3D vtd_as_to_context_entry(vtd_as, &ce); if (!ret && domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pas= id)) { uint32_t rid2pasid =3D PCI_NO_PASID; =20 @@ -2966,8 +2977,7 @@ static void vtd_piotlb_pasid_invalidate(IntelIOMMUSta= te *s, vtd_iommu_unlock(s); =20 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { - if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), - vtd_as->devfn, &ce) && + if (!vtd_as_to_context_entry(vtd_as, &ce) && domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pasid)) { uint32_t rid2pasid =3D VTD_CE_GET_RID2PASID(&ce); =20 @@ -4146,7 +4156,7 @@ static void vtd_report_ir_illegal_access(VTDAddressSp= ace *vtd_as, assert(vtd_as->pasid !=3D PCI_NO_PASID); =20 /* Try out best to fetch FPD, we can't do anything more */ - if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) =3D=3D 0) { + if (vtd_as_to_context_entry(vtd_as, &ce) =3D=3D 0) { is_fpd_set =3D ce.lo & VTD_CONTEXT_ENTRY_FPD; if (!is_fpd_set && s->root_scalable) { vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, vtd_as->pasid); @@ -4506,7 +4516,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu= _mr, IOMMUNotifier *n) /* replay is protected by BQL, page walk will re-setup it safely */ iova_tree_remove(vtd_as->iova_tree, map); =20 - if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) =3D=3D 0) { + if (vtd_as_to_context_entry(vtd_as, &ce) =3D=3D 0) { trace_vtd_replay_ce_valid(s->root_scalable ? "scalable mode" : "legacy mode", bus_n, PCI_SLOT(vtd_as->devfn), --=20 2.34.1