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=ham 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 19C1AC35641 for ; Sat, 22 Feb 2020 01:05:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D1E6C20828 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" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D1E6C20828 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 70E846B0003; Fri, 21 Feb 2020 20:05:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BED96B0006; Fri, 21 Feb 2020 20:05:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5AE856B0007; Fri, 21 Feb 2020 20:05:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0023.hostedemail.com [216.40.44.23]) by kanga.kvack.org (Postfix) with ESMTP id 3FE3B6B0003 for ; Fri, 21 Feb 2020 20:05:15 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id EFDD58248047 for ; Sat, 22 Feb 2020 01:05:14 +0000 (UTC) X-FDA: 76515969348.05.route04_4ade95fbd6337 X-HE-Tag: route04_4ade95fbd6337 X-Filterd-Recvd-Size: 4225 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Sat, 22 Feb 2020 01:05:14 +0000 (UTC) Received: by mail-pf1-f201.google.com with SMTP id z19so2295411pfn.18 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=Vv/wKtu5h7Di6ODw5QhiMB5IP8uPQ4aDQ8tle+WOMo5ODbLoiOOMKMAe6/4QOU/kNa 5tspKsJ5dH4jQqM8gmVuEr8v3hl5u1b+3+Wvx+PbQC1muf8EGxt3ERKUW9/V3sjXl4MM Ok6ZprfJRS4M3EwjhapWXFvXD/RrqjGgM9YoRz+swUap0UivwN7WLiARxzW09xjNvChX uhvabfV/hQzC0bOt6LUpdJpYV/XyykKLqjL3EWonoc7lMZEoGDIvn2GWLsuwHgpF1Pfp w6hLePmRZ8vXFwb5jwr23oO4iQSthyoH7HlIV5jOATiqQDUBKtg3Y49I4ioTngeYHCXp gJww== X-Gm-Message-State: APjAAAVBy+1bVgnJ59MFyHk/KI2fXUHJ+WXUxrzFlC/Oqw3b782h6+ci rmDbmMJpDeGF+LWGYq8Qt7he45AhmJLDUA== 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" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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