diff --git a/drivers/gpu/drm/asahi/g16/engine.rs b/drivers/gpu/drm/asahi/g16/engine.rs index 9021a4be96da3..9c7fa3d7bf4e2 100644 --- a/drivers/gpu/drm/asahi/g16/engine.rs +++ b/drivers/gpu/drm/asahi/g16/engine.rs @@ -2270,19 +2270,20 @@ impl Firmware { Ok(waits) } - /// Once per publication pass, before anything is posted. A compute UMA manager left bound - /// under the in-flight hold keeps its allocator cursors only in the hardware - /// (`Slots::bind_compute_uma`), so a GPU power-off drops them. + /// Once per publication pass, before anything is posted. The firmware saves a compute UMA + /// manager's allocator cursors into its record when it drains (`Slots::bind_compute_uma`). + /// A GPU power-off between its jobs still makes the host rebuild the record rather than rely + /// on that save. /// - /// Under the boot policy that holds the GPU powered (`Session::idle_power_off` false), - /// the GPU cannot lose a bound manager while idle: nothing is invalidated here, and only a - /// system sleep or a recovery invalidates. When the firmware may power the GPU off on its - /// own, there is no qualified witness of a power loss yet (such as a firmware power-loss - /// generation, or a power hold spanning this check through the firmware's consumption of - /// the next kick). So every pass that finds nothing in flight, after retiring what it can, - /// with a shared manager held, invalidates them all, and their next commands rebuild their - /// records. A flight in flight keeps the GPU busy, so a pass with work in flight - /// invalidates nothing, nor does one while no shared manager is held (render-only work). + /// Under the boot policy that holds the GPU powered (`Session::idle_power_off` false) + /// nothing is invalidated here, and only a system sleep or a recovery invalidates. When the + /// firmware may power the GPU off on its own, there is no qualified witness of a power loss + /// yet (such as a firmware power-loss generation, or a power hold spanning this check through + /// the firmware's consumption of the next kick). So every pass that finds nothing in flight, + /// after retiring what it can, with a shared manager used since the last invalidation, + /// invalidates them all, and their next commands rebuild their records. A flight in flight + /// keeps the GPU busy, so a pass with work in flight invalidates nothing, nor does one + /// without shared compute (render-only work). fn note_gpu_idle_if_any(&mut self) { if !self.session.idle_power_off() || !self.engine.flights.is_empty() @@ -3559,15 +3560,15 @@ impl Firmware { if f.claims.slot_count != 0 { let tail_sub = f.csubmitted[(f.claims.slot_count - 1) as usize]; shared.submitted.store(tail_sub, Ordering::Release); - // The in-flight hold keeps the manager bound between its jobs: *counter - // stays one past the submissions, so the firmware does not save and - // unbind the manager when this command finishes, and its next job finds - // the cursors still in the hardware (`Slots::bind_compute_uma`). - // `retire_oldest` writes the counter back once the manager drained, so - // an idle manager's counter equals COMPLETED, as in the macOS 26B123 - // procs-d8 snapshot (all eight managers). `prefix_compute` reserved the - // count. - shared.counter.w64(0, tail_sub.saturating_add(1))?; + // *counter is the submission count. A later queued command keeps the + // manager bound; its last command completes with COMPLETED equal to + // *counter, so the firmware saves and unbinds it. Never hold the counter + // past the submissions: that leaves a drained manager bound after its VM + // is idle or gone, and the slot's next user (a private SCS unit, or + // another VM) then displaces it without a save (linux#3707). An idle + // manager's counter equals COMPLETED on macOS 26B123 too (procs-d8 + // snapshot, all eight managers). + shared.counter.w64(0, tail_sub)?; } } else { for m in 0..f.claims.slot_count as usize { @@ -3803,8 +3804,6 @@ impl Firmware { let base = shared.submitted.load(Ordering::Acquire); pf.uma_delta = pf.uma_delta.checked_add(1).ok_or(EOVERFLOW)?; let submitted = base.checked_add(pf.uma_delta).ok_or(EOVERFLOW)?; - // Room for the in-flight hold on top of this submission (publication below). - submitted.checked_add(1).ok_or(EOVERFLOW)?; ( base, submitted, diff --git a/drivers/gpu/drm/asahi/g16/poll.rs b/drivers/gpu/drm/asahi/g16/poll.rs index 27b132a3b75c2..dede450c18ac8 100644 --- a/drivers/gpu/drm/asahi/g16/poll.rs +++ b/drivers/gpu/drm/asahi/g16/poll.rs @@ -1662,35 +1662,12 @@ impl Firmware { } if f.computes != 0 { // UMA residency: this manager's commands (or this flight's private units) are - // done. A drained manager drops `*counter` from the in-flight hold to its - // submissions, so an idle manager's counter equals COMPLETED; it stays bound - // until another record or descriptor takes the slot (`Slots::bind_compute_uma`). - let drained = f.job.resources.shared_compute_uma().is_some_and(|shared| { + // done. The firmware saves and unbinds a manager with its last command. + if let Some(shared) = f.job.resources.shared_compute_uma() { self.slots - .retire_compute_uma(×tamp_claims, shared, f.computes) - }); - self.slots.retire_private_uma(×tamp_claims); - let released = f - .job - .resources - .shared_compute_uma() - .filter(|_| drained) - .map(|shared| { - shared - .counter - .w64(0, shared.submitted.load(Ordering::Acquire)) - }); - if let Some(Err(error)) = released { - self.retain(&f.job); - complete(done, &f.job, Err(error)); - for j2 in f.group.iter() { - self.retain(j2); - complete(done, j2, Err(error)); - } - self.fail_stop(error, done); - core::mem::forget(f); - return; + .retire_compute_uma(×tamp_claims, shared, f.computes); } + self.slots.retire_private_uma(×tamp_claims); let p = (f.compute_pipe as usize).min(super::hw::sizes::COMPUTE_PIPES - 1); for &stamp in &f.cstamp[..(f.computes as usize).min(f.cstamp.len())] { self.recovery.last_retired.push(p, stamp); diff --git a/drivers/gpu/drm/asahi/g16/resources.rs b/drivers/gpu/drm/asahi/g16/resources.rs index e683521cac747..23ea427640879 100644 --- a/drivers/gpu/drm/asahi/g16/resources.rs +++ b/drivers/gpu/drm/asahi/g16/resources.rs @@ -946,19 +946,17 @@ impl SharedComputeUmaInner { self.outstanding.fetch_add(count, Ordering::AcqRel); } - /// `count` commands retired cleanly; returns whether that drained the manager. - pub(crate) fn note_retired(&self, count: u32) -> bool { + /// `count` commands retired cleanly. + pub(crate) fn note_retired(&self, count: u32) { let left = self.outstanding().saturating_sub(count); self.outstanding.store(left, Ordering::Release); - left == 0 } /// Initialises slot record `rec` for this manager, which has no command in flight: the /// manager image with the allocator cursors at their seed and COMPLETED at the host's /// submission count, so the firmware's next completion lines up with the next submission. - /// The firmware never writes a displaced or lost binding back (it saves a manager only at a - /// completion with COMPLETED equal to `*counter`, which the in-flight hold prevents), so the - /// hardware cursors of a manager that ran before are gone. Its pool is drained, and cursors + /// The cursors the firmware saved into a record another manager displaced since are not + /// host-readable, so they are not carried over. The pool is drained, and cursors /// at their seed describe its freelist exactly while the freelist still holds the seed /// ([`super::slots::uma_seed_intact`]) and the slot ledger's seed generation `seed_gen` /// has not advanced since the last seed or check. Otherwise seed the freelist afresh with diff --git a/drivers/gpu/drm/asahi/g16/slots.rs b/drivers/gpu/drm/asahi/g16/slots.rs index 446da10d89ae8..070f12fb7c0a2 100644 --- a/drivers/gpu/drm/asahi/g16/slots.rs +++ b/drivers/gpu/drm/asahi/g16/slots.rs @@ -788,8 +788,8 @@ enum UmaBound { Private, } -/// The shared manager bound in a slot. Its commands run under the in-flight hold, so the -/// firmware never saves it: its allocator cursors are only in the hardware, never in its record. +/// The shared manager that last ran in a slot: its record holds its allocator cursors, saved when +/// its last command completed, and nothing replaced it in the slot since. #[derive(Copy, Clone)] struct UmaLive { id: u64, @@ -894,17 +894,17 @@ pub(crate) struct ComputeOwners { /// `uma_mgr_table[COMPUTE_UMA_SLOT + slot]`. retained_uma: [Option>; sizes::COMPUTE_UMA_SLOTS], /// Compute UMA residency per hardware slot ([`Slots::bind_compute_uma`]): what the firmware - /// slot table names, the shared manager bound there with its cursors in the hardware, and - /// the shared and private (SCS) commands in flight there. Only identities and counts are - /// kept: no VM storage stays alive for the ledger. + /// slot table named last, the shared manager that ran there last, and the shared and private + /// (SCS) commands in flight there. Only identities and counts are kept: no VM storage stays + /// alive for the ledger. uma_bound: [UmaBound; sizes::COMPUTE_UMA_SLOTS], uma_live: [Option; sizes::COMPUTE_UMA_SLOTS], uma_shared_flights: [u32; sizes::COMPUTE_UMA_SLOTS], uma_private_flights: [u32; sizes::COMPUTE_UMA_SLOTS], - /// Advanced whenever the hardware allocator state of bound managers may be gone + /// Advanced whenever the saved cursors of shared managers are no longer used /// ([`Slots::invalidate_compute_uma`]). uma_live_gen: u64, - /// A shared manager was bound since the last advance of [`Self::uma_live_gen`]. + /// A shared manager ran since the last advance of [`Self::uma_live_gen`]. uma_held: bool, /// Advanced whenever no drained shared freelist is trusted to hold its seed, whatever it /// shows ([`super::resources::SharedComputeUmaInner::init_record`]): after a system sleep @@ -2069,17 +2069,16 @@ impl Slots { /// retirement on the slot repolls publication). [`Self::note_compute_uma_published`] /// records the use once it is visible. /// - /// Every shared command is published under the in-flight hold, so the firmware never saves a - /// shared manager: while it is bound its allocator cursors are only in the hardware, and - /// binding another record or descriptor in the slot drops them. So, in order: - /// - The manager is still bound in this residency generation (its own commands are in - /// flight, or it drained and nothing since could have dropped the hardware state: see + /// The firmware saves and unbinds a shared manager when its last command completes, so a + /// slot whose commands all retired holds no binding: its next user binds afresh. So, in order: + /// - The manager's record is current in this residency generation (its own commands are in + /// flight, or it drained and nothing since displaced or invalidated it: see /// [`Self::invalidate_compute_uma`]): keep its record and write nothing. /// - Commands of another user of the slot (a private SCS unit, or this manager while its /// binding is uncertain) are in flight: wait. Another VM's manager never runs in the slot /// at the same time (the engine's UMA conflict check). - /// - Otherwise its cursors are gone (displaced, or lost over an idle power-down, a sleep or - /// a recovery): rebuild a record the slot table does not name + /// - Otherwise its saved cursors are not used (displaced, or an idle power-down, a sleep or + /// a recovery since): rebuild a record the slot table did not name last /// ([`super::resources::SharedComputeUmaInner::init_record`] restarts the cursors and /// COMPLETED together, and reseeds the drained freelist only if it may no longer hold /// its seed). @@ -2145,7 +2144,7 @@ impl Slots { } /// Records a visible publication of private (SCS) commands in the slots `f.uma_private`: - /// each binds its own descriptor there, displacing the shared manager bound there. + /// each binds its own descriptor there, after the shared manager there was saved. pub(crate) fn note_private_uma_published(&mut self, f: &Claims) { let c = &mut self.compute; let mut mask = f.uma_private & COMPUTE_UMA_MASK; @@ -2158,26 +2157,23 @@ impl Slots { } } - /// Records the clean retirement of a flight whose `count` commands used `shared`. Returns - /// whether that drained the manager, so its counter has to drop from the in-flight hold to - /// its submission count. The manager stays bound, with its cursors in the hardware, until - /// another record or descriptor takes the slot. Aborted flights never come here: their slot - /// stays quarantined with the manager and their commands stay counted for good, so - /// `publish_prefix_body` fails a job naming a quarantined slot before it waits on these - /// counts, and no shared or private command publishes there again. + /// Records the clean retirement of a flight whose `count` commands used `shared`. Aborted + /// flights never come here: their slot stays quarantined with the manager and their commands + /// stay counted for good, so `publish_prefix_body` fails a job naming a quarantined slot + /// before it waits on these counts, and no shared or private command publishes there again. pub(crate) fn retire_compute_uma( &mut self, f: &Claims, shared: &super::resources::SharedComputeUma, count: u32, - ) -> bool { + ) { let (slot, rec) = (f.uma_slot as usize, f.uma_rec as usize); if rec >= UMA_RECORDS || slot >= sizes::COMPUTE_UMA_SLOTS || slot != shared.uma_slot_idx { - return false; + return; } let c = &mut self.compute; c.uma_shared_flights[slot] = c.uma_shared_flights[slot].saturating_sub(count); - shared.note_retired(count) + shared.note_retired(count); } /// Records the clean retirement of a flight with private (SCS) commands in the slots @@ -2196,9 +2192,8 @@ impl Slots { } } - /// The hardware allocator state of bound shared managers may be gone: the GPU may have - /// powered down while idle. Trust none of them: each rebuilds a record the slot table does - /// not name once its own commands have retired. + /// The GPU may have powered down while idle. Trust no shared manager's saved cursors: each + /// rebuilds a record the slot table did not name last once its own commands have retired. pub(crate) fn invalidate_compute_uma(&mut self) { let c = &mut self.compute; c.uma_live_gen = c.uma_live_gen.wrapping_add(1); @@ -2218,8 +2213,7 @@ impl Slots { self.compute.uma_seed_gen = self.compute.uma_seed_gen.wrapping_add(1); } - /// Whether a shared manager may be bound with its cursors only in the hardware: one was - /// bound since the last [`Self::invalidate_compute_uma`]. + /// Whether a shared manager ran since the last [`Self::invalidate_compute_uma`]. pub(crate) fn compute_uma_held(&self) -> bool { self.compute.uma_held } diff --git a/tools/testing/selftests/drivers/gpu/asahi-m4/test-publication.py b/tools/testing/selftests/drivers/gpu/asahi-m4/test-publication.py index 61f76d1c23deb..3fc484dd3320e 100644 --- a/tools/testing/selftests/drivers/gpu/asahi-m4/test-publication.py +++ b/tools/testing/selftests/drivers/gpu/asahi-m4/test-publication.py @@ -179,9 +179,14 @@ def item(declaration): ), 'the final boot control must follow the power policy the idle check reads' note_gpu_idle_fn = item('fn note_gpu_idle_if_any(') assert 'self.session.idle_power_off()' in note_gpu_idle_fn, note_gpu_idle_fn -# A shared manager runs under the in-flight hold, and no seeded freelist is trusted past a firmware -# restart or a recovery: both callers come before the firmware can run work again. -assert publish_prefix_body_fn.count('shared.counter.w64(0, tail_sub.saturating_add(1))?;') == 1 +# A shared manager's counter never runs past its submissions: its last command has to complete with +# COMPLETED equal to it, so that the firmware unbinds the manager before its slot changes hands +# (linux#3707). Retirement writes no counter. +assert publish_prefix_body_fn.count('shared.counter.w64(0, ') == 1 +assert publish_prefix_body_fn.count('shared.counter.w64(0, tail_sub)?;') == 1 +assert '.counter' not in item_in((production.parent / 'poll.rs').read_text(), 'fn retire_oldest(') +# No seeded freelist is trusted past a firmware restart or a recovery: both callers come before the +# firmware can run work again. sleep_restart_fn = item_in((production.parent / 'sleep.rs').read_text(), 'fn sleep_restart(') assert ( sleep_restart_fn.index('self.slots.restart_compute_uma();') diff --git a/tools/testing/selftests/drivers/gpu/asahi-m4/test-resources.py b/tools/testing/selftests/drivers/gpu/asahi-m4/test-resources.py index 10e8cf215f79a..c61cc01638602 100755 --- a/tools/testing/selftests/drivers/gpu/asahi-m4/test-resources.py +++ b/tools/testing/selftests/drivers/gpu/asahi-m4/test-resources.py @@ -4520,8 +4520,8 @@ def module(text, name): // Compute UMA residency (slots.rs `bind_compute_uma`): the 26B123 firmware binds a manager by // loading its record only, never writes a displaced binding back, and saves a manager (copying - // the hardware cursors into its record) only at a completion with COMPLETED == *counter, which - // the in-flight hold prevents for shared managers. + // the hardware cursors into its record) only at a completion with COMPLETED == *counter. The + // counter is the submission count, so a drained manager is saved and unbound. mod uma_residency { use super::*; use std::sync::atomic::{AtomicU32, Ordering}; @@ -4545,7 +4545,7 @@ def module(text, name): let u = SharedComputeUma::new(&g16::object::Factory, &bind, &v.vm, &live, slot, 0).unwrap(); (u, v, bind, live) } - // prefix_compute + the publication closure (counter under the in-flight hold) + + // prefix_compute + the publication closure (counter at the submissions) + // note_compute_uma_published. pub(super) fn publish(s: &mut Slots, u: &Uma, count: u32) -> Option { let rec = s.bind_compute_uma(u).unwrap()?; @@ -4553,19 +4553,15 @@ def module(text, name): c.uma_rec = rec as u8; c.uma_slot = u.uma_slot_idx as u8; let sub = u.submitted.load(Ordering::Acquire).checked_add(u64::from(count)).unwrap(); - let held = sub.checked_add(1).unwrap(); u.submitted.store(sub, Ordering::Release); - u.counter.w64(0, held).unwrap(); + u.counter.w64(0, sub).unwrap(); s.note_compute_uma_published(&c, u, count); Some(c) } - // poll::retire_oldest: a drained manager drops its counter to its submissions. + // poll::retire_oldest; whether that drained the manager. pub(super) fn retire(s: &mut Slots, u: &SharedComputeUma, c: &Claims, count: u32) -> bool { - let drained = s.retire_compute_uma(c, u, count); - if drained { - u.counter.w64(0, u.submitted.load(Ordering::Acquire)).unwrap(); - } - drained + s.retire_compute_uma(c, u, count); + u.outstanding() == 0 } // The record the host built for `u` before publishing `published` commands: the manager // image with the cursors at their seed and COMPLETED at the earlier submissions. @@ -4617,16 +4613,15 @@ def module(text, name): let mut s = Slots::new().unwrap(); let (a, _va, _ba, _la) = new_uma(3001, 5); let (b, _vb, _bb, _lb) = new_uma(3002, 5); - // A runs under the hold: the firmware keeps it bound and never saves it. Its - // returns wrap around the ring into the seed. + // A's returns wrap around the ring into the seed. let ca = publish(&mut s, &a, 2).unwrap(); let ra = ca.uma_rec; assert_rebuilt(&a, ra as usize, 2); - assert_eq!(rd64(&a.counter, 0), 3); + assert_eq!(rd64(&a.counter, 0), 2); wrap_on_gpu(&a, 64); a.page_list.w64(0, 0).unwrap(); assert!(retire(&mut s, &a, &ca, 2), "drained"); - assert_eq!(rd64(&a.counter, 0), 2, "counter back at the submissions"); + assert_eq!(rd64(&a.counter, 0), 2, "counter at the submissions"); // B (a 33rd VM sharing the slot) binds a record the slot table does not name, // displacing A's binding: A's cursors are lost with it. let cb = publish(&mut s, &b, 1).unwrap(); @@ -4643,7 +4638,7 @@ def module(text, name): assert_eq!(writes(&a.pool_state) - before.0, a.seeds as usize * 8 + 8, "whole freelist reseeded"); assert_eq!(writes(&a.page_list) - before.1, (a.seeds / g16::rt::SEEDS_PER_ROW) as usize * 8, "every pool row"); assert_eq!(rd64(&a.pool_state, (g16::rt::freelist_records(a.seeds) as usize - 1) * 8), 0); - assert_eq!(rd64(&a.counter, 0), 4); + assert_eq!(rd64(&a.counter, 0), 3); assert!(retire(&mut s, &a, &ca2, 1)); // B returns after A: rebuilt as well. Its returns stayed short of the end of the // ring, so its freelist still holds the seed and only its record and rows are @@ -4669,17 +4664,17 @@ def module(text, name): let c2 = publish(&mut s, &a, 3).unwrap(); assert_eq!(c2.uma_rec, c1.uma_rec); assert_eq!(uma_writes(&a), w); - assert_eq!(rd64(&a.counter, 0), 5); + assert_eq!(rd64(&a.counter, 0), 4); assert!(!retire(&mut s, &a, &c1, 1)); - assert_eq!(rd64(&a.counter, 0), 5, "still held while commands run"); + assert_eq!(rd64(&a.counter, 0), 4, "past COMPLETED while commands run"); assert!(retire(&mut s, &a, &c2, 3)); assert_eq!(rd64(&a.counter, 0), 4); - // Drained and still bound in this residency generation: one job at a time keeps - // the binding with no host write to the record, freelist or pool rows. + // Drained and current in this residency generation: one job at a time keeps + // the record with no host write to the record, freelist or pool rows. for n in 0..4u64 { let c = publish(&mut s, &a, 1).unwrap(); assert_eq!(c.uma_rec, c1.uma_rec); - assert_eq!(rd64(&a.counter, 0), 6 + n); + assert_eq!(rd64(&a.counter, 0), 5 + n); assert!(retire(&mut s, &a, &c, 1)); assert_eq!(rd64(&a.counter, 0), 5 + n); } @@ -4922,18 +4917,18 @@ def module(text, name): let mut f1 = publish(&mut s, &a, 1).unwrap(); f1.uma_private = 1u32 << 6; s.note_private_uma_published(&f1); - assert_eq!(rd64(&a.counter, 0), 2); + assert_eq!(rd64(&a.counter, 0), 1); // Flight 2: one more shared command of A. let f2 = publish(&mut s, &a, 1).unwrap(); assert_eq!(f2.uma_rec, f1.uma_rec); assert_eq!(a.outstanding(), 2); - assert_eq!(rd64(&a.counter, 0), 3); + assert_eq!(rd64(&a.counter, 0), 2); assert!(!s.compute_uma_private_ready(1), "A's commands are in flight in slot 1"); assert!(s.compute_uma_private_ready(6)); assert_eq!(s.bind_compute_uma(&c).unwrap(), None, "the SCS unit holds slot 6"); // Flight 2 retires first (another pipe): A is not drained, its counter stays. assert!(!retire(&mut s, &a, &f2, 1)); - assert_eq!((a.outstanding(), rd64(&a.counter, 0)), (1, 3)); + assert_eq!((a.outstanding(), rd64(&a.counter, 0)), (1, 2)); assert!(!s.compute_uma_private_ready(1)); // Flight 1 retires: A drains, the SCS unit leaves slot 6. assert!(retire(&mut s, &a, &f1, 1)); diff --git a/tools/testing/selftests/drivers/gpu/asahi-m4/test-stamp-wrap.py b/tools/testing/selftests/drivers/gpu/asahi-m4/test-stamp-wrap.py index 471b7ca815b5d..4ed6f93b5f5bc 100644 --- a/tools/testing/selftests/drivers/gpu/asahi-m4/test-stamp-wrap.py +++ b/tools/testing/selftests/drivers/gpu/asahi-m4/test-stamp-wrap.py @@ -889,13 +889,13 @@ def function(text, name): descriptor.set(manager::COUNTER,counter.va); SharedComputeUma{counter,descriptor} }; - // Two flights of one manager across pipes 0 and 1 (counter 3 under the hold, COMPLETED + // Two flights of one manager across pipes 0 and 1 (counter 2, COMPLETED // lagging at 1 as a cached record can): the pipe-1 flight retires Full while pipe 0 runs. let mut f=firmware(false,1); - f.engine.flights[0].job.resources.compute_uma=Some(mk_uma(3,1)); + f.engine.flights[0].job.resources.compute_uma=Some(mk_uma(2,1)); let mut second=firmware(false,2).engine.flights.pop().unwrap(); second.compute_pipe=1; - second.job.resources.compute_uma=Some(mk_uma(3,1)); + second.job.resources.compute_uma=Some(mk_uma(2,1)); f.graph.compute[1].fw_stamp.set(0,u64::from(second.target)); f.graph.compute[1].user_stamp.set(0,u64::from(second.target)); f.engine.target[1]=second.target;