Skip to contentSkip to conversation

scsi: ufs: Remove redundant ret check in rpmb

PATCH New Ao Sun 2 participants 2 messages Jun 18

Subsystems (2)
lore
Share & tools
lore

Ao Sun

author
#0 lore raw

PATCH 1/1 scsi: ufs: Remove redundant ret check in rpmb

From: Ao Sun <[email protected]>

All error paths return early with non-zero ret, so ret is always zero at the final read. Remove redundant check.

---
 drivers/ufs/core/ufs-rpmb.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/ufs/core/ufs-rpmb.c +3 -5
index ffad049872b9..55d0884dd412 100644
@@ -115,11 +115,9 @@ static int ufs_rpmb_route_frames(struct device *dev, u8 *req, unsigned int req_l
 		}
 	}
 
-	if (!ret) {
-		ret = ufs_sec_submit(hba, protocol_id, resp, resp_len, false);
-		if (ret)
-			dev_err(dev, "Response read failed with ret=%d\n", ret);
-	}
+	ret = ufs_sec_submit(hba, protocol_id, resp, resp_len, false);
+	if (ret)
+		dev_err(dev, "Response read failed with ret=%d\n", ret);
 
 	return ret;
 }
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Incorrect device lifecycle management for ufs_rpmb_dev causes a reference cycle and Use-After-Free on device removal. --

commit e8c5159f6cdc84b865b38447899724970aac1440
Author: Ao Sun <[email protected]>

scsi: ufs: Remove redundant ret check in rpmb

Removes an unnecessary check of the return value before calling ufs_sec_submit() at the end of ufs_rpmb_route_frames(). All earlier error paths return early, ensuring ret is zero at this point.

Quoted 9 lines
drivers/ufs/core/ufs-rpmb.c +0 -1
index ffad049872b9a..55d0884dd4124 100644
@@ -115,11 +115,9 @@ static int ufs_rpmb_route_frames(struct device *dev, u8 *req, unsigned int req_l
 		}
 	}
 
-	if (!ret) {

drivers/ufs/core/ufs-rpmb.c static int ufs_rpmb_route_frames(struct device *dev, u8 *req, unsigned int req_l · line 123

Quoted 10 lines
drivers/ufs/core/ufs-rpmb.c +3 -4
-		ret = ufs_sec_submit(hba, protocol_id, resp, resp_len, false);
-		if (ret)
-			dev_err(dev, "Response read failed with ret=%d\n", ret);
-	}
+	ret = ufs_sec_submit(hba, protocol_id, resp, resp_len, false);
+	if (ret)
+		dev_err(dev, "Response read failed with ret=%d\n", ret);
 
 	return ret;
 }

[Severity: Critical]
This isn't a bug introduced by this patch, but there is a device lifecycle issue causing a reference cycle and use-after-free on device removal.

The ufs_rpmb structure is allocated via devm_kzalloc() in ufs_rpmb_probe():

drivers/ufs/core/ufs-rpmb.c:ufs_rpmb_probe() {
    ...
    ufs_rpmb = devm_kzalloc(hba->dev, sizeof(*ufs_rpmb), GFP_KERNEL);
    ...
}

Because the child RPMB device (rdev) natively holds a reference to its parent device (ufs_rpmb->dev), unregistering the child in the parent's release callback creates an unbreakable cycle:

drivers/ufs/core/ufs-rpmb.c:ufs_rpmb_device_release() {
    ...
    rpmb_dev_unregister(ufs_rpmb->rdev);
    ...
}

Since ufs_rpmb_remove() unregisters the parent without explicitly unregistering the child device:

drivers/ufs/core/ufs-rpmb.c:ufs_rpmb_remove() {
    ...
    device_unregister(&ufs_rpmb->dev);
    ...
}

The parent's release callback will never execute because the child still holds a reference to it.

Because ufs_rpmb is allocated with devm_kzalloc(), its memory is forcefully freed when the host device unbinds. Does this leave the driver core and still-registered child device holding dangling pointers to freed memory, guaranteeing a use-after-free during driver unbind or module unload?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1