Skip to contentSkip to conversation

thermal/debugfs: Add thermal cooling device debugfs information

PATCH Superseded Daniel Lezcano 3 participants 6 messages Dec 27, 2023 – Dec 29, 2023 Thermal

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/2 thermal/debugfs: Add thermal cooling device debugfs information

The thermal framework does not have any debug information except a sysfs stat which is a bit controversial. This one allocates big chunks of memory for every cooling devices with a high number of states and could represent on some systems in production several megabytes of memory for just a portion of it. As the sysfs is limited to a page size, the output is not exploitable with large data array and gets truncated.

The patch provides the same information than sysfs except the transitions are dynamically allocated, thus they won't show more events than the ones which actually occurred. There is no longer a size limitation and it opens the field for more debugging information where the debugfs is designed for, not sysfs.

The thermal debugfs directory structure tries to stay consistent with the sysfs one but in a very simplified way:

thermal/
-- cooling_devices

    |-- 0
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 1
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 2
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 3
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    `-- 4
        |-- clear
        |-- time_in_state_ms
        |-- total_trans
        `-- trans_table

The content of the files in the cooling devices directory is the same as the sysfs one except for the trans_table which has the following format:

Transition Hits

1->0      	246
0->1      	246
2->1      	632
1->2      	632
3->2      	98
2->3      	98

---
Changelog:
- v5
- Removed a spurious change in thermal_helper.c, missed to remove it in v4 (Rafael)
- v4
- Fixed kerneldoc description ordering (Rafael)
- Fixed comment (Rafael)
- Renamed s/cdev_value/cdev_record/ (Rafael)
- Used union instead of a common 'value' field in cdev_record (Rafael)
- Renamed s/cdev/cdev_dbg/ for clarity (Rafael)
- Renamed s/dfs/thermal_dbg/ for clarity (Rafael)
- Renamed s/list/lists/ in the place where there are array of lists (Rafael)
- Inverted initialization logic when allocating a cdev_record (Rafael)
- Moved now = ktime_get() closer to the place where it is used (Rafael)
- Moved two lines down to a condition (Rafael)
- Removed strange and unexpected addition of function (Rafael)
- v3
- Fixed kerneldoc description (kbuild)
- Made some functions static
- v2
- Added parameter names to fix kbuild report
- Renamed 'reset' to 'clear' to avoid confusion (Rafael)
- Fixed several typos and rephrased some sentences (Rafael)
- Renamed structure field name s/list/node/ (Rafael)
- Documented structures and exported functions (Rafael)
- s/trans_list/transitions/ (Rafael)
- s/duration_list/durations/ (Rafael)
- Folded 'alloc' and 'insert' into a single function (Rafael)
- s/list/lists/ as it is an array of lists (Rafael)
- s/pos/entry/ (Rafael)
- Introduced a locking in the 'clear' callback function (Rafael)
- s/to/new_state/ and s/from/old_state/ (Rafael)
- Added some comments in thermal_debug_cdev_transition() (Rafael)
- Explained why char[11] (Rafael)
- s/Hits/Occurrences/ (Rafael)
- s/Time/Residency/ (Rafael)
- Constified structure pointer passed to thermal_debug_cdev_transition()
- s/thermal_debug_cdev_transition()/thermal_debug_cdev_state_update()/
- v1 (from RFC):
- Fixed typo "occurred"
- Changed Kconfig option name and description
- Removed comment in the Makefile
- Renamed exported function name s/debugfs/debug/
- Replaced thermal_debug_cdev_[unregister|register] by [add|remove]

---
 drivers/thermal/Kconfig           |   7 +
 drivers/thermal/Makefile          |   2 +
 drivers/thermal/thermal_core.c    |   6 +
 drivers/thermal/thermal_core.h    |   1 +
 drivers/thermal/thermal_debugfs.c | 445 ++++++++++++++++++++++++++++++
 drivers/thermal/thermal_debugfs.h |  14 +
 drivers/thermal/thermal_helpers.c |  20 +-
 include/linux/thermal.h           |   7 +
 8 files changed, 496 insertions(+), 6 deletions(-)
 create mode 100644 drivers/thermal/thermal_debugfs.c
 create mode 100644 drivers/thermal/thermal_debugfs.h
drivers/thermal/Kconfig +7 -0
index c81a00fbca7d..3ff7add3fb7c 100644
@@ -33,6 +33,13 @@ config THERMAL_STATISTICS
 
 	  If in doubt, say N.
 
+config THERMAL_DEBUGFS
+	bool "Thermal subsystem debug support"
+	depends on DEBUG_FS
+	help
+	  Say Y to allow the thermal subsystem to collect diagnostic
+	  information that can be accessed via debugfs.
+
 config THERMAL_EMERGENCY_POWEROFF_DELAY_MS
 	int "Emergency poweroff delay in milli-seconds"
 	default 0
drivers/thermal/Makefile +2 -0
index c934cab309ae..0f65ae86a9c6 100644
@@ -10,6 +10,8 @@ thermal_sys-y			+= thermal_trip.o thermal_helpers.o
 # netlink interface to manage the thermal framework
 thermal_sys-$(CONFIG_THERMAL_NETLINK)		+= thermal_netlink.o
 
+thermal_sys-$(CONFIG_THERMAL_DEBUGFS)	+= thermal_debugfs.o
+
 # interface to/from other layers providing sensors
 thermal_sys-$(CONFIG_THERMAL_HWMON)		+= thermal_hwmon.o
 thermal_sys-$(CONFIG_THERMAL_OF)		+= thermal_of.o
drivers/thermal/thermal_core.c +6 -0
index 625ba07cbe2f..33332d401b13 100644
@@ -945,6 +945,8 @@ __thermal_cooling_device_register(struct device_node *np,
 
 	mutex_unlock(&thermal_list_lock);
 
+	thermal_debug_cdev_add(cdev);
+	
 	return cdev;
 
 out_cooling_dev:
@@ -1151,6 +1153,8 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
 	if (!cdev)
 		return;
 
+	thermal_debug_cdev_remove(cdev);
+
 	mutex_lock(&thermal_list_lock);
 
 	if (!thermal_cooling_device_present(cdev)) {
@@ -1570,6 +1574,8 @@ static int __init thermal_init(void)
 {
 	int result;
 
+	thermal_debug_init();
+
 	result = thermal_netlink_init();
 	if (result)
 		goto error;
drivers/thermal/thermal_core.h +1 -0
index 7dfe6c8deb8e..4b3452a65a2f 100644
@@ -13,6 +13,7 @@
 #include <linux/thermal.h>
 
 #include "thermal_netlink.h"
+#include "thermal_debugfs.h"
 
 /* Default Thermal Governor */
 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
drivers/thermal/thermal_debugfs.c new file +445 -0

445 lines not on this page · raw message

drivers/thermal/thermal_debugfs.h new file +14 -0
new file mode 100644
index 000000000000..341499388448
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifdef CONFIG_THERMAL_DEBUGFS
+void thermal_debug_init(void);
+void thermal_debug_cdev_add(struct thermal_cooling_device *cdev);
+void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev);
+void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev, int state);
+#else
+static inline void thermal_debug_init(void) {}
+static inline void thermal_debug_cdev_add(struct thermal_cooling_device *cdev) {}
+static inline void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) {}
+static inline void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
+						   int state) {}
+#endif /* CONFIG_THERMAL_DEBUGFS */
drivers/thermal/thermal_helpers.c +14 -6
index 69e8ea4aa908..c9e3d6626453 100644
@@ -151,14 +151,22 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
 }
 EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
 
-static void thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev,
-				       int target)
+static int thermal_cdev_set_cur_state(struct thermal_cooling_device *cdev, int state)
 {
-	if (cdev->ops->set_cur_state(cdev, target))
-		return;
+	int ret;
+
+	/*
+	 * No check is needed for the ops->set_cur_state as the
+	 * registering function checked the ops are correctly set
+	 */
+	ret = cdev->ops->set_cur_state(cdev, state);
+	if (!ret) {
+		thermal_notify_cdev_state_update(cdev->id, state);
+		thermal_cooling_device_stats_update(cdev, state);
+		thermal_debug_cdev_state_update(cdev, state);
+	}
 
-	thermal_notify_cdev_state_update(cdev->id, target);
-	thermal_cooling_device_stats_update(cdev, target);
+	return ret;
 }
 
 void __thermal_cdev_update(struct thermal_cooling_device *cdev)
include/linux/thermal.h +7 -0
index 0ea99f50d57c..8461f008c3de 100644
@@ -32,6 +32,7 @@
 struct thermal_zone_device;
 struct thermal_cooling_device;
 struct thermal_instance;
+struct thermal_debugfs;
 struct thermal_attr;
 
 enum thermal_trend {
@@ -110,6 +111,9 @@ struct thermal_cooling_device {
 	struct mutex lock; /* protect thermal_instances list */
 	struct list_head thermal_instances;
 	struct list_head node;
+#ifdef CONFIG_THERMAL_DEBUGFS
+	struct thermal_debugfs *debugfs;
+#endif
 };
 
 /**
@@ -183,6 +187,9 @@ struct thermal_zone_device {
 	struct list_head node;
 	struct delayed_work poll_queue;
 	enum thermal_notify_event notify_event;
+#ifdef CONFIG_THERMAL_DEBUGFS
+	struct thermal_debugfs *debugfs;
+#endif
 };
 
 /**
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 2/2 thermal/debugfs: Add thermal debugfs information for mitigation episodes

The mitigation episodes are recorded. A mitigation episode happens when the first trip point is crossed the way up and then the way down. During this episode other trip points can be crossed also and are accounted for this mitigation episode. The interesting information is the average temperature at the trip point, the undershot and the overshot. The standard deviation of the mitigated temperature will be added later.

The thermal debugfs directory structure tries to stay consistent with the sysfs one but in a very simplified way:

thermal/
`-- thermal_zones

     |-- 0
     |   `-- mitigations
     `-- 1
         `-- mitigations

The content of the mitigations file has the following format:

,-Mitigation at 349988258us, duration=130136ms

| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |     130136 |     68227 |     62500 |     75625 |
|    1 |  passive |     75000 |      2000 |     104209 |     74857 |     71666 |     77500 |
,-Mitigation at 272451637us, duration=75000ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |      75000 |     68561 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      60714 |     74820 |     70555 |     77500 |
,-Mitigation at 238184119us, duration=27316ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |      27316 |     73377 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      19468 |     75284 |     69444 |     77500 |
,-Mitigation at 39863713us, duration=136196ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |     136196 |     73922 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      91721 |     74386 |     69444 |     78125 |

More information for a better understanding of the thermal behavior will be added after. The idea is to give detailed statistics information about the undershots and overshots, the temperature speed, etc... As all the information in a single file is too much, the idea would be to create a directory named with the mitigation timestamp where all data could be added.

Please note this code is immune against trip ordering but not against a trip temperature change while a mitigation is happening. However, this situation should be extremely rare, perhaps not happening and we might question ourselves if something should be done in the core framework for other components first.

---
Changelog:
- v5
- No changes
- v4
- Reworded "trip violation" (Rafael)
- Reworded some comments and clarified other ones (Rafael)
- Renamed s/tz/tz_dbg/ for clarity (Rafael)
- Used flexible array and struct_size for allocation (Rafael)
- Renamed s/dfs/thermal_dbg/ for clarity (Rafael)
- Used intermediate variable to prevent dereferencing multiple times a structure (Rafael)
- Renamed variable s/trip/trip_id/ (Rafael)
- Clarified trip_index usage in the comment (Rafael)
- v3
- Fixed kerneldoc (kbuild)
- Fixed wrong indentation s/<space>/<tab>/
- v2
- Applied changes based on comments from patch 1/2
- Constified structure in function parameters
- v1 (from RFC):
- Replaced exported function name s/debugfs/debug/
- Used "struct thermal_trip" parameter instead of "trip_id"
- Renamed handle_way_[up|down] by tz_trip_[up|down]
- Replaced thermal_debug_tz_[unregister|register] by [add|remove]

---
 drivers/thermal/thermal_core.c    |   7 +
 drivers/thermal/thermal_debugfs.c | 383 +++++++++++++++++++++++++++++-
 drivers/thermal/thermal_debugfs.h |  14 ++
 3 files changed, 403 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +7 -0
index 33332d401b13..a0cbe8d7b945 100644
@@ -367,6 +367,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
 			thermal_notify_tz_trip_up(tz->id,
 						  thermal_zone_trip_id(tz, trip),
 						  tz->temperature);
+			thermal_debug_tz_trip_up(tz, trip);
 			trip->threshold = trip->temperature - trip->hysteresis;
 		} else {
 			trip->threshold = trip->temperature;
@@ -386,6 +387,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
 			thermal_notify_tz_trip_down(tz->id,
 						    thermal_zone_trip_id(tz, trip),
 						    tz->temperature);
+			thermal_debug_tz_trip_down(tz, trip);
 			trip->threshold = trip->temperature;
 		} else {
 			trip->threshold = trip->temperature - trip->hysteresis;
@@ -417,6 +419,7 @@ static void update_temperature(struct thermal_zone_device *tz)
 	trace_thermal_temperature(tz);
 
 	thermal_genl_sampling_temp(tz->id, temp);
+	thermal_debug_update_temp(tz);
 }
 
 static void thermal_zone_device_init(struct thermal_zone_device *tz)
@@ -1396,6 +1399,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
 
 	thermal_notify_tz_create(tz->id, tz->type);
 
+	thermal_debug_tz_add(tz);
+
 	return tz;
 
 unregister:
@@ -1461,6 +1466,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
 	if (!tz)
 		return;
 
+	thermal_debug_tz_remove(tz);
+
 	tz_id = tz->id;
 
 	mutex_lock(&thermal_list_lock);
drivers/thermal/thermal_debugfs.c +382 -1

421 lines not on this page · raw message

drivers/thermal/thermal_debugfs.h +14 -0
index 341499388448..155b9af5fe87 100644
@@ -5,10 +5,24 @@ void thermal_debug_init(void);
 void thermal_debug_cdev_add(struct thermal_cooling_device *cdev);
 void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev);
 void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev, int state);
+void thermal_debug_tz_add(struct thermal_zone_device *tz);
+void thermal_debug_tz_remove(struct thermal_zone_device *tz);
+void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
+			      const struct thermal_trip *trip);
+void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
+				const struct thermal_trip *trip);
+void thermal_debug_update_temp(struct thermal_zone_device *tz);
 #else
 static inline void thermal_debug_init(void) {}
 static inline void thermal_debug_cdev_add(struct thermal_cooling_device *cdev) {}
 static inline void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) {}
 static inline void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
 						   int state) {}
+static inline void thermal_debug_tz_add(struct thermal_zone_device *tz) {}
+static inline void thermal_debug_tz_remove(struct thermal_zone_device *tz) {}
+static inline void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
+					    const struct thermal_trip *trip) {};
+static inline void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
+					      const struct thermal_trip *trip) {}
+static inline void thermal_debug_update_temp(struct thermal_zone_device *tz) {}
 #endif /* CONFIG_THERMAL_DEBUGFS */
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v5 2/2] thermal/debugfs: Add thermal debugfs information for mitigation episodes

Quoted 164 lines · Daniel Lezcano

The mitigation episodes are recorded. A mitigation episode happens when the first trip point is crossed the way up and then the way down. During this episode other trip points can be crossed also and are accounted for this mitigation episode. The interesting information is the average temperature at the trip point, the undershot and the overshot. The standard deviation of the mitigated temperature will be added later.

The thermal debugfs directory structure tries to stay consistent with the sysfs one but in a very simplified way:

thermal/
`-- thermal_zones

     |-- 0
     |   `-- mitigations
     `-- 1
         `-- mitigations

The content of the mitigations file has the following format:

,-Mitigation at 349988258us, duration=130136ms

| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |     130136 |     68227 |     62500 |     75625 |
|    1 |  passive |     75000 |      2000 |     104209 |     74857 |     71666 |     77500 |
,-Mitigation at 272451637us, duration=75000ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |      75000 |     68561 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      60714 |     74820 |     70555 |     77500 |
,-Mitigation at 238184119us, duration=27316ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |      27316 |     73377 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      19468 |     75284 |     69444 |     77500 |
,-Mitigation at 39863713us, duration=136196ms
| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |
|    0 |  passive |     65000 |      2000 |     136196 |     73922 |     62500 |     75000 |
|    1 |  passive |     75000 |      2000 |      91721 |     74386 |     69444 |     78125 |

More information for a better understanding of the thermal behavior will be added after. The idea is to give detailed statistics information about the undershots and overshots, the temperature speed, etc... As all the information in a single file is too much, the idea would be to create a directory named with the mitigation timestamp where all data could be added.

Please note this code is immune against trip ordering but not against a trip temperature change while a mitigation is happening. However, this situation should be extremely rare, perhaps not happening and we might question ourselves if something should be done in the core framework for other components first.

---
Changelog:
- v5
- No changes
- v4
- Reworded "trip violation" (Rafael)
- Reworded some comments and clarified other ones (Rafael)
- Renamed s/tz/tz_dbg/ for clarity (Rafael)
- Used flexible array and struct_size for allocation (Rafael)
- Renamed s/dfs/thermal_dbg/ for clarity (Rafael)
- Used intermediate variable to prevent dereferencing multiple times a structure (Rafael)
- Renamed variable s/trip/trip_id/ (Rafael)
- Clarified trip_index usage in the comment (Rafael)
- v3
- Fixed kerneldoc (kbuild)
- Fixed wrong indentation s/<space>/<tab>/
- v2
- Applied changes based on comments from patch 1/2
- Constified structure in function parameters
- v1 (from RFC):
- Replaced exported function name s/debugfs/debug/
- Used "struct thermal_trip" parameter instead of "trip_id"
- Renamed handle_way_[up|down] by tz_trip_[up|down]
- Replaced thermal_debug_tz_[unregister|register] by [add|remove]

---
 drivers/thermal/thermal_core.c    |   7 +
 drivers/thermal/thermal_debugfs.c | 383 +++++++++++++++++++++++++++++-
 drivers/thermal/thermal_debugfs.h |  14 ++
 3 files changed, 403 insertions(+), 1 deletion(-)
drivers/thermal/thermal_core.c +7 -0
index 33332d401b13..a0cbe8d7b945 100644
@@ -367,6 +367,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
                        thermal_notify_tz_trip_up(tz->id,
                                                  thermal_zone_trip_id(tz, trip),
                                                  tz->temperature);
+                       thermal_debug_tz_trip_up(tz, trip);
                        trip->threshold = trip->temperature - trip->hysteresis;
                } else {
                        trip->threshold = trip->temperature;
@@ -386,6 +387,7 @@ static void handle_thermal_trip(struct thermal_zone_device *tz,
                        thermal_notify_tz_trip_down(tz->id,
                                                    thermal_zone_trip_id(tz, trip),
                                                    tz->temperature);
+                       thermal_debug_tz_trip_down(tz, trip);
                        trip->threshold = trip->temperature;
                } else {
                        trip->threshold = trip->temperature - trip->hysteresis;
@@ -417,6 +419,7 @@ static void update_temperature(struct thermal_zone_device *tz)
        trace_thermal_temperature(tz);
 
        thermal_genl_sampling_temp(tz->id, temp);
+       thermal_debug_update_temp(tz);
 }
 
 static void thermal_zone_device_init(struct thermal_zone_device *tz)
@@ -1396,6 +1399,8 @@ thermal_zone_device_register_with_trips(const char *type, struct thermal_trip *t
 
        thermal_notify_tz_create(tz->id, tz->type);
 
+       thermal_debug_tz_add(tz);
+
        return tz;
 
 unregister:
@@ -1461,6 +1466,8 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz)
        if (!tz)
                return;
 
+       thermal_debug_tz_remove(tz);
+
        tz_id = tz->id;
 
        mutex_lock(&thermal_list_lock);
drivers/thermal/thermal_debugfs.c +19 -0
index 84e5ef3dc16c..0f0f0e603428 100644
@@ -14,8 +14,11 @@
 #include <linux/mutex.h>
 #include <linux/thermal.h>
 
+#include "thermal_core.h"
+
 static struct dentry *d_root;
 static struct dentry *d_cdev;
+static struct dentry *d_tz;
 
 /*
  * Length of the string containing the thermal zone id or the cooling
@@ -81,22 +84,87 @@ struct cdev_record {
         };
 };
 
+/**
+ * struct trip_stats - Thermal trip statistics
+ *
+ * The trip_stats structure has the relevant information to show the
+ * statistics related to temperature going above a trip point.
+ *
+ * @timestamp: the trip crossing timestamp
+ * @duration: total time when the zone temperature was above the trip point
+ * @count: the number of times the zone temperature was above the trip point
+ * @max: maximum recorded temperature above the trip point
+ * @min: minimum recorded temperature above the trip point
+ * @avg: average temperature above the trip point
+ */
+struct trip_stats {
+       ktime_t timestamp;
+       ktime_t duration;

drivers/thermal/thermal_debugfs.c struct cdev_record { · line 112

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+       int count;
+       int max;
+       int min;
+       int avg;
+};
+
+/**
+ * struct tz_events - Collection of all mitigation episodes for a thermal zone
+ *
+ * The tz_events structure describes a mitigation episode.

This sentence kind of contradicts the one-line description above: if it represents one episode (which I think is the case), it cannot be a collection of all of them at the same time.

Also, I still think that the name of this structure is not quite adequate, because it is not clear what events it refers to. It would be better to call it tz_episode, for example.

Quoted 18 lines
drivers/thermal/thermal_debugfs.c +18 -0
+ * A mitigation episode is when the mitigation begins and ends. During
+ * this episode we can have multiple trip points crossed the way up
+ * and down if there are multiple trip describes in the firmware.
+ *
+ * @timestamp: First trip point crossed the way up
+ * @duration: total duration of the mitigation episode
+ * @node: a list element to be added to the list of tz events
+ * @trip_stats: per trip point statistics, flexible array
+ */
+struct tz_events {
+       ktime_t timestamp;
+       ktime_t duration;
+       struct list_head node;
+       struct trip_stats trip_stats[];
+};
+
+/**
+ * struct tz_debugfs - Store all mitigations episodes for a thermal zone

drivers/thermal/thermal_debugfs.c struct cdev_record { · line 140

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+ *
+ * The tz_debugfs structure contains the list of the mitigation
+ * episodes and has to track which trip point has been crossed in
+ * order to handle correctly nested trip point mitigation episodes.
+ *
+ * We keep the history of the trip point crossed in an array and as we
+ * can go back and forth inside this history, eg. trip 0,1,2,1,2,1,0,
+ * we keep track of the current position in the history array.
+ *
+ * @tz_events: a list of thermal mitigation episodes

The name of this field could be more specific IMO, like "mitigations". As is, the connection between the "events" (whatever they are) and mitigation episodes for the given tz is not clear.

drivers/thermal/thermal_debugfs.c struct cdev_record { · line 148

Quoted 8 lines
drivers/thermal/thermal_debugfs.c +8 -0
+ * @trips_crossed: an array of trip points crossed by id
+ * @trip_index: the current trip point index in the trip points history
+ */
+struct tz_debugfs {
+       struct list_head tz_events;
+       int *trips_crossed;
+       int trip_index;
+};

As mentioned earlier, thermal_debug_tz_trip_down() assumes that the temperature ordering of trip points will not change during a mitigation episode, which is not guaranteed in general and the assumption is not even necessary.

To avoid making this assumption, I would instead make thermal_debug_tz_trip_down() always walk trips_crossed[] top-down until the trip passed to it is found (see below) and I would use nr_trips ("the number of trips engaged so far in the current mitigation episode").

drivers/thermal/thermal_debugfs.c struct cdev_record { · line 160

Quoted 13 lines
drivers/thermal/thermal_debugfs.c +3 -1
+
 /**
  * struct thermal_debugfs - High level structure for a thermal object in debugfs
  *
  * The thermal_debugfs structure is the common structure used by the
- * cooling device to compute the statistics.
+ * cooling device or the thermal zone to store the statistics.
  *
  * @d_top: top directory of the thermal object directory
  * @lock: per object lock to protect the internals
  *
  * @cdev: a cooling device debug structure
+ * @tz: a thermal zone debug structure

These are cdev_dbg and tz_dbg now.

Quoted 106 lines
drivers/thermal/thermal_debugfs.c +86 -0
  */
 struct thermal_debugfs {
        struct dentry *d_top;
        struct mutex lock;
        union {
                struct cdev_debugfs cdev_dbg;
+               struct tz_debugfs tz_dbg;
        };
 };
 
@@ -107,6 +175,10 @@ void thermal_debug_init(void)
                return;
 
        d_cdev = debugfs_create_dir("cooling_devices", d_root);
+       if (!d_cdev)
+               return;
+
+       d_tz = debugfs_create_dir("thermal_zones", d_root);
 }
 
 static struct thermal_debugfs *thermal_debugfs_add_id(struct dentry *d, int id)
@@ -443,3 +515,312 @@ void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev)
 
        mutex_unlock(&thermal_dbg->lock);
 }
+
+static struct tz_events *thermal_debugfs_tz_event_alloc(struct thermal_zone_device *tz,
+                                                       ktime_t now)
+{
+       struct tz_events *tze;
+       int i;
+
+       tze = kzalloc(struct_size(tze, trip_stats, tz->num_trips), GFP_KERNEL);
+       if (!tze)
+               return NULL;
+
+       INIT_LIST_HEAD(&tze->node);
+       tze->timestamp = now;
+
+       for (i = 0; i < tz->num_trips; i++) {
+               tze->trip_stats[i].min = INT_MAX;
+               tze->trip_stats[i].max = INT_MIN;
+       }
+
+       return tze;
+}
+
+void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
+                             const struct thermal_trip *trip)
+{
+       struct tz_events *tze;
+       struct tz_debugfs *tz_dbg;
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+       int temperature = tz->temperature;
+       int trip_id = thermal_zone_trip_id(tz, trip);
+       ktime_t now = ktime_get();
+
+       if (!thermal_dbg)
+               return;
+
+       mutex_lock(&thermal_dbg->lock);
+
+       tz_dbg = &thermal_dbg->tz_dbg;
+
+       /*
+        * The mitigation is starting. A mitigation can contain
+        * several episodes where each of them is related to a
+        * temperature crossing a trip point. The episodes are
+        * nested. That means when the temperature is crossing the
+        * first trip point, the duration begins to be measured. If
+        * the temperature continues to increase and reaches the
+        * second trip point, the duration of the first trip must be
+        * also accumulated.
+        *
+        * eg.
+        *
+        * temp
+        *   ^
+        *   |             --------
+        * trip 2         /        \         ------
+        *   |           /|        |\      /|      |\
+        * trip 1       / |        | `----  |      | \
+        *   |         /| |        |        |      | |\
+        * trip 0     / | |        |        |      | | \
+        *   |       /| | |        |        |      | | |\
+        *   |      / | | |        |        |      | | | `--
+        *   |     /  | | |        |        |      | | |
+        *   |-----   | | |        |        |      | | |
+        *   |        | | |        |        |      | | |
+        *    --------|-|-|--------|--------|------|-|-|------------------> time
+        *            | | |<--t2-->|        |<-t2'>| | |
+        *            | |                            | |
+        *            | |<------------t1------------>| |
+        *            |                                |
+        *            |<-------------t0--------------->|
+        *
+        */
+       if (tz_dbg->trip_index < 0) {
+               tze = thermal_debugfs_tz_event_alloc(tz, now);
+               if (!tze)
+                       return;
+
+               list_add(&tze->node, &tz_dbg->tz_events);
+       }
+
+       /*

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 608

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+        * The trip_index points to the current position in the
+        * trip_crossed[] array. Each time a trip point is crossed the
+        * way up, the trip_id is stored in the array and the
+        * trip_index is incremented. A negative trip_index means we
+        * are entering a mitigation episode.
+        *
+        * The trip ids may not be in the ascending order but the
+        * result in the array trips_crossed will be in the ascending
+        * temperature order.
+        */

As I said above, I would use nr_trips instead of trip_index and so I would adjust the comment above for that.

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 610

Quoted 2 lines
drivers/thermal/thermal_debugfs.c +2 -0
+       tz_dbg->trip_index++;
+       tz_dbg->trips_crossed[tz_dbg->trip_index] = trip_id;

+ tz_dbg->trips_crossed[tz_dbg->nr_trips++] = trip_id;

Quoted 9 lines
drivers/thermal/thermal_debugfs.c +9 -0
+
+       tze = list_first_entry(&tz_dbg->tz_events, struct tz_events, node);
+       tze->trip_stats[trip_id].timestamp = now;
+       tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, temperature);
+       tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, temperature);
+       tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
+               (temperature - tze->trip_stats[trip_id].avg) /
+               tze->trip_stats[trip_id].count;
+

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 629

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+       mutex_unlock(&thermal_dbg->lock);
+}
+
+void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
+                               const struct thermal_trip *trip)
+{
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+       struct tz_events *tze;
+       struct tz_debugfs *tz_dbg;
+

This blank line is redundant.

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 631

Quoted 2 lines
drivers/thermal/thermal_debugfs.c +2 -0
+       int trip_id = thermal_zone_trip_id(tz, trip);
+       ktime_t delta, now = ktime_get();

+ int i;

Quoted 7 lines
drivers/thermal/thermal_debugfs.c +7 -0
+
+       if (!thermal_dbg)
+               return;
+
+       mutex_lock(&thermal_dbg->lock);
+
+       tz_dbg = &thermal_dbg->tz_dbg;

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 648

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+
+       /*
+        * The temperature crosses the way down but there was not
+        * mitigation detected before. That may happen when the
+        * temperature is greater than a trip point when registering a
+        * thermal zone, which is a common use case as the kernel has
+        * no mitigation mechanism yet at boot time.
+        */
+       if (tz_dbg->trip_index < 0)
+               goto out;

+ if (!tz_dbg->nr_trips)
+ goto out;
+
+ for (i = tz_dbg->nr_trips - 1; i >= 0; i--) {
+ if (tz_dbg->trips_crossed[i] == trip_id)
+ break;
+ }
+ if (i < 0)
+ goto out;
+
+ tz_dbg->nr_trips--;
+
+ if (i < tz_dbg->nr_trips)
+ tz_dbg->trips_crossed[i] =
tz_dbg->trips_crossed[tz_dbg->nr_trips];

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 655

Quoted 7 lines
drivers/thermal/thermal_debugfs.c +7 -0
+
+       tze = list_first_entry(&tz_dbg->tz_events, struct tz_events, node);
+
+       delta = ktime_sub(now, tze->trip_stats[trip_id].timestamp);
+       tze->trip_stats[trip_id].duration = ktime_add(delta,
+                                                     tze->trip_stats[trip_id].duration);
+

The above does not depend on trip_index, so OK.

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 656

Quoted 1 line
drivers/thermal/thermal_debugfs.c +1 -0
+       tz_dbg->trip_index--;

The above goes away.

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 662

Quoted 6 lines
drivers/thermal/thermal_debugfs.c +6 -0
+
+       /*
+        * This event closes the mitigation as we are crossing the
+        * last trip point the way down.
+        */
+       if (tz_dbg->trip_index < 0)

+ if (!tz_dbg->nr_trips)

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 667

Quoted 5 lines
drivers/thermal/thermal_debugfs.c +5 -0
+               tze->duration = ktime_sub(now, tze->timestamp);
+
+out:
+       mutex_unlock(&thermal_dbg->lock);
+}

With the above modifications, it should work regardless of what happens to the trip points during the mitigation episode so long as the core reports the trip crossing events correctly.

Quoted 6 lines
drivers/thermal/thermal_debugfs.c +6 -0
+
+void thermal_debug_update_temp(struct thermal_zone_device *tz)
+{
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+       struct tz_events *tze;
+       struct tz_debugfs *tz_dbg;

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 683

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+       int trip_id;
+
+       if (!thermal_dbg)
+               return;
+
+       mutex_lock(&thermal_dbg->lock);
+
+       tz_dbg = &thermal_dbg->tz_dbg;
+
+       if (tz_dbg->trip_index >= 0) {

This would become

+ if (tz_dbg->nr_trips > 0) {

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 684

Quoted 1 line
drivers/thermal/thermal_debugfs.c +1 -0
+               trip_id = tz_dbg->trip_index;

but is this correct?

AFAICS, if anything, it should be

+ trip_id = tz_dbg->trips_crossed[tz_dbg->trip_index];

and this assumes that the last trip in the array is the one whose stats need to be updated, but why would it be the only one?

Shouldn't the stats be updated for all of them? After all, they all are still engaged in the current mitigation episode, so

+ for (i = 0; i < tz_dbg->nr_trips; i++) {
+ trip_id = tz_dbg->trips_crossed[i];

Quoted 45 lines
drivers/thermal/thermal_debugfs.c +45 -0
+               tze = list_first_entry(&tz_dbg->tz_events, struct tz_events, node);
+               tze->trip_stats[trip_id].count++;
+               tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, tz->temperature);
+               tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, tz->temperature);
+               tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
+                       (tz->temperature - tze->trip_stats[trip_id].avg) /
+                       tze->trip_stats[trip_id].count;
+       }
+
+       mutex_unlock(&thermal_dbg->lock);
+}
+
+static void *tze_seq_start(struct seq_file *s, loff_t *pos)
+{
+       struct thermal_zone_device *tz = s->private;
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+       struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
+
+       mutex_lock(&thermal_dbg->lock);
+
+       return seq_list_start(&tz_dbg->tz_events, *pos);
+}
+
+static void *tze_seq_next(struct seq_file *s, void *v, loff_t *pos)
+{
+       struct thermal_zone_device *tz = s->private;
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+       struct tz_debugfs *tz_dbg = &thermal_dbg->tz_dbg;
+
+       return seq_list_next(v, &tz_dbg->tz_events, pos);
+}
+
+static void tze_seq_stop(struct seq_file *s, void *v)
+{
+       struct thermal_zone_device *tz = s->private;
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+
+       mutex_unlock(&thermal_dbg->lock);
+}
+
+static int tze_seq_show(struct seq_file *s, void *v)
+{
+       struct thermal_zone_device *tz = s->private;
+       struct tz_events *tze;
+       int i;

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 739

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+
+       tze = list_entry((struct list_head *)v, struct tz_events, node);
+
+       seq_printf(s, ",-Mitigation at %lluus, duration=%llums\n",
+                  ktime_to_us(tze->timestamp),
+                  ktime_to_ms(tze->duration));
+
+       seq_printf(s, "| trip |     type | temp(°mC) | hyst(°mC) |  duration  |  avg(°mC) |  min(°mC) |  max(°mC) |\n");
+
+       for (i = 0; i < tz->num_trips; i++) {

Why can't for_each_trip() be used here?

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 745

Quoted 6 lines
drivers/thermal/thermal_debugfs.c +6 -0
+
+               struct thermal_trip trip;
+               const char *type;
+
+               if (__thermal_zone_get_trip(tz, i, &trip))
+                       continue;

So this would not be necessary?

Quoted 50 lines
drivers/thermal/thermal_debugfs.c +50 -0
+
+               /*
+                * There is no possible mitigation happening at the
+                * critical trip point, so the stats will be always
+                * zero, skip this trip point
+                */
+               if (trip.type == THERMAL_TRIP_CRITICAL)
+                       continue;
+
+               if (trip.type == THERMAL_TRIP_PASSIVE)
+                       type = "passive";
+               else if (trip.type == THERMAL_TRIP_ACTIVE)
+                       type = "active";
+               else
+                       type = "hot";
+
+               seq_printf(s, "| %*d | %*s | %*d | %*d | %*lld | %*d | %*d | %*d |\n",
+                          4 , i,
+                          8, type,
+                          9, trip.temperature,
+                          9, trip.hysteresis,
+                          10, ktime_to_ms(tze->trip_stats[i].duration),
+                          9, tze->trip_stats[i].avg,
+                          9, tze->trip_stats[i].min,
+                          9, tze->trip_stats[i].max);
+       }
+
+       return 0;
+}
+
+static const struct seq_operations tze_sops = {
+       .start = tze_seq_start,
+       .next = tze_seq_next,
+       .stop = tze_seq_stop,
+       .show = tze_seq_show,
+};
+
+DEFINE_SEQ_ATTRIBUTE(tze);
+
+void thermal_debug_tz_add(struct thermal_zone_device *tz)
+{
+       struct thermal_debugfs *thermal_dbg;
+       struct tz_debugfs *tz_dbg;
+
+       thermal_dbg = thermal_debugfs_add_id(d_tz, tz->id);
+       if (!thermal_dbg)
+               return;
+
+       tz_dbg = &thermal_dbg->tz_dbg;
+

drivers/thermal/thermal_debugfs.c void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) · line 805

Quoted 10 lines
drivers/thermal/thermal_debugfs.c +10 -0
+       tz_dbg->trips_crossed = kzalloc(sizeof(int) * tz->num_trips, GFP_KERNEL);
+       if (!tz_dbg->trips_crossed) {
+               thermal_debugfs_remove_id(thermal_dbg);
+               return;
+       }
+
+       /*
+        * Trip index '-1' means no mitigation
+        */
+       tz_dbg->trip_index = -1;

+ tz_dbg->nr_trips = 0;

or it can be dropped even.

Quoted 51 lines
drivers/thermal/thermal_debugfs.c +21 -0
+       INIT_LIST_HEAD(&tz_dbg->tz_events);
+
+       debugfs_create_file("mitigations", 0400, thermal_dbg->d_top, tz, &tze_fops);
+
+       tz->debugfs = thermal_dbg;
+}
+
+void thermal_debug_tz_remove(struct thermal_zone_device *tz)
+{
+       struct thermal_debugfs *thermal_dbg = tz->debugfs;
+
+       if (!thermal_dbg)
+               return;
+
+       mutex_lock(&thermal_dbg->lock);
+
+       tz->debugfs = NULL;
+       thermal_debugfs_remove_id(thermal_dbg);
+
+       mutex_unlock(&thermal_dbg->lock);
+}
drivers/thermal/thermal_debugfs.h +14 -1
index 341499388448..155b9af5fe87 100644
@@ -5,10 +5,24 @@ void thermal_debug_init(void);
 void thermal_debug_cdev_add(struct thermal_cooling_device *cdev);
 void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev);
 void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev, int state);
+void thermal_debug_tz_add(struct thermal_zone_device *tz);
+void thermal_debug_tz_remove(struct thermal_zone_device *tz);
+void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
+                             const struct thermal_trip *trip);
+void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
+                               const struct thermal_trip *trip);
+void thermal_debug_update_temp(struct thermal_zone_device *tz);
 #else
 static inline void thermal_debug_init(void) {}
 static inline void thermal_debug_cdev_add(struct thermal_cooling_device *cdev) {}
 static inline void thermal_debug_cdev_remove(struct thermal_cooling_device *cdev) {}
 static inline void thermal_debug_cdev_state_update(const struct thermal_cooling_device *cdev,
                                                   int state) {}
+static inline void thermal_debug_tz_add(struct thermal_zone_device *tz) {}
+static inline void thermal_debug_tz_remove(struct thermal_zone_device *tz) {}
+static inline void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
+                                           const struct thermal_trip *trip) {};
+static inline void thermal_debug_tz_trip_down(struct thermal_zone_device *tz,
+                                             const struct thermal_trip *trip) {}
+static inline void thermal_debug_update_temp(struct thermal_zone_device *tz) {}
 #endif /* CONFIG_THERMAL_DEBUGFS */
--
#3 lore raw

Re: [PATCH v5 2/2] thermal/debugfs: Add thermal debugfs information for mitigation episodes

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

Hi Daniel,

kernel test robot noticed the following build warnings:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-debugfs-Add-thermal-debugfs-information-for-mitigation-episodes/20231227-184900
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
patch link:    https://lore.kernel.org/r/20231227104532.2671761-2-daniel.lezcano%40linaro.org
patch subject: [PATCH v5 2/2] thermal/debugfs: Add thermal debugfs information for mitigation episodes
config: riscv-randconfig-r081-20231229 (https://download.01.org/0day-ci/archive/20231230/[email protected]/config)
compiler: riscv64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231230/[email protected]/reproduce)

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

All warnings (new ones prefixed by >>):

drivers/thermal/thermal_debugfs.c:85: warning: expecting prototype for struct cdev_value. Prototype was for struct cdev_record instead drivers/thermal/thermal_debugfs.c:169: warning: Function parameter or struct member 'cdev_dbg' not described in 'thermal_debugfs'

>> drivers/thermal/thermal_debugfs.c:169: warning: Function parameter or struct member 'tz_dbg' not described in 'thermal_debugfs'
   drivers/thermal/thermal_debugfs.c:169: warning: Excess struct member 'cdev' description in 'thermal_debugfs'
>> drivers/thermal/thermal_debugfs.c:169: warning: Excess struct member 'tz' description in 'thermal_debugfs'

vim +169 drivers/thermal/thermal_debugfs.c

fafb2eabb846fd Daniel Lezcano 2023-12-27  149  
520a83215f818c Daniel Lezcano 2023-12-27  150  /**
520a83215f818c Daniel Lezcano 2023-12-27  151   * struct thermal_debugfs - High level structure for a thermal object in debugfs
520a83215f818c Daniel Lezcano 2023-12-27  152   *
520a83215f818c Daniel Lezcano 2023-12-27  153   * The thermal_debugfs structure is the common structure used by the
fafb2eabb846fd Daniel Lezcano 2023-12-27  154   * cooling device or the thermal zone to store the statistics.
520a83215f818c Daniel Lezcano 2023-12-27  155   *
520a83215f818c Daniel Lezcano 2023-12-27  156   * @d_top: top directory of the thermal object directory
520a83215f818c Daniel Lezcano 2023-12-27  157   * @lock: per object lock to protect the internals
520a83215f818c Daniel Lezcano 2023-12-27  158   *
520a83215f818c Daniel Lezcano 2023-12-27  159   * @cdev: a cooling device debug structure
fafb2eabb846fd Daniel Lezcano 2023-12-27  160   * @tz: a thermal zone debug structure
520a83215f818c Daniel Lezcano 2023-12-27  161   */
520a83215f818c Daniel Lezcano 2023-12-27  162  struct thermal_debugfs {
520a83215f818c Daniel Lezcano 2023-12-27  163  	struct dentry *d_top;
520a83215f818c Daniel Lezcano 2023-12-27  164  	struct mutex lock;
520a83215f818c Daniel Lezcano 2023-12-27  165  	union {
520a83215f818c Daniel Lezcano 2023-12-27  166  		struct cdev_debugfs cdev_dbg;
fafb2eabb846fd Daniel Lezcano 2023-12-27  167  		struct tz_debugfs tz_dbg;
520a83215f818c Daniel Lezcano 2023-12-27  168  	};

520a83215f818c Daniel Lezcano 2023-12-27 @169 };
520a83215f818c Daniel Lezcano 2023-12-27 170

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to Daniel Lezcano
Quoted 58 lines · Daniel Lezcano

The thermal framework does not have any debug information except a sysfs stat which is a bit controversial. This one allocates big chunks of memory for every cooling devices with a high number of states and could represent on some systems in production several megabytes of memory for just a portion of it. As the sysfs is limited to a page size, the output is not exploitable with large data array and gets truncated.

The patch provides the same information than sysfs except the transitions are dynamically allocated, thus they won't show more events than the ones which actually occurred. There is no longer a size limitation and it opens the field for more debugging information where the debugfs is designed for, not sysfs.

The thermal debugfs directory structure tries to stay consistent with the sysfs one but in a very simplified way:

thermal/
-- cooling_devices

    |-- 0
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 1
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 2
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    |-- 3
    |   |-- clear
    |   |-- time_in_state_ms
    |   |-- total_trans
    |   `-- trans_table
    `-- 4
        |-- clear
        |-- time_in_state_ms
        |-- total_trans
        `-- trans_table

The content of the files in the cooling devices directory is the same as the sysfs one except for the trans_table which has the following format:

Transition      Hits
1->0            246
0->1            246
2->1            632
1->2            632
3->2            98
2->3            98
Quoted 5 lines

---
Changelog:
- v5
- Removed a spurious change in thermal_helper.c, missed to remove it in v4 (Rafael)

This one LGTM now.

I've only spotted a few very minor things that can be fixed up while applying the patch.

Quoted 41 lines

- v4
- Fixed kerneldoc description ordering (Rafael)
- Fixed comment (Rafael)
- Renamed s/cdev_value/cdev_record/ (Rafael)
- Used union instead of a common 'value' field in cdev_record (Rafael)
- Renamed s/cdev/cdev_dbg/ for clarity (Rafael)
- Renamed s/dfs/thermal_dbg/ for clarity (Rafael)
- Renamed s/list/lists/ in the place where there are array of lists (Rafael)
- Inverted initialization logic when allocating a cdev_record (Rafael)
- Moved now = ktime_get() closer to the place where it is used (Rafael)
- Moved two lines down to a condition (Rafael)
- Removed strange and unexpected addition of function (Rafael)
- v3
- Fixed kerneldoc description (kbuild)
- Made some functions static
- v2
- Added parameter names to fix kbuild report
- Renamed 'reset' to 'clear' to avoid confusion (Rafael)
- Fixed several typos and rephrased some sentences (Rafael)
- Renamed structure field name s/list/node/ (Rafael)
- Documented structures and exported functions (Rafael)
- s/trans_list/transitions/ (Rafael)
- s/duration_list/durations/ (Rafael)
- Folded 'alloc' and 'insert' into a single function (Rafael)
- s/list/lists/ as it is an array of lists (Rafael)
- s/pos/entry/ (Rafael)
- Introduced a locking in the 'clear' callback function (Rafael)
- s/to/new_state/ and s/from/old_state/ (Rafael)
- Added some comments in thermal_debug_cdev_transition() (Rafael)
- Explained why char[11] (Rafael)
- s/Hits/Occurrences/ (Rafael)
- s/Time/Residency/ (Rafael)
- Constified structure pointer passed to thermal_debug_cdev_transition()
- s/thermal_debug_cdev_transition()/thermal_debug_cdev_state_update()/
- v1 (from RFC):
- Fixed typo "occurred"
- Changed Kconfig option name and description
- Removed comment in the Makefile
- Renamed exported function name s/debugfs/debug/
- Replaced thermal_debug_cdev_[unregister|register] by [add|remove]
---

#5 lore raw
In reply to Daniel Lezcano
kernel test robot noticed the following build warnings:

Hi Daniel,

kernel test robot noticed the following build warnings:

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

url:    https://github.com/intel-lab-lkp/linux/commits/Daniel-Lezcano/thermal-debugfs-Add-thermal-debugfs-information-for-mitigation-episodes/20231227-184900
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
patch link:    https://lore.kernel.org/r/20231227104532.2671761-1-daniel.lezcano%40linaro.org
patch subject: [PATCH v5 1/2] thermal/debugfs: Add thermal cooling device debugfs information
config: riscv-randconfig-r081-20231229 (https://download.01.org/0day-ci/archive/20231229/[email protected]/config)
compiler: riscv64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231229/[email protected]/reproduce)

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

All warnings (new ones prefixed by >>):

drivers/thermal/thermal_debugfs.c:82: warning: expecting prototype for struct cdev_value. Prototype was for struct cdev_record instead

>> drivers/thermal/thermal_debugfs.c:101: warning: Function parameter or struct member 'cdev_dbg' not described in 'thermal_debugfs'
>> drivers/thermal/thermal_debugfs.c:101: warning: Excess struct member 'cdev' description in 'thermal_debugfs'

vim +101 drivers/thermal/thermal_debugfs.c

    83	
    84	/**
    85	 * struct thermal_debugfs - High level structure for a thermal object in debugfs
    86	 *
    87	 * The thermal_debugfs structure is the common structure used by the
    88	 * cooling device to compute the statistics.
    89	 *
    90	 * @d_top: top directory of the thermal object directory
    91	 * @lock: per object lock to protect the internals
    92	 *
    93	 * @cdev: a cooling device debug structure
    94	 */
    95	struct thermal_debugfs {
    96		struct dentry *d_top;
    97		struct mutex lock;
    98		union {
    99			struct cdev_debugfs cdev_dbg;
   100		};
 > 101	};

102

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki