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; 6+ 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] 6+ messages in thread

* [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test
  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 ` 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
  2 siblings, 1 reply; 6+ 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>

Add a kselftest (test_vcan_stats) that spawns concurrent PF_CAN
transmitters on vcan0 while reading /proc/net/dev in parallel and
verifies exact rx/tx packet and byte counter deltas.

Signed-off-by: T S Rameshkumar <rameshkumar.t@phytecembedded.in>
---
 tools/testing/selftests/net/can/.gitignore    |   1 +
 tools/testing/selftests/net/can/Makefile      |   5 +-
 .../selftests/net/can/test_vcan_stats.c       | 181 ++++++++++++++++++
 .../selftests/net/can/test_vcan_stats.sh      |  37 ++++
 4 files changed, 222 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/can/test_vcan_stats.c
 create mode 100755 tools/testing/selftests/net/can/test_vcan_stats.sh

diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore
index 764a53fc8..3dee21f85 100644
--- a/tools/testing/selftests/net/can/.gitignore
+++ b/tools/testing/selftests/net/can/.gitignore
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 test_raw_filter
+test_vcan_stats
diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile
index 5b82e60a0..c2e4903b9 100644
--- a/tools/testing/selftests/net/can/Makefile
+++ b/tools/testing/selftests/net/can/Makefile
@@ -3,9 +3,10 @@
 top_srcdir = ../../../../..
 
 CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INCLUDES)
+LDLIBS += -lpthread
 
-TEST_PROGS := test_raw_filter.sh
+TEST_PROGS := test_raw_filter.sh test_vcan_stats.sh
 
-TEST_GEN_FILES := test_raw_filter
+TEST_GEN_FILES := test_raw_filter test_vcan_stats
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.c b/tools/testing/selftests/net/can/test_vcan_stats.c
new file mode 100644
index 000000000..1a4f715ad
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.c
@@ -0,0 +1,181 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <errno.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <net/if.h>
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define NUM_THREADS 4
+#define FRAMES_PER_THREAD 1000
+#define CAN_FRAME_DLC 8
+
+static char canif[IFNAMSIZ];
+static atomic_int stop_reader;
+
+struct net_dev_stats {
+	unsigned long long rx_bytes;
+	unsigned long long rx_packets;
+	unsigned long long tx_bytes;
+	unsigned long long tx_packets;
+};
+
+static int read_if_stats(const char *ifname, struct net_dev_stats *st)
+{
+	char line[512];
+	FILE *f;
+	int found = 0;
+
+	f = fopen("/proc/net/dev", "r");
+	if (!f)
+		return -errno;
+
+	while (fgets(line, sizeof(line), f)) {
+		char *colon = strchr(line, ':');
+		char *name;
+
+		if (!colon)
+			continue;
+		*colon = '\0';
+		name = line;
+		while (*name == ' ' || *name == '\t')
+			name++;
+		if (strcmp(name, ifname) != 0)
+			continue;
+
+		unsigned long long dummy;
+
+		if (sscanf(colon + 1,
+			   "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
+			   &st->rx_bytes, &st->rx_packets,
+			   &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
+			   &st->tx_bytes, &st->tx_packets) == 10)
+			found = 1;
+		break;
+	}
+
+	fclose(f);
+	return found ? 0 : -ENOENT;
+}
+
+static void *tx_worker(void *arg)
+{
+	int id = (int)(long)arg;
+	struct sockaddr_can addr = {};
+	struct can_frame frame = {};
+	struct ifreq ifr = {};
+	int s;
+
+	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+	if (s < 0)
+		return (void *)(long)-errno;
+
+	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", canif);
+	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+		int err = -errno;
+
+		close(s);
+		return (void *)(long)err;
+	}
+
+	addr.can_family = AF_CAN;
+	addr.can_ifindex = ifr.ifr_ifindex;
+	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+		int err = -errno;
+
+		close(s);
+		return (void *)(long)err;
+	}
+
+	frame.can_id = 0x100 + id;
+	frame.can_dlc = CAN_FRAME_DLC;
+	memset(frame.data, 0xAA, CAN_FRAME_DLC);
+
+	for (int i = 0; i < FRAMES_PER_THREAD; i++) {
+		ssize_t ret = write(s, &frame, sizeof(frame));
+
+		if (ret < 0) {
+			if (errno == ENOBUFS) {
+				usleep(100);
+				i--;
+				continue;
+			}
+			int err = -errno;
+
+			close(s);
+			return (void *)(long)err;
+		}
+	}
+
+	close(s);
+	return NULL;
+}
+
+static void *stats_reader(void *arg)
+{
+	struct net_dev_stats st;
+
+	while (!atomic_load(&stop_reader))
+		read_if_stats(canif, &st);
+
+	return NULL;
+}
+
+TEST(vcan_concurrent_tx_stats)
+{
+	struct net_dev_stats before, after;
+	pthread_t tx[NUM_THREADS], reader;
+	unsigned long long exp_pkts, exp_bytes;
+	int ret;
+
+	ret = read_if_stats(canif, &before);
+	ASSERT_EQ(ret, 0)
+		TH_LOG("failed to read initial stats for %s", canif);
+
+	atomic_store(&stop_reader, 0);
+	ret = pthread_create(&reader, NULL, stats_reader, NULL);
+	ASSERT_EQ(ret, 0);
+
+	for (long i = 0; i < NUM_THREADS; i++) {
+		ret = pthread_create(&tx[i], NULL, tx_worker, (void *)i);
+		ASSERT_EQ(ret, 0);
+	}
+
+	for (int i = 0; i < NUM_THREADS; i++) {
+		void *res = NULL;
+
+		pthread_join(tx[i], &res);
+		ASSERT_EQ((long)res, 0);
+	}
+
+	atomic_store(&stop_reader, 1);
+	pthread_join(reader, NULL);
+
+	ret = read_if_stats(canif, &after);
+	ASSERT_EQ(ret, 0);
+
+	exp_pkts = NUM_THREADS * FRAMES_PER_THREAD;
+	exp_bytes = exp_pkts * CAN_FRAME_DLC;
+
+	EXPECT_EQ(after.tx_packets - before.tx_packets, exp_pkts);
+	EXPECT_EQ(after.tx_bytes - before.tx_bytes, exp_bytes);
+	EXPECT_EQ(after.rx_packets - before.rx_packets, exp_pkts);
+	EXPECT_EQ(after.rx_bytes - before.rx_bytes, exp_bytes);
+}
+
+int main(int argc, char **argv)
+{
+	char *env_canif = getenv("CANIF");
+
+	snprintf(canif, sizeof(canif), "%s", env_canif ? env_canif : "vcan0");
+	return test_harness_run(argc, argv);
+}
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.sh b/tools/testing/selftests/net/can/test_vcan_stats.sh
new file mode 100755
index 000000000..1153699cb
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ALL_TESTS="
+	test_vcan_stats
+"
+
+net_dir=$(dirname $0)/..
+source $net_dir/lib.sh
+
+export CANIF=${CANIF:-"vcan0"}
+
+setup()
+{
+	ip link add name $CANIF type vcan || exit $ksft_skip
+	ip link set dev $CANIF up
+}
+
+cleanup()
+{
+	ip link set dev $CANIF down
+	ip link delete $CANIF
+}
+
+test_vcan_stats()
+{
+	./test_vcan_stats
+	check_err $?
+	log_test "test_vcan_stats"
+}
+
+trap cleanup EXIT
+setup
+
+tests_run
+
+exit $EXIT_STATUS
-- 
2.34.1


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

* Re: [PATCH 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx
  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 12:05 ` [PATCH v2 " T S Rameshkumar
  2 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03 11:23 UTC (permalink / raw)
  To: T S Rameshkumar
  Cc: linux-can, Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
	Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] can: vcan: redundant ndo_get_stats64 callback with
  NETDEV_PCPU_STAT_TSTATS
--

commit 3a0edae3f5983fb3bb0c7e6ae9f7865bc39dc626
Author: T S Rameshkumar <rameshkumar.t@phytecembedded.in>

can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx

This commit converts vcan to use per-CPU statistics via
NETDEV_PCPU_STAT_TSTATS and dev_sw_netstats_{rx,tx}_add() instead of plain
accesses to dev->stats.

> diff --git a/drivers/net/can/vcan.c b/drivers/net/can/vcan.c
> index 76e6b7b5c6a11..15ebd814fe75f 100644
> --- a/drivers/net/can/vcan.c
> +++ b/drivers/net/can/vcan.c

[ ... ]

> @@ -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,
>  };

[Severity: Low]
Can .ndo_get_stats64 = dev_get_tstats64 be omitted here?

Since commit 3e2f544dd8a3 ("net: get stats64 if device if driver is
configured"), dev_get_stats() already calls dev_get_tstats64() directly when
dev->pcpu_stat_type is NETDEV_PCPU_STAT_TSTATS (as set in vcan_setup()) and
ndo_get_stats64 is unset.

Setting .ndo_get_stats64 explicitly takes the ops->ndo_get_stats64 branch in
dev_get_stats() instead, adding an unnecessary memset() and indirect call on
every stats read before dev_get_tstats64() overwrites storage via
netdev_stats_to_stats64().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203111103.379929-1-rameshsv06@gmail.com?part=1

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

* Re: [PATCH 2/2] selftests: net: can: add vcan concurrent tx/stats test
  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
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03 11:23 UTC (permalink / raw)
  To: T S Rameshkumar
  Cc: linux-can, Marc Kleine-Budde, Oliver Hartkopp, Vincent Mailhol,
	Oleksij Rempel

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] selftests: net: can: premature EXIT trap and missing netns
  isolation
- [Medium] selftests: net: can: missing barrier and unchecked reads in
  stats_reader()
- [Low] selftests: net: can: missing CAN_RAW skip handling in test and
  wrapper
--

commit b3f673b3db7d63b8eb092b5a5498ad7f078fc788
Author: T S Rameshkumar <rameshkumar.t@phytecembedded.in>

selftests: net: can: add vcan concurrent tx/stats test

This commit adds a kselftest that spawns concurrent PF_CAN transmitters
on a vcan interface while reading /proc/net/dev in parallel and checks
the resulting rx/tx packet and byte counter deltas.

> diff --git a/tools/testing/selftests/net/can/test_vcan_stats.c b/tools/testing/selftests/net/can/test_vcan_stats.c
> new file mode 100644
> index 0000000000000..1a4f715adead8
> --- /dev/null
> +++ b/tools/testing/selftests/net/can/test_vcan_stats.c

[ ... ]

> +static void *tx_worker(void *arg)
> +{
> +	int id = (int)(long)arg;
> +	struct sockaddr_can addr = {};
> +	struct can_frame frame = {};
> +	struct ifreq ifr = {};
> +	int s;
> +
> +	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
> +	if (s < 0)
> +		return (void *)(long)-errno;

[Severity: Low]
Should this test skip rather than fail when CONFIG_CAN_RAW is disabled?

CONFIG_CAN_VCAN does not depend on or select CONFIG_CAN_RAW, so creating
the vcan interface does not guarantee CAN_RAW socket support.  When
socket() fails with EPROTONOSUPPORT or EAFNOSUPPORT, tx_worker() returns
a negative errno and ASSERT_EQ((long)res, 0) fails the test instead of
probing CAN_RAW upfront and calling SKIP() with a reason string.

In test_vcan_stats.sh, check_err() also treats any non-zero exit status
(including ksft_skip) as a failure, so the wrapper would need to
propagate a skip exit status from test_vcan_stats.

[ ... ]

> +static void *stats_reader(void *arg)
> +{
> +	struct net_dev_stats st;
> +
> +	while (!atomic_load(&stop_reader))
> +		read_if_stats(canif, &st);
> +
> +	return NULL;
> +}
> +
> +TEST(vcan_concurrent_tx_stats)
> +{
> +	struct net_dev_stats before, after;
> +	pthread_t tx[NUM_THREADS], reader;
> +	unsigned long long exp_pkts, exp_bytes;
> +	int ret;
> +
> +	ret = read_if_stats(canif, &before);
> +	ASSERT_EQ(ret, 0)
> +		TH_LOG("failed to read initial stats for %s", canif);
> +
> +	atomic_store(&stop_reader, 0);
> +	ret = pthread_create(&reader, NULL, stats_reader, NULL);
> +	ASSERT_EQ(ret, 0);
> +
> +	for (long i = 0; i < NUM_THREADS; i++) {
> +		ret = pthread_create(&tx[i], NULL, tx_worker, (void *)i);
> +		ASSERT_EQ(ret, 0);
> +	}

[Severity: Medium]
Can all tx_worker() threads finish before stats_reader() ever runs?

Sending 1000 in-memory frames on vcan takes only a few milliseconds, and
no startup barrier (such as pthread_barrier_t) aligns the threads.  All
transmitters can finish and set stop_reader before stats_reader() runs
once, giving a silent false pass.

stats_reader() also ignores the return value of read_if_stats() and
never inspects or counts the snapshots it reads, so /proc/net/dev read
errors, zero concurrent reads, or non-monotonic counter values go
undetected.

> diff --git a/tools/testing/selftests/net/can/test_vcan_stats.sh b/tools/testing/selftests/net/can/test_vcan_stats.sh
> new file mode 100755
> index 0000000000000..1153699cb176b
> --- /dev/null
> +++ b/tools/testing/selftests/net/can/test_vcan_stats.sh

[ ... ]

> +setup()
> +{
> +	ip link add name $CANIF type vcan || exit $ksft_skip
> +	ip link set dev $CANIF up
> +}
> +
> +cleanup()
> +{
> +	ip link set dev $CANIF down
> +	ip link delete $CANIF
> +}

[ ... ]

> +trap cleanup EXIT
> +setup

[Severity: Medium]
Can cleanup() delete a pre-existing host vcan0 interface if setup()
fails?

trap cleanup EXIT is armed before setup() runs in the root network
namespace.  If $CANIF already exists on the host, ip link add fails with
EEXIST and exits with $ksft_skip, triggering cleanup() to bring down and
delete an interface this test did not create.

Running in the root namespace also lets concurrent host traffic skew the
exact /proc/net/dev deltas checked by test_vcan_stats.  Isolating the
device with setup_ns and cleanup_all_ns from the already-sourced
net/lib.sh avoids both issues.

In setup(), exiting on ip link add failure also omits a skip reason
message (while still firing cleanup() errors when vcan is unavailable),
and ip link set dev $CANIF up does not check its return status.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203111103.379929-1-rameshsv06@gmail.com?part=2

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

* [PATCH v2 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx
  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 ` [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
  2026-12-03 12:05   ` [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test T S Rameshkumar
  2 siblings, 1 reply; 6+ messages in thread
From: T S Rameshkumar @ 2026-12-03 12:05 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() 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


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

* [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test
  2026-12-03 12:05 ` [PATCH v2 " T S Rameshkumar
@ 2026-12-03 12:05   ` T S Rameshkumar
  0 siblings, 0 replies; 6+ messages in thread
From: T S Rameshkumar @ 2026-12-03 12:05 UTC (permalink / raw)
  To: Marc Kleine-Budde, Vincent Mailhol; +Cc: linux-can, T S Rameshkumar

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

Add a kselftest (test_vcan_stats) that runs in an isolated network
namespace, spawns concurrent PF_CAN transmitters on vcan0 while reading
/proc/net/dev in parallel behind a startup barrier, checks that
concurrent reads succeed monotonically, and verifies exact rx/tx packet
and byte counter deltas. Skip cleanly if CAN_RAW or vcan is
unavailable.

Signed-off-by: T S Rameshkumar <rameshkumar.t@phytecembedded.in>
---
 tools/testing/selftests/net/can/.gitignore    |   1 +
 tools/testing/selftests/net/can/Makefile      |   5 +-
 .../selftests/net/can/test_vcan_stats.c       | 231 ++++++++++++++++++
 .../selftests/net/can/test_vcan_stats.sh      |  50 ++++
 4 files changed, 285 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/net/can/test_vcan_stats.c
 create mode 100755 tools/testing/selftests/net/can/test_vcan_stats.sh

diff --git a/tools/testing/selftests/net/can/.gitignore b/tools/testing/selftests/net/can/.gitignore
index 764a53fc8..3dee21f85 100644
--- a/tools/testing/selftests/net/can/.gitignore
+++ b/tools/testing/selftests/net/can/.gitignore
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 test_raw_filter
+test_vcan_stats
diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile
index 5b82e60a0..c2e4903b9 100644
--- a/tools/testing/selftests/net/can/Makefile
+++ b/tools/testing/selftests/net/can/Makefile
@@ -3,9 +3,10 @@
 top_srcdir = ../../../../..
 
 CFLAGS += -Wall -Wl,--no-as-needed -O2 -g -I$(top_srcdir)/usr/include $(KHDR_INCLUDES)
+LDLIBS += -lpthread
 
-TEST_PROGS := test_raw_filter.sh
+TEST_PROGS := test_raw_filter.sh test_vcan_stats.sh
 
-TEST_GEN_FILES := test_raw_filter
+TEST_GEN_FILES := test_raw_filter test_vcan_stats
 
 include ../../lib.mk
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.c b/tools/testing/selftests/net/can/test_vcan_stats.c
new file mode 100644
index 000000000..aca72cc09
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.c
@@ -0,0 +1,231 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <errno.h>
+#include <linux/can.h>
+#include <linux/can/raw.h>
+#include <net/if.h>
+#include <pthread.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define NUM_THREADS 4
+#define FRAMES_PER_THREAD 1000
+#define CAN_FRAME_DLC 8
+
+static char canif[IFNAMSIZ];
+static pthread_barrier_t start_barrier;
+static atomic_int stop_reader;
+static atomic_int reader_err;
+static atomic_ulong reader_reads;
+
+struct net_dev_stats {
+	unsigned long long rx_bytes;
+	unsigned long long rx_packets;
+	unsigned long long tx_bytes;
+	unsigned long long tx_packets;
+};
+
+static int read_if_stats(const char *ifname, struct net_dev_stats *st)
+{
+	char line[512];
+	FILE *f;
+	int found = 0;
+
+	f = fopen("/proc/net/dev", "r");
+	if (!f)
+		return -errno;
+
+	while (fgets(line, sizeof(line), f)) {
+		char *colon = strchr(line, ':');
+		char *name;
+
+		if (!colon)
+			continue;
+		*colon = '\0';
+		name = line;
+		while (*name == ' ' || *name == '\t')
+			name++;
+		if (strcmp(name, ifname) != 0)
+			continue;
+
+		unsigned long long dummy;
+
+		if (sscanf(colon + 1,
+			   "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
+			   &st->rx_bytes, &st->rx_packets,
+			   &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
+			   &st->tx_bytes, &st->tx_packets) == 10)
+			found = 1;
+		break;
+	}
+
+	fclose(f);
+	return found ? 0 : -ENOENT;
+}
+
+static void *tx_worker(void *arg)
+{
+	int id = (int)(long)arg;
+	struct sockaddr_can addr = {};
+	struct can_frame frame = {};
+	struct ifreq ifr = {};
+	int s;
+
+	s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+	if (s < 0) {
+		int err = -errno;
+
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", canif);
+	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
+		int err = -errno;
+
+		close(s);
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	addr.can_family = AF_CAN;
+	addr.can_ifindex = ifr.ifr_ifindex;
+	if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
+		int err = -errno;
+
+		close(s);
+		pthread_barrier_wait(&start_barrier);
+		return (void *)(long)err;
+	}
+
+	frame.can_id = 0x100 + id;
+	frame.can_dlc = CAN_FRAME_DLC;
+	memset(frame.data, 0xAA, CAN_FRAME_DLC);
+
+	pthread_barrier_wait(&start_barrier);
+
+	for (int i = 0; i < FRAMES_PER_THREAD; i++) {
+		ssize_t ret = write(s, &frame, sizeof(frame));
+
+		if (ret < 0) {
+			if (errno == ENOBUFS) {
+				usleep(100);
+				i--;
+				continue;
+			}
+			int err = -errno;
+
+			close(s);
+			return (void *)(long)err;
+		}
+	}
+
+	close(s);
+	return NULL;
+}
+
+static void *stats_reader(void *arg)
+{
+	struct net_dev_stats prev = {}, st;
+	unsigned long reads = 0;
+
+	pthread_barrier_wait(&start_barrier);
+
+	while (!atomic_load(&stop_reader)) {
+		int ret = read_if_stats(canif, &st);
+
+		if (ret) {
+			atomic_store(&reader_err, ret);
+			break;
+		}
+		if (reads > 0 &&
+		    (st.rx_packets < prev.rx_packets ||
+		     st.rx_bytes < prev.rx_bytes ||
+		     st.tx_packets < prev.tx_packets ||
+		     st.tx_bytes < prev.tx_bytes)) {
+			atomic_store(&reader_err, -ERANGE);
+			break;
+		}
+		prev = st;
+		reads++;
+		atomic_fetch_add(&reader_reads, 1);
+	}
+
+	return NULL;
+}
+
+TEST(vcan_concurrent_tx_stats)
+{
+	struct net_dev_stats before, after;
+	pthread_t tx[NUM_THREADS], reader;
+	unsigned long long exp_pkts, exp_bytes;
+	int probe_sock, ret;
+
+	probe_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+	if (probe_sock < 0) {
+		if (errno == EPROTONOSUPPORT || errno == EAFNOSUPPORT)
+			SKIP(return, "CAN_RAW protocol not supported");
+		ASSERT_GE(probe_sock, 0)
+			TH_LOG("failed to create CAN_RAW socket: %d", errno);
+	}
+	close(probe_sock);
+
+	ret = read_if_stats(canif, &before);
+	ASSERT_EQ(ret, 0)
+		TH_LOG("failed to read initial stats for %s", canif);
+
+	ret = pthread_barrier_init(&start_barrier, NULL, NUM_THREADS + 1);
+	ASSERT_EQ(ret, 0);
+
+	atomic_store(&stop_reader, 0);
+	atomic_store(&reader_err, 0);
+	atomic_store(&reader_reads, 0);
+
+	ret = pthread_create(&reader, NULL, stats_reader, NULL);
+	ASSERT_EQ(ret, 0);
+
+	for (long i = 0; i < NUM_THREADS; i++) {
+		ret = pthread_create(&tx[i], NULL, tx_worker, (void *)i);
+		ASSERT_EQ(ret, 0);
+	}
+
+	for (int i = 0; i < NUM_THREADS; i++) {
+		void *res = NULL;
+
+		pthread_join(tx[i], &res);
+		ASSERT_EQ((long)res, 0);
+	}
+
+	atomic_store(&stop_reader, 1);
+	pthread_join(reader, NULL);
+	pthread_barrier_destroy(&start_barrier);
+
+	ASSERT_EQ(atomic_load(&reader_err), 0);
+	EXPECT_GT(atomic_load(&reader_reads), 0UL);
+
+	ret = read_if_stats(canif, &after);
+	ASSERT_EQ(ret, 0);
+
+	exp_pkts = NUM_THREADS * FRAMES_PER_THREAD;
+	exp_bytes = exp_pkts * CAN_FRAME_DLC;
+
+	EXPECT_EQ(after.tx_packets - before.tx_packets, exp_pkts);
+	EXPECT_EQ(after.tx_bytes - before.tx_bytes, exp_bytes);
+	EXPECT_EQ(after.rx_packets - before.rx_packets, exp_pkts);
+	EXPECT_EQ(after.rx_bytes - before.rx_bytes, exp_bytes);
+}
+
+int main(int argc, char **argv)
+{
+	char *env_canif = getenv("CANIF");
+
+	snprintf(canif, sizeof(canif), "%s", env_canif ? env_canif : "vcan0");
+	return test_harness_run(argc, argv);
+}
diff --git a/tools/testing/selftests/net/can/test_vcan_stats.sh b/tools/testing/selftests/net/can/test_vcan_stats.sh
new file mode 100755
index 000000000..2d182dd47
--- /dev/null
+++ b/tools/testing/selftests/net/can/test_vcan_stats.sh
@@ -0,0 +1,50 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ALL_TESTS="
+	test_vcan_stats
+"
+
+net_dir=$(dirname $0)/..
+source $net_dir/lib.sh
+
+export CANIF=${CANIF:-"vcan0"}
+
+setup()
+{
+	setup_ns ns || exit $ksft_skip
+	if ! ip -n "$ns" link add name "$CANIF" type vcan; then
+		echo "SKIP: vcan device creation not supported" >&2
+		exit $ksft_skip
+	fi
+	if ! ip -n "$ns" link set dev "$CANIF" up; then
+		echo "FAIL: failed to bring up $CANIF" >&2
+		exit $ksft_fail
+	fi
+}
+
+cleanup()
+{
+	cleanup_all_ns
+}
+
+test_vcan_stats()
+{
+	local rc
+
+	ip netns exec "$ns" ./test_vcan_stats
+	rc=$?
+	if ((rc == ksft_skip)); then
+		log_test_skip "test_vcan_stats"
+	else
+		RET=$rc
+		log_test "test_vcan_stats"
+	fi
+}
+
+trap cleanup EXIT
+setup
+
+tests_run
+
+exit $EXIT_STATUS
-- 
2.34.1


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

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

Thread overview: 6+ 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

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