Skip to contentSkip to conversation

udp_tunnel: Speed up UDP tunnel device destruction (Part II)

PATCH Applied Kuniyuki Iwashima 2 participants 11 messages May 18 – May 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover udp_tunnel: Speed up UDP tunnel device destruction (Part II)

Most of the UDP tunnel devices called synchronize_rcu() twice during destruction, for example, vxlan had

1) synchronize_rcu() in udp_tunnel_sock_release()

2) synchronize_net() in vxlan_sock_release()

The former was already removed by:
https://lore.kernel.org/netdev/[email protected]/

This series removes the latter.

Patch 1 & 2 & 4 remove synchronize_net() placed before udp_tunnel_sock_release().

Patch 3 removes yet another unnecessary synchronize_net() in geneve_unquiesce().

Patch 5 is a follow-up patch for a sparse report by kernel test robot.

Kuniyuki Iwashima (5):
vxlan: Remove synchronize_net() in vxlan_sock_release().
geneve: Remove synchronize_net() in geneve_sock_release().
geneve: Remove synchronize_net() in geneve_unquiesce().
bareudp: Remove synchronize_net() in bareudp_sock_release().
bareudp: Use rtnl_dereference() in bareudp_sock_release().

 drivers/net/bareudp.c          | 3 +--
 drivers/net/geneve.c           | 2 --
 drivers/net/vxlan/vxlan_core.c | 1 -
 3 files changed, 1 insertion(+), 5 deletions(-)
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#1 lore raw

PATCH 1/5 vxlan: Remove synchronize_net() in vxlan_sock_release().

Initially, a dedicated workqueue was used to defer calling udp_tunnel_sock_release(vxlan_sock->sock) and kfree(vxlan_sock).

Later, commit 0412bd931f5f ("vxlan: synchronously and race-free destruction of vxlan sockets") removed the workqueue and instead invoked these two functions immediately after synchronize_net().

This was intended to prevent UAF of the UDP socket in the fast path.

( Note that the "nondeterministic behaviour" mentioned in that
commit was not addressed, as another thread not waiting RCU gp still sees the same behaviour. )

However, a week prior to that change, commit ca065d0cf80f ("udp:
no longer use SLAB_DESTROY_BY_RCU") had already moved UDP socket freeing to after the RCU grace period. This made the synchronize_net() in vxlan_sock_release() completely redundant.

Since vxlan_sock now uses kfree_rcu() and is invoked after udp_tunnel_sock_release(), vxlan_sock is guaranteed to be freed either at the same time or after the UDP socket is released, following the RCU grace period.

Let's remove the redundant synchronize_net() in vxlan_sock_release().

---
 drivers/net/vxlan/vxlan_core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/vxlan/vxlan_core.c +0 -1
index 00facbfabced..8c3885665b58 100644
@@ -1515,7 +1515,6 @@ static void vxlan_sock_release(struct vxlan_dev *vxlan)
 #endif
 
 	RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
-	synchronize_net();
 
 	if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
 		vxlan_vs_del_vnigrp(vxlan);
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#2 lore raw

Re: [PATCH v1 net-next 1/5] vxlan: Remove synchronize_net() in vxlan_sock_release().

For Sashiko review:

Quoted 19 lines · Kuniyuki Iwashima

Initially, a dedicated workqueue was used to defer calling udp_tunnel_sock_release(vxlan_sock->sock) and kfree(vxlan_sock).

Later, commit 0412bd931f5f ("vxlan: synchronously and race-free destruction of vxlan sockets") removed the workqueue and instead invoked these two functions immediately after synchronize_net().

This was intended to prevent UAF of the UDP socket in the fast path.

( Note that the "nondeterministic behaviour" mentioned in that
commit was not addressed, as another thread not waiting RCU gp still sees the same behaviour. )

However, a week prior to that change, commit ca065d0cf80f ("udp:
no longer use SLAB_DESTROY_BY_RCU") had already moved UDP socket freeing to after the RCU grace period. This made the synchronize_net() in vxlan_sock_release() completely redundant.

Quoted 4 lines

Since vxlan_sock now uses kfree_rcu() and is invoked after udp_tunnel_sock_release(), vxlan_sock is guaranteed to be freed either at the same time or after the UDP socket is released, following the RCU grace period.

---8<---
Is this reasoning complete? The commit message notes that the socket memory is freed after the RCU grace period, but does this account for references taken inside the RCU read-side critical section that escape it? In the transmit path, vxlan_xmit() accesses vn4_sock inside an rcu_read_lock() block and passes the socket to udp_tunnel_xmit_skb(). This eventually calls skb_set_owner_w(), which increments sk->sk_wmem_alloc to keep the socket memory alive until the packet transmission completes. ---8<---

The skb_set_owner_w() Sashiko mentions is the one called for a fragmented skb in ip_do_fragment().

In this case, the original skb still holds the same amount of sk->sk_wmem_alloc and prevents sk from being freed, so skb_set_owner_w() there is safe without synchronize_net().

Quoted 13 lines

Let's remove the redundant synchronize_net() in vxlan_sock_release().

---
 drivers/net/vxlan/vxlan_core.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/vxlan/vxlan_core.c +0 -0
index 00facbfabced..8c3885665b58 100644

drivers/net/vxlan/vxlan_core.c static void vxlan_sock_release(struct vxlan_dev *vxlan) · line 1520

Quoted 7 lines
drivers/net/vxlan/vxlan_core.c +0 -1
@@ -1515,7 +1515,6 @@ static void vxlan_sock_release(struct vxlan_dev *vxlan)
 #endif
 
        RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
-       synchronize_net();
 
        if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
                vxlan_vs_del_vnigrp(vxlan);

---8<---
Does removing this synchronize_net() introduce a use-after-free regression when tearing down the socket while packets are concurrently transmitting? Without synchronize_net(), udp_tunnel_sock_release() runs immediately after setting vn4_sock to NULL. If sk_refcnt reaches zero, sk_free() is queued.
...
---8<---

Same.

#3 lore raw

PATCH 2/5 geneve: Remove synchronize_net() in geneve_sock_release().

In reply to Kuniyuki Iwashima

vxlan previously had an issue where the fast path could access stale pointers, which was fixed by commit c6fcc4fc5f8b ("vxlan:
avoid using stale vxlan socket.").

geneve later followed the same pattern, and commit fceb9c3e3825 ("geneve: avoid using stale geneve socket.") copied synchronize_net() from vxlan_sock_release() into geneve_sock_release().

However, that change occurred after commit ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU"), and geneve had already been using kfree_rcu() to free geneve_sock.

Therefore, the synchronize_net() was never actually needed there.

Let's remove the redundant synchronize_net() in geneve_sock_release().

---
 drivers/net/geneve.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/geneve.c +0 -1
index b36fad833724..4e5d0a09a82d 100644
@@ -1032,7 +1032,6 @@ static void geneve_sock_release(struct geneve_dev *geneve)
 #endif
 
 	rcu_assign_pointer(geneve->sock4, NULL);
-	synchronize_net();
 
 	__geneve_sock_release(gs4);
 #if IS_ENABLED(CONFIG_IPV6)
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#4 lore raw

Re: [PATCH v1 net-next 2/5] geneve: Remove synchronize_net() in geneve_sock_release().

For Sashiko review:

Quoted 27 lines · Kuniyuki Iwashima

vxlan previously had an issue where the fast path could access stale pointers, which was fixed by commit c6fcc4fc5f8b ("vxlan:
avoid using stale vxlan socket.").

geneve later followed the same pattern, and commit fceb9c3e3825 ("geneve: avoid using stale geneve socket.") copied synchronize_net() from vxlan_sock_release() into geneve_sock_release().

However, that change occurred after commit ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU"), and geneve had already been using kfree_rcu() to free geneve_sock.

Therefore, the synchronize_net() was never actually needed there.

Let's remove the redundant synchronize_net() in geneve_sock_release().

---
 drivers/net/geneve.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/geneve.c +0 -0
index b36fad833724..4e5d0a09a82d 100644

drivers/net/geneve.c static void geneve_sock_release(struct geneve_dev *geneve) · line 1036

Quoted 6 lines
drivers/net/geneve.c +0 -1
@@ -1032,7 +1032,6 @@ static void geneve_sock_release(struct geneve_dev *geneve)
 #endif
 
        rcu_assign_pointer(geneve->sock4, NULL);
-       synchronize_net();
 
        __geneve_sock_release(gs4);

---8<---
Does this function leave the device's hlist node linked in the socket's vni_list?
In geneve_open(), if the IPv6 socket setup succeeds, the device's hlist6 node is linked into the IPv6 socket's vni_list via hlist_add_head_rcu(). If the subsequent IPv4 socket setup fails, it calls geneve_sock_release() and returns an error.
While geneve_sock_release() drops the socket refcount, it doesn't remove the hlist node. Because geneve_open() failed, the interface never gets the IFF_UP flag. When the interface is subsequently deleted, dev_close() bypasses ndo_stop (geneve_stop()), which is where the hlist removal is normally handled.
If the IPv6 socket is shared and remains active, can concurrent RX traffic trigger a use-after-free when geneve6_lookup() iterates the vni_list and accesses the freed geneve_dev?
---8<---

pre-existing issue in geneve_open(), so orthogonal to this patch. I'll follow up on net.git separately.

#5 lore raw

Re: [PATCH v1 net-next 2/5] geneve: Remove synchronize_net() in geneve_sock_release().

Quoted 55 lines · Kuniyuki Iwashima

For Sashiko review:

Quoted 27 lines · Kuniyuki Iwashima

vxlan previously had an issue where the fast path could access stale pointers, which was fixed by commit c6fcc4fc5f8b ("vxlan:
avoid using stale vxlan socket.").

geneve later followed the same pattern, and commit fceb9c3e3825 ("geneve: avoid using stale geneve socket.") copied synchronize_net() from vxlan_sock_release() into geneve_sock_release().

However, that change occurred after commit ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU"), and geneve had already been using kfree_rcu() to free geneve_sock.

Therefore, the synchronize_net() was never actually needed there.

Let's remove the redundant synchronize_net() in geneve_sock_release().

---
 drivers/net/geneve.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/geneve.c +0 -0
index b36fad833724..4e5d0a09a82d 100644

drivers/net/geneve.c static void geneve_sock_release(struct geneve_dev *geneve) · line 1036

Quoted 6 lines
drivers/net/geneve.c +0 -1
@@ -1032,7 +1032,6 @@ static void geneve_sock_release(struct geneve_dev *geneve)
 #endif
 
        rcu_assign_pointer(geneve->sock4, NULL);
-       synchronize_net();
 
        __geneve_sock_release(gs4);

---8<---
Does this function leave the device's hlist node linked in the socket's vni_list?
In geneve_open(), if the IPv6 socket setup succeeds, the device's hlist6 node is linked into the IPv6 socket's vni_list via hlist_add_head_rcu(). If the subsequent IPv4 socket setup fails, it calls geneve_sock_release() and returns an error.
While geneve_sock_release() drops the socket refcount, it doesn't remove the hlist node. Because geneve_open() failed, the interface never gets the IFF_UP flag. When the interface is subsequently deleted, dev_close() bypasses ndo_stop (geneve_stop()), which is where the hlist removal is normally handled.
If the IPv6 socket is shared and remains active, can concurrent RX traffic trigger a use-after-free when geneve6_lookup() iterates the vni_list and accesses the freed geneve_dev?
---8<---

Quoted 2 lines

pre-existing issue in geneve_open(), so orthogonal to this patch. I'll follow up on net.git separately.

This was a false positive since geneve creates both IPv4/IPv6 sockets only for collect-metadata mode, but it does not allow sharing sockets.

#6 lore raw

PATCH 3/5 geneve: Remove synchronize_net() in geneve_unquiesce().

In reply to Kuniyuki Iwashima

When changing the geneve config, geneve_changelink() sandwiches the config memcpy() between geneve_quiesce() and geneve_unquiesce().

geneve_quiesce() temporarily clears geneve->sock[46] and their sk_user_data, and then calls synchronize_net() to wait for inflight fast paths to finish.

geneve_unquiesce() then restores the cleared pointers, but it also superfluously calls synchronize_net().

The latter synchronize_net() provides no benefit; with or without it, inflight fast paths can see either the NULL pointers or the original pointers alongside the new configuration.

Let's remove the redundant synchronize_net() in geneve_unquiesce().

---
 drivers/net/geneve.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/geneve.c +0 -1
index 4e5d0a09a82d..e8ff03ed87dc 100644
@@ -2206,7 +2206,6 @@ static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
 	if (gs6)
 		rcu_assign_sk_user_data(gs6->sk, gs6);
 #endif
-	synchronize_net();
 }
 
 static int geneve_changelink(struct net_device *dev, struct nlattr *tb[],
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#7 lore raw

Re: [PATCH v1 net-next 3/5] geneve: Remove synchronize_net() in geneve_unquiesce().

Quoted 27 lines · Kuniyuki Iwashima

When changing the geneve config, geneve_changelink() sandwiches the config memcpy() between geneve_quiesce() and geneve_unquiesce().

geneve_quiesce() temporarily clears geneve->sock[46] and their sk_user_data, and then calls synchronize_net() to wait for inflight fast paths to finish.

geneve_unquiesce() then restores the cleared pointers, but it also superfluously calls synchronize_net().

The latter synchronize_net() provides no benefit; with or without it, inflight fast paths can see either the NULL pointers or the original pointers alongside the new configuration.

Let's remove the redundant synchronize_net() in geneve_unquiesce().

---
 drivers/net/geneve.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/geneve.c +0 -0
index 4e5d0a09a82d..e8ff03ed87dc 100644

drivers/net/geneve.c static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4, · line 2209

Quoted 4 lines
drivers/net/geneve.c +0 -1
@@ -2206,7 +2206,6 @@ static void geneve_unquiesce(struct geneve_dev *geneve, struct geneve_sock *gs4,
        if (gs6)
                rcu_assign_sk_user_data(gs6->sk, gs6);
 #endif
-       synchronize_net();

---8<---
Can the fast path read a torn geneve->cfg during geneve_changelink()?
...
---8<---

Yes, it's a pre-existing issue, and probably that's why vxlan does not have even a function like geneve_quiesce() and accepts data-race during changelink.

#8 lore raw

PATCH 4/5 bareudp: Remove synchronize_net() in bareudp_sock_release().

In reply to Kuniyuki Iwashima

synchronize_net() in bareudp_sock_release() has existed since day 1, commit 571912c69f0e ("net: UDP tunnel encapsulation module for tunnelling different protocols like MPLS, IP, NSH etc.").

It was most likely copied from a similar tunneling device like vxlan or geneve.

bareudp_sock_release() is called from dev->netdev_ops->ndo_stop(), and synchronize_net() in unregister_netdevice_many_notify() ensures that inflight bareudp fast paths finish before bareudp_dev is freed.

Let's remove the redundant synchronize_net() in bareudp_sock_release().

---
 drivers/net/bareudp.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/bareudp.c +0 -1
index 890a0650d9cf..bfcecdcffe67 100644
@@ -287,7 +287,6 @@ static void bareudp_sock_release(struct bareudp_dev *bareudp)
 
 	sk = bareudp->sk;
 	rcu_assign_pointer(bareudp->sk, NULL);
-	synchronize_net();
 	udp_tunnel_sock_release(sk);
 }
 
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#9 lore raw

PATCH 5/5 bareudp: Use rtnl_dereference() in bareudp_sock_release().

In reply to Kuniyuki Iwashima

kernel test robot reported sparse warning in bareudp_sock_release():

drivers/net/bareudp.c:288:12: warning: incorrect type in assignment (different address spaces)
drivers/net/bareudp.c:288:12: expected struct sock *sk
drivers/net/bareudp.c:288:12: got struct sock [noderef] __rcu *sk

The warning is not new and exists since the initial bareudp commit 571912c69f0e ("net: UDP tunnel encapsulation module for tunnelling different protocols like MPLS, IP, NSH etc.").

Let's use rtnl_dereference().

Note that bareudp_sock_release() is called from bareudp_stop() under RTNL, so there is no real issue even without the helper.

---
I didn't add Fixes tag so we don't bother resolving conflicts for harmless warning.

---
 drivers/net/bareudp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/bareudp.c +1 -1
index bfcecdcffe67..5ef841c85526 100644
@@ -285,7 +285,7 @@ static void bareudp_sock_release(struct bareudp_dev *bareudp)
 {
 	struct sock *sk;
 
-	sk = bareudp->sk;
+	sk = rtnl_dereference(bareudp->sk);
 	rcu_assign_pointer(bareudp->sk, NULL);
 	udp_tunnel_sock_release(sk);
 }
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
#10 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Mon, 18 May 2026 05:07:07 +0000 you wrote:

Most of the UDP tunnel devices called synchronize_rcu() twice during destruction, for example, vxlan had

1) synchronize_rcu() in udp_tunnel_sock_release()

2) synchronize_net() in vxlan_sock_release()

[...]

Here is the summary with links:
- [v1,net-next,1/5] vxlan: Remove synchronize_net() in vxlan_sock_release().

    https://git.kernel.org/netdev/net-next/c/425e30577f17
  - [v1,net-next,2/5] geneve: Remove synchronize_net() in geneve_sock_release().
    https://git.kernel.org/netdev/net-next/c/2f7f86db1675
  - [v1,net-next,3/5] geneve: Remove synchronize_net() in geneve_unquiesce().
    https://git.kernel.org/netdev/net-next/c/567dbca22b37
  - [v1,net-next,4/5] bareudp: Remove synchronize_net() in bareudp_sock_release().
    https://git.kernel.org/netdev/net-next/c/d38be9217277
  - [v1,net-next,5/5] bareudp: Use rtnl_dereference() in bareudp_sock_release().
    https://git.kernel.org/netdev/net-next/c/e6409584dec6

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html