[PATCH] powerpc/perf: Clean up IMC code with some code refactoring

From: Anju T Sudhakar
Date: Tue Oct 31 2017 - 07:44:46 EST


Factor out memory freeing part for attribute elements from imc_common_mem_free(),
also handle the memory leak for pmu->events in update_events_in_group().

Signed-off-by: Anju T Sudhakar <anju@xxxxxxxxxxxxxxxxxx>
---
arch/powerpc/perf/imc-pmu.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/arch/powerpc/perf/imc-pmu.c b/arch/powerpc/perf/imc-pmu.c
index 88126245881b..45623428f164 100644
--- a/arch/powerpc/perf/imc-pmu.c
+++ b/arch/powerpc/perf/imc-pmu.c
@@ -224,8 +224,10 @@ static int update_events_in_group(struct device_node *node, struct imc_pmu *pmu)

/* Allocate memory for attribute group */
attr_group = kzalloc(sizeof(*attr_group), GFP_KERNEL);
- if (!attr_group)
+ if (!attr_group) {
+ kfree(pmu->events);
return -ENOMEM;
+ }

/*
* Allocate memory for attributes.
@@ -1143,6 +1145,15 @@ static void cleanup_all_thread_imc_memory(void)
}
}

+/* Function to free the attr_groups which are dynamically allocated */
+static void imc_common_mem_free(struct imc_pmu *pmu_ptr)
+{
+ kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
+ kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
+ kfree(pmu_ptr);
+ return;
+}
+
/*
* Common function to unregister cpu hotplug callback and
* free the memory.
@@ -1175,11 +1186,6 @@ static void imc_common_cpuhp_mem_free(struct imc_pmu *pmu_ptr)
cleanup_all_thread_imc_memory();
}

- /* Only free the attr_groups which are dynamically allocated */
- if (pmu_ptr->attr_groups[IMC_EVENT_ATTR])
- kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]->attrs);
- kfree(pmu_ptr->attr_groups[IMC_EVENT_ATTR]);
- kfree(pmu_ptr);
return;
}

@@ -1318,17 +1324,19 @@ int init_imc_pmu(struct device_node *parent, struct imc_pmu *pmu_ptr, int pmu_id

ret = update_pmu_ops(pmu_ptr);
if (ret)
- goto err_free;
+ goto err_free_mem;

ret = perf_pmu_register(&pmu_ptr->pmu, pmu_ptr->pmu.name, -1);
if (ret)
- goto err_free;
+ goto err_free_mem;

pr_info("%s performance monitor hardware support registered\n",
pmu_ptr->pmu.name);

return 0;

+err_free_mem:
+ imc_common_mem_free(pmu_ptr);
err_free:
imc_common_cpuhp_mem_free(pmu_ptr);
return ret;
--
2.14.1