Re: [PATCH v2 4/7] vfio: Introduce VFIO_DEVICE_FEATURE ioctl and first user

From: Alex Williamson
Date: Thu Mar 05 2020 - 15:51:17 EST


On Thu, 27 Feb 2020 18:34:07 +0100
Cornelia Huck <cohuck@xxxxxxxxxx> wrote:

> On Wed, 19 Feb 2020 11:54:18 -0700
> Alex Williamson <alex.williamson@xxxxxxxxxx> wrote:
>
> > The VFIO_DEVICE_FEATURE ioctl is meant to be a general purpose, device
> > agnostic ioctl for setting, retrieving, and probing device features.
> > This implementation provides a 16-bit field for specifying a feature
> > index, where the data porition of the ioctl is determined by the
> > semantics for the given feature. Additional flag bits indicate the
> > direction and nature of the operation; SET indicates user data is
> > provided into the device feature, GET indicates the device feature is
> > written out into user data. The PROBE flag augments determining
> > whether the given feature is supported, and if provided, whether the
> > given operation on the feature is supported.
> >
> > The first user of this ioctl is for setting the vfio-pci VF token,
> > where the user provides a shared secret key (UUID) on a SR-IOV PF
> > device, which users must provide when opening associated VF devices.
> >
> > Signed-off-by: Alex Williamson <alex.williamson@xxxxxxxxxx>
> > ---
> > drivers/vfio/pci/vfio_pci.c | 52 +++++++++++++++++++++++++++++++++++++++++++
> > include/uapi/linux/vfio.h | 37 +++++++++++++++++++++++++++++++
> > 2 files changed, 89 insertions(+)
> >
> > diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> > index 8dd6ef9543ca..e4d5d26e5e71 100644
> > --- a/drivers/vfio/pci/vfio_pci.c
> > +++ b/drivers/vfio/pci/vfio_pci.c
> > @@ -1180,6 +1180,58 @@ static long vfio_pci_ioctl(void *device_data,
> >
> > return vfio_pci_ioeventfd(vdev, ioeventfd.offset,
> > ioeventfd.data, count, ioeventfd.fd);
> > + } else if (cmd == VFIO_DEVICE_FEATURE) {
> > + struct vfio_device_feature feature;
> > + uuid_t uuid;
> > +
> > + minsz = offsetofend(struct vfio_device_feature, flags);
> > +
> > + if (copy_from_user(&feature, (void __user *)arg, minsz))
> > + return -EFAULT;
> > +
> > + if (feature.argsz < minsz)
> > + return -EINVAL;
> > +
> > + if (feature.flags & ~(VFIO_DEVICE_FEATURE_MASK |
> > + VFIO_DEVICE_FEATURE_SET |
> > + VFIO_DEVICE_FEATURE_GET |
> > + VFIO_DEVICE_FEATURE_PROBE))
> > + return -EINVAL;
>
> GET|SET|PROBE is well-defined, but what about GET|SET without PROBE? Do
> we want to fence this in the generic ioctl handler part? Or is there
> any sane way to implement that (read and then write back something?)

I'd be ok with discouraging combinations of GET|SET|!PROBE generically.
I don't think there's an intuitive answer to whether it should be
applied as GET|SET or SET|GET. If some future feature wanted an atomic
op we could add something like a test-and-set. Thanks,

Alex

> > +
> > + switch (feature.flags & VFIO_DEVICE_FEATURE_MASK) {
> > + case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN:
> > + if (!vdev->vf_token)
> > + return -ENOTTY;
> > +
> > + /*
> > + * We do not support GET of the VF Token UUID as this
> > + * could expose the token of the previous device user.
> > + */
> > + if (feature.flags & VFIO_DEVICE_FEATURE_GET)
> > + return -EINVAL;
> > +
> > + if (feature.flags & VFIO_DEVICE_FEATURE_PROBE)
> > + return 0;
> > +
> > + /* Don't SET unless told to do so */
> > + if (!(feature.flags & VFIO_DEVICE_FEATURE_SET))
> > + return -EINVAL;
> > +
> > + if (feature.argsz < minsz + sizeof(uuid))
> > + return -EINVAL;
> > +
> > + if (copy_from_user(&uuid, (void __user *)(arg + minsz),
> > + sizeof(uuid)))
> > + return -EFAULT;
> > +
> > + mutex_lock(&vdev->vf_token->lock);
> > + uuid_copy(&vdev->vf_token->uuid, &uuid);
> > + mutex_unlock(&vdev->vf_token->lock);
> > +
> > + return 0;
> > + default:
> > + return -ENOTTY;
> > + }
> > }
> >
> > return -ENOTTY;
> (...)