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

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,

View File

@ -745,6 +745,10 @@ components:
size:
type: integer
format: int64
deflate_on_oom:
type: boolean
default: false
description: Whether the balloon should deflate when the guest is under memory pressure.
FsConfig:
required:

View File

@ -1201,14 +1201,19 @@ impl Default for RngConfig {
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct BalloonConfig {
pub size: u64,
/// Option to deflate the balloon in case the guest is out of memory.
#[serde(default)]
pub deflate_on_oom: bool,
}
impl BalloonConfig {
pub const SYNTAX: &'static str = "Balloon parameters \"size=<balloon_size>\"";
pub const SYNTAX: &'static str =
"Balloon parameters \"size=<balloon_size>,deflate_on_oom=on|off\"";
pub fn parse(balloon: &str) -> Result<Self> {
let mut parser = OptionParser::new();
parser.add("size");
parser.add("deflate_on_oom");
parser.parse(balloon).map_err(Error::ParseBalloon)?;
let size = parser
@ -1217,7 +1222,16 @@ impl BalloonConfig {
.map(|v| v.0)
.unwrap_or(0);
Ok(BalloonConfig { size })
let deflate_on_oom = parser
.convert::<Toggle>("deflate_on_oom")
.map_err(Error::ParseBalloon)?
.unwrap_or(Toggle(false))
.0;
Ok(BalloonConfig {
size,
deflate_on_oom,
})
}
}

View File

@ -2610,6 +2610,7 @@ impl DeviceManager {
virtio_devices::Balloon::new(
id.clone(),
balloon_config.size,
balloon_config.deflate_on_oom,
self.seccomp_action.clone(),
)
.map_err(DeviceManagerError::CreateVirtioBalloon)?,