xfrm: Serialize offload stats updates with x->lock
Subsystems (3)
PATCH 1/1 xfrm: Serialize offload stats updates with x->lock
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.
---
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.
include/net/xfrm.h | 12 ++++++ net/xfrm/xfrm_state.c | 87 +++++++++++++++++++++++++++++++++++++++---- net/xfrm/xfrm_user.c | 45 ++++++++++++++++++++-- 3 files changed, 133 insertions(+), 11 deletions(-)
include/net/xfrm.h +12 -0
@@ -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);
net/xfrm/xfrm_state.c +80 -7
101 lines not on this page · raw message
net/xfrm/xfrm_user.c +41 -4
94 lines not on this page · raw message
base-commit: b88c75488ecc768acadb14e980bbfa087b1dbf38
Signature · 1 line
2.39.3
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.