From nobody Wed Sep 17 19:54:49 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDAA82FFDE0 for ; Tue, 12 Aug 2025 17:59:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755021548; cv=none; b=HqpH8apNc5CFI2gAcQPRq/Xz363Mpt2l0mWExcnT3E0RLRpyEwGO9ABWaChpBPYy6nlKexHyBw4mRbr5dKXShvi+6u1LJOz77PRXdzHs7r1FW1tJE6S7/S+nL+bFc48Uz75WZU/zGjOVhFzas6zla1/xzVw/+vy2KdBtmYJxgFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755021548; c=relaxed/simple; bh=KZsxQ9Vejvjn3ihqtI+VvNwL3Rw10GiDSiQrIYh5KLk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WcQGD4wlKufBxZRMpdnx6jS9N6pgZYmyxdPmtI/9JQSuaKQr34084dKFY7OpCJTSLxEexmvs8CBKOpwoToQEcuwt1yCQ3BSfUCgk9tR8pzT/xiDLNHGt8CJ/jkxK6Ck6GWrenNIfGsxAeCWx6bWWGJkU3fDsHfNUTlXPAizSCoE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LlR0morO; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LlR0morO" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-76bc511e226so5676285b3a.3 for ; Tue, 12 Aug 2025 10:59:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755021546; x=1755626346; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lmzqnAc1jsyellKSOYCRzvGhauBIubxeUGW9qtoYYWI=; b=LlR0morO4sZKYmE7vF9lIgIRM75IdYYxN5HAj+OYaDPAN68wOpHQOKazOq30eLf5Y4 JfSWHH+cpAUtbGQs9ABv9PATC7AS7EbROF4K7crS/l6I199TuYZJTabadsZWFAMYEfmR AN9yq1VU2HmyDMGL4tkZIiMyZmyVyxa7QenkegirTJjwYsZZdl/D6RufUcDqAI10BC5m VE4RJkwSsARVMsMJ4ElRyazEyYLiwH+ah4IAoYFj1fYvwH+0f1dj09a7bLrdmcxPH/d/ sY1lMdrCaHHbb/HIkcLyy2poIQSvMqgifiEKCnnRCuuxbk1qkpogFRQyrzpfoXWn4sUR nY1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755021546; x=1755626346; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lmzqnAc1jsyellKSOYCRzvGhauBIubxeUGW9qtoYYWI=; b=WTcnqTFM1ijhjZ0ecB68zPDpIYNdZa8oOPKYcFJc5mTuw22pn3Pi50U6/i1J6j07sF Qruv4ZAlPgBke0NUf6BlFl+gnBybbvEs4IsPllexMe3OfO1FjIiSSfBL2EylU2rUhdsj mMj+5ey/lIwxt0hSgh1YXfZVkIqm4h1IfB2BheKcOyULpnJA7fGUqZUvzmH3To2qSPFE UqeLPbmzngOIyZ9gaa2lxc+3kCDvlv+KaIG4KLp6d7XbiqkK/dh/gGG/AilfDxbV+hZu UtPH/PqCFMRMJ18mKmWU8wbzonQntnuQSCF9hPt//vnsVNwBAfoHSa2jlCD4fxRvMIuq HBlg== X-Forwarded-Encrypted: i=1; AJvYcCXV8r5LRrkiFxxjSKgDq0HmvBqXXFAS0TUUm1CzvsADX44m61olH3nzZAoIUtqE6d1KUcJ2zw==@lists.linux.dev X-Gm-Message-State: AOJu0YyQarXkpL9EOlfn96jDX/FUFQGzjhqoC7V0HkyY09AGKj8XA7XN T70NHmYOnc8IJw3unlN/6j6HqwKbSeqCRXnJwGwWUXlcyejCEKgLeBebEtnXgPGaLwYvyLiTAji 12akVRA== X-Google-Smtp-Source: AGHT+IGdZk7MwPY5BeI43bceFVbz9bcwzyVjG/myi4TvBkOWcTUGeu9X1TXG1+jvSNdfiHL0s8PKRPjYYcg= X-Received: from pfbcn7.prod.google.com ([2002:a05:6a00:3407:b0:739:8cd6:c16c]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:887:b0:76b:f9f0:bef2 with SMTP id d2e1a72fcca58-76e20fb0e4emr135908b3a.14.1755021546076; Tue, 12 Aug 2025 10:59:06 -0700 (PDT) Date: Tue, 12 Aug 2025 17:58:25 +0000 In-Reply-To: <20250812175848.512446-1-kuniyu@google.com> Precedence: bulk X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250812175848.512446-1-kuniyu@google.com> X-Mailer: git-send-email 2.51.0.rc0.205.g4a044479a3-goog Message-ID: <20250812175848.512446-8-kuniyu@google.com> Subject: [PATCH v3 net-next 07/12] net-memcg: Introduce mem_cgroup_sk_enabled(). From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Neal Cardwell , Paolo Abeni , Willem de Bruijn , Matthieu Baerts , Mat Martineau , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Andrew Morton , "=?UTF-8?q?Michal=20Koutn=C3=BD?=" , Tejun Heo Cc: Simon Horman , Geliang Tang , Muchun Song , Mina Almasry , Kuniyuki Iwashima , Kuniyuki Iwashima , netdev@vger.kernel.org, mptcp@lists.linux.dev, cgroups@vger.kernel.org, linux-mm@kvack.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The socket memcg feature is enabled by a static key and only works for non-root cgroup. We check both conditions in many places. Let's factorise it as a helper function. Signed-off-by: Kuniyuki Iwashima Reviewed-by: Eric Dumazet Acked-by: Roman Gushchin --- include/net/proto_memory.h | 2 +- include/net/sock.h | 10 ++++++++++ include/net/tcp.h | 2 +- net/core/sock.c | 6 +++--- net/ipv4/tcp_output.c | 2 +- net/mptcp/subflow.c | 2 +- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/include/net/proto_memory.h b/include/net/proto_memory.h index a6ab2f4f5e28..859e63de81c4 100644 --- a/include/net/proto_memory.h +++ b/include/net/proto_memory.h @@ -31,7 +31,7 @@ static inline bool sk_under_memory_pressure(const struct = sock *sk) if (!sk->sk_prot->memory_pressure) return false; =20 - if (mem_cgroup_sockets_enabled && sk->sk_memcg && + if (mem_cgroup_sk_enabled(sk) && mem_cgroup_under_socket_pressure(sk->sk_memcg)) return true; =20 diff --git a/include/net/sock.h b/include/net/sock.h index 811f95ea8d00..3efdf680401d 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2599,11 +2599,21 @@ static inline struct mem_cgroup *mem_cgroup_from_sk= (const struct sock *sk) { return sk->sk_memcg; } + +static inline bool mem_cgroup_sk_enabled(const struct sock *sk) +{ + return mem_cgroup_sockets_enabled && mem_cgroup_from_sk(sk); +} #else static inline struct mem_cgroup *mem_cgroup_from_sk(const struct sock *sk) { return NULL; } + +static inline bool mem_cgroup_sk_enabled(const struct sock *sk) +{ + return false; +} #endif =20 static inline long sock_rcvtimeo(const struct sock *sk, bool noblock) diff --git a/include/net/tcp.h b/include/net/tcp.h index 526a26e7a150..9f01b6be6444 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -275,7 +275,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 && + if (mem_cgroup_sk_enabled(sk) && mem_cgroup_under_socket_pressure(sk->sk_memcg)) return true; =20 diff --git a/net/core/sock.c b/net/core/sock.c index 000940ecf360..ab658fe23e1e 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1032,7 +1032,7 @@ static int sock_reserve_memory(struct sock *sk, int b= ytes) bool charged; int pages; =20 - if (!mem_cgroup_sockets_enabled || !sk->sk_memcg || !sk_has_account(sk)) + if (!mem_cgroup_sk_enabled(sk) || !sk_has_account(sk)) return -EOPNOTSUPP; =20 if (!bytes) @@ -3271,7 +3271,7 @@ int __sk_mem_raise_allocated(struct sock *sk, int siz= e, int amt, int kind) sk_memory_allocated_add(sk, amt); allocated =3D sk_memory_allocated(sk); =20 - if (mem_cgroup_sockets_enabled && sk->sk_memcg) { + if (mem_cgroup_sk_enabled(sk)) { memcg =3D sk->sk_memcg; charged =3D mem_cgroup_charge_skmem(memcg, amt, gfp_memcg_charge()); if (!charged) @@ -3398,7 +3398,7 @@ void __sk_mem_reduce_allocated(struct sock *sk, int a= mount) { sk_memory_allocated_sub(sk, amount); =20 - if (mem_cgroup_sockets_enabled && sk->sk_memcg) + if (mem_cgroup_sk_enabled(sk)) mem_cgroup_uncharge_skmem(sk->sk_memcg, amount); =20 if (sk_under_global_memory_pressure(sk) && diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index caf11920a878..37fb320e6f70 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -3578,7 +3578,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size) sk_forward_alloc_add(sk, amt << PAGE_SHIFT); sk_memory_allocated_add(sk, amt); =20 - if (mem_cgroup_sockets_enabled && sk->sk_memcg) + if (mem_cgroup_sk_enabled(sk)) mem_cgroup_charge_skmem(sk->sk_memcg, amt, gfp_memcg_charge() | __GFP_NOFAIL); } diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c index 6fb635a95baf..4874147e0b17 100644 --- a/net/mptcp/subflow.c +++ b/net/mptcp/subflow.c @@ -1723,7 +1723,7 @@ static void mptcp_attach_cgroup(struct sock *parent, = struct sock *child) } #endif /* CONFIG_SOCK_CGROUP_DATA */ =20 - if (mem_cgroup_sockets_enabled && parent->sk_memcg) + if (mem_cgroup_sk_enabled(parent)) mem_cgroup_sk_inherit(parent, child); } =20 --=20 2.51.0.rc0.205.g4a044479a3-goog