Skip to contentSkip to conversation

tee: optee: fix device enumeration error handling

PATCH In review Jens Wiklander 2 participants 2 messages Nov 6, 2019 – Nov 7, 2019

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

PATCH 1/1 tee: optee: fix device enumeration error handling

Prior to this patch in optee_probe() when optee_enumerate_devices() was called the struct optee was fully initialized. If optee_enumerate_devices() returns an error optee_probe() is supposed to clean up and free the struct optee completely, but will at this late stage need to call optee_remove() instead. This isn't done and thus freeing the struct optee prematurely.

With this patch the call to optee_enumerate_devices() is done after optee_probe() has returned successfully and in case optee_enumerate_devices() fails everything is cleaned up with a call to optee_remove().

Fixes c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
---
 drivers/tee/optee/core.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/tee/optee/core.c +12 -8
index 1854a3db7345..b830e0a87fba 100644
@@ -643,11 +643,6 @@ static struct optee *optee_probe(struct device_node *np)
 	if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
 		pr_info("dynamic shared memory is enabled\n");
 
-	rc = optee_enumerate_devices();
-	if (rc)
-		goto err;
-
-	pr_info("initialized driver\n");
 	return optee;
 err:
 	if (optee) {
@@ -702,9 +697,10 @@ static struct optee *optee_svc;
 
 static int __init optee_driver_init(void)
 {
-	struct device_node *fw_np;
-	struct device_node *np;
-	struct optee *optee;
+	struct device_node *fw_np = NULL;
+	struct device_node *np = NULL;
+	struct optee *optee = NULL;
+	int rc = 0;
 
 	/* Node is supposed to be below /firmware */
 	fw_np = of_find_node_by_name(NULL, "firmware");
@@ -723,6 +719,14 @@ static int __init optee_driver_init(void)
 	if (IS_ERR(optee))
 		return PTR_ERR(optee);
 
+	rc = optee_enumerate_devices();
+	if (rc) {
+		optee_remove(optee);
+		return rc;
+	}
+
+	pr_info("initialized driver\n");
+
 	optee_svc = optee;
 
 	return 0;
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Jens Wiklander

Prior to this patch in optee_probe() when optee_enumerate_devices() was called the struct optee was fully initialized. If optee_enumerate_devices() returns an error optee_probe() is supposed to clean up and free the struct optee completely, but will at this late stage need to call optee_remove() instead. This isn't done and thus freeing the struct optee prematurely.

With this patch the call to optee_enumerate_devices() is done after optee_probe() has returned successfully and in case optee_enumerate_devices() fails everything is cleaned up with a call to optee_remove().

Quoted 6 lines
Fixes c3fa24af9244 ("tee: optee: add TEE bus device enumeration support")
---
 drivers/tee/optee/core.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
Quoted 48 lines
drivers/tee/optee/core.c +12 -9
index 1854a3db7345..b830e0a87fba 100644
@@ -643,11 +643,6 @@ static struct optee *optee_probe(struct device_node *np)
        if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
                pr_info("dynamic shared memory is enabled\n");
 
-       rc = optee_enumerate_devices();
-       if (rc)
-               goto err;
-
-       pr_info("initialized driver\n");
        return optee;
 err:
        if (optee) {
@@ -702,9 +697,10 @@ static struct optee *optee_svc;
 
 static int __init optee_driver_init(void)
 {
-       struct device_node *fw_np;
-       struct device_node *np;
-       struct optee *optee;
+       struct device_node *fw_np = NULL;
+       struct device_node *np = NULL;
+       struct optee *optee = NULL;
+       int rc = 0;
 
        /* Node is supposed to be below /firmware */
        fw_np = of_find_node_by_name(NULL, "firmware");
@@ -723,6 +719,14 @@ static int __init optee_driver_init(void)
        if (IS_ERR(optee))
                return PTR_ERR(optee);
 
+       rc = optee_enumerate_devices();
+       if (rc) {
+               optee_remove(optee);
+               return rc;
+       }
+
+       pr_info("initialized driver\n");
+
        optee_svc = optee;
 
        return 0;
--

2.17.1