gsp_route: free a GA10x channel before its USERD or error-notifier memory

A GA10x channel hands GSP-RM the physical addresses of its USERD and error notifier, but the memory objects behind
them stayed freeable on their own: a client could free one while keeping the channel, and its frames went back to the
allocator while GSP-RM (and the GPU) could still write there. Stock RM makes the channel a dependant of its error
notifier (refAddDependant), so freeing that memory frees the channel first.

The channel now records both memory handles, and a free of a client's memory object (not a GSP-RM one) first frees
every GA10x channel of that client using it as USERD or error notifier: in GSP-RM, then its mirrors and kernel-side
state. If GSP-RM refuses the channel's free, the memory free fails with that status and the frames stay. A whole
client's free is unchanged: everything goes under the RM API node's lock, so no frame is handed out in between.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kamil Skowron
2026-12-01 11:47:34 +01:00
parent 8366b7244c
commit 9d4f937c40
2 changed files with 70 additions and 0 deletions

View File

@ -61,6 +61,10 @@ struct Chan {
inst: u64,
/// The error notifier's system-memory allocation (nvdriver's OS token), 0 = none.
err: u64,
/// The client's memory objects GSP-RM has the addresses of: USERD (`hUserdMemory[0]`) and the error notifier
/// (`hObjectError`, 0 = none). The channel goes before either of them (`channel_using_memory`).
userd_mem: u32,
err_mem: u32,
}
#[derive(Clone, Copy)]
@ -318,6 +322,8 @@ fn chan_release(g: &mut Ga10xGpu, i: usize) {
c.handle = 0;
c.client = 0;
c.err = 0;
c.userd_mem = 0;
c.err_mem = 0;
g.mem.free(c.inst, 0x1000);
}
#[inline(never)]
@ -575,6 +581,12 @@ pub(crate) fn channel_alloc(
c.chid = chid;
c.inst = inst;
c.err = if err_pa != 0 { err_token } else { 0 };
c.userd_mem = fw::rd32(user, 0x20).unwrap_or(0);
c.err_mem = if err_pa != 0 {
fw::rd32(user, 0).unwrap_or(0)
} else {
0
};
}
g.objs.chid_set(chid as usize, true);
if user.len() >= 0x88 {
@ -927,6 +939,18 @@ pub(crate) fn gr_object_alloc(
}
// ---- channel queries and controls the kernel side answers ----
/// A channel of `client` whose USERD or error notifier is the client's memory object `memory`: `(channel, group)`.
pub(crate) fn channel_using_memory(g: &Ga10xGpu, client: u32, memory: u32) -> Option<(u32, u32)> {
if memory == 0 {
return None;
}
g.objs
.chan
.iter()
.find(|c| c.handle != 0 && c.client == client && (c.userd_mem == memory || c.err_mem == memory))
.map(|c| (c.handle, c.tsg))
}
#[inline(never)]
pub(crate) fn channel_chid(g: &mut Ga10xGpu, client: u32, object: u32) -> Option<u32> {
let c = g.objs.chan(client, object)?;

View File

@ -288,6 +288,17 @@ pub(crate) fn pre(
params: &mut [u8],
nested: &mut [u8],
) -> Option<i32> {
if escape == 0x29 && top.len() >= 16 {
// NVOS00: hRoot, hObjectParent, hObjectOld, status
let (client, object) = (rd(top, 0), rd(top, 8));
if object != client && find(client, object).is_none() {
if let Some(st) = free_memory_users(client, object) {
wr(top, 12, st);
return Some(0);
}
}
return None;
}
if escape != 0x2a || top.len() < 32 {
return None;
}
@ -727,6 +738,41 @@ fn post_alloc(
}
}
/// Before nvdriver frees `client`'s object `object` (not a GSP-RM one): a GA10x channel that uses it as USERD or
/// error notifier is freed first, in GSP-RM and here, as stock RM frees a channel with its error notifier
/// (`refAddDependant`): GSP-RM must never keep the address of frames that go back to the allocator. `Some(status)`:
/// GSP-RM refused a channel's free; the free of the memory fails with it, and its frames stay.
#[inline(never)]
fn free_memory_users(client: u32, object: u32) -> Option<u32> {
let mut gpus = [u32::MAX; 8];
let mut n = 0;
for m in table().iter() {
if m.used && m.class == CLASS_ROOT && m.client == client && n < gpus.len() {
gpus[n] = m.gpu;
n += 1;
}
}
for &gpu in &gpus[..n] {
let Some(g) = ga10x::get(gpu) else { continue };
while let Some((chan, tsg)) = objs::channel_using_memory(g, client, object) {
let r = g.rm_free(client, tsg, chan);
trace(
b"free channel before its memory (channel, memory, status)\0",
chan,
object,
err(r),
0,
);
if r != Ok(0) {
return Some(status(r));
}
drop_subtree(Some(&mut *g), gpu, client, chan);
objs::obj_free(g, client, chan);
}
}
None
}
/// `AMPERE_CHANNEL_GPFIFO_A`: USERD = hUserdMemory[0] (+0x20) + userdOffset[0] (+0x40), which must be video memory
/// with the 512-byte USERD inside it; the error notifier = hObjectError (+0), its first system-memory page.
#[inline(never)]