diff --git a/nova/tests/fixtures/libvirt_data.py b/nova/tests/fixtures/libvirt_data.py
index cfc9a1412c69..99d361367396 100644
--- a/nova/tests/fixtures/libvirt_data.py
+++ b/nova/tests/fixtures/libvirt_data.py
@@ -229,7 +229,7 @@ FAKE_KVM_GUEST = """
-
+
diff --git a/nova/tests/unit/virt/libvirt/test_config.py b/nova/tests/unit/virt/libvirt/test_config.py
index 199d5807c76c..1d772f543294 100644
--- a/nova/tests/unit/virt/libvirt/test_config.py
+++ b/nova/tests/unit/virt/libvirt/test_config.py
@@ -4247,7 +4247,7 @@ class LibvirtConfigMemoryBalloonTest(LibvirtConfigBaseTest):
xml = balloon.to_xml()
expected_xml = """
-
+ """
@@ -4259,7 +4259,8 @@ class LibvirtConfigMemoryBalloonTest(LibvirtConfigBaseTest):
xml = balloon.to_xml()
expected_xml = """
- """
+ """
self.assertXmlEqual(expected_xml, xml)
@@ -4272,7 +4273,7 @@ class LibvirtConfigMemoryBalloonTest(LibvirtConfigBaseTest):
xml = balloon.to_xml()
expected_xml = """
-
+ """
diff --git a/nova/virt/libvirt/config.py b/nova/virt/libvirt/config.py
index 2c6e859f9990..f3253c734f3c 100644
--- a/nova/virt/libvirt/config.py
+++ b/nova/virt/libvirt/config.py
@@ -2302,6 +2302,8 @@ class LibvirtConfigMemoryBalloon(LibvirtConfigGuestDevice):
def format_dom(self):
dev = super(LibvirtConfigMemoryBalloon, self).format_dom()
dev.set('model', str(self.model))
+ dev.set('autodeflate', 'on')
+ dev.set('freePageReporting', 'on')
if self.period is not None:
dev.append(etree.Element('stats', period=str(self.period)))
if self.driver_iommu:
diff --git a/releasenotes/notes/libvirt-memballoon-autodeflate-freepage-reporting-13c25d410cca9d23.yaml b/releasenotes/notes/libvirt-memballoon-autodeflate-freepage-reporting-13c25d410cca9d23.yaml
new file mode 100644
index 000000000000..6988ac2c5abb
--- /dev/null
+++ b/releasenotes/notes/libvirt-memballoon-autodeflate-freepage-reporting-13c25d410cca9d23.yaml
@@ -0,0 +1,11 @@
+---
+features:
+ - |
+ The libvirt driver now automatically enables ``autodeflate`` and
+ ``freePageReporting`` features for virtio memory balloon devices.
+ The ``autodeflate`` feature allows the QEMU virtio memory balloon
+ to release memory at the last moment before a guest process is
+ killed by the Out of Memory killer. The ``freePageReporting``
+ feature enables the memory balloon to return unused pages back
+ to the hypervisor for use by other guests or processes, improving
+ overall memory efficiency on the compute host.