vfio/pci: Disable sriov on PF device close
Subsystems (2)
cover vfio/pci: Disable sriov on PF device close
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
When the PF is hardware-reset via VFIO, it implicitly disrupts SR-IOV operations at the device level. Because this reset happens without notifying the core PCI driver model, the kernel drivers bound to the VFs remain loaded and operate under the assumption that the VF hardware is still functional.
This creates a state mismatch between the kernel's view of the hardware and the actual device state. Subsequent attempts by the host OS or bound drivers to interact with the VFs will fail, leading to unexpected errors.
Disabling SRIOV before resetting the device avoids this issue by tearing down the VFs and removing the VF devices.
The fix in this patch only disables SRIOV on PF reset for devices bound to vfio-pci drivers. But this probably needs a fix for kernel drivers also, maybe return -EBUSY when SRIOV is enabled?
Looking forward to your feedback on this.
---
Samiullah Khawaja (1):
vfio/pci: Disable sriov on PF device close
drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)
base-commit: 0f6da28aab51b16762ed82e8fdeaa5042da45b08
Signature · 1 line
2.55.0.629.g250fe7f194-goog
RFC PATCH 1/1 vfio/pci: Disable sriov on PF device close
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
When the PF is hardware-reset via VFIO, it implicitly disrupts SR-IOV operations at the device level. Because this reset happens without notifying the core PCI driver model, the kernel drivers bound to the VFs remain loaded and operate under the assumption that the VF hardware is still functional.
This creates a state mismatch between the kernel's view of the hardware and the actual device state. Subsequent attempts by the host OS or bound drivers to interact with the VFs will fail, leading to unexpected errors.
Disable SR-IOV on the device prior to issuing the PF reset so that the VFs can teardown and remove at the software level also.
--- drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +7 -0
@@ -826,6 +826,13 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)#if IS_ENABLED (CONFIG_EEH )eeh_dev_release (vdev->pdev);#endif + +if (pci_num_vf (vdev->pdev)) { +device_lock (&vdev->pdev->dev); +vfio_pci_core_sriov_configure (vdev,0 ); +device_unlock (&vdev->pdev->dev); + } +vfio_pci_dma_buf_cleanup (vdev);vfio_pci_core_disable (vdev);
Signature · 1 line
2.55.0.629.g250fe7f194-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Acquiring `device_lock()` inside `vfio_pci_core_close_device()` introduces a guaranteed AB-BA…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Acquiring device_lock() inside vfio_pci_core_close_device() introduces a guaranteed AB-BA deadlock if the device is unbound from the driver while still in use by userspace.
- [High] Synchronously tearing down SR-IOV VFs from the PF's close_device path deadlocks if any VF is actively in use by VFIO.
- [High] Calling pci_disable_sriov() without holding pci_rescan_remove_lock causes an immediate lockdep splat and races with PCI bus enumeration.
- [Medium] Time-of-Check to Time-of-Use (TOCTOU) races around device_lock() allow VFs to be enabled immediately before hardware reset, defeating the purpose of the patch. --
--- Patch [1]: [RFC PATCH 1/1] vfio/pci: Disable sriov on PF device close --- commit 996eca93815cca91725d215b78730f993e057c49
Author: Samiullah Khawaja <[email protected]>
vfio/pci: Disable sriov on PF device close
When a user closes a VFIO device file descriptor for an SR-IOV Physical Function, the device is reset. This reset disrupts the VFs without notifying the core PCI driver model. This commit disables SR-IOV prior to issuing the PF reset so VFs are torn down gracefully at the software level.
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_close_device(struct vfio_device *core_vdev) · line 830
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0@@ -826,6 +826,13 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)#if IS_ENABLED (CONFIG_EEH )eeh_dev_release (vdev->pdev);#endif + +if (pci_num_vf (vdev->pdev)) {
[Severity: Medium]
Is it possible for VFs to be enabled concurrently right after this check?
Since pci_num_vf() is evaluated locklessly before acquiring the device lock, a concurrent sysfs write to sriov_numvfs could enable VFs immediately after this returns 0, causing the teardown to be skipped.
Additionally, because the device lock is dropped before the hardware reset in vfio_pci_core_disable(), a concurrent sysfs write can acquire the lock, successfully enable SR-IOV, and release the lock before the reset occurs.
Could this recreate the exact hardware state mismatch the patch attempts to fix?
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_close_device(struct vfio_device *core_vdev) · line 831
Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +1
-0+device_lock (&vdev->pdev->dev);
[Severity: High]
Does acquiring the device lock here introduce an AB-BA deadlock during device unbind?
When a device is unbound, the PCI core acquires the device lock in device_release_driver() and invokes the driver's remove callback. For vfio-pci, this calls vfio_unregister_group_dev(), which blocks indefinitely waiting for userspace to close all active file descriptors.
When userspace closes the device fd, vfio_pci_core_close_device() is invoked. Because this patch tries to acquire the device lock, the fd release thread will block forever waiting for the unbind thread, and the unbind thread will wait forever for the fd release thread to finish.
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_close_device(struct vfio_device *core_vdev) · line 832
Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +1
-0+vfio_pci_core_sriov_configure (vdev,0 );
[Severity: High]
Can tearing down VFs from the PF's close path deadlock if a VF is actively in use by VFIO?
When vfio_pci_core_close_device() calls pci_disable_sriov(), the PCI core synchronously removes all VF devices from the bus. If a VF is also bound to vfio-pci and currently opened by userspace, removing the VF triggers vfio_pci_remove(), which blocks indefinitely until the VF file descriptor is closed.
If a single process holds both the PF and VF open and exits, exit_files() schedules the fput() for both file descriptors sequentially. If the PF is closed first, the thread hangs here waiting for the VF to close, preventing the VF's fput() from ever running.
[Severity: High]
Does this code trigger a lockdep warning by manipulating the PCI bus topology without the required lock?
Calling vfio_pci_core_sriov_configure() here ultimately calls pci_stop_and_remove_bus_device() to remove VFs from the PCI bus. That function enforces a strict locking requirement via lockdep_assert_held(&pci_rescan_remove_lock).
Because this global mutex is omitted, closing a PF with active VFs could race against concurrent PCI hotplug or sysfs rescan operations.
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+device_unlock (&vdev->pdev->dev); + } +vfio_pci_dma_buf_cleanup (vdev);vfio_pci_core_disable (vdev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 33 lines
From: Samiullah Khawaja <[email protected]>
Sent: Wednesday, August 5, 2026 8:34 AMWhen userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
When the PF is hardware-reset via VFIO, it implicitly disrupts SR-IOV operations at the device level. Because this reset happens without notifying the core PCI driver model, the kernel drivers bound to the VFs remain loaded and operate under the assumption that the VF hardware is still functional.
This creates a state mismatch between the kernel's view of the hardware and the actual device state. Subsequent attempts by the host OS or bound drivers to interact with the VFs will fail, leading to unexpected errors.
Disable SR-IOV on the device prior to issuing the PF reset so that the VFs can teardown and remove at the software level also.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -826,6 +826,13 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_close_device(struct vfio_device · line 836
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0#if IS_ENABLED (CONFIG_EEH )eeh_dev_release (vdev->pdev);#endif + +if (pci_num_vf (vdev->pdev)) { +device_lock (&vdev->pdev->dev); +vfio_pci_core_sriov_configure (vdev,0 ); +device_unlock (&vdev->pdev->dev); + } +
Sashiko reported several locking issues with this change:
https://sashiko.dev/#/patchset/20260805003355.728299-2-skhawaja%40google.com
Actually it is a discouraged usage allowing kernel drivers bound to VFs which belongs to a PF owned by vfio userspace.
vfio_pci_bus_notifier prints a warning message upon BUS_NOTIFY_BOUND_DRIVER:
"VF %s bound to driver %s while PF bound to driver %s\n"
Fixing this alone is insufficient to remove that warning...
Quoted 46 lines · Kevin
Quoted 33 lines
From: Samiullah Khawaja <[email protected]>
Sent: Wednesday, August 5, 2026 8:34 AMWhen userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
When the PF is hardware-reset via VFIO, it implicitly disrupts SR-IOV operations at the device level. Because this reset happens without notifying the core PCI driver model, the kernel drivers bound to the VFs remain loaded and operate under the assumption that the VF hardware is still functional.
This creates a state mismatch between the kernel's view of the hardware and the actual device state. Subsequent attempts by the host OS or bound drivers to interact with the VFs will fail, leading to unexpected errors.
Disable SR-IOV on the device prior to issuing the PF reset so that the VFs can teardown and remove at the software level also.
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -826,6 +826,13 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_close_device(struct vfio_device · line 836
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0#if IS_ENABLED (CONFIG_EEH )eeh_dev_release (vdev->pdev);#endif + +if (pci_num_vf (vdev->pdev)) { +device_lock (&vdev->pdev->dev); +vfio_pci_core_sriov_configure (vdev,0 ); +device_unlock (&vdev->pdev->dev); + } +Sashiko reported several locking issues with this change:
Quoted 4 lines
https://sashiko.dev/#/patchset/20260805003355.728299-2-skhawaja%40google.com
Actually it is a discouraged usage allowing kernel drivers bound to VFs which belongs to a PF owned by vfio userspace.
Thanks for the feedback Kevin.
I sent this as an RFC specifically to discuss the right approach for resolving this issue before fixing the locking issues that sashiko raised. I will get back to this once the discussion to fix this the right way concludes in the other thread.
Thanks,
Sami
Alex Williamson
maintainerQuoted 5 lines · On Wed, 5 Aug 2026 00:33:55 +0000 Samiullah Khawaja <[email protected]>…
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
Wait, what? We actively try to prevent VFs from a vfio-pci owned PF from being bound to host drivers other than vfio-pci. You need to overwrite the imposed driver_override to make this happen and you're in a very precarious security model to have the VF owned by a trusted in-kernel driver while the PF is owned by userspace.
Quoted 4 lines
When the PF is hardware-reset via VFIO, it implicitly disrupts SR-IOV operations at the device level. Because this reset happens without notifying the core PCI driver model, the kernel drivers bound to the VFs remain loaded and operate under the assumption that the VF hardware is still functional.
And this is a) why we actively try to prevent VFs from being bound to native host kernel drivers and b) part of the vf_token negotiation of trust opt-in that must be performed by userspace drivers of the VFs.
Quoted 4 lines
This creates a state mismatch between the kernel's view of the hardware and the actual device state. Subsequent attempts by the host OS or bound drivers to interact with the VFs will fail, leading to unexpected errors.
The described model is not a supported use case, there are fundamental security issues with a userspace vfio-pci driver owning the PF with VFs bound to in-kernel drivers.
Quoted 2 lines
Disable SR-IOV on the device prior to issuing the PF reset so that the VFs can teardown and remove at the software level also.
The below only accounts for the close reset path, not reset in general. Also note that the SR-IOV model in vfio-pci does have a path by which a PF can be re-opened with existing VFs. Whether that's actually used is a separate question, but this fundamentally disallows that design.
It's possible there are gaps that closing the PF can interrupt the VFs and we need to defer a reset until the VFs are closed, but the behavior here seems counter to the original design, and the use case of in-kernel VF drivers is certainly counter to that design and guardrails installed to prevent it. Thanks,
Alex
Quoted 23 lines
Signed-off-by Samiullah Khawaja <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/vfio/pci/vfio_pci_core.c +7
-0@@ -826,6 +826,13 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)#if IS_ENABLED (CONFIG_EEH )eeh_dev_release (vdev->pdev);#endif + +if (pci_num_vf (vdev->pdev)) { +device_lock (&vdev->pdev->dev); +vfio_pci_core_sriov_configure (vdev,0 ); +device_unlock (&vdev->pdev->dev); + } +vfio_pci_dma_buf_cleanup (vdev);vfio_pci_core_disable (vdev);
Quoted 9 lines · Alex Williamson
Quoted 5 lines · On Wed, 5 Aug 2026 00:33:55 +0000 Samiullah Khawaja <[email protected]>…
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
Quoted 5 lines
Wait, what? We actively try to prevent VFs from a vfio-pci owned PF from being bound to host drivers other than vfio-pci. You need to overwrite the imposed
driver_overrideto make this happen and you're in a very precarious security model to have the VF owned by a trusted in-kernel driver while the PF is owned by userspace.
Maybe, it really depends on the device. I can easially see someone using a device where this would be safe. mlx5 for instance is pretty OK.
So I don't really mind someone doing this, we should block it and warn it and so on, but like noiommu and the other vfio insecure modes, why not give an opt in?
Quoted 2 lines
It's possible there are gaps that closing the PF can interrupt the VFs and we need to defer a reset until the VFs are closed,
Oh definately, when running in a SRIOV mode it is really problematic for the PF to reset while there are any active VFs. The PF controls a number of shared items (MMIO, ATS, etc) and when it blips everyone is at risk of unexpected fairly catastrophic system crashing errors related to the shared items going away.
So resetting the PF device unconditionally when vfio closes is definately wrong in principal. I can see it maybe working for simple systems, especially ones that don't MCE..
I don't think we can skip the PF reset on close because of dev_set reasons and leave a rouge device for the next user, so the thing looks somewhat troubled?
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
Jason
Alex Williamson
maintainerQuoted 16 lines · On Thu, 6 Aug 2026 16:47:39 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Alex Williamson
Quoted 5 lines · On Wed, 5 Aug 2026 00:33:55 +0000 Samiullah Khawaja <[email protected]>…
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
Quoted 5 lines
Wait, what? We actively try to prevent VFs from a vfio-pci owned PF from being bound to host drivers other than vfio-pci. You need to overwrite the imposed
driver_overrideto make this happen and you're in a very precarious security model to have the VF owned by a trusted in-kernel driver while the PF is owned by userspace.
Quoted 7 lines
Maybe, it really depends on the device. I can easially see someone using a device where this would be safe. mlx5 for instance is pretty OK.
So I don't really mind someone doing this, we should block it and warn it and so on, but like noiommu and the other vfio insecure modes, why not give an opt in?
An opt-in to what currently? We generate a warning, but nothing actually prevents the re-bind to an in-kernel driver. Whether that's sustainable in eliminating this reset gap is yet to be seen.
Binding a VF from a userspace owned PF flips our model on its head in two important ways. First is the security inversion. An in-kernel PF driver is considered trusted, we absolve ourselves of issues related to whether the PF has access to VF data or interrupts VF operations and state through reset. Second, the vf_token model explicitly defines the trust and coordination boundaries for the userspace SR-IOV ecosystem. An in-kernel bound VF lives entirely outside of that boundary. For example, one mechanism we might use to prevent an MCE around PF reset would be to zap VF mappings and invalidate dmabufs. Reinstatement of those mappings would require userspace coordination, which the vf_token model would define as an implementation detail in userspace relative to vf_token coordination and trust.
So we haven't done anything that explicitly blocks or taints this mode, yet, but correctly handling reset could be a tipping point.
Quoted 9 lines
Quoted 2 lines
It's possible there are gaps that closing the PF can interrupt the VFs and we need to defer a reset until the VFs are closed,
Oh definately, when running in a SRIOV mode it is really problematic for the PF to reset while there are any active VFs. The PF controls a number of shared items (MMIO, ATS, etc) and when it blips everyone is at risk of unexpected fairly catastrophic system crashing errors related to the shared items going away.
Quoted 7 lines
So resetting the PF device unconditionally when vfio closes is definately wrong in principal. I can see it maybe working for simple systems, especially ones that don't MCE..
I don't think we can skip the PF reset on close because of
dev_setreasons and leave a rouge device for the next user, so the thing looks somewhat troubled?
This is what our vf_token model would handle, if we could use it. Re-open with VFs in use through vfio-pci requires the vf_token proof/opt-in. Re-open with no VFs in use could reset the PF, but we can't account for VFs bound to in-kernel drivers.
Quoted 6 lines
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
I think we have 3 potential options for PF reset:
a) All resets are blocked or deferred while SR-IOV is active.
b) VF mappings are zapped/invalidated at PF reset and require coordination to be reinstated.
c) VFs are torn down on all PF resets.
Option (a) is complex and doesn't look like stable material. A deferred reset on .close needs to materialize on .remove or .sriov_configure (and of course .open), but to allow in-kernel VF drivers we can't rely on vfio usage counts, or even our vf-token trust boundaries, which is what leads to the .sriov_configure hook since we need to take advantage of every opportunity to issue a deferred reset when SR-IOV is not active when we can only rely on the PF state.
Option (b) is potentially more simple, it implements the blocking of access on reset in the kernel for enforcement, but defers the coordination of reinstantiating mappings to userspace, which is really how the vf_token model is intended to work. This is incompatible with in-kernel VF drivers, which I think means tainting on VF unbind from vfio-pci and those working outside the model would tread carefully.
Option (c) is the more heavy handed approach, it means that a PF driver cannot fail or exit and re-attach. We have existing logic that handles a PF driver re-opening the PF device with active VFs and authenticating the vf_token (logic also broken by in-kernel VF drivers that bypass the vf_token mechanics). There also appears to be significant locking challenges in this approach, ex. the inversion of getting the device lock for SR-IOV teardown from the ioctl, config space, and .close contexts.
I'm open to suggestions, PF resets with live VFs is very much a gap that I'd like to close in the vfio-pci SR-IOV model. Thanks,
Alex
Quoted 6 lines
From: Alex Williamson <[email protected]>
Sent: Monday, August 10, 2026 11:32 PMOn Thu, 6 Aug 2026 16:47:39 -0300
Jason Gunthorpe <[email protected]> wrote:
Quoted 8 lines
Quoted 6 lines
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
I think we have 3 potential options for PF reset:
Is there any coordination between in-kernel PF driver and in-kernel VF drivers regarding to reset? At a glance looks that requesting FLR on PF via sysfs would silently affect active VFs anyway...
Quoted 32 lines
a) All resets are blocked or deferred while SR-IOV is active.
b) VF mappings are zapped/invalidated at PF reset and require coordination to be reinstated.
c) VFs are torn down on all PF resets.
Option (a) is complex and doesn't look like stable material. A deferred reset on .close needs to materialize on .remove or .sriov_configure (and of course .open), but to allow in-kernel VF drivers we can't rely on vfio usage counts, or even our vf-token trust boundaries, which is what leads to the .sriov_configure hook since we need to take advantage of every opportunity to issue a deferred reset when SR-IOV is not active when we can only rely on the PF state.
Option (b) is potentially more simple, it implements the blocking of access on reset in the kernel for enforcement, but defers the coordination of reinstantiating mappings to userspace, which is really how the
vf_tokenmodel is intended to work. This is incompatible with in-kernel VF drivers, which I think means tainting on VF unbind from vfio-pci and those working outside the model would tread carefully.Option (c) is the more heavy handed approach, it means that a PF driver cannot fail or exit and re-attach. We have existing logic that handles a PF driver re-opening the PF device with active VFs and authenticating the
vf_token(logic also broken by in-kernel VF drivers that bypass thevf_tokenmechanics). There also appears to be significant locking challenges in this approach, ex. the inversion of getting the device lock for SR-IOV teardown from the ioctl, config space, and .close contexts.
Quoted 3 lines
I'm open to suggestions, PF resets with live VFs is very much a gap that I'd like to close in the vfio-pci SR-IOV model. Thanks,
Before closing the open on reset, does it make sense to first fit it into the coming trust infrastructure [1]? e.g. initially set to TRUST_NONE for any VF with a PF owned by vfio-pci, preventing any bind to in-kernel VF drivers. Then opt-in is allowed to promote the trust of such VFs to TRUST_ADVERSARY, allowing driver binding but also put it in precaution with IOMMU protection. So a malicious userspace PF driver cannot indirectly affect VFs to do dma-based attack.
somehow VFs in this scenario feel akin to Thunderbolt devices...
Quoted 9 lines · Kevin
Before closing the open on reset, does it make sense to first fit it into the coming trust infrastructure [1]? e.g. initially set to
TRUST_NONEfor any VF with a PF owned by vfio-pci, preventing any bind to in-kernel VF drivers. Then opt-in is allowed to promote the trust of such VFs toTRUST_ADVERSARY, allowing driver binding but also put it in precaution with IOMMU protection. So a malicious userspace PF driver cannot indirectly affect VFs to do dma-based attack.somehow VFs in this scenario feel akin to Thunderbolt devices...
That's certainly a novel idea that could make sense for drivers that support the trust level
Jason
Alex Williamson
maintainerQuoted 10 lines · Kevin
Quoted 10 lines
From: Alex Williamson <[email protected]>
Sent: Monday, August 10, 2026 11:32 PMQuoted 4 lines · On Thu, 6 Aug 2026 16:47:39 -0300 Jason Gunthorpe <[email protected]> wrote:
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
I think we have 3 potential options for PF reset:
Is there any coordination between in-kernel PF driver and in-kernel VF drivers regarding to reset? At a glance looks that requesting FLR on PF via sysfs would silently affect active VFs anyway...
Yes, and this is a key aspect that the RFC I sent last night tackles. pci_reset_function(), and it's exposure through pci-sysfs, is intended to have function level scope. We could walk the VFs, save and restore their PCI state, and trigger their reset callbacks, but this is a destructive operation relative to the VFs and their drivers. A better fit of our scope model would be if we simply declare that while a PF has active VFs, the PF does not have a function scoped reset.
This immediately, and cleanly, makes the open/close, config space, and ioctl resets in vfio-pci inaccessible for PFs in this state. We can then further push the vfio-pci hot-reset interface to require SR-IOV is first disabled on any affected PF devices, as the VFs are not currently accounted for in the set of affected devices.
Quoted 43 lines
Quoted 31 lines
a) All resets are blocked or deferred while SR-IOV is active.
b) VF mappings are zapped/invalidated at PF reset and require coordination to be reinstated.
c) VFs are torn down on all PF resets.
Option (a) is complex and doesn't look like stable material. A deferred reset on .close needs to materialize on .remove or .sriov_configure (and of course .open), but to allow in-kernel VF drivers we can't rely on vfio usage counts, or even our vf-token trust boundaries, which is what leads to the .sriov_configure hook since we need to take advantage of every opportunity to issue a deferred reset when SR-IOV is not active when we can only rely on the PF state.
Option (b) is potentially more simple, it implements the blocking of access on reset in the kernel for enforcement, but defers the coordination of reinstantiating mappings to userspace, which is really how the
vf_tokenmodel is intended to work. This is incompatible with in-kernel VF drivers, which I think means tainting on VF unbind from vfio-pci and those working outside the model would tread carefully.Option (c) is the more heavy handed approach, it means that a PF driver cannot fail or exit and re-attach. We have existing logic that handles a PF driver re-opening the PF device with active VFs and authenticating the
vf_token(logic also broken by in-kernel VF drivers that bypass thevf_tokenmechanics). There also appears to be significant locking challenges in this approach, ex. the inversion of getting the device lock for SR-IOV teardown from the ioctl, config space, and .close contexts.Quoted 3 lines
I'm open to suggestions, PF resets with live VFs is very much a gap that I'd like to close in the vfio-pci SR-IOV model. Thanks,
Before closing the open on reset, does it make sense to first fit it into the coming trust infrastructure [1]? e.g. initially set to
TRUST_NONEfor any VF with a PF owned by vfio-pci, preventing any bind to in-kernel VF drivers. Then opt-in is allowed to promote the trust of such VFs toTRUST_ADVERSARY, allowing driver binding but also put it in precaution with IOMMU protection. So a malicious userspace PF driver cannot indirectly affect VFs to do dma-based attack.
Quoted 3 lines
somehow VFs in this scenario feel akin to Thunderbolt devices...
That certainly puts a more cohesive driver-core story around binding VFs from a userspace owned PF to in-kernel drivers.
We currently have our own hand-rolled version for the PF/VF use case. Each bound PF registers a bus notifier that monitors actions for VFs whose physfn is the vfio-pci bound PF. On BUS_NOTIFY_ADD_DEVICE we write the VF driver_override to the PF driver name, preventing any other in-kernel driver from binding the VF without a userspace override. We also then monitor BUS_NOTIFY_BOUND_DRIVER and generate a pci_warn() if a VF is bound to any other driver.
So semantically, it still requires an administrative opt-in, but once opt'd in, the receiving in-kernel driver has no idea the device is driven by userspace.
Maybe the trust level of the device becomes more evident with an adversarial tag, but coordination between drivers relative to things like reset still seems like an orthogonal topic. Thanks,
Alex
Quoted 2 lines · Alex Williamson
On Thu, 6 Aug 2026 16:47:39 -0300
Jason Gunthorpe <[email protected]> wrote:
Thanks Alex and Jason for feedback.
Quoted 29 lines
Quoted 17 lines · Alex Williamson
Quoted 9 lines
Quoted 5 lines · On Wed, 5 Aug 2026 00:33:55 +0000 Samiullah Khawaja <[email protected]>…
When userspace closes a VFIO device file descriptor, the vfio driver performs a hardware reset on the PCIe device to ensure it is returned to a clean state. However, if the closed device is an SR-IOV Physical Function (PF), it may have instantiated Virtual Functions (VFs) that are actively bound to host kernel drivers (or other vfio instances).
Quoted 5 lines
Wait, what? We actively try to prevent VFs from a vfio-pci owned PF from being bound to host drivers other than vfio-pci. You need to overwrite the imposed
driver_overrideto make this happen and you're in a very precarious security model to have the VF owned by a trusted in-kernel driver while the PF is owned by userspace.Quoted 7 lines
Maybe, it really depends on the device. I can easially see someone using a device where this would be safe. mlx5 for instance is pretty OK.
So I don't really mind someone doing this, we should block it and warn it and so on, but like noiommu and the other vfio insecure modes, why not give an opt in?
An opt-in to what currently? We generate a warning, but nothing actually prevents the re-bind to an in-kernel driver. Whether that's sustainable in eliminating this reset gap is yet to be seen.
Quoted 12 lines
Binding a VF from a userspace owned PF flips our model on its head in two important ways. First is the security inversion. An in-kernel PF driver is considered trusted, we absolve ourselves of issues related to whether the PF has access to VF data or interrupts VF operations and state through reset. Second, the
vf_tokenmodel explicitly defines the trust and coordination boundaries for the userspace SR-IOV ecosystem. An in-kernel bound VF lives entirely outside of that boundary. For example, one mechanism we might use to prevent an MCE around PF reset would be to zap VF mappings and invalidate dmabufs. Reinstatement of those mappings would require userspace coordination, which thevf_tokenmodel would define as an implementation detail in userspace relative tovf_tokencoordination and trust.
As you rightly mentioned that this issue would occur with vfio-pci bound VFs also. For example a PF bound to vfio-pci can be closed and reset meanwhile the VFs bound to vfio-pci keep working as usual. This might be ok for some devices. But I think zapping VF mappings and invalidation of dmabufs might not be enough. For example these VF drivers can continue to generate ATS invalidations (posted PCIe messages that are dropped silently).
Quoted 17 lines
So we haven't done anything that explicitly blocks or taints this mode, yet, but correctly handling reset could be a tipping point.
Quoted 13 lines
Quoted 2 lines
It's possible there are gaps that closing the PF can interrupt the VFs and we need to defer a reset until the VFs are closed,
Oh definately, when running in a SRIOV mode it is really problematic for the PF to reset while there are any active VFs. The PF controls a number of shared items (MMIO, ATS, etc) and when it blips everyone is at risk of unexpected fairly catastrophic system crashing errors related to the shared items going away.
So resetting the PF device unconditionally when vfio closes is definately wrong in principal. I can see it maybe working for simple systems, especially ones that don't MCE..
Quoted 8 lines
Quoted 3 lines
I don't think we can skip the PF reset on close because of
dev_setreasons and leave a rouge device for the next user, so the thing looks somewhat troubled?This is what our
vf_tokenmodel would handle, if we could use it. Re-open with VFs in use through vfio-pci requires thevf_tokenproof/opt-in. Re-open with no VFs in use could reset the PF, but we can't account for VFs bound to in-kernel drivers.
I think even with this a PF reset blip can be catastrophic for the VFs in some usecases or devices. Maybe we need an opt-in where the PF driver can specify that keeping the SRIOV enabled during PF reset is catastrophic for it and so the reopen is not preferred by it?
Quoted 17 lines
Quoted 7 lines
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
I think we have 3 potential options for PF reset:
a) All resets are blocked or deferred while SR-IOV is active.
b) VF mappings are zapped/invalidated at PF reset and require coordination to be reinstated.
c) VFs are torn down on all PF resets.
Quoted 7 lines
Option (a) is complex and doesn't look like stable material. A deferred reset on .close needs to materialize on .remove or .sriov_configure (and of course .open), but to allow in-kernel VF drivers we can't rely on vfio usage counts, or even our vf-token trust boundaries, which is what leads to the .sriov_configure hook since we need to take advantage of every opportunity to issue a deferred reset when SR-IOV is not active when we can only rely on the PF state.
In my cover letter I pointed out that this problem probably exists in the in-kernel drivers (or PCI core) level also. For example an SRIOV PF driverA can reset the PF while VFs are bound to driverB. This makes me wonder whether we should fix this at PCI core level to maintain the same policy for all drivers when it comes to PF/VF lifecycle? Basically,
option1) On PF reset when there is SRIOV enabled, PCI core returns -EBUSY from the reset API.
option2) On PF reset, PCI core disables SRIOV. This might be tricky as some drivers might block this waiting for VF driver tear down and remove.
We probably cannot do this as default policy as it will break the vfio-pci model where the PF can be reopened as you mentioned above. I think if we go down this route, we would need some way of establishing trust and lifecycle rules?
Quoted 7 lines
Option (b) is potentially more simple, it implements the blocking of access on reset in the kernel for enforcement, but defers the coordination of reinstantiating mappings to userspace, which is really how the
vf_tokenmodel is intended to work. This is incompatible with in-kernel VF drivers, which I think means tainting on VF unbind from vfio-pci and those working outside the model would tread carefully.
While this might solve the issue for scenarios where the MMIO of these VFs is mapped or exported (dmabufs), this might not be enough as the software still remains out of sync as compared to the state of the device.
Quoted 9 lines
Option (c) is the more heavy handed approach, it means that a PF driver cannot fail or exit and re-attach. We have existing logic that handles a PF driver re-opening the PF device with active VFs and authenticating the
vf_token(logic also broken by in-kernel VF drivers that bypass thevf_tokenmechanics). There also appears to be significant locking challenges in this approach, ex. the inversion of getting the device lock for SR-IOV teardown from the ioctl, config space, and .close contexts.
I think this can be a reasonable opt-in option that the PF drivers can use to indicate that on PF reset keeping the VFs alive is catastrophic for it and it does not want to reconnect to the VFs. If we want to go for this one, I can maybe try figure out a way to break the locking issues.
Or we could certainly do this in a deferred way that is somewhat similar to the option (a). Basically PF openers can opt-in to not reset the PF on close if the VFs are enabled, and teardown the VFs on subsequent .open or .remove. This at least gives the system administrator (or scripts) opportunity to teardown the VFs in a proper way.
Quoted 3 lines
I'm open to suggestions, PF resets with live VFs is very much a gap that I'd like to close in the vfio-pci SR-IOV model. Thanks,
Thanks,
Sami
Quoted 2 lines
Alex
Alex Williamson
maintainerQuoted 26 lines · On Tue, 11 Aug 2026 20:31:49 +0000 Samiullah Khawaja <[email protected]>…
Quoted 16 lines · Alex Williamson
Quoted 14 lines · On Thu, 6 Aug 2026 16:47:39 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 2 lines · Alex Williamson
It's possible there are gaps that closing the PF can interrupt the VFs and we need to defer a reset until the VFs are closed,
Oh definately, when running in a SRIOV mode it is really problematic for the PF to reset while there are any active VFs. The PF controls a number of shared items (MMIO, ATS, etc) and when it blips everyone is at risk of unexpected fairly catastrophic system crashing errors related to the shared items going away.
So resetting the PF device unconditionally when vfio closes is definately wrong in principal. I can see it maybe working for simple systems, especially ones that don't MCE..
Quoted 8 lines
Quoted 3 lines
I don't think we can skip the PF reset on close because of
dev_setreasons and leave a rouge device for the next user, so the thing looks somewhat troubled?This is what our
vf_tokenmodel would handle, if we could use it. Re-open with VFs in use through vfio-pci requires thevf_tokenproof/opt-in. Re-open with no VFs in use could reset the PF, but we can't account for VFs bound to in-kernel drivers.
Quoted 4 lines
I think even with this a PF reset blip can be catastrophic for the VFs in some usecases or devices. Maybe we need an opt-in where the PF driver can specify that keeping the SRIOV enabled during PF reset is catastrophic for it and so the reopen is not preferred by it?
AFAICT, there's really no case where a VF can be operating independently from the PF that a PF reset is not catastrophic. Per the PCI spec, the default SR-IOV capability state is disabled, so post-reset the PF is effectively bringing up fresh, scrubbed VFs.
Quoted 33 lines
Quoted 16 lines
Quoted 6 lines
Adding the sriov disable here at least makes it defensibly safe, that we do still clear the PF on close, and we don't take risks that the active VFs will crash the system during the FLR blip.
Though I understand it was not the original intention, I feel we have ended up in a strange place with the SRIOV PF feature ..
I think we have 3 potential options for PF reset:
a) All resets are blocked or deferred while SR-IOV is active.
b) VF mappings are zapped/invalidated at PF reset and require coordination to be reinstated.
c) VFs are torn down on all PF resets.
Quoted 7 lines
Option (a) is complex and doesn't look like stable material. A deferred reset on .close needs to materialize on .remove or .sriov_configure (and of course .open), but to allow in-kernel VF drivers we can't rely on vfio usage counts, or even our vf-token trust boundaries, which is what leads to the .sriov_configure hook since we need to take advantage of every opportunity to issue a deferred reset when SR-IOV is not active when we can only rely on the PF state.
In my cover letter I pointed out that this problem probably exists in the in-kernel drivers (or PCI core) level also. For example an SRIOV PF driverA can reset the PF while VFs are bound to driverB. This makes me wonder whether we should fix this at PCI core level to maintain the same policy for all drivers when it comes to PF/VF lifecycle? Basically,
option1) On PF reset when there is SRIOV enabled, PCI core returns
-EBUSYfrom the reset API.
Quoted 8 lines
option2) On PF reset, PCI core disables SRIOV. This might be tricky as some drivers might block this waiting for VF driver tear down and remove.
We probably cannot do this as default policy as it will break the vfio-pci model where the PF can be reopened as you mentioned above. I think if we go down this route, we would need some way of establishing trust and lifecycle rules?
The PCI-core level change is the same conclusion I came to and posted in the RFC[1]. I think our only practical option is (1), function scoped resets must be rejected on PFs with SR-IOV enabled. This is compatible with the re-open scenario, but leans on the fact that resets are best effort.
The vf_token continues to form the boundary of trust for VFs operating within the vfio framework. The adversarial device support Kevin referenced could influence the security posture of the driver taking on an untrusted device like this, but I don't currently see much opportunity to introduce coordination between vfio and non-vfio drivers.
[1]https://lore.kernel.org/all/[email protected]/
Quoted 11 lines
Quoted 6 lines
Option (b) is potentially more simple, it implements the blocking of access on reset in the kernel for enforcement, but defers the coordination of reinstantiating mappings to userspace, which is really how the
vf_tokenmodel is intended to work. This is incompatible with in-kernel VF drivers, which I think means tainting on VF unbind from vfio-pci and those working outside the model would tread carefully.While this might solve the issue for scenarios where the MMIO of these VFs is mapped or exported (dmabufs), this might not be enough as the software still remains out of sync as compared to the state of the device.
This is the same problem as the .reset_prepare/.reset_done hooks, invalidating the dma-buf only prevents the machine check, it doesn't necessarily allow for ongoing operation. This gets back to the point that all PF resets are catastrophic for the VF driver.
Quoted 9 lines
Quoted 8 lines
Option (c) is the more heavy handed approach, it means that a PF driver cannot fail or exit and re-attach. We have existing logic that handles a PF driver re-opening the PF device with active VFs and authenticating the
vf_token(logic also broken by in-kernel VF drivers that bypass thevf_tokenmechanics). There also appears to be significant locking challenges in this approach, ex. the inversion of getting the device lock for SR-IOV teardown from the ioctl, config space, and .close contexts.
Quoted 5 lines
I think this can be a reasonable opt-in option that the PF drivers can use to indicate that on PF reset keeping the VFs alive is catastrophic for it and it does not want to reconnect to the VFs. If we want to go for this one, I can maybe try figure out a way to break the locking issues.
My exploration of this space suggests the locking issues run pretty deep with this approach.
Quoted 5 lines
Or we could certainly do this in a deferred way that is somewhat similar to the option (a). Basically PF openers can opt-in to not reset the PF on close if the VFs are enabled, and teardown the VFs on subsequent .open or .remove. This at least gives the system administrator (or scripts) opportunity to teardown the VFs in a proper way.
But VFs can be enabled before the PF is opened. If we're strictly within the vfio-pci model of using VFs, VFs cannot be opened until the vf_token is known, which requires a PF driver to set the token. However, binding VFs to in-kernel drivers entirely removes them from that ordering and coordination. The disposition of the VFs is changed without PF driver involvement.
I don't see how we really have an opportunity to create an opt-in beyond a module level global opt-in, but even then it's not clear what the opt-in can realistically choose. Thanks,
Alex