Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

thermal/of: Fix cdev lookup in thermal_of_should_bind()

Since thermal_of_should_bind() terminates the loop after processing
the first child found in cooling-maps, it will never match more than
one cdev to a given trip point which is incorrect, as there may be
cooling-maps associating one trip point with multiple cooling devices.

Address this by letting the loop continue until either all
children have been processed or a matching one has been found.

To avoid adding conditionals or goto statements, put the loop in
question into a separate function and make that function return
right away after finding a matching cooling-maps entry.

Fixes: 94c6110b0b13 ("thermal/of: Use the .should_bind() thermal zone callback")
Link: https://lore.kernel.org/linux-pm/20250219-fix-thermal-of-v1-1-de36e7a590c4@chromium.org/
Reported-by: Yu-Che Cheng <giver@chromium.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Yu-Che Cheng <giver@chromium.org>
Tested-by: Yu-Che Cheng <giver@chromium.org>
Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>
Tested-by: Lukasz Luba <lukasz.luba@arm.com>
Link: https://patch.msgid.link/2788228.mvXUDI8C0e@rjwysocki.net

+29 -21
+29 -21
drivers/thermal/thermal_of.c
··· 274 274 return true; 275 275 } 276 276 277 + static bool thermal_of_cm_lookup(struct device_node *cm_np, 278 + const struct thermal_trip *trip, 279 + struct thermal_cooling_device *cdev, 280 + struct cooling_spec *c) 281 + { 282 + for_each_child_of_node_scoped(cm_np, child) { 283 + struct device_node *tr_np; 284 + int count, i; 285 + 286 + tr_np = of_parse_phandle(child, "trip", 0); 287 + if (tr_np != trip->priv) 288 + continue; 289 + 290 + /* The trip has been found, look up the cdev. */ 291 + count = of_count_phandle_with_args(child, "cooling-device", 292 + "#cooling-cells"); 293 + if (count <= 0) 294 + pr_err("Add a cooling_device property with at least one device\n"); 295 + 296 + for (i = 0; i < count; i++) { 297 + if (thermal_of_get_cooling_spec(child, i, cdev, c)) 298 + return true; 299 + } 300 + } 301 + 302 + return false; 303 + } 304 + 277 305 static bool thermal_of_should_bind(struct thermal_zone_device *tz, 278 306 const struct thermal_trip *trip, 279 307 struct thermal_cooling_device *cdev, ··· 321 293 goto out; 322 294 323 295 /* Look up the trip and the cdev in the cooling maps. */ 324 - for_each_child_of_node_scoped(cm_np, child) { 325 - struct device_node *tr_np; 326 - int count, i; 327 - 328 - tr_np = of_parse_phandle(child, "trip", 0); 329 - if (tr_np != trip->priv) 330 - continue; 331 - 332 - /* The trip has been found, look up the cdev. */ 333 - count = of_count_phandle_with_args(child, "cooling-device", "#cooling-cells"); 334 - if (count <= 0) 335 - pr_err("Add a cooling_device property with at least one device\n"); 336 - 337 - for (i = 0; i < count; i++) { 338 - result = thermal_of_get_cooling_spec(child, i, cdev, c); 339 - if (result) 340 - break; 341 - } 342 - 343 - break; 344 - } 296 + result = thermal_of_cm_lookup(cm_np, trip, cdev, c); 345 297 346 298 of_node_put(cm_np); 347 299 out: