@@ -397,16 +397,21 @@ static umf_result_t file_alloc(void *provider, size_t size, size_t alignment,
397
397
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
398
398
}
399
399
400
- // alignment must be a power of two and a multiple of sizeof(void *)
401
- if (alignment &&
402
- ((alignment & (alignment - 1 )) || (alignment % sizeof (void * )))) {
403
- LOG_ERR ("wrong alignment: %zu (not a power of 2 or a multiple of "
404
- "sizeof(void *))" ,
405
- alignment );
400
+ file_memory_provider_t * file_provider = (file_memory_provider_t * )provider ;
401
+
402
+ // alignment must be a power of two and a multiple or a divider of the page size
403
+ if (alignment && ((alignment & (alignment - 1 )) ||
404
+ ((alignment % file_provider -> page_size ) &&
405
+ (file_provider -> page_size % alignment )))) {
406
+ LOG_ERR ("wrong alignment: %zu (not a power of 2 or a multiple or a "
407
+ "divider of the page size (%zu))" ,
408
+ alignment , file_provider -> page_size );
406
409
return UMF_RESULT_ERROR_INVALID_ALIGNMENT ;
407
410
}
408
411
409
- file_memory_provider_t * file_provider = (file_memory_provider_t * )provider ;
412
+ if (IS_NOT_ALIGNED (alignment , file_provider -> page_size )) {
413
+ alignment = ALIGN_UP (alignment , file_provider -> page_size );
414
+ }
410
415
411
416
void * addr = NULL ;
412
417
size_t alloc_offset_fd ; // needed for critnib_insert()
@@ -578,6 +583,8 @@ typedef struct file_ipc_data_t {
578
583
char path [PATH_MAX ];
579
584
size_t offset_fd ;
580
585
size_t size ;
586
+ unsigned protection ; // combination of OS-specific protection flags
587
+ unsigned visibility ; // memory visibility mode
581
588
} file_ipc_data_t ;
582
589
583
590
static umf_result_t file_get_ipc_handle_size (void * provider , size_t * size ) {
@@ -623,6 +630,8 @@ static umf_result_t file_get_ipc_handle(void *provider, const void *ptr,
623
630
file_ipc_data -> size = size ;
624
631
strncpy (file_ipc_data -> path , file_provider -> path , PATH_MAX - 1 );
625
632
file_ipc_data -> path [PATH_MAX - 1 ] = '\0' ;
633
+ file_ipc_data -> protection = file_provider -> protection ;
634
+ file_ipc_data -> visibility = file_provider -> visibility ;
626
635
627
636
return UMF_RESULT_SUCCESS ;
628
637
}
@@ -672,16 +681,31 @@ static umf_result_t file_open_ipc_handle(void *provider, void *providerIpcData,
672
681
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
673
682
}
674
683
675
- * ptr = utils_mmap_file (NULL , file_ipc_data -> size , file_provider -> protection ,
676
- file_provider -> visibility , fd ,
677
- file_ipc_data -> offset_fd );
684
+ // length and offset passed to mmap() have to be page-aligned
685
+ size_t offset_aligned = file_ipc_data -> offset_fd ;
686
+ size_t size_aligned = file_ipc_data -> size ;
687
+ utils_align_ptr_down_size_up ((void * * )& offset_aligned , & size_aligned ,
688
+ file_provider -> page_size );
689
+
690
+ char * addr = utils_mmap_file (NULL , size_aligned , file_ipc_data -> protection ,
691
+ file_ipc_data -> visibility , fd , offset_aligned );
678
692
(void )utils_close_fd (fd );
679
- if (* ptr == NULL ) {
693
+ if (addr == NULL ) {
680
694
file_store_last_native_error (UMF_FILE_RESULT_ERROR_ALLOC_FAILED , errno );
681
- LOG_PERR ("memory mapping failed" );
682
- ret = UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
695
+ LOG_PERR ("file mapping failed (path: %s, size: %zu, protection: %i, "
696
+ "fd: %i, offset: %zu)" ,
697
+ file_ipc_data -> path , size_aligned , file_ipc_data -> protection ,
698
+ fd , offset_aligned );
699
+ return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC ;
683
700
}
684
701
702
+ * ptr = addr + (file_ipc_data -> offset_fd - offset_aligned );
703
+
704
+ LOG_DEBUG ("file mapped (path: %s, size: %zu, protection: %i, fd: %i, "
705
+ "offset: %zu) at address %p" ,
706
+ file_ipc_data -> path , size_aligned , file_ipc_data -> protection , fd ,
707
+ offset_aligned , * ptr );
708
+
685
709
return ret ;
686
710
}
687
711
@@ -698,6 +722,9 @@ static umf_result_t file_close_ipc_handle(void *provider, void *ptr,
698
722
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
699
723
}
700
724
725
+ // ptr and size passed to munmap() have to be page-aligned in case of /dev/dax device
726
+ utils_align_ptr_down_size_up (& ptr , & size , file_provider -> page_size );
727
+
701
728
errno = 0 ;
702
729
int ret = utils_munmap (ptr , size );
703
730
// ignore error when size == 0
0 commit comments