🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #18765

[PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()

#18765
From: Eric Dumazet
Date: Wed, 18 Nov 2026 21:14
88 lines
10455 bytes
Closing an AF_PACKET socket waits for one RCU grace period per ring
in packet_set_ring(), then for another one at the end of
packet_release(). A socket with an rx ring (libpcap, tcpdump) pays for
two grace periods, a socket with both rx and tx rings for three.

All the RCU unpublishing is done at the beginning of packet_release(),
when unregister_prot_hook() removes the socket from the ptype lists or
from its fanout group. Nothing is unpublished after that point:

- When closing, packet_set_ring() finds the socket not running and
  does not touch the hooks.

- fanout_release() only clears po->fanout and unlinks the group from
  fanout_list, both under fanout_mutex. If it drops the last reference
  on the group, all members have already been unlinked, so the fanout
  hook was removed by __fanout_unlink().

Call fanout_release() and synchronize_net() right after the socket has
been detached, before the rings are torn down, and skip the wait in
packet_set_ring() when closing. That single grace period covers the
rings, po->rollover and the fanout group.

The setsockopt() path of packet_set_ring() is unchanged.

Average close() duration over 20 sockets bound to lo, in a 4 vCPU
virtme-ng guest:

                         before      after
  no ring                7.86 ms    7.76 ms
  rx ring                8.01 ms    4.01 ms
  tx ring                8.00 ms    4.00 ms
  rx + tx rings         11.96 ms    4.00 ms
  rx + tx rings, fanout 12.02 ms    4.01 ms

Tested with tools/testing/selftests/net/run_afpackettests
(psock_fanout, psock_tpacket, txring_overwrite) on a kernel without
KASAN or lockdep.

Signed-off-by: Eric Dumazet <edumazet@kernel.org>
---
 net/packet/af_packet.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 6ace2a5b2d177e3de89361a3092536a8f6c5e00a..96c5216a9d6b47700da682d864368eead8572297 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -3224,6 +3224,15 @@ static int packet_release(struct socket *sock)
 
 	packet_flush_mclist(sk);
 
+	f = fanout_release(sk);
+
+	/* The socket is no longer reachable from the receive path:
+	 * unregister_prot_hook() removed it from the ptype lists and
+	 * from its fanout group, if any. Wait once for packets currently
+	 * being received, this covers the rings, po->rollover and @f.
+	 */
+	synchronize_net();
+
 	lock_sock(sk);
 	if (po->rx_ring.pg_vec) {
 		memset(&req_u, 0, sizeof(req_u));
@@ -3236,10 +3245,6 @@ static int packet_release(struct socket *sock)
 	}
 	release_sock(sk);
 
-	f = fanout_release(sk);
-
-	synchronize_net();
-
 	kfree(po->rollover);
 	if (f) {
 		fanout_release_data(f);
@@ -4674,7 +4679,9 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
 
 	spin_unlock(&po->bind_lock);
 
-	synchronize_net();
+	/* packet_release() already detached the socket and waited. */
+	if (!closing)
+		synchronize_net();
 
 	err = -EBUSY;
 	mutex_lock(&po->pg_vec_lock);
-- 
2.53.0
Technical Details
Message-ID: <20261118211439.1453635-1-edumazet@kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366464-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-366464-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()
Date: Wed, 18 Nov 2026 22:14:39 +0100
Organization: Usenet Network
Message-ID: <20261118211439.1453635-1-edumazet@kernel.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2700801"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:dny7/8y4dYBPfQOwdpZmKsfD+Rw= sha256:SpRw0gvxWa3YERNQXnxp/GnwEnUbcWf79nvd1f2nd2A=
	sha1:+TZROjd4pPIb29FmDytoEguwamE= sha256:CWt7Sh8ssVafSm6AC9m5b1HiScWWsJ1+8ToO4DJ02WA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 8A8B31153E1; Thu, 19 Nov 2026 00:15:11 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id LN3coJUK6n0L;
	Thu, 19 Nov 2026 00:15:08 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-366464-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id A22A8115387; Thu, 19 Nov 2026 00:14:58 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id 6D4AFE7475; Wed, 18 Nov 2026 21:14:54 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795036493;
	b=kcXZqwUxwiHRzc5RB5wpzSettz9EghDFg+3isEpZ61FXk0bJDppvcGkhZGzoVwEx5nSU
	 XVTUy9qK5a7i8OLhITYBf5yAU+xjCxto3GTQGEGGpG9mJh+7haqXm2XuklzsfUivjhX9g
	 1nnVOH1bclembId6Mn5D15+uKs7QVZdF9R1GuPlA56Wyec9xeaE4qt7vYBo/iR/9p+cU5
	 7SUSjgmfkoXCHdKiSEqWnA9wWgQU1VwjBRbq5bk5PJL134lHN0ScvQr73cVqZXyIue7fC
	 OjrGyO6XaPvHAfJWXgX6jthk3SQBKuUdlRCjKlpd3Cbn1Em71PUdFr6GWEiZT/i3SWw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795036493;
	h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=IRN8e1YgeSGzhUuFsD2VLCnpoWk3n4qAeM580YrGeI4=;
	b=GDOemJS2qJ5JIsz+xrsPj1CU8eXXwREIV7FDYhGDLU7cpfxhHTAMmpu34UvL6HGRPaWy
	 rkyNy7kO9zVtfpU7tEbnf/cjbXEdlS9wBcq3ofP5aJqPHuTKgVbYGP9jMznk3QPjpBzCK
	 0N0+WZT7bRmvpJ0/6IUmsc72QLd4bhVV4dBVZFxDnPf3G+PJfJQBp2lEF8yixEdJr6pjc
	 I1f/3+QJyflTRjtOTbpaXmmd/hCejvP1lba8zO0sft68xc8KjoBBMfR6AZkVi0RdCPyJy
	 eqYiVCc+VSHzoB1WzU0gUgnUzX2XCdZyalUImYrOZxN3LrCWkwVBPABIzlI2y4x9WXQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZiA9haQ4;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 618C1606A336;
	Wed, 18 Nov 2026 21:14:53 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZiA9haQ4
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795036492;
	b=1V78/yRdqMWkx1WIsd7bnGmKgjcqfEQV3rR6tbDuUUKpcZf++DX1ozwWaZ74oR71dVVa
	 Rom3Z93Z5e+Wk2TdsT/w2VPiB/MDnu5GRWnS7kol9DW624dthDV1P3r1HL7c37uB0fvc0
	 oadWr5bOLpJ0q+3HlXFfRXVvhDjV6YKHLuFdk2PNY84Z7tXGtS6BILzcSK9HkSUk3U3V4
	 bvwAJqu9sZEUCNlQB26nlgCqDRmLt9YbljDnax7zJcaVjH997raoC6R4pENymk5AIu6cC
	 kq4mVw5dx+O5z5vZ2LQHq1lzXkzdePC5PAzL35dI09PtOOSJtS+BxZgB4FVSahZ4h5A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795036492;
	h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer:
	 MIME-Version:Content-Transfer-Encoding;
	bh=IRN8e1YgeSGzhUuFsD2VLCnpoWk3n4qAeM580YrGeI4=;
	b=JpepUGtP2Sv5lYI7daoRF57v7nCqt4cdqsjNlFjby1Cl+TdUmgeulytXShKLMc6VKepL
	 /m/Au80IceM6tA7Hx2bMWqx8d9rBsPWZVn56wY6v3L3Yf4qPg1uN9Xauh/CXX3dfGo6ma
	 +4eEKuFB7p6QCJLBGxdkQU5YV7Gloslhj/Ss7tNLVGG0IgSiw7MD0IfdBYzGUA8EUYnMi
	 jxy3GqN6KiZCNR7kXdibrJaKSQ3xdDrrAgMtTw71rAZ1UoDQc27t/reTjIOaF7S7oHvTR
	 jYJlrkAGReLwpZ56U2nzHlHrvJ5DPxNV8zk4JARKJyJfauP95+j/Ir/fac2FSN5da+Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZiA9haQ4;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D02C606A31C
	for <netdev@vger.kernel.org>; Wed, 18 Nov 2026 21:14:52 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF3301F000C4;
	Wed, 18 Nov 2026 21:14:50 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795036492;
	bh=IRN8e1YgeSGzhUuFsD2VLCnpoWk3n4qAeM580YrGeI4=;
	h=From:To:Cc:Subject:Date;
	b=ZiA9haQ4tMQKr8lYRgQ3BeMsgjR4ai24W4NxEIkuZ7EyAv6CcsWG9FioicltCbMll
	 2VoXCRYt9qh4ixjtsEYlg0FAoLIcPPKbfkufufr+CnETNblNsLRN6LNFdPolZG0pIj
	 B5fpXsCozRL0qSxe1iUqG++4BKcnReyAbQXxd8bCbPlHmdzM1cnIbieDeHp3jzFElw
	 2SX2jdwzVqsEjHGyQBMJrOHzBa0PePcHNPS0Wvzg2SMw3m0BxGBMF65GgtOnud6L/+
	 zbahE8ybS7ruc9fsTaLbbm5W0Ex+L4/Pj39wkymMMFGtnpUecorvedcjzqFd+YKCrY
	 euDydSniDeoJw==
X-Cc: Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org,
	Eric Dumazet <edumazet@kernel.org>
X-Mailer: git-send-email 2.53.0
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list