From patchwork Tue May 30 11:40:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abel Wu X-Patchwork-Id: 13259793 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18823C77B73 for ; Tue, 30 May 2023 11:40:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44B426B0074; Tue, 30 May 2023 07:40:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3FAC56B0075; Tue, 30 May 2023 07:40:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C2B1900002; Tue, 30 May 2023 07:40:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1F4456B0074 for ; Tue, 30 May 2023 07:40:37 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B6E421401D5 for ; Tue, 30 May 2023 11:40:36 +0000 (UTC) X-FDA: 80846728872.25.8FD27E9 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf15.hostedemail.com (Postfix) with ESMTP id BB7DFA0017 for ; Tue, 30 May 2023 11:40:34 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=hhLQ3LoK; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf15.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685446834; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zSeZy3dBgM/hAvW1l4S3j81M4ry+F3vLfbl4Wg72DeY=; b=huhgklOj9u8QTC4x0gIqAgA+3Pc/2XS2YZDOZ1IhermK29/y14hTO328VdAvUs+Lv/Jv+v wwxTVd93Jm8m2n5mQxiJQH3ZAHbSlMBBAkplSYSizBnTBMK87pWmKqUxZkZ2L69EOKhV2A jR+LWH0XtQ9aHYXEG+ckoYF+FR9Ms2M= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=hhLQ3LoK; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf15.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685446834; a=rsa-sha256; cv=none; b=IrdPuyVAf0JsKU//uSLGTwc5URhs3ksta84Bfd7NCF75ZU40dGpVD5rqvbUg/UZN/9ebZh QJUPAk67HwosFQetEB/olaKcCoolfJrMeS/fPQlOyZUOoFd2VR3bmAOeK5iYBjDP7/fSMK EFjyZBsaRrR++hJj/n4CTIil48JQZPE= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-64d5b4c3ffeso3113963b3a.2 for ; Tue, 30 May 2023 04:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1685446833; x=1688038833; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zSeZy3dBgM/hAvW1l4S3j81M4ry+F3vLfbl4Wg72DeY=; b=hhLQ3LoKBaJEJ5y7uGQ1a6tBrIrMZV+1pnIfqUg+3PPYZLcdnc+7VDqo7mmHd61hrj Sd4swO79Q16159eQDWb/4DVkqYPSM/uwLu9+nrIMavuQvSS0iMMA2tKxbWYOLOTyMNNW 8Q9YqbEBpn+Nx2pNK4smxSztJf5X5kZARTJKBTBELw90yTB1VKCkqnwAR8Px/p/KKR7V fOhc0w5nYnh7RRxeDgcc3lEBrXih07VolZo5Ebp7QGT1yNFQEfcQDeiZsXJhF9CLMeJk SavXV7b4KyPrvCtY6W/D4aQTnvHV5HjuN8C1ttrCgPPMzrSoHBP01IYNksy9EhRLI1EZ mTeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685446833; x=1688038833; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zSeZy3dBgM/hAvW1l4S3j81M4ry+F3vLfbl4Wg72DeY=; b=Qgm36iIJGJ4l6gB9RgOTcLeh8Ch1fKBuc5K4FH+VcMbUpKJ+XRrkHcuG/w/4Cv10iL KB3CevdB+1nBZ2y4XhUYyyCkM57cSb/mQ2cv6V1c7fIgdN2ZzOeU4SL7ViLY0ttADBj5 Q2F/ekDNWR1kMKg0yxRrrXjhbVxo1bEDKqzHdpifmS5opl2w9i0YXGsZ/VR8mz/syNx/ /EnNIKayc14lzns0GkzL/MiOWN+9NtzGM2ylZpb2q6qOlIYB8vr0BJZZsNIsNE2GACta JSjgJcrdp2JMzoiADxb4Y1yWe7CpZI1QKiRfAnQibqQHoMJfesSg/FsjdBWa/mCck0My FNFA== X-Gm-Message-State: AC+VfDzRcFR7r+b1hILMd84RBOTdmxzB09PvBbXP7rG95w84e4OQHr3y YQEIe7BGZtu1RMgHhjCdaHjLTQ== X-Google-Smtp-Source: ACHHUZ4xzkdDhUOHefB08/7eoqUUKTgwsuecz4t21GvJg0uPWZgET1FRSQPRi1s3ik542LqiUkJgag== X-Received: by 2002:a05:6a20:8e14:b0:103:b0f9:7110 with SMTP id y20-20020a056a208e1400b00103b0f97110mr2307010pzj.11.1685446833446; Tue, 30 May 2023 04:40:33 -0700 (PDT) Received: from C02DV8HUMD6R.bytedance.net ([203.208.167.147]) by smtp.gmail.com with ESMTPSA id j20-20020aa78dd4000000b00642ea56f06fsm1515103pfr.0.2023.05.30.04.40.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 04:40:32 -0700 (PDT) From: Abel Wu To: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Johannes Weiner , Michal Hocko , Vladimir Davydov , Shakeel Butt , Muchun Song Cc: Simon Horman , netdev@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Abel Wu Subject: [PATCH v4 1/4] net-memcg: Fold dependency into memcg pressure cond Date: Tue, 30 May 2023 19:40:08 +0800 Message-Id: <20230530114011.13368-2-wuyun.abel@bytedance.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20230530114011.13368-1-wuyun.abel@bytedance.com> References: <20230530114011.13368-1-wuyun.abel@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: BB7DFA0017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: tptgm3x1pcrdc9i31pbu9dydatau5ejg X-HE-Tag: 1685446834-169015 X-HE-Meta: U2FsdGVkX18pZ9gimLUHPXe6exhTjkI4bIBmLiMdC2fM8fNmPgS7CVFvOd7f2VfstgR+gkCoWDigE72SO1gkMIRJgOVYHeRFbSOO2l3YA6FMVAw+hbCvudrGKSay4sIulLtLl2hgEgHlSuCryhtNWUU83ZhzspD4pZ5TlfeGcGUbH4cy5LCa7ovOf9Y5+whn+UAQjCMEm7PRSjNGQzGt315ZGyneHmQHptm3yyZcCDBxs324xVouLjhAr1KS5Qzi2s+MU6nUdxB+MYf3gh4tHQCvDCd/SXCqbdLea9CrJ9ug6zOWacpAPHxVuOaKOSPBnDmoq2YXfHJi/nNG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ZqAcT29rPVj2Pwt0Owd6/66nDVrt5nr0eIv+y6l+u0ufj/LdB3pFQxtgtCs24YKouinNcI= 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: The callers of mem_cgroup_under_socket_pressure() should always make sure that (mem_cgroup_sockets_enabled && sk->sk_memcg) is true. So instead of coding around all the callsites, put the dependencies into mem_cgroup_under_socket_pressure() to avoid redundancy and possibly bugs. This change might also introduce slight function call overhead *iff* the function gets expanded in the future. But for now this change doesn't make binaries different (checked by vimdiff) except the one net/ipv4/tcp_input.o (by scripts/bloat-o-meter), which is probably negligible to performance: add/remove: 0/0 grow/shrink: 1/2 up/down: 5/-5 (0) Function old new delta tcp_grow_window 573 578 +5 tcp_try_rmem_schedule 1083 1081 -2 tcp_check_space 324 321 -3 Total: Before=44647, After=44647, chg +0.00% So folding the dependencies into mem_cgroup_under_socket_pressure() is generally a good thing and provides better readablility. Signed-off-by: Abel Wu --- include/linux/memcontrol.h | 2 ++ include/net/sock.h | 3 +-- include/net/tcp.h | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 222d7370134c..a1aead140ff8 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1743,6 +1743,8 @@ void mem_cgroup_sk_alloc(struct sock *sk); void mem_cgroup_sk_free(struct sock *sk); static inline bool mem_cgroup_under_socket_pressure(struct mem_cgroup *memcg) { + if (!mem_cgroup_sockets_enabled || !memcg) + return false; if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && memcg->tcpmem_pressure) return true; do { diff --git a/include/net/sock.h b/include/net/sock.h index 8b7ed7167243..641c9373b44b 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1414,8 +1414,7 @@ static inline bool sk_under_memory_pressure(const struct sock *sk) if (!sk->sk_prot->memory_pressure) return false; - if (mem_cgroup_sockets_enabled && sk->sk_memcg && - mem_cgroup_under_socket_pressure(sk->sk_memcg)) + if (mem_cgroup_under_socket_pressure(sk->sk_memcg)) return true; return !!*sk->sk_prot->memory_pressure; diff --git a/include/net/tcp.h b/include/net/tcp.h index 04a31643cda3..3c5e3718b454 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -261,8 +261,7 @@ extern unsigned long tcp_memory_pressure; /* optimized version of sk_under_memory_pressure() for TCP sockets */ static inline bool tcp_under_memory_pressure(const struct sock *sk) { - if (mem_cgroup_sockets_enabled && sk->sk_memcg && - mem_cgroup_under_socket_pressure(sk->sk_memcg)) + if (mem_cgroup_under_socket_pressure(sk->sk_memcg)) return true; return READ_ONCE(tcp_memory_pressure);