Netdev List
 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>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	Binbin Deng <18983559317@163.com>,
	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>,
	"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-next v2 2/3] net: multicast: account socket memberships and source lists to memcg
Date: Sat, 5 Dec 2026 08:19:18 +0000	[thread overview]
Message-ID: <20261205081916.2230062-1-x-professor@outlook.com> (raw)
In-Reply-To: <20261205081856.2229881-1-x-professor@outlook.com>

The IPv4 and IPv6 multicast memberships of a socket and their source
filter lists are allocated with sock_kmalloc().  It limits them with
the socket's optmem_max budget, but does not charge them to the memory
cgroup.  IPv6 has no other limit on the number of memberships, so an
unprivileged task can keep up to optmem_max (128 KiB by default) of
them per socket outside its memory.max limit.

Use GFP_KERNEL_ACCOUNT for these allocations.

sock_kmalloc() itself is left alone: it also serves short-lived
buffers, such as control messages in sendmsg(), and allocations made
in softirq context.  Other long-lived sock_kmalloc() users, such as
TCP-MD5 and TCP-AO keys, IPv6 anycast memberships and IPv6 extension
header options, are not changed here.

The device records that the same joins create are handled in the next
patch, which also has the measurements.

Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 net/ipv4/igmp.c  | 6 +++---
 net/ipv6/mcast.c | 9 +++++----
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 446c49db1adf..1ed7192e7cd6 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -2388,7 +2388,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;
 
@@ -2587,7 +2587,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;
@@ -2691,7 +2691,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/mcast.c b/net/ipv6/mcast.c
index da456f006f85..a9f2e474fad0 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;
 
@@ -427,7 +428,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);
@@ -465,7 +466,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;
@@ -559,7 +560,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


  parent reply	other threads:[~2026-12-05  8:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  8:18 [PATCH net-next v2 0/3] net: multicast: account memberships and device records to memcg Yingjie Wang
2026-12-05  8:19 ` [PATCH net-next v2 1/3] net: multicast: keep source filter state when adding a source fails Yingjie Wang
2026-12-05  8:19 ` Yingjie Wang [this message]
2026-12-05  8:19 ` [PATCH net-next v2 3/3] net: multicast: account device records created by sockets to memcg Yingjie Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205081916.2230062-1-x-professor@outlook.com \
    --to=x-professor@outlook.com \
    --cc=18983559317@163.com \
    --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=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox