From nobody Sat May 18 18:27:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=nutanix.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580192721273194.1724083288982; Mon, 27 Jan 2020 22:25:21 -0800 (PST) Received: from localhost ([::1]:54514 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKJL-0002CF-Vu for importer@patchew.org; Tue, 28 Jan 2020 01:25:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51443) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKIP-0000kf-Lj for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iwKIO-0003d7-IX for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:21 -0500 Received: from [192.146.154.1] (port=59387 helo=mcp01.nutanix.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iwKIO-0003ct-Db for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:20 -0500 Received: from localhost.localdomain.com (unknown [10.40.36.165]) by mcp01.nutanix.com (Postfix) with ESMTP id 24E361007D19; Tue, 28 Jan 2020 06:24:20 +0000 (UTC) From: Raphael Norwitz To: qemu-devel@nongnu.org, mst@redhat.com Subject: [PATCH v2 1/3] Fixed assert in vhost_user_set_mem_table_postcopy Date: Wed, 15 Jan 2020 21:57:04 -0500 Message-Id: <1579143426-18305-2-git-send-email-raphael.norwitz@nutanix.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> References: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 192.146.154.1 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: Peter Turschmid , raphael.s.norwitz@gmail.com, Raphael Norwitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The current vhost_user_set_mem_table_postcopy() implementation populates each region of the VHOST_USER_SET_MEM_TABLE message without first checking if there are more than VHOST_MEMORY_MAX_NREGIONS already populated. This can cause memory corruption if too many regions are added to the message during the postcopy step. This change moves an existing assert up such that attempting to construct a VHOST_USER_SET_MEM_TABLE message with too many memory regions will gracefully bring down qemu instead of corrupting memory. Signed-off-by: Raphael Norwitz Signed-off-by: Peter Turschmid --- hw/virtio/vhost-user.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 2e81f55..cce851a 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -443,6 +443,7 @@ static int vhost_user_set_mem_table_postcopy(struct vho= st_dev *dev, &offset); fd =3D memory_region_get_fd(mr); if (fd > 0) { + assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); trace_vhost_user_set_mem_table_withfd(fd_num, mr->name, reg->memory_size, reg->guest_phys_addr, @@ -455,7 +456,6 @@ static int vhost_user_set_mem_table_postcopy(struct vho= st_dev *dev, 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; } else { u->region_rb_offset[i] =3D 0; --=20 1.8.3.1 From nobody Sat May 18 18:27:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=nutanix.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580192729572430.928641028165; Mon, 27 Jan 2020 22:25:29 -0800 (PST) Received: from localhost ([::1]:54516 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKJT-0002VU-VV for importer@patchew.org; Tue, 28 Jan 2020 01:25:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51469) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKIU-0000rL-64 for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iwKIS-0003nr-Oq for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:26 -0500 Received: from [192.146.154.1] (port=1708 helo=mcp01.nutanix.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iwKIS-0003n9-HQ for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:24 -0500 Received: from localhost.localdomain.com (unknown [10.40.36.165]) by mcp01.nutanix.com (Postfix) with ESMTP id 3BFAE1007D19; Tue, 28 Jan 2020 06:24:24 +0000 (UTC) From: Raphael Norwitz To: qemu-devel@nongnu.org, mst@redhat.com Subject: [PATCH v2 2/3] Refactor vhost_user_set_mem_table functions Date: Wed, 15 Jan 2020 21:57:05 -0500 Message-Id: <1579143426-18305-3-git-send-email-raphael.norwitz@nutanix.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> References: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 192.146.154.1 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: Peter Turschmid , raphael.s.norwitz@gmail.com, Raphael Norwitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" vhost_user_set_mem_table() and vhost_user_set_mem_table_postcopy() have gotten convoluted, and have some identical code. This change moves the logic populating the VhostUserMemory struct and fds array from vhost_user_set_mem_table() and vhost_user_set_mem_table_postcopy() to a new function, vhost_user_fill_set_mem_table_msg(). No functionality is impacted. Signed-off-by: Raphael Norwitz Signed-off-by: Peter Turschmid --- hw/virtio/vhost-user.c | 143 +++++++++++++++++++++++----------------------= ---- 1 file changed, 67 insertions(+), 76 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index cce851a..af83fdd 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -407,18 +407,79 @@ static int vhost_user_set_log_base(struct vhost_dev *= dev, uint64_t base, return 0; } =20 +static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u, + struct vhost_dev *dev, + VhostUserMsg *msg, + int *fds, size_t *fd_num, + bool postcopy) +{ + int i, fd; + ram_addr_t offset; + MemoryRegion *mr; + struct vhost_memory_region *reg; + + msg->hdr.request =3D VHOST_USER_SET_MEM_TABLE; + + for (i =3D 0; i < dev->mem->nregions; ++i) { + reg =3D dev->mem->regions + i; + + assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_addr); + mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspace_a= ddr, + &offset); + fd =3D memory_region_get_fd(mr); + if (fd > 0) { + if (postcopy) { + assert(*fd_num < VHOST_MEMORY_MAX_NREGIONS); + trace_vhost_user_set_mem_table_withfd(*fd_num, mr->name, + reg->memory_size, + reg->guest_phys_addr, + reg->userspace_addr, + offset); + u->region_rb_offset[i] =3D offset; + u->region_rb[i] =3D mr->ram_block; + } else if (*fd_num =3D=3D VHOST_MEMORY_MAX_NREGIONS) { + error_report("Failed preparing vhost-user memory table msg= "); + return -1; + } + msg->payload.memory.regions[*fd_num].userspace_addr =3D + reg->userspace_addr; + msg->payload.memory.regions[*fd_num].memory_size =3D + reg->memory_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; + fds[(*fd_num)++] =3D fd; + } else if (postcopy) { + u->region_rb_offset[i] =3D 0; + u->region_rb[i] =3D NULL; + } + } + + 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=3Do= n"); + return -1; + } + + msg->hdr.size =3D sizeof(msg->payload.memory.nregions); + msg->hdr.size +=3D sizeof(msg->payload.memory.padding); + msg->hdr.size +=3D *fd_num * sizeof(VhostUserMemoryRegion); + + return 1; +} + static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev, struct vhost_memory *mem) { struct vhost_user *u =3D dev->opaque; int fds[VHOST_MEMORY_MAX_NREGIONS]; - int i, fd; size_t fd_num =3D 0; VhostUserMsg msg_reply; int region_i, msg_i; =20 VhostUserMsg msg =3D { - .hdr.request =3D VHOST_USER_SET_MEM_TABLE, .hdr.flags =3D VHOST_USER_VERSION, }; =20 @@ -433,48 +494,11 @@ static int vhost_user_set_mem_table_postcopy(struct v= host_dev *dev, u->region_rb_len =3D dev->mem->nregions; } =20 - 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_a= ddr, - &offset); - fd =3D memory_region_get_fd(mr); - if (fd > 0) { - assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); - trace_vhost_user_set_mem_table_withfd(fd_num, mr->name, - reg->memory_size, - reg->guest_phys_addr, - reg->userspace_addr, off= set); - u->region_rb_offset[i] =3D offset; - u->region_rb[i] =3D mr->ram_block; - msg.payload.memory.regions[fd_num].userspace_addr =3D - reg->userspace_addr; - msg.payload.memory.regions[fd_num].memory_size =3D reg->memor= y_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; - fds[fd_num++] =3D fd; - } else { - u->region_rb_offset[i] =3D 0; - u->region_rb[i] =3D NULL; - } - } - - 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=3Do= n"); + if (vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, + true) < 0) { return -1; } =20 - msg.hdr.size =3D sizeof(msg.payload.memory.nregions); - msg.hdr.size +=3D sizeof(msg.payload.memory.padding); - msg.hdr.size +=3D fd_num * sizeof(VhostUserMemoryRegion); - if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { return -1; } @@ -545,7 +569,6 @@ static int vhost_user_set_mem_table(struct vhost_dev *d= ev, { struct vhost_user *u =3D dev->opaque; int fds[VHOST_MEMORY_MAX_NREGIONS]; - int i, fd; size_t fd_num =3D 0; bool do_postcopy =3D u->postcopy_listen && u->postcopy_fd.handler; bool reply_supported =3D virtio_has_feature(dev->protocol_features, @@ -559,7 +582,6 @@ static int vhost_user_set_mem_table(struct vhost_dev *d= ev, } =20 VhostUserMsg msg =3D { - .hdr.request =3D VHOST_USER_SET_MEM_TABLE, .hdr.flags =3D VHOST_USER_VERSION, }; =20 @@ -567,42 +589,11 @@ static int vhost_user_set_mem_table(struct vhost_dev = *dev, msg.hdr.flags |=3D VHOST_USER_NEED_REPLY_MASK; } =20 - 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_a= ddr, - &offset); - fd =3D memory_region_get_fd(mr); - if (fd > 0) { - if (fd_num =3D=3D VHOST_MEMORY_MAX_NREGIONS) { - error_report("Failed preparing vhost-user memory table msg= "); - return -1; - } - msg.payload.memory.regions[fd_num].userspace_addr =3D - reg->userspace_addr; - msg.payload.memory.regions[fd_num].memory_size =3D reg->memor= y_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; - 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=3Do= n"); + if (vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, + false) < 0) { return -1; } =20 - msg.hdr.size =3D sizeof(msg.payload.memory.nregions); - msg.hdr.size +=3D sizeof(msg.payload.memory.padding); - msg.hdr.size +=3D fd_num * sizeof(VhostUserMemoryRegion); - if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { return -1; } --=20 1.8.3.1 From nobody Sat May 18 18:27:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.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 (zohomail.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=nutanix.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1580192736390562.8967963058062; Mon, 27 Jan 2020 22:25:36 -0800 (PST) Received: from localhost ([::1]:54518 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKJa-00034d-UF for importer@patchew.org; Tue, 28 Jan 2020 01:25:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51496) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iwKIX-0000x5-9q for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iwKIU-0003r1-Ve for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:29 -0500 Received: from [192.146.154.1] (port=42336 helo=mcp01.nutanix.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iwKIU-0003qC-NS for qemu-devel@nongnu.org; Tue, 28 Jan 2020 01:24:26 -0500 Received: from localhost.localdomain.com (unknown [10.40.36.165]) by mcp01.nutanix.com (Postfix) with ESMTP id 6F54F1007D19; Tue, 28 Jan 2020 06:24:26 +0000 (UTC) From: Raphael Norwitz To: qemu-devel@nongnu.org, mst@redhat.com Subject: [PATCH v2 3/3] Lift max memory slots limit imposed by vhost-user Date: Wed, 15 Jan 2020 21:57:06 -0500 Message-Id: <1579143426-18305-4-git-send-email-raphael.norwitz@nutanix.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> References: <1579143426-18305-1-git-send-email-raphael.norwitz@nutanix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 192.146.154.1 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: Peter Turschmid , raphael.s.norwitz@gmail.com, Raphael Norwitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The current vhost-user implementation in Qemu imposes a limit on the maximum number of memory slots exposed to a VM using a vhost-user device. This change provides a new protocol feature VHOST_USER_F_CONFIGURE_SLOTS which, when enabled, lifts this limit and allows a VM with a vhost-user device to expose a configurable number of memory slots, up to the ACPI defined maximum. Existing backends which do not support this protocol feature are unaffected. This feature works by using three new messages, VHOST_USER_GET_MAX_MEM_SLOTS, VHOST_USER_ADD_MEM_REG and VHOST_USER_REM_MEM_REG. VHOST_USER_GET_MAX_MEM_SLOTS gets the number of memory slots the backend is willing to accept when the backend is initialized. Then, when the memory tables are set or updated, a series of VHOST_USER_ADD_MEM_REG and VHOST_USER_REM_MEM_REG messages are sent to transmit the regions to map and/or unmap instead of trying to send all the regions in one fixed size VHOST_USER_SET_MEM_TABLE message. The vhost_user struct maintains a shadow state of the VM=E2=80=99s memory regions. When the memory tables are modified, the vhost_user_set_mem_table() function compares the new device memory state to the shadow state and only sends regions which need to be unmapped or mapped in. The regions which must be unmapped are sent first, followed by the new regions to be mapped in. After all the messages have been sent, the shadow state is set to the current virtual device state. The current feature implementation does not work with postcopy migration and cannot be enabled if the VHOST_USER_PROTOCOL_F_REPLY_ACK feature has also been negotiated. Signed-off-by: Raphael Norwitz Signed-off-by: Peter Turschmid Suggested-by: Mike Cui --- docs/interop/vhost-user.rst | 43 ++++++++ hw/virtio/vhost-user.c | 254 ++++++++++++++++++++++++++++++++++++++++= ---- 2 files changed, 275 insertions(+), 22 deletions(-) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 5f8b3a4..ae9acf2 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -786,6 +786,7 @@ Protocol features #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12 #define VHOST_USER_PROTOCOL_F_RESET_DEVICE 13 + #define VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS 14 =20 Master message types -------------------- @@ -1205,6 +1206,48 @@ Master message types Only valid if the ``VHOST_USER_PROTOCOL_F_RESET_DEVICE`` protocol feature is set by the backend. =20 +``VHOST_USER_GET_MAX_MEM_SLOTS`` + :id: 35 + :equivalent ioctl: N/A + :slave payload: u64 + + When the VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protocol feature has been + successfully negotiated, this message is submitted by master to the + slave. The slave should return the message with a u64 payload + containing the maximum number of memory slots for QEMU to expose to + the guest. This message is not supported with postcopy migration or if + the VHOST_USER_PROTOCOL_F_REPLY_ACK feature has also been negotiated. + +``VHOST_USER_ADD_MEM_REG`` + :id: 36 + :equivalent ioctl: N/A + :slave payload: memory region + + When the VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protocol feature has been + successfully negotiated, this message is submitted by master to the slav= e. + The message payload contains a memory region descriptor struct, describi= ng + a region of guest memory which the slave device must map in. When the + VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protocol feature has been successf= ully + negotiated, along with the VHOST_USER_REM_MEM_REG message, this message = is + used to set and update the memory tables of the slave device. This messa= ge + is not supported with postcopy migration or if the + VHOST_USER_PROTOCOL_F_REPLY_ACK feature has also been negotiated. + +``VHOST_USER_REM_MEM_REG`` + :id: 37 + :equivalent ioctl: N/A + :slave payload: memory region + + When the VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protocol feature has been + successfully negotiated, this message is submitted by master to the slav= e. + The message payload contains a memory region descriptor struct, describi= ng + a region of guest memory which the slave device must unmap. When the + VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protocol feature has been successf= ully + negotiated, along with the VHOST_USER_ADD_MEM_REG message, this message = is + used to set and update the memory tables of the slave device. This messa= ge + is not supported with postcopy migration or if the + VHOST_USER_PROTOCOL_F_REPLY_ACK feature has also been negotiated. + Slave message types ------------------- =20 diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index af83fdd..fed6d02 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -35,11 +35,29 @@ #include #endif =20 -#define VHOST_MEMORY_MAX_NREGIONS 8 +#define VHOST_MEMORY_LEGACY_NREGIONS 8 #define VHOST_USER_F_PROTOCOL_FEATURES 30 #define VHOST_USER_SLAVE_MAX_FDS 8 =20 /* + * Set maximum number of RAM slots supported to + * the maximum number supported by the target + * hardware plaform. + */ +#if defined(TARGET_X86) || defined(TARGET_X86_64) || \ + defined(TARGET_ARM) || defined(TARGET_ARM_64) +#include "hw/acpi/acpi.h" +#define VHOST_USER_MAX_RAM_SLOTS ACPI_MAX_RAM_SLOTS + +#elif defined(TARGET_PPC) || defined(TARGET_PPC_64) +#include "hw/ppc/spapr.h" +#define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS + +#else +#define VHOST_USER_MAX_RAM_SLOTS 512 +#endif + +/* * Maximum size of virtio device config space */ #define VHOST_USER_MAX_CONFIG_SIZE 256 @@ -59,6 +77,7 @@ enum VhostUserProtocolFeature { VHOST_USER_PROTOCOL_F_HOST_NOTIFIER =3D 11, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD =3D 12, VHOST_USER_PROTOCOL_F_RESET_DEVICE =3D 13, + VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS =3D 14, VHOST_USER_PROTOCOL_F_MAX }; =20 @@ -100,6 +119,9 @@ typedef enum VhostUserRequest { VHOST_USER_SET_INFLIGHT_FD =3D 32, VHOST_USER_GPU_SET_SOCKET =3D 33, VHOST_USER_RESET_DEVICE =3D 34, + VHOST_USER_GET_MAX_MEM_SLOTS =3D 35, + VHOST_USER_ADD_MEM_REG =3D 36, + VHOST_USER_REM_MEM_REG =3D 37, VHOST_USER_MAX } VhostUserRequest; =20 @@ -121,9 +143,14 @@ typedef struct VhostUserMemoryRegion { typedef struct VhostUserMemory { uint32_t nregions; uint32_t padding; - VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; + VhostUserMemoryRegion regions[VHOST_MEMORY_LEGACY_NREGIONS]; } VhostUserMemory; =20 +typedef struct VhostUserMemRegMsg { + uint32_t padding; + VhostUserMemoryRegion region; +} VhostUserMemRegMsg; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset; @@ -182,6 +209,7 @@ typedef union { struct vhost_vring_state state; struct vhost_vring_addr addr; VhostUserMemory memory; + VhostUserMemRegMsg mem_reg; VhostUserLog log; struct vhost_iotlb_msg iotlb; VhostUserConfig config; @@ -210,7 +238,7 @@ struct vhost_user { int slave_fd; NotifierWithReturn postcopy_notifier; struct PostCopyFD postcopy_fd; - uint64_t postcopy_client_bases[VHOST_MEMORY_MAX_NREGIONS]; + uint64_t postcopy_client_bases[VHOST_USER_MAX_RAM_SLOTS]; /* Length of the region_rb and region_rb_offset arrays */ size_t region_rb_len; /* RAMBlock associated with a given region */ @@ -222,6 +250,13 @@ struct vhost_user { =20 /* True once we've entered postcopy_listen */ bool postcopy_listen; + + /* Maximum number of RAM slots supported by the backend */ + uint64_t memory_slots; + + /* Our current regions */ + int num_shadow_regions; + VhostUserMemoryRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS]; }; =20 static bool ioeventfd_enabled(void) @@ -370,7 +405,7 @@ int vhost_user_gpu_set_socket(struct vhost_dev *dev, in= t fd) static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base, struct vhost_log *log) { - int fds[VHOST_MEMORY_MAX_NREGIONS]; + int fds[VHOST_USER_MAX_RAM_SLOTS]; size_t fd_num =3D 0; bool shmfd =3D virtio_has_feature(dev->protocol_features, VHOST_USER_PROTOCOL_F_LOG_SHMFD); @@ -429,7 +464,7 @@ static int vhost_user_fill_set_mem_table_msg(struct vho= st_user *u, fd =3D memory_region_get_fd(mr); if (fd > 0) { if (postcopy) { - assert(*fd_num < VHOST_MEMORY_MAX_NREGIONS); + assert(*fd_num < VHOST_MEMORY_LEGACY_NREGIONS); trace_vhost_user_set_mem_table_withfd(*fd_num, mr->name, reg->memory_size, reg->guest_phys_addr, @@ -437,7 +472,7 @@ static int vhost_user_fill_set_mem_table_msg(struct vho= st_user *u, offset); u->region_rb_offset[i] =3D offset; u->region_rb[i] =3D mr->ram_block; - } else if (*fd_num =3D=3D VHOST_MEMORY_MAX_NREGIONS) { + } else if (*fd_num =3D=3D VHOST_MEMORY_LEGACY_NREGIONS) { error_report("Failed preparing vhost-user memory table msg= "); return -1; } @@ -474,7 +509,7 @@ static int vhost_user_set_mem_table_postcopy(struct vho= st_dev *dev, struct vhost_memory *mem) { struct vhost_user *u =3D dev->opaque; - int fds[VHOST_MEMORY_MAX_NREGIONS]; + int fds[VHOST_MEMORY_LEGACY_NREGIONS]; size_t fd_num =3D 0; VhostUserMsg msg_reply; int region_i, msg_i; @@ -523,7 +558,7 @@ static int vhost_user_set_mem_table_postcopy(struct vho= st_dev *dev, } =20 memset(u->postcopy_client_bases, 0, - sizeof(uint64_t) * VHOST_MEMORY_MAX_NREGIONS); + sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS); =20 /* They're in the same order as the regions that were sent * but some of the regions were skipped (above) if they @@ -564,18 +599,151 @@ static int vhost_user_set_mem_table_postcopy(struct = vhost_dev *dev, return 0; } =20 +static inline bool reg_equal(VhostUserMemoryRegion *shadow_reg, + struct vhost_memory_region *vdev_reg) +{ + if (shadow_reg->guest_phys_addr =3D=3D vdev_reg->guest_phys_addr && + shadow_reg->userspace_addr =3D=3D vdev_reg->userspace_addr && + shadow_reg->memory_size =3D=3D vdev_reg->memory_size) { + return true; + } else { + return false; + } +} + +static int vhost_user_send_add_remove_regions(struct vhost_dev *dev, + struct vhost_memory *mem, + VhostUserMsg *msg) +{ + struct vhost_user *u =3D dev->opaque; + int i, j, fd; + bool found[VHOST_USER_MAX_RAM_SLOTS] =3D {}; + bool matching =3D false; + struct vhost_memory_region *reg; + ram_addr_t offset; + MemoryRegion *mr; + + /* + * Ensure the VHOST_USER_PROTOCOL_F_REPLY_ACK has not been + * negotiated and no postcopy migration is in progress. + */ + assert(!virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_REPLY_ACK)); + if (u->postcopy_listen && u->postcopy_fd.handler) { + error_report("Postcopy migration is not supported when the " + "VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS feature " + "has been negotiated"); + return -1; + } + + msg->hdr.size =3D sizeof(msg->payload.mem_reg.padding); + msg->hdr.size +=3D sizeof(VhostUserMemoryRegion); + + /* + * Send VHOST_USER_REM_MEM_REG for memory regions in our shadow state + * which are not found not in the device's memory state. + */ + for (i =3D 0; i < u->num_shadow_regions; ++i) { + reg =3D dev->mem->regions; + + for (j =3D 0; j < dev->mem->nregions; j++) { + reg =3D dev->mem->regions + j; + + assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_ad= dr); + mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspa= ce_addr, + &offset); + fd =3D memory_region_get_fd(mr); + + if (reg_equal(&u->shadow_regions[i], reg)) { + matching =3D true; + found[j] =3D true; + break; + } + } + + if (fd > 0 && !matching) { + msg->hdr.request =3D VHOST_USER_REM_MEM_REG; + msg->payload.mem_reg.region.userspace_addr =3D reg->userspace_= addr; + msg->payload.mem_reg.region.memory_size =3D reg->memory_size; + msg->payload.mem_reg.region.guest_phys_addr =3D + reg->guest_phys_addr; + msg->payload.mem_reg.region.mmap_offset =3D offset; + + if (vhost_user_write(dev, msg, &fd, 1) < 0) { + return -1; + } + } + } + + /* + * Send messages to add regions present in the device which are not + * in our shadow state. + */ + for (i =3D 0; i < dev->mem->nregions; ++i) { + reg =3D dev->mem->regions + i; + + /* + * If the region was in both the shadow and vdev state we don't + * need to send a VHOST_USER_ADD_MEM_REG message for it. + */ + if (found[i]) { + continue; + } + + assert((uintptr_t)reg->userspace_addr =3D=3D reg->userspace_addr); + mr =3D memory_region_from_host((void *)(uintptr_t)reg->userspace_a= ddr, + &offset); + fd =3D memory_region_get_fd(mr); + + if (fd > 0) { + msg->hdr.request =3D VHOST_USER_ADD_MEM_REG; + msg->payload.mem_reg.region.userspace_addr =3D reg->userspace_= addr; + msg->payload.mem_reg.region.memory_size =3D reg->memory_size; + msg->payload.mem_reg.region.guest_phys_addr =3D + reg->guest_phys_addr; + msg->payload.mem_reg.region.mmap_offset =3D offset; + + if (vhost_user_write(dev, msg, &fd, 1) < 0) { + return -1; + } + } + } + + /* Make our shadow state match the updated device state. */ + u->num_shadow_regions =3D dev->mem->nregions; + for (i =3D 0; i < dev->mem->nregions; ++i) { + reg =3D dev->mem->regions + i; + u->shadow_regions[i].guest_phys_addr =3D reg->guest_phys_addr; + u->shadow_regions[i].userspace_addr =3D reg->userspace_addr; + u->shadow_regions[i].memory_size =3D reg->memory_size; + } + + return 0; +} + static int vhost_user_set_mem_table(struct vhost_dev *dev, struct vhost_memory *mem) { struct vhost_user *u =3D dev->opaque; - int fds[VHOST_MEMORY_MAX_NREGIONS]; + int fds[VHOST_MEMORY_LEGACY_NREGIONS]; size_t fd_num =3D 0; bool do_postcopy =3D u->postcopy_listen && u->postcopy_fd.handler; bool reply_supported =3D virtio_has_feature(dev->protocol_features, VHOST_USER_PROTOCOL_F_REPLY_= ACK); + bool config_slots =3D + virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS); =20 if (do_postcopy) { - /* Postcopy has enough differences that it's best done in it's own + if (config_slots) { + error_report("Postcopy migration not supported with " + "VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS feature " + "enabled."); + return -1; + } + + /* + * Postcopy has enough differences that it's best done in it's own * version */ return vhost_user_set_mem_table_postcopy(dev, mem); @@ -589,17 +757,22 @@ static int vhost_user_set_mem_table(struct vhost_dev = *dev, msg.hdr.flags |=3D VHOST_USER_NEED_REPLY_MASK; } =20 - if (vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, - false) < 0) { - return -1; - } - - if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { - return -1; - } + if (config_slots && !reply_supported) { + if (vhost_user_send_add_remove_regions(dev, mem, &msg) < 0) { + return -1; + } + } else { + if (vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num, + false) < 0) { + return -1; + } + if (vhost_user_write(dev, &msg, fds, fd_num) < 0) { + return -1; + } =20 - if (reply_supported) { - return process_message_reply(dev, &msg); + if (reply_supported) { + return process_message_reply(dev, &msg); + } } =20 return 0; @@ -764,7 +937,7 @@ static int vhost_set_vring_file(struct vhost_dev *dev, VhostUserRequest request, struct vhost_vring_file *file) { - int fds[VHOST_MEMORY_MAX_NREGIONS]; + int fds[VHOST_USER_MAX_RAM_SLOTS]; size_t fd_num =3D 0; VhostUserMsg msg =3D { .hdr.request =3D request, @@ -866,6 +1039,23 @@ 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_get_max_memslots(struct vhost_dev *dev, + uint64_t *max_memslots) +{ + uint64_t backend_max_memslots; + int err; + + err =3D vhost_user_get_u64(dev, VHOST_USER_GET_MAX_MEM_SLOTS, + &backend_max_memslots); + if (err < 0) { + return err; + } + + *max_memslots =3D MIN(backend_max_memslots, VHOST_USER_MAX_RAM_SLOTS); + + return *max_memslots; +} + static int vhost_user_set_owner(struct vhost_dev *dev) { VhostUserMsg msg =3D { @@ -1439,6 +1629,24 @@ static int vhost_user_backend_init(struct vhost_dev = *dev, void *opaque) "slave-req protocol features."); return -1; } + + /* get max memory regions if backend supports configurable RAM slo= ts */ + if (!virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS)) { + u->memory_slots =3D VHOST_MEMORY_LEGACY_NREGIONS; + } else if (virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_REPLY_ACK)) { + error_report("The VHOST_USER_PROTOCOL_F_CONFIGURE_SLOTS protoc= ol " + "feature is not supported when the " + "VHOST_USER_PROTOCOL_F_REPLY_ACK features has als= o " + "been negotiated"); + return -1; + } else { + err =3D vhost_user_get_max_memslots(dev, &u->memory_slots); + if (err < 0) { + return err; + } + } } =20 if (dev->migration_blocker =3D=3D NULL && @@ -1502,7 +1710,9 @@ static int vhost_user_get_vq_index(struct vhost_dev *= dev, int idx) =20 static int vhost_user_memslots_limit(struct vhost_dev *dev) { - return VHOST_MEMORY_MAX_NREGIONS; + struct vhost_user *u =3D dev->opaque; + + return u->memory_slots; } =20 static bool vhost_user_requires_shm_log(struct vhost_dev *dev) --=20 1.8.3.1