LCOV - code coverage report
Current view: top level - util/shmem - fd_shmem_user.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 215 351 61.3 %
Date: 2025-07-01 05:00:49 Functions: 10 12 83.3 %

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

Generated by: LCOV version 1.14