mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Zahka <daniel.zahka@gmail.com>
To: Jakub Kicinski <kuba@kernel.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	 Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net v2 2/2] netdevsim: psp: use atomic64 for psp stats counters
Date: Fri, 29 May 2026 06:15:28 -0700	[thread overview]
Message-ID: <20260529-fix-psp-stats-v2-2-3a194eacf18e@gmail.com> (raw)
In-Reply-To: <20260529-fix-psp-stats-v2-0-3a194eacf18e@gmail.com>

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: 178f0763c5f3 ("netdevsim: implement psp device stats")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Daniel Zahka <daniel.zahka@gmail.com>
---
 drivers/net/netdevsim/netdevsim.h | 10 ++++------
 drivers/net/netdevsim/psp.c       | 24 ++++++++----------------
 2 files changed, 12 insertions(+), 22 deletions(-)

diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index d909c4160ea1..4c9cc96dcec3 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -22,7 +22,6 @@
 #include <linux/list.h>
 #include <linux/netdevice.h>
 #include <linux/ptp_mock.h>
-#include <linux/u64_stats_sync.h>
 #include <net/devlink.h>
 #include <net/udp_tunnel.h>
 #include <net/xdp.h>
@@ -115,11 +114,10 @@ struct netdevsim {
 	int rq_reset_mode;
 
 	struct {
-		u64_stats_t rx_packets;
-		u64_stats_t rx_bytes;
-		u64_stats_t tx_packets;
-		u64_stats_t tx_bytes;
-		struct u64_stats_sync syncp;
+		atomic64_t rx_packets;
+		atomic64_t rx_bytes;
+		atomic64_t tx_packets;
+		atomic64_t tx_bytes;
 		struct psp_dev __rcu *dev;
 		struct dentry *rereg;
 		struct mutex rereg_lock;
diff --git a/drivers/net/netdevsim/psp.c b/drivers/net/netdevsim/psp.c
index afe58b21041a..1c415b3d6f1a 100644
--- a/drivers/net/netdevsim/psp.c
+++ b/drivers/net/netdevsim/psp.c
@@ -50,10 +50,8 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
 	}
 
 	psp_len = skb->len - skb_inner_transport_offset(skb);
-	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);
+	atomic64_inc(&ns->psp.tx_packets);
+	atomic64_add(psp_len, &ns->psp.tx_bytes);
 
 	/* Now pretend we just received this frame */
 	peer_psd = rcu_dereference(peer_ns->psp.dev);
@@ -79,10 +77,8 @@ nsim_do_psp(struct sk_buff *skb, struct netdevsim *ns,
 		refcount_inc(&(*psp_ext)->refcnt);
 		skb->decrypted = 1;
 
-		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);
+		atomic64_inc(&peer_ns->psp.rx_packets);
+		atomic64_add(psp_len, &peer_ns->psp.rx_bytes);
 	} else {
 		struct ipv6hdr *ip6h __maybe_unused;
 		struct iphdr *iph;
@@ -180,20 +176,16 @@ static void nsim_assoc_del(struct psp_dev *psd, struct psp_assoc *pas)
 static void nsim_get_stats(struct psp_dev *psd, struct psp_dev_stats *stats)
 {
 	struct netdevsim *ns = psd->drv_priv;
-	unsigned int start;
 
 	/* WARNING: do *not* blindly zero stats in real drivers!
 	 * All required stats must be reported by the device!
 	 */
 	memset(stats, 0, sizeof(struct psp_dev_stats));
 
-	do {
-		start = u64_stats_fetch_begin(&ns->psp.syncp);
-		stats->rx_bytes = u64_stats_read(&ns->psp.rx_bytes);
-		stats->rx_packets = u64_stats_read(&ns->psp.rx_packets);
-		stats->tx_bytes = u64_stats_read(&ns->psp.tx_bytes);
-		stats->tx_packets = u64_stats_read(&ns->psp.tx_packets);
-	} while (u64_stats_fetch_retry(&ns->psp.syncp, start));
+	stats->rx_bytes = atomic64_read(&ns->psp.rx_bytes);
+	stats->rx_packets = atomic64_read(&ns->psp.rx_packets);
+	stats->tx_bytes = atomic64_read(&ns->psp.tx_bytes);
+	stats->tx_packets = atomic64_read(&ns->psp.tx_packets);
 }
 
 static struct psp_dev_ops nsim_psp_ops = {

-- 
2.52.0


  parent reply	other threads:[~2026-05-29 13:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-29 13:15 [PATCH net v2 0/2] netdevsim: psp: fix issues with stats collection Daniel Zahka
2026-05-29 13:15 ` [PATCH net v2 1/2] netdevsim: psp: update rx stats on the peer netdevsim Daniel Zahka
2026-05-29 13:15 ` Daniel Zahka [this message]
2026-06-02 20:00 ` [PATCH net v2 0/2] netdevsim: psp: fix issues with stats collection patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260529-fix-psp-stats-v2-2-3a194eacf18e@gmail.com \
    --to=daniel.zahka@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®