hypervisor: Suppress unused_unsafe warning

x86::__cpuid is safe on Rust ≥1.94 but unsafe on older versions.  This
causes unused_unsafe warnings when compiling with Rust ≥1.94.  However,
on earlier Rust versions, the code won’t compile if the unsafe blocks
are absent.

Work around this by adding #[allow(unused_unsafe)] where needed to
suppress the warnings.

See #7588 for more discussion.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour
2025-12-31 12:06:24 -05:00
committed by Bo Chen
parent dae66ce493
commit 02e3570bdd
2 changed files with 9 additions and 1 deletions

View File

@@ -559,6 +559,7 @@ pub fn generate_common_cpuid(
hypervisor: &dyn hypervisor::Hypervisor, hypervisor: &dyn hypervisor::Hypervisor,
config: &CpuidConfig, config: &CpuidConfig,
) -> super::Result<Vec<CpuIdEntry>> { ) -> super::Result<Vec<CpuIdEntry>> {
#[allow(unused_unsafe)]
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
if unsafe { x86_64::__cpuid(1) }.ecx & (1 << HYPERVISOR_ECX_BIT) == 1 << HYPERVISOR_ECX_BIT { if unsafe { x86_64::__cpuid(1) }.ecx & (1 << HYPERVISOR_ECX_BIT) == 1 << HYPERVISOR_ECX_BIT {
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
@@ -694,6 +695,7 @@ pub fn generate_common_cpuid(
// Copy host L1 cache details if not populated by KVM // Copy host L1 cache details if not populated by KVM
0x8000_0005 => { 0x8000_0005 => {
if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 { if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 {
#[allow(unused_unsafe)]
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
if unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax } >= 0x8000_0005 { if unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax } >= 0x8000_0005 {
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
@@ -708,8 +710,10 @@ pub fn generate_common_cpuid(
// Copy host L2 cache details if not populated by KVM // Copy host L2 cache details if not populated by KVM
0x8000_0006 => { 0x8000_0006 => {
if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 { if entry.eax == 0 && entry.ebx == 0 && entry.ecx == 0 && entry.edx == 0 {
#[allow(unused_unsafe)]
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
if unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax } >= 0x8000_0006 { if unsafe { std::arch::x86_64::__cpuid(0x8000_0000).eax } >= 0x8000_0006 {
#[allow(unused_unsafe)]
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
let leaf = unsafe { std::arch::x86_64::__cpuid(0x8000_0006) }; let leaf = unsafe { std::arch::x86_64::__cpuid(0x8000_0006) };
entry.eax = leaf.eax; entry.eax = leaf.eax;
@@ -747,6 +751,7 @@ pub fn generate_common_cpuid(
for i in 0x8000_0002..=0x8000_0004 { for i in 0x8000_0002..=0x8000_0004 {
cpuid.retain(|c| c.function != i); cpuid.retain(|c| c.function != i);
// SAFETY: call cpuid with valid leaves // SAFETY: call cpuid with valid leaves
#[allow(unused_unsafe)]
let leaf = unsafe { std::arch::x86_64::__cpuid(i) }; let leaf = unsafe { std::arch::x86_64::__cpuid(i) };
cpuid.push(CpuIdEntry { cpuid.push(CpuIdEntry {
function: i, function: i,
@@ -859,6 +864,7 @@ pub fn configure_vcpu(
// The TSC frequency CPUID leaf should not be included when running with HyperV emulation // The TSC frequency CPUID leaf should not be included when running with HyperV emulation
if !kvm_hyperv && let Some(tsc_khz) = vcpu.tsc_khz().map_err(Error::GetTscFrequency)? { if !kvm_hyperv && let Some(tsc_khz) = vcpu.tsc_khz().map_err(Error::GetTscFrequency)? {
// Need to check that the TSC doesn't vary with dynamic frequency // Need to check that the TSC doesn't vary with dynamic frequency
#[allow(unused_unsafe)]
// SAFETY: cpuid called with valid leaves // SAFETY: cpuid called with valid leaves
if unsafe { std::arch::x86_64::__cpuid(0x8000_0007) }.edx & (1u32 << INVARIANT_TSC_EDX_BIT) if unsafe { std::arch::x86_64::__cpuid(0x8000_0007) }.edx & (1u32 << INVARIANT_TSC_EDX_BIT)
> 0 > 0
@@ -1307,6 +1313,7 @@ pub fn initramfs_load_addr(
pub fn get_host_cpu_phys_bits(hypervisor: &dyn hypervisor::Hypervisor) -> u8 { pub fn get_host_cpu_phys_bits(hypervisor: &dyn hypervisor::Hypervisor) -> u8 {
// SAFETY: call cpuid with valid leaves // SAFETY: call cpuid with valid leaves
#[allow(unused_unsafe)]
unsafe { unsafe {
let leaf = x86_64::__cpuid(0x8000_0000); let leaf = x86_64::__cpuid(0x8000_0000);

View File

@@ -155,7 +155,8 @@ pub trait Hypervisor: Send + Sync {
/// Determine CPU vendor /// Determine CPU vendor
/// ///
fn get_cpu_vendor(&self) -> CpuVendor { fn get_cpu_vendor(&self) -> CpuVendor {
// SAFETY: call cpuid with valid leaves #[allow(unused_unsafe)]
// SAFETY: not actually unsafe, but considered unsafe by current stable
unsafe { unsafe {
let leaf = x86_64::__cpuid(0x0); let leaf = x86_64::__cpuid(0x0);