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: # changes memcg charging; not stable material Signed-off-by: Yingjie Wang --- 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