Skip to content

Commit 43a7283

Browse files
bgoing-micron-ossigaw
authored andcommitted
nvme: Make mmap functionality conditional on mmap support
Bypass the mmap functionality in mmap_registers if the target platform doesn't support mmap. Create munmap_registers for conditional unmap usage as well. Use NVME_HAVE_MMAP to avoid conflict with Python's HAVE_MMAP definition when building Python wrappers. Signed-off-by: Broc Going <[email protected]>
1 parent 6797dc1 commit 43a7283

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ conf.set(
416416

417417
conf.set('NVME_HAVE_SENDFILE', cc.has_function('sendfile'))
418418

419+
conf.set('NVME_HAVE_MMAP', cc.has_function('mmap'))
420+
419421
project_config_file = 'nvme-config.h'
420422
nvme_config_h = configure_file(output: project_config_file, configuration: conf)
421423
config_dep = declare_dependency(

nvme.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@
4141

4242
#include <linux/fs.h>
4343

44-
#include <sys/ioctl.h>
44+
#ifdef NVME_HAVE_MMAP
4545
#include <sys/mman.h>
46+
#endif
47+
48+
#include <sys/ioctl.h>
4649
#include <sys/stat.h>
4750
#include <sys/types.h>
4851

@@ -257,6 +260,7 @@ struct nvme_args nvme_args = {
257260
};
258261

259262
static void *mmap_registers(struct libnvme_transport_handle *hdl, bool writable);
263+
static int munmap_registers(void *addr);
260264

261265
static OPT_VALS(feature_name) = {
262266
VAL_BYTE("arbitration", NVME_FEAT_FID_ARBITRATION),
@@ -1107,7 +1111,7 @@ static int get_effects_log(int argc, char **argv, struct command *acmd, struct p
11071111

11081112
if (bar) {
11091113
cap = mmio_read64(bar + NVME_REG_CAP);
1110-
munmap(bar, getpagesize());
1114+
munmap_registers(bar);
11111115
} else {
11121116
nvme_init_get_property(&cmd, NVME_REG_CAP);
11131117
err = libnvme_submit_admin_passthru(hdl, &cmd);
@@ -5802,8 +5806,9 @@ static int nvme_get_properties(struct libnvme_transport_handle *hdl, void **pbar
58025806

58035807
static void *mmap_registers(struct libnvme_transport_handle *hdl, bool writable)
58045808
{
5809+
void *membase = NULL;
5810+
#ifdef NVME_HAVE_MMAP
58055811
char path[512];
5806-
void *membase;
58075812
int fd;
58085813
int prot = PROT_READ;
58095814

@@ -5832,9 +5837,19 @@ static void *mmap_registers(struct libnvme_transport_handle *hdl, bool writable)
58325837
}
58335838

58345839
close(fd);
5840+
#endif
58355841
return membase;
58365842
}
58375843

5844+
static int munmap_registers(void *addr)
5845+
{
5846+
#ifdef NVME_HAVE_MMAP
5847+
return munmap(addr, getpagesize());
5848+
#else
5849+
return 0;
5850+
#endif
5851+
}
5852+
58385853
static int show_registers(int argc, char **argv, struct command *acmd, struct plugin *plugin)
58395854
{
58405855
const char *desc = "Reads and shows the defined NVMe controller registers\n"
@@ -5886,7 +5901,7 @@ static int show_registers(int argc, char **argv, struct command *acmd, struct pl
58865901
if (cfg.fabrics)
58875902
free(bar);
58885903
else
5889-
munmap(bar, getpagesize());
5904+
munmap_registers(bar);
58905905

58915906
return 0;
58925907
}
@@ -6171,7 +6186,7 @@ static int get_register(int argc, char **argv, struct command *acmd, struct plug
61716186
if (fabrics)
61726187
free(bar);
61736188
else
6174-
munmap(bar, getpagesize());
6189+
munmap_registers(bar);
61756190

61766191
return err;
61776192
}
@@ -6455,7 +6470,7 @@ static int set_register(int argc, char **argv, struct command *acmd, struct plug
64556470
err = set_register_names(hdl, bar, opts, &cfg);
64566471

64576472
if (bar)
6458-
munmap(bar, getpagesize());
6473+
munmap_registers(bar);
64596474

64606475
return err;
64616476
}

0 commit comments

Comments
 (0)