bpf: Change bpf_sk_storage_*() to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON
authorMartin KaFai Lau <kafai@fb.com>
Fri, 25 Sep 2020 00:04:02 +0000 (17:04 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 25 Sep 2020 20:58:01 +0000 (13:58 -0700)
This patch changes the bpf_sk_storage_*() to take
ARG_PTR_TO_BTF_ID_SOCK_COMMON such that they will work with the pointer
returned by the bpf_skc_to_*() helpers also.

A micro benchmark has been done on a "cgroup_skb/egress" bpf program
which does a bpf_sk_storage_get().  It was driven by netperf doing
a 4096 connected UDP_STREAM test with 64bytes packet.
The stats from "kernel.bpf_stats_enabled" shows no meaningful difference.

The sk_storage_get_btf_proto, sk_storage_delete_btf_proto,
btf_sk_storage_get_proto, and btf_sk_storage_delete_proto are
no longer needed, so they are removed.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Lorenz Bauer <lmb@cloudflare.com>
Link: https://lore.kernel.org/bpf/20200925000402.3856307-1-kafai@fb.com
include/net/bpf_sk_storage.h
include/uapi/linux/bpf.h
kernel/bpf/bpf_lsm.c
net/core/bpf_sk_storage.c
net/ipv4/bpf_tcp_ca.c
tools/include/uapi/linux/bpf.h

index 119f4c9..3c516dd 100644 (file)
@@ -20,8 +20,6 @@ void bpf_sk_storage_free(struct sock *sk);
 
 extern const struct bpf_func_proto bpf_sk_storage_get_proto;
 extern const struct bpf_func_proto bpf_sk_storage_delete_proto;
-extern const struct bpf_func_proto sk_storage_get_btf_proto;
-extern const struct bpf_func_proto sk_storage_delete_btf_proto;
 
 struct bpf_local_storage_elem;
 struct bpf_sk_storage_diag;
index c96a56d..0ec6dbe 100644 (file)
@@ -2861,6 +2861,7 @@ union bpf_attr {
  *             0 on success.
  *
  *             **-ENOENT** if the bpf-local-storage cannot be found.
+ *             **-EINVAL** if sk is not a fullsock (e.g. a request_sock).
  *
  * long bpf_send_signal(u32 sig)
  *     Description
index 9cd1428..78ea8a7 100644 (file)
@@ -56,9 +56,9 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
        case BPF_FUNC_inode_storage_delete:
                return &bpf_inode_storage_delete_proto;
        case BPF_FUNC_sk_storage_get:
-               return &sk_storage_get_btf_proto;
+               return &bpf_sk_storage_get_proto;
        case BPF_FUNC_sk_storage_delete:
-               return &sk_storage_delete_btf_proto;
+               return &bpf_sk_storage_delete_proto;
        default:
                return tracing_prog_func_proto(func_id, prog);
        }
index 838efc6..c907f0d 100644 (file)
@@ -269,7 +269,7 @@ BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
 {
        struct bpf_local_storage_data *sdata;
 
-       if (flags > BPF_SK_STORAGE_GET_F_CREATE)
+       if (!sk || !sk_fullsock(sk) || flags > BPF_SK_STORAGE_GET_F_CREATE)
                return (unsigned long)NULL;
 
        sdata = sk_storage_lookup(sk, map, true);
@@ -299,6 +299,9 @@ BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
 
 BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)
 {
+       if (!sk || !sk_fullsock(sk))
+               return -EINVAL;
+
        if (refcount_inc_not_zero(&sk->sk_refcnt)) {
                int err;
 
@@ -355,7 +358,7 @@ const struct bpf_func_proto bpf_sk_storage_get_proto = {
        .gpl_only       = false,
        .ret_type       = RET_PTR_TO_MAP_VALUE_OR_NULL,
        .arg1_type      = ARG_CONST_MAP_PTR,
-       .arg2_type      = ARG_PTR_TO_SOCKET,
+       .arg2_type      = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
        .arg3_type      = ARG_PTR_TO_MAP_VALUE_OR_NULL,
        .arg4_type      = ARG_ANYTHING,
 };
@@ -375,27 +378,7 @@ const struct bpf_func_proto bpf_sk_storage_delete_proto = {
        .gpl_only       = false,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_CONST_MAP_PTR,
-       .arg2_type      = ARG_PTR_TO_SOCKET,
-};
-
-const struct bpf_func_proto sk_storage_get_btf_proto = {
-       .func           = bpf_sk_storage_get,
-       .gpl_only       = false,
-       .ret_type       = RET_PTR_TO_MAP_VALUE_OR_NULL,
-       .arg1_type      = ARG_CONST_MAP_PTR,
-       .arg2_type      = ARG_PTR_TO_BTF_ID,
-       .arg2_btf_id    = &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
-       .arg3_type      = ARG_PTR_TO_MAP_VALUE_OR_NULL,
-       .arg4_type      = ARG_ANYTHING,
-};
-
-const struct bpf_func_proto sk_storage_delete_btf_proto = {
-       .func           = bpf_sk_storage_delete,
-       .gpl_only       = false,
-       .ret_type       = RET_INTEGER,
-       .arg1_type      = ARG_CONST_MAP_PTR,
-       .arg2_type      = ARG_PTR_TO_BTF_ID,
-       .arg2_btf_id    = &btf_sock_ids[BTF_SOCK_TYPE_SOCK],
+       .arg2_type      = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
 };
 
 struct bpf_sk_storage_diag {
index 74a2ef5..618954f 100644 (file)
@@ -28,22 +28,6 @@ static u32 unsupported_ops[] = {
 static const struct btf_type *tcp_sock_type;
 static u32 tcp_sock_id, sock_id;
 
-static struct bpf_func_proto btf_sk_storage_get_proto __read_mostly;
-static struct bpf_func_proto btf_sk_storage_delete_proto __read_mostly;
-
-static void convert_sk_func_proto(struct bpf_func_proto *to, const struct bpf_func_proto *from)
-{
-       int i;
-
-       *to = *from;
-       for (i = 0; i < ARRAY_SIZE(to->arg_type); i++) {
-               if (to->arg_type[i] == ARG_PTR_TO_SOCKET) {
-                       to->arg_type[i] = ARG_PTR_TO_BTF_ID;
-                       to->arg_btf_id[i] = &tcp_sock_id;
-               }
-       }
-}
-
 static int bpf_tcp_ca_init(struct btf *btf)
 {
        s32 type_id;
@@ -59,9 +43,6 @@ static int bpf_tcp_ca_init(struct btf *btf)
        tcp_sock_id = type_id;
        tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 
-       convert_sk_func_proto(&btf_sk_storage_get_proto, &bpf_sk_storage_get_proto);
-       convert_sk_func_proto(&btf_sk_storage_delete_proto, &bpf_sk_storage_delete_proto);
-
        return 0;
 }
 
@@ -188,9 +169,9 @@ bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
        case BPF_FUNC_tcp_send_ack:
                return &bpf_tcp_send_ack_proto;
        case BPF_FUNC_sk_storage_get:
-               return &btf_sk_storage_get_proto;
+               return &bpf_sk_storage_get_proto;
        case BPF_FUNC_sk_storage_delete:
-               return &btf_sk_storage_delete_proto;
+               return &bpf_sk_storage_delete_proto;
        default:
                return bpf_base_func_proto(func_id);
        }
index c96a56d..0ec6dbe 100644 (file)
@@ -2861,6 +2861,7 @@ union bpf_attr {
  *             0 on success.
  *
  *             **-ENOENT** if the bpf-local-storage cannot be found.
+ *             **-EINVAL** if sk is not a fullsock (e.g. a request_sock).
  *
  * long bpf_send_signal(u32 sig)
  *     Description