2022-06-18 08:12:49

by Liang He

[permalink] [raw]
Subject: [PATCH] powerpc: perf: Fix refcount leak bug in imc-pmu.c

In update_events_in_group(), of_find_node_by_phandle() will return
a node pointer with refcount incremented. We should use of_node_put()
in fail path or when it is not used anymore.

Signed-off-by: Liang He <[email protected]>
---
arch/powerpc/perf/imc-pmu.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/arch/powerpc/perf/imc-pmu.c b/arch/powerpc/perf/imc-pmu.c
index d7976ab40d38..d517aba94d1b 100644
--- a/arch/powerpc/perf/imc-pmu.c
+++ b/arch/powerpc/perf/imc-pmu.c
@@ -240,8 +240,10 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
ct = of_get_child_count(pmu_events);

/* Get the event prefix */
- if (of_property_read_string(node, "events-prefix", &prefix))
+ if (of_property_read_string(node, "events-prefix", &prefix)) {
+ of_node_put(pmu_events);
return 0;
+ }

/* Get a global unit and scale data if available */
if (of_property_read_string(node, "scale", &g_scale))
@@ -255,8 +257,10 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)

/* Allocate memory for the events */
pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
- if (!pmu->events)
+ if (!pmu->events) {
+ of_node_put(pmu_events);
return -ENOMEM;
+ }

ct = 0;
/* Parse the events and update the struct */
@@ -266,6 +270,8 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
ct++;
}

+ of_node_put(pmu_events);
+
/* Allocate memory for attribute group */
attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
if (!attr_group) {
--
2.25.1


2022-06-27 09:09:34

by Athira Rajeev

[permalink] [raw]
Subject: Re: [PATCH] powerpc: perf: Fix refcount leak bug in imc-pmu.c



> On 18-Jun-2022, at 12:43 PM, Liang He <[email protected]> wrote:
>
> In update_events_in_group(), of_find_node_by_phandle() will return
> a node pointer with refcount incremented. We should use of_node_put()
> in fail path or when it is not used anymore.
>
> Signed-off-by: Liang He <[email protected]>

Reviewed-by: Athira Rajeev <[email protected]>
> ---
> arch/powerpc/perf/imc-pmu.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/arch/powerpc/perf/imc-pmu.c b/arch/powerpc/perf/imc-pmu.c
> index d7976ab40d38..d517aba94d1b 100644
> --- a/arch/powerpc/perf/imc-pmu.c
> +++ b/arch/powerpc/perf/imc-pmu.c
> @@ -240,8 +240,10 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
> ct = of_get_child_count(pmu_events);
>
> /* Get the event prefix */
> - if (of_property_read_string(node, "events-prefix", &prefix))
> + if (of_property_read_string(node, "events-prefix", &prefix)) {
> + of_node_put(pmu_events);
> return 0;
> + }
>
> /* Get a global unit and scale data if available */
> if (of_property_read_string(node, "scale", &g_scale))
> @@ -255,8 +257,10 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
>
> /* Allocate memory for the events */
> pmu->events = kcalloc(ct, sizeof(struct imc_events), GFP_KERNEL);
> - if (!pmu->events)
> + if (!pmu->events) {
> + of_node_put(pmu_events);
> return -ENOMEM;
> + }
>
> ct = 0;
> /* Parse the events and update the struct */
> @@ -266,6 +270,8 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)
> ct++;
> }
>
> + of_node_put(pmu_events);
> +
> /* Allocate memory for attribute group */
> attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
> if (!attr_group) {
> --
> 2.25.1
>

2022-09-09 12:46:36

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] powerpc: perf: Fix refcount leak bug in imc-pmu.c

On Sat, 18 Jun 2022 15:13:53 +0800, Liang He wrote:
> In update_events_in_group(), of_find_node_by_phandle() will return
> a node pointer with refcount incremented. We should use of_node_put()
> in fail path or when it is not used anymore.
>
>

Applied to powerpc/next.

[1/1] powerpc: perf: Fix refcount leak bug in imc-pmu.c
https://git.kernel.org/powerpc/c/0dd8d2c8066e672244975c171816fdd9dae87721

cheers