Skip to content

Commit a094ccf

Browse files
krzkrafaeljw
authored andcommitted
thermal: of: Use scoped memory and OF handling to simplify thermal_of_trips_init()
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>
1 parent 69f3aa6 commit a094ccf

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

drivers/thermal/thermal_of.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,45 +95,32 @@ static int thermal_of_populate_trip(struct device_node *np,
9595

9696
static struct thermal_trip *thermal_of_trips_init(struct device_node *np, int *ntrips)
9797
{
98-
struct thermal_trip *tt;
99-
struct device_node *trips;
10098
int ret, count;
10199

102100
*ntrips = 0;
103101

104-
trips = of_get_child_by_name(np, "trips");
102+
struct device_node *trips __free(device_node) = of_get_child_by_name(np, "trips");
105103
if (!trips)
106104
return NULL;
107105

108106
count = of_get_child_count(trips);
109107
if (!count)
110108
return NULL;
111109

112-
tt = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
113-
if (!tt) {
114-
ret = -ENOMEM;
115-
goto out_of_node_put;
116-
}
117-
118-
*ntrips = count;
110+
struct thermal_trip *tt __free(kfree) = kzalloc(sizeof(*tt) * count, GFP_KERNEL);
111+
if (!tt)
112+
return ERR_PTR(-ENOMEM);
119113

120114
count = 0;
121115
for_each_child_of_node_scoped(trips, trip) {
122116
ret = thermal_of_populate_trip(trip, &tt[count++]);
123117
if (ret)
124-
goto out_kfree;
118+
return ERR_PTR(ret);
125119
}
126120

127-
of_node_put(trips);
128-
129-
return tt;
130-
131-
out_kfree:
132-
kfree(tt);
133-
out_of_node_put:
134-
of_node_put(trips);
121+
*ntrips = count;
135122

136-
return ERR_PTR(ret);
123+
return no_free_ptr(tt);
137124
}
138125

139126
static struct device_node *of_thermal_zone_find(struct device_node *sensor, int id)

0 commit comments

Comments
 (0)