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=-8.3 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 271B3C3F2D1 for ; Thu, 5 Mar 2020 21:17:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DBB992166E for ; Thu, 5 Mar 2020 21:17:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QLDq4YaD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DBB992166E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 783B56B0005; Thu, 5 Mar 2020 16:17:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 75AE76B0006; Thu, 5 Mar 2020 16:17:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66FE26B0007; Thu, 5 Mar 2020 16:17:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0019.hostedemail.com [216.40.44.19]) by kanga.kvack.org (Postfix) with ESMTP id 4D8566B0005 for ; Thu, 5 Mar 2020 16:17:13 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 068702496 for ; Thu, 5 Mar 2020 21:17:13 +0000 (UTC) X-FDA: 76562569146.11.meal98_4bdf6165374b X-HE-Tag: meal98_4bdf6165374b X-Filterd-Recvd-Size: 5805 Received: from mail-pj1-f68.google.com (mail-pj1-f68.google.com [209.85.216.68]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Thu, 5 Mar 2020 21:17:12 +0000 (UTC) Received: by mail-pj1-f68.google.com with SMTP id o21so2665541pjs.0 for ; Thu, 05 Mar 2020 13:17:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=sMCdEJuXdOhoW7m3gO5zM39AjW+YF/Rrads4zZrd1kg=; b=QLDq4YaDIhaMBuukLE9flkTFbc+QtHMA/oiSdyTDoEucukwkh/qEIOtiLrIaSPrTK6 IbBwi9DE4rWJY/cRk6jqyRNQWADhqF4G3vuMdui0IZQ7kCZlXSJ6hvraMZnX89JIvTfq BHstEMkSF+S53O+U2m43bKmbZaKOP7JCwhNQoCViZCDX8rGi8rzzaJV2caPcb181w2cV 5Ih7zwO2yZySaFOkknNUNLu46EQk/KI0nqyA5a2gSpyIvBCNIcrXNyv4auEuOR6S6rYO wM8iKHLjtq8nNk20lh0Jm32XL9MiJhdHR8jU/J+3piz5zb/m8XsTiWgpo/DSCYRlR3js 37Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=sMCdEJuXdOhoW7m3gO5zM39AjW+YF/Rrads4zZrd1kg=; b=QFOH36NZWUXdEC+y59pkLMR41kmFNDYpsyo0vcp5YYfxb/UJcOdHLzsDnYrR4bkQ+Y Sk81Sk4DKK+u8Zba3Uu8sefzQqiWClt8S0HIwzqgE4tKtT0pIYnrr9Ac4XLVeXCKRAN3 bKkckm6+tOLw1bux92AYcitnTNR3056JshbOA+STPNqJ8QZzUxvHM9gviU9+za1NQsRW RVbaLe4ru5hRhqTAndLxXKUPQX8MVKlN3GbnOiUK6iaKh/Sqg/EwS/UsJ9ridYT6E2Uv QmU5rcsG7AA6fJPz0VOPQKW5Z/qmLk4CwmVOLyrCJdL5dE2IO+dh9LfvFPd2iVkuGnzZ Y3RA== X-Gm-Message-State: ANhLgQ2iTciffMkKyW1StYwGuiNUATWYRRVf5hXB6KBZVsKzKXNhVnBs hfmO+VvpNsH3x16hTfgced8= X-Google-Smtp-Source: ADFU+vuBValh/xWu8eZS3JQlD4ct4iHhbaZw6HRIxQki/aNn7M/w9mK7wdAYuhReqpJGtbECQIuY/w== X-Received: by 2002:a17:902:401:: with SMTP id 1mr94281ple.177.1583443031459; Thu, 05 Mar 2020 13:17:11 -0800 (PST) Received: from ?IPv6:2620:15c:2c1:200:55c7:81e6:c7d8:94b? ([2620:15c:2c1:200:55c7:81e6:c7d8:94b]) by smtp.gmail.com with ESMTPSA id na18sm8053910pjb.9.2020.03.05.13.17.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 05 Mar 2020 13:17:10 -0800 (PST) Subject: Re: [PATCH v3] net: memcg: late association of sock to memcg To: Shakeel Butt , 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 References: <20200305205525.245058-1-shakeelb@google.com> From: Eric Dumazet Message-ID: <9505d35b-f9fc-149b-6df5-e65ad95acabb@gmail.com> Date: Thu, 5 Mar 2020 13:17:09 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: <20200305205525.245058-1-shakeelb@google.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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: On 3/5/20 12:55 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 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 v2: > - Additional check for charging. > - Release the sock after charging. > > 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 | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c > index a4db79b1b643..5face55cf818 100644 > --- a/net/ipv4/inet_connection_sock.c > +++ b/net/ipv4/inet_connection_sock.c > @@ -482,6 +482,26 @@ 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, set and charge the > + * 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); > + if (newsk->sk_memcg && amt) > + mem_cgroup_charge_skmem(newsk->sk_memcg, amt); > + > + release_sock(newsk); > + } > out: > release_sock(sk); > if (req) > This patch looks fine, but why keeping the mem_cgroup_sk_alloc(newsk); in sk_clone_lock() ? Note that all TCP sk_clone_lock() calls happen in softirq context.