Skip to content

libnvme: abstract memory (de)allocation API#3351

Closed
igaw wants to merge 10 commits into
linux-nvme:masterfrom
igaw:win
Closed

libnvme: abstract memory (de)allocation API#3351
igaw wants to merge 10 commits into
linux-nvme:masterfrom
igaw:win

Conversation

@igaw
Copy link
Copy Markdown
Collaborator

@igaw igaw commented May 12, 2026

This is based on #3310 which does too many things in one patch.
Split the patch into baby steps and see how things go.

  • replace dprintf
  • platform support for mem allocation helpers
  • [ ]

@igaw igaw changed the title libnvme: refactor/restructe/reshuffle code for windows libnvme: refactor/restructure/reshuffle code for windows May 12, 2026
@igaw igaw force-pushed the win branch 4 times, most recently from 337ff92 to 50cb74c Compare May 12, 2026 14:27
Sort the entries alphabetically.

Signed-off-by: Daniel Wagner <wagi@kernel.org>
@igaw igaw force-pushed the win branch 3 times, most recently from b92f51a to b8d4b43 Compare May 12, 2026 15:24
@igaw igaw requested a review from Copilot May 12, 2026 16:37
@igaw igaw changed the title libnvme: refactor/restructure/reshuffle code for windows libnvme: abstract memory (de)allocation API May 12, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 to libnvme_alloc(), libnvme_realloc(), and libnvme_alloc_huge() (plus struct libnvme_mem_huge).
  • Introduce nvme/mem.h in libnvme and add Linux/Windows implementations (mem-linux.c, mem-win.c), exporting the new symbols.
  • Replace dprintf()-based logging with asprintf() + 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.

Comment thread libnvme/src/nvme/mem-win.c Outdated
Comment thread libnvme/src/nvme/mem-win.c
Comment thread libnvme/src/nvme/tree.c Outdated
Comment thread libnvme/src/nvme/log.c
@igaw igaw force-pushed the win branch 2 times, most recently from 9cfdcb3 to 42cd15a Compare May 12, 2026 17:48
@igaw igaw requested a review from Copilot May 12, 2026 17:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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: ctrl is non-NULL on successful allocation, so the function always returns false and never calls nvme_identify_ctrl(). This should be if (!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 p is NULL this is undefined behavior, and it also violates the documented behavior that NULL should act like libnvme_alloc(). Add an early if (!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.

Comment thread util/table.c Outdated
Comment thread util/table.c
Comment thread libnvme/src/nvme/mem-win.c
Comment thread libnvme/src/nvme/log.c
igaw and others added 9 commits May 12, 2026 20:08
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);
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.

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.

Suggested change
old_len = _msize(p);
old_len = _aligned_msize(p, getpagesize(), 0);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

alright, I'll pick up this version then :)

@igaw
Copy link
Copy Markdown
Collaborator Author

igaw commented May 13, 2026

merged these patches separately.

@igaw igaw closed this May 13, 2026
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