From: Kuniyuki Iwashima <kuniyu@google.com>
To: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <martin.lau@linux.dev>,
John Fastabend <john.fastabend@gmail.com>,
Stanislav Fomichev <sdf@fomichev.me>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Neal Cardwell <ncardwell@google.com>,
Willem de Bruijn <willemb@google.com>,
Mina Almasry <almasrymina@google.com>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
bpf@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH v10 bpf-next/net 3/6] net-memcg: Introduce net.core.memcg_exclusive sysctl.
Date: Fri, 19 Sep 2025 23:22:21 -0700 [thread overview]
Message-ID: <CAAVpQUAkhJ8L_z7Y3T9HkZ-rcgsZE2Sbk0QEh4EUCf_mcAV19Q@mail.gmail.com> (raw)
In-Reply-To: <bn7f2mwrkbdfhyodf74nfx6qnbpfmqm2gzkgvnuulcq3ha6sib@2oxhp2xgfwha>
On Fri, Sep 19, 2025 at 10:36 PM Shakeel Butt <shakeel.butt@linux.dev> wrote:
>
> On Sat, Sep 20, 2025 at 12:07:17AM +0000, Kuniyuki Iwashima wrote:
> > If net.core.memcg_exclusive is 1 when sk->sk_memcg is allocated,
> > the socket is flagged with SK_MEMCG_EXCLUSIVE internally and skips
> > the global per-protocol memory accounting.
> >
> > OTOH, for accept()ed child sockets, this flag is inherited from
> > the listening socket in sk_clone_lock() and set in __inet_accept().
> > This is to preserve the decision by BPF which will be supported later.
> >
> > Given sk->sk_memcg can be accessed in the fast path, it would
> > be preferable to place the flag field in the same cache line as
> > sk->sk_memcg.
> >
> > However, struct sock does not have such a 1-byte hole.
> >
> > Let's store the flag in the lowest bit of sk->sk_memcg and check
> > it in mem_cgroup_sk_exclusive().
> >
> > Tested with a script that creates local socket pairs and send()s a
> > bunch of data without recv()ing.
> >
> > Setup:
> >
> > # mkdir /sys/fs/cgroup/test
> > # echo $$ >> /sys/fs/cgroup/test/cgroup.procs
> > # sysctl -q net.ipv4.tcp_mem="1000 1000 1000"
> >
> > Without net.core.memcg_exclusive, charged to memcg & tcp_mem:
> >
> > # prlimit -n=524288:524288 bash -c "python3 pressure.py" &
> > # cat /sys/fs/cgroup/test/memory.stat | grep sock
> > sock 22642688 <-------------------------------------- charged to memcg
> > # cat /proc/net/sockstat| grep TCP
> > TCP: inuse 2006 orphan 0 tw 0 alloc 2008 mem 5376 <-- charged to tcp_mem
> > # ss -tn | head -n 5
> > State Recv-Q Send-Q Local Address:Port Peer Address:Port
> > ESTAB 2000 0 127.0.0.1:34479 127.0.0.1:53188
> > ESTAB 2000 0 127.0.0.1:34479 127.0.0.1:49972
> > ESTAB 2000 0 127.0.0.1:34479 127.0.0.1:53868
> > ESTAB 2000 0 127.0.0.1:34479 127.0.0.1:53554
> > # nstat | grep Pressure || echo no pressure
> > TcpExtTCPMemoryPressures 1 0.0
> >
> > With net.core.memcg_exclusive=1, only charged to memcg:
> >
> > # sysctl -q net.core.memcg_exclusive=1
> > # prlimit -n=524288:524288 bash -c "python3 pressure.py" &
> > # cat /sys/fs/cgroup/test/memory.stat | grep sock
> > sock 2757468160 <------------------------------------ charged to memcg
> > # cat /proc/net/sockstat | grep TCP
> > TCP: inuse 2006 orphan 0 tw 0 alloc 2008 mem 0 <- NOT charged to tcp_mem
> > # ss -tn | head -n 5
> > State Recv-Q Send-Q Local Address:Port Peer Address:Port
> > ESTAB 111000 0 127.0.0.1:36019 127.0.0.1:49026
> > ESTAB 110000 0 127.0.0.1:36019 127.0.0.1:45630
> > ESTAB 110000 0 127.0.0.1:36019 127.0.0.1:44870
> > ESTAB 111000 0 127.0.0.1:36019 127.0.0.1:45274
> > # nstat | grep Pressure || echo no pressure
> > no pressure
> >
> > Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> > ---
> > v8: Fix build failure when CONFIG_NET=n
> > ---
> > Documentation/admin-guide/sysctl/net.rst | 9 ++++++
> > include/net/netns/core.h | 3 ++
> > include/net/sock.h | 39 ++++++++++++++++++++++--
> > mm/memcontrol.c | 12 +++++++-
> > net/core/sock.c | 1 +
> > net/core/sysctl_net_core.c | 11 +++++++
> > net/ipv4/af_inet.c | 4 +++
> > 7 files changed, 76 insertions(+), 3 deletions(-)
> >
> > diff --git a/Documentation/admin-guide/sysctl/net.rst b/Documentation/admin-guide/sysctl/net.rst
> > index 2ef50828aff1..7272194dcf45 100644
> > --- a/Documentation/admin-guide/sysctl/net.rst
> > +++ b/Documentation/admin-guide/sysctl/net.rst
> > @@ -212,6 +212,15 @@ mem_pcpu_rsv
> >
> > Per-cpu reserved forward alloc cache size in page units. Default 1MB per CPU.
> >
> > +memcg_exclusive
> > +---------------
> > +
> > +Skip charging socket buffers to the per-protocol global memory accounting
> > +(controlled by net.ipv4.tcp_mem, etc) if they are already charged to the
> > +cgroup memory controller ("sock" in memory.stat file).
> > +
> > +Default: 0
> > +
> > rmem_default
> > ------------
> >
> > diff --git a/include/net/netns/core.h b/include/net/netns/core.h
> > index 9b36f0ff0c20..ec511088e67d 100644
> > --- a/include/net/netns/core.h
> > +++ b/include/net/netns/core.h
> > @@ -16,6 +16,9 @@ struct netns_core {
> > int sysctl_optmem_max;
> > u8 sysctl_txrehash;
> > u8 sysctl_tstamp_allow_data;
> > +#ifdef CONFIG_MEMCG
> > + u8 sysctl_memcg_exclusive;
> > +#endif
>
> Hmm will this be a system level or namespace level sysctl? Seems like ns
> level, any reason to go with netns level?
This is netns-level, I don't have a specific reason other than
granularity and have no preference. Anyway, either
system-level or netns-level require admin privilege.
next prev parent reply other threads:[~2025-09-20 6:22 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-20 0:07 [PATCH v10 bpf-next/net 0/6] bpf: Allow decoupling memcg from sk->sk_prot->memory_allocated Kuniyuki Iwashima
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 1/6] tcp: Save lock_sock() for memcg in inet_csk_accept() Kuniyuki Iwashima
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 2/6] net-memcg: Allow decoupling memcg from global protocol memory accounting Kuniyuki Iwashima
2025-09-20 5:25 ` Shakeel Butt
2025-09-25 22:40 ` Kuniyuki Iwashima
2025-09-27 14:49 ` Martin KaFai Lau
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 3/6] net-memcg: Introduce net.core.memcg_exclusive sysctl Kuniyuki Iwashima
2025-09-20 5:35 ` Shakeel Butt
2025-09-20 6:22 ` Kuniyuki Iwashima [this message]
2025-09-23 0:54 ` Shakeel Butt
2025-09-23 1:03 ` Kuniyuki Iwashima
2025-09-23 1:26 ` Kuniyuki Iwashima
2025-09-25 22:52 ` Shakeel Butt
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 4/6] bpf: Support bpf_setsockopt() for BPF_CGROUP_INET_SOCK_CREATE Kuniyuki Iwashima
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 5/6] bpf: Introduce SK_BPF_MEMCG_FLAGS and SK_BPF_MEMCG_EXCLUSIVE Kuniyuki Iwashima
2025-09-20 0:07 ` [PATCH v10 bpf-next/net 6/6] selftest: bpf: Add test for SK_MEMCG_EXCLUSIVE Kuniyuki Iwashima
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAAVpQUAkhJ8L_z7Y3T9HkZ-rcgsZE2Sbk0QEh4EUCf_mcAV19Q@mail.gmail.com \
--to=kuniyu@google.com \
--cc=almasrymina@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=hannes@cmpxchg.org \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=kuni1840@gmail.com \
--cc=martin.lau@linux.dev \
--cc=mhocko@kernel.org \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=sdf@fomichev.me \
--cc=shakeel.butt@linux.dev \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).