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