From 3d0e35e001deb29b7a62f46675c77d9e4918c4c9 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sat, 14 Nov 2026 09:32:43 +0530 Subject: [PATCH 1/4] icd_shim: a release note read while an allocation reply is awaited is queued, not fatal exchange_req knew every push on the scan-out channel except BufferReleased. A note that arrived ahead of the allocation reply fell through to Failure::Dead: the channel was closed and reconnected, and the note was lost. lsfg_config skipped the same note silently. The release watcher pushes whenever a fence signals, so this can happen on any allocation made while frames are being shown (a swapchain recreation, a generation destination grown mid-session). Both readers now hand the note to apply_release, the helper poll_notes uses, so it is queued and re-arms parking the same way wherever it is read. The exchange test sends one ahead of the reply and checks it is held. --- icd_shim/src/scanout.rs | 53 +++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/icd_shim/src/scanout.rs b/icd_shim/src/scanout.rs index ceb7cc9..6ac77d6 100644 --- a/icd_shim/src/scanout.rs +++ b/icd_shim/src/scanout.rs @@ -276,6 +276,8 @@ pub fn lsfg_config() -> Option { apply_vsync(note); } else if let Some(done) = gndrm_uapi::wire::PresentDone::decode(&buf[..n as usize]) { crate::present::record(done.id, done.latch_ns); + } else if let Some(released) = BufferReleased::decode(&buf[..n as usize]) { + apply_release(released); } else if gndrm_uapi::wire::FrameAcquired::decode(&buf[..n as usize]).is_some() { // The shim's own counter, echoed nowhere: accepted so it is not // mistaken for a frame this reader does not know. @@ -529,6 +531,17 @@ fn exchange_req(fd: RawFd, req: ScanoutAllocReq) -> Result if frames < MAX_BURST_FRAMES { continue; } + } else if let Some(released) = BufferReleased::decode(buf) { + // The release watcher pushes on its own schedule, so a note + // can stand ahead of the reply; dropping it would leave its + // park to the bound. + if recv_fd >= 0 { + libc::close(recv_fd); + } + apply_release(released); + if frames < MAX_BURST_FRAMES { + continue; + } } else if gndrm_uapi::wire::FrameAcquired::decode(buf).is_some() { // The acquire counter shares this channel and can arrive // while a reply is pending; it is not the reply. @@ -720,6 +733,20 @@ pub(crate) fn note_buffer_released(ticket: u64, ino: u64, source: u64) { }); } +/// Adopt one release note, from whichever reader took it off the channel. +fn apply_release(released: BufferReleased) { + // SurfaceFlinger is finished with this scan-out buffer: the producer may + // write into it again (see `note_buffer_released`). + note_buffer_released(released.ticket, released.ino, released.source); + // A release note is the only message that can end a park, so it is the + // only one that says parking is worth a bet again. Liveness keyed on the + // socket itself is a lie on a device whose daemon still pushes the panel + // grid: RP6 sent ~5 000 `VsyncNote`s while releasing nothing, and + // `channel=[unanswered:2 dead:false]` never armed the fail-open with + // `park_timeouts=5181` of 5 210 acquires. + crate::proxy::note_channel_alive(); +} + /// Notes that carried a ticket, and notes whose release did not rest on a /// signalled fence. Two counts because they answer two questions the census /// could not: whether the daemon is sending tickets at all, and whether a note @@ -915,16 +942,7 @@ pub(crate) fn poll_notes() { // The panel latched the frame this id was marked for. crate::present::record(done.id, done.latch_ns); } else if let Some(released) = BufferReleased::decode(&buf[..len]) { - // SurfaceFlinger is finished with this scan-out buffer: the producer may - // write into it again (see `note_buffer_released`). - note_buffer_released(released.ticket, released.ino, released.source); - // A release note is the only message that can end a park, so it is the - // only one that says parking is worth a bet again. Liveness keyed on the - // socket itself is a lie on a device whose daemon still pushes the panel - // grid: RP6 sent ~5 000 `VsyncNote`s while releasing nothing, and - // `channel=[unanswered:2 dead:false]` never armed the fail-open with - // `park_timeouts=5181` of 5 210 acquires. - crate::proxy::note_channel_alive(); + apply_release(released); } else if let Some(acq) = gndrm_uapi::wire::FrameAcquired::decode(&buf[..len]) { // Sent from the acquire path; nothing to apply here. Accepted so the // log does not report it as an unknown frame. @@ -1216,6 +1234,17 @@ mod tests { // The daemon answers a subscribe with the current target, ahead // of any reply: a note where a reply would be expected. send_bare(conn, &FrameCapNote { fps: 120 }.encode()); + // And the release watcher pushes whenever a fence signals. + send_bare( + conn, + &BufferReleased { + ticket: 4_242, + ino: 4_243, + latch_ns: 0, + source: gndrm_uapi::wire::RELEASE_FENCE, + } + .encode(), + ); let mut buf = [0u8; ScanoutAllocReq::LEN]; let n = libc::recv(conn, buf.as_mut_ptr().cast(), buf.len(), 0); assert_eq!(n as usize, ScanoutAllocReq::LEN); @@ -1239,6 +1268,7 @@ mod tests { } }); + let _serial = fresh_notes(); let fd = connect(&name); assert!(fd >= 0, "connect: {}", std::io::Error::last_os_error()); let got = exchange(fd, 1280, 960) @@ -1248,6 +1278,9 @@ mod tests { assert_eq!(got.ino, ino); // The note ahead of the reply set the target. assert_eq!(target_fps(), 120); + // The release note ahead of the reply was queued, not taken for a dead + // channel. + assert!(take_released_ticket(4_242)); // SAFETY: the received descriptor and the connection are live. unsafe { libc::close(got.fd); From 9ec0f6c03a9d1b049041f023bc4bd754879f0d55 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sat, 14 Nov 2026 09:34:42 +0530 Subject: [PATCH 2/4] icd_shim: a fallback arm does not spend a note older than the park's own ticket The inode arm matched a note on its inode alone, and the order arm took the oldest note whatever ticket it carried. Two notes could therefore free a slot that was still on screen: - a park on a buffer expires, the buffer is committed again and parked, and the late note for the earlier frame frees it through either arm; - a hidden slot or generation destination is committed with a ticket but never parked, and its note pays the next guest park by order, while that guest frame is the one that replaced it on the layer. Both notes name a ticket older than the park's. Such a note says nothing about the parked frame, so both fallback arms now pass the park's ticket and refuse it: the inode arm drops it as stale for that buffer, the order arm skips it and leaves it for its own ticket or the age floor. A newer ticket still pays. A note can arrive before its own slot is parked, and the ticket arm only looks at parked slots, so the order arm hands it to the oldest park, which left the layer before the note's frame did. Read from the code, that is the likeliest source of Odin 3's order traffic with nearly every note ticketed (not measured), and this rule leaves it alone. Notes or parks without a ticket are not judged. --- docs/24-release-notes.md | 7 +++ icd_shim/src/proxy.rs | 6 ++- icd_shim/src/scanout.rs | 104 +++++++++++++++++++++++++++------------ 3 files changed, 83 insertions(+), 34 deletions(-) diff --git a/docs/24-release-notes.md b/docs/24-release-notes.md index c0e84e8..d6f560d 100644 --- a/docs/24-release-notes.md +++ b/docs/24-release-notes.md @@ -73,6 +73,13 @@ first of: 3. **The order.** The oldest park by `commit_ns`, with a floor one display period before the commit, because the note that answers a park is emitted when the *next* frame latches and so legitimately predates the park. + + Neither fallback takes a note whose ticket is older than the park's own: that + is a late note for an earlier use of the buffer, or the note of a commit that + was never parked (a hidden slot, a generation destination), and the frame it + describes says nothing about the parked one. A newer ticket still pays — a + note can arrive before its own slot is parked, and the ticket arm only looks + at parked slots. 4. **The bound.** `min(2 × commit spacing, 4 × panel period)`, floored at two periods. 5. **The fail-open.** `PARK_UNANSWERED` counts parks that expired without a note; diff --git a/icd_shim/src/proxy.rs b/icd_shim/src/proxy.rs index ef92f6c..039c312 100644 --- a/icd_shim/src/proxy.rs +++ b/icd_shim/src/proxy.rs @@ -3084,6 +3084,7 @@ unsafe fn answer_park_from_notes( ) -> i32 { let mut oldest = -1i32; let mut oldest_commit = u64::MAX; + let mut oldest_ticket = 0u64; { let slots = unsafe { (*sc).slots.as_ptr() }; for k in first..end { @@ -3098,13 +3099,14 @@ unsafe fn answer_park_from_notes( if commit < oldest_commit { oldest_commit = commit; oldest = k as i32; + oldest_ticket = unsafe { (*s).ticket.load(Ordering::Acquire) }; } } } if oldest < 0 { return -1; } - if !crate::scanout::take_any_released(oldest_commit) { + if !crate::scanout::take_any_released(oldest_commit, oldest_ticket) { return -1; } NOTES_APPLIED.fetch_add(1, Ordering::Relaxed); @@ -3164,7 +3166,7 @@ unsafe fn slot_just_freed(s: *mut GnSlot, now: u64) -> bool { return true; } let since = unsafe { (*s).commit_ns }.min(parked); - if unsafe { crate::scanout::take_released((*s).ino, since) } { + if unsafe { crate::scanout::take_released((*s).ino, since, (*s).ticket.load(Ordering::Acquire)) } { NOTES_APPLIED.fetch_add(1, Ordering::Relaxed); PARK_UNANSWERED.store(0, Ordering::Relaxed); let lag = now.saturating_sub(parked); diff --git a/icd_shim/src/scanout.rs b/icd_shim/src/scanout.rs index 6ac77d6..5f895df 100644 --- a/icd_shim/src/scanout.rs +++ b/icd_shim/src/scanout.rs @@ -821,7 +821,7 @@ pub(crate) fn release_notes_received() -> u64 { /// note for a frame arrives (it rides the latch of the frame that replaced it). /// Without the margin, depth is effectively one image short — measured at 44 fps /// with a 60 cap on a three-image FIFO swapchain. -pub(crate) fn take_any_released(since_ns: u64) -> bool { +pub(crate) fn take_any_released(since_ns: u64, ticket: u64) -> bool { // One display period of margin, and no more. The note that answers a park is // emitted when the *next* frame latches, so it legitimately predates the // commit by up to one frame; a floor at the commit itself rejects it and @@ -831,20 +831,39 @@ pub(crate) fn take_any_released(since_ns: u64) -> bool { let floor = since_ns.saturating_sub(1_000_000_000 / hz); let mut q = RELEASED.lock().unwrap_or_else(|e| e.into_inner()); q.retain(|n| n.at >= floor); - let had = !q.is_empty(); - if had { - // The oldest note, unconditionally. It describes the frame that latched - // when this slot's buffer was replaced, which is the frame *before* the - // one parked here — so a floor at the commit would reject the only note - // that will ever mention that buffer and leave the park to its bound. - // - // The margin this gives up is one frame: slot N may be freed by the - // release of frame N+1. Both are in order, so the reuse is still older - // than the display's newest frame; the stricter identity match above is - // unaffected and still runs first where inodes line up. - q.remove(0); + // The oldest note that does not predate this park's frame. It describes the + // frame that latched when this slot's buffer was replaced, which is the frame + // *before* the one parked here — so a floor at the commit would reject the + // only note that will ever mention that buffer and leave the park to its + // bound. + // + // The margin this gives up is one frame: slot N may be freed by the release + // of frame N+1. Both are in order, so the reuse is still older than the + // display's newest frame; the stricter identity match above is unaffected + // and still runs first where inodes line up. + match q.iter().position(|n| !predates(n.ticket, ticket)) { + Some(i) => { + q.remove(i); + true + } + None => false, } - had +} + +/// Whether a note's ticket names a frame committed before the park's own. +/// +/// Such a note cannot answer the park by order or by inode: the frame it +/// describes left the display no later than the parked one arrived, so it says +/// nothing about the parked frame. It is a late note for an earlier use of the +/// same buffer (the park on it expired and the buffer was committed again), or +/// the note for a commit that was never parked (a hidden slot, a generation +/// destination), which the park's own frame replaced on the layer. A newer +/// ticket is still an answer, and is the common one: a note can arrive before +/// its own slot is parked, and then pays the oldest park, whose frame left the +/// layer before the note's did. 0 on either side is a daemon or a commit +/// without tickets, and is not judged. +fn predates(note_ticket: u64, park_ticket: u64) -> bool { + note_ticket != 0 && park_ticket != 0 && note_ticket < park_ticket } /// Take a release note for `ino` that arrived at or after `since_ns`, and drop @@ -852,8 +871,9 @@ pub(crate) fn take_any_released(since_ns: u64) -> bool { /// /// `since_ns` is when the slot was parked. A note older than that describes an /// earlier frame of the same buffer — it says nothing about the frame now on the -/// panel — and must not free the slot early. `true` when a note applied. -pub(crate) fn take_released(ino: u64, since_ns: u64) -> bool { +/// panel — and must not free the slot early. So does a note that arrived late +/// but names an earlier `ticket`. `true` when a note applied. +pub(crate) fn take_released(ino: u64, since_ns: u64, ticket: u64) -> bool { if ino == 0 { return false; } @@ -863,7 +883,7 @@ pub(crate) fn take_released(ino: u64, since_ns: u64) -> bool { if n.ino != ino { return true; } - if n.at < since_ns { + if n.at < since_ns || predates(n.ticket, ticket) { return false; } if applied { @@ -968,19 +988,19 @@ mod tests { fn a_release_note_only_applies_to_the_frame_it_describes() { let _serial = fresh_notes(); note_buffer_released(0, 9_001, gndrm_uapi::wire::RELEASE_FENCE); - assert!(take_released(9_001, 0), "a note after the park frees the slot"); - assert!(!take_released(9_001, 0), "and is consumed by it"); + assert!(take_released(9_001, 0, 0), "a note after the park frees the slot"); + assert!(!take_released(9_001, 0, 0), "and is consumed by it"); // A note taken before the slot was parked: dropped, and it must not free // the slot on the next pass either. note_buffer_released(0, 9_002, gndrm_uapi::wire::RELEASE_FENCE); let parked = crate::proxy::monotonic_ns() + 1; - assert!(!take_released(9_002, parked)); - assert!(!take_released(9_002, 0)); + assert!(!take_released(9_002, parked, 0)); + assert!(!take_released(9_002, 0, 0)); // An inode of 0 is never a registered buffer. note_buffer_released(0, 0, gndrm_uapi::wire::RELEASE_FENCE); - assert!(!take_released(0, 0)); + assert!(!take_released(0, 0, 0)); } /// A ticket pairs the note to its frame when the inode cannot, which is the @@ -993,13 +1013,33 @@ mod tests { // held — exactly what Odin 3 reports (`slot ino=11411`, `released=[11366 // 11368 11370]`). crate::scanout::note_buffer_released(7, 11_366, gndrm_uapi::wire::RELEASE_FENCE); - assert!(!take_released(11_411, 0), "the inode matches nothing"); + assert!(!take_released(11_411, 0, 0), "the inode matches nothing"); assert!(take_released_ticket(7), "the ticket still frees its slot"); assert!(!take_released_ticket(7), "and is consumed by it"); // A note with no ticket is left for the inode path. crate::scanout::note_buffer_released(0, 11_366, gndrm_uapi::wire::RELEASE_FENCE); assert!(!take_released_ticket(0), "no ticket, no ticket match"); - assert!(take_released(11_366, 0), "but the inode path still has it"); + assert!(take_released(11_366, 0, 0), "but the inode path still has it"); + } + + /// A note naming an earlier frame than the park's cannot answer it by inode + /// or by order; a note naming a later frame still can. + #[test] + fn a_note_for_an_earlier_ticket_does_not_answer_a_later_park() { + let _serial = fresh_notes(); + // Buffer 5 000 parked as ticket 10, expired, committed again as 13: the + // late note for 10 names the same buffer and must not free 13. + note_buffer_released(10, 5_000, gndrm_uapi::wire::RELEASE_FENCE); + assert!(!take_released(5_000, 0, 13), "a stale note for the same buffer"); + assert!(!take_any_released(0, 13), "and it was dropped, not left for order"); + + // A note for a commit that was never parked (ticket 20), with the oldest + // park at 21: skipped by order, while a note for 22 behind it pays. + note_buffer_released(20, 6_000, gndrm_uapi::wire::RELEASE_FENCE); + note_buffer_released(22, 6_001, gndrm_uapi::wire::RELEASE_FENCE); + assert!(take_any_released(0, 21), "the newer note answers the older park"); + assert!(!take_any_released(0, 21), "the older note does not"); + assert!(take_released_ticket(20), "and is still there for its own ticket"); } /// Its own note, not one an earlier test happened to leave: the suite runs @@ -1029,12 +1069,12 @@ mod tests { note_buffer_released(0, ino, gndrm_uapi::wire::RELEASE_FENCE); } // Identity never matches this ino, so the slot would wait out its bound. - assert!(!take_released(9_999, 0), "no note names this inode"); + assert!(!take_released(9_999, 0, 0), "no note names this inode"); // By order the oldest note is spent, and only one per call. - assert!(take_any_released(0), "the oldest note is available"); - assert!(take_any_released(0)); - assert!(take_any_released(0)); - assert!(!take_any_released(0), "and the list is spent"); + assert!(take_any_released(0, 0), "the oldest note is available"); + assert!(take_any_released(0, 0)); + assert!(take_any_released(0, 0)); + assert!(!take_any_released(0, 0), "and the list is spent"); } /// A note may be up to one display period older than the park's commit — the @@ -1048,13 +1088,13 @@ mod tests { note_buffer_released(0, 8_500, gndrm_uapi::wire::RELEASE_FENCE); // Parked two periods after the note: out of the margin, and dropped. let parked = crate::proxy::monotonic_ns() + 2 * period; - assert!(!take_any_released(parked), "older than one frame"); - assert!(!take_any_released(parked), "and it is dropped, not kept"); + assert!(!take_any_released(parked, 0), "older than one frame"); + assert!(!take_any_released(parked, 0), "and it is dropped, not kept"); // Within the margin it is usable: that is the case the floor exists for. note_buffer_released(0, 8_501, gndrm_uapi::wire::RELEASE_FENCE); let within = crate::proxy::monotonic_ns() + period / 2; - assert!(take_any_released(within), "inside the one-frame margin"); + assert!(take_any_released(within, 0), "inside the one-frame margin"); } #[test] From 735beb76f767708a159aa041633957b3c9881a1b Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sat, 14 Nov 2026 09:36:30 +0530 Subject: [PATCH 3/4] presenter: a new mark sender starts the ticket order again MARK_LAST_TICKET is a process-wide static that was never reset, and the order match refuses any mark whose ticket is not above it, before removing the mark from the queue. The shim's TICKET_SEQ starts again at 1 in every game process. So a second game in the same app process had every order match refused as skew, and the refused mark stayed at the head of PRESENT_PENDING, blocking the marks behind it. Its notes left without a ticket wherever the inode did not match. The previous game's unclaimed marks could also pair by inode with a recycled buffer and carry the old game's ticket. A subscribe that asks for present ids now clears the pending marks and the last ticket together, under the queue's lock. Only the shim subscribes with that cap, and it subscribes once per connection before any mark. A reconnect of the same process loses at most the marks in flight; their notes carry no ticket. --- modules/gndrm-android/src/asc.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/modules/gndrm-android/src/asc.rs b/modules/gndrm-android/src/asc.rs index 16542aa..1f3a0e7 100644 --- a/modules/gndrm-android/src/asc.rs +++ b/modules/gndrm-android/src/asc.rs @@ -627,6 +627,19 @@ fn take_present_mark(ino: u64) -> (u64, u64) { } } +/// Forget every mark and the ticket order they were checked against. +/// +/// Tickets are minted per shim process and start again at 1 in the next one, so +/// the previous process's highest ticket would refuse every order match of the +/// new one as skew, and its unclaimed marks would sit at the head of the queue. +/// A reconnect of the same process loses at most the marks in flight, whose notes +/// then carry no ticket. +fn reset_present_marks() { + let mut pending = PRESENT_PENDING.lock().unwrap_or_else(|e| e.into_inner()); + pending.clear(); + MARK_LAST_TICKET.store(0, Ordering::Relaxed); +} + /// Report a completed present to the shim: queued here, written by the channel. fn note_present_done(id: u64, latch_ns: u64) { if id == 0 { @@ -3224,6 +3237,8 @@ pub fn serve_scanout( } } if sub.caps & CAP_PRESENT_ID != 0 { + // A new mark sender, whose tickets may start a new sequence. + reset_present_marks(); PRESENT_AWARE.store(true, Ordering::Relaxed); PRESENT_FD.store(present_fd, Ordering::Relaxed); } From 68ea3b087a5aca5de3958394b6ca44e0d66e3675 Mon Sep 17 00:00:00 2001 From: Utkarsh Dalal Date: Sat, 14 Nov 2026 09:37:56 +0530 Subject: [PATCH 4/4] presenter: the LsfgConfig request fits the daemon's read buffer INBOUND_MAX says every inbound frame must fit, but it left out LsfgConfig. The shim sends one from lsfg_config() on the scan-out channel, and serve_scanout decodes it from the same INBOUND_MAX buffer. At 288 bytes against a 32-byte buffer, SEQPACKET truncated the request to 32, LsfgConfig::decode wanted the full length, and the request was logged as an unknown frame and never answered. The shim only got settings when they rode along with the subscribe answer, and otherwise fell back to its env defaults. LsfgConfig is now part of INBOUND_MAX and of the test that guards it. The two readers that use the constant (serve_scanout, drain_retired) now hold 288 bytes on the stack instead of 32. --- modules/gndrm-uapi/src/wire.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/gndrm-uapi/src/wire.rs b/modules/gndrm-uapi/src/wire.rs index eb36fbc..0ed9b0f 100644 --- a/modules/gndrm-uapi/src/wire.rs +++ b/modules/gndrm-uapi/src/wire.rs @@ -749,6 +749,9 @@ pub const INBOUND_MAX: usize = { if PresentMark::LEN > m { m = PresentMark::LEN; } + if LsfgConfig::LEN > m { + m = LsfgConfig::LEN; + } m }; @@ -1110,6 +1113,7 @@ mod tests { FrameCapSubscribe::LEN, FrameAcquired::LEN, PresentMark::LEN, + LsfgConfig::LEN, ] { assert!( len <= INBOUND_MAX,