🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Eric Dumazet Wed, 18 Nov 2026 23:07
[PATCH net-next] xfrm: remove three registration spinlocks
#18793
Author: Eric Dumazet
Date: Wed, 18 Nov 2026 23:07
168 lines
12796 bytes
xfrm_state_afinfo[], xfrm_translator and xfrm_mode_cbs_map[] are
RCU protected pointers which are only written when a module or an
address family registers or unregisters itself. All their readers
already use rcu_read_lock() and rcu_dereference(), and the unregister
functions already call synchronize_rcu().

Each of them has a dedicated spinlock which only serializes these
rare writers:

- xfrm_state_afinfo_lock and xfrm_translator_lock make "install if
  the slot is empty" and "clear if the slot is ours" atomic. Use
  cmpxchg() instead. The return values are unchanged: -EEXIST if the
  slot is already used at registration, -EINVAL when unregistering
  while another object is installed, 0 otherwise.

- xfrm_mode_cbs_map_lock protects nothing: registration overwrites
  the slot and unregistration clears it, both unconditionally. Remove
  it, the existing behavior is kept.

Tested with the xfrm_policy.sh and xfrm_state.sh selftests. The
translator code was only compile tested, with
CONFIG_XFRM_USER_COMPAT=y, and the unregister paths were not
exercised. sparse (C=2) reports no warning.

Signed-off-by: Eric Dumazet <edumazet@kernel.org>
Cc: Steffen Klassert <steffen.klassert@secunet.com>
---
 net/xfrm/xfrm_state.c | 60 ++++++++++++++-----------------------------
 1 file changed, 19 insertions(+), 41 deletions(-)

diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index 0555e5d796ede417ccaca21c998a0434f7146a21..0c02a5f06225f650c7e45a5399cf828769fd455f 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -221,7 +221,6 @@ static void xfrm_hash_resize(struct work_struct *work)
 	xfrm_hash_free(oseq, osize);
 }
 
-static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
 static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
 
 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
@@ -534,16 +533,13 @@ static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
 }
 
 static const struct xfrm_mode_cbs  __rcu *xfrm_mode_cbs_map[XFRM_MODE_MAX];
-static DEFINE_SPINLOCK(xfrm_mode_cbs_map_lock);
 
 int xfrm_register_mode_cbs(u8 mode, const struct xfrm_mode_cbs *mode_cbs)
 {
 	if (mode >= XFRM_MODE_MAX)
 		return -EINVAL;
 
-	spin_lock_bh(&xfrm_mode_cbs_map_lock);
 	rcu_assign_pointer(xfrm_mode_cbs_map[mode], mode_cbs);
-	spin_unlock_bh(&xfrm_mode_cbs_map_lock);
 
 	return 0;
 }
@@ -554,9 +550,7 @@ void xfrm_unregister_mode_cbs(u8 mode)
 	if (mode >= XFRM_MODE_MAX)
 		return;
 
-	spin_lock_bh(&xfrm_mode_cbs_map_lock);
 	RCU_INIT_POINTER(xfrm_mode_cbs_map[mode], NULL);
-	spin_unlock_bh(&xfrm_mode_cbs_map_lock);
 	synchronize_rcu();
 }
 EXPORT_SYMBOL(xfrm_unregister_mode_cbs);
@@ -3017,7 +3011,6 @@ static bool km_is_alive(const struct km_event *c)
 }
 
 #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
-static DEFINE_SPINLOCK(xfrm_translator_lock);
 static struct xfrm_translator __rcu *xfrm_translator;
 
 struct xfrm_translator *xfrm_get_translator(void)
@@ -3044,31 +3037,22 @@ EXPORT_SYMBOL_GPL(xfrm_put_translator);
 
 int xfrm_register_translator(struct xfrm_translator *xtr)
 {
-	int err = 0;
-
-	spin_lock_bh(&xfrm_translator_lock);
-	if (unlikely(xfrm_translator != NULL))
-		err = -EEXIST;
-	else
-		rcu_assign_pointer(xfrm_translator, xtr);
-	spin_unlock_bh(&xfrm_translator_lock);
+	if (cmpxchg(&xfrm_translator, NULL, RCU_INITIALIZER(xtr)))
+		return -EEXIST;
 
-	return err;
+	return 0;
 }
 EXPORT_SYMBOL_GPL(xfrm_register_translator);
 
 int xfrm_unregister_translator(struct xfrm_translator *xtr)
 {
+	struct xfrm_translator __rcu *old;
 	int err = 0;
 
-	spin_lock_bh(&xfrm_translator_lock);
-	if (likely(xfrm_translator != NULL)) {
-		if (rcu_access_pointer(xfrm_translator) != xtr)
-			err = -EINVAL;
-		else
-			RCU_INIT_POINTER(xfrm_translator, NULL);
-	}
-	spin_unlock_bh(&xfrm_translator_lock);
+	old = cmpxchg(&xfrm_translator, RCU_INITIALIZER(xtr), NULL);
+	if (old && old != RCU_INITIALIZER(xtr))
+		err = -EINVAL;
+
 	synchronize_rcu();
 
 	return err;
@@ -3156,36 +3140,30 @@ EXPORT_SYMBOL(xfrm_unregister_km);
 
 int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
 {
-	int err = 0;
-
 	if (WARN_ON(afinfo->family >= NPROTO))
 		return -EAFNOSUPPORT;
 
-	spin_lock_bh(&xfrm_state_afinfo_lock);
-	if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
-		err = -EEXIST;
-	else
-		rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
-	spin_unlock_bh(&xfrm_state_afinfo_lock);
-	return err;
+	if (cmpxchg(&xfrm_state_afinfo[afinfo->family], NULL,
+		    RCU_INITIALIZER(afinfo)))
+		return -EEXIST;
+
+	return 0;
 }
 EXPORT_SYMBOL(xfrm_state_register_afinfo);
 
 int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
 {
+	struct xfrm_state_afinfo __rcu *old;
 	int err = 0, family = afinfo->family;
 
 	if (WARN_ON(family >= NPROTO))
 		return -EAFNOSUPPORT;
 
-	spin_lock_bh(&xfrm_state_afinfo_lock);
-	if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
-		if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
-			err = -EINVAL;
-		else
-			RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
-	}
-	spin_unlock_bh(&xfrm_state_afinfo_lock);
+	old = cmpxchg(&xfrm_state_afinfo[family], RCU_INITIALIZER(afinfo),
+		      NULL);
+	if (old && old != RCU_INITIALIZER(afinfo))
+		err = -EINVAL;
+
 	synchronize_rcu();
 	return err;
 }
-- 
2.53.0
Re: [PATCH net-next] xfrm: remove three registration spinlocks
#22915
Author: Steffen Klassert
Date: Thu, 26 Nov 2026 06:31
30 lines
10563 bytes
On Thu, Nov 19, 2026 at 12:07:15AM +0100, Eric Dumazet wrote:
> xfrm_state_afinfo[], xfrm_translator and xfrm_mode_cbs_map[] are
> RCU protected pointers which are only written when a module or an
> address family registers or unregisters itself. All their readers
> already use rcu_read_lock() and rcu_dereference(), and the unregister
> functions already call synchronize_rcu().
> 
> Each of them has a dedicated spinlock which only serializes these
> rare writers:
> 
> - xfrm_state_afinfo_lock and xfrm_translator_lock make "install if
>   the slot is empty" and "clear if the slot is ours" atomic. Use
>   cmpxchg() instead. The return values are unchanged: -EEXIST if the
>   slot is already used at registration, -EINVAL when unregistering
>   while another object is installed, 0 otherwise.
> 
> - xfrm_mode_cbs_map_lock protects nothing: registration overwrites
>   the slot and unregistration clears it, both unconditionally. Remove
>   it, the existing behavior is kept.
> 
> Tested with the xfrm_policy.sh and xfrm_state.sh selftests. The
> translator code was only compile tested, with
> CONFIG_XFRM_USER_COMPAT=y, and the unregister paths were not
> exercised. sparse (C=2) reports no warning.
> 
> Signed-off-by: Eric Dumazet <edumazet@kernel.org>
> Cc: Steffen Klassert <steffen.klassert@secunet.com>

Applied to ipsec-next, thanks a lot Eric!
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