Skip to content

Commit

Permalink
nvme: do not scan topology when mmaping registers
Browse files Browse the repository at this point in the history
For accessing the registers we don't need to scan the topology. With the
device name we already know where to lookup the resource0 file in sysfs.

We also don't need to do the fallback lookup via the namespaces because
Linux kernel v4.0 has introduced the link in /sys/class/nvme/nvme%d. And
nvme-cli 2.x depends on Linux kernel >= 4.15, so it's safe to drop the
fallback.

This avoids all the topology scanning which is excessive and also might
issue an 'id command' via the nuse sysfs entry. So let's not do it.

Signed-off-by: Daniel Wagner <[email protected]>
  • Loading branch information
igaw committed Mar 19, 2024
1 parent aac3e51 commit a31081a
Showing 1 changed file with 9 additions and 30 deletions.
39 changes: 9 additions & 30 deletions nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ static const char space[51] = {[0 ... 49] = ' ', '\0'};
static char *output_format_val = "normal";
int verbose_level;

static void *mmap_registers(nvme_root_t r, struct nvme_dev *dev);
static void *mmap_registers(struct nvme_dev *dev);

const char *nvme_strerror(int errnum)
{
Expand Down Expand Up @@ -984,7 +984,7 @@ static int get_effects_log(int argc, char **argv, struct command *cmd, struct pl
__u64 cap;

r = nvme_scan(NULL);
bar = mmap_registers(r, dev);
bar = mmap_registers(dev);
nvme_free_tree(r);

if (bar) {
Expand Down Expand Up @@ -5249,31 +5249,13 @@ static int nvme_get_properties(int fd, void **pbar)
return err;
}

static void *mmap_registers(nvme_root_t r, struct nvme_dev *dev)
static void *mmap_registers(struct nvme_dev *dev)
{
nvme_ctrl_t c = NULL;
nvme_ns_t n = NULL;

char path[512];
void *membase;
int fd;

c = nvme_scan_ctrl(r, dev->name);
if (c) {
snprintf(path, sizeof(path), "%s/device/resource0",
nvme_ctrl_get_sysfs_dir(c));
nvme_free_ctrl(c);
} else {
n = nvme_scan_namespace(dev->name);
if (!n) {
nvme_show_error("Unable to find %s", dev->name);
return NULL;
}
snprintf(path, sizeof(path), "%s/device/device/resource0",
nvme_ns_get_sysfs_dir(n));
nvme_free_ns(n);
}

sprintf(path, "/sys/class/nvme/%s/device/resource0", dev->name);
fd = open(path, O_RDONLY);
if (fd < 0) {
if (log_level >= LOG_DEBUG)
Expand Down Expand Up @@ -5305,7 +5287,6 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
enum nvme_print_flags flags;
bool fabrics = false;
nvme_root_t r;
void *bar;
int err;

Expand All @@ -5324,21 +5305,20 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu
if (err)
return err;

r = nvme_scan(NULL);
err = validate_output_format(output_format_val, &flags);
if (err < 0) {
nvme_show_error("Invalid output format");
goto free_tree;
return err;
}

if (cfg.human_readable)
flags |= VERBOSE;

bar = mmap_registers(r, dev);
bar = mmap_registers(dev);
if (!bar) {
err = nvme_get_properties(dev_fd(dev), &bar);
if (err)
goto free_tree;
return err;
fabrics = true;
}

Expand All @@ -5347,9 +5327,8 @@ static int show_registers(int argc, char **argv, struct command *cmd, struct plu
free(bar);
else
munmap(bar, getpagesize());
free_tree:
nvme_free_tree(r);
return err;

return 0;
}

static int get_property(int argc, char **argv, struct command *cmd, struct plugin *plugin)
Expand Down

0 comments on commit a31081a

Please sign in to comment.