[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL v2 10/24] libvhost-user: Simplify VHOST_USER_REM_MEM_REG
|
From: |
Michael S. Tsirkin |
|
Subject: |
[PULL v2 10/24] libvhost-user: Simplify VHOST_USER_REM_MEM_REG |
|
Date: |
Sun, 6 Feb 2022 04:38:11 -0500 |
From: David Hildenbrand <david@redhat.com>
Let's avoid having to manually copy all elements. Copy only the ones
necessary to close the hole and perform the operation in-place without
a second array.
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Raphael Norwitz <raphael.norwitz@nutanix.com>
Message-Id: <20220117041050.19718-4-raphael.norwitz@nutanix.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
subprojects/libvhost-user/libvhost-user.c | 30 +++++++++++------------
1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/subprojects/libvhost-user/libvhost-user.c
b/subprojects/libvhost-user/libvhost-user.c
index 1a8fc9d600..7dd8e918b4 100644
--- a/subprojects/libvhost-user/libvhost-user.c
+++ b/subprojects/libvhost-user/libvhost-user.c
@@ -811,10 +811,8 @@ static inline bool reg_equal(VuDevRegion *vudev_reg,
static bool
vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
- int i, j;
- bool found = false;
- VuDevRegion shadow_regions[VHOST_USER_MAX_RAM_SLOTS] = {};
VhostUserMemoryRegion m = vmsg->payload.memreg.region, *msg_region = &m;
+ int i;
if (vmsg->fd_num != 1) {
vmsg_close_fds(vmsg);
@@ -841,28 +839,28 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) {
DPRINT(" mmap_offset 0x%016"PRIx64"\n",
msg_region->mmap_offset);
- for (i = 0, j = 0; i < dev->nregions; i++) {
- if (!reg_equal(&dev->regions[i], msg_region)) {
- shadow_regions[j].gpa = dev->regions[i].gpa;
- shadow_regions[j].size = dev->regions[i].size;
- shadow_regions[j].qva = dev->regions[i].qva;
- shadow_regions[j].mmap_addr = dev->regions[i].mmap_addr;
- shadow_regions[j].mmap_offset = dev->regions[i].mmap_offset;
- j++;
- } else {
- found = true;
+ for (i = 0; i < dev->nregions; i++) {
+ if (reg_equal(&dev->regions[i], msg_region)) {
VuDevRegion *r = &dev->regions[i];
void *m = (void *) (uintptr_t) r->mmap_addr;
if (m) {
munmap(m, r->size + r->mmap_offset);
}
+
+ break;
}
}
- if (found) {
- memcpy(dev->regions, shadow_regions,
- sizeof(VuDevRegion) * VHOST_USER_MAX_RAM_SLOTS);
+ if (i < dev->nregions) {
+ /*
+ * Shift all affected entries by 1 to close the hole at index i and
+ * zero out the last entry.
+ */
+ memmove(dev->regions + i, dev->regions + i + 1,
+ sizeof(VuDevRegion) * (dev->nregions - i - 1));
+ memset(dev->regions + dev->nregions - 1, 0,
+ sizeof(VuDevRegion));
DPRINT("Successfully removed a region\n");
dev->nregions--;
vmsg_set_reply_u64(vmsg, 0);
--
MST
- [PULL v2 00/24] virtio,pc: features, cleanups, fixes, Michael S. Tsirkin, 2022/02/06
- [PULL v2 07/24] tests: acpi: test short OEM_ID/OEM_TABLE_ID values in test_oem_fields(), Michael S. Tsirkin, 2022/02/06
- [PULL v2 03/24] tests: acpi: manually pad OEM_ID/OEM_TABLE_ID for test_oem_fields() test, Michael S. Tsirkin, 2022/02/06
- [PULL v2 06/24] tests: acpi: update expected blobs, Michael S. Tsirkin, 2022/02/06
- [PULL v2 05/24] acpi: fix OEM ID/OEM Table ID padding, Michael S. Tsirkin, 2022/02/06
- [PULL v2 10/24] libvhost-user: Simplify VHOST_USER_REM_MEM_REG,
Michael S. Tsirkin <=
- [PULL v2 02/24] hw/i386: Add the possibility to disable the 'isapc' machine, Michael S. Tsirkin, 2022/02/06
- [PULL v2 04/24] tests: acpi: whitelist nvdimm's SSDT and FACP.slic expected blobs, Michael S. Tsirkin, 2022/02/06
- [PULL v2 09/24] libvhost-user: Add vu_add_mem_reg input validation, Michael S. Tsirkin, 2022/02/06
- [PULL v2 13/24] libvhost-user: handle removal of identical regions, Michael S. Tsirkin, 2022/02/06
- [PULL v2 08/24] libvhost-user: Add vu_rem_mem_reg input validation, Michael S. Tsirkin, 2022/02/06
- [PULL v2 01/24] cpuid: use unsigned for max cpuid, Michael S. Tsirkin, 2022/02/06
- [PULL v2 12/24] libvhost-user: prevent over-running max RAM slots, Michael S. Tsirkin, 2022/02/06
- [PULL v2 14/24] libvhost-user: Map shared RAM with MAP_NORESERVE to support virtio-mem with hugetlb, Michael S. Tsirkin, 2022/02/06
- [PULL v2 15/24] ACPI ERST: bios-tables-test.c steps 1 and 2, Michael S. Tsirkin, 2022/02/06
- [PULL v2 16/24] ACPI ERST: PCI device_id for ERST, Michael S. Tsirkin, 2022/02/06