Skip to content

Commit 13b79d3

Browse files
lmbAlexei Starovoitov
authored andcommitted
bpf: sockmap: Call sock_map_update_elem directly
Don't go via map->ops to call sock_map_update_elem, since we know what function to call in bpf_map_update_value. Since we currently don't allow calling map_update_elem from BPF context, we can remove ops->map_update_elem and rename the function to sock_map_update_elem_sys. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200821102948.21918-4-lmb@cloudflare.com
1 parent 38e12f9 commit 13b79d3

3 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,7 @@ int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
16481648
struct bpf_prog *old, u32 which);
16491649
int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
16501650
int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
1651+
int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
16511652
void sock_map_unhash(struct sock *sk);
16521653
void sock_map_close(struct sock *sk, long timeout);
16531654
#else
@@ -1669,6 +1670,12 @@ static inline int sock_map_prog_detach(const union bpf_attr *attr,
16691670
{
16701671
return -EOPNOTSUPP;
16711672
}
1673+
1674+
static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
1675+
u64 flags)
1676+
{
1677+
return -EOPNOTSUPP;
1678+
}
16721679
#endif /* CONFIG_BPF_STREAM_PARSER */
16731680

16741681
#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)

‎kernel/bpf/syscall.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
157157
if (bpf_map_is_dev_bound(map)) {
158158
return bpf_map_offload_update_elem(map, key, value, flags);
159159
} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
160-
map->map_type == BPF_MAP_TYPE_SOCKHASH ||
161-
map->map_type == BPF_MAP_TYPE_SOCKMAP ||
162160
map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
163161
return map->ops->map_update_elem(map, key, value, flags);
162+
} else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
163+
map->map_type == BPF_MAP_TYPE_SOCKMAP) {
164+
return sock_map_update_elem_sys(map, key, value, flags);
164165
} else if (IS_FD_PROG_ARRAY(map)) {
165166
return bpf_fd_array_map_update_elem(map, f.file, key, value,
166167
flags);

‎net/core/sock_map.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
562562
static int sock_hash_update_common(struct bpf_map *map, void *key,
563563
struct sock *sk, u64 flags);
564564

565-
static int sock_map_update_elem(struct bpf_map *map, void *key,
566-
void *value, u64 flags)
565+
int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
566+
u64 flags)
567567
{
568568
struct socket *sock;
569569
struct sock *sk;
@@ -687,7 +687,6 @@ const struct bpf_map_ops sock_map_ops = {
687687
.map_free = sock_map_free,
688688
.map_get_next_key = sock_map_get_next_key,
689689
.map_lookup_elem_sys_only = sock_map_lookup_sys,
690-
.map_update_elem = sock_map_update_elem,
691690
.map_delete_elem = sock_map_delete_elem,
692691
.map_lookup_elem = sock_map_lookup,
693692
.map_release_uref = sock_map_release_progs,
@@ -1181,7 +1180,6 @@ const struct bpf_map_ops sock_hash_ops = {
11811180
.map_alloc = sock_hash_alloc,
11821181
.map_free = sock_hash_free,
11831182
.map_get_next_key = sock_hash_get_next_key,
1184-
.map_update_elem = sock_map_update_elem,
11851183
.map_delete_elem = sock_hash_delete_elem,
11861184
.map_lookup_elem = sock_hash_lookup,
11871185
.map_lookup_elem_sys_only = sock_hash_lookup_sys,

0 commit comments

Comments
 (0)