Socket joins, source filters and leaves also create device records: IPv4 and IPv6 group records, their source entries, and records of deleted groups and sources, which are kept until the state change reports for them have been sent. None of them are charged to the memory cgroup. Every group a socket joins can pin a group record, and an unprivileged IPv6 socket can join thousands of groups. 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. About 19 MiB of that were group and membership records; most of the rest were link-layer address entries, see below. In a user and network namespace, loops that join and leave distinct IPv4 or IPv6 groups kept up to about 14 or 17 MiB of deleted group records at a time, also uncharged. Charge these records when a socket operation creates them: - IPv4: ____ip_mc_inc_group() returns an error now, so that a socket join fails with ENOBUFS when the group record cannot be allocated, instead of leaving a membership without a device entry. Socket leaves and closes pass GFP_KERNEL_ACCOUNT down to the deleted group record. Source entries and deleted source records keep GFP_ATOMIC, which they need under the group's spinlock, and add __GFP_ACCOUNT. GFP_ATOMIC includes __GFP_HIGH, so these charges are forced and can leave memory.current above memory.max for a while. - IPv6: mca_alloc() and mld_add_delrec() take the gfp flags from their callers, as igmpv3_add_delrec() does for IPv4. Socket joins and leaves pass GFP_KERNEL_ACCOUNT; address configuration, ndisc, the bridge and link events keep GFP_KERNEL. Source entries and deleted source records use GFP_KERNEL_ACCOUNT. Source entries only exist for socket source filters and are charged to the task that changes the filter. A record is charged to the task that creates it, even if other sockets share it later, and a group created by address configuration stays uncharged when a socket joins it. A deleted record that cannot be charged is not created, as already happens when its allocation fails, and its state change report is then not sent. Link-layer addresses added with dev_mc_add(), and the copies of them that the rx mode work keeps, are not charged. Signed-off-by: Yingjie Wang --- With memory.max set to 16 MiB, the IPv6 joins above, IPv4 joins and IPv4 source filters in a user and network namespace with raised multicast limits, and the join/leave loops ran on the base kernel without a memory.max event; with this change the memcg OOM killer stopped all of them. The IPv4 source filters reached a memory.peak of about 28 MiB because of the forced charges. include/net/addrconf.h | 3 ++- net/ipv4/igmp.c | 34 +++++++++++++++---------- net/ipv6/addrconf.c | 2 +- net/ipv6/mcast.c | 58 ++++++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 40 deletions(-) diff --git a/include/net/addrconf.h b/include/net/addrconf.h index 0ec4db881404..5be6f30e8877 100644 --- a/include/net/addrconf.h +++ b/include/net/addrconf.h @@ -273,7 +273,8 @@ bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr, const struct in6_addr *src_addr); int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr); -int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr); +int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr, + gfp_t gfp); int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr); void ipv6_mc_up(struct inet6_dev *idev); void ipv6_mc_down(struct inet6_dev *idev); diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c index 1ed7192e7cd6..eb48c940397a 100644 --- a/net/ipv4/igmp.c +++ b/net/ipv4/igmp.c @@ -1574,8 +1574,8 @@ static void inet_ifmcaddr_notify(struct net_device *dev, /* * A socket has joined a multicast group on device dev. */ -static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, - unsigned int mode, gfp_t gfp) +static int ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, + unsigned int mode, gfp_t gfp) { struct ip_mc_list __rcu **mc_hash; struct ip_mc_list *im; @@ -1602,12 +1602,12 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, if (im) { WRITE_ONCE(im->users, im->users + 1); ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0); - goto out; + return 0; } im = kzalloc_obj(*im, gfp); if (!im) - goto out; + return -ENOBUFS; WRITE_ONCE(im->users, 1); im->interface = in_dev; @@ -1637,8 +1637,7 @@ static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr, inet_ifmcaddr_notify(in_dev->dev, im, RTM_NEWMULTICAST); if (!in_dev->dead) ip_rt_multicast_event(in_dev); -out: - return; + return 0; } void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp) @@ -2062,7 +2061,9 @@ 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; + + dpsf = kmalloc_obj(*dpsf, GFP_ATOMIC | __GFP_ACCOUNT); if (dpsf) { *dpsf = *psf; @@ -2162,7 +2163,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; @@ -2244,7 +2245,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; @@ -2392,13 +2394,17 @@ static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr, if (!iml) goto done; + err = ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL_ACCOUNT); + if (err) { + sock_kfree_s(sk, iml, sizeof(*iml)); + goto done; + } + memcpy(&iml->multi, imr, sizeof(*imr)); iml->next_rcu = inet->mc_list; iml->sflist = NULL; iml->sfmode = mode; rcu_assign_pointer(inet->mc_list, iml); - ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL); - err = 0; done: return err; } @@ -2477,7 +2483,7 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) *imlp = iml->next_rcu; if (in_dev) - ip_mc_dec_group(in_dev, group); + __ip_mc_dec_group(in_dev, group, GFP_KERNEL_ACCOUNT); /* decrease mem now to avoid the memleak warning */ atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); @@ -2923,7 +2929,9 @@ void ip_mc_drop_socket(struct sock *sk) in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); (void) ip_mc_leave_src(sk, iml, in_dev); if (in_dev) - ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); + __ip_mc_dec_group(in_dev, + iml->multi.imr_multiaddr.s_addr, + GFP_KERNEL_ACCOUNT); /* decrease mem now to avoid the memleak warning */ atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); kfree_rcu(iml, rcu); diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c index 0c4f0a0ad451..ab06970229a9 100644 --- a/net/ipv6/addrconf.c +++ b/net/ipv6/addrconf.c @@ -2314,7 +2314,7 @@ void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr) return; addrconf_addr_solict_mult(addr, &maddr); - __ipv6_dev_mc_dec(idev, &maddr); + __ipv6_dev_mc_dec(idev, &maddr, GFP_KERNEL); } static void addrconf_join_anycast(struct inet6_ifaddr *ifp) diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c index a9f2e474fad0..be648c768ecb 100644 --- a/net/ipv6/mcast.c +++ b/net/ipv6/mcast.c @@ -74,7 +74,7 @@ static struct workqueue_struct *mld_wq; static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT; static void igmp6_join_group(struct ifmcaddr6 *ma); -static void igmp6_leave_group(struct ifmcaddr6 *ma); +static void igmp6_leave_group(struct ifmcaddr6 *ma, gfp_t gfp); static void mld_mca_work(struct work_struct *work); static void mld_ifc_event(struct inet6_dev *idev); @@ -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); @@ -275,7 +276,8 @@ static void __ipv6_sock_mc_drop(struct sock *sk, struct ipv6_mc_socklist *mc_lst ip6_mc_leave_src(sk, mc_lst, idev); if (idev) { - __ipv6_dev_mc_dec(idev, &mc_lst->addr); + __ipv6_dev_mc_dec(idev, &mc_lst->addr, + GFP_KERNEL_ACCOUNT); in6_dev_put(idev); } @@ -739,7 +741,7 @@ static void igmp6_group_added(struct ifmcaddr6 *mc) mld_ifc_event(mc->idev); } -static void igmp6_group_dropped(struct ifmcaddr6 *mc) +static void igmp6_group_dropped(struct ifmcaddr6 *mc, gfp_t gfp) { struct net_device *dev = mc->idev->dev; /* RTNL might not be held: dev->type and dev->addr_len can change. @@ -763,14 +765,15 @@ static void igmp6_group_dropped(struct ifmcaddr6 *mc) return; if (!mc->idev->dead) - igmp6_leave_group(mc); + igmp6_leave_group(mc, gfp); if (cancel_delayed_work(&mc->mca_work)) refcount_dec(&mc->mca_refcnt); } /* deleted ifmcaddr6 manipulation */ -static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im) +static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im, + gfp_t gfp) { struct ifmcaddr6 *pmc; @@ -782,7 +785,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); if (!pmc) return; @@ -906,13 +909,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; @@ -975,7 +978,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; @@ -1003,7 +1007,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); @@ -1023,14 +1027,15 @@ 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); /* * device multicast group del */ -int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr) +int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr, + gfp_t gfp) { struct ifmcaddr6 *ma, __rcu **map; @@ -1047,7 +1052,7 @@ int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr) rcu_assign_pointer(*map, mc_dereference(ma->next, idev)); - igmp6_group_dropped(ma); + igmp6_group_dropped(ma, gfp); inet6_ifmcaddr_notify(idev->dev, ma, RTM_DELMULTICAST); ip6_mc_clear_src(ma); @@ -1074,7 +1079,7 @@ int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr) if (!idev) return -ENODEV; - err = __ipv6_dev_mc_dec(idev, addr); + err = __ipv6_dev_mc_dec(idev, addr, GFP_KERNEL); in6_dev_put(idev); return err; @@ -2396,7 +2401,9 @@ 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; + + dpsf = kmalloc_obj(*dpsf, GFP_KERNEL_ACCOUNT); if (dpsf) { *dpsf = *psf; @@ -2481,7 +2488,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; @@ -2566,7 +2573,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; @@ -2725,7 +2732,7 @@ static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, return err; } -static void igmp6_leave_group(struct ifmcaddr6 *ma) +static void igmp6_leave_group(struct ifmcaddr6 *ma, gfp_t gfp) { mc_assert_locked(ma->idev); @@ -2735,7 +2742,7 @@ static void igmp6_leave_group(struct ifmcaddr6 *ma) ICMPV6_MGM_REDUCTION); } } else { - mld_add_delrec(ma->idev, ma); + mld_add_delrec(ma->idev, ma, gfp); mld_ifc_event(ma->idev); } } @@ -2811,7 +2818,7 @@ void ipv6_mc_unmap(struct inet6_dev *idev) mutex_lock(&idev->mc_lock); for_each_mc_mclock(idev, i) - igmp6_group_dropped(i); + igmp6_group_dropped(i, GFP_KERNEL); mutex_unlock(&idev->mc_lock); } @@ -2828,7 +2835,7 @@ void ipv6_mc_down(struct inet6_dev *idev) mutex_lock(&idev->mc_lock); /* Withdraw multicast list */ for_each_mc_mclock(idev, i) - igmp6_group_dropped(i); + igmp6_group_dropped(i, GFP_KERNEL); mutex_unlock(&idev->mc_lock); /* Should stop work after group drop. or we will @@ -2912,10 +2919,11 @@ void ipv6_mc_destroy_dev(struct inet6_dev *idev) * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will * fail. */ - __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes); + __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes, GFP_KERNEL); if (idev->cnf.forwarding) - __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters); + __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters, + GFP_KERNEL); mutex_lock(&idev->mc_lock); while ((i = mc_dereference(idev->mc_list, idev))) { -- 2.43.0