Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions alioth/src/virtio/dev/balloon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
// limitations under the License.

use std::fmt::Debug;
use std::io::{IoSlice, IoSliceMut};
use std::io::{Error, ErrorKind, IoSlice, IoSliceMut};
use std::sync::Arc;
use std::thread::JoinHandle;

use alioth_macros::Layout;
use libc::{_SC_PAGESIZE, sysconf};
use libc::{_SC_PAGESIZE, MADV_DONTNEED};
use mio::Registry;
use mio::event::Event;
use parking_lot::RwLock;
Expand Down Expand Up @@ -68,9 +68,19 @@ impl Mmio for BalloonConfigMmio {
BalloonConfig::LAYOUT_ACTUAL => {
config.actual = val as u32;
log::info!(
"{}: update: num_pages = {:#x}, actual = {val:#x}",
"{}: update: num_pages = {:#x}, actual = {:#x}",
self.name,
config.num_pages,
config.actual,
);
Ok(Action::None)
}
BalloonConfig::LAYOUT_POISON_VAL => {
config.poison_val = val as u32;
log::info!(
"{}: update: poison_val = {:#x}",
self.name,
config.poison_val,
);
Ok(Action::None)
}
Expand Down Expand Up @@ -105,7 +115,7 @@ consts! {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BalloonQueue {
Inflate,
Deflate,
Expand All @@ -120,33 +130,37 @@ pub struct Balloon {
name: Arc<str>,
config: Arc<BalloonConfigMmio>,
feature: BalloonFeature,
driver_feature: BalloonFeature,
queues: [BalloonQueue; 5],
}

impl Balloon {
pub fn new(spec: BalloonSpec, name: impl Into<Arc<str>>) -> Result<Self> {
if unsafe { sysconf(_SC_PAGESIZE) } != 1 << 12 {
let err = std::io::ErrorKind::Unsupported;
Err(std::io::Error::from(err))?;
}
fn new_inner(spec: BalloonSpec, name: Arc<str>) -> Self {
let config = BalloonConfig {
num_pages: 0,
..Default::default()
};
let mut feature = BalloonFeature::all();
if !spec.free_page_reporting {
feature.remove(BalloonFeature::PAGE_REPORTING);
};
let name = name.into();
Ok(Balloon {
}
Balloon {
name: name.clone(),
config: Arc::new(BalloonConfigMmio {
config: RwLock::new(config),
name,
}),
feature,
driver_feature: BalloonFeature::empty(),
queues: [BalloonQueue::NotExist; 5],
})
}
}

pub fn new(spec: BalloonSpec, name: impl Into<Arc<str>>) -> Result<Self> {
if unsafe { libc::sysconf(_SC_PAGESIZE) } != 1 << 12 {
Err(Error::from(ErrorKind::Unsupported))?;
}
Ok(Self::new_inner(spec, name.into()))
}

fn inflate(&self, desc: &[IoSlice], ram: &Ram) {
Expand All @@ -160,7 +174,7 @@ impl Balloon {
continue;
};
let gpa = (page_num as u64) << 12;
if let Err(e) = ram.madvise(gpa, 1 << 12, libc::MADV_DONTNEED) {
if let Err(e) = ram.madvise(gpa, 1 << 12, MADV_DONTNEED) {
log::error!("{}: inflate at GPA {gpa:#x}: {e:?}", self.name);
} else {
log::trace!("{}: freed GPA {gpa:#x}", self.name);
Expand All @@ -169,15 +183,23 @@ impl Balloon {
}
}

fn should_reclaim_reported_pages(&self) -> bool {
!self.driver_feature.contains(BalloonFeature::PAGE_POISON)
|| self.config.config.read().poison_val == 0
}

fn free_reporting(&self, desc: &mut [IoSliceMut]) {
if !self.should_reclaim_reported_pages() {
return;
}
for buf in desc.iter_mut() {
let addr = buf.as_mut_ptr();
let len = buf.len();
let ret = ffi!(unsafe { libc::madvise(addr as _, len, libc::MADV_DONTNEED) });
let ret = ffi!(unsafe { libc::madvise(addr as _, len, MADV_DONTNEED) });
if let Err(e) = ret {
log::error!("freeing pages: {addr:p} {len:#x}: {e:?}");
log::error!("{}: freeing pages: {addr:p} {len:#x}: {e:?}", self.name);
} else {
log::trace!("freed pages: {addr:p} {len:#x}");
log::trace!("{}: freed pages: {addr:p} {len:#x}", self.name);
}
}
}
Expand Down Expand Up @@ -222,6 +244,8 @@ impl VirtioMio for Balloon {
S: IrqSender,
{
let feature = BalloonFeature::from_bits_retain(feature);
self.driver_feature = feature;
self.queues = [BalloonQueue::NotExist; 5];
self.queues[0] = BalloonQueue::Inflate;
self.queues[1] = BalloonQueue::Deflate;
let mut index = 2;
Expand Down Expand Up @@ -291,6 +315,10 @@ impl VirtioMio for Balloon {

fn reset(&mut self, _registry: &Registry) {
self.queues = [BalloonQueue::NotExist; 5];
self.driver_feature = BalloonFeature::empty();
let mut config = self.config.config.write();
config.actual = 0;
config.poison_val = 0;
}
}

Expand All @@ -308,3 +336,7 @@ impl DevSpec for BalloonSpec {
Balloon::new(self, name)
}
}

#[cfg(test)]
#[path = "balloon_test.rs"]
mod tests;
Loading
Loading