Line data Source code
1 : #if FD_HAS_HOSTED
2 : #define _GNU_SOURCE
3 : #endif
4 :
5 : #include "fd_shmem_private.h"
6 :
7 : #if FD_HAS_HOSTED
8 :
9 : #include <errno.h>
10 : #include <unistd.h>
11 : #include <fcntl.h>
12 : #include <sys/mman.h>
13 : #include <sys/random.h>
14 :
15 : /* fd_shmem_private_key converts the cstr pointed to by name into a
16 : valid key and stores it at the location pointed to by key assumed
17 : valid). Returns key on success and NULL on failure (i.e. name does
18 : not point to a valid shmem region name). All bytes of key will be
19 : unambiguously initialized so there is no issue with using things like
20 : memcmp to compare keys, etc. */
21 :
22 : static inline fd_shmem_private_key_t *
23 : fd_shmem_private_key( fd_shmem_private_key_t * key,
24 2673 : char const * name ) {
25 2673 : ulong len = fd_shmem_name_len( name );
26 2673 : if( FD_UNLIKELY( !len ) ) return NULL;
27 2547 : fd_memset( key->cstr, 0, FD_SHMEM_NAME_MAX );
28 2547 : fd_memcpy( key->cstr, name, len );
29 2547 : return key;
30 2673 : }
31 :
32 : static fd_shmem_private_key_t const fd_shmem_private_key_null; /* Will be zeros at thread group start */
33 :
34 684270 : #define FD_SHMEM_PRIVATE_MAP_LG_SLOT_CNT (8)
35 677778 : #define FD_SHMEM_PRIVATE_MAP_SLOT_CNT (1UL<<FD_SHMEM_PRIVATE_MAP_LG_SLOT_CNT)
36 : FD_STATIC_ASSERT( FD_SHMEM_JOIN_MAX < FD_SHMEM_PRIVATE_MAP_SLOT_CNT, increase_lg_slot_count );
37 :
38 : #define MAP_NAME fd_shmem_private_map
39 4620 : #define MAP_T fd_shmem_join_info_t
40 6492 : #define MAP_LG_SLOT_CNT FD_SHMEM_PRIVATE_MAP_LG_SLOT_CNT
41 6492 : #define MAP_KEY_T fd_shmem_private_key_t
42 1872 : #define MAP_KEY_NULL fd_shmem_private_key_null
43 683883 : #define MAP_KEY_INVAL(k) (!((k).cstr[0]))
44 1641 : #define MAP_KEY_EQUAL(k0,k1) (!memcmp( (k0).cstr, (k1).cstr, FD_SHMEM_NAME_MAX ))
45 : #define MAP_KEY_EQUAL_IS_SLOW (1)
46 4620 : #define MAP_KEY_HASH(k) ((uint)fd_hash( 0UL, (k).cstr, FD_SHMEM_NAME_MAX ))
47 : #include "../tmpl/fd_map.c"
48 :
49 : /* fd_shmem_private_map_query_by_{join,addr} are some extra
50 : fd_shmem_private_map APIs allow looking up the join info for a region
51 : by its join handle and/or by a pointer at a byte in the region int
52 : the thread group's local address space. These aren't algorithmically
53 : efficient but aren't expected to be and are plenty fast in normal use
54 : anyway. */
55 :
56 : static inline fd_shmem_join_info_t *
57 : fd_shmem_private_map_query_by_join( fd_shmem_join_info_t * map,
58 : void const * join,
59 2475 : fd_shmem_join_info_t * def ) {
60 273006 : for( ulong slot_idx=0UL; slot_idx<FD_SHMEM_PRIVATE_MAP_SLOT_CNT; slot_idx++ )
61 272829 : if( ((!fd_shmem_private_map_key_inval( map[slot_idx].key )) & (map[slot_idx].join==join)) ) return &map[slot_idx];
62 177 : return def;
63 2475 : }
64 :
65 : static inline fd_shmem_join_info_t *
66 : fd_shmem_private_map_query_by_addr( fd_shmem_join_info_t * map,
67 : ulong a0,
68 : ulong a1, /* Assumes a1>=a0 */
69 2760 : fd_shmem_join_info_t * def ) {
70 404772 : for( ulong slot_idx=0UL; slot_idx<FD_SHMEM_PRIVATE_MAP_SLOT_CNT; slot_idx++ ) {
71 404562 : ulong j0 = (ulong)map[slot_idx].shmem;
72 404562 : ulong j1 = j0 + map[slot_idx].page_sz*map[slot_idx].page_cnt - 1UL;
73 404562 : if( ((!fd_shmem_private_map_key_inval( map[slot_idx].key )) & (a1>=j0) & (a0<=j1)) ) return &map[slot_idx];
74 404562 : }
75 210 : return def;
76 2760 : }
77 :
78 : /*
79 : * fd_shmem_private_grab_region will attempt to map a region at the passed
80 : * address with the passed size. If the return value of `mmap` equals the
81 : * passed address this means the area of memory was unmapped previously and
82 : * we have successfully "grabbed" the region. We can then call `mmap` with
83 : * MAP_FIXED over the region and be certain no corruption occurs. If the
84 : * return value of `mmap` does not return the passed address this means that
85 : * the passed region is already atleast partially mapped and we cannot grab it.
86 : */
87 : static void *
88 : fd_shmem_private_grab_region( ulong addr,
89 : ulong size,
90 1896 : int prot ) {
91 1896 : void * mmap_ret = mmap( (void*)addr, size, prot, MAP_ANON|MAP_PRIVATE, -1, 0 );
92 1896 : if( FD_UNLIKELY( mmap_ret == MAP_FAILED ) ) return mmap_ret;
93 :
94 1896 : if( FD_UNLIKELY( (ulong)mmap_ret != addr ) ) {
95 0 : if( munmap( mmap_ret, size ) ) {
96 0 : FD_LOG_ERR(( "failed to unmap temporary mapping, munmap() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
97 0 : }
98 0 : return MAP_FAILED;
99 0 : }
100 :
101 1896 : return mmap_ret;
102 1896 : }
103 :
104 : void *
105 : fd_shmem_private_map_rand( ulong size,
106 : ulong align,
107 1896 : int prot ) {
108 1896 : ulong ret_addr = 0;
109 :
110 : /* Failure is unlikely, 1000 iterations should guarantee success */
111 1896 : for( ulong i = 0; i < 1000; i++ ) {
112 1896 : long n = getrandom( &ret_addr, sizeof(ret_addr), 0 );
113 1896 : if( FD_UNLIKELY( n!=sizeof(ret_addr) ) ) FD_LOG_ERR(( "could not generate random address, getrandom() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
114 :
115 : /* Assume 47-bit virtual addressing */
116 1896 : ret_addr &= 0x00007FFFFFFFFFFFUL;
117 1896 : ret_addr = fd_ulong_align_up( ret_addr, align );
118 :
119 1896 : if( fd_shmem_private_grab_region( ret_addr, size, prot )!=MAP_FAILED ) {
120 1896 : return (void *)ret_addr;
121 1896 : }
122 1896 : }
123 :
124 0 : FD_LOG_ERR(( "unable to find random address for memory map after 1000 attempts" ));
125 0 : }
126 :
127 : static fd_shmem_join_info_t fd_shmem_private_map[ FD_SHMEM_PRIVATE_MAP_SLOT_CNT ]; /* Empty on thread group start */
128 : static ulong fd_shmem_private_map_cnt; /* 0 on thread group start */
129 :
130 : void *
131 : fd_shmem_join( char const * name,
132 : int mode,
133 : fd_shmem_joinleave_func_t join_func,
134 : void * context,
135 2496 : fd_shmem_join_info_t * opt_info ) {
136 :
137 : /* Check input args */
138 :
139 2496 : fd_shmem_private_key_t key;
140 2496 : if( FD_UNLIKELY( !fd_shmem_private_key( &key, name ) ) ) {
141 126 : FD_LOG_WARNING(( "bad name (%s)", name ? name : "NULL" ));
142 126 : return NULL;
143 126 : }
144 :
145 2370 : if( FD_UNLIKELY( !( (mode==FD_SHMEM_JOIN_MODE_READ_ONLY) | (mode==FD_SHMEM_JOIN_MODE_READ_WRITE) ) ) ) {
146 0 : FD_LOG_WARNING(( "unsupported join mode (%i) for %s", mode, name ));
147 0 : return NULL;
148 0 : }
149 :
150 2370 : FD_SHMEM_LOCK;
151 :
152 : /* Query for an existing mapping */
153 :
154 2370 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query( fd_shmem_private_map, key, NULL );
155 2370 : if( join_info ) {
156 426 : if( FD_UNLIKELY( join_info->ref_cnt<0L ) ) {
157 0 : FD_LOG_WARNING(( "join/leave circular dependency detected for %s", name ));
158 0 : FD_SHMEM_UNLOCK;
159 0 : return NULL;
160 0 : }
161 426 : join_info->ref_cnt++;
162 :
163 426 : if( opt_info ) *opt_info = *join_info;
164 426 : FD_SHMEM_UNLOCK;
165 426 : return join_info->join;
166 426 : }
167 :
168 : /* Not currently mapped. See if we have enough room. */
169 :
170 1944 : if( FD_UNLIKELY( fd_shmem_private_map_cnt>=FD_SHMEM_JOIN_MAX ) ) {
171 0 : FD_SHMEM_UNLOCK;
172 0 : FD_LOG_WARNING(( "too many concurrent joins for %s", name ));
173 0 : return NULL;
174 0 : }
175 :
176 : /* We have enough room for it. Try to map the memory. */
177 :
178 1944 : fd_shmem_info_t shmem_info[1];
179 1944 : if( FD_UNLIKELY( fd_shmem_info( name, 0UL, shmem_info ) ) ) {
180 48 : FD_SHMEM_UNLOCK;
181 48 : FD_LOG_WARNING(( "unable to query region \"%s\"\n\tprobably does not exist or bad permissions", name ));
182 48 : return NULL;
183 48 : }
184 1896 : ulong page_sz = shmem_info->page_sz;
185 1896 : ulong page_cnt = shmem_info->page_cnt;
186 1896 : ulong sz = page_sz*page_cnt;
187 1896 : int rw = (mode==FD_SHMEM_JOIN_MODE_READ_WRITE);
188 :
189 : /* Map the region into our address space. */
190 :
191 1896 : char path[ FD_SHMEM_PRIVATE_PATH_BUF_MAX ];
192 1896 : int fd = open( fd_shmem_private_path( name, page_sz, path ), rw ? O_RDWR : O_RDONLY, (mode_t)0 );
193 1896 : if( FD_UNLIKELY( fd==-1 ) ) {
194 0 : FD_SHMEM_UNLOCK;
195 0 : FD_LOG_WARNING(( "open(\"%s\",%s,0) failed (%i-%s)", path, rw ? "O_RDWR" : "O_RDONLY", errno, fd_io_strerror( errno ) ));
196 0 : return NULL;
197 0 : }
198 :
199 : /* Generate a random address that we are guaranteed to be able to map */
200 1896 : void * const map_addr = fd_shmem_private_map_rand( sz, page_sz, PROT_READ );
201 1896 : if( FD_UNLIKELY( map_addr==MAP_FAILED ) ) FD_LOG_ERR(( "fd_shmem_private_map_rand failed" ));
202 :
203 : /* Note that MAP_HUGETLB and MAP_HUGE_* are implied by the mount point */
204 1896 : void * shmem = mmap( map_addr, sz, PROT_READ|( rw?PROT_WRITE:0 ) , MAP_SHARED|MAP_FIXED, fd, (off_t)0 );
205 :
206 1896 : int mmap_errno = errno;
207 1896 : if( FD_UNLIKELY( close( fd ) ) )
208 0 : FD_LOG_WARNING(( "close(\"%s\") failed (%i-%s); attempting to continue", path, errno, fd_io_strerror( errno ) ));
209 :
210 : /* Validate the mapping */
211 :
212 1896 : if( FD_UNLIKELY( shmem==MAP_FAILED ) ) {
213 0 : FD_SHMEM_UNLOCK;
214 0 : FD_LOG_WARNING(( "mmap(%p,%lu KiB,%s,MAP_SHARED,\"%s\",0) failed (%i-%s)",
215 0 : map_addr, sz>>10, rw ? "PROT_READ|PROT_WRITE" : "PROT_READ", path, mmap_errno, fd_io_strerror( mmap_errno ) ));
216 0 : return NULL;
217 0 : }
218 :
219 1896 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, page_sz ) ) ) {
220 0 : if( FD_UNLIKELY( munmap( shmem, sz ) ) )
221 0 : FD_LOG_WARNING(( "munmap(\"%s\",%lu KiB) failed (%i-%s); attempting to continue",
222 0 : path, sz>>10, errno, fd_io_strerror( errno ) ));
223 0 : FD_SHMEM_UNLOCK;
224 0 : FD_LOG_WARNING(( "misaligned memory mapping for \"%s\"\n\t"
225 0 : "This thread group's hugetlbfs mount path (--shmem-path / FD_SHMEM_PATH):\n\t"
226 0 : "\t%s\n\t"
227 0 : "has probably been corrupted and needs to be redone.\n\t"
228 0 : "See 'bin/fd_shmem_cfg help' for more information.",
229 0 : path, fd_shmem_private_base ));
230 0 : return NULL;
231 0 : }
232 :
233 : /* Lock this region in DRAM to prevent it going to swap and (try) to
234 : keep the virtual to physical DRAM mapping fixed for the join
235 : duration. Also advise the kernel to not dump this region to avoid
236 : large shared mappings in concurrent use by multiple processes
237 : destroying the system with core files if a bunch of thread using
238 : this mapping seg fault concurrently. */
239 :
240 1896 : if( FD_UNLIKELY( fd_numa_mlock( shmem, sz ) ) )
241 0 : FD_LOG_WARNING(( "fd_numa_mlock(\"%s\",%lu KiB) failed (%i-%s); attempting to continue",
242 1896 : path, sz>>10, errno, fd_io_strerror( errno ) ));
243 :
244 1896 : if( FD_UNLIKELY( madvise( shmem, sz, MADV_DONTDUMP ) ) )
245 0 : FD_LOG_WARNING(( "madvise(\"%s\",%lu KiB) failed (%i-%s); attempting to continue",
246 1896 : path, sz>>10, errno, fd_io_strerror( errno ) ));
247 :
248 : /* We have mapped the region. Try to complete the join. Note:
249 : map_query above and map_insert could be combined to improve
250 : efficiency further here (and eliminate the paranoid if check in the
251 : process). */
252 :
253 1896 : join_info = fd_shmem_private_map_insert( fd_shmem_private_map, key );
254 1896 : if( FD_UNLIKELY( !join_info ) ) /* should be impossible */
255 0 : FD_LOG_ERR(( "unable to insert region \"%s\" (internal error)", name ));
256 1896 : fd_shmem_private_map_cnt++;
257 :
258 1896 : join_info->ref_cnt = -1L; /* Mark join/leave in progress so we can detect circular join/leave dependencies */
259 1896 : join_info->join = NULL; /* Overridden below */
260 1896 : join_info->shmem = shmem;
261 1896 : join_info->page_sz = page_sz;
262 1896 : join_info->page_cnt = page_cnt;
263 1896 : join_info->mode = mode;
264 : /* join_info->hash handled by insert */
265 : /* join_info->name " */
266 : /* join_info->key " */
267 :
268 1896 : void * join = join_func ? join_func( context, join_info ): shmem; /* Reset by the join func if provided */
269 1896 : if( FD_UNLIKELY( !join ) ) {
270 0 : fd_shmem_private_map_remove( fd_shmem_private_map, join_info );
271 0 : fd_shmem_private_map_cnt--;
272 0 : if( FD_UNLIKELY( munmap( shmem, sz ) ) )
273 0 : FD_LOG_WARNING(( "munmap(\"%s\",%lu KiB) failed (%i-%s); attempting to continue",
274 0 : name, sz>>10, errno, fd_io_strerror( errno ) ));
275 0 : FD_SHMEM_UNLOCK;
276 0 : FD_LOG_WARNING(( "unable to join region \"%s\"", name ));
277 0 : return NULL;
278 0 : }
279 1896 : join_info->ref_cnt = 1UL;
280 1896 : join_info->join = join;
281 :
282 1896 : if( opt_info ) *opt_info = *join_info;
283 1896 : FD_SHMEM_UNLOCK;
284 1896 : return join;
285 1896 : }
286 :
287 : int
288 : fd_shmem_leave( void * join,
289 : fd_shmem_joinleave_func_t leave_func,
290 2235 : void * context ) {
291 2235 : if( FD_UNLIKELY( !join ) ) { FD_LOG_WARNING(( "NULL join" )); return 1; }
292 :
293 2235 : FD_SHMEM_LOCK;
294 :
295 2235 : if( FD_UNLIKELY( !fd_shmem_private_map_cnt ) ) {
296 3 : FD_SHMEM_UNLOCK;
297 3 : FD_LOG_WARNING(( "join is not a current join" ));
298 3 : return 1;
299 3 : }
300 2232 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query_by_join( fd_shmem_private_map, join, NULL );
301 2232 : if( FD_UNLIKELY( !join_info ) ) {
302 0 : FD_SHMEM_UNLOCK;
303 0 : FD_LOG_WARNING(( "join is not a current join" ));
304 0 : return 1;
305 0 : }
306 :
307 2232 : long ref_cnt = join_info->ref_cnt;
308 2232 : if( join_info->ref_cnt>1L ) {
309 426 : join_info->ref_cnt = ref_cnt-1L;
310 426 : FD_SHMEM_UNLOCK;
311 426 : return 0;
312 426 : }
313 :
314 1806 : if( join_info->ref_cnt==-1L ) {
315 0 : FD_SHMEM_UNLOCK;
316 0 : FD_LOG_WARNING(( "join/leave circular dependency detected for %s", join_info->name ));
317 0 : return 1;
318 0 : }
319 :
320 1806 : if( FD_UNLIKELY( join_info->ref_cnt!=1L ) ) /* Should be impossible */
321 0 : FD_LOG_WARNING(( "unexpected ref count for %s; attempting to continue", join_info->name ));
322 :
323 1806 : char const * name = join_info->name; /* Just in case leave_func clobbers */
324 1806 : void * shmem = join_info->shmem; /* " */
325 1806 : ulong page_sz = join_info->page_sz; /* " */
326 1806 : ulong page_cnt = join_info->page_cnt; /* " */
327 :
328 1806 : if( leave_func ) {
329 1746 : join_info->ref_cnt = -1L; /* Mark join/leave is in progress so we can detect join/leave circular dependencies */
330 1746 : leave_func( context, join_info );
331 1746 : }
332 :
333 1806 : int error = 0;
334 1806 : ulong sz = page_sz*page_cnt;
335 1806 : if( FD_UNLIKELY( munmap( shmem, sz ) ) ) {
336 0 : FD_LOG_WARNING(( "munmap(\"%s\",%lu KiB) failed (%i-%s); attempting to continue",
337 0 : name, sz>>10, errno, fd_io_strerror( errno ) ));
338 0 : error = 1;
339 0 : }
340 :
341 1806 : fd_shmem_private_map_remove( fd_shmem_private_map, join_info );
342 1806 : fd_shmem_private_map_cnt--;
343 1806 : FD_SHMEM_UNLOCK;
344 1806 : return error;
345 1806 : }
346 :
347 : int
348 : fd_shmem_join_query_by_name( char const * name,
349 0 : fd_shmem_join_info_t * opt_info ) {
350 0 : fd_shmem_private_key_t key;
351 0 : if( FD_UNLIKELY( !fd_shmem_private_key( &key, name ) ) ) return EINVAL;
352 :
353 0 : FD_SHMEM_LOCK;
354 :
355 0 : if( !fd_shmem_private_map_cnt ) { FD_SHMEM_UNLOCK; return ENOENT; }
356 0 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query( fd_shmem_private_map, key, NULL );
357 0 : if( !join_info ) { FD_SHMEM_UNLOCK; return ENOENT; }
358 0 : if( opt_info ) *opt_info = *join_info;
359 :
360 0 : FD_SHMEM_UNLOCK;
361 0 : return 0;
362 0 : }
363 :
364 : int
365 : fd_shmem_join_query_by_join( void const * join,
366 0 : fd_shmem_join_info_t * opt_info ) {
367 0 : if( FD_UNLIKELY( !join ) ) return EINVAL;
368 :
369 0 : FD_SHMEM_LOCK;
370 :
371 0 : if( !fd_shmem_private_map_cnt ) { FD_SHMEM_UNLOCK; return ENOENT; }
372 0 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query_by_join( fd_shmem_private_map, join, NULL );
373 0 : if( FD_UNLIKELY( !join_info ) ) { FD_SHMEM_UNLOCK; return ENOENT; }
374 0 : if( opt_info ) *opt_info = *join_info;
375 :
376 0 : FD_SHMEM_UNLOCK;
377 0 : return 0;
378 0 : }
379 :
380 : int
381 : fd_shmem_join_query_by_addr( void const * addr,
382 : ulong sz,
383 2595 : fd_shmem_join_info_t * opt_info ) {
384 2595 : if( FD_UNLIKELY( !sz ) ) return ENOENT; /* empty range */
385 2595 : ulong a0 = (ulong)addr;
386 2595 : ulong a1 = a0+sz-1UL;
387 2595 : if( FD_UNLIKELY( a1<a0 ) ) return EINVAL; /* cyclic wrap range */
388 :
389 2595 : FD_SHMEM_LOCK;
390 :
391 2595 : if( !fd_shmem_private_map_cnt ) { FD_SHMEM_UNLOCK; return ENOENT; }
392 2583 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query_by_addr( fd_shmem_private_map, a0, a1, NULL );
393 2583 : if( FD_UNLIKELY( !join_info ) ) { FD_SHMEM_UNLOCK; return ENOENT; }
394 2550 : if( opt_info ) *opt_info = *join_info;
395 :
396 2550 : FD_SHMEM_UNLOCK;
397 2550 : return 0;
398 2583 : }
399 :
400 : int
401 : fd_shmem_join_anonymous( char const * name,
402 : int mode,
403 : void * join,
404 : void * mem,
405 : ulong page_sz,
406 177 : ulong page_cnt ) {
407 :
408 : /* Check input args */
409 :
410 177 : fd_shmem_private_key_t key;
411 177 : if( FD_UNLIKELY( !fd_shmem_private_key( &key, name ) ) ) {
412 0 : FD_LOG_WARNING(( "bad name (%s)", name ? name : "NULL" ));
413 0 : return EINVAL;
414 0 : }
415 :
416 177 : if( FD_UNLIKELY( !( (mode==FD_SHMEM_JOIN_MODE_READ_ONLY) | (mode==FD_SHMEM_JOIN_MODE_READ_WRITE) ) ) ) {
417 0 : FD_LOG_WARNING(( "unsupported join mode (%i) for %s", mode, name ));
418 0 : return EINVAL;
419 0 : }
420 :
421 177 : if( FD_UNLIKELY( !join ) ) {
422 0 : FD_LOG_WARNING(( "NULL join" ));
423 0 : return EINVAL;
424 0 : }
425 :
426 177 : if( FD_UNLIKELY( !mem ) ) {
427 0 : FD_LOG_WARNING(( "NULL mem" ));
428 0 : return EINVAL;
429 0 : }
430 :
431 177 : if( FD_UNLIKELY( !fd_shmem_is_page_sz( page_sz ) ) ) {
432 0 : FD_LOG_WARNING(( "unsupported page_sz (%lu)", page_sz ));
433 0 : return EINVAL;
434 0 : }
435 :
436 177 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, page_sz ) ) ) {
437 0 : FD_LOG_WARNING(( "misaligned mem" ));
438 0 : return EINVAL;
439 0 : }
440 :
441 177 : if( FD_UNLIKELY( !page_cnt ) ) {
442 0 : FD_LOG_WARNING(( "unsupported page_sz (%lu)", page_sz ));
443 0 : return EINVAL;
444 0 : }
445 :
446 177 : if( FD_UNLIKELY( page_cnt > (ULONG_MAX/page_sz) ) ) {
447 0 : FD_LOG_WARNING(( "too large page cnt (%lu)", page_cnt ));
448 0 : return EINVAL;
449 0 : }
450 :
451 177 : ulong sz = page_cnt*page_sz;
452 177 : ulong a0 = (ulong)mem;
453 177 : ulong a1 = a0 + sz-1UL;
454 177 : if( FD_UNLIKELY( a1<a0 ) ) {
455 0 : FD_LOG_WARNING(( "bad mem range" ));
456 0 : return EINVAL;
457 0 : }
458 :
459 177 : FD_SHMEM_LOCK;
460 :
461 : /* Query for an existing mapping */
462 :
463 177 : fd_shmem_join_info_t * join_info;
464 :
465 177 : join_info = fd_shmem_private_map_query( fd_shmem_private_map, key, NULL );
466 177 : if( FD_UNLIKELY( join_info ) ) {
467 0 : FD_SHMEM_UNLOCK;
468 0 : FD_LOG_WARNING(( "%s already joined", name ));
469 0 : return EINVAL;
470 0 : }
471 :
472 177 : join_info = fd_shmem_private_map_query_by_join( fd_shmem_private_map, join, NULL );
473 177 : if( FD_UNLIKELY( join_info ) ) {
474 0 : FD_SHMEM_UNLOCK;
475 0 : FD_LOG_WARNING(( "%s join handle already in use", name ));
476 0 : return EINVAL;
477 0 : }
478 :
479 177 : join_info = fd_shmem_private_map_query_by_addr( fd_shmem_private_map, a0, a1, NULL );
480 177 : if( FD_UNLIKELY( join_info ) ) {
481 0 : FD_SHMEM_UNLOCK;
482 0 : FD_LOG_WARNING(( "%s join memory already mapped", name ));
483 0 : return EINVAL;
484 0 : }
485 :
486 : /* Not currently mapped. See if we have enough room. */
487 :
488 177 : if( FD_UNLIKELY( fd_shmem_private_map_cnt>=FD_SHMEM_JOIN_MAX ) ) {
489 0 : FD_SHMEM_UNLOCK;
490 0 : FD_LOG_WARNING(( "too many concurrent joins for %s", name ));
491 0 : return EINVAL;
492 0 : }
493 :
494 : /* We have enough room for it. Try to "map" the memory. */
495 :
496 177 : fd_shmem_info_t shmem_info[1];
497 177 : if( FD_UNLIKELY( !fd_shmem_info( name, 0UL, shmem_info ) ) )
498 0 : FD_LOG_WARNING(( "anonymous join to %s will shadow an existing shared memory region in this thread group; "
499 177 : "attempting to continue", name ));
500 :
501 177 : join_info = fd_shmem_private_map_insert( fd_shmem_private_map, key );
502 177 : if( FD_UNLIKELY( !join_info ) ) /* should be impossible */
503 0 : FD_LOG_ERR(( "unable to insert region \"%s\" (internal error)", name ));
504 177 : fd_shmem_private_map_cnt++;
505 :
506 177 : join_info->ref_cnt = 1L;
507 177 : join_info->join = join;
508 177 : join_info->shmem = mem;
509 177 : join_info->page_sz = page_sz;
510 177 : join_info->page_cnt = page_cnt;
511 177 : join_info->mode = mode;
512 : /* join_info->hash handled by insert */
513 : /* join_info->name " */
514 : /* join_info->key " */
515 :
516 177 : FD_SHMEM_UNLOCK;
517 177 : return 0;
518 177 : }
519 :
520 : int
521 : fd_shmem_leave_anonymous( void * join,
522 72 : fd_shmem_join_info_t * opt_info ) {
523 :
524 72 : if( FD_UNLIKELY( !join ) ) {
525 3 : FD_LOG_WARNING(( "NULL join" ));
526 3 : return EINVAL;
527 3 : }
528 :
529 69 : FD_SHMEM_LOCK;
530 :
531 69 : if( FD_UNLIKELY( !fd_shmem_private_map_cnt ) ) {
532 3 : FD_SHMEM_UNLOCK;
533 3 : FD_LOG_WARNING(( "join is not a current join" ));
534 3 : return EINVAL;
535 3 : }
536 :
537 66 : fd_shmem_join_info_t * join_info = fd_shmem_private_map_query_by_join( fd_shmem_private_map, join, NULL );
538 66 : if( FD_UNLIKELY( !join_info ) ) {
539 0 : FD_SHMEM_UNLOCK;
540 0 : FD_LOG_WARNING(( "join is not a current join" ));
541 0 : return EINVAL;
542 0 : }
543 :
544 66 : if( FD_UNLIKELY( join_info->ref_cnt!=1L ) ) {
545 0 : FD_SHMEM_UNLOCK;
546 0 : FD_LOG_WARNING(( "join ref_cnt is not 1" ));
547 0 : return EINVAL;
548 0 : }
549 :
550 66 : if( opt_info ) {
551 63 : *opt_info = *join_info;
552 63 : opt_info->ref_cnt = 0L;
553 63 : }
554 :
555 66 : fd_shmem_private_map_remove( fd_shmem_private_map, join_info );
556 66 : fd_shmem_private_map_cnt--;
557 66 : FD_SHMEM_UNLOCK;
558 66 : return 0;
559 66 : }
560 :
561 : fd_shmem_join_info_t const *
562 0 : fd_shmem_iter_begin( void ) {
563 0 : fd_shmem_join_info_t const * map = fd_shmem_private_map;
564 0 : for( ulong slot_idx=0UL; slot_idx<FD_SHMEM_PRIVATE_MAP_SLOT_CNT; slot_idx++ ) {
565 0 : if( !fd_shmem_private_map_key_inval( map[slot_idx].key ) ) return &map[slot_idx];
566 0 : }
567 0 : return NULL;
568 0 : }
569 :
570 : fd_shmem_join_info_t const *
571 0 : fd_shmem_iter_next( fd_shmem_join_info_t const * iter ) {
572 0 : fd_shmem_join_info_t const * map = fd_shmem_private_map;
573 0 : for( ulong slot_idx=(ulong)(iter-map) + 1UL; slot_idx<FD_SHMEM_PRIVATE_MAP_SLOT_CNT; slot_idx++ ) {
574 0 : if( !fd_shmem_private_map_key_inval( map[slot_idx].key ) ) return &map[slot_idx];
575 0 : }
576 0 : return NULL;
577 0 : }
578 :
579 : #endif
|