* Re: [Intel-gfx] [PATCH v2 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM
[not found] ` <20220518212607.467538-2-mjrosato@linux.ibm.com>
@ 2022-05-19 6:43 ` Tian, Kevin
2022-05-19 6:48 ` hch
2022-05-19 12:33 ` Jason Gunthorpe
1 sibling, 1 reply; 4+ messages in thread
From: Tian, Kevin @ 2022-05-19 6:43 UTC (permalink / raw)
To: Matthew Rosato, jgg@nvidia.com, alex.williamson@redhat.com
Cc: jjherne@linux.ibm.com, akrowiak@linux.ibm.com,
kvm@vger.kernel.org, linux-s390@vger.kernel.org,
intel-gfx@lists.freedesktop.org, cohuck@redhat.com,
linux-kernel@vger.kernel.org, hch@infradead.org,
pasic@linux.ibm.com, borntraeger@linux.ibm.com,
intel-gvt-dev@lists.freedesktop.org
> From: Matthew Rosato
> Sent: Thursday, May 19, 2022 5:26 AM
>
> Rather than relying on a notifier for associating the KVM with
> the group, let's assume that the association has already been
> made prior to device_open. The first time a device is opened
> associate the group KVM with the device.
>
> This fixes a user-triggerable oops in GVT.
No changelog.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
Not sure whether Christoph wants a s-o-b here when he wrote
the snippet to remove the release work of gvt...
> @@ -1083,11 +1083,22 @@ static struct file *vfio_device_open(struct
> vfio_device *device)
>
> mutex_lock(&device->dev_set->lock);
> device->open_count++;
> + down_read(&device->group->group_rwsem);
> + if (device->open_count == 1 && device->group->kvm) {
> + /*
> + * Here we pass the KVM pointer with the group under the
> read
> + * lock. If the device driver will use it, it must obtain a
> + * reference and release it during close_device.
> + */
> + device->kvm = device->group->kvm;
> + }
> +
> if (device->open_count == 1 && device->ops->open_device) {
Merge the two branches so both are under if (device->open_count == 1) {}
(and group_rwsem can be also moved inside)
> @@ -1315,9 +1330,13 @@ static int vfio_device_fops_release(struct inode
> *inode, struct file *filep)
>
> mutex_lock(&device->dev_set->lock);
> vfio_assert_device_open(device);
> + down_read(&device->group->group_rwsem);
> if (device->open_count == 1 && device->ops->close_device)
> device->ops->close_device(device);
> device->open_count--;
> + if (device->open_count == 0 && device->kvm)
> + device->kvm = NULL;
This can be moved out of group_rwsem as there is no reference to
vfio_group.
> + up_read(&device->group->group_rwsem);
otherwise,
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Thanks
Kevin
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [Intel-gfx] [PATCH v2 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM
[not found] ` <20220518212607.467538-2-mjrosato@linux.ibm.com>
2022-05-19 6:43 ` [Intel-gfx] [PATCH v2 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM Tian, Kevin
@ 2022-05-19 12:33 ` Jason Gunthorpe
1 sibling, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2022-05-19 12:33 UTC (permalink / raw)
To: Matthew Rosato
Cc: jjherne, akrowiak, kvm, hch, linux-s390, intel-gfx, cohuck,
linux-kernel, pasic, borntraeger, intel-gvt-dev
On Wed, May 18, 2022 at 05:26:07PM -0400, Matthew Rosato wrote:
> Rather than relying on a notifier for associating the KVM with
> the group, let's assume that the association has already been
> made prior to device_open. The first time a device is opened
> associate the group KVM with the device.
>
> This fixes a user-triggerable oops in GVT.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
> ---
> drivers/gpu/drm/i915/gvt/gtt.c | 4 +-
> drivers/gpu/drm/i915/gvt/gvt.h | 3 -
> drivers/gpu/drm/i915/gvt/kvmgt.c | 82 ++++++---------------------
> drivers/s390/crypto/vfio_ap_ops.c | 38 ++++---------
> drivers/s390/crypto/vfio_ap_private.h | 3 -
> drivers/vfio/vfio.c | 75 ++++++++----------------
> include/linux/vfio.h | 5 +-
> 7 files changed, 56 insertions(+), 154 deletions(-)
I'm fine with this, thanks for finishing it
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> index e8914024f5b1..17a56bb4cf25 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -1196,6 +1196,8 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
> {
> struct ap_matrix_mdev *m;
>
> + kvm_get_kvm(kvm);
> +
[..]
> + if (kvm)
> + kvm_put_kvm(kvm);
> }
This extra ref traffic is not necessary, the kvm will have a valid ref
because we are under the group_rwsem here and then the function will
obtain a ref when it stores it in matrix_mdev->kvm.
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index cfcff7764403..272acb62facd 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -1083,11 +1083,22 @@ static struct file *vfio_device_open(struct vfio_device *device)
>
> mutex_lock(&device->dev_set->lock);
> device->open_count++;
> + down_read(&device->group->group_rwsem);
> + if (device->open_count == 1 && device->group->kvm) {
> + /*
> + * Here we pass the KVM pointer with the group under the read
> + * lock. If the device driver will use it, it must obtain a
> + * reference and release it during close_device.
> + */
> + device->kvm = device->group->kvm;
> + }
But it is a bit ugly to keep the device->kvm in the struct device
without holding a reference count.
It is probably worth adding a comment to the definition in the struct
as well that the device driver must reference it or never touch it.
Jason
^ permalink raw reply [flat|nested] 4+ messages in thread