Skip to content

Commit 741fb48

Browse files
committed
Add kvm guest memfd related capabilities
The capabilities are required to properly setup a guest_memfd to provide better host and guest memory isolation. The memory attributes capability returns an integer with each bits representing different configs. For example, the return integer & KVM_MEMORY_ATTRIBUTE_PRIVATE > 0 means the vm is capable of setting memory pages to private. Signed-off-by: Sida Chen <[email protected]>
1 parent e75abe5 commit 741fb48

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- [[#273](https://github.com/rust-vmm/kvm-ioctls/pull/273)]: `DeviceFd::get_device_attr` is now
1010
marked as unsafe.
1111
- [[#277](https://github.com/rust-vmm/kvm-ioctls/pull/277)]: Updated kvm-bindings to 0.9.1.
12+
- [[#287](https://github.com/rust-vmm/kvm-ioctls/pull/287)]: Add kvm guest memfd related
13+
capabilities.
1214

1315
## v0.18.0
1416

src/cap.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,7 @@ pub enum Cap {
171171
ExitHypercall = KVM_CAP_EXIT_HYPERCALL,
172172
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
173173
MemoryFaultInfo = KVM_CAP_MEMORY_FAULT_INFO,
174+
UserMemory2 = KVM_CAP_USER_MEMORY2,
175+
GuestMemfd = KVM_CAP_GUEST_MEMFD,
176+
MemoryAttributes = KVM_CAP_MEMORY_ATTRIBUTES,
174177
}

src/ioctls/vm.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,53 @@ impl VmFd {
13901390
/// Wrapper over `KVM_CHECK_EXTENSION`.
13911391
///
13921392
/// Returns 0 if the capability is not available and a positive integer otherwise.
1393-
fn check_extension_int(&self, c: Cap) -> i32 {
1394-
// SAFETY: Safe because we know that our file is a VM fd and that the extension is one of
1395-
// the ones defined by kernel.
1396-
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
1393+
/// See the documentation for `KVM_CHECK_EXTENSION`.
1394+
///
1395+
/// # Arguments
1396+
///
1397+
/// * `c` - KVM capability to check.
1398+
///
1399+
/// # Example
1400+
///
1401+
/// ```
1402+
/// extern crate kvm_bindings;
1403+
///
1404+
/// # use kvm_bindings::{KVM_MEMORY_ATTRIBUTE_PRIVATE};
1405+
/// # use kvm_ioctls::Kvm;
1406+
/// use kvm_ioctls::Cap;
1407+
///
1408+
/// let kvm = Kvm::new().unwrap();
1409+
/// assert!(
1410+
/// kvm.check_extension_int(Cap::MemoryAttributes) as u32 & KVM_MEMORY_ATTRIBUTE_PRIVATE > 0
1411+
/// );
1412+
/// ```
1413+
pub fn check_extension_int(&self, c: Cap) -> i32 {
1414+
self.check_extension_raw(c as c_ulong)
1415+
}
1416+
1417+
/// Wrapper over `KVM_CHECK_EXTENSION`.
1418+
///
1419+
/// Returns 0 if the capability is not available and a positive integer otherwise.
1420+
/// See the documentation for `KVM_CHECK_EXTENSION`.
1421+
///
1422+
/// # Arguments
1423+
///
1424+
/// * `c` - KVM capability to check in a form of a raw integer.
1425+
///
1426+
/// # Example
1427+
///
1428+
/// ```
1429+
/// # use kvm_ioctls::Kvm;
1430+
/// # use std::os::raw::c_ulong;
1431+
/// use kvm_ioctls::Cap;
1432+
///
1433+
/// let kvm = Kvm::new().unwrap();
1434+
/// assert!(kvm.check_extension_raw(Cap::GuestMemfd as c_ulong) > 0);
1435+
/// ```
1436+
pub fn check_extension_raw(&self, c: c_ulong) -> i32 {
1437+
// SAFETY: Safe because we know that our file is a KVM fd.
1438+
// If `c` is not a known kernel extension, kernel will return 0.
1439+
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c) }
13971440
}
13981441

13991442
/// Checks if a particular `Cap` is available.
@@ -2513,6 +2556,20 @@ mod tests {
25132556
assert!(vm.check_extension(Cap::MpState));
25142557
}
25152558

2559+
#[test]
2560+
fn test_check_extension_int() {
2561+
let kvm = Kvm::new().unwrap();
2562+
let vm = kvm.create_vm().unwrap();
2563+
assert!(vm.check_extension_int(Cap::MpState) > 0);
2564+
}
2565+
2566+
#[test]
2567+
fn test_check_extension_raw() {
2568+
let kvm = Kvm::new().unwrap();
2569+
let vm = kvm.create_vm().unwrap();
2570+
assert!(vm.check_extension_raw(Cap::MpState as c_ulong) > 0);
2571+
}
2572+
25162573
#[test]
25172574
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
25182575
#[cfg_attr(not(has_sev), ignore)]

0 commit comments

Comments
 (0)