diff mbox series

[2/7] thermal: of: Use scoped device node handling to simplify thermal_of_trips_init()

Message ID 20240814-b4-cleanup-h-of-node-put-thermal-v1-2-7a1381e1627e@linaro.org
State Superseded
Headers show
Series thermal: scope/cleanup.h improvements | expand

Commit Message

Krzysztof Kozlowski Aug. 14, 2024, 8:17 p.m. UTC
Obtain the device node reference with scoped/cleanup.h to reduce error
handling and make the code a bit simpler.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 drivers/thermal/thermal_of.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

Comments

Chen-Yu Tsai Aug. 15, 2024, 7:15 a.m. UTC | #1
On Wed, Aug 14, 2024 at 10:17:48PM +0200, Krzysztof Kozlowski wrote:
> Obtain the device node reference with scoped/cleanup.h to reduce error
> handling and make the code a bit simpler.
> 
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>  drivers/thermal/thermal_of.c | 14 ++++----------
>  1 file changed, 4 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
> index a2278d4ad886..c8ded4462bb8 100644
> --- a/drivers/thermal/thermal_of.c
> +++ b/drivers/thermal/thermal_of.c
> @@ -117,10 +117,9 @@ static int thermal_of_populate_trip(struct device_node *np,
>  static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
>  {
>  	struct thermal_trip *tt;
> -	struct device_node *trips;
>  	int ret, count;
>  
> -	trips = of_get_child_by_name(np, "trips");
> +	struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
>  	if (!trips) {
>  		pr_err("Failed to find 'trips' node\n");
>  		return ERR_PTR(-EINVAL);
> @@ -129,15 +128,12 @@ static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *n
>  	count = of_get_child_count(trips);
>  	if (!count) {
>  		pr_err("No trip point defined\n");
> -		ret = -EINVAL;
> -		goto out_of_node_put;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
> -	if (!tt) {
> -		ret = -ENOMEM;
> -		goto out_of_node_put;
> -	}
> +	if (!tt)
> +		return ERR_PTR(-ENOMEM);
>  
>  	*ntrips = count;

Also drop the "of_node_put(trips);" in the successful path?

Once fixed,

Reviewed-by: Chen-Yu Tsai <wenst@chromium.org>

And I plan to send a patch on top of this making "tt" auto released,
thereby eliminating the error path.

>  
> @@ -155,8 +151,6 @@ static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *n
>  out_kfree:
>  	kfree(tt);
>  	*ntrips = 0;
> -out_of_node_put:
> -	of_node_put(trips);
>  
>  	return ERR_PTR(ret);
>  }
> 
> -- 
> 2.43.0
>
Krzysztof Kozlowski Aug. 16, 2024, 5:28 a.m. UTC | #2
On 15/08/2024 09:15, Chen-Yu Tsai wrote:
>>  
>>  	tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
>> -	if (!tt) {
>> -		ret = -ENOMEM;
>> -		goto out_of_node_put;
>> -	}
>> +	if (!tt)
>> +		return ERR_PTR(-ENOMEM);
>>  
>>  	*ntrips = count;
> 
> Also drop the "of_node_put(trips);" in the successful path?
> 

Right.

Best regards,
Krzysztof
Dragan Simic Aug. 17, 2024, 2:47 a.m. UTC | #3
On 2024-08-15 09:15, Chen-Yu Tsai wrote:
> On Wed, Aug 14, 2024 at 10:17:48PM +0200, Krzysztof Kozlowski wrote:
>> Obtain the device node reference with scoped/cleanup.h to reduce error
>> handling and make the code a bit simpler.
>> 
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>> ---
>>  drivers/thermal/thermal_of.c | 14 ++++----------
>>  1 file changed, 4 insertions(+), 10 deletions(-)
>> 
>> diff --git a/drivers/thermal/thermal_of.c 
>> b/drivers/thermal/thermal_of.c
>> index a2278d4ad886..c8ded4462bb8 100644
>> --- a/drivers/thermal/thermal_of.c
>> +++ b/drivers/thermal/thermal_of.c
>> @@ -117,10 +117,9 @@ static int thermal_of_populate_trip(struct 
>> device_node *np,
>>  static struct thermal_trip *thermal_of_trips_init(struct device_node 
>> *np, int *ntrips)
>>  {
>>  	struct thermal_trip *tt;
>> -	struct device_node *trips;
>>  	int ret, count;
>> 
>> -	trips = of_get_child_by_name(np, "trips");
>> +	struct device_node *trips __free(device_node) = 
>> of_get_child_by_name(np, "trips");
>>  	if (!trips) {
>>  		pr_err("Failed to find 'trips' node\n");
>>  		return ERR_PTR(-EINVAL);
>> @@ -129,15 +128,12 @@ static struct thermal_trip 
>> *thermal_of_trips_init(struct device_node *np, int *n
>>  	count = of_get_child_count(trips);
>>  	if (!count) {
>>  		pr_err("No trip point defined\n");
>> -		ret = -EINVAL;
>> -		goto out_of_node_put;
>> +		return ERR_PTR(-EINVAL);
>>  	}
>> 
>>  	tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
>> -	if (!tt) {
>> -		ret = -ENOMEM;
>> -		goto out_of_node_put;
>> -	}
>> +	if (!tt)
>> +		return ERR_PTR(-ENOMEM);
>> 
>>  	*ntrips = count;
> 
> Also drop the "of_node_put(trips);" in the successful path?
> 
> Once fixed,
> 
> Reviewed-by: Chen-Yu Tsai <wenst@chromium.org>
> 
> And I plan to send a patch on top of this making "tt" auto released,
> thereby eliminating the error path.

Ah, with that additional patch in place, I totally agree with
moving "*ntrips = 0" to the top.

>> @@ -155,8 +151,6 @@ static struct thermal_trip 
>> *thermal_of_trips_init(struct device_node *np, int *n
>>  out_kfree:
>>  	kfree(tt);
>>  	*ntrips = 0;
>> -out_of_node_put:
>> -	of_node_put(trips);
>> 
>>  	return ERR_PTR(ret);
>>  }
>> 
>> --
>> 2.43.0
>>
diff mbox series

Patch

diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index a2278d4ad886..c8ded4462bb8 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -117,10 +117,9 @@  static int thermal_of_populate_trip(struct device_node *np,
 static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
 {
 	struct thermal_trip *tt;
-	struct device_node *trips;
 	int ret, count;
 
-	trips = of_get_child_by_name(np, "trips");
+	struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
 	if (!trips) {
 		pr_err("Failed to find 'trips' node\n");
 		return ERR_PTR(-EINVAL);
@@ -129,15 +128,12 @@  static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *n
 	count = of_get_child_count(trips);
 	if (!count) {
 		pr_err("No trip point defined\n");
-		ret = -EINVAL;
-		goto out_of_node_put;
+		return ERR_PTR(-EINVAL);
 	}
 
 	tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
-	if (!tt) {
-		ret = -ENOMEM;
-		goto out_of_node_put;
-	}
+	if (!tt)
+		return ERR_PTR(-ENOMEM);
 
 	*ntrips = count;
 
@@ -155,8 +151,6 @@  static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *n
 out_kfree:
 	kfree(tt);
 	*ntrips = 0;
-out_of_node_put:
-	of_node_put(trips);
 
 	return ERR_PTR(ret);
 }