Re: [PATCH V6] i2c: tegra: remove BUG, BUG_ON

From: Dmitry Osipenko
Date: Mon Jun 17 2019 - 15:37:18 EST


17.06.2019 22:08, Bitan Biswas ÐÐÑÐÑ:
>
>
> On 6/14/19 10:51 AM, Dmitry Osipenko wrote:
>> 14.06.2019 18:50, Bitan Biswas ÐÐÑÐÑ:
>>> Remove redundant BUG_ON calls or replace with WARN_ON_ONCE
>>> as needed. Remove BUG() and mask Rx interrupt similar as Tx
>>> for message fully sent case. Add WARN_ON_ONCE check
>>> for non-zero rx_fifo_avail in tegra_i2c_empty_rx_fifo()
>>> after all processing. Error handling in tegra_i2c_empty_rx_fifo
>>> caller is also added.
>>>
>>> Signed-off-by: Bitan Biswas <bbiswas@xxxxxxxxxx>
>>> ---
>>> Â drivers/i2c/busses/i2c-tegra.c | 46 ++++++++++++++++++++++++++++++++++--------
>>> Â 1 file changed, 38 insertions(+), 8 deletions(-)
>>>
>>> diff --git a/drivers/i2c/busses/i2c-tegra.c b/drivers/i2c/busses/i2c-tegra.c
>>> index 4dfb4c1..26a7c8c 100644
>>> --- a/drivers/i2c/busses/i2c-tegra.c
>>> +++ b/drivers/i2c/busses/i2c-tegra.c
>>> @@ -73,6 +73,7 @@
>>> Â #define I2C_ERR_NO_ACKÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ BIT(0)
>>> Â #define I2C_ERR_ARBITRATION_LOSTÂÂÂÂÂÂÂ BIT(1)
>>> Â #define I2C_ERR_UNKNOWN_INTERRUPTÂÂÂÂÂÂÂ BIT(2)
>>> +#define I2C_ERR_UNEXPECTED_STATUSÂÂÂÂÂÂÂ BIT(3)
>>
>> What about I2C_ERR_RX_BUFFER_OVERFLOW?
> OK.
>
>>
>>> Â #define PACKET_HEADER0_HEADER_SIZE_SHIFTÂÂÂ 28
>>> Â #define PACKET_HEADER0_PACKET_ID_SHIFTÂÂÂÂÂÂÂ 16
>>> @@ -515,15 +516,23 @@ static int tegra_i2c_empty_rx_fifo(struct tegra_i2c_dev
>>> *i2c_dev)
>>> ÂÂÂÂÂÂ * prevent overwriting past the end of buf
>>> ÂÂÂÂÂÂ */
>>> ÂÂÂÂÂ if (rx_fifo_avail > 0 && buf_remaining > 0) {
>>> -ÂÂÂÂÂÂÂ BUG_ON(buf_remaining > 3);
>>> +ÂÂÂÂÂÂÂ /* buf_remaining > 3 check not needed as rx_fifo_avail == 0
>>> +ÂÂÂÂÂÂÂÂ * when (words_to_transfer was > rx_fifo_avail) earlier
>>> +ÂÂÂÂÂÂÂÂ * in this function
>>> +ÂÂÂÂÂÂÂÂ */
>>
>> Please start all multiline comments with an empty "/*", it should be the correct
>> style. There are some places in the kernel where style like yours is used, but I
>> assume they are not very correct. Besides, yours variant is not consistent with the
>> style of the rest of comments in this source file. And put a dot in the end for
>> completeness. Same for the other comments in this patch.
>>
> OK
>
>>> ÂÂÂÂÂÂÂÂÂ val = i2c_readl(i2c_dev, I2C_RX_FIFO);
>>> ÂÂÂÂÂÂÂÂÂ val = cpu_to_le32(val);
>>> ÂÂÂÂÂÂÂÂÂ memcpy(buf, &val, buf_remaining);
>>> ÂÂÂÂÂÂÂÂÂ buf_remaining = 0;
>>> ÂÂÂÂÂÂÂÂÂ rx_fifo_avail--;
>>> ÂÂÂÂÂ }
>>
>> Please add a newline here. All logical parts of the code should be separated to ease
>> reading and following.
> OK
>
>>
>>> +ÂÂÂ if (WARN_ON_ONCE(rx_fifo_avail))
>>> +ÂÂÂÂÂÂÂ return -EINVAL;
>>> Â -ÂÂÂ BUG_ON(rx_fifo_avail > 0 && buf_remaining > 0);
>>> +ÂÂÂ /* buf_remaining > 0 at this point can only have rx_fifo_avail == 0
>>> +ÂÂÂÂ * as this corresponds to (words_to_transfer was > rx_fifo_avail)
>>> +ÂÂÂÂ * case earlier in this function
>>> +ÂÂÂÂ */
>>> ÂÂÂÂÂ i2c_dev->msg_buf_remaining = buf_remaining;
>>> ÂÂÂÂÂ i2c_dev->msg_buf = buf;
>>> Â @@ -581,7 +590,10 @@ static int tegra_i2c_fill_tx_fifo(struct tegra_i2c_dev
>>> *i2c_dev)
>>> ÂÂÂÂÂÂ * boundary and fault.
>>> ÂÂÂÂÂÂ */
>>> ÂÂÂÂÂ if (tx_fifo_avail > 0 && buf_remaining > 0) {
>>> -ÂÂÂÂÂÂÂ BUG_ON(buf_remaining > 3);
>>> +ÂÂÂÂÂÂÂ /* buf_remaining > 3 check not needed as tx_fifo_avail == 0
>>> +ÂÂÂÂÂÂÂÂ * when (words_to_transfer was > tx_fifo_avail) earlier
>>> +ÂÂÂÂÂÂÂÂ * in this function for non-zero words_to_transfer
>>> +ÂÂÂÂÂÂÂÂ */
>>> ÂÂÂÂÂÂÂÂÂ memcpy(&val, buf, buf_remaining);
>>> ÂÂÂÂÂÂÂÂÂ val = le32_to_cpu(val);
>>> Â @@ -811,6 +823,7 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>>> ÂÂÂÂÂ u32 status;
>>> ÂÂÂÂÂ const u32 status_err = I2C_INT_NO_ACK | I2C_INT_ARBITRATION_LOST;
>>> ÂÂÂÂÂ struct tegra_i2c_dev *i2c_dev = dev_id;
>>> +ÂÂÂ int err_val;
>>> Â ÂÂÂÂÂ status = i2c_readl(i2c_dev, I2C_INT_STATUS);
>>> Â @@ -847,10 +860,21 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>>> Â ÂÂÂÂÂ if (!i2c_dev->is_curr_dma_xfer) {
>>> ÂÂÂÂÂÂÂÂÂ if (i2c_dev->msg_read && (status & I2C_INT_RX_FIFO_DATA_REQ)) {
>>> -ÂÂÂÂÂÂÂÂÂÂÂ if (i2c_dev->msg_buf_remaining)
>>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ tegra_i2c_empty_rx_fifo(i2c_dev);
>>> -ÂÂÂÂÂÂÂÂÂÂÂ else
>>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ BUG();
>>> +ÂÂÂÂÂÂÂÂÂÂÂ err_val = tegra_i2c_empty_rx_fifo(i2c_dev);
>>> +ÂÂÂÂÂÂÂÂÂÂÂ if ((!(i2c_dev->msg_buf_remaining)) &&
>>
>> Let's move this check into tegra_i2c_empty_rx_fifo() and return -EINVAL for that case.
>> This will make code to look cleaner.
> OK.
>
>
>>
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ (!(status & I2C_INT_PACKET_XFER_COMPLETE)) &&
>>
>> It shouldn't matter that XFER_COMPLETE is set if RX FIFO isn't fully emptied because
>> it always shall be emptied. Hence this check is not needed and we should error out
>> regardless.
> OK
>
>>
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ err_val) {
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ /*
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * Overflow error condition: message fully sent,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * with no XFER_COMPLETE interrupt but hardware
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * asks to transfer more.
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ */
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ tegra_i2c_mask_irq(i2c_dev,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ I2C_INT_RX_FIFO_DATA_REQ);
>>
>> No need to mask RX_FIFO_DATA_REQ here because all interrupts are masked on "goto
>> err:", hence just remove the tegra_i2c_mask_irq().
>>
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ i2c_dev->msg_err |=
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ I2C_ERR_UNEXPECTED_STATUS;
>>
>> No need to split this into two lines because it's less than 80 chars, write this in a
>> single line.
> OK
>
>>
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto err;
>>> +ÂÂÂÂÂÂÂÂÂÂÂ }
>>> ÂÂÂÂÂÂÂÂÂ }
>>> Â ÂÂÂÂÂÂÂÂÂ if (!i2c_dev->msg_read && (status & I2C_INT_TX_FIFO_DATA_REQ)) {
>>> @@ -876,7 +900,13 @@ static irqreturn_t tegra_i2c_isr(int irq, void *dev_id)
>>> ÂÂÂÂÂ if (status & I2C_INT_PACKET_XFER_COMPLETE) {
>>> ÂÂÂÂÂÂÂÂÂ if (i2c_dev->is_curr_dma_xfer)
>>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ i2c_dev->msg_buf_remaining = 0;
>>> -ÂÂÂÂÂÂÂ BUG_ON(i2c_dev->msg_buf_remaining);
>>> +ÂÂÂÂÂÂÂ /* Underflow error condition: XFER_COMPLETE before message
>>> +ÂÂÂÂÂÂÂÂ * fully sent.
>>> +ÂÂÂÂÂÂÂÂ */
>>> +ÂÂÂÂÂÂÂ if (WARN_ON_ONCE(i2c_dev->msg_buf_remaining)) {
>>> +ÂÂÂÂÂÂÂÂÂÂÂ i2c_dev->msg_err |= I2C_ERR_UNKNOWN_INTERRUPT;
>>> +ÂÂÂÂÂÂÂÂÂÂÂ goto err;
>>> +ÂÂÂÂÂÂÂ }
>>> ÂÂÂÂÂÂÂÂÂ complete(&i2c_dev->msg_complete);
>>> ÂÂÂÂÂ }
>>> ÂÂÂÂÂ goto done;
>>>
>>
>> Please address comments in the next revision.
>>
>
> Sorry for the delayed reply. I shared Patch V7 with above changes earlier today.

No problems, please take your time. The V7 is almost good, looking forward to v8!