From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9CD8EC35669 for ; Sat, 22 Feb 2020 01:05:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6C1C5206EF for ; Sat, 22 Feb 2020 01:05:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ymt8dFnW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727851AbgBVBFO (ORCPT ); Fri, 21 Feb 2020 20:05:14 -0500 Received: from mail-pg1-f202.google.com ([209.85.215.202]:54322 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726777AbgBVBFO (ORCPT ); Fri, 21 Feb 2020 20:05:14 -0500 Received: by mail-pg1-f202.google.com with SMTP id l17so2084357pgh.21 for ; Fri, 21 Feb 2020 17:05:14 -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=WpsIiC8eAZfWUPch2OvSdA4y/da0NaMtIWWqtbXOlWmT4ppKnfARWDxAYtFUCk80Ix ZRJe8fbggo8Raxi6PGmdQfRe4WHgTREAsRTIxvwHeCTvz7KqKG89jhs2UsWpHqrUoHfl mQD3hm5xxnsX/4lCTddgW2gc20xpNacJhnOjcc59f9yY9U3t6/qla+TZV2RjzLH2r/Gw Aodx5Su5+390ePLUvgCZA7Co6Yj0Ouf6LcYL5/N/HtPRnrVOkeGwnJqMjZF2BPBLIlW2 gYtoLrHbocUvhUDHlA/3546gfbfZsYv72WBYqwIw5S3EVtMKdAJCaVTX+YfXVtcZlWrM /uyA== X-Gm-Message-State: APjAAAWNGuG/a9DwV6/89GGo5Vfov/7zA5mjSrPPVjuXvHVKQdpsCUbi /6fhBqhrAubyJ11zto0ZaLpYIL/+4UPvvQ== X-Google-Smtp-Source: APXvYqySyYWuzpx0yLr88ZPOnTJL5Aj9ggpD223mgI5KwnpI3NXOrlQ8OJZIxV17ZZyymsTdktXl89KbgD69Wg== 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: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@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