Instead of sending a single ioctl every time ->region_* or ->log_* callbacks are called, "queue" all memory regions in a list that will be emptied only when committing. This allow the KVM kernel API to be extended and support multiple memslots updates in a single call. Signed-off-by: Emanuele Giuseppe Esposito <eesposit@xxxxxxxxxx> --- accel/kvm/kvm-all.c | 99 ++++++++++++++++++++++++++++----------- include/sysemu/kvm_int.h | 6 +++ linux-headers/linux/kvm.h | 9 ++++ 3 files changed, 87 insertions(+), 27 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 645f0a249a..3afa46b2ef 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -357,39 +357,40 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram, return ret; } +static void kvm_memory_region_node_add(KVMMemoryListener *kml, + struct kvm_userspace_memory_region *mem) +{ + MemoryRegionNode *node; + + node = g_malloc(sizeof(MemoryRegionNode)); + *node = (MemoryRegionNode) { + .mem = mem, + }; + QTAILQ_INSERT_TAIL(&kml->mem_list, node, list); +} + static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, bool new) { - KVMState *s = kvm_state; - struct kvm_userspace_memory_region mem; - int ret; + struct kvm_userspace_memory_region *mem; - mem.slot = slot->slot | (kml->as_id << 16); - mem.guest_phys_addr = slot->start_addr; - mem.userspace_addr = (unsigned long)slot->ram; - mem.flags = slot->flags; + mem = g_malloc(sizeof(struct kvm_userspace_memory_region)); - if (slot->memory_size && !new && (mem.flags ^ slot->old_flags) & KVM_MEM_READONLY) { + mem->slot = slot->slot | (kml->as_id << 16); + mem->guest_phys_addr = slot->start_addr; + mem->userspace_addr = (unsigned long)slot->ram; + mem->flags = slot->flags; + + if (slot->memory_size && !new && (mem->flags ^ slot->old_flags) & + KVM_MEM_READONLY) { /* Set the slot size to 0 before setting the slot to the desired * value. This is needed based on KVM commit 75d61fbc. */ - mem.memory_size = 0; - ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); - if (ret < 0) { - goto err; - } - } - mem.memory_size = slot->memory_size; - ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem); - slot->old_flags = mem.flags; -err: - trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr, - mem.memory_size, mem.userspace_addr, ret); - if (ret < 0) { - error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=%d," - " start=0x%" PRIx64 ", size=0x%" PRIx64 ": %s", - __func__, mem.slot, slot->start_addr, - (uint64_t)mem.memory_size, strerror(errno)); + mem->memory_size = 0; + kvm_memory_region_node_add(kml, mem); } - return ret; + mem->memory_size = slot->memory_size; + kvm_memory_region_node_add(kml, mem); + slot->old_flags = mem->flags; + return 0; } static int do_kvm_destroy_vcpu(CPUState *cpu) @@ -1517,12 +1518,52 @@ static void kvm_region_add(MemoryListener *listener, static void kvm_region_del(MemoryListener *listener, MemoryRegionSection *section) { - KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener); + KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, + listener); kvm_set_phys_mem(kml, section, false); memory_region_unref(section->mr); } +static void kvm_begin(MemoryListener *listener) +{ + KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, + listener); + assert(QTAILQ_EMPTY(&kml->mem_list)); +} + +static void kvm_commit(MemoryListener *listener) +{ + KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, + listener); + MemoryRegionNode *node, *next; + KVMState *s = kvm_state; + + QTAILQ_FOREACH_SAFE(node, &kml->mem_list, list, next) { + struct kvm_userspace_memory_region *mem = node->mem; + int ret; + + ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, mem); + + trace_kvm_set_user_memory(mem->slot, mem->flags, mem->guest_phys_addr, + mem->memory_size, mem->userspace_addr, 0); + + if (ret < 0) { + error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=%d," + " start=0x%" PRIx64 ": %s", + __func__, mem->slot, + (uint64_t)mem->memory_size, strerror(errno)); + } + + QTAILQ_REMOVE(&kml->mem_list, node, list); + g_free(mem); + g_free(node); + } + + + +} + static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) { @@ -1664,8 +1705,12 @@ void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml, kml->slots[i].slot = i; } + QTAILQ_INIT(&kml->mem_list); + kml->listener.region_add = kvm_region_add; kml->listener.region_del = kvm_region_del; + kml->listener.begin = kvm_begin; + kml->listener.commit = kvm_commit; kml->listener.log_start = kvm_log_start; kml->listener.log_stop = kvm_log_stop; kml->listener.priority = 10; diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index 1f5487d9b7..eab8598007 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -30,9 +30,15 @@ typedef struct KVMSlot ram_addr_t ram_start_offset; } KVMSlot; +typedef struct MemoryRegionNode { + struct kvm_userspace_memory_region *mem; + QTAILQ_ENTRY(MemoryRegionNode) list; +} MemoryRegionNode; + typedef struct KVMMemoryListener { MemoryListener listener; KVMSlot *slots; + QTAILQ_HEAD(, MemoryRegionNode) mem_list; int as_id; } KVMMemoryListener; diff --git a/linux-headers/linux/kvm.h b/linux-headers/linux/kvm.h index f089349149..f215efdaa8 100644 --- a/linux-headers/linux/kvm.h +++ b/linux-headers/linux/kvm.h @@ -103,6 +103,13 @@ struct kvm_userspace_memory_region { __u64 userspace_addr; /* start of the userspace allocated memory */ }; +/* for KVM_SET_USER_MEMORY_REGION_LIST */ +struct kvm_userspace_memory_region_list { + __u32 nent; + __u32 flags; + struct kvm_userspace_memory_region entries[0]; +}; + /* * The bit 0 ~ bit 15 of kvm_memory_region::flags are visible for userspace, * other bits are reserved for kvm internal use which are defined in @@ -1426,6 +1433,8 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region) #define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47) #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_USER_MEMORY_REGION_LIST _IOW(KVMIO, 0x49, \ + struct kvm_userspace_memory_region_list) /* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping { -- 2.31.1