From: Eric Dumazet <edumazet@kernel.org>
To: "David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>
Cc: Simon Horman <horms@kernel.org>,
netdev@vger.kernel.org, Eric Dumazet <edumazet@kernel.org>,
Steffen Klassert <steffen.klassert@secunet.com>
Subject: [PATCH net-next] xfrm: remove three registration spinlocks
Date: Thu, 19 Nov 2026 00:07:15 +0100 [thread overview]
Message-ID: <20261118230715.1630439-1-edumazet@kernel.org> (raw)
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
reply other threads:[~2026-11-18 23:07 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261118230715.1630439-1-edumazet@kernel.org \
--to=edumazet@kernel.org \
--cc=davem@davemloft.net \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=steffen.klassert@secunet.com \
--cc=willemb@google.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