Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Antonio Quartulli
Fri, 13 Nov 2026 21:22
[PATCH net v6 0/2] tun: bound the NAPI receive queue
Author: Antonio Quartulli
Date: Fri, 13 Nov 2026 21:22
Date: Fri, 13 Nov 2026 21:22
82 lines
12853 bytes
12853 bytes
With IFF_NAPI, tun queues written packets for NAPI on an unbounded list, which an unprivileged user can grow until the system runs out of memory. Patch 1 bounds it by netdev_max_backlog; patch 2 adds a kselftest for the vhost-net paths into that queue. v6: - Complete the zerocopy buffer of a dropped packet with net_zcopy_put(), as a success, so that vhost-net does not fall back to copying (Willem) - selftest: slim it down, and add a wrapper that loads vhost_net and also runs the zerocopy variant (Willem) - Rebase on net v5: https://lore.kernel.org/netdev/20261111014208.1795777-1-antonio@mandelbit.com/ - Take at most budget packets in the NAPI poll instead of keeping the leftovers on a separate queue (Willem) - Keep only the lockless check, before allocating, also in tun_xdp_one() (Willem) - Say that an OOM is still possible with large packets and many queues, if hard to reach in practice (Sashiko) - selftest: little-endian ring, buffers returned in any order, lower netdev_max_backlog only after setup and restore it first, any non-zero experimental_zcopytx, .gitignore entry (Sashiko) - Rebase on net, which conflicted in selftests/net/config v4: https://lore.kernel.org/netdev/20261103142232.1869185-1-antonio@mandelbit.com/ - Keep the poll's leftovers on a separate queue, like process_backlog(): v3 did not bound the queue when the poll ran on another CPU (Sashiko) - Schedule NAPI when dropping: with netdev_max_backlog below NAPI_POLL_WEIGHT, MSG_MORE senders (vhost-net) stalled the queue (Sashiko) - Use goto free_skb for the locked drop (Willem) - Add a vhost-net kselftest (patch 2); the tun kselftest passes (Willem) - Cc vhost, kselftest and XDP maintainers and the Fixes author/acker v3: https://lore.kernel.org/netdev/20261031231444.1528758-1-antonio@mandelbit.com/ - Also bound the sendmsg() paths, which share the queue: zerocopy buffers are completed when dropped early, and the batched XDP buffers of tun_xdp_one() are covered too (Willem) - Drop the IFF_NAPI_FRAGS exclusion (Willem) - Recheck under the queue lock before queueing (Willem) - Compare with > like enqueue_to_backlog() (Willem) - Keep the includes sorted (Willem) - Add the reproducer and a testing statement to the commit message v2: https://lore.kernel.org/netdev/20261030152540.1409884-1-antonio@mandelbit.com/ - Cc netdev and the maintainers Testing, in an x86-64 VM with 1 and 2 vCPUs, on net 7e1392a42ea2: - Reproducer from patch 1, 15 seconds: memory use stays flat, no OOM; forwarding rate unchanged (113755 packets in 3 s without the patch, 113990 with it). - tun_vhost (patch 2), through the wrapper and with vhost_net experimental_zcopytx=0 and =1: passes with patch 1. Without it, every variant fails; removing the tun_xdp_one() check fails the batch variant, removing the zerocopy completion fails the zerocopy one, and removing the NAPI scheduling on drop fails the copy and zerocopy ones. - tun kselftest: pass:91 fail:0 xfail:12, as without the patch. Notes: - Affected since 943170998b20 (v4.15). - Mitigation: disallow unprivileged user namespaces or restrict access to /dev/net/tun. Antonio Quartulli (2): tun: bound the NAPI receive queue selftests: net: add a vhost-net test for the tun NAPI queue bound drivers/net/tun.c | 55 +++++- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/config | 1 + tools/testing/selftests/net/tun_vhost.c | 222 +++++++++++++++++++++++ tools/testing/selftests/net/tun_vhost.sh | 23 +++ 6 files changed, 296 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/net/tun_vhost.c create mode 100755 tools/testing/selftests/net/tun_vhost.sh base-commit: ead6fa9a0ab194f02a2f36d634b193e9c5f9a1c6 -- 2.56.0
[PATCH net v6 2/2] selftests: net: add a vhost-net test for the tun NAPI queue bound
Author: Antonio Quartulli
Date: Fri, 13 Nov 2026 21:22
Date: Fri, 13 Nov 2026 21:22
324 lines
19480 bytes
19480 bytes
Feed a tap device with IFF_NAPI from vhost-net with netdev_max_backlog
set to 8, in the three ways vhost-net passes packets to tun: batches of
XDP buffers, single packets with MSG_MORE and zerocopy buffers. Check
that excess packets are dropped and counted, that the queue keeps
draining and that every buffer comes back.
The wrapper loads vhost_net and, if it can reload it, also runs the
zerocopy variant with experimental_zcopytx=1.
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
tools/testing/selftests/net/.gitignore | 1 +
tools/testing/selftests/net/Makefile | 2 +
tools/testing/selftests/net/config | 1 +
tools/testing/selftests/net/tun_vhost.c | 222 +++++++++++++++++++++++
tools/testing/selftests/net/tun_vhost.sh | 23 +++
5 files changed, 249 insertions(+)
create mode 100644 tools/testing/selftests/net/tun_vhost.c
create mode 100755 tools/testing/selftests/net/tun_vhost.sh
diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore
index 27761239d069..2f4104b1aa6d 100644
--- a/tools/testing/selftests/net/.gitignore
+++ b/tools/testing/selftests/net/.gitignore
@@ -56,6 +56,7 @@ timestamping
tls
tools
tun
+tun_vhost
txring_overwrite
txtimestamp
udp_splice_checksum
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index baea3e7f0f8f..b3965a976451 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -123,6 +123,7 @@ TEST_PROGS := \
test_vxlan_vnifiltering.sh \
tfo_passive.sh \
traceroute.sh \
+ tun_vhost.sh \
txtimestamp.sh \
udpgro.sh \
udpgro_bench.sh \
@@ -177,6 +178,7 @@ TEST_GEN_FILES := \
tcp_mmap \
tfo \
timestamping \
+ tun_vhost \
txring_overwrite \
txtimestamp \
udpgso \
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index 3e7ce41bd64d..c3bb0d572325 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -136,6 +136,7 @@ CONFIG_TRACEPOINTS=y
CONFIG_TUN=y
CONFIG_USER_NS=y
CONFIG_VETH=y
+CONFIG_VHOST_NET=m
CONFIG_VLAN_8021Q=y
CONFIG_VXLAN=m
CONFIG_XFRM_INTERFACE=m
diff --git a/tools/testing/selftests/net/tun_vhost.c b/tools/testing/selftests/net/tun_vhost.c
new file mode 100644
index 000000000000..c9bf6d07cc26
--- /dev/null
+++ b/tools/testing/selftests/net/tun_vhost.c
@@ -0,0 +1,222 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Feed a tap device with IFF_NAPI from vhost-net, in batches of XDP buffers,
+ * as single packets with MSG_MORE, or as zerocopy buffers, with
+ * netdev_max_backlog set to 8: excess packets must be dropped and counted,
+ * the queue must keep draining, and every buffer must come back.
+ *
+ * Author: Antonio Quartulli <antonio@mandelbit.com>
+ */
+
+#define _GNU_SOURCE
+#include <endian.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+#include <linux/vhost.h>
+#include <linux/virtio_config.h>
+#include <linux/virtio_net.h>
+#include <linux/virtio_ring.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+
+#include "kselftest_harness.h"
+
+#define RING 256
+#define BUFSZ 2048
+#define HDRLEN sizeof(struct virtio_net_hdr_v1)
+#define PKTS (64 * RING)
+
+static long read_long(const char *fmt, const char *arg, long def)
+{
+ char path[128];
+ long v = def;
+ FILE *f;
+
+ snprintf(path, sizeof(path), fmt, arg);
+ f = fopen(path, "r");
+ if (f) {
+ if (fscanf(f, "%ld", &v) != 1)
+ v = def;
+ fclose(f);
+ }
+ return v;
+}
+
+FIXTURE(tun_vhost)
+{
+ char ifname[IFNAMSIZ];
+ char bl[16]; /* saved netdev_max_backlog */
+ int bl_fd;
+ struct vring vr;
+ int vhost;
+ int kick;
+ int tap;
+};
+
+FIXTURE_VARIANT(tun_vhost)
+{
+ bool limit_sndbuf; /* no XDP batches */
+ bool zerocopy;
+ int pktlen;
+};
+
+FIXTURE_VARIANT_ADD(tun_vhost, batch)
+{
+ .pktlen = 64,
+};
+
+FIXTURE_VARIANT_ADD(tun_vhost, copy)
+{
+ .limit_sndbuf = true,
+ .pktlen = 64,
+};
+
+FIXTURE_VARIANT_ADD(tun_vhost, zerocopy)
+{
+ .limit_sndbuf = true,
+ .zerocopy = true,
+ .pktlen = 1000,
+};
+
+FIXTURE_SETUP(tun_vhost)
+{
+ struct ifreq ifr = { .ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR |
+ IFF_NAPI };
+ unsigned long long features = 1ULL << VIRTIO_F_VERSION_1;
+ struct vhost_vring_state state = { .index = 1, .num = RING };
+ struct vhost_vring_file file = { .index = 1 };
+ struct vhost_vring_addr addr = { .index = 1 };
+ int hdrlen = HDRLEN, sndbuf = 1 << 20, i, fd;
+ struct vhost_memory *mem;
+ unsigned char *bufs;
+ void *ring;
+ bool zc;
+
+ self->bl_fd = -1;
+ self->vhost = open("/dev/vhost-net", O_RDWR);
+ if (self->vhost < 0)
+ SKIP(return, "no /dev/vhost-net");
+ zc = read_long("/sys/module/vhost_net/parameters/%s",
+ "experimental_zcopytx", 0);
+ if (variant->zerocopy && !zc)
+ SKIP(return, "vhost_net experimental_zcopytx not set");
+ if (!variant->limit_sndbuf && zc)
+ SKIP(return, "vhost_net does not batch with zerocopy");
+
+ self->tap = open("/dev/net/tun", O_RDWR);
+ ASSERT_GE(self->tap, 0);
+ strcpy(ifr.ifr_name, "tvh%d");
+ ASSERT_EQ(ioctl(self->tap, TUNSETIFF, &ifr), 0);
+ memcpy(self->ifname, ifr.ifr_name, IFNAMSIZ);
+ ASSERT_EQ(ioctl(self->tap, TUNSETVNETHDRSZ, &hdrlen), 0);
+ if (variant->limit_sndbuf)
+ ASSERT_EQ(ioctl(self->tap, TUNSETSNDBUF, &sndbuf), 0);
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ ASSERT_EQ(ioctl(fd, SIOCGIFFLAGS, &ifr), 0);
+ ifr.ifr_flags |= IFF_UP;
+ ASSERT_EQ(ioctl(fd, SIOCSIFFLAGS, &ifr), 0);
+ close(fd);
+
+ /* each buffer: vnet header and an IPv4 frame for another host, which
+ * the stack drops without counting it
+ */
+ ASSERT_EQ(posix_memalign((void **)&bufs, 4096, RING * BUFSZ), 0);
+ memset(bufs, 0, RING * BUFSZ);
+ ASSERT_EQ(posix_memalign(&ring, 4096, vring_size(RING, 4096)), 0);
+ memset(ring, 0, vring_size(RING, 4096));
+ vring_init(&self->vr, RING, ring, 4096);
+ for (i = 0; i < RING; i++) {
+ unsigned char *eth = bufs + i * BUFSZ + HDRLEN;
+
+ eth[0] = 0x02;
+ eth[5] = 0x02;
+ eth[12] = 0x08;
+ eth[14] = 0x45;
+ self->vr.desc[i].addr = htole64((unsigned long)eth - HDRLEN);
+ self->vr.desc[i].len = htole32(HDRLEN + variant->pktlen);
+ self->vr.avail->ring[i] = htole16(i);
+ }
+
+ /* buffer addresses are passed as is: map them 1:1 */
+ mem = calloc(1, sizeof(*mem) + sizeof(mem->regions[0]));
+ ASSERT_NE(mem, NULL);
+ mem->nregions = 1;
+ mem->regions[0].guest_phys_addr = (unsigned long)bufs;
+ mem->regions[0].userspace_addr = (unsigned long)bufs;
+ mem->regions[0].memory_size = RING * BUFSZ;
+
+ self->kick = eventfd(0, 0);
+ ASSERT_GE(self->kick, 0);
+ addr.desc_user_addr = (unsigned long)self->vr.desc;
+ addr.avail_user_addr = (unsigned long)self->vr.avail;
+ addr.used_user_addr = (unsigned long)self->vr.used;
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_OWNER, NULL), 0);
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_FEATURES, &features), 0);
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_MEM_TABLE, mem), 0);
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_VRING_NUM, &state), 0);
+ state.num = 0;
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_VRING_BASE, &state), 0);
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_VRING_ADDR, &addr), 0);
+ file.fd = self->kick;
+ ASSERT_EQ(ioctl(self->vhost, VHOST_SET_VRING_KICK, &file), 0);
+ file.fd = self->tap;
+ ASSERT_EQ(ioctl(self->vhost, VHOST_NET_SET_BACKEND, &file), 0);
+ free(mem);
+}
+
+FIXTURE_TEARDOWN(tun_vhost)
+{
+ /* restore first: closing vhost blocks if buffers are not returned */
+ if (self->bl_fd >= 0 &&
+ pwrite(self->bl_fd, self->bl, strlen(self->bl), 0) < 0)
+ TH_LOG("cannot restore netdev_max_backlog to %s", self->bl);
+ close(self->vhost);
+ close(self->tap);
+}
+
+TEST_F(tun_vhost, napi_queue_bounded)
+{
+ const char *stat = "/sys/class/net/%s/statistics/rx_packets";
+ const char *drop = "/sys/class/net/%s/statistics/rx_dropped";
+ long rx = read_long(stat, self->ifname, -1);
+ long dropped = read_long(drop, self->ifname, -1);
+ unsigned short idx = 0;
+ int n, ms;
+
+ /* lowered only here, so that a failed setup leaves it alone */
+ self->bl_fd = open("/proc/sys/net/core/netdev_max_backlog", O_RDWR);
+ ASSERT_GE(self->bl_fd, 0);
+ n = pread(self->bl_fd, self->bl, sizeof(self->bl) - 1, 0);
+ ASSERT_GT(n, 0);
+ self->bl[n] = '\0';
+ ASSERT_EQ(pwrite(self->bl_fd, "8", 1, 0), 1);
+
+ /* post the whole ring, wait for every buffer to come back, repeat */
+ for (n = 0; n < PKTS; n += RING) {
+ idx += RING;
+ __atomic_store_n(&self->vr.avail->idx, htole16(idx),
+ __ATOMIC_RELEASE);
+ ASSERT_EQ(eventfd_write(self->kick, 1), 0);
+ for (ms = 0; le16toh(__atomic_load_n(&self->vr.used->idx,
+ __ATOMIC_ACQUIRE)) != idx;
+ ms++) {
+ ASSERT_LT(ms, 5000) TH_LOG("buffers not returned");
+ usleep(1000);
+ }
+ }
+
+ rx = read_long(stat, self->ifname, -1) - rx;
+ dropped = read_long(drop, self->ifname, -1) - dropped;
+ TH_LOG("sent %d, delivered %ld, dropped %ld", PKTS, rx, dropped);
+ EXPECT_GT(dropped, 0); /* bounded */
+ EXPECT_GE(rx, PKTS / 16); /* draining while the sender sends */
+ EXPECT_EQ(rx + dropped, PKTS);
+}
+
+TEST_HARNESS_MAIN
diff --git a/tools/testing/selftests/net/tun_vhost.sh b/tools/testing/selftests/net/tun_vhost.sh
new file mode 100755
index 000000000000..d92707de5e29
--- /dev/null
+++ b/tools/testing/selftests/net/tun_vhost.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Run tun_vhost with vhost_net loaded, and once more with zerocopy enabled
+# if the module can be reloaded.
+
+ksft_skip=4
+param=/sys/module/vhost_net/parameters/experimental_zcopytx
+
+modprobe -q vhost_net
+[ -e /dev/vhost-net ] || exit $ksft_skip
+
+./tun_vhost
+ret=$?
+
+zc=$(cat "$param" 2>/dev/null)
+if [ "$zc" = 0 ] && modprobe -q -r vhost_net 2>/dev/null; then
+ modprobe vhost_net experimental_zcopytx=1 &&
+ { ./tun_vhost -v zerocopy || ret=1; }
+ modprobe -q -r vhost_net
+ modprobe vhost_net experimental_zcopytx="$zc"
+fi
+exit $ret
--
2.56.0
Re: [PATCH net v6 2/2] selftests: net: add a vhost-net test for the tun NAPI queue bound
Author: netdev-bot+sashiko
Date: Tue, 17 Nov 2026 22:17
Date: Tue, 17 Nov 2026 22:17
276 lines
17972 bytes
17972 bytes
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 4 · Low: 2
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] In tools/testing/selftests/net/tun_vhost.c, TEST_F(tun_vhost,
napi_queue_bounded) opens /proc/sys/net/core/netdev_max_backlog at…
- [Medium] napi_queue_bounded() writes "8" to the system-wide
/proc/sys/net/core/netdev_max_backlog (line 198) and restores it only…
- [Medium] tools/testing/selftests/net/tun_vhost.sh does not reliably
restore vhost_net's module parameter.
- [Medium] In the zerocopy variant, when ASSERT_LT(ms, 5000) fires
because buffers were not returned, __bail() runs FIXTURE_TEARDOWN,…
- [Low] The test is named after the tun NAPI queue bound, but it cannot
catch a regression in the poll half of that fix.
- [Low] The comment in tun_vhost.c FIXTURE_SETUP (lines 126-128) says the
IPv4 frame for another host is one "which the stack drops without…
--
selftests: net: add a vhost-net test for the tun NAPI queue bound
This adds a kselftest that feeds an IFF_NAPI tap device from vhost-net
with netdev_max_backlog lowered to 8, covering XDP batches, MSG_MORE
single packets and zerocopy buffers. It checks that excess packets are
dropped and counted, that the queue keeps draining and that every buffer
comes back. A wrapper script loads vhost_net and, if possible, reloads it
with experimental_zcopytx=1 for the zerocopy variant.
> diff --git a/tools/testing/selftests/net/tun_vhost.c b/tools/testing/selftests/net/tun_vhost.c
> new file mode 100644
> index 0000000000000..c9bf6d07cc265
> --- /dev/null
> +++ b/tools/testing/selftests/net/tun_vhost.c
> @@ -0,0 +1,222 @@
[ ... ]
> +FIXTURE_SETUP(tun_vhost)
> +{
[ ... ]
> + fd = socket(AF_INET, SOCK_DGRAM, 0);
> + ASSERT_EQ(ioctl(fd, SIOCGIFFLAGS, &ifr), 0);
> + ifr.ifr_flags |= IFF_UP;
> + ASSERT_EQ(ioctl(fd, SIOCSIFFLAGS, &ifr), 0);
> + close(fd);
[Severity: Low]
Would this test notice a regression in the poll side of the tun NAPI
queue bound, the "dequeue at most budget" change in tun_napi_receive()?
The tap is brought up here, but threaded NAPI is never turned on through
/sys/class/net/<ifname>/threaded. So the NAPI poll runs inline from the
vhost worker's local_bh_enable(), serialised with the writer, and the
queue is already empty when the next packet arrives.
Turning on threaded NAPI would not be enough either. netdev_max_backlog=8
is below the NAPI budget of 64. The old behaviour (splice the whole
queue, process budget, splice the rest back) would process everything it
took out and never put anything back.
The checks at the end of napi_queue_bounded():
EXPECT_GT(dropped, 0);
EXPECT_GE(rx, PKTS / 16);
EXPECT_EQ(rx + dropped, PKTS);
would all still pass if that tun_napi_receive() change were reverted. The
drop threshold and the schedule-NAPI-on-drop changes do look covered.
> +
> + /* each buffer: vnet header and an IPv4 frame for another host, which
> + * the stack drops without counting it
> + */
[Severity: Low]
Is this comment accurate? The destination MAC 02:00:00:00:00:02 makes
eth_type_trans() mark the frame PACKET_OTHERHOST, and ip_rcv_core()
counts it:
net/ipv4/ip_input.c:ip_rcv_core() {
...
if (skb->pkt_type == PACKET_OTHERHOST) {
dev_core_stats_rx_otherhost_dropped_inc(skb->dev);
drop_reason = SKB_DROP_REASON_OTHERHOST;
goto drop;
}
...
}
dev_get_stats() reports this as rx_otherhost_dropped. The drop is
counted, just not in rx_dropped, which is the only counter the test
reads.
[ ... ]
> +FIXTURE_TEARDOWN(tun_vhost)
> +{
> + /* restore first: closing vhost blocks if buffers are not returned */
> + if (self->bl_fd >= 0 &&
> + pwrite(self->bl_fd, self->bl, strlen(self->bl), 0) < 0)
> + TH_LOG("cannot restore netdev_max_backlog to %s", self->bl);
> + close(self->vhost);
[Severity: Medium]
In the zerocopy variant, can this close() leave the test process stuck
in D state for good?
When ASSERT_LT(ms, 5000) fires because buffers were not returned, the
teardown runs while skbs in the tap's NAPI queue still hold zerocopy
ubufs. The last fput of the vhost fd then goes through:
vhost_net_release()
vhost_net_flush()
vhost_net_ubuf_put_and_wait()
wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
That wait is uninterruptible and has no timeout. Nothing in the teardown
schedules the tap's NAPI, and restoring netdev_max_backlog does not do it
either. The tun file can only be released, which purges the queue, after
sockfd_put(tx_sock). In vhost_net_release() that call comes after the
flush:
vhost_net_stop(n, &tx_sock, &rx_sock);
vhost_net_flush(n);
...
if (tx_sock)
sockfd_put(tx_sock);
One concrete case is a regression in the schedule-NAPI-on-drop part of
the tun fix. The final non-MSG_MORE packet would be dropped without a
napi_schedule(), and several zerocopy skbs would stay queued.
The harness SIGKILL on timeout cannot reap such a task. It keeps
/dev/vhost-net and the tap open, keeps vhost_net from being unloaded
(which also breaks the restore in tun_vhost.sh), and triggers hung task
warnings.
Would kicking the queue before close(self->vhost) avoid this? For
example, a write() to the tap fd without MSG_MORE would make
tun_get_user() call napi_schedule().
> + close(self->tap);
> +}
> +
> +TEST_F(tun_vhost, napi_queue_bounded)
> +{
[ ... ]
> + /* lowered only here, so that a failed setup leaves it alone */
> + self->bl_fd = open("/proc/sys/net/core/netdev_max_backlog", O_RDWR);
> + ASSERT_GE(self->bl_fd, 0);
[Severity: Medium]
What happens when this runs outside init_net? netdev_max_backlog is in
net_core_table, which is only registered for init_net:
net/core/sysctl_net_core.c:sysctl_core_init() {
...
register_net_sysctl(&init_net, "net/core", net_core_table);
...
}
The per-netns table does not include it. In any other network namespace
the file is missing, so this open() fails with ENOENT.
run_kselftest.sh -n runs each test in a fresh netns through
run_in_netns() in kselftest/runner.sh. FIXTURE_SETUP still passes there,
because /dev/vhost-net, /dev/net/tun, TUNSETIFF and SIOCSIFFLAGS all work
inside the namespace. In that mode the test reports FAIL rather than
SKIP every time.
Should this SKIP when the sysctl is not present?
> + n = pread(self->bl_fd, self->bl, sizeof(self->bl) - 1, 0);
> + ASSERT_GT(n, 0);
> + self->bl[n] = '\0';
> + ASSERT_EQ(pwrite(self->bl_fd, "8", 1, 0), 1);
> +
> + /* post the whole ring, wait for every buffer to come back, repeat */
> + for (n = 0; n < PKTS; n += RING) {
> + idx += RING;
> + __atomic_store_n(&self->vr.avail->idx, htole16(idx),
> + __ATOMIC_RELEASE);
> + ASSERT_EQ(eventfd_write(self->kick, 1), 0);
> + for (ms = 0; le16toh(__atomic_load_n(&self->vr.used->idx,
> + __ATOMIC_ACQUIRE)) != idx;
> + ms++) {
> + ASSERT_LT(ms, 5000) TH_LOG("buffers not returned");
> + usleep(1000);
> + }
> + }
[Severity: Medium]
Can netdev_max_backlog stay at 8 for the whole system if this loop runs
past the harness timeout?
The only restore is in FIXTURE_TEARDOWN, which runs only in the forked
test child. With FIXTURE_TEARDOWN, unlike FIXTURE_TEARDOWN_PARENT,
_teardown_parent is false, so the parent-side teardown does nothing.
TEST_F uses the default 30 second timeout. On timeout, __wait_for_test()
does:
kill(-(t->pid), SIGKILL);
so no cleanup runs in the child.
The 5000 x usleep(1000) bound resets for each of the PKTS / RING = 64
rings. The body can therefore run for about 320 seconds without any
assertion firing. A drain that is slow but still making progress, for
example on a debug or KASAN kernel, would hit the timeout.
tun_vhost.sh does not save or restore the value either. Later tests and
workloads on the host would then run with netdev_max_backlog=8.
[ ... ]
> diff --git a/tools/testing/selftests/net/tun_vhost.sh b/tools/testing/selftests/net/tun_vhost.sh
> new file mode 100755
> index 0000000000000..d92707de5e29e
> --- /dev/null
> +++ b/tools/testing/selftests/net/tun_vhost.sh
> @@ -0,0 +1,23 @@
[ ... ]
> +modprobe -q vhost_net
> +[ -e /dev/vhost-net ] || exit $ksft_skip
> +
> +./tun_vhost
> +ret=$?
> +
> +zc=$(cat "$param" 2>/dev/null)
> +if [ "$zc" = 0 ] && modprobe -q -r vhost_net 2>/dev/null; then
> + modprobe vhost_net experimental_zcopytx=1 &&
> + { ./tun_vhost -v zerocopy || ret=1; }
> + modprobe -q -r vhost_net
> + modprobe vhost_net experimental_zcopytx="$zc"
> +fi
[Severity: Medium]
Can experimental_zcopytx still be 1 after this script exits?
The parameter is read-only at runtime:
drivers/vhost/net.c:
module_param(experimental_zcopytx, int, 0444);
so only a reload can change it back. The result of the second
modprobe -q -r is not checked. Removal fails if something still holds
/dev/vhost-net open, such as a tun_vhost child stuck in
vhost_net_release() as described above, or another vhost user that
started in the meantime. In that case the next modprobe finds the module
already loaded and returns success without changing anything.
The script also sets no EXIT or INT trap, so interrupting it during the
zerocopy run skips the restore.
A separate point: the first modprobe -q vhost_net always loads the
module, and nothing unloads it afterwards if it was not loaded before.
> +exit $ret
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261113212237.1742984-1-antonio%40mandelbit.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads