From nobody Thu May 2 14:04:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499847336448912.7799393028176; Wed, 12 Jul 2017 01:15:36 -0700 (PDT) Received: from localhost ([::1]:50985 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCo3-0001wf-4q for importer@patchew.org; Wed, 12 Jul 2017 04:15:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50731) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCmU-0000ql-Oo for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:13:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVCmR-0001r6-Ha for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:13:58 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47642) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVCmR-0001qJ-C1 for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:13:55 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 74E6E4E33D for ; Wed, 12 Jul 2017 08:13:54 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-238.nay.redhat.com [10.66.14.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 02DA060841; Wed, 12 Jul 2017 08:13:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 74E6E4E33D Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=peterx@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 74E6E4E33D From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 12 Jul 2017 16:13:40 +0800 Message-Id: <1499847223-8078-2-git-send-email-peterx@redhat.com> In-Reply-To: <1499847223-8078-1-git-send-email-peterx@redhat.com> References: <1499847223-8078-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Wed, 12 Jul 2017 08:13:54 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/4] intel_iommu: fix VTD_PAGE_MASK X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , peterx@redhat.com, "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" IOMMUTLBEntry.iova is returned incorrectly in one PT path (though mostly we cannot really trigger this path, even if we do, we are mostly disgarding this value, so it didn't break anything). Fix it by converting the VTD_PAGE_MASK into normal definition (normally it should be pfn mask, not offset mask), then switch the other user of it. Fixes: b93130 ("intel_iommu: cleanup vtd_{do_}iommu_translate()") Signed-off-by: Peter Xu --- hw/i386/intel_iommu.c | 2 +- hw/i386/intel_iommu_internal.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 88dc042..392da45 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1141,7 +1141,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, if (vtd_ce_get_type(&ce) =3D=3D VTD_CONTEXT_TT_PASS_THROUGH) { entry->iova =3D addr & VTD_PAGE_MASK; entry->translated_addr =3D entry->iova; - entry->addr_mask =3D VTD_PAGE_MASK; + entry->addr_mask =3D ~VTD_PAGE_MASK; entry->perm =3D IOMMU_RW; trace_vtd_translate_pt(source_id, entry->iova); =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index f50ecd8..754cf8a 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -384,7 +384,7 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo; /* Pagesize of VTD paging structures, including root and context tables */ #define VTD_PAGE_SHIFT 12 #define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT) -#define VTD_PAGE_MASK (VTD_PAGE_SIZE - 1) +#define VTD_PAGE_MASK (~(VTD_PAGE_SIZE - 1)) =20 #define VTD_PAGE_SHIFT_4K 12 #define VTD_PAGE_MASK_4K (~((1ULL << VTD_PAGE_SHIFT_4K) - 1)) --=20 2.7.4 From nobody Thu May 2 14:04:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499847337827117.52449037290808; Wed, 12 Jul 2017 01:15:37 -0700 (PDT) Received: from localhost ([::1]:50987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCo4-0001yq-FM for importer@patchew.org; Wed, 12 Jul 2017 04:15:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50755) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCmW-0000sW-4X for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVCmV-0001tJ-2U for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57866) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVCmU-0001rl-Px for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:13:59 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DA11D7F3E5 for ; Wed, 12 Jul 2017 08:13:57 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-238.nay.redhat.com [10.66.14.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA39360841; Wed, 12 Jul 2017 08:13:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com DA11D7F3E5 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=peterx@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com DA11D7F3E5 From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 12 Jul 2017 16:13:41 +0800 Message-Id: <1499847223-8078-3-git-send-email-peterx@redhat.com> In-Reply-To: <1499847223-8078-1-git-send-email-peterx@redhat.com> References: <1499847223-8078-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 12 Jul 2017 08:13:58 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 2/4] intel_iommu: let iotlb size tunable X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , peterx@redhat.com, "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We were having static IOTLB size as 1024. Let it be a tunable. We can also turns IOTLB off if we want, by specify the size as zero. The tunable is named as "x-iotlb-size" since that should not really be something used by user yet, but mostly for debugging purpose now. Signed-off-by: Peter Xu --- hw/i386/intel_iommu.c | 11 ++++++++++- hw/i386/intel_iommu_internal.h | 1 - include/hw/i386/intel_iommu.h | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 392da45..9c36866 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -222,6 +222,10 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState= *s, uint16_t source_id, uint64_t key; int level; =20 + if (s->iotlb_size =3D=3D 0) { + return NULL; + } + for (level =3D VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) { key =3D vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level), source_id, level); @@ -244,8 +248,12 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint1= 6_t source_id, uint64_t *key =3D g_malloc(sizeof(*key)); uint64_t gfn =3D vtd_get_iotlb_gfn(addr, level); =20 + if (s->iotlb_size =3D=3D 0) { + return; + } + trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); - if (g_hash_table_size(s->iotlb) >=3D VTD_IOTLB_MAX_SIZE) { + if (g_hash_table_size(s->iotlb) >=3D s->iotlb_size) { trace_vtd_iotlb_reset("iotlb exceeds size limit"); vtd_reset_iotlb(s); } @@ -2397,6 +2405,7 @@ static Property vtd_properties[] =3D { ON_OFF_AUTO_AUTO), DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false), DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), + DEFINE_PROP_UINT16("x-iotlb-size", IntelIOMMUState, iotlb_size, 1024), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 754cf8a..2d77249 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -116,7 +116,6 @@ /* The shift of source_id in the key of IOTLB hash table */ #define VTD_IOTLB_SID_SHIFT 36 #define VTD_IOTLB_LVL_SHIFT 52 -#define VTD_IOTLB_MAX_SIZE 1024 /* Max size of the hash table = */ =20 /* IOTLB_REG */ #define VTD_TLB_GLOBAL_FLUSH (1ULL << 60) /* Global invalidation */ diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 3e51876..a57f419 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -288,6 +288,7 @@ struct IntelIOMMUState { =20 uint32_t context_cache_gen; /* Should be in [1,MAX] */ GHashTable *iotlb; /* IOTLB */ + uint16_t iotlb_size; /* IOTLB max cache entries */ =20 MemoryRegionIOMMUOps iommu_ops; GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* r= eference */ --=20 2.7.4 From nobody Thu May 2 14:04:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1499847459299907.6300285241988; Wed, 12 Jul 2017 01:17:39 -0700 (PDT) Received: from localhost ([::1]:50996 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCq2-0003Sk-4A for importer@patchew.org; Wed, 12 Jul 2017 04:17:38 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50782) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCmX-0000wd-VE for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVCmW-0001uF-UV for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51986) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVCmW-0001th-M2 for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:00 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B723580F6D for ; Wed, 12 Jul 2017 08:13:59 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-238.nay.redhat.com [10.66.14.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4942A60841; Wed, 12 Jul 2017 08:13:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B723580F6D Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=peterx@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B723580F6D From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 12 Jul 2017 16:13:42 +0800 Message-Id: <1499847223-8078-4-git-send-email-peterx@redhat.com> In-Reply-To: <1499847223-8078-1-git-send-email-peterx@redhat.com> References: <1499847223-8078-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 12 Jul 2017 08:13:59 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 3/4] intel_iommu: use access_flags for iotlb X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , peterx@redhat.com, "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It was cached by read/write separately. Let's merge them. Signed-off-by: Peter Xu --- hw/i386/intel_iommu.c | 15 +++++++-------- include/hw/i386/intel_iommu.h | 3 +-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 9c36866..e17340c 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -241,8 +241,7 @@ out: =20 static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, uint16_t domain_id, hwaddr addr, uint64_t slp= te, - bool read_flags, bool write_flags, - uint32_t level) + uint8_t access_flags, uint32_t level) { VTDIOTLBEntry *entry =3D g_malloc(sizeof(*entry)); uint64_t *key =3D g_malloc(sizeof(*key)); @@ -261,8 +260,7 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16= _t source_id, entry->gfn =3D gfn; entry->domain_id =3D domain_id; entry->slpte =3D slpte; - entry->read_flags =3D read_flags; - entry->write_flags =3D write_flags; + entry->access_flags =3D access_flags; entry->mask =3D vtd_slpt_level_page_mask(level); *key =3D vtd_get_iotlb_key(gfn, source_id, level); g_hash_table_replace(s->iotlb, key, entry); @@ -1095,6 +1093,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, bool is_fpd_set =3D false; bool reads =3D true; bool writes =3D true; + uint8_t access_flags; VTDIOTLBEntry *iotlb_entry; =20 /* @@ -1109,8 +1108,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, iotlb_entry->domain_id); slpte =3D iotlb_entry->slpte; - reads =3D iotlb_entry->read_flags; - writes =3D iotlb_entry->write_flags; + access_flags =3D iotlb_entry->access_flags; page_mask =3D iotlb_entry->mask; goto out; } @@ -1180,13 +1178,14 @@ static bool vtd_do_iommu_translate(VTDAddressSpace = *vtd_as, PCIBus *bus, } =20 page_mask =3D vtd_slpt_level_page_mask(level); + access_flags =3D IOMMU_ACCESS_FLAG(reads, writes); vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slp= te, - reads, writes, level); + access_flags, level); out: entry->iova =3D addr & page_mask; entry->translated_addr =3D vtd_get_slpte_addr(slpte) & page_mask; entry->addr_mask =3D ~page_mask; - entry->perm =3D IOMMU_ACCESS_FLAG(reads, writes); + entry->perm =3D access_flags; return true; =20 error: diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index a57f419..1b51c9f 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -101,8 +101,7 @@ struct VTDIOTLBEntry { uint16_t domain_id; uint64_t slpte; uint64_t mask; - bool read_flags; - bool write_flags; + uint8_t access_flags; }; =20 /* VT-d Source-ID Qualifier types */ --=20 2.7.4 From nobody Thu May 2 14:04:46 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149984752976439.69040332604891; Wed, 12 Jul 2017 01:18:49 -0700 (PDT) Received: from localhost ([::1]:50999 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCrA-00042V-5f for importer@patchew.org; Wed, 12 Jul 2017 04:18:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVCma-0000yw-K9 for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVCmY-0001vP-Vj for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46350) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVCmY-0001v3-Lw for qemu-devel@nongnu.org; Wed, 12 Jul 2017 04:14:02 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B2626C057FAB for ; Wed, 12 Jul 2017 08:14:01 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-238.nay.redhat.com [10.66.14.238]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27DFB60841; Wed, 12 Jul 2017 08:13:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B2626C057FAB Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=peterx@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com B2626C057FAB From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 12 Jul 2017 16:13:43 +0800 Message-Id: <1499847223-8078-5-git-send-email-peterx@redhat.com> In-Reply-To: <1499847223-8078-1-git-send-email-peterx@redhat.com> References: <1499847223-8078-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 12 Jul 2017 08:14:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , peterx@redhat.com, "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry. This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot. After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations. Signed-off-by: Peter Xu --- hw/i386/intel_iommu.c | 75 +++++++++++++++++++++++++-------------= ---- hw/i386/intel_iommu_internal.h | 8 ----- hw/i386/trace-events | 1 - include/hw/i386/intel_iommu.h | 6 +++- 4 files changed, 50 insertions(+), 40 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index e17340c..6320dea 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -37,6 +37,9 @@ #include "kvm_i386.h" #include "trace.h" =20 +#define FOREACH_IOTLB_SAFE(entry, s, entry_n) \ + QTAILQ_FOREACH_SAFE(entry, &(s)->iotlb_head, link, entry_n) + static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val, uint64_t wmask, uint64_t w1cmask) { @@ -139,14 +142,6 @@ static guint vtd_uint64_hash(gconstpointer v) return (guint)*(const uint64_t *)v; } =20 -static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value, - gpointer user_data) -{ - VTDIOTLBEntry *entry =3D (VTDIOTLBEntry *)value; - uint16_t domain_id =3D *(uint16_t *)user_data; - return entry->domain_id =3D=3D domain_id; -} - /* The shift of an addr for a certain level of paging structure */ static inline uint32_t vtd_slpt_level_shift(uint32_t level) { @@ -159,18 +154,6 @@ static inline uint64_t vtd_slpt_level_page_mask(uint32= _t level) return ~((1ULL << vtd_slpt_level_shift(level)) - 1); } =20 -static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value, - gpointer user_data) -{ - VTDIOTLBEntry *entry =3D (VTDIOTLBEntry *)value; - VTDIOTLBPageInvInfo *info =3D (VTDIOTLBPageInvInfo *)user_data; - uint64_t gfn =3D (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask; - uint64_t gfn_tlb =3D (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K; - return (entry->domain_id =3D=3D info->domain_id) && - (((entry->gfn & info->mask) =3D=3D gfn) || - (entry->gfn =3D=3D gfn_tlb)); -} - /* Reset all the gen of VTDAddressSpace to zero and set the gen of * IntelIOMMUState to 1. */ @@ -201,6 +184,7 @@ static void vtd_reset_iotlb(IntelIOMMUState *s) { assert(s->iotlb); g_hash_table_remove_all(s->iotlb); + QTAILQ_INIT(&s->iotlb_head); } =20 static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id, @@ -231,6 +215,9 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState = *s, uint16_t source_id, source_id, level); entry =3D g_hash_table_lookup(s->iotlb, &key); if (entry) { + /* Move the entry to the head of MRU list */ + QTAILQ_REMOVE(&s->iotlb_head, entry, link); + QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link); goto out; } } @@ -239,11 +226,23 @@ out: return entry; } =20 +static void vtd_iotlb_remove_entry(IntelIOMMUState *s, VTDIOTLBEntry *entr= y) +{ + uint64_t key =3D entry->key; + + /* + * To remove an entry, we need to both remove it from the MRU + * list, and also from the hash table. + */ + QTAILQ_REMOVE(&s->iotlb_head, entry, link); + g_hash_table_remove(s->iotlb, &key); +} + static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, uint16_t domain_id, hwaddr addr, uint64_t slp= te, uint8_t access_flags, uint32_t level) { - VTDIOTLBEntry *entry =3D g_malloc(sizeof(*entry)); + VTDIOTLBEntry *entry =3D g_new0(VTDIOTLBEntry, 1), *last; uint64_t *key =3D g_malloc(sizeof(*key)); uint64_t gfn =3D vtd_get_iotlb_gfn(addr, level); =20 @@ -253,8 +252,9 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16= _t source_id, =20 trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); if (g_hash_table_size(s->iotlb) >=3D s->iotlb_size) { - trace_vtd_iotlb_reset("iotlb exceeds size limit"); - vtd_reset_iotlb(s); + /* Remove the Least Recently Used cache */ + last =3D QTAILQ_LAST(&s->iotlb_head, VTDIOTLBEntryHead); + vtd_iotlb_remove_entry(s, last); } =20 entry->gfn =3D gfn; @@ -263,7 +263,11 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint1= 6_t source_id, entry->access_flags =3D access_flags; entry->mask =3D vtd_slpt_level_page_mask(level); *key =3D vtd_get_iotlb_key(gfn, source_id, level); + entry->key =3D *key; g_hash_table_replace(s->iotlb, key, entry); + + /* Update MRU list */ + QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link); } =20 /* Given the reg addr of both the message data and address, generate an @@ -1354,11 +1358,15 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUS= tate *s, uint16_t domain_id) IntelIOMMUNotifierNode *node; VTDContextEntry ce; VTDAddressSpace *vtd_as; + VTDIOTLBEntry *entry, *entry_n; =20 trace_vtd_inv_desc_iotlb_domain(domain_id); =20 - g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain, - &domain_id); + FOREACH_IOTLB_SAFE(entry, s, entry_n) { + if (entry->domain_id =3D=3D domain_id) { + vtd_iotlb_remove_entry(s, entry); + } + } =20 QLIST_FOREACH(node, &s->notifiers_list, next) { vtd_as =3D node->vtd_as; @@ -1400,15 +1408,22 @@ static void vtd_iotlb_page_invalidate_notify(IntelI= OMMUState *s, static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_= id, hwaddr addr, uint8_t am) { - VTDIOTLBPageInvInfo info; + VTDIOTLBEntry *entry, *entry_n; + uint64_t gfn, mask; =20 trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am); =20 assert(am <=3D VTD_MAMV); - info.domain_id =3D domain_id; - info.addr =3D addr; - info.mask =3D ~((1 << am) - 1); - g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); + + mask =3D ~((1 << am) - 1); + gfn =3D (addr >> VTD_PAGE_SHIFT) & mask; + + FOREACH_IOTLB_SAFE(entry, s, entry_n) { + if (entry->domain_id =3D=3D domain_id && (entry->gfn & mask) =3D= =3D gfn) { + vtd_iotlb_remove_entry(s, entry); + } + } + vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); } =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 2d77249..76efc66 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -372,14 +372,6 @@ typedef union VTDInvDesc VTDInvDesc; #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8 =20 -/* Information about page-selective IOTLB invalidate */ -struct VTDIOTLBPageInvInfo { - uint16_t domain_id; - uint64_t addr; - uint8_t mask; -}; -typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo; - /* Pagesize of VTD paging structures, including root and context tables */ #define VTD_PAGE_SHIFT 12 #define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT) diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 42d8a7e..b552815 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -34,7 +34,6 @@ vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t = slpte, uint16_t domain) vtd_iotlb_page_update(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_= t domain) "IOTLB page update sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRI= x64" domain 0x%"PRIx16 vtd_iotlb_cc_hit(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, = uint32_t gen) "IOTLB context hit bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"P= RIx64" low 0x%"PRIx64" gen %"PRIu32 vtd_iotlb_cc_update(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t lo= w, uint32_t gen1, uint32_t gen2) "IOTLB context update bus 0x%"PRIx8" devfn= 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32" -> gen %"PRIu32 -vtd_iotlb_reset(const char *reason) "IOTLB reset (reason: %s)" vtd_fault_disabled(void) "Fault processing disabled for context entry" vtd_replay_ce_valid(uint8_t bus, uint8_t dev, uint8_t fn, uint16_t domain,= uint64_t hi, uint64_t lo) "replay valid context device %02"PRIx8":%02"PRIx= 8".%02"PRIx8" domain 0x%"PRIx16" hi 0x%"PRIx64" lo 0x%"PRIx64 vtd_replay_ce_invalid(uint8_t bus, uint8_t dev, uint8_t fn) "replay invali= d context device %02"PRIx8":%02"PRIx8".%02"PRIx8 diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 1b51c9f..d2caab3 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -98,9 +98,11 @@ struct VTDBus { =20 struct VTDIOTLBEntry { uint64_t gfn; - uint16_t domain_id; uint64_t slpte; uint64_t mask; + uint64_t key; + QTAILQ_ENTRY(VTDIOTLBEntry) link; + uint16_t domain_id; uint8_t access_flags; }; =20 @@ -288,6 +290,8 @@ struct IntelIOMMUState { uint32_t context_cache_gen; /* Should be in [1,MAX] */ GHashTable *iotlb; /* IOTLB */ uint16_t iotlb_size; /* IOTLB max cache entries */ + /* Head of IOTLB MRU list */ + QTAILQ_HEAD(VTDIOTLBEntryHead, VTDIOTLBEntry) iotlb_head; =20 MemoryRegionIOMMUOps iommu_ops; GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* r= eference */ --=20 2.7.4