diff mbox series

drivers: nvdimm: fix memleak

Message ID 20230817115945.771826-1-konstantin.meskhidze@huawei.com (mailing list archive)
State Accepted
Commit 85ae42c72142346645e63c33835da947dfa008b3
Headers show
Series drivers: nvdimm: fix memleak | expand

Commit Message

Konstantin Meskhidze (A) Aug. 17, 2023, 11:59 a.m. UTC
Memory pointed by 'nd_pmu->pmu.attr_groups' is allocated in function
'register_nvdimm_pmu' and is lost after 'kfree(nd_pmu)' call in function
'unregister_nvdimm_pmu'.

Co-developed-by: Ivanov Mikhail <ivanov.mikhail1@huawei-partners.com>
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
---
 drivers/nvdimm/nd_perf.c | 1 +
 1 file changed, 1 insertion(+)

Comments

Jeff Moyer Aug. 17, 2023, 2:32 p.m. UTC | #1
Konstantin Meskhidze <konstantin.meskhidze@huawei.com> writes:

> Memory pointed by 'nd_pmu->pmu.attr_groups' is allocated in function
> 'register_nvdimm_pmu' and is lost after 'kfree(nd_pmu)' call in function
> 'unregister_nvdimm_pmu'.
>
> Co-developed-by: Ivanov Mikhail <ivanov.mikhail1@huawei-partners.com>
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> ---
>  drivers/nvdimm/nd_perf.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/nvdimm/nd_perf.c b/drivers/nvdimm/nd_perf.c
> index 433bbb68a..14881c4e0 100644
> --- a/drivers/nvdimm/nd_perf.c
> +++ b/drivers/nvdimm/nd_perf.c
> @@ -323,7 +323,8 @@ EXPORT_SYMBOL_GPL(register_nvdimm_pmu);
>  void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu)
>  {
>  	perf_pmu_unregister(&nd_pmu->pmu);
>  	nvdimm_pmu_free_hotplug_memory(nd_pmu);
> +	kfree(nd_pmu->pmu.attr_groups);
>  	kfree(nd_pmu);
>  }
>  EXPORT_SYMBOL_GPL(unregister_nvdimm_pmu);

Reviewed-by: Jeff Moyer <jmoyer@redhat.com>
Dan Williams Aug. 17, 2023, 3:06 p.m. UTC | #2
[ add Kajol and Madhavan ]

Konstantin Meskhidze wrote:
> Memory pointed by 'nd_pmu->pmu.attr_groups' is allocated in function
> 'register_nvdimm_pmu' and is lost after 'kfree(nd_pmu)' call in function
> 'unregister_nvdimm_pmu'.

Yes, looks like a real issue, but also completely avoidable by using statically
defined groups.

My fault for not catching this earlier, but Kajol, why is nd_perf not using
statically defined sysfs attribute groups?
Dave Jiang Aug. 17, 2023, 4 p.m. UTC | #3
On 8/17/23 04:59, Konstantin Meskhidze wrote:
> Memory pointed by 'nd_pmu->pmu.attr_groups' is allocated in function
> 'register_nvdimm_pmu' and is lost after 'kfree(nd_pmu)' call in function
> 'unregister_nvdimm_pmu'.
> 
> Co-developed-by: Ivanov Mikhail <ivanov.mikhail1@huawei-partners.com>
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>

Applied and changed subject to:
nvdimm: Fix memleak of pmu attr_groups in unregister_nvdimm_pmu()

Also added fixes tag:
Fixes: 0fab1ba6ad6b ("drivers/nvdimm: Add perf interface to expose 
nvdimm performance stats")


> ---
>   drivers/nvdimm/nd_perf.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/nvdimm/nd_perf.c b/drivers/nvdimm/nd_perf.c
> index 433bbb68a..14881c4e0 100644
> --- a/drivers/nvdimm/nd_perf.c
> +++ b/drivers/nvdimm/nd_perf.c
> @@ -323,7 +323,8 @@ EXPORT_SYMBOL_GPL(register_nvdimm_pmu);
>   void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu)
>   {
>   	perf_pmu_unregister(&nd_pmu->pmu);
>   	nvdimm_pmu_free_hotplug_memory(nd_pmu);
> +	kfree(nd_pmu->pmu.attr_groups);
>   	kfree(nd_pmu);
>   }
>   EXPORT_SYMBOL_GPL(unregister_nvdimm_pmu);
diff mbox series

Patch

diff --git a/drivers/nvdimm/nd_perf.c b/drivers/nvdimm/nd_perf.c
index 433bbb68a..14881c4e0 100644
--- a/drivers/nvdimm/nd_perf.c
+++ b/drivers/nvdimm/nd_perf.c
@@ -323,7 +323,8 @@  EXPORT_SYMBOL_GPL(register_nvdimm_pmu);
 void unregister_nvdimm_pmu(struct nvdimm_pmu *nd_pmu)
 {
 	perf_pmu_unregister(&nd_pmu->pmu);
 	nvdimm_pmu_free_hotplug_memory(nd_pmu);
+	kfree(nd_pmu->pmu.attr_groups);
 	kfree(nd_pmu);
 }
 EXPORT_SYMBOL_GPL(unregister_nvdimm_pmu);