Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2497630ybv; Fri, 21 Feb 2020 17:06:39 -0800 (PST) X-Google-Smtp-Source: APXvYqzGM/0KZkuy7h0XwAEWdcVvSuVHAUdcXCjZtVuc4arkdfrKiXBGxHr/C7NDhM7QoFlzHCyV X-Received: by 2002:aca:1a17:: with SMTP id a23mr4255715oia.84.1582333599255; Fri, 21 Feb 2020 17:06:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582333599; cv=none; d=google.com; s=arc-20160816; b=iKhwIFu/hld/DqIp38rCeAmY1hjnXxoCY9ZhNmtDzSNEAP4UAgASx346UIMPbsZJet 2KA24HhlwXFN+SQ9ar5dzPpaKh2tJv4c4JrznVwWR1NndopTzthKncCJhDoINpcBxGrl nih6x/3hi1+/wOLWGX+zxoTkoQC9QbPPrZKTxVIo1YwpoTMnVMeA/++9GMaBUFWXfrvl 0/eM08TGN3qfYa2yhipQuY/JOGhJgAtNy6BLPM4/mllUA+r0B0bBn8YXUjLlsetlropz MTqGZq+PpHkd6jhc+Xjqa0UnQRVqIL7hliFmmoEjh5MiA9TrTjkDR9hVHlCIFP+Dp8A5 tkQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=/3yl8VhgDmjkXmLfWG6CmIalz2F0UKze8er/2PUvhvg=; b=yAFVSl2IW0B1MKlpeYR+2jbsqopA8gNULdKV1CdbKqbIf40YoPpt5rJJWSOebA8/WX 24la7mJp3DfZAIUwtzuMSBYtr0vdXRtPlf3Ck73e83v9V8PnMWwWAjkIu5sdWG/EjJTY Pw7oL0HPQTVulCRE5do0lOPX5D2ozUtlDcpPj0L9qiIggoQM/KOuU0k3RuFH/mHocG5F BuKOCeQ2mc3pM7rQeTDeUdB9zqOZrlVA/k7fy0zpQEs3O6IoK8Y3AuKLROzRUuZigxx2 8/lxjsKn5V5aQzxpg4jf3DTg0drznS7w2NCeXBV6cX3Fx0z+uO0wDp+ipbedQ1FkBqzg bHfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Ymt8dFnW; 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 k4si2189434otp.186.2020.02.21.17.06.26; Fri, 21 Feb 2020 17:06:39 -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=Ymt8dFnW; 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 S1727869AbgBVBFP (ORCPT + 99 others); Fri, 21 Feb 2020 20:05:15 -0500 Received: from mail-pf1-f202.google.com ([209.85.210.202]:33116 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726777AbgBVBFP (ORCPT ); Fri, 21 Feb 2020 20:05:15 -0500 Received: by mail-pf1-f202.google.com with SMTP id c72so2315406pfc.0 for ; Fri, 21 Feb 2020 17:05:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=/3yl8VhgDmjkXmLfWG6CmIalz2F0UKze8er/2PUvhvg=; b=Ymt8dFnWLKbA5tN9IDfmujvOxbWEG48HlhlzvroTZMjYPzSWjt8utQxlTL3bXJPDYx 3+TiA/F/b9pYowmG3T3VbN/DGLEOC2bKTeD5ayJ2A5UsPgaDnjvvNH9PposF/IfzuLNU qPiH1MUNmwrRUgprTKTn+LoFP4wLI9l538CvuVVlgYb0B5bgOpORGGZ5hL6/+iBY5dTl GGDZy85UVPb6XXZEBAg8tDB2ikz64+InMXYMGnjlQIl+q5P81K6yA3IIrP0cxI7g2DiY 45WmsE4Hy/XwAldUMG09+sOyAdKtSqw1bGbN4rfRiDpOgO3mSN3xbiRp9ddpm4e2Mq6M 7rZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=/3yl8VhgDmjkXmLfWG6CmIalz2F0UKze8er/2PUvhvg=; b=EJ/2CYbMSs40y9ckHQktKavMjb1Qp1WztvR2ZdNsYyJKn00uoGFxqdbCih9ZWYJCl0 5cZugKf6i6z05EzffhXtBWuV9eqKPwk95T1kyzFlZF4rjY25V8OPDQ8kwdLCk3efKmwH 70Zb/8HXh+m9EliA5tm5+kU2o5TZlJTNfNKRz3XFDgzyphEtl6aIHhoHa1T0NvxL4CAf CUdYU1Ib86ogpwl3xxxFk2xfZIKxxUjCW+ywTtGKD5a680azaPTZzBbRp0n3eOQ2A7YX 1K3+95f+ZsFsROVXKmgM9hJkbRNnr+MBJMRNze9BeQcx3nicQU0HPtiO5y2NlhOcyF55 6/6Q== X-Gm-Message-State: APjAAAV8FsyUcqy0BuHC7kJEay/vxhcRhDTlqA3xqLB/O23Mfq7I7Ed1 uRRlQkcqbQb/d8/dmTUZep+3v1E17359Tg== X-Received: by 2002:a63:d710:: with SMTP id d16mr40563177pgg.393.1582333512913; Fri, 21 Feb 2020 17:05:12 -0800 (PST) Date: Fri, 21 Feb 2020 17:04:56 -0800 Message-Id: <20200222010456.40635-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH] net: memcg: late association of sock to memcg From: Shakeel Butt To: Eric Dumazet , Roman Gushchin Cc: Johannes Weiner , Michal Hocko , Andrew Morton , "David S . Miller" , Alexey Kuznetsov , netdev@vger.kernel.org, Hideaki YOSHIFUJI , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Shakeel Butt 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 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)); + } out: release_sock(sk); if (req) -- 2.25.0.265.gbab2e86ba0-goog