mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
Mechanical, behavior-neutral: edition/resolver keys, unsafe extern blocks, unsafe(no_mangle)/unsafe(export_name), unsafe blocks inside unsafe fn, r#gen renames, --edition flags in Makefiles/tools, rustfmt style_edition pinned to 2021 so formatting stays edition-independent, and clippy collapsible_if allowed workspace-wide (let-chain churn). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
440 lines
15 KiB
Rust
440 lines
15 KiB
Rust
#![allow(dead_code)]
|
|
#[path = "../../src/nvidia/kernel/device_software.rs"]
|
|
mod device_software;
|
|
#[path = "../../src/nvidia/kernel/dma.rs"]
|
|
mod dma;
|
|
#[path = "../../src/nvidia/kernel/gpu.rs"]
|
|
mod gpu;
|
|
#[path = "../../src/nvidia/kernel/hypervisor.rs"]
|
|
mod hypervisor;
|
|
#[path = "../../src/nvidia/kernel/pf_software.rs"]
|
|
mod pf_software;
|
|
#[path = "../../src/nvidia/kernel/registry.rs"]
|
|
mod registry;
|
|
#[path = "../../src/nvidia/kernel/software_manager.rs"]
|
|
mod software_manager;
|
|
#[path = "../../src/nvidia/kernel/system_os.rs"]
|
|
mod system_os;
|
|
#[path = "../../src/nvidia/kernel/system_platform.rs"]
|
|
mod system_platform;
|
|
#[path = "../../src/nvidia/kernel/thread.rs"]
|
|
mod thread;
|
|
#[path = "../../src/nvidia/kernel/thread_state.rs"]
|
|
mod thread_state;
|
|
#[path = "../../src/nvidia/kernel/timeout_runtime.rs"]
|
|
mod timeout_runtime;
|
|
use core::{
|
|
ffi::c_void,
|
|
mem::MaybeUninit,
|
|
sync::atomic::{AtomicU32, Ordering},
|
|
};
|
|
use nv_rs_abi::NvStatus;
|
|
use nv_rs_core::gpu::thread_timeout::{Database, Node, Services};
|
|
use nv_rs_core::hypervisor::HypervisorType;
|
|
use std::{
|
|
num::NonZeroU32,
|
|
sync::{Arc, Mutex},
|
|
};
|
|
static LOCK: Mutex<()> = Mutex::new(());
|
|
unsafe extern "C" {
|
|
fn fixture_pci_create() -> *mut c_void;
|
|
fn fixture_pci_close(p: *mut c_void) -> i32;
|
|
fn fixture_pci_remove(p: *mut c_void);
|
|
fn fixture_module_pins() -> u32;
|
|
fn fixture_fault_cpu_live() -> u32;
|
|
fn fixture_cpu_alloc_at(n: u32);
|
|
fn fixture_cpu_free_at(n: u32);
|
|
fn fixture_cpu_alloc_calls() -> u32;
|
|
fn fixture_operation_create() -> *mut c_void;
|
|
fn fixture_operation_lock(p: *mut c_void) -> i32;
|
|
fn fixture_operation_unlock(p: *mut c_void);
|
|
fn fixture_operation_destroy(p: *mut c_void);
|
|
fn fixture_context(pid: u64, cpu: u32, context: u32, flags: u32);
|
|
fn fixture_fail_context_closes(n: u32);
|
|
fn fixture_boot_config(boot4: u32, fail_at: u32);
|
|
fn fixture_boot_reads() -> u32;
|
|
fn fixture_boot_offset(index: u32) -> u64;
|
|
}
|
|
struct Counts {
|
|
destroyed: AtomicU32,
|
|
retries: AtomicU32,
|
|
}
|
|
struct Owner {
|
|
gpu: *mut MaybeUninit<gpu::KernelGpu>,
|
|
pci: *mut c_void,
|
|
operation: *mut c_void,
|
|
counts: Arc<Counts>,
|
|
}
|
|
unsafe extern "C" fn destroy(p: *mut c_void) {
|
|
unsafe {
|
|
let o = Box::from_raw(p.cast::<Owner>());
|
|
assert_eq!(fixture_pci_close(o.pci), 0);
|
|
fixture_operation_destroy(o.operation);
|
|
o.counts.destroyed.fetch_add(1, Ordering::AcqRel);
|
|
drop(Box::from_raw(o.gpu));
|
|
}
|
|
}
|
|
unsafe extern "C" fn retry(p: *mut c_void) {
|
|
unsafe {
|
|
(&(*p.cast::<Owner>()).counts)
|
|
.retries
|
|
.fetch_add(1, Ordering::AcqRel);
|
|
}
|
|
}
|
|
unsafe extern "C" fn lock(p: *mut c_void) -> i32 {
|
|
unsafe { fixture_operation_lock((*p.cast::<Owner>()).operation) }
|
|
}
|
|
unsafe extern "C" fn unlock(p: *mut c_void) {
|
|
unsafe { fixture_operation_unlock((*p.cast::<Owner>()).operation) }
|
|
}
|
|
unsafe fn make() -> (*mut gpu::KernelGpu, *mut c_void, Arc<Counts>) {
|
|
unsafe {
|
|
let pci = fixture_pci_create();
|
|
let raw = Box::into_raw(Box::new(MaybeUninit::<gpu::KernelGpu>::uninit()));
|
|
let counts = Arc::new(Counts {
|
|
destroyed: AtomicU32::new(0),
|
|
retries: AtomicU32::new(0),
|
|
});
|
|
let owner = Box::into_raw(Box::new(Owner {
|
|
gpu: raw,
|
|
pci,
|
|
operation: fixture_operation_create(),
|
|
counts: counts.clone(),
|
|
}));
|
|
let info = gpu::ProbeInfo {
|
|
vendor: 0x10de,
|
|
device: 0x1db5,
|
|
subvendor: 0x10de,
|
|
subdevice: 0,
|
|
boot0: 0x140000a1,
|
|
boot1: 0,
|
|
boot42: 0x14000000,
|
|
};
|
|
assert_eq!(
|
|
gpu::nv_rs_gpu_prepare_owned(
|
|
pci,
|
|
&info,
|
|
raw.cast(),
|
|
owner.cast(),
|
|
Some(destroy),
|
|
Some(retry),
|
|
Some(lock),
|
|
Some(unlock)
|
|
),
|
|
0
|
|
);
|
|
// Identical PCI tuples deliberately do not collapse distinct storage owners.
|
|
assert_eq!(
|
|
gpu::nv_rs_gpu_set_probe_identity(raw.cast(), 0, 0x1a, 0, 0, 0),
|
|
0
|
|
);
|
|
(raw.cast(), pci, counts)
|
|
}
|
|
}
|
|
unsafe fn remove(raw: *mut gpu::KernelGpu, pci: *mut c_void) {
|
|
unsafe {
|
|
gpu::nv_rs_gpu_mark_removed(raw);
|
|
fixture_pci_remove(pci);
|
|
gpu::nv_rs_gpu_release_driver(raw);
|
|
}
|
|
}
|
|
struct Clock(u64);
|
|
impl Services for Clock {
|
|
fn monotonic_ns(&mut self) -> Result<u64, NvStatus> {
|
|
self.0 += 1;
|
|
Ok(self.0)
|
|
}
|
|
fn delay_us(&mut self, _: u32) -> Result<(), NvStatus> {
|
|
panic!("unexpected delay")
|
|
}
|
|
fn print_node(&mut self, _: &Node) -> Result<(), NvStatus> {
|
|
panic!("unexpected diagnostic")
|
|
}
|
|
fn log_configured_timeout(&mut self, _: u64) -> Result<(), NvStatus> {
|
|
panic!("unexpected expiry")
|
|
}
|
|
fn assert_timeout(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected assert")
|
|
}
|
|
fn schedule_cpu(&mut self) -> Result<NvStatus, NvStatus> {
|
|
panic!("unexpected yield")
|
|
}
|
|
fn log_yield(&mut self) -> Result<(), NvStatus> {
|
|
panic!("unexpected log")
|
|
}
|
|
}
|
|
fn setup() {
|
|
unsafe {
|
|
fixture_cpu_alloc_at(0);
|
|
fixture_cpu_free_at(0);
|
|
fixture_context(0x9888, 0, 0, 0)
|
|
}
|
|
software_manager::initialize(HypervisorType::Unknown).unwrap();
|
|
}
|
|
fn teardown() {
|
|
software_manager::shutdown().unwrap();
|
|
assert_eq!(unsafe { fixture_fault_cpu_live() }, 0);
|
|
assert_eq!(unsafe { fixture_module_pins() }, 0);
|
|
}
|
|
#[test]
|
|
fn distinct_installed_graphs_and_real_scoped_timeout_identity() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
setup();
|
|
unsafe {
|
|
let (a, pa, ca) = make();
|
|
let (b, pb, cb) = make();
|
|
let la = gpu::GpuLease::acquire_pinned(a).unwrap();
|
|
let lb = gpu::GpuLease::acquire_pinned(b).unwrap();
|
|
let mut ga = la.lock_register_io().unwrap();
|
|
let mut gb = lb.lock_register_io().unwrap();
|
|
assert!(device_software::prepare(&la, &mut gb).is_err());
|
|
device_software::prepare(&la, &mut ga).unwrap();
|
|
let calls = fixture_cpu_alloc_calls();
|
|
device_software::prepare(&la, &mut ga).unwrap();
|
|
assert_eq!(fixture_cpu_alloc_calls(), calls);
|
|
device_software::prepare(&lb, &mut gb).unwrap();
|
|
let first = device_software::with(&la, &mut ga, |v| {
|
|
(
|
|
v.timeout_key(),
|
|
v.parent.logical_base().unwrap(),
|
|
v.parent.timeout_fields().unwrap().instance,
|
|
)
|
|
})
|
|
.unwrap();
|
|
let second = device_software::with(&lb, &mut gb, |v| {
|
|
(
|
|
v.timeout_key(),
|
|
v.parent.logical_base().unwrap(),
|
|
v.parent.timeout_fields().unwrap().instance,
|
|
)
|
|
})
|
|
.unwrap();
|
|
assert_ne!(first.0, second.0);
|
|
assert_ne!(first.1, second.1);
|
|
assert_ne!(first.2, second.2);
|
|
assert_eq!(software_manager::shutdown(), Err(-16));
|
|
let registry = thread_state::Registry::new(
|
|
Database {
|
|
setup_flags: 0x87,
|
|
timeout_flags: 8,
|
|
compute_ms: 0,
|
|
non_compute_ms: 0,
|
|
compute_gpu_mask: 0,
|
|
},
|
|
NonZeroU32::new(4).unwrap(),
|
|
)
|
|
.unwrap();
|
|
let mut entry = registry.enter(0, 0, &mut Clock(10)).unwrap().unwrap();
|
|
let mut product = hypervisor::BuildProduct::current().unwrap();
|
|
device_software::with(&la, &mut ga, |mut v| {
|
|
let links = v
|
|
.parent
|
|
.with_image(|i| {
|
|
(
|
|
u64::from_le_bytes(i[0x9d8..0x9e0].try_into().unwrap()),
|
|
u64::from_le_bytes(i[0x1e60..0x1e68].try_into().unwrap()),
|
|
)
|
|
})
|
|
.unwrap();
|
|
assert_eq!(links.0, v.context_logical.get());
|
|
assert_ne!(links.0, links.1);
|
|
v.initialize_timeout(&mut entry, &mut product, &mut Clock(100))
|
|
.unwrap();
|
|
assert_eq!(
|
|
v.with_timeout_data(|d| (d.default_us, d.default_flags, d.gpu))
|
|
.unwrap(),
|
|
(4_000_000, 8, Some(first.0))
|
|
);
|
|
assert!(!la.hardware_ready());
|
|
})
|
|
.unwrap();
|
|
drop(entry);
|
|
drop(registry);
|
|
drop(gb);
|
|
drop(ga);
|
|
remove(a, pa);
|
|
remove(b, pb);
|
|
drop(la);
|
|
drop(lb);
|
|
assert_eq!(ca.destroyed.load(Ordering::Acquire), 1);
|
|
assert_eq!(cb.destroyed.load(Ordering::Acquire), 1);
|
|
}
|
|
teardown();
|
|
}
|
|
#[test]
|
|
fn every_cpu_allocation_failure_retains_until_original_owner_cleanup() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
for failure in 1..=7 {
|
|
setup();
|
|
unsafe {
|
|
let (raw, pci, counts) = make();
|
|
let lease = gpu::GpuLease::acquire_pinned(raw).unwrap();
|
|
let mut guard = lease.lock_register_io().unwrap();
|
|
fixture_cpu_alloc_at(failure);
|
|
let result = device_software::prepare(&lease, &mut guard);
|
|
let calls = fixture_cpu_alloc_calls();
|
|
fixture_cpu_alloc_at(0);
|
|
if failure <= 6 {
|
|
assert!(result.is_err(), "missing failure {failure}");
|
|
assert_eq!(calls, failure);
|
|
} else {
|
|
assert!(result.is_ok());
|
|
assert_eq!(calls, 6);
|
|
}
|
|
let pending = software_manager::with(|m| Ok(m.instances.pending_count())).unwrap();
|
|
assert_eq!(pending, if (3..=6).contains(&failure) { 1 } else { 0 });
|
|
if result.is_err() {
|
|
assert!(device_software::with(&lease, &mut guard, |_| ()).is_err());
|
|
}
|
|
drop(guard);
|
|
remove(raw, pci);
|
|
drop(lease);
|
|
assert_eq!(counts.destroyed.load(Ordering::Acquire), 1);
|
|
assert_eq!(counts.retries.load(Ordering::Acquire), 0);
|
|
}
|
|
teardown();
|
|
}
|
|
}
|
|
#[test]
|
|
fn cleanup_retries_after_children_and_dma_have_already_closed() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
for fail_at in 1..=5 {
|
|
setup();
|
|
unsafe {
|
|
let (raw, pci, counts) = make();
|
|
let lease = gpu::GpuLease::acquire_pinned(raw).unwrap();
|
|
let mut guard = lease.lock_register_io().unwrap();
|
|
device_software::prepare(&lease, &mut guard).unwrap();
|
|
drop(guard);
|
|
fixture_cpu_free_at(fail_at);
|
|
remove(raw, pci);
|
|
drop(lease);
|
|
fixture_cpu_free_at(0);
|
|
if counts.destroyed.load(Ordering::Acquire) == 0 {
|
|
let occupied = software_manager::with(|m| Ok(m.instances.live_count())).unwrap();
|
|
if occupied != 0 {
|
|
assert_eq!(software_manager::shutdown(), Err(-16));
|
|
}
|
|
gpu::nv_rs_gpu_retry_cleanup(raw);
|
|
}
|
|
assert_eq!(counts.destroyed.load(Ordering::Acquire), 1);
|
|
}
|
|
teardown();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn partial_constructor_keeps_instance_through_repeated_close_failure_and_other_device() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
setup();
|
|
unsafe {
|
|
let (a, pa, ca) = make();
|
|
let la = gpu::GpuLease::acquire_pinned(a).unwrap();
|
|
let mut ga = la.lock_register_io().unwrap();
|
|
fixture_cpu_alloc_at(4);
|
|
assert!(device_software::prepare(&la, &mut ga).is_err());
|
|
fixture_cpu_alloc_at(0);
|
|
assert_eq!(
|
|
software_manager::with(|m| Ok(m.instances.pending_count())).unwrap(),
|
|
1
|
|
);
|
|
assert!(device_software::prepare(&la, &mut ga).is_err());
|
|
assert_eq!(fixture_cpu_alloc_calls(), 0);
|
|
drop(ga);
|
|
fixture_cpu_free_at(1);
|
|
remove(a, pa);
|
|
drop(la);
|
|
assert_eq!(ca.retries.load(Ordering::Acquire), 1);
|
|
fixture_cpu_free_at(1);
|
|
gpu::nv_rs_gpu_retry_cleanup(a);
|
|
assert_eq!(ca.retries.load(Ordering::Acquire), 2);
|
|
assert_eq!(
|
|
software_manager::with(|m| Ok(m.instances.pending_count())).unwrap(),
|
|
1
|
|
);
|
|
fixture_cpu_free_at(0);
|
|
let (b, pb, cb) = make();
|
|
let lb = gpu::GpuLease::acquire_pinned(b).unwrap();
|
|
let mut gb = lb.lock_register_io().unwrap();
|
|
device_software::prepare(&lb, &mut gb).unwrap();
|
|
assert_eq!(
|
|
device_software::with(&lb, &mut gb, |v| v
|
|
.parent
|
|
.timeout_fields()
|
|
.unwrap()
|
|
.instance)
|
|
.unwrap(),
|
|
1
|
|
);
|
|
drop(gb);
|
|
remove(b, pb);
|
|
drop(lb);
|
|
assert_eq!(cb.destroyed.load(Ordering::Acquire), 1);
|
|
gpu::nv_rs_gpu_retry_cleanup(a);
|
|
assert_eq!(ca.destroyed.load(Ordering::Acquire), 1);
|
|
}
|
|
teardown();
|
|
}
|
|
#[test]
|
|
fn dma_close_failure_keeps_instance_after_all_cpu_children_closed() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
setup();
|
|
unsafe {
|
|
let (raw, pci, counts) = make();
|
|
let lease = gpu::GpuLease::acquire_pinned(raw).unwrap();
|
|
let mut guard = lease.lock_register_io().unwrap();
|
|
device_software::prepare(&lease, &mut guard).unwrap();
|
|
drop(guard);
|
|
fixture_fail_context_closes(1);
|
|
remove(raw, pci);
|
|
drop(lease);
|
|
assert_eq!(counts.retries.load(Ordering::Acquire), 1);
|
|
assert_eq!(
|
|
software_manager::with(|m| Ok(m.instances.live_count())).unwrap(),
|
|
1
|
|
);
|
|
gpu::nv_rs_gpu_retry_cleanup(raw);
|
|
assert_eq!(counts.destroyed.load(Ordering::Acquire), 1);
|
|
}
|
|
teardown();
|
|
}
|
|
|
|
#[test]
|
|
fn factory_samples_current_boot_registers_before_allocating() {
|
|
let _lock = LOCK.lock().unwrap();
|
|
setup();
|
|
unsafe {
|
|
let (raw, pci, counts) = make();
|
|
let lease = gpu::GpuLease::acquire_pinned(raw).unwrap();
|
|
let mut guard = lease.lock_register_io().unwrap();
|
|
// Probe observed a physical device. The factory must honor the new
|
|
// virtual-device selector and avoid allocating a partial graph.
|
|
fixture_boot_config(2 << 16, 0);
|
|
fixture_cpu_alloc_at(0);
|
|
assert!(device_software::prepare(&lease, &mut guard).is_err());
|
|
assert_eq!(fixture_boot_reads(), 3);
|
|
assert_eq!(fixture_cpu_alloc_calls(), 0);
|
|
|
|
fixture_boot_config(1 << 16, 4);
|
|
let error = device_software::prepare(&lease, &mut guard).unwrap_err();
|
|
assert_eq!(error.kernel, -5);
|
|
assert_eq!(fixture_boot_reads(), 4);
|
|
assert_eq!(fixture_cpu_alloc_calls(), 0);
|
|
for (index, offset) in [0, 0xa00, 4, 0x8f020].into_iter().enumerate() {
|
|
assert_eq!(fixture_boot_offset(index as u32), offset);
|
|
}
|
|
|
|
fixture_boot_config(1 << 16, 0);
|
|
device_software::prepare(&lease, &mut guard).unwrap();
|
|
assert_eq!(fixture_boot_reads(), 4);
|
|
device_software::prepare(&lease, &mut guard).unwrap();
|
|
assert_eq!(fixture_boot_reads(), 4);
|
|
fixture_boot_config(0, 0);
|
|
drop(guard);
|
|
remove(raw, pci);
|
|
drop(lease);
|
|
assert_eq!(counts.destroyed.load(Ordering::Acquire), 1);
|
|
}
|
|
teardown();
|
|
}
|