LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_bpf_frames.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 141 146 96.6 %
Date: 2026-11-30 13:42:38 Functions: 17 17 100.0 %

          Line data    Source code
       1             : #include "fd_bpf_frames.h"
       2             : #include "../fd_rwlock.h"
       3             : 
       4             : union __attribute__((aligned(FD_BPF_FRAMES_ALIGN))) fd_bpf_frame {
       5             :   ulong next; /* pool link while free */
       6             :   uchar data[ BPF_LOADER_SERIALIZATION_FOOTPRINT ];
       7             : };
       8             : typedef union fd_bpf_frame fd_bpf_frame_t;
       9             : 
      10             : #define POOL_NAME frame_pool
      11        5679 : #define POOL_T    fd_bpf_frame_t
      12             : #include "../../util/tmpl/fd_pool.c"
      13             : 
      14        2757 : #define FD_BPF_FRAMES_MAGIC (0xf17eda2c3bf2a3e0UL) /* FIREDANCE BPF FRAMES V0 */
      15             : 
      16             : struct __attribute__((aligned(FD_BPF_FRAMES_ALIGN))) fd_bpf_frames_shmem {
      17             :   ulong       magic;
      18             :   fd_rwlock_t lock;
      19             :   ulong       has_owner;      /* 1 while some join is the owner, the one
      20             :                                  tile allowed into the owner's reserve */
      21             :   ulong       joiner_cnt;
      22             :   ulong       joiner_cnt_max;
      23             :   ulong       local_cnt;
      24             : 
      25             :   /* local_cnt local frames for each of joiner_cnt_max joins follow,
      26             :      then frame_pool, then one deploy scratch for each join */
      27             : };
      28             : 
      29             : struct fd_bpf_frames {
      30             :   fd_bpf_frames_shmem_t *   shmem;
      31             :   fd_bpf_frame_t *          pool;
      32             :   fd_bpf_frame_t *          local;
      33             :   fd_bpf_deploy_scratch_t * deploy;
      34             :   ulong                     local_cnt;
      35             :   ulong                     local_used;  /* local[ 0, local_used ) are in use */
      36             :   ulong                     reserve_cnt; /* frames held back for each of the owner and
      37             :                                             the priority caller, the most shared
      38             :                                             frames one stack can need */
      39             :   int                       is_owner;
      40             :   ulong                     held_cnt;    /* shared frames */
      41             : };
      42             : 
      43             : FD_FN_CONST ulong
      44       11646 : fd_bpf_frames_shmem_align( void ) {
      45       11646 :   return fd_ulong_max( alignof(fd_bpf_frames_shmem_t), frame_pool_align() );
      46       11646 : }
      47             : 
      48             : FD_FN_CONST ulong
      49             : fd_bpf_frames_shmem_footprint( ulong frame_cnt,
      50             :                                ulong joiner_cnt_max,
      51        2925 :                                ulong local_cnt ) {
      52        2925 :   if( FD_UNLIKELY( local_cnt>FD_MAX_INSTRUCTION_STACK_DEPTH                 ) ) return 0UL;
      53        2922 :   if( FD_UNLIKELY( frame_cnt<FD_BPF_FRAMES_HELD_CNT( local_cnt )            ) ) return 0UL;
      54        2916 :   if( FD_UNLIKELY( frame_cnt>FD_BPF_FRAMES_CNT_MAX                          ) ) return 0UL;
      55        2913 :   if( FD_UNLIKELY( joiner_cnt_max>FD_BPF_FRAMES_CNT_MAX                     ) ) return 0UL;
      56        2910 :   if( FD_UNLIKELY( frame_cnt+joiner_cnt_max*local_cnt>FD_BPF_FRAMES_CNT_MAX ) ) return 0UL;
      57        2907 :   ulong l = FD_LAYOUT_INIT;
      58        2907 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bpf_frames_shmem_t),   sizeof(fd_bpf_frames_shmem_t)                   );
      59        2907 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bpf_frame_t),          joiner_cnt_max*local_cnt*sizeof(fd_bpf_frame_t) );
      60        2907 :   l = FD_LAYOUT_APPEND( l, frame_pool_align(),               frame_pool_footprint( frame_cnt )               );
      61        2907 :   l = FD_LAYOUT_APPEND( l, alignof(fd_bpf_deploy_scratch_t), joiner_cnt_max*sizeof(fd_bpf_deploy_scratch_t)  );
      62        2907 :   return FD_LAYOUT_FINI( l, fd_bpf_frames_shmem_align() );
      63        2910 : }
      64             : 
      65             : static fd_bpf_frame_t *
      66       11523 : local_frames( fd_bpf_frames_shmem_t * shmem ) {
      67       11523 :   return (fd_bpf_frame_t *)( shmem+1 );
      68       11523 : }
      69             : 
      70             : static void *
      71        8601 : pool_mem( fd_bpf_frames_shmem_t * shmem ) {
      72        8601 :   return (void *)fd_ulong_align_up( (ulong)( local_frames( shmem )+shmem->joiner_cnt_max*shmem->local_cnt ), frame_pool_align() );
      73        8601 : }
      74             : 
      75             : static fd_bpf_deploy_scratch_t *
      76             : deploy_scratches( fd_bpf_frames_shmem_t * shmem,
      77        2922 :                   fd_bpf_frame_t *        pool ) {
      78        2922 :   ulong pool_end = (ulong)pool_mem( shmem )+frame_pool_footprint( frame_pool_max( pool ) );
      79        2922 :   return (fd_bpf_deploy_scratch_t *)fd_ulong_align_up( pool_end, alignof(fd_bpf_deploy_scratch_t) );
      80        2922 : }
      81             : 
      82             : void *
      83             : fd_bpf_frames_shmem_new( void * shmem,
      84             :                          ulong  frame_cnt,
      85             :                          ulong  joiner_cnt_max,
      86        2766 :                          ulong  local_cnt ) {
      87        2766 :   if( FD_UNLIKELY( !shmem ) ) {
      88           3 :     FD_LOG_WARNING(( "NULL shmem" ));
      89           3 :     return NULL;
      90           3 :   }
      91        2763 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_bpf_frames_shmem_align() ) ) ) {
      92           3 :     FD_LOG_WARNING(( "misaligned shmem" ));
      93           3 :     return NULL;
      94           3 :   }
      95        2760 :   if( FD_UNLIKELY( !fd_bpf_frames_shmem_footprint( frame_cnt, joiner_cnt_max, local_cnt ) ) ) {
      96           3 :     FD_LOG_WARNING(( "invalid frame_cnt %lu, joiner_cnt_max %lu or local_cnt %lu", frame_cnt, joiner_cnt_max, local_cnt ));
      97           3 :     return NULL;
      98           3 :   }
      99             : 
     100        2757 :   fd_bpf_frames_shmem_t * frames = (fd_bpf_frames_shmem_t *)shmem;
     101        2757 :   fd_rwlock_new( &frames->lock );
     102        2757 :   frames->has_owner      = 0UL;
     103        2757 :   frames->joiner_cnt     = 0UL;
     104        2757 :   frames->joiner_cnt_max = joiner_cnt_max;
     105        2757 :   frames->local_cnt      = local_cnt;
     106        2757 :   FD_TEST( frame_pool_new( pool_mem( frames ), frame_cnt ) );
     107             : 
     108        2757 :   FD_COMPILER_MFENCE();
     109        2757 :   FD_VOLATILE( frames->magic ) = FD_BPF_FRAMES_MAGIC;
     110        2757 :   FD_COMPILER_MFENCE();
     111        2757 :   return shmem;
     112        2757 : }
     113             : 
     114             : FD_FN_CONST ulong
     115        3672 : fd_bpf_frames_align( void ) {
     116        3672 :   return alignof(fd_bpf_frames_t);
     117        3672 : }
     118             : 
     119             : FD_FN_CONST ulong
     120         498 : fd_bpf_frames_footprint( void ) {
     121         498 :   return sizeof(fd_bpf_frames_t);
     122         498 : }
     123             : 
     124             : fd_bpf_frames_t *
     125             : fd_bpf_frames_join( void * ljoin,
     126        2931 :                     void * shframes ) {
     127        2931 :   fd_bpf_frames_t *       join  = (fd_bpf_frames_t *)ljoin;
     128        2931 :   fd_bpf_frames_shmem_t * shmem = (fd_bpf_frames_shmem_t *)shframes;
     129        2931 :   if( FD_UNLIKELY( !join || !fd_ulong_is_aligned( (ulong)join, fd_bpf_frames_align() ) ) ) {
     130           3 :     FD_LOG_WARNING(( "NULL or misaligned ljoin" ));
     131           3 :     return NULL;
     132           3 :   }
     133        2928 :   if( FD_UNLIKELY( !shmem || !fd_ulong_is_aligned( (ulong)shmem, fd_bpf_frames_shmem_align() ) ) ) {
     134           3 :     FD_LOG_WARNING(( "NULL or misaligned shframes" ));
     135           3 :     return NULL;
     136           3 :   }
     137        2925 :   if( FD_UNLIKELY( shmem->magic!=FD_BPF_FRAMES_MAGIC ) ) {
     138           0 :     FD_LOG_WARNING(( "bad magic" ));
     139           0 :     return NULL;
     140           0 :   }
     141        2925 :   ulong idx = FD_ATOMIC_FETCH_AND_ADD( &shmem->joiner_cnt, 1UL );
     142        2925 :   if( FD_UNLIKELY( idx>=shmem->joiner_cnt_max ) ) {
     143           3 :     FD_LOG_WARNING(( "too many joins (max %lu)", shmem->joiner_cnt_max ));
     144           3 :     return NULL;
     145           3 :   }
     146             : 
     147        2922 :   join->shmem       = shmem;
     148        2922 :   join->pool        = frame_pool_join( pool_mem( shmem ) );
     149        2922 :   join->local       = local_frames( shmem )+idx*shmem->local_cnt;
     150        2922 :   join->local_cnt   = shmem->local_cnt;
     151        2922 :   join->reserve_cnt = FD_MAX_INSTRUCTION_STACK_DEPTH-shmem->local_cnt;
     152        2922 :   join->local_used  = 0UL;
     153        2922 :   join->is_owner    = 0;
     154        2922 :   join->held_cnt    = 0UL;
     155        2922 :   FD_TEST( join->pool );
     156        2922 :   join->deploy      = deploy_scratches( shmem, join->pool )+idx;
     157        2922 :   return join;
     158        2922 : }
     159             : 
     160             : static uchar *
     161             : fd_bpf_frames_try_acquire( fd_bpf_frames_t * join,
     162     2161275 :                            int               priority ) {
     163     2161275 :   if( FD_LIKELY( join->local_used<join->local_cnt ) ) {
     164     1296900 :     return (uchar *)( join->local+join->local_used++ );
     165     1296900 :   }
     166             : 
     167             :   /* Take the lock, and give up right away if no frames are free. */
     168      864375 :   fd_bpf_frames_shmem_t * shmem = join->shmem;
     169      864375 :   fd_rwlock_write( &shmem->lock );
     170      864375 :   ulong free_cnt = frame_pool_free( join->pool );
     171      864375 :   if( FD_UNLIKELY( !free_cnt ) ) {
     172           6 :     fd_rwlock_unwrite( &shmem->lock );
     173           6 :     return NULL;
     174           6 :   }
     175             : 
     176             :   /* Become owner if you can. */
     177      864369 :   if( !shmem->has_owner ) {
     178      432078 :     shmem->has_owner = 1UL;
     179      432078 :     join->is_owner   = 1;
     180      432078 :   }
     181             : 
     182             :   /* keep is how many free frames this call must leave for others.  The
     183             :      owner never needs more than reserve_cnt frames, so it cannot run
     184             :      into the priority caller's reserve. */
     185      864369 :   ulong keep;
     186      864369 :   if( priority || join->is_owner ) {
     187      864363 :     keep = 0UL;
     188      864363 :   } else {
     189           6 :     keep = 2UL*join->reserve_cnt; /* leave both reserves */
     190           6 :   }
     191             : 
     192      864369 :   uchar * frame = NULL;
     193      864369 :   if( FD_LIKELY( free_cnt>keep ) ) {
     194      864363 :     frame = (uchar *)frame_pool_ele_acquire( join->pool );
     195      864363 :     join->held_cnt++;
     196      864363 :   }
     197             : 
     198      864369 :   fd_rwlock_unwrite( &shmem->lock );
     199      864369 :   return frame;
     200      864375 : }
     201             : 
     202             : uchar *
     203             : fd_bpf_frames_acquire( fd_bpf_frames_t * join,
     204     2161137 :                        int               priority ) {
     205     2161137 :   for(;;) {
     206     2161137 :     uchar * frame = fd_bpf_frames_try_acquire( join, priority );
     207     2161137 :     if( FD_LIKELY( frame ) ) return frame;
     208           0 :     FD_SPIN_PAUSE();
     209           0 :   }
     210     2161137 : }
     211             : 
     212             : void
     213             : fd_bpf_frames_release( fd_bpf_frames_t * join,
     214     2161263 :                        uchar *           frame ) {
     215     2161263 :   if( FD_LIKELY( !join->held_cnt ) ) {
     216     1296900 :     FD_TEST( join->local_used && frame==(uchar *)( join->local+join->local_used-1UL ) );
     217     1296900 :     join->local_used--;
     218     1296900 :     return;
     219     1296900 :   }
     220             : 
     221      864363 :   fd_bpf_frames_shmem_t * shmem = join->shmem;
     222      864363 :   fd_rwlock_write( &shmem->lock );
     223      864363 :   frame_pool_ele_release( join->pool, (fd_bpf_frame_t *)frame );
     224      864363 :   join->held_cnt--;
     225      864363 :   if( !join->held_cnt && join->is_owner ) {
     226      432078 :     shmem->has_owner = 0UL;
     227      432078 :     join->is_owner   = 0;
     228      432078 :   }
     229      864363 :   fd_rwlock_unwrite( &shmem->lock );
     230      864363 : }
     231             : 
     232             : fd_bpf_deploy_scratch_t *
     233          81 : fd_bpf_frames_deploy_scratch( fd_bpf_frames_t * join ) {
     234          81 :   return join->deploy;
     235          81 : }

Generated by: LCOV version 1.14