[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 21/68] libvhost-user: Speedup gpa_to_mem_region() and vu_gpa_to_va
From: |
Michael S. Tsirkin |
Subject: |
[PULL 21/68] libvhost-user: Speedup gpa_to_mem_region() and vu_gpa_to_va() |
Date: |
Tue, 12 Mar 2024 18:26:34 -0400 |
From: David Hildenbrand <david@redhat.com>
Let's speed up GPA to memory region / virtual address lookup. Store the
memory regions ordered by guest physical addresses, and use binary
search for address translation, as well as when adding/removing memory
regions.
Most importantly, this will speed up GPA->VA address translation when we
have many memslots.
Reviewed-by: Raphael Norwitz <raphael@enfabrica.net>
Acked-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
Message-Id: <20240214151701.29906-11-david@redhat.com>
Tested-by: Mario Casquero <mcasquer@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
subprojects/libvhost-user/libvhost-user.c | 49 +++++++++++++++++++++--
1 file changed, 45 insertions(+), 4 deletions(-)
diff --git a/subprojects/libvhost-user/libvhost-user.c
b/subprojects/libvhost-user/libvhost-user.c
index d72f25396d..ef6353d847 100644
--- a/subprojects/libvhost-user/libvhost-user.c
+++ b/subprojects/libvhost-user/libvhost-user.c
@@ -199,19 +199,30 @@ vu_panic(VuDev *dev, const char *msg, ...)
static VuDevRegion *
vu_gpa_to_mem_region(VuDev *dev, uint64_t guest_addr)
{
- unsigned int i;
+ int low = 0;
+ int high = dev->nregions - 1;
/*
* Memory regions cannot overlap in guest physical address space. Each
* GPA belongs to exactly one memory region, so there can only be one
* match.
+ *
+ * We store our memory regions ordered by GPA and can simply perform a
+ * binary search.
*/
- for (i = 0; i < dev->nregions; i++) {
- VuDevRegion *cur = &dev->regions[i];
+ while (low <= high) {
+ unsigned int mid = low + (high - low) / 2;
+ VuDevRegion *cur = &dev->regions[mid];
if (guest_addr >= cur->gpa && guest_addr < cur->gpa + cur->size) {
return cur;
}
+ if (guest_addr >= cur->gpa + cur->size) {
+ low = mid + 1;
+ }
+ if (guest_addr < cur->gpa) {
+ high = mid - 1;
+ }
}
return NULL;
}
@@ -273,9 +284,14 @@ vu_remove_all_mem_regs(VuDev *dev)
static void
_vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_region, int fd)
{
+ const uint64_t start_gpa = msg_region->guest_phys_addr;
+ const uint64_t end_gpa = start_gpa + msg_region->memory_size;
int prot = PROT_READ | PROT_WRITE;
VuDevRegion *r;
void *mmap_addr;
+ int low = 0;
+ int high = dev->nregions - 1;
+ unsigned int idx;
DPRINT("Adding region %d\n", dev->nregions);
DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n",
@@ -295,6 +311,29 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion
*msg_region, int fd)
prot = PROT_NONE;
}
+ /*
+ * We will add memory regions into the array sorted by GPA. Perform a
+ * binary search to locate the insertion point: it will be at the low
+ * index.
+ */
+ while (low <= high) {
+ unsigned int mid = low + (high - low) / 2;
+ VuDevRegion *cur = &dev->regions[mid];
+
+ /* Overlap of GPA addresses. */
+ if (start_gpa < cur->gpa + cur->size && cur->gpa < end_gpa) {
+ vu_panic(dev, "regions with overlapping guest physical addresses");
+ return;
+ }
+ if (start_gpa >= cur->gpa + cur->size) {
+ low = mid + 1;
+ }
+ if (start_gpa < cur->gpa) {
+ high = mid - 1;
+ }
+ }
+ idx = low;
+
/*
* We don't use offset argument of mmap() since the mapped address has
* to be page aligned, and we use huge pages.
@@ -308,7 +347,9 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion
*msg_region, int fd)
DPRINT(" mmap_addr: 0x%016"PRIx64"\n",
(uint64_t)(uintptr_t)mmap_addr);
- r = &dev->regions[dev->nregions];
+ /* Shift all affected entries by 1 to open a hole at idx. */
+ r = &dev->regions[idx];
+ memmove(r + 1, r, sizeof(VuDevRegion) * (dev->nregions - idx));
r->gpa = msg_region->guest_phys_addr;
r->size = msg_region->memory_size;
r->qva = msg_region->userspace_addr;
--
MST
- [PULL 11/68] vdpa: fix network breakage after cancelling migration, (continued)
- [PULL 11/68] vdpa: fix network breakage after cancelling migration, Michael S. Tsirkin, 2024/03/12
- [PULL 13/68] libvhost-user: Bump up VHOST_USER_MAX_RAM_SLOTS to 509, Michael S. Tsirkin, 2024/03/12
- [PULL 14/68] libvhost-user: Factor out removing all mem regions, Michael S. Tsirkin, 2024/03/12
- [PULL 15/68] libvhost-user: Merge vu_set_mem_table_exec_postcopy() into vu_set_mem_table_exec(), Michael S. Tsirkin, 2024/03/12
- [PULL 16/68] libvhost-user: Factor out adding a memory region, Michael S. Tsirkin, 2024/03/12
- [PULL 17/68] libvhost-user: No need to check for NULL when unmapping, Michael S. Tsirkin, 2024/03/12
- [PULL 18/68] libvhost-user: Don't zero out memory for memory regions, Michael S. Tsirkin, 2024/03/12
- [PULL 19/68] libvhost-user: Don't search for duplicates when removing memory regions, Michael S. Tsirkin, 2024/03/12
- [PULL 23/68] libvhost-user: Factor out vq usability check, Michael S. Tsirkin, 2024/03/12
- [PULL 26/68] pcie: Support PCIe Gen5/Gen6 link speeds, Michael S. Tsirkin, 2024/03/12
- [PULL 21/68] libvhost-user: Speedup gpa_to_mem_region() and vu_gpa_to_va(),
Michael S. Tsirkin <=
- [PULL 22/68] libvhost-user: Use most of mmap_offset as fd_offset, Michael S. Tsirkin, 2024/03/12
- [PULL 27/68] vdpa: stash memory region properties in vars, Michael S. Tsirkin, 2024/03/12
- [PULL 20/68] libvhost-user: Factor out search for memory region by GPA and simplify, Michael S. Tsirkin, 2024/03/12
- [PULL 24/68] libvhost-user: Dynamically remap rings after (temporarily?) removing memory regions, Michael S. Tsirkin, 2024/03/12
- [PULL 28/68] vdpa: trace skipped memory sections, Michael S. Tsirkin, 2024/03/12
- [PULL 29/68] hw/pci-bridge/pxb-cxl: Drop RAS capability from host bridge., Michael S. Tsirkin, 2024/03/12
- [PULL 30/68] hw/audio/virtio-sound: return correct command response size, Michael S. Tsirkin, 2024/03/12
- [PULL 31/68] hw/virtio: check owner for removing objects, Michael S. Tsirkin, 2024/03/12
- [PULL 33/68] hw/cxl/cxl-host: Fix missing ERRP_GUARD() in cxl_fixed_memory_window_config(), Michael S. Tsirkin, 2024/03/12