vfio/pci: Check the device set open count on reset
Subsystems (2)
v5 cover vfio/pci: Check the device set open count on reset
This series fixes an issue where devices bound to vfio-pci are not reset when they are released. Skipping the reset has unpredictable results depending on the device, and can cause errors when accessing the device later or binding to a different driver.
This revision replaces the patch that introduced an open_count field in vfio_device_set with one that exports a new function that can be used for the same purpose, i.e., determining whether any device in the set is in use.
The first patch in this series fixes a life cycle issue that was discovered in an earlier revision of the series.
Anthony
v4 -> v5:
- Replaced patch 2 with a patch that introduces a new function to get the open count of a device set
- Updated patch 3 to use the new function
v4: https://lore.kernel.org/kvm/[email protected]/
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: Export the device set open count vfio/pci: Check the device set open count on reset
drivers/vfio/pci/vfio_pci_core.c | 10 +++++----- drivers/vfio/vfio_main.c | 24 +++++++++++++++++++----- include/linux/vfio.h | 1 + 3 files changed, 25 insertions(+), 10 deletions(-)
Signature · 1 line
2.37.4
PATCH v5 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 v5 1/3] vfio: Fix container device registration life cycle
Quoted 9 lines · Anthony DeRossi
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 8 lines
Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.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(-)
This seems to only effect the mbochs sample, but it looks OK
This will generate conflicts with the iommufd treee, so please lets think about how to avoid them..
Jason
RE: [PATCH v5 1/3] vfio: Fix container device registration life cycle
In reply to Anthony DeRossiQuoted 4 lines
From: Anthony DeRossi <[email protected]>
Sent: Sunday, November 6, 2022 6:45 AMIn
vfio_device_open(),vfio_container_device_register()is always called
vfio_device_container_register()
Quoted 8 lines
when
open_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 6 lines
Treating container device registration/unregistration uniformly (always when
open_count== 1) fixes both issues.Fixes ce4b4657ff18 ("vfio: Replace the DMA unmapping notifier with acallback")
Signed-off-by Anthony DeRossi <[email protected]>
PATCH v5 2/3 vfio: Export the device set open count
In reply to Anthony DeRossiThe open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value 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 | 11 +++++++++++ include/linux/vfio.h | 1 + 2 files changed, 12 insertions(+)
drivers/vfio/vfio_main.c +11 -0
@@ -125,6 +125,17 @@ static void vfio_release_device_set(struct vfio_device *device)xa_unlock (&vfio_device_set_xa); } +unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set) +{ +struct vfio_device *cur; +unsigned int open_count =0 ; + +list_for_each_entry (cur, &dev_set->device_list, dev_set_list) + open_count += cur->open_count; +return open_count; +} +EXPORT_SYMBOL_GPL (vfio_device_set_open_count); +/* * Group objects - create, release, get, put, search */
include/linux/vfio.h +1 -0
@@ -189,6 +189,7 @@ int vfio_register_emulated_iommu_dev(struct vfio_device *device);void vfio_unregister_group_dev (struct vfio_device *device);int vfio_assign_device_set (struct vfio_device *device,void *set_id); +unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set); int vfio_mig_get_next_state(struct vfio_device *device, enum vfio_device_mig_state cur_fsm,
Signature · 1 line
2.37.4
Re: [PATCH v5 2/3] vfio: Export the device set open count
Quoted 16 lines · On Sat, 5 Nov 2022 15:44:57 -0700 Anthony DeRossi <[email protected]> wrote:
The open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value to determine whether shared resources are in use without tracking them manually or accessing the private
open_countinvfio_device.Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/vfio_main.c | 11 +++++++++++ include/linux/vfio.h | 1 + 2 files changed, 12 insertions(+)drivers/vfio/vfio_main.c +0
-0
drivers/vfio/vfio_main.c static void vfio_release_device_set(struct vfio_device *device) · line 131
Quoted 7 lines
drivers/vfio/vfio_main.c +4
-0@@ -125,6 +125,17 @@ static void vfio_release_device_set(struct vfio_device *device)xa_unlock (&vfio_device_set_xa); } +unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set) +{ +struct vfio_device *cur; +unsigned int open_count =0 ;
This can only be called while holding the dev_set->lock, so we should have an assert here:
lockdep_assert_held(&dev_set->lock);
The series looks ok to me otherwise, hopefully we'll get some additional reviews. Thanks,
Alex
Quoted 22 lines
drivers/vfio/vfio_main.c +7
-0+ +list_for_each_entry (cur, &dev_set->device_list, dev_set_list) + open_count += cur->open_count; +return open_count; +} +EXPORT_SYMBOL_GPL (vfio_device_set_open_count); +/* * Group objects - create, release, get, put, search */ include/linux/vfio.h +1
-0@@ -189,6 +189,7 @@ int vfio_register_emulated_iommu_dev(struct vfio_device *device);void vfio_unregister_group_dev (struct vfio_device *device);int vfio_assign_device_set (struct vfio_device *device,void *set_id); +unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set); int vfio_mig_get_next_state(struct vfio_device *device, enum vfio_device_mig_state cur_fsm,
Re: [PATCH v5 2/3] vfio: Export the device set open count
In reply to Anthony DeRossiQuoted 12 lines · Anthony DeRossi
The open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value to determine whether shared resources are in use without tracking them manually or accessing the private
open_countinvfio_device.Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/vfio_main.c | 11 +++++++++++ include/linux/vfio.h | 1 + 2 files changed, 12 insertions(+)
drivers/vfio/vfio_main.c static void vfio_release_device_set(struct vfio_device *device) · line 131
Quoted 4 lines
drivers/vfio/vfio_main.c +4
-0+unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set) +{ +struct vfio_device *cur; +unsigned int open_count =0 ;
I'd probably just make this a bool
'vfio_device_set_last_close()'
And roll in the < 1 logic too
Nothing will ever need to know the number of fds open across the set.
But this is fine as written
Jason
Re: [PATCH v5 2/3] vfio: Export the device set open count
Quoted 20 lines · On Tue, 8 Nov 2022 20:48:08 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 12 lines · Anthony DeRossi
The open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value to determine whether shared resources are in use without tracking them manually or accessing the private
open_countinvfio_device.Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/vfio_main.c | 11 +++++++++++ include/linux/vfio.h | 1 + 2 files changed, 12 insertions(+)drivers/vfio/vfio_main.c static void vfio_release_device_set(struct vfio_device *device) · line 131
Quoted 4 lines
drivers/vfio/vfio_main.c +4
-0+unsigned int vfio_device_set_open_count (struct vfio_device_set *dev_set) +{ +struct vfio_device *cur; +unsigned int open_count =0 ;I'd probably just make this a bool
Quoted 5 lines
'
vfio_device_set_last_close()'And roll in the < 1 logic too
Nothing will ever need to know the number of fds open across the set.
'last_close' presumes the caller though, which seems bad form. It's possible there are use cases for this in a 'first_open' scenario too. Something along the lines of 'singleton_open', but that's a horrible name, so we might as well just provide the count since we already have it. Thanks,
Alex
Quoted 6 lines
But this is fine as written
Reviewed-by Jason Gunthorpe <[email protected]>Jason
RE: [PATCH v5 2/3] vfio: Export the device set open count
In reply to Anthony DeRossiQuoted 9 lines
From: Anthony DeRossi <[email protected]>
Sent: Sunday, November 6, 2022 6:45 AMThe open count of a device set is the sum of the open counts of all devices in the set. Drivers can use this value to determine whether shared resources are in use without tracking them manually or accessing the private
open_countinvfio_device.Signed-off-by Anthony DeRossi <[email protected]>
Apart from remarks from Alex/Jason, good to me:
PATCH v5 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 allowed. 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 vfio_device_set_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 accessed later or 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 (vfio_device_set_open_count (dev_set) >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
Quoted 32 lines · Anthony DeRossi
vfio_pci_dev_set_needs_reset()inspects theopen_countof every device in the set to determine whether a reset is allowed. The current device always hasopen_count== 1 withinvfio_pci_core_disable(), effectively disabling the reset logic. This field is also documented as private invfio_device, so it should not be used to determine whether other devices in the set are open.Checking for
vfio_device_set_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 toopen_countbeing decremented after callingclose_device()in the error path.After commit
eadd86f835c6("vfio: Remove calls tovfio_group_add_container_user()"), releasing a device would always skip the reset due to an ordering change invfio_device_fops_release().Failing to reset the device leaves it in an unknown state, potentially causing errors when it is accessed later or 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
Quoted 6 lines
Fixes 2cd8b14aaa66 ("vfio/pci: Move to the device set infrastructure")Fixes eadd86f835c6 ("vfio: Remove calls to vfio_group_add_container_user()")Signed-off-by Anthony DeRossi <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Jason
Quoted 37 lines
From: Anthony DeRossi <[email protected]>
Sent: Sunday, November 6, 2022 6:45 AM
vfio_pci_dev_set_needs_reset()inspects theopen_countof every device in the set to determine whether a reset is allowed. The current device always hasopen_count== 1 withinvfio_pci_core_disable(), effectively disabling the reset logic. This field is also documented as private invfio_device, so it should not be used to determine whether other devices in the set are open.Checking for
vfio_device_set_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 toopen_countbeing decremented after callingclose_device()in the error path.After commit
eadd86f835c6("vfio: Remove calls tovfio_group_add_container_user()"), releasing a device would always skip the reset due to an ordering change invfio_device_fops_release().Failing to reset the device leaves it in an unknown state, potentially causing errors when it is accessed later or 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
Quoted 4 lines
Fixes 2cd8b14aaa66 ("vfio/pci: Move to the device set infrastructure")Fixes eadd86f835c6 ("vfio: Remove calls to
vfio_group_add_container_user()")Signed-off-by Anthony DeRossi <[email protected]>