Skip to content

nvme: output nvme_alloc() error message#2855

Closed
ikegami-t wants to merge 1 commit intolinux-nvme:masterfrom
ikegami-t:nvme-alloc-error
Closed

nvme: output nvme_alloc() error message#2855
ikegami-t wants to merge 1 commit intolinux-nvme:masterfrom
ikegami-t:nvme-alloc-error

Conversation

@ikegami-t
Copy link
Copy Markdown
Contributor

Currently only the value -ENOMEM returned for the error. Also fixed some nvme_alloc() error handling.

@ikegami-t
Copy link
Copy Markdown
Contributor Author

This related to the fix #2850.

Comment thread nvme.c Outdated
if (!smart_log) {
nvme_show_error("failed to allocate aligned memory");
return -ENOMEM;
}
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.

can these 4 lines be a macro call, something like this to save code lines (any name for macro):
Check_And_Return_If_Mem_Error( mem);

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.

Seems nvme_alloc() itself also can be added into the macro. By the way is there any actual example for the macro in nvme-cli, libnvme or linux kernel do you know? Thank you.

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.

What I am referring is to have a simple macro to eliminate lots of code with 1 liner. The macro would be:
#define SHOW_ERROR_AND_RETURN_IF_NO_MEM(mem)
if (!mem) {
nvme_show_error("failed to allocate aligned memory");
return -ENOMEM;
} \

end the code above will become:

SHOW_ERROR_AND_RETURN_IF_NO_MEM(smart_log)

or below
SHOW_ERROR_AND_RETURN_IF_NO_MEM(ctrl)

Adding nvme_alloc() to the macro, if possible, would make the code ugly, but you can try.

@ikegami-t
Copy link
Copy Markdown
Contributor Author

The PR for the nvme_alloc() is based on the PR: #2850 for the issue: #2848 for the nvme_alloc_huge() error and just checked the PRs before then found the related PR: #2337 for -ENOMEM alloc error but closed the PR without merging. Also the PR was for the similar issue: #2336. So now I am thinking to close the PR as same.

@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Jul 1, 2025

That nvme_alloc_huge fails is not 0%. This is due to the type of allocation we do with it to: larg linear buffer allocation.

But in case of nvme_alloc this is very unlikely to fail. The Linux virtual memory manager normally does overcommiting (see /proc/sys/vm/overcommit_memory. That means malloc returns a pointer but when we try to use it, the OOT kills the process.

And we have many mallocs around where we don't print anything. I'd rather leave this as it is.

An idea I am toying with is that we just fail on allocation failures in nvme-cli. There is absolut no point in trying to recover from such scenario. E.g. we could use these hooks for malloc (https://www.gnu.org/software/libc/manual/html_node/Hooks-for-Malloc.html) and call abort. This would get rid of the error handling code, no one ever tests:

    ptr = malloc(sizeof(*ptr));
    if (!ptr)
        return -ENOMEM;

Though for libnvme this is not an option.

@ikegami-t ikegami-t force-pushed the nvme-alloc-error branch 3 times, most recently from 27420df to 7694358 Compare July 5, 2025 07:07
@ikegami-t
Copy link
Copy Markdown
Contributor Author

Just fixed the patch to abort if malloc failed. About the macro for error return will do consider for other error cases later. Thank you.

@ikegami-t ikegami-t force-pushed the nvme-alloc-error branch 2 times, most recently from dd78fdf to e44f63d Compare July 5, 2025 15:52
Comment thread meson.build Outdated
''',
name: 'weak_malloc'
),
description: 'Does struct tm have a tm_gmtoff field?'
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.

The description needs to be updated.

Comment thread util/mem.c

return result;
}
#endif /* HAVE_WEAK_MALLOC */
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 guess we need the same thing at least for calloc and realloc.

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.

Thanks for looking into this. After looking at this again with fresh eyes, I think we don't gain alot with this approach. Because we can't really remove the p = malloc(...); if (p) { printf(...); return -1; } code. We still need it when the allocation fails.

That means it would make more sense to introduce nvme_malloc & Co which would check the allocation and update all call sites and remove the memory allocation checks. Basically like the idea from above but with function instead ugly macros.

Since the nvme-cli 2.x is entering the maintenance mode today this is something we could consider for nvme-cli3

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.

Thanks for your comment. I will do for nvme-cli3.

Comment thread util/mem.h Outdated
if (!ptr) \
abort(); \
} while (false)

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 we need this macro as API. The whole point if this would be that we nvme-cli aborts directly when the allocation fails. Nothing should call this macro.

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.

just I thoght as we can use the macro for other pointer handling case but deleted the macro as mentioned. In future if really needed the macro I will do consider again to add. Thank you.

The __malloc_hook deprecated and malloc function is weak function.
Then we can add malloc function to abort when failed to allocate.

Signed-off-by: Tokunori Ikegami <[email protected]>
@ikegami-t
Copy link
Copy Markdown
Contributor Author

Fixed the review comments. Thank you.

@ikegami-t ikegami-t closed this Nov 3, 2025
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