Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] i2c: omap: DMA wait ordering and cmd_err gating in omap_i2c_xfer_msg() - [Medium] i2c: omap: NULL dereference in omap_i2c_dma_free() on probe error - [Medium] i2c: omap: uninitialized pio_irq_mask and missing DMA cleanup on timeout --
commit 7af8eb0f57b65f48015dfec1a234f795398aca59 Author: Antoni Pokusinski <[email protected]> i2c: omap: add DMA support This commit adds TX and RX DMA transfer support to the OMAP I2C driver for messages of at least 8 bytes whose length is a multiple of the configured FIFO threshold. > diff --git a/drivers/i2c/busses/i2c-omap.c b/drivers/i2c/busses/i2c-omap.c > index 3f7060d57a869..acff27f3f4385 100644 > --- a/drivers/i2c/busses/i2c-omap.c > +++ b/drivers/i2c/busses/i2c-omap.c [ ... ] > @@ -663,8 +837,8 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, > { > struct omap_i2c_dev *omap = i2c_get_adapdata(adap); > unsigned long time_left; > - u16 w; > - int ret; > + u16 w, pio_irq_mask; > + int ret, use_dma; > > dev_dbg(omap->dev, "addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n", > msg->addr, msg->len, msg->flags, stop); [ ... ] > @@ -692,6 +874,27 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, > reinit_completion(&omap->cmd_complete); > omap->cmd_err = 0; > > + if (use_dma) { > + ret = omap_i2c_dma_xfer(omap, msg); > + if (ret) { > + omap_i2c_reset(omap); > + __omap_i2c_init(omap); > + return ret; > + } > + > + omap_i2c_dma_enable_req(omap); > + } else { > + pio_irq_mask = OMAP_I2C_IE_XRDY | OMAP_I2C_IE_RRDY | > + ((omap->fifo_size) ? (OMAP_I2C_IE_RDR | > + OMAP_I2C_IE_XDR) : 0); > + > + if (omap->scheme == OMAP_I2C_SCHEME_0) > + omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, > + omap->iestate | pio_irq_mask); > + else > + omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, pio_irq_mask); > + } [ ... ] > @@ -727,6 +930,16 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, > if (time_after(jiffies, delay)) { > dev_err(omap->dev, "controller timed out " > "waiting for start condition to finish\n"); > + > + if (omap->scheme == OMAP_I2C_SCHEME_0) > + omap_i2c_write_reg(omap, > + OMAP_I2C_IE_REG, > + omap->iestate); > + else > + omap_i2c_write_reg(omap, > + > OMAP_I2C_IP_V2_IRQENABLE_CLR, > + pio_irq_mask); > + > return -ETIMEDOUT; > } > cpu_relax(); [Severity: Medium] Is pio_irq_mask used uninitialized here when use_dma is true? pio_irq_mask is only assigned in the !use_dma branch, which triggers a -Wmaybe-uninitialized warning on this timeout path. If this timeout is reached during a DMA transfer, returning directly also leaves the DMA channel active and leaks the DMA mapping and bounce buffer without calling dmaengine_terminate_sync(), omap_i2c_dma_cleanup() and omap_i2c_dma_disable_req(). > @@ -742,8 +955,15 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, > * into arbitration and we're currently unable to recover from it. > */ > if (!polling) { > - time_left = wait_for_completion_timeout(&omap->cmd_complete, > - OMAP_I2C_TIMEOUT); > + time_left = OMAP_I2C_TIMEOUT; > + > + if (use_dma) > + time_left = > wait_for_completion_timeout(&omap->dma->complete, > + time_left); > + > + if (time_left && !omap->cmd_err) > + time_left = > wait_for_completion_timeout(&omap->cmd_complete, > + time_left); > } else { > do { > omap_i2c_wait(omap); > @@ -753,7 +973,23 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, > time_left = !ret; > } > > - if (time_left == 0) { > + if (use_dma) { > + if (!time_left || omap->cmd_err) > + dmaengine_terminate_sync(omap->dma->chan_using); > + > + omap_i2c_dma_cleanup(omap, time_left && !omap->cmd_err); > + > + omap_i2c_dma_disable_req(omap); > + } else { > + if (omap->scheme == OMAP_I2C_SCHEME_0) > + omap_i2c_write_reg(omap, OMAP_I2C_IE_REG, > + omap->iestate); > + else > + omap_i2c_write_reg(omap, OMAP_I2C_IP_V2_IRQENABLE_CLR, > + pio_irq_mask); > + } > + > + if (!time_left && likely(!omap->cmd_err)) { > omap_i2c_reset(omap); > __omap_i2c_init(omap); > return -ETIMEDOUT; [Severity: High] Does waiting on omap->dma->complete before omap->cmd_complete stall for the full OMAP_I2C_TIMEOUT (1 second) whenever a DMA transfer encounters a NACK or arbitration loss? When a bus error occurs before all bytes are transferred (for example, on any RX DMA transfer or multi-burst TX DMA transfer), the controller stops asserting DMA requests and the IRQ handler signals omap->cmd_complete via omap_i2c_complete_cmd(), while omap->dma->complete is never signaled. Additionally, in omap_i2c_xfer_data(), a standalone NACK interrupt sets omap->cmd_err |= OMAP_I2C_STAT_NACK and returns -EAGAIN to wait for the subsequent ARDY interrupt before completing omap->cmd_complete: drivers/i2c/busses/i2c-omap.c:omap_i2c_xfer_data() { ... if (stat & OMAP_I2C_STAT_NACK) { omap->cmd_err |= OMAP_I2C_STAT_NACK; omap_i2c_ack_stat(omap, OMAP_I2C_STAT_NACK); if (!(stat & ~OMAP_I2C_STAT_NACK)) { err = -EAGAIN; break; } } ... } Guarding wait_for_completion_timeout(&omap->cmd_complete, ...) with !omap->cmd_err causes omap_i2c_xfer_msg() to skip waiting for ARDY if the standalone NACK IRQ has already set omap->cmd_err, accessing controller registers before hardware is ready and leaving a stale completion on omap->cmd_complete when ARDY later fires. Similarly, changing the timeout check to if (!time_left && likely(!omap->cmd_err)) skips omap_i2c_reset() and fails to return -ETIMEDOUT when waiting for ARDY times out after a standalone NACK interrupt (and even returns 0 if I2C_M_IGNORE_NAK is set). [ ... ] > @@ -1336,10 +1562,70 @@ static struct i2c_bus_recovery_info > omap_i2c_bus_recovery_info = { > .recover_bus = i2c_generic_scl_recovery, > }; > [ ... ] > +static void omap_i2c_dma_free(struct omap_i2c_dev *omap) > +{ > + struct omap_i2c_dma *dma = omap->dma; > + > + dma->dma_buf = 0; > + dma->dma_len = 0; > + > + dma_release_channel(dma->chan_tx); > + dma->chan_tx = NULL; > + > + dma_release_channel(dma->chan_rx); > + dma->chan_rx = NULL; > + > + dma->chan_using = NULL; > +} [ ... ] > @@ -1476,6 +1762,22 @@ omap_i2c_probe(struct platform_device *pdev) > goto err_unuse_clocks; > } > > + if (omap->scheme != OMAP_I2C_SCHEME_0) { > + omap->dma = omap_i2c_dma_init(omap, (dma_addr_t)res->start); > + if (PTR_ERR(omap->dma) == -EPROBE_DEFER) { > + r = -EPROBE_DEFER; > + dev_err_probe(&pdev->dev, r, > + "can't get DMA channels\n"); > + goto err_unuse_clocks; > + } else if (PTR_ERR(omap->dma) == -ENODEV) { > + dev_dbg(omap->dev, "DMA setup failed\n"); > + omap->dma = NULL; > + } else if (IS_ERR(omap->dma)) { > + r = PTR_ERR(omap->dma); > + goto err_unuse_clocks; > + } > + } > + > adap = &omap->adapter; > i2c_set_adapdata(adap, omap); > adap->owner = THIS_MODULE; > @@ -1491,7 +1793,7 @@ omap_i2c_probe(struct platform_device *pdev) > adap->nr = pdev->id; > r = i2c_add_numbered_adapter(adap); > if (r) > - goto err_unuse_clocks; > + goto err_dma_free; > > dev_info(omap->dev, "bus %d rev%d.%d at %d kHz\n", adap->nr, > major, minor, omap->speed); > @@ -1500,6 +1802,8 @@ omap_i2c_probe(struct platform_device *pdev) > > return 0; > > +err_dma_free: > + omap_i2c_dma_free(omap); [Severity: Medium] Can err_dma_free trigger a NULL pointer dereference in omap_i2c_dma_free()? When omap->scheme == OMAP_I2C_SCHEME_0 or omap_i2c_dma_init() returns ERR_PTR(-ENODEV), omap->dma is NULL. If i2c_add_numbered_adapter() then fails, omap_i2c_dma_free(omap) is called without an if (omap->dma) check and dereferences omap->dma. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=4
