Skip to content

Commit f89aef1

Browse files
q2venKernel Patches Daemon
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]>
1 parent 6b91087 commit f89aef1

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,9 +5731,37 @@ static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
57315731
.arg5_type = ARG_CONST_SIZE,
57325732
};
57335733

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

@@ -5751,6 +5779,9 @@ static const struct bpf_func_proto bpf_sock_create_setsockopt_proto = {
57515779
BPF_CALL_5(bpf_sock_create_getsockopt, struct sock *, sk, int, level,
57525780
int, optname, char *, optval, int, optlen)
57535781
{
5782+
if (level == SOL_SOCKET && optname == SK_BPF_BYPASS_PROT_MEM)
5783+
return sk_bpf_set_get_bypass_prot_mem(sk, optval, optlen, true);
5784+
57545785
return __bpf_getsockopt(sk, level, optname, optval, optlen);
57555786
}
57565787

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)