The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Samiullah Khawaja <skhawaja@google.com>,
	bhelgaas@google.com, Kevin Tian <kevin.tian@intel.com>,
	Leon Romanovsky <leon@kernel.org>,
	dmatlack@google.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, alex@shazbot.org
Subject: Re: [RFC PATCH 1/1] vfio/pci: Disable sriov on PF device close
Date: Mon, 10 Aug 2026 09:32:23 -0600	[thread overview]
Message-ID: <20260810093223.50dbc8d0@shazbot.org> (raw)
In-Reply-To: <20260806194739.GH28508@ziepe.ca>

On Thu, 6 Aug 2026 16:47:39 -0300
Jason Gunthorpe <jgg@ziepe.ca> wrote:

> 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@google.com> 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.

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.
 
> 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

      reply	other threads:[~2026-08-10 15:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05  0:33 [RFC PATCH 0/1] vfio/pci: Disable sriov on PF device close Samiullah Khawaja
2026-08-05  0:33 ` [RFC PATCH 1/1] " Samiullah Khawaja
2026-08-05  9:34   ` Tian, Kevin
2026-08-10 21:47     ` Samiullah Khawaja
2026-08-05 15:03   ` Alex Williamson
2026-08-06 19:47     ` Jason Gunthorpe
2026-08-10 15:32       ` Alex Williamson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260810093223.50dbc8d0@shazbot.org \
    --to=alex@shazbot.org \
    --cc=bhelgaas@google.com \
    --cc=dmatlack@google.com \
    --cc=jgg@ziepe.ca \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=skhawaja@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox