Skip to contentSkip to conversation

thermal/drivers/core: Add init section table for self-encapsulation

PATCH In review Daniel Lezcano 2 participants 5 messages Jun 12, 2019 – Jun 27, 2019

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

PATCH 1/2 thermal/drivers/core: Add init section table for self-encapsulation

Currently the governors are declared in their respective files but they export their [un]register functions which in turn call the [un]register governors core's functions. That implies a cyclic dependency which is not desirable. There is a way to self-encapsulate the governors by letting them to declare themselves in a __init section table.

Define the table in the asm generic linker description like the other tables and provide the specific macros to deal with.

---
 drivers/thermal/thermal_core.h    | 15 +++++++++++++++
 include/asm-generic/vmlinux.lds.h | 11 +++++++++++
 2 files changed, 26 insertions(+)
drivers/thermal/thermal_core.h +15 -0
index 0df190ed82a7..be901e84aa65 100644
@@ -15,6 +15,21 @@
 /* Initial state of a cooling device during binding */
 #define THERMAL_NO_TARGET -1UL
 
+/* Init section thermal table */
+extern struct thermal_governor *__governor_thermal_table[];
+extern struct thermal_governor *__governor_thermal_table_end[];
+
+#define THERMAL_TABLE_ENTRY(table, name)			\
+	(static typeof(name) *__thermal_table_entry_##name	\
+	__used __section(__##table##_thermal_table) = &name)
+
+#define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
+
+#define for_each_governor_table(__governor)		\
+	for (__governor = __governor_thermal_table;	\
+	     __governor < __governor_thermal_table_end;	\
+	     __governor++)
+
 /*
  * This structure is used to describe the behavior of
  * a certain cooling device on a certain trip point
include/asm-generic/vmlinux.lds.h +11 -0
index f8f6f04c4453..8312fdc2b2fa 100644
@@ -239,6 +239,16 @@
 #define ACPI_PROBE_TABLE(name)
 #endif
 
+#ifdef CONFIG_THERMAL
+#define THERMAL_TABLE(name)						\
+	. = ALIGN(8);							\
+	__##name##_thermal_table = .;					\
+	KEEP(*(__##name##_thermal_table))				\
+	__##name##_thermal_table_end = .;
+#else
+#define THERMAL_TABLE(name)
+#endif
+
 #define KERNEL_DTB()							\
 	STRUCT_ALIGN();							\
 	__dtb_start = .;						\
@@ -609,6 +619,7 @@
 	IRQCHIP_OF_MATCH_TABLE()					\
 	ACPI_PROBE_TABLE(irqchip)					\
 	ACPI_PROBE_TABLE(timer)						\
+	THERMAL_TABLE(governor)						\
 	EARLYCON_TABLE()						\
 	LSM_TABLE()
 
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 thermal/drivers/core: Use governor table to initialize

Now that the governor table is in place and the macro allows to browse the table, declare the governor so the entry is added in the governor table in the init section.

The [un]register_thermal_governors function does no longer need to use the exported [un]register thermal governor's specific function which in turn call the [un]register_thermal_governor. The governors are fully self-encapsulated.

The cyclic dependency is no longer needed, remove it.

---
 drivers/thermal/fair_share.c      | 12 +------
 drivers/thermal/gov_bang_bang.c   | 11 +------
 drivers/thermal/power_allocator.c | 11 +------
 drivers/thermal/step_wise.c       | 11 +------
 drivers/thermal/thermal_core.c    | 52 +++++++++++++++++--------------
 drivers/thermal/thermal_core.h    | 40 ------------------------
 drivers/thermal/user_space.c      | 12 +------
 7 files changed, 34 insertions(+), 115 deletions(-)
drivers/thermal/fair_share.c +1 -11
index d3469fbc5207..bda2afc63471 100644
@@ -129,14 +129,4 @@ static struct thermal_governor thermal_gov_fair_share = {
 	.name		= "fair_share",
 	.throttle	= fair_share_throttle,
 };
-
-int thermal_gov_fair_share_register(void)
-{
-	return thermal_register_governor(&thermal_gov_fair_share);
-}
-
-void thermal_gov_fair_share_unregister(void)
-{
-	thermal_unregister_governor(&thermal_gov_fair_share);
-}
-
+THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
drivers/thermal/gov_bang_bang.c +1 -10
index fc5e5057f0de..c5e19c7d63da 100644
@@ -126,13 +126,4 @@ static struct thermal_governor thermal_gov_bang_bang = {
 	.name		= "bang_bang",
 	.throttle	= bang_bang_control,
 };
-
-int thermal_gov_bang_bang_register(void)
-{
-	return thermal_register_governor(&thermal_gov_bang_bang);
-}
-
-void thermal_gov_bang_bang_unregister(void)
-{
-	thermal_unregister_governor(&thermal_gov_bang_bang);
-}
+THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
drivers/thermal/power_allocator.c +1 -10
index 3055f9a12a17..44636475b2a3 100644
@@ -651,13 +651,4 @@ static struct thermal_governor thermal_gov_power_allocator = {
 	.unbind_from_tz	= power_allocator_unbind,
 	.throttle	= power_allocator_throttle,
 };
-
-int thermal_gov_power_allocator_register(void)
-{
-	return thermal_register_governor(&thermal_gov_power_allocator);
-}
-
-void thermal_gov_power_allocator_unregister(void)
-{
-	thermal_unregister_governor(&thermal_gov_power_allocator);
-}
+THERMAL_GOVERNOR_DECLARE(thermal_gov_power_allocator);
drivers/thermal/step_wise.c +1 -10
index ee047ca43084..6cd251ab56fc 100644
@@ -218,13 +218,4 @@ static struct thermal_governor thermal_gov_step_wise = {
 	.name		= "step_wise",
 	.throttle	= step_wise_throttle,
 };
-
-int thermal_gov_step_wise_register(void)
-{
-	return thermal_register_governor(&thermal_gov_step_wise);
-}
-
-void thermal_gov_step_wise_unregister(void)
-{
-	thermal_unregister_governor(&thermal_gov_step_wise);
-}
+THERMAL_GOVERNOR_DECLARE(thermal_gov_step_wise);
drivers/thermal/thermal_core.c +29 -23

65 lines not on this page · raw message

drivers/thermal/thermal_core.h +0 -40
index be901e84aa65..cb423b97ef01 100644
@@ -89,46 +89,6 @@ thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
 				    unsigned long new_state) {}
 #endif /* CONFIG_THERMAL_STATISTICS */
 
-#ifdef CONFIG_THERMAL_GOV_STEP_WISE
-int thermal_gov_step_wise_register(void);
-void thermal_gov_step_wise_unregister(void);
-#else
-static inline int thermal_gov_step_wise_register(void) { return 0; }
-static inline void thermal_gov_step_wise_unregister(void) {}
-#endif /* CONFIG_THERMAL_GOV_STEP_WISE */
-
-#ifdef CONFIG_THERMAL_GOV_FAIR_SHARE
-int thermal_gov_fair_share_register(void);
-void thermal_gov_fair_share_unregister(void);
-#else
-static inline int thermal_gov_fair_share_register(void) { return 0; }
-static inline void thermal_gov_fair_share_unregister(void) {}
-#endif /* CONFIG_THERMAL_GOV_FAIR_SHARE */
-
-#ifdef CONFIG_THERMAL_GOV_BANG_BANG
-int thermal_gov_bang_bang_register(void);
-void thermal_gov_bang_bang_unregister(void);
-#else
-static inline int thermal_gov_bang_bang_register(void) { return 0; }
-static inline void thermal_gov_bang_bang_unregister(void) {}
-#endif /* CONFIG_THERMAL_GOV_BANG_BANG */
-
-#ifdef CONFIG_THERMAL_GOV_USER_SPACE
-int thermal_gov_user_space_register(void);
-void thermal_gov_user_space_unregister(void);
-#else
-static inline int thermal_gov_user_space_register(void) { return 0; }
-static inline void thermal_gov_user_space_unregister(void) {}
-#endif /* CONFIG_THERMAL_GOV_USER_SPACE */
-
-#ifdef CONFIG_THERMAL_GOV_POWER_ALLOCATOR
-int thermal_gov_power_allocator_register(void);
-void thermal_gov_power_allocator_unregister(void);
-#else
-static inline int thermal_gov_power_allocator_register(void) { return 0; }
-static inline void thermal_gov_power_allocator_unregister(void) {}
-#endif /* CONFIG_THERMAL_GOV_POWER_ALLOCATOR */
-
 /* device tree support */
 #ifdef CONFIG_THERMAL_OF
 int of_parse_thermal_zones(void);
drivers/thermal/user_space.c +1 -11
index 8e92a06ef48a..5fac99e5221d 100644
@@ -56,14 +56,4 @@ static struct thermal_governor thermal_gov_user_space = {
 	.name		= "user_space",
 	.throttle	= notify_user_space,
 };
-
-int thermal_gov_user_space_register(void)
-{
-	return thermal_register_governor(&thermal_gov_user_space);
-}
-
-void thermal_gov_user_space_unregister(void)
-{
-	thermal_unregister_governor(&thermal_gov_user_space);
-}
-
+THERMAL_GOVERNOR_DECLARE(thermal_gov_user_space);
Signature · 1 line
2.17.1
#2 lore raw
In reply to Daniel Lezcano

Any chance this patch gets merged for v5.4?

Thanks
-- Daniel

Quoted 72 lines · Daniel Lezcano

Currently the governors are declared in their respective files but they export their [un]register functions which in turn call the [un]register governors core's functions. That implies a cyclic dependency which is not desirable. There is a way to self-encapsulate the governors by letting them to declare themselves in a __init section table.

Define the table in the asm generic linker description like the other tables and provide the specific macros to deal with.

---
 drivers/thermal/thermal_core.h    | 15 +++++++++++++++
 include/asm-generic/vmlinux.lds.h | 11 +++++++++++
 2 files changed, 26 insertions(+)
drivers/thermal/thermal_core.h +15 -0
index 0df190ed82a7..be901e84aa65 100644
@@ -15,6 +15,21 @@
 /* Initial state of a cooling device during binding */
 #define THERMAL_NO_TARGET -1UL
 
+/* Init section thermal table */
+extern struct thermal_governor *__governor_thermal_table[];
+extern struct thermal_governor *__governor_thermal_table_end[];
+
+#define THERMAL_TABLE_ENTRY(table, name)			\
+	(static typeof(name) *__thermal_table_entry_##name	\
+	__used __section(__##table##_thermal_table) = &name)
+
+#define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
+
+#define for_each_governor_table(__governor)		\
+	for (__governor = __governor_thermal_table;	\
+	     __governor < __governor_thermal_table_end;	\
+	     __governor++)
+
 /*
  * This structure is used to describe the behavior of
  * a certain cooling device on a certain trip point
include/asm-generic/vmlinux.lds.h +11 -0
index f8f6f04c4453..8312fdc2b2fa 100644
@@ -239,6 +239,16 @@
 #define ACPI_PROBE_TABLE(name)
 #endif
 
+#ifdef CONFIG_THERMAL
+#define THERMAL_TABLE(name)						\
+	. = ALIGN(8);							\
+	__##name##_thermal_table = .;					\
+	KEEP(*(__##name##_thermal_table))				\
+	__##name##_thermal_table_end = .;
+#else
+#define THERMAL_TABLE(name)
+#endif
+
 #define KERNEL_DTB()							\
 	STRUCT_ALIGN();							\
 	__dtb_start = .;						\
@@ -609,6 +619,7 @@
 	IRQCHIP_OF_MATCH_TABLE()					\
 	ACPI_PROBE_TABLE(irqchip)					\
 	ACPI_PROBE_TABLE(timer)						\
+	THERMAL_TABLE(governor)						\
 	EARLYCON_TABLE()						\
 	LSM_TABLE()
 
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
#3 lore raw
Quoted 5 lines · Daniel Lezcano

Any chance this patch gets merged for v5.4?

Thanks
  -- Daniel

have you run compile test for the patch?
I got the following errors when compiling.

In file included from drivers/thermal/fair_share.c:16:0:
drivers/thermal/thermal_core.h:23:3: error: expected identifier or ‘(’ before ‘static’
  (static typeof(name) *__thermal_table_entry_##name \
   ^
drivers/thermal/thermal_core.h:26:40: note: in expansion of macro ‘THERMAL_TABLE_ENTRY’
 #define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
                                        ^ drivers/thermal/fair_share.c:120:1: note: in expansion of macro ‘THERMAL_GOVERNOR_DECLARE’ THERMAL_GOVERNOR_DECLARE(thermal_gov_fair_share);
 ^
drivers/thermal/fair_share.c:116:32: warning: ‘thermal_gov_fair_share’ defined but not used [-Wunused-variable]
 static struct thermal_governor thermal_gov_fair_share = { ^
make[2]: *** [drivers/thermal/fair_share.o] Error 1 make[2]: *** Waiting for unfinished jobs....
In file included from drivers/thermal/gov_bang_bang.c:14:0:
drivers/thermal/thermal_core.h:23:3: error: expected identifier or ‘(’ before ‘static’
  (static typeof(name) *__thermal_table_entry_##name \
   ^
drivers/thermal/thermal_core.h:26:40: note: in expansion of macro ‘THERMAL_TABLE_ENTRY’
 #define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
                                        ^ drivers/thermal/gov_bang_bang.c:119:1: note: in expansion of macro ‘THERMAL_GOVERNOR_DECLARE’ THERMAL_GOVERNOR_DECLARE(thermal_gov_bang_bang);
 ^
drivers/thermal/gov_bang_bang.c:115:32: warning:
‘thermal_gov_bang_bang’ defined but not used [-Wunused-variable] static struct thermal_governor thermal_gov_bang_bang = {
                                ^
make[2]: *** [drivers/thermal/gov_bang_bang.o] Error 1 make[1]: *** [drivers/thermal] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [drivers] Error 2

Fix the problem by removing the round brackets of THERMAL_TABLE_ENTRY(), and applied.

thanks,
rui

Quoted 96 lines · Daniel Lezcano
Quoted 94 lines

Currently the governors are declared in their respective files but they
export their [un]register functions which in turn call the [un]register
governors core's functions. That implies a cyclic dependency which is
not desirable. There is a way to self-encapsulate the governors by letting
them to declare themselves in a __init section table.

Define the table in the asm generic linker description like the other
tables and provide the specific macros to deal with.

---
 drivers/thermal/thermal_core.h    | 15 +++++++++++++++ include/asm-generic/vmlinux.lds.h | 11 +++++++++++

 2 files changed, 26 insertions(+)
drivers/thermal/thermal_core.h +0 -0
index 0df190ed82a7..be901e84aa65 100644
@@ -15,6 +15,21 @@
  /* Initial state of a cooling device during binding */
  #define THERMAL_NO_TARGET -1UL

drivers/thermal/thermal_core.h +6 -0
+/* Init section thermal table */
+extern struct thermal_governor *__governor_thermal_table[];
+extern struct thermal_governor *__governor_thermal_table_end[];
+
+#define THERMAL_TABLE_ENTRY(table, name)			\
+	(static typeof(name) *__thermal_table_entry_##name	

\

drivers/thermal/thermal_core.h +3 -0
+	__used __section(__##table##_thermal_table) = &name)
+
+#define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(

governor, name)

drivers/thermal/thermal_core.h +6 -0
+
+#define for_each_governor_table(__governor)		\
+	for (__governor = __governor_thermal_table;	\
+	     __governor < __governor_thermal_table_end;	\
+	     __governor++)
+

 /*
  * This structure is used to describe the behavior of
  * a certain cooling device on a certain trip point

include/asm- renamed from include/asm-generic/vmlinux.lds.h +0 -0

generic/vmlinux.lds.h
index f8f6f04c4453..8312fdc2b2fa 100644

include/asm-generic/vmlinux.lds.h +0 -0
@@ -239,6 +239,16 @@
  #define ACPI_PROBE_TABLE(name)
  #endif

include/asm-generic/vmlinux.lds.h +2 -0
+#ifdef CONFIG_THERMAL
+#define THERMAL_TABLE(name)					
	\
include/asm-generic/vmlinux.lds.h +1 -0
+	. = ALIGN(8);						
	\
include/asm-generic/vmlinux.lds.h +1 -0
+	__##name##_thermal_table = .;				
	\
include/asm-generic/vmlinux.lds.h +1 -0
+	KEEP(*(__##name##_thermal_table))				

\

include/asm-generic/vmlinux.lds.h +5 -0
+	__##name##_thermal_table_end = .;
+#else
+#define THERMAL_TABLE(name)
+#endif
+
 #define KERNEL_DTB()						
	\
 	STRUCT_ALIGN();						
	\

  __dtb_start = .;
\

include/asm-generic/vmlinux.lds.h +1 -0
@@ -609,6 +619,7 @@
  	IRQCHIP_OF_MATCH_TABLE()					
 \
  	ACPI_PROBE_TABLE(irqchip)					
 \
  	ACPI_PROBE_TABLE(timer)					
 	\
+	THERMAL_TABLE(governor)					
	\

  EARLYCON_TABLE()
\
  LSM_TABLE()

#4 lore raw
Quoted 9 lines · Zhang Rui
Quoted 5 lines · Daniel Lezcano

Any chance this patch gets merged for v5.4?

Thanks
  -- Daniel

have you run compile test for the patch?
I got the following errors when compiling.

Yes I did and also booted, changed the governor at runtime, etc ... I already got this error and fixed it. I probably forgot to fold or commit the fix ... :/

Quoted 10 lines

In file included from drivers/thermal/fair_share.c:16:0:
drivers/thermal/thermal_core.h:23:3: error: expected identifier or ‘(’ before ‘static’
  (static typeof(name) *__thermal_table_entry_##name \
   ^
drivers/thermal/thermal_core.h:26:40: note: in expansion of macro ‘THERMAL_TABLE_ENTRY’
 #define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
                                        ^

[ ... ]

Quoted 8 lines

                                ^
make[2]: *** [drivers/thermal/gov_bang_bang.o] Error 1 make[1]: *** [drivers/thermal] Error 2 make[1]: *** Waiting for unfinished jobs....
make: *** [drivers] Error 2

Fix the problem by removing the round brackets of THERMAL_TABLE_ENTRY(), and applied.

Ok, thanks for fixing it!

-- Daniel

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