Hold a reference on an IPC session for the length of a request, and copy the transfer memory under the kernel lock.

SendSyncRequest looked the session up with kern_handle_get, which holds the kernel lock only for the lookup, and then
dispatched to the handler with no reference. A CloseHandle of the session's last handle on another thread meanwhile
reached ipc_handle_closed, which ran the close callback and freed the session while the handler still used it; for
the audio renderer, renderer_close joins the render thread and frees the renderer a RequestUpdate is reading.
OpenAudioRenderer read the transfer memory the same way, and a CloseHandle frees that object at once.

kernel.c gains kern_handle_hold, which runs a hold callback under the kernel lock after the lookup (handle_close
clears the slot under that lock before it calls ipc_handle_closed, so a close either comes first and the lookup
fails, or comes after and finds the extra reference), and kern_handle_copy, which copies an object under the lock.
ipc_svc takes the session with kern_handle_hold and drops the request's reference through ipc_handle_closed after the
answer, so the close callback runs on whichever thread drops the last reference; the request thread holds no service
lock by then. audren.c copies the transfer memory into a local IpcTransferMemory. Domain sub-objects are not freed
yet (IpcSession.objects), so their lookups are left for the domain object change.

Not reproduced before the change in a game run (no log shows a renderer closed during play). tests/ipc_session_check.py
builds ipc.c with kernel.c linked: a request whose handler blocks while another thread closes the session's only handle
must leave the close callback until the handler returns, and a closed transfer memory handle must copy nothing.
Against the previous ipc.c it fails with the close callback run while the handler was blocked. system_event_check,
sync_wake_check and the compile check of every runtime file pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:07:41 -05:00
parent 465ffe67bb
commit 63228e7ca8
6 changed files with 243 additions and 5 deletions

View File

@@ -717,8 +717,10 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs
AudrenParameter p = IPC_ARG(rq, AudrenParameter, 0);
uint64_t work = IPC_ARG(rq, uint64_t, 0x38);
need(rq->ncopy >= 1, "OpenAudioRenderer without a transfer memory handle");
IpcTransferMemory *tm = kern_handle_get(rq->copy[0], KERN_TMEM);
need(tm && tm->size >= work, "OpenAudioRenderer work buffer is not a transfer memory of the stated size");
// A copy made under the kernel lock: a CloseHandle of the transfer memory on another thread frees the object.
IpcTransferMemory tm;
int have_tm = kern_handle_copy(rq->copy[0], KERN_TMEM, &tm, sizeof tm);
need(have_tm && tm.size >= work, "OpenAudioRenderer work buffer is not a transfer memory of the stated size");
AudioRenderer *r = renderer_new(&p);
if (rt_knob("AUDIO_STATS", "0")) {
r->stats = calloc(1, sizeof *r->stats);
@@ -743,7 +745,7 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs
"splitters %d/%d, REV%d, work buffer %#llx+%#llx",
p.sample_rate, p.sample_count, p.mix_buffer_count, p.voice_count, p.effect_count, p.submix_count,
p.sink_count, p.splitter_count, p.splitter_destination_count, revision_number(p.revision),
(unsigned long long)tm->addr, (unsigned long long)work);
(unsigned long long)tm.addr, (unsigned long long)work);
rs->move[rs->nmove++] = ipc_session_new("IAudioRenderer", renderer_handler, r, renderer_close);
return;
}

View File

@@ -344,7 +344,11 @@ void kern_core_diag_frame(uint64_t frame, uint64_t interval_ns); // perf_prese
void kern_sched_report(uint64_t frame); // NX2NAVI_TRACE=sched: per-thread time split, on present
uint32_t kern_handle_new(int kind, void *obj); // kind: KERN_SESSION or KERN_TMEM (ipc.c)
void *kern_handle_get(uint32_t h, int kind);
void kern_handle_close(uint32_t h); // CloseHandle from the host side, any thread; fatal on a
void *kern_handle_hold(uint32_t h, int kind, void (*hold)(void *obj)); // kern_handle_get, hold(obj) run under the
// kernel lock (a reference the caller drops)
int kern_handle_copy(uint32_t h, int kind, void *out, size_t n); // a copy of the object, made under the
// kernel lock; 0 for a bad handle
void kern_handle_close(uint32_t h); // CloseHandle from the host side, any thread; fatal on a
// bad handle
enum { KERN_SESSION = 4, KERN_TMEM = 5 };
uint32_t kern_event_new(void); // writable handle

View File

@@ -254,6 +254,9 @@ static uint32_t session_clone(IpcSession *s) {
return kern_handle_new(KERN_SESSION, s);
}
// kern_handle_hold's callback: a request's reference, dropped through ipc_handle_closed once the request is answered.
static void session_hold(void *o) { plat_inc32(&((IpcSession *)o)->refs); }
void ipc_handle_closed(int kind, void *obj) {
if (kind == KERN_TMEM) {
free(obj);
@@ -529,7 +532,10 @@ int ipc_svc(Cpu *c, uint32_t imm) {
}
case 0x21: case 0x22: { // SendSyncRequest(w0 handle) / WithUserBuffer(x0 buffer, x1 size, w2 handle)
uint32_t h = (uint32_t)(imm == 0x21 ? x[0] : x[2]);
IpcSession *s = kern_handle_get(h, KERN_SESSION);
// The request holds a reference on the session until it is answered: a CloseHandle of the session on another
// thread meanwhile leaves the object, and its close callback (renderer_close frees the audio renderer the
// handler uses), to whichever drops the last reference.
IpcSession *s = kern_handle_hold(h, KERN_SESSION, session_hold);
if (!s) {
rt_log("ipc: svc %#x on %#x, not a session handle", imm, h);
x[0] = IPC_R_BAD_HANDLE;
@@ -542,6 +548,7 @@ int ipc_svc(Cpu *c, uint32_t imm) {
t_req = c;
dispatch(s, m);
t_req = outer;
ipc_handle_closed(KERN_SESSION, s); // the request's reference
x[0] = 0;
return 1;
}

View File

@@ -2090,6 +2090,26 @@ void *kern_handle_get(uint32_t h, int kind) {
return o;
}
// kern_handle_get that also runs hold(obj) holding G, so the caller can take a reference before a CloseHandle on
// another thread drops the handle's own: handle_close clears the slot holding G and only then calls ipc_handle_closed.
void *kern_handle_hold(uint32_t h, int kind, void (*hold)(void *obj)) {
lock();
void *o = h_peek(h, kind);
if (o) hold(o);
unlock();
return o;
}
// Copies n bytes of a handle's object holding G, for objects without a reference count (KERN_TMEM, freed by its
// CloseHandle): 1 when h is a handle of that kind, else 0 and nothing copied.
int kern_handle_copy(uint32_t h, int kind, void *out, size_t n) {
lock();
const void *o = h_peek(h, kind);
if (o) memcpy(out, o, n);
unlock();
return o != NULL;
}
uint32_t kern_event_new(void) {
Event *e = calloc(1, sizeof *e);
if (!e) rt_fatal("kernel: event allocation");