Skip to contentSkip to conversation

thermal: netlink: Improve the initcall ordering

PATCH In review Daniel Lezcano 2 participants 4 messages Jul 17, 2020 – Jul 20, 2020 Thermal

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 thermal: netlink: Improve the initcall ordering

The initcalls like to play joke. In our case, the thermal-netlink initcall is called after the thermal-core initcall but this one sends a notification before the former is initialzed. No issue was spotted, but it could lead to a memory corruption, so instead of relying on the core_initcall for the thermal-netlink, let's initialize directly from the thermal-core init routine, so we have full control of the init ordering.

---
 drivers/thermal/thermal_core.c    | 4 ++++
 drivers/thermal/thermal_netlink.c | 3 +--
 drivers/thermal/thermal_netlink.h | 6 ++++++
 3 files changed, 11 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +4 -0
index 25ef29123f72..c2e7d7aaa354 100644
@@ -1581,6 +1581,10 @@ static int __init thermal_init(void)
 {
 	int result;
 
+	result = thermal_netlink_init();
+	if (result)
+		goto error;
+
 	mutex_init(&poweroff_lock);
 	result = thermal_register_governors();
 	if (result)
drivers/thermal/thermal_netlink.c +1 -2
index dd0a3b889674..42eace7401da 100644
@@ -641,8 +641,7 @@ static struct genl_family thermal_gnl_family __ro_after_init = {
 	.n_mcgrps	= ARRAY_SIZE(thermal_genl_mcgrps),
 };
 
-static int __init thermal_netlink_init(void)
+int __init thermal_netlink_init(void)
 {
 	return genl_register_family(&thermal_gnl_family);
 }
-core_initcall(thermal_netlink_init);
drivers/thermal/thermal_netlink.h +6 -0
index 0ec28d105da5..828d1dddfa98 100644
@@ -6,6 +6,7 @@
 
 /* Netlink notification function */
 #ifdef CONFIG_THERMAL_NETLINK
+int __init thermal_netlink_init(void);
 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);
@@ -23,6 +24,11 @@ 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);
 #else
+static inline int thermal_netlink_init(void)
+{
+	return 0;
+}
+
 static inline int thermal_notify_tz_create(int tz_id, const char *name)
 {
 	return 0;
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 thermal: core: Move initialization after core initcall

The generic netlink is initialized at subsys_initcall, so far after the thermal init routine and the thermal generic netlink family initialization.

On ŝome platforms, that leads to a memory corruption.

The fix was sent to netdev@ to move the genetlink framework initialization at core_initcall.

Move the thermal core initialization to postcore level which is very close to core level.

---
 drivers/thermal/thermal_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thermal/thermal_core.c +1 -1
index c2e7d7aaa354..79551bb6cd4c 100644
@@ -1617,4 +1617,4 @@ static int __init thermal_init(void)
 	mutex_destroy(&poweroff_lock);
 	return result;
 }
-core_initcall(thermal_init);
+postcore_initcall(thermal_init);
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] thermal: core: Move initialization after core initcall

Quoted 25 lines · Daniel Lezcano

The generic netlink is initialized at subsys_initcall, so far after the thermal init routine and the thermal generic netlink family initialization.

On ŝome platforms, that leads to a memory corruption.

The fix was sent to netdev@ to move the genetlink framework initialization at core_initcall.

Move the thermal core initialization to postcore level which is very close to core level.

---
 drivers/thermal/thermal_core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/thermal/thermal_core.c +0 -0
index c2e7d7aaa354..79551bb6cd4c 100644

drivers/thermal/thermal_core.c static int __init thermal_init(void) · line 1620

Quoted 5 lines
drivers/thermal/thermal_core.c +1 -1
@@ -1617,4 +1617,4 @@ static int __init thermal_init(void)
        mutex_destroy(&poweroff_lock);
        return result;
 }
-core_initcall(thermal_init);
+postcore_initcall(thermal_init);

For posterity, we moved to core_initcall from fs_initcall since we had removed netlink support from the thermal framework and we wanted to initialise it as quickly as possible after cpufreq to allow early mitigation possibility.

Given the addition of the new netlink-based API to thermal framework, I think moving to postcore_initcall is an acceptable compromise.

Quoted 3 lines

--
2.17.1

Reviewed-by on v1 1/2
#3 lore raw
In reply to Daniel Lezcano
Quoted 4 lines · Daniel Lezcano

The initcalls like to play joke. In our case, the thermal-netlink initcall is called after the thermal-core initcall but this one sends a notification before the former is initialzed. No issue was spotted,

typo: initialized

Quoted 8 lines

but it could lead to a memory corruption, so instead of relying on the core_initcall for the thermal-netlink, let's initialize directly from the thermal-core init routine, so we have full control of the init ordering.

Quoted 62 lines
---
 drivers/thermal/thermal_core.c    | 4 ++++
 drivers/thermal/thermal_netlink.c | 3 +--
 drivers/thermal/thermal_netlink.h | 6 ++++++
 3 files changed, 11 insertions(+), 2 deletions(-)
drivers/thermal/thermal_core.c +4 -0
index 25ef29123f72..c2e7d7aaa354 100644
@@ -1581,6 +1581,10 @@ static int __init thermal_init(void)
 {
        int result;
 
+       result = thermal_netlink_init();
+       if (result)
+               goto error;
+
        mutex_init(&poweroff_lock);
        result = thermal_register_governors();
        if (result)
drivers/thermal/thermal_netlink.c +1 -2
index dd0a3b889674..42eace7401da 100644
@@ -641,8 +641,7 @@ static struct genl_family thermal_gnl_family __ro_after_init = {
        .n_mcgrps       = ARRAY_SIZE(thermal_genl_mcgrps),
 };
 
-static int __init thermal_netlink_init(void)
+int __init thermal_netlink_init(void)
 {
        return genl_register_family(&thermal_gnl_family);
 }
-core_initcall(thermal_netlink_init);
drivers/thermal/thermal_netlink.h +6 -1
index 0ec28d105da5..828d1dddfa98 100644
@@ -6,6 +6,7 @@
 
 /* Netlink notification function */
 #ifdef CONFIG_THERMAL_NETLINK
+int __init thermal_netlink_init(void);
 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);
@@ -23,6 +24,11 @@ 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);
 #else
+static inline int thermal_netlink_init(void)
+{
+       return 0;
+}
+
 static inline int thermal_notify_tz_create(int tz_id, const char *name)
 {
        return 0;
--

2.17.1