EDAC/sysfs: Fix memory leak when creating a csrow object [Linux 4.4.187]

This Linux kernel change "EDAC/sysfs: Fix memory leak when creating a csrow object" is included in the Linux 4.4.187 release. This change is authored by Pan Bian <bianpan2016 [at] 163.com> on Thu Apr 18 10:27:18 2019 +0800. The commit for this change in Linux stable tree is 473e040 (patch) which is from upstream commit 585fb3d. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 585fb3d.

EDAC/sysfs: Fix memory leak when creating a csrow object

[ Upstream commit 585fb3d93d32dbe89e718b85009f9c322cc554cd ]

In edac_create_csrow_object(), the reference to the object is not
released when adding the device to the device hierarchy fails
(device_add()). This may result in a memory leak.

Signed-off-by: Pan Bian <[email protected]>
Signed-off-by: Borislav Petkov <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Cc: James Morse <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: linux-edac <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Sasha Levin <[email protected]>

There are 8 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/edac/edac_mc_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index 3c8f19f..0c53f2d 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -426,6 +426,8 @@ static inline int nr_pages_per_csrow(struct csrow_info *csrow)
 static int edac_create_csrow_object(struct mem_ctl_info *mci,
                    struct csrow_info *csrow, int index)
 {
+   int err;
+
    csrow->dev.type = &csrow_attr_type;
    csrow->dev.bus = mci->bus;
    csrow->dev.groups = csrow_dev_groups;
@@ -438,7 +440,11 @@ static int edac_create_csrow_object(struct mem_ctl_info *mci,
    edac_dbg(0, "creating (virtual) csrow node %s\n",
         dev_name(&csrow->dev));

-   return device_add(&csrow->dev);
+   err = device_add(&csrow->dev);
+   if (err)
+       put_device(&csrow->dev);
+
+   return err;
 }

 /* Create a CSROW object under specifed edac_mc_device */

Leave a Reply

Your email address will not be published. Required fields are marked *