fix(gpu): close the completion waiter's file before the GPU stops, have a timed-out waiter look at the device while work awaits completion, and unload GSP at a level-3 PM transition

This commit is contained in:
Tyler Smith committed 2026-11-11 02:52:57 +00:00
1 parent 7176d91e24
commit c27e6c667b
5 files changed
+126 -3

No files matched your search

@@ -407,6 +407,13 @@ unsafe fn perform(gpu: &Installed, operation: Operation) -> i64 {
}
Operation::Shutdown => {
let gpu = installed().expect("requests are posted to a running environment");
// A completion waiter's file is a nouveau client: freed after
// the stop began, its RM objects outlive GSP's unload.
if !super::waiter::wait_gone() {
output::arguments(format_args!(
"GPU completion waiter still open at the stop\n"
));
}
gpu.start
._attached
.iter()
+7
View File
@@ -994,6 +994,13 @@ pub(super) fn watch_completions() {
}
}
/// Whether service `index` has submitted work not yet published complete.
pub(super) fn awaits_completion(index: usize) -> bool {
shared()
.get(index)
.is_some_and(|service| service.awaits_completion())
}
/// Service `index`'s completion timeline `timeline` reached `value`.
pub(super) fn completed_through(index: usize, timeline: u64, value: u64) {
if let Some(service) = shared().get(index) {
+57 -3
View File
@@ -13,12 +13,17 @@
//! Each point it sees signaled goes straight to the device's service
//! (`SharedService::completed_through`), which tells the guest at once even
//! while a call, such as a compile, holds the service.
//!
//! Its file is a nouveau client with RM objects (device, subdevice, VA
//! space). The GPU's stop waits for every waiter to close it
//! ([`wait_gone`]): frees that reach nouveau after the stop began are refused,
//! and GSP-RM is then asked to unload with that client still allocated.
use super::{client, gate, look_again};
use alloc::boxed::Box;
use core::{
ffi::{c_int, c_void},
ffi::{c_int, c_uint, c_void},
ptr,
sync::atomic::{AtomicU64, Ordering},
sync::atomic::{AtomicU32, AtomicU64, Ordering},
};
/// The waiters' generation: a waiter of another one leaves.
@@ -27,6 +32,25 @@ static GENERATION: AtomicU64 = AtomicU64::new(0);
/// The generation of the waiter that waits on its timeline now, 0 for none.
static WATCHING: AtomicU64 = AtomicU64::new(0);
/// Waiters started that have not yet closed their file.
static RUNNING: AtomicU32 = AtomicU32::new(0);
/// Counts a waiter as running until it is dropped, after its file closed.
struct Running;
impl Running {
fn start() -> Self {
RUNNING.fetch_add(1, Ordering::AcqRel);
Self
}
}
impl Drop for Running {
fn drop(&mut self) {
RUNNING.fetch_sub(1, Ordering::AcqRel);
}
}
/// Whether the newest waiter waits on its timeline: every fence of it that
/// signals, in an interrupt or not, ends its wait and has the services look.
pub(super) fn watching() -> bool {
@@ -88,6 +112,7 @@ struct Watch {
generation: u64,
service: usize,
timeline: u64,
_running: Running,
}
/// Waits on `fd`, the completion timeline `timeline` of service `service`'s
@@ -100,6 +125,7 @@ pub(super) fn start(fd: c_int, service: usize, timeline: u64) {
generation,
service,
timeline,
_running: Running::start(),
}));
if client::spawn(run, watch.cast()) != 0 {
// SAFETY: the thread was refused, so the box is still ours.
@@ -113,6 +139,26 @@ pub(super) fn stop() {
GENERATION.fetch_add(1, Ordering::AcqRel);
}
/// The longest the stop waits for the waiters to close their files: one
/// wait of [`WAIT_NS`], its syncobj's destruction and the file's release.
const GONE_NS: u64 = 500_000_000;
/// Has every waiter leave and waits, within [`GONE_NS`], until each closed
/// its file, so its nouveau client is freed before the GPU stops; says
/// whether they did. On a Linux task that may sleep.
pub(super) fn wait_gone() -> bool {
unsafe extern "C" {
fn msleep(msecs: c_uint);
}
stop();
let deadline = client::now_ns().saturating_add(GONE_NS);
while RUNNING.load(Ordering::Acquire) != 0 && client::now_ns() < deadline {
// SAFETY: Linux's sleep, on the caller's task, which may sleep.
unsafe { msleep(1) };
}
RUNNING.load(Ordering::Acquire) == 0
}
unsafe extern "C" fn run(argument: *mut c_void) {
// SAFETY: `start` gave this thread the box.
let watch = unsafe { Box::from_raw(argument.cast::<Watch>()) };
@@ -173,7 +219,15 @@ fn wait(file: c_int, handle: u32, watch: &Watch) {
gate::completed_through(watch.service, watch.timeline, reached);
look_again();
}
result if result == -ETIME || result == -EINTR => failures = 0,
result if result == -ETIME || result == -EINTR => {
failures = 0;
// A stalled timeline with work outstanding, as after a
// failed threaded submission: the device look notices the
// loss no point or interrupt will signal. Never while idle.
if result == -ETIME && gate::awaits_completion(watch.service) {
look_again();
}
}
_ => failures += 1,
}
}
@@ -691,6 +691,15 @@ impl<B: Backend, C: Compiler + 'static> SharedService<B, C> {
Some((fd, self.timeline.fetch_add(1, Ordering::AcqRel) + 1))
}
/// Whether work was submitted that is not yet published complete: a
/// waiter whose wait times out then has the platform look at the device,
/// which notices a loss the stalled timeline never signals. Not once
/// polls are unavailable: the loss is already known.
pub fn awaits_completion(&self) -> bool {
let reached = self.reached.load(Ordering::Acquire);
reached != POLL_UNAVAILABLE && self.submitted.load(Ordering::Acquire) > reached
}
/// The platform's waiter saw the completion timeline of generation
/// `timeline` reach `value`, every serial through it complete. Published
/// at once when a waiter may publish, so a call holding the service
@@ -1233,6 +1242,21 @@ mod tests {
drop(held);
}
#[test]
fn only_unpublished_submissions_await_completion() {
let shared = shared();
// Idle: nothing submitted past what was published.
assert!(!shared.awaits_completion());
shared.submitted.store(3, Ordering::Release);
assert!(shared.awaits_completion());
shared.reached.store(3, Ordering::Release);
assert!(!shared.awaits_completion());
// A known loss needs no look.
shared.submitted.store(4, Ordering::Release);
shared.reached.store(POLL_UNAVAILABLE, Ordering::Release);
assert!(!shared.awaits_completion());
}
#[test]
fn without_coherent_memory_completion_waits_for_a_turn() {
let shared = shared();
+31
View File
@@ -0,0 +1,31 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
Date: Tue, 10 Nov 2026 19:32:28 -0500
Subject: [PATCH] fix(nouveau): unload GSP at power-off as a PM transition to
level 3, as nouveau's suspend path does
---
drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c
index 7e8727ac2288bd72cc975a65b9682eae45ce912d..7f49dd8220a029468e23cd9e4ae3c2659da3d742 100644
--- a/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c
+++ b/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/rm/r535/gsp.c
@@ -1866,7 +1866,15 @@ r535_gsp_fini(struct nvkm_gsp *gsp, enum nvkm_suspend_state suspend)
if (READ_ONCE(gsp->dead))
return -ENODEV;
- ret = r535_gsp_rpc_unloading_guest_driver(gsp, suspend);
+ /*
+ * A power-off unloads as a PM transition to level 3, the arguments
+ * nouveau's shutdown sent through nouveau_do_suspend() before it
+ * unloaded at power-off directly; nothing is saved to resume from.
+ * GSP-RM never answered a level-0 unload sent with the device and its
+ * channels still initialised (level 0 is what upstream sends only on
+ * driver removal, after every engine and client is torn down).
+ */
+ ret = r535_gsp_rpc_unloading_guest_driver(gsp, true);
if (WARN_ON(ret))
return ret;