@@ -31,29 +31,65 @@ utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
3131 case UMF_MEM_MAP_SHARED :
3232 * out_flag = MAP_SHARED ;
3333 return UMF_RESULT_SUCCESS ;
34+ case UMF_MEM_MAP_SYNC :
35+ * out_flag = MAP_SYNC ;
36+ return UMF_RESULT_SUCCESS ;
3437 }
3538 return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
3639}
3740
3841/*
39- * MMap a /dev/dax device.
40- * First try to mmap with (MAP_SHARED_VALIDATE | MAP_SYNC) flags
41- * which allows flushing from the user-space. If MAP_SYNC fails
42- * 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).
4347 */
44- void * utils_devdax_mmap (void * hint_addr , size_t length , int prot , int fd ) {
45- void * ptr = utils_mmap (hint_addr , length , prot ,
46- MAP_SHARED_VALIDATE | MAP_SYNC , fd , 0 );
47- if (ptr ) {
48- LOG_DEBUG (
49- "devdax mapped with the (MAP_SHARED_VALIDATE | MAP_SYNC) flags" );
50- return ptr ;
51- }
52-
53- ptr = utils_mmap (hint_addr , length , prot , MAP_SHARED , fd , 0 );
54- if (ptr ) {
55- LOG_DEBUG ("devdax mapped with the MAP_SHARED flag" );
56- 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 ;
65+ }
66+
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+ }
78+
79+ LOG_PERR ("mapping file with the MAP_SYNC flag failed" );
80+ }
81+
82+ if ((!(flags & MAP_SYNC )) || errno == EINVAL || errno == ENOTSUP ||
83+ errno == EOPNOTSUPP ) {
84+ /* try to mmap with MAP_SHARED flag (without MAP_SYNC) */
85+ const int shared_flags = (flags & (~MAP_SYNC )) | MAP_SHARED ;
86+ addr = utils_mmap (hint_addr , length , prot , shared_flags , fd , fd_offset );
87+ if (addr ) {
88+ LOG_DEBUG ("file mapped with the MAP_SHARED flag" );
89+ return addr ;
90+ }
91+
92+ LOG_PERR ("mapping file with the MAP_SHARED flag failed" );
5793 }
5894
5995 return NULL ;
0 commit comments