Skip to content

Commit 80128c4

Browse files
committed
Fix race between critnib_release() and free_leaf() in critnib
Fix race between critnib_release() and free_leaf() in critnib: critnib_release() decremented ref_count to 0 and (before it called c->cb_free_leaf(k->to_be_freed)) free_leaf() added this leaf to the c->deleted_leaf list and alloc_leaf() reused it and zeroed k->to_be_freed before it could be freed in critnib_release(). This patch fixes this issue. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent c6d8e11 commit 80128c4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/critnib/critnib.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
392392
utils_atomic_store_release_u8(&k->pending_deleted_leaf, 0);
393393

394394
if (c->cb_free_leaf) {
395-
// mark the leaf as valid (ref_count == 1)
396-
utils_atomic_store_release_u64(&k->ref_count, 1ULL);
395+
// mark the leaf as valid (ref_count == 2)
396+
utils_atomic_store_release_u64(&k->ref_count, 2ULL);
397397
} else {
398398
// the reference counter is not used in this case
399399
utils_atomic_store_release_u64(&k->ref_count, 0ULL);
@@ -609,13 +609,17 @@ int critnib_release(struct critnib *c, void *ref) {
609609
}
610610

611611
/* decrement the reference count */
612-
if (utils_atomic_decrement_u64(&k->ref_count) == 0) {
612+
if (utils_atomic_decrement_u64(&k->ref_count) == 1) {
613613
void *to_be_freed = NULL;
614614
utils_atomic_load_acquire_ptr(&k->to_be_freed, &to_be_freed);
615615
if (to_be_freed) {
616616
utils_atomic_store_release_ptr(&k->to_be_freed, NULL);
617617
c->cb_free_leaf(c->leaf_allocator, to_be_freed);
618618
}
619+
620+
// mark the leaf as not used (ref_count == 0)
621+
utils_atomic_store_release_u64(&k->ref_count, 0ULL);
622+
619623
uint8_t pending_deleted_leaf;
620624
utils_atomic_load_acquire_u8(&k->pending_deleted_leaf,
621625
&pending_deleted_leaf);

0 commit comments

Comments
 (0)