[PATCH net 3/3] netdevsim: psp: serialize psp stats writers
From: Daniel Zahka
Date: Wed May 13 2026 - 09:10:54 EST
The u64_stats_* api requires mutual exclusion on writers. The simplest
way to do this is just to add a spinlock to the writer path, rather
than making the stats per queue.
Synchronization of the reader and writer paths use the
u64_stats_update_begin() api and does not require any fixes.
Fixes: 178f0763c5f3 ("netdevsim: implement psp device stats")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Daniel Zahka <daniel.zahka@xxxxxxxxx>
---
drivers/net/netdevsim/netdevsim.h | 1 +
drivers/net/netdevsim/psp.c | 5 +++++
2 files changed, 6 insertions(+)
diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index d909c4160ea1..bc36033b511b 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -115,6 +115,7 @@ struct netdevsim {
int rq_reset_mode;
struct {
+ spinlock_t stats_lock;
u64_stats_t rx_packets;
u64_stats_t rx_bytes;
u64_stats_t tx_packets;
diff --git a/drivers/net/netdevsim/psp.c b/drivers/net/netdevsim/psp.c
index 5ff91da24539..56ddbf146489 100644
--- a/drivers/net/netdevsim/psp.c
+++ b/drivers/net/netdevsim/psp.c
@@ -50,10 +50,12 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
}
psp_len = skb->len - skb_inner_transport_offset(skb);
+ spin_lock_bh(&ns->psp.stats_lock);
u64_stats_update_begin(&ns->psp.syncp);
u64_stats_inc(&ns->psp.tx_packets);
u64_stats_add(&ns->psp.tx_bytes, psp_len);
u64_stats_update_end(&ns->psp.syncp);
+ spin_unlock_bh(&ns->psp.stats_lock);
/* Now pretend we just received this frame */
peer_psd = rcu_dereference(peer_ns->psp.dev);
@@ -79,10 +81,12 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
refcount_inc(&(*psp_ext)->refcnt);
skb->decrypted = 1;
+ spin_lock_bh(&peer_ns->psp.stats_lock);
u64_stats_update_begin(&peer_ns->psp.syncp);
u64_stats_inc(&peer_ns->psp.rx_packets);
u64_stats_add(&peer_ns->psp.rx_bytes, psp_len);
u64_stats_update_end(&peer_ns->psp.syncp);
+ spin_unlock_bh(&peer_ns->psp.stats_lock);
} else {
struct ipv6hdr *ip6h __maybe_unused;
struct iphdr *iph;
@@ -271,6 +275,7 @@ int nsim_psp_init(struct netdevsim *ns)
struct dentry *ddir = ns->nsim_dev_port->ddir;
struct psp_dev *psd;
+ spin_lock_init(&ns->psp.stats_lock);
u64_stats_init(&ns->psp.syncp);
psd = psp_dev_create(ns->netdev, &nsim_psp_ops, &nsim_psp_caps, ns);
--
2.52.0