devices: vhost-user: fs: remove global executor variable

This variable can be passed as part of the backend structure.

BUG=None
TEST=cargo build

Change-Id: Ic2f5987110acb54bd5486bd63924d052bded17cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3451755
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
This commit is contained in:
Alexandre Courbot 2022-02-10 16:37:43 +09:00 committed by Commit Bot
parent 95d104fdd9
commit d368c1520a

View file

@ -17,7 +17,6 @@ use fuse::Server;
use futures::future::{AbortHandle, Abortable};
use hypervisor::ProtectionType;
use minijail::{self, Minijail};
use once_cell::sync::OnceCell;
use sync::Mutex;
use vm_memory::GuestMemory;
use vmm_vhost::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
@ -30,8 +29,6 @@ use crate::virtio::vhost::user::device::handler::{
DeviceRequestHandler, Doorbell, VhostUserBackend,
};
static FS_EXECUTOR: OnceCell<Executor> = OnceCell::new();
async fn handle_fs_queue(
mut queue: virtio::Queue,
mem: GuestMemory,
@ -114,6 +111,7 @@ fn jail_and_fork(
}
struct FsBackend {
ex: Executor,
server: Arc<fuse::Server<PassthroughFs>>,
tag: [u8; FS_MAX_TAG_LEN],
avail_features: u64,
@ -124,7 +122,7 @@ struct FsBackend {
}
impl FsBackend {
pub fn new(tag: &str) -> anyhow::Result<Self> {
pub fn new(ex: &Executor, tag: &str) -> anyhow::Result<Self> {
if tag.len() > FS_MAX_TAG_LEN {
bail!(
"fs tag is too long: {} (max supported: {})",
@ -147,6 +145,7 @@ impl FsBackend {
let server = Arc::new(Server::new(fs));
Ok(FsBackend {
ex: ex.clone(),
server,
tag: fs_tag,
avail_features,
@ -226,29 +225,27 @@ impl VhostUserBackend for FsBackend {
handle.abort();
}
// Safe because the executor is initialized in main() below.
let ex = FS_EXECUTOR.get().expect("Executor not initialized");
// Enable any virtqueue features that were negotiated (like VIRTIO_RING_F_EVENT_IDX).
queue.ack_features(self.acked_features);
let kick_evt =
EventAsync::new(kick_evt.0, ex).context("failed to create EventAsync for kick_evt")?;
let kick_evt = EventAsync::new(kick_evt.0, &self.ex)
.context("failed to create EventAsync for kick_evt")?;
let (handle, registration) = AbortHandle::new_pair();
let (_, fs_device_tube) = Tube::pair()?;
ex.spawn_local(Abortable::new(
handle_fs_queue(
queue,
mem,
doorbell,
kick_evt,
self.server.clone(),
Arc::new(Mutex::new(fs_device_tube)),
),
registration,
))
.detach();
self.ex
.spawn_local(Abortable::new(
handle_fs_queue(
queue,
mem,
doorbell,
kick_evt,
self.server.clone(),
Arc::new(Mutex::new(fs_device_tube)),
),
registration,
))
.detach();
self.workers[idx] = Some(handle);
Ok(())
@ -293,7 +290,8 @@ pub fn run_fs_device(program_name: &str, args: &[&str]) -> anyhow::Result<()> {
base::syslog::init().context("Failed to initialize syslog")?;
let fs_device = FsBackend::new(&opts.tag)?;
let ex = Executor::new().context("Failed to create executor")?;
let fs_device = FsBackend::new(&ex, &opts.tag)?;
// Create and bind unix socket
let listener = UnixListener::bind(opts.socket).map(UnlinkUnixListener)?;
@ -331,11 +329,6 @@ pub fn run_fs_device(program_name: &str, args: &[&str]) -> anyhow::Result<()> {
}
}
// Child, we can continue by spawning the executor and set up the device
let ex = Executor::new().context("Failed to create executor")?;
let _ = FS_EXECUTOR.set(ex.clone());
// run_until() returns an Result<Result<..>> which the ? operator lets us flatten.
ex.run_until(handler.run_with_listener(listener, &ex))?
}