Skip to contentSkip to conversation

PCI: dwc: Fix signedness bug in fault injection test code

PATCH Applied Dan Carpenter 4 participants 4 messages May 12 – May 19

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: dwc: Fix signedness bug in fault injection test code

The kstrtou32() function returns negative error code or zero on success. However, in this case "val" is a u32 and the function returns signed longs so negative error codes from kstrtou32() are returned as high positive values.

Store the error code in an int instead.

Fixes d20ee8e2dbd6 ("PCI: dwc: Add debugfs based Error Injection support for DWC")
---
 drivers/pci/controller/dwc/pcie-designware-debugfs.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pci/controller/dwc/pcie-designware-debugfs.c +4 -3
index 945f8f9b6d0e..a6737bdc46ac 100644
@@ -305,6 +305,7 @@ static ssize_t err_inj_write(struct file *file, const char __user *buf,
 	u32 val, counter, vc_num, err_group, type_mask;
 	int val_diff = 0;
 	char *kern_buf;
+	int ret;
 
 	err_group = err_inj_list[pdata->idx].err_inj_group;
 	type_mask = err_inj_type_mask[err_group];
@@ -326,10 +327,10 @@ static ssize_t err_inj_write(struct file *file, const char __user *buf,
 			return -EINVAL;
 		}
 	} else {
-		val = kstrtou32(kern_buf, 0, &counter);
-		if (val) {
+		ret = kstrtou32(kern_buf, 0, &counter);
+		if (ret) {
 			kfree(kern_buf);
-			return val;
+			return ret;
 		}
 	}
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Dan Carpenter

The kstrtou32() function returns negative error code or zero on success. However, in this case "val" is a u32 and the function returns signed longs so negative error codes from kstrtou32() are returned as high positive values.

Store the error code in an int instead.

Fixes d20ee8e2dbd6 ("PCI: dwc: Add debugfs based Error Injection support for DWC")
Quoted 30 lines
---
  drivers/pci/controller/dwc/pcie-designware-debugfs.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
drivers/pci/controller/dwc/pcie-designware-debugfs.c +4 -3
index 945f8f9b6d0e..a6737bdc46ac 100644
@@ -305,6 +305,7 @@ static ssize_t err_inj_write(struct file *file, const char __user *buf,
  	u32 val, counter, vc_num, err_group, type_mask;
  	int val_diff = 0;
  	char *kern_buf;
+	int ret;
 
  	err_group = err_inj_list[pdata->idx].err_inj_group;
  	type_mask = err_inj_type_mask[err_group];
@@ -326,10 +327,10 @@ static ssize_t err_inj_write(struct file *file, const char __user *buf,
  			return -EINVAL;
  		}
  	} else {
-		val = kstrtou32(kern_buf, 0, &counter);
-		if (val) {
+		ret = kstrtou32(kern_buf, 0, &counter);
+		if (ret) {
  			kfree(kern_buf);
-			return val;
+			return ret;
  		}
  	}
 
#2 lore raw
In reply to Dan Carpenter
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Register accesses without ensuring active power state (missing pm_runtime). --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Register accesses without ensuring active power state (missing pm_runtime). --

commit 4899a9300a95dd54caf44bf07153849d88f68ae7
Author: Dan Carpenter <[email protected]>

PCI: dwc: Fix signedness bug in fault injection test code

This commit fixes a signedness bug in the fault injection debugfs handler. It ensures that negative error codes from kstrtou32 are properly handled by storing the return value in an int instead of a u32.

Quoted 4 lines
drivers/pci/controller/dwc/pcie-designware-debugfs.c +0 -0
index d0884253be97e..4bfc1748f2d73 100644

[ ... ]

drivers/pci/controller/dwc/pcie-designware-debugfs.c static ssize_t err_inj_write(struct file *file, const char __user *buf, · line 336

Quoted 13 lines
drivers/pci/controller/dwc/pcie-designware-debugfs.c +3 -3
@@ -327,10 +328,10 @@ static ssize_t err_inj_write(struct file *file, const char __user *buf,
 			return -EINVAL;
 		}
 	} else {
-		val = kstrtou32(kern_buf, 0, &counter);
-		if (val) {
+		ret = kstrtou32(kern_buf, 0, &counter);
+		if (ret) {
 			kfree(kern_buf);
-			return val;
+			return ret;
 		}
 	}

This isn't a regression introduced by this commit, but is it safe to access the hardware registers immediately after this block without ensuring the device is in an active power state?

This function handles user writes to debugfs, which can happen at any time. If the PCIe controller is runtime-suspended, accessing its memory-mapped registers via dw_pcie_readl_dbi() might cause a bus hang or kernel panic.

Would it make sense to call pm_runtime_get_sync() before interacting with the hardware registers later in this function?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Dan Carpenter
Quoted 8 lines · Dan Carpenter

The kstrtou32() function returns negative error code or zero on success. However, in this case "val" is a u32 and the function returns signed longs so negative error codes from kstrtou32() are returned as high positive values.

Store the error code in an int instead.

[...]

Applied, thanks!

[1/1] PCI: dwc: Fix signedness bug in fault injection test code

      commit: 94ac934d2c054fba4a22d8dc84749094c5fa0ec0

Best regards,

Signature · 1 line
Manivannan Sadhasivam <[email protected]>