Skip to contentSkip to conversation

libdaxctl: fix memory leaks with daxctl_memory objects

PATCH In review Vishal Verma 2 participants 4 messages Oct 1, 2019 – Oct 3, 2019 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH 1/1 libdaxctl: fix memory leaks with daxctl_memory objects

The daxctl_dev_alloc_mem() helper which is used to instantiate a new memory object did so, but neglected to attach the memory object to the parent 'dev' object. As a result, every invocation of 'daxctl_dev_get_memory() resulted in a new, orphan memory object being created, which also resulted in libdaxctl leaking memory.

Fix the parent association for 'mem' objects, and in free_mem, remove the check for 'dev' being present - mem objects will always associated with a dev.

Additionally, we were neglecting to free 'mem->mem_buf' in free_mem, so fix this up as well.

Fixes e8bf803e359b ("libdaxctl: add a 'daxctl_memory' object for memory based operations")
---
 daxctl/lib/libdaxctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
daxctl/lib/libdaxctl.c +3 -1
index 8abfd64..639224c 100644
@@ -204,8 +204,9 @@ DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u
 
 static void free_mem(struct daxctl_dev *dev)
 {
-	if (dev && dev->mem) {
+	if (dev->mem) {
 		free(dev->mem->node_path);
+		free(dev->mem->mem_buf);
 		free(dev->mem);
 		dev->mem = NULL;
 	}
@@ -450,6 +451,7 @@ static struct daxctl_memory *daxctl_dev_alloc_mem(struct daxctl_dev *dev)
 		goto err_node;
 	mem->buf_len = strlen(node_base) + 256;
 
+	dev->mem = mem;
 	return mem;
 
 err_node:
Signature · 4 lines
2.20.1
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Vishal Verma

The daxctl_dev_alloc_mem() helper which is used to instantiate a new memory object did so, but neglected to attach the memory object to the parent 'dev' object. As a result, every invocation of 'daxctl_dev_get_memory() resulted in a new, orphan memory object being created, which also resulted in libdaxctl leaking memory.

Fix the parent association for 'mem' objects, and in free_mem, remove the check for 'dev' being present - mem objects will always associated with a dev.

Quoted 7 lines

Additionally, we were neglecting to free 'mem->mem_buf' in free_mem, so fix this up as well.

Fixes e8bf803e359b ("libdaxctl: add a 'daxctl_memory' object for memory based operations")

Minor NIT below.

Quoted 9 lines
---
 daxctl/lib/libdaxctl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
daxctl/lib/libdaxctl.c +0 -0
index 8abfd64..639224c 100644

daxctl/lib/libdaxctl.c DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u · line 207

Quoted 5 lines
daxctl/lib/libdaxctl.c +1 -1
@@ -204,8 +204,9 @@ DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u
 
 static void free_mem(struct daxctl_dev *dev)
 {
-	if (dev && dev->mem) {
+	if (dev->mem) {

There is a comment in daxctl_dev_disable() which says:

        /* If there is a memory object, first free that */

I'm 100% sure that dev can't be NULL there. So that comment no longer applies.

May want to remove that comment.

Ira

Quoted 18 lines
daxctl/lib/libdaxctl.c +2 -0
 		free(dev->mem->node_path);
+		free(dev->mem->mem_buf);
 		free(dev->mem);
 		dev->mem = NULL;
 	}
@@ -450,6 +451,7 @@ static struct daxctl_memory *daxctl_dev_alloc_mem(struct daxctl_dev *dev)
 		goto err_node;
 	mem->buf_len = strlen(node_base) + 256;
 
+	dev->mem = mem;
 	return mem;
 
 err_node:
Signature · 4 lines
2.20.1
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]

author maintainer
#2 lore raw
Quoted 14 lines · Ira Weiny

daxctl/lib/libdaxctl.c DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u · line 207

Quoted 11 lines
daxctl/lib/libdaxctl.c +1 -1
index 8abfd64..639224c 100644
@@ -204,8 +204,9 @@ DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u
 
 static void free_mem(struct daxctl_dev *dev)
 {
-	if (dev && dev->mem) {
+	if (dev->mem) {

There is a comment in daxctl_dev_disable() which says:

Quoted 6 lines
        /* If there is a memory object, first free that */

I'm 100% sure that dev can't be NULL there. So that comment no longer applies.

May want to remove that comment.

Hm, I'm not sure I follow.

Yes, dev can't be null there, but 'mem' can me. Hence the comment saying if /mem/ is present, free it.

The check in free_mem() only checks for non-NULL mem too.

_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]

#3 lore raw
Quoted 19 lines · Vishal Verma
Quoted 18 lines · Ira Weiny

daxctl/lib/libdaxctl.c DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u · line 207

Quoted 11 lines
daxctl/lib/libdaxctl.c +1 -1
index 8abfd64..639224c 100644
@@ -204,8 +204,9 @@ DAXCTL_EXPORT void daxctl_region_get_uuid(struct daxctl_region *region, uuid_t u
 
 static void free_mem(struct daxctl_dev *dev)
 {
-	if (dev && dev->mem) {
+	if (dev->mem) {

There is a comment in daxctl_dev_disable() which says:

        /* If there is a memory object, first free that */

I'm 100% sure that dev can't be NULL there. So that comment no longer applies.

Quoted 8 lines
Quoted 2 lines

May want to remove that comment.

Hm, I'm not sure I follow.

Yes, dev can't be null there, but 'mem' can me. Hence the comment saying if /mem/ is present, free it.

The check in free_mem() only checks for non-NULL mem too.

Ah yea ok. Sorry
Ira
_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]