Skip to content

Commit a5fa25a

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: Change bpf_sk_release and bpf_sk_*cgroup_id to accept ARG_PTR_TO_BTF_ID_SOCK_COMMON
The previous patch allows the networking bpf prog to use the bpf_skc_to_*() helpers to get a PTR_TO_BTF_ID socket pointer, e.g. "struct tcp_sock *". It allows the bpf prog to read all the fields of the tcp_sock. This patch changes the bpf_sk_release() and bpf_sk_*cgroup_id() 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. For example, the following will work: sk = bpf_skc_lookup_tcp(skb, tuple, tuplen, BPF_F_CURRENT_NETNS, 0); if (!sk) return; tp = bpf_skc_to_tcp_sock(sk); if (!tp) { bpf_sk_release(sk); return; } lsndtime = tp->lsndtime; /* Pass tp to bpf_sk_release() will also work */ bpf_sk_release(tp); Since PTR_TO_BTF_ID could be NULL, the helper taking ARG_PTR_TO_BTF_ID_SOCK_COMMON has to check for NULL at runtime. A btf_id of "struct sock" may not always mean a fullsock. Regardless the helper's running context may get a non-fullsock or not, considering fullsock check/handling is pretty cheap, it is better to keep the same verifier expectation on helper that takes ARG_PTR_TO_BTF_ID* will be able to handle the minisock situation. In the bpf_sk_*cgroup_id() case, it will try to get a fullsock by using sk_to_full_sk() as its skb variant bpf_sk"b"_*cgroup_id() has already been doing. bpf_sk_release can already handle minisock, so nothing special has to be done. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200925000356.3856047-1-kafai@fb.com
1 parent 1df8f55 commit a5fa25a

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ union bpf_attr {
25122512
* result is from *reuse*\ **->socks**\ [] using the hash of the
25132513
* tuple.
25142514
*
2515-
* long bpf_sk_release(struct bpf_sock *sock)
2515+
* long bpf_sk_release(void *sock)
25162516
* Description
25172517
* Release the reference held by *sock*. *sock* must be a
25182518
* non-**NULL** pointer that was returned from
@@ -3234,11 +3234,11 @@ union bpf_attr {
32343234
*
32353235
* **-EOVERFLOW** if an overflow happened: The same object will be tried again.
32363236
*
3237-
* u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
3237+
* u64 bpf_sk_cgroup_id(void *sk)
32383238
* Description
32393239
* Return the cgroup v2 id of the socket *sk*.
32403240
*
3241-
* *sk* must be a non-**NULL** pointer to a full socket, e.g. one
3241+
* *sk* must be a non-**NULL** pointer to a socket, e.g. one
32423242
* returned from **bpf_sk_lookup_xxx**\ (),
32433243
* **bpf_sk_fullsock**\ (), etc. The format of returned id is
32443244
* same as in **bpf_skb_cgroup_id**\ ().
@@ -3248,7 +3248,7 @@ union bpf_attr {
32483248
* Return
32493249
* The id is returned or 0 in case the id could not be retrieved.
32503250
*
3251-
* u64 bpf_sk_ancestor_cgroup_id(struct bpf_sock *sk, int ancestor_level)
3251+
* u64 bpf_sk_ancestor_cgroup_id(void *sk, int ancestor_level)
32523252
* Description
32533253
* Return id of cgroup v2 that is ancestor of cgroup associated
32543254
* with the *sk* at the *ancestor_level*. The root cgroup is at

‎net/core/filter.c‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,18 +4088,17 @@ static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
40884088
{
40894089
struct cgroup *cgrp;
40904090

4091+
sk = sk_to_full_sk(sk);
4092+
if (!sk || !sk_fullsock(sk))
4093+
return 0;
4094+
40914095
cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
40924096
return cgroup_id(cgrp);
40934097
}
40944098

40954099
BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
40964100
{
4097-
struct sock *sk = skb_to_full_sk(skb);
4098-
4099-
if (!sk || !sk_fullsock(sk))
4100-
return 0;
4101-
4102-
return __bpf_sk_cgroup_id(sk);
4101+
return __bpf_sk_cgroup_id(skb->sk);
41034102
}
41044103

41054104
static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
@@ -4115,6 +4114,10 @@ static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
41154114
struct cgroup *ancestor;
41164115
struct cgroup *cgrp;
41174116

4117+
sk = sk_to_full_sk(sk);
4118+
if (!sk || !sk_fullsock(sk))
4119+
return 0;
4120+
41184121
cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
41194122
ancestor = cgroup_ancestor(cgrp, ancestor_level);
41204123
if (!ancestor)
@@ -4126,12 +4129,7 @@ static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
41264129
BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
41274130
ancestor_level)
41284131
{
4129-
struct sock *sk = skb_to_full_sk(skb);
4130-
4131-
if (!sk || !sk_fullsock(sk))
4132-
return 0;
4133-
4134-
return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4132+
return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
41354133
}
41364134

41374135
static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
@@ -4151,7 +4149,7 @@ static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
41514149
.func = bpf_sk_cgroup_id,
41524150
.gpl_only = false,
41534151
.ret_type = RET_INTEGER,
4154-
.arg1_type = ARG_PTR_TO_SOCKET,
4152+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
41554153
};
41564154

41574155
BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
@@ -4163,7 +4161,7 @@ static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
41634161
.func = bpf_sk_ancestor_cgroup_id,
41644162
.gpl_only = false,
41654163
.ret_type = RET_INTEGER,
4166-
.arg1_type = ARG_PTR_TO_SOCKET,
4164+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
41674165
.arg2_type = ARG_ANYTHING,
41684166
};
41694167
#endif
@@ -5697,7 +5695,7 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
56975695

56985696
BPF_CALL_1(bpf_sk_release, struct sock *, sk)
56995697
{
5700-
if (sk_is_refcounted(sk))
5698+
if (sk && sk_is_refcounted(sk))
57015699
sock_gen_put(sk);
57025700
return 0;
57035701
}
@@ -5706,7 +5704,7 @@ static const struct bpf_func_proto bpf_sk_release_proto = {
57065704
.func = bpf_sk_release,
57075705
.gpl_only = false,
57085706
.ret_type = RET_INTEGER,
5709-
.arg1_type = ARG_PTR_TO_SOCK_COMMON,
5707+
.arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
57105708
};
57115709

57125710
BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,7 +2512,7 @@ union bpf_attr {
25122512
* result is from *reuse*\ **->socks**\ [] using the hash of the
25132513
* tuple.
25142514
*
2515-
* long bpf_sk_release(struct bpf_sock *sock)
2515+
* long bpf_sk_release(void *sock)
25162516
* Description
25172517
* Release the reference held by *sock*. *sock* must be a
25182518
* non-**NULL** pointer that was returned from
@@ -3234,11 +3234,11 @@ union bpf_attr {
32343234
*
32353235
* **-EOVERFLOW** if an overflow happened: The same object will be tried again.
32363236
*
3237-
* u64 bpf_sk_cgroup_id(struct bpf_sock *sk)
3237+
* u64 bpf_sk_cgroup_id(void *sk)
32383238
* Description
32393239
* Return the cgroup v2 id of the socket *sk*.
32403240
*
3241-
* *sk* must be a non-**NULL** pointer to a full socket, e.g. one
3241+
* *sk* must be a non-**NULL** pointer to a socket, e.g. one
32423242
* returned from **bpf_sk_lookup_xxx**\ (),
32433243
* **bpf_sk_fullsock**\ (), etc. The format of returned id is
32443244
* same as in **bpf_skb_cgroup_id**\ ().
@@ -3248,7 +3248,7 @@ union bpf_attr {
32483248
* Return
32493249
* The id is returned or 0 in case the id could not be retrieved.
32503250
*
3251-
* u64 bpf_sk_ancestor_cgroup_id(struct bpf_sock *sk, int ancestor_level)
3251+
* u64 bpf_sk_ancestor_cgroup_id(void *sk, int ancestor_level)
32523252
* Description
32533253
* Return id of cgroup v2 that is ancestor of cgroup associated
32543254
* with the *sk* at the *ancestor_level*. The root cgroup is at

0 commit comments

Comments
 (0)