Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2630146ybv; Mon, 24 Feb 2020 08:40:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxdFIFYwRYuKQY/Kps1MaVHVwhT+pTCxgyYiTrVzls5ug+VmrYEv64lM0TFRxFmkhTJsvVJ X-Received: by 2002:a9d:aea:: with SMTP id 97mr40526582otq.51.1582562429891; Mon, 24 Feb 2020 08:40:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582562429; cv=none; d=google.com; s=arc-20160816; b=mLWJRNmj5N1o8PzvBywqSaLM2UQvzyh7UyoLWzmuq6OQ+LsCf053xMj0oqpj2uYTuF 3fBH6dzkQq3o8dzrC91IBoNjMWnO7SxUXQoSFomiNEP7pzt95eEcSYS03Ai0ZTYbWtwB 6/cnL1EYW9qMDU/u73JM6UxpXT7X665M4CeyImxCIzbT5gGG7uzr6v0jfP8XW0UpfnVO j0BsxUq9fu+edH2ZJwbczz6ak5x1LMCWESxLAk8VosjjVSe3h0m22OcyTR08XwP13aPG GJMhaxhxZXlREuRD6gLhz2VqHmJL7qJzkWb7ukhv8wZHd0z453bmnf0lqj/QaacCYNII Ym1A== 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=5gO1Y0WzjkHxYIBlGhH4yAQ+tmF9LMDtnMgPaRxoyaM=; b=pCfQJeRUFq1LrvVd2Sl02ZFE7IcV1GJYPfiB10MHfhgOx9nsGe/BssBCLVoYMnZTn5 WfZ8+p02mL6YjLXMYeULVhUiB5FEOC992B4JCnhpYJq6K3vJ4X/vN7YstRBLVy45DGbk QPkD+96mkd8+rFsGLQH/I54SoLoiBYUySf8VtdpAgQOoowtGOnrDmbq0AmQFTesA5rGc ZrgFR67aGDQVwQwLcfo4j5TZo++BkNja1iF/bLfIqbuvyDK3AoPNpp8GcfSQt7/eS509 vKPLEAeR3Mn1/i7pB8Kau35J2ag8x1peoK9GVkhUWGYJvIR4Mb7LkvFRHeOn2IbrcM4W Ea/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cj0x+qTl; 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 z24si6066814otm.135.2020.02.24.08.40.15; Mon, 24 Feb 2020 08:40:29 -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=cj0x+qTl; 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 S1728042AbgBXQir (ORCPT + 99 others); Mon, 24 Feb 2020 11:38:47 -0500 Received: from mail-ot1-f65.google.com ([209.85.210.65]:45122 "EHLO mail-ot1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727796AbgBXQir (ORCPT ); Mon, 24 Feb 2020 11:38:47 -0500 Received: by mail-ot1-f65.google.com with SMTP id 59so9244786otp.12 for ; Mon, 24 Feb 2020 08:38:47 -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=5gO1Y0WzjkHxYIBlGhH4yAQ+tmF9LMDtnMgPaRxoyaM=; b=cj0x+qTlOH8bogprsI1KxF0PwXK+DJOMYjbq8x5Xr0Peoeo3LPolVoXCH2d+1bFHyn cJGd+9woHFNvbMXIOehX7Uzt631Xj8BTQdWcqeMXoFhgFbKjRr7mJfKxZJ9B9dypEoi+ 0a/c7ukYfIWAmmQ2dGInzkXyM3vzdfJmtfUzx21/4NbAICg5fQ5csiFBDWljYeEiu/Kc ks5XUrla6niH4fjrg/j57lJHfVnvBdGq1Hccd9QyueUwpBWZdCtJgXSm2A8RnugrhcWq A//qAoh7mL2CDYzh7DZXY86nzBSrghF1gLfSmibRpfRYSp4ADTbJUFptp8GZpd6JsWt5 clyA== 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=5gO1Y0WzjkHxYIBlGhH4yAQ+tmF9LMDtnMgPaRxoyaM=; b=jHh4qn5TiQGavX6Y87NyoQWBhk2BFoX85Kw/a9tkWOsLUz4njlybVIdt5Cm7NEDKsL +MWPaFPPKeUogsB7JPWhy6Ehax7dHHjhp2UK5SHf+st1mnBOzdo0T1qVxItQLA+eCPxQ g1yXVpGFn8vodnHwR+f91+4TTm4cP+V0QyDuBmvB8V8lEHiClBmZkTY9k1fqpzdRd3DN DMXCa94wF7OR/KaDLPZYQlJAw2N1D+3Xh9LBvHun2x/YlgCA6xB0iwFvc9t6inSdOg6m Kw7uVHUFU4BX8FVDz+hhz62ty6Exj1y9OvpzOEwvWj/sbDhPN/6IpqUl/HPchH4Gg7eO E/rQ== X-Gm-Message-State: APjAAAUFjC+NMU2RZJETmd3Y9K0R14SG341RzbBZgtM2zrngB64mFVq0 MHmQNdmtuxdIOZVQS/4V7kurcvdmWZ9X2n8rjbsw9w== X-Received: by 2002:a9d:6ac2:: with SMTP id m2mr41277314otq.191.1582562326606; Mon, 24 Feb 2020 08:38:46 -0800 (PST) MIME-Version: 1.0 References: <20200222010456.40635-1-shakeelb@google.com> In-Reply-To: From: Shakeel Butt Date: Mon, 24 Feb 2020 08:38:35 -0800 Message-ID: Subject: Re: [PATCH] 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 , netdev , Hideaki YOSHIFUJI , 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 Sun, Feb 23, 2020 at 11:29 PM Eric Dumazet wrote: > > On Fri, Feb 21, 2020 at 5:05 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 suck 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 > > --- > > net/ipv4/inet_connection_sock.c | 7 +++++++ > > 1 file changed, 7 insertions(+) > > > > diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c > > index a4db79b1b643..df9c8ef024a2 100644 > > --- a/net/ipv4/inet_connection_sock.c > > +++ b/net/ipv4/inet_connection_sock.c > > @@ -482,6 +482,13 @@ 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) { > > + mem_cgroup_sk_alloc(newsk); > > + if (newsk->sk_memcg) > > + mem_cgroup_charge_skmem(newsk->sk_memcg, > > + sk_mem_pages(newsk->sk_forward_alloc)); > > I am not sure what you are trying to do here. > > sk->sk_forward_alloc is not the total amount of memory used by a TCP socket. > It is only some part that has been reserved, but not yet consumed. > > For example, every skb that has been stored in TCP receive queue or > out-of-order queue might have > used memory. > > I guess that if we assume that a not yet accepted socket can not have > any outstanding data in its transmit queue, > you need to use sk->sk_rmem_alloc as well. Thanks a lot. I will add that with a comment. BTW for my knowledge which field represents the transmit queue size? > > To test this patch, make sure to add a delay before accept(), so that > 2MB worth of data can be queued before accept() happens. Yes, I will test this with a delay. thanks, Shakeel