๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
4 messages
4 total messages Started by Eric Dumazet Wed, 18 Nov 2026 21:14
[PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()
#18765
Author: 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
Re: [PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()
#19307
Author: Willem de Bruijn
Date: Thu, 19 Nov 2026 17:59
47 lines
12901 bytes
Eric Dumazet wrote:
> 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

Just curious, any idea why no ring seems to take twice as long?
Don't spend time on it. It's just not obvious to me.

> 
> 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>

Reviewed-by: Willem de Bruijn <willemb@google.com>
Re: [PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()
#19313
Author: Eric Dumazet
Date: Thu, 19 Nov 2026 18:25
62 lines
11456 bytes
Le jeu. 19 nov. 2026 ร  18:59, Willem de Bruijn
<willemdebruijn.kernel@gmail.com> a รฉcrit :
>
> Eric Dumazet wrote:
> > 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
>
> Just curious, any idea why no ring seems to take twice as long?

Benchmark artefact: the test closes sockets in a tight loop, so
close() calls synchronize_net() while another grace period is already
in progress, and waits for the rest of that one plus a full one.

Adding a 20ms sleep before close() brings the no ring case to ~4 ms,
same as the others. In the ring cases, the PACKET_{RX,TX}_RING
setsockopt() (which also calls synchronize_net()) sits between bind()
and close(), which presumably absorbs it.

> Don't spend time on it. It's just not obvious to me.
>
> >
> > 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>
>
> Reviewed-by: Willem de Bruijn <willemb@google.com>

Thanks!
Re: [PATCH net-next] net/packet: wait for a single RCU grace period in packet_release()
#19578
Author: patchwork-bot+netdevbpf
Date: Fri, 20 Nov 2026 03:20
26 lines
8777 bytes
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Wed, 18 Nov 2026 22:14:39 +0100 you wrote:
> 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:
> 
> [...]

Here is the summary with links:
  - [net-next] net/packet: wait for a single RCU grace period in packet_release()
    https://git.kernel.org/netdev/net-next/c/2208ab00f4f2

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads