[1/2] thermal: netlink: Improve the initcall ordering

Message ID 20200717164217.18819-1-daniel.lezcano@linaro.org
State Accepted
Commit d2a89b52839597c70746d9f46a21b1aab0a26c02
Headers show
Series
  • [1/2] thermal: netlink: Improve the initcall ordering
Related show

Commit Message

Daniel Lezcano July 17, 2020, 4:42 p.m.
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.

Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

---
 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(-)

-- 
2.17.1

Comments

Amit Kucheria July 20, 2020, 5:43 a.m. | #1
On Fri, Jul 17, 2020 at 10:12 PM Daniel Lezcano
<daniel.lezcano@linaro.org> wrote:
>

> 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

> 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.


> Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>

> Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>

> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>


Reviewed-by: Amit Kucheria <amit.kucheria@linaro.org>


> ---

>  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(-)

>

> diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c

> index 25ef29123f72..c2e7d7aaa354 100644

> --- a/drivers/thermal/thermal_core.c

> +++ b/drivers/thermal/thermal_core.c

> @@ -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)

> diff --git a/drivers/thermal/thermal_netlink.c b/drivers/thermal/thermal_netlink.c

> index dd0a3b889674..42eace7401da 100644

> --- a/drivers/thermal/thermal_netlink.c

> +++ b/drivers/thermal/thermal_netlink.c

> @@ -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);

> diff --git a/drivers/thermal/thermal_netlink.h b/drivers/thermal/thermal_netlink.h

> index 0ec28d105da5..828d1dddfa98 100644

> --- a/drivers/thermal/thermal_netlink.h

> +++ b/drivers/thermal/thermal_netlink.h

> @@ -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

>

Patch

diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c
index 25ef29123f72..c2e7d7aaa354 100644
--- a/drivers/thermal/thermal_core.c
+++ b/drivers/thermal/thermal_core.c
@@ -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)
diff --git a/drivers/thermal/thermal_netlink.c b/drivers/thermal/thermal_netlink.c
index dd0a3b889674..42eace7401da 100644
--- a/drivers/thermal/thermal_netlink.c
+++ b/drivers/thermal/thermal_netlink.c
@@ -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);
diff --git a/drivers/thermal/thermal_netlink.h b/drivers/thermal/thermal_netlink.h
index 0ec28d105da5..828d1dddfa98 100644
--- a/drivers/thermal/thermal_netlink.h
+++ b/drivers/thermal/thermal_netlink.h
@@ -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;