2024-04-23 08:28:09

by Javier Carrasco

[permalink] [raw]
Subject: [PATCH 0/2] cpufreq: dt/dt-platdev: eliminate uses of of_node_put()

This series removes uses of of_node_put() in dt and dt-platdev, which
can be replaced with the clenaup handler introduced with
54da6a092431 ("locking: Introduce __cleanup() based infrastructure").

This change provides a scope-based cleanup mechanism to avoid potential
memory leaks that can appear if of_node_put() is not used correctly.

The series is based on linux-next (next-20240422) and has been validated
with a Rockchip RK3568 that makes use of these drivers.

Signed-off-by: Javier Carrasco <[email protected]>
---
Javier Carrasco (2):
cpufreq: dt: eliminate uses of of_node_put()
cpufreq: dt-platdev: eliminate uses of of_node_put()

drivers/cpufreq/cpufreq-dt-platdev.c | 7 ++-----
drivers/cpufreq/cpufreq-dt.c | 21 ++++++---------------
2 files changed, 8 insertions(+), 20 deletions(-)
---
base-commit: f529a6d274b3b8c75899e949649d231298f30a32
change-id: 20240422-of_node_put_cpufreq_dt-9c5354d9e9f0

Best regards,
--
Javier Carrasco <[email protected]>



2024-04-23 08:28:24

by Javier Carrasco

[permalink] [raw]
Subject: [PATCH 1/2] cpufreq: dt: eliminate uses of of_node_put()

Make use of the __free() cleanup handler to automatically free nodes
when they get out of scope.

Only find_supply_name() is affected, and the new mechanism removes the
need for a 'goto' and the 'name' local variable.

Signed-off-by: Javier Carrasco <[email protected]>
---
drivers/cpufreq/cpufreq-dt.c | 21 ++++++---------------
1 file changed, 6 insertions(+), 15 deletions(-)

diff --git a/drivers/cpufreq/cpufreq-dt.c b/drivers/cpufreq/cpufreq-dt.c
index 2d83bbc65dd0..907e22632fda 100644
--- a/drivers/cpufreq/cpufreq-dt.c
+++ b/drivers/cpufreq/cpufreq-dt.c
@@ -68,12 +68,9 @@ static int set_target(struct cpufreq_policy *policy, unsigned int index)
*/
static const char *find_supply_name(struct device *dev)
{
- struct device_node *np;
+ struct device_node *np __free(device_node) = of_node_get(dev->of_node);
struct property *pp;
int cpu = dev->id;
- const char *name = NULL;
-
- np = of_node_get(dev->of_node);

/* This must be valid for sure */
if (WARN_ON(!np))
@@ -82,22 +79,16 @@ static const char *find_supply_name(struct device *dev)
/* Try "cpu0" for older DTs */
if (!cpu) {
pp = of_find_property(np, "cpu0-supply", NULL);
- if (pp) {
- name = "cpu0";
- goto node_put;
- }
+ if (pp)
+ return "cpu0";
}

pp = of_find_property(np, "cpu-supply", NULL);
- if (pp) {
- name = "cpu";
- goto node_put;
- }
+ if (pp)
+ return "cpu";

dev_dbg(dev, "no regulator for cpu%d\n", cpu);
-node_put:
- of_node_put(np);
- return name;
+ return NULL;
}

static int cpufreq_init(struct cpufreq_policy *policy)

--
2.40.1


2024-04-23 08:28:28

by Javier Carrasco

[permalink] [raw]
Subject: [PATCH 2/2] cpufreq: dt-platdev: eliminate uses of of_node_put()

Make use of the __free() cleanup handler to automatically free nodes
when they get out of scope.

Signed-off-by: Javier Carrasco <[email protected]>
---
drivers/cpufreq/cpufreq-dt-platdev.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/cpufreq/cpufreq-dt-platdev.c b/drivers/cpufreq/cpufreq-dt-platdev.c
index 86d8baa81679..c74dd1e01e0d 100644
--- a/drivers/cpufreq/cpufreq-dt-platdev.c
+++ b/drivers/cpufreq/cpufreq-dt-platdev.c
@@ -198,19 +198,18 @@ static const struct of_device_id blocklist[] __initconst = {

static bool __init cpu0_node_has_opp_v2_prop(void)
{
- struct device_node *np = of_cpu_device_node_get(0);
+ struct device_node *np __free(device_node) = of_cpu_device_node_get(0);
bool ret = false;

if (of_property_present(np, "operating-points-v2"))
ret = true;

- of_node_put(np);
return ret;
}

static int __init cpufreq_dt_platdev_init(void)
{
- struct device_node *np = of_find_node_by_path("/");
+ struct device_node *np __free(device_node) = of_find_node_by_path("/");
const struct of_device_id *match;
const void *data = NULL;

@@ -226,11 +225,9 @@ static int __init cpufreq_dt_platdev_init(void)
if (cpu0_node_has_opp_v2_prop() && !of_match_node(blocklist, np))
goto create_pdev;

- of_node_put(np);
return -ENODEV;

create_pdev:
- of_node_put(np);
return PTR_ERR_OR_ZERO(platform_device_register_data(NULL, "cpufreq-dt",
-1, data,
sizeof(struct cpufreq_dt_platform_data)));

--
2.40.1


2024-04-23 09:30:52

by Viresh Kumar

[permalink] [raw]
Subject: Re: [PATCH 0/2] cpufreq: dt/dt-platdev: eliminate uses of of_node_put()

On 23-04-24, 10:27, Javier Carrasco wrote:
> This series removes uses of of_node_put() in dt and dt-platdev, which
> can be replaced with the clenaup handler introduced with
> 54da6a092431 ("locking: Introduce __cleanup() based infrastructure").
>
> This change provides a scope-based cleanup mechanism to avoid potential
> memory leaks that can appear if of_node_put() is not used correctly.
>
> The series is based on linux-next (next-20240422) and has been validated
> with a Rockchip RK3568 that makes use of these drivers.
>
> Signed-off-by: Javier Carrasco <[email protected]>
> ---
> Javier Carrasco (2):
> cpufreq: dt: eliminate uses of of_node_put()
> cpufreq: dt-platdev: eliminate uses of of_node_put()
>
> drivers/cpufreq/cpufreq-dt-platdev.c | 7 ++-----
> drivers/cpufreq/cpufreq-dt.c | 21 ++++++---------------
> 2 files changed, 8 insertions(+), 20 deletions(-)

Applied. Thanks.

--
viresh