Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vhost-device-gpu/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

### Added

- [[#903]] (https://github.com/rust-vmm/vhost-device/pull/903) vhost-device-gpu: Add support for GPU device path

### Changed

- [[#852]] (https://github.com/rust-vmm/vhost-device/pull/890) vhost-device-gpu: Refactor vhost-device-gpu
Expand Down
12 changes: 12 additions & 0 deletions vhost-device-gpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ A virtio-gpu device using the vhost-user protocol.
[default: true]
[possible values: true, false]

--gpu-device <PATH>
GPU device path (e.g., /dev/dri/renderD128)

[Optional] Specifies which GPU device to use for rendering. Only
applicable when using the virglrenderer backend.

-h, --help
Print help (see a summary with '-h')

Expand Down Expand Up @@ -134,6 +140,12 @@ First start the daemon on the host machine using one of the available gpu modes:
host# vhost-device-gpu --socket-path /tmp/gpu.socket --gpu-mode virglrenderer
```

To specify a particular GPU device (e.g., when you have multiple GPUs):

```shell
host# vhost-device-gpu --socket-path /tmp/gpu.socket --gpu-mode virglrenderer --gpu-device /dev/dri/renderD128
```

With QEMU, there are two device front-ends you can use with this device.
You can either use `vhost-user-gpu-pci` or `vhost-user-vga`, which also
implements VGA, that allows you to see boot messages before the guest
Expand Down
12 changes: 8 additions & 4 deletions vhost-device-gpu/src/backend/gfxstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::{
cell::RefCell,
collections::BTreeMap,
io::IoSliceMut,
io::{self, IoSliceMut},
os::{fd::FromRawFd, raw::c_void},
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -103,7 +103,11 @@ pub struct GfxstreamAdapter {
}

impl GfxstreamAdapter {
pub fn new(queue_ctl: &VringRwLock, gpu_config: &GpuConfig, gpu_backend: GpuBackend) -> Self {
pub fn new(
queue_ctl: &VringRwLock,
gpu_config: &GpuConfig,
gpu_backend: GpuBackend,
) -> io::Result<Self> {
let fence_state = Arc::new(Mutex::new(FenceState::default()));
let fence = Self::create_fence_handler(queue_ctl.clone(), fence_state.clone());

Expand All @@ -116,12 +120,12 @@ impl GfxstreamAdapter {
}
});

Self {
Ok(Self {
gpu_backend,
fence_state,
resources: BTreeMap::new(),
scanouts: Default::default(),
}
})
}

fn create_fence_handler(
Expand Down
10 changes: 6 additions & 4 deletions vhost-device-gpu/src/backend/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause

use std::io;

use log::trace;
use rutabaga_gfx::RutabagaFence;
use vhost::vhost_user::{
Expand All @@ -28,11 +30,11 @@ impl NullAdapter {
_queue_ctl: &vhost_user_backend::VringRwLock,
_config: &GpuConfig,
gpu_backend: GpuBackend,
) -> Self {
) -> io::Result<Self> {
trace!("NullAdapter created");
Self {
Ok(Self {
_gpu_backend: gpu_backend,
}
})
}
}

Expand Down Expand Up @@ -264,7 +266,7 @@ mod tests {
let vring = VringRwLock::new(mem, 0x100).unwrap();
let config = GpuConfig::new(GpuMode::Null, None, GpuFlags::default()).unwrap();

NullAdapter::new(&vring, &config, gpu_backend)
NullAdapter::new(&vring, &config, gpu_backend).unwrap()
}

#[test]
Expand Down
42 changes: 35 additions & 7 deletions vhost-device-gpu/src/backend/virgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use std::{
collections::BTreeMap,
io::IoSliceMut,
io::{self, IoSliceMut},
os::fd::{AsFd, FromRawFd, IntoRawFd, RawFd},
sync::{Arc, Mutex},
};

use libc::c_void;
use log::{debug, error, trace, warn};
use rutabaga_gfx::RutabagaFence;
use thiserror::Error as ThisError;
use vhost::vhost_user::{
gpu_message::{
VhostUserGpuCursorPos, VhostUserGpuDMABUFScanout, VhostUserGpuDMABUFScanout2,
Expand Down Expand Up @@ -51,6 +52,20 @@ const CAPSET_ID_VIRGL: u32 = 1;
const CAPSET_ID_VIRGL2: u32 = 2;
const CAPSET_ID_VENUS: u32 = 4;

#[derive(Debug, ThisError)]
pub enum VirglAdapterError {
#[error("Failed to clone GPU device FD: {0}")]
CloneGpuDeviceFd(io::Error),
#[error("Failed to initialize virglrenderer: {0:?}")]
InitVirglRenderer(virglrenderer::VirglError),
}

impl From<VirglAdapterError> for io::Error {
fn from(e: VirglAdapterError) -> Self {
io::Error::other(e)
}
}

#[derive(Clone)]
pub struct GpuResource {
pub virgl_resource: VirglResource,
Expand Down Expand Up @@ -142,7 +157,11 @@ pub struct VirglRendererAdapter {
}

impl VirglRendererAdapter {
pub fn new(queue_ctl: &VringRwLock, config: &GpuConfig, gpu_backend: GpuBackend) -> Self {
pub fn new(
queue_ctl: &VringRwLock,
config: &GpuConfig,
gpu_backend: GpuBackend,
) -> io::Result<Self> {
let virglrenderer_flags = VirglRendererFlags::new()
.use_virgl(true)
.use_venus(true)
Expand All @@ -159,16 +178,25 @@ impl VirglRendererAdapter {
fence_state.clone(),
));

let renderer = VirglRenderer::init(virglrenderer_flags, fence_handler, None)
.expect("Failed to initialize virglrenderer");
Self {
// Use the GPU device FD if provided (already opened and validated at startup)
let render_server_fd = config
.flags()
.render_server_fd
.as_ref()
.map(|fd| fd.try_clone())
.transpose()
.map_err(VirglAdapterError::CloneGpuDeviceFd)?;

let renderer = VirglRenderer::init(virglrenderer_flags, fence_handler, render_server_fd)
.map_err(VirglAdapterError::InitVirglRenderer)?;
Ok(Self {
renderer,
gpu_backend,
fence_state,
resources: BTreeMap::new(),
contexts: BTreeMap::new(),
scanouts: Default::default(),
}
})
}
}

Expand Down Expand Up @@ -758,7 +786,7 @@ mod virgl_cov_tests {
create_vring(&mem, &[] as &[TestingDescChainArgs], GuestAddress(0x2000), GuestAddress(0x4000), 64);

let backend = dummy_gpu_backend();
let mut gpu = VirglRendererAdapter::new(&vring, &cfg, backend);
let mut gpu = VirglRendererAdapter::new(&vring, &cfg, backend).unwrap();

gpu.event_poll();
let edid_req = VhostUserGpuEdidRequest {
Expand Down
2 changes: 1 addition & 1 deletion vhost-device-gpu/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ macro_rules! handle_adapter {
// Pass $vrings to the call
let (control_vring, gpu_backend) = $self.extract_backend_and_vring($vrings)?;

let renderer = $new_adapter(control_vring, gpu_backend);
let renderer = $new_adapter(control_vring, gpu_backend)?;

event_poll_fd = renderer.get_event_poll_fd();
maybe_renderer.insert(renderer)
Expand Down
22 changes: 19 additions & 3 deletions vhost-device-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ pub mod renderer;
#[cfg(test)]
pub(crate) mod testutils;

#[cfg(feature = "backend-virgl")]
use std::os::fd::OwnedFd;
use std::{
fmt::{Display, Formatter},
path::Path,
path::{Path, PathBuf},
};

use bitflags::bitflags;
Expand Down Expand Up @@ -117,20 +119,22 @@ impl GpuCapset {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
/// This structure holds the configuration for the GPU backend
pub struct GpuConfig {
gpu_mode: GpuMode,
capset: GpuCapset,
flags: GpuFlags,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug)]
pub struct GpuFlags {
pub use_egl: bool,
pub use_glx: bool,
pub use_gles: bool,
pub use_surfaceless: bool,
#[cfg(feature = "backend-virgl")]
pub render_server_fd: Option<OwnedFd>,
}

impl GpuFlags {
Expand All @@ -141,6 +145,8 @@ impl GpuFlags {
use_glx: false,
use_gles: true,
use_surfaceless: true,
#[cfg(feature = "backend-virgl")]
render_server_fd: None,
}
}
}
Expand All @@ -157,6 +163,10 @@ pub enum GpuConfigError {
CapsetUnsupportedByMode(GpuMode, GpuCapset),
#[error("Requested gfxstream-gles capset, but gles is disabled")]
GlesRequiredByGfxstream,
#[error("GPU path can only be specified when using virglrenderer mode")]
GpuPathNotSupportedByMode,
#[error("Failed to open GPU device '{0}'")]
InvalidGpuDevice(PathBuf),
}

impl GpuConfig {
Expand Down Expand Up @@ -208,6 +218,12 @@ impl GpuConfig {
return Err(GpuConfigError::GlesRequiredByGfxstream);
}

// Validate that render_server_fd is only used with virglrenderer
#[cfg(feature = "backend-virgl")]
if flags.render_server_fd.is_some() && !matches!(gpu_mode, GpuMode::VirglRenderer) {
return Err(GpuConfigError::GpuPathNotSupportedByMode);
}

Comment on lines +223 to +226
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must insist on using a builder pattern, so the validation can be higher in the caller

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created an issue here - #910

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not fix it now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned previously, the builder pattern refactor is out of scope for this PR. This PR adds the --gpu-device feature following the existing GpuConfig::new() pattern used throughout the codebase. Changing to a builder pattern would require refactoring how all backends instantiate GpuConfig and is a separate architectural change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a rush to merge this PR though? It'd make sense to first make the builder pattern change (in another PR or in a commit in this PR), then add the device path logic. There's no need to add something that you know you will remove.

I don't think it's out of scope btw, for what it's worth.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree that this is in scope. The builder pattern would be a significant refactoring affecting the entire GpuConfig API and all three backends. The current implementation follows the existing pattern already in the codebase. This is a much bigger change than adding the --gpu-device feature alone. But if you feel strongly about it, I can implement the builder pattern first in another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly disagree, I'm 100% with @epilys here, it doesn't make any sense to add something to be removed later.

I also don't think it will require lots of changes most places will change from GpuConfig::new(...) to something like GpuConfig::new(...).build(), and for VirglRenderer mode something like GpuConfig::new(GpuMode::VirglRenderer, ...).gpu_device(fd).build(), or something like that. Likewise, I think this PR is small enough to accommodate those changes.

As a side note, the description on issue #910 is unhelpful, adding a link to this thread is ok, but it should have more context

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok of doing it in another PR first, thanks

Ok(Self {
gpu_mode,
capset,
Expand Down
Loading