fix(iommu): give a retired AMD-Vi unit's configuration page and register mapping back to their owner, and recheck the stopped fabric page by page beside a kept unit, where the claim a dropped unit kept withheld every bridge's forwarding and refused the GPU

This commit is contained in:
Tyler Smith committed 2026-11-14 01:33:59 -05:00
1 parent 8828d24d15
commit 97bc0e1ea0
4 files changed
+56 -5

No files matched your search

+17 -2
View File
@@ -311,11 +311,26 @@ struct Cold {
reserved: Vec<(u16, Range<u64>)>,
}
/// Stop a cold unit the policy does not take. False when it did not stop.
/// Stop a cold unit the policy does not take and give back its function's
/// configuration page and its register mapping: a unit dropped with them
/// keeps both claimed, and the check that reopens the PCI bridges then
/// cannot map the configuration space. False when it did not stop or a
/// mapping did not retire.
fn retire(mut unit: Unit<Platform, ObservedConfig>) -> bool {
// SAFETY: no requester was attached and the unit blocks nothing it
// translated; retiring stops it.
unsafe { unit.retire() }.is_ok()
if unsafe { unit.retire() }.is_err() {
return false;
}
let Some(mut inputs) = unit.into_inputs() else {
return false;
};
let mut retired = true;
for mapping in &mut inputs.mappings {
// SAFETY: the stopped unit was the mapping's only user.
retired &= unsafe { mapping.retire() }.is_ok();
}
retired & inputs.function.config().retire().is_ok()
}
/// Take every unit of `table` (an IVRS) the policy allows, each in the
@@ -188,11 +188,13 @@ impl Remappers {
.is_some_and(|original| core::ptr::eq(original, source));
// SAFETY: this startup step precedes every driver and competing PCI
// writer. The same complete blocked remappers and line owners are kept.
// A unit kept in any mode holds its own function's configuration
// page, which the whole-space view cannot map beside it.
let stopped = unsafe {
if amd {
self.recheck_amd_stopped(pci)
} else {
if self.amd.is_empty() {
pci.recheck_stopped()
} else {
self.recheck_amd_stopped(pci)
}
};
if let Err(error) = stopped {
@@ -785,6 +785,25 @@ impl<P: ColdPlatform, C: UnitConfig> Unit<P, C> {
self.engine.retired.set(true);
Ok(())
}
/// After a successful [`Self::retire`]: the unit's function and register
/// mappings, for the caller to retire with their original owner. Left
/// inside a dropped unit they stay claimed, and nothing else can map
/// the configuration space they lie in. `None`, with the unit dropped
/// as before, when it was not retired or its engine is still shared.
pub fn into_inputs(self) -> Option<registers::Inputs<C>> {
let Self { engine, table, .. } = self;
drop(table);
let engine = Rc::try_unwrap(engine).ok()?;
if !engine.retired.get() {
return None;
}
let mut engine = ManuallyDrop::new(engine);
// SAFETY: the sole, retired engine; its `Drop` never runs, so the
// resources are taken exactly once.
let Resources { inspection, .. } = unsafe { ManuallyDrop::take(&mut engine.resources) };
Some(inspection.into_inputs())
}
}
#[cfg(test)]
@@ -380,6 +380,21 @@ fn a_stopped_unit_with_firmware_leftovers_comes_up() {
assert_ne!(peer.register(DEVICE_TABLE), 0x7654_3000 | 0x1ff);
}
/// A retired unit hands its function and register mappings back for their
/// owner to retire; one that still runs keeps them.
#[test]
fn a_retired_unit_gives_its_mappings_back() {
let (mut unit, peer) = model::unit();
// SAFETY: the peer has no requester, producer or other register user.
unsafe { unit.retire() }.unwrap();
let inputs = unit.into_inputs().expect("a retired unit's inputs");
assert!(!inputs.mappings.is_empty());
assert_eq!(peer.live_ram(), 0);
let (unit, _peer) = model::unit();
assert!(unit.into_inputs().is_none());
}
fn unit_with_entries(entries: &[u8]) -> (Unit<Platform>, Peer) {
let (inspection, platform, peer, bytes) = inputs_with_entries(entries);
let ivrs = Ivrs::parse(&bytes, 8).unwrap();