Skip to contentSkip to conversation

i2c: designware: Fix improper usage of readl

PATCH Applied Jan Dabros 4 participants 6 messages Feb 18, 2022 – Mar 1, 2022

Subsystems (3)
lore
Share & tools
lore

Jan Dabros

author
#0 lore raw

PATCH 1/1 i2c: designware: Fix improper usage of readl

Kernel test robot reported incorrect type in argument 1 of readl(), but more importantly it brought attention that MMIO accessor shouldn't be used in this case, since req->hdr.status is part of a command-response buffer in system memory.

Since its value may be altered by PSP outside of the scope of current thread (somehow similar to IRQ handler case), we need to use READ_ONCE() to ensure compiler won't optimize this call.

Fix also 'status' variable type to reflect that corresponding field in command-response buffer is platform-independent u32.

---
 drivers/i2c/busses/i2c-designware-amdpsp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +3 -2
index 752e0024db03..c64e459afb5c 100644
@@ -160,9 +160,10 @@ static int psp_send_cmd(struct psp_i2c_req *req)
 /* Helper to verify status returned by PSP */
 static int check_i2c_req_sts(struct psp_i2c_req *req)
 {
-	int status;
+	u32 status;
 
-	status = readl(&req->hdr.status);
+	/* Status field in command-response buffer is updated by PSP */
+	status = READ_ONCE(req->hdr.status);
 
 	switch (status) {
 	case PSP_I2C_REQ_STS_OK:
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jan Dabros

Kernel test robot reported incorrect type in argument 1 of readl(), but more importantly it brought attention that MMIO accessor shouldn't be used in this case, since req->hdr.status is part of a command-response buffer in system memory.

Since its value may be altered by PSP outside of the scope of current thread (somehow similar to IRQ handler case), we need to use READ_ONCE() to ensure compiler won't optimize this call.

Fix also 'status' variable type to reflect that corresponding field in command-response buffer is platform-independent u32.

Thanks for the fix, seems reasonable to me.

Quoted 26 lines
---
 drivers/i2c/busses/i2c-designware-amdpsp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-designware-amdpsp.c +3 -2
index 752e0024db03..c64e459afb5c 100644
@@ -160,9 +160,10 @@ static int psp_send_cmd(struct psp_i2c_req *req)
 /* Helper to verify status returned by PSP */
 static int check_i2c_req_sts(struct psp_i2c_req *req)
 {
-	int status;
+	u32 status;
 
-	status = readl(&req->hdr.status);
+	/* Status field in command-response buffer is updated by PSP */
+	status = READ_ONCE(req->hdr.status);
 
 	switch (status) {
 	case PSP_I2C_REQ_STS_OK:
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by
#2 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Jan Dabros

Kernel test robot reported incorrect type in argument 1 of readl(), but more importantly it brought attention that MMIO accessor shouldn't be used in this case, since req->hdr.status is part of a command-response buffer in system memory.

Since its value may be altered by PSP outside of the scope of current thread (somehow similar to IRQ handler case), we need to use READ_ONCE() to ensure compiler won't optimize this call.

Quoted 6 lines
Quoted 2 lines

Fix also 'status' variable type to reflect that corresponding field in command-response buffer is platform-independent u32.

Thanks for the fix, seems reasonable to me.

Applied
#3 lore raw
In reply to Jan Dabros
Quoted 9 lines · Jan Dabros

Kernel test robot reported incorrect type in argument 1 of readl(), but more importantly it brought attention that MMIO accessor shouldn't be used in this case, since req->hdr.status is part of a command-response buffer in system memory.

Since its value may be altered by PSP outside of the scope of current thread (somehow similar to IRQ handler case), we need to use READ_ONCE() to ensure compiler won't optimize this call.

Quoted 5 lines

Fix also 'status' variable type to reflect that corresponding field in command-response buffer is platform-independent u32.

Applied to for-next, thanks!

Jan, I wonder if you want to be the maintainer for this driver? If you'd like, then please send me the patch adding you to MAINTAINERS. So, you will get notified if people want to enhance this driver.

#4 lore raw

wt., 1 mar 2022 o 16:16 Wolfram Sang <[email protected]> napisał(a):

Quoted 11 lines · Jan Dabros
Quoted 9 lines

Kernel test robot reported incorrect type in argument 1 of readl(), but more importantly it brought attention that MMIO accessor shouldn't be used in this case, since req->hdr.status is part of a command-response buffer in system memory.

Since its value may be altered by PSP outside of the scope of current thread (somehow similar to IRQ handler case), we need to use READ_ONCE() to ensure compiler won't optimize this call.

Quoted 7 lines
Quoted 5 lines

Fix also 'status' variable type to reflect that corresponding field in command-response buffer is platform-independent u32.

Applied to for-next, thanks!

Thanks!

Quoted 3 lines

Jan, I wonder if you want to be the maintainer for this driver? If you'd like, then please send me the patch adding you to MAINTAINERS. So, you will get notified if people want to enhance this driver.

So actually I've already added myself as a R:eviewer for i2c-designware-* files in one of the previous patches with the purpose of reviewing code touching this driver. This makes sense since I can also test modifications on my device.

Best Regards,
Jan

#5 lore raw
Quoted 3 lines

So actually I've already added myself as a R:eviewer for i2c-designware-* files in one of the previous patches with the purpose

Ah, this is perfect then. I was only grepping for amdpsp, so I missed it. Thanks for the heads up!