From patchwork Fri Jul 1 09:46:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prerna Saxena X-Patchwork-Id: 9209605 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0A9FC607D8 for ; Fri, 1 Jul 2016 09:47:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF62D28529 for ; Fri, 1 Jul 2016 09:47:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E3F0C2867A; Fri, 1 Jul 2016 09:47:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5C9E328529 for ; Fri, 1 Jul 2016 09:47:12 +0000 (UTC) Received: from localhost ([::1]:60708 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIv2V-0005yS-Gm for patchwork-qemu-devel@patchwork.kernel.org; Fri, 01 Jul 2016 05:47:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60037) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIv21-0005wB-NM for qemu-devel@nongnu.org; Fri, 01 Jul 2016 05:46:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bIv20-0007iA-DC for qemu-devel@nongnu.org; Fri, 01 Jul 2016 05:46:41 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:32928) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bIv20-0007hG-1w for qemu-devel@nongnu.org; Fri, 01 Jul 2016 05:46:40 -0400 Received: by mail-pf0-x242.google.com with SMTP id c74so9702991pfb.0 for ; Fri, 01 Jul 2016 02:46:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=j9q53IOgYP8IyqytdB04b2Pb/a2uQgK+i3m4KzMF0Bw=; b=lbjo0/wRiVdbDil2nntDYIClYWMtuMAB5eZ/JCHepgyzpoVrQYKj4O/rgZ/aVeX5IM 2EjIPP//Dj3yTJUotJAzt/Nba/QcRKUNtCCUyfQ8enWui3edxTXi53iy1EYWYDyCAJem GUE08jWjVhwa3GApaq9GW9/VGK6FOdx2i0UqzwwZrCboUo+pOu1ItkqMo8uUojr+zB5/ 4mtJWOWVkseD2qxA9Y7iRjmL/LqaVwNMXt9v9Q3NvXtSQjFdOAIBRalYYCMRyqv7LYBt HlgwKgL++BhBjvdeMlopXWVMALti5ngkBETPAL7HGR9GvE9ZINkndwfd4jZ16yq6gcRx fKUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=j9q53IOgYP8IyqytdB04b2Pb/a2uQgK+i3m4KzMF0Bw=; b=mfjtO8YPMp3Yf10zJyf3L6SyP7/6FyfP+QFMFpmq0oNzUDBzN24ojIO6pKPnVvMSVp l4pgNC4fk4meGtRENu1l2nNIi91QZw0GjB+UBOy/Z1TqFxp1LZIn0gDs0k0AF3VxCuLD zVQVLZWi/+jeLLPiZmRnzr+Ievzs5fxQsmQc+UE4jzDT6l0nhHoiyapGNb5IJ8gPGBUX FjGR1TJPV7Lt1BoMu79domaP9NZeouqQ9CV8idbQCYP1J7T3qyu+SQFpbXtauoFA+YRA kfQx2VPRwVzOxIKApFA3w5ZhidqyphNrQes+h60hTEtPrVl+dO4iZSOVcYS+q3SNMJci m6Tg== X-Gm-Message-State: ALyK8tLvJcsIs4Vm3wMgexvccH2GeGdtX3cgLf39lQtOXS6KGbheJQ8QME7Kzo0UML2TmA== X-Received: by 10.98.91.196 with SMTP id p187mr30166118pfb.135.1467366398041; Fri, 01 Jul 2016 02:46:38 -0700 (PDT) Received: from prerna-saxena.dev.eng.nutanix.com. (206-15-90-246.static.twtelecom.net. [206.15.90.246]) by smtp.gmail.com with ESMTPSA id t24sm3828591pfj.75.2016.07.01.02.46.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Jul 2016 02:46:36 -0700 (PDT) From: Prerna Saxena To: qemu-devel@nongnu.org Date: Fri, 1 Jul 2016 02:46:21 -0700 Message-Id: <1467366382-7216-2-git-send-email-saxenap.ltc@gmail.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1467366382-7216-1-git-send-email-saxenap.ltc@gmail.com> References: <1467366382-7216-1-git-send-email-saxenap.ltc@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH 1/2] vhost-user: Attempt to fix a race with set_mem_table. 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: Prerna Saxena , anilkumar.boggarapu@nutanix.com, mst@redhat.com, marcandre.lureau@gmail.com, felipe@nutanix.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Prerna Saxena The set_mem_table command currently does not seek a reply. Hence, there is no easy way for a remote application to notify to QEMU when it finished setting up memory, or if there were errors doing the so. As an example: (1) Qemu sends a SET_MEM_TABLE to the backend (eg, a vhost-user net application). SET_MEM_TABLE does not require a reply according to the spec. (2) Qemu commits the memory to the guest. (3) Guest issues an I/O operation over a new memory region which was configured on (1). (4) The application has not yet remapped the memory, but it sees the I/O request. (5) The application cannot satisfy the request because it does not know about those GPAs. While a guaranteed fix would require a protocol extension (committed separately), a best-effort workaround for existing applications is to send a GET_FEATURES message before completing the vhost_user_set_mem_table() call. Since GET_FEATURES requires a reply, an application that process vhost-user messages synchronously would probably have completed the SET_MEM_TABLE before replying. For a vhost-user application that processes mesages strictly in order, a response against GET_FEATURES will ensure that the application has finished processing the previous set_mem request too. Signed-off-by: Prerna Saxena --- hw/virtio/vhost-user.c | 104 +++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 495e09f..858a1bb 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -233,53 +233,6 @@ static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base, return 0; } -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 = 0; - VhostUserMsg msg = { - .request = VHOST_USER_SET_MEM_TABLE, - .flags = VHOST_USER_VERSION, - }; - - for (i = 0; i < dev->mem->nregions; ++i) { - struct vhost_memory_region *reg = dev->mem->regions + i; - ram_addr_t offset; - MemoryRegion *mr; - - assert((uintptr_t)reg->userspace_addr == reg->userspace_addr); - mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr, - &offset); - fd = memory_region_get_fd(mr); - if (fd > 0) { - msg.payload.memory.regions[fd_num].userspace_addr = reg->userspace_addr; - msg.payload.memory.regions[fd_num].memory_size = reg->memory_size; - msg.payload.memory.regions[fd_num].guest_phys_addr = reg->guest_phys_addr; - msg.payload.memory.regions[fd_num].mmap_offset = offset; - assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); - fds[fd_num++] = fd; - } - } - - msg.payload.memory.nregions = fd_num; - - if (!fd_num) { - error_report("Failed initializing vhost-user memory map, " - "consider using -object memory-backend-file share=on"); - return -1; - } - - msg.size = sizeof(msg.payload.memory.nregions); - msg.size += sizeof(msg.payload.memory.padding); - msg.size += fd_num * sizeof(VhostUserMemoryRegion); - - vhost_user_write(dev, &msg, fds, fd_num); - - return 0; -} - static int vhost_user_set_vring_addr(struct vhost_dev *dev, struct vhost_vring_addr *addr) { @@ -482,6 +435,63 @@ static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features) return vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features); } +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 = 0; + uint64_t features; + VhostUserMsg msg = { + .request = VHOST_USER_SET_MEM_TABLE, + .flags = VHOST_USER_VERSION, + }; + + for (i = 0; i < dev->mem->nregions; ++i) { + struct vhost_memory_region *reg = dev->mem->regions + i; + ram_addr_t offset; + MemoryRegion *mr; + + assert((uintptr_t)reg->userspace_addr == reg->userspace_addr); + mr = memory_region_from_host((void *)(uintptr_t)reg->userspace_addr, + &offset); + fd = memory_region_get_fd(mr); + if (fd > 0) { + msg.payload.memory.regions[fd_num].userspace_addr \ + = reg->userspace_addr; + msg.payload.memory.regions[fd_num].memory_size \ + = reg->memory_size; + msg.payload.memory.regions[fd_num].guest_phys_addr \ + = reg->guest_phys_addr; + msg.payload.memory.regions[fd_num].mmap_offset = offset; + assert(fd_num < VHOST_MEMORY_MAX_NREGIONS); + fds[fd_num++] = fd; + } + } + + msg.payload.memory.nregions = fd_num; + + if (!fd_num) { + error_report("Failed initializing vhost-user memory map, " + "consider using -object memory-backend-file share=on"); + return -1; + } + + msg.size = sizeof(msg.payload.memory.nregions); + msg.size += sizeof(msg.payload.memory.padding); + msg.size += fd_num * sizeof(VhostUserMemoryRegion); + + vhost_user_write(dev, &msg, fds, fd_num); + + /* Note: It is (yet) unknown when the client application has finished + * 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 = {