# Safety `userspace_addr` and `memory_size` must have previously been passed to `create_userspace_mapping`. # Errors If this function fails there is no way to clean up resources and you should probably crash the process.
(
&mut self,
guest_phys_addr: u64,
memory_size: usize,
userspace_addr: *mut u8,
mergeable: bool,
slot: u32,
)
| 2372 | /// If this function fails there is no way to clean up resources and you |
| 2373 | /// should probably crash the process. |
| 2374 | pub unsafe fn remove_userspace_mapping( |
| 2375 | &mut self, |
| 2376 | guest_phys_addr: u64, |
| 2377 | memory_size: usize, |
| 2378 | userspace_addr: *mut u8, |
| 2379 | mergeable: bool, |
| 2380 | slot: u32, |
| 2381 | ) -> Result<(), Error> { |
| 2382 | // SAFETY: Caller promises parameters are correct. |
| 2383 | unsafe { |
| 2384 | self.vm |
| 2385 | .remove_user_memory_region( |
| 2386 | slot, |
| 2387 | guest_phys_addr, |
| 2388 | memory_size, |
| 2389 | userspace_addr, |
| 2390 | false, /* readonly -- don't care */ |
| 2391 | false, /* log dirty */ |
| 2392 | ) |
| 2393 | .map_err(Error::RemoveUserMemoryRegion)?; |
| 2394 | } |
| 2395 | |
| 2396 | // Mark the pages as unmergeable if there were previously marked as |
| 2397 | // mergeable. |
| 2398 | if mergeable { |
| 2399 | // SAFETY: the address and size are valid as the region was |
| 2400 | // previously advised. |
| 2401 | let ret = unsafe { |
| 2402 | libc::madvise( |
| 2403 | userspace_addr.cast(), |
| 2404 | memory_size as libc::size_t, |
| 2405 | libc::MADV_UNMERGEABLE, |
| 2406 | ) |
| 2407 | }; |
| 2408 | if ret != 0 { |
| 2409 | let err = io::Error::last_os_error(); |
| 2410 | // Safe to unwrap because the error is constructed with |
| 2411 | // last_os_error(), which ensures the output will be Some(). |
| 2412 | let errno = err.raw_os_error().unwrap(); |
| 2413 | if errno == libc::EINVAL { |
| 2414 | warn!("kernel not configured with CONFIG_KSM"); |
| 2415 | } else { |
| 2416 | warn!("madvise error: {err}"); |
| 2417 | } |
| 2418 | warn!("failed to mark pages as unmergeable"); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | info!( |
| 2423 | "Removed userspace mapping: {guest_phys_addr:x} -> {userspace_addr_:x} {memory_size:x}", |
| 2424 | userspace_addr_ = userspace_addr as u64 |
| 2425 | ); |
| 2426 | |
| 2427 | Ok(()) |
| 2428 | } |
| 2429 | |
| 2430 | pub fn virtio_mem_resize(&mut self, id: &str, size: u64) -> Result<(), Error> { |
| 2431 | if let Some(memory_zone) = self.memory_zones.get_mut(id) { |
no test coverage detected