[5874] Add virtio-balloon support
Revision: 5874
http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5874 Author: aliguori Date: 2008-12-04 20:33:06 +0000 (Thu, 04 Dec 2008)
Log Message:
-----------
Add virtio-balloon support
This adds a VirtIO based balloon driver. It uses madvise() to actually balloon the memory when possible.
Until 2.6.27, KVM forced memory pinning so we must disable ballooning unless the kernel actually supports it when using KVM. It's always safe when using TCG.
Modified Paths:
--------------
trunk/Makefile.target
trunk/hw/pc.c
trunk/kvm-all.c
trunk/kvm.h
trunk/monitor.c
Added Paths:
-----------
trunk/hw/virtio-balloon.c
trunk/hw/virtio-balloon.h
Modified: trunk/Makefile.target
===================================================================
Makefile.target +2 -2
@@ -665,7 +665,7 @@ OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o # virtio support -OBJS+= virtio.o virtio-blk.o +OBJS+= virtio.o virtio-blk.o virtio-balloon.o CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE endif ifeq ($(TARGET_BASE_ARCH), ppc)
@@ -684,7 +684,7 @@ # PowerPC 4xx boards OBJS+= pflash_cfi02.o ppc4xx_devs.o ppc4xx_pci.o ppc405_uc.o ppc405_boards.o # virtio support -OBJS+= virtio.o virtio-blk.o +OBJS+= virtio.o virtio-blk.o virtio-balloon.o endif ifeq ($(TARGET_BASE_ARCH), mips) OBJS+= mips_r4k.o mips_jazz.o mips_malta.o mips_mipssim.o
Modified: trunk/hw/pc.c
===================================================================
hw/pc.c +5 -0
@@ -34,6 +34,7 @@#include "console.h" #include "fw_cfg.h" #include "virtio-blk.h" +#include "virtio-balloon.h" /* output Bochs bios info messages */ //#define DEBUG_BIOS
@@ -1105,6 +1106,10 @@ unit_id++; } } + +/* Add virtio balloon device */ +if (pci_enabled) +virtio_balloon_init (pci_bus); } static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
Added: trunk/hw/virtio-balloon.c
===================================================================
hw/virtio-balloon.c +194 -0
194 lines not on this page · raw message
Added: trunk/hw/virtio-balloon.h
===================================================================
hw/virtio-balloon.h +42 -0
42 lines not on this page · raw message
Modified: trunk/kvm-all.c
===================================================================
kvm-all.c +12 -0
@@ -549,3 +549,15 @@return ret; } + +int kvm_has_sync_mmu (void ) +{ +KVMState *s = kvm_state; + +#ifdef KVM_CAP_SYNC_MMU +if (kvm_ioctl (s,KVM_CHECK_EXTENSION ,KVM_CAP_SYNC_MMU ) >0 ) +return 1 ; +#endif + +return 0 ; +}
Modified: trunk/kvm.h
===================================================================
kvm.h +3 -0
@@ -42,6 +42,9 @@int kvm_log_start (target_phys_addr_t phys_addr,target_phys_addr_t len);int kvm_log_stop (target_phys_addr_t phys_addr,target_phys_addr_t len); + +int kvm_has_sync_mmu (void ); +/* internal API */ struct KVMState ;
Modified: trunk/monitor.c
===================================================================
monitor.c +3 -1
@@ -1402,7 +1402,9 @@ram_addr_t actual; actual =qemu_balloon_status (); -if (actual ==0 ) +if (kvm_enabled () && !kvm_has_sync_mmu ()) +term_printf ("Using KVM without synchronous MMU, ballooning disabled\n" ); +else if (actual ==0 )term_printf ("Ballooning not activated in VM\n" );else term_printf ("balloon: actual=%d\n" , (int )(actual >>20 ));
hw/virtio-balloon.c line 47
Quoted 9 lines · Anthony Liguori
hw/virtio-balloon.c +8
-0+static void balloon_page (void *addr,int deflate) +{ +#if defined(__linux__) +if (!kvm_enabled () ||kvm_has_sync_mmu ()) +madvise (addr,TARGET_PAGE_SIZE , + deflate ?MADV_WILLNEED :MADV_DONTNEED ); +#endif +}
Hmm, I just noticed this... we need to use VIRTIO_BALLOON_PFN_SHIFT like Rusty did on the kernel side.
However, in general I'm not sure how this is supposed to work. Isn't it true that madvise() is a no-op if 0 < length < getpagesize()? If so, how should the guest know the chunk size needed on the host?
What happens when a guest tries to balloon 4K pages when it's backed on the host by hugetlbfs? We can't even use getpagesize() there.
Maybe the virtio balloon interface needs to advertise a unit size from the host, and use that size instead of alloc_page() in the guest?
Signature · 2 lines
Hollis Blanchard IBM Linux Technology Center
Quoted 15 lines · Hollis Blanchard
hw/virtio-balloon.c line 47
Quoted 9 lines · Anthony Liguori
hw/virtio-balloon.c +8
-0+static void balloon_page (void *addr,int deflate) +{ +#if defined(__linux__) +if (!kvm_enabled () ||kvm_has_sync_mmu ()) +madvise (addr,TARGET_PAGE_SIZE , + deflate ?MADV_WILLNEED :MADV_DONTNEED ); +#endif +}Hmm, I just noticed this... we need to use
VIRTIO_BALLOON_PFN_SHIFTlike Rusty did on the kernel side.
Quoted 4 lines
However, in general I'm not sure how this is supposed to work. Isn't it true that
madvise()is a no-op if 0 < length <getpagesize()? If so, how should the guest know the chunk size needed on the host?
We need to pass multiple of TARGET_PAGE_SIZE to madvise() but we can certainly adjust that depending on VIRTIO_BALLOON_PFN_SHIFT. But basically, if the two aren't equal, we shouldn't even try madvise().
Quoted 3 lines
What happens when a guest tries to balloon 4K pages when it's backed on the host by hugetlbfs? We can't even use
getpagesize()there.
Nothing. For ballooning to work in this circumstance, the guest would have to balloon 2MB pages which isn't something that's reasonable for it to do.
Quoted 3 lines
Maybe the virtio balloon interface needs to advertise a unit size from the host, and use that size instead of
alloc_page()in the guest?
That's a possibility. May make sense to give it the ability to balloon memory up to unit size because I don't think it'll be able to meet the reservation for large pages only.
Regards,
Anthony Liguori
Quoted 14 lines · Anthony Liguori
Quoted 13 lines · Hollis Blanchard
hw/virtio-balloon.c line 47
Quoted 8 lines · Anthony Liguori
hw/virtio-balloon.c +8
-0+static void balloon_page (void *addr,int deflate) +{ +#if defined(__linux__) +if (!kvm_enabled () ||kvm_has_sync_mmu ()) +madvise (addr,TARGET_PAGE_SIZE , + deflate ?MADV_WILLNEED :MADV_DONTNEED ); +#endif +}Hmm, I just noticed this... we need to use
VIRTIO_BALLOON_PFN_SHIFTlike Rusty did on the kernel side.
Quoted 5 lines
Quoted 3 lines
However, in general I'm not sure how this is supposed to work. Isn't it true that
madvise()is a no-op if 0 < length <getpagesize()? If so, how should the guest know the chunk size needed on the host?We need to pass multiple of
TARGET_PAGE_SIZEtomadvise()
You mean HOST_PAGE_SIZE?
My linux manpage says that the address must be page aligned.
Paul
Quoted 18 lines · Paul Brook
Quoted 16 lines · Anthony Liguori
Quoted 14 lines · Hollis Blanchard
hw/virtio-balloon.c line 47
Quoted 9 lines · Anthony Liguori
hw/virtio-balloon.c +8
-0+static void balloon_page (void *addr,int deflate) +{ +#if defined(__linux__) +if (!kvm_enabled () ||kvm_has_sync_mmu ()) +madvise (addr,TARGET_PAGE_SIZE , + deflate ?MADV_WILLNEED :MADV_DONTNEED ); +#endif +}Hmm, I just noticed this... we need to use
VIRTIO_BALLOON_PFN_SHIFTlike Rusty did on the kernel side.
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
However, in general I'm not sure how this is supposed to work. Isn't it true that
madvise()is a no-op if 0 < length <getpagesize()? If so, how should the guest know the chunk size needed on the host?We need to pass multiple of
TARGET_PAGE_SIZEtomadvise()You mean
HOST_PAGE_SIZE?
I change my previous answer :-)
I think we should just pass addr and the appropriate size of the memory the guest is ballooning. Whether the host can handle it is up to it. Worst case scenario, madvise() returns an error and no harm is done.
Regards,
Anthony Liguori
Quoted 4 lines
My linux manpage says that the address must be page aligned.
Paul