Userspace memory registration
PATCH 2/7 do not use mem_hole anymore.
In reply to Glauber Costamemory holes are totally evil. Right now they work for some basic tests, but had never been stressed enough. Using memory holes leaves open questions like:
* what happens if a area being registered span two slots?
* what happens if there is already data in the slots?
also, the code behaves badly if the piece to be removed lies in the boundaries of the current slot. Luckily, we don't really need it. Remove it, and make sure we never hit it.
--- libkvm/libkvm.c | 69 +----------------------------------------------------- libkvm/libkvm.h | 2 - qemu/qemu-kvm.c | 13 +++++---- 3 files changed, 9 insertions(+), 75 deletions(-)
libkvm/libkvm.c +2 -67
@@ -439,74 +439,9 @@ int kvm_is_allocated_mem(kvm_context_t kvm, unsigned long phys_start,return 0 ; } -int kvm_create_mem_hole (kvm_context_t kvm,unsigned long phys_start, -unsigned long len) -{ -int slot; -int r; -struct kvm_userspace_memory_region rmslot; -struct kvm_userspace_memory_region newslot1; -struct kvm_userspace_memory_region newslot2; - - len = (len +PAGE_SIZE -1 ) &PAGE_MASK ; - - slot =get_intersecting_slot (phys_start); -/* no need to create hole, as there is already hole */ -if (slot ==-1 ) -return 0 ; - -memset (&rmslot,0 ,sizeof (struct kvm_userspace_memory_region )); -memset (&newslot1,0 ,sizeof (struct kvm_userspace_memory_region )); -memset (&newslot2,0 ,sizeof (struct kvm_userspace_memory_region )); - - rmslot.guest_phys_addr = slots[slot].phys_addr; - rmslot.slot = slot; - - newslot1.guest_phys_addr = slots[slot].phys_addr; - newslot1.memory_size = phys_start - slots[slot].phys_addr; - newslot1.slot = slot; - newslot1.userspace_addr = slots[slot].userspace_addr; - newslot1.flags = slots[slot].flags; - - newslot2.guest_phys_addr = newslot1.guest_phys_addr + - newslot1.memory_size + len; - newslot2.memory_size = slots[slot].phys_addr + - slots[slot].len - newslot2.guest_phys_addr; - newslot2.userspace_addr = newslot1.userspace_addr + - newslot1.memory_size; - newslot2.slot =get_free_slot (kvm); - newslot2.flags = newslot1.flags; - - r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &rmslot); -if (r ==-1 ) { -fprintf (stderr,"kvm_create_mem_hole: %s\n" ,strerror (errno)); -return -1 ; - } -free_slot (slot); - - r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &newslot1); -if (r ==-1 ) { -fprintf (stderr,"kvm_create_mem_hole: %s\n" ,strerror (errno)); -return -1 ; - } -register_slot (newslot1.slot, newslot1.guest_phys_addr, - newslot1.memory_size, newslot1.userspace_addr, - newslot1.flags); - - r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &newslot2); -if (r ==-1 ) { -fprintf (stderr,"kvm_create_mem_hole: %s\n" ,strerror (errno)); -return -1 ; - } -register_slot (newslot2.slot, newslot2.guest_phys_addr, - newslot2.memory_size, newslot2.userspace_addr, - newslot2.flags); -return 0 ; -} -int kvm_register_phys_mem (kvm_context_t kvm, -unsigned long phys_start,void *userspace_addr, -unsigned long len,int log) +unsigned long phys_start,void *userspace_addr, +unsigned long len,int log) {struct kvm_userspace_memory_region memory = {
libkvm/libkvm.h +0 -2
@@ -457,8 +457,6 @@ void kvm_destroy_phys_mem(kvm_context_t, unsigned long phys_start,int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start);int kvm_is_allocated_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len); -int kvm_create_mem_hole (kvm_context_t kvm,unsigned long phys_start, -unsigned long len);int kvm_register_phys_mem (kvm_context_t kvm,unsigned long phys_start,void *userspace_addr,unsigned long len,int log);
qemu/qemu-kvm.c +7 -6
@@ -773,12 +773,13 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_allocated_mem (kvm_context, start_addr, size);if (r)return ; - r =kvm_is_intersecting_mem (kvm_context, start_addr); -if (r) -kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_phys_mem (kvm_context, start_addr, - phys_ram_base + phys_offset, - size,0 ); + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) { +printf ("Ignoring intersecting memory %llx (%lx)\n" , start_addr, size); + }else + r =kvm_register_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 );if (r <0 ) {printf ("kvm_cpu_register_physical_memory: failed\n" );exit (1 );
Signature · 1 line
1.5.5.1
PATCH 6/7 register mmio slots
In reply to Glauber CostaBy analysing phys_offset, we know whether a region is an mmio region or not. If it is, we don't want to have kvm caring about it, so just return.
--- qemu/qemu-kvm.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
qemu/qemu-kvm.c +4 -0
@@ -780,6 +780,10 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_containing_region (kvm_context, start_addr, size);if (r)return ; + +if (area_flags >=TLB_MMIO ) +return ; + r =kvm_register_phys_mem (kvm_context, start_addr, phys_ram_base + phys_offset, size,0 );
Signature · 1 line
1.5.5.1
cover Userspace memory registration
Hey folks,
Userspace memory registration is back, now simpler due to some comments being addressed. we don't keep track of mmio regions at all now, which makes the code base even simpler. OTOH, mmio coalescing is left to a later work, since it now seems completely orthogonal to what I aim at this series.
PATCH 1/7 Don't separate registrations with IO_MEM_ROM set
Actually, all registrations are the same. If IO_MEM_ROM is set, we only need to take care of not passing its value as the phys_offset.
--- qemu/qemu-kvm.c | 31 +++++++++++-------------------- 1 files changed, 11 insertions(+), 20 deletions(-)
qemu/qemu-kvm.c +11 -20
@@ -768,26 +768,17 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, unsigned long phys_offset) {int r =0 ; -if (!(phys_offset & ~TARGET_PAGE_MASK )) { - r =kvm_is_allocated_mem (kvm_context, start_addr, size); -if (r) -return ; - r =kvm_is_intersecting_mem (kvm_context, start_addr); -if (r) -kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_phys_mem (kvm_context, start_addr, - phys_ram_base + phys_offset, - size,0 ); - } -if (phys_offset &IO_MEM_ROM ) { - phys_offset &= ~IO_MEM_ROM ; - r =kvm_is_intersecting_mem (kvm_context, start_addr); -if (r) -kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_phys_mem (kvm_context, start_addr, - phys_ram_base + phys_offset, - size,0 ); - } + + phys_offset &= ~IO_MEM_ROM ; + r =kvm_is_allocated_mem (kvm_context, start_addr, size); +if (r) +return ; + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r =kvm_register_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 );if (r <0 ) {printf ("kvm_cpu_register_physical_memory: failed\n" );exit (1 );
Signature · 1 line
1.5.5.1
PATCH 5/7 unregister memory area depending on their flags
In reply to Glauber Costa--- libkvm/libkvm.c | 12 ++++++++++++ libkvm/libkvm.h | 3 +++ qemu/qemu-kvm.c | 7 +++++++ 3 files changed, 22 insertions(+), 0 deletions(-)
libkvm/libkvm.c +12 -0
@@ -508,6 +508,18 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start,free_slot (memory.slot); } +void kvm_unregister_memory_area (kvm_context_t kvm,uint64_t phys_addr,unsigned long size) +{ + +int slot =get_container_slot (phys_addr, size); + +if (slot !=-1 ) { +DPRINTF ("Unregistering memory region %llx (%lx)\n" , phys_addr, size); +kvm_destroy_phys_mem (kvm, phys_addr, size); +return ; + } +} +static int kvm_get_map (kvm_context_t kvm,int ioctl_num,int slot,void *buf) {int r;
libkvm/libkvm.h +3 -0
@@ -454,6 +454,9 @@ void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start, unsigned long len, int log, int writable);void kvm_destroy_phys_mem (kvm_context_t ,unsigned long phys_start,unsigned long len); +void kvm_unregister_memory_area (kvm_context_t ,uint64_t phys_start, +unsigned long len); +int kvm_is_containing_region (kvm_context_t kvm,unsigned long phys_start,unsigned long size); int kvm_register_phys_mem(kvm_context_t kvm, unsigned long phys_start, void *userspace_addr,
qemu/qemu-kvm.c +7 -0
@@ -768,8 +768,15 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, unsigned long phys_offset) {int r =0 ; +unsigned long area_flags = phys_offset & ~TARGET_PAGE_MASK ; phys_offset &= ~IO_MEM_ROM ; + +if (area_flags ==IO_MEM_UNASSIGNED ) { +kvm_unregister_memory_area (kvm_context, start_addr, size); +return ; + } + r =kvm_is_containing_region (kvm_context, start_addr, size);if (r)return ;
Signature · 1 line
1.5.5.1
PATCH 7/7 move kvm memory registration inside qemu's
In reply to Glauber CostaRemove explicit calls to kvm_cpu_register_physical_memory, and bundle it together with qemu's memory registration function.
--- qemu/exec.c | 5 +++++ qemu/hw/ipf.c | 8 -------- qemu/hw/pc.c | 22 +--------------------- qemu/hw/ppc440_bamboo.c | 2 -- 4 files changed, 6 insertions(+), 31 deletions(-)
qemu/exec.c +5 -0
@@ -2215,6 +2215,11 @@ void cpu_register_physical_memory(target_phys_addr_t start_addr, kqemu_set_phys_mem(start_addr, size, phys_offset); }#endif +#ifdef USE_KVM +if (kvm_enabled ()) +kvm_cpu_register_physical_memory (start_addr, size, phys_offset); +#endif + size = (size +TARGET_PAGE_SIZE -1 ) &TARGET_PAGE_MASK ; end_addr = start_addr + (target_phys_addr_t )size;for (addr = start_addr; addr != end_addr; addr +=TARGET_PAGE_SIZE ) {
qemu/hw/ipf.c +0 -8
@@ -420,18 +420,14 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,if (kvm_enabled ()) { ram_addr =qemu_ram_alloc (0xa0000 );cpu_register_physical_memory (0 ,0xa0000 , ram_addr); -kvm_cpu_register_physical_memory (0 ,0xa0000 , ram_addr); ram_addr =qemu_ram_alloc (0x20000 );// Workaround 0xa0000-0xc0000 ram_addr =qemu_ram_alloc (0x40000 );cpu_register_physical_memory (0xc0000 ,0x40000 , ram_addr); -kvm_cpu_register_physical_memory (0xc0000 ,0x40000 , ram_addr); ram_addr =qemu_ram_alloc (ram_size -0x100000 );cpu_register_physical_memory (0x100000 , ram_size -0x100000 , ram_addr); -kvm_cpu_register_physical_memory (0x100000 , ram_size -0x100000 , - ram_addr); }else { ram_addr =qemu_ram_alloc (ram_size);
@@ -444,9 +440,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,if (above_4g_mem_size >0 ) { ram_addr =qemu_ram_alloc (above_4g_mem_size);cpu_register_physical_memory (0x100000000 , above_4g_mem_size, ram_addr); -if (kvm_enabled ()) -kvm_cpu_register_physical_memory (0x100000000 , above_4g_mem_size, - ram_addr); }/*Load firware to its proper position.*/
@@ -468,7 +461,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size, fw_image_start = fw_start +GFW_SIZE - image_size;cpu_register_physical_memory (GFW_START ,GFW_SIZE , fw_offset); -kvm_cpu_register_physical_memory (GFW_START ,GFW_SIZE , fw_offset);memcpy (fw_image_start, image, image_size);free (image);
qemu/hw/pc.c +1 -21
@@ -777,9 +777,7 @@ static int load_option_rom(const char *filename, int offset, int type)cpu_register_physical_memory (0xd0000 + offset, size, option_rom_offset | type);option_rom_setup_reset (0xd0000 + offset, size); -if (kvm_enabled ()) -kvm_cpu_register_physical_memory (0xd0000 + offset, - size, option_rom_offset | type); +return size; }
@@ -852,7 +850,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,/* allocate RAM */ ram_addr =qemu_ram_alloc (0xa0000 );cpu_register_physical_memory (0 ,0xa0000 , ram_addr); -kvm_cpu_register_physical_memory (0 ,0xa0000 , ram_addr);/* Allocate, even though we won't register, so we don't break the * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
@@ -863,9 +860,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,cpu_register_physical_memory (0x100000 , below_4g_mem_size -0x100000 , ram_addr); -kvm_cpu_register_physical_memory (0x100000 , - below_4g_mem_size -0x100000 , - ram_addr);/* above 4giga memory allocation */ if (above_4g_mem_size >0 ) {
@@ -881,9 +875,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,cpu_register_physical_memory (0x100000000ULL , above_4g_mem_size, ram_addr); -kvm_cpu_register_physical_memory (0x100000000ULL , - above_4g_mem_size, - ram_addr); }/* allocate VGA RAM */
@@ -927,9 +918,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,/* setup basic memory access */ cpu_register_physical_memory (0xc0000 ,0x10000 , vga_bios_offset |IO_MEM_ROM ); -if (kvm_enabled ()) -kvm_cpu_register_physical_memory (0xc0000 ,0x10000 , - vga_bios_offset |IO_MEM_ROM );/* map the last 128KB of the BIOS in ISA space */ isa_bios_size = bios_size;
@@ -941,10 +929,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,cpu_register_physical_memory (0x100000 - isa_bios_size, isa_bios_size, (bios_offset + bios_size - isa_bios_size)/* | IO_MEM_ROM */ ); -if (kvm_enabled ()) -kvm_cpu_register_physical_memory (0x100000 - isa_bios_size, - isa_bios_size, - (bios_offset + bios_size - isa_bios_size) |IO_MEM_ROM );/* XXX: for DDIM support, "ROM space" should be writable during initialization, and (optionally) marked readonly by the BIOS
@@ -963,10 +947,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,/* map all the bios at the top of memory */ cpu_register_physical_memory ((uint32_t )(-bios_size), bios_size, bios_offset |IO_MEM_ROM ); -if (kvm_enabled ()) { -kvm_cpu_register_physical_memory ((uint32_t )(-bios_size), - bios_size, bios_offset |IO_MEM_ROM ); - }bochs_bios_init ();
qemu/hw/ppc440_bamboo.c +0 -2
@@ -93,8 +93,6 @@ void bamboo_init(ram_addr_t ram_size, int vga_ram_size,/* Register mem */ cpu_register_physical_memory (0 , ram_size,0 ); -if (kvm_enabled ()) -kvm_cpu_register_physical_memory (0 , ram_size,0 );/* load kernel with uboot loader */ printf ("%s: load kernel\n" , __func__);
Signature · 1 line
1.5.5.1
PATCH 4/7 add debuging facilities to memory registration at libkvm
In reply to Glauber Costa--- libkvm/libkvm.c | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-)
libkvm/libkvm.c +13 -0
@@ -52,6 +52,15 @@#include "kvm-s390.h" #endif +//#define DEBUG_MEMREG +#ifdef DEBUG_MEMREG +#define DPRINTF(fmt, args...) \ + do { fprintf(stderr, "%s:%d " fmt , __func__, __LINE__, ##args); } while (0) +#else +#define DPRINTF(fmt, args...) do {} while (0) +#endif + +int kvm_abi =EXPECTED_KVM_API_VERSION ;int kvm_page_size;
@@ -445,6 +454,9 @@ int kvm_register_phys_mem(kvm_context_t kvm,int r; memory.slot =get_free_slot (kvm); +DPRINTF ("memory: gpa: %llx, size: %llx, uaddr: %llx, slot: %x, flags: %lx\n" , + memory.guest_phys_addr, memory.memory_size, + memory.userspace_addr, memory.slot, memory.flags); r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory);if (r ==-1 ) {fprintf (stderr,"create_userspace_phys_mem: %s\n" ,strerror (errno));
@@ -983,6 +995,7 @@ int kvm_unregister_coalesced_mmio(kvm_context_t kvm, uint64_t addr, uint32_t sizperror ("kvm_unregister_coalesced_mmio_zone" ); return -errno; } +DPRINTF ("Unregistered coalesced mmio region for %llx (%lx)\n" , addr, size);return 0 ; }#endif
Signature · 1 line
1.5.5.1
PATCH 3/7 substitute is_allocated_mem with more general is_containing_region
In reply to Glauber Costais_allocated_mem is a function that checks if every relevant aspect of the memory slot match (start and size). Replace it with a more generic function that checks if a memory region is totally contained into another. The former case is also covered.
--- libkvm/libkvm.c | 34 +++++++++++++--------------------- libkvm/libkvm.h | 4 +--- qemu/qemu-kvm.c | 12 ++++-------- 3 files changed, 18 insertions(+), 32 deletions(-)
libkvm/libkvm.c +13 -21
@@ -125,17 +125,27 @@ int get_slot(unsigned long phys_addr)return -1 ; } -int get_intersecting_slot (unsigned long phys_addr) +/* Returns -1 if this slot is not totally contained on any other, +* and the number of the slot otherwise */ +int get_container_slot (uint64_t phys_addr,unsigned long size) {int i;for (i =0 ; i <KVM_MAX_NUM_MEM_REGIONS ; ++i) -if (slots[i].len && slots[i].phys_addr < phys_addr && - (slots[i].phys_addr + slots[i].len) > phys_addr) +if (slots[i].len && slots[i].phys_addr <= phys_addr && + (slots[i].phys_addr + slots[i].len) >= phys_addr + size)return i;return -1 ; } +int kvm_is_containing_region (kvm_context_t kvm,unsigned long phys_addr,unsigned long size) +{ +int slot =get_container_slot (phys_addr, size); +if (slot ==-1 ) +return 0 ; +return 1 ; +} +/* * dirty pages logging control */
@@ -421,24 +431,6 @@ void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,return ptr; } -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start) -{ -return get_intersecting_slot (phys_start) !=-1 ; -} - -int kvm_is_allocated_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len) -{ -int slot; - - slot =get_slot (phys_start); -if (slot ==-1 ) -return 0 ; -if (slots[slot].len == len) -return 1 ; -return 0 ; -} -int kvm_register_phys_mem (kvm_context_t kvm,unsigned long phys_start,void *userspace_addr,unsigned long len,int log)
libkvm/libkvm.h +1 -3
@@ -454,9 +454,7 @@ void *kvm_create_phys_mem(kvm_context_t, unsigned long phys_start, unsigned long len, int log, int writable);void kvm_destroy_phys_mem (kvm_context_t ,unsigned long phys_start,unsigned long len); -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start); -int kvm_is_allocated_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len); +int kvm_is_containing_region (kvm_context_t kvm,unsigned long phys_start,unsigned long size);int kvm_register_phys_mem (kvm_context_t kvm,unsigned long phys_start,void *userspace_addr,unsigned long len,int log);
qemu/qemu-kvm.c +4 -8
@@ -770,16 +770,12 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,int r =0 ; phys_offset &= ~IO_MEM_ROM ; - r =kvm_is_allocated_mem (kvm_context, start_addr, size); + r =kvm_is_containing_region (kvm_context, start_addr, size);if (r)return ; - r =kvm_is_intersecting_mem (kvm_context, start_addr); -if (r) { -printf ("Ignoring intersecting memory %llx (%lx)\n" , start_addr, size); - }else - r =kvm_register_phys_mem (kvm_context, start_addr, - phys_ram_base + phys_offset, - size,0 ); + r =kvm_register_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 );if (r <0 ) {printf ("kvm_cpu_register_physical_memory: failed\n" );exit (1 );
Signature · 1 line
1.5.5.1
Avi Kivity
AppliedQuoted 7 lines · Glauber Costa
Hey folks,
Userspace memory registration is back, now simpler due to some comments being addressed. we don't keep track of mmio regions at all now, which makes the code base even simpler. OTOH, mmio coalescing is left to a later work, since it now seems completely orthogonal to what I aim at this series.
Applied, thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function