Bug 277473 - Virtio memory ballooning does not return unused memory to host
Summary: Virtio memory ballooning does not return unused memory to host
Status: Closed Works As Intended
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 14.0-RELEASE
Hardware: amd64 Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-03-04 12:07 UTC by Laurent Chardon
Modified: 2024-05-13 13:25 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Laurent Chardon 2024-03-04 12:07:18 UTC
The memory ballooning in a FreeBSD kvm/qemu guest does not seem to return the unused memory to the host. 
The ballooning seems to happen correctly within FreeBSD, but the host might not be told properly (that's only my limited understanding as a non-expert).
This is true whether setting the current memory size automatically of manually.
Here are some scenarios to explain the problem.

=== Scenario 1

With currentMemory and memory both set to 64GB in the kvm config file

$ vmstat
 procs    memory    page                      disks     faults       cpu
 r  b  w  avm  fre  flt  re  pi  po   fr   sr nd0 pa0   in   sy   cs us sy id
 0  0  0 429M  62G 2.3K   0  17   0 3.4K    4   0   0   76 2.2K 1.3K  0  1 99

neofetch shows:
Memory: 2272MiB / 65495MiB

On the Linux host, top shows:
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  57685 libvirt+  20   0   66.8g  64.2g  26112 S   0.0  51.1   1:01.37 qemu-system-x86

This is all as expected 

=== Scenario 2

With currentMemory set to 8G and memory set to 64GB in the kvm config file

$ vmstat
 procs    memory    page                      disks     faults       cpu
 r  b  w  avm  fre  flt  re  pi  po   fr   sr nd0 pa0   in   sy   cs us sy id
 0  0  0 429M 5.7G 3.1K   0  23   0 4.6K    4   0   0 1381 3.0K 3.5K  0  4 96

and now neofetch shows:
Memory: 59607MiB / 65495MiB

On the Linux host from the 'top' command:

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  57062 libvirt+  20   0   66.8g  64.2g  26112 S   0.0  51.0   1:32.51 qemu-system-x86

vmstat shows less available memory than in Scenario 1, as expected.
The host however sees no difference in memory usage from FreeBSD compared to Scenario 1. Expected: the memory usage on the host should have decreased. 
Interestingly, neofetch sees much more memory usage than previously. I don't know if it's relevant, but it might be a clue so I'm including this here.

=== Scenario 3

With currentMemory and memory both set to 64GB in kvm config file, as in Scenario 1. I type 'sudo virsh setmem FreeBSD 8G' on the host. FreeBSD's vmstat initially shows 64G free, and transitions gradually to 5.7G free, as expected. 
On the host side however, there is no change in the memory utilization of the FreeBSD VM. Expected: the memory usage on the host should have decreased. 

=== Test notes

The tests are done on a physical server with 32 cores (2 sockets), 64 threads, 128GB RAM. 
The OS is Ubuntu 22.04
Other guests such as Windows and Linux behave as expected in kvm/qemu. The memory usage inside the guest matches the memory usage seen from the host.
virtio_balloon.ko is compiled in kernel.
Comment 1 Laurent Chardon 2024-03-04 12:11:41 UTC
Adding bryanv@FreeBSD.org because his email in in VIRTIO_BALLOON(4). I hope that's OK.
Comment 2 Laurent Chardon 2024-03-04 13:02:17 UTC
Possibly related to, but not duplicate of #198344. Regression?
Comment 3 Laurent Chardon 2024-05-13 13:25:59 UTC
The memory ballooning works fine if I'm not using a PCI block device passthrough. I suspect that the problem might be with libvirt rather than freebsd. My workaround is to use a regular block device in my qemu config (no host PCI passthrough).