Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2133401ybv; Sun, 23 Feb 2020 23:29:40 -0800 (PST) X-Google-Smtp-Source: APXvYqxp8NiBtW+yLav6Ca7FsLZseNeW1Kg+t2vLOgZRZAV1oM5R5w7OkjGHEX//qhbesfeIkYur X-Received: by 2002:a9d:6457:: with SMTP id m23mr39806637otl.162.1582529380622; Sun, 23 Feb 2020 23:29:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582529380; cv=none; d=google.com; s=arc-20160816; b=U+WwkVcRe+UMNNQGqlkXCRduWRecIQqpay/OnmVSD4JgEPtAmOFpksHATnm+TUrKvO vN5/e4AHJ5q5SDr3I+/1HVxn1KYfFE2G/QdPTzqTNpd/6vbdoa3Tg8DjTHkWSpePfuvf GHTc/iM6HYpnOKBSSfS+C8vxzcdR3am1yPPDuDSWotgxY/uYZgjtLCerawZtxZZf3S8H yWAdv4TNFhV/zL8a4Z/0jz33TkSrGRa6htQlWtTcQ9mM1cdcH3cSkmT/Gzh50aOklY2B Jwb2NTRwaF9+cJilbDJzbH6bYMZdOW6mgNuyNyitC7TeoodFl2YiYWjm9/DMeZVGLopb mvQw== 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=pu7YZUk9S4E/pNGkKq9v6WlWWKovjGT+ogGtMRUpTlY=; b=jP6JM0hDHBpn+iCkrPZWq1fqbs1wGdjNeTIXtb8ng9NCyAOX+1V/P+0rwcM+GNhPL1 VHw/atOHT7Vckm3l6L75XQ3+VumXQQDSxPsXn1SUuujP+kePkRVb8/ZiSvA7asDsBKaj VSXzX9L0hUNZQpqS37WBgRtKak97NAYA8sV391DWQMLECyM33CSIyoHT8CSANvoVubQs HQLnIxMF1RRGVqTZp06pZMUkgHgUCH+JHN+cbUTpsZ5rAoVodoms8reUwBFzGP+Ne/dn QhlJvKAqM1KhU6JTlPcsoqZcbUOjk22tZzVTYtsySqHkgRwgWNr9tgm559cM84wpD429 sy4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cT1aVW9a; 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 o20si5442057otl.60.2020.02.23.23.29.28; Sun, 23 Feb 2020 23:29:40 -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=cT1aVW9a; 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 S1727206AbgBXH3U (ORCPT + 99 others); Mon, 24 Feb 2020 02:29:20 -0500 Received: from mail-yw1-f68.google.com ([209.85.161.68]:42070 "EHLO mail-yw1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725792AbgBXH3T (ORCPT ); Mon, 24 Feb 2020 02:29:19 -0500 Received: by mail-yw1-f68.google.com with SMTP id b81so4793901ywe.9 for ; Sun, 23 Feb 2020 23:29:17 -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=pu7YZUk9S4E/pNGkKq9v6WlWWKovjGT+ogGtMRUpTlY=; b=cT1aVW9aEmNffyJsbSnpD1pePBSTJmnbrzeS1G1xYMa+f/EI6Ix7TCrDA2k7COPesT nCNnqeSTzMOzj9Dop4WbLhHHv4N6u/cpIlGFjewYNHHviMvEopaL5JfcJWNiNYOy828a 5q0KS5TdQWwz9hJAk7r952F3mLit5GROR1edHOg1LGu+yE8oWQ3LB3TLurElJ+N/IUhS yJOqnGyl9XxLMZrwR8wmW/j6Hkht8FySi7RJe6fJtKM5dYhMdn9bTuo8r9THb3skjq7K /3mYHGC5MFfzeDS085t/m1H8wI9bVvbmxd3lRfq7AH9PkcpWJXQWmBQKz0NmH5Zm2SVy CC+g== 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=pu7YZUk9S4E/pNGkKq9v6WlWWKovjGT+ogGtMRUpTlY=; b=d+NpYbbZqS3CeC6wEK1c1f1ijCJ7yPoK1NohuHdKquiiOwApxBGTXrDy9t7Hf+cw3+ 4sNEgLgm1a/Qz58AUDyhXk4YXrvC7CmOAuEmHwqDzPIu327A9PA1IFYg6EERqVyhDy3Z NCCk3HjuohPFyN/PSMDyKH/QdMQ/PljFpvhOCzzmD1o0VtKrsUkwmzXp9FL8YDXh6ROw 0xhSSOFnV2aTVL/fZ+H/Lv9UvoUfMhfnrxt8SRpQnYScMsid3dxXtSEv2b0FbHyBFpGU zgoamkiANOqQ44+E1YrLBsiowon29Zs2FT6aevleFOOXPlE3VR4aGgcVxwU0VE94ie6j 5c9A== X-Gm-Message-State: APjAAAX5BFl30e/sziUgiiEQv4zSryBI0jfioOuUMv+NLuqXdWsEnv7V b68+IfTqwPUgpM1KM5AmhnjEhiF8+2/LK0nzBhZ2ZQ== X-Received: by 2002:a81:3a06:: with SMTP id h6mr39722556ywa.170.1582529356484; Sun, 23 Feb 2020 23:29:16 -0800 (PST) MIME-Version: 1.0 References: <20200222010456.40635-1-shakeelb@google.com> In-Reply-To: <20200222010456.40635-1-shakeelb@google.com> From: Eric Dumazet Date: Sun, 23 Feb 2020 23:29:04 -0800 Message-ID: Subject: Re: [PATCH] net: memcg: late association of sock to memcg To: Shakeel Butt 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 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. To test this patch, make sure to add a delay before accept(), so that 2MB worth of data can be queued before accept() happens. Thanks.