[PATCH] virtio_balloon: implement STATS_VQ, DEFLATE_ON_OOM, and fix OOM re-inflation

From: xiangzhe <xiangzhedev_at_gmail.com>
Date: Thu, 09 Apr 2026 05:26:26 UTC
Hi,
I've submitted a patch series to enhance the FreeBSD virtio_balloon 
driver with features that have been in Linux for years but are missing 
in FreeBSD. The patches are available for review as a GitHub PR:
https://github.com/freebsd/freebsd-src/pull/2116
The series contains 5 commits:
   1. virtio_balloon: implement VIRTIO_BALLOON_F_STATS_VQ support
      Add memory statistics reporting via the stats virtqueue, enabling 
hypervisors (QEMU/Proxmox/libvirt) to monitor guest memory and make 
informed ballooning decisions.
   2. virtio_balloon: implement VIRTIO_BALLOON_F_DEFLATE_ON_OOM support
      Register a vm_lowmem event handler so the balloon automatically 
deflates under memory pressure before the OOM killer is invoked.
   3. virtio_balloon: fix OOM deflation immediately re-inflated by 
balloon thread
      After OOM deflation, the balloon thread would immediately 
re-inflate because it re-read the unchanged host target. Add a WANTSIZE 
flag so inflate/deflate only runs on explicit host config change interrupts.
   4. virtio_balloon: replace unconditional OOM log with proper 
observability
      Replace noisy per-event device_printf with sysctl counters and a 
per-instance debug toggle.
   5. virtio_balloon: improve S_AVAIL to match virtio spec available 
memory semantics
      Replace (free + inactive) with a FreeBSD analogue of Linux's 
si_mem_available() for more accurate reporting.
Motivation:
Without STATS_VQ, hypervisors have no visibility into guest memory usage 
and cannot make informed ballooning decisions. Without DEFLATE_ON_OOM, 
guests under memory pressure have no way to automatically reclaim 
balloon pages before processes are killed. These are standard virtio 
features supported by Linux and NetBSD guests, and their absence in 
FreeBSD has been reported multiple times.
Related Bugzilla PR:
   - PR 292570: [virtio] virtio-balloon does not balloon correctly 
<https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=292570>
Tested on FreeBSD 15-CURRENT and OPNsense (backport to FreeBSD 14 
kernel) under QEMU/KVM with libvirt, confirming stats reporting works in 
Proxmox and OOM deflation prevents unnecessary process kills.
Review and feedback welcome.
Thanks,
Xiangzhe