Skip to content

Commit 5bd398e

Browse files
Youssef SamirMani-Sadhasivam
authored andcommitted
bus: mhi: host: Detect events pointing to unexpected TREs
When a remote device sends a completion event to the host, it contains a pointer to the consumed TRE. The host uses this pointer to process all of the TREs between it and the host's local copy of the ring's read pointer. This works when processing completion for chained transactions, but can lead to nasty results if the device sends an event for a single-element transaction with a read pointer that is multiple elements ahead of the host's read pointer. For instance, if the host accesses an event ring while the device is updating it, the pointer inside of the event might still point to an old TRE. If the host uses the channel's xfer_cb() to directly free the buffer pointed to by the TRE, the buffer will be double-freed. This behavior was observed on an ep that used upstream EP stack without 'commit 6f18d17 ("bus: mhi: ep: Update read pointer only after buffer is written")'. Where the device updated the events ring pointer before updating the event contents, so it left a window where the host was able to access the stale data the event pointed to, before the device had the chance to update them. The usual pattern was that the host received an event pointing to a TRE that is not immediately after the last processed one, so it got treated as if it was a chained transaction, processing all of the TREs in between the two read pointers. This commit aims to harden the host by ensuring transactions where the event points to a TRE that isn't local_rp + 1 are chained. Fixes: 1d3173a ("bus: mhi: core: Add support for processing events from client device") Signed-off-by: Youssef Samir <[email protected]> [mani: added stable tag and reworded commit message] Signed-off-by: Manivannan Sadhasivam <[email protected]> Reviewed-by: Jeff Hugo <[email protected]> Cc: [email protected] Link: https://patch.msgid.link/[email protected]
1 parent 0d63055 commit 5bd398e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

drivers/bus/mhi/host/main.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl,
602602
{
603603
dma_addr_t ptr = MHI_TRE_GET_EV_PTR(event);
604604
struct mhi_ring_element *local_rp, *ev_tre;
605-
void *dev_rp;
605+
void *dev_rp, *next_rp;
606606
struct mhi_buf_info *buf_info;
607607
u16 xfer_len;
608608

@@ -621,6 +621,16 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl,
621621
result.dir = mhi_chan->dir;
622622

623623
local_rp = tre_ring->rp;
624+
625+
next_rp = local_rp + 1;
626+
if (next_rp >= tre_ring->base + tre_ring->len)
627+
next_rp = tre_ring->base;
628+
if (dev_rp != next_rp && !MHI_TRE_DATA_GET_CHAIN(local_rp)) {
629+
dev_err(&mhi_cntrl->mhi_dev->dev,
630+
"Event element points to an unexpected TRE\n");
631+
break;
632+
}
633+
624634
while (local_rp != dev_rp) {
625635
buf_info = buf_ring->rp;
626636
/* If it's the last TRE, get length from the event */

0 commit comments

Comments
 (0)