Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor enhancement and bugfixes #151

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 src/cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub enum Cap {
S390UserSigp = KVM_CAP_S390_USER_SIGP,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
SplitIrqchip = KVM_CAP_SPLIT_IRQCHIP,
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
DisableExits = KVM_CAP_X86_DISABLE_EXITS,
ImmediateExit = KVM_CAP_IMMEDIATE_EXIT,
ArmVmIPASize = KVM_CAP_ARM_VM_IPA_SIZE,
MsiDevid = KVM_CAP_MSI_DEVID,
Expand Down
60 changes: 53 additions & 7 deletions src/ioctls/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,12 @@ impl Kvm {
/// assert!(kvm.get_max_vcpus() > 0);
/// ```
pub fn get_max_vcpus(&self) -> usize {
match self.check_extension_int(Cap::MaxVcpus) {
0 => self.get_nr_vcpus(),
x => x as usize,
let v = self.check_extension_int(Cap::MaxVcpus);

if v <= 0 {
self.get_nr_vcpus()
} else {
v as usize
}
}

Expand All @@ -233,9 +236,38 @@ impl Kvm {
/// assert!(kvm.get_max_vcpu_id() > 0);
/// ```
pub fn get_max_vcpu_id(&self) -> usize {
match self.check_extension_int(Cap::MaxVcpuId) {
0 => self.get_max_vcpus(),
x => x as usize,
let v = self.check_extension_int(Cap::MaxVcpuId);

if v <= 0 {
self.get_max_vcpus()
} else {
v as usize
}
}

/// Returns the "disable idle exiting" capability.
///
/// The `KVM_CAP_X86_DISABLE_EXITS` capability provides userspace with per-VM capability
/// to not intercept MWAIT/HLT/PAUSE/CSTATE, which means though instructions won't cause
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// to not intercept MWAIT/HLT/PAUSE/CSTATE, which means though instructions won't cause
/// to not intercept MWAIT/HLT/PAUSE/CSTATE, which means such instructions won't cause

or these

/// vm-exits and helps to improve latency in some workloads.
///
/// # Example
///
/// ```
/// # extern crate kvm_bindings;
/// # use kvm_bindings::{KVM_X86_DISABLE_EXITS_HLT, KVM_X86_DISABLE_EXITS_PAUSE};
/// # use kvm_ioctls::Kvm;
/// let kvm = Kvm::new().unwrap();
/// assert!(kvm.get_disable_exits() & KVM_X86_DISABLE_EXITS_HLT != 0);
/// assert!(kvm.get_disable_exits() & KVM_X86_DISABLE_EXITS_PAUSE != 0);
/// ```
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub fn get_disable_exits(&self) -> u32 {
let x = self.check_extension_int(Cap::DisableExits);
if x > 0 {
x as u32
} else {
0
}
}

Expand Down Expand Up @@ -542,7 +574,9 @@ impl FromRawFd for Kvm {
mod tests {
use super::*;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use kvm_bindings::KVM_MAX_CPUID_ENTRIES;
use kvm_bindings::{
KVM_MAX_CPUID_ENTRIES, KVM_X86_DISABLE_EXITS_HLT, KVM_X86_DISABLE_EXITS_PAUSE,
};
use libc::{fcntl, FD_CLOEXEC, F_GETFD};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use vmm_sys_util::fam::FamStruct;
Expand Down Expand Up @@ -598,6 +632,18 @@ mod tests {
assert!(kvm.get_nr_memslots() >= 32);
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[test]
fn test_kvm_get_disable_exits() {
let kvm = Kvm::new().unwrap();
let disable_exits = kvm.get_disable_exits();

if disable_exits != 0 {
assert!(disable_exits & KVM_X86_DISABLE_EXITS_HLT != 0);
assert!(disable_exits & KVM_X86_DISABLE_EXITS_PAUSE != 0);
Comment on lines +642 to +643
Copy link
Collaborator

Choose a reason for hiding this comment

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

Running this test on my laptop fails, should we check here for some capability before doing the asserts? Maybe this way we can also have a test case for get_disable_exits returning 0.

}
}

#[test]
fn test_create_vm() {
let kvm = Kvm::new().unwrap();
Expand Down