> From: Alex Williamson <alex.williamson@xxxxxxxxxx> > Sent: Friday, January 20, 2023 4:47 AM > > On Tue, 17 Jan 2023 05:49:37 -0800 > Yi Liu <yi.l.liu@xxxxxxxxx> wrote: > > > 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. > > > > In the blocked state, currently only the bind operation is allowed, > > other device accesses are not 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. > > > > Due to this scheme it is not possible to unbind the FD, once it is bound, > > it remains bound until the FD is closed. > > > > Suggested-by: Jason Gunthorpe <jgg@xxxxxxxxxx> > > Signed-off-by: Yi Liu <yi.l.liu@xxxxxxxxx> > > --- > > drivers/vfio/vfio.h | 1 + > > drivers/vfio/vfio_main.c | 29 +++++++++++++++++++++++++++++ > > 2 files changed, 30 insertions(+) > > > > diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h > > index 3d8ba165146c..c69a9902ea84 100644 > > --- a/drivers/vfio/vfio.h > > +++ b/drivers/vfio/vfio.h > > @@ -20,6 +20,7 @@ struct vfio_device_file { > > struct vfio_device *device; > > struct kvm *kvm; > > struct iommufd_ctx *iommufd; > > + bool access_granted; > > }; > > > > void vfio_device_put_registration(struct vfio_device *device); > > diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c > > index 3df71bd9cd1e..d442ebaa4b21 100644 > > --- a/drivers/vfio/vfio_main.c > > +++ b/drivers/vfio/vfio_main.c > > @@ -430,6 +430,11 @@ int vfio_device_open(struct vfio_device_file *df) > > } > > } > > > > + /* > > + * Paired with smp_load_acquire() in vfio_device_fops::ioctl/ > > + * read/write/mmap > > + */ > > + smp_store_release(&df->access_granted, true); > > Why is this happening outside of the first-open branch? Thanks, The reason is the group path allows multiple device fds. But only the first device fd open instance will trigger the first-open branch. But all the device fd open instances need to set the access_granted flag. Surely for the cdev path, this can be moved to the first-open branch as cdev path only allows one device fd open. Regards, Yi Liu