Skip to content

Commit

Permalink
virtio-devices, vmm: Deprecate "GuestMemory::with_regions(_mut)"
Browse files Browse the repository at this point in the history
Function "GuestMemory::with_regions(_mut)" were mainly temporary methods
to access the regions in `GuestMemory` as the lack of iterator-based
access, and hence they are deprecated in the upstream vm-memory crate [1].

[1] rust-vmm/vm-memory#133

Signed-off-by: Bo Chen <chen.bo@intel.com>
  • Loading branch information
likebreath committed May 27, 2021
1 parent aba10fb commit fdcd1e8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
9 changes: 3 additions & 6 deletions virtio-devices/src/vhost_user/vu_common_ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub struct VhostUserConfig {

pub fn update_mem_table(vu: &mut Master, mem: &GuestMemoryMmap) -> Result<()> {
let mut regions: Vec<VhostUserMemoryRegionInfo> = Vec::new();
mem.with_regions_mut(|_, region| {
for region in mem.iter() {
let (mmap_handle, mmap_offset) = match region.file_offset() {
Some(_file_offset) => (_file_offset.file().as_raw_fd(), _file_offset.start()),
None => return Err(MmapError::NoMemoryRegion),
None => return Err(Error::VhostUserMemoryRegion(MmapError::NoMemoryRegion)),
};

let vhost_user_net_reg = VhostUserMemoryRegionInfo {
Expand All @@ -39,10 +39,7 @@ pub fn update_mem_table(vu: &mut Master, mem: &GuestMemoryMmap) -> Result<()> {
};

regions.push(vhost_user_net_reg);

Ok(())
})
.map_err(Error::VhostUserMemoryRegion)?;
}

vu.set_mem_table(regions.as_slice())
.map_err(Error::VhostUserSetMemTable)?;
Expand Down
12 changes: 4 additions & 8 deletions vmm/src/memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ impl MemoryManager {
log_dirty,
}));

guest_memory.memory().with_regions(|_, region| {
for region in guest_memory.memory().iter() {
let mut mm = memory_manager.lock().unwrap();
let slot = mm.create_userspace_mapping(
region.start_addr().raw_value(),
Expand All @@ -788,9 +788,7 @@ impl MemoryManager {
size: region.len(),
slot,
});

Ok(())
})?;
}

for region in virtio_mem_regions.drain(..) {
let mut mm = memory_manager.lock().unwrap();
Expand Down Expand Up @@ -1942,7 +1940,7 @@ impl Snapshottable for MemoryManager {

let mut memory_regions: Vec<MemoryRegion> = Vec::new();

guest_memory.with_regions_mut(|index, region| {
for (index, region) in guest_memory.iter().enumerate() {
if region.len() == 0 {
return Err(MigratableError::Snapshot(anyhow!("Zero length region")));
}
Expand Down Expand Up @@ -1970,9 +1968,7 @@ impl Snapshottable for MemoryManager {
start_addr: region.start_addr().0,
size: region.len(),
});

Ok(())
})?;
}

// Store locally this list of regions as it will be used through the
// Transportable::send() implementation. The point is to avoid the
Expand Down
11 changes: 5 additions & 6 deletions vmm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1989,13 +1989,12 @@ impl Vm {
let mut table = MemoryRangeTable::default();
let guest_memory = self.memory_manager.lock().as_ref().unwrap().guest_memory();

guest_memory.memory().with_regions_mut(|_, region| {
for region in guest_memory.memory().iter() {
table.push(MemoryRange {
gpa: region.start_addr().raw_value(),
length: region.len() as u64,
});
Ok(())
})?;
}

Ok(table)
}
Expand Down Expand Up @@ -2490,7 +2489,7 @@ pub fn test_vm() {
let hv = hypervisor::new().unwrap();
let vm = hv.create_vm().expect("new VM creation failed");

mem.with_regions(|index, region| {
for (index, region) in mem.iter().enumerate() {
let mem_region = vm.make_user_memory_region(
index as u32,
region.start_addr().raw_value(),
Expand All @@ -2501,8 +2500,8 @@ pub fn test_vm() {
);

vm.set_user_memory_region(mem_region)
})
.expect("Cannot configure guest memory");
.expect("Cannot configure guest memory");
}
mem.write_slice(&code, load_addr)
.expect("Writing code to memory failed");

Expand Down

0 comments on commit fdcd1e8

Please sign in to comment.