Skip to content

Commit e47adfa

Browse files
committed
iommufd-ioctls: Add destroy_iommu_object
Signed-off-by: Bo Chen <bchen@crusoe.ai>
1 parent 4728ed3 commit e47adfa

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

iommufd-ioctls/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Changed
44

55
## Added
6+
- [[9]](https://github.com/cloud-hypervisor/iommufd/pull/9) Add `Iommufd::destroy_iommu_object` to release iommufd
7+
objects explicitly.
68

79
## Fixed
810

iommufd-ioctls/src/iommufd_ioctls.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ impl IommuFd {
2626
Ok(IommuFd { iommufd })
2727
}
2828

29+
pub fn destroy_iommu_object(&self, id: u32) -> Result<()> {
30+
let destroy_data = iommu_destroy {
31+
size: std::mem::size_of::<iommu_destroy>() as u32,
32+
id,
33+
};
34+
iommufd_syscall::destroy_iommu_object(self, &destroy_data)
35+
}
36+
2937
pub fn alloc_iommu_ioas(&self, alloc_data: &mut iommu_ioas_alloc) -> Result<()> {
3038
iommufd_syscall::alloc_iommu_ioas(self, alloc_data)
3139
}
@@ -44,6 +52,7 @@ impl AsRawFd for IommuFd {
4452
}
4553
}
4654

55+
ioctl_io_nr!(IOMMU_DESTROY, IOMMUFD_TYPE as u32, IOMMUFD_CMD_DESTROY);
4756
ioctl_io_nr!(
4857
IOMMU_IOAS_ALLOC,
4958
IOMMUFD_TYPE as u32,
@@ -64,6 +73,23 @@ pub(crate) mod iommufd_syscall {
6473
use super::*;
6574
use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref};
6675

76+
pub(crate) fn destroy_iommu_object(
77+
iommufd: &IommuFd,
78+
destroy_data: &iommu_destroy,
79+
) -> Result<()> {
80+
// SAFETY:
81+
// 1. The file descriptor provided by 'iommufd' is valid and open.
82+
// 2. The 'destroy_data' points to initialized memory with expected data structure,
83+
// and remains valid for the duration of sysca
84+
// 3. The return value is checked.
85+
let ret = unsafe { ioctl_with_ref(iommufd, IOMMU_DESTROY(), destroy_data) };
86+
if ret < 0 {
87+
Err(IommufdError::IommuDestroy(SysError::last()))
88+
} else {
89+
Ok(())
90+
}
91+
}
92+
6793
pub(crate) fn alloc_iommu_ioas(
6894
iommufd: &IommuFd,
6995
alloc_data: &mut iommu_ioas_alloc,
@@ -115,6 +141,7 @@ mod tests {
115141

116142
#[test]
117143
fn test_iommufd_ioctl_code() {
144+
assert_eq!(IOMMU_DESTROY(), 15232);
118145
assert_eq!(IOMMU_IOAS_ALLOC(), 15233);
119146
assert_eq!(IOMMU_IOAS_MAP(), 15237);
120147
assert_eq!(IOMMU_IOAS_UNMAP(), 15238);

iommufd-ioctls/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub use iommufd_ioctls::*;
1818
pub enum IommufdError {
1919
#[error("failed to open /dev/iommufd: {0}")]
2020
OpenIommufd(#[source] io::Error),
21+
#[error("failed to destroy iommufd object: {0}")]
22+
IommuDestroy(#[source] SysError),
2123
#[error("failed to allocate IOAS: {0}")]
2224
IommuIoasAlloc(#[source] SysError),
2325
#[error("failed to map an IOVA range to the IOAS: {0}")]

0 commit comments

Comments
 (0)