Skip to content

Commit 58e1d04

Browse files
q2venMartin KaFai Lau
authored andcommitted
bpf: Introduce SK_BPF_BYPASS_PROT_MEM.
If a socket has sk->sk_bypass_prot_mem flagged, the socket opts out of the global protocol memory accounting. This is easily controlled by net.core.bypass_prot_mem sysctl, but it lacks flexibility. Let's support flagging (and clearing) sk->sk_bypass_prot_mem via bpf_setsockopt() at the BPF_CGROUP_INET_SOCK_CREATE hook. int val = 1; bpf_setsockopt(ctx, SOL_SOCKET, SK_BPF_BYPASS_PROT_MEM, &val, sizeof(val)); As with net.core.bypass_prot_mem, this is inherited to child sockets, and BPF always takes precedence over sysctl at socket(2) and accept(2). SK_BPF_BYPASS_PROT_MEM is only supported at BPF_CGROUP_INET_SOCK_CREATE and not supported on other hooks for some reasons: 1. UDP charges memory under sk->sk_receive_queue.lock instead of lock_sock() 2. Modifying the flag after skb is charged to sk requires such adjustment during bpf_setsockopt() and complicates the logic unnecessarily We can support other hooks later if a real use case justifies that. Most changes are inline and hard to trace, but a microbenchmark on __sk_mem_raise_allocated() during neper/tcp_stream showed that more samples completed faster with sk->sk_bypass_prot_mem == 1. This will be more visible under tcp_mem pressure (but it's not a fair comparison). # bpftrace -e 'kprobe:__sk_mem_raise_allocated { @start[tid] = nsecs; } kretprobe:__sk_mem_raise_allocated /@start[tid]/ { @EnD[tid] = nsecs - @start[tid]; @times = hist(@EnD[tid]); delete(@start[tid]); }' # tcp_stream -6 -F 1000 -N -T 256 Without bpf prog: [128, 256) 3846 | | [256, 512) 1505326 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [512, 1K) 1371006 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [1K, 2K) 198207 |@@@@@@ | [2K, 4K) 31199 |@ | With bpf prog in the next patch: (must be attached before tcp_stream) # bpftool prog load sk_bypass_prot_mem.bpf.o /sys/fs/bpf/test type cgroup/sock_create # bpftool cgroup attach /sys/fs/cgroup/test cgroup_inet_sock_create pinned /sys/fs/bpf/test [128, 256) 6413 | | [256, 512) 1868425 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [512, 1K) 1101697 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [1K, 2K) 117031 |@@@@ | [2K, 4K) 11773 | | Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Acked-by: Roman Gushchin <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 13b77b2 commit 58e1d04

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7200,6 +7200,8 @@ enum {
72007200
TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */
72017201
TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */
72027202
SK_BPF_CB_FLAGS = 1009, /* Get or set sock ops flags in socket */
7203+
SK_BPF_BYPASS_PROT_MEM = 1010, /* Get or Set sk->sk_bypass_prot_mem */
7204+
72037205
};
72047206

72057207
enum {

net/core/filter.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5733,9 +5733,37 @@ static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
57335733
.arg5_type = ARG_CONST_SIZE,
57345734
};
57355735

5736+
static int sk_bpf_set_get_bypass_prot_mem(struct sock *sk,
5737+
char *optval, int optlen,
5738+
bool getopt)
5739+
{
5740+
int val;
5741+
5742+
if (optlen != sizeof(int))
5743+
return -EINVAL;
5744+
5745+
if (!sk_has_account(sk))
5746+
return -EOPNOTSUPP;
5747+
5748+
if (getopt) {
5749+
*(int *)optval = sk->sk_bypass_prot_mem;
5750+
return 0;
5751+
}
5752+
5753+
val = *(int *)optval;
5754+
if (val < 0 || val > 1)
5755+
return -EINVAL;
5756+
5757+
sk->sk_bypass_prot_mem = val;
5758+
return 0;
5759+
}
5760+
57365761
BPF_CALL_5(bpf_sock_create_setsockopt, struct sock *, sk, int, level,
57375762
int, optname, char *, optval, int, optlen)
57385763
{
5764+
if (level == SOL_SOCKET && optname == SK_BPF_BYPASS_PROT_MEM)
5765+
return sk_bpf_set_get_bypass_prot_mem(sk, optval, optlen, false);
5766+
57395767
return __bpf_setsockopt(sk, level, optname, optval, optlen);
57405768
}
57415769

@@ -5753,6 +5781,15 @@ static const struct bpf_func_proto bpf_sock_create_setsockopt_proto = {
57535781
BPF_CALL_5(bpf_sock_create_getsockopt, struct sock *, sk, int, level,
57545782
int, optname, char *, optval, int, optlen)
57555783
{
5784+
if (level == SOL_SOCKET && optname == SK_BPF_BYPASS_PROT_MEM) {
5785+
int err = sk_bpf_set_get_bypass_prot_mem(sk, optval, optlen, true);
5786+
5787+
if (err)
5788+
memset(optval, 0, optlen);
5789+
5790+
return err;
5791+
}
5792+
57565793
return __bpf_getsockopt(sk, level, optname, optval, optlen);
57575794
}
57585795

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7200,6 +7200,7 @@ enum {
72007200
TCP_BPF_SYN_MAC = 1007, /* Copy the MAC, IP[46], and TCP header */
72017201
TCP_BPF_SOCK_OPS_CB_FLAGS = 1008, /* Get or Set TCP sock ops flags */
72027202
SK_BPF_CB_FLAGS = 1009, /* Get or set sock ops flags in socket */
7203+
SK_BPF_BYPASS_PROT_MEM = 1010, /* Get or Set sk->sk_bypass_prot_mem */
72037204
};
72047205

72057206
enum {

0 commit comments

Comments
 (0)