patches and low-level development discussion
 help / color / mirror / code / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH nixpkgs] spectrumPackages.spectrum-testhost: fix driver bind
Date: Wed, 21 Apr 2021 10:16:05 +0000	[thread overview]
Message-ID: <20210421101605.20790-1-hi@alyssa.is> (raw)

The following operations are available to us in /sys/bus/pci/drivers/vfio-pci:
 bind, unbind, new_id, remove_id

bind and unbind control attachment of a device to a driver.
new_id and remove_id control whether a device should be considered
"supported" by a driver.

But when an unassigned device is newly supported by a driver, the
kernel will automatically bind it to that driver.  We were relying on
the new_id operation binding the device to the driver in this way.

But if the driver is in the supported list, but not bound to anything,
this won't happen.  new_id won't do anything, because it's already in
the supported list.  So we can't rely on new_id binding for us, and
need to try bind as well (at least if new_id fails).

To reproduce:

    echo 8086 1502 > /sys/bus/pci/drivers/vfio-pci/new_id
    echo 0000:00:19.0 > /sys/bus/pci/drivers/vfio-pci/unbind

(Use lspci -nn to find the right values for your hardware.)
This will leave you with a device that is supported by vfio-pci but
not bound to it, which would previously cause vm-net to fail to start,
but should not after this change.
---
 .../linux/spectrum/testhost/default.nix        | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/spectrum/testhost/default.nix b/pkgs/os-specific/linux/spectrum/testhost/default.nix
index 7e1a973e8c6..21c585f1490 100644
--- a/pkgs/os-specific/linux/spectrum/testhost/default.nix
+++ b/pkgs/os-specific/linux/spectrum/testhost/default.nix
@@ -142,7 +142,9 @@ let
             printf "%s" $PCI_LOCATION
           }
 
-          # (Re)bind the device to the VFIO PCI driver.
+          # Tell the VFIO driver it should support our device.  This
+          # is allowed to fail because it might already know that, in
+          # which case it'll return EEXIST.
           if { modprobe vfio-pci }
           backtick -in device_id {
             if { dd bs=2 skip=1 count=2 status=none if=''${PCI_PATH}/vendor }
@@ -155,6 +157,20 @@ let
             printf "%s" $device_id
           }
 
+          # Bind the device to the VFIO driver.  This is allowed to
+          # fail because the new_id operation we just tried will have
+          # bound it automatically for us if it succeeded.  In such a
+          # case, the kernel will return ENODEV (conistency!).
+          foreground {
+            redirfd -w 1 /sys/bus/pci/drivers/vfio-pci/bind
+            printf "%s" $PCI_LOCATION
+          }
+
+          # Because we allow both new_id and bind to fail, we need to
+          # manually make sure now that at least one of them succeeded
+          # and the device is actually attached to the vfio-driver.
+          if { test -e /sys/bus/pci/drivers/vfio-pci/''${PCI_LOCATION} }
+
           foreground { mkdir env }
 
           ${cloud-hypervisor}/bin/cloud-hypervisor
-- 
2.31.0


             reply	other threads:[~2021-04-21 10:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21 10:16 Alyssa Ross [this message]
2021-04-22  1:27 ` [PATCH nixpkgs] spectrumPackages.spectrum-testhost: fix driver bind Cole Helbling
2021-04-22  8:02   ` Alyssa Ross

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=20210421101605.20790-1-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=devel@spectrum-os.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).