diff --git a/fuzz/fuzz_targets/http_api.rs b/fuzz/fuzz_targets/http_api.rs index 720facba1..18e44b379 100644 --- a/fuzz/fuzz_targets/http_api.rs +++ b/fuzz/fuzz_targets/http_api.rs @@ -9,7 +9,6 @@ use once_cell::sync::Lazy; use std::os::unix::io::AsRawFd; use std::path::PathBuf; use std::sync::mpsc::{channel, Receiver}; -use std::sync::{Arc, Mutex}; use std::thread; use vm_migration::MigratableError; use vmm::api::{ @@ -81,7 +80,7 @@ fn generate_request(bytes: &[u8]) -> Option { struct StubApiRequestHandler; impl RequestHandler for StubApiRequestHandler { - fn vm_create(&mut self, _: Arc>) -> Result<(), VmError> { + fn vm_create(&mut self, _: Box) -> Result<(), VmError> { Ok(()) } @@ -120,7 +119,7 @@ impl RequestHandler for StubApiRequestHandler { fn vm_info(&self) -> Result { Ok(VmInfoResponse { - config: Arc::new(Mutex::new(VmConfig { + config: Box::new(VmConfig { cpus: CpusConfig { boot_vcpus: 1, max_vcpus: 1, @@ -194,7 +193,7 @@ impl RequestHandler for StubApiRequestHandler { preserved_fds: None, landlock_enable: false, landlock_rules: None, - })), + }), state: VmState::Running, memory_actual_size: 0, device_tree: None, diff --git a/src/main.rs b/src/main.rs index 9568fbe87..f47144150 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use signal_hook::consts::SIGSYS; use std::fs::File; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::sync::mpsc::channel; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use std::{env, io}; use thiserror::Error; #[cfg(feature = "dbus_api")] @@ -787,7 +787,7 @@ fn start_vmm(cmd_arguments: ArgMatches) -> Result, Error> { .send( api_evt.try_clone().unwrap(), api_request_sender, - Arc::new(Mutex::new(vm_config)), + Box::new(vm_config), ) .map_err(Error::VmCreate)?; vmm::api::VmBoot diff --git a/vmm/src/api/dbus/mod.rs b/vmm/src/api/dbus/mod.rs index fb7a9b20a..108fd5d9c 100644 --- a/vmm/src/api/dbus/mod.rs +++ b/vmm/src/api/dbus/mod.rs @@ -20,7 +20,7 @@ use hypervisor::HypervisorType; use seccompiler::{apply_filter, SeccompAction}; use std::panic::AssertUnwindSafe; use std::sync::mpsc::Sender; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::thread; use vmm_sys_util::eventfd::EventFd; use zbus::fdo::{self, Result}; @@ -201,7 +201,7 @@ impl DBusApi { let api_sender = self.clone_api_sender().await; let api_notifier = self.clone_api_notifier()?; - let mut vm_config: VmConfig = serde_json::from_str(&vm_config).map_err(api_error)?; + let mut vm_config: Box = serde_json::from_str(&vm_config).map_err(api_error)?; if let Some(ref mut nets) = vm_config.net { if nets.iter().any(|net| net.fds.is_some()) { @@ -212,11 +212,9 @@ impl DBusApi { } } - blocking::unblock(move || { - VmCreate.send(api_notifier, api_sender, Arc::new(Mutex::new(vm_config))) - }) - .await - .map_err(api_error)?; + blocking::unblock(move || VmCreate.send(api_notifier, api_sender, vm_config)) + .await + .map_err(api_error)?; Ok(()) } diff --git a/vmm/src/api/http/http_endpoint.rs b/vmm/src/api/http/http_endpoint.rs index 3d8d0c504..e310dee05 100644 --- a/vmm/src/api/http/http_endpoint.rs +++ b/vmm/src/api/http/http_endpoint.rs @@ -18,7 +18,6 @@ use micro_http::{Body, Method, Request, Response, StatusCode, Version}; use std::fs::File; use std::os::unix::io::IntoRawFd; use std::sync::mpsc::Sender; -use std::sync::{Arc, Mutex}; use vmm_sys_util::eventfd::EventFd; // /api/v1/vm.create handler @@ -36,7 +35,7 @@ impl EndpointHandler for VmCreate { match &req.body { Some(body) => { // Deserialize into a VmConfig - let mut vm_config: VmConfig = match serde_json::from_slice(body.raw()) + let mut vm_config: Box = match serde_json::from_slice(body.raw()) .map_err(HttpError::SerdeJsonDeserialize) { Ok(config) => config, @@ -53,7 +52,7 @@ impl EndpointHandler for VmCreate { } match crate::api::VmCreate - .send(api_notifier, api_sender, Arc::new(Mutex::new(vm_config))) + .send(api_notifier, api_sender, vm_config) .map_err(HttpError::ApiError) { Ok(_) => Response::new(Version::Http11, StatusCode::NoContent), diff --git a/vmm/src/api/mod.rs b/vmm/src/api/mod.rs index 26c9e22d6..6fa973631 100644 --- a/vmm/src/api/mod.rs +++ b/vmm/src/api/mod.rs @@ -51,7 +51,6 @@ use serde::{Deserialize, Serialize}; use std::fmt::Display; use std::io; use std::sync::mpsc::{channel, RecvError, SendError, Sender}; -use std::sync::{Arc, Mutex}; use vm_migration::MigratableError; use vmm_sys_util::eventfd::EventFd; @@ -210,10 +209,10 @@ impl Display for ApiError { #[derive(Clone, Deserialize, Serialize)] pub struct VmInfoResponse { - pub config: Arc>, + pub config: Box, pub state: VmState, pub memory_actual_size: u64, - pub device_tree: Option>>, + pub device_tree: Option, } #[derive(Clone, Deserialize, Serialize)] @@ -287,7 +286,7 @@ pub enum ApiResponsePayload { pub type ApiResponse = Result; pub trait RequestHandler { - fn vm_create(&mut self, config: Arc>) -> Result<(), VmError>; + fn vm_create(&mut self, config: Box) -> Result<(), VmError>; fn vm_boot(&mut self) -> Result<(), VmError>; @@ -827,7 +826,7 @@ impl ApiAction for VmCounters { pub struct VmCreate; impl ApiAction for VmCreate { - type RequestBody = Arc>; + type RequestBody = Box; type ResponseBody = (); fn request( diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 424147999..113f1a037 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -1239,11 +1239,11 @@ fn apply_landlock(vm_config: Arc>) -> result::Result<(), Landloc } impl RequestHandler for Vmm { - fn vm_create(&mut self, config: Arc>) -> result::Result<(), VmError> { + fn vm_create(&mut self, config: Box) -> result::Result<(), VmError> { // We only store the passed VM config. // The VM will be created when being asked to boot it. if self.vm_config.is_none() { - self.vm_config = Some(config); + self.vm_config = Some(Arc::new(Mutex::new(*config))); self.console_info = Some(pre_create_console_devices(self).map_err(VmError::CreateConsoleDevices)?); @@ -1554,23 +1554,25 @@ impl RequestHandler for Vmm { fn vm_info(&self) -> result::Result { match &self.vm_config { - Some(config) => { + Some(vm_config) => { let state = match &self.vm { Some(vm) => vm.get_state()?, None => VmState::Created, }; + let config = vm_config.lock().unwrap().clone(); - let config = Arc::clone(config); - - let mut memory_actual_size = config.lock().unwrap().memory.total_size(); + let mut memory_actual_size = config.memory.total_size(); if let Some(vm) = &self.vm { memory_actual_size -= vm.balloon_size(); } - let device_tree = self.vm.as_ref().map(|vm| vm.device_tree()); + let device_tree = self + .vm + .as_ref() + .map(|vm| vm.device_tree().lock().unwrap().clone()); Ok(VmInfoResponse { - config, + config: Box::new(config), state, memory_actual_size, device_tree, @@ -2164,8 +2166,8 @@ mod unit_tests { .unwrap() } - fn create_dummy_vm_config() -> Arc> { - Arc::new(Mutex::new(VmConfig { + fn create_dummy_vm_config() -> Box { + Box::new(VmConfig { cpus: CpusConfig { boot_vcpus: 1, max_vcpus: 1, @@ -2242,7 +2244,7 @@ mod unit_tests { preserved_fds: None, landlock_enable: false, landlock_rules: None, - })) + }) } #[test]