Skip to contentSkip to conversation

i2c: i801: fix potential race in i801_block_transaction_byte_by_byte

PATCH Superseded Heiner Kallweit 4 participants 8 messages Sep 2, 2023 – Sep 6, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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().

---
v2:
- remove incorrect Fixes tag

---
 drivers/i2c/busses/i2c-i801.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-i801.c +9 -10
index 7a0ccc584..8acf09539 100644
@@ -679,15 +679,11 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
 		return result ? priv->status : -ETIMEDOUT;
 	}
 
-	for (i = 1; i <= len; i++) {
-		if (i == len && read_write == I2C_SMBUS_READ)
-			smbcmd |= SMBHSTCNT_LAST_BYTE;
-		outb_p(smbcmd, SMBHSTCNT(priv));
-
-		if (i == 1)
-			outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
-			       SMBHSTCNT(priv));
+	if (len == 1 && read_write == I2C_SMBUS_READ)
+		smbcmd |= SMBHSTCNT_LAST_BYTE;
+	outb_p(smbcmd | SMBHSTCNT_START, SMBHSTCNT(priv));
 
+	for (i = 1; i <= len; i++) {
 		status = i801_wait_byte_done(priv);
 		if (status)
 			return status;
@@ -710,9 +706,12 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
 			data->block[0] = len;
 		}
 
-		/* Retrieve/store value in SMBBLKDAT */
-		if (read_write == I2C_SMBUS_READ)
+		if (read_write == I2C_SMBUS_READ) {
 			data->block[i] = inb_p(SMBBLKDAT(priv));
+			if (i == len - 1)
+				outb_p(smbcmd | SMBHSTCNT_LAST_BYTE, SMBHSTCNT(priv));
+		}
+
 		if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
 			outb_p(data->block[i+1], SMBBLKDAT(priv));
 
Signature · 1 line
2.42.0
#1 lore raw
Quoted 9 lines · Heiner Kallweit

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

Note for Wolfram: checkpatch says we should insert here:

Quoted 48 lines

---
v2:
- remove incorrect Fixes tag

---
 drivers/i2c/busses/i2c-i801.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-i801.c +9 -10
index 7a0ccc584..8acf09539 100644
@@ -679,15 +679,11 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
 		return result ? priv->status : -ETIMEDOUT;
 	}
 
-	for (i = 1; i <= len; i++) {
-		if (i == len && read_write == I2C_SMBUS_READ)
-			smbcmd |= SMBHSTCNT_LAST_BYTE;
-		outb_p(smbcmd, SMBHSTCNT(priv));
-
-		if (i == 1)
-			outb_p(inb(SMBHSTCNT(priv)) | SMBHSTCNT_START,
-			       SMBHSTCNT(priv));
+	if (len == 1 && read_write == I2C_SMBUS_READ)
+		smbcmd |= SMBHSTCNT_LAST_BYTE;
+	outb_p(smbcmd | SMBHSTCNT_START, SMBHSTCNT(priv));
 
+	for (i = 1; i <= len; i++) {
 		status = i801_wait_byte_done(priv);
 		if (status)
 			return status;
@@ -710,9 +706,12 @@ static int i801_block_transaction_byte_by_byte(struct i801_priv *priv,
 			data->block[0] = len;
 		}
 
-		/* Retrieve/store value in SMBBLKDAT */
-		if (read_write == I2C_SMBUS_READ)
+		if (read_write == I2C_SMBUS_READ) {
 			data->block[i] = inb_p(SMBBLKDAT(priv));
+			if (i == len - 1)
+				outb_p(smbcmd | SMBHSTCNT_LAST_BYTE, SMBHSTCNT(priv));
+		}
+
 		if (read_write == I2C_SMBUS_WRITE && i+1 <= len)
 			outb_p(data->block[i+1], SMBBLKDAT(priv));
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
maintainer Acked-by
#2 lore raw

Hi Jean,

Quoted 9 lines · Jean Delvare
Quoted 8 lines · Heiner Kallweit

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

Quoted 5 lines
Quoted 1 line

Note for Wolfram: checkpatch says we should insert here:

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Anyway, you can add:

Thanks,
Andi

#3 lore raw
Quoted 12 lines · Andi Shyti

Hi Jean,

Quoted 9 lines · Jean Delvare
Quoted 8 lines · Heiner Kallweit

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

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Note for Wolfram: checkpatch says we should insert here:

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Quoting Jean from previous communication about this patch:
As far as I see, the race condition already existed when the kernel switched to git, so there's no point in having a Fixes statement.

Quoted 6 lines

Anyway, you can add:

Thanks,
Andi

maintainer
#4 lore raw

Hi Heiner,

Quoted 17 lines · Heiner Kallweit
Quoted 16 lines · Andi Shyti

Hi Jean,

Quoted 13 lines · Jean Delvare
Quoted 9 lines · Heiner Kallweit

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

Note for Wolfram: checkpatch says we should insert here:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Quoting Jean from previous communication about this patch:
As far as I see, the race condition already existed when the kernel switched to git, so there's no point in having a Fixes statement.

true... I forgot about this comment.

Anyway I think that this should, then, go to all the stable kernels and I believe that without the Fixes tag this will never be picked up.

Maybe Greg can advise here.

Would you mind resending this patch Cc'eing the stable kernel and adding a note after the '---'?

Andi

Quoted 7 lines
Quoted 6 lines

Anyway, you can add:

Thanks,
Andi

#5 lore raw
Quoted 29 lines · Andi Shyti

Hi Heiner,

Quoted 17 lines · Heiner Kallweit
Quoted 16 lines · Andi Shyti

Hi Jean,

Quoted 13 lines · Jean Delvare
Quoted 9 lines · Heiner Kallweit

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

Note for Wolfram: checkpatch says we should insert here:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Quoting Jean from previous communication about this patch:
As far as I see, the race condition already existed when the kernel switched to git, so there's no point in having a Fixes statement.

Quoted 6 lines

true... I forgot about this comment.

Anyway I think that this should, then, go to all the stable kernels and I believe that without the Fixes tag this will never be picked up.

Then we may have to set the Fixes tag to the following? 1da177e4c3f4 ("Linux-2.6.12-rc2")
Plus a comment that the issue existed before already.

Quoted 2 lines

Maybe Greg can advise here.

I *think* Greg (or a bot of him) would complain when receiving something for stable w/o a Fixes tag.

Quoted 3 lines

Would you mind resending this patch Cc'eing the stable kernel and adding a note after the '---'?

OK

Quoted 9 lines

Andi

Quoted 7 lines
Quoted 6 lines

Anyway, you can add:

Thanks,
Andi

#6 lore raw
Quoted 37 lines · Heiner Kallweit
Quoted 29 lines · Andi Shyti

Hi Heiner,

Quoted 17 lines · Heiner Kallweit
Quoted 16 lines · Andi Shyti

Hi Jean,

Quoted 13 lines · Jean Delvare
Quoted 9 lines · Heiner Kallweit

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

Note for Wolfram: checkpatch says we should insert here:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Quoting Jean from previous communication about this patch:
As far as I see, the race condition already existed when the kernel switched to git, so there's no point in having a Fixes statement.

Quoted 6 lines

true... I forgot about this comment.

Anyway I think that this should, then, go to all the stable kernels and I believe that without the Fixes tag this will never be picked up.

Quoted 8 lines

Then we may have to set the Fixes tag to the following? 1da177e4c3f4 ("Linux-2.6.12-rc2")
Plus a comment that the issue existed before already.

Quoted 2 lines

Maybe Greg can advise here.

I *think* Greg (or a bot of him) would complain when receiving something for stable w/o a Fixes tag.

No, fixes tags are not required, but they are nice. For the full set of rules on how to do this, please see:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html

#7 lore raw
In reply to Andi Shyti
Quoted 27 lines · Andi Shyti
Quoted 15 lines · Heiner Kallweit
Quoted 14 lines · Andi Shyti
Quoted 13 lines · Jean Delvare
Quoted 9 lines · Heiner Kallweit

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

Note for Wolfram: checkpatch says we should insert here:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

does this also need a Fixes: tag? I tried to check it, but there was an intricate jungle of commits in these lines.

Quoting Jean from previous communication about this patch:
As far as I see, the race condition already existed when the kernel switched to git, so there's no point in having a Fixes statement.

true... I forgot about this comment.

Quoted 8 lines

Anyway I think that this should, then, go to all the stable kernels and I believe that without the Fixes tag this will never be picked up.

Maybe Greg can advise here.

Would you mind resending this patch Cc'eing the stable kernel and adding a note after the '---'?

Again, no. This is a theoretical bug, that was discovered by code inspection. It's been present in the driver for over 20 years and we have no evidence that anyone ever hit it.

Furthermore, it only happens if the driver is in polling mode, which is uncommon, and only for block transactions, and only when the block buffer isn't used. And then the only negative effect of the bug is to shift the internal register pointer by 1. So it would only ever be a problem if someone mixes block reads and immediate byte reads on the same device. That's a very uncommon usage model. That's definitely not "a real bug that bothers people".

Not every fix needs to go to stable. In this case, our engineering time is better used elsewhere.

Signature · 2 lines
Jean Delvare
SUSE L3 Support