Thread View: linux.kernel.netdev
5 messages
5 total messages
Started by Yingjie Wang
Thu, 19 Nov 2026 15:34
[PATCH net 0/3] net: account socket and multicast allocations to memcg
Author: Yingjie Wang
Date: Thu, 19 Nov 2026 15:34
Date: Thu, 19 Nov 2026 15:34
74 lines
24365 bytes
24365 bytes
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
[PATCH net 1/3] net: multicast: keep source filter state when a source update fails
Author: Yingjie Wang
Date: Thu, 19 Nov 2026 15:34
Date: Thu, 19 Nov 2026 15:34
206 lines
34580 bytes
34580 bytes
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
[PATCH net 3/3] net: multicast: account group and source allocations to memcg
Author: Yingjie Wang
Date: Thu, 19 Nov 2026 15:34
Date: Thu, 19 Nov 2026 15:34
128 lines
28674 bytes
28674 bytes
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
Re: [PATCH net 1/3] net: multicast: keep source filter state when a source update fails
Author: netdev-bot+sashiko
Date: Mon, 23 Nov 2026 16:24
Date: Mon, 23 Nov 2026 16:24
270 lines
18004 bytes
18004 bytes
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
Re: [PATCH net 3/3] net: multicast: account group and source allocations to memcg
Author: netdev-bot+sashiko
Date: Mon, 23 Nov 2026 16:24
Date: Mon, 23 Nov 2026 16:24
181 lines
14415 bytes
14415 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads