Skip to contentSkip to conversation

thermal: core: Add helpers to browse the cdev, tz and governor list

PATCH Superseded Daniel Lezcano 4 participants 33 messages Jun 25, 2020 – Jul 2, 2020 Thermal

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/5 thermal: core: Add helpers to browse the cdev, tz and governor list

The cdev, tz and governor list, as well as their respective locks are statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each thermal entity.

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51 ++++++++++++++++++++++++++++++++++
 drivers/thermal/thermal_core.h |  9 ++++++
 2 files changed, 60 insertions(+)
drivers/thermal/thermal_core.c +51 -0
index 2a3f83265d8b..e2f8d2550ecd 100644
@@ -611,6 +611,57 @@ void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
 	mutex_unlock(&thermal_list_lock);
 }
 
+int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
+			      void *data)
+{
+	struct thermal_governor *gov;
+	int ret = 0;
+
+	mutex_lock(&thermal_governor_lock);
+	list_for_each_entry(gov, &thermal_governor_list, governor_list) {
+		ret = cb(gov, data);
+		if (ret)
+			break;
+	}
+	mutex_unlock(&thermal_governor_lock);
+
+	return ret;
+}
+
+int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
+					      void *), void *data)
+{
+	struct thermal_cooling_device *cdev;
+	int ret = 0;
+
+	mutex_lock(&thermal_list_lock);
+	list_for_each_entry(cdev, &thermal_cdev_list, node) {
+		ret = cb(cdev, data);
+		if (ret)
+			break;
+	}
+	mutex_unlock(&thermal_list_lock);
+
+	return ret;
+}
+
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
+			  void *data)
+{
+	struct thermal_zone_device *tz;
+	int ret = 0;
+
+	mutex_lock(&thermal_list_lock);
+	list_for_each_entry(tz, &thermal_tz_list, node) {
+		ret = cb(tz, data);
+		if (ret)
+			break;
+	}
+	mutex_unlock(&thermal_list_lock);
+
+	return ret;
+}
+
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
 					  const char *cdev_type, size_t size)
 {
drivers/thermal/thermal_core.h +9 -0
index 4e271016b7a9..bb8f8aee79eb 100644
@@ -41,6 +41,15 @@ extern struct thermal_governor *__governor_thermal_table_end[];
 	     __governor < __governor_thermal_table_end;	\
 	     __governor++)
 
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
+			  void *);
+
+int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
+					      void *), void *);
+
+int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
+			      void *thermal_governor);
+
 struct thermal_attr {
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/5 thermal: core: Get thermal zone by id

The next patch will introduce the generic netlink protocol to handle events, sampling and command from the thermal framework. In order to deal with the thermal zone, it uses its unique identifier to characterize it in the message. Passing an integer is more efficient than passing an entire string.

This change provides a function returning back a thermal zone pointer corresponding to the identifier passed as parameter.

---
 drivers/thermal/thermal_core.c | 14 ++++++++++++++
 drivers/thermal/thermal_core.h |  2 ++
 2 files changed, 16 insertions(+)
drivers/thermal/thermal_core.c +14 -0
index e2f8d2550ecd..58c95aeafb7f 100644
@@ -662,6 +662,20 @@ int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
 	return ret;
 }
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id)
+{
+	struct thermal_zone_device *tz = NULL;
+
+	mutex_lock(&thermal_list_lock);
+	list_for_each_entry(tz, &thermal_tz_list, node) {
+		if (tz->id == id)
+			break;
+	}
+	mutex_unlock(&thermal_list_lock);
+
+	return tz;
+}
+
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
 					  const char *cdev_type, size_t size)
 {
drivers/thermal/thermal_core.h +2 -0
index bb8f8aee79eb..7e8f45db6bbf 100644
@@ -50,6 +50,8 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
 			      void *thermal_governor);
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id);
+
 struct thermal_attr {
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
Signature · 1 line
2.17.1
Reviewed-by on v2 2/5
#2 lore raw

Re: [PATCH v2 2/5] thermal: core: Get thermal zone by id

Quoted 11 lines · Daniel Lezcano

The next patch will introduce the generic netlink protocol to handle events, sampling and command from the thermal framework. In order to deal with the thermal zone, it uses its unique identifier to characterize it in the message. Passing an integer is more efficient than passing an entire string.

This change provides a function returning back a thermal zone pointer corresponding to the identifier passed as parameter.

Quoted 46 lines
---
 drivers/thermal/thermal_core.c | 14 ++++++++++++++
 drivers/thermal/thermal_core.h |  2 ++
 2 files changed, 16 insertions(+)
drivers/thermal/thermal_core.c +14 -0
index e2f8d2550ecd..58c95aeafb7f 100644
@@ -662,6 +662,20 @@ int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
        return ret;
 }
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id)
+{
+       struct thermal_zone_device *tz = NULL;
+
+       mutex_lock(&thermal_list_lock);
+       list_for_each_entry(tz, &thermal_tz_list, node) {
+               if (tz->id == id)
+                       break;
+       }
+       mutex_unlock(&thermal_list_lock);
+
+       return tz;
+}
+
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
                                          const char *cdev_type, size_t size)
 {
drivers/thermal/thermal_core.h +2 -1
index bb8f8aee79eb..7e8f45db6bbf 100644
@@ -50,6 +50,8 @@ int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
                              void *thermal_governor);
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id);
+
 struct thermal_attr {
        struct device_attribute attr;
        char name[THERMAL_NAME_LENGTH];
--

2.17.1

reviewer
#3 lore raw

Re: [PATCH v2 2/5] thermal: core: Get thermal zone by id

In reply to Daniel Lezcano
Quoted 29 lines · Daniel Lezcano

The next patch will introduce the generic netlink protocol to handle events, sampling and command from the thermal framework. In order to deal with the thermal zone, it uses its unique identifier to characterize it in the message. Passing an integer is more efficient than passing an entire string.

This change provides a function returning back a thermal zone pointer corresponding to the identifier passed as parameter.

---
 drivers/thermal/thermal_core.c | 14 ++++++++++++++
 drivers/thermal/thermal_core.h |  2 ++
 2 files changed, 16 insertions(+)
drivers/thermal/thermal_core.c +4 -0
index e2f8d2550ecd..58c95aeafb7f 100644
@@ -662,6 +662,20 @@ int for_each_thermal_zone(int (*cb)(struct
 thermal_zone_device *, void *),
 	return ret;
 }
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id)
+{
+	struct thermal_zone_device *tz = NULL;
+

drivers/thermal/thermal_core.c int for_each_thermal_zone(int (*cb)(struct · line 679

Quoted 10 lines
drivers/thermal/thermal_core.c +10 -0
+	mutex_lock(&thermal_list_lock);
+	list_for_each_entry(tz, &thermal_tz_list, node) {
+		if (tz->id == id)
+			break;
+	}
+	mutex_unlock(&thermal_list_lock);
+
+	return tz;
+}
+

IMO, this one, as well as thermal_zone_get_zone_by_name(), should all be in thermal_helper.c

thanks,
rui

Quoted 21 lines
drivers/thermal/thermal_core.c +0 -0
 void thermal_zone_device_unbind_exception(struct thermal_zone_device
 *tz,

const char *cdev_type, size_t
size)
{

drivers/thermal/thermal_core.h +2 -0
index bb8f8aee79eb..7e8f45db6bbf 100644
@@ -50,6 +50,8 @@ int for_each_thermal_cooling_device(int
 (*cb)(struct thermal_cooling_device *,
 int for_each_thermal_governor(int (*cb)(struct thermal_governor *,
 void *),
 			      void *thermal_governor);
 
+struct thermal_zone_device *thermal_zone_get_by_id(int id);
+
 struct thermal_attr {
drivers/thermal/thermal_core.h +0 -0
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
#4 lore raw

PATCH v2 3/5 thermal: core: Remove old uapi generic netlink

In reply to Daniel Lezcano

In order to set the scene for the new generic netlink thermal management and notifications, let remove the old dead code remaining in the uapi headers.

---
 include/linux/thermal.h      |  5 -----
 include/uapi/linux/thermal.h | 12 +-----------
 2 files changed, 1 insertion(+), 16 deletions(-)
include/linux/thermal.h +0 -5
index faf7ad031e42..fc93a6348082 100644
@@ -302,11 +302,6 @@ struct thermal_zone_params {
 	int offset;
 };
 
-struct thermal_genl_event {
-	u32 orig;
-	enum events event;
-};
-
 /**
  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
  *
include/uapi/linux/thermal.h +1 -11
index 96218378dda8..22df67ed9e9c 100644
@@ -6,21 +6,12 @@
 
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02
 #define THERMAL_GENL_MCAST_GROUP_NAME           "thermal_mc_grp"
 
-/* Events supported by Thermal Netlink */
-enum events {
-	THERMAL_AUX0,
-	THERMAL_AUX1,
-	THERMAL_CRITICAL,
-	THERMAL_DEV_FAULT,
-};
-
 /* attributes of thermal_genl_family */
 enum {
 	THERMAL_GENL_ATTR_UNSPEC,
-	THERMAL_GENL_ATTR_EVENT,
 	__THERMAL_GENL_ATTR_MAX,
 };
 #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
@@ -28,7 +19,6 @@ enum {
 /* commands supported by the thermal_genl_family */
 enum {
 	THERMAL_GENL_CMD_UNSPEC,
-	THERMAL_GENL_CMD_EVENT,
 	__THERMAL_GENL_CMD_MAX,
 };
 #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
Signature · 1 line
2.17.1
#5 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 3 lines · Daniel Lezcano

In order to set the scene for the new generic netlink thermal management and notifications, let remove the old dead code remaining

s/management/management api/

s/let/let's/

Quoted 29 lines

in the uapi headers.

---
 include/linux/thermal.h      |  5 -----
 include/uapi/linux/thermal.h | 12 +-----------
 2 files changed, 1 insertion(+), 16 deletions(-)
include/linux/thermal.h +0 -5
index faf7ad031e42..fc93a6348082 100644
@@ -302,11 +302,6 @@ struct thermal_zone_params {
        int offset;
 };
 
-struct thermal_genl_event {
-       u32 orig;
-       enum events event;
-};
-
 /**
  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
  *
include/uapi/linux/thermal.h +0 -0
index 96218378dda8..22df67ed9e9c 100644

include/uapi/linux/thermal.h line 9

Quoted 5 lines
include/uapi/linux/thermal.h +1 -1
@@ -6,21 +6,12 @@
 
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Quoted 28 lines
include/uapi/linux/thermal.h +0 -11
 #define THERMAL_GENL_MCAST_GROUP_NAME           "thermal_mc_grp"
 
-/* Events supported by Thermal Netlink */
-enum events {
-       THERMAL_AUX0,
-       THERMAL_AUX1,
-       THERMAL_CRITICAL,
-       THERMAL_DEV_FAULT,
-};
-
 /* attributes of thermal_genl_family */
 enum {
        THERMAL_GENL_ATTR_UNSPEC,
-       THERMAL_GENL_ATTR_EVENT,
        __THERMAL_GENL_ATTR_MAX,
 };
 #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
@@ -28,7 +19,6 @@ enum {
 /* commands supported by the thermal_genl_family */
 enum {
        THERMAL_GENL_CMD_UNSPEC,
-       THERMAL_GENL_CMD_EVENT,
        __THERMAL_GENL_CMD_MAX,
 };
 #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
--

2.17.1

#6 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 40 lines · Amit Kucheria
Quoted 3 lines · Daniel Lezcano

In order to set the scene for the new generic netlink thermal management and notifications, let remove the old dead code remaining

s/management/management api/

s/let/let's/

Quoted 30 lines

in the uapi headers.

---
 include/linux/thermal.h      |  5 -----
 include/uapi/linux/thermal.h | 12 +-----------
 2 files changed, 1 insertion(+), 16 deletions(-)
include/linux/thermal.h +0 -5
index faf7ad031e42..fc93a6348082 100644
@@ -302,11 +302,6 @@ struct thermal_zone_params {
        int offset;
 };
 
-struct thermal_genl_event {
-       u32 orig;
-       enum events event;
-};
-
 /**
  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
  *
include/uapi/linux/thermal.h +0 -0
index 96218378dda8..22df67ed9e9c 100644
@@ -6,21 +6,12 @@
 
Quoted 7 lines

include/uapi/linux/thermal.h line 9

Quoted 4 lines
include/uapi/linux/thermal.h +1 -1
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Well, I've been very strict here for git-bisecting.

I move to V2 because of the removal, but when adding the new genetlink code, the family name changed, so we returned back to the V1 as it is a new genetlink thermal brand.

The name is change because it is no longer event based but also sampling and commands.

Quoted 28 lines
Quoted 28 lines
include/uapi/linux/thermal.h +0 -11
 #define THERMAL_GENL_MCAST_GROUP_NAME           "thermal_mc_grp"
 
-/* Events supported by Thermal Netlink */
-enum events {
-       THERMAL_AUX0,
-       THERMAL_AUX1,
-       THERMAL_CRITICAL,
-       THERMAL_DEV_FAULT,
-};
-
 /* attributes of thermal_genl_family */
 enum {
        THERMAL_GENL_ATTR_UNSPEC,
-       THERMAL_GENL_ATTR_EVENT,
        __THERMAL_GENL_ATTR_MAX,
 };
 #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
@@ -28,7 +19,6 @@ enum {
 /* commands supported by the thermal_genl_family */
 enum {
        THERMAL_GENL_CMD_UNSPEC,
-       THERMAL_GENL_CMD_EVENT,
        __THERMAL_GENL_CMD_MAX,
 };
 #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
--

2.17.1

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
#7 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 50 lines · Daniel Lezcano
Quoted 40 lines · Amit Kucheria
Quoted 3 lines · Daniel Lezcano

In order to set the scene for the new generic netlink thermal management and notifications, let remove the old dead code remaining

s/management/management api/

s/let/let's/

Quoted 30 lines

in the uapi headers.

---
 include/linux/thermal.h      |  5 -----
 include/uapi/linux/thermal.h | 12 +-----------
 2 files changed, 1 insertion(+), 16 deletions(-)
include/linux/thermal.h +0 -5
index faf7ad031e42..fc93a6348082 100644
@@ -302,11 +302,6 @@ struct thermal_zone_params {
        int offset;
 };
 
-struct thermal_genl_event {
-       u32 orig;
-       enum events event;
-};
-
 /**
  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
  *
include/uapi/linux/thermal.h +0 -0
index 96218378dda8..22df67ed9e9c 100644
@@ -6,21 +6,12 @@
 
Quoted 7 lines

include/uapi/linux/thermal.h line 9

Quoted 4 lines
include/uapi/linux/thermal.h +1 -1
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Quoted 5 lines

Well, I've been very strict here for git-bisecting.

I move to V2 because of the removal, but when adding the new genetlink code, the family name changed, so we returned back to the V1 as it is a new genetlink thermal brand.

I don't understand the move to v2 for an empty skeleton UAPI. For the purposes of bisection, couldn't you just remove all the v1 UAPI (w/o bumping to v2) and then add a new UAPI in the next patch?

Quoted 2 lines

The name is change because it is no longer event based but also sampling and commands.

In this case, just to avoid any confusion, the new UAPI could be v2 making the transition clear in case of bisection.

I'm afraid the v1->v2->v1 is a bit more confusing.

/Amit

#8 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 61 lines · Amit Kucheria
Quoted 50 lines · Daniel Lezcano
Quoted 40 lines · Amit Kucheria
Quoted 3 lines · Daniel Lezcano

In order to set the scene for the new generic netlink thermal management and notifications, let remove the old dead code remaining

s/management/management api/

s/let/let's/

Quoted 30 lines

in the uapi headers.

---
 include/linux/thermal.h      |  5 -----
 include/uapi/linux/thermal.h | 12 +-----------
 2 files changed, 1 insertion(+), 16 deletions(-)
include/linux/thermal.h +0 -5
index faf7ad031e42..fc93a6348082 100644
@@ -302,11 +302,6 @@ struct thermal_zone_params {
        int offset;
 };
 
-struct thermal_genl_event {
-       u32 orig;
-       enum events event;
-};
-
 /**
  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
  *
include/uapi/linux/thermal.h +0 -0
index 96218378dda8..22df67ed9e9c 100644
@@ -6,21 +6,12 @@
 
Quoted 7 lines

include/uapi/linux/thermal.h line 9

Quoted 4 lines
include/uapi/linux/thermal.h +1 -1
 /* Adding event notification support elements */
 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Quoted 5 lines

Well, I've been very strict here for git-bisecting.

I move to V2 because of the removal, but when adding the new genetlink code, the family name changed, so we returned back to the V1 as it is a new genetlink thermal brand.

I don't understand the move to v2 for an empty skeleton UAPI. For the purposes of bisection, couldn't you just remove all the v1 UAPI (w/o bumping to v2) and then add a new UAPI in the next patch?

Quoted 7 lines
Quoted 2 lines

The name is change because it is no longer event based but also sampling and commands.

In this case, just to avoid any confusion, the new UAPI could be v2 making the transition clear in case of bisection.

I'm afraid the v1->v2->v1 is a bit more confusing.

Let me elaborate a bit:

Why there is this patch ?
- By removing this code first, the next patch will just contain additions, I thought it would be clearer

Why increase the version here ?
- Code must continue to compile and as the 'thermal_event' family is now different from V1, the version is changed

Why the version goes to V1 in the next patch ?
- The family name is changed as it is not doing event only, so it is a new netlink thermal protocol and we begin at V1

So the main reason of this patch is to be very strict in the iteration changes. May be it is too much, in this case I can merge this patch with 4/5, the old netlink protocol removal will be lost in the addition of the new protocol. I'm fine with that if you think it is simpler.

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
#9 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 45 lines · Daniel Lezcano
Quoted 25 lines · Amit Kucheria
Quoted 14 lines · Daniel Lezcano
Quoted 11 lines · Amit Kucheria

include/uapi/linux/thermal.h line 9

Quoted 6 lines · Daniel Lezcano

/* Adding event notification support elements */
#define THERMAL_GENL_FAMILY_NAME "thermal_event"

include/uapi/linux/thermal.h +1 -1
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Quoted 5 lines

Well, I've been very strict here for git-bisecting.

I move to V2 because of the removal, but when adding the new genetlink code, the family name changed, so we returned back to the V1 as it is a new genetlink thermal brand.

I don't understand the move to v2 for an empty skeleton UAPI. For the purposes of bisection, couldn't you just remove all the v1 UAPI (w/o bumping to v2) and then add a new UAPI in the next patch?

Quoted 7 lines
Quoted 2 lines

The name is change because it is no longer event based but also sampling and commands.

In this case, just to avoid any confusion, the new UAPI could be v2 making the transition clear in case of bisection.

I'm afraid the v1->v2->v1 is a bit more confusing.

Let me elaborate a bit:

Why there is this patch ?
- By removing this code first, the next patch will just contain additions, I thought it would be clearer

Why increase the version here ?
- Code must continue to compile and as the 'thermal_event' family is now different from V1, the version is changed

Quoted 8 lines

Why the version goes to V1 in the next patch ?
- The family name is changed as it is not doing event only, so it is a new netlink thermal protocol and we begin at V1

So the main reason of this patch is to be very strict in the iteration changes. May be it is too much, in this case I can merge this patch with 4/5, the old netlink protocol removal will be lost in the addition of the new protocol. I'm fine with that if you think it is simpler.

Considering that there are no users of v1 currently, it feels a bit over engineered, IMHO.

Also, the new UAPI doesn't need to begin at v1. Just having it start at v2 will avoid this confusion, no?

#10 lore raw

Re: [PATCH v2 3/5] thermal: core: Remove old uapi generic netlink

Quoted 55 lines · Amit Kucheria
Quoted 45 lines · Daniel Lezcano
Quoted 25 lines · Amit Kucheria
Quoted 14 lines · Daniel Lezcano
Quoted 11 lines · Amit Kucheria

include/uapi/linux/thermal.h line 9

Quoted 6 lines · Daniel Lezcano

/* Adding event notification support elements */
#define THERMAL_GENL_FAMILY_NAME "thermal_event"

include/uapi/linux/thermal.h +1 -1
-#define THERMAL_GENL_VERSION                    0x01
+#define THERMAL_GENL_VERSION                    0x02

This hunk should be removed since you set version back to 1 in the next patch and we don't actually intend to bump the version yet.

Quoted 5 lines

Well, I've been very strict here for git-bisecting.

I move to V2 because of the removal, but when adding the new genetlink code, the family name changed, so we returned back to the V1 as it is a new genetlink thermal brand.

I don't understand the move to v2 for an empty skeleton UAPI. For the purposes of bisection, couldn't you just remove all the v1 UAPI (w/o bumping to v2) and then add a new UAPI in the next patch?

Quoted 7 lines
Quoted 2 lines

The name is change because it is no longer event based but also sampling and commands.

In this case, just to avoid any confusion, the new UAPI could be v2 making the transition clear in case of bisection.

I'm afraid the v1->v2->v1 is a bit more confusing.

Let me elaborate a bit:

Why there is this patch ?
- By removing this code first, the next patch will just contain additions, I thought it would be clearer

Why increase the version here ?
- Code must continue to compile and as the 'thermal_event' family is now different from V1, the version is changed

Quoted 8 lines

Why the version goes to V1 in the next patch ?
- The family name is changed as it is not doing event only, so it is a new netlink thermal protocol and we begin at V1

So the main reason of this patch is to be very strict in the iteration changes. May be it is too much, in this case I can merge this patch with 4/5, the old netlink protocol removal will be lost in the addition of the new protocol. I'm fine with that if you think it is simpler.

Quoted 5 lines

Considering that there are no users of v1 currently, it feels a bit over engineered, IMHO.

Also, the new UAPI doesn't need to begin at v1. Just having it start at v2 will avoid this confusion, no?

Ok, I will merge both patches but I will keep the V1 because the netlink protocol is a new one.

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
#11 lore raw

PATCH v2 4/5 thermal: core: genetlink support for events/cmd/sampling

In reply to Daniel Lezcano

Initially the thermal framework had a very simple notification mechanism to send generic netlink messages to the userspace.

The notification function was never called from anywhere and the corresponding dead code was removed. It was probably a first attempt to introduce the netlink notification.

At LPC2018, the presentation "Linux thermal: User kernel interface", proposed to create the notifications to the userspace via a kfifo.

The advantage of the kfifo is the performance. It is usually used from a 1:1 communication channel where a driver captures data and send them as fast as possible to an userspace process.

The inconvenient is the process uses the notification channel exclusively, thus no other process is allowed to use the channel to get temperature or notifications.

The purpose of this series is to provide a fully netlink featured thermal management.

This patch is defining a generic netlink API to discover the current thermal setup, get events and temperature sampling. As any genetlink protocol, it can evolve and the versionning allows to keep the backward compatibility.

In order to not flood the user with a single channel data, there are two multicast channels, one for the temperature sampling when the thermal zone is updated and another one for the events, so the user can get the events only without the thermal zone temperature sampling. All these events are from the ones presented at the LPC2018.

Also, a list of commands to discover the thermal setup is given and can be extended on purpose.

---
 drivers/thermal/Makefile          |   2 +-
 drivers/thermal/thermal_core.h    |  19 +
 drivers/thermal/thermal_netlink.c | 645 ++++++++++++++++++++++++++++++
 include/linux/thermal.h           |  12 -
 include/uapi/linux/thermal.h      |  80 +++-
 5 files changed, 738 insertions(+), 20 deletions(-)
 create mode 100644 drivers/thermal/thermal_netlink.c
drivers/thermal/Makefile +1 -1
index 0c8b84a09b9a..1bbf0805fb04 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_THERMAL)		+= thermal_sys.o
 thermal_sys-y			+= thermal_core.o thermal_sysfs.o \
-					thermal_helpers.o
+					thermal_helpers.o thermal_netlink.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
drivers/thermal/thermal_core.h +19 -0
index 7e8f45db6bbf..08eb03fe4f69 100644
@@ -52,6 +52,25 @@ int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
 
 struct thermal_zone_device *thermal_zone_get_by_id(int id);
 
+/* Netlink notification function */
+int thermal_notify_tz_create(int tz_id, const char *name);
+int thermal_notify_tz_delete(int tz_id);
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+			       int temp, int hyst);
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+				  int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+			    int min_state, int max_state);
+int thermal_notify_cdev_delete(int cdev_id);
+int thermal_notify_tz_gov_change(int tz_id, const char *name);
+int thermal_genl_sampling_temp(int id, int temp);
+
 struct thermal_attr {
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
drivers/thermal/thermal_netlink.c new file +645 -0

645 lines not on this page · raw message

include/linux/thermal.h +0 -12
index fc93a6348082..d92643e310e2 100644
@@ -37,18 +37,6 @@ struct thermal_cooling_device;
 struct thermal_instance;
 struct thermal_attr;
 
-enum thermal_device_mode {
-	THERMAL_DEVICE_DISABLED = 0,
-	THERMAL_DEVICE_ENABLED,
-};
-
-enum thermal_trip_type {
-	THERMAL_TRIP_ACTIVE = 0,
-	THERMAL_TRIP_PASSIVE,
-	THERMAL_TRIP_HOT,
-	THERMAL_TRIP_CRITICAL,
-};
-
 enum thermal_trend {
 	THERMAL_TREND_STABLE, /* temperature is stable */
 	THERMAL_TREND_RAISING, /* temperature is raising */
include/uapi/linux/thermal.h +73 -7

94 lines not on this page · raw message

Signature · 1 line
2.17.1
#12 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Quoted 8 lines · Daniel Lezcano

Initially the thermal framework had a very simple notification mechanism to send generic netlink messages to the userspace.

The notification function was never called from anywhere and the corresponding dead code was removed. It was probably a first attempt to introduce the netlink notification.

Quoted 5 lines

At LPC2018, the presentation "Linux thermal: User kernel interface", proposed to create the notifications to the userspace via a kfifo.

The advantage of the kfifo is the performance. It is usually used from a 1:1 communication channel where a driver captures data and send them

s/send them/sends it/

Quoted 1 line

as fast as possible to an userspace process.

s/an/a/

Quoted 3 lines

The inconvenient is the process uses the notification channel exclusively, thus no other process is allowed to use the channel to

Suggest rephrasing to "The drawback is that only one process uses the notification channel exclusively,"

Quoted 4 lines

get temperature or notifications.

The purpose of this series is to provide a fully netlink featured thermal management.

Suggest getting rid of this sentence, probably more appropriate for the cover letter and instead

Quoted 2 lines

This patch is defining a generic netlink API to discover the current

s/defining/defines/

s/generic/fully-featured generic/

Quoted 1 line

thermal setup, get events and temperature sampling. As any genetlink

s/setup, get events/setup, and adds event notifications/

Quoted 1 line

protocol, it can evolve and the versionning allows to keep the backward

Spell check: versioning

Quoted 3 lines

compatibility.

In order to not flood the user with a single channel data, there are

Consider rephrasing to "In order to prevent the user from getting flooded with data on a single channel"

Quoted 4 lines

two multicast channels, one for the temperature sampling when the thermal zone is updated and another one for the events, so the user can get the events only without the thermal zone temperature sampling. All these events are from the ones presented at the LPC2018.

We should remove this reference to LPC2018 here and just list the events here, or remove this sentence and leave it for the code.

Quoted 2 lines

Also, a list of commands to discover the thermal setup is given and

s/given/added/

Quoted 1 line

can be extended on purpose.

s/on purpose/when needed/

Quoted 102 lines
---
 drivers/thermal/Makefile          |   2 +-
 drivers/thermal/thermal_core.h    |  19 +
 drivers/thermal/thermal_netlink.c | 645 ++++++++++++++++++++++++++++++
 include/linux/thermal.h           |  12 -
 include/uapi/linux/thermal.h      |  80 +++-
 5 files changed, 738 insertions(+), 20 deletions(-)
 create mode 100644 drivers/thermal/thermal_netlink.c
drivers/thermal/Makefile +1 -1
index 0c8b84a09b9a..1bbf0805fb04 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_THERMAL)          += thermal_sys.o
 thermal_sys-y                  += thermal_core.o thermal_sysfs.o \
-                                       thermal_helpers.o
+                                       thermal_helpers.o thermal_netlink.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)            += thermal_hwmon.o
drivers/thermal/thermal_core.h +19 -0
index 7e8f45db6bbf..08eb03fe4f69 100644
@@ -52,6 +52,25 @@ int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
 
 struct thermal_zone_device *thermal_zone_get_by_id(int id);
 
+/* Netlink notification function */
+int thermal_notify_tz_create(int tz_id, const char *name);
+int thermal_notify_tz_delete(int tz_id);
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+                              int temp, int hyst);
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+                                 int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+                           int min_state, int max_state);
+int thermal_notify_cdev_delete(int cdev_id);
+int thermal_notify_tz_gov_change(int tz_id, const char *name);
+int thermal_genl_sampling_temp(int id, int temp);
+
 struct thermal_attr {
        struct device_attribute attr;
        char name[THERMAL_NAME_LENGTH];
drivers/thermal/thermal_netlink.c new file +42 -0
new file mode 100644
index 000000000000..a8d6a815a21d
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Linaro Limited
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ *
+ * Generic netlink for thermal management framework
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <net/genetlink.h>
+#include <uapi/linux/thermal.h>
+
+#include "thermal_core.h"
+
+static const struct genl_multicast_group thermal_genl_mcgrps[] = {
+       { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
+       { .name = THERMAL_GENL_EVENT_GROUP_NAME,  },
+};
+
+static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
+       /* Thermal zone */
+       [THERMAL_GENL_ATTR_TZ]                  = { .type = NLA_NESTED },
+       [THERMAL_GENL_ATTR_TZ_ID]               = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_TEMP]             = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_TRIP]             = { .type = NLA_NESTED },
+       [THERMAL_GENL_ATTR_TZ_TRIP_ID]          = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_TRIP_TEMP]        = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_TRIP_TYPE]        = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_TRIP_HYST]        = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_MODE]             = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT]      = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_TZ_NAME]             = { .type = NLA_STRING,
+                                                   .len = THERMAL_NAME_LENGTH },
+       /* Governor(s) */
+       [THERMAL_GENL_ATTR_TZ_GOV]              = { .type = NLA_NESTED },
+       [THERMAL_GENL_ATTR_TZ_GOV_NAME]         = { .type = NLA_STRING,
+                                                   .len = THERMAL_NAME_LENGTH },
+       /* Cooling devices */
+       [THERMAL_GENL_ATTR_CDEV]                = { .type = NLA_NESTED },
+       [THERMAL_GENL_ATTR_CDEV_ID]             = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_CDEV_CUR_STATE]      = { .type = NLA_U32 },

drivers/thermal/thermal_netlink.c line 52

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +10 -0
+       [THERMAL_GENL_ATTR_CDEV_MAX_STATE]      = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_CDEV_MIN_STATE]      = { .type = NLA_U32 },
+       [THERMAL_GENL_ATTR_CDEV_NAME]           = { .type = NLA_STRING,
+                                                   .len = THERMAL_NAME_LENGTH },
+};
+
+struct param {
+       struct nlattr ** attrs;
+       struct sk_buff *msg;
+       const char *name;

You're reusing this variable to pass the name of the thermal zone, cooling device and governor name. I guess its OK, but for the other variables below, it makes sense to prefix with some string to make the code easier to read.

drivers/thermal/thermal_netlink.c line 55

Quoted 3 lines
drivers/thermal/thermal_netlink.c new file +3 -0
+       int tz_id;
+       int cdev_id;
+       int id;

Suggest renaming this to trip_id to make it easier to read in the code

drivers/thermal/thermal_netlink.c line 58

Quoted 3 lines
drivers/thermal/thermal_netlink.c new file +3 -0
+       int temp;
+       int type;
+       int hyst;

Suggest prefixing these three with trip since these are properties of trip points

drivers/thermal/thermal_netlink.c line 61

Quoted 3 lines
drivers/thermal/thermal_netlink.c new file +3 -0
+       int state;
+       int min_state;
+       int max_state;

Suggest prefixing these three with cdev since these are properties of cooling devices

Quoted 162 lines
drivers/thermal/thermal_netlink.c new file +162 -0
+};
+
+typedef int (*cb_t)(struct param *);
+
+static struct genl_family thermal_gnl_family;
+
+/************************** Sampling encoding *******************************/
+
+int thermal_genl_sampling_temp(int id, int temp)
+{
+       struct sk_buff *skb;
+       void *hdr;
+
+       skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+       if (!skb)
+               return -ENOMEM;
+
+       hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0,
+                         THERMAL_GENL_SAMPLING_TEMP);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
+               goto out_cancel;
+
+       if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
+               goto out_cancel;
+
+       genlmsg_end(skb, hdr);
+
+       genlmsg_multicast(&thermal_gnl_family, skb, 0, 0, GFP_KERNEL);
+
+       return 0;
+out_cancel:
+       genlmsg_cancel(skb, hdr);
+       nlmsg_free(skb);
+
+       return -EMSGSIZE;
+}
+
+/**************************** Event encoding *********************************/
+
+static int thermal_genl_event_tz_create(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
+           nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_tz(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_tz_trip_up(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->id))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_tz_trip_add(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->type) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->temp) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->hyst))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_tz_trip_delete(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->id))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_cdev_add(struct param *p)
+{
+       if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
+                          p->name) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
+                       p->cdev_id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MIN_STATE,
+                       p->min_state) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
+                       p->max_state))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_cdev_delete(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_cdev_update(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id) ||
+           nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE, p->state))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_event_gov_change(struct param *p)
+{
+       if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
+           nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+int thermal_genl_event_tz_delete(struct param *p)
+       __attribute__((alias("thermal_genl_event_tz")));
+
+int thermal_genl_event_tz_enable(struct param *p)
+       __attribute__((alias("thermal_genl_event_tz")));
+
+int thermal_genl_event_tz_disable(struct param *p)
+       __attribute__((alias("thermal_genl_event_tz")));
+
+int thermal_genl_event_tz_trip_down(struct param *p)
+       __attribute__((alias("thermal_genl_event_tz_trip_up")));
+
+int thermal_genl_event_tz_trip_change(struct param *)
+       __attribute__((alias("thermal_genl_event_tz_trip_add")));
+
+static cb_t event_cb[] = {
+       [THERMAL_GENL_EVENT_TZ_CREATE]          = thermal_genl_event_tz_create,
+       [THERMAL_GENL_EVENT_TZ_DELETE]          = thermal_genl_event_tz_delete,
+       [THERMAL_GENL_EVENT_TZ_ENABLE]          = thermal_genl_event_tz_enable,
+       [THERMAL_GENL_EVENT_TZ_DISABLE]         = thermal_genl_event_tz_disable,
+       [THERMAL_GENL_EVENT_TZ_TRIP_UP]         = thermal_genl_event_tz_trip_up,
+       [THERMAL_GENL_EVENT_TZ_TRIP_DOWN]       = thermal_genl_event_tz_trip_down,
+       [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE]     = thermal_genl_event_tz_trip_change,
+       [THERMAL_GENL_EVENT_TZ_TRIP_ADD]        = thermal_genl_event_tz_trip_add,
+       [THERMAL_GENL_EVENT_TZ_TRIP_DELETE]     = thermal_genl_event_tz_trip_delete,
+       [THERMAL_GENL_EVENT_CDEV_ADD]           = thermal_genl_event_cdev_add,
+       [THERMAL_GENL_EVENT_CDEV_DELETE]        = thermal_genl_event_cdev_delete,
+       [THERMAL_GENL_EVENT_CDEV_UPDATE]        = thermal_genl_event_cdev_update,
+       [THERMAL_GENL_EVENT_TZ_GOV_CHANGE]      = thermal_genl_event_gov_change,
+};
+
+/*

drivers/thermal/thermal_netlink.c line 233

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +10 -0
+ * Generic netlink event encoding
+ */
+static int thermal_genl_send_event(enum thermal_genl_event event,
+                                  struct param *p)
+{
+       struct sk_buff *msg;
+       int ret = -EMSGSIZE;
+       void *hdr;
+
+        msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);

Leading Whitespace error

Quoted 48 lines
drivers/thermal/thermal_netlink.c new file +48 -0
+       if (!msg)
+               return -ENOMEM;
+       p->msg = msg;
+
+       hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
+       if (!hdr)
+               goto out_free_msg;
+
+       ret = event_cb[event](p);
+       if (ret)
+               goto out_cancel_msg;
+
+       genlmsg_end(msg, hdr);
+
+       genlmsg_multicast(&thermal_gnl_family, msg, 0, 1, GFP_KERNEL);
+
+       return 0;
+
+out_cancel_msg:
+       genlmsg_cancel(msg, hdr);
+out_free_msg:
+       nlmsg_free(msg);
+
+       return ret;
+}
+
+int thermal_notify_tz_create(int tz_id, const char *name)
+{
+       struct param p = { .tz_id = tz_id, .name = name };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
+}
+
+int thermal_notify_tz_delete(int tz_id)
+{
+       struct param p = { .tz_id = tz_id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
+}
+
+int thermal_notify_tz_enable(int tz_id)
+{
+       struct param p = { .tz_id = tz_id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
+}
+
+int thermal_notify_tz_disable(int tz_id)

drivers/thermal/thermal_netlink.c line 291

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +10 -0
+{
+       struct param p = { .tz_id = tz_id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
+}
+
+int thermal_notify_tz_trip_down(int tz_id, int id)
+{
+       struct param p = { .tz_id = tz_id, .id = id };
+

it would be nice for id to be trip_id for readability here and further below.

Quoted 275 lines
drivers/thermal/thermal_netlink.c new file +275 -0
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
+}
+
+int thermal_notify_tz_trip_up(int tz_id, int id)
+{
+       struct param p = { .tz_id = tz_id, .id = id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
+}
+
+int thermal_notify_tz_trip_add(int tz_id, int id, int type, int temp, int hyst)
+{
+       struct param p = { .tz_id = tz_id, .id = id, .type = type,
+                          .temp = temp, .hyst = hyst };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_ADD, &p);
+}
+
+int thermal_notify_tz_trip_delete(int tz_id, int id)
+{
+       struct param p = { .tz_id = tz_id, .id = id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DELETE, &p);
+}
+
+int thermal_notify_tz_trip_change(int tz_id, int id, int type, int temp, int hyst)
+{
+       struct param p = { .tz_id = tz_id, .id = id, .type = type,
+                          .temp = temp, .hyst = hyst };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
+}
+
+int thermal_notify_cdev_update(int cdev_id, int state)
+{
+       struct param p = { .cdev_id = cdev_id, .state = state };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_UPDATE, &p);
+}
+
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+                           int min_state, int max_state)
+{
+       struct param p = { .cdev_id = cdev_id, .name = name,
+                          .min_state = min_state, .max_state = max_state };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
+}
+
+int thermal_notify_cdev_delete(int cdev_id)
+{
+       struct param p = { .cdev_id = cdev_id };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
+}
+
+int thermal_notify_tz_gov_change(int tz_id, const char *name)
+{
+       struct param p = { .tz_id = tz_id, .name = name };
+
+       return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
+}
+
+/*************************** Command encoding ********************************/
+
+static int __thermal_genl_cmd_tz_get(struct thermal_zone_device *tz, void *data)
+{
+       struct sk_buff *msg = data;
+
+       if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
+           nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_cmd_tz_get(struct param *p)
+{
+       struct sk_buff *msg = p->msg;
+       struct nlattr *start_tz;
+       int ret;
+
+       start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
+       if (!start_tz)
+               return -EMSGSIZE;
+
+       ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get, msg);
+       if (ret)
+               goto out_cancel_nest;
+
+       nla_nest_end(msg, start_tz);
+
+       return 0;
+
+out_cancel_nest:
+       nla_nest_cancel(msg, start_tz);
+
+       return ret;
+}
+
+static int thermal_genl_cmd_tz_get_trip(struct param *p)
+{
+       struct sk_buff *msg = p->msg;
+       struct thermal_zone_device *tz;
+       struct nlattr *start_trip;
+       int i, id;
+
+       if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
+               return -EINVAL;
+
+       id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
+
+       tz = thermal_zone_get_by_id(id);
+       if (!tz)
+               return -EINVAL;
+
+       start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
+       if (!start_trip)
+               return -EMSGSIZE;
+
+       mutex_lock(&tz->lock);
+
+       for (i = 0; i < tz->trips; i++) {
+
+               enum thermal_trip_type type;
+               int temp, hyst;
+
+               tz->ops->get_trip_type(tz, i, &type);
+               tz->ops->get_trip_temp(tz, i, &temp);
+               tz->ops->get_trip_hyst(tz, i, &hyst);
+
+               if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) ||
+                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, type) ||
+                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, temp) ||
+                   nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, hyst))
+                       goto out_cancel_nest;
+       }
+
+       mutex_unlock(&tz->lock);
+
+       nla_nest_end(msg, start_trip);
+
+       return 0;
+
+out_cancel_nest:
+       mutex_unlock(&tz->lock);
+
+       return -EMSGSIZE;
+}
+
+static int thermal_genl_cmd_tz_get_temp(struct param *p)
+{
+       struct sk_buff *msg = p->msg;
+       struct thermal_zone_device *tz;
+       int temp, ret, id;
+
+       if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
+               return -EINVAL;
+
+       id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
+
+       tz = thermal_zone_get_by_id(id);
+       if (!tz)
+               return -EINVAL;
+
+       ret = thermal_zone_get_temp(tz, &temp);
+       if (ret)
+               return ret;
+
+       if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
+           nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_cmd_tz_get_gov(struct param *p)
+{
+       struct sk_buff *msg = p->msg;
+       struct thermal_zone_device *tz;
+       int id, ret = 0;
+
+       if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
+               return -EINVAL;
+
+       id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
+
+       tz = thermal_zone_get_by_id(id);
+       if (!tz)
+               return -EINVAL;
+
+       mutex_lock(&tz->lock);
+
+       if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
+           nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
+                          tz->governor->name))
+               ret = -EMSGSIZE;
+
+       mutex_unlock(&tz->lock);
+
+       return ret;
+}
+
+static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
+                                      void *data)
+{
+       struct sk_buff *msg = data;
+
+       if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
+               return -EMSGSIZE;
+
+       if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+static int thermal_genl_cmd_cdev_get(struct param *p)
+{
+       struct sk_buff *msg = p->msg;
+       struct nlattr *start_cdev;
+       int ret;
+
+       start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
+       if (!start_cdev)
+               return -EMSGSIZE;
+
+       ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
+       if (ret)
+               goto out_cancel_nest;
+
+       nla_nest_end(msg, start_cdev);
+
+       return 0;
+out_cancel_nest:
+       nla_nest_cancel(msg, start_cdev);
+
+       return ret;
+}
+
+static cb_t cmd_cb[] = {
+       [THERMAL_GENL_CMD_TZ_GET]       = thermal_genl_cmd_tz_get,
+       [THERMAL_GENL_CMD_TZ_GET_TRIP]  = thermal_genl_cmd_tz_get_trip,
+       [THERMAL_GENL_CMD_TZ_GET_TEMP]  = thermal_genl_cmd_tz_get_temp,
+       [THERMAL_GENL_CMD_TZ_GET_GOV]   = thermal_genl_cmd_tz_get_gov,
+       [THERMAL_GENL_CMD_CDEV_GET]     = thermal_genl_cmd_cdev_get,
+};
+
+static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
+                                  struct netlink_callback *cb)
+{
+       struct param p = { .msg = skb };
+       const struct genl_dumpit_info *info = genl_dumpit_info(cb);
+       int cmd = info->ops->cmd;
+       int ret = -EMSGSIZE;
+       void *hdr;
+
+       hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       ret = cmd_cb[cmd](&p);
+       if (ret)
+               goto out_cancel_msg;
+
+       genlmsg_end(skb, hdr);
+
+       return 0;
+
+out_cancel_msg:
+       genlmsg_cancel(skb, hdr);
+
+       return ret;
+}
+

drivers/thermal/thermal_netlink.c line 576

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +10 -0
+static int thermal_genl_cmd_doit(struct sk_buff *skb,
+                                struct genl_info *info)
+{
+       struct param p = { .attrs = info->attrs };
+       struct sk_buff *msg;
+       void *hdr;
+       int cmd = info->genlhdr->cmd;
+       int ret = -EMSGSIZE;
+
+        msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);

whitespace error

drivers/thermal/thermal_netlink.c line 581

Quoted 5 lines
drivers/thermal/thermal_netlink.c new file +5 -0
+       if (!msg)
+               return -ENOMEM;
+       p.msg = msg;
+
+        hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);

whitespace error

Quoted 74 lines
drivers/thermal/thermal_netlink.c new file +64 -0
+       if (!hdr)
+               goto out_free_msg;
+
+       ret = cmd_cb[cmd](&p);
+       if (ret)
+               goto out_cancel_msg;
+
+       genlmsg_end(msg, hdr);
+
+       return genlmsg_reply(msg, info);
+
+out_cancel_msg:
+       genlmsg_cancel(msg, hdr);
+out_free_msg:
+       nlmsg_free(msg);
+
+       return ret;
+}
+
+static const struct genl_ops thermal_genl_ops[] = {
+       {
+               .cmd = THERMAL_GENL_CMD_TZ_GET,
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+               .dumpit = thermal_genl_cmd_dumpit,
+       },
+       {
+               .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+               .doit = thermal_genl_cmd_doit,
+       },
+       {
+               .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+               .doit = thermal_genl_cmd_doit,
+       },
+       {
+               .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+               .doit = thermal_genl_cmd_doit,
+       },
+       {
+               .cmd = THERMAL_GENL_CMD_CDEV_GET,
+               .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
+               .dumpit = thermal_genl_cmd_dumpit,
+       },
+};
+
+static struct genl_family thermal_gnl_family __ro_after_init = {
+       .hdrsize        = 0,
+       .name           = THERMAL_GENL_FAMILY_NAME,
+       .version        = THERMAL_GENL_VERSION,
+       .maxattr        = THERMAL_GENL_ATTR_MAX,
+       .policy         = thermal_genl_policy,
+       .ops            = thermal_genl_ops,
+       .n_ops          = ARRAY_SIZE(thermal_genl_ops),
+       .mcgrps         = thermal_genl_mcgrps,
+       .n_mcgrps       = ARRAY_SIZE(thermal_genl_mcgrps),
+};
+
+static int __init thermal_netlink_init(void)
+{
+       return genl_register_family(&thermal_gnl_family);
+}
+core_initcall(thermal_netlink_init);
include/linux/thermal.h +0 -2
index fc93a6348082..d92643e310e2 100644
@@ -37,18 +37,6 @@ struct thermal_cooling_device;
 struct thermal_instance;
 struct thermal_attr;
 
-enum thermal_device_mode {
-       THERMAL_DEVICE_DISABLED = 0,

include/linux/thermal.h struct thermal_cooling_device; · line 51

Quoted 10 lines
include/linux/thermal.h +0 -10
-       THERMAL_DEVICE_ENABLED,
-};
-
-enum thermal_trip_type {
-       THERMAL_TRIP_ACTIVE = 0,
-       THERMAL_TRIP_PASSIVE,
-       THERMAL_TRIP_HOT,
-       THERMAL_TRIP_CRITICAL,
-};
-

This move should happen in a separate patch, IMO.

Quoted 19 lines
include/linux/thermal.h +0 -0
 enum thermal_trend {
        THERMAL_TREND_STABLE, /* temperature is stable */
        THERMAL_TREND_RAISING, /* temperature is raising */
include/uapi/linux/thermal.h +8 -0
index 22df67ed9e9c..49289f9d993b 100644
@@ -4,21 +4,87 @@
 
 #define THERMAL_NAME_LENGTH    20
 
+enum thermal_device_mode {
+       THERMAL_DEVICE_DISABLED = 0,
+       THERMAL_DEVICE_ENABLED,
+};
+
+enum thermal_trip_type {
+       THERMAL_TRIP_ACTIVE = 0,
+       THERMAL_TRIP_PASSIVE,

include/uapi/linux/thermal.h line 21

Quoted 10 lines
include/uapi/linux/thermal.h +6 -3
+       THERMAL_TRIP_HOT,
+       THERMAL_TRIP_CRITICAL,
+};
+
 /* Adding event notification support elements */
-#define THERMAL_GENL_FAMILY_NAME                "thermal_event"
-#define THERMAL_GENL_VERSION                    0x02
-#define THERMAL_GENL_MCAST_GROUP_NAME           "thermal_mc_grp"
+#define THERMAL_GENL_FAMILY_NAME               "thermal"
+#define THERMAL_GENL_VERSION                   0x01

You set it to 2 in the previous patch and now reset it to 1 here.

I think we are sticking to version 1, so the hunk can just be removed from the previous patch.

Quoted 55 lines
include/uapi/linux/thermal.h +45 -4
+#define THERMAL_GENL_SAMPLING_GROUP_NAME       "sampling"
+#define THERMAL_GENL_EVENT_GROUP_NAME          "event"
 
-/* attributes of thermal_genl_family */
-enum {
+/* Attributes of thermal_genl_family */
+enum thermal_genl_attr {
        THERMAL_GENL_ATTR_UNSPEC,
+       THERMAL_GENL_ATTR_TZ,
+       THERMAL_GENL_ATTR_TZ_ID,
+       THERMAL_GENL_ATTR_TZ_TEMP,
+       THERMAL_GENL_ATTR_TZ_TRIP,
+       THERMAL_GENL_ATTR_TZ_TRIP_ID,
+       THERMAL_GENL_ATTR_TZ_TRIP_TYPE,
+       THERMAL_GENL_ATTR_TZ_TRIP_TEMP,
+       THERMAL_GENL_ATTR_TZ_TRIP_HYST,
+       THERMAL_GENL_ATTR_TZ_MODE,
+       THERMAL_GENL_ATTR_TZ_NAME,
+       THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT,
+       THERMAL_GENL_ATTR_TZ_GOV,
+       THERMAL_GENL_ATTR_TZ_GOV_NAME,
+       THERMAL_GENL_ATTR_CDEV,
+       THERMAL_GENL_ATTR_CDEV_ID,
+       THERMAL_GENL_ATTR_CDEV_CUR_STATE,
+       THERMAL_GENL_ATTR_CDEV_MAX_STATE,
+       THERMAL_GENL_ATTR_CDEV_MIN_STATE,
+       THERMAL_GENL_ATTR_CDEV_NAME,
+       THERMAL_GENL_ATTR_GOV_NAME,
+
        __THERMAL_GENL_ATTR_MAX,
 };
 #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
 
-/* commands supported by the thermal_genl_family */
-enum {
+enum thermal_genl_sampling {
+       THERMAL_GENL_SAMPLING_TEMP,
+       __THERMAL_GENL_SAMPLING_MAX,
+};
+#define THERMAL_GENL_SAMPLING_MAX (__THERMAL_GENL_SAMPLING_MAX - 1)
+
+/* Events of thermal_genl_family */
+enum thermal_genl_event {
+       THERMAL_GENL_EVENT_UNSPEC,
+       THERMAL_GENL_EVENT_TZ_CREATE,           /* Thermal zone creation */
+       THERMAL_GENL_EVENT_TZ_DELETE,           /* Thermal zone deletion */
+       THERMAL_GENL_EVENT_TZ_DISABLE,          /* Thermal zone disabed */
+       THERMAL_GENL_EVENT_TZ_ENABLE,           /* Thermal zone enabled */
+       THERMAL_GENL_EVENT_TZ_TRIP_UP,          /* Trip point crossed the way up */
+       THERMAL_GENL_EVENT_TZ_TRIP_DOWN,        /* Trip point crossed the way down */
+       THERMAL_GENL_EVENT_TZ_TRIP_CHANGE,      /* Trip point changed */
+       THERMAL_GENL_EVENT_TZ_TRIP_ADD,         /* Trip point added */
+       THERMAL_GENL_EVENT_TZ_TRIP_DELETE,      /* Trip point deleted */
+       THERMAL_GENL_EVENT_CDEV_ADD,            /* Cdev bound to the thermal zone */
+       THERMAL_GENL_EVENT_CDEV_DELETE,         /* Cdev unbound */

include/uapi/linux/thermal.h line 82

Quoted 10 lines
include/uapi/linux/thermal.h +9 -0
+       THERMAL_GENL_EVENT_CDEV_UPDATE,         /* Cdev state updated */
+       THERMAL_GENL_EVENT_TZ_GOV_CHANGE,       /* Governor policy changed  */
+       __THERMAL_GENL_EVENT_MAX,
+};
+#define THERMAL_GENL_EVENT_MAX (__THERMAL_GENL_EVENT_MAX - 1)
+
+/* Commands supported by the thermal_genl_family */
+enum thermal_genl_cmd {
        THERMAL_GENL_CMD_UNSPEC,
+       THERMAL_GENL_CMD_TZ_GET,        /* List thermal zones id */

Rename to THERMAL_GENL_CMD_TZ_GET_IDS to match intent ?

Also, s/List/List of/

Quoted 11 lines
include/uapi/linux/thermal.h +5 -1
+       THERMAL_GENL_CMD_TZ_GET_TRIP,   /* List of thermal trips */
+       THERMAL_GENL_CMD_TZ_GET_TEMP,   /* Get the thermal zone temperature */
+       THERMAL_GENL_CMD_TZ_GET_GOV,    /* Get the thermal zone governor */
+       THERMAL_GENL_CMD_TZ_GET_MODE,   /* Get the thermal zone mode */
+       THERMAL_GENL_CMD_CDEV_GET,      /* List of cdev id */
        __THERMAL_GENL_CMD_MAX,
 };
 #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
--

2.17.1

reviewer
#13 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

In reply to Daniel Lezcano
Quoted 71 lines · Daniel Lezcano

Initially the thermal framework had a very simple notification mechanism to send generic netlink messages to the userspace.

The notification function was never called from anywhere and the corresponding dead code was removed. It was probably a first attempt to introduce the netlink notification.

At LPC2018, the presentation "Linux thermal: User kernel interface", proposed to create the notifications to the userspace via a kfifo.

The advantage of the kfifo is the performance. It is usually used from
a 1:1 communication channel where a driver captures data and send them
as fast as possible to an userspace process.

The inconvenient is the process uses the notification channel exclusively, thus no other process is allowed to use the channel to get temperature or notifications.

The purpose of this series is to provide a fully netlink featured thermal management.

This patch is defining a generic netlink API to discover the current thermal setup, get events and temperature sampling. As any genetlink protocol, it can evolve and the versionning allows to keep the backward
compatibility.

In order to not flood the user with a single channel data, there are two multicast channels, one for the temperature sampling when the thermal zone is updated and another one for the events, so the user can get the events only without the thermal zone temperature sampling. All these events are from the ones presented at the LPC2018.

Also, a list of commands to discover the thermal setup is given and can be extended on purpose.

---
 drivers/thermal/Makefile          |   2 +-
 drivers/thermal/thermal_core.h    |  19 +
 drivers/thermal/thermal_netlink.c | 645
++++++++++++++++++++++++++++++
 include/linux/thermal.h           |  12 -
 include/uapi/linux/thermal.h      |  80 +++-
 5 files changed, 738 insertions(+), 20 deletions(-)
 create mode 100644 drivers/thermal/thermal_netlink.c
drivers/thermal/Makefile +1 -1
index 0c8b84a09b9a..1bbf0805fb04 100644
@@ -5,7 +5,7 @@
 
 obj-$(CONFIG_THERMAL)		+= thermal_sys.o
 thermal_sys-y			+= thermal_core.o
 thermal_sysfs.o \
-					thermal_helpers.o
+					thermal_helpers.o

thermal_netlink.o

 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
drivers/thermal/thermal_core.h +0 -0
index 7e8f45db6bbf..08eb03fe4f69 100644

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct · line 61

Quoted 10 lines
drivers/thermal/thermal_core.h +5 -0
@@ -52,6 +52,25 @@ int for_each_thermal_governor(int (*cb)(struct
 thermal_governor *, void *),
 
 struct thermal_zone_device *thermal_zone_get_by_id(int id);
 
+/* Netlink notification function */
+int thermal_notify_tz_create(int tz_id, const char *name);
+int thermal_notify_tz_delete(int tz_id);
 
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);

these two will be used after merging the mode enhancement patches from Andrzej Pietrasiewicz, right?

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 64

Quoted 6 lines
drivers/thermal/thermal_core.h +5 -0
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
 
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+			       int temp, int hyst);

is there any case we need to use these two?

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 67

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+				  int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);

This is used when the cdev cur_state is changed.
what about cases that cdev->max_state changes? I think we need an extra event for it, right?

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 70

Quoted 4 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+			    int min_state, int max_state);
+int thermal_notify_cdev_delete(int cdev_id);

These two should be used in patch 5/5.

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 73

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_gov_change(int tz_id, const char *name);
+int thermal_genl_sampling_temp(int id, int temp);
+

struct thermal_attr {

Quoted 68 lines
drivers/thermal/thermal_core.h +0 -0
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
drivers/thermal/thermal_netlink.c new file +21 -0
new file mode 100644
index 000000000000..a8d6a815a21d
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Linaro Limited
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ *
+ * Generic netlink for thermal management framework
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <net/genetlink.h>
+#include <uapi/linux/thermal.h>
+
+#include "thermal_core.h"
+
+static const struct genl_multicast_group thermal_genl_mcgrps[] = {
+	{ .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
+	{ .name = THERMAL_GENL_EVENT_GROUP_NAME,  },
+};
+
+static const struct nla_policy

thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Thermal zone */
+	[THERMAL_GENL_ATTR_TZ]			= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +2 -0
+	[THERMAL_GENL_ATTR_TZ_ID]		= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TEMP]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_ID]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +4 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_TEMP]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_TYPE]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_HYST]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_MODE]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Governor(s) */
+	[THERMAL_GENL_ATTR_TZ_GOV]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_GOV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +3 -0
+	/* Cooling devices */
+	[THERMAL_GENL_ATTR_CDEV]		= { .type = NLA_NESTED },
+	[THERMAL_GENL_ATTR_CDEV_ID]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_CUR_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MAX_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MIN_STATE]	= { .type = NLA_U32
Quoted 1 line

},

is there any case that min_state does not equal 0?

Quoted 6 lines
drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+};
+

[...]

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +4 -0
+
+static cb_t cmd_cb[] = {
+	[THERMAL_GENL_CMD_TZ_GET]	= thermal_genl_cmd_tz_get,
 
+	[THERMAL_GENL_CMD_TZ_GET_TRIP]	=

thermal_genl_cmd_tz_get_trip,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_TEMP]	=

thermal_genl_cmd_tz_get_temp,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_GOV]	=

thermal_genl_cmd_tz_get_gov,

A dumb question, can we share the same command for the above three? Say,
THERMAL_GENL_CMD_GET_ALL_TZ or THERMAL_GENL_CMD_GET_TZS returns the id && name of all the thermal zones.
THERMAL_GENL_CMD_GET_TZ returns general information of a specified thermal zone, include trip points, governor and temperature. My understanding is that all these information will be queried once, in the very beginning, and then userpsace relies on the netlink events to give updated information, right?

This could simplify the kernel code and also userspace a bit.

thanks,
rui

#14 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Hi Zhang,

thanks for taking the time to review

On 30/06/2020 18:12, Zhang Rui wrote:

[ ... ]

Quoted 5 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 59

Quoted 2 lines
drivers/thermal/thermal_core.h +2 -0
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);

these two will be used after merging the mode enhancement patches from Andrzej Pietrasiewicz, right?

Yes, that is correct.

Quoted 8 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 64

Quoted 6 lines
drivers/thermal/thermal_core.h +5 -0
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
 
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+			       int temp, int hyst);

is there any case we need to use these two?

There is the initial proposal to add/del trip points via sysfs which is not merged because of no comments and the presentation from Srinivas giving the 'add' and 'del' notification description, so I assumed the feature would be added soon.

These functions are here ready to be connected to the core code. If anyone is planning to implement add/del trip point, he won't have to implement these two notifications as they will be ready to be called.

Quoted 7 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 67

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+				  int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);

This is used when the cdev cur_state is changed.
what about cases that cdev->max_state changes? I think we need an extra event for it, right?

Sure, I can add:

int thermal_notify_cdev_change(...)

Quoted 5 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 70

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+			    int min_state, int max_state);
+int thermal_notify_cdev_delete(int cdev_id);

These two should be used in patch 5/5.

Sure.

Quoted 68 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 73

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_gov_change(int tz_id, const char *name);
+int thermal_genl_sampling_temp(int id, int temp);
+

struct thermal_attr {

Quoted 63 lines
drivers/thermal/thermal_core.h +0 -0
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
drivers/thermal/thermal_netlink.c new file +21 -0
new file mode 100644
index 000000000000..a8d6a815a21d
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Linaro Limited
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ *
+ * Generic netlink for thermal management framework
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <net/genetlink.h>
+#include <uapi/linux/thermal.h>
+
+#include "thermal_core.h"
+
+static const struct genl_multicast_group thermal_genl_mcgrps[] = {
+	{ .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
+	{ .name = THERMAL_GENL_EVENT_GROUP_NAME,  },
+};
+
+static const struct nla_policy

thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Thermal zone */
+	[THERMAL_GENL_ATTR_TZ]			= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +2 -0
+	[THERMAL_GENL_ATTR_TZ_ID]		= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TEMP]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_ID]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +4 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_TEMP]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_TYPE]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_HYST]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_MODE]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Governor(s) */
+	[THERMAL_GENL_ATTR_TZ_GOV]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_GOV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +3 -0
+	/* Cooling devices */
+	[THERMAL_GENL_ATTR_CDEV]		= { .type = NLA_NESTED },
+	[THERMAL_GENL_ATTR_CDEV_ID]		= { .type = NLA_U32

},

Quoted 8 lines
Quoted 6 lines
drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_CUR_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MAX_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MIN_STATE]	= { .type = NLA_U32

},

is there any case that min_state does not equal 0?

You are right, there is no min state for a cooling device, only for the instances in the thermal zones. I will fix that.

Quoted 29 lines
Quoted 6 lines
drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+};
+

[...]

Quoted 10 lines
drivers/thermal/thermal_netlink.c new file +4 -0
+
+static cb_t cmd_cb[] = {
+	[THERMAL_GENL_CMD_TZ_GET]	= thermal_genl_cmd_tz_get,
 
+	[THERMAL_GENL_CMD_TZ_GET_TRIP]	=

thermal_genl_cmd_tz_get_trip,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_TEMP]	=

thermal_genl_cmd_tz_get_temp,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_GOV]	=

thermal_genl_cmd_tz_get_gov,

A dumb question, can we share the same command for the above three? Say,
THERMAL_GENL_CMD_GET_ALL_TZ or THERMAL_GENL_CMD_GET_TZS returns the id && name of all the thermal zones.
THERMAL_GENL_CMD_GET_TZ returns general information of a specified thermal zone, include trip points, governor and temperature. My understanding is that all these information will be queried once, in the very beginning, and then userpsace relies on the netlink events to give updated information, right?

Quoted 1 line

This could simplify the kernel code and also userspace a bit.

Actually the userspace still need a 'get temp' or 'get trip' commands.

get temp : Get the temperature at any time, like reading the sysfs temperature

get trip : Get the trip point information when a trip event happens, no need to get a full discovery of the thermal zones before.

If I send a bulk message containing all the thermal zones, their trips point, the governors and the cooling devices, that will force the userspace to deal with multiple level of nested arrays. With netlinks that makes the code really more complicated and prone to errors.

With this approach, if the userspace is interested only on "gpu", it can get the thermal zone id when getting all the thermal zones <id, name> and then ask for the information it is interested in.

Well I thought that is be more flexible.

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
reviewer
#15 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Hi, Daniel,

Quoted 32 lines · Daniel Lezcano

Hi Zhang,

thanks for taking the time to review

On 30/06/2020 18:12, Zhang Rui wrote:

[ ... ]

Quoted 6 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 59

Quoted 2 lines
drivers/thermal/thermal_core.h +2 -0
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);

these two will be used after merging the mode enhancement patches from
Andrzej Pietrasiewicz, right?

Yes, that is correct.

Quoted 7 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 64

Quoted 5 lines
drivers/thermal/thermal_core.h +5 -0
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+			       int temp, int hyst);

is there any case we need to use these two?

There is the initial proposal to add/del trip points via sysfs which is
not merged because of no comments and the presentation from Srinivas giving the 'add' and 'del' notification description, so I assumed the feature would be added soon.

Quoted 4 lines

These functions are here ready to be connected to the core code. If anyone is planning to implement add/del trip point, he won't have to implement these two notifications as they will be ready to be called.

Then I'd prefer we only introduce the events that are used or will be used soon, like the tz disable/enable, to avoid some potential dead code.
We can easily add more events when they are needed.

Srinivas, do you have plan to use the trip add/delete events?

Quoted 9 lines
Quoted 8 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 67

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+				  int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);

This is used when the cdev cur_state is changed.
what about cases that cdev->max_state changes? I think we need an extra
event for it, right?

Quoted 3 lines

Sure, I can add:

int thermal_notify_cdev_change(...)

thermal_notify_cdev_change() and thermal_notify_cdev_update() looks confusing to me.
Can we use thermal_notify_cdev_update_cur() and thermal_notify_cdev_update_max()?
Or can we use one event, with updated cur_state and max_state?

Quoted 140 lines
Quoted 5 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 70

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_cdev_add(int cdev_id, const char *name,
+			    int min_state, int max_state);
+int thermal_notify_cdev_delete(int cdev_id);

These two should be used in patch 5/5.

Sure.

Quoted 70 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 73

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_gov_change(int tz_id, const char *name);
+int thermal_genl_sampling_temp(int id, int temp);
+

struct thermal_attr {

Quoted 65 lines
drivers/thermal/thermal_core.h +0 -0
 	struct device_attribute attr;
 	char name[THERMAL_NAME_LENGTH];
drivers/thermal/thermal_netlink.c new file +16 -0
new file mode 100644
index 000000000000..a8d6a815a21d
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Linaro Limited
+ *
+ * Author: Daniel Lezcano <[email protected]>
+ *
+ * Generic netlink for thermal management framework
+ */
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <net/genetlink.h>
+#include <uapi/linux/thermal.h>
+
+#include "thermal_core.h"
+
+static const struct genl_multicast_group thermal_genl_mcgrps[] =

{

drivers/thermal/thermal_netlink.c new file +5 -0
+	{ .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
+	{ .name = THERMAL_GENL_EVENT_GROUP_NAME,  },
+};
+
+static const struct nla_policy

thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Thermal zone */
+	[THERMAL_GENL_ATTR_TZ]			= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +2 -0
+	[THERMAL_GENL_ATTR_TZ_ID]		= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TEMP]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_ID]		= { .type =

NLA_U32
},

drivers/thermal/thermal_netlink.c new file +4 -0
+	[THERMAL_GENL_ATTR_TZ_TRIP_TEMP]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_TYPE]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_TRIP_HYST]	= { .type = NLA_U32 },
+	[THERMAL_GENL_ATTR_TZ_MODE]		= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+	/* Governor(s) */
+	[THERMAL_GENL_ATTR_TZ_GOV]		= { .type =

NLA_NESTED },

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_TZ_GOV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +3 -0
+	/* Cooling devices */
+	[THERMAL_GENL_ATTR_CDEV]		= { .type = NLA_NESTED },
+	[THERMAL_GENL_ATTR_CDEV_ID]		= { .type = NLA_U32

},

Quoted 8 lines
Quoted 6 lines
drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_CUR_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MAX_STATE]	= { .type = NLA_U32

},

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_MIN_STATE]	= { .type = NLA_U32

},

is there any case that min_state does not equal 0?

You are right, there is no min state for a cooling device, only for the
instances in the thermal zones. I will fix that.

Quoted 31 lines
Quoted 6 lines
drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_ATTR_CDEV_NAME]		= { .type =

NLA_STRING,

drivers/thermal/thermal_netlink.c new file +1 -0
+						    .len =

THERMAL_NAME_LENGTH },

drivers/thermal/thermal_netlink.c new file +2 -0
+};
+

[...]

Quoted 9 lines
drivers/thermal/thermal_netlink.c new file +4 -0
+
+static cb_t cmd_cb[] = {
+	[THERMAL_GENL_CMD_TZ_GET]	= thermal_genl_cmd_tz_get,
+	[THERMAL_GENL_CMD_TZ_GET_TRIP]	=

thermal_genl_cmd_tz_get_trip,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_TEMP]	=

thermal_genl_cmd_tz_get_temp,

drivers/thermal/thermal_netlink.c new file +1 -0
+	[THERMAL_GENL_CMD_TZ_GET_GOV]	=

thermal_genl_cmd_tz_get_gov,

A dumb question, can we share the same command for the above three? Say,
THERMAL_GENL_CMD_GET_ALL_TZ or THERMAL_GENL_CMD_GET_TZS returns the id
&& name of all the thermal zones. THERMAL_GENL_CMD_GET_TZ returns general information of a specified thermal zone, include trip points, governor and temperature. My understanding is that all these information will be queried once, in
the very beginning, and then userpsace relies on the netlink events to
give updated information, right?

Quoted 1 line

This could simplify the kernel code and also userspace a bit.

Actually the userspace still need a 'get temp' or 'get trip' commands.

get temp : Get the temperature at any time, like reading the sysfs temperature

get trip : Get the trip point information when a trip event happens, no
need to get a full discovery of the thermal zones before.

If I send a bulk message containing all the thermal zones, their trips
point, the governors and the cooling devices, that will force the userspace to deal with multiple level of nested arrays. With netlinks that makes the code really more complicated and prone to errors.

Quoted 6 lines

With this approach, if the userspace is interested only on "gpu", it can
get the thermal zone id when getting all the thermal zones <id, name> and then ask for the information it is interested in.

Well I thought that is be more flexible.

Then I'm totally okay with this implementation. Thanks for the explanation.

thanks,
rui

Quoted 5 lines
#16 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Quoted 35 lines · Zhang Rui

Hi, Daniel,

Quoted 32 lines · Daniel Lezcano

Hi Zhang,

thanks for taking the time to review

On 30/06/2020 18:12, Zhang Rui wrote:

[ ... ]

Quoted 6 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 59

Quoted 2 lines
drivers/thermal/thermal_core.h +2 -0
+int thermal_notify_tz_enable(int tz_id);
+int thermal_notify_tz_disable(int tz_id);

these two will be used after merging the mode enhancement patches from
Andrzej Pietrasiewicz, right?

Yes, that is correct.

Quoted 7 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 64

Quoted 5 lines
drivers/thermal/thermal_core.h +5 -0
+int thermal_notify_tz_trip_down(int tz_id, int id);
+int thermal_notify_tz_trip_up(int tz_id, int id);
+int thermal_notify_tz_trip_delete(int tz_id, int id);
+int thermal_notify_tz_trip_add(int tz_id, int id, int type,
+			       int temp, int hyst);

is there any case we need to use these two?

There is the initial proposal to add/del trip points via sysfs which is
not merged because of no comments and the presentation from Srinivas giving the 'add' and 'del' notification description, so I assumed the feature would be added soon.

Quoted 8 lines
Quoted 4 lines

These functions are here ready to be connected to the core code. If anyone is planning to implement add/del trip point, he won't have to implement these two notifications as they will be ready to be called.

Then I'd prefer we only introduce the events that are used or will be used soon, like the tz disable/enable, to avoid some potential dead code.
We can easily add more events when they are needed.

Sure, if Srinivas does not need them, I can drop these notifications.

However, I would suggest to keep at the uapi header file with the definition of the events and the attributes in order to reduce the impact of the userspace change when adding these two notifications in the future.

Quoted 13 lines

Srinivas, do you have plan to use the trip add/delete events?

Quoted 11 lines
Quoted 8 lines

drivers/thermal/thermal_core.h int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *), · line 67

Quoted 3 lines
drivers/thermal/thermal_core.h +3 -0
+int thermal_notify_tz_trip_change(int tz_id, int id, int type,
+				  int temp, int hyst);
+int thermal_notify_cdev_update(int cdev_id, int state);

This is used when the cdev cur_state is changed.
what about cases that cdev->max_state changes? I think we need an extra
event for it, right?

Sure, I can add:

Quoted 7 lines
Quoted 1 line

int thermal_notify_cdev_change(...)

thermal_notify_cdev_change() and thermal_notify_cdev_update() looks confusing to me.
Can we use thermal_notify_cdev_update_cur() and thermal_notify_cdev_update_max()?
Or can we use one event, with updated cur_state and max_state?

I think it is a good idea to use the same event and depending on the change we can add the cur_state or the max_state attribute. Up to the userspace to figure out which one is present.

[ ... ]

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
#17 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

In reply to Zhang Rui

[...]

Quoted 6 lines

Then I'd prefer we only introduce the events that are used or will be used soon, like the tz disable/enable, to avoid some potential dead code.
We can easily add more events when they are needed.

Srinivas, do you have plan to use the trip add/delete events?

Yes and also trip modify.
Also I need to have one more event for heartbeat like event which needs confirmation from user space to hardware the user process controlling thermal is active not dead. So whenever hardware wants to check health it will send an event, which user space should acknowledge

Thanks,
Srinivas

Quoted 3 lines
Quoted 3 lines
#18 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Quoted 10 lines · Srinivas Pandruvada

[...]

Quoted 6 lines

Then I'd prefer we only introduce the events that are used or will be used soon, like the tz disable/enable, to avoid some potential dead code.
We can easily add more events when they are needed.

Srinivas, do you have plan to use the trip add/delete events?

Yes and also trip modify.

Ok I will keep those then.

Quoted 4 lines

Also I need to have one more event for heartbeat like event which needs confirmation from user space to hardware the user process controlling thermal is active not dead. So whenever hardware wants to check health it will send an event, which user space should acknowledge

Could it be the opposite? The userspace sends periodically a message to tell it is alive instead of having the kernel asking the userspace?

Is it ok if we add this in a separate series ?

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
#19 lore raw

Re: [PATCH v2 4/5] thermal: core: genetlink support for events/cmd/sampling

Quoted 23 lines · Daniel Lezcano
Quoted 11 lines · Srinivas Pandruvada

[...]

Quoted 8 lines

Then I'd prefer we only introduce the events that are used or will be
used soon, like the tz disable/enable, to avoid some potential dead
code.
We can easily add more events when they are needed.

Srinivas, do you have plan to use the trip add/delete events?

Yes and also trip modify.

Ok I will keep those then.

Quoted 7 lines

Also I need to have one more event for heartbeat like event which needs
confirmation from user space to hardware the user process controlling
thermal is active not dead. So whenever hardware wants to check health
it will send an event, which user space should acknowledge

Quoted 4 lines

Could it be the opposite? The userspace sends periodically a message to
tell it is alive instead of having the kernel asking the userspace?

Kernel doesn't ask (there is no timer in the kernel). The HW/FW send special event for confirmation and expects reply (This is upto the vendors). So user space can't send randomly.

Quoted 1 line

Is it ok if we add this in a separate series ?

Yes.

Thanks,
Srinivas

Quoted 3 lines
#20 lore raw

PATCH v2 5/5 thermal: core: Add notifications call in the framework

In reply to Daniel Lezcano

The generic netlink protocol is implemented but the different notification functions are not yet connected to the core code.

These changes add the notification calls in the different corresponding places.

---
 drivers/thermal/thermal_core.c    | 21 +++++++++++++++++++++
 drivers/thermal/thermal_helpers.c | 11 +++++++++--
 drivers/thermal/thermal_sysfs.c   | 15 ++++++++++++++-
 3 files changed, 44 insertions(+), 3 deletions(-)
drivers/thermal/thermal_core.c +21 -0
index 58c95aeafb7f..1388c03d1190 100644
@@ -215,6 +215,8 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
 	mutex_unlock(&tz->lock);
 	mutex_unlock(&thermal_governor_lock);
 
+	thermal_notify_tz_gov_change(tz->id, policy);
+
 	return ret;
 }
 
@@ -406,12 +408,25 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
 {
 	enum thermal_trip_type type;
+	int trip_temp, hyst = 0;
 
 	/* Ignore disabled trip points */
 	if (test_bit(trip, &tz->trips_disabled))
 		return;
 
+	tz->ops->get_trip_temp(tz, trip, &trip_temp);
 	tz->ops->get_trip_type(tz, trip, &type);
+	if (tz->ops->get_trip_hyst)
+		tz->ops->get_trip_hyst(tz, trip, &hyst);
+
+	if (tz->last_temperature != THERMAL_TEMP_INVALID) {
+		if (tz->last_temperature < trip_temp &&
+		    tz->temperature >= trip_temp)
+			thermal_notify_tz_trip_up(tz->id, trip);
+		if (tz->last_temperature >= trip_temp &&
+		    tz->temperature < (trip_temp - hyst))
+			thermal_notify_tz_trip_down(tz->id, trip);
+	}
 
 	if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
 		handle_critical_trips(tz, trip, type);
@@ -443,6 +458,8 @@ static void update_temperature(struct thermal_zone_device *tz)
 	mutex_unlock(&tz->lock);
 
 	trace_thermal_temperature(tz);
+
+	thermal_genl_sampling_temp(tz->id, temp);
 }
 
 static void thermal_zone_device_init(struct thermal_zone_device *tz)
@@ -1398,6 +1415,8 @@ thermal_zone_device_register(const char *type, int trips, int mask,
 	if (atomic_cmpxchg(&tz->need_update, 1, 0))
 		thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
 
+	thermal_notify_tz_create(tz->id, tz->type);
+
 	return tz;
 
 unregister:
@@ -1469,6 +1488,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 	ida_destroy(&tz->ida);
 	mutex_destroy(&tz->lock);
 	device_unregister(&tz->device);
+
+	thermal_notify_tz_delete(tz->id);
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
 
drivers/thermal/thermal_helpers.c +9 -2
index 6ed24b4e23d3..7893ace1d90f 100644
@@ -175,6 +175,14 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz)
 	mutex_unlock(&tz->lock);
 }
 
+void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int target)
+{
+	if (cdev->ops->set_cur_state(cdev, target))
+		return;
+	thermal_notify_cdev_update(cdev->id, target);
+	thermal_cooling_device_stats_update(cdev, target);
+}
+
 void thermal_cdev_update(struct thermal_cooling_device *cdev)
 {
 	struct thermal_instance *instance;
@@ -192,8 +200,7 @@ void thermal_cdev_update(struct thermal_cooling_device *cdev)
 			target = instance->target;
 	}
 
-	if (!cdev->ops->set_cur_state(cdev, target))
-		thermal_cooling_device_stats_update(cdev, target);
+	thermal_cdev_set_cur_state(cdev, target);
 
 	mutex_unlock(&cdev->lock);
 	trace_cdev_update(cdev, target);
drivers/thermal/thermal_sysfs.c +14 -1
index aa99edb4dff7..ff449943f757 100644
@@ -124,7 +124,8 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 {
 	struct thermal_zone_device *tz = to_thermal_zone(dev);
 	int trip, ret;
-	int temperature;
+	int temperature, hyst = 0;
+	enum thermal_trip_type type;
 
 	if (!tz->ops->set_trip_temp)
 		return -EPERM;
@@ -139,6 +140,18 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
+	if (tz->ops->get_trip_hyst) {
+		ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
+		if (ret)
+			return ret;
+	}
+
+	ret = tz->ops->get_trip_type(tz, trip, &type);
+	if (ret)
+		return ret;
+
+	thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
+
 	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
 
 	return count;
Signature · 1 line
2.17.1
#21 lore raw

Re: [PATCH v2 5/5] thermal: core: Add notifications call in the framework

Quoted 54 lines · Daniel Lezcano

The generic netlink protocol is implemented but the different notification functions are not yet connected to the core code.

These changes add the notification calls in the different corresponding places.

---
 drivers/thermal/thermal_core.c    | 21 +++++++++++++++++++++
 drivers/thermal/thermal_helpers.c | 11 +++++++++--
 drivers/thermal/thermal_sysfs.c   | 15 ++++++++++++++-
 3 files changed, 44 insertions(+), 3 deletions(-)
drivers/thermal/thermal_core.c +15 -0
index 58c95aeafb7f..1388c03d1190 100644
@@ -215,6 +215,8 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
        mutex_unlock(&tz->lock);
        mutex_unlock(&thermal_governor_lock);
 
+       thermal_notify_tz_gov_change(tz->id, policy);
+
        return ret;
 }
 
@@ -406,12 +408,25 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
 {
        enum thermal_trip_type type;
+       int trip_temp, hyst = 0;
 
        /* Ignore disabled trip points */
        if (test_bit(trip, &tz->trips_disabled))
                return;
 
+       tz->ops->get_trip_temp(tz, trip, &trip_temp);
        tz->ops->get_trip_type(tz, trip, &type);
+       if (tz->ops->get_trip_hyst)
+               tz->ops->get_trip_hyst(tz, trip, &hyst);
+
+       if (tz->last_temperature != THERMAL_TEMP_INVALID) {
+               if (tz->last_temperature < trip_temp &&
+                   tz->temperature >= trip_temp)
+                       thermal_notify_tz_trip_up(tz->id, trip);
+               if (tz->last_temperature >= trip_temp &&
+                   tz->temperature < (trip_temp - hyst))
+                       thermal_notify_tz_trip_down(tz->id, trip);
+       }
 
        if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
                handle_critical_trips(tz, trip, type);

drivers/thermal/thermal_core.c static void update_temperature(struct thermal_zone_device *tz) · line 462

Quoted 5 lines
drivers/thermal/thermal_core.c +2 -0
@@ -443,6 +458,8 @@ static void update_temperature(struct thermal_zone_device *tz)
        mutex_unlock(&tz->lock);
 
        trace_thermal_temperature(tz);
+
+       thermal_genl_sampling_temp(tz->id, temp);

Does this need any rate limiting? How many times is update_temperature called on a platform with a dozen sensors?

Quoted 86 lines
drivers/thermal/thermal_core.c +4 -0
 }
 
 static void thermal_zone_device_init(struct thermal_zone_device *tz)
@@ -1398,6 +1415,8 @@ thermal_zone_device_register(const char *type, int trips, int mask,
        if (atomic_cmpxchg(&tz->need_update, 1, 0))
                thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
 
+       thermal_notify_tz_create(tz->id, tz->type);
+
        return tz;
 
 unregister:
@@ -1469,6 +1488,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
        ida_destroy(&tz->ida);
        mutex_destroy(&tz->lock);
        device_unregister(&tz->device);
+
+       thermal_notify_tz_delete(tz->id);
 }
 EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
 
drivers/thermal/thermal_helpers.c +9 -2
index 6ed24b4e23d3..7893ace1d90f 100644
@@ -175,6 +175,14 @@ void thermal_zone_set_trips(struct thermal_zone_device *tz)
        mutex_unlock(&tz->lock);
 }
 
+void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int target)
+{
+       if (cdev->ops->set_cur_state(cdev, target))
+               return;
+       thermal_notify_cdev_update(cdev->id, target);
+       thermal_cooling_device_stats_update(cdev, target);
+}
+
 void thermal_cdev_update(struct thermal_cooling_device *cdev)
 {
        struct thermal_instance *instance;
@@ -192,8 +200,7 @@ void thermal_cdev_update(struct thermal_cooling_device *cdev)
                        target = instance->target;
        }
 
-       if (!cdev->ops->set_cur_state(cdev, target))
-               thermal_cooling_device_stats_update(cdev, target);
+       thermal_cdev_set_cur_state(cdev, target);
 
        mutex_unlock(&cdev->lock);
        trace_cdev_update(cdev, target);
drivers/thermal/thermal_sysfs.c +14 -2
index aa99edb4dff7..ff449943f757 100644
@@ -124,7 +124,8 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
 {
        struct thermal_zone_device *tz = to_thermal_zone(dev);
        int trip, ret;
-       int temperature;
+       int temperature, hyst = 0;
+       enum thermal_trip_type type;
 
        if (!tz->ops->set_trip_temp)
                return -EPERM;
@@ -139,6 +140,18 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr,
        if (ret)
                return ret;
 
+       if (tz->ops->get_trip_hyst) {
+               ret = tz->ops->get_trip_hyst(tz, trip, &hyst);
+               if (ret)
+                       return ret;
+       }
+
+       ret = tz->ops->get_trip_type(tz, trip, &type);
+       if (ret)
+               return ret;
+
+       thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst);
+
        thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
 
        return count;
--

2.17.1

#22 lore raw

Re: [PATCH v2 5/5] thermal: core: Add notifications call in the framework

Quoted 58 lines · Amit Kucheria
Quoted 56 lines · Daniel Lezcano

The generic netlink protocol is implemented but the different notification functions are not yet connected to the core code.

These changes add the notification calls in the different corresponding places.

---
 drivers/thermal/thermal_core.c    | 21 +++++++++++++++++++++
 drivers/thermal/thermal_helpers.c | 11 +++++++++--
 drivers/thermal/thermal_sysfs.c   | 15 ++++++++++++++-
 3 files changed, 44 insertions(+), 3 deletions(-)
drivers/thermal/thermal_core.c +15 -0
index 58c95aeafb7f..1388c03d1190 100644
@@ -215,6 +215,8 @@ int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
        mutex_unlock(&tz->lock);
        mutex_unlock(&thermal_governor_lock);
 
+       thermal_notify_tz_gov_change(tz->id, policy);
+
        return ret;
 }
 
@@ -406,12 +408,25 @@ static void handle_critical_trips(struct thermal_zone_device *tz,
 static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
 {
        enum thermal_trip_type type;
+       int trip_temp, hyst = 0;
 
        /* Ignore disabled trip points */
        if (test_bit(trip, &tz->trips_disabled))
                return;
 
+       tz->ops->get_trip_temp(tz, trip, &trip_temp);
        tz->ops->get_trip_type(tz, trip, &type);
+       if (tz->ops->get_trip_hyst)
+               tz->ops->get_trip_hyst(tz, trip, &hyst);
+
+       if (tz->last_temperature != THERMAL_TEMP_INVALID) {
+               if (tz->last_temperature < trip_temp &&
+                   tz->temperature >= trip_temp)
+                       thermal_notify_tz_trip_up(tz->id, trip);
+               if (tz->last_temperature >= trip_temp &&
+                   tz->temperature < (trip_temp - hyst))
+                       thermal_notify_tz_trip_down(tz->id, trip);
+       }
 
        if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
                handle_critical_trips(tz, trip, type);
@@ -443,6 +458,8 @@ static void update_temperature(struct thermal_zone_device *tz)
        mutex_unlock(&tz->lock);
 
Quoted 6 lines

drivers/thermal/thermal_core.c static void update_temperature(struct thermal_zone_device *tz) · line 462

Quoted 3 lines
drivers/thermal/thermal_core.c +2 -0
        trace_thermal_temperature(tz);
+
+       thermal_genl_sampling_temp(tz->id, temp);

Does this need any rate limiting? How many times is update_temperature called on a platform with a dozen sensors?

Assuming they are all in polling mode, it is dozen messages average per second.

If *all* sensors are doing passive cooling with 100ms, then it is around 120 messages per second in the very worst case (fast polling, all above the trip point).

We do not need any rate limiting ATM. If we reach this limit in the future we can improve the notification.

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
#23 lore raw
In reply to Daniel Lezcano
Quoted 9 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks are statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each thermal entity.

Quoted 6 lines

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51 ++++++++++++++++++++++++++++++++++

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

Quoted 90 lines
 drivers/thermal/thermal_core.h |  9 ++++++
 2 files changed, 60 insertions(+)
drivers/thermal/thermal_core.c +51 -0
index 2a3f83265d8b..e2f8d2550ecd 100644
@@ -611,6 +611,57 @@ void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
        mutex_unlock(&thermal_list_lock);
 }
 
+int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
+                             void *data)
 
 
+{
+       struct thermal_governor *gov;
+       int ret = 0;
+
+       mutex_lock(&thermal_governor_lock);
+       list_for_each_entry(gov, &thermal_governor_list, governor_list) {
+               ret = cb(gov, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_governor_lock);
+
+       return ret;
+}
+
+int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
+                                             void *), void *data)
+{
+       struct thermal_cooling_device *cdev;
+       int ret = 0;
+
+       mutex_lock(&thermal_list_lock);
+       list_for_each_entry(cdev, &thermal_cdev_list, node) {
+               ret = cb(cdev, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_list_lock);
+
+       return ret;
+}
+
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
+                         void *data)
+{
+       struct thermal_zone_device *tz;
+       int ret = 0;
+
+       mutex_lock(&thermal_list_lock);
+       list_for_each_entry(tz, &thermal_tz_list, node) {
+               ret = cb(tz, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_list_lock);
+
+       return ret;
+}
+
 void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,

const char *cdev_type, size_t size)
{

drivers/thermal/thermal_core.h +9 -1
index 4e271016b7a9..bb8f8aee79eb 100644
@@ -41,6 +41,15 @@ extern struct thermal_governor *__governor_thermal_table_end[];
             __governor < __governor_thermal_table_end; \
             __governor++)
 
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
+                         void *);
+
+int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
+                                             void *), void *);
+
+int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
+                             void *thermal_governor);
+
 struct thermal_attr {
        struct device_attribute attr;
        char name[THERMAL_NAME_LENGTH];
--

2.17.1

reviewer
#24 lore raw

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 16 lines · Amit Kucheria
Quoted 14 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each
thermal entity.

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

Quoted 8 lines
Quoted 4 lines
---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

I agree these changes should be in thermal_helper.c

thanks,
rui

Quoted 106 lines
Quoted 106 lines

 drivers/thermal/thermal_core.h |  9 ++++++
 2 files changed, 60 insertions(+)
drivers/thermal/thermal_core.c +0 -0
index 2a3f83265d8b..e2f8d2550ecd 100644
@@ -611,6 +611,57 @@ void

thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,

        mutex_unlock(&thermal_list_lock);

}

+int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),

drivers/thermal/thermal_core.c +7 -0
+                             void *data)
 
 
+{
+       struct thermal_governor *gov;
+       int ret = 0;
+
+       mutex_lock(&thermal_governor_lock);
+       list_for_each_entry(gov, &thermal_governor_list,

governor_list) {

drivers/thermal/thermal_core.c +10 -0
+               ret = cb(gov, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_governor_lock);
+
+       return ret;
+}
+
+int for_each_thermal_cooling_device(int (*cb)(struct

thermal_cooling_device *,

drivers/thermal/thermal_core.c +17 -0
+                                             void *), void *data)
+{
+       struct thermal_cooling_device *cdev;
+       int ret = 0;
+
+       mutex_lock(&thermal_list_lock);
+       list_for_each_entry(cdev, &thermal_cdev_list, node) {
+               ret = cb(cdev, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_list_lock);
+
+       return ret;
+}
+
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *,

void *),

drivers/thermal/thermal_core.c +16 -0
+                         void *data)
+{
+       struct thermal_zone_device *tz;
+       int ret = 0;
+
+       mutex_lock(&thermal_list_lock);
+       list_for_each_entry(tz, &thermal_tz_list, node) {
+               ret = cb(tz, data);
+               if (ret)
+                       break;
+       }
+       mutex_unlock(&thermal_list_lock);
+
+       return ret;
+}
+
 void thermal_zone_device_unbind_exception(struct

thermal_zone_device *tz,
const char *cdev_type,
size_t size)
{

drivers/thermal/thermal_core.h +7 -0
index 4e271016b7a9..bb8f8aee79eb 100644
@@ -41,6 +41,15 @@ extern struct thermal_governor
 *__governor_thermal_table_end[];
             __governor < __governor_thermal_table_end; \
             __governor++)
 
+int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *,
 void *),
+                         void *);
+
+int for_each_thermal_cooling_device(int (*cb)(struct
 thermal_cooling_device *,
+                                             void *), void *);
+
+int for_each_thermal_governor(int (*cb)(struct thermal_governor *,

void *),

drivers/thermal/thermal_core.h +2 -1
+                             void *thermal_governor);
+
 struct thermal_attr {
        struct device_attribute attr;
        char name[THERMAL_NAME_LENGTH];
--

2.17.1

#25 lore raw
Quoted 27 lines · Zhang Rui

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 21 lines · Amit Kucheria
Quoted 18 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each
thermal entity.

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Quoted 5 lines
Quoted 3 lines

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

I agree these changes should be in thermal_helper.c

I was actually serious about killing thermal_helper.c :-)

What is the reason for those 5-6 functions to live outside thermal_core.c? Functions in thermal_helper.c are called by governors and drivers, just like the functions in thermal_core.c. I couldn't find a pattern.

Regards,
Amit

#26 lore raw
Quoted 32 lines · Amit Kucheria
Quoted 31 lines · Zhang Rui

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 24 lines · Amit Kucheria
Quoted 11 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each
thermal entity.

Quoted 7 lines

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

Quoted 8 lines
Quoted 1 line

I agree these changes should be in thermal_helper.c

I was actually serious about killing thermal_helper.c :-)

What is the reason for those 5-6 functions to live outside thermal_core.c? Functions in thermal_helper.c are called by governors and drivers, just like the functions in thermal_core.c. I couldn't find a pattern.

I propose to move these functions in the thermal_helper.c for now as requested by Rui. Then you can send a patch to merge thermal_helper.c to thermal_core.c after and we can comment the move.

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
#27 lore raw
In reply to Zhang Rui
Quoted 26 lines · Zhang Rui

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 21 lines · Amit Kucheria
Quoted 18 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for each
thermal entity.

We keep the self-encapsulation and ensure the locks are correctly taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Quoted 5 lines
Quoted 3 lines

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

I agree these changes should be in thermal_helper.c

Oh, actually I remind put those functions in the thermal_core.c file because they need the locks which are statically defined in there.

If the functions are moved to thermal_helper.c that will imply to export the locks outside of the file, thus breaking the self-encapsulation.

Do you want to move them out?

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
reviewer
#28 lore raw
Quoted 37 lines · Daniel Lezcano
Quoted 30 lines · Zhang Rui

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 25 lines · Amit Kucheria
Quoted 22 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks
are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the
thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for
each
thermal entity.

We keep the self-encapsulation and ensure the locks are correctly
taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Quoted 5 lines
Quoted 3 lines

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

I agree these changes should be in thermal_helper.c

Quoted 8 lines

Oh, actually I remind put those functions in the thermal_core.c file because they need the locks which are statically defined in there.

If the functions are moved to thermal_helper.c that will imply to export
the locks outside of the file, thus breaking the self-encapsulation.

Do you want to move them out?

Then no. I don't have any objection of removing thermal_helper.c, so you can just leave these functions in thermal_core.c

thanks,
rui

Quoted 2 lines
#29 lore raw
Quoted 42 lines · Zhang Rui
Quoted 40 lines · Daniel Lezcano
Quoted 30 lines · Zhang Rui

Hi, Daniel,

seems that you forgot to cc linux-pm mailing list.

Quoted 25 lines · Amit Kucheria
Quoted 22 lines · Daniel Lezcano

The cdev, tz and governor list, as well as their respective locks
are
statically defined in the thermal_core.c file.

In order to give a sane access to these list, like browsing all the
thermal zones or all the cooling devices, let's define a set of helpers where we pass a callback as a parameter to be called for
each
thermal entity.

We keep the self-encapsulation and ensure the locks are correctly
taken when looking at the list.

---
 drivers/thermal/thermal_core.c | 51

++++++++++++++++++++++++++++++++++

Quoted 5 lines
Quoted 3 lines

Is the idea to not use thermal_helpers.c from now on? It fits perfectly with a patch I have to merge all its contents to thermal_core.c :-)

I agree these changes should be in thermal_helper.c

Oh, actually I remind put those functions in the thermal_core.c file because they need the locks which are statically defined in there.

Quoted 8 lines
Quoted 5 lines

If the functions are moved to thermal_helper.c that will imply to export
the locks outside of the file, thus breaking the self-encapsulation.

Do you want to move them out?

Then no. I don't have any objection of removing thermal_helper.c, so you can just leave these functions in thermal_core.c

In that case, Daniel, please find attached a patch to move the contents of thermal_helpers into thermal_core.c

Feel free to include it at the top of this series and modify as you see fit.

Regards,
Amit

From e09befc6c6717c3e4554b0688c7d83b0696d2554 Mon Sep 17 00:00:00 2001
Message-Id: <e09befc6c6717c3e4554b0688c7d83b0696d2554.1593595413.git.amit.kucheria@linaro.org>
From: Amit Kucheria <[email protected]>
Date: Wed, 1 Jul 2020 14:48:47 +0530
Subject: [PATCH] thermal/core: Merge thermal_helpers.c into thermal_core.c

Moving several helper functions to this file require unnecessarily exposing locks outside thermal_core.c.

It is better to simply move the few functions in there directly into thermal_core.c.

---
 drivers/thermal/Makefile          |   3 +-
 drivers/thermal/thermal_core.c    | 216 +++++++++++++++++++++++++++
 drivers/thermal/thermal_helpers.c | 238 ------------------------------
 3 files changed, 217 insertions(+), 240 deletions(-)
 delete mode 100644 drivers/thermal/thermal_helpers.c
drivers/thermal/Makefile +1 -2
index 0c8b84a09b9aa..ed2fc83fe2128 100644
@@ -4,8 +4,7 @@
 #
 
 obj-$(CONFIG_THERMAL)		+= thermal_sys.o
-thermal_sys-y			+= thermal_core.o thermal_sysfs.o \
-					thermal_helpers.o
+thermal_sys-y			+= thermal_core.o thermal_sysfs.o
 
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
drivers/thermal/thermal_core.c +216 -0

222 lines not on this page · raw message

drivers/thermal/thermal_helpers.c deleted +0 -238

238 lines not on this page · raw message

Signature · 1 line
2.25.1
#30 lore raw
Quoted 1 line · Amit Kucheria

On Wed, Jul 1, 2020 at 1:27 PM Zhang Rui <[email protected]> wrote:

[ ... ]

Quoted 7 lines
Quoted 2 lines

Then no. I don't have any objection of removing thermal_helper.c, so you can just leave these functions in thermal_core.c

In that case, Daniel, please find attached a patch to move the contents of thermal_helpers into thermal_core.c

Feel free to include it at the top of this series and modify as you see fit.

I will keep it separate for the moment until this series is accepted and then send a respin of this patch on top of the merged series.

Thanks

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
#31 lore raw
In reply to Zhang Rui

On 01/07/2020 09:57, Zhang Rui wrote:

[ ... ]

Quoted 4 lines
Quoted 1 line

Do you want to move them out?

Then no. I don't have any objection of removing thermal_helper.c, so you can just leave these functions in thermal_core.c

Shall I consider that as an ack for this patch ?

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
reviewer Acked-by
#32 lore raw
Quoted 4 lines · Daniel Lezcano

On 01/07/2020 09:57, Zhang Rui wrote:

[ ... ]

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Do you want to move them out?

Then no. I don't have any objection of removing thermal_helper.c, so
you can just leave these functions in thermal_core.c

Shall I consider that as an ack for this patch ?

sure.

thanks,
rui

Quoted 1 line