@@ -378,9 +378,11 @@ static umf_result_t devdax_allocation_merge(void *provider, void *lowPtr,
378378}
379379
380380typedef struct devdax_ipc_data_t {
381- char dd_path [PATH_MAX ]; // path to the /dev/dax
382- size_t dd_size ; // size of the /dev/dax
383- size_t offset ; // offset of the data
381+ char path [PATH_MAX ]; // path to the /dev/dax
382+ unsigned protection ; // combination of OS-specific memory protection flags
383+ // offset of the data (from the beginning of the devdax mapping) - see devdax_get_ipc_handle()
384+ size_t offset ;
385+ size_t length ; // length of the data
384386} devdax_ipc_data_t ;
385387
386388static umf_result_t devdax_get_ipc_handle_size (void * provider , size_t * size ) {
@@ -395,8 +397,6 @@ static umf_result_t devdax_get_ipc_handle_size(void *provider, size_t *size) {
395397
396398static umf_result_t devdax_get_ipc_handle (void * provider , const void * ptr ,
397399 size_t size , void * providerIpcData ) {
398- (void )size ; // unused
399-
400400 if (provider == NULL || ptr == NULL || providerIpcData == NULL ) {
401401 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
402402 }
@@ -405,11 +405,12 @@ static umf_result_t devdax_get_ipc_handle(void *provider, const void *ptr,
405405 (devdax_memory_provider_t * )provider ;
406406
407407 devdax_ipc_data_t * devdax_ipc_data = (devdax_ipc_data_t * )providerIpcData ;
408+ strncpy (devdax_ipc_data -> path , devdax_provider -> path , PATH_MAX - 1 );
409+ devdax_ipc_data -> path [PATH_MAX - 1 ] = '\0' ;
410+ devdax_ipc_data -> protection = devdax_provider -> protection ;
408411 devdax_ipc_data -> offset =
409412 (size_t )((uintptr_t )ptr - (uintptr_t )devdax_provider -> base );
410- strncpy (devdax_ipc_data -> dd_path , devdax_provider -> path , PATH_MAX - 1 );
411- devdax_ipc_data -> dd_path [PATH_MAX - 1 ] = '\0' ;
412- devdax_ipc_data -> dd_size = devdax_provider -> size ;
413+ devdax_ipc_data -> length = size ;
413414
414415 return UMF_RESULT_SUCCESS ;
415416}
@@ -425,16 +426,9 @@ static umf_result_t devdax_put_ipc_handle(void *provider,
425426 devdax_ipc_data_t * devdax_ipc_data = (devdax_ipc_data_t * )providerIpcData ;
426427
427428 // verify the path of the /dev/dax
428- if (strncmp (devdax_ipc_data -> dd_path , devdax_provider -> path , PATH_MAX )) {
429+ if (strncmp (devdax_ipc_data -> path , devdax_provider -> path , PATH_MAX )) {
429430 LOG_ERR ("devdax path mismatch (local: %s, ipc: %s)" ,
430- devdax_provider -> path , devdax_ipc_data -> dd_path );
431- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
432- }
433-
434- // verify the size of the /dev/dax
435- if (devdax_ipc_data -> dd_size != devdax_provider -> size ) {
436- LOG_ERR ("devdax size mismatch (local: %zu, ipc: %zu)" ,
437- devdax_provider -> size , devdax_ipc_data -> dd_size );
431+ devdax_provider -> path , devdax_ipc_data -> path );
438432 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
439433 }
440434
@@ -447,58 +441,57 @@ static umf_result_t devdax_open_ipc_handle(void *provider,
447441 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
448442 }
449443
450- devdax_memory_provider_t * devdax_provider =
451- (devdax_memory_provider_t * )provider ;
452444 devdax_ipc_data_t * devdax_ipc_data = (devdax_ipc_data_t * )providerIpcData ;
453445
454- // verify it is the same devdax - first verify the path
455- if (strncmp (devdax_ipc_data -> dd_path , devdax_provider -> path , PATH_MAX )) {
456- LOG_ERR ("devdax path mismatch (local: %s, ipc: %s) " ,
457- devdax_provider -> path , devdax_ipc_data -> dd_path );
446+ // length and offset passed to mmap() have to be page-aligned
447+ if (IS_NOT_ALIGNED (devdax_ipc_data -> offset , DEVDAX_PAGE_SIZE_2MB )) {
448+ LOG_ERR ("incorrect offset (%zu) in IPC handle - it is not page-aligned " ,
449+ devdax_ipc_data -> offset );
458450 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
459451 }
460452
461- // verify the size of the /dev/dax
462- if (devdax_ipc_data -> dd_size != devdax_provider -> size ) {
463- LOG_ERR ("devdax size mismatch (local: %zu, ipc: %zu)" ,
464- devdax_provider -> size , devdax_ipc_data -> dd_size );
465- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
466- }
453+ // length and offset passed to mmap() have to be page-aligned
454+ size_t length_aligned = devdax_ipc_data -> length ;
455+ utils_align_ptr_down_size_up ((void * * )& devdax_ipc_data -> offset ,
456+ & length_aligned , DEVDAX_PAGE_SIZE_2MB );
467457
468- umf_result_t ret = UMF_RESULT_SUCCESS ;
469- int fd = utils_devdax_open (devdax_provider -> path );
458+ int fd = utils_devdax_open (devdax_ipc_data -> path );
470459 if (fd == -1 ) {
471- LOG_PERR ("opening a devdax (%s) failed" , devdax_provider -> path );
460+ LOG_PERR ("opening the devdax (%s) failed" , devdax_ipc_data -> path );
472461 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
473462 }
474463
475464 unsigned map_sync_flag = 0 ;
476465 utils_translate_mem_visibility_flag (UMF_MEM_MAP_SYNC , & map_sync_flag );
477466
478467 // mmap /dev/dax with the MAP_SYNC xor MAP_SHARED flag (if MAP_SYNC fails)
479- char * base = utils_mmap_file ( NULL , devdax_provider -> size ,
480- devdax_provider -> protection , map_sync_flag , fd ,
481- 0 /* offset */ );
482- if (base == NULL ) {
468+ char * addr =
469+ utils_mmap_file ( NULL , length_aligned , devdax_ipc_data -> protection ,
470+ map_sync_flag , fd , devdax_ipc_data -> offset );
471+ if (addr == NULL ) {
483472 devdax_store_last_native_error (UMF_DEVDAX_RESULT_ERROR_ALLOC_FAILED ,
484473 errno );
485474 LOG_PERR ("devdax mapping failed (path: %s, size: %zu, protection: %i, "
486- "fd: %i)" ,
487- devdax_provider -> path , devdax_provider -> size ,
488- devdax_provider -> protection , fd );
489- ret = UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
475+ "fd: %i, offset: %zu)" ,
476+ devdax_ipc_data -> path , length_aligned ,
477+ devdax_ipc_data -> protection , fd , devdax_ipc_data -> offset );
478+
479+ * ptr = NULL ;
480+ (void )utils_close_fd (fd );
481+
482+ return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
490483 }
491484
492485 LOG_DEBUG ("devdax mapped (path: %s, size: %zu, protection: %i, fd: %i, "
493486 "offset: %zu)" ,
494- devdax_provider -> path , devdax_provider -> size ,
495- devdax_provider -> protection , fd , devdax_ipc_data -> offset );
487+ devdax_ipc_data -> path , length_aligned ,
488+ devdax_ipc_data -> protection , fd , devdax_ipc_data -> offset );
496489
497- ( void ) utils_close_fd ( fd ) ;
490+ * ptr = addr ;
498491
499- * ptr = base + devdax_ipc_data -> offset ;
492+ ( void ) utils_close_fd ( fd ) ;
500493
501- return ret ;
494+ return UMF_RESULT_SUCCESS ;
502495}
503496
504497static umf_result_t devdax_close_ipc_handle (void * provider , void * ptr ,
@@ -507,11 +500,16 @@ static umf_result_t devdax_close_ipc_handle(void *provider, void *ptr,
507500 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
508501 }
509502
510- devdax_memory_provider_t * devdax_provider =
511- (devdax_memory_provider_t * )provider ;
503+ // ptr and size passed to munmap() have to be page-aligned
504+ if (IS_NOT_ALIGNED ((uintptr_t )ptr , DEVDAX_PAGE_SIZE_2MB )) {
505+ LOG_ERR ("incorrect ptr (%p) - it is not page-aligned" , ptr );
506+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
507+ }
508+
509+ utils_align_ptr_down_size_up (& ptr , & size , DEVDAX_PAGE_SIZE_2MB );
512510
513511 errno = 0 ;
514- int ret = utils_munmap (devdax_provider -> base , devdax_provider -> size );
512+ int ret = utils_munmap (ptr , size );
515513 // ignore error when size == 0
516514 if (ret && (size > 0 )) {
517515 devdax_store_last_native_error (UMF_DEVDAX_RESULT_ERROR_FREE_FAILED ,
0 commit comments