Skip to content

Commit 3dceca2

Browse files
q2venKernel Patches Daemon
authored andcommitted
bpf: Introduce SK_BPF_MEMCG_FLAGS and SK_BPF_MEMCG_EXCLUSIVE.
If a socket has sk->sk_memcg with SK_MEMCG_EXCLUSIVE, it is decoupled from the global protocol memory accounting. This is controlled by net.core.memcg_exclusive sysctl, but it lacks flexibility. Let's support flagging (and clearing) SK_MEMCG_EXCLUSIVE via bpf_setsockopt() at the BPF_CGROUP_INET_SOCK_CREATE hook. u32 flags = SK_BPF_MEMCG_EXCLUSIVE; bpf_setsockopt(ctx, SOL_SOCKET, SK_BPF_MEMCG_FLAGS, &flags, sizeof(flags)); As with net.core.memcg_exclusive, this is inherited to child sockets, and BPF always takes precedence over sysctl at socket(2) and accept(2). SK_BPF_MEMCG_FLAGS 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. For TCP child sockets, memory accounting is adjusted only in __inet_accept() which sk->sk_memcg allocation is deferred to 3. 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_MEMCG_EXCLUSIVE. This will be more visible under tcp_mem pressure. # 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_memcg.bpf.o /sys/fs/bpf/sk_memcg type cgroup/sock_create # bpftool cgroup attach /sys/fs/cgroup/test cgroup_inet_sock_create pinned /sys/fs/bpf/sk_memcg [128, 256) 6413 | | [256, 512) 1868425 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [512, 1K) 1101697 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [1K, 2K) 117031 |@@@@ | [2K, 4K) 11773 | | Signed-off-by: Kuniyuki Iwashima <[email protected]>
1 parent b3a960c commit 3dceca2

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 6 additions & 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_MEMCG_FLAGS = 1010, /* Get or Set flags saved in sk->sk_memcg */
72037204
};
72047205

72057206
enum {
@@ -7222,6 +7223,11 @@ enum {
72227223
*/
72237224
};
72247225

7226+
enum {
7227+
SK_BPF_MEMCG_EXCLUSIVE = (1UL << 0),
7228+
SK_BPF_MEMCG_FLAG_MAX = (1UL << 1),
7229+
};
7230+
72257231
struct bpf_perf_event_value {
72267232
__u64 counter;
72277233
__u64 enabled;

mm/memcontrol.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4995,6 +4995,9 @@ EXPORT_SYMBOL(memcg_sockets_enabled_key);
49954995

49964996
static void mem_cgroup_sk_set(struct sock *sk, struct mem_cgroup *memcg)
49974997
{
4998+
BUILD_BUG_ON((unsigned short)SK_MEMCG_EXCLUSIVE != SK_BPF_MEMCG_EXCLUSIVE);
4999+
BUILD_BUG_ON((unsigned short)SK_MEMCG_FLAG_MAX != SK_BPF_MEMCG_FLAG_MAX);
5000+
49985001
sk->sk_memcg = memcg;
49995002

50005003
#ifdef CONFIG_NET

net/core/filter.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5731,9 +5731,39 @@ 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_memcg_flags(struct sock *sk,
5735+
char *optval, int optlen,
5736+
bool getopt)
5737+
{
5738+
u32 flags;
5739+
5740+
if (optlen != sizeof(u32))
5741+
return -EINVAL;
5742+
5743+
if (!sk_has_account(sk))
5744+
return -EOPNOTSUPP;
5745+
5746+
if (getopt) {
5747+
*(u32 *)optval = mem_cgroup_sk_get_flags(sk);
5748+
return 0;
5749+
}
5750+
5751+
flags = *(u32 *)optval;
5752+
if (flags >= SK_BPF_MEMCG_FLAG_MAX)
5753+
return -EINVAL;
5754+
5755+
mem_cgroup_sk_set_flags(sk, flags);
5756+
5757+
return 0;
5758+
}
5759+
57345760
BPF_CALL_5(bpf_sock_create_setsockopt, struct sock *, sk, int, level,
57355761
int, optname, char *, optval, int, optlen)
57365762
{
5763+
if (IS_ENABLED(CONFIG_MEMCG) &&
5764+
level == SOL_SOCKET && optname == SK_BPF_MEMCG_FLAGS)
5765+
return sk_bpf_set_get_memcg_flags(sk, optval, optlen, false);
5766+
57375767
return __bpf_setsockopt(sk, level, optname, optval, optlen);
57385768
}
57395769

@@ -5751,6 +5781,10 @@ static const struct bpf_func_proto bpf_sock_create_setsockopt_proto = {
57515781
BPF_CALL_5(bpf_sock_create_getsockopt, struct sock *, sk, int, level,
57525782
int, optname, char *, optval, int, optlen)
57535783
{
5784+
if (IS_ENABLED(CONFIG_MEMCG) &&
5785+
level == SOL_SOCKET && optname == SK_BPF_MEMCG_FLAGS)
5786+
return sk_bpf_set_get_memcg_flags(sk, optval, optlen, true);
5787+
57545788
return __bpf_getsockopt(sk, level, optname, optval, optlen);
57555789
}
57565790

tools/include/uapi/linux/bpf.h

Lines changed: 6 additions & 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_MEMCG_FLAGS = 1010, /* Get or Set flags saved in sk->sk_memcg */
72037204
};
72047205

72057206
enum {
@@ -7222,6 +7223,11 @@ enum {
72227223
*/
72237224
};
72247225

7226+
enum {
7227+
SK_BPF_MEMCG_EXCLUSIVE = (1UL << 0),
7228+
SK_BPF_MEMCG_FLAG_MAX = (1UL << 1),
7229+
};
7230+
72257231
struct bpf_perf_event_value {
72267232
__u64 counter;
72277233
__u64 enabled;

0 commit comments

Comments
 (0)