All of lore.kernel.org
 help / color / mirror / Atom feed
From: T S Rameshkumar <rameshsv06@gmail.com>
To: Marc Kleine-Budde <mkl@pengutronix.de>,
	Vincent Mailhol <mailhol@kernel.org>
Cc: linux-can@vger.kernel.org,
	T S Rameshkumar <rameshkumar.t@phytecembedded.in>
Subject: [PATCH v2 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx
Date: Thu,  3 Dec 2026 17:35:58 +0530	[thread overview]
Message-ID: <20261203120559.421646-1-rameshsv06@gmail.com> (raw)
In-Reply-To: <20261203111103.379929-1-rameshsv06@gmail.com>

From: T S Rameshkumar <rameshkumar.t@phytecembedded.in>

vcan_tx() and vcan_rx() update dev->stats.{tx,rx}_{packets,bytes} with
plain non-atomic accesses, which can trigger KCSAN data-race reports
when statistics are read or updated concurrently.

Convert vcan to use NETDEV_PCPU_STAT_TSTATS with
dev_sw_netstats_tx_add() and dev_sw_netstats_rx_add(). Since
dev_get_stats() automatically invokes dev_get_tstats64() when
NETDEV_PCPU_STAT_TSTATS is set and ndo_get_stats64 is unset, no
ndo_get_stats64 callback is needed, and dev->stats.tx_dropped
updated by can_dropped_invalid_skb() is preserved.

Signed-off-by: T S Rameshkumar <rameshkumar.t@phytecembedded.in>
---
 drivers/net/can/vcan.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/drivers/net/can/vcan.c b/drivers/net/can/vcan.c
index 76e6b7b5c..0108c6742 100644
--- a/drivers/net/can/vcan.c
+++ b/drivers/net/can/vcan.c
@@ -71,10 +71,7 @@ MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
 
 static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
 {
-	struct net_device_stats *stats = &dev->stats;
-
-	stats->rx_packets++;
-	stats->rx_bytes += can_skb_get_data_len(skb);
+	dev_sw_netstats_rx_add(dev, can_skb_get_data_len(skb));
 
 	skb->pkt_type  = PACKET_BROADCAST;
 	skb->dev       = dev;
@@ -85,7 +82,6 @@ static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
 
 static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
 {
-	struct net_device_stats *stats = &dev->stats;
 	unsigned int len;
 	int loop;
 
@@ -93,8 +89,7 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
 		return NETDEV_TX_OK;
 
 	len = can_skb_get_data_len(skb);
-	stats->tx_packets++;
-	stats->tx_bytes += len;
+	dev_sw_netstats_tx_add(dev, 1, len);
 
 	/* set flag whether this packet has to be looped back */
 	loop = skb->pkt_type == PACKET_LOOPBACK;
@@ -107,8 +102,7 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
 			/* only count the packets here, because the
 			 * CAN core already did the echo for us
 			 */
-			stats->rx_packets++;
-			stats->rx_bytes += len;
+			dev_sw_netstats_rx_add(dev, len);
 		}
 		consume_skb(skb);
 		return NETDEV_TX_OK;
@@ -185,6 +179,7 @@ static void vcan_setup(struct net_device *dev)
 	dev->netdev_ops		= &vcan_netdev_ops;
 	dev->ethtool_ops	= &vcan_ethtool_ops;
 	dev->needs_free_netdev	= true;
+	dev->pcpu_stat_type	= NETDEV_PCPU_STAT_TSTATS;
 }
 
 static struct rtnl_link_ops vcan_link_ops __read_mostly = {
-- 
2.34.1


  parent reply	other threads:[~2026-12-03 12:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:11 [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx T S Rameshkumar
2026-12-03 11:11 ` [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test T S Rameshkumar
2026-12-03 11:23   ` sashiko-bot
2026-12-05  0:14   ` netdev-bot+sashiko
2026-12-03 11:23 ` [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx sashiko-bot
2026-12-03 12:05 ` T S Rameshkumar [this message]
2026-12-03 12:05   ` [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test T S Rameshkumar
2026-12-03 12:37     ` sashiko-bot
2026-12-03 12:50     ` Oliver Hartkopp
2026-12-05  0:41     ` netdev-bot+sashiko
2026-12-05  0:41   ` [PATCH v2 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx netdev-bot+sashiko
2026-12-05  0:14 ` [PATCH " netdev-bot+sashiko

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=20261203120559.421646-1-rameshsv06@gmail.com \
    --to=rameshsv06@gmail.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=rameshkumar.t@phytecembedded.in \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.