* [PATCH net 0/3] net: account socket and multicast allocations to memcg
@ 2026-11-19 15:34 Yingjie Wang
2026-11-19 15:34 ` [PATCH net 1/3] net: multicast: keep source filter state when a source update fails Yingjie Wang
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Yingjie Wang @ 2026-11-19 15:34 UTC (permalink / raw)
To: netdev@vger.kernel.org
Cc: David Ahern, Ido Schimmel, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Johannes Weiner,
Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song,
Andrew Morton, Vladimir Davydov, cgroups@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Several allocations retained by sockets or multicast device state are
not charged to memory cgroups. A task can retain substantially more
kernel memory than its memory.max limit permits.
With the default optmem_max, an unprivileged user retaining 2,000
IPV6_2292PKTOPTIONS buffers consumed about 254 MiB of slab while a
cgroup limited to 64 MiB reported about 5.5 MiB in memory.current. In a
user and network namespace with raised multicast limits, 1,200 IPv4
memberships with 1,024 sources each retained about 85 MiB of slab while
the cgroup reported less than 1 MiB. Both were measured on the base of
this series.
Patch 1 fixes source-filter error handling before making more
allocations failable at a memcg limit. Removing a never-added source
from an any-source membership returns EADDRNOTAVAIL but changes the
empty filter to INCLUDE, so the socket stops receiving the group. The
patch restores the old mode on failure, checks device-side source
allocation errors before publishing the socket filter, and removes a
membership created by a failed source join.
Patch 2 accounts IPV6_2292PKTOPTIONS buffers and IPv4/IPv6 socket
membership and source-list allocations. It leaves sock_kmalloc()
unchanged because its other callers have different execution contexts
and charge attribution. Patch 3 accounts device source and tomb entries,
deleted IPv6 group records, and live IPv6 device groups created by
socket joins. Live groups created by automatic address configuration
keep their existing allocation policy. IPv4 keeps GFP_ATOMIC, so its
charges are forced and can temporarily exceed memory.max. IPv6 uses
GFP_KERNEL_ACCOUNT.
IPv4 device group records remain unaccounted: their allocation helper
cannot report failure and has callers with different GFP constraints.
Accounting socket memberships limits the membership-only case, but each
socket record can still pin a larger unaccounted device group when every
membership uses a different group. This series does not close that case.
The pending patch "igmp: use copy-on-write RCU updates in
ip_mc_source()"
(https://lore.kernel.org/r/20261102095312.47767-1-18983559317@163.com)
also changes the IPv4 function. It is not in this base; this series
leaves the existing in-place RCU update races to that work.
Validation on this base and the corresponding patched tree:
- full x86_64 W=1 bzImage builds;
- affected-object W=1 builds at each intermediate commit, plus final
allmodconfig, i386 and CONFIG_IP_MULTICAST=n builds;
- IPv4/IPv6 group reception changed from 4/4 to 0/4 after an invalid
source leave on the base, and stayed 4/4 with the series;
- valid source join/leave sequences passed, and FAILSLAB tests
confirmed ENOBUFS with preserved filter/membership state on the
patched kernel;
- large option, membership and source-filter cases were constrained by
a 64 MiB cgroup limit; small operations succeeded;
- with delayed MLD reports and a 16 MiB cgroup limit on both kernels,
the base completed 200,000 IPv6 join/leave operations without a
memory.max event; the memcg OOM killer stopped the patched run after
at least 60,000 operations;
- no unexpected BUG, WARNING, Oops or panic in the guest consoles.
Kernel selftests were not run, and the large membership case was run
only on the patched kernel.
Patch 1 is an independent correctness fix suitable for stable. Patches 2
and 3 change charging behaviour and are marked noautosel. If you would
rather take patches 2 and 3 through net-next, I can resend them there
once patch 1 has been merged.
Yingjie Wang (3):
net: multicast: keep source filter state when a source update fails
net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg
net: multicast: account group and source allocations to memcg
net/ipv4/igmp.c | 88 ++++++++++++++++++++++++++--------------
net/ipv4/ip_sockglue.c | 33 ++++++++-------
net/ipv6/ipv6_sockglue.c | 13 +++++-
net/ipv6/mcast.c | 50 +++++++++++++++--------
4 files changed, 120 insertions(+), 64 deletions(-)
base-commit: e203d8a4751d48aa00eda31e3b320ba8e0d47dbf
--
2.43.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH net 1/3] net: multicast: keep source filter state when a source update fails
2026-11-19 15:34 [PATCH net 0/3] net: account socket and multicast allocations to memcg Yingjie Wang
@ 2026-11-19 15:34 ` Yingjie Wang
2026-11-23 16:24 ` netdev-bot+sashiko
2026-11-19 15:34 ` [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg Yingjie Wang
2026-11-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations " Yingjie Wang
2 siblings, 1 reply; 7+ messages in thread
From: Yingjie Wang @ 2026-11-19 15:34 UTC (permalink / raw)
To: netdev@vger.kernel.org
Cc: David Ahern, Ido Schimmel, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Johannes Weiner,
Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song,
Andrew Morton, Vladimir Davydov, cgroups@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
ip_mc_source() and ip6_mc_source() switch the mode of an empty socket
filter before they look up or add the source, but do not restore it when
the operation then fails.
For example, after IP_ADD_MEMBERSHIP or IPV6_JOIN_GROUP the socket filter
is EXCLUDE with no sources. IP_DROP_SOURCE_MEMBERSHIP or
MCAST_LEAVE_SOURCE_GROUP for a source which was never added returns
EADDRNOTAVAIL, but leaves the filter in INCLUDE mode with no sources, so
the socket silently stops receiving the group. In a QEMU guest running
an unpatched kernel, a UDP socket received 4 of 4 group datagrams before
such a call and 0 of 4 afterwards, for both IPv4 and IPv6. No privilege
is needed.
The add paths leave partial state behind as well. They ignore errors
returned while adding the source to the interface filter, so if the
device-side source entry cannot be allocated, the socket-side filter is
still committed and setsockopt() returns success.
MCAST_JOIN_SOURCE_GROUP and IP_ADD_SOURCE_MEMBERSHIP create an INCLUDE
membership before adding the source and keep that empty membership if
the source update fails.
Restore the previous mode when an operation on an empty filter fails.
Return device-side allocation errors before publishing the socket filter.
Drop a membership which the same setsockopt() call created.
This was found while testing memcg accounting with slab fault injection;
review of the same error paths then found the EADDRNOTAVAIL case above.
Before the fix, injected IPv4 and IPv6 allocation failures either
returned success or left the filter in INCLUDE mode with no source.
After the fix, the EADDRNOTAVAIL case keeps the EXCLUDE filter and the
group is still received, and injected allocation failures return ENOBUFS
and preserve the previous filter and membership state. Kernel selftests
were not run.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
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 | 74 ++++++++++++++++++++++++++--------------
net/ipv4/ip_sockglue.c | 33 ++++++++++--------
net/ipv6/ipv6_sockglue.c | 11 +++++-
net/ipv6/mcast.c | 16 +++++++--
4 files changed, 92 insertions(+), 42 deletions(-)
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 52cde8782eeb..bfa448028f8a 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2491,9 +2491,10 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
struct ip_mc_socklist *pmc;
struct in_device *in_dev = NULL;
struct inet_sock *inet = inet_sk(sk);
- struct ip_sf_socklist *psl;
+ struct ip_sf_socklist *newpsl = NULL, *psl;
struct net *net = sock_net(sk);
int leavegroup = 0;
+ int oldmode = -1;
int i, j, rv;
if (!ipv4_is_multicast(addr))
@@ -2530,9 +2531,10 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
}
} else if (pmc->sfmode != omode) {
/* allow mode switches for empty-set filters */
+ oldmode = pmc->sfmode;
ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
- ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
- NULL, 0);
+ ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, oldmode, 0,
+ NULL, 0);
pmc->sfmode = omode;
}
@@ -2572,8 +2574,17 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
err = -ENOBUFS;
goto done;
}
+ rv = 1; /* > 0 for insert logic below if sl_count is 0 */
+ for (i = 0; psl && i < psl->sl_count; i++) {
+ rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
+ sizeof(__be32));
+ if (rv == 0)
+ break;
+ }
+ if (rv == 0) /* address already there is an error */
+ goto done;
+
if (!psl || psl->sl_count == psl->sl_max) {
- struct ip_sf_socklist *newpsl;
int count = IP_SFBLOCK;
if (psl)
@@ -2585,10 +2596,30 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
goto done;
}
newpsl->sl_max = count;
- newpsl->sl_count = count - IP_SFBLOCK;
+ newpsl->sl_count = psl ? psl->sl_count : 0;
+ if (psl) {
+ for (j = 0; j < psl->sl_count; j++)
+ newpsl->sl_addr[j] = psl->sl_addr[j];
+ }
+ for (j = newpsl->sl_count - 1; j >= i; j--)
+ newpsl->sl_addr[j + 1] = newpsl->sl_addr[j];
+ newpsl->sl_addr[i] = mreqs->imr_sourceaddr;
+ newpsl->sl_count++;
+ }
+
+ /* update the interface list */
+ err = ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
+ &mreqs->imr_sourceaddr, 1);
+ if (err) {
+ if (newpsl)
+ sock_kfree_s(sk, newpsl,
+ struct_size(newpsl, sl_addr,
+ newpsl->sl_max));
+ goto done;
+ }
+
+ if (newpsl) {
if (psl) {
- for (i = 0; i < psl->sl_count; i++)
- newpsl->sl_addr[i] = psl->sl_addr[i];
/* decrease mem now to avoid the memleak warning */
atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
&sk->sk_omem_alloc);
@@ -2596,26 +2627,19 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
rcu_assign_pointer(pmc->sflist, newpsl);
if (psl)
kfree_rcu(psl, rcu);
- psl = newpsl;
- }
- rv = 1; /* > 0 for insert logic below if sl_count is 0 */
- for (i = 0; i < psl->sl_count; i++) {
- rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
- sizeof(__be32));
- if (rv == 0)
- break;
+ } else {
+ for (j = psl->sl_count - 1; j >= i; j--)
+ psl->sl_addr[j + 1] = psl->sl_addr[j];
+ psl->sl_addr[i] = mreqs->imr_sourceaddr;
+ psl->sl_count++;
}
- if (rv == 0) /* address already there is an error */
- goto done;
- for (j = psl->sl_count-1; j >= i; j--)
- psl->sl_addr[j+1] = psl->sl_addr[j];
- psl->sl_addr[i] = mreqs->imr_sourceaddr;
- psl->sl_count++;
- err = 0;
- /* update the interface list */
- ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
- &mreqs->imr_sourceaddr, 1);
done:
+ if (err && oldmode >= 0) {
+ ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, oldmode, 0,
+ NULL, 0);
+ ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
+ pmc->sfmode = oldmode;
+ }
if (leavegroup)
err = ip_mc_leave_group(sk, &imr);
return err;
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index e06c1f48ecad..b4d466ddc80b 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -723,7 +723,9 @@ static int do_mcast_group_source(struct sock *sk, int optname,
{
struct group_source_req greqs;
struct ip_mreq_source mreqs;
+ struct ip_mreqn joined = {};
struct sockaddr_in *psin;
+ bool drop_group = false;
int omode, add, err;
err = copy_group_source_from_sockptr(&greqs, optval, optlen);
@@ -747,23 +749,24 @@ static int do_mcast_group_source(struct sock *sk, int optname,
omode = MCAST_EXCLUDE;
add = 0;
} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
- struct ip_mreqn mreq;
-
psin = (struct sockaddr_in *)&greqs.gsr_group;
- mreq.imr_multiaddr = psin->sin_addr;
- mreq.imr_address.s_addr = 0;
- mreq.imr_ifindex = greqs.gsr_interface;
- err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
+ joined.imr_multiaddr = psin->sin_addr;
+ joined.imr_ifindex = greqs.gsr_interface;
+ err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE);
if (err && err != -EADDRINUSE)
return err;
- greqs.gsr_interface = mreq.imr_ifindex;
+ drop_group = !err;
+ greqs.gsr_interface = joined.imr_ifindex;
omode = MCAST_INCLUDE;
add = 1;
} else /* MCAST_LEAVE_SOURCE_GROUP */ {
omode = MCAST_INCLUDE;
add = 0;
}
- return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
+ err = ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
+ if (err && drop_group)
+ ip_mc_leave_group(sk, &joined);
+ return err;
}
static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
@@ -1286,7 +1289,9 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
case IP_ADD_SOURCE_MEMBERSHIP:
case IP_DROP_SOURCE_MEMBERSHIP:
{
+ struct ip_mreqn joined = {};
struct ip_mreq_source mreqs;
+ bool drop_group = false;
int omode, add;
if (optlen != sizeof(struct ip_mreq_source))
@@ -1302,14 +1307,12 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
omode = MCAST_EXCLUDE;
add = 0;
} else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
- struct ip_mreqn mreq;
-
- mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
- mreq.imr_address.s_addr = mreqs.imr_interface;
- mreq.imr_ifindex = 0;
- err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
+ joined.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
+ joined.imr_address.s_addr = mreqs.imr_interface;
+ err = ip_mc_join_group_ssm(sk, &joined, MCAST_INCLUDE);
if (err && err != -EADDRINUSE)
break;
+ drop_group = !err;
omode = MCAST_INCLUDE;
add = 1;
} else /* IP_DROP_SOURCE_MEMBERSHIP */ {
@@ -1317,6 +1320,8 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
add = 0;
}
err = ip_mc_source(add, omode, sk, &mreqs, 0);
+ if (err && drop_group)
+ ip_mc_leave_group(sk, &joined);
break;
}
case MCAST_JOIN_GROUP:
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index 4b3536571c98..82573260b740 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -147,6 +147,7 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
sockptr_t optval, int optlen)
{
struct group_source_req greqs;
+ bool drop_group = false;
int omode, add;
int ret;
@@ -175,13 +176,21 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
/* prior join w/ different source is ok */
if (retv && retv != -EADDRINUSE)
return retv;
+ drop_group = !retv;
omode = MCAST_INCLUDE;
add = 1;
} else /* MCAST_LEAVE_SOURCE_GROUP */ {
omode = MCAST_INCLUDE;
add = 0;
}
- return ip6_mc_source(add, omode, sk, &greqs);
+ ret = ip6_mc_source(add, omode, sk, &greqs);
+ if (ret && drop_group) {
+ struct sockaddr_in6 *psin6;
+
+ psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
+ ipv6_sock_mc_drop(sk, greqs.gsr_interface, &psin6->sin6_addr);
+ }
+ return ret;
}
static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index ecef55f26189..0d982f7d29cd 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -363,6 +363,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
struct ipv6_mc_socklist *pmc;
struct inet6_dev *idev;
int leavegroup = 0;
+ int oldmode = -1;
int i, j;
int err;
@@ -403,8 +404,9 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
}
} else if (pmc->sfmode != omode) {
/* allow mode switches for empty-set filters */
+ oldmode = pmc->sfmode;
ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
- ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
+ ip6_mc_del_src(idev, group, oldmode, 0, NULL, 0);
pmc->sfmode = omode;
}
@@ -485,7 +487,12 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
newpsl->sl_addr[i - 1] = *source;
/* update the interface list */
- ip6_mc_add_src(idev, group, omode, 1, source, 1);
+ err = ip6_mc_add_src(idev, group, omode, 1, source, 1);
+ if (err) {
+ sock_kfree_s(sk, newpsl,
+ struct_size(newpsl, sl_addr, newpsl->sl_max));
+ goto done;
+ }
if (psl)
atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
@@ -494,6 +501,11 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
kfree_rcu(psl, rcu);
err = 0;
done:
+ if (err && oldmode >= 0) {
+ ip6_mc_add_src(idev, group, oldmode, 0, NULL, 0);
+ ip6_mc_del_src(idev, group, omode, 0, NULL, 0);
+ pmc->sfmode = oldmode;
+ }
mutex_unlock(&idev->mc_lock);
in6_dev_put(idev);
if (leavegroup)
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg
2026-11-19 15:34 [PATCH net 0/3] net: account socket and multicast allocations to memcg 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
2026-11-23 16:24 ` netdev-bot+sashiko
2026-11-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations " Yingjie Wang
2 siblings, 1 reply; 7+ messages in thread
From: Yingjie Wang @ 2026-11-19 15:34 UTC (permalink / raw)
To: netdev@vger.kernel.org
Cc: David Ahern, Ido Schimmel, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Johannes Weiner,
Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song,
Andrew Morton, Vladimir Davydov, cgroups@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
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
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH net 3/3] net: multicast: account group and source allocations to memcg
2026-11-19 15:34 [PATCH net 0/3] net: account socket and multicast allocations to memcg 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 ` [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg Yingjie Wang
@ 2026-11-19 15:34 ` Yingjie Wang
2026-11-23 16:24 ` netdev-bot+sashiko
2 siblings, 1 reply; 7+ messages in thread
From: Yingjie Wang @ 2026-11-19 15:34 UTC (permalink / raw)
To: netdev@vger.kernel.org
Cc: David Ahern, Ido Schimmel, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Johannes Weiner,
Michal Hocko, Roman Gushchin, Shakeel Butt, Muchun Song,
Andrew Morton, Vladimir Davydov, cgroups@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
IPv4 and IPv6 multicast source filter entries and their tomb records, as
well as live and deleted IPv6 device multicast group records, can
outlive the socket operation which creates them. These allocations do
not request memcg accounting, so socket operations can retain this
memory outside the caller's memory.max limit.
This was found while fuzzing socket option paths and reproduced after
entering a user and network namespace, then raising per-netns multicast
limits. With 1,200 memberships and 1,024 sources per membership, IPv4
retained about 85 MiB of slab while a cgroup with memory.max set to 64 MiB
reported less than 1 MiB in memory.current.
Account the IPv4 source and tomb allocations with __GFP_ACCOUNT while
preserving their GFP_ATOMIC constraint under the multicast spinlock.
GFP_ATOMIC includes __GFP_HIGH, so memcg force-charges these allocations
instead of failing them. Such charges can leave memory.current above
memory.max until the cgroup later performs a blocking charge or the
objects are freed.
Use GFP_KERNEL_ACCOUNT for the corresponding IPv6 source and tomb
allocations and for deleted IPv6 device group records, which are
allocated under a mutex. Pass GFP_KERNEL_ACCOUNT when a socket join
creates a live IPv6 device group; socket joins already propagate
allocation failure to the caller. Keep GFP_KERNEL for ipv6_dev_mc_inc()
callers such as address configuration, which may ignore allocation
failure.
For socket operations, the charge goes to the task that allocates a
shared group, source entry or tomb record, even if other sockets later
share it. A group created by address configuration stays uncharged when
a socket joins it.
In a QEMU guest with memory.max set to 64 MiB, normal IPv4 and IPv6
source operations succeeded. A 1,200-by-1,024 IPv4 source-filter run
was stopped by the memcg OOM killer. Its memory.peak reached about 82 MiB
against the 64 MiB limit, consistent with the forced atomic charges.
With delayed MLD reports and a 16 MiB limit on both kernels, the
unpatched kernel completed 200,000 IPv6 join/leave operations without
reaching the limit; with this change, the memcg OOM killer stopped the
run after at least 60,000 operations. 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 | 8 +++++---
net/ipv6/mcast.c | 25 ++++++++++++++-----------
2 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 5fe8736c7b47..ff2bb189dd64 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2055,7 +2055,8 @@ static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
#ifdef CONFIG_IP_MULTICAST
if (psf->sf_oldin &&
!IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
- struct ip_sf_list *dpsf = kmalloc_obj(*dpsf, GFP_ATOMIC);
+ struct ip_sf_list *dpsf = kmalloc_obj(*dpsf,
+ GFP_ATOMIC | __GFP_ACCOUNT);
if (dpsf) {
*dpsf = *psf;
@@ -2155,7 +2156,7 @@ static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
psf_prev = psf;
}
if (!psf) {
- psf = kzalloc_obj(*psf, GFP_ATOMIC);
+ psf = kzalloc_obj(*psf, GFP_ATOMIC | __GFP_ACCOUNT);
if (!psf)
return -ENOBUFS;
psf->sf_inaddr = *psfsrc;
@@ -2237,7 +2238,8 @@ static int sf_setstate(struct ip_mc_list *pmc)
break;
}
if (!dpsf) {
- dpsf = kmalloc_obj(*dpsf, GFP_ATOMIC);
+ dpsf = kmalloc_obj(*dpsf,
+ GFP_ATOMIC | __GFP_ACCOUNT);
if (!dpsf)
continue;
*dpsf = *psf;
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index ede1f0825bad..e7afe6187113 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -91,7 +91,8 @@ static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca,
static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml,
struct inet6_dev *idev);
static int __ipv6_dev_mc_inc(struct net_device *dev,
- const struct in6_addr *addr, unsigned int mode);
+ const struct in6_addr *addr, unsigned int mode,
+ gfp_t gfp);
#define MLD_QRV_DEFAULT 2
/* RFC3810, 9.2. Query Interval */
@@ -232,7 +233,7 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
RCU_INIT_POINTER(mc_lst->sflist, NULL);
/* now add/increase the group membership on the device */
- err = __ipv6_dev_mc_inc(dev, addr, mode);
+ err = __ipv6_dev_mc_inc(dev, addr, mode, GFP_KERNEL_ACCOUNT);
dev_put(dev);
@@ -770,7 +771,7 @@ static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
* for deleted items allows change reports to use common code with
* non-deleted or query-response MCA's.
*/
- pmc = kzalloc_obj(*pmc);
+ pmc = kzalloc_obj(*pmc, GFP_KERNEL_ACCOUNT);
if (!pmc)
return;
@@ -894,13 +895,13 @@ static void ma_put(struct ifmcaddr6 *mc)
static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
const struct in6_addr *addr,
- unsigned int mode)
+ unsigned int mode, gfp_t gfp)
{
struct ifmcaddr6 *mc;
mc_assert_locked(idev);
- mc = kzalloc_obj(*mc);
+ mc = kzalloc_obj(*mc, gfp);
if (!mc)
return NULL;
@@ -963,7 +964,8 @@ static void inet6_ifmcaddr_notify(struct net_device *dev,
* device multicast group inc (add if not found)
*/
static int __ipv6_dev_mc_inc(struct net_device *dev,
- const struct in6_addr *addr, unsigned int mode)
+ const struct in6_addr *addr, unsigned int mode,
+ gfp_t gfp)
{
struct inet6_dev *idev;
struct ifmcaddr6 *mc;
@@ -991,7 +993,7 @@ static int __ipv6_dev_mc_inc(struct net_device *dev,
}
}
- mc = mca_alloc(idev, addr, mode);
+ mc = mca_alloc(idev, addr, mode, gfp);
if (!mc) {
mutex_unlock(&idev->mc_lock);
in6_dev_put(idev);
@@ -1011,7 +1013,7 @@ static int __ipv6_dev_mc_inc(struct net_device *dev,
int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
{
- return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE);
+ return __ipv6_dev_mc_inc(dev, addr, MCAST_EXCLUDE, GFP_KERNEL);
}
EXPORT_SYMBOL(ipv6_dev_mc_inc);
@@ -2384,7 +2386,8 @@ static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode,
if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) &&
!mld_in_v1_mode(idev)) {
- struct ip6_sf_list *dpsf = kmalloc_obj(*dpsf);
+ struct ip6_sf_list *dpsf = kmalloc_obj(*dpsf,
+ GFP_KERNEL_ACCOUNT);
if (dpsf) {
*dpsf = *psf;
@@ -2469,7 +2472,7 @@ static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode,
psf_prev = psf;
}
if (!psf) {
- psf = kzalloc_obj(*psf);
+ psf = kzalloc_obj(*psf, GFP_KERNEL_ACCOUNT);
if (!psf)
return -ENOBUFS;
@@ -2554,7 +2557,7 @@ static int sf_setstate(struct ifmcaddr6 *pmc)
&psf->sf_addr))
break;
if (!dpsf) {
- dpsf = kmalloc_obj(*dpsf);
+ dpsf = kmalloc_obj(*dpsf, GFP_KERNEL_ACCOUNT);
if (!dpsf)
continue;
*dpsf = *psf;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH net 1/3] net: multicast: keep source filter state when a source update fails
2026-11-19 15:34 ` [PATCH net 1/3] net: multicast: keep source filter state when a source update fails Yingjie Wang
@ 2026-11-23 16:24 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 16:24 UTC (permalink / raw)
To: x-professor
Cc: netdev, dsahern, idosch, davem, edumazet, kuba, pabeni, horms,
hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
vdavydov.dev, cgroups, linux-mm, linux-kernel
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] ip_mc_source() (net/ipv4/igmp.c) and ip6_mc_source()
(net/ipv6/mcast.c) still switch the mode of an empty filter before…
- [Medium] The patch now returns every non-zero result of
ip_mc_add_src()/ip6_mc_add_src() from the add paths, not only…
- [Medium] The new `done:` rollback in ip6_mc_source()
(net/ipv6/mcast.c:504-508), and its IPv4 twin in ip_mc_source()…
--
net: multicast: keep source filter state when a source update fails
This patch restores the previous socket filter mode when ip_mc_source() or
ip6_mc_source() fail after switching the mode of an empty filter. It also
returns device-side errors from ip_mc_add_src()/ip6_mc_add_src() before
the socket filter is published. When the source update fails, it drops a
membership that the same MCAST_JOIN_SOURCE_GROUP or
IP_ADD_SOURCE_MEMBERSHIP call created.
> diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
> index 52cde8782eebd..bfa448028f8a1 100644
> --- a/net/ipv4/igmp.c
> +++ b/net/ipv4/igmp.c
[ ... ]
> @@ -2530,9 +2531,10 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
> }
> } else if (pmc->sfmode != omode) {
> /* allow mode switches for empty-set filters */
> + oldmode = pmc->sfmode;
> ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
> - ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
> - NULL, 0);
> + ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, oldmode, 0,
> + NULL, 0);
> pmc->sfmode = omode;
> }
>
[Severity: Medium]
The mode switch still happens before the request is validated. It is only
undone at done: if the request fails. While the switch is in effect, can
other code see the temporary INCLUDE{} state?
At the socket level, ip_mc_sf_allow() runs under rcu_read_lock() only and
does this:
psl = rcu_dereference(pmc->sflist);
ret = (pmc->sfmode == MCAST_EXCLUDE);
if (!psl)
goto unlock;
inet6_mc_check() follows the same pattern:
psl = rcu_dereference(mc->sflist);
if (!psl) {
rv = mc->sfmode == MCAST_EXCLUDE;
So group datagrams that arrive inside the window would be dropped for this
socket.
At the device level on IPv4, suppose this socket holds the only EXCLUDE
reference. The forward ip_mc_del_src() then drops sfcount[MCAST_EXCLUDE]
to 0 and switches im->sfmode to MCAST_INCLUDE. ip_check_mc_rcu() reads
this without a lock. Would traffic for the group be rejected for every
socket on the host until the rollback runs?
ip_mc_del_src() also calls igmp_ifc_event(), which arms mr_ifc_timer.
igmpv3_send_cr() only takes pmc->lock, not RTNL. If the timer fires inside
the window, it looks like it could send a CHANGE_TO_INCLUDE{} record,
which is a leave on the wire.
Even if the timer does not fire, the rollback is a second device-level
mode change. A call that changed nothing would still produce qrv
CHANGE_TO_EXCLUDE records, or MLD change reports on IPv6.
When !add and sflist == NULL, the call always ends here:
if (!psl)
goto done; /* err = -EADDRNOTAVAIL */
So that path never needs the switch, and it is always rolled back.
On the add path, the window includes a GFP_KERNEL sock_kmalloc(), which
can sleep in reclaim. Allocation failure under memory pressure is the
failure case this commit is fixing.
The commit message says "the EADDRNOTAVAIL case keeps the EXCLUDE filter
and the group is still received". That does not hold during the window.
Could the code fail early when !add && !sflist? On the add path, could
the allocation and validation run before the mode switch, so the window
goes away rather than just getting shorter? The same applies to
ip6_mc_source().
[ ... ]
> @@ -2585,10 +2596,30 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
[ ... ]
> + /* update the interface list */
> + err = ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
> + &mreqs->imr_sourceaddr, 1);
> + if (err) {
> + if (newpsl)
> + sock_kfree_s(sk, newpsl,
> + struct_size(newpsl, sl_addr,
> + newpsl->sl_max));
> + goto done;
> + }
[Severity: Medium]
This now returns any non-zero result from ip_mc_add_src(). The matching
change in ip6_mc_source() does the same for ip6_mc_add_src(). Both
helpers also return -ESRCH when the device has no entry for the group:
if (!pmc) {
/* MCA not found?? bug */
rcu_read_unlock();
return -ESRCH;
}
Can setsockopt() now fail with ESRCH in cases that used to return 0? That
would affect IP_BLOCK_SOURCE, IP_ADD_SOURCE_MEMBERSHIP, MCAST_BLOCK_SOURCE
and MCAST_JOIN_SOURCE_GROUP.
This looks reachable in a few ways.
On IPv6, without privilege: a socket joins G on interface A with an
explicit ifindex. It then calls MCAST_BLOCK_SOURCE with gsr_interface ==
0. The idev comes from ip6_mc_find_dev()->rt6_lookup() and can be
interface B. The pmc loop in ip6_mc_source() still matches the A
membership, because the ifindex check is skipped. ip6_mc_add_src(B, ...)
then returns -ESRCH.
Before this patch, the socket-level filter in that case was committed and
still enforced by inet6_mc_check(), which matches by address only. The
call returned 0. Now the call fails and the filter is not applied.
On IPv4: lowering the MTU below the minimum and raising it again goes
through inetdev_event()->inetdev_destroy() and a later inetdev_init().
The socket memberships survive, but the new in_device has no group entry.
Source operations would then return ESRCH.
Also on IPv4: __ip_mc_join_group() ignores the result of
____ip_mc_inc_group(). If that allocation fails, the membership has no
device entry. A new IP_ADD_SOURCE_MEMBERSHIP or MCAST_JOIN_SOURCE_GROUP
would then drop the membership and return ESRCH, not ENOBUFS.
The commit message says:
Return device-side allocation errors before publishing the socket filter.
Should only allocation errors (-ENOBUFS) be returned here, with the
-ESRCH cases handled separately?
[ ... ]
> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index ecef55f261890..0d982f7d29cda 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c
[ ... ]
> @@ -403,8 +404,9 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
> }
> } else if (pmc->sfmode != omode) {
> /* allow mode switches for empty-set filters */
> + oldmode = pmc->sfmode;
> ip6_mc_add_src(idev, group, omode, 0, NULL, 0);
> - ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0);
> + ip6_mc_del_src(idev, group, oldmode, 0, NULL, 0);
> pmc->sfmode = omode;
> }
>
[ ... ]
> @@ -494,6 +501,11 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
> kfree_rcu(psl, rcu);
> err = 0;
> done:
> + if (err && oldmode >= 0) {
> + ip6_mc_add_src(idev, group, oldmode, 0, NULL, 0);
> + ip6_mc_del_src(idev, group, omode, 0, NULL, 0);
> + pmc->sfmode = oldmode;
> + }
[Severity: Medium]
This rollback undoes the forward mode-switch calls without checking
whether they succeeded. The forward call
ip6_mc_del_src(idev, group, oldmode, 0, NULL, 0);
returns -EINVAL and changes nothing when mca_sfcount[oldmode] is 0:
if (!delta) {
if (!pmc->mca_sfcount[sfmode])
return -EINVAL;
pmc->mca_sfcount[sfmode]--;
}
The rollback's ip6_mc_add_src(idev, group, oldmode, 0, NULL, 0) still
increments mca_sfcount[oldmode] every time. Can this leave a phantom
reference on another socket's device entry?
This looks reachable without privilege, through an existing mismatch in
ip6_mc_source(). With gsr_interface == 0, the pmc loop matches any
membership for the group, while idev comes from a route lookup. Nothing
checks that idev->dev->ifindex == pmc->ifindex. For example:
1. Socket S2 joins G on device B with MCAST_JOIN_SOURCE_GROUP. B now has
mca_sfcount[MCAST_INCLUDE] = 1, mca_sfcount[MCAST_EXCLUDE] = 0, and
mode INCLUDE.
2. Socket S1 joins G on device A with IPV6_JOIN_GROUP (EXCLUDE{}). The
route for G points to B.
3. S1 calls MCAST_LEAVE_SOURCE_GROUP with gsr_interface = 0 for a source
it never added.
4. The forward ops run on B. The INCLUDE add raises the INCLUDE count to
2. The EXCLUDE del fails with -EINVAL. The call then fails with
-EADDRNOTAVAIL.
5. The rollback runs on B. The EXCLUDE add sets the EXCLUDE count to 1 and
switches B to EXCLUDE. The INCLUDE del lowers the INCLUDE count to 1.
B would be left in EXCLUDE mode with a phantom EXCLUDE count. MLD would
report EXCLUDE{} for G on B, and device-level filtering on B would accept
all sources.
S1's later leave works on device A, because __ipv6_sock_mc_drop() uses
mc_lst->ifindex. So the phantom count would remain as long as B's group
entry exists.
Before this patch, the same sequence only left a phantom INCLUDE count
on B.
The IPv4 rollback in ip_mc_source() has the same unchecked pattern. It can
be hit when a socket membership has no device entry of its own, for
example after an MTU drop and restore or a failed ____ip_mc_inc_group().
Another socket then has to create the entry later.
This also seems to conflict with the commit message's claim that
failures "preserve the previous filter and membership state".
Could the rollback undo only the forward operations that actually
succeeded?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795102467.git.x-professor%40outlook.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg
2026-11-19 15:34 ` [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg Yingjie Wang
@ 2026-11-23 16:24 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 16:24 UTC (permalink / raw)
To: x-professor
Cc: netdev, dsahern, idosch, davem, edumazet, kuba, pabeni, horms,
hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
vdavydov.dev, cgroups, linux-mm, linux-kernel
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The patch charges the IPV6_2292PKTOPTIONS buffer to memcg only
where setsockopt() first allocates it (do_ipv6_setsockopt(),…
- [Medium] This patch charges the per-socket multicast records but not
the per-device objects that the same join and source operations create.
- [Low] In the source-removal (!add) branch of ip6_mc_source()
(net/ipv6/mcast.c:437 in this commit), the code allocates a smaller…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the commit
message describes it as a general sock_kmalloc() issue: 'Allocations…
--
net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg
This commit changes several long-lived socket allocations to
GFP_KERNEL_ACCOUNT so they are charged to the memory cgroup. The affected
allocations are the IPV6_2292PKTOPTIONS cmsg buffer, the IPv4 and IPv6
multicast source lists, and the IPv4 and IPv6 multicast membership
records. sock_kmalloc() itself is left unchanged because some of its
callers run in softirq context.
> 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.
[ ... ]
> 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.
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. The
commit message describes this as a general sock_kmalloc() problem, but the
reason given for leaving sock_kmalloc() alone only covers softirq callers.
Several process-context sock_kmalloc() sites also keep allocations for the
life of the socket. They are uncharged and limited only by optmem_max:
net/ipv4/tcp_ipv4.c:__tcp_md5_do_add() (TCP_MD5SIG, no capability check):
key = sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO);
net/ipv4/tcp_ao.c:tcp_ao_key_alloc() (called from tcp_ao_add_cmd()):
key = sock_kmalloc(sk, size, GFP_KERNEL);
net/ipv6/anycast.c:ipv6_sock_ac_join() (needs ns_capable(CAP_NET_ADMIN),
which a user namespace provides):
pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
net/ipv6/exthdrs.c:ipv6_renew_options() (unprivileged via IPV6_RTHDR, also
HOPOPTS/DSTOPTS; smaller, since each header is at most 8*255 bytes):
opt2 = sk ? sock_kmalloc(sk, tot_len, GFP_ATOMIC) :
kmalloc(tot_len, GFP_ATOMIC);
The anycast record is the twin of the IPv6 multicast membership record that
this patch converts in __ipv6_sock_mc_join().
The TCP_MD5SIG keys alone let an unprivileged TCP socket hold close to
optmem_max (128 KiB by default). That is the same per-socket size as the
IPV6_2292PKTOPTIONS reproducer.
Should these sites be converted too? If not, could the commit message say
why they stay uncharged?
> diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
> index bfa448028f8a1..5fe8736c7b472 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;
>
[Severity: Medium]
The socket record is charged here. What about the per-device group record
that __ip_mc_join_group() creates a few lines later?
rcu_assign_pointer(inet->mc_list, iml);
____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
____ip_mc_inc_group() allocates struct ip_mc_list with:
im = kzalloc_obj(*im, gfp);
On leave, __ip_mc_dec_group()->igmpv3_add_delrec() creates an IGMPv3 tomb
with:
pmc = kzalloc_obj(*pmc, gfp);
Neither allocation uses __GFP_ACCOUNT.
The caller picks the group address, so each charged ip_mc_socklist can pin
a separate, larger, uncharged ip_mc_list (timer, lock, refcounts).
In a user+net namespace with igmp_max_memberships raised, a socket could
hold its optmem_max budget in charged records plus several times that in
uncharged ip_mc_list objects. Join/leave churn on an IGMPv3 device also
builds up uncharged tombs.
A later patch in this series, "net: multicast: account group and source
allocations to memcg", charges the IPv6 versions: mca_alloc() for
ifmcaddr6, plus the ip6_sf_list entries and tombs. It also charges the IPv4
ip_sf_list entries and tombs. At the end of the series, though, the IPv4
ip_mc_list and its igmpv3_add_delrec() tombs still seem to be uncharged.
____ip_mc_inc_group() returns void, and __ip_mc_join_group() ignores its
allocation failure, which may be why it was left out. Could one of the
commit messages say so, or should these allocations be charged as well?
[ ... ]
> diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
> index 82573260b7409..89394f8d963f6 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);
[Severity: High]
This charges the buffer where setsockopt() first allocates it. Can the same
payload still be copied without any charge?
IPV6_2292PKTOPTIONS can be set on an IPv6 TCP listener; the getsockopt()
side is even limited to SOCK_STREAM. Each child that completes the
handshake then copies the listener's options:
net/ipv6/tcp_ipv6.c:tcp_v6_syn_recv_sock() {
...
opt = ireq->ipv6_opt;
if (!opt)
opt = rcu_dereference(np->opt);
if (opt) {
opt = ipv6_dup_options(newsk, opt);
RCU_INIT_POINTER(newnp->opt, opt);
...
}
ipv6_dup_options() copies the full tot_len:
net/ipv6/exthdrs.c:ipv6_dup_options() {
...
opt2 = sock_kmemdup(sk, opt, opt->tot_len, GFP_ATOMIC);
...
}
That copy uses GFP_ATOMIC with no __GFP_ACCOUNT. It also runs in softirq,
where current_obj_cgroup() would not pick the user's memcg anyway.
sk_clone_lock() also resets the child's budget:
atomic_set(&newsk->sk_omem_alloc, 0);
so every child passes its own optmem_max check.
With one charged listener and N connections, does this still keep N copies
of up to ~64 KiB each (in ~128 KiB kmalloc_large allocations) outside
memory.max? The copies are held whether the children sit in the accept
queue (up to the backlog) or have been accepted.
MPTCP (net/mptcp/protocol.c) and SCTP (net/sctp/ipv6.c) make the same
uncharged copy.
The copy runs in softirq, so __GFP_ACCOUNT at that call would not charge
the right memcg. Would closing this need a charge against the listener's
memcg instead?
> retv = -ENOBUFS;
> if (!opt)
> break;
> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index 0d982f7d29cda..ede1f0825bad5 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c
[ ... ]
> @@ -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);
[Severity: Low]
This is the source-removal (!add) branch of ip6_mc_source(). It is reached
through MCAST_LEAVE_SOURCE_GROUP or MCAST_UNBLOCK_SOURCE when sl_count > 1.
With the charge added here, can an operation that shrinks the socket's
memory now fail?
If the cgroup is at memory.max, the charge can enter memcg reclaim or the
memcg OOM killer. If that cannot make progress, or the task is dying or is
already the OOM victim, sock_kmalloc() returns NULL. The source then stays
in the filter and setsockopt() returns -ENOBUFS. The error path does keep
the filter state consistent.
The IPv4 counterpart, ip_mc_source(), removes the source in place without
allocating:
for (j = i+1; j < psl->sl_count; j++)
psl->sl_addr[j-1] = psl->sl_addr[j];
psl->sl_count--;
Could this allocation stay uncharged? Alternatively, psl could be shrunk in
place the way IPv4 does it.
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795102467.git.x-professor%40outlook.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH net 3/3] net: multicast: account group and source allocations to memcg
2026-11-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations " Yingjie Wang
@ 2026-11-23 16:24 ` netdev-bot+sashiko
0 siblings, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 16:24 UTC (permalink / raw)
To: x-professor
Cc: netdev, dsahern, idosch, davem, edumazet, kuba, pabeni, horms,
hannes, mhocko, roman.gushchin, shakeel.butt, muchun.song, akpm,
vdavydov.dev, cgroups, linux-mm, linux-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix leaves the IPv4 device group records uncharged, even
though their IPv6 counterparts are now charged.
- [Medium] Several IPv6 allocations whose failure is always silently
ignored now use GFP_KERNEL_ACCOUNT, so they can fail on memcg charge…
--
net: multicast: account group and source allocations to memcg
This commit adds memcg accounting to the IPv4 and IPv6 multicast source
filter entries and their tomb records. It also accounts the IPv6 live and
deleted device group records. IPv4 keeps GFP_ATOMIC and adds __GFP_ACCOUNT,
IPv6 switches to GFP_KERNEL_ACCOUNT, and a gfp parameter is passed through
__ipv6_dev_mc_inc() and mca_alloc() so that only socket joins charge the
live IPv6 group.
> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index ede1f0825bad5..e7afe61871132 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c
[ ... ]
> @@ -232,7 +233,7 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
> RCU_INIT_POINTER(mc_lst->sflist, NULL);
>
> /* now add/increase the group membership on the device */
> - err = __ipv6_dev_mc_inc(dev, addr, mode);
> + err = __ipv6_dev_mc_inc(dev, addr, mode, GFP_KERNEL_ACCOUNT);
>
> dev_put(dev);
>
[Severity: High]
The IPv6 socket join now charges the live ifmcaddr6 group record. What
about the IPv4 equivalents?
__ip_mc_join_group() in net/ipv4/igmp.c still creates the live ip_mc_list
without accounting:
net/ipv4/igmp.c:__ip_mc_join_group() {
...
rcu_assign_pointer(inet->mc_list, iml);
____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
...
}
____ip_mc_inc_group() then allocates the record with
kzalloc_obj(*im, gfp).
The IPv4 deleted group record is also left uncharged. On leave or close
the chain is:
ip_mc_leave_group() / ip_mc_drop_socket()
ip_mc_dec_group()
igmp_group_dropped()
__igmp_group_dropped(im, GFP_KERNEL)
igmpv3_add_delrec(in_dev, im, gfp)
pmc = kzalloc_obj(*pmc, gfp);
Its IPv6 counterpart, mld_add_delrec(), is switched to GFP_KERNEL_ACCOUNT
by this patch.
These tomb records stay on in_dev->mc_tomb until crcount counts down
through IGMPv3 interface-change reports. crcount comes from in_dev->mr_qrv
or the per-netns igmp_qrv sysctl. igmp_max_memberships does not limit
these records.
Could a user namespace root in its own netns raise igmp_qrv and the report
interval, and then join and leave distinct groups in a loop? That would
pile up uncharged ip_mc_list records in the same way as the delayed MLD
join/leave reproducer in the commit message, which this patch fixes only
for IPv6.
The commit message gives this reason for charging the IPv6 join:
"socket joins already propagate allocation failure to the caller"
That is not true for IPv4, since ____ip_mc_inc_group() returns void. It
might explain leaving out the IPv4 live group. It does not explain leaving
out the deleted record, because igmpv3_add_delrec() already ignores
allocation failure the same way mld_add_delrec() does.
Should the IPv4 group records be charged too? For example, an accounted
gfp could be passed to igmpv3_add_delrec() on socket paths, and the IPv4
join could either propagate the failure or force the charge. If they are
left out on purpose, could the commit message say why?
> @@ -770,7 +771,7 @@ static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im)
> * for deleted items allows change reports to use common code with
> * non-deleted or query-response MCA's.
> */
> - pmc = kzalloc_obj(*pmc);
> + pmc = kzalloc_obj(*pmc, GFP_KERNEL_ACCOUNT);
> if (!pmc)
> return;
>
[Severity: Medium]
With GFP_KERNEL_ACCOUNT, can this allocation now fail when the memcg
charge fails?
For requests without __GFP_HIGH or __GFP_NOFAIL, try_charge_memcg()
returns -ENOMEM in several cases:
- the current task is an OOM victim with MMF_OOM_SKIP
- passed_oom && task_is_dying()
- mem_cgroup_oom() makes no progress
The relevant check is:
mm/memcontrol.c:try_charge_memcg() {
...
if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) {
ret = -ENOMEM;
goto out;
}
...
}
mld_add_delrec() returns void and just returns when the allocation fails.
In that case no deleted group record is created, and the MLDv2 leave
state-change report is never sent.
A typical case is an OOM-killed task in a memcg that exits and closes its
multicast sockets:
ipv6_sock_mc_close() / __ipv6_sock_mc_drop()
__ipv6_dev_mc_dec()
igmp6_group_dropped()
igmp6_leave_group()
mld_add_delrec()
kzalloc_obj(*pmc, GFP_KERNEL_ACCOUNT) returns NULL
The tomb allocations this patch changes in ip6_mc_del1_src() and
sf_setstate() look similar.
In ip6_mc_del1_src(), when dpsf is NULL, psf is still unlinked and freed
with rv = 0, so the source removal is never announced.
In sf_setstate(), this check drops the record:
if (!dpsf)
continue;
The IPv4 tomb conversions avoid this, because GFP_ATOMIC includes
__GFP_HIGH and the charge is forced.
mld_add_delrec() also hardcodes GFP_KERNEL_ACCOUNT, but non-socket callers
reach it through igmp6_group_dropped(). Examples are __ipv6_dev_mc_dec()
called from addrconf, ndisc and the bridge, as well as ipv6_mc_down() and
ipv6_mc_unmap(). On those paths, isn't the deleted record charged to
whichever task happens to trigger the operation?
That seems to conflict with the commit message, which keeps GFP_KERNEL
for:
"ipv6_dev_mc_inc() callers such as address configuration, which may
ignore allocation failure"
It also says:
"A group created by address configuration stays uncharged"
Would it make sense for callers to pass a gfp into mld_add_delrec(), as
igmpv3_add_delrec() already takes one on the IPv4 side?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795102467.git.x-professor%40outlook.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-23 16:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 15:34 [PATCH net 0/3] net: account socket and multicast allocations to memcg 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-23 16:24 ` netdev-bot+sashiko
2026-11-19 15:34 ` [PATCH net 2/3] net: account IPV6_2292PKTOPTIONS and multicast socket state to memcg Yingjie Wang
2026-11-23 16:24 ` netdev-bot+sashiko
2026-11-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations " Yingjie Wang
2026-11-23 16:24 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox