Skip to content

bpf: Allow decoupling memcg from sk->sk_prot->memory_allocated.

Kuniyuki Iwashima bpf netdev 10 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v10 Latest6 patches 9 repliesIn review
  2. v96 patches 5 repliesSuperseded
  3. v86 patches 2 repliesSuperseded
Earlier versions · 7
VersionPostedPatchesRepliesStatus
v76 patches1 replySuperseded
v65 patches4 repliesSuperseded
v55 patches4 repliesSuperseded
v45 patches16 repliesSuperseded
v35 patches12 repliesSuperseded
v28 patches2 repliesSuperseded
v18 patches8 repliesSuperseded

Changes in v10 from the cover letter

* Patch 6:
* Clean up iterations in ->create_sockets() (Martin)
* Move ASSERT inside if to avoid noisy log in ->create_sockets()
* Make run_test() static (Martin)
* Remove fexit progs & possible infinite loop in get_memory_allocated() (Martin)
* Use test__start_subtest() with if (Martin)
* Drain UDP recv queue before close() (Martin)
* Reduce NR_PAGES to 32 to make test faster

Patches in v10 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6tcp: Save lock_sock() for memcg in inet_csk_accept().net/ipv4/af_inet.c net/ipv4/inet_connection_sock.c+22 -250 replies
2/6net-memcg: Allow decoupling memcg from global protocol memory accounting.include/net/proto_memory.h include/net/sock.h include/net/tcp.h +7 more+100 -323 replies
3/6net-memcg: Introduce net.core.memcg_exclusive sysctl.Documentation/admin-guide/sysctl/net.rst include/net/netns/core.h include/net/sock.h +4 more+76 -36 replies
4/6bpf: Support bpf_setsockopt() for BPF_CGROUP_INET_SOCK_CREATE.net/core/filter.c+48 -00 replies
5/6bpf: Introduce SK_BPF_MEMCG_FLAGS and SK_BPF_MEMCG_EXCLUSIVE.include/uapi/linux/bpf.h mm/memcontrol.c net/core/filter.c +1 more+49 -00 replies
6/6selftest: bpf: Add test for SK_MEMCG_EXCLUSIVE.tools/testing/selftests/bpf/prog_tests/sk_memcg.c tools/testing/selftests/bpf/progs/sk_memcg.c+387 -00 replies

Reviews trailers per patch

3 of 6 patches have a Reviewed-by or Acked-by.

PatchShakeel ButtJohannes Weiner
1/6R-b
2/6R-b Nacked-by
3/6R-b
4/6
5/6
6/6

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v10 thread

Cover letter [PATCH v10 bpf-next/net 0/6] bpf: Allow decoupling memcg from sk->sk_prot->memory_allocated.

Some protocols (e.g., TCP, UDP) have their own memory accounting for socket buffers and charge memory to global per-protocol counters such as /proc/net/ipv4/tcp_mem.

If the socket has sk->sk_memcg, this memory is also charged to the memcg as sock in memory.stat.

We do not need to pay costs for two orthogonal memory accounting mechanisms.

This series allows decoupling memcg from the global memory accounting (memcg + tcp_mem -> memcg) if socket is configured as such by sysctl or BPF prog.

Overview of the series:

patch 1 & 2 prepares for decoupling memcg from sk_prot->memory_allocated
based on the SK_MEMCG_EXCLUSIVE flag
patch 3 introduces net.core.memcg_exclusive
patch 4 & 5 supports flagging SK_MEMCG_EXCLUSIVE via bpf_setsockopt() patch 6 is selftest

Changes:
v10:
* Patch 6:
* Clean up iterations in ->create_sockets() (Martin)
* Move ASSERT inside if to avoid noisy log in ->create_sockets()
* Make run_test() static (Martin)
* Remove fexit progs & possible infinite loop in get_memory_allocated() (Martin)
* Use test__start_subtest() with if (Martin)
* Drain UDP recv queue before close() (Martin)
* Reduce NR_PAGES to 32 to make test faster

v9: https://lore.kernel.org/netdev/[email protected]/
* Patch 1: Drop sk_is_mptcp() as it's always true for MPTCP subflow and unnecessary for SCTP (kernel-test-robot)

v8: https://lore.kernel.org/netdev/[email protected]/
* Patch 3: Fix build failure when CONFIG_NET=n (kernel-test-robot)

v7: https://lore.kernel.org/netdev/[email protected]/
* Rename s/ISOLATED/EXCLUSIVE/ (Shakeel)
* Add patch 3 (net.core.memcg_exclusive sysctl) (Shakeel)
* Reorder the core patch 2 before sysctl + bpf changes
* Patch 6
* Add test for sysctl

v6: https://lore.kernel.org/netdev/[email protected]/
* Patch 4
* Update commit message (Shakeel)
* Patch 5
* Trace sk_prot->memory_allocated + sk_prot->memory_per_cpu_fw_alloc (Martin)

v5: https://lore.kernel.org/netdev/[email protected]/
* Patch 2
* Rename new variants to bpf_sock_create_{get,set}sockopt() (Martin)
* Patch 3
* Limit getsockopt() to BPF_CGROUP_INET_SOCK_CREATE (Martin)
* Patch 5
* Use kern_sync_rcu() (Martin)
* Double NR_SEND to 128

v4: https://lore.kernel.org/netdev/[email protected]/
* Patch 2
* Use __bpf_setsockopt() instead of _bpf_setsockopt() (Martin)
* Add getsockopt() for a cgroup with multiple bpf progs running (Martin)
* Patch 3
* Only allow inet_create() to set flags (Martin)
* Inherit flags from listener to child in sk_clone_lock() (Martin)
* Support clearing flags (Martin)
* Patch 5
* Only use inet_create() hook
* Test bpf_getsockopt()
* Add serial_ prefix
* Reduce sleep() and the amount of sent data

v3: https://lore.kernel.org/netdev/[email protected]/
* Drop patches for accept() hook
* Patch 1
* Merge if blocks
* Patch2
* Drop bpf_func_proto for accept()
* Patch 3
* Allow flagging without sk->sk_memcg
* Inherit SK_BPF_MEMCG_SOCK_ISOLATED in __inet_accept()

v2: https://lore.kernel.org/bpf/[email protected]/
* Patch 2
* Define BPF_CGROUP_RUN_PROG_INET_SOCK_ACCEPT() when CONFIG_CGROUP_BPF=n
* Patch 5
* Make 2 new bpf_func_proto static
* Patch 6
* s/mem_cgroup_sk_set_flag/mem_cgroup_sk_set_flags/ when CONFIG_MEMCG=n
* Use finer CONFIG_CGROUP_BPF instead of CONFIG_BPF_SYSCALL for ifdef

v1: https://lore.kernel.org/netdev/[email protected]/

Kuniyuki Iwashima (6):
tcp: Save lock_sock() for memcg in inet_csk_accept().
net-memcg: Allow decoupling memcg from global protocol memory
accounting.
net-memcg: Introduce net.core.memcg_exclusive sysctl.
bpf: Support bpf_setsockopt() for BPF_CGROUP_INET_SOCK_CREATE.
bpf: Introduce SK_BPF_MEMCG_FLAGS and SK_BPF_MEMCG_EXCLUSIVE.
selftest: bpf: Add test for SK_MEMCG_EXCLUSIVE.

 Documentation/admin-guide/sysctl/net.rst      |   9 +
 include/net/netns/core.h                      |   3 +
 include/net/proto_memory.h                    |  15 +-
 include/net/sock.h                            |  47 ++-
 include/net/tcp.h                             |  10 +-
 include/uapi/linux/bpf.h                      |   6 +
 mm/memcontrol.c                               |  15 +-
 net/core/filter.c                             |  82 +++++
 net/core/sock.c                               |  65 ++--
 net/core/sysctl_net_core.c                    |  11 +
 net/ipv4/af_inet.c                            |  36 +++
 net/ipv4/inet_connection_sock.c               |  26 +-
 net/ipv4/tcp.c                                |   3 +-
 net/ipv4/tcp_output.c                         |  10 +-
 net/mptcp/protocol.c                          |   3 +-
 net/tls/tls_device.c                          |   4 +-
 tools/include/uapi/linux/bpf.h                |   6 +
 .../selftests/bpf/prog_tests/sk_memcg.c       | 282 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/sk_memcg.c  | 105 +++++++
 19 files changed, 680 insertions(+), 58 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sk_memcg.c
 create mode 100644 tools/testing/selftests/bpf/progs/sk_memcg.c
Signature · 1 line
2.51.0.470.ga7dc726c21-goog