linux-kerneloperating-systemkvmballoonvirtio

KVM Virtio-balloon: how to activate free page reporting feature on the device side?


I'm trying to get free page reporting running on my KVM guest (Ubuntu 22.04 server with custom kernel from upstream Linux 6.5). My host is Ubuntu 20.04 with 6.5 Linux. Free page reporting is listed as a feature for my balloon driver:

static unsigned int features[] = {
     VIRTIO_BALLOON_F_MUST_TELL_HOST,
     VIRTIO_BALLOON_F_STATS_VQ,
     VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
     VIRTIO_BALLOON_F_FREE_PAGE_HINT,
     VIRTIO_BALLOON_F_PAGE_POISON,
     VIRTIO_BALLOON_F_REPORTING,
}

static struct virtio_driver virtio_balloon_driver = {
    .feature_table = features,
    ...
}

However, when I start my guest, free page reporting does not work, despite the normal balloon inflate/deflate operations working correctly. It seems that the underlying balloon "device" does not have the feature based on this conditional returning false:

if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))

I have CONFIG_PAGE_REPORTING and CONFIG_VIRTIO_BALLOON set to y in both host and guest kernel configs.

My guest KVM config file shows this (auto-generated) device:

<memballoon model="virtio">
  <address type="pci" domain="0x0000" bus="0x00" slot="0x07" function="0x0"/>
</memballoon>

How do I turn on this feature for the virtio-balloon device to match the driver feature? Is this something that needs to be configured in the host, either by modifying my host kernel or changing my guest KVM config file? Or is this something that I need to modify in my guest-kernel to get working?


Solution

  • Is this something that needs to be configured in the host, either by modifying my host kernel or changing my guest KVM config file? Or is this something that I need to modify in my guest-kernel to get working?

    The answer is none of the above. In KVM, the emulator is responsible for creating virtio devices. Typically, this is Qemu's job. You can verify what emulator you're using by looking for the emulator line in the KVM guest config file (in my case by doing virsh edit <guestname>):

    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    

    If you look in the Qemu github repo (as of version 8.1.2), you'll find hw/virtio/virtio-balloon.c which contains the code you're looking for:

    static Property virtio_balloon_properties[] = {
        ...
        DEFINE_PROP_BIT("free-page-reporting", VirtIOBalloon, host_features,
                        VIRTIO_BALLOON_F_REPORTING, false),
        ...
        DEFINE_PROP_END_OF_LIST(),
    };
    

    By default, free page reporting is turned off, so you'd need to turn this feature on by changing false to true, compiling Qemu for you machine, and then using this qemu binary to run your KVM guest.