virtio-balloon: add deflate_on_oom support

Sometimes we need balloon deflate automatically to give memory
back to guest, especially for some low priority guest processes
under memory pressure. Enable deflate_on_oom to support this.

Usage: --balloon "size=0,deflate_on_oom=on" \

Signed-off-by: Fei Li <lifei.shirley@bytedance.com>
This commit is contained in:
Fei Li
2021-01-21 19:22:29 +08:00
committed by Sebastien Boeuf
parent a6fe4aa7e9
commit aa27f0e743
4 changed files with 34 additions and 4 deletions
+13 -2
View File
@@ -51,6 +51,9 @@ const DEFLATE_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3;
// Size of a PFN in the balloon interface.
const VIRTIO_BALLOON_PFN_SHIFT: u64 = 12;
// Deflate balloon on OOM
const VIRTIO_BALLOON_F_DEFLATE_ON_OOM: u64 = 2;
#[derive(Debug)]
pub enum Error {
// Guest gave us bad memory addresses.
@@ -319,8 +322,16 @@ pub struct Balloon {
impl Balloon {
// Create a new virtio-balloon.
pub fn new(id: String, size: u64, seccomp_action: SeccompAction) -> io::Result<Self> {
let avail_features = 1u64 << VIRTIO_F_VERSION_1;
pub fn new(
id: String,
size: u64,
deflate_on_oom: bool,
seccomp_action: SeccompAction,
) -> io::Result<Self> {
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
if deflate_on_oom {
avail_features |= 1u64 << VIRTIO_BALLOON_F_DEFLATE_ON_OOM;
}
let config = VirtioBalloonConfig {
num_pages: (size >> VIRTIO_BALLOON_PFN_SHIFT) as u32,