From dcfa3ff436ae43730a6779e0b7860bdae2e19114 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:03:56 -0647 Subject: [PATCH] firmware: samsung: acpm: release a timed-out request's sequence slot A request claims one of the channel's 63 sequence slots in acpm_prepare_xfer() and gives it back in acpm_dequeue_by_polling(), on the path where the answer arrives. The timeout path prints and returns -ETIME with the slot still claimed, and nothing ever releases it, so every timeout costs the channel one slot for good. After 63 of them the channel answers every request with -EBUSY and "ACPM sequence number pool exhausted", and only a reboot brings it back. This is reachable: on a Pixel 9a whose thermal channel stopped answering, the console shows repeated "Timeout! ch:9 s:46 bitmap:600000000000" and "s:47" with the same bitmap. Those two bits are the two requests that timed out together (tx_lock is released before the wait, so two can be outstanding), not a leaked slot; what the log shows is the rate: thermal polls about once a second, so against a stalled firmware the pool is gone in about a minute. The same code serves the PMIC and DVFS channels. The slot cannot simply be released on timeout: the firmware may still answer, and the answer carries that sequence number, so a slot handed to the next request would deliver the old answer as the new request's. Mark the slot abandoned instead, and let acpm_get_rx() release it when the late answer arrives, dropping that answer. Take that decision under rx_lock after one more look at completed: another thread draining the queue may have saved this answer just before the deadline, and abandoning the slot after that would leak it, since no further answer will come; the request has then succeeded, late, and is treated as such. A firmware that never answers at all still exhausts the pool, which is then an honest report of a dead channel rather than the result of bookkeeping. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- drivers/firmware/samsung/exynos-acpm.c | 46 ++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/drivers/firmware/samsung/exynos-acpm.c b/drivers/firmware/samsung/exynos-acpm.c index dfac2a004c..c042110ea7 100644 --- a/drivers/firmware/samsung/exynos-acpm.c +++ b/drivers/firmware/samsung/exynos-acpm.c @@ -118,6 +118,9 @@ struct acpm_queue { * @cmd: pointer to where the data shall be saved. * @cmdcnt: allocated capacity of the @cmd buffer in 32-bit words. * @rxcnt: expected length of the response in 32-bit words. + * @abandoned: flag indicating that the requester gave up on this sequence + * number after a timeout, so a late response is dropped and the + * sequence slot is released when it arrives * @completed: flag indicating if the firmware response has been fully * processed. */ @@ -125,6 +128,7 @@ struct acpm_rx_data { u32 *cmd __counted_by_ptr(cmdcnt); size_t cmdcnt; size_t rxcnt; + bool abandoned; bool completed; }; @@ -277,7 +281,17 @@ static int acpm_get_rx(struct acpm_chan *achan, const struct acpm_xfer *xfer, seqnum = rx_seqnum - 1; rx_data = &achan->rx_data[seqnum]; - if (rx_data->rxcnt) { + /* Pairs with smp_store_release() in acpm_dequeue_by_polling(). */ + if (smp_load_acquire(&rx_data->abandoned)) { + /* + * The requester timed out and gave up. Drop the + * answer and give the sequence slot back; the slot + * was held until now so that it could not be reused + * while this answer was still owed. + */ + rx_data->abandoned = false; + clear_bit_unlock(seqnum, achan->bitmap_seqnum); + } else if (rx_data->rxcnt) { if (rx_seqnum == tx_seqnum) { __ioread32_copy(xfer->rxd, addr, xfer->rxcnt); /* @@ -366,8 +380,33 @@ static int acpm_dequeue_by_polling(struct acpm_chan *achan, udelay(20); } while (ktime_before(ktime_get(), timeout)); - dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx.\n", - achan->id, seqnum, achan->bitmap_seqnum[0]); + /* + * Abandon the request rather than hold its sequence slot for good: + * acpm_get_rx() releases the slot if the firmware answers later, and + * drops that answer. The slot cannot be released here, because it + * would be handed to the next request while the firmware still owes + * an answer carrying the same sequence number, and that answer would + * then be delivered as the new request's. + * + * Decide under rx_lock, after one more look at completed: another + * thread draining the queue may have saved this answer just before + * the deadline, and marking the slot abandoned after that would leak + * it, since no further answer will come to release it. In that case + * the request has in fact succeeded, late. + */ + scoped_guard(mutex, &achan->rx_lock) { + /* Pairs with smp_store_release() in acpm_get_rx(). */ + if (smp_load_acquire(&achan->rx_data[seqnum - 1].completed)) { + if (!native_match) + acpm_get_saved_rx(achan, xfer, seqnum); + clear_bit_unlock(seqnum - 1, achan->bitmap_seqnum); + return 0; + } + dev_err(dev, "Timeout! ch:%u s:%u bitmap:%lx.\n", + achan->id, seqnum, achan->bitmap_seqnum[0]); + /* Pairs with smp_load_acquire() in acpm_get_rx(). */ + smp_store_release(&achan->rx_data[seqnum - 1].abandoned, true); + } return -ETIME; } @@ -444,6 +483,7 @@ static int acpm_prepare_xfer(struct acpm_chan *achan, /* Clear data for upcoming responses */ rx_data = &achan->rx_data[bit]; + rx_data->abandoned = false; rx_data->completed = false; memset(rx_data->cmd, 0, sizeof(*rx_data->cmd) * rx_data->cmdcnt); /* zero means no response expected */