Skip to content

Commit 5ce9442

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 11e94da commit 5ce9442

File tree

1 file changed

+43
-52
lines changed

1 file changed

+43
-52
lines changed

src/provider/provider_devdax_memory.c

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,10 @@ 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+
size_t offset; // offset of the data
375+
size_t length; // length of the data
375376
} devdax_ipc_data_t;
376377

377378
static umf_result_t devdax_get_ipc_handle_size(void *provider, size_t *size) {
@@ -386,8 +387,6 @@ static umf_result_t devdax_get_ipc_handle_size(void *provider, size_t *size) {
386387

387388
static umf_result_t devdax_get_ipc_handle(void *provider, const void *ptr,
388389
size_t size, void *providerIpcData) {
389-
(void)size; // unused
390-
391390
if (provider == NULL || ptr == NULL || providerIpcData == NULL) {
392391
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
393392
}
@@ -396,11 +395,12 @@ static umf_result_t devdax_get_ipc_handle(void *provider, const void *ptr,
396395
(devdax_memory_provider_t *)provider;
397396

398397
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
398+
strncpy(devdax_ipc_data->path, devdax_provider->path, PATH_MAX - 1);
399+
devdax_ipc_data->path[PATH_MAX - 1] = '\0';
400+
devdax_ipc_data->protection = devdax_provider->protection;
399401
devdax_ipc_data->offset =
400402
(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;
403+
devdax_ipc_data->length = size;
404404

405405
return UMF_RESULT_SUCCESS;
406406
}
@@ -416,16 +416,9 @@ static umf_result_t devdax_put_ipc_handle(void *provider,
416416
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
417417

418418
// verify the path of the /dev/dax
419-
if (strncmp(devdax_ipc_data->dd_path, devdax_provider->path, PATH_MAX)) {
419+
if (strncmp(devdax_ipc_data->path, devdax_provider->path, PATH_MAX)) {
420420
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);
421+
devdax_provider->path, devdax_ipc_data->path);
429422
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
430423
}
431424

@@ -438,58 +431,52 @@ static umf_result_t devdax_open_ipc_handle(void *provider,
438431
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
439432
}
440433

441-
devdax_memory_provider_t *devdax_provider =
442-
(devdax_memory_provider_t *)provider;
443434
devdax_ipc_data_t *devdax_ipc_data = (devdax_ipc_data_t *)providerIpcData;
444435

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);
436+
int fd = utils_devdax_open(devdax_ipc_data->path);
461437
if (fd == -1) {
462-
LOG_PERR("opening a devdax (%s) failed", devdax_provider->path);
438+
LOG_PERR("opening the devdax (%s) failed", devdax_ipc_data->path);
463439
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
464440
}
465441

466442
unsigned map_sync_flag = 0;
467443
utils_translate_mem_visibility_flag(UMF_MEM_MAP_SYNC, &map_sync_flag);
468444

445+
// length and offset passed to mmap() have to be page-aligned
446+
size_t page_size = utils_get_page_size();
447+
size_t offset_aligned = ALIGN_DOWN(devdax_ipc_data->offset, page_size);
448+
size_t offset_diff = devdax_ipc_data->offset - offset_aligned;
449+
size_t new_length = devdax_ipc_data->length + offset_diff;
450+
size_t length_aligned = ALIGN_UP(new_length, page_size);
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, devdax_ipc_data->length,
462+
devdax_ipc_data->protection, fd, devdax_ipc_data->offset);
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, devdax_ipc_data->length,
473+
devdax_ipc_data->protection, fd, devdax_ipc_data->offset);
487474

488-
(void)utils_close_fd(fd);
475+
*ptr = addr + offset_diff;
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,15 @@ 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 page-aligned
489+
size_t page_size = utils_get_page_size();
490+
void *ptr_aligned = (void *)ALIGN_DOWN((uintptr_t)ptr, page_size);
491+
size_t ptr_diff = (uintptr_t)ptr - (uintptr_t)ptr_aligned;
492+
size_t new_size = size + ptr_diff;
493+
size_t size_aligned = ALIGN_UP(new_size, page_size);
503494

504495
errno = 0;
505-
int ret = utils_munmap(devdax_provider->base, devdax_provider->size);
496+
int ret = utils_munmap(ptr_aligned, size_aligned);
506497
// ignore error when size == 0
507498
if (ret && (size > 0)) {
508499
devdax_store_last_native_error(UMF_DEVDAX_RESULT_ERROR_FREE_FAILED,

0 commit comments

Comments
 (0)