diff mbox series

[bpf] bpf: hash_map: decrement counter on error

Message ID 1530276500-29765-1-git-send-email-mauricio.vasquez@polito.it
State Accepted, archived
Delegated to: BPF Maintainers
Headers show
Series [bpf] bpf: hash_map: decrement counter on error | expand

Commit Message

Mauricio Vasquez June 29, 2018, 12:48 p.m. UTC
Decrement the number of elements in the map in case the allocation
of a new node fails.

Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
---
 kernel/bpf/hashtab.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Comments

Daniel Borkmann June 30, 2018, 11:20 p.m. UTC | #1
On 06/29/2018 02:48 PM, Mauricio Vasquez B wrote:
> Decrement the number of elements in the map in case the allocation
> of a new node fails.
> 
> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>

Thanks for the fix, Mauricio!

Could you reply with a Fixes: tag in order to track the commit originally
introducing this bug?

Thanks,
Daniel
Mauricio Vasquez July 1, 2018, 4:33 p.m. UTC | #2
On 06/30/2018 06:20 PM, Daniel Borkmann wrote:
> On 06/29/2018 02:48 PM, Mauricio Vasquez B wrote:
>> Decrement the number of elements in the map in case the allocation
>> of a new node fails.
>>
>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
> Thanks for the fix, Mauricio!
>
> Could you reply with a Fixes: tag in order to track the commit originally
> introducing this bug?
>
> Thanks,
> Daniel
>

Sure Daniel,

Fixes: 6c9059817432 ("bpf: pre-allocate hash map elements")

Thanks,
Mauricio
Alexei Starovoitov July 3, 2018, 8:28 p.m. UTC | #3
On Sun, Jul 01, 2018 at 11:33:58AM -0500, Mauricio Vasquez wrote:
> 
> On 06/30/2018 06:20 PM, Daniel Borkmann wrote:
> > On 06/29/2018 02:48 PM, Mauricio Vasquez B wrote:
> > > Decrement the number of elements in the map in case the allocation
> > > of a new node fails.
> > > 
> > > Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
> > Thanks for the fix, Mauricio!
> > 
> > Could you reply with a Fixes: tag in order to track the commit originally
> > introducing this bug?
> > 
> > Thanks,
> > Daniel
> > 
> 
> Sure Daniel,
> 
> Fixes: 6c9059817432 ("bpf: pre-allocate hash map elements")

Good catch. Thanks for the fix.
Acked-by: Alexei Starovoitov <ast@kernel.org>
Daniel Borkmann July 3, 2018, 9:28 p.m. UTC | #4
On 07/03/2018 10:28 PM, Alexei Starovoitov wrote:
> On Sun, Jul 01, 2018 at 11:33:58AM -0500, Mauricio Vasquez wrote:
>> On 06/30/2018 06:20 PM, Daniel Borkmann wrote:
>>> On 06/29/2018 02:48 PM, Mauricio Vasquez B wrote:
>>>> Decrement the number of elements in the map in case the allocation
>>>> of a new node fails.
>>>>
>>>> Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
>>> Thanks for the fix, Mauricio!
>>>
>>> Could you reply with a Fixes: tag in order to track the commit originally
>>> introducing this bug?
>>
>> Sure Daniel,
>>
>> Fixes: 6c9059817432 ("bpf: pre-allocate hash map elements")
> 
> Good catch. Thanks for the fix.
> Acked-by: Alexei Starovoitov <ast@kernel.org>

Applied to bpf, thanks Mauricio!
diff mbox series

Patch

diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index 3ca2198..513d9df 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -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);
@@ -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;
 			}
 		}
 
@@ -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,