]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
thermal: of: Use scoped memory and OF handling to simplify thermal_of_trips_init()
authorKrzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Thu, 10 Oct 2024 18:06:18 +0000 (20:06 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Tue, 26 Nov 2024 12:28:43 +0000 (13:28 +0100)
Obtain the device node reference and allocate memory with
scoped/cleanup.h to reduce error handling and make the code a bit
simpler.

The code is not equivalent in one minor aspect: outgoing parameter
"*ntrips" will not be zeroed on errors of memory allocation.  This
difference is not important, because code was already not zeroing it in
case of earlier errors and the only caller does not rely on ntrips being
0 in case of errors.

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Reviewed-by: Chen-Yu Tsai <wenst@chromium.org>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://patch.msgid.link/20241010-b4-cleanup-h-of-node-put-thermal-v4-2-bfbe29ad81f4@linaro.org
[ rjw: Rebase ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/thermal/thermal_of.c

index e406b9150a2bc31a86ecd62cf1e2194dec5fd34b..9b0a3ec36a9035992de1a1d6ec824875a82db94e 100644 (file)
@@ -95,13 +95,11 @@ 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;
 
        *ntrips = 0;
        
-       trips = of_get_child_by_name(np, "trips");
+       struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
        if (!trips)
                return NULL;
 
@@ -109,31 +107,20 @@ static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *n
        if (!count)
                return NULL;
 
-       tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
-       if (!tt) {
-               ret = -ENOMEM;
-               goto out_of_node_put;
-       }
-
-       *ntrips = count;
+       struct thermal_trip *tt __free(kfree) = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
+       if (!tt)
+               return ERR_PTR(-ENOMEM);
 
        count = 0;
        for_each_child_of_node_scoped(trips, trip) {
                ret = thermal_of_populate_trip(trip, &tt[count++]);
                if (ret)
-                       goto out_kfree;
+                       return ERR_PTR(ret);
        }
 
-       of_node_put(trips);
-
-       return tt;
-
-out_kfree:
-       kfree(tt);
-out_of_node_put:
-       of_node_put(trips);
+       *ntrips = count;
 
-       return ERR_PTR(ret);
+       return no_free_ptr(tt);
 }
 
 static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)