vfio/pci: Check the device set open_count on reset
Subsystems (2)
v4 cover vfio/pci: Check the device set open_count on reset
I took Alex's suggestion on the v2 patch to add an open count to the device set. This simplified the final patch and might generally be useful for other drivers.
The first patch in the series fixes a life cycle issue that was discovered while adding open_count to vfio_device_set. It is also fixed by a patch [1] in Jason's "Connect VFIO to IOMMU" series.
The third patch is a rework of the original fix that uses the new device set open_count instead of the private open_count in vfio_device.
[1] https://lore.kernel.org/kvm/[email protected]/
Anthony
v3 -> v4:
- Added a patch to fix device registration life cycle
- Added a patch to add a public open_count on vfio_device_set
- Changed the implementation to avoid private open_count usage
v3: https://lore.kernel.org/kvm/[email protected]/
v2 -> v3:
- Added WARN_ON()
- Revised commit message
v2: https://lore.kernel.org/kvm/[email protected]/
v1 -> v2:
- Changed reset behavior instead of open_count ordering
- Retitled from "vfio: Decrement open_count before close_device()"
v1: https://lore.kernel.org/kvm/[email protected]/
Anthony DeRossi (3):
vfio: Fix container device registration life cycle
vfio: Add an open counter to vfio_device_set vfio/pci: Check the device set open_count on reset
drivers/vfio/pci/vfio_pci_core.c | 10 +++++----- drivers/vfio/vfio_main.c | 16 +++++++++++----- include/linux/vfio.h | 1 + 3 files changed, 17 insertions(+), 10 deletions(-)
Signature · 1 line
2.37.4
PATCH v4 1/3 vfio: Fix container device registration life cycle
In vfio_device_open(), vfio_container_device_register() is always called when open_count == 1. On error, vfio_device_container_unregister() is only called when open_count == 1 and close_device is set. This leaks a registration for devices without a close_device implementation.
In vfio_device_fops_release(), vfio_device_container_unregister() is called unconditionally. This can cause a device to be unregistered multiple times.
Treating container device registration/unregistration uniformly (always when open_count == 1) fixes both issues.
--- drivers/vfio/vfio_main.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/vfio/vfio_main.c +8 -5
@@ -801,8 +801,9 @@ static struct file *vfio_device_open(struct vfio_device *device) err_close_device:mutex_lock (&device->dev_set->lock);mutex_lock (&device->group->group_lock); -if (device->open_count ==1 && device->ops->close_device) { - device->ops->close_device (device); +if (device->open_count ==1 ) { +if (device->ops->close_device) + device->ops->close_device (device);vfio_device_container_unregister (device); }
@@ -1017,10 +1018,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)mutex_lock (&device->dev_set->lock);vfio_assert_device_open (device);mutex_lock (&device->group->group_lock); -if (device->open_count ==1 && device->ops->close_device) - device->ops->close_device (device); +if (device->open_count ==1 ) { +if (device->ops->close_device) + device->ops->close_device (device); -vfio_device_container_unregister (device); +vfio_device_container_unregister (device); + }mutex_unlock (&device->group->group_lock); device->open_count--;if (device->open_count ==0 )
Signature · 1 line
2.37.4
Re: [PATCH v4 1/3] vfio: Fix container device registration life cycle
Quoted 11 lines · On Fri, 4 Nov 2022 12:57:25 -0700 Anthony DeRossi <[email protected]> wrote:
In
vfio_device_open(),vfio_container_device_register()is always called whenopen_count== 1. On error,vfio_device_container_unregister()is only called whenopen_count== 1 andclose_deviceis set. This leaks a registration for devices without aclose_deviceimplementation.In
vfio_device_fops_release(),vfio_device_container_unregister()is called unconditionally. This can cause a device to be unregistered multiple times.Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.
Good catch, I see that Jason does subtly fix this in "vfio: Move vfio_device driver open/close code to a function", but I'd rather see it more overtly fixed in a discrete patch like this. All "real" drivers provide a close_device callback, but mdpy and mtty do not. Thanks,
Alex
Quoted 38 lines
Fixes ce4b4657ff18 ("vfio: Replace the DMA unmapping notifier with a callback")Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/vfio_main.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)drivers/vfio/vfio_main.c +8
-5@@ -801,8 +801,9 @@ static struct file *vfio_device_open(struct vfio_device *device) err_close_device:mutex_lock (&device->dev_set->lock);mutex_lock (&device->group->group_lock); -if (device->open_count ==1 && device->ops->close_device) { - device->ops->close_device (device); +if (device->open_count ==1 ) { +if (device->ops->close_device) + device->ops->close_device (device);vfio_device_container_unregister (device); }@@ -1017,10 +1018,12 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)mutex_lock (&device->dev_set->lock);vfio_assert_device_open (device);mutex_lock (&device->group->group_lock); -if (device->open_count ==1 && device->ops->close_device) - device->ops->close_device (device); +if (device->open_count ==1 ) { +if (device->ops->close_device) + device->ops->close_device (device); -vfio_device_container_unregister (device); +vfio_device_container_unregister (device); + }mutex_unlock (&device->group->group_lock); device->open_count--;if (device->open_count ==0 )
Re: [PATCH v4 1/3] vfio: Fix container device registration life cycle
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 4 Nov 2022 12:57:25 -0700 Anthony DeRossi <[email protected]> wrote:
In
vfio_device_open(),vfio_container_device_register()is always called whenopen_count== 1. On error,vfio_device_container_unregister()is only called whenopen_count== 1 andclose_deviceis set. This leaks a registration for devices without aclose_deviceimplementation.In
vfio_device_fops_release(),vfio_device_container_unregister()is called unconditionally. This can cause a device to be unregistered multiple times.
Quoted 7 lines
Quoted 2 lines
Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.Good catch, I see that Jason does subtly fix this in "vfio: Move
vfio_devicedriver open/close code to a function", but I'd rather see it more overtly fixed in a discrete patch like this. All "real" drivers provide aclose_devicecallback, but mdpy and mtty do not.
Given it only impacts the samples maybe I should just stick it in the iommufd series before that patch?
Jason
Re: [PATCH v4 1/3] vfio: Fix container device registration life cycle
Quoted 16 lines · On Tue, 8 Nov 2022 20:51:43 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 15 lines · Alex Williamson
Quoted 11 lines · On Fri, 4 Nov 2022 12:57:25 -0700 Anthony DeRossi <[email protected]> wrote:
In
vfio_device_open(),vfio_container_device_register()is always called whenopen_count== 1. On error,vfio_device_container_unregister()is only called whenopen_count== 1 andclose_deviceis set. This leaks a registration for devices without aclose_deviceimplementation.In
vfio_device_fops_release(),vfio_device_container_unregister()is called unconditionally. This can cause a device to be unregistered multiple times.Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.
Quoted 7 lines
Quoted 4 lines
Good catch, I see that Jason does subtly fix this in "vfio: Move
vfio_devicedriver open/close code to a function", but I'd rather see it more overtly fixed in a discrete patch like this. All "real" drivers provide aclose_devicecallback, but mdpy and mtty do not.Given it only impacts the samples maybe I should just stick it in the iommufd series before that patch?
The series in general though fixes a regression. Is there any reason we shouldn't try to push it into 6.1? Thanks,
Alex
Re: [PATCH v4 1/3] vfio: Fix container device registration life cycle
Quoted 24 lines · Alex Williamson
Quoted 21 lines · On Tue, 8 Nov 2022 20:51:43 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 19 lines · Alex Williamson
Quoted 11 lines · On Fri, 4 Nov 2022 12:57:25 -0700 Anthony DeRossi <[email protected]> wrote:
In
vfio_device_open(),vfio_container_device_register()is always called whenopen_count== 1. On error,vfio_device_container_unregister()is only called whenopen_count== 1 andclose_deviceis set. This leaks a registration for devices without aclose_deviceimplementation.In
vfio_device_fops_release(),vfio_device_container_unregister()is called unconditionally. This can cause a device to be unregistered multiple times.Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.Good catch, I see that Jason does subtly fix this in "vfio: Move
vfio_devicedriver open/close code to a function", but I'd rather see it more overtly fixed in a discrete patch like this. All "real" drivers provide aclose_devicecallback, but mdpy and mtty do not.
Quoted 5 lines
Quoted 2 lines
Given it only impacts the samples maybe I should just stick it in the iommufd series before that patch?
The series in general though fixes a regression. Is there any reason we shouldn't try to push it into 6.1? Thanks,
That works for me too.
Jason
PATCH v4 2/3 vfio: Add an open counter to vfio_device_set
In reply to Anthony DeRossiopen_count is incremented before open_device() and decremented after close_device() for each device in the set. This allows devices to determine whether shared resources are in use without tracking them manually or accessing the private open_count in vfio_device.
--- drivers/vfio/vfio_main.c | 3 +++ include/linux/vfio.h | 1 + 2 files changed, 4 insertions(+)
drivers/vfio/vfio_main.c +3 -0
@@ -761,6 +761,7 @@ static struct file *vfio_device_open(struct vfio_device *device)mutex_lock (&device->group->group_lock); device->kvm = device->group->kvm; + device->dev_set->open_count++;if (device->ops->open_device) { ret = device->ops->open_device (device);if (ret)
@@ -809,6 +810,7 @@ static struct file *vfio_device_open(struct vfio_device *device) } err_undo_count:mutex_unlock (&device->group->group_lock); + device->dev_set->open_count--; device->open_count--;if (device->open_count ==0 && device->kvm) device->kvm =NULL ;
@@ -1023,6 +1025,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) device->ops->close_device (device);vfio_device_container_unregister (device); + device->dev_set->open_count--; }mutex_unlock (&device->group->group_lock); device->open_count--;
include/linux/vfio.h +1 -0
@@ -28,6 +28,7 @@ struct vfio_device_set {struct mutex lock;struct list_head device_list;unsigned int device_count; +unsigned int open_count; };struct vfio_device {
Signature · 1 line
2.37.4
Re: [PATCH v4 2/3] vfio: Add an open counter to vfio_device_set
Quoted 16 lines · On Fri, 4 Nov 2022 12:57:26 -0700 Anthony DeRossi <[email protected]> wrote:
open_countis incremented beforeopen_device()and decremented afterclose_device()for each device in the set. This allows devices to determine whether shared resources are in use without tracking them manually or accessing the privateopen_countinvfio_device.Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/vfio_main.c | 3 +++ include/linux/vfio.h | 1 + 2 files changed, 4 insertions(+)drivers/vfio/vfio_main.c +0
-0
drivers/vfio/vfio_main.c static struct file *vfio_device_open(struct vfio_device *device) · line 764
Quoted 4 lines
drivers/vfio/vfio_main.c +1
-0@@ -761,6 +761,7 @@ static struct file *vfio_device_open(struct vfio_device *device)mutex_lock (&device->group->group_lock); device->kvm = device->group->kvm; + device->dev_set->open_count++;
Note that this is on the device first open path...
drivers/vfio/vfio_main.c static struct file *vfio_device_open(struct vfio_device *device) · line 813
Quoted 8 lines
drivers/vfio/vfio_main.c +1
-0if (device->ops->open_device) { ret = device->ops->open_device (device);if (ret)@@ -809,6 +810,7 @@ static struct file *vfio_device_open(struct vfio_device *device) } err_undo_count:mutex_unlock (&device->group->group_lock); + device->dev_set->open_count--;
But this can be reached for non-first open faults.
drivers/vfio/vfio_main.c static int vfio_device_fops_release(struct inode *inode, struct file *filep) · line 1028
Quoted 8 lines
drivers/vfio/vfio_main.c +1
-0device->open_count--;if (device->open_count ==0 && device->kvm) device->kvm =NULL ;@@ -1023,6 +1025,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) device->ops->close_device (device);vfio_device_container_unregister (device); + device->dev_set->open_count--;
This is on the last-close path, so aside from the above bug this is more of an open device counter across the device set rather than than a number of open device file descriptors counter as we have on each device.
There's some complexity thinking about the difference between those for our scenario, it works, but requires a bit of deduction. For example, a device set might have a count of 1 here, but that one device could be opened multiple times. It works for the scenario you address in the next patch, but is maybe not as generically useful.
Like it seems maybe you're going for by your more recent comment, I was thinking an interface rather than tracking a new field on the device set. Thanks,
Alex
Quoted 15 lines
drivers/vfio/vfio_main.c +0
-0}mutex_unlock (&device->group->group_lock); device->open_count--;include/linux/vfio.h +1
-0@@ -28,6 +28,7 @@ struct vfio_device_set {struct mutex lock;struct list_head device_list;unsigned int device_count; +unsigned int open_count; };struct vfio_device {
Re: [PATCH v4 2/3] vfio: Add an open counter to vfio_device_set
Quoted 3 lines · Alex Williamson
Like it seems maybe you're going for by your more recent comment, I was thinking an interface rather than tracking a new field on the device set.
Thanks for the feedback. I sent an updated series with this change.
v5: https://lore.kernel.org/kvm/[email protected]/
Anthony
PATCH v4 3/3 vfio/pci: Check the device set open_count on reset
In reply to Anthony DeRossivfio_pci_dev_set_needs_reset() inspects the open_count of every device in the set to determine whether a reset is needed. The current device always has open_count == 1 within vfio_pci_core_disable(), effectively disabling the reset logic. This field is also documented as private in vfio_device, so it should not be used to determine whether other devices in the set are open.
Checking for open_count > 1 on the device set fixes both issues.
After commit 2cd8b14aaa66 ("vfio/pci: Move to the device set infrastructure"), failure to create a new file for a device would cause the reset to be skipped due to open_count being decremented after calling close_device() in the error path.
After commit eadd86f835c6 ("vfio: Remove calls to vfio_group_add_container_user()"), releasing a device would always skip the reset due to an ordering change in vfio_device_fops_release().
Failing to reset the device leaves it in an unknown state, potentially causing errors when it is bound to a different driver.
This issue was observed with a Radeon RX Vega 56 [1002:687f] (rev c3) assigned to a Windows guest. After shutting down the guest, unbinding the device from vfio-pci, and binding the device to amdgpu:
[ 548.007102] [drm:psp_hw_start [amdgpu]] *ERROR* PSP create ring failed! [ 548.027174] [drm:psp_hw_init [amdgpu]] *ERROR* PSP firmware loading failed [ 548.027242] [drm:amdgpu_device_fw_loading [amdgpu]] *ERROR* hw_init of IP block <psp> failed -22 [ 548.027306] amdgpu 0000:0a:00.0: amdgpu: amdgpu_device_ip_init failed [ 548.027308] amdgpu 0000:0a:00.0: amdgpu: Fatal error during GPU init
--- drivers/vfio/pci/vfio_pci_core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +5 -5
@@ -2488,12 +2488,12 @@ static bool vfio_pci_dev_set_needs_reset(struct vfio_device_set *dev_set)struct vfio_pci_core_device *cur;bool needs_reset = false; -list_for_each_entry (cur, &dev_set->device_list, vdev.dev_set_list) { -/* No VFIO device in the set can have an open device FD */ -if (cur->vdev.open_count) -return false; +/* No other VFIO device in the set can be open. */ +if (dev_set->open_count >1 ) +return false; + +list_for_each_entry (cur, &dev_set->device_list, vdev.dev_set_list) needs_reset |= cur->needs_reset; - }return needs_reset; }
Signature · 1 line
2.37.4