Netdev List
 help / color / mirror / Atom feed
* [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; 4+ 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] 4+ 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-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, 0 replies; 4+ 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] 4+ 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-19 15:34 ` [PATCH net 3/3] net: multicast: account group and source allocations " Yingjie Wang
  2 siblings, 0 replies; 4+ 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] 4+ 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
  2 siblings, 0 replies; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2026-11-19 15:34 UTC | newest]

Thread overview: 4+ 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-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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox