Netdev List
 help / color / mirror / Atom feed
* [PATCH ipsec] xfrm: Serialize offload stats updates with x->lock
@ 2026-11-25  2:08 Jianbo Liu
  2026-11-25  2:12 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Jianbo Liu @ 2026-11-25  2:08 UTC (permalink / raw)
  To: netdev, ipsec
  Cc: Steffen Klassert, Herbert Xu, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Leon Romanovsky,
	Raed Salem, Saeed Mahameed, Cen Zhang, linux-kernel, Jianbo Liu,
	Carolina Jubran

xfrm_dev_state_update_stats() calls into the driver, which adds to
x->curlft and x->stats what the device counted since the last call.
Callers use different locks, so two of them can refresh the same
state at once and count the same packets twice, and a dump can copy
the counters while another caller updates them.

Serialize all device stats refreshes with x->lock and assert it in
xfrm_dev_state_update_stats(). Build the SA message of a netlink
dump, get or notification under the same lock.

The SA dump and xfrm_state_update_stats() walk the states under
xfrm_state_lock, where x->lock cannot be taken: the lock order is
x->lock, then xfrm_state_lock. Add xfrm_state_walk_batch(), which
takes references on at most 32 states per xfrm_state_lock hold, and
handle each state under x->lock after the list lock is dropped.
States rejected by the dump filter count against the 32, so a filter
that matches few states cannot keep the list lock held over the whole
list. xfrm_state_walk() cannot give this bound: it calls back only
for states that pass the filter. Walk markers and states being
deleted do not count, so a call always covers 32 states or reaches
the end of the list.

Keep the refresh right before each state is copied, so every part of
a multi-part dump still has current counters. When a state does not
fit into the skb, put the walk marker back in front of it and drop
the references, so no state stays referenced between two dump calls.
If the state was unlinked meanwhile, skip it and go on.

copy_to_user_state() cannot take x->lock itself: build_expire() calls
it with the lock held. Move the refresh out of it and let each caller
refresh under x->lock. PF_KEY never refreshed and does not change.

Fixes: c9fa320b00cf ("xfrm: copy_to_user_state fetch offloaded SA packets/bytes statistics")
Fixes: f9f221c98fd8 ("xfrm: get global statistics from the offloaded device")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Carolina Jubran <cjubran@nvidia.com>
---
Cen Zhang's "[PATCH ipsec v2 1/2] xfrm: protect offload statistics
updates from device teardown" [1] adds x->lock around the same two
calls in xfrm_state_netlink() and xfrm_notify_sa(), so only one of
the two patches can be applied. This patch takes the lock in the SA
dump and in xfrm_state_update_stats() as well.

[1] https://lore.kernel.org/netdev/pm-xfrm-core-objects-candidate-0050-v4-3c6b8c5369a802e9be0e-1@gmail.com/

 include/net/xfrm.h    | 12 ++++++
 net/xfrm/xfrm_state.c | 87 +++++++++++++++++++++++++++++++++++++++----
 net/xfrm/xfrm_user.c  | 45 ++++++++++++++++++++--
 3 files changed, 133 insertions(+), 11 deletions(-)

diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index 445223a8dc6f..22e82be62556 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -130,6 +130,13 @@ struct xfrm_state_walk {
 	struct xfrm_address_filter *filter;
 };
 
+#define XFRM_STATE_WALK_BATCH_SIZE	32
+
+struct xfrm_state_walk_batch {
+	struct xfrm_state	*states[XFRM_STATE_WALK_BATCH_SIZE];
+	unsigned int		count;
+};
+
 enum {
 	XFRM_DEV_OFFLOAD_IN = 1,
 	XFRM_DEV_OFFLOAD_OUT,
@@ -1718,6 +1725,9 @@ void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
 			  struct xfrm_address_filter *filter);
 int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
 		    int (*func)(struct xfrm_state *, int, void*), void *);
+bool xfrm_state_walk_batch(struct net *net, struct xfrm_state_walk *walk,
+			   struct xfrm_state_walk_batch *batch);
+bool xfrm_state_walk_rewind(struct xfrm_state_walk *walk, struct xfrm_state *x);
 void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
 struct xfrm_state *xfrm_state_alloc(struct net *net);
 void xfrm_state_free(struct xfrm_state *x);
@@ -1742,6 +1752,8 @@ static inline void xfrm_dev_state_update_stats(struct xfrm_state *x)
 	struct xfrm_dev_offload *xdo = &x->xso;
 	struct net_device *dev = READ_ONCE(xdo->dev);
 
+	lockdep_assert_held(&x->lock);
+
 	if (dev && dev->xfrmdev_ops &&
 	    dev->xfrmdev_ops->xdo_dev_state_update_stats)
 		dev->xfrmdev_ops->xdo_dev_state_update_stats(x);
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index f4a4ddaeb9b3..041d8aa86256 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -2390,15 +2390,28 @@ EXPORT_SYMBOL(xfrm_state_check_expire);
 
 void xfrm_state_update_stats(struct net *net)
 {
+	struct xfrm_state_walk_batch batch;
+	struct xfrm_state_walk walk;
 	struct xfrm_state *x;
-	int i;
+	unsigned int i;
+	bool done;
 
-	spin_lock_bh(&net->xfrm.xfrm_state_lock);
-	for (i = 0; i <= net->xfrm.state_hmask; i++) {
-		hlist_for_each_entry(x, xfrm_state_deref_prot(net->xfrm.state_bydst, net) + i, bydst)
-			xfrm_dev_state_update_stats(x);
-	}
-	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
+	xfrm_state_walk_init(&walk, 0, NULL);
+	do {
+		done = xfrm_state_walk_batch(net, &walk, &batch);
+		for (i = 0; i < batch.count; i++) {
+			x = batch.states[i];
+			if (READ_ONCE(x->xso.dev)) {
+				spin_lock_bh(&x->lock);
+				if (x->km.state != XFRM_STATE_DEAD)
+					xfrm_dev_state_update_stats(x);
+				spin_unlock_bh(&x->lock);
+			}
+			xfrm_state_put(x);
+		}
+		cond_resched();
+	} while (!done);
+	xfrm_state_walk_done(&walk, net);
 }
 
 struct xfrm_state *
@@ -2822,6 +2835,66 @@ int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
 }
 EXPORT_SYMBOL(xfrm_state_walk);
 
+bool xfrm_state_walk_batch(struct net *net, struct xfrm_state_walk *walk,
+			   struct xfrm_state_walk_batch *batch)
+{
+	struct xfrm_state_walk *x;
+	struct xfrm_state *state;
+	unsigned int visited = 0;
+	bool done = true;
+
+	batch->count = 0;
+	if (walk->seq != 0 && list_empty(&walk->all))
+		return true;
+
+	spin_lock_bh(&net->xfrm.xfrm_state_lock);
+	if (list_empty(&walk->all))
+		x = list_first_entry(&net->xfrm.state_all,
+				     struct xfrm_state_walk, all);
+	else
+		x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
+	list_for_each_entry_from(x, &net->xfrm.state_all, all) {
+		/* Callers recheck state under x->lock before processing. */
+		if (data_race(READ_ONCE(x->state)) == XFRM_STATE_DEAD)
+			continue;
+		if (visited++ == ARRAY_SIZE(batch->states)) {
+			list_move_tail(&walk->all, &x->all);
+			done = false;
+			goto out;
+		}
+		state = container_of(x, struct xfrm_state, km);
+		if (!xfrm_id_proto_match(state->id.proto, walk->proto))
+			continue;
+		if (!__xfrm_state_filter_match(state, walk->filter))
+			continue;
+		xfrm_state_hold(state);
+		batch->states[batch->count++] = state;
+	}
+	list_del_init(&walk->all);
+out:
+	walk->seq = 1;
+	spin_unlock_bh(&net->xfrm.xfrm_state_lock);
+	return done;
+}
+EXPORT_SYMBOL(xfrm_state_walk_batch);
+
+bool xfrm_state_walk_rewind(struct xfrm_state_walk *walk, struct xfrm_state *x)
+{
+	struct net *net = xs_net(x);
+	bool linked;
+
+	lockdep_assert_held(&x->lock);
+
+	spin_lock(&net->xfrm.xfrm_state_lock);
+	linked = !hlist_unhashed(&x->bydst);
+	if (linked)
+		list_move_tail(&walk->all, &x->km.all);
+	spin_unlock(&net->xfrm.xfrm_state_lock);
+
+	return linked;
+}
+EXPORT_SYMBOL(xfrm_state_walk_rewind);
+
 void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
 			  struct xfrm_address_filter *filter)
 {
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 217f65faa2bd..94f73349d22d 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -1181,8 +1181,6 @@ static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
 	memcpy(&p->id, &x->id, sizeof(p->id));
 	memcpy(&p->sel, &x->sel, sizeof(p->sel));
 	memcpy(&p->lft, &x->lft, sizeof(p->lft));
-	if (x->xso.dev)
-		xfrm_dev_state_update_stats(x);
 	memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
 	put_unaligned(x->stats.replay_window, &p->stats.replay_window);
 	put_unaligned(x->stats.replay, &p->stats.replay);
@@ -1566,11 +1564,34 @@ static int xfrm_dump_sa_done(struct netlink_callback *cb)
 	return 0;
 }
 
+static int refresh_and_dump_state(struct xfrm_state_walk *walk,
+				  struct xfrm_state *x,
+				  struct xfrm_dump_info *info)
+{
+	int err = 0;
+
+	spin_lock_bh(&x->lock);
+	if (x->km.state != XFRM_STATE_DEAD) {
+		xfrm_dev_state_update_stats(x);
+		err = dump_one_state(x, 0, info);
+		if (err && !xfrm_state_walk_rewind(walk, x))
+			err = 0;
+	}
+	spin_unlock_bh(&x->lock);
+
+	return err;
+}
+
 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
 {
 	struct net *net = sock_net(skb->sk);
 	struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
+	struct xfrm_state_walk_batch batch;
 	struct xfrm_dump_info info;
+	struct xfrm_state *x;
+	unsigned int i;
+	int err = 0;
+	bool done;
 
 	BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
 		     sizeof(cb->args) - sizeof(cb->args[0]));
@@ -1584,7 +1605,6 @@ static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
 		struct nlattr *attrs[XFRMA_MAX+1];
 		struct xfrm_address_filter *filter = NULL;
 		u8 proto = 0;
-		int err;
 
 		err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
 					     xfrma_policy, cb->extack);
@@ -1614,7 +1634,16 @@ static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
 		cb->args[0] = 1;
 	}
 
-	(void) xfrm_state_walk(net, walk, dump_one_state, &info);
+	do {
+		done = xfrm_state_walk_batch(net, walk, &batch);
+		for (i = 0; i < batch.count; i++) {
+			x = batch.states[i];
+			if (!err)
+				err = refresh_and_dump_state(walk, x, &info);
+			xfrm_state_put(x);
+		}
+		cond_resched();
+	} while (!done && !err);
 
 	return skb->len;
 }
@@ -1635,7 +1664,10 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
 	info.nlmsg_seq = seq;
 	info.nlmsg_flags = 0;
 
+	spin_lock_bh(&x->lock);
+	xfrm_dev_state_update_stats(x);
 	err = dump_one_state(x, 0, &info);
+	spin_unlock_bh(&x->lock);
 	if (err) {
 		kfree_skb(skb);
 		return ERR_PTR(err);
@@ -3973,6 +4005,8 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct
 	struct nlmsghdr *nlh;
 	int err;
 
+	xfrm_dev_state_update_stats(x);
+
 	nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
 	if (nlh == NULL)
 		return -EMSGSIZE;
@@ -4170,7 +4204,10 @@ static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
 
 		p = nla_data(attr);
 	}
+	spin_lock_bh(&x->lock);
+	xfrm_dev_state_update_stats(x);
 	err = copy_to_user_state_extra(x, p, skb);
+	spin_unlock_bh(&x->lock);
 	if (err)
 		goto out_free_skb;
 

base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  2:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  2:08 [PATCH ipsec] xfrm: Serialize offload stats updates with x->lock Jianbo Liu
2026-11-25  2:12 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox