Commit bcfeacab authored by Igor Mammedov's avatar Igor Mammedov Committed by Michael S. Tsirkin

vhost: use binary search instead of linear in find_region()

For default region layouts performance stays the same
as linear search i.e. it takes around 210ns average for
translate_desc() that inlines find_region().

But it scales better with larger amount of regions,
235ns BS vs 300ns LS with 55 memory regions
and it will be about the same values when allowed number
of slots is increased to 509 like it has been done in kvm.
Signed-off-by: default avatarIgor Mammedov <imammedo@redhat.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 8b0a9d42
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <linux/kthread.h> #include <linux/kthread.h>
#include <linux/cgroup.h> #include <linux/cgroup.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/sort.h>
#include "vhost.h" #include "vhost.h"
...@@ -663,6 +664,16 @@ int vhost_vq_access_ok(struct vhost_virtqueue *vq) ...@@ -663,6 +664,16 @@ int vhost_vq_access_ok(struct vhost_virtqueue *vq)
} }
EXPORT_SYMBOL_GPL(vhost_vq_access_ok); EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
{
const struct vhost_memory_region *r1 = p1, *r2 = p2;
if (r1->guest_phys_addr < r2->guest_phys_addr)
return 1;
if (r1->guest_phys_addr > r2->guest_phys_addr)
return -1;
return 0;
}
static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
{ {
struct vhost_memory mem, *newmem, *oldmem; struct vhost_memory mem, *newmem, *oldmem;
...@@ -682,9 +693,11 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) ...@@ -682,9 +693,11 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
memcpy(newmem, &mem, size); memcpy(newmem, &mem, size);
if (copy_from_user(newmem->regions, m->regions, if (copy_from_user(newmem->regions, m->regions,
mem.nregions * sizeof *m->regions)) { mem.nregions * sizeof *m->regions)) {
kfree(newmem); kvfree(newmem);
return -EFAULT; return -EFAULT;
} }
sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
vhost_memory_reg_sort_cmp, NULL);
if (!memory_access_ok(d, newmem, 0)) { if (!memory_access_ok(d, newmem, 0)) {
kfree(newmem); kfree(newmem);
...@@ -992,17 +1005,22 @@ EXPORT_SYMBOL_GPL(vhost_dev_ioctl); ...@@ -992,17 +1005,22 @@ EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
static const struct vhost_memory_region *find_region(struct vhost_memory *mem, static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
__u64 addr, __u32 len) __u64 addr, __u32 len)
{ {
struct vhost_memory_region *reg; const struct vhost_memory_region *reg;
int i; int start = 0, end = mem->nregions;
/* linear search is not brilliant, but we really have on the order of 6 while (start < end) {
* regions in practice */ int slot = start + (end - start) / 2;
for (i = 0; i < mem->nregions; ++i) { reg = mem->regions + slot;
reg = mem->regions + i; if (addr >= reg->guest_phys_addr)
if (reg->guest_phys_addr <= addr && end = slot;
reg->guest_phys_addr + reg->memory_size - 1 >= addr) else
return reg; start = slot + 1;
} }
reg = mem->regions + start;
if (addr >= reg->guest_phys_addr &&
reg->guest_phys_addr + reg->memory_size > addr)
return reg;
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment