i2c: Fix Critical and High issues reported by Sashiko
Subsystems (17)
cover i2c: Fix Critical and High issues reported by Sashiko
These issues were reported by Sashiko.
Lee Jones (10):
i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in
lpi2c_imx_dma_xfer()
i2c: qcom-geni: Clear dma_buf pointer after releasing DMA buffer
i2c: sh_mobile: Terminate DMA before freeing bounce buffer in
sh_mobile_xfer()
i2c: omap: Check buf_len before buffer accesses in data transfer
helpers
i2c: stm32f7: Prevent duplicate execution of
stm32f7_i2c_dma_callback()
i2c: stm32f7: Allocate smbus_buf separately for DMA safety
i2c: usbio: Validate full reply length in split-read loop
i2c: amd-mp2: Synchronise IRQ completion with command timeout
i2c: mlxbf: Reject zero-length SMBus reads to prevent stack overflow
i2c: octeon: Guard to_pci_dev() with dev_is_pci() in
octeon_i2c_set_clock()drivers/i2c/busses/i2c-amd-mp2-pci.c | 29 ++++++++++++++---- drivers/i2c/busses/i2c-amd-mp2-plat.c | 5 ---- drivers/i2c/busses/i2c-amd-mp2.h | 3 ++ drivers/i2c/busses/i2c-imx-lpi2c.c | 4 ++- drivers/i2c/busses/i2c-mlxbf.c | 8 +++++ drivers/i2c/busses/i2c-octeon-core.c | 3 +- drivers/i2c/busses/i2c-omap.c | 14 ++++++--- drivers/i2c/busses/i2c-qcom-geni.c | 2 ++ drivers/i2c/busses/i2c-sh_mobile.c | 18 ++++++----- drivers/i2c/busses/i2c-stm32f7.c | 43 ++++++++++++++++++++++----- drivers/i2c/busses/i2c-usbio.c | 4 +-- 11 files changed, 99 insertions(+), 34 deletions(-)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 1/10 i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer()
lpi2c_imx->dma persists across transfers. On a DMA read, the disable_dma path frees dma->rx_cmd_buf without clearing the pointer. If a subsequent DMA read fails in lpi2c_dma_config() or lpi2c_dma_submit() before lpi2c_imx_alloc_rx_cmd_buf() runs, the cleanup path calls kfree(dma->rx_cmd_buf) a second time on the stale pointer.
Reset dma->rx_cmd_buf to NULL immediately after freeing it.
--- drivers/i2c/busses/i2c-imx-lpi2c.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-imx-lpi2c.c +3 -1
@@ -1097,8 +1097,10 @@ static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,/* Disable I2C DMA function */ writel (0 , lpi2c_imx->base +LPI2C_MDER ); -if (dma->dma_msg_flag &I2C_M_RD ) +if (dma->dma_msg_flag &I2C_M_RD ) {kfree (dma->rx_cmd_buf); + dma->rx_cmd_buf =NULL ; + }if (ret)i2c_put_dma_safe_msg_buf (dma->dma_buf, msg, false);
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 2/10 i2c: qcom-geni: Clear dma_buf pointer after releasing DMA buffer
In reply to Lee Jonesgeni_i2c_rx_msg_cleanup() and geni_i2c_tx_msg_cleanup() release gi2c->dma_buf without setting it back to NULL. If a subsequent transfer fails geni_se_{rx,tx}_dma_prep() and falls back to FIFO mode, gi2c->dma_buf retains the stale pointer from the previous transfer, causing the cleanup helper to unprepare and free the buffer a second time.
Set gi2c->dma_buf to NULL after releasing it in both cleanup functions.
--- drivers/i2c/busses/i2c-qcom-geni.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/i2c/busses/i2c-qcom-geni.c +2 -0
@@ -505,6 +505,7 @@ static void geni_i2c_rx_msg_cleanup(struct geni_i2c_dev *gi2c,geni_i2c_rx_fsm_rst (gi2c);geni_se_rx_dma_unprep (&gi2c->se, gi2c->dma_addr, gi2c->xfer_len);i2c_put_dma_safe_msg_buf (gi2c->dma_buf, cur, !gi2c->err); + gi2c->dma_buf =NULL ; } }
@@ -517,6 +518,7 @@ static void geni_i2c_tx_msg_cleanup(struct geni_i2c_dev *gi2c,geni_i2c_tx_fsm_rst (gi2c);geni_se_tx_dma_unprep (&gi2c->se, gi2c->dma_addr, gi2c->xfer_len);i2c_put_dma_safe_msg_buf (gi2c->dma_buf, cur, !gi2c->err); + gi2c->dma_buf =NULL ; } }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 3/10 i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer()
In reply to Lee JonesIn sh_mobile_xfer(), i2c_put_dma_safe_msg_buf() is called immediately after wait_event_timeout(), before checking for a timeout or an active DMA transfer and calling sh_mobile_i2c_cleanup_dma(). On a timeout or early NACK wakeup while DMA is still running, the bounce buffer is freed while still mapped for DMA.
Select the DMA channel from the immutable message direction flag rather than mutable pd->dma_direction, always synchronise and terminate the channel in sh_mobile_xfer() before calling i2c_put_dma_safe_msg_buf(), re-check pd->dma_direction after dmaengine_terminate_sync() to avoid double unmapping, and update pd->pos, pd->stop_after_dma, and ICIC before clearing pd->dma_direction in sh_mobile_i2c_dma_callback().
--- drivers/i2c/busses/i2c-sh_mobile.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/i2c/busses/i2c-sh_mobile.c +11 -7
@@ -445,12 +445,17 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)static void sh_mobile_i2c_cleanup_dma (struct sh_mobile_i2c_data *pd,bool terminate) { -struct dma_chan *chan = pd->dma_direction ==DMA_FROM_DEVICE +struct dma_chan *chan = (pd->msg->flags &I2C_M_RD ) ? pd->dma_rx : pd->dma_tx;/* only allowed from thread context! */ -if (terminate) +if (terminate) { +if (IS_ERR_OR_NULL (chan)) +return ;dmaengine_terminate_sync (chan); +if (pd->dma_direction ==DMA_NONE ) +return ; + }dma_unmap_single (chan->device->dev,sg_dma_address (&pd->sg), pd->msg->len, pd->dma_direction);
@@ -462,11 +467,10 @@ static void sh_mobile_i2c_dma_callback(void *data) {struct sh_mobile_i2c_data *pd = data; -sh_mobile_i2c_cleanup_dma (pd, false); pd->pos = pd->msg->len; pd->stop_after_dma = true; -iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); +sh_mobile_i2c_cleanup_dma (pd, false); } static struct dma_chan *sh_mobile_i2c_request_dma_chan(struct device *dev,
@@ -682,15 +686,15 @@ static int sh_mobile_xfer(struct sh_mobile_i2c_data *pd, pd->sr & (ICSR_TACK |SW_DONE ), pd->adap.timeout); +if (pd->dma_buf) +sh_mobile_i2c_cleanup_dma (pd, true); +/* 'stop_after_dma' tells if DMA xfer was complete */ i2c_put_dma_safe_msg_buf (pd->dma_buf, pd->msg, pd->stop_after_dma); }if (!time_left) { -if (pd->dma_direction !=DMA_NONE ) -sh_mobile_i2c_cleanup_dma (pd, true); - err = -ETIMEDOUT ;break ; }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 4/10 i2c: omap: Check buf_len before buffer accesses in data transfer helpers
In reply to Lee Jonesomap_i2c_receive_data() and omap_i2c_transmit_data() dereference omap->buf without checking omap->buf_len. On controllers with OMAP_I2C_FLAG_16BIT_DATA_REG, any odd-length message reads or writes one byte past msg->buf and underflows omap->buf_len; FIFO counts exceeding omap->buf_len also overflow the message buffer.
Guard all omap->buf accesses and omap->buf_len decrements against a zero remaining length, matching omap_i2c_omap1_isr().
--- drivers/i2c/busses/i2c-omap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-omap.c +10 -4
@@ -1003,14 +1003,17 @@ static void omap_i2c_receive_data(struct omap_i2c_dev *omap, u8 num_bytes,while (num_bytes--) { w =omap_i2c_read_reg (omap,OMAP_I2C_DATA_REG ); - *omap->buf++ = w; - omap->buf_len--; +if (omap->buf_len) { + *omap->buf++ = w; + omap->buf_len--; + }/* * Data reg in 2430, omap3 and * omap4 is 8 bit wide */ -if (omap->flags &OMAP_I2C_FLAG_16BIT_DATA_REG ) { +if ((omap->flags &OMAP_I2C_FLAG_16BIT_DATA_REG ) && + omap->buf_len) { *omap->buf++ = w >>8 ; omap->buf_len--; }
@@ -1023,6 +1026,8 @@ static int omap_i2c_transmit_data(struct omap_i2c_dev *omap, u8 num_bytes,u16 w;while (num_bytes--) { +if (!omap->buf_len) +break ; w = *omap->buf++; omap->buf_len--;
@@ -1030,7 +1035,8 @@ static int omap_i2c_transmit_data(struct omap_i2c_dev *omap, u8 num_bytes,* Data reg in 2430, omap3 and * omap4 is 8 bit wide */ -if (omap->flags &OMAP_I2C_FLAG_16BIT_DATA_REG ) { +if ((omap->flags &OMAP_I2C_FLAG_16BIT_DATA_REG ) && + omap->buf_len) { w |= *omap->buf++ <<8 ; omap->buf_len--; }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 5/10 i2c: stm32f7: Prevent duplicate execution of stm32f7_i2c_dma_callback()
In reply to Lee Jonesstm32f7_i2c_dma_callback() can be invoked both by the DMA engine upon transfer completion and directly by the threaded IRQ handler on NACK, error, or timeout conditions. When these race, dma_unmap_single() and i2c_put_dma_safe_msg_buf() execute twice on the same buffer, causing double DMA unmap and double free.
Guard stm32f7_i2c_dma_callback() with an atomic dma_cleanup_pending flag so cleanup runs at most once per prepared DMA transfer, and route threaded IRQ error/timeout paths through a helper that calls dmaengine_terminate_sync() first so any in-flight DMA callback has completed before proceeding or advancing to the next message.
--- drivers/i2c/busses/i2c-stm32f7.c | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-)
drivers/i2c/busses/i2c-stm32f7.c +28 -7
@@ -14,6 +14,7 @@* This driver is based on i2c-stm32f4.c * */ +#include <linux/atomic.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/err.h>
@@ -320,6 +321,7 @@ struct stm32f7_i2c_alert {* slave) * @dma: dma data * @use_dma: boolean to know if dma is used in the current transfer +* @dma_cleanup_pending: tracks whether a DMA transfer awaits cleanup * @regmap: holds SYSCFG phandle for Fast Mode Plus bits * @fmp_sreg: register address for setting Fast Mode Plus bits * @fmp_creg: register address for clearing Fast Mode Plus bits
@@ -353,6 +355,7 @@ struct stm32f7_i2c_dev {bool master_mode;struct stm32_i2c_dma *dma;bool use_dma; +atomic_t dma_cleanup_pending;struct regmap *regmap;u32 fmp_sreg;u32 fmp_creg;
@@ -735,6 +738,9 @@ static void stm32f7_i2c_dma_callback(void *arg)struct stm32_i2c_dma *dma = i2c_dev->dma;struct stm32f7_i2c_msg *f7_msg = &i2c_dev->f7_msg; +if (!atomic_xchg (&i2c_dev->dma_cleanup_pending,0 )) +return ; +stm32f7_i2c_disable_dma_req (i2c_dev);dmaengine_terminate_async (dma->chan_using); dma_unmap_single(i2c_dev->dev, dma->dma_buf, dma->dma_len,
@@ -744,6 +750,12 @@ static void stm32f7_i2c_dma_callback(void *arg)complete (&dma->dma_complete); } +static void stm32f7_i2c_dma_abort (struct stm32f7_i2c_dev *i2c_dev) +{ +dmaengine_terminate_sync (i2c_dev->dma->chan_using); +stm32f7_i2c_dma_callback (i2c_dev); +} +static void stm32f7_i2c_hw_config (struct stm32f7_i2c_dev *i2c_dev) {struct stm32f7_i2c_timings *t = &i2c_dev->timing;
@@ -928,12 +940,14 @@ static void stm32f7_i2c_xfer_msg(struct stm32f7_i2c_dev *i2c_dev, dma_buf =i2c_get_dma_safe_msg_buf (msg,STM32F7_I2C_DMA_LEN_MIN );if (dma_buf) { f7_msg->buf = dma_buf; +atomic_set (&i2c_dev->dma_cleanup_pending,1 ); ret =stm32_i2c_prep_dma_xfer (i2c_dev->dev, i2c_dev->dma, msg->flags &I2C_M_RD , f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev);if (ret) { +atomic_set (&i2c_dev->dma_cleanup_pending,0 );dev_warn (i2c_dev->dev,"can't use DMA\n" );i2c_put_dma_safe_msg_buf (f7_msg->buf, msg, false); f7_msg->buf = msg->buf;
@@ -1102,15 +1116,18 @@ static int stm32f7_i2c_smbus_xfer_msg(struct stm32f7_i2c_dev *i2c_dev,/* Configure DMA or enable RX/TX interrupt */ i2c_dev->use_dma = false;if (i2c_dev->dma && f7_msg->count >=STM32F7_I2C_DMA_LEN_MIN ) { +atomic_set (&i2c_dev->dma_cleanup_pending,1 ); ret =stm32_i2c_prep_dma_xfer (i2c_dev->dev, i2c_dev->dma, cr2 &STM32F7_I2C_CR2_RD_WRN , f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev); -if (!ret) +if (!ret) { i2c_dev->use_dma = true; -else + }else { +atomic_set (&i2c_dev->dma_cleanup_pending,0 );dev_warn (i2c_dev->dev,"can't use DMA\n" ); + } }if (!i2c_dev->use_dma) {
@@ -1196,16 +1213,19 @@ static void stm32f7_i2c_smbus_rep_start(struct stm32f7_i2c_dev *i2c_dev)if (i2c_dev->dma && f7_msg->count >=STM32F7_I2C_DMA_LEN_MIN && f7_msg->size !=I2C_SMBUS_BLOCK_DATA && f7_msg->size !=I2C_SMBUS_BLOCK_PROC_CALL ) { +atomic_set (&i2c_dev->dma_cleanup_pending,1 ); ret =stm32_i2c_prep_dma_xfer (i2c_dev->dev, i2c_dev->dma, cr2 &STM32F7_I2C_CR2_RD_WRN , f7_msg->count, f7_msg->buf, stm32f7_i2c_dma_callback, i2c_dev); -if (!ret) +if (!ret) { i2c_dev->use_dma = true; -else + }else { +atomic_set (&i2c_dev->dma_cleanup_pending,0 );dev_warn (i2c_dev->dev,"can't use DMA\n" ); + } }if (!i2c_dev->use_dma)
@@ -1552,7 +1572,7 @@ static irqreturn_t stm32f7_i2c_handle_isr_errs(struct stm32f7_i2c_dev *i2c_dev,/* Disable dma */ if (i2c_dev->use_dma) -stm32f7_i2c_dma_callback (i2c_dev); +stm32f7_i2c_dma_abort (i2c_dev); i2c_dev->master_mode = false;complete (&i2c_dev->complete);
@@ -1617,7 +1637,7 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data) __func__, f7_msg->addr);writel_relaxed (STM32F7_I2C_ICR_NACKCF , base +STM32F7_I2C_ICR );if (i2c_dev->use_dma) -stm32f7_i2c_dma_callback (i2c_dev); +stm32f7_i2c_dma_abort (i2c_dev); f7_msg->result = -ENXIO ; }
@@ -1635,8 +1655,9 @@ static irqreturn_t stm32f7_i2c_isr_event_thread(int irq, void *data) ret =wait_for_completion_timeout (&i2c_dev->dma->dma_complete,HZ );if (!ret) {dev_dbg (i2c_dev->dev,"<%s>: Timed out\n" , __func__); -stm32f7_i2c_dma_callback (i2c_dev); +stm32f7_i2c_dma_abort (i2c_dev); f7_msg->result = -ETIMEDOUT ; + f7_msg->stop = true; } }if (f7_msg->stop) {
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 6/10 i2c: stm32f7: Allocate smbus_buf separately for DMA safety
In reply to Lee Jonessmbus_buf is embedded directly inside struct stm32f7_i2c_msg (and thus struct stm32f7_i2c_dev), sharing a CPU cacheline with adjacent driver state. Passing it to dma_map_single() on non-coherent ARM/ARM64 SoCs can corrupt adjacent fields during cache invalidation or corrupt DMA data via concurrent CPU writes.
Allocate smbus_buf separately with devm_kzalloc() during probe so it is isolated in its own DMA-safe allocation.
--- drivers/i2c/busses/i2c-stm32f7.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-stm32f7.c +7 -1
@@ -285,7 +285,7 @@ struct stm32f7_i2c_msg {bool smbus;int size;char read_write; -u8 smbus_buf[I2C_SMBUS_BLOCK_MAX +3 ]__aligned(4) ; +u8 *smbus_buf; };/**
@@ -2186,6 +2186,12 @@ static int stm32f7_i2c_probe(struct platform_device *pdev)if (!i2c_dev)return -ENOMEM ; + i2c_dev->f7_msg.smbus_buf =devm_kzalloc (&pdev->dev, +I2C_SMBUS_BLOCK_MAX +3 , +GFP_KERNEL ); +if (!i2c_dev->f7_msg.smbus_buf) +return -ENOMEM ; + setup =of_device_get_match_data (&pdev->dev);if (!setup)return dev_err_probe (&pdev->dev, -ENODEV ,"Can't get device data\n" );
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 7/10 i2c: usbio: Validate full reply length in split-read loop
In reply to Lee JonesIn usbio_i2c_read(), the split-read loop checks only whether usbio_bulk_msg() returned a negative error code before copying rxchunk bytes from rbuf->data into msg->buf. On a short USB bulk reply, stale bytes remaining in the reused i2c->rwbuf buffer are copied into msg->buf and leaked to callers.
Validate that usbio_bulk_msg() returns exactly sizeof(*rbuf) + rxchunk, matching the non-split path.
--- drivers/i2c/busses/i2c-usbio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-usbio.c +2 -2
@@ -103,8 +103,8 @@ static int usbio_i2c_read(struct i2c_adapter *adap, struct i2c_msg *msg)USBIO_I2CCMD_READ , true, rbuf, len ==0 ?sizeof (*rbuf) :0 , rbuf, sizeof(*rbuf) + rxchunk); -if (ret <0 ) -return ret; +if (ret !=sizeof (*rbuf) + rxchunk) +return (ret <0 ) ? ret : -EIO ;memcpy (&msg->buf[len], rbuf->data, rxchunk); len += rxchunk;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 8/10 i2c: amd-mp2: Synchronise IRQ completion with command timeout
In reply to Lee JonesOn transfer timeout, amd_mp2_rw_timeout() resets i2c_common->reqcmd to i2c_none without synchronising against amd_mp2_irq_isr() or clearing i2c_common->msg. A delayed interrupt then invokes i2c_amd_cmd_completion(), which dereferences the freed i2c_common->msg->buf in dev_dbg() and unconditionally calls complete(), prematurely waking a subsequent transfer.
Protect command state transitions and IRQ completion dispatch with a spinlock, ignore interrupts when reqcmd is i2c_none, clear i2c_common->msg when a command ends, and drop the racy msg->buf dev_dbg() from i2c_amd_cmd_completion().
--- drivers/i2c/busses/i2c-amd-mp2-pci.c | 29 +++++++++++++++++++++------ drivers/i2c/busses/i2c-amd-mp2-plat.c | 5 ----- drivers/i2c/busses/i2c-amd-mp2.h | 3 +++ 3 files changed, 26 insertions(+), 11 deletions(-)
drivers/i2c/busses/i2c-amd-mp2-pci.c +23 -6
@@ -43,13 +43,15 @@ static int amd_mp2_cmd(struct amd_i2c_common *i2c_common, union i2c_cmd_base i2c_cmd_base) {struct amd_mp2_dev *privdata = i2c_common->mp2_dev; +unsigned long flags;void __iomem *reg; - i2c_common->reqcmd = i2c_cmd_base.s.i2c_cmd; - reg = privdata->mmio + ((i2c_cmd_base.s.bus_id ==1 ) ?AMD_C2P_MSG1 :AMD_C2P_MSG0 ); +spin_lock_irqsave (&privdata->lock, flags); + i2c_common->reqcmd = i2c_cmd_base.s.i2c_cmd;writel (i2c_cmd_base.ul, reg); +spin_unlock_irqrestore (&privdata->lock, flags);return 0 ; }
@@ -184,6 +186,7 @@ void amd_mp2_process_event(struct amd_i2c_common *i2c_common) {struct amd_mp2_dev *privdata = i2c_common->mp2_dev;struct pci_dev *pdev = privdata->pci_dev; +unsigned long flags;if (unlikely (i2c_common->reqcmd == i2c_none)) { pci_warn(pdev, "received msg but no cmd was sent (bus = %d)!\n",
@@ -193,7 +196,10 @@ void amd_mp2_process_event(struct amd_i2c_common *i2c_common)__amd_mp2_process_event (i2c_common); +spin_lock_irqsave (&privdata->lock, flags); i2c_common->reqcmd = i2c_none; + i2c_common->msg =NULL ; +spin_unlock_irqrestore (&privdata->lock, flags);amd_mp2_c2p_mutex_unlock (i2c_common); }EXPORT_SYMBOL_GPL (amd_mp2_process_event);
@@ -203,6 +209,7 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev)struct amd_mp2_dev *privdata = dev;struct pci_dev *pdev = privdata->pci_dev;struct amd_i2c_common *i2c_common; +unsigned long flags;u32 val;unsigned int bus_id;void __iomem *reg;
@@ -215,15 +222,19 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev) reg = privdata->mmio + ((bus_id ==0 ) ?AMD_P2C_MSG1 :AMD_P2C_MSG2 ); +spin_lock_irqsave (&privdata->lock, flags); val =readl (reg);if (val !=0 ) {writel (0 , reg);writel (0 , privdata->mmio +AMD_P2C_MSG_INTEN ); - i2c_common->eventval.ul = val; - i2c_common->cmd_completion (i2c_common); +if (i2c_common->reqcmd != i2c_none) { + i2c_common->eventval.ul = val; + i2c_common->cmd_completion (i2c_common); + } ret =IRQ_HANDLED ; } +spin_unlock_irqrestore (&privdata->lock, flags); }if (ret !=IRQ_HANDLED ) {
@@ -240,7 +251,13 @@ static irqreturn_t amd_mp2_irq_isr(int irq, void *dev)void amd_mp2_rw_timeout (struct amd_i2c_common *i2c_common) { +struct amd_mp2_dev *privdata = i2c_common->mp2_dev; +unsigned long flags; + +spin_lock_irqsave (&privdata->lock, flags); i2c_common->reqcmd = i2c_none; + i2c_common->msg =NULL ; +spin_unlock_irqrestore (&privdata->lock, flags);amd_mp2_c2p_mutex_unlock (i2c_common); }EXPORT_SYMBOL_GPL (amd_mp2_rw_timeout);
@@ -350,12 +367,12 @@ static int amd_mp2_pci_probe(struct pci_dev *pci_dev,return -ENOMEM ; privdata->pci_dev = pci_dev; +mutex_init (&privdata->c2p_lock); +spin_lock_init (&privdata->lock); rc =amd_mp2_pci_init (privdata, pci_dev);if (rc)return rc; -mutex_init (&privdata->c2p_lock); -pm_runtime_set_autosuspend_delay (&pci_dev->dev,1000 );pm_runtime_use_autosuspend (&pci_dev->dev);pm_runtime_put_autosuspend (&pci_dev->dev);
drivers/i2c/busses/i2c-amd-mp2-plat.c +0 -5
@@ -85,11 +85,6 @@ static void i2c_amd_start_cmd(struct amd_i2c_dev *i2c_dev)static void i2c_amd_cmd_completion (struct amd_i2c_common *i2c_common) {struct amd_i2c_dev *i2c_dev =amd_i2c_dev_common (i2c_common); -union i2c_event *event = &i2c_common->eventval; - -if (event->r.status == i2c_readcomplete_event) -dev_dbg (&i2c_dev->pdev->dev,"readdata:%*ph\n" , event->r.length, - i2c_common->msg->buf);complete (&i2c_dev->cmd_complete); }
drivers/i2c/busses/i2c-amd-mp2.h +3 -0
@@ -12,6 +12,7 @@#include <linux/i2c.h> #include <linux/pci.h> #include <linux/pm_runtime.h> +#include <linux/spinlock.h> #define PCI_DEVICE_ID_AMD_MP2 0x15E6
@@ -174,6 +175,7 @@ struct amd_i2c_common {* @mmio: iommapped registers * @c2p_lock: controls access to the C2P mailbox shared between * the two adapters +* @lock: protects command state against concurrent IRQ handling * @c2p_lock_busid: id of the adapter which locked c2p_lock */ struct amd_mp2_dev {
@@ -181,6 +183,7 @@ struct amd_mp2_dev {struct amd_i2c_common *busses[2 ];void __iomem *mmio;struct mutex c2p_lock; +spinlock_t lock;/* protects command state against IRQ handling */ u8 c2p_lock_busid;unsigned int probed;int dev_irq;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 9/10 i2c: mlxbf: Reject zero-length SMBus reads to prevent stack overflow
In reply to Lee JonesFor I2C_SMBUS_QUICK with read set, mlxbf_i2c_smbus_quick_command() sets operation->length to 0 and leaves operation->buffer NULL while setting MLXBF_I2C_F_READ. In mlxbf_i2c_smbus_start_transaction(), subtracting 1 from the 8-bit length underflows read_len to 255, overflowing the 128-byte data_desc stack buffer and dereferencing NULL in memcpy().
Reject I2C_SMBUS_QUICK reads with -EOPNOTSUPP in mlxbf_i2c_smbus_xfer() and validate operation->length and operation->buffer in mlxbf_i2c_smbus_start_transaction() before computing read_len.
--- drivers/i2c/busses/i2c-mlxbf.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/i2c/busses/i2c-mlxbf.c +8 -0
@@ -772,6 +772,12 @@ mlxbf_i2c_smbus_start_transaction(struct mlxbf_i2c_priv *priv,* than once? */ if (flags &MLXBF_I2C_F_READ ) { +if (!operation->length || + operation->length >MLXBF_I2C_MASTER_DATA_DESC_SIZE || + !operation->buffer) { + ret = -EINVAL ; + goto out_unlock; + } read_en =1 ;/* Subtract 1 as required by HW. */ read_len = operation->length -1 ;
@@ -2038,6 +2044,8 @@ static s32 mlxbf_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr,switch (size) {case I2C_SMBUS_QUICK : +if (read) +return -EOPNOTSUPP ;mlxbf_i2c_smbus_quick_command (&request, read);dev_dbg (&adap->dev,"smbus quick, slave 0x%02x\n" , addr);break ;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 10/10 i2c: octeon: Guard to_pci_dev() with dev_is_pci() in octeon_i2c_set_clock()
In reply to Lee Jonesocteon_i2c_set_clock() unconditionally passes i2c->dev to to_pci_dev() and inspects pdev->subsystem_device. When invoked from the OCTEON platform driver, i2c->dev belongs to a struct platform_device rather than a struct pci_dev, resulting in an out-of-bounds slab read.
Check dev_is_pci(i2c->dev) before calling to_pci_dev().
--- drivers/i2c/busses/i2c-octeon-core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-octeon-core.c +2 -1
@@ -833,7 +833,8 @@ void octeon_i2c_set_clock(struct octeon_i2c *i2c)unsigned int thp, mdiv_min, mdiv =2 , ndiv =0 , ds =10 ;unsigned int delta_hz =INITIAL_DELTA_HZ ; - is_plat_otx2 =octeon_i2c_is_otx2 (to_pci_dev (i2c->dev)); + is_plat_otx2 =dev_is_pci (i2c->dev) && +octeon_i2c_is_otx2 (to_pci_dev (i2c->dev));if (is_plat_otx2) { thp =TWSI_MASTER_CLK_REG_OTX2_VAL ;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH 03/10] i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer()
In reply to Lee JonesThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] i2c: sh_mobile: data race on dma_direction in sh_mobile_i2c_dma_callback() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] i2c: sh_mobile: data race on dma_direction in sh_mobile_i2c_dma_callback()
--
commit c6b6af1c3ce9c1df00cdd2b1dc80f991c809d4d9
Author: Lee Jones <[email protected]>
i2c: sh_mobile: Terminate DMA before freeing bounce buffer in sh_mobile_xfer()
This commit terminates and synchronizes any active DMA channel in sh_mobile_xfer() before freeing the bounce buffer with i2c_put_dma_safe_msg_buf(). It also selects the DMA channel from pd->msg->flags, re-checks pd->dma_direction after dmaengine_terminate_sync(), and reorders sh_mobile_i2c_dma_callback().
Quoted 4 lines
drivers/i2c/busses/i2c-sh_mobile.c +0
-0
[ ... ]
drivers/i2c/busses/i2c-sh_mobile.c static void sh_mobile_i2c_dma_callback(void *data) · line 474
Quoted 11 lines
drivers/i2c/busses/i2c-sh_mobile.c +1
-2@@ -462,11 +467,10 @@ static void sh_mobile_i2c_dma_callback(void *data) {struct sh_mobile_i2c_data *pd = data; -sh_mobile_i2c_cleanup_dma (pd, false); pd->pos = pd->msg->len; pd->stop_after_dma = true; -iic_set_clr (pd,ICIC ,0 ,ICIC_TDMAE |ICIC_RDMAE ); +sh_mobile_i2c_cleanup_dma (pd, false); }
[Severity: Low]
Does moving sh_mobile_i2c_cleanup_dma() after iic_set_clr() introduce a data race on pd->dma_direction?
Clearing ICIC_TDMAE and ICIC_RDMAE unmasks the I2C hardware interrupt before dma_unmap_single() and the store to pd->dma_direction complete:
CPU0 (DMAC threaded IRQ) CPU1 (<hardirq>)sh_mobile_i2c_dma_callback()
iic_set_clr(pd, ICIC, 0, ...)
sh_mobile_i2c_cleanup_dma() sh_mobile_i2c_isr()
dma_unmap_single(...)
pd->dma_direction = DMA_NONE if (pd->dma_direction == ...)
<-- data racesh_mobile_i2c_isr() reads pd->dma_direction unconditionally without locking or READ_ONCE(), triggering a KCSAN data race on every normal DMA completion. It can also wake sh_mobile_xfer() before dma_unmap_single() finishes, forcing dmaengine_terminate_sync() to wait on the callback.
Calling sh_mobile_i2c_cleanup_dma(pd, false) after setting pd->pos and pd->stop_after_dma, but before iic_set_clr(), avoids both the data race and the premature wakeup.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 01/10] i2c: imx-lpi2c: Clear rx_cmd_buf pointer after freeing in lpi2c_imx_dma_xfer()
In reply to Lee JonesQuoted 7 lines · Lee Jones
lpi2c_imx->dmapersists across transfers. On a DMA read, thedisable_dmapath freesdma->rx_cmd_bufwithout clearing the pointer. If a subsequent DMA read fails inlpi2c_dma_config()orlpi2c_dma_submit()beforelpi2c_imx_alloc_rx_cmd_buf()runs, the cleanup path callskfree(dma->rx_cmd_buf)a second time on the stale pointer.
Quoted 7 lines
Reset
dma->rx_cmd_bufto NULL immediately after freeing it.Fixes a09c8b3f9047 ("i2c: imx-lpi2c: add eDMA mode support for LPI2C")Signed-off-by Lee Jones <[email protected]>---
Quoted 23 lines
drivers/i2c/busses/i2c-imx-lpi2c.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/i2c/busses/i2c-imx-lpi2c.c +3
-2@@ -1097,8 +1097,10 @@ static int lpi2c_imx_dma_xfer(struct lpi2c_imx_struct *lpi2c_imx,/* Disable I2C DMA function */ writel (0 , lpi2c_imx->base +LPI2C_MDER ); -if (dma->dma_msg_flag &I2C_M_RD ) +if (dma->dma_msg_flag &I2C_M_RD ) {kfree (dma->rx_cmd_buf); + dma->rx_cmd_buf =NULL ; + }if (ret)i2c_put_dma_safe_msg_buf (dma->dma_buf, msg, false); --2.56.1.911.g883d8bcb20-goog