From nobody Mon Feb 9 21:21:40 2026 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518117412312325.7416454083842; Thu, 8 Feb 2018 11:16:52 -0800 (PST) Received: from localhost ([::1]:38531 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejrgb-00042V-0I for importer@patchew.org; Thu, 08 Feb 2018 14:16:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejrZA-00062Z-AD for qemu-devel@nongnu.org; Thu, 08 Feb 2018 14:09:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ejrZ9-0008G0-11 for qemu-devel@nongnu.org; Thu, 08 Feb 2018 14:09:04 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:50872 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ejrZ8-0008FX-RO for qemu-devel@nongnu.org; Thu, 08 Feb 2018 14:09:02 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7E4B78182D2D; Thu, 8 Feb 2018 19:09:02 +0000 (UTC) Received: from redhat.com (ovpn-120-144.rdu2.redhat.com [10.10.120.144]) by smtp.corp.redhat.com (Postfix) with SMTP id 47A71B3008; Thu, 8 Feb 2018 19:09:02 +0000 (UTC) Date: Thu, 8 Feb 2018 21:09:02 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1518116908-10852-6-git-send-email-mst@redhat.com> References: <1518116908-10852-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1518116908-10852-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 08 Feb 2018 19:09:02 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Thu, 08 Feb 2018 19:09:02 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mst@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 05/26] vhost: Simplify ring verification checks 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: Peter Maydell , "Dr. David Alan Gilbert" , Igor Mammedov 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 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" vhost_verify_ring_mappings() were used to verify that rings are still accessible and related memory hasn't been moved after flatview is updated. It was doing checks by mapping ring's GPA+len and checking that HVA hadn't changed with new memory map. To avoid maybe expensive mapping call, we were identifying address range that changed and were doing mapping only if ring was in changed range. However it's not neccessary to perform ring's GPA mapping as we already have its current HVA and all we need is to verify that ring's GPA translates to the same HVA in updated flatview. This will allow the following patches to simplify the range comparison that was previously needed to avoid expensive verify_ring_mapping calls. Signed-off-by: Igor Mammedov with modifications by: Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/vhost.c | 79 +++++++++++++++++++++++++++++----------------------= ---- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 92c9500..91cab51 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -455,35 +455,37 @@ static void vhost_memory_unmap(struct vhost_dev *dev,= void *buffer, } } =20 -static int vhost_verify_ring_part_mapping(struct vhost_dev *dev, - void *part, - uint64_t part_addr, - uint64_t part_size, - uint64_t start_addr, - uint64_t size) -{ - hwaddr l; - void *p; - int r =3D 0; - - if (!ranges_overlap(start_addr, size, part_addr, part_size)) { +static int vhost_verify_ring_part_mapping(void *ring_hva, + uint64_t ring_gpa, + uint64_t ring_size, + void *reg_hva, + uint64_t reg_gpa, + uint64_t reg_size) +{ + uint64_t hva_ring_offset; + uint64_t ring_last =3D range_get_last(ring_gpa, ring_size); + uint64_t reg_last =3D range_get_last(reg_gpa, reg_size); + + if (ring_last < reg_gpa || ring_gpa > reg_last) { return 0; } - l =3D part_size; - p =3D vhost_memory_map(dev, part_addr, &l, 1); - if (!p || l !=3D part_size) { - r =3D -ENOMEM; + /* check that whole ring's is mapped */ + if (ring_last > reg_last) { + return -ENOMEM; } - if (p !=3D part) { - r =3D -EBUSY; + /* check that ring's MemoryRegion wasn't replaced */ + hva_ring_offset =3D ring_gpa - reg_gpa; + if (ring_hva !=3D reg_hva + hva_ring_offset) { + return -EBUSY; } - vhost_memory_unmap(dev, p, l, 0, 0); - return r; + + return 0; } =20 static int vhost_verify_ring_mappings(struct vhost_dev *dev, - uint64_t start_addr, - uint64_t size) + void *reg_hva, + uint64_t reg_gpa, + uint64_t reg_size) { int i, j; int r =3D 0; @@ -497,22 +499,25 @@ static int vhost_verify_ring_mappings(struct vhost_de= v *dev, struct vhost_virtqueue *vq =3D dev->vqs + i; =20 j =3D 0; - r =3D vhost_verify_ring_part_mapping(dev, vq->desc, vq->desc_phys, - vq->desc_size, start_addr, size= ); + r =3D vhost_verify_ring_part_mapping( + vq->desc, vq->desc_phys, vq->desc_size, + reg_hva, reg_gpa, reg_size); if (r) { break; } =20 j++; - r =3D vhost_verify_ring_part_mapping(dev, vq->avail, vq->avail_phy= s, - vq->avail_size, start_addr, siz= e); + r =3D vhost_verify_ring_part_mapping( + vq->desc, vq->desc_phys, vq->desc_size, + reg_hva, reg_gpa, reg_size); if (r) { break; } =20 j++; - r =3D vhost_verify_ring_part_mapping(dev, vq->used, vq->used_phys, - vq->used_size, start_addr, size= ); + r =3D vhost_verify_ring_part_mapping( + vq->desc, vq->desc_phys, vq->desc_size, + reg_hva, reg_gpa, reg_size); if (r) { break; } @@ -635,13 +640,11 @@ static void vhost_commit(MemoryListener *listener) { struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, memory_listener); - hwaddr start_addr =3D 0; - ram_addr_t size =3D 0; MemoryRegionSection *old_sections; int n_old_sections; - uint64_t log_size; int r; + int i; =20 old_sections =3D dev->mem_sections; n_old_sections =3D dev->n_mem_sections; @@ -658,12 +661,14 @@ static void vhost_commit(MemoryListener *listener) goto out; } =20 - if (dev->started) { - start_addr =3D dev->mem_changed_start_addr; - size =3D dev->mem_changed_end_addr - dev->mem_changed_start_addr += 1; - - r =3D vhost_verify_ring_mappings(dev, start_addr, size); - assert(r >=3D 0); + for (i =3D 0; i < dev->mem->nregions; i++) { + if (vhost_verify_ring_mappings(dev, + (void *)(uintptr_t)dev->mem->regions[i].userspace_a= ddr, + dev->mem->regions[i].guest_phys_addr, + dev->mem->regions[i].memory_size)) { + error_report("Verify ring failure on region %d", i); + abort(); + } } =20 if (!dev->log_enabled) { --=20 MST