Skip to content

Commit 163bea8

Browse files
Daniel Zahkakuba-moo
authored andcommitted
netdevsim: psp: use atomic64 for psp stats counters
The existing u64_stats_t-based psp counters had two preexisting api usage bugs: u64_stats_init() was never called on the syncp object, and the writer side of the u64_stats_update_begin()/end() api was not serialized. Switch the counters to atomic64_t instead. Atomics need no initialization and are inherently safe against concurrent writers, eliminating both bugs at once. Use atomic64_t rather than atomic_long_t so byte counters don't wrap at 4 GiB on 32-bit builds. Fixes: 178f076 ("netdevsim: implement psp device stats") Cc: <stable+noautosel@kernel.org> # netdevsim is a test harness, it's never loaded on production systems Signed-off-by: Daniel Zahka <daniel.zahka@gmail.com> Link: https://patch.msgid.link/20260529-fix-psp-stats-v2-2-3a194eacf18e@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent abaef7e commit 163bea8

2 files changed

Lines changed: 12 additions & 22 deletions

File tree

drivers/net/netdevsim/netdevsim.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <linux/list.h>
2323
#include <linux/netdevice.h>
2424
#include <linux/ptp_mock.h>
25-
#include <linux/u64_stats_sync.h>
2625
#include <net/devlink.h>
2726
#include <net/udp_tunnel.h>
2827
#include <net/xdp.h>
@@ -115,11 +114,10 @@ struct netdevsim {
115114
int rq_reset_mode;
116115

117116
struct {
118-
u64_stats_t rx_packets;
119-
u64_stats_t rx_bytes;
120-
u64_stats_t tx_packets;
121-
u64_stats_t tx_bytes;
122-
struct u64_stats_sync syncp;
117+
atomic64_t rx_packets;
118+
atomic64_t rx_bytes;
119+
atomic64_t tx_packets;
120+
atomic64_t tx_bytes;
123121
struct psp_dev __rcu *dev;
124122
struct dentry *rereg;
125123
struct mutex rereg_lock;

drivers/net/netdevsim/psp.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
5050
}
5151

5252
psp_len = skb->len - skb_inner_transport_offset(skb);
53-
u64_stats_update_begin(&ns->psp.syncp);
54-
u64_stats_inc(&ns->psp.tx_packets);
55-
u64_stats_add(&ns->psp.tx_bytes, psp_len);
56-
u64_stats_update_end(&ns->psp.syncp);
53+
atomic64_inc(&ns->psp.tx_packets);
54+
atomic64_add(psp_len, &ns->psp.tx_bytes);
5755

5856
/* Now pretend we just received this frame */
5957
peer_psd = rcu_dereference(peer_ns->psp.dev);
@@ -79,10 +77,8 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
7977
refcount_inc(&(*psp_ext)->refcnt);
8078
skb->decrypted = 1;
8179

82-
u64_stats_update_begin(&peer_ns->psp.syncp);
83-
u64_stats_inc(&peer_ns->psp.rx_packets);
84-
u64_stats_add(&peer_ns->psp.rx_bytes, psp_len);
85-
u64_stats_update_end(&peer_ns->psp.syncp);
80+
atomic64_inc(&peer_ns->psp.rx_packets);
81+
atomic64_add(psp_len, &peer_ns->psp.rx_bytes);
8682
} else {
8783
struct ipv6hdr *ip6h __maybe_unused;
8884
struct iphdr *iph;
@@ -191,20 +187,16 @@ static void nsim_assoc_del(struct psp_dev *psd, struct psp_assoc *pas)
191187
static void nsim_get_stats(struct psp_dev *psd, struct psp_dev_stats *stats)
192188
{
193189
struct netdevsim *ns = psd->drv_priv;
194-
unsigned int start;
195190

196191
/* WARNING: do *not* blindly zero stats in real drivers!
197192
* All required stats must be reported by the device!
198193
*/
199194
memset(stats, 0, sizeof(struct psp_dev_stats));
200195

201-
do {
202-
start = u64_stats_fetch_begin(&ns->psp.syncp);
203-
stats->rx_bytes = u64_stats_read(&ns->psp.rx_bytes);
204-
stats->rx_packets = u64_stats_read(&ns->psp.rx_packets);
205-
stats->tx_bytes = u64_stats_read(&ns->psp.tx_bytes);
206-
stats->tx_packets = u64_stats_read(&ns->psp.tx_packets);
207-
} while (u64_stats_fetch_retry(&ns->psp.syncp, start));
196+
stats->rx_bytes = atomic64_read(&ns->psp.rx_bytes);
197+
stats->rx_packets = atomic64_read(&ns->psp.rx_packets);
198+
stats->tx_bytes = atomic64_read(&ns->psp.tx_bytes);
199+
stats->tx_packets = atomic64_read(&ns->psp.tx_packets);
208200
}
209201

210202
static struct psp_dev_ops nsim_psp_ops = {

0 commit comments

Comments
 (0)