Skip to contentSkip to conversation

EDAC: Fix memory leak in creating CSROW object

PATCH In review Pan Bian 3 participants 11 messages Apr 18, 2019 – May 8, 2019 1 message not in the archive EDAC-core

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

In the function that creates a CSROW object, the object is not released when failing to add the device to device hierarchy. This may result in a memory leak bug.

---
 drivers/edac/edac_mc_sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/edac/edac_mc_sysfs.c +5 -1
index 4641746..2dafb08 100644
@@ -404,6 +404,7 @@ 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.groups = csrow_dev_groups;
 	device_initialize(&csrow->dev);
@@ -415,7 +416,10 @@ 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 */
maintainer
#1 lore raw
Quoted 2 lines · Pan Bian

In the function that creates a CSROW object, the object is not released when failing to add the device to device hierarchy.

Are you sure about this?

Quoted 1 line

This may result in a memory leak bug.

"May"?

I see a loop which unwinds by putting the already created devices. Do you?

A message that is not in the archive (sent off-list or not yet received): 20190419003536.GA57795@[email protected]

maintainer
#3 lore raw
Quoted 3 lines · PanBian

Yes, I see that. Because the loop start with (--i), there is no put operation for the device that fails to create. So, I think we cannot rule out the possibility of memory leak.

Ok, so this is not something you trigger - you're basically staring at the code?

Well, there's something else questionable in that code which I asked Greg about today but we didn't finish that conversation, let me CC him.

So AFAIU, devices for which device_add() has returned success, should be removed with their counterpart device_del(). edac_create_csrow_objects(), however, does put_device() on those in the "unwinding" loop.

And for the case where device_add() fails, you should do put_device() to it. I.e., what you're saying.

So I think we need to figure what needs to be done when before fixing this properly.

Greg?

#4 lore raw
Quoted 16 lines · Borislav Petkov
Quoted 3 lines · PanBian

Yes, I see that. Because the loop start with (--i), there is no put operation for the device that fails to create. So, I think we cannot rule out the possibility of memory leak.

Ok, so this is not something you trigger - you're basically staring at the code?

Well, there's something else questionable in that code which I asked Greg about today but we didn't finish that conversation, let me CC him.

So AFAIU, devices for which device_add() has returned success, should be removed with their counterpart device_del(). edac_create_csrow_objects(), however, does put_device() on those in the "unwinding" loop.

Quoted 7 lines

And for the case where device_add() fails, you should do put_device() to it. I.e., what you're saying.

So I think we need to figure what needs to be done when before fixing this properly.

Greg?

How about this patch, I think it fixes up everything you need to do here, right?

drivers/edac/edac_mc_sysfs.c +9 -1
index 464174685589..0fb2d1de6d0e 100644
@@ -404,6 +404,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 retval;
+
 	csrow->dev.type = &csrow_attr_type;
 	csrow->dev.groups = csrow_dev_groups;
 	device_initialize(&csrow->dev);
@@ -415,7 +417,10 @@ 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);
+	retval = device_add(&csrow->dev);
+	if (retval)
+		put_device(&csrow->dev);
+	return retval;
 }
 
 /* Create a CSROW object under specifed edac_mc_device */
@@ -649,6 +654,8 @@ static int edac_create_dimm_object(struct mem_ctl_info *mci,
 
 	edac_dbg(0, "creating rank/dimm device %s\n", dev_name(&dimm->dev));
 
+	if (err)
+		put_device(&dimm->dev);
 	return err;
 }
 
@@ -928,6 +935,7 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
 	err = device_add(&mci->dev);
 	if (err < 0) {
 		edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
+		put_device(mci->dev);
 		goto out;
 	}
 
maintainer
#5 lore raw
Quoted 2 lines · Greg KH

How about this patch, I think it fixes up everything you need to do here, right?

Almost, see the two patches as a reply to this message. I've taken Pan's original patch because it is correct and I doubt you're dying for attribution :-)

Then, I productized yours, with some additions. :)

Thoughts?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#6 lore raw
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · Greg KH

How about this patch, I think it fixes up everything you need to do here, right?

Almost, see the two patches as a reply to this message. I've taken Pan's original patch because it is correct and I doubt you're dying for attribution :-)

Nope, no need for that :)

Quoted 1 line

Then, I productized yours, with some additions. :)

Looks good to me, ship it!

maintainer
#7 lore raw
Quoted 1 line · Greg KH

Looks good to me, ship it!

Thx, done!

:-)

Signature · 4 lines
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
maintainer
#8 lore raw

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

In reply to Greg KH

From 28e7f23939208bea639d6cd3d492cde3f65a7e4f Mon Sep 17 00:00:00 2001
From: Pan Bian <[email protected]>
Date: Thu, 18 Apr 2019 10:27:18 +0800

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.

---
 drivers/edac/edac_mc_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/edac/edac_mc_sysfs.c +7 -1
index 464174685589..9b7d396f26e9 100644
@@ -404,6 +404,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.groups = csrow_dev_groups;
 	device_initialize(&csrow->dev);
@@ -415,7 +417,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 */
Signature · 8 lines
2.21.0


-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
Reviewed-by on v1 1/2
#9 lore raw

Re: [PATCH 1/2] EDAC/sysfs: Fix memory leak when creating a csrow object

Quoted 15 lines · Borislav Petkov
Quoted 1 line

From 28e7f23939208bea639d6cd3d492cde3f65a7e4f Mon Sep 17 00:00:00 2001

From: Pan Bian <[email protected]>
Date: Thu, 18 Apr 2019 10:27:18 +0800

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.

Quoted 3 lines
---
 drivers/edac/edac_mc_sysfs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
maintainer
#10 lore raw

PATCH 2/2 EDAC/sysfs: Drop device references properly

In reply to Greg KH

--
From: Greg KH <[email protected]>

Do put_device() if device_add() fails.

[ bp: do device_del() for the successfully created devices in
edac_create_csrow_objects(), on the unwind path. ]

---
 drivers/edac/edac_mc_sysfs.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/edac/edac_mc_sysfs.c +7 -3
index 9b7d396f26e9..7c01e1cc030c 100644
@@ -449,7 +449,8 @@ static int edac_create_csrow_objects(struct mem_ctl_info *mci)
 		csrow = mci->csrows[i];
 		if (!nr_pages_per_csrow(csrow))
 			continue;
-		put_device(&mci->csrows[i]->dev);
+
+		device_del(&mci->csrows[i]->dev);
 	}
 
 	return err;
@@ -651,9 +652,11 @@ static int edac_create_dimm_object(struct mem_ctl_info *mci,
 	dev_set_drvdata(&dimm->dev, dimm);
 	pm_runtime_forbid(&mci->dev);
 
-	err =  device_add(&dimm->dev);
+	err = device_add(&dimm->dev);
+	if (err)
+		put_device(&dimm->dev);
 
-	edac_dbg(0, "creating rank/dimm device %s\n", dev_name(&dimm->dev));
+	edac_dbg(0, "created rank/dimm device %s\n", dev_name(&dimm->dev));
 
 	return err;
 }
@@ -934,6 +937,7 @@ int edac_create_sysfs_mci_device(struct mem_ctl_info *mci,
 	err = device_add(&mci->dev);
 	if (err < 0) {
 		edac_dbg(1, "failure: create device %s\n", dev_name(&mci->dev));
+		put_device(&mci->dev);
 		goto out;
 	}
 
Signature · 8 lines
2.21.0


-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#11 lore raw

Re: [PATCH 2/2] EDAC/sysfs: Drop device references properly

Quoted 7 lines · Borislav Petkov

--
From: Greg KH <[email protected]>

Do put_device() if device_add() fails.

[ bp: do device_del() for the successfully created devices in
edac_create_csrow_objects(), on the unwind path. ]

Yes, good catch, looks good, thanks!

greg k-h