Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp7580765rwd; Tue, 20 Jun 2023 03:24:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6gqgbScSvIAmurwyM58/wzAx6wn5vcgg7oGvIreI4pQCLJC5KO4og8AFzu2hlnBC1XXDKo X-Received: by 2002:a05:6a20:6a03:b0:10a:eea0:6987 with SMTP id p3-20020a056a206a0300b0010aeea06987mr16941553pzk.26.1687256646764; Tue, 20 Jun 2023 03:24:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687256646; cv=none; d=google.com; s=arc-20160816; b=kHR8qezaT7BgHmQ+W+TAyj/VcPB8YlbmlB9h1Gx7pr3RgC3FsY2sIJEwWKAlx2hWGN QM0YumnewCcZLKkTe5/CKnOpuZmb4+e0LVKpL+Yhmn7JNnPJ027n9PgOxwmoh/8e5Rxe /54jNoIOJKoYfAwgB+qrtw0Vbv/uD5pNowLIjWK++cMKJylYnSRNjt2F3WzU5Qtrc3kh atNtgQj7urjZT9VBRzFAO9A8+IoG1vxiILhsRDxl+6BIoGDn7FnhGKrNSXK19bPsvpmQ uK8QVnuvtYVYJan0zJlb06LiCStZtFEtfwy88rV+ZrtAlhjrqn4GI2mdfPQ84YTA8SEC RT6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=hfUr1e9aEQZRasLBwYauLBgpBkHg6F78JJmqZq7mi1o=; b=a46RYBHnnAGK+sYVtl6whqoC9J+dMVXyTChvLdS3k9hEsc42ulLyjwDZ/feqepLngu jdT+QhMS2qNMVLQHI9CSeMq8f6XYEpygbdWYZjaxmkSxCRA3hU1kjGhEm310WQRoGB9F O57enJTjwtsn9hehA7+ccSzFbvIJ02qtUifVzZZlFRDeQMDgQts/JINwPA0gkor+qnV/ aOHHrmrvwgd0X+Rj9Kywg/OeRgOjKHM9RXxW+CsROCI/aEcue7QsBjCkiKpOEXVTQ9Rf f848kgVqYCE3HlWkiYw2JQMPTGEQbK4PAsHg4Ir3YQll3rvItPwNQsrwFBEvJQIi7eiN 89LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=kGAeI2k3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g4-20020aa79dc4000000b006687ee37224si1427441pfq.0.2023.06.20.03.23.52; Tue, 20 Jun 2023 03:24:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=kGAeI2k3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231561AbjFTKN3 (ORCPT + 99 others); Tue, 20 Jun 2023 06:13:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231232AbjFTKN1 (ORCPT ); Tue, 20 Jun 2023 06:13:27 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6059DA2 for ; Tue, 20 Jun 2023 03:13:26 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1b505665e2fso37151495ad.0 for ; Tue, 20 Jun 2023 03:13:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1687256006; x=1689848006; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=hfUr1e9aEQZRasLBwYauLBgpBkHg6F78JJmqZq7mi1o=; b=kGAeI2k3c7yrjwWMuk9zKQSyRj2fyG04oBVEuSGxLYzlpdg1RRLqQ7vgmusYieQWNq rXUbQkyU5cdc14ox53xkgIKMau5Mhro6ux/G7dWapSTmk8pZBq89mctgu8eisA/8O9HC p40SMFfbn+a8xjDhJKTdGZqslkEP9dtUhf0LDMYYoMLa7YrxSZXtv/eUdwZ953rE+6iC tftSJje1HVjU3LUTDS/mcmPRnKsRcq2b0WuDCgPda5ilR5CLwbsqMOFjPXLhWSOR4JCr /xwzY0vHRGiYhFLclXS5Bb8OzE674jpHuR24JOYdlWXtkWpO65LmiZmQ9T/6YIk4JSGy pcmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687256006; x=1689848006; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hfUr1e9aEQZRasLBwYauLBgpBkHg6F78JJmqZq7mi1o=; b=kuVzWnBm9bFaBiH2/tVjWkuo5vRkKJfXRbniW1zKcSCxFtLp3KRrpryAKKgIly0YD6 J/1q2RHFnfgHi2DCO+8Ko3MtQa+OFt2Dpc9WY/nzNP0d9AeUhFqFMBC1dh/WwGsjIUVl 21AO+w8Tn54OIjmxaaNORAuvQuaGZULncK8hr6e6oiUUeDIH/kBVoETwzagHqdgLVtCm xd30Qj6mtjjdpoZzg8NN12Rp1G3HtT+gMIH1HjY+SKViAZs8L7q7rXEDDJd4QAg1T42O e9zHKgpQyL13uXkugwn5A/3T8upw1vur1kt2j9kuSEscoIOh6+uOtFTaV51ALMtmStM8 QSAA== X-Gm-Message-State: AC+VfDwXco5Q0EbePaDx4LryJJh2UySA+hbTlB08eefbBma0dtEHZutf S55WTtzfybik7gERitHr00VKUA== X-Received: by 2002:a17:902:d2ca:b0:1b0:5ce9:adc7 with SMTP id n10-20020a170902d2ca00b001b05ce9adc7mr23389792plc.28.1687256005848; Tue, 20 Jun 2023 03:13:25 -0700 (PDT) Received: from [10.94.58.170] ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id n21-20020a170902969500b001ab1b7bae5asm1270189plp.184.2023.06.20.03.13.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Jun 2023 03:13:25 -0700 (PDT) Message-ID: <23d45f7e-3a34-44b3-f1a0-b992bbb5076f@bytedance.com> Date: Tue, 20 Jun 2023 18:13:18 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: Re: [PATCH net-next] inet: Save one atomic op if no memcg to charge Content-Language: en-US To: Eric Dumazet Cc: "David S. Miller" , David Ahern , Jakub Kicinski , Paolo Abeni , "open list:NETWORKING [IPv4/IPv6]" , open list References: <20230619082547.73929-1-wuyun.abel@bytedance.com> <6ed78c81-c1ac-dba4-059c-12f6b2bb9c53@bytedance.com> From: Abel Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 6/20/23 4:46 PM, Eric Dumazet wrote: > On Tue, Jun 20, 2023 at 5:04 AM Abel Wu wrote: >> >> On 6/19/23 6:08 PM, Eric Dumazet wrote: >>> On Mon, Jun 19, 2023 at 10:26 AM Abel Wu wrote: >>>> >>>> If there is no net-memcg associated with the sock, don't bother >>>> calculating its memory usage for charge. >>>> >>>> Signed-off-by: Abel Wu >>>> --- >>>> net/ipv4/inet_connection_sock.c | 18 +++++++++++------- >>>> 1 file changed, 11 insertions(+), 7 deletions(-) >>>> >>>> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c >>>> index 65ad4251f6fd..73798282c1ef 100644 >>>> --- a/net/ipv4/inet_connection_sock.c >>>> +++ b/net/ipv4/inet_connection_sock.c >>>> @@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) >>>> out: >>>> release_sock(sk); >>>> if (newsk && mem_cgroup_sockets_enabled) { >>>> - int amt; >>>> + int amt = 0; >>>> >>>> /* atomically get the memory usage, set and charge the >>>> * newsk->sk_memcg. >>>> */ >>>> lock_sock(newsk); >>>> >>>> - /* The socket has not been accepted yet, no need to look at >>>> - * newsk->sk_wmem_queued. >>>> - */ >>>> - amt = sk_mem_pages(newsk->sk_forward_alloc + >>>> - atomic_read(&newsk->sk_rmem_alloc)); >>>> mem_cgroup_sk_alloc(newsk); >>>> - if (newsk->sk_memcg && amt) >>>> + if (newsk->sk_memcg) { >>>> + /* The socket has not been accepted yet, no need >>>> + * to look at newsk->sk_wmem_queued. >>>> + */ >>>> + amt = sk_mem_pages(newsk->sk_forward_alloc + >>>> + atomic_read(&newsk->sk_rmem_alloc)); >>>> + >>>> + } >>>> + >>>> + if (amt) >>>> mem_cgroup_charge_skmem(newsk->sk_memcg, amt, >>>> GFP_KERNEL | __GFP_NOFAIL); >>> >>> This looks correct, but claiming reading an atomic_t is an 'atomic op' >>> is a bit exaggerated. >> >> Yeah, shall I change subject to 'inet: Skip usage calculation if no >> memcg to charge'? Or do you have any suggestions? > > I would call this a cleanup or refactoring, maybe... Alright, I have changed to 'cleanup', please take a look at v2. Yet I have another question about this condition: 'if (newsk && mem_cgroup_sockets_enabled)' IMHO in the scope of cgroup v1, 'mem_cgroup_sockets_enabled' doesn't imply socket accounting enabled for current's memcg. As the listening socket and the newly accepted socket are processing same traffic, can we make this condition more specific like this: 'if (newsk && mem_cgroup_sockets_enabled && sk->sk_memcg)' would you mind shedding some light please? Thanks! Abel