Multicast memberships, their source filters and the device records they create are not charged to memory cgroups, so a task can keep a lot of kernel memory outside its memory.max limit. In a QEMU guest, an unprivileged user with default sysctls joined 58,500 IPv6 groups from 25 sockets on one interface; slab grew by about 40 MiB while the user's cgroup was charged less than 2 MiB. Patch 1 makes adding a source to a socket filter fail without changing state. The allocation of the source list can already fail today, when the socket's optmem_max budget is used up, and then leaves the filter in INCLUDE mode with no sources. Patches 2 and 3 charge the socket and the device allocations. What a join still leaves uncharged is the link-layer address entry and the two copies of it that the rx mode work keeps; on the dummy device used here, that is about as much again as what is now charged. Changes in v2: - Rebased on net-next and sent there, without Fixes and stable tags. The EADDRNOTAVAIL fix from v1 patch 1 is already in net-next as commit 2f14be6c9b9e ("ipv4/ipv6: preserve multicast filters after a failed source leave"). - Patch 1: allocate the source list first and update the device in one call, instead of switching the mode up front and rolling it back; keep ignoring -ESRCH (Sashiko). - Patch 2: drop the IPV6_2292PKTOPTIONS change. The copies made for TCP children in softirq context would stay uncharged (Sashiko), so that needs a different fix. - Patch 3: also charge IPv4 group records and deleted group records, and pass the gfp flags to mld_add_delrec(), so that address configuration and link events stay uncharged (Sashiko). v1: https://lore.kernel.org/r/cove…@outlook.com Patch 1 overlaps with the pending "igmp: use copy-on-write RCU updates in ip_mc_source()" [1]; I can rebase on it if it goes in first. Tested in x86_64 QEMU guests on net-next d5cf954cebe4, with and without this series: - W=1 builds of the kernel, of the affected objects after each patch, and with allmodconfig, i386 and CONFIG_IP_MULTICAST=n; - the optmem_max case from patch 1, as uid 1000: 4/4 -> 0/4 group datagrams on the base, 4/4 -> 4/4 with the series; - slab fault injection on the source list and source entry allocations: the base reports success with the source missing from the device filter, the series returns ENOBUFS and keeps the filter and the membership; - MCAST_BLOCK_SOURCE with gsr_interface 0 while the route for the group uses a different device, normal IPv4 and IPv6 source join/leave sequences, and leaving a source that was never added behave the same on both kernels; so do the IGMPv3 and MLDv2 records sent when an any-source membership becomes INCLUDE {S}; - with memory.max at 16 MiB: the IPv6 joins above, IPv4 joins and IPv4 source filters in a user and network namespace, and IPv4 and IPv6 join/leave loops with a 10 minute report interval ran without a memory.max event on the base, and were stopped by the memcg OOM killer with the series. The join/leave loops kept up to about 14 (IPv4) and 17 (IPv6) MiB of deleted group records at a time; with IGMPv2 or MLDv1, which keep no such records, the same loops stayed below 3 MiB; - 200,000 join/leave cycles in a non-root memory cgroup, in IGMPv2 and MLDv1 mode to leave out deleted records, were not slower with the series (five runs each; a coarse timing that includes namespace setup). No warnings, BUG reports or splats in the guest consoles. Kernel selftests were not run. [1] https://lore.kernel.org/r/2026…@163.com Yingjie Wang (3): net: multicast: keep source filter state when adding a source fails net: multicast: account socket memberships and source lists to memcg net: multicast: account device records created by sockets to memcg include/net/addrconf.h | 3 +- net/ipv4/igmp.c | 120 +++++++++++++++++++++++---------------- net/ipv4/ip_sockglue.c | 33 ++++++----- net/ipv6/addrconf.c | 2 +- net/ipv6/ipv6_sockglue.c | 11 +++- net/ipv6/mcast.c | 97 ++++++++++++++++++------------- 6 files changed, 159 insertions(+), 107 deletions(-) base-commit: d5cf954cebe4f175dfd834e1dc471a3aa203ec7d
-- 2.43.0