Received: from eggs.gnu.org ([2001:4830:134:3::10]:37558) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhums-0001pf-Gj for qemu-devel@nongnu.org; Thu, 08 Sep 2016 04:34:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bhump-0005TR-I9 for qemu-devel@nongnu.org; Thu, 08 Sep 2016 04:34:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35248) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bhump-0005T4-At for qemu-devel@nongnu.org; Thu, 08 Sep 2016 04:34:19 -0400 From: Maxime Coquelin Date: Thu, 8 Sep 2016 10:34:09 +0200 Message-Id: <1473323650-13298-2-git-send-email-maxime.coquelin@redhat.com> In-Reply-To: <1473323650-13298-1-git-send-email-maxime.coquelin@redhat.com> References: <1473323650-13298-1-git-send-email-maxime.coquelin@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 1/2] Revert "Revert "vhost-user: Attempt to fix a race with set_mem_table."" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: mst@redhat.com, qemu-devel@nongnu.org Cc: prerna.saxena@nutanix.com, marcandre.lureau@redhat.com, peter.maydell@linaro.org, Maxime Coquelin , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= This reverts commit 94c9cb31c04737f86be29afefbff401cd23bc24d. Analysis of the race shows that it would happen only when QEMU relies on TCG. Since commit cdafe929 ("vhost-user-test: Use libqos instead of pxe-virtio.rom"), vhost-user-test don't use TCG, so the race no more appear. Cc: Michael S. Tsirkin Cc: Marc-Andr=C3=A9 Lureau Cc: Prerna Saxena Cc: Peter Maydell Signed-off-by: Maxime Coquelin --- hw/virtio/vhost-user.c | 127 ++++++++++++++++++++++++++-----------------= ------ 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index b57454a..1a7d53c 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -263,66 +263,6 @@ static int vhost_user_set_log_base(struct vhost_dev = *dev, uint64_t base, return 0; } =20 -static int vhost_user_set_mem_table(struct vhost_dev *dev, - struct vhost_memory *mem) -{ - int fds[VHOST_MEMORY_MAX_NREGIONS]; - int i, fd; - size_t fd_num =3D 0; - bool reply_supported =3D virtio_has_feature(dev->protocol_features, - VHOST_USER_PROTOCOL_F_REPL= Y_ACK); - - VhostUserMsg msg =3D { - .request =3D VHOST_USER_SET_MEM_TABLE, - .flags =3D VHOST_USER_VERSION, - }; - - if (reply_supported) { - msg.flags |=3D VHOST_USER_NEED_REPLY_MASK; - } - - for (i =3D 0; i < dev->mem->nregions; ++i) { - struct vhost_memory_region *reg =3D dev->mem->regions + i; - ram_addr_t offset; - MemoryRegion *mr; - - assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_addr= ); - mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspace= _addr, - &offset); - fd =3D memory_region_get_fd(mr); - if (fd > 0) { - msg.payload.memory.regions[fd_num].userspace_addr =3D reg->u= serspace_addr; - msg.payload.memory.regions[fd_num].memory_size =3D reg->mem= ory_size; - msg.payload.memory.regions[fd_num].guest_phys_addr =3D reg->= guest_phys_addr; - msg.payload.memory.regions[fd_num].mmap_offset =3D offset; - assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); - fds[fd_num++] =3D fd; - } - } - - msg.payload.memory.nregions =3D fd_num; - - if (!fd_num) { - error_report("Failed initializing vhost-user memory map, " - "consider using -object memory-backend-file share=3D= on"); - return -1; - } - - msg.size =3D sizeof(msg.payload.memory.nregions); - msg.size +=3D sizeof(msg.payload.memory.padding); - msg.size +=3D fd_num * sizeof(VhostUserMemoryRegion); - - if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { - return -1; - } - - if (reply_supported) { - return process_message_reply(dev, msg.request); - } - - return 0; -} - static int vhost_user_set_vring_addr(struct vhost_dev *dev, struct vhost_vring_addr *addr) { @@ -537,6 +477,73 @@ static int vhost_user_get_features(struct vhost_dev = *dev, uint64_t *features) return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features); } =20 +static int vhost_user_set_mem_table(struct vhost_dev *dev, + struct vhost_memory *mem) +{ + int fds[VHOST_MEMORY_MAX_NREGIONS]; + int i, fd; + size_t fd_num =3D 0; + uint64_t features; + bool reply_supported =3D virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_REPL= Y_ACK); + + VhostUserMsg msg =3D { + .request =3D VHOST_USER_SET_MEM_TABLE, + .flags =3D VHOST_USER_VERSION, + }; + + if (reply_supported) { + msg.flags |=3D VHOST_USER_NEED_REPLY_MASK; + } + + for (i =3D 0; i < dev->mem->nregions; ++i) { + struct vhost_memory_region *reg =3D dev->mem->regions + i; + ram_addr_t offset; + MemoryRegion *mr; + + assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_addr= ); + mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspace= _addr, + &offset); + fd =3D memory_region_get_fd(mr); + if (fd > 0) { + msg.payload.memory.regions[fd_num].userspace_addr + =3D reg->userspace_addr; + msg.payload.memory.regions[fd_num].memory_size =3D reg->mem= ory_size; + msg.payload.memory.regions[fd_num].guest_phys_addr + =3D reg->guest_phys_addr; + msg.payload.memory.regions[fd_num].mmap_offset =3D offset; + assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); + fds[fd_num++] =3D fd; + } + } + + msg.payload.memory.nregions =3D fd_num; + + if (!fd_num) { + error_report("Failed initializing vhost-user memory map, " + "consider using -object memory-backend-file share=3D= on"); + return -1; + } + + msg.size =3D sizeof(msg.payload.memory.nregions); + msg.size +=3D sizeof(msg.payload.memory.padding); + msg.size +=3D fd_num * sizeof(VhostUserMemoryRegion); + + vhost_user_write(dev, &msg, fds, fd_num); + + if (reply_supported) { + return process_message_reply(dev, msg.request); + } else { + /* Note: It is (yet) unknown when the client application has fin= ished + * remapping the GPA. + * Attempt to prevent a race by sending a command that requires = a reply. + */ + vhost_user_get_features(dev, &features); + } + + return 0; +} + static int vhost_user_set_owner(struct vhost_dev *dev) { VhostUserMsg msg =3D { --=20 2.7.4