forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

reset.rs169 lines, 5.4 KiBHistoryRawDownload
1//! Process-wide GPU reset epoch.
2//!
3//! When the kernel resets a hung VCN ring, every VA context alive in the
4//! process is reset with it, the innocent ones too, and Mesa aborts the whole
5//! process on the next submit to any of them. One session noticing (a sync that
6//! errors or comes back after a ring reset) therefore has to condemn all the
7//! others: each session remembers the epoch it was created in and refuses to
8//! submit once the epoch has moved.
9//!
10//! New sessions are refused during a short grace period after the event, while
11//! the ring is still being reset, and work normally afterwards.
12//!
13//! A DRM `AMDGPU_CTX_OP_QUERY_STATE2` query was considered and rejected: the
14//! context id belongs to Mesa's winsys, not to ec-va, so there is nothing to
15//! query from outside, and `vaQuerySurfaceStatus` does not report resets.
16
17use std::sync::OnceLock;
18use std::sync::atomic::{AtomicU64, Ordering};
19use std::time::{Duration, Instant};
20
21/// How long after a reset new sessions are refused. The kernel's ring reset
22/// takes about 2 s after the 2 s job timeout has fired; the sync that detects
23/// it returns after the reset.
24pub const GRACE: Duration = Duration::from_secs(3);
25
26/// The epoch counter and the time of the last reset, in ms since `base` plus 1
27/// (0 means never).
28#[derive(Debug, Default)]
29pub(crate) struct ResetClock {
30 epoch: AtomicU64,
31 last_ms: AtomicU64,
32}
33
34impl ResetClock {
35 pub(crate) const fn new() -> ResetClock {
36 ResetClock {
37 epoch: AtomicU64::new(0),
38 last_ms: AtomicU64::new(0),
39 }
40 }
41
42 pub(crate) fn epoch(&self) -> u64 {
43 self.epoch.load(Ordering::Acquire)
44 }
45
46 pub(crate) fn report(&self, now_ms: u64) {
47 self.last_ms.store(now_ms + 1, Ordering::Release);
48 self.epoch.fetch_add(1, Ordering::AcqRel);
49 }
50
51 pub(crate) fn in_grace(&self, now_ms: u64, grace: Duration) -> bool {
52 match self.last_ms.load(Ordering::Acquire) {
53 0 => false,
54 last => u128::from(now_ms.saturating_sub(last - 1)) < grace.as_millis(),
55 }
56 }
57}
58
59/// Hard limit on one wait for a picture; past it the sync gives up.
60#[cfg(target_os = "linux")]
61pub(crate) const SYNC_LIMIT: Duration = Duration::from_secs(8);
62
63/// A wait that took longer than this spent its time in a ring reset: a
64/// healthy picture takes milliseconds. 1080p..8K pictures stay far below it.
65#[cfg(target_os = "linux")]
66pub(crate) const SLOW_PICTURE: Duration = Duration::from_millis(1500);
67
68/// Wait for `surface`, and condemn every session if the wait shows that the
69/// GPU was reset: an error, or a wait longer than [`SLOW_PICTURE`].
70#[cfg(target_os = "linux")]
71pub(crate) fn sync_watched<D: ec_va::va::SurfaceMemoryDescriptor>(
72 surface: &ec_va::va::Surface<D>,
73) -> crate::Result<()> {
74 use ec_va::SurfaceExt;
75
76 let started = Instant::now();
77 let synced = surface.sync_timeout(SYNC_LIMIT);
78 let took = started.elapsed();
79 match synced {
80 Ok(()) if took <= SLOW_PICTURE => Ok(()),
81 Ok(()) => {
82 report_gpu_reset();
83 Err(crate::Error::hw_reset(format!(
84 "a picture took {took:?} (limit {SLOW_PICTURE:?}); the GPU ring was probably reset"
85 )))
86 }
87 Err(e) => {
88 report_gpu_reset();
89 Err(e.into())
90 }
91 }
92}
93
94static CLOCK: ResetClock = ResetClock::new();
95
96fn now_ms() -> u64 {
97 static BASE: OnceLock<Instant> = OnceLock::new();
98 u64::try_from(BASE.get_or_init(Instant::now).elapsed().as_millis()).unwrap_or(u64::MAX)
99}
100
101/// Record that the GPU was reset (or a context was lost, or a job timed out).
102/// Every session created before this call refuses further submits.
103pub fn report_gpu_reset() {
104 // Make sure BASE is initialised before the first reading is taken.
105 CLOCK.report(now_ms());
106}
107
108/// The number of resets seen by this process so far.
109pub fn gpu_reset_epoch() -> u64 {
110 CLOCK.epoch()
111}
112
113/// Whether a reset happened within the last [`GRACE`]: a hardware session
114/// created now would be reset under the caller, so use software instead.
115pub fn gpu_recently_reset() -> bool {
116 CLOCK.in_grace(now_ms(), GRACE)
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn a_fresh_clock_is_calm() {
125 let c = ResetClock::new();
126 assert_eq!(c.epoch(), 0);
127 assert!(!c.in_grace(0, GRACE));
128 assert!(!c.in_grace(1_000_000, GRACE));
129 }
130
131 #[test]
132 fn a_report_moves_the_epoch_and_opens_the_grace_period() {
133 let c = ResetClock::new();
134 c.report(10_000);
135 assert_eq!(c.epoch(), 1);
136 assert!(c.in_grace(10_000, GRACE));
137 assert!(c.in_grace(12_999, GRACE));
138 assert!(!c.in_grace(13_000, GRACE));
139 }
140
141 #[test]
142 fn a_report_at_time_zero_still_counts() {
143 let c = ResetClock::new();
144 c.report(0);
145 assert!(c.in_grace(0, GRACE));
146 assert!(c.in_grace(2_999, GRACE));
147 assert!(!c.in_grace(3_000, GRACE));
148 }
149
150 #[test]
151 fn a_second_report_restarts_the_grace_period() {
152 let c = ResetClock::new();
153 c.report(1_000);
154 c.report(3_500);
155 assert_eq!(c.epoch(), 2);
156 assert!(c.in_grace(6_000, GRACE));
157 assert!(!c.in_grace(6_500, GRACE));
158 }
159
160 #[test]
161 fn a_session_born_before_the_report_is_stale_one_born_after_is_not() {
162 let c = ResetClock::new();
163 let old = c.epoch();
164 c.report(5);
165 let young = c.epoch();
166 assert_ne!(c.epoch(), old);
167 assert_eq!(c.epoch(), young);
168 }
169}