From nobody Tue Oct 7 04:10:56 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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 ARC-Seal: i=1; a=rsa-sha256; t=1589891518; cv=none; d=zohomail.com; s=zohoarc; b=JiOvNx/SA5j1r2fXpRLCTwUE+L8H+2BhUtW4TDbBX71jKXUUnflY9kGSrCiQNp+CJbJdGUlVb4NuZkNMc2KM8XD8g4U7wVEEDHrhEm2EqRRQQdHIjCuf+MgtCzaIemo3qLJ2TWNGdhgEnKKFjuCyj1qp8mJhmI9AbT0s3/M6gQ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589891518; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=jT92JqZtZUmA3PFJRrvDF8fwEZnfiERhVDKGu5hwiQ4=; b=Y1BlrVZ8XA6j+PQSqLv2o4EK0HK306rUovoSDksr/Fg4wRXsQ0qLIgD7FoTCm2C82F8ps4OZWcwGxIgNgO71YQbp/zjPYy7RurJHWAotDOZKcYNkivBYWwYOYMzIwTfXANUXq6Chr/FhphhaIuPevOBN0iuMcUeVhB50u2YCZVg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1589891518528916.5899725556524; Tue, 19 May 2020 05:31:58 -0700 (PDT) Received: from localhost ([::1]:35018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jb1PZ-0000uM-9M for importer@patchew.org; Tue, 19 May 2020 08:31:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51710) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jb1Jg-0000J6-A7 for qemu-devel@nongnu.org; Tue, 19 May 2020 08:25:52 -0400 Received: from o1.dev.nutanix.com ([198.21.4.205]:36374) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jb1Jf-0003Br-7L for qemu-devel@nongnu.org; Tue, 19 May 2020 08:25:51 -0400 Received: by filterdrecv-p3iad2-8ddf98858-szfkb with SMTP id filterdrecv-p3iad2-8ddf98858-szfkb-20-5EC3D04D-6 2020-05-19 12:25:49.190975261 +0000 UTC m=+4706298.008098870 Received: from localhost.localdomain.com (unknown) by ismtpd0002p1lon1.sendgrid.net (SG) with ESMTP id lkz1JrdfRLuDvJ5OvT0Jjg Tue, 19 May 2020 12:25:48.926 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sendgrid.net; h=from:subject:in-reply-to:references:to:cc:content-type: content-transfer-encoding; s=smtpapi; bh=jT92JqZtZUmA3PFJRrvDF8fwEZnfiERhVDKGu5hwiQ4=; b=ZQhl7QBTbZqYUh6Z5d+XgdkPNOVARmpugKSJmwVIPXw0CuZieOdsR6/5oXyVRhef7xOy 444lqNj+TRsMMpgzZc0RyPjRfJTBGKPxCWRcKi+RF6fcU4bHEXvZp8j153Y9jzZ+FKcyKa Oq1uY7mDL+YFkagWV7d8HKUfWOsMp+lJE= From: Raphael Norwitz Subject: [PATCH v3 09/10] Support individual region unmap in libvhost-user Date: Tue, 19 May 2020 12:25:49 +0000 (UTC) Message-Id: <1588473683-27067-10-git-send-email-raphael.norwitz@nutanix.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1588473683-27067-1-git-send-email-raphael.norwitz@nutanix.com> References: <1588473683-27067-1-git-send-email-raphael.norwitz@nutanix.com> X-SG-EID: =?us-ascii?Q?YCLURHX+pjNDm1i7d69iKyMnQi=2FdvWah9veFa8nllaoUC0ScIWrCgiaWGu43Vg?= =?us-ascii?Q?xFdB4istXUBpN9H93OJgc8zSN6V8XISlCxxStuT?= =?us-ascii?Q?F=2FSD=2FCRgKNDHTJ+8Y+=2FFdyf6lzmistsVxaKPxtP?= =?us-ascii?Q?dBxSJR8DMIFC1GRq7o1EYP4sEyR9tDHlXy4glAI?= =?us-ascii?Q?=2FG4Qj1aSHEpZKdvzbJMxP9IuiZJjqNbBqx0C0uP?= =?us-ascii?Q?eVK+nzO=2Fr7J0Jgr51NLm7Z4ttavJs1Z8yE=2FkhZn?= =?us-ascii?Q?38AvYPNnFFl6eGmm0vigQ=3D=3D?= To: qemu-devel@nongnu.org, mst@redhat.com, marcandre.lureau@redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=198.21.4.205; envelope-from=bounces+16159052-3d09-qemu-devel=nongnu.org@sendgrid.net; helo=o1.dev.nutanix.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/19 08:25:15 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action 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: raphael.s.norwitz@gmail.com, marcandre.lureau@gmail.com, Raphael Norwitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When the VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS protocol feature is enabled, on memory hot-unplug qemu will transmit memory regions to remove individually using the new message VHOST_USER_REM_MEM_REG message. With this change, vhost-user backends build with libvhost-user can now unmap individual memory regions when receiving the VHOST_USER_REM_MEM_REG message. Qemu only sends VHOST_USER_REM_MEM_REG messages when the VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS feature is negotiated, and support for that feature has not yet been added in libvhost-user, this new functionality is not yet used. Signed-off-by: Raphael Norwitz --- contrib/libvhost-user/libvhost-user.c | 63 +++++++++++++++++++++++++++++++= ++++ contrib/libvhost-user/libvhost-user.h | 1 + 2 files changed, 64 insertions(+) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/= libvhost-user.c index 2c2a8d9..635cfb1 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -139,6 +139,7 @@ vu_request_to_string(unsigned int req) REQ(VHOST_USER_VRING_KICK), REQ(VHOST_USER_GET_MAX_MEM_SLOTS), REQ(VHOST_USER_ADD_MEM_REG), + REQ(VHOST_USER_REM_MEM_REG), REQ(VHOST_USER_MAX), }; #undef REQ @@ -763,6 +764,66 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { } } =20 +static inline bool reg_equal(VuDevRegion *vudev_reg, + VhostUserMemoryRegion *msg_reg) +{ + if (vudev_reg->gpa =3D=3D msg_reg->guest_phys_addr && + vudev_reg->qva =3D=3D msg_reg->userspace_addr && + vudev_reg->size =3D=3D msg_reg->memory_size) { + return true; + } + + return false; +} + +static bool +vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { + int i, j; + bool found =3D false; + VuDevRegion shadow_regions[VHOST_MEMORY_MAX_NREGIONS] =3D {}; + VhostUserMemoryRegion *msg_region =3D &vmsg->payload.memreg.region; + + DPRINT("Removing region:\n"); + DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", + msg_region->guest_phys_addr); + DPRINT(" memory_size: 0x%016"PRIx64"\n", + msg_region->memory_size); + DPRINT(" userspace_addr 0x%016"PRIx64"\n", + msg_region->userspace_addr); + DPRINT(" mmap_offset 0x%016"PRIx64"\n", + msg_region->mmap_offset); + + for (i =3D 0, j =3D 0; i < dev->nregions; i++) { + if (!reg_equal(&dev->regions[i], msg_region)) { + shadow_regions[j].gpa =3D dev->regions[i].gpa; + shadow_regions[j].size =3D dev->regions[i].size; + shadow_regions[j].qva =3D dev->regions[i].qva; + shadow_regions[j].mmap_offset =3D dev->regions[i].mmap_offset; + j++; + } else { + found =3D true; + VuDevRegion *r =3D &dev->regions[i]; + void *m =3D (void *) (uintptr_t) r->mmap_addr; + + if (m) { + munmap(m, r->size + r->mmap_offset); + } + } + } + + if (found) { + memcpy(dev->regions, shadow_regions, + sizeof(VuDevRegion) * VHOST_MEMORY_MAX_NREGIONS); + DPRINT("Successfully removed a region\n"); + dev->nregions--; + vmsg_set_reply_u64(vmsg, 0); + } else { + vu_panic(dev, "Specified region not found\n"); + } + + return true; +} + static bool vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg) { @@ -1771,6 +1832,8 @@ vu_process_message(VuDev *dev, VhostUserMsg *vmsg) return vu_handle_get_max_memslots(dev, vmsg); case VHOST_USER_ADD_MEM_REG: return vu_add_mem_reg(dev, vmsg); + case VHOST_USER_REM_MEM_REG: + return vu_rem_mem_reg(dev, vmsg); default: vmsg_close_fds(vmsg); vu_panic(dev, "Unhandled request: %d", vmsg->request); diff --git a/contrib/libvhost-user/libvhost-user.h b/contrib/libvhost-user/= libvhost-user.h index 60ef7fd..f843971 100644 --- a/contrib/libvhost-user/libvhost-user.h +++ b/contrib/libvhost-user/libvhost-user.h @@ -99,6 +99,7 @@ typedef enum VhostUserRequest { VHOST_USER_VRING_KICK =3D 35, VHOST_USER_GET_MAX_MEM_SLOTS =3D 36, VHOST_USER_ADD_MEM_REG =3D 37, + VHOST_USER_REM_MEM_REG =3D 38, VHOST_USER_MAX } VhostUserRequest; =20 --=20 1.8.3.1