kvm tools: mmap guest kernel instead of reading it into memory
PATCH 1/1 kvm tools: mmap guest kernel instead of reading it into memory
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
The advantages are:
- Smaller memory footprint (same effect as KSM if running multiple guests)
- Faster loading of larger kernels.
--- tools/kvm/x86/include/kvm/kvm-arch.h | 4 ++ tools/kvm/x86/kvm.c | 60 +++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 12 deletions(-)
tools/kvm/x86/include/kvm/kvm-arch.h +4 -0
@@ -35,6 +35,10 @@ struct kvm {u64 ram_size;void *ram_start; +int bz_fd; +void *bz_start; +u32 bz_len; +bool nmi_disabled;bool single_step;
tools/kvm/x86/kvm.c +48 -12
102 lines not on this page · raw message
Signature · 1 line
1.7.8
Quoted 5 lines · Sasha Levin
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
The advantages are:
- Smaller memory footprint (same effect as KSM if running multiple guests)
KSM isn't free so it's smaller footprint with less CPU cycles. It's mostly useful for the special case of running tons of guests using the same kernel image.
Quoted 10 lines · Pekka Enberg
Quoted 5 lines · Sasha Levin
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
The advantages are:
- Smaller memory footprint (same effect as KSM if running multiple guests)KSM isn't free so it's smaller footprint with less CPU cycles. It's mostly useful for the special case of running tons of guests using the same kernel image.
Another usecase which I've only first heard about couple of days ago is loading extremely large bzImages. Those images are 300MB+ in size and come with built in filesystem. Apparently it's some sort of a livecd variant.
Signature · 1 line
Sasha.
Quoted 11 lines · Sasha Levin
+ /* mmap the actual kernel */
+kvm->bz_fd=dup(fd_kernel);
+kvm->bz_len= st.st_size;
+setup_end=ALIGN(setup_size - PAGE_SIZE, PAGE_SIZE); /* Need it aligned toPAGE_SIZE*/
+kvm->bz_start= mmap(NULL,kvm->bz_len,PROT_READ|PROT_WRITE,
+MAP_PRIVATE,kvm->bz_fd,setup_end);- while ((nr =
read(fd_kernel, p, 65536)) > 0)
- p += nr;
+ /* NOP everything before the kernel start */
+memset(kvm->bz_start, 0x90, setup_size - setup_end);
So what's the deal with this NOP thing? It really needs a comment that explains it all.
Quoted 8 lines · Pekka Enberg
Quoted 7 lines · Sasha Levin
tools/kvm/x86/kvm.c +6
-0+/* mmap the actual kernel */ + kvm->bz_fd =dup (fd_kernel); + kvm->bz_len = st.st_size; + setup_end =ALIGN (setup_size -PAGE_SIZE ,PAGE_SIZE );/* Need it aligned to PAGE_SIZE */ + kvm->bz_start =mmap (NULL , kvm->bz_len,PROT_READ |PROT_WRITE , +MAP_PRIVATE , kvm->bz_fd, setup_end);
Quoted 7 lines
tools/kvm/x86/kvm.c bool load_bzimage(struct kvm *kvm, int fd_kernel, · line 311
Quoted 4 lines
tools/kvm/x86/kvm.c +2
-2-while ((nr =read (fd_kernel, p,65536 )) >0 ) - p += nr; +/* NOP everything before the kernel start */ +memset (kvm->bz_start,0x90 , setup_size - setup_end);So what's the deal with this NOP thing? It really needs a comment that explains it all.
Right, I'll explain it here and if it sounds right to you I'll add it into the patch.
Since the start of the actual kernel image is somewhere into the bzImage, and is not aligned to anything, we can't mmap() directly to the beginning of it.
So what we do is mmap the kernel with <PAGE_SIZE bytes before it which belong to the setup code.
KVM expects page aligned pointers for both in-guest physical memory start, and the corresponding userspace address. This means that we can't simply pass an offset within the memory we mapped before since it won't be page aligned.
The solution is to NOP the bytes which belong to the setup code right before the kernel starts. In practice it means <PAGE_SIZE NOPs before actual kernel code starts running.
Signature · 1 line
Sasha.
tools/kvm/x86/kvm.c bool load_bzimage(struct kvm *kvm, int fd_kernel, · line 311
Quoted 5 lines · Sasha Levin
tools/kvm/x86/kvm.c +4
-0+ kvm->bz_start =mmap (NULL , kvm->bz_len,PROT_READ |PROT_WRITE , +MAP_PRIVATE , kvm->bz_fd, setup_end); +/* NOP everything before the kernel start */ +memset (kvm->bz_start,0x90 , setup_size - setup_end);
You should really, really think about the case where mmap() fails.
Thanks,
Ingo
Quoted 6 lines · Sasha Levin
Another usecase which I've only first heard about couple of days ago is loading extremely large bzImages. Those images are 300MB+ in size and come with built in filesystem. Apparently it's some sort of a livecd variant.
Having a bootable one-file kernel/distro is useful for lots of reasons. Upgrading all my servers by replacing one file not the least of them ;)
But since I often run a pile of these simultaneously in vms, I can vouch that this functionality will be very useful indeed.
Andrew Walrond
Now if only linus will accept the rootfs is tmpfs not ramfs patch...
Quoted 27 lines · Sasha Levin
Quoted 8 lines · Pekka Enberg
Quoted 7 lines · Sasha Levin
tools/kvm/x86/kvm.c +6
-0+/* mmap the actual kernel */ + kvm->bz_fd =dup (fd_kernel); + kvm->bz_len = st.st_size; + setup_end =ALIGN (setup_size -PAGE_SIZE ,PAGE_SIZE );/* Need it aligned to PAGE_SIZE */ + kvm->bz_start =mmap (NULL , kvm->bz_len,PROT_READ |PROT_WRITE , +MAP_PRIVATE , kvm->bz_fd, setup_end);Quoted 7 lines
tools/kvm/x86/kvm.c bool load_bzimage(struct kvm *kvm, int fd_kernel, · line 311
Quoted 4 lines
tools/kvm/x86/kvm.c +2
-2-while ((nr =read (fd_kernel, p,65536 )) >0 ) - p += nr; +/* NOP everything before the kernel start */ +memset (kvm->bz_start,0x90 , setup_size - setup_end);So what's the deal with this NOP thing? It really needs a comment that explains it all.
Right, I'll explain it here and if it sounds right to you I'll add it into the patch.
Since the start of the actual kernel image is somewhere into the bzImage, and is not aligned to anything, we can't
mmap()directly to the beginning of it.So what we do is mmap the kernel with <
PAGE_SIZEbytes before it which belong to the setup code.
Quoted 8 lines
KVM expects page aligned pointers for both in-guest physical memory start, and the corresponding userspace address. This means that we can't simply pass an offset within the memory we mapped before since it won't be page aligned.
The solution is to NOP the bytes which belong to the setup code right before the kernel starts. In practice it means <
PAGE_SIZENOPs before actual kernel code starts running.
Can't you just adjust rip to point to the starting code?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Sasha Levin
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
The advantages are:
- Smaller memory footprint (same effect as KSM if running multiple guests)
- Faster loading of larger kernels.
How many COW faults do you get when loading a kernel? Would be interesting to try both smp and up guests.
Surprisingly, there is no tracepoint for that that I can see, but you can probe do_wp_page() and count it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Sasha Levin
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
tools/kvm/x86/kvm.c +3
-2- }else { -/* First RAM range from zero to the PCI gap: */ +/* Mapped kernel */ + phys_start =BZ_KERNEL_START ; + phys_size = bzl;
tools/kvm/x86/kvm.c void kvm__init_ram(struct kvm *kvm) · line 117
Quoted 10 lines
tools/kvm/x86/kvm.c +10
-0+ host_mem = kvm->bz_start; + +kvm__register_mem (kvm, phys_start, phys_size, host_mem); + +/* Rest of the memory */ + phys_start =BZ_KERNEL_START + bzl; + phys_size = kvm->ram_size - (BZ_KERNEL_START + bzl); + host_mem = kvm->ram_start + (BZ_KERNEL_START + bzl); + +kvm__register_mem (kvm, phys_start, phys_size, host_mem);
You don't actually need separate slots for this (there is no requirement that a slot == one vma).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 17 lines · Avi Kivity
Quoted 16 lines · Sasha Levin
This patch mmaps guest kernel into it's own memory slot instead of reading it into the memory.
tools/kvm/x86/kvm.c +10
-2- }else { -/* First RAM range from zero to the PCI gap: */ +/* Mapped kernel */ + phys_start =BZ_KERNEL_START ; + phys_size = bzl; + host_mem = kvm->bz_start; + +kvm__register_mem (kvm, phys_start, phys_size, host_mem); + +/* Rest of the memory */ + phys_start =BZ_KERNEL_START + bzl; + phys_size = kvm->ram_size - (BZ_KERNEL_START + bzl);
Quoted 8 lines
tools/kvm/x86/kvm.c void kvm__init_ram(struct kvm *kvm) · line 117
Quoted 4 lines
tools/kvm/x86/kvm.c +3
-0+ host_mem = kvm->ram_start + (BZ_KERNEL_START + bzl); + +kvm__register_mem (kvm, phys_start, phys_size, host_mem);You don't actually need separate slots for this (there is no requirement that a slot == one vma).
How exactly would I put it into one slot?
Signature · 1 line
Sasha.