OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
From: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
To: khorenko@virtuozzo.com, vladimir.riabchun@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 1/9] ms/tcp: Save lock_sock() for memcg in inet_csk_accept().
Date: Thu, 19 Nov 2026 06:35:26 +0200	[thread overview]
Message-ID: <20261119044702.1218722-2-eva.kurchatova@virtuozzo.com> (raw)
In-Reply-To: <20261119044702.1218722-1-eva.kurchatova@virtuozzo.com>

From: Kuniyuki Iwashima <kuniyu@google.com>

If memcg is enabled, accept() acquires lock_sock() twice for each new
TCP/MPTCP socket in inet_csk_accept() and __inet_accept().

Let's move memcg operations from inet_csk_accept() to __inet_accept().

Note that SCTP somehow allocates a new socket by sk_alloc() in
sk->sk_prot->accept() and clones fields manually, instead of using
sk_clone_lock().

mem_cgroup_sk_alloc() is called for SCTP before __inet_accept(),
so I added the protocol check in __inet_accept(), but this can be
removed once SCTP uses sk_clone_lock().

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Link: https://patch.msgid.link/20251014235604.3057003-2-kuniyu@google.com
(cherry picked from commit 4a997d49d92ad9dda603f60881faa6c800d435e9)

Rebase note: this tree has neither mem_cgroup_from_sk() nor
mem_cgroup_sk_charge(), so sk->sk_memcg and mem_cgroup_charge_skmem()
are used directly. SCTP, which gets the memcg in sk_alloc(), is left
out by its protocol rather than by !sk_is_tcp(): that check would skip
the accepted MPTCP socket too, and this tree builds SCTP. Mainline
dropped the check when SCTP moved to sk_clone().

https://virtuozzo.atlassian.net/browse/VSTOR-155321
Feature: fix ms/net

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 net/ipv4/af_inet.c              | 22 ++++++++++++++++++++++
 net/ipv4/inet_connection_sock.c | 23 -----------------------
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index db683e4a50c4..5c2097f96c8f 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -760,6 +760,28 @@ EXPORT_SYMBOL(inet_stream_connect);
 
 void __inet_accept(struct socket *sock, struct socket *newsock, struct sock *newsk)
 {
+	/* SCTP sets the memcg of its accepted sockets in sk_alloc() */
+	if (mem_cgroup_sockets_enabled && newsk->sk_protocol != IPPROTO_SCTP) {
+		gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
+
+		mem_cgroup_sk_alloc(newsk);
+
+		if (newsk->sk_memcg) {
+			int amt;
+
+			/* 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);
+		}
+
+		kmem_cache_charge(newsk, gfp);
+	}
+
 	sock_rps_record_flow(newsk);
 	WARN_ON(!((1 << newsk->sk_state) &
 		  (TCPF_ESTABLISHED | TCPF_SYN_RECV |
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 96a73fc5d3d9..723737f8ae6f 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -697,30 +697,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)
 
 out:
 	release_sock(sk);
-	if (newsk && mem_cgroup_sockets_enabled) {
-		gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL;
-		int amt = 0;
 
-		/* atomically get the memory usage, set and charge the
-		 * newsk->sk_memcg.
-		 */
-		lock_sock(newsk);
-
-		mem_cgroup_sk_alloc(newsk);
-		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);
-		kmem_cache_charge(newsk, gfp);
-
-		release_sock(newsk);
-	}
 	if (req)
 		reqsk_put(req);
 
-- 
2.56.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  reply	other threads:[~2026-11-19  4:48 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  4:35 [Devel] [PATCH hci-8.2 0/9] ms/mptcp: backport fixes for dropped stream segments Eva Kurchatova
2026-11-19  4:35 ` Eva Kurchatova [this message]
2026-11-19  4:53   ` [Devel] [PATCH hci-8.2 1/9] ms/tcp: Save lock_sock() for memcg in inet_csk_accept() no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 2/9] ms/mptcp: Fix up subflow's memcg when CONFIG_SOCK_CGROUP_DATA=n Eva Kurchatova
2026-11-19  4:52   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 3/9] ms/net: factor-out _sk_charge() helper Eva Kurchatova
2026-11-19  4:53   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 4/9] ms/mptcp: factor-out cgroup data inherit helper Eva Kurchatova
2026-11-19  4:54   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 5/9] ms/mptcp: grafting MPJ subflow earlier Eva Kurchatova
2026-11-19  4:59   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 6/9] ms/mptcp: fix memcg accounting for passive sockets Eva Kurchatova
2026-11-19  4:58   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 7/9] ms/mptcp: handle first subflow closing consistently Eva Kurchatova
2026-11-19  4:59   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 8/9] ms/mptcp: borrow forward memory from subflow Eva Kurchatova
2026-11-19  5:03   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19  4:35 ` [Devel] [PATCH hci-8.2 9/9] ms/mptcp: reclaim forward-allocated memory on RX path errors Eva Kurchatova
2026-11-19  5:01   ` no-reply
2026-11-19 17:08   ` Konstantin Khorenko
2026-11-19 17:08 ` [Devel] [PATCH hci-8.2 0/9] ms/mptcp: backport fixes for dropped stream segments Konstantin Khorenko
2026-11-19 17:08 ` Konstantin Khorenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119044702.1218722-2-eva.kurchatova@virtuozzo.com \
    --to=eva.kurchatova@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    --cc=vladimir.riabchun@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox