]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
vfio: Block device access via device fd until device is opened
authorYi Liu <yi.l.liu@intel.com>
Tue, 18 Jul 2023 13:55:32 +0000 (06:55 -0700)
committerAlex Williamson <alex.williamson@redhat.com>
Tue, 25 Jul 2023 16:18:50 +0000 (10:18 -0600)
Allow the vfio_device file to be in a state where the device FD is
opened but the device cannot be used by userspace (i.e. its .open_device()
hasn't been called). This inbetween state is not used when the device
FD is spawned from the group FD, however when we create the device FD
directly by opening a cdev it will be opened in the blocked state.

The reason for the inbetween state is that userspace only gets a FD but
doesn't gain access permission until binding the FD to an iommufd. So in
the blocked state, only the bind operation is allowed. Completing bind
will allow user to further access the device.

This is implemented by adding a flag in struct vfio_device_file to mark
the blocked state and using a simple smp_load_acquire() to obtain the
flag value and serialize all the device setup with the thread accessing
this device.

Following this lockless scheme, it can safely handle the device FD
unbound->bound but it cannot handle bound->unbound. To allow this we'd
need to add a lock on all the vfio ioctls which seems costly. So once
device FD is bound, it remains bound until the FD is closed.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Tested-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-8-yi.l.liu@intel.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/group.c
drivers/vfio/vfio.h
drivers/vfio/vfio_main.c

index caf53716ddb2ea3ef2d124313a5db984b7dbc732..088dd34c89319d69e13a6861c21b9294b039ade4 100644 (file)
@@ -194,9 +194,18 @@ static int vfio_df_group_open(struct vfio_device_file *df)
        df->iommufd = device->group->iommufd;
 
        ret = vfio_df_open(df);
-       if (ret)
+       if (ret) {
                df->iommufd = NULL;
+               goto out_put_kvm;
+       }
+
+       /*
+        * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
+        * read/write/mmap and vfio_file_has_device_access()
+        */
+       smp_store_release(&df->access_granted, true);
 
+out_put_kvm:
        if (device->open_count == 0)
                vfio_device_put_kvm(device);
 
index 2094f5a4ef045833703e01cfa352153397e52e6e..4478a1e77a5ed7a06c6c667b8e165464a1abb6ed 100644 (file)
@@ -19,6 +19,7 @@ struct vfio_container;
 struct vfio_device_file {
        struct vfio_device *device;
 
+       u8 access_granted;
        spinlock_t kvm_ref_lock; /* protect kvm field */
        struct kvm *kvm;
        struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
index 825b1eeaebe20d39240e5094ae2dc0cf81d7267e..c37fc14599d0e17ec5c98725f4a90d5df34f4be1 100644 (file)
@@ -1129,6 +1129,10 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
        struct vfio_device *device = df->device;
        int ret;
 
+       /* Paired with smp_store_release() following vfio_df_open() */
+       if (!smp_load_acquire(&df->access_granted))
+               return -EINVAL;
+
        ret = vfio_device_pm_runtime_get(device);
        if (ret)
                return ret;
@@ -1156,6 +1160,10 @@ static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
        struct vfio_device_file *df = filep->private_data;
        struct vfio_device *device = df->device;
 
+       /* Paired with smp_store_release() following vfio_df_open() */
+       if (!smp_load_acquire(&df->access_granted))
+               return -EINVAL;
+
        if (unlikely(!device->ops->read))
                return -EINVAL;
 
@@ -1169,6 +1177,10 @@ static ssize_t vfio_device_fops_write(struct file *filep,
        struct vfio_device_file *df = filep->private_data;
        struct vfio_device *device = df->device;
 
+       /* Paired with smp_store_release() following vfio_df_open() */
+       if (!smp_load_acquire(&df->access_granted))
+               return -EINVAL;
+
        if (unlikely(!device->ops->write))
                return -EINVAL;
 
@@ -1180,6 +1192,10 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
        struct vfio_device_file *df = filep->private_data;
        struct vfio_device *device = df->device;
 
+       /* Paired with smp_store_release() following vfio_df_open() */
+       if (!smp_load_acquire(&df->access_granted))
+               return -EINVAL;
+
        if (unlikely(!device->ops->mmap))
                return -EINVAL;