Files
nvdriver/src/nvidia/kernel/timeout_runtime.rs
nvidia-driver-rs 4526e6a816 Baseline: validated 30/30 tree with passing Level 7 hardware regression
- Updated tools/post_reboot.sh to check kernel oops taint bit and stuck driver tasks instead of <86400s uptime
- Level 7 hardware regression passed on 0000:b3:00.0 (module 37e6fa430835eee2b426019db3ad70eeb7a9e9070c09c84bd9b973cf56cbe529, 107 inventory words, 0 static mismatches, CUDA baseline verified before and after)
- Updated .gitignore to exclude build archives (*.a, *.rlib), __pycache__, and raw trace dumps
2026-11-23 23:48:38 +00:00

426 lines
16 KiB
Rust

//! Serialized CPU timeout initialization over the actual canonical GPU owner.
//! This configures software deadlines only; it never admits hardware engines.
use crate::{pf_software::PfSoftware, thread_state::Entry};
use core::num::NonZeroU64;
use nv_rs_abi::NvStatus;
use nv_rs_core::gpu::{
thread_timeout::{Database, Services},
timeout::{
self,
config::{self, Defaults, Hypervisor, OsParams, Params},
GpuId,
},
};
/// Actual logical-object lookup, retaining the same GPU for every resolved key.
/// # Safety
/// Implementations own/pin the real GPU graph throughout this borrow, reject
/// foreign/retired logical objects, and never derive keys from PCI/BOOT IDs.
/// No production implementation exists until the parent owns that association.
pub unsafe trait Identity {
fn resolve(&self, logical: NonZeroU64) -> Result<GpuId, NvStatus>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Phase {
Fresh,
Configuring,
Configured,
Unavailable,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
Parent(i32),
Identity(NvStatus),
Foreign,
Registry(crate::registry::Error),
Service(NvStatus),
Quarantined,
}
/// Pointer-free persistent configuration. The actual per-device owner embeds
/// this value; dropping a scoped operation never clears retained failure state.
pub struct Record {
phase: Phase,
owner: Option<NonZeroU64>,
gpu: Option<GpuId>,
defaults: Option<Defaults>,
pending_database: Option<Database>,
last: Option<Error>,
last_base_error: Option<crate::registry::Error>,
}
impl Default for Record {
fn default() -> Self {
Self::new()
}
}
impl Record {
pub const fn new() -> Self {
Self {
phase: Phase::Fresh,
owner: None,
gpu: None,
defaults: None,
pending_database: None,
last: None,
last_base_error: None,
}
}
pub const fn phase(&self) -> Phase {
self.phase
}
pub const fn last_error(&self) -> Option<Error> {
self.last
}
pub const fn last_base_error(&self) -> Option<crate::registry::Error> {
self.last_base_error
}
pub const fn partial_defaults(&self) -> Option<Defaults> {
self.defaults
}
/// Failed publication retains the actual already-mutated database here.
pub fn pending_database(&self) -> Option<&Database> {
self.pending_database.as_ref()
}
/// Borrow this exact persistent record and retained identity for one
/// operation. Binding never resets configuration, quarantine or errors.
/// The actual owning GPU mutex must serialize mutable record/parent access.
pub fn bind<'a, I: Identity>(&'a mut self, identity: &'a I) -> Operation<'a, I> {
Operation {
record: self,
identity,
}
}
}
/// A nonescaping operational borrow, not an embedded self-reference or a lease.
/// The concrete Identity provider pins its actual GPU through this borrow.
pub struct Operation<'a, I: Identity> {
record: &'a mut Record,
identity: &'a I,
}
impl<I: Identity> Operation<'_, I> {
/// Inspect initialized Data only while its actual GPU association is pinned.
/// This does not prove GPU readiness or authorize any register access.
pub fn with_data<R>(
&self,
parent: &PfSoftware,
read: impl FnOnce(&timeout::Data) -> R,
) -> Result<R, Error> {
if self.record.phase != Phase::Configured {
return Err(Error::Quarantined);
}
let logical = parent.logical_base().map_err(Error::Parent)?;
if Some(logical) != self.record.owner
|| Some(self.identity.resolve(logical).map_err(Error::Identity)?) != self.record.gpu
{
return Err(Error::Foreign);
}
let fields = parent.timeout_fields().map_err(Error::Parent)?;
if fields.gpu_link != logical.get() {
return Err(Error::Foreign);
}
let mut defaults = fields.defaults;
defaults.gpu = self.record.gpu;
Ok(read(&defaults.timeout_data()))
}
/// Execute059508 with real named Linux/NBSI reads and mandatory diagnostics.
/// # Safety
/// Same retained GPU/configuration serialization as initialize; base and
/// NBSI providers belong to this actual canonical parent and system graph.
pub unsafe fn apply_overrides<D: Diagnostics>(
&mut self,
parent: &mut PfSoftware,
base: &mut crate::registry::LinuxRegistry,
nbsi: &mut dyn crate::registry::NbsiDwordRegistry,
diagnostics: &mut D,
) -> Result<(), Error> {
let mut defaults = self
.with_data(parent, |_| ())
.and_then(|()| parent.timeout_fields().map_err(Error::Parent))?
.defaults;
let gpu = self.record.gpu.ok_or(Error::Foreign)?;
defaults.gpu = Some(gpu);
let mut internal = parent
.timeout_fields()
.map_err(Error::Parent)?
.override_internal_ms;
let mut io = RegistryIo {
parent,
base,
nbsi,
diagnostics,
gpu,
last_base_error: None,
};
let result = config::registry::apply(&mut io, gpu, &mut defaults, &mut internal);
self.record.defaults = Some(defaults);
self.record.last_base_error = io.last_base_error;
// The core checkpoints before each subsequent service boundary. Final
// commit also retains mutation made by a callback that returned Err.
let committed = if defaults.gpu != Some(gpu) {
Err(-22)
} else {
unsafe { io.parent.commit_timeout_defaults(&defaults) }
}
.and_then(|()| unsafe { io.parent.commit_timeout_override_internal(internal) })
.map_err(Error::Parent);
let result = committed.and_then(|()| {
result.map_err(|status| {
io.last_base_error
.map_or(Error::Service(status), Error::Registry)
})
});
if let Err(error) = result {
self.record.phase = Phase::Unavailable;
self.record.last = Some(error);
}
result
}
/// Execute059485 independently, without inventing a caller-stage order.
/// # Safety
/// Same ownership/serialization contract as apply_overrides. current must
/// be the actual calling thread; its registry is the sole publication target.
pub unsafe fn apply_setup_override<D: Diagnostics>(
&mut self,
parent: &mut PfSoftware,
current: &mut Entry<'_>,
base: &mut crate::registry::LinuxRegistry,
nbsi: &mut dyn crate::registry::NbsiDwordRegistry,
diagnostics: &mut D,
) -> Result<(), Error> {
self.with_data(parent, |_| ())?;
current.snapshot().map_err(Error::Service)?;
let gpu = self.record.gpu.ok_or(Error::Foreign)?;
let mut database = current.registry().timeout_database();
let mut io = RegistryIo {
parent,
base,
nbsi,
diagnostics,
gpu,
last_base_error: None,
};
let applied =
config::registry::apply_setup(&mut io, gpu, &mut database).map_err(|status| {
io.last_base_error
.map_or(Error::Service(status), Error::Registry)
});
self.record.last_base_error = io.last_base_error;
self.record.pending_database = Some(database);
let result = current
.registry()
.replace_timeout_database(database)
.map_err(Error::Service)
.and_then(|()| {
self.record.pending_database = None;
applied
});
if let Err(error) = result {
self.record.phase = Phase::Unavailable;
self.record.last = Some(error);
}
result
}
/// Whole059507 composition with actual canonical writes before child calls.
/// Outer failures quarantine this configuration and preserve partial fields.
/// # Safety
/// Hold the actual parent GPU/RM configuration serialization through the
/// entire call, including current-thread/database updates. Call only at the
/// actual parent factory boundary corresponding to031028→059507. Identity owns
/// this exact canonical parent's association; no competing mode/reset writes.
pub unsafe fn initialize<H: Hypervisor, S: Services>(
&mut self,
parent: &mut PfSoftware,
current: &mut Entry<'_>,
hypervisor: &mut H,
services: &mut S,
) -> Result<(), Error> {
if self.record.phase == Phase::Unavailable || self.record.phase == Phase::Configuring {
return Err(Error::Quarantined);
}
let result = (|| {
let logical = parent.logical_base().map_err(Error::Parent)?;
let gpu = self.identity.resolve(logical).map_err(Error::Identity)?;
if self.record.owner.is_some_and(|owner| owner != logical)
|| self.record.gpu.is_some_and(|old| old != gpu)
{
return Err(Error::Foreign);
}
let initial = parent.timeout_fields().map_err(Error::Parent)?;
if initial.gpu_link != 0 && initial.gpu_link != logical.get() {
return Err(Error::Foreign);
}
// Validate actual current OS-thread identity before modifying its DB.
current.snapshot().map_err(Error::Service)?;
self.record.owner = Some(logical);
self.record.gpu = Some(gpu);
self.record.phase = Phase::Configuring;
let mut defaults = initial.defaults;
defaults.gpu = Some(gpu);
self.record.defaults = Some(defaults);
unsafe { parent.commit_timeout_defaults(&defaults) }.map_err(Error::Parent)?;
let params = config::linux_params(
parent.timeout_fields().map_err(Error::Parent)?.policy,
hypervisor,
)
.map_err(Error::Service)?;
defaults.flags = params.flags;
defaults.scale = params.scale;
if !defaults.overridden {
defaults.default_us = params.timeout_us;
defaults.reset_us = params.timeout_us;
defaults.scaled = false;
}
if parent
.timeout_fields()
.map_err(Error::Parent)?
.force_os_timer
{
defaults.flags = timeout::OS_TIMER;
}
if !defaults.scaled {
defaults.default_us = defaults.default_us.wrapping_mul(defaults.scale);
defaults.scaled = true;
}
self.record.defaults = Some(defaults);
unsafe { parent.commit_timeout_defaults(&defaults) }.map_err(Error::Parent)?;
let fields = parent.timeout_fields().map_err(Error::Parent)?;
let mut database = current.registry().timeout_database();
let mut os = Query {
parent,
hypervisor,
gpu,
};
let configured = config::configure_thread(
&mut database,
&mut os,
gpu,
config::ThreadPolicy {
instance: fields.instance,
compute_refs: fields.policy.compute_refs,
scale: fields.defaults.scale,
},
defaults.default_us,
defaults.flags,
);
self.record.pending_database = Some(database);
current
.registry()
.replace_timeout_database(database)
.map_err(Error::Service)?;
self.record.pending_database = None;
let _executed = configured.map_err(Error::Service)?;
let _executed = current.reset_timeout(services).map_err(Error::Service)?;
self.record.phase = Phase::Configured;
Ok(())
})();
if let Err(error) = result {
self.record.phase = Phase::Unavailable;
self.record.last = Some(error);
}
result
}
}
struct Query<'a, H> {
parent: &'a PfSoftware,
hypervisor: &'a mut H,
gpu: GpuId,
}
impl<H: Hypervisor> OsParams for Query<'_, H> {
fn params(&mut self, gpu: GpuId) -> Result<Params, NvStatus> {
if gpu != self.gpu {
return Err(NvStatus(0x40));
}
config::linux_params(
self.parent
.timeout_fields()
.map_err(|_| NvStatus(0x40))?
.policy,
self.hypervisor,
)
}
}
/// Explicit constructed-table prefix only; actual table-init invalidates it.
pub struct InitialNbsi<'a>(pub &'a mut crate::system_platform::PlatformReference);
impl crate::registry::NbsiDwordRegistry for InitialNbsi<'_> {
fn read_dword_nbsi(
&mut self,
parent: &PfSoftware,
key: &core::ffi::CStr,
) -> Result<nv_rs_core::mmu::init::RegistryRead, NvStatus> {
self.0.read_initial(parent, key.to_bytes())
}
}
/// Actual logging/assertion services. No executed success is supplied here.
pub trait Diagnostics {
fn diagnostic(
&mut self,
parent: &PfSoftware,
defaults: &mut Defaults,
event: config::registry::Diagnostic,
) -> Result<(), NvStatus>;
fn setup_diagnostic(&mut self, parent: &PfSoftware, old: u32, new: u32)
-> Result<(), NvStatus>;
}
struct RegistryIo<'a, D> {
parent: &'a mut PfSoftware,
base: &'a mut crate::registry::LinuxRegistry,
nbsi: &'a mut dyn crate::registry::NbsiDwordRegistry,
diagnostics: &'a mut D,
gpu: GpuId,
last_base_error: Option<crate::registry::Error>,
}
impl<D> config::registry::Read for RegistryIo<'_, D> {
fn read(
&mut self,
gpu: GpuId,
key: config::registry::Key,
) -> Result<nv_rs_core::mmu::init::RegistryRead, NvStatus> {
if gpu != self.gpu {
return Err(NvStatus(0x40));
}
let mut bytes = [0u8; 64];
let name = key.name().as_bytes();
if name.len() >= bytes.len() {
return Err(NvStatus(0x40));
}
bytes[..name.len()].copy_from_slice(name);
let name = core::ffi::CStr::from_bytes_until_nul(&bytes).map_err(|_| NvStatus(0x40))?;
let mut complete =
crate::registry::CompleteDwordRegistry::new(self.base, self.parent, self.nbsi);
let result = complete.read(name);
self.last_base_error = complete.last_base_error();
result
}
}
impl<D: Diagnostics> config::registry::Io for RegistryIo<'_, D> {
fn checkpoint(&mut self, defaults: &Defaults, internal: u32) -> Result<(), NvStatus> {
if defaults.gpu != Some(self.gpu) {
return Err(NvStatus(0x40));
}
// Safety: public entry requires retained same-owner serialization.
unsafe { self.parent.commit_timeout_defaults(defaults) }
.and_then(|()| unsafe { self.parent.commit_timeout_override_internal(internal) })
.map_err(|_| NvStatus(0x40))
}
fn diagnostic(
&mut self,
defaults: &mut Defaults,
event: config::registry::Diagnostic,
) -> Result<(), NvStatus> {
self.diagnostics.diagnostic(self.parent, defaults, event)
}
}
impl<D: Diagnostics> config::registry::SetupIo for RegistryIo<'_, D> {
fn setup_diagnostic(&mut self, old: u32, new: u32) -> Result<(), NvStatus> {
self.diagnostics.setup_diagnostic(self.parent, old, new)
}
}
/// This receipt exists only when the actual driver build product was resolved.
impl Hypervisor for crate::hypervisor::BuildProduct {
fn is_vgx(&mut self) -> Result<bool, NvStatus> {
Ok(self.is_vgx_hyper())
}
}