From nobody Tue May 14 18:29:24 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1561358474; cv=none; d=zoho.com; s=zohoarc; b=hdG6hLZ9HTfa6AqlywG0I9J7JRIxr8RbZqK56Olp7eimhMJ2je3ef02pj1908b4Sh4cNEFsw3YI9L0N2vMYDtqKeN9agJ4FpHZeLyR8XcfAtB75TpThTyBwU2mtt22mLz0guPRZxZTx+1kse8r2NJvHSROa9aFKzLj8ANqOTniE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561358474; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=pJwZd5/s+9xvTF0JoVH2awVd7ETJ3d0OHTRgLT+wGvQ=; b=Kft8Pbh8/Kvf9p12/f2xobJXWNHXuPIBdmFEZLrzmv2j379GqOOzTXkd0yTe0Ie1crflFL5vwoIUCYrQvUg7txPwg+qfMtZfZjiSOMfgB3PFW3F2yzSimfSji2ncQX6JqoIERaWB+WEbywDnJtjbs2JHSqz1JHYcvetw7xTyLko= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561358474572998.0883002018794; Sun, 23 Jun 2019 23:41:14 -0700 (PDT) Received: from localhost ([::1]:48268 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfIf9-0001eo-4n for importer@patchew.org; Mon, 24 Jun 2019 02:41:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43505) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfIc5-0007YE-8Z for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:38:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hfIc3-0004j4-Hg for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:38:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53694) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hfIc3-0004cb-B3 for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:37: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 AC15F85A07; Mon, 24 Jun 2019 06:37:54 +0000 (UTC) Received: from xz-x1.redhat.com (ovpn-12-60.pek2.redhat.com [10.72.12.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5DBBE608E4; Mon, 24 Jun 2019 06:37:51 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Mon, 24 Jun 2019 14:37:32 +0800 Message-Id: <20190624063733.22079-2-peterx@redhat.com> In-Reply-To: <20190624063733.22079-1-peterx@redhat.com> References: <20190624063733.22079-1-peterx@redhat.com> MIME-Version: 1.0 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.26]); Mon, 24 Jun 2019 06:37:54 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] intel_iommu: Fix incorrect "end" for vtd_address_space_unmap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yan Zhao , "Michael S . Tsirkin" , Jason Wang , peterx@redhat.com, Auger Eric , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Yan Zhao IOMMUNotifier is with inclusive ranges, so we should check against (VTD_ADDRESS_SIZE(s->aw_bits) - 1). Signed-off-by: Yan Zhao [peterx: split from another bigger patch] Signed-off-by: Peter Xu Reviewed-by: Eric Auger --- hw/i386/intel_iommu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 44b1231157..719ce19ab3 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3379,12 +3379,12 @@ static void vtd_address_space_unmap(VTDAddressSpace= *as, IOMMUNotifier *n) * VT-d spec), otherwise we need to consider overflow of 64 bits. */ =20 - if (end > VTD_ADDRESS_SIZE(s->aw_bits)) { + if (end > VTD_ADDRESS_SIZE(s->aw_bits) - 1) { /* * Don't need to unmap regions that is bigger than the whole * VT-d supported address space size */ - end =3D VTD_ADDRESS_SIZE(s->aw_bits); + end =3D VTD_ADDRESS_SIZE(s->aw_bits) - 1; } =20 assert(start <=3D end); --=20 2.21.0 From nobody Tue May 14 18:29:24 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1561358368; cv=none; d=zoho.com; s=zohoarc; b=TwWPCDTpacyDTGD3zPC5CrCx7Sm+Ql+FhjoaIeRCPJYXCoG5RgmfMJYT/IqcIeH1AYg+Tim7cNlJm4llFLZ5NgtuqMkp+FIWIHNik6htk6xATP5J/y4wyBYDumv8a7fwCJB3PJfTp4ku20cCqpy5xGz7qxqvHAgfav96k6H996Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561358368; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=boAO8YgipMnrDzosnyTSDEWZ9ulbVHvHrSyTKb5nxuM=; b=CnfoyZ44ZR3J1HwG42zQWCOznL1yqgdDB9mA6wOPQL4egcDEdUm3QQofK0pS9xyTWLjwp0WGoU3l/Sqqcl/NtEh1jW1KDUNGtZ7pMG7v/Ul1Pbupbs7kBKNBiWaUxbFgRtrZ3OZJ6z8T789i/YLjtIDNy1YbdRrsW3iqbZMopoE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1561358368853783.2263825650542; Sun, 23 Jun 2019 23:39:28 -0700 (PDT) Received: from localhost ([::1]:48260 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfIdN-0008WD-OS for importer@patchew.org; Mon, 24 Jun 2019 02:39:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43578) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hfIcL-0007c8-Jv for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:38:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hfIcK-0005Mp-3X for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:38:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54060) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hfIcH-0005G1-Bi for qemu-devel@nongnu.org; Mon, 24 Jun 2019 02:38:15 -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 AD010882F5; Mon, 24 Jun 2019 06:38:07 +0000 (UTC) Received: from xz-x1.redhat.com (ovpn-12-60.pek2.redhat.com [10.72.12.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77765608D0; Mon, 24 Jun 2019 06:37:55 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Mon, 24 Jun 2019 14:37:33 +0800 Message-Id: <20190624063733.22079-3-peterx@redhat.com> In-Reply-To: <20190624063733.22079-1-peterx@redhat.com> References: <20190624063733.22079-1-peterx@redhat.com> MIME-Version: 1.0 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.28]); Mon, 24 Jun 2019 06:38:07 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/2] intel_iommu: Fix unexpected unmaps during global unmap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yan Zhao , "Michael S . Tsirkin" , Jason Wang , peterx@redhat.com, Auger Eric , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini This is an replacement work of Yan Zhao's patch: https://www.mail-archive.com/qemu-devel@nongnu.org/msg625340.html vtd_address_space_unmap() will do proper page mask alignment to make sure each IOTLB message will have correct masks for notification messages (2^N-1), but sometimes it can be expanded to even supercede the registered range. That could lead to unexpected UNMAP of already mapped regions in some other notifiers. Instead of doing mindless expension of the start address and address mask, we split the range into smaller ones and guarantee that each small range will have correct masks (2^N-1) and at the same time we should also try our best to generate as less IOTLB messages as possible. Reported-by: Yan Zhao Signed-off-by: Paolo Bonzini [peterx: fixup mask generation algos and other touchups, introduce vtd_get_next_mask(), write commit message] Signed-off-by: Peter Xu Tested-by: Yan Zhao --- hw/i386/intel_iommu.c | 70 +++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 719ce19ab3..39cedf73b8 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3363,11 +3363,31 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s= , PCIBus *bus, int devfn) return vtd_dev_as; } =20 +static uint64_t vtd_get_next_mask(uint64_t start, uint64_t size, int gaw) +{ + /* Tries to find smallest mask from start first */ + uint64_t rmask =3D start & -start, max_mask =3D 1ULL << gaw; + + assert(size && gaw > 0 && gaw < 64); + + /* Zero start, or too big */ + if (!rmask || rmask > max_mask) { + rmask =3D max_mask; + } + + /* If the start mask worked, then use it */ + if (rmask <=3D size) { + return rmask; + } + + /* Find the largest page mask from size */ + return 1ULL << (63 - clz64(size)); +} + /* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) { - IOMMUTLBEntry entry; - hwaddr size; + hwaddr size, remain; hwaddr start =3D n->start; hwaddr end =3D n->end; IntelIOMMUState *s =3D as->iommu_state; @@ -3388,39 +3408,37 @@ static void vtd_address_space_unmap(VTDAddressSpace= *as, IOMMUNotifier *n) } =20 assert(start <=3D end); - size =3D end - start; + size =3D remain =3D end - start + 1; =20 - if (ctpop64(size) !=3D 1) { - /* - * This size cannot format a correct mask. Let's enlarge it to - * suite the minimum available mask. - */ - int n =3D 64 - clz64(size); - if (n > s->aw_bits) { - /* should not happen, but in case it happens, limit it */ - n =3D s->aw_bits; - } - size =3D 1ULL << n; + while (remain > 0) { + IOMMUTLBEntry entry; + uint64_t mask =3D vtd_get_next_mask(start, remain, s->aw_bits); + + assert(mask); + + entry.iova =3D start; + entry.addr_mask =3D mask - 1; + entry.target_as =3D &address_space_memory; + entry.perm =3D IOMMU_NONE; + /* This field is meaningless for unmap */ + entry.translated_addr =3D 0; + + memory_region_notify_one(n, &entry); + + start +=3D mask; + remain -=3D mask; } =20 - entry.target_as =3D &address_space_memory; - /* Adjust iova for the size */ - entry.iova =3D n->start & ~(size - 1); - /* This field is meaningless for unmap */ - entry.translated_addr =3D 0; - entry.perm =3D IOMMU_NONE; - entry.addr_mask =3D size - 1; + assert(!remain); =20 trace_vtd_as_unmap_whole(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn), VTD_PCI_FUNC(as->devfn), - entry.iova, size); + n->start, size); =20 - map.iova =3D entry.iova; - map.size =3D entry.addr_mask; + map.iova =3D n->start; + map.size =3D size; iova_tree_remove(as->iova_tree, &map); - - memory_region_notify_one(n, &entry); } =20 static void vtd_address_space_unmap_all(IntelIOMMUState *s) --=20 2.21.0