Skip to content

Commit 532d6b3

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Handle in-place update for full LPM trie correctly
When a LPM trie is full, in-place updates of existing elements incorrectly return -ENOSPC. Fix this by deferring the check of trie->n_entries. For new insertions, n_entries must not exceed max_entries. However, in-place updates are allowed even when the trie is full. Fixes: b95a5c4 ("bpf: add a longest prefix match trie map implementation") Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Signed-off-by: Hou Tao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent eae6a07 commit 532d6b3

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

kernel/bpf/lpm_trie.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ static struct lpm_trie_node *lpm_trie_node_alloc(const struct lpm_trie *trie,
310310
return node;
311311
}
312312

313+
static int trie_check_add_elem(struct lpm_trie *trie, u64 flags)
314+
{
315+
if (flags == BPF_EXIST)
316+
return -ENOENT;
317+
if (trie->n_entries == trie->map.max_entries)
318+
return -ENOSPC;
319+
trie->n_entries++;
320+
return 0;
321+
}
322+
313323
/* Called from syscall or from eBPF program */
314324
static long trie_update_elem(struct bpf_map *map,
315325
void *_key, void *value, u64 flags)
@@ -333,20 +343,12 @@ static long trie_update_elem(struct bpf_map *map,
333343
spin_lock_irqsave(&trie->lock, irq_flags);
334344

335345
/* Allocate and fill a new node */
336-
337-
if (trie->n_entries == trie->map.max_entries) {
338-
ret = -ENOSPC;
339-
goto out;
340-
}
341-
342346
new_node = lpm_trie_node_alloc(trie, value);
343347
if (!new_node) {
344348
ret = -ENOMEM;
345349
goto out;
346350
}
347351

348-
trie->n_entries++;
349-
350352
new_node->prefixlen = key->prefixlen;
351353
RCU_INIT_POINTER(new_node->child[0], NULL);
352354
RCU_INIT_POINTER(new_node->child[1], NULL);
@@ -375,10 +377,10 @@ static long trie_update_elem(struct bpf_map *map,
375377
* simply assign the @new_node to that slot and be done.
376378
*/
377379
if (!node) {
378-
if (flags == BPF_EXIST) {
379-
ret = -ENOENT;
380+
ret = trie_check_add_elem(trie, flags);
381+
if (ret)
380382
goto out;
381-
}
383+
382384
rcu_assign_pointer(*slot, new_node);
383385
goto out;
384386
}
@@ -392,10 +394,10 @@ static long trie_update_elem(struct bpf_map *map,
392394
ret = -EEXIST;
393395
goto out;
394396
}
395-
trie->n_entries--;
396-
} else if (flags == BPF_EXIST) {
397-
ret = -ENOENT;
398-
goto out;
397+
} else {
398+
ret = trie_check_add_elem(trie, flags);
399+
if (ret)
400+
goto out;
399401
}
400402

401403
new_node->child[0] = node->child[0];
@@ -407,10 +409,9 @@ static long trie_update_elem(struct bpf_map *map,
407409
goto out;
408410
}
409411

410-
if (flags == BPF_EXIST) {
411-
ret = -ENOENT;
412+
ret = trie_check_add_elem(trie, flags);
413+
if (ret)
412414
goto out;
413-
}
414415

415416
/* If the new node matches the prefix completely, it must be inserted
416417
* as an ancestor. Simply insert it between @node and *@slot.
@@ -424,6 +425,7 @@ static long trie_update_elem(struct bpf_map *map,
424425

425426
im_node = lpm_trie_node_alloc(trie, NULL);
426427
if (!im_node) {
428+
trie->n_entries--;
427429
ret = -ENOMEM;
428430
goto out;
429431
}
@@ -445,12 +447,8 @@ static long trie_update_elem(struct bpf_map *map,
445447
rcu_assign_pointer(*slot, im_node);
446448

447449
out:
448-
if (ret) {
449-
if (new_node)
450-
trie->n_entries--;
450+
if (ret)
451451
kfree(new_node);
452-
}
453-
454452
spin_unlock_irqrestore(&trie->lock, irq_flags);
455453
kfree_rcu(free_node, rcu);
456454

0 commit comments

Comments
 (0)