[PATCH v5] soc: qcom: apr: Add check for idr_alloc and of_property_read_string_index

Jiasheng Jiang posted 1 patch 3 years, 6 months ago
There is a newer version of this series
drivers/soc/qcom/apr.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
[PATCH v5] soc: qcom: apr: Add check for idr_alloc and of_property_read_string_index
Posted by Jiasheng Jiang 3 years, 6 months ago
As idr_alloc() and of_property_read_string_index() can return negative
numbers, it should be better to check the return value and deal with
the exception.
Therefore, it should be better to use goto statement to stop and return
error.

Fixes: 6adba21eb434 ("soc: qcom: Add APR bus driver")
Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
---
Changelog:

v4 -> v5:

1. Change dev_err and goto statements.

v3 -> v4:

1. Change the title and remove the kfree.

v2 -> v3:

1. Change the title and use goto statement to deal with the exception.

v1 -> v2:

1. Add dev_err and put_device in order to maintain the code consistency.
---
 drivers/soc/qcom/apr.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c
index b4046f393575..a942c8a8dde2 100644
--- a/drivers/soc/qcom/apr.c
+++ b/drivers/soc/qcom/apr.c
@@ -454,11 +454,21 @@ static int apr_add_device(struct device *dev, struct device_node *np,
 	adev->dev.driver = NULL;
 
 	spin_lock(&apr->svcs_lock);
-	idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC);
+	ret = idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC);
 	spin_unlock(&apr->svcs_lock);
+	if (ret < 0) {
+		dev_err(dev, "idr_alloc failed: %d\n", ret);
+		put_device(&adev->dev);
+		goto out;
+	}
 
-	of_property_read_string_index(np, "qcom,protection-domain",
-				      1, &adev->service_path);
+	ret = of_property_read_string_index(np, "qcom,protection-domain",
+					    1, &adev->service_path);
+	if (ret < 0) {
+		dev_err(dev, "Failed to read second value of qcom,protection-domain\n");
+		put_device(&adev->dev);
+		goto out;
+	}
 
 	dev_info(dev, "Adding APR/GPR dev: %s\n", dev_name(&adev->dev));
 
@@ -468,6 +478,7 @@ static int apr_add_device(struct device *dev, struct device_node *np,
 		put_device(&adev->dev);
 	}
 
+out:
 	return ret;
 }
 
-- 
2.25.1
Re: [PATCH v5] soc: qcom: apr: Add check for idr_alloc and of_property_read_string_index
Posted by Bjorn Andersson 3 years, 5 months ago
On Fri, Oct 07, 2022 at 11:32:40AM +0800, Jiasheng Jiang wrote:
> As idr_alloc() and of_property_read_string_index() can return negative
> numbers, it should be better to check the return value and deal with
> the exception.
> Therefore, it should be better to use goto statement to stop and return
> error.
> 
> Fixes: 6adba21eb434 ("soc: qcom: Add APR bus driver")
> Signed-off-by: Jiasheng Jiang <jiasheng@iscas.ac.cn>
> ---
> Changelog:
> 
> v4 -> v5:
> 
> 1. Change dev_err and goto statements.
> 
> v3 -> v4:
> 
> 1. Change the title and remove the kfree.
> 
> v2 -> v3:
> 
> 1. Change the title and use goto statement to deal with the exception.
> 
> v1 -> v2:
> 
> 1. Add dev_err and put_device in order to maintain the code consistency.
> ---
>  drivers/soc/qcom/apr.c | 17 ++++++++++++++---
>  1 file changed, 14 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/soc/qcom/apr.c b/drivers/soc/qcom/apr.c
> index b4046f393575..a942c8a8dde2 100644
> --- a/drivers/soc/qcom/apr.c
> +++ b/drivers/soc/qcom/apr.c
> @@ -454,11 +454,21 @@ static int apr_add_device(struct device *dev, struct device_node *np,
>  	adev->dev.driver = NULL;
>  
>  	spin_lock(&apr->svcs_lock);
> -	idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC);
> +	ret = idr_alloc(&apr->svcs_idr, svc, svc_id, svc_id + 1, GFP_ATOMIC);
>  	spin_unlock(&apr->svcs_lock);
> +	if (ret < 0) {
> +		dev_err(dev, "idr_alloc failed: %d\n", ret);
> +		put_device(&adev->dev);
> +		goto out;
> +	}
>  
> -	of_property_read_string_index(np, "qcom,protection-domain",
> -				      1, &adev->service_path);
> +	ret = of_property_read_string_index(np, "qcom,protection-domain",
> +					    1, &adev->service_path);
> +	if (ret < 0) {
> +		dev_err(dev, "Failed to read second value of qcom,protection-domain\n");
> +		put_device(&adev->dev);
> +		goto out;
> +	}
>  
>  	dev_info(dev, "Adding APR/GPR dev: %s\n", dev_name(&adev->dev));
>  
> @@ -468,6 +478,7 @@ static int apr_add_device(struct device *dev, struct device_node *np,
>  		put_device(&adev->dev);

This put_device() rolls back the call to device_register(). It should
therefor not be replicated in the newly introduced error paths.

Other than that, this looks good to me.

Thanks,
Bjorn

>  	}
>  
> +out:
>  	return ret;
>  }
>  
> -- 
> 2.25.1
>