From: Yingjie Wang <x-professor@outlook.com>
To: "netdev@vger.kernel.org" <netdev@vger.kernel.org>
Cc: David Ahern <dsahern@kernel.org>,
Ido Schimmel <idosch@nvidia.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
Vladimir Davydov <vdavydov.dev@gmail.com>,
"cgroups@vger.kernel.org" <cgroups@vger.kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg
Date: Thu, 19 Nov 2026 15:34:39 +0000 [thread overview]
Message-ID: <5f2e25611e300561db512cd3ad3190f4d782484a.1795102467.git.x-professor@outlook.com> (raw)
In-Reply-To: <cover.1795102467.git.x-professor@outlook.com>
sock_kmalloc() limits allocations using sk_omem_alloc and optmem_max,
but does not request kernel memory cgroup accounting. Allocations retained
by a socket can therefore consume kernel memory without contributing to
memory.current.
This was found while fuzzing socket option paths and confirmed with cgroup
accounting measurements. As an unprivileged user, 2,000 allocations
through IPV6_2292PKTOPTIONS retained about 254 MiB of slab while a cgroup
with memory.max set to 64 MiB reported about 5.5 MiB in memory.current.
Use GFP_KERNEL_ACCOUNT for these long-lived socket allocations: the
IPV6_2292PKTOPTIONS cmsg stream, the IPv4 and IPv6 multicast socket source
lists, and the IPv4 and IPv6 multicast membership records.
sock_kmalloc() itself is left unchanged: some callers run in softirq
context, where current_obj_cgroup() uses an explicitly active memcg or
the root memcg, so those allocations have different charge-attribution
semantics.
The per-socket optmem_max budget is unchanged; cgroup charging adds a
limit for memory which outlives the syscall. With this change, the same
workload hit the 64 MiB limit: setsockopt() failed with ENOBUFS after
497 successful allocations. Small option and membership operations still
succeeded. Kernel selftests were not run.
Fixes: a9bb7e620efd ("memcg: only account kmem allocations marked as __GFP_ACCOUNT")
Cc: <stable+noautosel@kernel.org> # changes memcg charging; not stable material
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
Tested on net/main e203d8a4751d with this series: x86_64 W=1 kernel
build, affected-object builds for allmodconfig, i386 and
CONFIG_IP_MULTICAST=n, and the QEMU reproducers and regression tests
described in the cover letter.
net/ipv4/igmp.c | 6 +++---
net/ipv6/ipv6_sockglue.c | 2 +-
net/ipv6/mcast.c | 9 +++++----
3 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index bfa448028f8a..5fe8736c7b47 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2381,7 +2381,7 @@ static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
err = -ENOBUFS;
if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
goto done;
- iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
+ iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL_ACCOUNT);
if (!iml)
goto done;
@@ -2590,7 +2590,7 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
if (psl)
count += psl->sl_max;
newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
- GFP_KERNEL);
+ GFP_KERNEL_ACCOUNT);
if (!newpsl) {
err = -ENOBUFS;
goto done;
@@ -2693,7 +2693,7 @@ int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
if (msf->imsf_numsrc) {
newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
msf->imsf_numsrc),
- GFP_KERNEL);
+ GFP_KERNEL_ACCOUNT);
if (!newpsl) {
err = -ENOBUFS;
goto done;
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index 82573260b740..89394f8d963f 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -748,7 +748,7 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
if (optlen > 64*1024)
break;
- opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
+ opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL_ACCOUNT);
retv = -ENOBUFS;
if (!opt)
break;
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index 0d982f7d29cd..ede1f0825bad 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -213,7 +213,8 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
return -EADDRINUSE;
}
- mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
+ mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist),
+ GFP_KERNEL_ACCOUNT);
if (!mc_lst)
return -ENOMEM;
@@ -435,7 +436,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
} else {
newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
psl->sl_count - 1),
- GFP_KERNEL);
+ GFP_KERNEL_ACCOUNT);
if (!newpsl) {
atomic_add(struct_size(psl, sl_addr, psl->sl_max),
&sk->sk_omem_alloc);
@@ -473,7 +474,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
i = psl ? psl->sl_count + 1 : 1;
newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, i),
- GFP_KERNEL);
+ GFP_KERNEL_ACCOUNT);
if (!newpsl) {
err = -ENOBUFS;
goto done;
@@ -564,7 +565,7 @@ int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
if (gsf->gf_numsrc) {
newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
gsf->gf_numsrc),
- GFP_KERNEL);
+ GFP_KERNEL_ACCOUNT);
if (!newpsl) {
err = -ENOBUFS;
goto done;
--
2.43.0
next prev parent reply other threads:[~2026-11-19 15:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 15:34 [PATCH net 0/3] net: account socket and multicast allocations " Yingjie Wang
2026-11-19 15:34 ` [PATCH net 1/3] net: multicast: keep source filter state when a source update fails Yingjie Wang
2026-11-19 15:34 ` Yingjie Wang [this message]
2026-11-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations to memcg Yingjie Wang
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=5f2e25611e300561db512cd3ad3190f4d782484a.1795102467.git.x-professor@outlook.com \
--to=x-professor@outlook.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=vdavydov.dev@gmail.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
all inboxes | Powered by JetHome®