Skip to content

RFC net: charge socket memory budget to memcg upfront

Cai Xinchen cgroups linux-doc linux-kselftest linux-mm lkml mptcp netdev 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest5 patches 5 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5net: charge socket memory budget to memcg upfrontinclude/net/sock.h net/core/sock.c net/ipv4/tcp.c+215 -490 replies
2/5tcp: sync memcg budget on protocol buffer updatesnet/ipv4/tcp.c net/ipv4/tcp_input.c+13 -20 replies
3/5mptcp: sync memcg budget and drop backlog page compensationnet/mptcp/protocol.c net/mptcp/protocol.h net/mptcp/sockopt.c +1 more+15 -370 replies
4/5Docs/admin-guide/cgroup-v2: document upfront socket budget chargingDocumentation/admin-guide/cgroup-v2.rst+5 -10 replies
5/5selftests/cgroup: compare socket memory deltas in test_memcg_socktools/testing/selftests/cgroup/test_memcontrol.c+13 -50 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH RFC -next 0/5] net: charge socket memory budget to memcg upfront

The memcg socket accounting currently charges pages to the memory cgroup per grant (__sk_mem_schedule() publishing forward allocation) and refunds them later from skb destructors. The refund side folds per-skb "was this charged" snapshots back into the socket balance under concurrent lockless RMW, and races there can drive the memcg socket balance negative, ending with:

page_counter underflow
WARNING: ... mm/page_counter.c ... page_counter_cancel()

This series flips the model: a socket is charged its whole memory budget (sk_sndbuf + sk_rcvbuf + sk_reserved_mem) to its memcg when the budget is established or grows, and refunded when the budget shrinks or the socket dies. Grants and per-skb charge/uncharge stop touching the memcg entirely, so the racy refund pairing has no code left to go
wrong: refunds can never exceed charges and the balance cannot underflow by construction.

Beyond removing the racy pairing, charging the budget is the more accurate accounting: once the budget is established the memory is effectively allocated to that socket - the protocol fills the buffers on demand and no other socket in the cgroup can spend that capacity. The "sock" counter therefore reports memory the sockets already own rather than memory that happens to be in flight at the sampling instant, the same model vm_committed_as uses for claimed address space and hugetlb uses for reservations (charged at creation, not at first touch). The visible consequence is that an idle socket holds its full budget charged for its lifetime. Global protocol accounting (memory_allocated, tcp_mem pressure) is untouched.

Patch overview:

1/5 net: charge socket memory budget to memcg upfront
Core tracker (sk_memcg_budget), lifecycle charge points, memcg removal from the grant path, lazy shrink sync in sk_mem_reclaim().

2/5 tcp: sync memcg budget on protocol buffer updates
tcp_init_sock(), tcp_set_rcvlowat() and the four autotune/pressure buffer writes in tcp_input.c.

3/5 mptcp: sync memcg budget and drop backlog page compensation
MPTCP budget writes, memcg inheritance transfer, and removal of the now double-charging backlog_unaccounted compensation.

4/5 Docs/admin-guide/cgroup-v2: document upfront socket budget
charging

5/5 selftests/cgroup: compare socket memory deltas in test_memcg_sock
Make the test valid under both the old and the new model.

Tested: full arm64 build with 0 warnings; each intermediate state compiles (bisectable); tools/testing/selftests/cgroup builds clean. Runtime validation on the workload that used to trigger the underflow is pending.

Cai Xinchen (5):
net: charge socket memory budget to memcg upfront
tcp: sync memcg budget on protocol buffer updates
mptcp: sync memcg budget and drop backlog page compensation Docs/admin-guide/cgroup-v2: document upfront socket budget charging selftests/cgroup: compare socket memory deltas in test_memcg_sock

 Documentation/admin-guide/cgroup-v2.rst       |   6 +-
 include/net/sock.h                            |  21 ++
 net/core/sock.c                               | 237 ++++++++++++++----
 net/ipv4/tcp.c                                |  11 +-
 net/ipv4/tcp_input.c                          |  10 +-
 net/mptcp/protocol.c                          |  42 +---
 net/mptcp/protocol.h                          |   2 +-
 net/mptcp/sockopt.c                           |   3 +
 net/mptcp/subflow.c                           |   5 +
 .../selftests/cgroup/test_memcontrol.c        |  18 +-
 10 files changed, 261 insertions(+), 94 deletions(-)
Signature · 1 line
2.18.0.huawei.25