Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx
@ 2026-12-03 11:11 T S Rameshkumar
  2026-12-03 11:11 ` [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test T S Rameshkumar
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: T S Rameshkumar @ 2026-12-03 11:11 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol; +Cc: linux-can, T S Rameshkumar

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(), dev_sw_netstats_rx_add(), and
dev_get_tstats64(). dev_get_tstats64() also preserves
dev->stats.tx_dropped updated by can_dropped_invalid_skb().

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

diff --git a/drivers/net/can/vcan.c b/drivers/net/can/vcan.c
index 76e6b7b5c..15ebd814f 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;
@@ -161,6 +155,7 @@ static int vcan_change_mtu(struct net_device *dev, int new_mtu)
 static const struct net_device_ops vcan_netdev_ops = {
 	.ndo_start_xmit = vcan_tx,
 	.ndo_change_mtu = vcan_change_mtu,
+	.ndo_get_stats64 = dev_get_tstats64,
 };
 
 static const struct ethtool_ops vcan_ethtool_ops = {
@@ -185,6 +180,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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-03 12:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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-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 ` [PATCH v2 " T S Rameshkumar
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox