Re: [RFC PATCH 1/1] vfio/pci: Disable sriov on PF device close
From: Samiullah Khawaja
Date: Tue Aug 11 2026 - 16:32:44 EST
On Mon, Aug 10, 2026 at 09:32:23AM -0600, Alex Williamson wrote:
On Thu, 6 Aug 2026 16:47:39 -0300
Jason Gunthorpe <jgg@xxxxxxxx> wrote:
Thanks Alex and Jason for feedback.
On Wed, Aug 05, 2026 at 09:03:23AM -0600, Alex Williamson wrote:
> On Wed, 5 Aug 2026 00:33:55 +0000
> Samiullah Khawaja <skhawaja@xxxxxxxxxx> wrote:
>
> > 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.
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.
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).
So we haven't done anything that explicitly blocks or taints this mode,
yet, but correctly handling reset could be a tipping point.
> 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?
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.
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?
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.
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?
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.
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.
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 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.
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
Alex