Skip to content

Commit 2e54f44

Browse files
committed
Rename utils_devdax_mmap to utils_mmap_file and improve it
Rename utils_devdax_mmap to utils_mmap_file and improve it: - add support for MAP_PRIVATE - add flags and fd_offset arguments Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 77f9221 commit 2e54f44

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

src/provider/provider_devdax_memory.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,13 @@ static umf_result_t devdax_initialize(void *params, void **provider) {
130130
goto err_free_devdax_provider;
131131
}
132132

133-
devdax_provider->base = utils_devdax_mmap(NULL, devdax_provider->size,
134-
devdax_provider->protection, fd);
133+
unsigned map_sync_flag = 0;
134+
utils_translate_mem_visibility_flag(UMF_MEM_MAP_SYNC, &map_sync_flag);
135+
136+
// mmap /dev/dax with the MAP_SYNC xor MAP_SHARED flag (if MAP_SYNC fails)
137+
devdax_provider->base = utils_mmap_file(NULL, devdax_provider->size,
138+
devdax_provider->protection,
139+
map_sync_flag, fd, 0 /* offset */);
135140
utils_close_fd(fd);
136141
if (devdax_provider->base == NULL) {
137142
LOG_PDEBUG("devdax memory mapping failed (path=%s, size=%zu)",
@@ -458,8 +463,13 @@ static umf_result_t devdax_open_ipc_handle(void *provider,
458463
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
459464
}
460465

461-
char *base = utils_devdax_mmap(NULL, devdax_provider->size,
462-
devdax_provider->protection, fd);
466+
unsigned map_sync_flag = 0;
467+
utils_translate_mem_visibility_flag(UMF_MEM_MAP_SYNC, &map_sync_flag);
468+
469+
// mmap /dev/dax with the MAP_SYNC xor MAP_SHARED flag (if MAP_SYNC fails)
470+
char *base = utils_mmap_file(NULL, devdax_provider->size,
471+
devdax_provider->protection, map_sync_flag, fd,
472+
0 /* offset */);
463473
if (base == NULL) {
464474
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED,
465475
errno);

src/utils/utils_common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ int utils_set_file_size(int fd, size_t size);
124124
void *utils_mmap(void *hint_addr, size_t length, int prot, int flag, int fd,
125125
size_t fd_offset);
126126

127-
void *utils_devdax_mmap(void *hint_addr, size_t length, int prot, int fd);
127+
void *utils_mmap_file(void *hint_addr, size_t length, int prot, int flags,
128+
int fd, size_t fd_offset);
128129

129130
int utils_munmap(void *addr, size_t length);
130131

src/utils/utils_linux_common.c

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,57 @@ utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
3939
}
4040

4141
/*
42-
* MMap a /dev/dax device.
43-
* First try to mmap with (MAP_SHARED_VALIDATE | MAP_SYNC) flags
44-
* which allows flushing from the user-space. If MAP_SYNC fails
45-
* try to mmap with MAP_SHARED flag (without MAP_SYNC).
42+
* Map given file into memory.
43+
* If (flags & MAP_PRIVATE) it uses just mmap. Otherwise, if (flags & MAP_SYNC)
44+
* it tries to mmap with (flags | MAP_SHARED_VALIDATE | MAP_SYNC)
45+
* which allows flushing from the user-space. If MAP_SYNC fails and the user
46+
* did not specify it by himself it tries to mmap with (flags | MAP_SHARED).
4647
*/
47-
void *utils_devdax_mmap(void *hint_addr, size_t length, int prot, int fd) {
48-
void *ptr = utils_mmap(hint_addr, length, prot,
49-
MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0);
50-
if (ptr) {
51-
LOG_DEBUG(
52-
"devdax mapped with the (MAP_SHARED_VALIDATE | MAP_SYNC) flags");
53-
return ptr;
48+
void *utils_mmap_file(void *hint_addr, size_t length, int prot, int flags,
49+
int fd, size_t fd_offset) {
50+
void *addr;
51+
52+
/*
53+
* MAP_PRIVATE and MAP_SHARED are mutually exclusive,
54+
* therefore mmap with MAP_PRIVATE is executed separately.
55+
*/
56+
if (flags & MAP_PRIVATE) {
57+
addr = utils_mmap(hint_addr, length, prot, flags, fd, fd_offset);
58+
if (addr == MAP_FAILED) {
59+
LOG_PERR("mapping file with the MAP_PRIVATE flag failed");
60+
return NULL;
61+
}
62+
63+
LOG_DEBUG("file mapped with the MAP_PRIVATE flag");
64+
return addr;
5465
}
5566

56-
ptr = utils_mmap(hint_addr, length, prot, MAP_SHARED, fd, 0);
57-
if (ptr) {
58-
LOG_DEBUG("devdax mapped with the MAP_SHARED flag");
59-
return ptr;
67+
errno = 0;
68+
69+
if (flags & MAP_SYNC) {
70+
/* try to mmap with MAP_SYNC flag */
71+
const int sync_flags = MAP_SHARED_VALIDATE | MAP_SYNC;
72+
addr = utils_mmap(hint_addr, length, prot, flags | sync_flags, fd,
73+
fd_offset);
74+
if (addr) {
75+
LOG_DEBUG("file mapped with the MAP_SYNC flag");
76+
return addr;
77+
}
6078
}
6179

80+
if ((!(flags & MAP_SYNC)) || errno == EINVAL || errno == ENOTSUP ||
81+
errno == EOPNOTSUPP) {
82+
/* try to mmap with MAP_SHARED flag (without MAP_SYNC) */
83+
addr = utils_mmap(hint_addr, length, prot, flags | MAP_SHARED, fd,
84+
fd_offset);
85+
if (addr) {
86+
LOG_DEBUG("file mapped with the MAP_SHARED flag");
87+
return addr;
88+
}
89+
}
90+
91+
LOG_PERR("memory mapping failed");
92+
6293
return NULL;
6394
}
6495

src/utils/utils_macosx_common.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
2929
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
3030
}
3131

32-
void *utils_devdax_mmap(void *hint_addr, size_t length, int prot, int fd) {
32+
void *utils_mmap_file(void *hint_addr, size_t length, int prot, int flags,
33+
int fd, size_t fd_offset) {
3334
(void)hint_addr; // unused
3435
(void)length; // unused
3536
(void)prot; // unused
37+
(void)flags; // unused
3638
(void)fd; // unused
39+
(void)fd_offset; // unused
3740
return NULL; // not supported
3841
}
3942

src/utils/utils_windows_common.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,15 @@ void *utils_mmap(void *hint_addr, size_t length, int prot, int flag, int fd,
147147
return VirtualAlloc(hint_addr, length, MEM_RESERVE | MEM_COMMIT, prot);
148148
}
149149

150-
void *utils_devdax_mmap(void *hint_addr, size_t length, int prot, int fd) {
150+
void *utils_mmap_file(void *hint_addr, size_t length, int prot, int flags,
151+
int fd, size_t fd_offset) {
151152
(void)hint_addr; // unused
152153
(void)length; // unused
153154
(void)prot; // unused
155+
(void)flags; // unused
154156
(void)fd; // unused
155-
return NULL; // not supported on Windows
157+
(void)fd_offset; // unused
158+
return NULL; // not supported
156159
}
157160

158161
int utils_munmap(void *addr, size_t length) {

0 commit comments

Comments
 (0)