Skip to content

Commit 792eac1

Browse files
committed
hwmon: (core) Add comment describing how hwdev is freed in error path
The hwmon core registers the hwmon device before adding sensors to the thermal core. If that fails, the hwmon device is released and an error is returned to the caller. From the code flow, it appears to be necessary to free struct hwmon_device *, allocated with kzalloc(), in that situation. This is incorrect, since the data structure will be freed automatically in hwmon_dev_release() when device_unregister() is called. This used to result in a double free, which was found and fixed with commit 74e3512 ("hwmon: (core) Fix double-free in __hwmon_device_register()"). This is, however, not obvious; any reader may erroneously conclude that the data structure is not freed. Add comment explaining why kfree() is not necessary in this situation. Reported-by: Eduardo Valentin <eduval@amazon.com> Cc: Eduardo Valentin <eduval@amazon.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 5fe625c commit 792eac1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

drivers/hwmon/hwmon.c

+6
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
651651
hwdev, j);
652652
if (err) {
653653
device_unregister(hdev);
654+
/*
655+
* Don't worry about hwdev;
656+
* hwmon_dev_release(), called
657+
* from device_unregister(),
658+
* will free it.
659+
*/
654660
goto ida_remove;
655661
}
656662
}

0 commit comments

Comments
 (0)