svm: return chunks to slice on fifo detach 54/31354/5
authorFlorin Coras <fcoras@cisco.com>
Fri, 19 Feb 2021 05:35:23 +0000 (21:35 -0800)
committerDave Barach <openvpp@barachs.net>
Sat, 20 Feb 2021 19:10:59 +0000 (19:10 +0000)
Ensure chunk alloc distribution is maintained on fifo detach.

Type: fix

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: I5aa5524e06a703dc50e90da6d177663d2d997aa4

src/svm/fifo_segment.c
src/svm/fifo_types.h

index f23b2db..e3e6caa 100644 (file)
@@ -942,9 +942,8 @@ fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f)
 {
   fifo_slice_private_t *pfss;
   fifo_segment_slice_t *fss;
-  u32 fl_index, slice_index;
-  svm_fifo_chunk_t **c;
   svm_fifo_t *of = *f;
+  u32 slice_index;
 
   slice_index = of->master_thread_index;
   fss = fsh_slice_get (fs->h, slice_index);
@@ -953,13 +952,9 @@ fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f)
   if (of->flags & SVM_FIFO_F_LL_TRACKED)
     pfss_fifo_del_active_list (pfss, of);
 
-  /* Update slice counts for chunks that were detached */
-  vec_foreach (c, of->chunks_at_attach)
-    {
-      fl_index = fs_freelist_for_size ((*c)->length);
-      clib_atomic_fetch_sub_rel (&fss->num_chunks[fl_index], 1);
-    }
-  vec_free (of->chunks_at_attach);
+  /* Collect chunks that were provided in return for those detached */
+  fsh_slice_collect_chunks (fs->h, fss, of->chunks_at_attach);
+  of->chunks_at_attach = 0;
 
   clib_mem_bulk_free (pfss->fifos, *f);
   *f = 0;
@@ -968,11 +963,10 @@ fifo_segment_detach_fifo (fifo_segment_t *fs, svm_fifo_t **f)
 void
 fifo_segment_attach_fifo (fifo_segment_t *fs, svm_fifo_t **f, u32 slice_index)
 {
+  svm_fifo_chunk_t *c, *nc, *pc = 0;
   fifo_slice_private_t *pfss;
   fifo_segment_slice_t *fss;
-  svm_fifo_chunk_t *c;
   svm_fifo_t *nf, *of;
-  u32 fl_index;
 
   nf = fs_fifo_alloc (fs, slice_index);
   clib_memcpy_fast (nf, *f, sizeof (*nf));
@@ -984,17 +978,19 @@ fifo_segment_attach_fifo (fifo_segment_t *fs, svm_fifo_t **f, u32 slice_index)
   if (nf->flags & SVM_FIFO_F_LL_TRACKED)
     pfss_fifo_add_active_list (pfss, nf);
 
-  /* Update allocated chunks for fifo segment and build list
-   * of chunks to be freed at detach */
+  /* Update allocated chunks for fifo segment and build list of
+   * chunks to be freed, i.e, returned to old slice at detach */
   of = *f;
-  of->chunks_at_attach = 0;
 
   c = fs_chunk_ptr (fs->h, nf->shr->start_chunk);
+  of->chunks_at_attach = pc = fsh_try_alloc_chunk (fs->h, fss, c->length);
+  c = fs_chunk_ptr (fs->h, c->next);
+
   while (c)
     {
-      fl_index = fs_freelist_for_size (c->length);
-      clib_atomic_fetch_add_rel (&fss->num_chunks[fl_index], 1);
-      vec_add1 (of->chunks_at_attach, c);
+      nc = fsh_try_alloc_chunk (fs->h, fss, c->length);
+      pc->next = fs_chunk_sptr (fs->h, nc);
+      pc = nc;
       c = fs_chunk_ptr (fs->h, c->next);
     }
 
index 7b844f0..a1b5774 100644 (file)
@@ -109,7 +109,7 @@ typedef struct _svm_fifo
   struct _svm_fifo *next; /**< prev in active chain */
   struct _svm_fifo *prev; /**< prev in active chain */
 
-  svm_fifo_chunk_t **chunks_at_attach; /**< chunks to be accounted at detach */
+  svm_fifo_chunk_t *chunks_at_attach; /**< chunks to be accounted at detach */
 
 #if SVM_FIFO_TRACE
   svm_fifo_trace_elem_t *trace;