[RFC PATCH v2 3/8] vhost-net: allow configuring extended features
From: Paolo Abeni <pabeni@redhat.com>
Date: 2025-05-30 14:50:07
Subsystem:
the rest, virtio host (vhost) · Maintainers:
Linus Torvalds, "Michael S. Tsirkin", Jason Wang
Use the extended feature type for 'acked_features' and implement two new ioctls operation allowing the user-space to set/query an unbounded amount of features. The actual number of processed features is limited by virtio_features_t size, and attempts to set features above such limit fail with EOPNOTSUPP. Note that the legacy ioctls implicitly truncate the negotiated features to the lower 64 bits range. Signed-off-by: Paolo Abeni <pabeni@redhat.com> --- v1 -> v2: - change the ioctl to use an extensible API --- drivers/vhost/net.c | 61 ++++++++++++++++++++++++++++++-- drivers/vhost/vhost.h | 2 +- include/uapi/linux/vhost.h | 7 ++++ include/uapi/linux/vhost_types.h | 5 +++ 4 files changed, 71 insertions(+), 4 deletions(-)
diff --git a/drivers/vhost/net.c b/drivers/vhost/net.c
index 7cbfc7d718b3..f53294440695 100644
--- a/drivers/vhost/net.c
+++ b/drivers/vhost/net.c@@ -77,6 +77,8 @@ enum { (1ULL << VIRTIO_F_RING_RESET) }; +#define VHOST_NET_ALL_FEATURES VHOST_NET_FEATURES + enum { VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) };
@@ -1614,7 +1616,7 @@ static long vhost_net_reset_owner(struct vhost_net *n) return err; } -static int vhost_net_set_features(struct vhost_net *n, u64 features) +static int vhost_net_set_features(struct vhost_net *n, virtio_features_t features) { size_t vhost_hlen, sock_hlen, hdr_len; int i;
@@ -1685,8 +1687,9 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl, void __user *argp = (void __user *)arg; u64 __user *featurep = argp; struct vhost_vring_file backend; - u64 features; - int r; + virtio_features_t all_features; + u64 features, count; + int r, i; switch (ioctl) { case VHOST_NET_SET_BACKEND:
@@ -1704,6 +1707,58 @@ static long vhost_net_ioctl(struct file *f, unsigned int ioctl, if (features & ~VHOST_NET_FEATURES) return -EOPNOTSUPP; return vhost_net_set_features(n, features); + case VHOST_GET_FEATURES_ARRAY: + { + if (copy_from_user(&count, argp, sizeof(u64))) + return -EFAULT; + + /* Copy the net features, up to the user-provided buffer size */ + all_features = VHOST_NET_ALL_FEATURES; + for (i = 0; i < min(VIRTIO_FEATURES_WORDS / 2, count); ++i) { + argp += sizeof(u64); + features = all_features >> (64 * i); + if (copy_to_user(argp, &features, sizeof(u64))) + return -EFAULT; + } + + /* Zero the trailing space provided by user-space, if any */ + features = 0; + for (; i < count; ++i) { + argp += sizeof(u64); + if (copy_to_user(argp, &features, sizeof(u64))) + return -EFAULT; + } + return 0; + } + case VHOST_SET_FEATURES_ARRAY: + { + if (copy_from_user(&count, argp, sizeof(u64))) + return -EFAULT; + + all_features = 0; + for (i = 0; i < min(count, VIRTIO_FEATURES_WORDS / 2); ++i) { + argp += sizeof(u64); + if (copy_from_user(&features, argp, sizeof(u64))) + return -EFAULT; + + all_features |= ((virtio_features_t)features) << (64 * i); + } + + /* Any feature specified by user-space above VIRTIO_FEATURES_MAX is + * not supported by definition. + */ + for (; i < count; ++i) { + if (copy_from_user(&features, argp, sizeof(u64))) + return -EFAULT; + if (features) + return -EOPNOTSUPP; + } + + if (all_features & ~VHOST_NET_ALL_FEATURES) + return -EOPNOTSUPP; + + return vhost_net_set_features(n, all_features); + } case VHOST_GET_BACKEND_FEATURES: features = VHOST_NET_BACKEND_FEATURES; if (copy_to_user(featurep, &features, sizeof(features)))
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index bb75a292d50c..ef1c7fd6f4e1 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h@@ -133,7 +133,7 @@ struct vhost_virtqueue { struct vhost_iotlb *umem; struct vhost_iotlb *iotlb; void *private_data; - u64 acked_features; + virtio_features_t acked_features; u64 acked_backend_features; /* Log write descriptors */ void __user *log_base;
diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h
index d4b3e2ae1314..d6ad01fbb8d2 100644
--- a/include/uapi/linux/vhost.h
+++ b/include/uapi/linux/vhost.h@@ -235,4 +235,11 @@ */ #define VHOST_VDPA_GET_VRING_SIZE _IOWR(VHOST_VIRTIO, 0x82, \ struct vhost_vring_state) + +/* Extended features manipulation */ +#define VHOST_GET_FEATURES_ARRAY _IOR(VHOST_VIRTIO, 0x83, \ + struct vhost_features_array) +#define VHOST_SET_FEATURES_ARRAY _IOW(VHOST_VIRTIO, 0x83, \ + struct vhost_features_array) + #endif
diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h
index d7656908f730..3f227114c557 100644
--- a/include/uapi/linux/vhost_types.h
+++ b/include/uapi/linux/vhost_types.h@@ -110,6 +110,11 @@ struct vhost_msg_v2 { }; }; +struct vhost_features_array { + __u64 count; /* number of entries present in features array */ + __u64 features[]; +}; + struct vhost_memory_region { __u64 guest_phys_addr; __u64 memory_size; /* bytes */
--
2.49.0