Skip to contentSkip to conversation

driver core/thermal: Fail registration of thermal object when thermal_class is not registered

PATCH In review Rafael J. Wysocki 4 participants 10 messages Jan 20, 2023 – Jan 24, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover driver core/thermal: Fail registration of thermal object when thermal_class is not registered

Hi All,

If thermal_class is not registered, the whole thermal framework is basically unusable, because the governors are not there and its sysfs interface is not present.

In that case it doesn't make sense to register thermal zones and cooling devices, because they cannot be used as expected anyway, so make it possible to fail the registration of these things if the registration of thermal_class has failed.

To that end, make sure that the private pointer of a class is NULL when that class is not registered with the driver core (patch [1/2]).

Next, make the thermal framework check the value of the thermal_class' private pointer against NULL and fail the registration of thermal zones and cooling devices when it is NULL (patch [2/2]).

Thanks!

#1 lore raw

PATCH 1/2 driver core: class: Clear private pointer on registration failures

From: Rafael J. Wysocki <[email protected]>

Clear the class private pointer if __class_register() fails for it, so as to allow its users to verify that the class is usable by checking the value of that pointer.

For consistency, clear that pointer before freeing the object pointed to by it in class_release().

---
 drivers/base/class.c |   16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/base/class.c +11 -5
Index: linux-pm/drivers/base/class.c
===================================================================
@@ -53,6 +53,8 @@ static void class_release(struct kobject
 
 	pr_debug("class '%s': release.\n", class->name);
 
+	class->p = NULL;
+
 	if (class->class_release)
 		class->class_release(class);
 	else
@@ -186,17 +188,21 @@ int __class_register(struct class *cls,
 	cls->p = cp;
 
 	error = kset_register(&cp->subsys);
-	if (error) {
-		kfree(cp);
-		return error;
-	}
+	if (error)
+		goto err_out;
+
 	error = class_add_groups(class_get(cls), cls->class_groups);
 	class_put(cls);
 	if (error) {
 		kobject_del(&cp->subsys.kobj);
 		kfree_const(cp->subsys.kobj.name);
-		kfree(cp);
+		goto err_out;
 	}
+	return 0;
+
+err_out:
+	kfree(cp);
+	cls->p = NULL;
 	return error;
 }
 EXPORT_SYMBOL_GPL(__class_register);
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] driver core: class: Clear private pointer on registration failures

Quoted 11 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Clear the class private pointer if __class_register() fails for it, so as to allow its users to verify that the class is usable by checking the value of that pointer.

For consistency, clear that pointer before freeing the object pointed to by it in class_release().

---

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#3 lore raw

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

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 drivers/thermal/thermal_core.c |    6 ++++++
 include/linux/device/class.h   |    5 +++++
 2 files changed, 11 insertions(+)
include/linux/device/class.h +5 -0
Index: 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,
drivers/thermal/thermal_core.c +6 -0
Index: 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);
Acked-by on v1 2/2
#4 lore raw

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

Quoted 7 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

Quoted 6 lines

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.

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#5 lore raw

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

In reply to Rafael J. Wysocki
Quoted 23 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 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
===================================================================

include/linux/device/class.h struct class_dev_iter { · line 87

Quoted 6 lines
include/linux/device/class.h +3 -0
@@ -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;

I really do not like this as it is exposing internals to drivers and whenever we do that, it gets abused and we have to unwind the mess in a few years.

Overall, I'm trying to remove the ->p usage, but that's a longterm goal of mine (to allow class and bus structures to be in read-only memory), which isn't your issue here, but it's good to think about why you want to know this information (more below.)

Quoted 10 lines
include/linux/device/class.h +2 -0
+}
+
 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
===================================================================

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct · line 884

Quoted 5 lines
drivers/thermal/thermal_core.c +2 -0
@@ -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);

If the class isn't registered, then sommething went wrong with the thermal core code, right? So why isn't the thermal core keeping a local variable of "class was registered" and relying on the driver core to know this?

The number of individual users that should be doing one thing or another if a class is not registered feels very very slim. How come this code is being called at all if the thermal class was not registered in the first place? What would have prevented that from happening? Is it an ordering issue, or a kernel configuration issue?

thanks,

greg k-h

#6 lore raw

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

Quoted 62 lines · Greg Kroah-Hartman
Quoted 23 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 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
===================================================================

include/linux/device/class.h struct class_dev_iter { · line 87

Quoted 6 lines
include/linux/device/class.h +3 -0
@@ -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;

I really do not like this as it is exposing internals to drivers and whenever we do that, it gets abused and we have to unwind the mess in a few years.

Overall, I'm trying to remove the ->p usage, but that's a longterm goal of mine (to allow class and bus structures to be in read-only memory), which isn't your issue here, but it's good to think about why you want to know this information (more below.)

Quoted 10 lines
include/linux/device/class.h +2 -0
+}
+
 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
===================================================================

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct · line 884

Quoted 5 lines
drivers/thermal/thermal_core.c +2 -0
@@ -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);

If the class isn't registered, then sommething went wrong with the thermal core code, right? So why isn't the thermal core keeping a local variable of "class was registered" and relying on the driver core to know this?

Quoted 5 lines

The number of individual users that should be doing one thing or another if a class is not registered feels very very slim. How come this code is being called at all if the thermal class was not registered in the first place? What would have prevented that from happening? Is it an ordering issue, or a kernel configuration issue?

It's basically a matter of class_register() returning an error.

Yes, we could use an extra variable for this purpose, but that would be a bit wasteful, because thermal_class will then sit unused and occupy memory in vain.

Oh well, we may as well just allocate it dynamically.

#7 lore raw

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

Quoted 64 lines · Rafael J. Wysocki
Quoted 62 lines · Greg Kroah-Hartman
Quoted 23 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 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
===================================================================

include/linux/device/class.h struct class_dev_iter { · line 87

Quoted 6 lines
include/linux/device/class.h +3 -0
@@ -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;

I really do not like this as it is exposing internals to drivers and whenever we do that, it gets abused and we have to unwind the mess in a few years.

Overall, I'm trying to remove the ->p usage, but that's a longterm goal of mine (to allow class and bus structures to be in read-only memory), which isn't your issue here, but it's good to think about why you want to know this information (more below.)

Quoted 10 lines
include/linux/device/class.h +2 -0
+}
+
 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
===================================================================

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct · line 884

Quoted 5 lines
drivers/thermal/thermal_core.c +2 -0
@@ -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);

If the class isn't registered, then sommething went wrong with the thermal core code, right? So why isn't the thermal core keeping a local variable of "class was registered" and relying on the driver core to know this?

Quoted 7 lines
Quoted 5 lines

The number of individual users that should be doing one thing or another if a class is not registered feels very very slim. How come this code is being called at all if the thermal class was not registered in the first place? What would have prevented that from happening? Is it an ordering issue, or a kernel configuration issue?

It's basically a matter of class_register() returning an error.

Ok, so not a real problem then :)

Quoted 3 lines

Yes, we could use an extra variable for this purpose, but that would be a bit wasteful, because thermal_class will then sit unused and occupy memory in vain.

How would it retain memory if class_register() failed?

Quoted 1 line

Oh well, we may as well just allocate it dynamically.

Allocate what?

confused,

greg k-h

#8 lore raw

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

Quoted 74 lines · Greg Kroah-Hartman
Quoted 64 lines · Rafael J. Wysocki
Quoted 62 lines · Greg Kroah-Hartman
Quoted 23 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

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.

---
 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
===================================================================

include/linux/device/class.h struct class_dev_iter { · line 87

Quoted 6 lines
include/linux/device/class.h +3 -0
@@ -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;

I really do not like this as it is exposing internals to drivers and whenever we do that, it gets abused and we have to unwind the mess in a few years.

Overall, I'm trying to remove the ->p usage, but that's a longterm goal of mine (to allow class and bus structures to be in read-only memory), which isn't your issue here, but it's good to think about why you want to know this information (more below.)

Quoted 10 lines
include/linux/device/class.h +2 -0
+}
+
 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
===================================================================

drivers/thermal/thermal_core.c __thermal_cooling_device_register(struct · line 884

Quoted 5 lines
drivers/thermal/thermal_core.c +2 -0
@@ -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);

If the class isn't registered, then sommething went wrong with the thermal core code, right? So why isn't the thermal core keeping a local variable of "class was registered" and relying on the driver core to know this?

Quoted 7 lines
Quoted 5 lines

The number of individual users that should be doing one thing or another if a class is not registered feels very very slim. How come this code is being called at all if the thermal class was not registered in the first place? What would have prevented that from happening? Is it an ordering issue, or a kernel configuration issue?

It's basically a matter of class_register() returning an error.

Quoted 7 lines

Ok, so not a real problem then :)

Quoted 3 lines

Yes, we could use an extra variable for this purpose, but that would be a bit wasteful, because thermal_class will then sit unused and occupy memory in vain.

How would it retain memory if class_register() failed?

The point was that we might use the existing (but not registered) class object to "flag" the fact that the class could not be used without adding extra variables.

Quoted 3 lines
Quoted 1 line

Oh well, we may as well just allocate it dynamically.

Allocate what?

Well, that was a bit terse, sorry.

This patch implements what I meant:
https://patchwork.kernel.org/project/linux-pm/patch/5660360.DvuYhMxLoT@kreacher/

#9 lore raw

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

Quoted 10 lines · Rafael J. Wysocki
Quoted 3 lines · Greg Kroah-Hartman
Quoted 1 line

Oh well, we may as well just allocate it dynamically.

Allocate what?

Well, that was a bit terse, sorry.

This patch implements what I meant:
https://patchwork.kernel.org/project/linux-pm/patch/5660360.DvuYhMxLoT@kreacher/

That looks good, I like that change, it's much simpler overall.

greg k-h