libnvme: abstract memory (de)allocation API#3351
Conversation
337ff92 to
50cb74c
Compare
Sort the entries alphabetically. Signed-off-by: Daniel Wagner <wagi@kernel.org>
b92f51a to
b8d4b43
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors memory allocation helpers to be provided by libnvme (with OS-specific implementations) and updates nvme-cli/plugins to use the new libnvme_alloc*() APIs, as part of incremental work toward Windows builds. It also adjusts libnvme logging to avoid dprintf().
Changes:
- Remove the legacy
util/mem.*helpers and switch call sites tolibnvme_alloc(),libnvme_realloc(), andlibnvme_alloc_huge()(plusstruct libnvme_mem_huge). - Introduce
nvme/mem.hin libnvme and add Linux/Windows implementations (mem-linux.c,mem-win.c), exporting the new symbols. - Replace
dprintf()-based logging withasprintf()+write().
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| util/meson.build | Drops util/mem.c from util build sources. |
| util/mem.h | Removes legacy util allocation API header. |
| util/cleanup.h | Switches __cleanup_huge to libnvme_free_huge. |
| plugins/zns/zns.c | Migrates huge allocation usage to libnvme (libnvme_alloc_huge, libnvme_mem_huge). |
| plugins/wdc/wdc-nvme.c | Migrates allocation helpers to libnvme APIs. |
| plugins/solidigm/solidigm-internal-logs.c | Migrates allocation helpers to libnvme APIs. |
| plugins/scaleflux/sfx-nvme.c | Migrates huge allocation helpers to libnvme APIs. |
| plugins/ocp/ocp-nvme.c | Migrates allocation helpers to libnvme APIs. |
| plugins/micron/micron-nvme.c | Migrates huge allocation helper to libnvme API. |
| plugins/lm/lm-nvme.c | Migrates huge allocation helper to libnvme API and updates error text. |
| plugins/ibm/ibm-nvme.c | Migrates allocation helper to libnvme API. |
| plugins/feat/feat-nvme.c | Migrates allocation helper to libnvme API. |
| nvme.h | Removes include of deleted util/mem.h. |
| nvme.c | Migrates many allocations to libnvme APIs and updates huge allocation types. |
| libnvme/src/nvme/util.c | Removes internal __libnvme_alloc/__libnvme_realloc implementation. |
| libnvme/src/nvme/tree.c | Switches identify-buffer allocation to libnvme_alloc(). |
| libnvme/src/nvme/private.h | Drops declarations for removed __libnvme_alloc/__libnvme_realloc. |
| libnvme/src/nvme/nvme-cmds.c | Switches allocations to libnvme_alloc/libnvme_realloc. |
| libnvme/src/nvme/mem.h | Adds public libnvme memory allocation API header. |
| libnvme/src/nvme/mem-win.c | Adds Windows implementation of libnvme memory allocation helpers. |
| libnvme/src/nvme/mem-linux.c | Updates Linux implementation to new libnvme allocation API + exports. |
| libnvme/src/nvme/log.c | Replaces dprintf() logging with formatted buffer + write(). |
| libnvme/src/nvme/fabrics.c | Switches allocations to libnvme_alloc(). |
| libnvme/src/meson.build | Adds mem sources per-OS and installs new nvme/mem.h. |
| libnvme/src/libnvme.ld | Exports new libnvme_alloc*()/libnvme_free*() symbols and reorders some entries. |
| libnvme/src/libnvme.h.in | Installs nvme/mem.h in the public libnvme umbrella header. |
Comments suppressed due to low confidence (2)
libnvme/src/nvme/mem-linux.c:43
- libnvme_realloc() calls malloc_usable_size(p) before checking whether p is NULL, but nvme/mem.h documents that passing NULL should behave like libnvme_alloc(). Guard p == NULL first to avoid undefined behavior.
nvme.c:2905 - The allocation failure check is inverted: this returns false when ctrl is non-NULL (allocation succeeded), and then proceeds with a NULL ctrl when allocation fails. This should be
if (!ctrl) return false;(or equivalent).
__cleanup_free struct nvme_id_ctrl *ctrl = libnvme_alloc(sizeof(*ctrl));
if (ctrl)
return false;
err = nvme_identify_ctrl(hdl, ctrl);
if (err)
return false;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9cfdcb3 to
42cd15a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 29 out of 29 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (3)
nvme.c:2903
- This check is inverted:
ctrlis non-NULL on successful allocation, so the function always returns false and never calls nvme_identify_ctrl(). This should beif (!ctrl) return false;.
__cleanup_libnvme_free struct nvme_id_ctrl *ctrl = libnvme_alloc(sizeof(*ctrl));
if (ctrl)
return false;
err = nvme_identify_ctrl(hdl, ctrl);
libnvme/src/nvme/mem-linux.c:43
- libnvme_realloc() calls malloc_usable_size(p) unconditionally; if
pis NULL this is undefined behavior, and it also violates the documented behavior that NULL should act like libnvme_alloc(). Add an earlyif (!p) return libnvme_alloc(len);before calling malloc_usable_size().
libnvme/src/nvme/mem-linux.c:19 - This file uses memset/memcpy but doesn’t include <string.h>, which can cause build failures due to missing prototypes. Add the appropriate header include.
dprintf is not available on Windows, so open-code it using asprintf and write. Signed-off-by: Daniel Wagner <wagi@kernel.org>
libnvme uses posix_memalign to ensure that allocated memory buffers are properly aligned. This API does not exist on Windows, so move this code into a platform abstraction. Since we have to abstract the same APIs for nvme-cli itself, add these helpers to the libnvme API. Signed-off-by: Daniel Wagner <wagi@kernel.org>
Add Windows implementations of libnvme_alloc, libnvme_realloc, and related memory allocation helpers. This provides a consistent cross-platform memory allocation interface for libnvme on Windows systems. Signed-off-by: Broc Going <bgoing@micron.com> Signed-off-by: Brandon Capener <bcapener@micron.com> [wagi: cherry-picked code, fixed libnvme_realloc] Signed-off-by: Daniel Wagner <wagi@kernel.org>
The library added platform abstraction code for memory allocation. Switch over to this API and remove the duplicated code in nvme-cli. Signed-off-by: Daniel Wagner <wagi@kernel.org>
Move these helpers to the library, so it's abstraction code for the mem API are in one place. Signed-off-by: Daniel Wagner <wagi@kernel.org>
Add Windows support for the huge memory allocation APIs by implementing libnvme_alloc_huge() and libnvme_free_huge() using Windows-specific memory allocation mechanisms. This brings the huge page memory API support closer to feature parity across supported platforms. Signed-off-by: Broc Going <bgoing@micron.com> Signed-off-by: Brandon Capener <bcapener@micron.com> [wagi: cherry picked code] Signed-off-by: Daniel Wagner <wagi@kernel.org>
Introduce libnvme_free as the common deallocation API for memory allocated via libnvme_alloc() and libnvme_realloc(). This completes the basic memory management interface by providing a consistent wrapper for freeing libnvme-managed allocations. Signed-off-by: Daniel Wagner <wagi@kernel.org>
In case reallocarray is not available provide an open-coded version. Signed-off-by: Daniel Wagner <wagi@kernel.org>
getline is not available on all platforms. Since the UUID input has a fixed and well-defined format, use fgets instead. This avoids the dynamic allocation from getline and improves portability Signed-off-by: Daniel Wagner <wagi@kernel.org>
| if (!p) | ||
| return libnvme_alloc(len); | ||
|
|
||
| old_len = _msize(p); |
There was a problem hiding this comment.
I found today during some testing that _msize was incorrect for working with aligned allocations. There is an _aligned_msize that needs to be used instead.
| old_len = _msize(p); | |
| old_len = _aligned_msize(p, getpagesize(), 0); |
There was a problem hiding this comment.
alright, I'll pick up this version then :)
|
merged these patches separately. |
This is based on #3310 which does too many things in one patch.
Split the patch into baby steps and see how things go.