Skip to content

Commit b138d59

Browse files
Luka Oreskovickernel-patches-bot
authored andcommitted
Since this function already exists, it made sense to implement it for
map types other than stack and queue. This patch adds the necessary parts from bpf_map_lookup_elem and bpf_map_delete_elem so it works as expected for all map types. Signed-off-by: Luka Oreskovic <luka.oreskovic@sartura.hr> CC: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr> CC: Luka Perkov <luka.perkov@sartura.hr> --- kernel/bpf/syscall.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-)
1 parent cb2afb9 commit b138d59

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

kernel/bpf/syscall.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,9 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
14751475
if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
14761476
return -EINVAL;
14771477

1478+
if (attr->flags & ~BPF_F_LOCK)
1479+
return -EINVAL;
1480+
14781481
f = fdget(ufd);
14791482
map = __bpf_map_get(f);
14801483
if (IS_ERR(map))
@@ -1485,13 +1488,19 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
14851488
goto err_put;
14861489
}
14871490

1491+
if ((attr->flags & BPF_F_LOCK) &&
1492+
!map_value_has_spin_lock(map)) {
1493+
err = -EINVAL;
1494+
goto err_put;
1495+
}
1496+
14881497
key = __bpf_copy_key(ukey, map->key_size);
14891498
if (IS_ERR(key)) {
14901499
err = PTR_ERR(key);
14911500
goto err_put;
14921501
}
14931502

1494-
value_size = map->value_size;
1503+
value_size = bpf_map_value_size(map);
14951504

14961505
err = -ENOMEM;
14971506
value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
@@ -1502,7 +1511,24 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
15021511
map->map_type == BPF_MAP_TYPE_STACK) {
15031512
err = map->ops->map_pop_elem(map, value);
15041513
} else {
1505-
err = -ENOTSUPP;
1514+
err = bpf_map_copy_value(map, key, value, attr->flags);
1515+
if (err)
1516+
goto free_value;
1517+
1518+
if (bpf_map_is_dev_bound(map)) {
1519+
err = bpf_map_offload_delete_elem(map, key);
1520+
} else if (IS_FD_PROG_ARRAY(map) ||
1521+
map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1522+
/* These maps require sleepable context */
1523+
err = map->ops->map_delete_elem(map, key);
1524+
} else {
1525+
bpf_disable_instrumentation();
1526+
rcu_read_lock();
1527+
err = map->ops->map_delete_elem(map, key);
1528+
rcu_read_unlock();
1529+
bpf_enable_instrumentation();
1530+
maybe_wait_bpf_programs(map);
1531+
}
15061532
}
15071533

15081534
if (err)

0 commit comments

Comments
 (0)