Virtio balloon

After someone asked me a question about “balloons” (in the virtualization sense) today, I noticed that there is not very much documentation around. This post explains what the KVM virtio_balloon driver is all about.

First of all, what is a balloon driver if you’ve never even heard of the concept? It’s a way to give or take RAM from a guest. (In theory at least), if your guest needs more RAM, you can use the balloon driver to give it more RAM. Or if the host needs to take RAM away from guests, it can do so. All of this is done without needing to pause or reboot the guest.

You might think that this would work as a RAM “hot add” feature, rather like hot adding disks to a guest. Although RAM hot add would (IMHO) be much better, currently this is not how ballooning works.

What we have is a kernel driver inside the guest called virtio_balloon. This driver acts like a kind of weird process, either expanding its own memory usage or shrinking down to nearly nothing, as in the diagrams below:

When the balloon driver expands, normal applications running in the guest suddenly have a lot less memory and the guest does the usual things it does when there’s not much memory, including swapping stuff out and starting up the OOM killer. (The balloon itself is non-swappable and un-killable in case you were wondering).

So what’s the point of a kernel driver which wastes memory? There are two points: Firstly, the driver communicates with the host (over the virtio channel), and the host gives it instructions (“expand to this size”, “shrink down now”). The guest cooperates, but doesn’t directly control the balloon.

Secondly, memory pages in the balloon are unmapped from the guest and handed back to the host, so the host can hand them out to other guests. It’s like the guest’s memory has a chunk missing from it:

Libvirt has two settings you can control called currentMemory and maxMemory (“memory” in the libvirt XML):

maxMemory (or just <memory>) is the memory allocated at boot time to a guest. KVM and Xen guests currently cannot exceed this. currentMemory controls what memory you’re requesting to give to the guest’s applications. The balloon fills the rest of the memory and gives it back to the host for the host to use elsewhere.

You can adjust this manually for your guests, either by editing the XML, or by using the virsh setmem command.

12 Comments

Filed under Uncategorized

12 responses to “Virtio balloon

  1. Gianluca

    Thanks for the explanation.
    Some questions:
    in currentMemory definition: “If this is omitted, it defaults to the same value as the memory element”

    Does this mean that by default (memory=currentMemory) there is no balloon at all for a VM?
    Does this mean that if I want for example to “reserve” (or grant) at least 512MB but not all of my 2Gb ram defined VM for its applications, I can set currentMemory to 512Mb and so give more memory available for other VMs (1,5Gb in this case)?
    Is the currentMemory parameter dynamically adjustable?
    Thanks,
    Gianluca

  2. Ethan Waldo

    The memory ballooning driver for Windows 7/2008 may not work because Windows automatically recognizes it as “PCI standard RAM Controller”. You can look at the properties of this system device and under details make sure that “Location information” shows the same PCI device that kvm/qemu set for the memory ballooning driver (in my case, it is PCI bus 0, device 5, function 0).

    Be sure to go back to the Driver tab and update your driver to the actual balloon driver.

  3. Kevin Chin

    Thanks for a great explanation, Richard.

  4. Pravin

    Thank ya Richard! Excellent explanation.

  5. Pingback: Virtio Balloon in action — with native linux kvm tool | Kashyap Chamarthy

  6. Pankaj

    Hi can you please tell me the correct procedure to use a virtioballoon
    I in Linux . Is there any command that need to be executed between guest or host in order for it to work .

  7. Persson

    Does this need something special running *on the host* to function?

  8. Pingback: KVM/Xen and libvirt: currentMemory, memory and ballooning. Where did my memory go? | golb

  9. Pingback: Windows 7 in QEMU/KVM « Just moozing

  10. Pingback: Adeus chroot… seja bem vindo LXC… | Vinipsmaker labs

  11. Pingback: Wywiad z Pawłem Dziepakiem – deweloperem Haiku | OSWorld.pl

  12. Pingback: Drivers VirtIO para guests Windows en KVM | Pollo sin Cabeza

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s