From 669d9a8ae8e5f8f5418f80394c2907be5c089c7f Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Thu, 28 Nov 2019 12:56:35 +0100 Subject: [PATCH] vhost_user_backend: fix memory region offsetting The way in which offsets are currently use in memory regions is derived from QEMU's contrib/libvhost-user, but while this one works mainly by translating vmm va's to local va's, vm-memory expects us to use proper guest addresses and thus, define memory regions that actually match the guest's memory disposition. With this change, we create the memory regions with the proper length and offsets, extend AddrMapping to store the guest physical address, and use the latter instead of offset in vmm_va_to_gpa(). Signed-off-by: Sergio Lopez --- vhost_user_backend/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vhost_user_backend/src/lib.rs b/vhost_user_backend/src/lib.rs index 8a92435e2..e5db87ad4 100644 --- a/vhost_user_backend/src/lib.rs +++ b/vhost_user_backend/src/lib.rs @@ -169,7 +169,7 @@ impl VhostUserDaemon { struct AddrMapping { vmm_addr: u64, size: u64, - offset: u64, + gpa_base: u64, } struct Memory { @@ -446,7 +446,7 @@ impl VhostUserHandler { if let Some(memory) = &self.memory { for mapping in memory.mappings.iter() { if vmm_va >= mapping.vmm_addr && vmm_va < mapping.vmm_addr + mapping.size { - return Ok(vmm_va - mapping.vmm_addr + mapping.offset); + return Ok(vmm_va - mapping.vmm_addr + mapping.gpa_base); } } } @@ -524,15 +524,15 @@ impl VhostUserSlaveReqHandler for VhostUserHandler { for (idx, region) in ctx.iter().enumerate() { let g_addr = GuestAddress(region.guest_phys_addr); - let len = (region.memory_size + region.mmap_offset) as usize; + let len = region.memory_size as usize; let file = unsafe { File::from_raw_fd(fds[idx]) }; - let f_off = FileOffset::new(file, 0); + let f_off = FileOffset::new(file, region.mmap_offset); regions.push((g_addr, len, Some(f_off))); mappings.push(AddrMapping { vmm_addr: region.user_addr, size: region.memory_size, - offset: region.mmap_offset, + gpa_base: region.guest_phys_addr, }); }