From nobody Wed May 1 22:14:08 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021744893819.4437416663968; Mon, 11 Dec 2017 11:49:04 -0800 (PST) Received: from localhost ([::1]:55196 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU49-0001zJ-Fn for importer@patchew.org; Mon, 11 Dec 2017 14:48:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU1t-00084P-AT for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU1r-0006Tu-Pj for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:21 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34442) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU1r-0006SU-JY for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:19 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A0499267E1; Mon, 11 Dec 2017 19:46:18 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 76E6660562; Mon, 11 Dec 2017 19:46:17 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:03 +0000 Message-Id: <20171211194610.3962-2-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 11 Dec 2017 19:46:18 +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/8] memory: address_space_iterate 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Iterate through an address space calling a function for each section. The iteration is done in order. Signed-off-by: Dr. David Alan Gilbert --- include/exec/memory.h | 23 +++++++++++++++++++++++ memory.c | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 5ed4042f87..f5a9df642e 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -1987,6 +1987,29 @@ address_space_write_cached(MemoryRegionCache *cache,= hwaddr addr, address_space_write(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIF= IED, buf, len); } =20 +/** + * ASIterateCallback: Function type called by address_space_iterate + * + * Return 0 on success or a negative error code. + * + * @mrs: Memory region section for this range + * @opaque: The opaque value passed in to the iterator. + */ +typedef int (*ASIterateCallback)(MemoryRegionSection *mrs, void *opaque); + +/** + * address_space_iterate: Call the function for each address range in the + * AddressSpace, in sorted order. + * + * Return 0 on success or a negative error code. + * + * @as: Address space to iterate over + * @cb: Function to call. If the function returns none-0 the iteration wi= ll + * stop. + * @opaque: Value to pass to the function + */ +int +address_space_iterate(AddressSpace *as, ASIterateCallback cb, void *opaque= ); #endif =20 #endif diff --git a/memory.c b/memory.c index e26e5a3b1d..f45137f25e 100644 --- a/memory.c +++ b/memory.c @@ -2810,6 +2810,28 @@ void address_space_destroy(AddressSpace *as) call_rcu(as, do_address_space_destroy, rcu); } =20 +int address_space_iterate(AddressSpace *as, ASIterateCallback cb, + void *opaque) +{ + int res =3D 0; + FlatView *fv =3D address_space_to_flatview(as); + FlatRange *range; + + flatview_ref(fv); + + FOR_EACH_FLAT_RANGE(range, fv) { + MemoryRegionSection mrs =3D section_from_flat_range(range, fv); + res =3D cb(&mrs, opaque); + if (res) { + break; + } + } + + flatview_unref(fv); + + return res; +} + static const char *memory_region_type(MemoryRegion *mr) { if (memory_region_is_ram_device(mr)) { --=20 2.14.3 From nobody Wed May 1 22:14:08 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021881225582.5172656538796; Mon, 11 Dec 2017 11:51:21 -0800 (PST) Received: from localhost ([::1]:55217 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU6c-0004Li-St for importer@patchew.org; Mon, 11 Dec 2017 14:51:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33827) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU1u-00084W-9Q for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU1t-0006WP-64 for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34450) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU1s-0006V1-Vg for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:21 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F63C267F9; Mon, 11 Dec 2017 19:46:20 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id E8BFF605E1; Mon, 11 Dec 2017 19:46:18 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:04 +0000 Message-Id: <20171211194610.3962-3-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 11 Dec 2017 19:46:20 +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/8] vhost: Move log_dirty check 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com 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" From: "Dr. David Alan Gilbert" Move the log_dirty check into vhost_section. Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/trace-events | 3 +++ hw/virtio/vhost.c | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 775461ae98..4a493bcd46 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -1,5 +1,8 @@ # See docs/devel/tracing.txt for syntax documentation. =20 +# hw/virtio/vhost.c +vhost_section(const char *name, int r) "%s:%d" + # hw/virtio/virtio.c virtqueue_alloc_element(void *elem, size_t sz, unsigned in_num, unsigned o= ut_num) "elem %p size %zd in_num %u out_num %u" virtqueue_fill(void *vq, const void *elem, unsigned int len, unsigned int = idx) "vq %p elem %p len %u idx %u" diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index e4290ce93d..e923219e63 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -27,6 +27,7 @@ #include "hw/virtio/virtio-access.h" #include "migration/blocker.h" #include "sysemu/dma.h" +#include "trace.h" =20 /* enabled until disconnected backend stabilizes */ #define _VHOST_DEBUG 1 @@ -567,18 +568,12 @@ static void vhost_set_memory(MemoryListener *listener, memory_listener); hwaddr start_addr =3D section->offset_within_address_space; ram_addr_t size =3D int128_get64(section->size); - bool log_dirty =3D - memory_region_get_dirty_log_mask(section->mr) & ~(1 << DIRTY_MEMOR= Y_MIGRATION); int s =3D offsetof(struct vhost_memory, regions) + (dev->mem->nregions + 1) * sizeof dev->mem->regions[0]; void *ram; =20 dev->mem =3D g_realloc(dev->mem, s); =20 - if (log_dirty) { - add =3D false; - } - assert(size); =20 /* Optimize no-change case. At least cirrus_vga does this a lot at thi= s time. */ @@ -611,8 +606,19 @@ static void vhost_set_memory(MemoryListener *listener, =20 static bool vhost_section(MemoryRegionSection *section) { - return memory_region_is_ram(section->mr) && + bool result; + bool log_dirty =3D memory_region_get_dirty_log_mask(section->mr) & + ~(1 << DIRTY_MEMORY_MIGRATION); + result =3D memory_region_is_ram(section->mr) && !memory_region_is_rom(section->mr); + + /* Vhost doesn't handle any block which is doing dirty-tracking other + * than migration; this typically fires on VGA areas. + */ + result &=3D !log_dirty; + + trace_vhost_section(section->mr->name, result); + return result; } =20 static void vhost_begin(MemoryListener *listener) --=20 2.14.3 From nobody Wed May 1 22:14:08 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021740986550.6312306575902; Mon, 11 Dec 2017 11:49:00 -0800 (PST) Received: from localhost ([::1]:55198 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU4H-00024M-A0 for importer@patchew.org; Mon, 11 Dec 2017 14:48:49 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU1v-00084f-US for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU1u-0006Zs-PA for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:23 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50724) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU1u-0006Xp-Ft for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:22 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 962A085540; Mon, 11 Dec 2017 19:46:21 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6CA68605E1; Mon, 11 Dec 2017 19:46:20 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:05 +0000 Message-Id: <20171211194610.3962-4-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 11 Dec 2017 19:46:21 +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/8] 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com 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" 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 --- hw/virtio/vhost.c | 74 ++++++++++++++++++++++++++++++---------------------= ---- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index e923219e63..c7ce7baf9b 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -450,35 +450,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) +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) { - hwaddr l; - void *p; - int r =3D 0; + 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); =20 - if (!ranges_overlap(start_addr, size, part_addr, part_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 -EBUSY; } - 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 -ENOMEM; } - 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; @@ -492,22 +494,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; } @@ -633,8 +638,6 @@ 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; uint64_t log_size; int r; =20 @@ -649,11 +652,16 @@ static void vhost_commit(MemoryListener *listener) } =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); + int i; + for (i =3D 0; i < dev->mem->nregions; i++) { + if (vhost_verify_ring_mappings(dev, + (void *)dev->mem->regions[i].userspace_addr, + 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 2.14.3 From nobody Wed May 1 22:14:08 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021885526598.2085575667057; Mon, 11 Dec 2017 11:51:25 -0800 (PST) Received: from localhost ([::1]:55218 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU6h-0004PG-4m for importer@patchew.org; Mon, 11 Dec 2017 14:51:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33873) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU1y-00086R-28 for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU1x-0006fX-A3 for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49888) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU1x-0006dv-49 for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:25 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 37CA85D68B; Mon, 11 Dec 2017 19:46:24 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id E180A605FB; Mon, 11 Dec 2017 19:46:21 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:06 +0000 Message-Id: <20171211194610.3962-5-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 11 Dec 2017 19:46:24 +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/8] vhost: New memory update functions 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com 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" From: "Dr. David Alan Gilbert" vhost_update_mem will replace the existing update mechanism. They make use of the Flatview we have now to make the update simpler. This commit just adds the basic structure. Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/vhost.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c7ce7baf9b..6d720036db 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -634,11 +634,51 @@ static void vhost_begin(MemoryListener *listener) dev->mem_changed_start_addr =3D -1; } =20 +struct vhost_update_mem_tmp { + struct vhost_dev *dev; + uint32_t nregions; + struct vhost_memory_region *regions; +}; + +/* Called for each MRS from vhost_update_mem */ +static int vhost_update_mem_cb(MemoryRegionSection *mrs, void *opaque) +{ + if (!vhost_section(mrs)) { + return 0; + } + + /* TODO */ + return 0; +} + +static int vhost_update_mem(struct vhost_dev *dev, bool *changed) +{ + int res; + struct vhost_update_mem_tmp vtmp; + vtmp.regions =3D 0; + vtmp.nregions =3D 0; + vtmp.dev =3D dev; + + *changed =3D false; + res =3D address_space_iterate(&address_space_memory, + vhost_update_mem_cb, &vtmp); + if (res) { + goto out; + } + + /* TODO */ + *changed =3D dev->mem_changed_start_addr < dev->mem_changed_end_addr; +out: + g_free(vtmp.regions); + return res; +} + static void vhost_commit(MemoryListener *listener) { struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, memory_listener); uint64_t log_size; + bool changed; int r; =20 if (!dev->memory_changed) { @@ -647,7 +687,12 @@ static void vhost_commit(MemoryListener *listener) if (!dev->started) { return; } - if (dev->mem_changed_start_addr > dev->mem_changed_end_addr) { + if (vhost_update_mem(dev, &changed)) { + return; + } + + if (!changed) { + /* None of the mappings we care about changed */ return; } =20 @@ -1523,6 +1568,7 @@ void vhost_ack_features(struct vhost_dev *hdev, const= int *feature_bits, int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) { int i, r; + bool changed; =20 /* should only be called after backend is connected */ assert(hdev->vhost_ops); @@ -1535,6 +1581,9 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODev= ice *vdev) goto fail_features; } =20 + if (vhost_update_mem(hdev, &changed)) { + goto fail_mem; + } if (vhost_dev_has_iommu(hdev)) { memory_listener_register(&hdev->iommu_listener, vdev->dma_as); } --=20 2.14.3 From nobody Wed May 1 22:14:08 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021742815655.6176615988519; Mon, 11 Dec 2017 11:49:02 -0800 (PST) Received: from localhost ([::1]:55200 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU4L-00027S-Ib for importer@patchew.org; Mon, 11 Dec 2017 14:48:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33886) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU1z-00086T-TZ for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU1y-0006hl-SO for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40510) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU1y-0006gU-J2 for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:26 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A803180C08; Mon, 11 Dec 2017 19:46:25 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EB02605E1; Mon, 11 Dec 2017 19:46:24 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:07 +0000 Message-Id: <20171211194610.3962-6-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 11 Dec 2017 19:46:25 +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 5/8] vhost: update_mem_cb implementation 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Add the meat of update_mem_cb; this is called for each region, to add a region to our temporary list. Our temporary list is in order we look to see if this region can be merged with the current head of list. Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/trace-events | 2 ++ hw/virtio/vhost.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 4a493bcd46..92fadec192 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -2,6 +2,8 @@ =20 # hw/virtio/vhost.c vhost_section(const char *name, int r) "%s:%d" +vhost_update_mem_cb(const char *name, uint64_t gpa, uint64_t size, uint64_= t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64 +vhost_update_mem_cb_abut(const char *name, uint64_t new_size) "%s: 0x%"PRI= x64 =20 # hw/virtio/virtio.c virtqueue_alloc_element(void *elem, size_t sz, unsigned in_num, unsigned o= ut_num) "elem %p size %zd in_num %u out_num %u" diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 6d720036db..646a3480c1 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -643,11 +643,63 @@ struct vhost_update_mem_tmp { /* Called for each MRS from vhost_update_mem */ static int vhost_update_mem_cb(MemoryRegionSection *mrs, void *opaque) { + struct vhost_update_mem_tmp *vtmp =3D opaque; + struct vhost_memory_region *cur_vmr; + bool need_add =3D true; + uint64_t mrs_size; + uint64_t mrs_gpa; + uintptr_t mrs_host; + if (!vhost_section(mrs)) { return 0; } + mrs_size =3D int128_get64(mrs->size); + mrs_gpa =3D mrs->offset_within_address_space; + mrs_host =3D (uintptr_t)memory_region_get_ram_ptr(mrs->mr) + + mrs->offset_within_region; + + trace_vhost_update_mem_cb(mrs->mr->name, mrs_gpa, mrs_size, mrs_host); + + if (vtmp->nregions) { + /* Since we already have at least one region, lets see if + * this extends it; since we're scanning in order, we only + * have to look at the last one, and the FlatView that calls + * us shouldn't have overlaps. + */ + struct vhost_memory_region *prev_vmr =3D vtmp->regions + + (vtmp->nregions - 1); + uint64_t prev_gpa_start =3D prev_vmr->guest_phys_addr; + uint64_t prev_gpa_end =3D range_get_last(prev_gpa_start, + prev_vmr->memory_size); + uint64_t prev_host_start =3D prev_vmr->userspace_addr; + uint64_t prev_host_end =3D range_get_last(prev_host_start, + prev_vmr->memory_size); + + if (prev_gpa_end + 1 =3D=3D mrs_gpa && + prev_host_end + 1 =3D=3D mrs_host && + (!vtmp->dev->vhost_ops->vhost_backend_can_merge || + vtmp->dev->vhost_ops->vhost_backend_can_merge(vtmp->dev, + mrs_host, mrs_size, + prev_host_start, prev_vmr->memory_size))) { + /* The two regions abut */ + need_add =3D false; + mrs_size =3D mrs_size + prev_vmr->memory_size; + prev_vmr->memory_size =3D mrs_size; + trace_vhost_update_mem_cb_abut(mrs->mr->name, mrs_size); + } + } + + if (need_add) { + vtmp->nregions++; + vtmp->regions =3D g_realloc_n(vtmp->regions, vtmp->nregions, + sizeof(vtmp->regions[0])); + cur_vmr =3D &vtmp->regions[vtmp->nregions - 1]; + cur_vmr->guest_phys_addr =3D mrs_gpa; + cur_vmr->memory_size =3D mrs_size; + cur_vmr->userspace_addr =3D mrs_host; + cur_vmr->flags_padding =3D 0; + } =20 - /* TODO */ return 0; } =20 --=20 2.14.3 From nobody Wed May 1 22:14:08 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513021946162951.975992935238; Mon, 11 Dec 2017 11:52:26 -0800 (PST) Received: from localhost ([::1]:55220 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU7V-00052k-Mh for importer@patchew.org; Mon, 11 Dec 2017 14:52:09 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33919) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU23-00089C-4m for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU20-0006lL-9w for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:31 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48018) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU20-0006kH-3n for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:28 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 31199C0567A3; Mon, 11 Dec 2017 19:46:27 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id F113E60562; Mon, 11 Dec 2017 19:46:25 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:08 +0000 Message-Id: <20171211194610.3962-7-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 11 Dec 2017 19:46:27 +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 6/8] vhost: Compare and copy updated region data into device state 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Compare the temporary region data with the original, and if it's different update the original in the device state. Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/trace-events | 2 ++ hw/virtio/vhost.c | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 92fadec192..fac89aaba5 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -2,6 +2,8 @@ =20 # hw/virtio/vhost.c vhost_section(const char *name, int r) "%s:%d" +vhost_update_mem(void) "" +vhost_update_mem_changed(void) "" vhost_update_mem_cb(const char *name, uint64_t gpa, uint64_t size, uint64_= t host) "%s: 0x%"PRIx64"+0x%"PRIx64" @ 0x%"PRIx64 vhost_update_mem_cb_abut(const char *name, uint64_t new_size) "%s: 0x%"PRI= x64 =20 diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 646a3480c1..c00d82f96a 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -707,10 +707,12 @@ static int vhost_update_mem(struct vhost_dev *dev, bo= ol *changed) { int res; struct vhost_update_mem_tmp vtmp; + size_t mem_size; vtmp.regions =3D 0; vtmp.nregions =3D 0; vtmp.dev =3D dev; =20 + trace_vhost_update_mem(); *changed =3D false; res =3D address_space_iterate(&address_space_memory, vhost_update_mem_cb, &vtmp); @@ -718,8 +720,21 @@ static int vhost_update_mem(struct vhost_dev *dev, boo= l *changed) goto out; } =20 - /* TODO */ - *changed =3D dev->mem_changed_start_addr < dev->mem_changed_end_addr; + mem_size =3D offsetof(struct vhost_memory, regions) + + (vtmp.nregions + 1) * sizeof dev->mem->regions[0]; + + if (vtmp.nregions !=3D dev->mem->nregions || + memcmp(vtmp.regions, dev->mem->regions, mem_size)) { + *changed =3D true; + /* Update the main regions list from our tmp */ + dev->mem =3D g_realloc(dev->mem, mem_size); + dev->mem->nregions =3D vtmp.nregions; + memcpy(dev->mem->regions, vtmp.regions, + vtmp.nregions * sizeof dev->mem->regions[0]); + used_memslots =3D vtmp.nregions; + trace_vhost_update_mem_changed(); + } + out: g_free(vtmp.regions); return res; --=20 2.14.3 From nobody Wed May 1 22:14:08 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513022045863647.8005957833554; Mon, 11 Dec 2017 11:54:05 -0800 (PST) Received: from localhost ([::1]:55229 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU9A-0007wv-1E for importer@patchew.org; Mon, 11 Dec 2017 14:53:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33925) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU23-00089R-Dp for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU21-0006o9-OO for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:31 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48032) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU21-0006mT-FA for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:29 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 99007C005F96; Mon, 11 Dec 2017 19:46:28 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7037B60562; Mon, 11 Dec 2017 19:46:27 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:09 +0000 Message-Id: <20171211194610.3962-8-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 11 Dec 2017 19:46:28 +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 7/8] vhost: Remove old vhost_set_memory etc 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com 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" From: "Dr. David Alan Gilbert" Remove the old update mechanism, vhost_set_memory, and the functions it uses and the memory_changed flags we no longer use. Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/vhost.c | 254 ------------------------------------------= ---- include/hw/virtio/vhost.h | 3 - 2 files changed, 257 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c00d82f96a..0bf6fb0577 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -156,160 +156,6 @@ static void vhost_log_sync_range(struct vhost_dev *de= v, } } =20 -/* Assign/unassign. Keep an unsorted array of non-overlapping - * memory regions in dev->mem. */ -static void vhost_dev_unassign_memory(struct vhost_dev *dev, - uint64_t start_addr, - uint64_t size) -{ - int from, to, n =3D dev->mem->nregions; - /* Track overlapping/split regions for sanity checking. */ - int overlap_start =3D 0, overlap_end =3D 0, overlap_middle =3D 0, spli= t =3D 0; - - for (from =3D 0, to =3D 0; from < n; ++from, ++to) { - struct vhost_memory_region *reg =3D dev->mem->regions + to; - uint64_t reglast; - uint64_t memlast; - uint64_t change; - - /* clone old region */ - if (to !=3D from) { - memcpy(reg, dev->mem->regions + from, sizeof *reg); - } - - /* No overlap is simple */ - if (!ranges_overlap(reg->guest_phys_addr, reg->memory_size, - start_addr, size)) { - continue; - } - - /* Split only happens if supplied region - * is in the middle of an existing one. Thus it can not - * overlap with any other existing region. */ - assert(!split); - - reglast =3D range_get_last(reg->guest_phys_addr, reg->memory_size); - memlast =3D range_get_last(start_addr, size); - - /* Remove whole region */ - if (start_addr <=3D reg->guest_phys_addr && memlast >=3D reglast) { - --dev->mem->nregions; - --to; - ++overlap_middle; - continue; - } - - /* Shrink region */ - if (memlast >=3D reglast) { - reg->memory_size =3D start_addr - reg->guest_phys_addr; - assert(reg->memory_size); - assert(!overlap_end); - ++overlap_end; - continue; - } - - /* Shift region */ - if (start_addr <=3D reg->guest_phys_addr) { - change =3D memlast + 1 - reg->guest_phys_addr; - reg->memory_size -=3D change; - reg->guest_phys_addr +=3D change; - reg->userspace_addr +=3D change; - assert(reg->memory_size); - assert(!overlap_start); - ++overlap_start; - continue; - } - - /* This only happens if supplied region - * is in the middle of an existing one. Thus it can not - * overlap with any other existing region. */ - assert(!overlap_start); - assert(!overlap_end); - assert(!overlap_middle); - /* Split region: shrink first part, shift second part. */ - memcpy(dev->mem->regions + n, reg, sizeof *reg); - reg->memory_size =3D start_addr - reg->guest_phys_addr; - assert(reg->memory_size); - change =3D memlast + 1 - reg->guest_phys_addr; - reg =3D dev->mem->regions + n; - reg->memory_size -=3D change; - assert(reg->memory_size); - reg->guest_phys_addr +=3D change; - reg->userspace_addr +=3D change; - /* Never add more than 1 region */ - assert(dev->mem->nregions =3D=3D n); - ++dev->mem->nregions; - ++split; - } -} - -/* Called after unassign, so no regions overlap the given range. */ -static void vhost_dev_assign_memory(struct vhost_dev *dev, - uint64_t start_addr, - uint64_t size, - uint64_t uaddr) -{ - int from, to; - struct vhost_memory_region *merged =3D NULL; - for (from =3D 0, to =3D 0; from < dev->mem->nregions; ++from, ++to) { - struct vhost_memory_region *reg =3D dev->mem->regions + to; - uint64_t prlast, urlast; - uint64_t pmlast, umlast; - uint64_t s, e, u; - - /* clone old region */ - if (to !=3D from) { - memcpy(reg, dev->mem->regions + from, sizeof *reg); - } - prlast =3D range_get_last(reg->guest_phys_addr, reg->memory_size); - pmlast =3D range_get_last(start_addr, size); - urlast =3D range_get_last(reg->userspace_addr, reg->memory_size); - umlast =3D range_get_last(uaddr, size); - - /* check for overlapping regions: should never happen. */ - assert(prlast < start_addr || pmlast < reg->guest_phys_addr); - /* Not an adjacent or overlapping region - do not merge. */ - if ((prlast + 1 !=3D start_addr || urlast + 1 !=3D uaddr) && - (pmlast + 1 !=3D reg->guest_phys_addr || - umlast + 1 !=3D reg->userspace_addr)) { - continue; - } - - if (dev->vhost_ops->vhost_backend_can_merge && - !dev->vhost_ops->vhost_backend_can_merge(dev, uaddr, size, - reg->userspace_addr, - reg->memory_size)) { - continue; - } - - if (merged) { - --to; - assert(to >=3D 0); - } else { - merged =3D reg; - } - u =3D MIN(uaddr, reg->userspace_addr); - s =3D MIN(start_addr, reg->guest_phys_addr); - e =3D MAX(pmlast, prlast); - uaddr =3D merged->userspace_addr =3D u; - start_addr =3D merged->guest_phys_addr =3D s; - size =3D merged->memory_size =3D e - s + 1; - assert(merged->memory_size); - } - - if (!merged) { - struct vhost_memory_region *reg =3D dev->mem->regions + to; - memset(reg, 0, sizeof *reg); - reg->memory_size =3D size; - assert(reg->memory_size); - reg->guest_phys_addr =3D start_addr; - reg->userspace_addr =3D uaddr; - ++to; - } - assert(to <=3D dev->mem->nregions + 1); - dev->mem->nregions =3D to; -} - static uint64_t vhost_get_log_size(struct vhost_dev *dev) { uint64_t log_size =3D 0; @@ -526,89 +372,6 @@ static int vhost_verify_ring_mappings(struct vhost_dev= *dev, return r; } =20 -static struct vhost_memory_region *vhost_dev_find_reg(struct vhost_dev *de= v, - uint64_t start_addr, - uint64_t size) -{ - int i, n =3D dev->mem->nregions; - for (i =3D 0; i < n; ++i) { - struct vhost_memory_region *reg =3D dev->mem->regions + i; - if (ranges_overlap(reg->guest_phys_addr, reg->memory_size, - start_addr, size)) { - return reg; - } - } - return NULL; -} - -static bool vhost_dev_cmp_memory(struct vhost_dev *dev, - uint64_t start_addr, - uint64_t size, - uint64_t uaddr) -{ - struct vhost_memory_region *reg =3D vhost_dev_find_reg(dev, start_addr= , size); - uint64_t reglast; - uint64_t memlast; - - if (!reg) { - return true; - } - - reglast =3D range_get_last(reg->guest_phys_addr, reg->memory_size); - memlast =3D range_get_last(start_addr, size); - - /* Need to extend region? */ - if (start_addr < reg->guest_phys_addr || memlast > reglast) { - return true; - } - /* userspace_addr changed? */ - return uaddr !=3D reg->userspace_addr + start_addr - reg->guest_phys_a= ddr; -} - -static void vhost_set_memory(MemoryListener *listener, - MemoryRegionSection *section, - bool add) -{ - struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, - memory_listener); - hwaddr start_addr =3D section->offset_within_address_space; - ram_addr_t size =3D int128_get64(section->size); - int s =3D offsetof(struct vhost_memory, regions) + - (dev->mem->nregions + 1) * sizeof dev->mem->regions[0]; - void *ram; - - dev->mem =3D g_realloc(dev->mem, s); - - assert(size); - - /* Optimize no-change case. At least cirrus_vga does this a lot at thi= s time. */ - ram =3D memory_region_get_ram_ptr(section->mr) + section->offset_withi= n_region; - if (add) { - if (!vhost_dev_cmp_memory(dev, start_addr, size, (uintptr_t)ram)) { - /* Region exists with same address. Nothing to do. */ - return; - } - } else { - if (!vhost_dev_find_reg(dev, start_addr, size)) { - /* Removing region that we don't access. Nothing to do. */ - return; - } - } - - vhost_dev_unassign_memory(dev, start_addr, size); - if (add) { - /* Add given mapping, merging adjacent regions if any */ - vhost_dev_assign_memory(dev, start_addr, size, (uintptr_t)ram); - } else { - /* Remove old mapping for this memory, if any. */ - vhost_dev_unassign_memory(dev, start_addr, size); - } - dev->mem_changed_start_addr =3D MIN(dev->mem_changed_start_addr, start= _addr); - dev->mem_changed_end_addr =3D MAX(dev->mem_changed_end_addr, start_add= r + size - 1); - dev->memory_changed =3D true; - used_memslots =3D dev->mem->nregions; -} - static bool vhost_section(MemoryRegionSection *section) { bool result; @@ -626,14 +389,6 @@ static bool vhost_section(MemoryRegionSection *section) return result; } =20 -static void vhost_begin(MemoryListener *listener) -{ - struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, - memory_listener); - dev->mem_changed_end_addr =3D 0; - dev->mem_changed_start_addr =3D -1; -} - struct vhost_update_mem_tmp { struct vhost_dev *dev; uint32_t nregions; @@ -748,9 +503,6 @@ static void vhost_commit(MemoryListener *listener) bool changed; int r; =20 - if (!dev->memory_changed) { - return; - } if (!dev->started) { return; } @@ -781,7 +533,6 @@ static void vhost_commit(MemoryListener *listener) if (r < 0) { VHOST_OPS_DEBUG("vhost_set_mem_table failed"); } - dev->memory_changed =3D false; return; } log_size =3D vhost_get_log_size(dev); @@ -800,7 +551,6 @@ static void vhost_commit(MemoryListener *listener) if (dev->log_size > log_size + VHOST_LOG_BUFFER) { vhost_dev_log_resize(dev, log_size); } - dev->memory_changed =3D false; } =20 static void vhost_region_add(MemoryListener *listener, @@ -818,7 +568,6 @@ static void vhost_region_add(MemoryListener *listener, dev->n_mem_sections); dev->mem_sections[dev->n_mem_sections - 1] =3D *section; memory_region_ref(section->mr); - vhost_set_memory(listener, section, true); } =20 static void vhost_region_del(MemoryListener *listener, @@ -832,7 +581,6 @@ static void vhost_region_del(MemoryListener *listener, return; } =20 - vhost_set_memory(listener, section, false); memory_region_unref(section->mr); for (i =3D 0; i < dev->n_mem_sections; ++i) { if (dev->mem_sections[i].offset_within_address_space @@ -1416,7 +1164,6 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, hdev->features =3D features; =20 hdev->memory_listener =3D (MemoryListener) { - .begin =3D vhost_begin, .commit =3D vhost_commit, .region_add =3D vhost_region_add, .region_del =3D vhost_region_del, @@ -1462,7 +1209,6 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, hdev->log_size =3D 0; hdev->log_enabled =3D false; hdev->started =3D false; - hdev->memory_changed =3D false; memory_listener_register(&hdev->memory_listener, &address_space_memory= ); QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); return 0; diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 467dc7794b..34ace450ab 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -67,9 +67,6 @@ struct vhost_dev { bool log_enabled; uint64_t log_size; Error *migration_blocker; - bool memory_changed; - hwaddr mem_changed_start_addr; - hwaddr mem_changed_end_addr; const VhostOps *vhost_ops; void *opaque; struct vhost_log *log; --=20 2.14.3 From nobody Wed May 1 22:14:08 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 1513022002868259.8125616575817; Mon, 11 Dec 2017 11:53:22 -0800 (PST) Received: from localhost ([::1]:55228 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU8a-0006c4-CM for importer@patchew.org; Mon, 11 Dec 2017 14:53:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33988) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eOU2D-0008GR-3G for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eOU2A-00074q-0C for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50882) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eOU29-00072w-OJ for qemu-devel@nongnu.org; Mon, 11 Dec 2017 14:46:37 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DA1C085540; Mon, 11 Dec 2017 19:46:36 +0000 (UTC) Received: from dgilbert-t530.redhat.com (ovpn-117-205.ams2.redhat.com [10.36.117.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2F7060562; Mon, 11 Dec 2017 19:46:28 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, imammedo@redhat.com Date: Mon, 11 Dec 2017 19:46:10 +0000 Message-Id: <20171211194610.3962-9-dgilbert@redhat.com> In-Reply-To: <20171211194610.3962-1-dgilbert@redhat.com> References: <20171211194610.3962-1-dgilbert@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 11 Dec 2017 19:46:36 +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 8/8] vhost: Move mem_sections maintenance into commit/update routines 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: maxime.coquelin@redhat.com, groug@kaod.org, mst@redhat.com 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" From: "Dr. David Alan Gilbert" Move the maintenance of mem_sections into the vhost_update_mem routines, this removes the need for the vhost_region_add/del callbacks. Suggested-by: Igor Mammedov (and mostly written by Igor!) Signed-off-by: Dr. David Alan Gilbert --- hw/virtio/vhost.c | 58 +++++++++++++++------------------------------------= ---- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 0bf6fb0577..5d3f921be5 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -408,6 +408,13 @@ static int vhost_update_mem_cb(MemoryRegionSection *mr= s, void *opaque) if (!vhost_section(mrs)) { return 0; } + ++vtmp->dev->n_mem_sections; + vtmp->dev->mem_sections =3D g_renew(MemoryRegionSection, + vtmp->dev->mem_sections, + vtmp->dev->n_mem_sections); + vtmp->dev->mem_sections[vtmp->dev->n_mem_sections - 1] =3D *mrs; + memory_region_ref(mrs->mr); + mrs_size =3D int128_get64(mrs->size); mrs_gpa =3D mrs->offset_within_address_space; mrs_host =3D (uintptr_t)memory_region_get_ram_ptr(mrs->mr) + @@ -461,6 +468,7 @@ static int vhost_update_mem_cb(MemoryRegionSection *mrs= , void *opaque) static int vhost_update_mem(struct vhost_dev *dev, bool *changed) { int res; + unsigned i; struct vhost_update_mem_tmp vtmp; size_t mem_size; vtmp.regions =3D 0; @@ -469,6 +477,14 @@ static int vhost_update_mem(struct vhost_dev *dev, boo= l *changed) =20 trace_vhost_update_mem(); *changed =3D false; + /* Clear out the section list, it'll get rebuilt */ + for (i =3D 0; i < dev->n_mem_sections; i++) { + memory_region_unref(dev->mem_sections[i].mr); + } + g_free(dev->mem_sections); + dev->mem_sections =3D NULL; + dev->n_mem_sections =3D 0; + res =3D address_space_iterate(&address_space_memory, vhost_update_mem_cb, &vtmp); if (res) { @@ -553,46 +569,6 @@ static void vhost_commit(MemoryListener *listener) } } =20 -static void vhost_region_add(MemoryListener *listener, - MemoryRegionSection *section) -{ - struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, - memory_listener); - - if (!vhost_section(section)) { - return; - } - - ++dev->n_mem_sections; - dev->mem_sections =3D g_renew(MemoryRegionSection, dev->mem_sections, - dev->n_mem_sections); - dev->mem_sections[dev->n_mem_sections - 1] =3D *section; - memory_region_ref(section->mr); -} - -static void vhost_region_del(MemoryListener *listener, - MemoryRegionSection *section) -{ - struct vhost_dev *dev =3D container_of(listener, struct vhost_dev, - memory_listener); - int i; - - if (!vhost_section(section)) { - return; - } - - memory_region_unref(section->mr); - for (i =3D 0; i < dev->n_mem_sections; ++i) { - if (dev->mem_sections[i].offset_within_address_space - =3D=3D section->offset_within_address_space) { - --dev->n_mem_sections; - memmove(&dev->mem_sections[i], &dev->mem_sections[i+1], - (dev->n_mem_sections - i) * sizeof(*dev->mem_sections)= ); - break; - } - } -} - static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotl= b) { struct vhost_iommu *iommu =3D container_of(n, struct vhost_iommu, n); @@ -1165,8 +1141,6 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, =20 hdev->memory_listener =3D (MemoryListener) { .commit =3D vhost_commit, - .region_add =3D vhost_region_add, - .region_del =3D vhost_region_del, .region_nop =3D vhost_region_nop, .log_start =3D vhost_log_start, .log_stop =3D vhost_log_stop, --=20 2.14.3