Skip to content

Commit

Permalink
bpf: hash map: decrement counter on error
Browse files Browse the repository at this point in the history
Decrement the number of elements in the map in case the allocation
of a new node fails.

Fixes: 6c90598 ("bpf: pre-allocate hash map elements")
Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
mauriciovasquezbernal authored and borkmann committed Jul 3, 2018
1 parent 39d393c commit ed2b82c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions kernel/bpf/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,15 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
* old element will be freed immediately.
* Otherwise return an error
*/
atomic_dec(&htab->count);
return ERR_PTR(-E2BIG);
l_new = ERR_PTR(-E2BIG);
goto dec_count;
}
l_new = kmalloc_node(htab->elem_size, GFP_ATOMIC | __GFP_NOWARN,
htab->map.numa_node);
if (!l_new)
return ERR_PTR(-ENOMEM);
if (!l_new) {
l_new = ERR_PTR(-ENOMEM);
goto dec_count;
}
}

memcpy(l_new->key, key, key_size);
Expand All @@ -766,7 +768,8 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
GFP_ATOMIC | __GFP_NOWARN);
if (!pptr) {
kfree(l_new);
return ERR_PTR(-ENOMEM);
l_new = ERR_PTR(-ENOMEM);
goto dec_count;
}
}

Expand All @@ -780,6 +783,9 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,

l_new->hash = hash;
return l_new;
dec_count:
atomic_dec(&htab->count);
return l_new;
}

static int check_flags(struct bpf_htab *htab, struct htab_elem *l_old,
Expand Down

0 comments on commit ed2b82c

Please sign in to comment.