Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
vfio_device: remove RawFd from function parameters
Browse files Browse the repository at this point in the history
Generally speaking functions take RawFd should be marked as unsafe.
device_add_group and device_del group are such functions.

Instead of marks them as unsafe, we can make these two functions take a
reference to VfioGroup. This is far more elegant.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
  • Loading branch information
liuw authored and jiangliu committed Sep 7, 2021
1 parent e6a5934 commit 7120e0b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/vfio_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ impl VfioContainer {
Ok(())
}

fn device_add_group(&self, group_fd: RawFd) -> Result<()> {
let group_fd_ptr = &group_fd as *const i32;
fn device_add_group(&self, group: &VfioGroup) -> Result<()> {
let group_fd_ptr = &group.as_raw_fd() as *const i32;

#[cfg(feature = "kvm")]
let dev_attr = kvm_device_attr {
Expand All @@ -256,8 +256,8 @@ impl VfioContainer {
.map_err(VfioError::SetDeviceAttr)
}

fn device_del_group(&self, group_fd: RawFd) -> Result<()> {
let group_fd_ptr = &group_fd as *const i32;
fn device_del_group(&self, group: &VfioGroup) -> Result<()> {
let group_fd_ptr = &group.as_raw_fd() as *const i32;
#[cfg(feature = "kvm")]
let dev_attr = kvm_device_attr {
flags: 0,
Expand Down Expand Up @@ -308,7 +308,7 @@ impl VfioContainer {
}

// Add the new group object to the hypervisor driver.
if let Err(e) = self.device_add_group(group.as_raw_fd()) {
if let Err(e) = self.device_add_group(&group) {
let _ =
unsafe { ioctl_with_ref(&*group, VFIO_GROUP_UNSET_CONTAINER(), &self.as_raw_fd()) };
return Err(e);
Expand All @@ -329,7 +329,7 @@ impl VfioContainer {
// - one reference cloned in VfioDevice.drop() and passed into here
// - one reference held by the groups hashmap
if Arc::strong_count(&group) == 3 {
match self.device_del_group(group.as_raw_fd()) {
match self.device_del_group(&group) {
Ok(_) => {}
Err(e) => {
error!("Could not delete VFIO group: {:?}", e);
Expand Down

0 comments on commit 7120e0b

Please sign in to comment.