On Mon, 23 Nov 2026 15:58:45 +0100 Laura Nao <[email protected]> wrote:
> From: Daniel Almeida <[email protected]> > > HwGate::access() takes gate_lock, which is a mutex, so it cannot be > called from atomic context. IRQ handlers need to access the GPU > registers, and those are only reachable through the gate. > > Add HwGate::try_access_atomic(). It enters SRCU without taking gate_lock > and returns None if the reset worker has closed the gate. close() now > sets a closed flag before waiting for SRCU readers, and HwClosedGuard > clears it when dropped. SRCU guarantees that a reader either sees the > flag set or is waited for. > > An IRQ handler that gets None cannot acknowledge its interrupt, so its > interrupt has to stay masked while the gate is closed. Document this on > close(). No caller needs to do this yet: the only reset runs at probe, > before any IRQ handler is registered. > > There are no users yet. The Job IRQ handler will be the first one. > > Signed-off-by: Daniel Almeida <[email protected]> LGTM Reviewed-by: Onur Ozkan <[email protected]> > --- > drivers/gpu/drm/tyr/reset/hw_gate.rs | 55 > ++++++++++++++++++++++++++++++++++-- > 1 file changed, 53 insertions(+), 2 deletions(-) > > diff --git a/drivers/gpu/drm/tyr/reset/hw_gate.rs > b/drivers/gpu/drm/tyr/reset/hw_gate.rs > index 201f8c953078..40b38f07c472 100644 > --- a/drivers/gpu/drm/tyr/reset/hw_gate.rs > +++ b/drivers/gpu/drm/tyr/reset/hw_gate.rs > @@ -6,10 +6,18 @@ > //! access with reset. Readers hold the mutex while entering SRCU, then > release > //! it before accessing hardware. The reset worker holds the mutex while > waiting > //! for admitted readers and resetting hardware. > +//! > +//! Readers that cannot sleep use [`HwGate::try_access_atomic`] instead. It > +//! enters SRCU without taking the mutex and fails if the reset worker has > set > +//! `closed`, which it does before waiting for readers. > > use kernel::{ > prelude::*, > sync::{ > + atomic::{ > + Atomic, > + Relaxed, // > + }, > new_mutex, > srcu, > Mutex, > @@ -32,6 +40,9 @@ pub(crate) struct HwGate<'hw> { > /// Drains readers that entered before the reset worker acquired > `gate_lock`. > #[pin] > srcu: Srcu, > + /// Set while the reset worker owns the hardware. Checked by readers that > + /// cannot take `gate_lock`. > + closed: Atomic<bool>, > } > > impl<'hw> HwGate<'hw> { > @@ -41,6 +52,7 @@ pub(crate) fn new(iomem: IoMem<'hw>) -> impl PinInit<Self, > Error> { > iomem, > gate_lock <- new_mutex!(()), > srcu <- kernel::new_srcu!(), > + closed: Atomic::new(false), > }) > } > > @@ -60,15 +72,45 @@ pub(crate) fn access(&self) -> HwAccessGuard<'_, 'hw> { > } > } > > + /// Enters a reset-sensitive hardware-access section without sleeping. > + /// > + /// Returns `None` while the reset worker owns the hardware, in which > case > + /// the caller must skip its hardware access. > + #[expect(dead_code)] > + pub(crate) fn try_access_atomic(&self) -> Option<HwAccessGuard<'_, 'hw>> > { > + let srcu = self.srcu.read_lock(); > + > + // `close()` sets `closed` before waiting for readers, so SRCU > guarantees > + // that this reader either sees it set or is waited for. > + if self.closed.load(Relaxed) { > + return None; > + } > + > + Some(HwAccessGuard { > + gate: self, > + _srcu: srcu, > + }) > + } > + > /// Stops new readers and drains admitted readers for the reset worker. > /// > /// Callers must serialize write-side access. The reset controller's > state > /// machine provides that serialization. > + /// > + /// An IRQ handler that uses [`HwGate::try_access_atomic`] cannot > acknowledge > + /// its interrupt while the gate is closed. Callers must mask such an > + /// interrupt before closing the gate and keep it masked until the gate > + /// reopens. > pub(super) fn close(&self) -> HwClosedGuard<'_, 'hw> { > let gate_lock = self.gate_lock.lock(); > > - // Holding `gate_lock` prevents new readers from entering SRCU. > Readers > - // admitted before us are enrolled, so wait for their read-side work. > + // Stop `try_access_atomic()` readers. This must happen before > waiting > + // for readers below. > + self.closed.store(true, Relaxed); > + > + // Holding `gate_lock` prevents new readers from entering SRCU > through > + // `access()`. Readers admitted before us are enrolled, so wait for > their > + // read-side work. > self.srcu.synchronize(); > > HwClosedGuard { > @@ -104,3 +146,12 @@ pub(super) fn iomem(&self) -> &IoMem<'hw> { > &self.gate.iomem > } > } > + > +impl Drop for HwClosedGuard<'_, '_> { > + fn drop(&mut self) { > + // Reopen for `try_access_atomic()`. A reader that races with this > and > + // still sees `closed` set returns `None`, as it would have before > the > + // store. > + self.gate.closed.store(false, Relaxed); > + } > +} > > -- > 2.39.5 >
