mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 3/3] net: multicast: account group and source allocations to memcg
Date: Thu, 19 Nov 2026 15:34:40 +0000	[thread overview]
Message-ID: <eabf67fe94dc239f6cc59720e8468f2bf9b4cd62.1795102467.git.x-professor@outlook.com> (raw)
In-Reply-To: <cover.1795102467.git.x-professor@outlook.com>

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


      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 " 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 [this message]

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=eabf67fe94dc239f6cc59720e8468f2bf9b4cd62.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®