Skip to contentSkip to conversation

ti_sci: Address some issues found by Smatch

PATCH In review Andrew Goodbody 4 participants 7 messages Oct 8, 2025 – Oct 16, 2025 Texas instruments' system control interface (TISCI) protocol driver

lore
Share & tools
lore
#0 lore raw

v3 cover ti_sci: Address some issues found by Smatch

Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.

---
Changes in v3:
- Pick up review tags
- Change dev_dbg call to dev_err to be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add cast when assigning to resp
- Replace malloc with devm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value

 drivers/firmware/ti_sci.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

---
base-commit: c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdba

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

PATCH v3 1/3 ti_sci: Prevent memory leak

temp is assigned the pointer returned by malloc which is used without a NULL check and then never freed. Add a NULL check and ensure temp is freed on all return paths.

This issue was found by Smatch.

---
 drivers/firmware/ti_sci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/firmware/ti_sci.c +5 -1
index 8013afef304dfb70ab14150ddc49d43ebebb5902..2042d88583766213e9d74dd17289cfd3562f0acb 100644
@@ -3083,7 +3083,10 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
 		dev_err(dev, "%s resource type ids not available\n", of_prop);
 		return ERR_PTR(sets);
 	}
-	temp = malloc(sets);
+	temp = devm_kmalloc(dev, sets, GFP_KERNEL);
+	if (!temp)
+		return ERR_PTR(-ENOMEM);
+
 	sets /= sizeof(u32);
 	res->sets = sets;
 
@@ -3123,6 +3126,7 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
 			return ERR_PTR(-ENOMEM);
 	}
 
+	devm_kfree(dev, temp);
 	if (valid_set)
 		return res;
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH v3 2/3 ti_sci: Provide error code on error exit

In reply to Andrew Goodbody

In ti_sci_get_response the check for message sequence will return ret on a fail but ret will be 0 at that point. Instead return -EINVAL. Also change dev_dbg call to dev_err to be consistent with other error detection code in the same function.

This issue was found by Smatch.

---
 drivers/firmware/ti_sci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/firmware/ti_sci.c +2 -2
index 2042d88583766213e9d74dd17289cfd3562f0acb..534326be5ecedba1bf221d04d708b6db15f09706 100644
@@ -191,9 +191,9 @@ static int ti_sci_get_response(struct ti_sci_info *info,
 
 	/* Sanity check for message response */
 	if (hdr->seq != info->seq) {
-		dev_dbg(info->dev, "%s: Message for %d is not expected\n",
+		dev_err(info->dev, "%s: Message for %d is not expected\n",
 			__func__, hdr->seq);
-		return ret;
+		return -EINVAL;
 	}
 
 	if (msg->len > info->desc->max_msg_size) {
Signature · 1 line
2.47.3
#3 lore raw

PATCH v3 3/3 ti_sci: Pointer is never assigned to valid value

In reply to Andrew Goodbody

The pointer resp is declared but never assigned a value but is then dereferenced. Fix this by assigning the pointer to the message buffer.

This issue was found by Smatch.

---
 drivers/firmware/ti_sci.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/firmware/ti_sci.c +2 -0
index 534326be5ecedba1bf221d04d708b6db15f09706..6f57dcfe8de8f874318cd53372c798d182faae73 100644
@@ -1365,6 +1365,8 @@ static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
 	if (ret)
 		return ret;
 
+	resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->tx_message.buf;
+
 	*parent_id = resp->parent_id;
 
 	return ret;
Signature · 1 line
2.47.3
Tested-by on the v3 series
#4 lore raw
In reply to Andrew Goodbody
Quoted 24 lines · Andrew Goodbody

Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.

---
Changes in v3:
- Pick up review tags
- Change dev_dbg call to dev_err to be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add cast when assigning to resp
- Replace malloc with devm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value

Quoted 7 lines
 drivers/firmware/ti_sci.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

---
base-commit: c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdba

Best regards,

For the entire series:

Thanks for the cleanup!
Regards,
Anshul

maintainer Reviewed-by on the v3 series
#5 lore raw
In reply to Andrew Goodbody
Quoted 24 lines · Andrew Goodbody

Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.

---
Changes in v3:
- Pick up review tags
- Change dev_dbg call to dev_err to be consistent with surrounding code in 2/3
- Update subject line of 3/3 to be more explicit
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add cast when assigning to resp
- Replace malloc with devm_kmalloc
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (3):
ti_sci: Prevent memory leak
ti_sci: Provide error code on error exit
ti_sci: Pointer is never assigned to valid value

Quoted 10 lines
 drivers/firmware/ti_sci.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

---
base-commit: c280c4f7e52f08255f491783b3143a3be1fc08b0
change-id: 20250725-ti_sci-7bc55cb0cdba

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>

Also tested with UART boot against u-boot master commit 92dcb3ad5d98 ("cmd/dma: implement dmareset command")

https://gist.github.com/nmenon/1f569681f43cfed456fcce729e0c6e29

Signature · 4 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
https://ti.com/opensource
#6 lore raw
In reply to Andrew Goodbody
Quoted 5 lines · Andrew Goodbody

Prevent a memory leak from a non-freed malloc. Return an error code on an error path.
Assign a value to a pointer before it is dereferenced.

Applied to u-boot/master, thanks!

[1/3] ti_sci: Prevent memory leak

      commit: 4b8bc5af80f1637cd71a1b6a0dffa7588a03f27c
[2/3] ti_sci: Provide error code on error exit
      commit: 4602fa92202388665ddfbb622f001d62b1473a08
[3/3] ti_sci: Pointer is never assigned to valid value
      commit: 5b9125746dfde0a29911ccd542801da6b8f94239
Signature · 1 line
Tom