Re: [PATCH 3/5] bpf: convert sock map and hash to map_copy_value
From: Jakub Sitnicki
Date: Wed Mar 11 2020 - 09:55:54 EST
On Tue, Mar 10, 2020 at 06:47 PM CET, Lorenz Bauer wrote:
> Migrate sockmap and sockhash to use map_copy_value instead of
> map_lookup_elem_sys_only.
>
> Signed-off-by: Lorenz Bauer <lmb@xxxxxxxxxxxxxx>
> ---
> net/core/sock_map.c | 48 ++++++++++++++++++++++++++++++---------------
> 1 file changed, 32 insertions(+), 16 deletions(-)
>
> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index a7075b3b4489..03e04426cd21 100644
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -344,19 +344,34 @@ static void *sock_map_lookup(struct bpf_map *map, void *key)
> return __sock_map_lookup_elem(map, *(u32 *)key);
> }
>
> -static void *sock_map_lookup_sys(struct bpf_map *map, void *key)
> +static int __sock_map_copy_value(struct bpf_map *map, struct sock *sk,
> + void *value)
> +{
> + switch (map->value_size) {
> + case sizeof(u64):
> + sock_gen_cookie(sk);
> + *(u64 *)value = atomic64_read(&sk->sk_cookie);
You could use sock_gen_cookie return value to merge the two above
statements into one. sock_gen_cookie also reads out the value.
> + return 0;
> +
> + default:
> + return -ENOSPC;
> + }
> +}
> +
> +static int sock_map_copy_value(struct bpf_map *map, void *key, void *value)
> {
> struct sock *sk;
> + int ret = -ENOENT;
>
> - if (map->value_size != sizeof(u64))
> - return ERR_PTR(-ENOSPC);
> -
> + rcu_read_lock();
> sk = __sock_map_lookup_elem(map, *(u32 *)key);
> if (!sk)
> - return ERR_PTR(-ENOENT);
> + goto out;
>
> - sock_gen_cookie(sk);
> - return &sk->sk_cookie;
> + ret = __sock_map_copy_value(map, sk, value);
> +out:
> + rcu_read_unlock();
> + return ret;
> }
>
> static int __sock_map_delete(struct bpf_stab *stab, struct sock *sk_test,
> @@ -636,7 +651,7 @@ const struct bpf_map_ops sock_map_ops = {
> .map_alloc = sock_map_alloc,
> .map_free = sock_map_free,
> .map_get_next_key = sock_map_get_next_key,
> - .map_lookup_elem_sys_only = sock_map_lookup_sys,
> + .map_copy_value = sock_map_copy_value,
> .map_update_elem = sock_map_update_elem,
> .map_delete_elem = sock_map_delete_elem,
> .map_lookup_elem = sock_map_lookup,
> @@ -1030,19 +1045,20 @@ static void sock_hash_free(struct bpf_map *map)
> kfree(htab);
> }
>
> -static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)
> +static int sock_hash_copy_value(struct bpf_map *map, void *key, void *value)
> {
> struct sock *sk;
> + int ret = -ENOENT;
>
> - if (map->value_size != sizeof(u64))
> - return ERR_PTR(-ENOSPC);
> -
> + rcu_read_lock();
> sk = __sock_hash_lookup_elem(map, key);
> if (!sk)
> - return ERR_PTR(-ENOENT);
> + goto out;
>
> - sock_gen_cookie(sk);
> - return &sk->sk_cookie;
> + ret = __sock_map_copy_value(map, sk, value);
> +out:
> + rcu_read_unlock();
> + return ret;
> }
>
> static void *sock_hash_lookup(struct bpf_map *map, void *key)
> @@ -1139,7 +1155,7 @@ const struct bpf_map_ops sock_hash_ops = {
> .map_update_elem = sock_hash_update_elem,
> .map_delete_elem = sock_hash_delete_elem,
> .map_lookup_elem = sock_hash_lookup,
> - .map_lookup_elem_sys_only = sock_hash_lookup_sys,
> + .map_copy_value = sock_hash_copy_value,
> .map_release_uref = sock_hash_release_progs,
> .map_check_btf = map_check_no_btf,
> };