Skip to content

Commit f78ca48

Browse files
hkallweitwsakernel
authored andcommitted
i2c: i801: fix potential race in i801_block_transaction_byte_by_byte
Currently we set SMBHSTCNT_LAST_BYTE only after the host has started receiving the last byte. If we get e.g. preempted before setting SMBHSTCNT_LAST_BYTE, the host may be finished with receiving the byte before SMBHSTCNT_LAST_BYTE is set. Therefore change the code to set SMBHSTCNT_LAST_BYTE before writing SMBHSTSTS_BYTE_DONE for the byte before the last byte. Now the code is also consistent with what we do in i801_isr_byte_done(). Reported-by: Jean Delvare <jdelvare@suse.com> Closes: https://lore.kernel.org/linux-i2c/20230828152747.09444625@endymion.delvare/ Cc: stable@vger.kernel.org Acked-by: Andi Shyti <andi.shyti@kernel.org> Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com> Reviewed-by: Jean Delvare <jdelvare@suse.de> Signed-off-by: Wolfram Sang <wsa@kernel.org>
1 parent ce9ecca commit f78ca48

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

drivers/i2c/busses/i2c-i801.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,15 +679,11 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
679679
return result ? priv->status : -ETIMEDOUT;
680680
}
681681

682-
for (i = 1; i <= len; i++) {
683-
if (i == len && read_write == I2C_SMBUS_READ)
684-
smbcmd |= SMBHSTCNT_LAST_BYTE;
685-
outb_p(smbcmd, SMBHSTCNT(priv));
686-
687-
if (i == 1)
688-
outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
689-
SMBHSTCNT(priv));
682+
if (len == 1 && read_write == I2C_SMBUS_READ)
683+
smbcmd |= SMBHSTCNT_LAST_BYTE;
684+
outb_p(smbcmd | SMBHSTCNT_START, SMBHSTCNT(priv));
690685

686+
for (i = 1; i <= len; i++) {
691687
status = i801_wait_byte_done(priv);
692688
if (status)
693689
return status;
@@ -710,9 +706,12 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
710706
data->block[0] = len;
711707
}
712708

713-
/* Retrieve/store value in SMBBLKDAT */
714-
if (read_write == I2C_SMBUS_READ)
709+
if (read_write == I2C_SMBUS_READ) {
715710
data->block[i] = inb_p(SMBBLKDAT(priv));
711+
if (i == len - 1)
712+
outb_p(smbcmd | SMBHSTCNT_LAST_BYTE, SMBHSTCNT(priv));
713+
}
714+
716715
if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
717716
outb_p(data->block[i+1], SMBBLKDAT(priv));
718717

0 commit comments

Comments
 (0)