Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
dbs-virtio-devices: fix balloon pfn using u32
Browse files Browse the repository at this point in the history
We fixes a problem that offset should be parsed as u32 instead of
u64 in virtio-balloon.

Fixes: #284

Signed-off-by: Helin Guo <helinguo@linux.alibaba.com>
  • Loading branch information
HerlinCoder authored and studychao committed May 30, 2023
1 parent 21838b7 commit 3fbd6ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/dbs-virtio-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kvm-bindings = "0.6.0"
kvm-ioctls = "0.12.0"
libc = "0.2.119"
log = "0.4.14"
nix = "0.23.1"
nix = "0.24.3"
nydus-api = "0.2.0"
nydus-blobfs = "0.2.0"
nydus-rafs = "0.2.0"
Expand Down
57 changes: 34 additions & 23 deletions crates/dbs-virtio-devices/src/balloon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ const PMD_SIZE: u64 = 1 << PMD_SHIFT;
const INFLATE_QUEUE_AVAIL_EVENT: u32 = 0;
// New descriptors are pending on the virtio queue.
const DEFLATE_QUEUE_AVAIL_EVENT: u32 = 1;
//// New descriptors are pending on the virtio queue.
const REPORTING_QUEUE_AVAIL_EVENT: u32 = 2;
// New descriptors are pending on the virtio queue.
const INFLATE_CONT_QUEUE_AVAIL_EVENT: u32 = 3;
// New descriptors are pending on the virtio queue.
const DEFLATE_CONT_QUEUE_AVAIL_EVENT: u32 = 4;
const REPORTING_QUEUE_AVAIL_EVENT: u32 = 2;
// The device has been dropped.
const KILL_EVENT: u32 = 5;
const KILL_EVENT: u32 = 3;
// The device should be paused.
const PAUSE_EVENT: u32 = 6;
const BALLOON_EVENTS_COUNT: u32 = 7;
const PAUSE_EVENT: u32 = 4;
const BALLOON_EVENTS_COUNT: u32 = 5;

// Page shift in the host.
const PAGE_SHIFT: u32 = 12;
Expand Down Expand Up @@ -299,7 +295,7 @@ impl<AS: DbsGuestAddressSpace, Q: QueueT + Send, R: GuestMemoryRegion>
continue;
}
let avail_desc_len = avail_desc.len();
if avail_desc_len as usize % size_of::<u64>() != 0 {
if avail_desc_len as usize % size_of::<u32>() != 0 {
error!(
"{}: the request size {} is not right",
BALLOON_DRIVER_NAME, avail_desc_len
Expand All @@ -322,7 +318,7 @@ impl<AS: DbsGuestAddressSpace, Q: QueueT + Send, R: GuestMemoryRegion>
break;
}
};
offset += size_of::<u64>() as u64;
offset += size_of::<u32>() as u64;

// Get pfn_len
let pfn_len = match idx {
Expand All @@ -343,12 +339,10 @@ impl<AS: DbsGuestAddressSpace, Q: QueueT + Send, R: GuestMemoryRegion>
pfn_len
);

let guest_addr = pfn << VIRTIO_BALLOON_PFN_SHIFT;
let guest_addr = (pfn as u64) << VIRTIO_BALLOON_PFN_SHIFT;

if let Some(region) = mem.find_region(GuestAddress(guest_addr.into())) {
let host_addr = mem
.get_host_address(GuestAddress(guest_addr.into()))
.unwrap();
if let Some(region) = mem.find_region(GuestAddress(guest_addr)) {
let host_addr = mem.get_host_address(GuestAddress(guest_addr)).unwrap();
if advice == libc::MADV_DONTNEED && region.file_offset().is_some() {
advice = libc::MADV_REMOVE;
}
Expand Down Expand Up @@ -428,6 +422,11 @@ where
AS: 'static + GuestAddressSpace + Send + Sync,
{
fn init(&mut self, ops: &mut EventOps) {
trace!(
target: BALLOON_DRIVER_NAME,
"{}: BalloonEpollHandler::init()",
BALLOON_DRIVER_NAME,
);
let events = Events::with_data(
self.inflate.eventfd.as_ref(),
INFLATE_QUEUE_AVAIL_EVENT,
Expand Down Expand Up @@ -472,8 +471,12 @@ where
let _mem = guard.deref();
let idx = events.data();

trace!("{}: process idx {}", BALLOON_DRIVER_NAME, idx);

trace!(
target: BALLOON_DRIVER_NAME,
"{}: BalloonEpollHandler::process() idx {}",
BALLOON_DRIVER_NAME,
idx
);
match idx {
INFLATE_QUEUE_AVAIL_EVENT | DEFLATE_QUEUE_AVAIL_EVENT => {
if !self.process_queue(idx) {
Expand Down Expand Up @@ -588,10 +591,24 @@ where
}

fn set_acked_features(&mut self, page: u32, value: u32) {
trace!(
target: BALLOON_DRIVER_NAME,
"{}: VirtioDevice::set_acked_features({}, 0x{:x})",
BALLOON_DRIVER_NAME,
page,
value
);
self.device_info.set_acked_features(page, value)
}

fn read_config(&mut self, offset: u64, mut data: &mut [u8]) {
trace!(
target: BALLOON_DRIVER_NAME,
"{}: VirtioDevice::read_config(0x{:x}, {:?})",
BALLOON_DRIVER_NAME,
offset,
data
);
let config = &self.config.lock().unwrap();
let config_space = config.as_slice().to_vec();
let config_len = config_space.len() as u64;
Expand Down Expand Up @@ -626,12 +643,6 @@ where
self.device_info.check_queue_sizes(&config.queues)?;
self.device_change_notifier = config.device_change_notifier.clone();

let _queue_evts: Vec<RawFd> = config
.queues
.iter()
.map(|q| q.eventfd.as_raw_fd())
.collect();

trace!(
"{}: activate acked_features 0x{:x}",
BALLOON_DRIVER_NAME,
Expand Down

0 comments on commit 3fbd6ff

Please sign in to comment.