Skip to content

Commit 4decbbc

Browse files
ahunter6alexandrebelloni
authored andcommitted
i3c: mipi-i3c-hci: Fix race in DMA ring enqueue for parallel xfers
The I3C subsystem allows multiple transfers to be queued concurrently. However, the MIPI I3C HCI driver's DMA enqueue path, hci_dma_queue_xfer(), lacks sufficient serialization. In particular, the allocation of the enqueue_ptr and its subsequent update in the RING_OPERATION1 register, must be done atomically. Otherwise, for example, it would be possible for 2 transfers to be allocated the same enqueue_ptr. Extend the use of the existing spinlock for that purpose. Keep a count of the number of xfers enqueued so that it is easy to determine if the ring has enough space. Fixes: 9ad9a52 ("i3c/master: introduce the mipi-i3c-hci driver") Cc: stable@vger.kernel.org Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Reviewed-by: Frank Li <Frank.Li@nxp.com> Link: https://patch.msgid.link/20260306072451.11131-6-adrian.hunter@intel.com Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
1 parent fa12bb9 commit 4decbbc

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

  • drivers/i3c/master/mipi-i3c-hci

drivers/i3c/master/mipi-i3c-hci/dma.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct hci_rh_data {
129129
dma_addr_t xfer_dma, resp_dma, ibi_status_dma, ibi_data_dma;
130130
unsigned int xfer_entries, ibi_status_entries, ibi_chunks_total;
131131
unsigned int xfer_struct_sz, resp_struct_sz, ibi_status_sz, ibi_chunk_sz;
132-
unsigned int done_ptr, ibi_chunk_ptr;
132+
unsigned int done_ptr, ibi_chunk_ptr, xfer_space;
133133
struct hci_xfer **src_xfers;
134134
struct completion op_done;
135135
};
@@ -260,6 +260,7 @@ static void hci_dma_init_rh(struct i3c_hci *hci, struct hci_rh_data *rh, int i)
260260

261261
rh->done_ptr = 0;
262262
rh->ibi_chunk_ptr = 0;
263+
rh->xfer_space = rh->xfer_entries;
263264
}
264265

265266
static void hci_dma_init_rings(struct i3c_hci *hci)
@@ -470,7 +471,7 @@ static int hci_dma_queue_xfer(struct i3c_hci *hci,
470471
struct hci_rings_data *rings = hci->io_data;
471472
struct hci_rh_data *rh;
472473
unsigned int i, ring, enqueue_ptr;
473-
u32 op1_val, op2_val;
474+
u32 op1_val;
474475
int ret;
475476

476477
ret = hci_dma_map_xfer_list(hci, rings->sysdev, xfer_list, n);
@@ -481,6 +482,14 @@ static int hci_dma_queue_xfer(struct i3c_hci *hci,
481482
ring = 0;
482483
rh = &rings->headers[ring];
483484

485+
spin_lock_irq(&hci->lock);
486+
487+
if (n > rh->xfer_space) {
488+
spin_unlock_irq(&hci->lock);
489+
hci_dma_unmap_xfer(hci, xfer_list, n);
490+
return -EBUSY;
491+
}
492+
484493
op1_val = rh_reg_read(RING_OPERATION1);
485494
enqueue_ptr = FIELD_GET(RING_OP1_CR_ENQ_PTR, op1_val);
486495
for (i = 0; i < n; i++) {
@@ -518,22 +527,10 @@ static int hci_dma_queue_xfer(struct i3c_hci *hci,
518527
xfer->ring_entry = enqueue_ptr;
519528

520529
enqueue_ptr = (enqueue_ptr + 1) % rh->xfer_entries;
521-
522-
/*
523-
* We may update the hardware view of the enqueue pointer
524-
* only if we didn't reach its dequeue pointer.
525-
*/
526-
op2_val = rh_reg_read(RING_OPERATION2);
527-
if (enqueue_ptr == FIELD_GET(RING_OP2_CR_DEQ_PTR, op2_val)) {
528-
/* the ring is full */
529-
hci_dma_unmap_xfer(hci, xfer_list, n);
530-
return -EBUSY;
531-
}
532530
}
533531

534-
/* take care to update the hardware enqueue pointer atomically */
535-
spin_lock_irq(&hci->lock);
536-
op1_val = rh_reg_read(RING_OPERATION1);
532+
rh->xfer_space -= n;
533+
537534
op1_val &= ~RING_OP1_CR_ENQ_PTR;
538535
op1_val |= FIELD_PREP(RING_OP1_CR_ENQ_PTR, enqueue_ptr);
539536
rh_reg_write(RING_OPERATION1, op1_val);
@@ -601,6 +598,7 @@ static void hci_dma_xfer_done(struct i3c_hci *hci, struct hci_rh_data *rh)
601598
{
602599
u32 op1_val, op2_val, resp, *ring_resp;
603600
unsigned int tid, done_ptr = rh->done_ptr;
601+
unsigned int done_cnt = 0;
604602
struct hci_xfer *xfer;
605603

606604
for (;;) {
@@ -632,10 +630,12 @@ static void hci_dma_xfer_done(struct i3c_hci *hci, struct hci_rh_data *rh)
632630

633631
done_ptr = (done_ptr + 1) % rh->xfer_entries;
634632
rh->done_ptr = done_ptr;
633+
done_cnt += 1;
635634
}
636635

637636
/* take care to update the software dequeue pointer atomically */
638637
spin_lock(&hci->lock);
638+
rh->xfer_space += done_cnt;
639639
op1_val = rh_reg_read(RING_OPERATION1);
640640
op1_val &= ~RING_OP1_CR_SW_DEQ_PTR;
641641
op1_val |= FIELD_PREP(RING_OP1_CR_SW_DEQ_PTR, done_ptr);

0 commit comments

Comments
 (0)