Skip to content

Commit 18dee99

Browse files
committed
Fix devdax_open_ipc_handle() and devdax_close_ipc_handle()
devdax_open_ipc_handle() has to use the path of the remote /dev/dax got from the IPC handle, not the local one. devdax_open_ipc_handle() has to use also the memory protection got from the IPC handle, so let's add the memory protection to the IPC handle. devdax_open_ipc_handle() should mmap only the required range of memory, not the whole /dev/dax device, so let's add the length of the allocation to the IPC handle. devdax_close_ipc_handle() should unmap only the required range of memory, not the whole /dev/dax device. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent d72a841 commit 18dee99

File tree

1 file changed

+40
-53
lines changed

1 file changed

+40
-53
lines changed

src/provider/provider_devdax_memory.c

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ umf_memory_provider_ops_t *umfDevDaxMemoryProviderOps(void) {
3131
#include "utils_concurrency.h"
3232
#include "utils_log.h"
3333

34-
#define NODESET_STR_BUF_LEN 1024
34+
#define DEVDAX_ALIGNMENT_2MB (2 * 1024 * 1024) // == 2 MB
3535

3636
#define TLS_MSG_BUF_LEN 1024
3737

@@ -369,9 +369,11 @@ static umf_result_t devdax_allocation_merge(void *provider, void *lowPtr,
369369
}
370370

371371
typedef struct devdax_ipc_data_t {
372-
char dd_path[PATH_MAX]; // path to the /dev/dax
373-
size_t dd_size; // size of the /dev/dax
374-
size_t offset; // offset of the data
372+
char path[PATH_MAX]; // path to the /dev/dax
373+
unsigned protection; // combination of OS-specific memory protection flags
374+
// offset of the data (from the beginning of the devdax mapping) - see devdax_get_ipc_handle()
375+
size_t offset;
376+
size_t length; // length of the data
375377
} devdax_ipc_data_t;
376378

377379
static umf_result_t devdax_get_ipc_handle_size(void *provider, size_t *size) {
@@ -386,8 +388,6 @@ static umf_result_t devdax_get_ipc_handle_size(void *provider, size_t *size) {
386388

387389
static umf_result_t devdax_get_ipc_handle(void *provider, const void *ptr,
388390
size_t size, void *providerIpcData) {
389-
(void)size; // unused
390-
391391
if (provider == NULL || ptr == NULL || providerIpcData == NULL) {
392392
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
393393
}
@@ -396,11 +396,12 @@ static umf_result_t devdax_get_ipc_handle(void *provider, const void *ptr,
396396
(devdax_memory_provider_t *)provider;
397397

398398
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
399+
strncpy(devdax_ipc_data->path, devdax_provider->path, PATH_MAX - 1);
400+
devdax_ipc_data->path[PATH_MAX - 1] = '\0';
401+
devdax_ipc_data->protection = devdax_provider->protection;
399402
devdax_ipc_data->offset =
400403
(size_t)((uintptr_t)ptr - (uintptr_t)devdax_provider->base);
401-
strncpy(devdax_ipc_data->dd_path, devdax_provider->path, PATH_MAX - 1);
402-
devdax_ipc_data->dd_path[PATH_MAX - 1] = '\0';
403-
devdax_ipc_data->dd_size = devdax_provider->size;
404+
devdax_ipc_data->length = size;
404405

405406
return UMF_RESULT_SUCCESS;
406407
}
@@ -416,16 +417,9 @@ static umf_result_t devdax_put_ipc_handle(void *provider,
416417
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
417418

418419
// verify the path of the /dev/dax
419-
if (strncmp(devdax_ipc_data->dd_path, devdax_provider->path, PATH_MAX)) {
420+
if (strncmp(devdax_ipc_data->path, devdax_provider->path, PATH_MAX)) {
420421
LOG_ERR("devdax path mismatch (local: %s, ipc: %s)",
421-
devdax_provider->path, devdax_ipc_data->dd_path);
422-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
423-
}
424-
425-
// verify the size of the /dev/dax
426-
if (devdax_ipc_data->dd_size != devdax_provider->size) {
427-
LOG_ERR("devdax size mismatch (local: %zu, ipc: %zu)",
428-
devdax_provider->size, devdax_ipc_data->dd_size);
422+
devdax_provider->path, devdax_ipc_data->path);
429423
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
430424
}
431425

@@ -438,58 +432,51 @@ static umf_result_t devdax_open_ipc_handle(void *provider,
438432
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
439433
}
440434

441-
devdax_memory_provider_t *devdax_provider =
442-
(devdax_memory_provider_t *)provider;
443435
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
444436

445-
// verify it is the same devdax - first verify the path
446-
if (strncmp(devdax_ipc_data->dd_path, devdax_provider->path, PATH_MAX)) {
447-
LOG_ERR("devdax path mismatch (local: %s, ipc: %s)",
448-
devdax_provider->path, devdax_ipc_data->dd_path);
449-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
450-
}
451-
452-
// verify the size of the /dev/dax
453-
if (devdax_ipc_data->dd_size != devdax_provider->size) {
454-
LOG_ERR("devdax size mismatch (local: %zu, ipc: %zu)",
455-
devdax_provider->size, devdax_ipc_data->dd_size);
456-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
457-
}
458-
459-
umf_result_t ret = UMF_RESULT_SUCCESS;
460-
int fd = utils_devdax_open(devdax_provider->path);
437+
int fd = utils_devdax_open(devdax_ipc_data->path);
461438
if (fd == -1) {
462-
LOG_PERR("opening a devdax (%s) failed", devdax_provider->path);
439+
LOG_PERR("opening the devdax (%s) failed", devdax_ipc_data->path);
463440
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
464441
}
465442

466443
unsigned map_sync_flag = 0;
467444
utils_translate_mem_visibility_flag(UMF_MEM_MAP_SYNC, &map_sync_flag);
468445

446+
// length and offset passed to mmap() have to be 2MB-aligned in case of /dev/dax device
447+
size_t offset_aligned = devdax_ipc_data->offset;
448+
size_t length_aligned = devdax_ipc_data->length;
449+
utils_align_ptr_down_size_up((void **)&offset_aligned, &length_aligned,
450+
DEVDAX_ALIGNMENT_2MB);
451+
469452
// 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 */);
473-
if (base == NULL) {
453+
char *addr =
454+
utils_mmap_file(NULL, length_aligned, devdax_ipc_data->protection,
455+
map_sync_flag, fd, offset_aligned);
456+
if (addr == NULL) {
474457
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED,
475458
errno);
476459
LOG_PERR("devdax mapping failed (path: %s, size: %zu, protection: %i, "
477-
"fd: %i)",
478-
devdax_provider->path, devdax_provider->size,
479-
devdax_provider->protection, fd);
480-
ret = UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
460+
"fd: %i, offset: %zu)",
461+
devdax_ipc_data->path, length_aligned,
462+
devdax_ipc_data->protection, fd, offset_aligned);
463+
464+
*ptr = NULL;
465+
(void)utils_close_fd(fd);
466+
467+
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
481468
}
482469

483470
LOG_DEBUG("devdax mapped (path: %s, size: %zu, protection: %i, fd: %i, "
484471
"offset: %zu)",
485-
devdax_provider->path, devdax_provider->size,
486-
devdax_provider->protection, fd, devdax_ipc_data->offset);
472+
devdax_ipc_data->path, length_aligned,
473+
devdax_ipc_data->protection, fd, offset_aligned);
487474

488-
(void)utils_close_fd(fd);
475+
*ptr = addr + (devdax_ipc_data->offset - offset_aligned);
489476

490-
*ptr = base + devdax_ipc_data->offset;
477+
(void)utils_close_fd(fd);
491478

492-
return ret;
479+
return UMF_RESULT_SUCCESS;
493480
}
494481

495482
static umf_result_t devdax_close_ipc_handle(void *provider, void *ptr,
@@ -498,11 +485,11 @@ static umf_result_t devdax_close_ipc_handle(void *provider, void *ptr,
498485
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
499486
}
500487

501-
devdax_memory_provider_t *devdax_provider =
502-
(devdax_memory_provider_t *)provider;
488+
// ptr and size passed to munmap() have to be 2MB-aligned in case of /dev/dax device
489+
utils_align_ptr_down_size_up(&ptr, &size, DEVDAX_ALIGNMENT_2MB);
503490

504491
errno = 0;
505-
int ret = utils_munmap(devdax_provider->base, devdax_provider->size);
492+
int ret = utils_munmap(ptr, size);
506493
// ignore error when size == 0
507494
if (ret && (size > 0)) {
508495
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_FREE_FAILED,

0 commit comments

Comments
 (0)