* [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
` (3 more replies)
0 siblings, 4 replies; 12+ 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] 12+ 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-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 ` (2 subsequent siblings) 3 siblings, 2 replies; 12+ 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] 12+ 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 2026-12-05 0:14 ` netdev-bot+sashiko 1 sibling, 0 replies; 12+ 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] 12+ 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 @ 2026-12-05 0:14 ` netdev-bot+sashiko 1 sibling, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 0:14 UTC (permalink / raw) To: rameshsv06; +Cc: mkl, mailhol, linux-can, rameshkumar.t, kuba Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 2 · Low: 1 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] test_vcan_stats passes the same way on kernels with and without the companion fix "can: vcan: use per-CPU stats to fix KCSAN… - [Medium] test_vcan_stats.sh sets `trap cleanup EXIT` (line 32) before it calls `setup` (line 33). - [Low] The new test_vcan_stats.sh runs `source $net_dir/lib.sh` (line 9, i.e. tools/testing/selftests/net/lib.sh), and net/lib.sh in turn… -- selftests: net: can: add vcan concurrent tx/stats test This adds a kselftest, test_vcan_stats. It starts four concurrent PF_CAN raw transmitters on vcan0 while another thread reads /proc/net/dev. It then checks the exact rx/tx packet and byte counter deltas. A wrapper script creates the vcan interface and deletes it afterwards. > diff --git a/tools/testing/selftests/net/can/Makefile b/tools/testing/selftests/net/can/Makefile > index 5b82e60a03e75..c2e4903b9f43b 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 [Severity: Low] test_vcan_stats.sh sources $net_dir/lib.sh, and net/lib.sh in turn sources lib/sh/defer.sh. Should these files be listed in TEST_INCLUDES here? net/mptcp/Makefile and net/forwarding/Makefile both do this: TEST_INCLUDES := ../lib.sh $(wildcard ../lib/sh/*.sh) Only the parent net target ships net/lib.sh, through its TEST_FILES. So after "make -C tools/testing/selftests TARGETS=net/can install" the installed script can't source lib.sh. That leaves ksft_skip, check_err, log_test and tests_run undefined, and the test doesn't run. test_raw_filter.sh already has the same gap. This patch adds a second script that depends on lib.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 0000000000000..1a4f715adead8 > --- /dev/null > +++ b/tools/testing/selftests/net/can/test_vcan_stats.c > @@ -0,0 +1,181 @@ [ ... ] > +static void *stats_reader(void *arg) > +{ > + struct net_dev_stats st; > + > + while (!atomic_load(&stop_reader)) > + read_if_stats(canif, &st); > + > + return NULL; > +} [ ... ] > + 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); > +} [Severity: Medium] Can these checks ever fail because of the stats race this test seems to target? vcan_setup() sets dev->tx_queue_len = 0, so vcan gets the noqueue qdisc, and vcan does not set lltx. That means every vcan_tx() call is serialized by the txq xmit lock: write()->can_send()->dev_queue_xmit()->__dev_queue_xmit() HARD_TX_LOCK(dev, txq, cpu); dev_hard_start_xmit()->vcan_tx() All the tx and rx counter updates in vcan_tx(), and in vcan_rx() for echo mode, happen inside that one critical section. So the older plain dev->stats increments already gave exact deltas. As far as I can tell, these EXPECT_EQ checks pass the same way with or without the companion change "can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx". That race is between a serialized writer and the lockless /proc/net/dev reader. It only shows up as a KCSAN report in dmesg. stats_reader() throws away every sample and ignores the return value of read_if_stats(). Nothing in the test looks at dmesg for KCSAN reports. Could the commit message say which defect this test is meant to catch, and whether it needs a KCSAN-enabled kernel? Alternatively, could the test check something the concurrent reader side can actually get wrong? [ ... ] > 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 > @@ -0,0 +1,37 @@ [ ... ] > +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] What happens when "ip link add" fails in setup()? Two ways that can happen are vcan0 already existing, or CANIF being exported as a real CAN device such as can0. setup() then runs exit $ksft_skip. The EXIT trap is already armed, so cleanup() runs: ip link set dev $CANIF down ip link delete $CANIF Won't that take down or delete an interface this script didn't create? With CANIF=can0 the hardware CAN interface is set down. With a pre-existing vcan0, the user's interface is deleted. test_raw_filter.sh uses the same CANIF variable and guards the delete with: if [[ $CANIF == vcan* ]]; then The new script has no such guard. It also doesn't record whether it created the link, and it doesn't use a private netns. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203111103.379929-1-rameshsv06%40gmail.com ^ permalink raw reply [flat|nested] 12+ 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 2026-12-05 0:14 ` [PATCH " netdev-bot+sashiko 3 siblings, 0 replies; 12+ 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] 12+ 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 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 3 siblings, 2 replies; 12+ 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] 12+ 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 2026-12-03 12:37 ` sashiko-bot ` (2 more replies) 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 1 sibling, 3 replies; 12+ 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] 12+ messages in thread
* Re: [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test 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 2 siblings, 0 replies; 12+ messages in thread From: sashiko-bot @ 2026-12-03 12:37 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: CAN_RAW SKIP() in test_vcan_stats.c reported as PASS - [Medium] selftests: net: can: raw RET=$rc in test_vcan_stats.sh masks failures as PASS - [Medium] selftests: net: can: barrier race in stats_reader() causes zero reader_reads -- commit f4c5f107871d0554eacce5fe33a073ddcc6bb5a8 Author: T S Rameshkumar <rameshkumar.t@phytecembedded.in> selftests: net: can: add vcan concurrent tx/stats test This commit adds a kselftest (test_vcan_stats) that creates a vcan0 interface in a network namespace, transmits CAN frames from concurrent threads while reading /proc/net/dev in parallel, and verifies monotonic reads and exact 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..aca72cc0995fe > --- /dev/null > +++ b/tools/testing/selftests/net/can/test_vcan_stats.c [ ... ] > +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)) { [Severity: Medium] Can stats_reader() exit without performing any /proc/net/dev reads and cause EXPECT_GT(atomic_load(&reader_reads), 0UL) to fail spuriously? pthread_barrier_wait() only synchronizes arrival, not scheduling order. On a busy host or single-vCPU VM, all tx_worker() threads can finish their 1000 writes to vcan0 and be joined before stats_reader() runs again: stats_reader tx_worker / main pthread_barrier_wait() pthread_barrier_wait() tx_worker() writes all frames and exits main joins tx_worker threads atomic_store(&stop_reader, 1) while (!atomic_load(&stop_reader)) <-- exits with 0 reads EXPECT_GT(reader_reads, 0UL) fails Using a do { ... } while (!atomic_load(&stop_reader)) loop in stats_reader() ensures at least one read completes. [ ... ] > +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"); [Severity: Medium] Does SKIP() here cause test_vcan_stats.sh to report pass instead of skip when CAN_RAW is unavailable? In kselftest_harness.h, __test_passed() treats KSFT_SKIP as passed, so test_harness_run() calls ksft_exit(ret == 0) and exits with 0 instead of KSFT_SKIP (4). test_vcan_stats.sh checks for rc == ksft_skip, so when CONFIG_CAN_VCAN is enabled without CONFIG_CAN_RAW it falls into the else branch and exits 0. Probing the CAN_RAW socket in main() before test_harness_run() and exiting with ksft_exit_skip() would return KSFT_SKIP to the wrapper. [ ... ] > 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..2d182dd47b94f > --- /dev/null > +++ b/tools/testing/selftests/net/can/test_vcan_stats.sh [ ... ] > +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 > +} [Severity: Medium] Can assigning the raw exit status to RET mask failures as a pass in EXIT_STATUS? log_test() merges RET into EXIT_STATUS via ksft_exit_status_merge(), which only assigns weights to ksft_xfail (2), ksft_pass (0), ksft_skip (4), and ksft_fail (1): tools/testing/selftests/net/lib.sh:__ksft_status_merge() { ... if [[ ${weights[$a]} -ge ${weights[$b]} ]]; then echo "$a" return 0 ... } If ./test_vcan_stats crashes or is missing (for example rc=127 or 139), weights[$rc] is empty and evaluates to 0, which loses to weights[0]=1 and leaves EXIT_STATUS at 0. An rc of 2 is also treated as xfail and leaves EXIT_STATUS at 0. Normalizing non-zero rc to ksft_fail (or using check_err $rc with RET=0) avoids masking these failures. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261203120559.421646-1-rameshsv06@gmail.com?part=2 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test 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 2 siblings, 0 replies; 12+ messages in thread From: Oliver Hartkopp @ 2026-12-03 12:50 UTC (permalink / raw) To: T S Rameshkumar, Marc Kleine-Budde, Vincent Mailhol Cc: linux-can, T S Rameshkumar On 03.12.26 13:05, T S Rameshkumar wrote: > 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> Thanks for the "use per-CPU stats" patch for the vcan driver. But I think this selftest is way too much for such a change. Best regards, Oliver > --- > 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 ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 2/2] selftests: net: can: add vcan concurrent tx/stats test 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 2 siblings, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 0:41 UTC (permalink / raw) To: rameshsv06; +Cc: mkl, mailhol, linux-can, rameshkumar.t, kuba Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 3 · Low: 0 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] When CAN_RAW is missing, the test is reported as PASS instead of SKIP. - [Medium] Nothing guarantees that the stats reader overlaps with the transmitters, and the reader can finish with zero reads, which makes… - [Medium] selftests: net: can: raw RET=$rc in test_vcan_stats.sh masks failures as PASS -- selftests: net: can: add vcan concurrent tx/stats test This adds a kselftest that creates vcan0 in a separate network namespace. It runs four PF_CAN raw transmitters while a second thread reads /proc/net/dev, then checks that the counters only increase and that the rx/tx packet and byte deltas match exactly. The test is meant to skip when CAN_RAW or vcan is unavailable. > 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..aca72cc0995fe > --- /dev/null > +++ b/tools/testing/selftests/net/can/test_vcan_stats.c > @@ -0,0 +1,231 @@ [ ... ] > +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"); [Severity: Medium] The commit message says "Skip cleanly if CAN_RAW or vcan is unavailable". Is the CAN_RAW case actually reported as a skip? SKIP() only sets the per-test exit_code to KSFT_SKIP. test_harness_run() then counts a skipped test as passed: tools/testing/selftests/kselftest_harness.h:__test_passed() { return metadata->exit_code != KSFT_FAIL && metadata->exit_code <= KSFT_SKIP; } and it finishes with: ksft_exit(ret == 0); That goes through ksft_exit_pass(), so the binary exits with KSFT_PASS (0) and never with KSFT_SKIP (4). If so, this branch in test_vcan_stats() in test_vcan_stats.sh is never taken for the CAN_RAW case: if ((rc == ksft_skip)); then log_test_skip "test_vcan_stats" else RET=$rc log_test "test_vcan_stats" fi With rc == 0, RET is set to 0 and log_test() reports PASS, even though no frames were sent. This case can happen in practice. net/can/config lists only CONFIG_CAN, CONFIG_CAN_DEV and CONFIG_CAN_VCAN, and CONFIG_CAN_RAW is a separate tristate. If CAN_RAW is disabled or the can-raw module isn't installed, can_create() returns -EPROTONOSUPPORT. The wrapper still creates vcan0 without error in that case. > + ASSERT_GE(probe_sock, 0) > + TH_LOG("failed to create CAN_RAW socket: %d", errno); > + } > + close(probe_sock); [ ... ] > + 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); [Severity: Medium] Can this EXPECT_GT() fail spuriously if the reader thread never runs while the frames are being sent? stats_reader() tests the flag before its first read: pthread_barrier_wait(&start_barrier); while (!atomic_load(&stop_reader)) { int ret = read_if_stats(canif, &st); ... The barrier releases the four tx_worker() threads and the reader together, but nothing orders them after that. Each tx_worker() does 1000 synchronous write() calls on vcan. vcan_concurrent_tx_stats() sets stop_reader as soon as it has joined the tx threads. The reader may not get scheduled before that store, for example on a single vCPU CI VM or under heavy load. Then the loop body never runs, reader_reads stays 0, and this check fails. A related point: even when the reader does run, nothing makes sure that any read happens while transmits are still in progress. Could the check the commit message describes as "checks that concurrent reads succeed monotonically" pass when every read happened after TX had finished? [ ... ] > diff --git a/tools/testing/selftests/net/can/test_vcan_stats.sh b/tools/testing/selftests/net/can/test_vcan_stats.sh > --- /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 [Severity: Medium] Can this else branch hide a failing run? RET is set to the raw exit status of ip netns exec and passed to log_test() without being mapped to a ksft_* code. log_test() in net/lib.sh prints FAIL for any value it does not recognise. It then folds RET into EXIT_STATUS with ksft_exit_status_merge(), which goes through __ksft_status_merge(). __ksft_status_merge() only has weights for ksft_xfail, ksft_pass, ksft_skip and ksft_fail. Any other status gets an empty weight and loses against the weight of ksft_pass, so EXIT_STATUS stays at 0. A status of 2 is logged as XFAIL and also leaves EXIT_STATUS at 0. So the script exits with KSFT_PASS whenever ip netns exec or the binary returns something other than 0, 1 or 4. One example is 128 plus the signal number when test_vcan_stats is killed by a signal. The runner then records a pass even though FAIL was printed. test_raw_filter.sh in the same directory avoids this by passing the status through check_err, which maps a non-zero status to ksft_fail. Would it make sense to do the same here? That would mean setting RET=0 at the start of test_vcan_stats(), since tests_run does not reset it, then calling check_err on rc and keeping the explicit ksft_skip case. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203120559.421646-1-rameshsv06%40gmail.com ^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v2 1/2] can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx 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-05 0:41 ` netdev-bot+sashiko 1 sibling, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 0:41 UTC (permalink / raw) To: rameshsv06; +Cc: mkl, mailhol, linux-can, rameshkumar.t, kuba Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 1 · Low: 0 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [Medium] The fix is incomplete: vcan_tx() still makes a plain, non- atomic dev->stats update. -- can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx This patch converts vcan to NETDEV_PCPU_STAT_TSTATS. The plain dev->stats tx/rx packet and byte updates in vcan_tx() and vcan_rx() become dev_sw_netstats_tx_add() and dev_sw_netstats_rx_add() calls. Reporting goes through the core dev_get_tstats64() path, so the dev->stats.tx_dropped value from can_dropped_invalid_skb() still shows up in the totals. > diff --git a/drivers/net/can/vcan.c b/drivers/net/can/vcan.c > index 76e6b7b5c6a11..0108c674262ee 100644 > --- a/drivers/net/can/vcan.c > +++ b/drivers/net/can/vcan.c [ ... ] > @@ -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); [Severity: Medium] Does this conversion fully close the KCSAN race in vcan_tx()? The return just above comes from the can_dropped_invalid_skb() check at the top of vcan_tx(). For an invalid frame, that helper still does a plain read-modify-write on dev->stats: drivers/net/can/dev/skb.c:can_dropped_invalid_skb() { ... inval_skb: dev_kfree_skb_any(skb); dev->stats.tx_dropped++; return true; } With NETDEV_PCPU_STAT_TSTATS set, the reader path is dev_get_stats()->dev_get_tstats64()->netdev_stats_to_stats64(s, &dev->stats). That reads the same fields with atomic_long_read(), and it can run on any CPU, for example from rtnetlink or /proc/net/dev. Can this still trigger a KCSAN report against the tx path? Two writers can also race here and lose tx_dropped increments. vcan has no ndo_select_queue, and rtnl_create_link() accepts IFLA_NUM_TX_QUEUES. So "ip link add type vcan numtxqueues N" lets vcan_tx() run on several CPUs at once under different txq locks. A CAP_NET_RAW sender can reach this path. One example is an AF_PACKET socket bound to vcan that sends a frame whose skb->protocol is not ETH_P_CAN, ETH_P_CANFD or ETH_P_CANXL, or whose length is wrong. Could can_dropped_invalid_skb() use DEV_STATS_INC(dev, tx_dropped)? If not, could the commit message say this counter is out of scope? Right now it only says: and dev->stats.tx_dropped updated by can_dropped_invalid_skb() is preserved. [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203120559.421646-1-rameshsv06%40gmail.com ^ permalink raw reply [flat|nested] 12+ 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 ` (2 preceding siblings ...) 2026-12-03 12:05 ` [PATCH v2 " T S Rameshkumar @ 2026-12-05 0:14 ` netdev-bot+sashiko 3 siblings, 0 replies; 12+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 0:14 UTC (permalink / raw) To: rameshsv06; +Cc: mkl, mailhol, linux-can, rameshkumar.t, kuba Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 0 · Low: 1 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] The patch only partly fixes the KCSAN data race in vcan_tx() that it claims to fix. - [Low] can: vcan: redundant ndo_get_stats64 callback with NETDEV_PCPU_STAT_TSTATS -- can: vcan: use per-CPU stats to fix KCSAN data-race in vcan_tx This commit moves the vcan tx/rx packet and byte counters from plain dev->stats updates to per-CPU tstats with dev_sw_netstats_tx_add() and dev_sw_netstats_rx_add(). It also installs dev_get_tstats64() as ndo_get_stats64, so dev->stats.tx_dropped is still reported. > 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 [ ... ] > @@ -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); [Severity: High] Does this fully fix the data race in vcan_tx() that the subject refers to? Just above this, vcan_tx() still calls can_dropped_invalid_skb(). For an invalid skb, that helper does a plain read-modify-write on dev->stats: drivers/net/can/dev/skb.c:can_dropped_invalid_skb() { ... inval_skb: dev_kfree_skb_any(skb); dev->stats.tx_dropped++; return true; } With dev_get_tstats64() installed as ndo_get_stats64, the reader still goes through netdev_stats_to_stats64(s, &dev->stats). That function reads each field with atomic_long_read(). So the plain tx_dropped++ can still race with dev_get_stats(), which is reached from /proc/net/dev, rtnetlink or sysfs: vcan_tx() can_dropped_invalid_skb() dev->stats.tx_dropped++ dev_get_stats() dev_get_tstats64() netdev_stats_to_stats64() atomic_long_read() KCSAN treats ++ as a compound read-write access, so it would still report this race under the default config. vcan can be created with several TX queues and is not LLTX. Could two CPUs then run this increment at the same time and lose updates? The commit message relies on this counter: "dev_get_tstats64() also preserves dev->stats.tx_dropped updated by can_dropped_invalid_skb()" Would it make sense to convert can_dropped_invalid_skb() to DEV_STATS_INC(dev, tx_dropped) as part of this fix? An invalid frame sent to a vcan device through AF_PACKET, for example one with the wrong length or protocol, would reach this path. The selftest in the series ("selftests: net: can: add vcan concurrent tx/stats test") only sends valid CAN_RAW frames, so it does not exercise this path. [ ... ] > @@ -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 = { [Severity: Low] Is the explicit ndo_get_stats64 assignment needed here? vcan_setup() now sets dev->pcpu_stat_type to NETDEV_PCPU_STAT_TSTATS. With that set, dev_get_stats() in net/core/dev.c already calls dev_get_tstats64() itself when the driver provides neither ndo_get_stats64 nor ndo_get_stats: net/core/dev.c:dev_get_stats() { ... } else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) { dev_get_tstats64(dev, storage); ... } Because the callback is set, dev_get_stats() takes the ops->ndo_get_stats64 branch instead. That branch does a memset(storage, 0, sizeof(*storage)) and an indirect call. Then dev_get_tstats64() overwrites and zero-pads the whole buffer through netdev_stats_to_stats64() anyway, so the memset is wasted work. The reported values are identical either way, so this is only a small cleanup. Drivers that opt into NETDEV_PCPU_STAT_TSTATS normally leave ndo_get_stats64 unset and let the core handle the collection. Could this line be dropped, with the commit message updated so it no longer says dev_get_tstats64() is wired up explicitly? The core path still goes through dev_get_tstats64(), so dev->stats.tx_dropped from can_dropped_invalid_skb() is still reported. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203111103.379929-1-rameshsv06%40gmail.com ^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-05 0:42 UTC | newest] Thread overview: 12+ 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-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 ` [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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox