Commit bd3220870f677d5b08f59d7e786ac18bde1c1b7c

Authored by aliguori
1 parent df751fa8

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.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>



git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5874 c046a42c-6fe2-441c-8c8c-71466251a162
Makefile.target
... ... @@ -665,7 +665,7 @@ OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o
665 665 OBJS+= cirrus_vga.o apic.o parallel.o acpi.o piix_pci.o
666 666 OBJS+= usb-uhci.o vmmouse.o vmport.o vmware_vga.o
667 667 # virtio support
668   -OBJS+= virtio.o virtio-blk.o
  668 +OBJS+= virtio.o virtio-blk.o virtio-balloon.o
669 669 CPPFLAGS += -DHAS_AUDIO -DHAS_AUDIO_CHOICE
670 670 endif
671 671 ifeq ($(TARGET_BASE_ARCH), ppc)
... ... @@ -684,7 +684,7 @@ OBJS+= unin_pci.o ppc_chrp.o
684 684 # PowerPC 4xx boards
685 685 OBJS+= pflash_cfi02.o ppc4xx_devs.o ppc4xx_pci.o ppc405_uc.o ppc405_boards.o
686 686 # virtio support
687   -OBJS+= virtio.o virtio-blk.o
  687 +OBJS+= virtio.o virtio-blk.o virtio-balloon.o
688 688 endif
689 689 ifeq ($(TARGET_BASE_ARCH), mips)
690 690 OBJS+= mips_r4k.o mips_jazz.o mips_malta.o mips_mipssim.o
... ...
... ... @@ -34,6 +34,7 @@
34 34 #include "console.h"
35 35 #include "fw_cfg.h"
36 36 #include "virtio-blk.h"
  37 +#include "virtio-balloon.h"
37 38  
38 39 /* output Bochs bios info messages */
39 40 //#define DEBUG_BIOS
... ... @@ -1105,6 +1106,10 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
1105 1106 unit_id++;
1106 1107 }
1107 1108 }
  1109 +
  1110 + /* Add virtio balloon device */
  1111 + if (pci_enabled)
  1112 + virtio_balloon_init(pci_bus);
1108 1113 }
1109 1114  
1110 1115 static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
... ...
hw/virtio-balloon.c 0 → 100644
  1 +/*
  2 + * Virtio Block Device
  3 + *
  4 + * Copyright IBM, Corp. 2008
  5 + *
  6 + * Authors:
  7 + * Anthony Liguori <aliguori@us.ibm.com>
  8 + *
  9 + * This work is licensed under the terms of the GNU GPL, version 2. See
  10 + * the COPYING file in the top-level directory.
  11 + *
  12 + */
  13 +
  14 +#include "qemu-common.h"
  15 +#include "virtio.h"
  16 +#include "pc.h"
  17 +#include "sysemu.h"
  18 +#include "cpu.h"
  19 +#include "balloon.h"
  20 +#include "virtio-balloon.h"
  21 +#include "kvm.h"
  22 +
  23 +#if defined(__linux__)
  24 +#include <sys/mman.h>
  25 +#endif
  26 +
  27 +typedef struct VirtIOBalloon
  28 +{
  29 + VirtIODevice vdev;
  30 + VirtQueue *ivq, *dvq;
  31 + uint32_t num_pages;
  32 + uint32_t actual;
  33 +} VirtIOBalloon;
  34 +
  35 +static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
  36 +{
  37 + return (VirtIOBalloon *)vdev;
  38 +}
  39 +
  40 +static void balloon_page(void *addr, int deflate)
  41 +{
  42 +#if defined(__linux__)
  43 + if (!kvm_enabled() || kvm_has_sync_mmu())
  44 + madvise(addr, TARGET_PAGE_SIZE,
  45 + deflate ? MADV_WILLNEED : MADV_DONTNEED);
  46 +#endif
  47 +}
  48 +
  49 +/* FIXME: once we do a virtio refactoring, this will get subsumed into common
  50 + * code */
  51 +static size_t memcpy_from_iovector(void *data, size_t offset, size_t size,
  52 + struct iovec *iov, int iovlen)
  53 +{
  54 + int i;
  55 + uint8_t *ptr = data;
  56 + size_t iov_off = 0;
  57 + size_t data_off = 0;
  58 +
  59 + for (i = 0; i < iovlen && size; i++) {
  60 + if (offset < (iov_off + iov[i].iov_len)) {
  61 + size_t len = MIN((iov_off + iov[i].iov_len) - offset , size);
  62 +
  63 + memcpy(ptr + data_off, iov[i].iov_base + (offset - iov_off), len);
  64 +
  65 + data_off += len;
  66 + offset += len;
  67 + size -= len;
  68 + }
  69 +
  70 + iov_off += iov[i].iov_len;
  71 + }
  72 +
  73 + return data_off;
  74 +}
  75 +
  76 +static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
  77 +{
  78 + VirtIOBalloon *s = to_virtio_balloon(vdev);
  79 + VirtQueueElement elem;
  80 +
  81 + while (virtqueue_pop(vq, &elem)) {
  82 + size_t offset = 0;
  83 + uint32_t pfn;
  84 +
  85 + while (memcpy_from_iovector(&pfn, offset, 4,
  86 + elem.out_sg, elem.out_num) == 4) {
  87 + ram_addr_t pa;
  88 + ram_addr_t addr;
  89 +
  90 + pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
  91 + offset += 4;
  92 +
  93 + addr = cpu_get_physical_page_desc(pa);
  94 + if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
  95 + continue;
  96 +
  97 + balloon_page(phys_ram_base + addr, !!(vq == s->dvq));
  98 + }
  99 +
  100 + virtqueue_push(vq, &elem, offset);
  101 + virtio_notify(vdev, vq);
  102 + }
  103 +}
  104 +
  105 +static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
  106 +{
  107 + VirtIOBalloon *dev = to_virtio_balloon(vdev);
  108 + struct virtio_balloon_config config;
  109 +
  110 + config.num_pages = cpu_to_le32(dev->num_pages);
  111 + config.actual = cpu_to_le32(dev->actual);
  112 +
  113 + memcpy(config_data, &config, 8);
  114 +}
  115 +
  116 +static void virtio_balloon_set_config(VirtIODevice *vdev,
  117 + const uint8_t *config_data)
  118 +{
  119 + VirtIOBalloon *dev = to_virtio_balloon(vdev);
  120 + struct virtio_balloon_config config;
  121 + memcpy(&config, config_data, 8);
  122 + dev->actual = config.actual;
  123 +}
  124 +
  125 +static uint32_t virtio_balloon_get_features(VirtIODevice *vdev)
  126 +{
  127 + return 0;
  128 +}
  129 +
  130 +static ram_addr_t virtio_balloon_to_target(void *opaque, ram_addr_t target)
  131 +{
  132 + VirtIOBalloon *dev = opaque;
  133 +
  134 + if (target > ram_size)
  135 + target = ram_size;
  136 +
  137 + if (target) {
  138 + dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
  139 + virtio_notify_config(&dev->vdev);
  140 + }
  141 +
  142 + return ram_size - (dev->actual << VIRTIO_BALLOON_PFN_SHIFT);
  143 +}
  144 +
  145 +static void virtio_balloon_save(QEMUFile *f, void *opaque)
  146 +{
  147 + VirtIOBalloon *s = opaque;
  148 +
  149 + virtio_save(&s->vdev, f);
  150 +
  151 + qemu_put_be32(f, s->num_pages);
  152 + qemu_put_be32(f, s->actual);
  153 +}
  154 +
  155 +static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
  156 +{
  157 + VirtIOBalloon *s = opaque;
  158 +
  159 + if (version_id != 1)
  160 + return -EINVAL;
  161 +
  162 + virtio_load(&s->vdev, f);
  163 +
  164 + s->num_pages = qemu_get_be32(f);
  165 + s->actual = qemu_get_be32(f);
  166 +
  167 + return 0;
  168 +}
  169 +
  170 +void *virtio_balloon_init(PCIBus *bus)
  171 +{
  172 + VirtIOBalloon *s;
  173 +
  174 + s = (VirtIOBalloon *)virtio_init_pci(bus, "virtio-balloon",
  175 + 6900, 0x1002,
  176 + 0, VIRTIO_ID_BALLOON,
  177 + 0x05, 0x00, 0x00,
  178 + 8, sizeof(VirtIOBalloon));
  179 + if (s == NULL)
  180 + return NULL;
  181 +
  182 + s->vdev.get_config = virtio_balloon_get_config;
  183 + s->vdev.set_config = virtio_balloon_set_config;
  184 + s->vdev.get_features = virtio_balloon_get_features;
  185 +
  186 + s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
  187 + s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
  188 +
  189 + qemu_add_balloon_handler(virtio_balloon_to_target, s);
  190 +
  191 + register_savevm("virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s);
  192 +
  193 + return &s->vdev;
  194 +}
... ...
hw/virtio-balloon.h 0 → 100644
  1 +/*
  2 + * Virtio Support
  3 + *
  4 + * Copyright IBM, Corp. 2007-2008
  5 + *
  6 + * Authors:
  7 + * Anthony Liguori <aliguori@us.ibm.com>
  8 + * Rusty Russell <rusty@rustcorp.com.au>
  9 + *
  10 + * This work is licensed under the terms of the GNU GPL, version 2. See
  11 + * the COPYING file in the top-level directory.
  12 + *
  13 + */
  14 +
  15 +#ifndef _QEMU_VIRTIO_BALLOON_H
  16 +#define _QEMU_VIRTIO_BALLOON_H
  17 +
  18 +#include "virtio.h"
  19 +#include "pci.h"
  20 +
  21 +/* from Linux's linux/virtio_balloon.h */
  22 +
  23 +/* The ID for virtio_balloon */
  24 +#define VIRTIO_ID_BALLOON 5
  25 +
  26 +/* The feature bitmap for virtio balloon */
  27 +#define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */
  28 +
  29 +/* Size of a PFN in the balloon interface. */
  30 +#define VIRTIO_BALLOON_PFN_SHIFT 12
  31 +
  32 +struct virtio_balloon_config
  33 +{
  34 + /* Number of pages host wants Guest to give up. */
  35 + uint32_t num_pages;
  36 + /* Number of pages we've actually got in balloon. */
  37 + uint32_t actual;
  38 +};
  39 +
  40 +void *virtio_balloon_init(PCIBus *bus);
  41 +
  42 +#endif
... ...
kvm-all.c
... ... @@ -549,3 +549,15 @@ int kvm_vcpu_ioctl(CPUState *env, int type, ...)
549 549  
550 550 return ret;
551 551 }
  552 +
  553 +int kvm_has_sync_mmu(void)
  554 +{
  555 + KVMState *s = kvm_state;
  556 +
  557 +#ifdef KVM_CAP_SYNC_MMU
  558 + if (kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_SYNC_MMU) > 0)
  559 + return 1;
  560 +#endif
  561 +
  562 + return 0;
  563 +}
... ...
... ... @@ -42,6 +42,9 @@ void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr, target_phys_a
42 42  
43 43 int kvm_log_start(target_phys_addr_t phys_addr, target_phys_addr_t len);
44 44 int kvm_log_stop(target_phys_addr_t phys_addr, target_phys_addr_t len);
  45 +
  46 +int kvm_has_sync_mmu(void);
  47 +
45 48 /* internal API */
46 49  
47 50 struct KVMState;
... ...
monitor.c
... ... @@ -1402,7 +1402,9 @@ static void do_info_balloon(void)
1402 1402 ram_addr_t actual;
1403 1403  
1404 1404 actual = qemu_balloon_status();
1405   - if (actual == 0)
  1405 + if (kvm_enabled() && !kvm_has_sync_mmu())
  1406 + term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
  1407 + else if (actual == 0)
1406 1408 term_printf("Ballooning not activated in VM\n");
1407 1409 else
1408 1410 term_printf("balloon: actual=%d\n", (int)(actual >> 20));
... ...