2023-04-18 08:34:58

by Daniel Lezcano

[permalink] [raw]
Subject: [PATCH v1 0/3] Move the thermal zone device structure in private header

The following series applies on top of the changes:

https://lore.kernel.org/all/[email protected]/

There is no functional change, only code reordering and self-encapsulation.

The series adds a macro check in thermal_core.h in order to warn in
case a driver is including the thermal core private headers.

Then the thermal zone device structure is moved from the exported
thermal.h header to the thermal_core.h

Daniel Lezcano (3):
thermal/core: Hardening the self-encapsulation
thermal/core: Reorder the headers inclusion
thermal/core: Move the thermal zone structure to the private core
header

drivers/thermal/gov_bang_bang.c | 1 +
drivers/thermal/gov_fair_share.c | 1 +
drivers/thermal/gov_power_allocator.c | 7 ++-
drivers/thermal/gov_step_wise.c | 1 +
drivers/thermal/gov_user_space.c | 1 +
drivers/thermal/thermal_acpi.c | 1 +
drivers/thermal/thermal_core.c | 7 ++-
drivers/thermal/thermal_core.h | 79 +++++++++++++++++++++++++++
drivers/thermal/thermal_helpers.c | 1 +
drivers/thermal/thermal_hwmon.c | 1 +
drivers/thermal/thermal_netlink.c | 1 +
drivers/thermal/thermal_of.c | 1 +
drivers/thermal/thermal_sysfs.c | 1 +
drivers/thermal/thermal_trip.c | 1 +
include/linux/thermal.h | 75 -------------------------
15 files changed, 98 insertions(+), 81 deletions(-)

--
2.34.1


2023-04-18 08:35:08

by Daniel Lezcano

[permalink] [raw]
Subject: [PATCH v1 3/3] thermal/core: Move the thermal zone structure to the private core header

The code in the different drivers have been sanitized and they no
longer access the thermal zone structure internals. This one is just a
pointer passed around to the thermal core code as a handler.

Move the structure definition to the private core code header to
self-encapsulate the core code.

Signed-off-by: Daniel Lezcano <[email protected]>
---
drivers/thermal/thermal_core.h | 75 ++++++++++++++++++++++++++++++++++
include/linux/thermal.h | 75 ----------------------------------
2 files changed, 75 insertions(+), 75 deletions(-)

diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index feb02c48beba..e55d1eaa8bdb 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -18,6 +18,81 @@
#warning This header can only be included by the thermal core code
#endif

+/**
+ * struct thermal_zone_device - structure for a thermal zone
+ * @id: unique id number for each thermal zone
+ * @type: the thermal zone device type
+ * @device: &struct device for this thermal zone
+ * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
+ * @trip_type_attrs: attributes for trip points for sysfs: trip type
+ * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
+ * @mode: current mode of this thermal zone
+ * @devdata: private pointer for device private data
+ * @trips: an array of struct thermal_trip
+ * @num_trips: number of trip points the thermal zone supports
+ * @trips_disabled; bitmap for disabled trips
+ * @passive_delay_jiffies: number of jiffies to wait between polls when
+ * performing passive cooling.
+ * @polling_delay_jiffies: number of jiffies to wait between polls when
+ * checking whether trip points have been crossed (0 for
+ * interrupt driven systems)
+ * @temperature: current temperature. This is only for core code,
+ * drivers should use thermal_zone_get_temp() to get the
+ * current temperature
+ * @last_temperature: previous temperature read
+ * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
+ * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
+ * @prev_low_trip: the low current temperature if you've crossed a passive
+ trip point.
+ * @prev_high_trip: the above current temperature if you've crossed a
+ passive trip point.
+ * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
+ * @ops: operations this &thermal_zone_device supports
+ * @tzp: thermal zone parameters
+ * @governor: pointer to the governor for this thermal zone
+ * @governor_data: private pointer for governor data
+ * @thermal_instances: list of &struct thermal_instance of this thermal zone
+ * @ida: &struct ida to generate unique id for this zone's cooling
+ * devices
+ * @lock: lock to protect thermal_instances list
+ * @node: node in thermal_tz_list (in thermal_core.c)
+ * @poll_queue: delayed work for polling
+ * @notify_event: Last notification event
+ */
+struct thermal_zone_device {
+ int id;
+ char type[THERMAL_NAME_LENGTH];
+ struct device device;
+ struct attribute_group trips_attribute_group;
+ struct thermal_attr *trip_temp_attrs;
+ struct thermal_attr *trip_type_attrs;
+ struct thermal_attr *trip_hyst_attrs;
+ enum thermal_device_mode mode;
+ void *devdata;
+ struct thermal_trip *trips;
+ int num_trips;
+ unsigned long trips_disabled; /* bitmap for disabled trips */
+ unsigned long passive_delay_jiffies;
+ unsigned long polling_delay_jiffies;
+ int temperature;
+ int last_temperature;
+ int emul_temperature;
+ int passive;
+ int prev_low_trip;
+ int prev_high_trip;
+ atomic_t need_update;
+ struct thermal_zone_device_ops *ops;
+ struct thermal_zone_params *tzp;
+ struct thermal_governor *governor;
+ void *governor_data;
+ struct list_head thermal_instances;
+ struct ida ida;
+ struct mutex lock;
+ struct list_head node;
+ struct delayed_work poll_queue;
+ enum thermal_notify_event notify_event;
+};
+
/* Default Thermal Governor */
#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
#define DEFAULT_THERMAL_GOVERNOR "step_wise"
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 87837094d549..3e8bedb21755 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -112,81 +112,6 @@ struct thermal_cooling_device {
struct list_head node;
};

-/**
- * struct thermal_zone_device - structure for a thermal zone
- * @id: unique id number for each thermal zone
- * @type: the thermal zone device type
- * @device: &struct device for this thermal zone
- * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
- * @trip_type_attrs: attributes for trip points for sysfs: trip type
- * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
- * @mode: current mode of this thermal zone
- * @devdata: private pointer for device private data
- * @trips: an array of struct thermal_trip
- * @num_trips: number of trip points the thermal zone supports
- * @trips_disabled; bitmap for disabled trips
- * @passive_delay_jiffies: number of jiffies to wait between polls when
- * performing passive cooling.
- * @polling_delay_jiffies: number of jiffies to wait between polls when
- * checking whether trip points have been crossed (0 for
- * interrupt driven systems)
- * @temperature: current temperature. This is only for core code,
- * drivers should use thermal_zone_get_temp() to get the
- * current temperature
- * @last_temperature: previous temperature read
- * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
- * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
- * @prev_low_trip: the low current temperature if you've crossed a passive
- trip point.
- * @prev_high_trip: the above current temperature if you've crossed a
- passive trip point.
- * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
- * @ops: operations this &thermal_zone_device supports
- * @tzp: thermal zone parameters
- * @governor: pointer to the governor for this thermal zone
- * @governor_data: private pointer for governor data
- * @thermal_instances: list of &struct thermal_instance of this thermal zone
- * @ida: &struct ida to generate unique id for this zone's cooling
- * devices
- * @lock: lock to protect thermal_instances list
- * @node: node in thermal_tz_list (in thermal_core.c)
- * @poll_queue: delayed work for polling
- * @notify_event: Last notification event
- */
-struct thermal_zone_device {
- int id;
- char type[THERMAL_NAME_LENGTH];
- struct device device;
- struct attribute_group trips_attribute_group;
- struct thermal_attr *trip_temp_attrs;
- struct thermal_attr *trip_type_attrs;
- struct thermal_attr *trip_hyst_attrs;
- enum thermal_device_mode mode;
- void *devdata;
- struct thermal_trip *trips;
- int num_trips;
- unsigned long trips_disabled; /* bitmap for disabled trips */
- unsigned long passive_delay_jiffies;
- unsigned long polling_delay_jiffies;
- int temperature;
- int last_temperature;
- int emul_temperature;
- int passive;
- int prev_low_trip;
- int prev_high_trip;
- atomic_t need_update;
- struct thermal_zone_device_ops *ops;
- struct thermal_zone_params *tzp;
- struct thermal_governor *governor;
- void *governor_data;
- struct list_head thermal_instances;
- struct ida ida;
- struct mutex lock;
- struct list_head node;
- struct delayed_work poll_queue;
- enum thermal_notify_event notify_event;
-};
-
/**
* struct thermal_governor - structure that holds thermal governor information
* @name: name of the governor
--
2.34.1

2023-04-18 08:35:11

by Daniel Lezcano

[permalink] [raw]
Subject: [PATCH v1 1/3] thermal/core: Hardening the self-encapsulation

The thermal private header has leaked all around the drivers which
interacted with the core internals. The thermal zone structure which
was part of the exported header led also to a leakage of the fields
into the different drivers, making very difficult to improve the core
code without having to change the drivers.

Now we fixed how the thermal drivers were interacting with the thermal
zones (actually fixed how they should not interact). The thermal zone
structure has been moved to the private thermal core header. This
header has been removed from the different drivers and must belong to
the core code only. In order to prevent this private header to be
included again in the drivers, make explicit only the core code can
include this header by defining a THERMAL_CORE_SUBSYS macro. The
private header will contain a check against this macro.

The Tegra SoCtherm driver needs to access thermal_core.h to have the
get_thermal_instance() function definition. It is the only one
remaining driver which need to access the thermal_core.h header, so
the check will emit a warning at compilation time.

Thierry Reding is reworking the driver to get rid of this function [1]
and thus when the changes will be merged, the compilation warning will
be converted to a compilation error, closing definitively the door to
the drivers willing to play with the thermal zone device internals.

[1] https://lore.kernel.org/all/[email protected]/

Cc: Thierry Reding <[email protected]>
Signed-off-by: Daniel Lezcano <[email protected]>
---
drivers/thermal/gov_bang_bang.c | 1 +
drivers/thermal/gov_fair_share.c | 1 +
drivers/thermal/gov_power_allocator.c | 1 +
drivers/thermal/gov_step_wise.c | 1 +
drivers/thermal/gov_user_space.c | 1 +
drivers/thermal/thermal_acpi.c | 1 +
drivers/thermal/thermal_core.c | 1 +
drivers/thermal/thermal_core.h | 4 ++++
drivers/thermal/thermal_helpers.c | 1 +
drivers/thermal/thermal_hwmon.c | 1 +
drivers/thermal/thermal_netlink.c | 1 +
drivers/thermal/thermal_of.c | 1 +
drivers/thermal/thermal_sysfs.c | 1 +
drivers/thermal/thermal_trip.c | 1 +
14 files changed, 17 insertions(+)

diff --git a/drivers/thermal/gov_bang_bang.c b/drivers/thermal/gov_bang_bang.c
index 1b121066521f..752c627075ba 100644
--- a/drivers/thermal/gov_bang_bang.c
+++ b/drivers/thermal/gov_bang_bang.c
@@ -11,6 +11,7 @@

#include <linux/thermal.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

static int thermal_zone_trip_update(struct thermal_zone_device *tz, int trip_id)
diff --git a/drivers/thermal/gov_fair_share.c b/drivers/thermal/gov_fair_share.c
index 03c2daeb6ee8..108cb5074594 100644
--- a/drivers/thermal/gov_fair_share.c
+++ b/drivers/thermal/gov_fair_share.c
@@ -13,6 +13,7 @@
#include <linux/thermal.h>
#include "thermal_trace.h"

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/**
diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
index 8642f1096b91..d1c6ad92e5b4 100644
--- a/drivers/thermal/gov_power_allocator.c
+++ b/drivers/thermal/gov_power_allocator.c
@@ -14,6 +14,7 @@
#define CREATE_TRACE_POINTS
#include "thermal_trace_ipa.h"

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

#define INVALID_TRIP -1
diff --git a/drivers/thermal/gov_step_wise.c b/drivers/thermal/gov_step_wise.c
index 3d3067804df2..bfc9adf882d6 100644
--- a/drivers/thermal/gov_step_wise.c
+++ b/drivers/thermal/gov_step_wise.c
@@ -14,6 +14,7 @@
#include <linux/minmax.h>
#include "thermal_trace.h"

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/*
diff --git a/drivers/thermal/gov_user_space.c b/drivers/thermal/gov_user_space.c
index 8bc1c22aaf03..8883c9ca930f 100644
--- a/drivers/thermal/gov_user_space.c
+++ b/drivers/thermal/gov_user_space.c
@@ -13,6 +13,7 @@
#include <linux/slab.h>
#include <linux/thermal.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

static int user_space_bind(struct thermal_zone_device *tz)
diff --git a/drivers/thermal/thermal_acpi.c b/drivers/thermal/thermal_acpi.c
index 0e5698818f69..556c9f0cc40d 100644
--- a/drivers/thermal/thermal_acpi.c
+++ b/drivers/thermal/thermal_acpi.c
@@ -9,6 +9,7 @@
#include <linux/acpi.h>
#include <linux/units.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/*
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 842f678c1c3e..6bca97e27d59 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -24,6 +24,7 @@
#define CREATE_TRACE_POINTS
#include "thermal_trace.h"

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"
#include "thermal_hwmon.h"

diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h
index 5eacbcd10a40..feb02c48beba 100644
--- a/drivers/thermal/thermal_core.h
+++ b/drivers/thermal/thermal_core.h
@@ -14,6 +14,10 @@

#include "thermal_netlink.h"

+#ifndef THERMAL_CORE_SUBSYS
+#warning This header can only be included by the thermal core code
+#endif
+
/* Default Thermal Governor */
#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
#define DEFAULT_THERMAL_GOVERNOR "step_wise"
diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c
index cfba0965a22d..164c4627949e 100644
--- a/drivers/thermal/thermal_helpers.c
+++ b/drivers/thermal/thermal_helpers.c
@@ -19,6 +19,7 @@
#include <linux/string.h>
#include <linux/sysfs.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"
#include "thermal_trace.h"

diff --git a/drivers/thermal/thermal_hwmon.c b/drivers/thermal/thermal_hwmon.c
index fbe55509e307..3401258e55c6 100644
--- a/drivers/thermal/thermal_hwmon.c
+++ b/drivers/thermal/thermal_hwmon.c
@@ -17,6 +17,7 @@
#include <linux/thermal.h>

#include "thermal_hwmon.h"
+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/* hwmon sys I/F */
diff --git a/drivers/thermal/thermal_netlink.c b/drivers/thermal/thermal_netlink.c
index 08bc46c3ec7b..f3ac6432bf5f 100644
--- a/drivers/thermal/thermal_netlink.c
+++ b/drivers/thermal/thermal_netlink.c
@@ -11,6 +11,7 @@
#include <net/genetlink.h>
#include <uapi/linux/thermal.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

static const struct genl_multicast_group thermal_genl_mcgrps[] = {
diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 33c5be929ad5..40faabb59183 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -17,6 +17,7 @@
#include <linux/types.h>
#include <linux/string.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/*** functions parsing device tree nodes ***/
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 7ad1e40397e3..7fb24d1e2fad 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -19,6 +19,7 @@
#include <linux/string.h>
#include <linux/jiffies.h>

+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

/* sys I/F for thermal zone */
diff --git a/drivers/thermal/thermal_trip.c b/drivers/thermal/thermal_trip.c
index e1539e1d96e6..a12980a8bac5 100644
--- a/drivers/thermal/thermal_trip.c
+++ b/drivers/thermal/thermal_trip.c
@@ -7,6 +7,7 @@
*
* Thermal trips handling
*/
+#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

static const char * const trip_types[] = {
--
2.34.1

2023-04-18 08:35:53

by Daniel Lezcano

[permalink] [raw]
Subject: [PATCH v1 2/3] thermal/core: Reorder the headers inclusion

The next changes will move the thermal device structure inside the
thermal core code. Consequently, the traces must be included after
thermal_core.h as this one contains the thermal zone device structure
definition the traces need.

Reorder the inclusions.

Signed-off-by: Daniel Lezcano <[email protected]>
---
drivers/thermal/gov_power_allocator.c | 6 +++---
drivers/thermal/thermal_core.c | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
index d1c6ad92e5b4..6056ed15460b 100644
--- a/drivers/thermal/gov_power_allocator.c
+++ b/drivers/thermal/gov_power_allocator.c
@@ -11,12 +11,12 @@
#include <linux/slab.h>
#include <linux/thermal.h>

-#define CREATE_TRACE_POINTS
-#include "thermal_trace_ipa.h"
-
#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"

+#define CREATE_TRACE_POINTS
+#include "thermal_trace_ipa.h"
+
#define INVALID_TRIP -1

#define FRAC_BITS 10
diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 6bca97e27d59..afcd4197babd 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -21,13 +21,13 @@
#include <linux/of.h>
#include <linux/suspend.h>

-#define CREATE_TRACE_POINTS
-#include "thermal_trace.h"
-
#define THERMAL_CORE_SUBSYS
#include "thermal_core.h"
#include "thermal_hwmon.h"

+#define CREATE_TRACE_POINTS
+#include "thermal_trace.h"
+
static DEFINE_IDA(thermal_tz_ida);
static DEFINE_IDA(thermal_cdev_ida);

--
2.34.1