Skip to content

Commit

Permalink
linux: Allocate aligned payload for nvme_get_telemetry_log
Browse files Browse the repository at this point in the history
Use nvme_realloc to allocate alighed payload for
nvme_get_telemetry_log.
Fix a compilation warning in tree.c also.

Signed-off-by: Liang Yan <[email protected]>
  • Loading branch information
liang3zy22 committed Mar 20, 2024
1 parent 553c15e commit a9a6fa2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/nvme/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_

*size = 0;

log = malloc(xfer);
log = __nvme_alloc(xfer);
if (!log) {
errno = ENOMEM;
return -1;
Expand Down Expand Up @@ -239,7 +239,7 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_
}

*size = (dalb + 1) * xfer;
tmp = realloc(log, *size);
tmp = __nvme_realloc(log, *size);
if (!tmp) {
errno = ENOMEM;
return -1;
Expand Down
2 changes: 2 additions & 0 deletions src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,

void *__nvme_alloc(size_t len);

void *__nvme_realloc(void *p, size_t len);

#if (LOG_FUNCNAME == 1)
#define __nvme_log_func __func__
#else
Expand Down
2 changes: 1 addition & 1 deletion src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
{
nvme_ctrl_t c, p;
_cleanup_free_ char *addr = NULL, *address = NULL;
char *a, *e;
char *a = NULL, *e = NULL;
_cleanup_free_ char *transport = NULL;
char *traddr = NULL, *trsvcid = NULL;
char *host_traddr = NULL, *host_iface = NULL;
Expand Down
15 changes: 15 additions & 0 deletions src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <malloc.h>

#include <sys/stat.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1135,3 +1136,17 @@ void *__nvme_alloc(size_t len)
memset(p, 0, _len);
return p;
}

void *__nvme_realloc(void *p, size_t len)
{
size_t old_len = malloc_usable_size(p);

void *result = __nvme_alloc(len);

if (p) {
memcpy(result, p, min(old_len, len));
free(p);
}

return result;
}
2 changes: 2 additions & 0 deletions src/nvme/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ char *kv_keymatch(const char *kv, const char *key);
*/
char *startswith(const char *s, const char *prefix);

#define min(x, y) ((x) > (y) ? (y) : (x))

#define __round_mask(val, mult) ((__typeof__(val))((mult)-1))

/**
Expand Down

0 comments on commit a9a6fa2

Please sign in to comment.