Skip to contentSkip to conversation

bus: mhi: ep: aggregate raw buffer completions

PATCH In review Senna 2 participants 2 messages Oct 28 – Oct 30 2 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Senna

author
#0 lore raw

PATCH 1/1 bus: mhi: ep: aggregate raw buffer completions

Aggregate raw-buffer completions in the endpoint core and invoke the client callback once after all accepted TREs complete. This prevents the loopback worker from freeing a buffer still referenced by an earlier asynchronous submission.

Fixes 0b1883ca75c0 ("bus: mhi: ep: clients: Add loopback driver for data path testing")
---
 drivers/bus/mhi/ep/clients/loopback.c |   4 +
 drivers/bus/mhi/ep/main.c             | 131 ++++++++++++++++++++++----
 include/linux/mhi_ep.h                |  19 ++--
 3 files changed, 130 insertions(+), 24 deletions(-)
drivers/bus/mhi/ep/clients/loopback.c +4 -0
index c7452428d0b7..6f6a1b5a51c4 100644
@@ -71,6 +71,10 @@ static void mhi_ep_loopback_ul_callback(struct mhi_ep_device *mhi_dev,
 static void mhi_ep_loopback_dl_callback(struct mhi_ep_device *mhi_dev,
 					struct mhi_result *mhi_res)
 {
+	if (mhi_res->transaction_status)
+		dev_err(&mhi_dev->dev, "Downlink transfer failed: %d\n",
+			mhi_res->transaction_status);
+
 	kfree(mhi_res->buf_addr);
 }
 
drivers/bus/mhi/ep/main.c +115 -16

210 lines not on this page · raw message

include/linux/mhi_ep.h +11 -8
index 852f331f03fd..ac4f484373ee 100644
@@ -307,16 +307,19 @@ int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb);
 /**
  * mhi_ep_queue_buf - Transfer buffer contents to host over MHI Endpoint
  * @mhi_dev: Device associated with the DL channel
- * @buf: Buffer to be queued. On success, ownership passes to the MHI stack;
- *       the caller must not free @buf until the DL transfer callback fires
- *       with result->buf_addr equal to @buf. On failure, the caller retains
- *       ownership and must free @buf.
- *       Note: if @len spans multiple host DL TREs, the DL transfer callback
- *       fires once per TRE, each time with result->buf_addr equal to @buf.
+ * @buf: Buffer to be queued. If at least one TRE is queued, ownership passes
+ *       to the MHI stack and the DL transfer callback fires once after all
+ *       queued TREs complete, with result->buf_addr equal to @buf and
+ *       result->bytes_xferd equal to the total completed bytes. If queuing
+ *       fails before any TRE is accepted, the caller retains ownership.
+ *       A failure after a TRE has been accepted is reported through
+ *       result->transaction_status in the callback.
  * @len: Size of the buffer
  *
- * Return: 0 if the buffer contents have been transferred successfully, a
- * negative error code otherwise.
+ * Return: 0 if one or more TREs were accepted, including when a later TRE
+ * submission fails. In that case, the callback reports the error after all
+ * accepted TREs complete. A negative error code is returned if no TRE was
+ * accepted and no callback will be issued.
  */
 int mhi_ep_queue_buf(struct mhi_ep_device *mhi_dev, void *buf, size_t len);
 
Signature · 1 line
2.56.0

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): CAOCq4u-nbp5COcbAJ8S27DsexZzHGNbqqXE8ktmpO_RkJ=NzqQ@mail.gmail.com

#3 lore raw
Quoted 1 line · Senna

My bad i didn't reply to the list

Adding the last review here

"Could you share how you are hit this? With the current in-tree host driver UL and DL TREs are same size, Each echoed buffer fits in one DL TRE, For a multi tre case to hit a different host client would be needed which posts smaller DL TREs.

That said, the underlying problem with the mhi_ep_queue (for both buf and skb cases) is genuine,

With the in-tree pcie-epf-mhi, write sync completions are in order (synchronous for iATU, Single TX channel for dma), so invoking the xfer_cb only for the final EOT TRE would be enough,

Can check the available TREs before queueing the first one to avoid running into the TREs exhaustion mid-transfer something like this should work

```
/* Check that the TREs the host has queued can hold @len before queueing any */
static bool mhi_ep_queue_has_room(struct mhi_ep_ring *ring,
				  struct mhi_ep_chan *mhi_chan, size_t len)
{
	size_t rd = mhi_chan->rd_offset;

	while (len) {
		struct mhi_ring_element *el;
		if (rd == ring->wr_offset)
			return false;

		el = &ring->ring_cache[rd];
		len -= min_t(size_t, len, MHI_TRE_DATA_GET_LEN(el));
	rd = (rd + 1) % ring->ring_size;
	}

	return true;
}
```

and use this function in mhi_ep_queue.

Thanks,
Sumit"

Quoted 8 lines

Hi Sumit,

Thank you for the code review. I found this bug by reading the code, not on hardware, and I agree that the in-tree host setup will not trigger it.

I will rework this for v2 with the lines you suggest: check TRE room up front with a helper like mhi_ep_queue_has_room(), and invoke xfer_cb only for the final EOT TRE.

A question on error handling: even with the pre-check, write_async could still fail on a later TRE after an earlier one has already been queued, so that earlier TRE would still be using the buffer after the caller gets an error. Will v2 need to handle that case, or is it acceptable to leave it because the write completions are in order with the in-tree pcie-epf-mhi? I’m thinking of leaving it as-is but I would like your view.

On the error case: I don't think in-order completion covers it. If write_async fails on a later TRE, the earlier TREs are still in flight, so if we return an error and the caller frees the buffer, those transfers can still read freed memory, independent of ordering. The host also sees OVERFLOW events without a closing EOT. With the EOT-only callback the double free goes away, but this window remains.

That said, the remaining failures are local ones (DMA mapping, descriptor prep, allocation), so I'm fine leaving it out of v2 as long as it's documented, e.g. a comment in mhi_ep_queue() and a note in the mhi_ep_queue_buf() kdoc.

The proper fix is to not start any transfer until all TREs for the buffer are ready, and then submit them as one batch. Reordering the loop in mhi_ep_queue() alone isn't enough, since the failures happen inside write_async. It needs the controller op to take all the buffers at once, so the EPF can prepare a single batched descriptor: either it fails before anything is started, or the whole buffer is in flight with one completion.

This lines up with a series I have in review that adds batched DMA transfers for MHI EP [1]. Mani's suggestion there is to rework the read callbacks to take multiple buffers and let the EPF decide how to transfer them. The dmaengine API shape is still being discussed, but once it settles, doing the same for write_async would close this

[1] https://lore.kernel.org/mhi/[email protected]/

Thanks,
Sumit> Thanks,

Quoted 2 lines

Senna