Get rid of kernel memory allocation
cover Get rid of kernel memory allocation
Hey guys,
Some eons before the dinosaurs went extinct, we used to support a method of memory allocation different than the one advertised by KVM_CAP_USER_MEMORY.
This series of patches attempt on removing the support for it in kvm-userspace entirely. It will make the job of integrating kvm and qemu much easier. As a matter of fact, platforms other than x86 (and ia64, because it seems to borrow a great deal of code from x86) don't even support that method.
I remind you that for those who still want to run userspaces old enough for Hypervisors that lack user memory capability, you always have the option of running an old enough userspace, so to match.
This patch series leaves one test for this capability in place, at machine
initialization: KVM will refuse to run if it's not in there. Later on, if we deprecate the capability altogether from the kernel, we may do it through an ABI check. But for now, I think this is enough.
series stat:
Glauber Costa (12):
start removing kernel memory functions remove tests of user memory from vl.c coalesce memory allocation
remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c remove KVM_CAP_USER_MEMORY from libkvm.c. remove create_kernel_phys_mem
get rid of kvm_create_userspace_phys_mem remove user_alloc field from slot representation merge destroy phys mem functions
rename kvm_register_userspace_phys_mem remove kernel memory code from hw/pc.c remove kernel memory allocation code from ipf.c
libkvm/kvm-common.h | 8 +-- libkvm/libkvm-ia64.c | 31 ---------- libkvm/libkvm-powerpc.c | 21 ------- libkvm/libkvm-s390.c | 21 ------- libkvm/libkvm-x86.c | 124 ---------------------------------------- libkvm/libkvm.c | 146 ++++++++++++++--------------------------------- libkvm/libkvm.h | 2 +- qemu/hw/ipf.c | 25 ++------- qemu/hw/pc.c | 19 +------ qemu/qemu-kvm.c | 52 +++++++---------- qemu/vl.c | 26 ++------ 11 files changed, 79 insertions(+), 396 deletions(-)
PATCH 1/1 start removing kernel memory functions
From: Glauber Costa <[email protected]>
kvm_arch_create_default_phys_mem and kvm_alloc_kernel_memory are legacy. Remove them, and their callers. The whole removal will be split for bisectability. These ones goes first because they are only called at initialization
--- libkvm/kvm-common.h | 3 -- libkvm/libkvm-ia64.c | 25 ------------- libkvm/libkvm-powerpc.c | 14 ------- libkvm/libkvm-s390.c | 14 ------- libkvm/libkvm-x86.c | 91 ----------------------------------------------- libkvm/libkvm.c | 19 +++------- 6 files changed, 5 insertions(+), 161 deletions(-)
libkvm/kvm-common.h +0 -3
@@ -78,9 +78,6 @@ void *kvm_create_kernel_phys_mem(kvm_context_t kvm, unsigned long phys_start,int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem); -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem);int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu);
libkvm/libkvm-ia64.c +0 -25
@@ -33,31 +33,6 @@#include <fcntl.h> #include <stdlib.h> -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -int zfd; - - zfd =open ("/dev/zero" ,O_RDONLY ); -if (zfd ==-1 ) { -perror ("open /dev/zero" ); -return -1 ; - } -mmap (*vm_mem +0xa8000 ,0x8000 ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_FIXED , zfd,0 ); -close (zfd); - -return 0 ; -} - - -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -return 0 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
libkvm/libkvm-powerpc.c +0 -14
@@ -43,13 +43,6 @@ int handle_dcr(struct kvm_run *run, kvm_context_t kvm, int vcpu)return ret; } -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return -1 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -102,13 +95,6 @@ int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,return 0 ; } -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -return 0 ; -} -int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu) {int ret =0 ;
libkvm/libkvm-s390.c +0 -14
@@ -22,13 +22,6 @@#include <stdio.h> #include <inttypes.h> -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return -1 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -87,13 +80,6 @@ int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,return 0 ; } -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -return 0 ; -} -int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu) {int ret =0 ;
libkvm/libkvm-x86.c +0 -91
103 lines not on this page · raw message
libkvm/libkvm.c +5 -14
@@ -328,24 +328,15 @@ static int kvm_create_default_phys_mem(kvm_context_t kvm,unsigned long phys_mem_bytes, void **vm_mem) { - unsigned long memory = (phys_mem_bytes + PAGE_SIZE - 1) & PAGE_MASK; -int r; - #ifdef KVM_CAP_USER_MEMORY - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ); + int r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);if (r >0 )return 0 ; - else +fprintf (stderr,"Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n" ); +#else +#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported#endif - r =kvm_alloc_kernel_memory (kvm, memory, vm_mem); -if (r <0 ) -return r; - - r =kvm_arch_create_default_phys_mem (kvm, phys_mem_bytes, vm_mem); -if (r <0 ) -return r; - -return 0 ; +return -1 ; }int kvm_check_extension (kvm_context_t kvm,int ext)
Signature · 1 line
1.5.5.1
PATCH 1/1 remove tests of user memory from vl.c
From: Glauber Costa <[email protected]>
kvm_qemu_create_context() will fail if we don't have userspace memory capability, so no need to check it here.
--- qemu/vl.c | 18 +++++------------- 1 files changed, 5 insertions(+), 13 deletions(-)
qemu/vl.c +5 -13
@@ -9934,20 +9934,12 @@ int main(int argc, char **argv)fprintf (stderr,"Could not create KVM context\n" );exit (1 ); } -#ifdef KVM_CAP_USER_MEMORY -{ -int ret; - ret =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (ret) { - phys_ram_base =qemu_alloc_physram (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } - } -} -#endif + phys_ram_base =qemu_alloc_physram (phys_ram_size); +if (!phys_ram_base) { +fprintf (stderr,"Could not allocate physical memory\n" ); +exit (1 ); + } }else { phys_ram_base =qemu_vmalloc (phys_ram_size);if (!phys_ram_base) {
Signature · 1 line
1.5.5.1
PATCH 1/1 coalesce memory allocation
From: Glauber Costa <[email protected]>
Put all memory allocation under qemu_alloc_physram(). Protect the mem_area code with #ifdef USE_KVM. Although it is not really needed, it help us keep track of the fact that this is a kvm-only feature to the moment.
--- qemu/vl.c | 18 +++++++----------- 1 files changed, 7 insertions(+), 11 deletions(-)
qemu/vl.c +7 -11
@@ -9019,8 +9019,10 @@ void *qemu_alloc_physram(unsigned long memory)void *area =NULL ;unsigned long map_len = memory; +#ifdef USE_KVM if (mem_path) area =alloc_mem_area (memory, &map_len, mem_path); +#endif if (!area) area =qemu_vmalloc (memory);#ifdef USE_KVM
@@ -9934,18 +9936,12 @@ int main(int argc, char **argv)fprintf (stderr,"Could not create KVM context\n" );exit (1 ); } + } - phys_ram_base =qemu_alloc_physram (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } - }else { - phys_ram_base =qemu_vmalloc (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } + phys_ram_base =qemu_alloc_physram (phys_ram_size); +if (!phys_ram_base) { +fprintf (stderr,"Could not allocate physical memory\n" ); +exit (1 ); }/* init the dynamic translator */
Signature · 1 line
1.5.5.1
PATCH 1/1 remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
From: Glauber Costa <[email protected]>
kvm_cpu_register_physical_memory() is its only user. Remove it.
--- qemu/qemu-kvm.c | 52 +++++++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 31 deletions(-)
qemu/qemu-kvm.c +21 -31
@@ -775,42 +775,32 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,unsigned long size, unsigned long phys_offset) { -#ifdef KVM_CAP_USER_MEMORY int r = 0; - - r =kvm_check_extension (kvm_context,KVM_CAP_USER_MEMORY ); -if (r) { -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_userspace_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_userspace_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 ); - } -return ; +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_userspace_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 ); } -#endif if (phys_offset &IO_MEM_ROM ) { phys_offset &= ~IO_MEM_ROM ; -memcpy (phys_ram_base + start_addr, phys_ram_base + phys_offset, size); + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r =kvm_register_userspace_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 ); } +return ; }int kvm_setup_guest_memory (void *area,unsigned long size)
Signature · 1 line
1.5.5.1
PATCH 1/1 remove KVM_CAP_USER_MEMORY from libkvm.c.
From: Glauber Costa <[email protected]>
One of them is left, which is the initial check. Code that does ioctl checking is left to be removed in a future commit.
--- libkvm/libkvm.c | 18 ------------------ 1 files changed, 0 insertions(+), 18 deletions(-)
libkvm/libkvm.c +0 -18
@@ -153,7 +153,6 @@ static int kvm_dirty_pages_log_change(kvm_context_t kvm, unsigned long phys_addrreturn 1 ; } flag |= slots[slot].flags; -#ifdef KVM_CAP_USER_MEMORYif (slots[slot].user_alloc) {struct kvm_userspace_memory_region mem = { .slot = slot,
@@ -164,7 +163,6 @@ static int kvm_dirty_pages_log_change(kvm_context_t kvm, unsigned long phys_addr }; r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); } -#endif if (!slots[slot].user_alloc) {struct kvm_memory_region mem = { .slot = slot,
@@ -388,8 +386,6 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem) } -#ifdef KVM_CAP_USER_MEMORY -void *kvm_create_userspace_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -457,20 +453,14 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,free_slot (memory.slot); } -#endif -void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) { -#ifdef KVM_CAP_USER_MEMORY -int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY );if (r >0 )return kvm_create_userspace_phys_mem (kvm, phys_start, len, log, writable);else -#endif return kvm_create_kernel_phys_mem (kvm, phys_start, len, log, writable); }
@@ -496,7 +486,6 @@ int kvm_is_allocated_mem(kvm_context_t kvm, unsigned long phys_start,int kvm_create_mem_hole (kvm_context_t kvm,unsigned long phys_start,unsigned long len) { -#ifdef KVM_CAP_USER_MEMORY int slot;int r;struct kvm_userspace_memory_region rmslot;
@@ -556,7 +545,6 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,register_slot (newslot2.slot, newslot2.guest_phys_addr, newslot2.memory_size,1 , newslot2.userspace_addr, newslot2.flags); -#endif return 0 ; }
@@ -565,7 +553,6 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm, unsigned long len, int log) { -#ifdef KVM_CAP_USER_MEMORY struct kvm_userspace_memory_region memory = { .memory_size = len, .guest_phys_addr = phys_start,
@@ -583,9 +570,6 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm,register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size,1 , memory.userspace_addr, memory.flags);return 0 ; -#else -return -ENOSYS ; -#endif }
@@ -611,11 +595,9 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -#ifdef KVM_CAP_USER_MEMORY if (ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ) >0 )kvm_destroy_userspace_phys_mem (kvm, phys_start);else -#endif kvm_create_kernel_phys_mem (kvm, phys_start,0 ,0 ,0 ); }
Signature · 1 line
1.5.5.1
PATCH 1/1 remove create_kernel_phys_mem
From: Glauber Costa <[email protected]>
Wipe out create_kernel_phys_mem(), and all of its users.
--- libkvm/kvm-common.h | 3 --- libkvm/libkvm-ia64.c | 6 ------ libkvm/libkvm-powerpc.c | 7 ------- libkvm/libkvm-s390.c | 7 ------- libkvm/libkvm-x86.c | 33 --------------------------------- libkvm/libkvm.c | 14 +++----------- 6 files changed, 3 insertions(+), 67 deletions(-)
libkvm/kvm-common.h +0 -3
@@ -72,9 +72,6 @@ int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory, void **vm_mem);int kvm_alloc_userspace_memory (kvm_context_t kvm,unsigned long memory,void **vm_mem); -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable); -int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem);
libkvm/libkvm-ia64.c +0 -6
@@ -33,12 +33,6 @@#include <fcntl.h> #include <stdlib.h> -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -return NULL ; -} -int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem) {
libkvm/libkvm-powerpc.c +0 -7
@@ -43,13 +43,6 @@ int handle_dcr(struct kvm_run *run, kvm_context_t kvm, int vcpu)return ret; } -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return NULL ; -} -void kvm_show_code (kvm_context_t kvm,int vcpu) {fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__);
libkvm/libkvm-s390.c +0 -7
@@ -22,13 +22,6 @@#include <stdio.h> #include <inttypes.h> -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return NULL ; -} -void kvm_show_code (kvm_context_t kvm,int vcpu) {fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__);
libkvm/libkvm-x86.c +0 -33
@@ -145,39 +145,6 @@ int kvm_arch_run(struct kvm_run *run,kvm_context_t kvm, int vcpu)return r; } -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -int r; -int prot =PROT_READ ; -void *ptr; -struct kvm_memory_region memory = { - .memory_size = len, - .guest_phys_addr = phys_start, - .flags = log ?KVM_MEM_LOG_DIRTY_PAGES :0 , - }; - - memory.slot =get_free_slot (kvm); - r =ioctl (kvm->vm_fd,KVM_SET_MEMORY_REGION , &memory); -if (r ==-1 ) { -fprintf (stderr,"create_kernel_phys_mem: %s" ,strerror (errno)); -return 0 ; - } -register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -0 ,0 , memory.flags); - -if (writable) - prot |=PROT_WRITE ; - - ptr =mmap (NULL , len, prot,MAP_SHARED , kvm->vm_fd, phys_start); -if (ptr ==MAP_FAILED ) { -fprintf (stderr,"create_kernel_phys_mem: %s" ,strerror (errno)); -return 0 ; - } - -return ptr; -} -#define MAX_ALIAS_SLOTS 4 static struct { uint64_t start;
libkvm/libkvm.c +3 -11
@@ -456,13 +456,8 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) { - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ); -if (r >0 ) -return kvm_create_userspace_phys_mem (kvm, phys_start, len, - log, writable); -else -return kvm_create_kernel_phys_mem (kvm, phys_start, len, - log, writable); +return kvm_create_userspace_phys_mem (kvm, phys_start, len, + log, writable); }int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start)
@@ -595,10 +590,7 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -if (ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ) >0 ) -kvm_destroy_userspace_phys_mem (kvm, phys_start); -else -kvm_create_kernel_phys_mem (kvm, phys_start,0 ,0 ,0 ); +kvm_destroy_userspace_phys_mem (kvm, phys_start); }static int kvm_get_map (kvm_context_t kvm,int ioctl_num,int slot,void *buf)
Signature · 1 line
1.5.5.1
PATCH 1/1 get rid of kvm_create_userspace_phys_mem
From: Glauber Costa <[email protected]>
It is just kvm_create_phys_mem() right now.
--- libkvm/libkvm.c | 15 ++++----------- 1 files changed, 4 insertions(+), 11 deletions(-)
libkvm/libkvm.c +4 -11
@@ -386,8 +386,8 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem) } -void *kvm_create_userspace_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) +void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start, +unsigned long len,int log,int writable) {int r;int prot =PROT_READ ;
@@ -408,7 +408,7 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start, MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS, -1, 0);#endif if (ptr ==MAP_FAILED ) { -fprintf (stderr,"create_userspace_phys_mem: %s" ,strerror (errno)); +fprintf (stderr,"%s: %s" , __func__,strerror (errno));return 0 ; }
@@ -418,7 +418,7 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start, memory.slot =get_free_slot (kvm); r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory);if (r ==-1 ) { -fprintf (stderr,"create_userspace_phys_mem: %s" ,strerror (errno)); +fprintf (stderr,"%s: %s" , __func__,strerror (errno));return 0 ; } register_slot(memory.slot, memory.guest_phys_addr, memory.memory_size,
@@ -453,13 +453,6 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,free_slot (memory.slot); } -void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -return kvm_create_userspace_phys_mem (kvm, phys_start, len, - log, writable); -} -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start) {return get_intersecting_slot (phys_start) !=-1 ;
Signature · 1 line
1.5.5.1
PATCH 1/1 remove user_alloc field from slot representation
From: Glauber Costa <[email protected]>
All allocations are "user" now. So we don't need this field anymore.
--- libkvm/kvm-common.h | 2 +- libkvm/libkvm.c | 39 +++++++++++++++------------------------ 2 files changed, 16 insertions(+), 25 deletions(-)
libkvm/kvm-common.h +1 -1
@@ -63,7 +63,7 @@ struct kvm_context {void init_slots (void );int get_free_slot (kvm_context_t kvm);void register_slot (int slot,unsigned long phys_addr,unsigned long len, -int user_alloc,unsigned long userspace_addr,unsigned flags); +unsigned long userspace_addr,unsigned flags);void free_slot (int slot);int get_slot (unsigned long phys_addr);
libkvm/libkvm.c +15 -24
@@ -58,7 +58,6 @@ int kvm_page_size;struct slot_info {unsigned long phys_addr;unsigned long len; -int user_alloc;unsigned long userspace_addr;unsigned flags; };
@@ -101,11 +100,10 @@ int get_free_slot(kvm_context_t kvm) }void register_slot (int slot,unsigned long phys_addr,unsigned long len, -int user_alloc,unsigned long userspace_addr,unsigned flags) +unsigned long userspace_addr,unsigned flags) { slots[slot].phys_addr = phys_addr; slots[slot].len = len; - slots[slot].user_alloc = user_alloc; slots[slot].userspace_addr = userspace_addr; slots[slot].flags = flags; }
@@ -144,36 +142,29 @@ int get_intersecting_slot(unsigned long phys_addr)static int kvm_dirty_pages_log_change (kvm_context_t kvm,unsigned long phys_addr ,__u32 flag) { -int r; -int slot; - - slot =get_slot (phys_addr); +int r =-1 ; +int slot =get_slot (phys_addr);if (slot ==-1 ) {fprintf (stderr,"BUG: %s: invalid parameters\n" , __FUNCTION__);return 1 ; } + flag |= slots[slot].flags; -if (slots[slot].user_alloc) { + + {struct kvm_userspace_memory_region mem = { - .slot = slot, .memory_size = slots[slot].len, .guest_phys_addr = slots[slot].phys_addr, .userspace_addr = slots[slot].userspace_addr, .flags = flag, }; + + + r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); +if (r ==-1 ) +fprintf (stderr,"%s: %m\n" , __FUNCTION__); } -if (!slots[slot].user_alloc) { -struct kvm_memory_region mem = { - .slot = slot, - .memory_size = slots[slot].len, - .guest_phys_addr = slots[slot].phys_addr, - .flags = flag, - }; - r =ioctl (kvm->vm_fd,KVM_SET_MEMORY_REGION , &mem); - } -if (r ==-1 ) -fprintf (stderr,"%s: %m\n" , __FUNCTION__);return r; }
@@ -422,7 +413,7 @@ void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,return 0 ; }register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -1 , memory.userspace_addr, memory.flags); + memory.userspace_addr, memory.flags);return ptr; }
@@ -522,7 +513,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return -1 ; }register_slot (newslot1.slot, newslot1.guest_phys_addr, - newslot1.memory_size,1 , newslot1.userspace_addr, + newslot1.memory_size, newslot1.userspace_addr, newslot1.flags); r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &newslot2);
@@ -531,7 +522,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return -1 ; }register_slot (newslot2.slot, newslot2.guest_phys_addr, - newslot2.memory_size,1 , newslot2.userspace_addr, + newslot2.memory_size, newslot2.userspace_addr, newslot2.flags);return 0 ; }
@@ -556,7 +547,7 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm,return -1 ; }register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -1 , memory.userspace_addr, memory.flags); + memory.userspace_addr, memory.flags);return 0 ; }
Signature · 1 line
1.5.5.1
PATCH 1/1 merge destroy phys mem functions
From: Glauber Costa <[email protected]>
kvm_destroy_userspace_phys_mem() and kvm_destroy_phys_mem() can be merged into a single function, now that kernel allocation is no more.
--- libkvm/libkvm.c | 45 +++++++++++++++++---------------------------- 1 files changed, 17 insertions(+), 28 deletions(-)
libkvm/libkvm.c +17 -28
@@ -418,32 +418,6 @@ void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,return ptr; } -void kvm_destroy_userspace_phys_mem (kvm_context_t kvm, -unsigned long phys_start) -{ -int r; -struct kvm_userspace_memory_region memory = { - .memory_size =0 , - .guest_phys_addr = phys_start, - .flags =0 , - }; - - memory.userspace_addr =0 ; - memory.slot =get_slot (phys_start); - -if (memory.slot ==-1 ) -return ; - - r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory); -if (r ==-1 ) { -fprintf (stderr,"destroy_userspace_phys_mem: %s" , -strerror (errno)); -return ; - } - -free_slot (memory.slot); -} -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start) {return get_intersecting_slot (phys_start) !=-1 ;
@@ -559,10 +533,17 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, unsigned long len) {int slot; +int r; +struct kvm_userspace_memory_region memory = { + .memory_size =0 , + .guest_phys_addr = phys_start, + .userspace_addr =0 , + .flags =0 , + }; slot =get_slot (phys_start); -if (slot >=KVM_MAX_NUM_MEM_REGIONS ) { +if ((slot >=KVM_MAX_NUM_MEM_REGIONS ) || (slot ==-1 )) {fprintf (stderr,"BUG: %s: invalid parameters (slot=%d)\n" , __FUNCTION__, slot);return ;
@@ -574,7 +555,15 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -kvm_destroy_userspace_phys_mem (kvm, phys_start); + memory.slot = slot; + r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory); +if (r ==-1 ) { +fprintf (stderr,"destroy_userspace_phys_mem: %s" , +strerror (errno)); +return ; + } + +free_slot (memory.slot); }static int kvm_get_map (kvm_context_t kvm,int ioctl_num,int slot,void *buf)
Signature · 1 line
1.5.5.1
PATCH 1/1 rename kvm_register_userspace_phys_mem
From: Glauber Costa <[email protected]>
Change its name to kvm_register_phys_mem().
--- libkvm/libkvm.c | 2 +- libkvm/libkvm.h | 2 +- qemu/qemu-kvm.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
libkvm/libkvm.c +1 -1
@@ -501,7 +501,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return 0 ; } -int kvm_register_userspace_phys_mem (kvm_context_t kvm, +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 -1
@@ -459,7 +459,7 @@ 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_userspace_phys_mem (kvm_context_t kvm, +int kvm_register_phys_mem (kvm_context_t kvm,unsigned long phys_start,void *userspace_addr,unsigned long len,int log);int kvm_get_dirty_pages (kvm_context_t ,unsigned long phys_addr,void *buf);
qemu/qemu-kvm.c +2 -2
@@ -783,7 +783,7 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_intersecting_mem (kvm_context, start_addr);if (r)kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + r =kvm_register_phys_mem (kvm_context, start_addr, phys_ram_base + phys_offset, size,0 ); }
@@ -792,7 +792,7 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_intersecting_mem (kvm_context, start_addr);if (r)kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + r =kvm_register_phys_mem (kvm_context, start_addr, phys_ram_base + phys_offset, size,0 ); }
Signature · 1 line
1.5.5.1
PATCH 1/1 remove kernel memory code from hw/pc.c
From: Glauber Costa <[email protected]>
--- qemu/hw/pc.c | 19 ++----------------- 1 files changed, 2 insertions(+), 17 deletions(-)
qemu/hw/pc.c +2 -17
@@ -842,8 +842,7 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,vmport_init ();/* allocate RAM */ -#ifdef KVM_CAP_USER_MEMORY -if (kvm_enabled () &&kvm_qemu_check_extension (KVM_CAP_USER_MEMORY )) { +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);
@@ -876,7 +875,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, ram_addr); } }else -#endif { ram_addr =qemu_ram_alloc (ram_size);cpu_register_physical_memory (0 , below_4g_mem_size, ram_addr);
@@ -965,21 +963,8 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,cpu_register_physical_memory ((uint32_t )(-bios_size), bios_size, bios_offset |IO_MEM_ROM );if (kvm_enabled ()) { -int r; -#ifdef KVM_CAP_USER_MEMORY - r =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (r) -kvm_cpu_register_physical_memory ((uint32_t )(-bios_size), +kvm_cpu_register_physical_memory ((uint32_t )(-bios_size), bios_size, bios_offset |IO_MEM_ROM ); - else -#endif - { - bios_mem =kvm_cpu_create_phys_mem ((uint32_t )(-bios_size), - bios_size,0 ,1 ); -if (!bios_mem) -exit (1 ); -memcpy (bios_mem, phys_ram_base + bios_offset, bios_size); - } }bochs_bios_init ();
Signature · 1 line
1.5.5.1
PATCH 1/1 remove kernel memory allocation code from ipf.c
From: Glauber Costa <[email protected]>
--- qemu/hw/ipf.c | 25 +++++-------------------- 1 files changed, 5 insertions(+), 20 deletions(-)
qemu/hw/ipf.c +5 -20
@@ -417,8 +417,7 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size, }/* allocate RAM */ -#ifdef KVM_CAP_USER_MEMORY -if (kvm_enabled () &&kvm_qemu_check_extension (KVM_CAP_USER_MEMORY )) { +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);
@@ -434,7 +433,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,kvm_cpu_register_physical_memory (0x100000 , ram_size -0x100000 , ram_addr); }else -#endif { ram_addr =qemu_ram_alloc (ram_size);cpu_register_physical_memory (0 , ram_size, ram_addr);
@@ -469,23 +467,10 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size, } fw_image_start = fw_start +GFW_SIZE - image_size; -#ifdef KVM_CAP_USER_MEMORY - r =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (r) { -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); - } - else -#endif - { - fw_start =kvm_create_phys_mem (kvm_context, (uint32_t )(-image_size), - image_size,0 ,1 ); -if (!fw_start) -exit (1 ); - fw_image_start = fw_start +GFW_SIZE - image_size; -memcpy (fw_image_start, image, 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);flush_icache_range ((unsigned long )fw_image_start, (unsigned long )fw_image_start + image_size);
Signature · 1 line
1.5.5.1
Re: [PATCH] remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
In reply to Glauber CostaQuoted 50 lines · Glauber Costa
From: Glauber Costa <[email protected]>
kvm_cpu_register_physical_memory()is its only user. Remove it.Signed-off-by Glauber Costa <[email protected]>--- qemu/qemu-kvm.c | 52 +++++++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 31 deletions(-)qemu/qemu-kvm.c +3
-29@@ -775,42 +775,32 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,unsigned long size, unsigned long phys_offset) { -#ifdef KVM_CAP_USER_MEMORY int r = 0; - - r =kvm_check_extension (kvm_context,KVM_CAP_USER_MEMORY ); -if (r) { -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_userspace_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_userspace_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 ); - } -return ; +if (!(phys_offset & ~TARGET_PAGE_MASK )) { + r =kvm_is_allocated_mem (kvm_context, start_addr, size); +if (r)
qemu/qemu-kvm.c void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, · line 790
Quoted 10 lines
qemu/qemu-kvm.c +7
-1+return ; + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 ); } -#endif if (phys_offset &IO_MEM_ROM ) {
At this chance: Shouldn't this become 'else if'?
Quoted 7 lines
qemu/qemu-kvm.c +5
-1phys_offset &= ~IO_MEM_ROM ; -memcpy (phys_ram_base + start_addr, phys_ram_base + phys_offset, size); + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r = kvm_register_userspace_phys_mem(kvm_context, start_addr, + phys_ram_base + phys_offset,
qemu/qemu-kvm.c void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, · line 806
Quoted 10 lines
qemu/qemu-kvm.c +6
-0+ size, 0); + } +if (r <0 ) { +printf ("kvm_cpu_register_physical_memory: failed\n" ); +exit (1 ); } +return ; }int kvm_setup_guest_memory (void *area,unsigned long size)
Jan
Re: [PATCH] remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
Quoted 55 lines · Jan Kiszka
Quoted 54 lines · Glauber Costa
From: Glauber Costa <[email protected]>
kvm_cpu_register_physical_memory()is its only user. Remove it.Signed-off-by Glauber Costa <[email protected]>--- qemu/qemu-kvm.c | 52 +++++++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 31 deletions(-)qemu/qemu-kvm.c +7
-29@@ -775,42 +775,32 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,unsigned long size, unsigned long phys_offset) { -#ifdef KVM_CAP_USER_MEMORY int r = 0; - - r =kvm_check_extension (kvm_context,KVM_CAP_USER_MEMORY ); -if (r) { -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_userspace_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_userspace_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 ); - } -return ; +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);
Quoted 8 lines
qemu/qemu-kvm.c void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, · line 790
Quoted 6 lines
qemu/qemu-kvm.c +3
-1+ r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 ); } -#endif if (phys_offset &IO_MEM_ROM ) {At this chance: Shouldn't this become 'else if'?
fyi: just sent out a new series, this time with numbers and no tabs in qemu ;-)
As a coding style practice, yes. But they are mutually exclusive anyway, so it shouldn't matter. We could definitely try to make it better, but they way to go is to have only one case instead of two: look at how close they are to each other! I have a later series that do that, so I'm just trying to set up the ground here.
Quoted 21 lines
Quoted 8 lines
qemu/qemu-kvm.c +5
-1phys_offset &= ~IO_MEM_ROM ; -memcpy (phys_ram_base + start_addr, phys_ram_base + phys_offset, size); + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r = kvm_register_userspace_phys_mem(kvm_context, start_addr, + phys_ram_base + phys_offset,qemu/qemu-kvm.c void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, · line 806
Quoted 10 lines
qemu/qemu-kvm.c +6
-0+ size, 0); + } +if (r <0 ) { +printf ("kvm_cpu_register_physical_memory: failed\n" ); +exit (1 ); } +return ; }int kvm_setup_guest_memory (void *area,unsigned long size)Jan
Re: [PATCH] remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
Quoted 64 lines · Glauber Costa
Quoted 54 lines · Jan Kiszka
Quoted 53 lines · Glauber Costa
From: Glauber Costa <[email protected]>
kvm_cpu_register_physical_memory()is its only user. Remove it.Signed-off-by Glauber Costa <[email protected]>--- qemu/qemu-kvm.c | 52 +++++++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 31 deletions(-)qemu/qemu-kvm.c +6
-29@@ -775,42 +775,32 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,unsigned long size, unsigned long phys_offset) { -#ifdef KVM_CAP_USER_MEMORY int r = 0; - - r =kvm_check_extension (kvm_context,KVM_CAP_USER_MEMORY ); -if (r) { -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_userspace_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_userspace_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 ); - } -return ; +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)Quoted 8 lines
qemu/qemu-kvm.c void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, · line 790
Quoted 7 lines
qemu/qemu-kvm.c +4
-1+kvm_create_mem_hole (kvm_context, start_addr, size); + r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 ); } -#endif if (phys_offset &IO_MEM_ROM ) {At this chance: Shouldn't this become 'else if'?
Quoted 1 line
fyi: just sent out a new series, this time with numbers and no tabs in qemu ;-)
Yeah, much better. :)
Quoted 5 lines
As a coding style practice, yes. But they are mutually exclusive anyway, so it shouldn't matter. We could definitely try to make it better, but they way to go is to have only one case instead of two: look at how close they are to each other! I have a later series that do that, so I'm just trying to set up the ground here.
Well, they are close partly because the IO_MEM_ROM part is incomplete (it ignores the 'read-only' property).
However, I just stumbled over it while trying to understand how this function works. Without the 'else', I had to check twice that the cases are exclusive in practice...
Jan
Quoted 15 lines · Glauber Costa
Hey guys,
Some eons before the dinosaurs went extinct, we used to support a method of memory allocation different than the one advertised by
KVM_CAP_USER_MEMORY.This series of patches attempt on removing the support for it in kvm-userspace entirely. It will make the job of integrating kvm and qemu much easier. As a matter of fact, platforms other than x86 (and ia64, because it seems to borrow a great deal of code from x86) don't even support that method.
I remind you that for those who still want to run userspaces old enough for Hypervisors that lack user memory capability, you always have the option of running an old enough userspace, so to match.
Quoted 7 lines
This patch series leaves one test for this capability in place, at machine
initialization: KVM will refuse to run if it's not in there. Later on, if we deprecate the capability altogether from the kernel, we may do it through an ABI check. But for now, I think this is enough.series stat:
Looks good. Two comments that are simultaneously critical and minor:
- use git send-email -n to number patches so I they are lexically sorted for git am (oh and --no-chain-reply-to also helps)
- qemu code is formatted with 4-space indents, no tabs. your patches seem to have 4-position tabs (which could only have been created by the recently-opened LHC, as they don't occur naturally). please talk to your editor.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 21 lines · Avi Kivity
Quoted 20 lines · Glauber Costa
Hey guys,
Some eons before the dinosaurs went extinct, we used to support a method of memory allocation different than the one advertised by
KVM_CAP_USER_MEMORY.This series of patches attempt on removing the support for it in kvm-userspace entirely. It will make the job of integrating kvm and qemu much easier. As a matter of fact, platforms other than x86 (and ia64, because it seems to borrow a great deal of code from x86) don't even support that method.
I remind you that for those who still want to run userspaces old enough for Hypervisors that lack user memory capability, you always have the option of running an old enough userspace, so to match.
This patch series leaves one test for this capability in place, at machine
initialization: KVM will refuse to run if it's not in there. Later on, if we deprecate the capability altogether from the kernel, we may do it through an ABI check. But for now, I think this is enough.
Quoted 7 lines
Quoted 2 lines
series stat:
Looks good. Two comments that are simultaneously critical and minor:
- use git send-email -n to number patches so I they are lexically sorted for git am (oh and --no-chain-reply-to also helps)
gee. I use this regularly, but this time I forgot ;-( Sorry master.
Quoted 4 lines
- qemu code is formatted with 4-space indents, no tabs. your patches seem to have 4-position tabs (which could only have been created by the recently-opened LHC, as they don't occur naturally). please talk to your editor.
Part of them. Of course one of them might have been a screw up, but mostly, I'm using 4 spaces for code under qemu/, and tabs for code in libkvm, which seems to be the standard. Should I use spaces for libkvm too?
Quoted 4 lines
Signature · 2 lines
-- error compiling committee.c: too many arguments to function
Quoted 7 lines · Glauber Costa
Quoted 3 lines
- qemu code is formatted with 4-space indents, no tabs. your patches
seem to have 4-position tabs (which could only have been created by the recently-opened LHC, as they don't occur naturally). please talk to your editor.Part of them. Of course one of them might have been a screw up, but mostly, I'm using 4 spaces for code under qemu/, and tabs for code in libkvm, which seems to be the standard. Should I use spaces for libkvm too?
No, libkvm uses those tabs. Early design error.
Signature · 1 line
error compiling committee.c: too many arguments to function
cover get rid of kernel memory registration
In reply to Glauber CostaAvi,
I hope it is better now.
For libkvm code, I'm still using tabs. For qemu, spaces. However, I'm not fixing places that were already using it. There are a lot of patches in flight right now, anthony's integration effort being the most significant ones, and simple whitespace fixes could break them. We can do a later pass that removes all the tabs.
Re: [PATCH 0/12] get rid of kernel memory registration
Quoted 7 lines · Glauber Costa
Avi,
I hope it is better now.
For libkvm code, I'm still using tabs. For qemu, spaces. However, I'm not fixing places that were already using it. There are a lot of patches in flight right now, anthony's integration effort being the most significant ones, and simple whitespace fixes could break them.
Definitely the right approach.
Quoted 3 lines
We can do
a later pass that removes all the tabs.
Or wait for the natural churn to fix it.
Who invented tabs, and what did he think he was doing?
Anyway applied the lot, thanks.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
PATCH 1/12 start removing kernel memory functions
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
kvm_arch_create_default_phys_mem and kvm_alloc_kernel_memory are legacy. Remove them, and their callers. The whole removal will be split for bisectability. These ones goes first because they are only called at initialization
--- libkvm/kvm-common.h | 3 -- libkvm/libkvm-ia64.c | 25 ------------- libkvm/libkvm-powerpc.c | 14 ------- libkvm/libkvm-s390.c | 14 ------- libkvm/libkvm-x86.c | 91 ----------------------------------------------- libkvm/libkvm.c | 19 +++------- 6 files changed, 5 insertions(+), 161 deletions(-)
libkvm/kvm-common.h +0 -3
@@ -78,9 +78,6 @@ void *kvm_create_kernel_phys_mem(kvm_context_t kvm, unsigned long phys_start,int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem); -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem);int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu);
libkvm/libkvm-ia64.c +0 -25
@@ -33,31 +33,6 @@#include <fcntl.h> #include <stdlib.h> -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -int zfd; - - zfd =open ("/dev/zero" ,O_RDONLY ); -if (zfd ==-1 ) { -perror ("open /dev/zero" ); -return -1 ; - } -mmap (*vm_mem +0xa8000 ,0x8000 ,PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_FIXED , zfd,0 ); -close (zfd); - -return 0 ; -} - - -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -return 0 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
libkvm/libkvm-powerpc.c +0 -14
@@ -43,13 +43,6 @@ int handle_dcr(struct kvm_run *run, kvm_context_t kvm, int vcpu)return ret; } -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return -1 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -102,13 +95,6 @@ int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,return 0 ; } -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -return 0 ; -} -int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu) {int ret =0 ;
libkvm/libkvm-s390.c +0 -14
@@ -22,13 +22,6 @@#include <stdio.h> #include <inttypes.h> -int kvm_alloc_kernel_memory (kvm_context_t kvm,unsigned long memory, -void **vm_mem) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return -1 ; -} -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -87,13 +80,6 @@ int kvm_arch_create(kvm_context_t kvm, unsigned long phys_mem_bytes,return 0 ; } -int kvm_arch_create_default_phys_mem (kvm_context_t kvm, -unsigned long phys_mem_bytes, -void **vm_mem) -{ -return 0 ; -} -int kvm_arch_run (struct kvm_run *run,kvm_context_t kvm,int vcpu) {int ret =0 ;
libkvm/libkvm-x86.c +0 -91
103 lines not on this page · raw message
libkvm/libkvm.c +5 -14
@@ -328,24 +328,15 @@ static int kvm_create_default_phys_mem(kvm_context_t kvm,unsigned long phys_mem_bytes, void **vm_mem) { - unsigned long memory = (phys_mem_bytes + PAGE_SIZE - 1) & PAGE_MASK; -int r; - #ifdef KVM_CAP_USER_MEMORY - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ); + int r = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);if (r >0 )return 0 ; - else +fprintf (stderr,"Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n" ); +#else +#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported#endif - r =kvm_alloc_kernel_memory (kvm, memory, vm_mem); -if (r <0 ) -return r; - - r =kvm_arch_create_default_phys_mem (kvm, phys_mem_bytes, vm_mem); -if (r <0 ) -return r; - -return 0 ; +return -1 ; }int kvm_check_extension (kvm_context_t kvm,int ext)
Signature · 1 line
1.5.5.1
PATCH 2/12 remove tests of user memory from vl.c
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
kvm_qemu_create_context() will fail if we don't have userspace memory capability, so no need to check it here.
--- qemu/vl.c | 19 +++++-------------- 1 files changed, 5 insertions(+), 14 deletions(-)
qemu/vl.c +5 -14
@@ -9934,20 +9934,11 @@ int main(int argc, char **argv)fprintf (stderr,"Could not create KVM context\n" );exit (1 ); } -#ifdef KVM_CAP_USER_MEMORY -{ -int ret; - - ret =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (ret) { - phys_ram_base =qemu_alloc_physram (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } - } -} -#endif + phys_ram_base =qemu_alloc_physram (phys_ram_size); +if (!phys_ram_base) { +fprintf (stderr,"Could not allocate physical memory\n" ); +exit (1 ); + } }else { phys_ram_base =qemu_vmalloc (phys_ram_size);if (!phys_ram_base) {
Signature · 1 line
1.5.5.1
PATCH 3/12 coalesce memory allocation
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
Put all memory allocation under qemu_alloc_physram(). Protect the mem_area code with #ifdef USE_KVM. Although it is not really needed, it help us keep track of the fact that this is a kvm-only feature to the moment.
--- qemu/vl.c | 19 ++++++++----------- 1 files changed, 8 insertions(+), 11 deletions(-)
qemu/vl.c +8 -11
@@ -9019,8 +9019,10 @@ void *qemu_alloc_physram(unsigned long memory)void *area =NULL ;unsigned long map_len = memory; +#ifdef USE_KVM if (mem_path) area =alloc_mem_area (memory, &map_len, mem_path); +#endif if (!area) area =qemu_vmalloc (memory);#ifdef USE_KVM
@@ -9934,17 +9936,12 @@ int main(int argc, char **argv)fprintf (stderr,"Could not create KVM context\n" );exit (1 ); } - phys_ram_base =qemu_alloc_physram (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } - }else { - phys_ram_base =qemu_vmalloc (phys_ram_size); -if (!phys_ram_base) { -fprintf (stderr,"Could not allocate physical memory\n" ); -exit (1 ); - } + } + + phys_ram_base =qemu_alloc_physram (phys_ram_size); +if (!phys_ram_base) { +fprintf (stderr,"Could not allocate physical memory\n" ); +exit (1 ); }/* init the dynamic translator */
Signature · 1 line
1.5.5.1
PATCH 4/12 remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
kvm_cpu_register_physical_memory() is its only user. Remove it.
--- qemu/qemu-kvm.c | 52 +++++++++++++++++++++------------------------------- 1 files changed, 21 insertions(+), 31 deletions(-)
qemu/qemu-kvm.c +21 -31
@@ -775,42 +775,32 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,unsigned long size, unsigned long phys_offset) { -#ifdef KVM_CAP_USER_MEMORY int r = 0; - - r =kvm_check_extension (kvm_context,KVM_CAP_USER_MEMORY ); -if (r) { -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_userspace_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_userspace_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 ); - } -return ; +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_userspace_phys_mem (kvm_context, start_addr, + phys_ram_base + phys_offset, + size,0 ); } -#endif if (phys_offset &IO_MEM_ROM ) { phys_offset &= ~IO_MEM_ROM ; -memcpy (phys_ram_base + start_addr, phys_ram_base + phys_offset, size); + r =kvm_is_intersecting_mem (kvm_context, start_addr); +if (r) +kvm_create_mem_hole (kvm_context, start_addr, size); + r =kvm_register_userspace_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 ); } +return ; }int kvm_setup_guest_memory (void *area,unsigned long size)
Signature · 1 line
1.5.5.1
PATCH 5/12 remove KVM_CAP_USER_MEMORY from libkvm.c.
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
One of them is left, which is the initial check. Code that does ioctl checking is left to be removed in a future commit.
--- libkvm/libkvm.c | 18 ------------------ 1 files changed, 0 insertions(+), 18 deletions(-)
libkvm/libkvm.c +0 -18
@@ -153,7 +153,6 @@ static int kvm_dirty_pages_log_change(kvm_context_t kvm, unsigned long phys_addrreturn 1 ; } flag |= slots[slot].flags; -#ifdef KVM_CAP_USER_MEMORYif (slots[slot].user_alloc) {struct kvm_userspace_memory_region mem = { .slot = slot,
@@ -164,7 +163,6 @@ static int kvm_dirty_pages_log_change(kvm_context_t kvm, unsigned long phys_addr }; r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); } -#endif if (!slots[slot].user_alloc) {struct kvm_memory_region mem = { .slot = slot,
@@ -388,8 +386,6 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem) } -#ifdef KVM_CAP_USER_MEMORY -void *kvm_create_userspace_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) {
@@ -457,20 +453,14 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,free_slot (memory.slot); } -#endif -void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) { -#ifdef KVM_CAP_USER_MEMORY -int r; - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY );if (r >0 )return kvm_create_userspace_phys_mem (kvm, phys_start, len, log, writable);else -#endif return kvm_create_kernel_phys_mem (kvm, phys_start, len, log, writable); }
@@ -496,7 +486,6 @@ int kvm_is_allocated_mem(kvm_context_t kvm, unsigned long phys_start,int kvm_create_mem_hole (kvm_context_t kvm,unsigned long phys_start,unsigned long len) { -#ifdef KVM_CAP_USER_MEMORY int slot;int r;struct kvm_userspace_memory_region rmslot;
@@ -556,7 +545,6 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,register_slot (newslot2.slot, newslot2.guest_phys_addr, newslot2.memory_size,1 , newslot2.userspace_addr, newslot2.flags); -#endif return 0 ; }
@@ -565,7 +553,6 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm, unsigned long len, int log) { -#ifdef KVM_CAP_USER_MEMORY struct kvm_userspace_memory_region memory = { .memory_size = len, .guest_phys_addr = phys_start,
@@ -583,9 +570,6 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm,register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size,1 , memory.userspace_addr, memory.flags);return 0 ; -#else -return -ENOSYS ; -#endif }
@@ -611,11 +595,9 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -#ifdef KVM_CAP_USER_MEMORY if (ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ) >0 )kvm_destroy_userspace_phys_mem (kvm, phys_start);else -#endif kvm_create_kernel_phys_mem (kvm, phys_start,0 ,0 ,0 ); }
Signature · 1 line
1.5.5.1
PATCH 6/12 remove create_kernel_phys_mem
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
Wipe out create_kernel_phys_mem(), and all of its users.
--- libkvm/kvm-common.h | 3 --- libkvm/libkvm-ia64.c | 6 ------ libkvm/libkvm-powerpc.c | 7 ------- libkvm/libkvm-s390.c | 7 ------- libkvm/libkvm-x86.c | 33 --------------------------------- libkvm/libkvm.c | 14 +++----------- 6 files changed, 3 insertions(+), 67 deletions(-)
libkvm/kvm-common.h +0 -3
@@ -72,9 +72,6 @@ int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory, void **vm_mem);int kvm_alloc_userspace_memory (kvm_context_t kvm,unsigned long memory,void **vm_mem); -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable); -int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem);
libkvm/libkvm-ia64.c +0 -6
@@ -33,12 +33,6 @@#include <fcntl.h> #include <stdlib.h> -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -return NULL ; -} -int kvm_arch_create (kvm_context_t kvm,unsigned long phys_mem_bytes,void **vm_mem) {
libkvm/libkvm-powerpc.c +0 -7
@@ -43,13 +43,6 @@ int handle_dcr(struct kvm_run *run, kvm_context_t kvm, int vcpu)return ret; } -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return NULL ; -} -void kvm_show_code (kvm_context_t kvm,int vcpu) {fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__);
libkvm/libkvm-s390.c +0 -7
@@ -22,13 +22,6 @@#include <stdio.h> #include <inttypes.h> -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__); -return NULL ; -} -void kvm_show_code (kvm_context_t kvm,int vcpu) {fprintf (stderr,"%s: Operation not supported\n" , __FUNCTION__);
libkvm/libkvm-x86.c +0 -33
@@ -145,39 +145,6 @@ int kvm_arch_run(struct kvm_run *run,kvm_context_t kvm, int vcpu)return r; } -void *kvm_create_kernel_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -int r; -int prot =PROT_READ ; -void *ptr; -struct kvm_memory_region memory = { - .memory_size = len, - .guest_phys_addr = phys_start, - .flags = log ?KVM_MEM_LOG_DIRTY_PAGES :0 , - }; - - memory.slot =get_free_slot (kvm); - r =ioctl (kvm->vm_fd,KVM_SET_MEMORY_REGION , &memory); -if (r ==-1 ) { -fprintf (stderr,"create_kernel_phys_mem: %s" ,strerror (errno)); -return 0 ; - } -register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -0 ,0 , memory.flags); - -if (writable) - prot |=PROT_WRITE ; - - ptr =mmap (NULL , len, prot,MAP_SHARED , kvm->vm_fd, phys_start); -if (ptr ==MAP_FAILED ) { -fprintf (stderr,"create_kernel_phys_mem: %s" ,strerror (errno)); -return 0 ; - } - -return ptr; -} -#define MAX_ALIAS_SLOTS 4 static struct { uint64_t start;
libkvm/libkvm.c +3 -11
@@ -456,13 +456,8 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start,unsigned long len,int log,int writable) { - r =ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ); -if (r >0 ) -return kvm_create_userspace_phys_mem (kvm, phys_start, len, - log, writable); -else -return kvm_create_kernel_phys_mem (kvm, phys_start, len, - log, writable); +return kvm_create_userspace_phys_mem (kvm, phys_start, len, + log, writable); }int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start)
@@ -595,10 +590,7 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -if (ioctl (kvm->fd,KVM_CHECK_EXTENSION ,KVM_CAP_USER_MEMORY ) >0 ) -kvm_destroy_userspace_phys_mem (kvm, phys_start); -else -kvm_create_kernel_phys_mem (kvm, phys_start,0 ,0 ,0 ); +kvm_destroy_userspace_phys_mem (kvm, phys_start); }static int kvm_get_map (kvm_context_t kvm,int ioctl_num,int slot,void *buf)
Signature · 1 line
1.5.5.1
PATCH 7/12 get rid of kvm_create_userspace_phys_mem
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
It is just kvm_create_phys_mem() right now.
--- libkvm/libkvm.c | 15 ++++----------- 1 files changed, 4 insertions(+), 11 deletions(-)
libkvm/libkvm.c +4 -11
@@ -386,8 +386,8 @@ int kvm_create(kvm_context_t kvm, unsigned long phys_mem_bytes, void **vm_mem) } -void *kvm_create_userspace_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) +void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start, +unsigned long len,int log,int writable) {int r;int prot =PROT_READ ;
@@ -408,7 +408,7 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start, MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS, -1, 0);#endif if (ptr ==MAP_FAILED ) { -fprintf (stderr,"create_userspace_phys_mem: %s" ,strerror (errno)); +fprintf (stderr,"%s: %s" , __func__,strerror (errno));return 0 ; }
@@ -418,7 +418,7 @@ void *kvm_create_userspace_phys_mem(kvm_context_t kvm, unsigned long phys_start, memory.slot =get_free_slot (kvm); r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory);if (r ==-1 ) { -fprintf (stderr,"create_userspace_phys_mem: %s" ,strerror (errno)); +fprintf (stderr,"%s: %s" , __func__,strerror (errno));return 0 ; } register_slot(memory.slot, memory.guest_phys_addr, memory.memory_size,
@@ -453,13 +453,6 @@ void kvm_destroy_userspace_phys_mem(kvm_context_t kvm,free_slot (memory.slot); } -void *kvm_create_phys_mem (kvm_context_t kvm,unsigned long phys_start, -unsigned long len,int log,int writable) -{ -return kvm_create_userspace_phys_mem (kvm, phys_start, len, - log, writable); -} -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start) {return get_intersecting_slot (phys_start) !=-1 ;
Signature · 1 line
1.5.5.1
PATCH 8/12 remove user_alloc field from slot representation
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
All allocations are "user" now. So we don't need this field anymore.
--- libkvm/kvm-common.h | 2 +- libkvm/libkvm.c | 39 +++++++++++++++------------------------ 2 files changed, 16 insertions(+), 25 deletions(-)
libkvm/kvm-common.h +1 -1
@@ -63,7 +63,7 @@ struct kvm_context {void init_slots (void );int get_free_slot (kvm_context_t kvm);void register_slot (int slot,unsigned long phys_addr,unsigned long len, -int user_alloc,unsigned long userspace_addr,unsigned flags); +unsigned long userspace_addr,unsigned flags);void free_slot (int slot);int get_slot (unsigned long phys_addr);
libkvm/libkvm.c +15 -24
@@ -58,7 +58,6 @@ int kvm_page_size;struct slot_info {unsigned long phys_addr;unsigned long len; -int user_alloc;unsigned long userspace_addr;unsigned flags; };
@@ -101,11 +100,10 @@ int get_free_slot(kvm_context_t kvm) }void register_slot (int slot,unsigned long phys_addr,unsigned long len, -int user_alloc,unsigned long userspace_addr,unsigned flags) +unsigned long userspace_addr,unsigned flags) { slots[slot].phys_addr = phys_addr; slots[slot].len = len; - slots[slot].user_alloc = user_alloc; slots[slot].userspace_addr = userspace_addr; slots[slot].flags = flags; }
@@ -144,36 +142,29 @@ int get_intersecting_slot(unsigned long phys_addr)static int kvm_dirty_pages_log_change (kvm_context_t kvm,unsigned long phys_addr ,__u32 flag) { -int r; -int slot; - - slot =get_slot (phys_addr); +int r =-1 ; +int slot =get_slot (phys_addr);if (slot ==-1 ) {fprintf (stderr,"BUG: %s: invalid parameters\n" , __FUNCTION__);return 1 ; } + flag |= slots[slot].flags; -if (slots[slot].user_alloc) { + + {struct kvm_userspace_memory_region mem = { - .slot = slot, .memory_size = slots[slot].len, .guest_phys_addr = slots[slot].phys_addr, .userspace_addr = slots[slot].userspace_addr, .flags = flag, }; + + + r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &mem); +if (r ==-1 ) +fprintf (stderr,"%s: %m\n" , __FUNCTION__); } -if (!slots[slot].user_alloc) { -struct kvm_memory_region mem = { - .slot = slot, - .memory_size = slots[slot].len, - .guest_phys_addr = slots[slot].phys_addr, - .flags = flag, - }; - r =ioctl (kvm->vm_fd,KVM_SET_MEMORY_REGION , &mem); - } -if (r ==-1 ) -fprintf (stderr,"%s: %m\n" , __FUNCTION__);return r; }
@@ -422,7 +413,7 @@ void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,return 0 ; }register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -1 , memory.userspace_addr, memory.flags); + memory.userspace_addr, memory.flags);return ptr; }
@@ -522,7 +513,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return -1 ; }register_slot (newslot1.slot, newslot1.guest_phys_addr, - newslot1.memory_size,1 , newslot1.userspace_addr, + newslot1.memory_size, newslot1.userspace_addr, newslot1.flags); r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &newslot2);
@@ -531,7 +522,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return -1 ; }register_slot (newslot2.slot, newslot2.guest_phys_addr, - newslot2.memory_size,1 , newslot2.userspace_addr, + newslot2.memory_size, newslot2.userspace_addr, newslot2.flags);return 0 ; }
@@ -556,7 +547,7 @@ int kvm_register_userspace_phys_mem(kvm_context_t kvm,return -1 ; }register_slot (memory.slot, memory.guest_phys_addr, memory.memory_size, -1 , memory.userspace_addr, memory.flags); + memory.userspace_addr, memory.flags);return 0 ; }
Signature · 1 line
1.5.5.1
PATCH 9/12 merge destroy phys mem functions
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
kvm_destroy_userspace_phys_mem() and kvm_destroy_phys_mem() can be merged into a single function, now that kernel allocation is no more.
--- libkvm/libkvm.c | 45 +++++++++++++++++---------------------------- 1 files changed, 17 insertions(+), 28 deletions(-)
libkvm/libkvm.c +17 -28
@@ -418,32 +418,6 @@ void *kvm_create_phys_mem(kvm_context_t kvm, unsigned long phys_start,return ptr; } -void kvm_destroy_userspace_phys_mem (kvm_context_t kvm, -unsigned long phys_start) -{ -int r; -struct kvm_userspace_memory_region memory = { - .memory_size =0 , - .guest_phys_addr = phys_start, - .flags =0 , - }; - - memory.userspace_addr =0 ; - memory.slot =get_slot (phys_start); - -if (memory.slot ==-1 ) -return ; - - r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory); -if (r ==-1 ) { -fprintf (stderr,"destroy_userspace_phys_mem: %s" , -strerror (errno)); -return ; - } - -free_slot (memory.slot); -} -int kvm_is_intersecting_mem (kvm_context_t kvm,unsigned long phys_start) {return get_intersecting_slot (phys_start) !=-1 ;
@@ -559,10 +533,17 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, unsigned long len) {int slot; +int r; +struct kvm_userspace_memory_region memory = { + .memory_size =0 , + .guest_phys_addr = phys_start, + .userspace_addr =0 , + .flags =0 , + }; slot =get_slot (phys_start); -if (slot >=KVM_MAX_NUM_MEM_REGIONS ) { +if ((slot >=KVM_MAX_NUM_MEM_REGIONS ) || (slot ==-1 )) {fprintf (stderr,"BUG: %s: invalid parameters (slot=%d)\n" , __FUNCTION__, slot);return ;
@@ -574,7 +555,15 @@ void kvm_destroy_phys_mem(kvm_context_t kvm, unsigned long phys_start, phys_start = slots[slot].phys_addr; } -kvm_destroy_userspace_phys_mem (kvm, phys_start); + memory.slot = slot; + r =ioctl (kvm->vm_fd,KVM_SET_USER_MEMORY_REGION , &memory); +if (r ==-1 ) { +fprintf (stderr,"destroy_userspace_phys_mem: %s" , +strerror (errno)); +return ; + } + +free_slot (memory.slot); }static int kvm_get_map (kvm_context_t kvm,int ioctl_num,int slot,void *buf)
Signature · 1 line
1.5.5.1
PATCH 10/12 rename kvm_register_userspace_phys_mem
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
Change its name to kvm_register_phys_mem().
--- libkvm/libkvm.c | 2 +- libkvm/libkvm.h | 2 +- qemu/qemu-kvm.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
libkvm/libkvm.c +1 -1
@@ -501,7 +501,7 @@ int kvm_create_mem_hole(kvm_context_t kvm, unsigned long phys_start,return 0 ; } -int kvm_register_userspace_phys_mem (kvm_context_t kvm, +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 -1
@@ -459,7 +459,7 @@ 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_userspace_phys_mem (kvm_context_t kvm, +int kvm_register_phys_mem (kvm_context_t kvm,unsigned long phys_start,void *userspace_addr,unsigned long len,int log);int kvm_get_dirty_pages (kvm_context_t ,unsigned long phys_addr,void *buf);
qemu/qemu-kvm.c +2 -2
@@ -783,7 +783,7 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_intersecting_mem (kvm_context, start_addr);if (r)kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + r =kvm_register_phys_mem (kvm_context, start_addr, phys_ram_base + phys_offset, size,0 ); }
@@ -792,7 +792,7 @@ void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr, r =kvm_is_intersecting_mem (kvm_context, start_addr);if (r)kvm_create_mem_hole (kvm_context, start_addr, size); - r =kvm_register_userspace_phys_mem (kvm_context, start_addr, + r =kvm_register_phys_mem (kvm_context, start_addr, phys_ram_base + phys_offset, size,0 ); }
Signature · 1 line
1.5.5.1
PATCH 11/12 remove kernel memory code from hw/pc.c
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
--- qemu/hw/pc.c | 19 ++----------------- 1 files changed, 2 insertions(+), 17 deletions(-)
qemu/hw/pc.c +2 -17
@@ -842,8 +842,7 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,vmport_init ();/* allocate RAM */ -#ifdef KVM_CAP_USER_MEMORY -if (kvm_enabled () &&kvm_qemu_check_extension (KVM_CAP_USER_MEMORY )) { +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);
@@ -876,7 +875,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size, ram_addr); } }else -#endif { ram_addr =qemu_ram_alloc (ram_size);cpu_register_physical_memory (0 , below_4g_mem_size, ram_addr);
@@ -965,21 +963,8 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,cpu_register_physical_memory ((uint32_t )(-bios_size), bios_size, bios_offset |IO_MEM_ROM );if (kvm_enabled ()) { -int r; -#ifdef KVM_CAP_USER_MEMORY - r =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (r) -kvm_cpu_register_physical_memory ((uint32_t )(-bios_size), +kvm_cpu_register_physical_memory ((uint32_t )(-bios_size), bios_size, bios_offset |IO_MEM_ROM ); - else -#endif - { - bios_mem =kvm_cpu_create_phys_mem ((uint32_t )(-bios_size), - bios_size,0 ,1 ); -if (!bios_mem) -exit (1 ); -memcpy (bios_mem, phys_ram_base + bios_offset, bios_size); - } }bochs_bios_init ();
Signature · 1 line
1.5.5.1
PATCH 12/12 remove kernel memory allocation code from ipf.c
In reply to Glauber CostaFrom: Glauber Costa <[email protected]>
--- qemu/hw/ipf.c | 27 ++++++--------------------- 1 files changed, 6 insertions(+), 21 deletions(-)
qemu/hw/ipf.c +6 -21
@@ -416,9 +416,8 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,qemu_register_reset (main_cpu_reset, env); } -/* allocate RAM */ -#ifdef KVM_CAP_USER_MEMORY -if (kvm_enabled () &&kvm_qemu_check_extension (KVM_CAP_USER_MEMORY )) { +/* allocate RAM */ +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);
@@ -434,7 +433,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,kvm_cpu_register_physical_memory (0x100000 , ram_size -0x100000 , ram_addr); }else -#endif { ram_addr =qemu_ram_alloc (ram_size);cpu_register_physical_memory (0 , ram_size, ram_addr);
@@ -469,23 +467,10 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size, } fw_image_start = fw_start +GFW_SIZE - image_size; -#ifdef KVM_CAP_USER_MEMORY - r =kvm_qemu_check_extension (KVM_CAP_USER_MEMORY ); -if (r) { -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); - } - else -#endif - { - fw_start =kvm_create_phys_mem (kvm_context, (uint32_t )(-image_size), - image_size,0 ,1 ); -if (!fw_start) -exit (1 ); - fw_image_start = fw_start +GFW_SIZE - image_size; -memcpy (fw_image_start, image, 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);flush_icache_range ((unsigned long )fw_image_start, (unsigned long )fw_image_start + image_size);
Signature · 1 line
1.5.5.1