[PATCH v1 2/2] thermal: Fail object registration if thermal class is not registered

From: Rafael J. Wysocki
Date: Fri Jan 20 2023 - 14:48:58 EST


From: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>

If thermal_class is not registered with the driver core, there is no way
to expose the interfaces used by the thermal control framework, so
prevent thermal zones and cooling devices from being registered in
that case by returning an error from object registration functions.

For this purpose, introduce class_is_registered() that checks the
private pointer of the given class and returns 'false' if it is NULL,
which means that the class has not been registered, and use it in the
thermal framework.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
---
drivers/thermal/thermal_core.c | 6 ++++++
include/linux/device/class.h | 5 +++++
2 files changed, 11 insertions(+)

Index: linux-pm/include/linux/device/class.h
===================================================================
--- linux-pm.orig/include/linux/device/class.h
+++ linux-pm/include/linux/device/class.h
@@ -82,6 +82,11 @@ struct class_dev_iter {
const struct device_type *type;
};

+static inline bool class_is_registered(struct class *class)
+{
+ return !!class->p;
+}
+
extern struct kobject *sysfs_dev_block_kobj;
extern struct kobject *sysfs_dev_char_kobj;
extern int __must_check __class_register(struct class *class,
Index: linux-pm/drivers/thermal/thermal_core.c
===================================================================
--- linux-pm.orig/drivers/thermal/thermal_core.c
+++ linux-pm/drivers/thermal/thermal_core.c
@@ -880,6 +880,9 @@ __thermal_cooling_device_register(struct
!ops->set_cur_state)
return ERR_PTR(-EINVAL);

+ if (!class_is_registered(&thermal_class))
+ return ERR_PTR(-ENODEV);
+
cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
if (!cdev)
return ERR_PTR(-ENOMEM);
@@ -1342,6 +1345,9 @@ thermal_zone_device_register_with_trips(
if (num_trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp) && !trips)
return ERR_PTR(-EINVAL);

+ if (!class_is_registered(&thermal_class))
+ return ERR_PTR(-ENODEV);
+
tz = kzalloc(sizeof(*tz), GFP_KERNEL);
if (!tz)
return ERR_PTR(-ENOMEM);