Re: [PATCH] vhost: mask the in-order used-ring log cursor

From: fadouse

Date: Mon Nov 23 2026 - 07:31:50 EST


Hi Jiaming,

> The mask looks right to me. What I don't follow is how `nheads[0]`
> gets to 64 on a 32-entry queue. Those buffers haven't been returned to
> the driver yet, so what can it put back on the ring? Can this happen
> with a conforming driver, or does the test guest reuse descriptors
> that are still in flight?

It is a non-conforming guest reusing in-flight ring slots; a conforming
driver would not advance avail->idx past last_used_idx + vq->num.

vhost accepts this because vhost_get_avail_idx() checks against
vq->last_avail_idx rather than vq->last_used_idx:

if (unlikely((u16)(avail_idx - vq->last_avail_idx) > vq->num))

With vq->num = 32 and an 8180-byte frame (+ 12B vnet hdr = 64 x 128B
buffers) followed by a 64-byte frame (1 buffer):

1. Guest publishes avail->idx = 32 and kicks. get_rx_bufs() runs dry
at 32 buffers and calls vhost_discard_vq_desc(), rewinding
last_avail_idx to 0 while leaving vq->avail_idx = 32 cached.
2. Guest advances avail->idx to 64 and kicks again.
vhost_get_vq_desc_n() consumes cached 0..31 (last_avail_idx -> 32,
last_used_idx still 0), then refreshes avail->idx = 64:
(u16)(64 - 32) == 32 <= vq->num passes, so get_rx_bufs() collects
32..63 into the same frame (nheads[0] = 64).
3. The test polls buf0[12] (with pages 1..31 dropped via
madvise(MADV_DONTNEED) to widen the copy window) and tries to
publish avail->idx = 65 during the frame-0 copy so handle_rx() can
pick up the 1-buffer frame and flush both in a single
vhost_add_used_n_in_order() batch (count = 2, nheads = {64, 1}).

(Minor correction to my commit message: without VIRTIO_RING_F_EVENT_IDX
the used ring is 260 bytes at [0x7ef8, 0x7ffc) rather than 262 bytes;
vq_log_used_access_ok() still validates only log_base[0], and the
unmasked flush logs [0x7ffc, 0x8004) into log_base[1].)

> Could you share the RX test program you used?

Build with `gcc -O2 -Wall -Wextra test_vhost_inorder.c -o test_vhost_inorder`
and run as root (exit 2 = bug reproduced, 0 = fixed, 3 = timing retry):

----8<----
// SPDX-License-Identifier: GPL-2.0
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.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 <net/if.h>
#include <netpacket/packet.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/eventfd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>

#define GUEST_MEM_SIZE (16UL * 1024 * 1024)
#define IOVA_DESC 0x0000
#define IOVA_AVAIL 0x1000
#define IOVA_USED 0x7ef8
#define BUF_IOVA_BASE 0x20000
#define VQ_NUM 32
#define BUF_LEN 128
#define FRAME0_LEN 8180 /* + 12B vnet hdr = 64 * 128B */
#define FRAME1_LEN 64
#define NHEADS0 64
#define TOTAL_AVAIL 65

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint64_t u64;

static void die(const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}

static void publish_avail(struct vring_avail *avail, u16 v)
{
__atomic_store_n(&avail->idx, v, __ATOMIC_RELEASE);
}

static void kick(int efd)
{
u64 one = 1;

if (write(efd, &one, sizeof(one)) != sizeof(one))
die("kick");
}

int main(void)
{
int vfd, tfd, pkt, sock, kick_fd, call_fd, ifindex;
struct ifreq ifr = {};
struct vring_desc *desc;
struct vring_avail *avail;
struct vring_used *used;
struct sockaddr_ll sll = {};
unsigned char frame0[FRAME0_LEN], frame1[FRAME1_LEN];
struct {
struct vhost_memory m;
struct vhost_memory_region r[1];
} memtable = {
.m = { .nregions = 1 },
.r = { { .guest_phys_addr = 0, .memory_size = GUEST_MEM_SIZE } },
};
struct vhost_vring_state vs = {};
struct vhost_vring_addr va = {};
struct vhost_vring_file vf = {};
u64 features, log_base_ptr;
void *guest_mem;
unsigned char *log_base;
u8 *buf0;

guest_mem = mmap(NULL, GUEST_MEM_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
if (guest_mem == MAP_FAILED)
die("mmap guest_mem");
log_base = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
if (log_base == MAP_FAILED)
die("mmap log_base");

desc = (struct vring_desc *)((u8 *)guest_mem + IOVA_DESC);
avail = (struct vring_avail *)((u8 *)guest_mem + IOVA_AVAIL);
used = (struct vring_used *)((u8 *)guest_mem + IOVA_USED);
buf0 = (u8 *)guest_mem + BUF_IOVA_BASE;

for (int i = 0; i < VQ_NUM; i++) {
desc[i] = (struct vring_desc){
.addr = BUF_IOVA_BASE + 4096 * i,
.len = BUF_LEN,
.flags = VRING_DESC_F_WRITE,
};
avail->ring[i] = i;
}
for (int i = 1; i < VQ_NUM; i++)
madvise((u8 *)guest_mem + BUF_IOVA_BASE + 4096 * i,
4096, MADV_DONTNEED);

tfd = open("/dev/net/tun", O_RDWR);
if (tfd < 0)
die("open /dev/net/tun");
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
snprintf(ifr.ifr_name, IFNAMSIZ, "vh%d", getpid() % 1000);
if (ioctl(tfd, TUNSETIFF, &ifr))
die("TUNSETIFF");
{
int hdr_sz = 12;

if (ioctl(tfd, TUNSETVNETHDRSZ, &hdr_sz))
die("TUNSETVNETHDRSZ");
}
{
char path[128];
int fd;

snprintf(path, sizeof(path),
"/proc/sys/net/ipv6/conf/%s/disable_ipv6",
ifr.ifr_name);
fd = open(path, O_WRONLY);
if (fd >= 0) {
if (write(fd, "1", 1) < 0) {}
close(fd);
}
}

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
die("socket");
if (ioctl(sock, SIOCGIFFLAGS, &ifr))
die("SIOCGIFFLAGS");
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
if (ioctl(sock, SIOCSIFFLAGS, &ifr))
die("SIOCSIFFLAGS");
if (ioctl(sock, SIOCGIFINDEX, &ifr))
die("SIOCGIFINDEX");
ifindex = ifr.ifr_ifindex;
ifr.ifr_mtu = 9000;
if (ioctl(sock, SIOCSIFMTU, &ifr))
die("SIOCSIFMTU");

pkt = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (pkt < 0)
die("socket packet");
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_ALL);
sll.sll_ifindex = ifindex;
if (bind(pkt, (struct sockaddr *)&sll, sizeof(sll)))
die("bind");

vfd = open("/dev/vhost-net", O_RDWR);
if (vfd < 0)
die("open /dev/vhost-net");
if (ioctl(vfd, VHOST_SET_OWNER, 0))
die("VHOST_SET_OWNER");
memtable.r[0].userspace_addr = (u64)guest_mem;
if (ioctl(vfd, VHOST_SET_MEM_TABLE, &memtable))
die("VHOST_SET_MEM_TABLE");
log_base_ptr = (u64)log_base;
if (ioctl(vfd, VHOST_SET_LOG_BASE, &log_base_ptr))
die("VHOST_SET_LOG_BASE");

features = (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
(1ULL << VIRTIO_F_IN_ORDER) |
(1ULL << VHOST_F_LOG_ALL);
if (ioctl(vfd, VHOST_SET_FEATURES, &features))
die("VHOST_SET_FEATURES");

kick_fd = eventfd(0, 0);
call_fd = eventfd(0, 0);
if (kick_fd < 0 || call_fd < 0)
die("eventfd");

vs.num = VQ_NUM;
if (ioctl(vfd, VHOST_SET_VRING_NUM, &vs))
die("VHOST_SET_VRING_NUM");
vs.num = 0;
if (ioctl(vfd, VHOST_SET_VRING_BASE, &vs))
die("VHOST_SET_VRING_BASE");

va.flags = 1U << VHOST_VRING_F_LOG;
va.desc_user_addr = (u64)desc;
va.avail_user_addr = (u64)avail;
va.used_user_addr = (u64)used;
va.log_guest_addr = IOVA_USED;
if (ioctl(vfd, VHOST_SET_VRING_ADDR, &va))
die("VHOST_SET_VRING_ADDR");

vf.fd = kick_fd;
if (ioctl(vfd, VHOST_SET_VRING_KICK, &vf))
die("VHOST_SET_VRING_KICK");
vf.fd = call_fd;
if (ioctl(vfd, VHOST_SET_VRING_CALL, &vf))
die("VHOST_SET_VRING_CALL");
vf.fd = tfd;
if (ioctl(vfd, VHOST_NET_SET_BACKEND, &vf))
die("VHOST_NET_SET_BACKEND");

memset(frame0, 0xAB, sizeof(frame0));
memset(frame1, 0xCD, sizeof(frame1));
frame0[12] = frame1[12] = 0x08;
frame0[13] = frame1[13] = 0x00;
if (sendto(pkt, frame0, sizeof(frame0), 0, NULL, 0) != sizeof(frame0))
die("sendto frame0");
if (sendto(pkt, frame1, sizeof(frame1), 0, NULL, 0) != sizeof(frame1))
die("sendto frame1");

publish_avail(avail, VQ_NUM);
kick(kick_fd);
usleep(100 * 1000);
publish_avail(avail, NHEADS0);
kick(kick_fd);

for (int i = 0; i < 5000000 && *(volatile u8 *)(buf0 + 12) == 0; i++)
;
publish_avail(avail, TOTAL_AVAIL);

for (int i = 0; i < 30 && used->idx < TOTAL_AVAIL; i++)
usleep(100 * 1000);

printf("avail->idx=%u used->idx=%u log_base[0]=0x%02x log_base[1]=0x%02x\n",
avail->idx, used->idx, log_base[0], log_base[1]);
if (used->idx != TOTAL_AVAIL)
return 3;
return log_base[1] ? 2 : 0;
}
---->8----

unpatched: avail->idx=65 used->idx=65 log_base[0]=0x80 log_base[1]=0x01 (exit 2)
patched: avail->idx=65 used->idx=65 log_base[0]=0x80 log_base[1]=0x00 (exit 0)

Thanks,
YanXin

Attachment: publickey - fadouse@pm.me - 0x99485AA3.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature