@@ -31,29 +31,65 @@ utils_translate_mem_visibility_flag(umf_memory_visibility_t in_flag,
31
31
case UMF_MEM_MAP_SHARED :
32
32
* out_flag = MAP_SHARED ;
33
33
return UMF_RESULT_SUCCESS ;
34
+ case UMF_MEM_MAP_SYNC :
35
+ * out_flag = MAP_SYNC ;
36
+ return UMF_RESULT_SUCCESS ;
34
37
}
35
38
return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
36
39
}
37
40
38
41
/*
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).
43
47
*/
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" );
57
93
}
58
94
59
95
return NULL ;
0 commit comments