[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <9eae960a-0226-46e2-a6f4-95c91800268c@daynix.com>
Date: Sat, 31 May 2025 15:15:12 +0900
From: Akihiko Odaki <akihiko.odaki@...nix.com>
To: Paolo Abeni <pabeni@...hat.com>, netdev@...r.kernel.org
Cc: Willem de Bruijn <willemdebruijn.kernel@...il.com>,
Jason Wang <jasowang@...hat.com>, Andrew Lunn <andrew+netdev@...n.ch>,
"David S. Miller" <davem@...emloft.net>, Eric Dumazet <edumazet@...gle.com>,
Jakub Kicinski <kuba@...nel.org>, "Michael S. Tsirkin" <mst@...hat.com>,
Xuan Zhuo <xuanzhuo@...ux.alibaba.com>, Eugenio Pérez
<eperezma@...hat.com>, Yuri Benditovich <yuri.benditovich@...nix.com>
Subject: Re: [RFC PATCH v2 3/8] vhost-net: allow configuring extended features
On 2025/05/30 23:49, Paolo Abeni wrote:
> 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@...hat.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) {
I think you need to use: array_index_nospec()
> + 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;
There is clear_user().
> + }
> + 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[];
An alternative idea:
#define VHOST_GET_FEATURES_ARRAY(len) _IOC(_IOC_READ, VHOST_VIRTIO,
0x00, (len))
By doing so, the kernel can have share the code for
VHOST_GET_FEATURES_ARRAY() with VHOST_GET_FEATURES() since
VHOST_GET_FEATURES() will be just a specialized definition.
It also makes the life of the userspace a bit easier by not making it
construct struct vhost_features_array.
Looking at include/uapi, it seems there are examples of both your
pattern and my alternative, so please pick what you prefer.
If you are going to keep struct vhost_features_array, you may want to
use __counted_by().
> +};
> +
> struct vhost_memory_region {
> __u64 guest_phys_addr;
> __u64 memory_size; /* bytes */
Powered by blists - more mailing lists