Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/24-release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions icd_shim/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
157 changes: 115 additions & 42 deletions icd_shim/src/scanout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ pub fn lsfg_config() -> Option<LsfgConfig> {
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.
Expand Down Expand Up @@ -529,6 +531,17 @@ fn exchange_req(fd: RawFd, req: ScanoutAllocReq) -> Result<ScanoutBuf, Failure>
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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -794,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
Expand All @@ -804,29 +831,49 @@ 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
/// every older note for the same buffer.
///
/// `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;
}
Expand All @@ -836,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 {
Expand Down Expand Up @@ -915,16 +962,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.
Expand All @@ -950,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
Expand All @@ -975,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
Expand Down Expand Up @@ -1011,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
Expand All @@ -1030,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]
Expand Down Expand Up @@ -1216,6 +1274,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);
Expand All @@ -1239,6 +1308,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)
Expand All @@ -1248,6 +1318,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);
Expand Down
15 changes: 15 additions & 0 deletions modules/gndrm-android/src/asc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions modules/gndrm-uapi/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,9 @@ pub const INBOUND_MAX: usize = {
if PresentMark::LEN > m {
m = PresentMark::LEN;
}
if LsfgConfig::LEN > m {
m = LsfgConfig::LEN;
}
m
};

Expand Down Expand Up @@ -1110,6 +1113,7 @@ mod tests {
FrameCapSubscribe::LEN,
FrameAcquired::LEN,
PresentMark::LEN,
LsfgConfig::LEN,
] {
assert!(
len <= INBOUND_MAX,
Expand Down
Loading