[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250207210248.qy4i5Wkl@linutronix.de>
Date: Fri, 7 Feb 2025 22:02:48 +0100
From: Sebastian Andrzej Siewior <bigeasy@...utronix.de>
To: netdev@...r.kernel.org
Cc: Steffen Klassert <steffen.klassert@...unet.com>,
Herbert Xu <herbert@...dor.apana.org.au>,
"David S. Miller" <davem@...emloft.net>,
Thomas Gleixner <tglx@...utronix.de>
Subject: [PATCH net-next] xfrm: Use rcuref_t for xfrm_state' reference
counting.
xfrm_state::ref is used for reference counting of xfrm_state itself. The
structure already follows RCU rules for its lifetime.
Using rcuref_t instead of refcount_t has the advantage that
refcount_inc_not_zero() can be replaced with a regular rcuref_get()
which does not suffer from a cmpxchg() loop. The cmpxchg() loop might be
repeated several times if there are puts or gets in parallel.
Replace xfrm_state's reference counting with rcuref_t. The
__xfrm_state_put() and xfrm_state_hold() which should always succeed
have a warning in case they don't succeed.
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@...utronix.de>
---
include/net/xfrm.h | 12 ++++++------
net/xfrm/xfrm_state.c | 4 ++--
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index ed4b83696c77f..3da13b8a5afda 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -14,7 +14,7 @@
#include <linux/mutex.h>
#include <linux/audit.h>
#include <linux/slab.h>
-#include <linux/refcount.h>
+#include <linux/rcuref.h>
#include <linux/sockptr.h>
#include <net/sock.h>
@@ -189,7 +189,7 @@ struct xfrm_state {
struct hlist_node state_cache;
struct hlist_node state_cache_input;
- refcount_t refcnt;
+ rcuref_t ref;
spinlock_t lock;
u32 pcpu_num;
@@ -901,24 +901,24 @@ void __xfrm_state_destroy(struct xfrm_state *, bool);
static inline void __xfrm_state_put(struct xfrm_state *x)
{
- refcount_dec(&x->refcnt);
+ WARN_ON(rcuref_put(&x->ref));
}
static inline void xfrm_state_put(struct xfrm_state *x)
{
- if (refcount_dec_and_test(&x->refcnt))
+ if (rcuref_put(&x->ref))
__xfrm_state_destroy(x, false);
}
static inline void xfrm_state_put_sync(struct xfrm_state *x)
{
- if (refcount_dec_and_test(&x->refcnt))
+ if (rcuref_put(&x->ref))
__xfrm_state_destroy(x, true);
}
static inline void xfrm_state_hold(struct xfrm_state *x)
{
- refcount_inc(&x->refcnt);
+ WARN_ON(!rcuref_get(&x->ref));
}
static inline bool addr_match(const void *token1, const void *token2,
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index ad2202fa82f34..ded79ec53cbdd 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -55,7 +55,7 @@ static HLIST_HEAD(xfrm_state_dev_gc_list);
static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
{
- return refcount_inc_not_zero(&x->refcnt);
+ return rcuref_get(&x->ref);
}
static inline unsigned int xfrm_dst_hash(struct net *net,
@@ -738,7 +738,7 @@ struct xfrm_state *xfrm_state_alloc(struct net *net)
if (x) {
write_pnet(&x->xs_net, net);
- refcount_set(&x->refcnt, 1);
+ rcuref_init(&x->ref, 1);
atomic_set(&x->tunnel_users, 0);
INIT_LIST_HEAD(&x->km.all);
INIT_HLIST_NODE(&x->state_cache);
--
2.47.2
Powered by blists - more mailing lists