Skip to content

tree: fix mem leaks in multiple nvme ctrl routines#1028

Closed
martin-gpy wants to merge 1 commit intolinux-nvme:masterfrom
martin-gpy:mem_leak_ctrl
Closed

tree: fix mem leaks in multiple nvme ctrl routines#1028
martin-gpy wants to merge 1 commit intolinux-nvme:masterfrom
martin-gpy:mem_leak_ctrl

Conversation

@martin-gpy
Copy link
Copy Markdown
Contributor

Valgrind revealed memory leaks in multiple nvme ctrl routines including nvme_configure_ctrl(), nvme_ctrl_alloc() & nvme_scan_ctrl(). This was because previously allocated memory was not freed before assigning new attr values to the respective nvme_ctrl struct members in these routines. Fix the same.

Valgrind revealed memory leaks in multiple nvme ctrl routines
including nvme_configure_ctrl(), nvme_ctrl_alloc() & nvme_scan_ctrl().
This was because previously allocated memory was not freed before
assigning new attr values to the respective nvme_ctrl struct members
in these routines. Fix the same.

Signed-off-by: Martin George <[email protected]>
Comment thread src/nvme/tree.c
void nvme_ctrl_set_addr(nvme_ctrl_t c, const char *addr)
{
if (c->address) {
free(c->address);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

free() is NULL-safe. So you could just call it w/o first cheking that the pointer in non-NULL.

https://man7.org/linux/man-pages/man3/free.3p.html

Copy link
Copy Markdown
Contributor Author

@martin-gpy martin-gpy Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Guess this should be cleaned up in other parts of the code too.

Comment thread src/nvme/tree.h
* @c: Controller whose address to set
* @addr: Address
*/
void nvme_ctrl_set_addr(nvme_ctrl_t c, const char *addr);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these function should be exposed at all.

Copy link
Copy Markdown
Contributor Author

@martin-gpy martin-gpy Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying these pointers should be freed within nvme_configure_ctrl() & nvme_ctrl_alloc() itself? That would make the code ugly to read though...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still reading through the code. Not sure what is best, but I really don't want these setters in the public API.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Jun 26, 2025

nvme_configure_ctrl is called either in nvme_init_ctrl or nvme_ctrl_alloc. Hmm, I thought these function should only initialize the ctrl object once.

@martin-gpy
Copy link
Copy Markdown
Contributor Author

nvme_configure_ctrl is called either in nvme_init_ctrl or nvme_ctrl_alloc. Hmm, I thought these function should only initialize the ctrl object once.

So how should we proceed here? Do you want me to resubmit after addressing @martin-belanger's comment above? Or avoid exposing these new ctrl functions altogether and free the individual ctrl poiners within nvme_configure_ctrl() & nvme_ctrl_alloc() itself. Please clarify.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Jun 27, 2025

So how should we proceed here? Do you want me to resubmit after addressing @martin-belanger's comment above? Or avoid exposing these new ctrl functions altogether and free the individual ctrl poiners within nvme_configure_ctrl() & nvme_ctrl_alloc() itself. Please clarify.

I haven't had enough time to look into it yet. The issue I have is that all those function have been written with the assumption that we only initialize a ctrl object once. But doesn't seem the case. Thus I want to figure out if there is a fundamental problem which we need to address. I don't want to paper over design issues.

Comment thread src/nvme/tree.c
if (!c)
return NULL;

path = NULL;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unrelated? path already gets freed and set to NULL in the helper function in my patch. So there was no need to separately set it to NULL here to avoid getting it freed through the cleanup macro. Otherwise this would have resulted in a double free.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah didn't realize there is the cleanup hook. Hmm I think we should avoid passing owner ship like this around, it leads to more problems in the long run. If nvme_ctrl_alloc needs the path variable it should duplicate it. Let me look into it.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Jul 1, 2025

We already have nvme_deconfigure_ctrl which does all the freeing. Let's just use this one in nvme_configure_ctrl with a comment explaining why it is necessary to free stuff first.

In nvme_ctrl_alloc use the FREE_CTRL_ATTR to stay consistent.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Jul 1, 2025

urgh, this doesn't work either, because nvme_alloc_ctrl is doing a lookup... this is so an antipattern. we should have never done it this way. let me see how to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants