[PATCH 3/3] EDAC/pci: Fix a potential memory leak

From: Qiuxu Zhuo
Date: Sun Oct 08 2023 - 04:06:45 EST


From: Lili Li <lili.li@xxxxxxxxx>

The EDAC PCI core misses kfreeing 'pvt_info' which may result in a memory
leak. Fix it by adding a flag to indicate whether 'pvt_info' is allocated
by the EDAC PCI core and kfreeing 'pvt_info' by the EDAC PCI core when the
flag is set to true.

Fixes: fb8cd45ca39b ("EDAC/pci: Get rid of the silly one-shot memory allocation in edac_pci_alloc_ctl_info()")
Suggested-by: Qiuxu Zhuo <qiuxu.zhuo@xxxxxxxxx>
Signed-off-by: Lili Li <lili.li@xxxxxxxxx>
---
drivers/edac/edac_pci.c | 1 +
drivers/edac/edac_pci.h | 5 +++++
drivers/edac/edac_pci_sysfs.c | 2 ++
3 files changed, 8 insertions(+)

diff --git a/drivers/edac/edac_pci.c b/drivers/edac/edac_pci.c
index 64c142aecca7..7c9d1d9115c4 100644
--- a/drivers/edac/edac_pci.c
+++ b/drivers/edac/edac_pci.c
@@ -40,6 +40,7 @@ struct edac_pci_ctl_info *edac_pci_alloc_ctl_info(unsigned int sz_pvt,
pci->pvt_info = kzalloc(sz_pvt, GFP_KERNEL);
if (!pci->pvt_info)
goto free;
+ pci->pvt_managed_by_edac_core = true;
}

pci->op_state = OP_ALLOC;
diff --git a/drivers/edac/edac_pci.h b/drivers/edac/edac_pci.h
index 5175f5724cfa..27fecf6bfafc 100644
--- a/drivers/edac/edac_pci.h
+++ b/drivers/edac/edac_pci.h
@@ -69,6 +69,11 @@ struct edac_pci_ctl_info {
const char *dev_name; /* pci/platform/etc... name */

void *pvt_info; /* pointer to 'private driver' info */
+ /*
+ * Indicate whether the resource pointed by pvt_info is managed by
+ * EDAC core
+ */
+ bool pvt_managed_by_edac_core;

unsigned long start_time; /* edac_pci load start time (jiffies) */

diff --git a/drivers/edac/edac_pci_sysfs.c b/drivers/edac/edac_pci_sysfs.c
index 287cc51dbc86..520fb2fa9411 100644
--- a/drivers/edac/edac_pci_sysfs.c
+++ b/drivers/edac/edac_pci_sysfs.c
@@ -83,6 +83,8 @@ static void edac_pci_instance_release(struct kobject *kobj)
/* decrement reference count on top main kobj */
kobject_put(edac_pci_top_main_kobj);

+ if (pci->pvt_managed_by_edac_core)
+ kfree(pci->pvt_info);
kfree(pci); /* Free the control struct */
}

--
2.17.1