Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp5259067ybf; Wed, 4 Mar 2020 20:55:19 -0800 (PST) X-Google-Smtp-Source: ADFU+vvNyNilXQ144+4uunPkzaRict/T1P2RvtE1Uj8LAJXKOae4SwXWxep44KpQ7x/2aJVKhnZT X-Received: by 2002:a05:6830:1606:: with SMTP id g6mr5428954otr.120.1583384118925; Wed, 04 Mar 2020 20:55:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583384118; cv=none; d=google.com; s=arc-20160816; b=wHO2MgZG0q4YpfEl7g/sb5EkoIQK/db5T9fw+0csdkHjNWD/FlM2ZG+SlKxsJQ704N 5eFgl4qW/2U4SdIY6yJgFaVS2NUys4gyp7+Vno2+BZ5fXLjrFNiaq2xhBf4Oqi7FlabH ghAmVThW36MrzzMlIcKqyqTGszIl5lTGYM+oryff9dxT8imB0TXJDTXBPyL0woJzslhs hnvR1uM02IUoLOfFTatoaYs5HbakX+c62bMfUvYCZa4q2AdI7BmnpnJfyeQWz9I+no0D o8V6IX2199XXQhb3F06qExBgTllyNIKiAHja/jCFTzqOm2/1mON+qku4G/7lcRjENrEF S8og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=wxMB2pp+9PjI13jJjWt8ByuXtWLK+X9MK3SPC71Gnu4=; b=wqLtzJM8cucn5CfYdANAqt4KuqgZsULsuXDJGCK1Q4IJknZqgNKUSukE0Ctk3HgR1r CpzHnyYrq4fTxL3nb+GK4zDjiuV5y+McqAnrg2XpCkLVroNxu0AErfzTxIj88Ao7deA2 GdE1mg5r44McUUrBfbZV/L8hTVzlmnfu/iVzuf/NT4wMOH5MNDsv1yfTn/PuHkNvpFJC aCOaXbaOAKXJo6NNaNSbz6mO+wp686SFYbN6y59vwxvEcKicOxmajpbRLL6rwsLjVAD1 Nj2ZSvCE1NimJMqZtp76J4EbwVy25wO6QuyikxfqydI+Y7REw+/qrWdg9LgsW0x3glMP hsyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="tJ/6smOa"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m7si2758109oie.138.2020.03.04.20.55.07; Wed, 04 Mar 2020 20:55:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="tJ/6smOa"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726090AbgCEEyr (ORCPT + 99 others); Wed, 4 Mar 2020 23:54:47 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:47028 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725937AbgCEEyq (ORCPT ); Wed, 4 Mar 2020 23:54:46 -0500 Received: by mail-ot1-f65.google.com with SMTP id g96so4433351otb.13 for ; Wed, 04 Mar 2020 20:54:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wxMB2pp+9PjI13jJjWt8ByuXtWLK+X9MK3SPC71Gnu4=; b=tJ/6smOavDVAgu3Gdv6UPcaz/ASw6W4rh6d3f/b7iBjY6Foxfv6i1G0OlFGnI0h645 wA0vSFhrAVZuAr2PJDr9E4WHV/usr5bRjaixTFpn8JjPp3QzkHVa+tncWWsSfQ9gGU/O 6OCZybxu/Ly8SyivT03y5Svx2ipVvnyrSkVDGOiGek+CA3OhEXmERwV4mxW5DXyzwWmt ODfNLKdcYqr5PNNI8tKVwTv4MkqptJjbpXUwR/rR/dN19neY/vhWLvyoY2YrNTsIPkvP nKghJJEO1ErZxQXjl2DWtHTDR4Lk8Iahf874jvpX4hFCMpZlNuHHd8+wXH5Jn3IHb92E JRsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wxMB2pp+9PjI13jJjWt8ByuXtWLK+X9MK3SPC71Gnu4=; b=JO4F6zYXge6ioS0N1Lj8K/67oMEFKYY/XEk0xM6ozQ4aogxdAAoKYKOL3fEG/DrqPS uin0tki6o7auQbcLa2z4AczulPilXQq1fVH9exHtpxh2vAzAbibXEEJOdzaOhKKwOKBL 3P5c1FUfLvh2zTlMW9t7Iy/Y3UlssW1Zul0McqsY0sguOVTLKNXdUDpIJMR8exRrZpDB +TEklyhnNI+QiBM37ANBpE/YuZwgC0hrAL4x7422oCIxXViU+kN3kkjRM6BVesaYWHxw zz41SD97Guephk0pp1up81EDB77fnOWl8T9aKu/gZ3ZN/zzIjkZ9dqj1zWEoica6bnao tObQ== X-Gm-Message-State: ANhLgQ0qCytqEEgNLihOXD6pV6iVgroZS0/F7KtPhjki6p974j3UeXtH FNEpRFvyqdEAqqtmAo//LjHsiEnXAgrx/gML7DynPw== X-Received: by 2002:a9d:6:: with SMTP id 6mr5250710ota.191.1583384085099; Wed, 04 Mar 2020 20:54:45 -0800 (PST) MIME-Version: 1.0 References: <20200304233856.257891-1-shakeelb@google.com> In-Reply-To: From: Shakeel Butt Date: Wed, 4 Mar 2020 20:54:34 -0800 Message-ID: Subject: Re: [PATCH v2] net: memcg: late association of sock to memcg To: Eric Dumazet Cc: Roman Gushchin , Johannes Weiner , Michal Hocko , Andrew Morton , "David S . Miller" , Alexey Kuznetsov , Hideaki YOSHIFUJI , netdev , linux-mm , Cgroups , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 4, 2020 at 8:38 PM Eric Dumazet wrote: > > On Wed, Mar 4, 2020 at 6:19 PM Shakeel Butt wrote: > > > > On Wed, Mar 4, 2020 at 5:36 PM Eric Dumazet wrote: > > > > > > On Wed, Mar 4, 2020 at 3:39 PM Shakeel Butt wrote: > > > > > > > > If a TCP socket is allocated in IRQ context or cloned from unassociated > > > > (i.e. not associated to a memcg) in IRQ context then it will remain > > > > unassociated for its whole life. Almost half of the TCPs created on the > > > > system are created in IRQ context, so, memory used by such sockets will > > > > not be accounted by the memcg. > > > > > > > > This issue is more widespread in cgroup v1 where network memory > > > > accounting is opt-in but it can happen in cgroup v2 if the source socket > > > > for the cloning was created in root memcg. > > > > > > > > To fix the issue, just do the late association of the unassociated > > > > sockets at accept() time in the process context and then force charge > > > > the memory buffer already reserved by the socket. > > > > > > > > Signed-off-by: Shakeel Butt > > > > --- > > > > Changes since v1: > > > > - added sk->sk_rmem_alloc to initial charging. > > > > - added synchronization to get memory usage and set sk_memcg race-free. > > > > > > > > net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++ > > > > 1 file changed, 19 insertions(+) > > > > > > > > diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c > > > > index a4db79b1b643..7bcd657cd45e 100644 > > > > --- a/net/ipv4/inet_connection_sock.c > > > > +++ b/net/ipv4/inet_connection_sock.c > > > > @@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) > > > > } > > > > spin_unlock_bh(&queue->fastopenq.lock); > > > > } > > > > + > > > > + if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) { > > > > + int amt; > > > > + > > > > + /* atomically get the memory usage and set sk->sk_memcg. */ > > > > + lock_sock(newsk); > > > > + > > > > + /* The sk has not been accepted yet, no need to look at > > > > + * sk->sk_wmem_queued. > > > > + */ > > > > + amt = sk_mem_pages(newsk->sk_forward_alloc + > > > > + atomic_read(&sk->sk_rmem_alloc)); > > > > + mem_cgroup_sk_alloc(newsk); > > > > + > > > > + release_sock(newsk); > > > > + > > > > + if (newsk->sk_memcg) > > > > > > Most sockets in accept queue should have amt == 0, so maybe avoid > > > calling this thing only when amt == 0 ? > > > > > > > Thanks, will do in the next version. BTW I have tested with adding > > mdelay() here and running iperf3 and I did see non-zero amt. > > > > > Also I would release_sock(newsk) after this, otherwise incoming > > > packets could mess with newsk->sk_forward_alloc > > > > > > > I think that is fine. Once sk->sk_memcg is set then > > mem_cgroup_charge_skmem() will be called for new incoming packets. > > Here we just need to call mem_cgroup_charge_skmem() with amt before > > sk->sk_memcg was set. > > > Unfortunately, as soon as release_sock(newsk) is done, incoming > packets can be fed to the socket, > and completely change memory usage of the socket. > > For example, the whole queue might have been zapped, or collapsed, if > we receive a RST packet, > or if memory pressure asks us to prune the out of order queue. > > So you might charge something, then never uncharge it, since at > close() time the socket will have zero bytes to uncharge. > Ok, thanks for the explanation. I will fix this in the next version.