Skip to content

drm/apple: afk: limit the compact replies waiting to be sent - #2937

Merged
RyanTheTide merged 1 commit into
mainfrom
hardening/afk-compact-20261123
Nov 26, 2026
Merged

RyanTheTide merged 1 commit into
mainfrom
hardening/afk-compact-20261123

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Compact service calls from the DCP firmware each get a reply buffer, queued on the endpoint's ordered transmit workqueue. A reply can wait there for ring space while the receive side keeps taking calls, and nothing limited how many replies were queued.

Change

  • The payload bytes of replies that are queued and not yet sent are counted per endpoint under ep->lock and capped at AFK_COMPACT_MESSAGE_MAX, the largest single message (also what the receive side holds in assembly). Any one valid call is always taken when nothing is queued. A call beyond the cap is dropped with a rate-limited warning before anything is allocated or the handler runs.
  • A call that is dropped, or whose reply buffer cannot be allocated, is never answered. With the coordinated sleep transport (v27_sleep_transport) the endpoint is now marked sleep_poisoned on both paths, as the reply worker already does when a send fails, so a coordinated sleep does not proceed with a call outstanding.

No parameters, no chip/board/firmware gates.

Testing

  • KUnit (arm64 qemu) on integration/aurora a7638d5 + this commit: apple-epic-parser and the rest of the C group, 52 run, 51 passed, 1 skipped (apple_g17_rx_util_min_test, needs CONFIG_UCLAMP_TASK).
  • afk_compact_call_reply_budget_test stalls the transmit queue, fills the budget, checks that later calls are dropped until the queue drains, and (new) that a dropped call sets sleep_poisoned with the sleep transport on. With the two new poison calls removed it FAILS at afk-parser-test.c:259.
  • checkpatch --strict: clean.
  • Hardware: see the rows below.

Still to run

  • An external display with hotplug on a Mac whose DCP runs this path with external displays (J713, a desktop on 26.x DCP firmware). Our J473 (M2 desktop) DCP does not bind on its 27.2 firmware on this base, so it cannot exercise this path.

Hardware rows

Pending: one-shot boots of fleet82 + this series on the default cmdline (M1 Pro, M2, J616s, J473), compared with the same rows on the production kernel. They will be added here.

Reviewer detail
  • afk_compact_call_unanswered() takes ep->lock (irqsave) and sets sleep_poisoned only when ep->dcp->v27_sleep_transport; the worker's failed-send path now uses the same helper (no behaviour change there).
  • afk_compact_reply_reserve()/_release() run under ep->lock. A dropped call is not counted in sleep_jobs. The worker releases the reply's bytes after the send whether it succeeded or failed; teardown (destroy_workqueue(tx_wq)) drains through the worker, so the count returns to zero.
  • The malformed-call early return is unchanged: such a call has no valid header to answer.
  • Split out of Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 as asked in review; addresses its afk point (poison on the drop and OOM paths).

Every compact service call from the firmware gets a reply buffer of the
call's size, which is queued on the endpoint's ordered transmit queue.
A reply can wait there for transmit ring space for up to five seconds
while the receive worker keeps taking calls, and nothing limits how many
replies, or how many bytes of them, are queued in the meantime. A
firmware that keeps calling while it does not drain its ring makes the
queue grow without bound.

Count the payload bytes of the replies that are queued and not yet sent,
under the endpoint lock, and take a call only while its reply fits.
The room is AFK_COMPACT_MESSAGE_MAX, the largest single message, which
is also what the receive side holds in assembly: one call of any valid
size is always taken when nothing is queued, and the short calls the
display services receive stay far below it. A call beyond that is
dropped with a rate-limited warning before its buffer is allocated and
before the service handler runs, so it is not counted as a pending job
either. The reply worker returns the room once the send has finished or
failed, which covers the queue flush at teardown as well.

A dropped call, like one whose reply buffer cannot be allocated, is
never answered, which the firmware sees the same way as a reply that
failed to send. With the coordinated sleep transport, mark the endpoint
so that sleep does not proceed, as the reply worker already does for a
failed send.

Add a KUnit case that stalls the transmit queue, feeds calls until the
room is used up and checks that later calls are dropped until the queue
has drained, and that a dropped call holds off the coordinated sleep.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review at 33f05ac.

  1. This resolves Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 finding 1. afk.c:1210 (budget drop) and afk.c:1216 (kvzalloc failure) now call afk_compact_call_unanswered() (afk.c:1148). It sets sleep_poisoned under ep->lock when v27_sleep_transport is set, the same as the failed-send path. The re-check under the lock in the sleep path (afk.c:1594) catches a drop that lands after afk_v27_sleep_check().
  2. The patch changed from Apple drivers: bound firmware-sized work and fix teardown and restore lifetimes (admac, macsmc, aop_motion, aop_devmotion, aop_lpai, afk) #2911 (patch-id 042beca0 vs aed047e8): the helper and the two new call sites were added, the worker was refactored, and the test now asserts that the 5th (dropped) call poisons. Nothing else moved.
  3. Nit, afk.c:1163-1168: two consecutive if (ret) blocks in afk_compact_reply_worker(). Please fold the poison call into the dev_err block.
  4. Nit: the kvzalloc-failure poison (afk.c:1216) has no test coverage. The KUnit case only reaches the budget drop.

Rows owed at this head (none posted): an external display with hotplug on J713 and on a desktop with 26.x DCP firmware; one coordinated s2r cycle on a v27_sleep_transport Mac, to show that sleep is not poisoned in normal use.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR checklist review at 33f05ac891c96fe7bb61bde2c6fed3bc9050afb6.

Checklist Summary

  • §1 No model gating: Pass — applies uniformly to compact service calls; v27_sleep_transport is an existing transport capability flag.
  • §2 Production-ready: Pass — no new parameters or workarounds.
  • §3 Safety: Pass — afk_recv_handle_compact_call() already enforces payload_size >= sizeof(*request_desc) + sizeof(*call) > 0, so zero-byte calls cannot bypass the byte cap; afk_compact_reply_reserve() uses overflow-safe subtraction under ep->lock; both budget-drop (afk.c:1210) and kvzalloc-failure (afk.c:1216) paths now call afk_compact_call_unanswered() to set sleep_poisoned under ep->lock when v27_sleep_transport is active; afk_compact_reply_worker() releases the reserved bytes on both success and failure, and destroy_workqueue(tx_wq) drains queued replies cleanly on teardown.
  • §4 Clean-room & attribution: Pass — human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by:, zero AI trailers in commit.
  • §5 Evidence: Pending — KUnit afk_compact_call_reply_budget_test passes (51/52, 1 unrelated skip), but hardware rows are still owed (external display with hotplug on J713 / 26.x DCP desktop + coordinated s2r on a v27_sleep_transport Mac to confirm normal operation does not poison sleep).
  • §6 Naming: Pass.
  • CI (at review time): host-tests green.

Non-blocking Nits

  1. drivers/gpu/drm/apple/afk.c:1163-1168: two consecutive if (ret) blocks in afk_compact_reply_worker() can be folded into one block.
  2. The kvzalloc OOM poison path (afk.c:1214-1217) is verified by inspection but not directly exercised by KUnit.

Verdict

COMMENT — 0 code/policy blockers and CI is green; waiting on the owed §5 hardware rows before approval.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent source review at 33f05ac891c9. The byte budget is released by the reply worker and exhaustion poisons the prepared sleep path where applicable. The current J473 does not bind its DCP, so a clean boot there does not exercise this callback. Add an admitted J473 27.2 display hotplug/DPMS row alongside a J413 13.5 path that uses the affected transport, and confirm sleep clears the pending-reply state after unwind.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR re-review at 33f05ac891c96fe7bb61bde2c6fed3bc9050afb6 against live integration/aurora (70a00e583b03).

Status & Code Assessment (0 code, CI, or attribution blockers)

  • Not superseded: Live integration/aurora (drivers/gpu/drm/apple/afk.c:1175-1259, afk.h:575) still has unbounded compact reply queuing and does not poison coordinated sleep on OOM/drop.
  • §1 No model gating: Pass — applies uniformly to compact service calls; v27_sleep_transport is an existing transport capability flag.
  • §2 Production-ready & §3 Safety: Pass —
    • afk_recv_handle_compact_call() requires payload_size >= sizeof(*request_desc) + sizeof(*call) > 0 and ep->tx_wq before reserving, so zero-byte calls cannot bypass the budget.
    • afk_compact_reply_reserve() uses overflow-safe subtraction under ep->lock (irqsave); budget-drop and kvzalloc failure both call afk_compact_call_unanswered() to set ep->sleep_poisoned under ep->lock when v27_sleep_transport is active.
    • afk_compact_reply_worker() sets sleep_poisoned (on send failure) and releases compact_reply_bytes before atomic_dec_return_release(&r->ep->sleep_jobs) and kvfree(r); teardown (afk_quiesce()) destroys ep->wq before ep->tx_wq, draining all queued replies cleanly.
  • §4 Clean-room & attribution: Pass — commit 33f05ac891c9 carries human author/committer (Ryan Murray <[email protected]>) and human Signed-off-by: only, zero AI trailers; Assisted-by: is in the PR body.
  • §6 Naming & Live CI: Pass — host-tests is COMPLETED / SUCCESS on 33f05ac891c9, and QEMU arm64 KUnit (afk_compact_call_reply_budget_test) passes.
  • Non-blocking nit: The two consecutive if (ret) blocks in afk_compact_reply_worker() (afk.c:1163-1168) can be folded into one.

Blocker Holding APPROVE / MERGETRAIN-LINUX4

  1. §5 Hardware test & regression rows missing: Zero hardware rows have been posted yet (### Hardware rows still lists fleet82 one-shot boots on M1 Pro, M2, J616s, J473 as Pending). Please post exact-head hardware evidence on the default cmdline exercising the compact-EPIC display path (including external display hotplug/DPMS on a host whose DCP binds this path, plus a coordinated S2R cycle on a v27_sleep_transport Mac confirming normal operation does not poison sleep and clears pending reply state cleanly) with §5 regression numbers (0 SELinux denials, 0 failed units, 0 new kernel errors).

@github-actions github-actions Bot added area:display Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at 33f05ac8: ready to merge

Check Detail
CI at head ok 1 check passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok comment at 33f05ac8
Conflicts ok none
PR text adopt when you next edit add a 'Tested:' line; a 'Risk:' line (door and Macs) (guide)

Areas: area:display
Merge: gh pr merge 2937 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH, then merge at 33f05ac891c9: the code is ready; it needs only a row at this head.

Blockers: none in the code. Missing row: one boot at this head on one of the pending Macs (M1 Pro, M2, J616s or J473) with the compact-EPIC display path in use (internal panel, plus an external display where there is one) and no AFK errors.

Checked: no Apple material, no gates. The compact replies waiting to be sent are bounded. CI green.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 25, 2026 04:36

Superseded by the review at 33f05ac

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 25, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

33f05ac891c9 (batched cleanly in J616s Batch #1 f07c8e067bc2 over main f3bfe5007089, 0 merge-tree diffs across all 3 changed files) J616s (MacBook Pro 16" M4 Pro, T6040, Apple M4 Pro (G16S B1), macOS 26.6.2 fw, guarded one-shot 7.3.0-aurora-pr2905+, default cmdline, stock Stage 2 1.0.0b17, boot_id=e83ea75b) PASS: 0 failed units, Enforcing, 0 new AVC (1 matching baseline), 0 GPU faults, 0 subsystem errors, real rtc1 s2idle 2/2 (success=2 fail=0), TB 0-1 20.0 Gb/s x2 pre/post-sleep, Borealis VK dEQP-VK.api.smoke.* 8/8 (pre & post-sleep) + descriptor_pool.* 6/6, eDP connected pre/post-sleep, /dev/accel/accel0 + ane_t6021 + apple_sio + pmc + dart + hid_magicmouse loaded; stock 7.3.0-aurora-fleet89 restored & verified (7d2519ed).

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-hw-row No hardware row at the PR head (bot) labels Nov 25, 2026
@RyanTheTide
RyanTheTide merged commit 7e8d344 into main Nov 26, 2026
3 of 4 checks passed
@RyanTheTide
RyanTheTide deleted the hardening/afk-compact-20261123 branch November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:display Area from CODEOWNERS (bot) needs-template PR body lacks Tested or Risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants