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 90AD5C3F2D1 for ; Wed, 4 Mar 2020 23:39:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4D1062084E for ; Wed, 4 Mar 2020 23:39:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Wh+3HRWn" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4D1062084E 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 B023D6B0003; Wed, 4 Mar 2020 18:39:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AB20F6B0005; Wed, 4 Mar 2020 18:39:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9ED4C6B0007; Wed, 4 Mar 2020 18:39:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0056.hostedemail.com [216.40.44.56]) by kanga.kvack.org (Postfix) with ESMTP id 850E26B0003 for ; Wed, 4 Mar 2020 18:39:45 -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 408668248047 for ; Wed, 4 Mar 2020 23:39:45 +0000 (UTC) X-FDA: 76559299530.05.crack71_1aeced95e2c41 X-HE-Tag: crack71_1aeced95e2c41 X-Filterd-Recvd-Size: 4667 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Wed, 4 Mar 2020 23:39:44 +0000 (UTC) Received: by mail-pg1-f201.google.com with SMTP id b130so2141092pga.3 for ; Wed, 04 Mar 2020 15:39:44 -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=a2ls/0+XhOrLTmzZ82TksSq7Jty/zbg+jikoqthrwds=; b=Wh+3HRWnRFld+I3x/Iifo4+Qouth/uqsuqouyd2n/JVRzs9fT4wd7TvoPNIYriz/Kp q/ZzDURRIGkj4pUguk91KE/6rt+WdnjL0J5r6Qz0wKMf4mZmTjGIBYrLk+a9icvBsfWy PzVErlzx+MQRl34XA1leAHz1Ui4XPsgnEjvPb6bFxpbQ8c0zsORYd/0t1HdfNr7eUipJ UYaLKuTFBQU3V4w48Zs6JfULm4w7tz/MYUXXUZGzPjsKpKo2/vhRg0oXzHVj9LREpp7Q eaLrpnMd04umIx9Pfv/QovyVUcvq4z5TO9ANWCic7ekQ/kF+iashcbQirvtdtNHDBSkw gmpw== 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=a2ls/0+XhOrLTmzZ82TksSq7Jty/zbg+jikoqthrwds=; b=A0rf7wn/BE9UaHFZSbfi0VZa2GBoyEKoakSn8OM4xogOzGqhiAxBxtIXCoomDXyNMF Pi5yk56FLYVWx7do+XDAfdhnK3VE4T90YDvtbUxg1cBUEfqi4PJxF1gNdDOadk9eEtMT fz8HfIiS6Jxjz+pB0ygRjyDsCwjiQtL9uw0IteNHeE7Bpk5SO1XT+zFwmhYJNN53eBjV /ERBbI7rw8RUEVkh5/prP3FKlYo1lphUx/1xe8upnb5/hkYAjjlsrVX4AtKHGJMtWPZx TQPyACqiWJiJYFvaiMaE824mV5JA9Uc0fR0RbdU8qufyuba2tdy/tqvR1ivXn5NmHnLz PjVg== X-Gm-Message-State: ANhLgQ39qGci6r3YM31jaSF/jg48TV190gqpzHXwHD2ifpB0Zc5bOS32 GfvyL5+SIF4VtVO+NXK7z+3jJhJQfrE8sw== X-Google-Smtp-Source: ADFU+vsMk53h0+vE5U7wBkmRmT2KSMldVQZ0OPGijtXBgWljM7h4j+abzTpnMdLnMtsyvZtdLF1qFkjtxpGxZA== X-Received: by 2002:a63:7783:: with SMTP id s125mr4643014pgc.214.1583365183575; Wed, 04 Mar 2020 15:39:43 -0800 (PST) Date: Wed, 4 Mar 2020 15:38:56 -0800 Message-Id: <20200304233856.257891-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.265.gbab2e86ba0-goog Subject: [PATCH v2] 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 , Hideaki YOSHIFUJI , netdev@vger.kernel.org, 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 such 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 --- Changes since v1: - added sk->sk_rmem_alloc to initial charging. - added synchronization to get memory usage and set sk_memcg race-free. net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c index a4db79b1b643..7bcd657cd45e 100644 --- a/net/ipv4/inet_connection_sock.c +++ b/net/ipv4/inet_connection_sock.c @@ -482,6 +482,25 @@ 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) { + int amt; + + /* atomically get the memory usage and set sk->sk_memcg. */ + lock_sock(newsk); + + /* The sk has not been accepted yet, no need to look at + * sk->sk_wmem_queued. + */ + amt = sk_mem_pages(newsk->sk_forward_alloc + + atomic_read(&sk->sk_rmem_alloc)); + mem_cgroup_sk_alloc(newsk); + + release_sock(newsk); + + if (newsk->sk_memcg) + mem_cgroup_charge_skmem(newsk->sk_memcg, amt); + } out: release_sock(sk); if (req) -- 2.25.0.265.gbab2e86ba0-goog