Skip to contentSkip to conversation

thermal/core: Use thermal_zone_device_param

RFC PATCH In review Daniel Lezcano 3 participants 5 messages Aug 23, 2024 – Aug 29, 2024 Thermal

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/2 thermal/core: Use thermal_zone_device_param

The function thermal_zone_device_register_*() have now a significant number of parameters.

Simplify the parameters by extending the thermal_zone_device_param structure with the parameters usually used when registering the thermal zone.

With that change we have a simpler function:

thermal_zone_device_register()

which can be reused in the different drivers and replace the duplicate thermal_zone_device_register_with_trips() and thermal_zone_device_register_tripless() functions.

Cc Jérémie Garcia <[email protected]>
Cc Alexandre Bailon <[email protected]>
---
 drivers/thermal/thermal_core.c |  9 +++++++
 include/linux/thermal.h        | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
drivers/thermal/thermal_core.c +9 -0
index e6669aeda1ff..5869562caf9e 100644
@@ -1390,6 +1390,15 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
 }
 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
 
+struct thermal_zone_device *thermal_zone_device_register(const char *type,
+							 const struct thermal_zone_params *tzp)
+{
+	return thermal_zone_device_register_with_trips(type, tzp->trips, tzp->num_trips,
+						       tzp->devdata, tzp->ops,
+						       tzp, tzp->passive_delay,
+						       tzp->polling_delay);
+}
+
 /**
  * thermal_zone_device_register_with_trips() - register a new thermal zone device
  * @type:	the thermal zone device type
include/linux/thermal.h +43 -0

66 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH 2/2 thermal/of: Use thermal_zone_device_param

As we can use the thermal_zone_device_param structure to pass the thermal zone parameters, let's use it in thermal-of code.

Cc Jérémie Garcia <[email protected]>
Cc Alexandre Bailon <[email protected]>
---
 drivers/thermal/thermal_of.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
drivers/thermal/thermal_of.c +7 -12
index 1f252692815a..3e494ff88648 100644
@@ -474,12 +474,9 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
 {
 	struct thermal_zone_device_ops of_ops = *ops;
 	struct thermal_zone_device *tz;
-	struct thermal_trip *trips;
-	struct thermal_zone_params tzp = {};
+	struct thermal_zone_params tzp = { .devdata = data, .ops = &of_ops };
 	struct device_node *np;
 	const char *action;
-	int delay, pdelay;
-	int ntrips;
 	int ret;
 
 	np = of_thermal_zone_find(sensor, id);
@@ -489,14 +486,14 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
 		return ERR_CAST(np);
 	}
 
-	trips = thermal_of_trips_init(np, &ntrips);
-	if (IS_ERR(trips)) {
+	tzp.trips = thermal_of_trips_init(np, &tzp.num_trips);
+	if (IS_ERR(tzp.trips)) {
 		pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
 		ret = PTR_ERR(trips);
 		goto out_of_node_put;
 	}
 
-	ret = thermal_of_monitor_init(np, &delay, &pdelay);
+	ret = thermal_of_monitor_init(np, &tzp.polling_delay, &tzp.passive_delay);
 	if (ret) {
 		pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
 		goto out_kfree_trips;
@@ -512,9 +509,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
 		if (!of_ops.critical && !strcasecmp(action, "reboot"))
 			of_ops.critical = thermal_zone_device_critical_reboot;
 
-	tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips,
-						     data, &of_ops, &tzp,
-						     pdelay, delay);
+	tz = thermal_zone_device_register(np->name, &tzp);
 	if (IS_ERR(tz)) {
 		ret = PTR_ERR(tz);
 		pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
@@ -522,7 +517,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
 	}
 
 	of_node_put(np);
-	kfree(trips);
+	kfree(tzp.trips);
 
 	ret = thermal_zone_device_enable(tz);
 	if (ret) {
@@ -535,7 +530,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
 	return tz;
 
 out_kfree_trips:
-	kfree(trips);
+	kfree(tzp.trips);
 out_of_node_put:
 	of_node_put(np);
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH 2/2] thermal/of: Use thermal_zone_device_param

kernel test robot noticed the following build errors:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on rafael-pm/thermal]
[also build test ERROR on linus/master v6.11-rc5 next-20240828] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-of-Use-thermal_zone_device_param/20240826-151207
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
patch link:    https://lore.kernel.org/r/20240823154245.1553458-2-daniel.lezcano%40linaro.org
patch subject: [RFC PATCH 2/2] thermal/of: Use thermal_zone_device_param
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20240829/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240829/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/thermal/thermal_of.c: In function 'thermal_of_zone_register':

>> drivers/thermal/thermal_of.c:492:31: error: 'trips' undeclared (first use in this function)
     492 |                 ret = PTR_ERR(trips);
         |                               ^~~~~

drivers/thermal/thermal_of.c:492:31: note: each undeclared identifier is reported only once for each function it appears in

vim +/trips +492 drivers/thermal/thermal_of.c

3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  451  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  452  /**
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  453   * thermal_of_zone_register - Register a thermal zone with device node
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  454   * sensor
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  455   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  456   * The thermal_of_zone_register() parses a device tree given a device
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  457   * node sensor and identifier. It searches for the thermal zone
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  458   * associated to the couple sensor/id and retrieves all the thermal
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  459   * zone properties and registers new thermal zone with those
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  460   * properties.
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  461   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  462   * @sensor: A device node pointer corresponding to the sensor in the device tree
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  463   * @id: An integer as sensor identifier
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  464   * @data: A private data to be stored in the thermal zone dedicated private area
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  465   * @ops: A set of thermal sensor ops
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  466   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  467   * Return: a valid thermal zone structure pointer on success.
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  468   *	- EINVAL: if the device tree thermal description is malformed
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  469   *	- ENOMEM: if one structure can not be allocated
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  470   *	- Other negative errors are returned by the underlying called functions
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  471   */
ac614a9b4c35bf Daniel Lezcano      2023-04-04  472  static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  473  							    const struct thermal_zone_device_ops *ops)
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  474  {
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  475  	struct thermal_zone_device_ops of_ops = *ops;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  476  	struct thermal_zone_device *tz;
5313e239ab6b58 Daniel Lezcano      2024-08-23  477  	struct thermal_zone_params tzp = { .devdata = data, .ops = &of_ops };
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  478  	struct device_node *np;
62e79e38b257a5 Fabio Estevam       2023-11-29  479  	const char *action;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  480  	int ret;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  481  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  482  	np = of_thermal_zone_find(sensor, id);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  483  	if (IS_ERR(np)) {
9d6792df07367a Daniel Lezcano      2022-08-09  484  		if (PTR_ERR(np) != -ENODEV)
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  485  			pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  486  		return ERR_CAST(np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  487  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  488  
5313e239ab6b58 Daniel Lezcano      2024-08-23  489  	tzp.trips = thermal_of_trips_init(np, &tzp.num_trips);
5313e239ab6b58 Daniel Lezcano      2024-08-23  490  	if (IS_ERR(tzp.trips)) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  491  		pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14 @492  		ret = PTR_ERR(trips);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  493  		goto out_of_node_put;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  494  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  495  
5313e239ab6b58 Daniel Lezcano      2024-08-23  496  	ret = thermal_of_monitor_init(np, &tzp.polling_delay, &tzp.passive_delay);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  497  	if (ret) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  498  		pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  499  		goto out_kfree_trips;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  500  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  501  
ac4436a5b20e0e Ahmad Fatoum        2023-07-08  502  	thermal_of_parameters_init(np, &tzp);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  503  
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  504  	of_ops.bind = thermal_of_bind;
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  505  	of_ops.unbind = thermal_of_unbind;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  506  
62e79e38b257a5 Fabio Estevam       2023-11-29  507  	ret = of_property_read_string(np, "critical-action", &action);
62e79e38b257a5 Fabio Estevam       2023-11-29  508  	if (!ret)
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  509  		if (!of_ops.critical && !strcasecmp(action, "reboot"))
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  510  			of_ops.critical = thermal_zone_device_critical_reboot;
62e79e38b257a5 Fabio Estevam       2023-11-29  511  
5313e239ab6b58 Daniel Lezcano      2024-08-23  512  	tz = thermal_zone_device_register(np->name, &tzp);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  513  	if (IS_ERR(tz)) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  514  		ret = PTR_ERR(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  515  		pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
ac4436a5b20e0e Ahmad Fatoum        2023-07-08  516  		goto out_kfree_trips;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  517  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  518  
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  519  	of_node_put(np);
5313e239ab6b58 Daniel Lezcano      2024-08-23  520  	kfree(tzp.trips);
9b0a62758665e4 Rafael J. Wysocki   2024-02-22  521  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  522  	ret = thermal_zone_device_enable(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  523  	if (ret) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  524  		pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  525  		       tz->type, tz->id, ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  526  		thermal_of_zone_unregister(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  527  		return ERR_PTR(ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  528  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  529  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  530  	return tz;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  531  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  532  out_kfree_trips:
5313e239ab6b58 Daniel Lezcano      2024-08-23  533  	kfree(tzp.trips);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  534  out_of_node_put:
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  535  	of_node_put(np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  536  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  537  	return ERR_PTR(ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  538  }
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  539  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#3 lore raw

Re: [RFC PATCH 2/2] thermal/of: Use thermal_zone_device_param

In reply to Daniel Lezcano
kernel test robot noticed the following build errors:

Hi Daniel,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on rafael-pm/thermal]
[also build test ERROR on linus/master v6.11-rc5 next-20240828] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-of-Use-thermal_zone_device_param/20240826-151207
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
patch link:    https://lore.kernel.org/r/20240823154245.1553458-2-daniel.lezcano%40linaro.org
patch subject: [RFC PATCH 2/2] thermal/of: Use thermal_zone_device_param
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20240829/[email protected]/config)
compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 08e5a1de8227512d4774a534b91cb2353cef6284)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240829/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/thermal/thermal_of.c:15:
In file included from include/linux/thermal.h:15:
In file included from include/linux/device.h:32:
In file included from include/linux/device/driver.h:21:
In file included from include/linux/module.h:19:
In file included from include/linux/elf.h:6:
In file included from arch/s390/include/asm/elf.h:181:
In file included from arch/s390/include/asm/mmu_context.h:11:
In file included from arch/s390/include/asm/pgalloc.h:18:
In file included from include/linux/mm.h:2228:
include/linux/vmstat.h:503:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

     503 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     504 |                            item];
         |                            ~~~~
   include/linux/vmstat.h:510:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     510 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     511 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
   include/linux/vmstat.h:517:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
     517 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
         |                               ~~~~~~~~~~~ ^ ~~~
   include/linux/vmstat.h:523:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
     523 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~ ^
     524 |                            NR_VM_NUMA_EVENT_ITEMS +
         |                            ~~~~~~~~~~~~~~~~~~~~~~
>> drivers/thermal/thermal_of.c:492:17: error: use of undeclared identifier 'trips'
     492 |                 ret = PTR_ERR(trips);
         |                               ^

4 warnings and 1 error generated.

vim +/trips +492 drivers/thermal/thermal_of.c

3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  451  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  452  /**
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  453   * thermal_of_zone_register - Register a thermal zone with device node
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  454   * sensor
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  455   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  456   * The thermal_of_zone_register() parses a device tree given a device
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  457   * node sensor and identifier. It searches for the thermal zone
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  458   * associated to the couple sensor/id and retrieves all the thermal
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  459   * zone properties and registers new thermal zone with those
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  460   * properties.
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  461   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  462   * @sensor: A device node pointer corresponding to the sensor in the device tree
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  463   * @id: An integer as sensor identifier
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  464   * @data: A private data to be stored in the thermal zone dedicated private area
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  465   * @ops: A set of thermal sensor ops
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  466   *
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  467   * Return: a valid thermal zone structure pointer on success.
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  468   *	- EINVAL: if the device tree thermal description is malformed
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  469   *	- ENOMEM: if one structure can not be allocated
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  470   *	- Other negative errors are returned by the underlying called functions
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  471   */
ac614a9b4c35bf Daniel Lezcano      2023-04-04  472  static struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int id, void *data,
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  473  							    const struct thermal_zone_device_ops *ops)
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  474  {
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  475  	struct thermal_zone_device_ops of_ops = *ops;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  476  	struct thermal_zone_device *tz;
5313e239ab6b58 Daniel Lezcano      2024-08-23  477  	struct thermal_zone_params tzp = { .devdata = data, .ops = &of_ops };
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  478  	struct device_node *np;
62e79e38b257a5 Fabio Estevam       2023-11-29  479  	const char *action;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  480  	int ret;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  481  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  482  	np = of_thermal_zone_find(sensor, id);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  483  	if (IS_ERR(np)) {
9d6792df07367a Daniel Lezcano      2022-08-09  484  		if (PTR_ERR(np) != -ENODEV)
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  485  			pr_err("Failed to find thermal zone for %pOFn id=%d\n", sensor, id);
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  486  		return ERR_CAST(np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  487  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  488  
5313e239ab6b58 Daniel Lezcano      2024-08-23  489  	tzp.trips = thermal_of_trips_init(np, &tzp.num_trips);
5313e239ab6b58 Daniel Lezcano      2024-08-23  490  	if (IS_ERR(tzp.trips)) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  491  		pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14 @492  		ret = PTR_ERR(trips);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  493  		goto out_of_node_put;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  494  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  495  
5313e239ab6b58 Daniel Lezcano      2024-08-23  496  	ret = thermal_of_monitor_init(np, &tzp.polling_delay, &tzp.passive_delay);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  497  	if (ret) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  498  		pr_err("Failed to initialize monitoring delays from %pOFn\n", np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  499  		goto out_kfree_trips;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  500  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  501  
ac4436a5b20e0e Ahmad Fatoum        2023-07-08  502  	thermal_of_parameters_init(np, &tzp);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  503  
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  504  	of_ops.bind = thermal_of_bind;
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  505  	of_ops.unbind = thermal_of_unbind;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  506  
62e79e38b257a5 Fabio Estevam       2023-11-29  507  	ret = of_property_read_string(np, "critical-action", &action);
62e79e38b257a5 Fabio Estevam       2023-11-29  508  	if (!ret)
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  509  		if (!of_ops.critical && !strcasecmp(action, "reboot"))
698a1eb1f75eb6 Rafael J. Wysocki   2024-02-22  510  			of_ops.critical = thermal_zone_device_critical_reboot;
62e79e38b257a5 Fabio Estevam       2023-11-29  511  
5313e239ab6b58 Daniel Lezcano      2024-08-23  512  	tz = thermal_zone_device_register(np->name, &tzp);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  513  	if (IS_ERR(tz)) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  514  		ret = PTR_ERR(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  515  		pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret);
ac4436a5b20e0e Ahmad Fatoum        2023-07-08  516  		goto out_kfree_trips;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  517  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  518  
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  519  	of_node_put(np);
5313e239ab6b58 Daniel Lezcano      2024-08-23  520  	kfree(tzp.trips);
9b0a62758665e4 Rafael J. Wysocki   2024-02-22  521  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  522  	ret = thermal_zone_device_enable(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  523  	if (ret) {
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  524  		pr_err("Failed to enabled thermal zone '%s', id=%d: %d\n",
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  525  		       tz->type, tz->id, ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  526  		thermal_of_zone_unregister(tz);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  527  		return ERR_PTR(ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  528  	}
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  529  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  530  	return tz;
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  531  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  532  out_kfree_trips:
5313e239ab6b58 Daniel Lezcano      2024-08-23  533  	kfree(tzp.trips);
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  534  out_of_node_put:
662b52b761bfe0 Krzysztof Kozlowski 2024-08-14  535  	of_node_put(np);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  536  
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  537  	return ERR_PTR(ret);
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  538  }
3fd6d6e2b4e80f Daniel Lezcano      2022-08-05  539  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to Daniel Lezcano
Quoted 3 lines · Daniel Lezcano

The function thermal_zone_device_register_*() have now a significant number of parameters.

Which may or may not be regarded as a problem.

To me, there are two arguments for doing a change like this:

(a) A struct initialization is less error-prone than passing a long list of arguments to a function. In the particular case of thermal_zone_device_register_with_trips(), the last two arguments are easy to mishandle because they are of the same type and similar meaning.

(b) It gets rid of multiline function invocations that are hard to read.

Quoted 11 lines

Simplify the parameters by extending the thermal_zone_device_param structure with the parameters usually used when registering the thermal zone.

With that change we have a simpler function:

thermal_zone_device_register()

which can be reused in the different drivers and replace the duplicate thermal_zone_device_register_with_trips() and thermal_zone_device_register_tripless() functions.

I actually think that having a special helper for registering a tripless zone is useful because it makes it super easy to find the code paths doing that.

Quoted 13 lines
Cc Jérémie Garcia <[email protected]>
Cc Alexandre Bailon <[email protected]>
---
 drivers/thermal/thermal_core.c |  9 +++++++
 include/linux/thermal.h        | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
drivers/thermal/thermal_core.c +0 -0
index e6669aeda1ff..5869562caf9e 100644

drivers/thermal/thermal_core.c int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp) · line 1399

Quoted 10 lines
drivers/thermal/thermal_core.c +7 -0
@@ -1390,6 +1390,15 @@ int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
 }
 EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
 
+struct thermal_zone_device *thermal_zone_device_register(const char *type,
+                                                        const struct thermal_zone_params *tzp)
+{
+       return thermal_zone_device_register_with_trips(type, tzp->trips, tzp->num_trips,
+                                                      tzp->devdata, tzp->ops,
+                                                      tzp, tzp->passive_delay,
+                                                      tzp->polling_delay);

My basic concern with this approach is the copying of pointers that may become invalid going forward to tzp.

Generally speaking, it is less than useful to hold on to copies of all data that is only used during thermal zone registration (like a pointer to the trips table for one example).

I would define a new struct type to hold all of the current thermal_zone_device_register_with_trips() parameters:

struct thermal_zone_data {
        const char *type,
        const struct thermal_trip *trips;
        int num_trips;
        void *devdata;
        const struct thermal_zone_device_ops *ops,
        const struct thermal_zone_params *tzp;
        unsigned int passive_delay;
        unsigned int polling_delay;
};

and pass that to a wrapper function:

int thermal_zone_register(struct thermal_zone_data *tzdata)

{
        return thermal_zone_device_register_with_trips(tzdata->type,

tzdata->trips, tzdata->num_trips,

              tzdata->devdata, tzdata->ops, tzdata->tzp,

              tzdata->passiva_delay, tzdata->polling_delay);
}

BTW, I don't think that the "device" part of the function name adds any value, so I wouldn't use it.

A similar thing can be done for the tripless case:

struct thermal_tripless_zone_data {
const char *type,
void *devdata;
const struct thermal_zone_device_ops *ops, bool no_hwmon;
};

int thermal_tripless_zone_register(thermal_tripless_zone_data *tzdata)

{
        struct thermal_zone_params tzp = { .no_hwmon = tzdata->no_hwmon };

        return thermal_zone_device_register_with_trips(tzdata->type, NULL, 0,

              tzdata->devdata, tzdata->ops, &tzp,

              0, 0);

}

And yes, I would do it so that the users of tripless thermal zones don't need to use a full struct thermal_zone_params just in order to pass the no_hwmon value.

Quoted 81 lines
drivers/thermal/thermal_core.c +2 -0
+}
+
 /**
  * thermal_zone_device_register_with_trips() - register a new thermal zone device
  * @type:      the thermal zone device type
include/linux/thermal.h +43 -1
index b86ddca46b9e..1681b9ddd890 100644
@@ -174,11 +174,45 @@ struct thermal_zone_params {
         *              Used by thermal zone drivers.
         */
        int slope;
+
        /*
         * @offset:     offset of a linear temperature adjustment curve.
         *              Used by thermal zone drivers (default 0).
         */
        int offset;
+
+       /*
+        * @trips:      a pointer to an array of thermal trips
+        */
+       const struct thermal_trip *trips;
+
+       /*
+        * @num_trips:  the number of trip points the thermal zone support
+        */
+       int num_trips;
+
+       /*
+        * @devdata:    private device data
+        */
+       void *devdata;
+
+       /*
+        * @ops:        standard thermal zone device callbacks
+        */
+       const struct thermal_zone_device_ops *ops;
+
+       /*
+        * @passive_delay:      number of milliseconds to wait between polls when
+        *                      performing passive cooling
+        */
+       unsigned int passive_delay;
+
+       /*
+        * @polling_delay:      number of milliseconds to wait between polls when checking
+        *                      whether trip points have been crossed (0 for interrupt
+        *                      driven systems)
+        */
+       unsigned int polling_delay;
 };
 
 /* Function declarations */
@@ -218,6 +252,10 @@ void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
 
 #ifdef CONFIG_THERMAL
+
+struct thermal_zone_device *thermal_zone_device_register(const char *type,
+                                                        const struct thermal_zone_params *tzp);
+
 struct thermal_zone_device *thermal_zone_device_register_with_trips(
                                        const char *type,
                                        const struct thermal_trip *trips,
@@ -281,6 +319,11 @@ int thermal_zone_device_enable(struct thermal_zone_device *tz);
 int thermal_zone_device_disable(struct thermal_zone_device *tz);
 void thermal_zone_device_critical(struct thermal_zone_device *tz);
 #else
+static inline struct thermal_zone_device *thermal_zone_device_register(
+       const char *type,
+       const struct thermal_zone_params *tzp)
+{ return ERR_PTR(-ENODEV); }
+
 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
                                        const char *type,
                                        const struct thermal_trip *trips,
--

2.43.0