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().

> diff --git a/drivers/i2c/busses/i2c-sh_mobile.c 
> b/drivers/i2c/busses/i2c-sh_mobile.c
> index 0d10742a8ec0c..d7aa710767c75 100644
> --- a/drivers/i2c/busses/i2c-sh_mobile.c
> +++ b/drivers/i2c/busses/i2c-sh_mobile.c

[ ... ]

> @@ -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 race

sh_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.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to