From 33f05ac891c96fe7bb61bde2c6fed3bc9050afb6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 12:47:40 +1100 Subject: [PATCH] drm/apple: afk: limit the compact replies waiting to be sent 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 --- drivers/gpu/drm/apple/afk-parser-test.c | 99 +++++++++++++++++++++++++ drivers/gpu/drm/apple/afk.c | 56 ++++++++++++-- drivers/gpu/drm/apple/afk.h | 2 + 3 files changed, 152 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/afk-parser-test.c b/drivers/gpu/drm/apple/afk-parser-test.c index c1a956a6616f1..99873d53329d1 100644 --- a/drivers/gpu/drm/apple/afk-parser-test.c +++ b/drivers/gpu/drm/apple/afk-parser-test.c @@ -182,6 +182,104 @@ static void afk_compact_service_call_report_test(struct kunit *test) KUNIT_EXPECT_EQ(test, cap.pa, 0x3400); } +/* + * Replies to firmware calls wait on the endpoint's transmit queue. While it + * makes no progress, further calls are taken only as long as their replies + * fit the room of one message. + */ +struct compact_reply_gate { + struct work_struct work; + struct completion open; +}; + +static void compact_reply_gate_fn(struct work_struct *work) +{ + struct compact_reply_gate *gate = + container_of(work, struct compact_reply_gate, work); + + wait_for_completion(&gate->open); +} + +static int compact_call_count_cb(struct apple_epic_service *service, u16 group, + u32 command, const void *data, size_t size, + void *reply, size_t room) +{ + unsigned int *count = service->cookie; + + (*count)++; + return 0; +} + +static const struct apple_epic_service_ops compact_call_count_ops = { + .name = "compact-call-test", + .call_v2 = compact_call_count_cb, +}; + +static void afk_compact_call_reply_budget_test(struct kunit *test) +{ + /* Four calls of this size take all the room; the timestamp and sub-header precede. */ + const size_t payload_size = AFK_COMPACT_MESSAGE_MAX / 4; + const size_t wire_size = 16 + payload_size; + struct apple_dcp_afkep *ep = test->priv; + struct compact_reply_gate gate; + struct epic_compact_sub_hdr *sub; + struct epic_service_call *call; + unsigned int handled = 0, i; + u8 *wire; + + wire = kunit_kzalloc(test, wire_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, wire); + sub = (void *)(wire + 8); + sub->category = 1; + sub->type = EPIC_SUBTYPE_STD_SERVICE; + call = (void *)((u8 *)(sub + 1) + sizeof(struct epic_compact_desc)); + call->command = cpu_to_le32(4); + call->data_len = cpu_to_le32(payload_size - sizeof(struct epic_compact_desc) - + sizeof(*call)); + call->magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC); + + ep->services[0].ops = &compact_call_count_ops; + ep->services[0].cookie = &handled; + ep->tx_wq = alloc_ordered_workqueue("afk-parser-test-tx", 0); + KUNIT_ASSERT_NOT_NULL(test, ep->tx_wq); + + /* The queue is ordered: nothing behind the gate runs until it opens. */ + INIT_WORK_ONSTACK(&gate.work, compact_reply_gate_fn); + init_completion(&gate.open); + queue_work(ep->tx_wq, &gate.work); + + /* A call left without a reply must hold off a coordinated sleep. */ + ep->dcp->v27_sleep_transport = true; + for (i = 0; i < 4; i++) + afk_recv_compact_message(ep, 1, wire, wire_size); + KUNIT_EXPECT_FALSE(test, ep->sleep_poisoned); + afk_recv_compact_message(ep, 1, wire, wire_size); + KUNIT_EXPECT_EQ(test, handled, 4U); + KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 4); + KUNIT_EXPECT_TRUE(test, ep->sleep_poisoned); + + /* A small call no longer fits either. */ + call->data_len = cpu_to_le32(0); + afk_recv_compact_message(ep, 1, wire, + 16 + sizeof(struct epic_compact_desc) + sizeof(*call)); + KUNIT_EXPECT_EQ(test, handled, 4U); + KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 4); + + /* The replies fail without a transmit ring, which frees their room. */ + complete(&gate.open); + flush_workqueue(ep->tx_wq); + KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 0); + + afk_recv_compact_message(ep, 1, wire, + 16 + sizeof(struct epic_compact_desc) + sizeof(*call)); + KUNIT_EXPECT_EQ(test, handled, 5U); + + destroy_workqueue(ep->tx_wq); + ep->tx_wq = NULL; + destroy_work_on_stack(&gate.work); + KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 0); +} + /* * Slot reuse for re-announced compact services. The firmware re-announces a * service under a fresh interface on every DP relink without closing the old @@ -341,6 +439,7 @@ static void afk_compact_cut_before_wildcard_test(struct kunit *test) static struct kunit_case afk_parser_cases[] = { KUNIT_CASE(afk_serialized_report_test), KUNIT_CASE(afk_compact_service_call_report_test), + KUNIT_CASE(afk_compact_call_reply_budget_test), KUNIT_CASE(afk_compact_header_test), KUNIT_CASE(afk_short_epic_test), KUNIT_CASE(afk_short_reply_test), diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 724a78c4bb93c..c354ae19a01db 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -1116,6 +1116,43 @@ struct afk_compact_reply { u8 data[]; }; +/* + * Replies wait on the transmit queue for ring space that only the firmware + * frees, while the receive worker keeps taking calls, so what is queued + * needs a limit of its own. One message is at most AFK_COMPACT_MESSAGE_MAX + * bytes and the receive side assembles one at a time; the queued replies + * together get the same room. A reply is never larger than its call, so + * any single call fits when nothing else is queued, and the calls the + * firmware makes are a small fraction of that even in a burst. + */ +static bool afk_compact_reply_reserve(struct apple_dcp_afkep *ep, size_t size) +{ + guard(spinlock_irqsave)(&ep->lock); + if (size > AFK_COMPACT_MESSAGE_MAX || + ep->compact_reply_bytes > AFK_COMPACT_MESSAGE_MAX - size) + return false; + ep->compact_reply_bytes += size; + return true; +} + +static void afk_compact_reply_release(struct apple_dcp_afkep *ep, size_t size) +{ + guard(spinlock_irqsave)(&ep->lock); + ep->compact_reply_bytes -= size; +} + +/* + * A call that gets no reply leaves the firmware waiting on it, so a + * coordinated sleep must not run against it afterwards. + */ +static void afk_compact_call_unanswered(struct apple_dcp_afkep *ep) +{ + if (!ep->dcp->v27_sleep_transport) + return; + guard(spinlock_irqsave)(&ep->lock); + ep->sleep_poisoned = true; +} + static void afk_compact_reply_worker(struct work_struct *work) { struct afk_compact_reply *r = container_of(work, struct afk_compact_reply, work); @@ -1127,10 +1164,9 @@ static void afk_compact_reply_worker(struct work_struct *work) dev_err(r->ep->dcp->dev, "AFK[ep:%02x]: failed to reply to compact service call: %d\n", r->ep->endpoint, ret); - if (ret && r->ep->dcp->v27_sleep_transport) { - guard(spinlock_irqsave)(&r->ep->lock); - r->ep->sleep_poisoned = true; - } + if (ret) + afk_compact_call_unanswered(r->ep); + afk_compact_reply_release(r->ep, r->size); atomic_dec_return_release(&r->ep->sleep_jobs); kvfree(r); } @@ -1167,9 +1203,19 @@ static void afk_recv_handle_compact_call(struct apple_dcp_afkep *ep, group = le16_to_cpu(call->group); command = le32_to_cpu(call->command); + if (!afk_compact_reply_reserve(ep, payload_size)) { + dev_warn_ratelimited(ep->dcp->dev, + "AFK[ep:%02x]: too many replies queued, dropping compact service %u call %u:%u\n", + ep->endpoint, channel, group, command); + afk_compact_call_unanswered(ep); + return; + } r = kvzalloc(struct_size(r, data, payload_size), GFP_KERNEL); - if (!r) + if (!r) { + afk_compact_reply_release(ep, payload_size); + afk_compact_call_unanswered(ep); return; + } INIT_WORK(&r->work, afk_compact_reply_worker); r->ep = ep; r->channel = channel; diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 28b31aee6c2e3..e3fd234749359 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -573,6 +573,8 @@ struct apple_dcp_afkep { bool compact_tx_desync; /* Replies to firmware calls, see afk_compact_reply_worker(). */ struct workqueue_struct *tx_wq; + /* Payload bytes of the replies queued there and not yet sent (lock). */ + size_t compact_reply_bytes; /* Accepted RX and queued replies, until each worker has returned. */ atomic_t sleep_jobs; struct mutex compact_tx_lock;