Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Josh Hilke <jrhilke@google.com>
To: Alex Williamson <alex@shazbot.org>
Cc: Josh Hilke <jrhilke@google.com>,
	David Matlack <dmatlack@google.com>,
	kvm@vger.kernel.org,  linux-kernel@vger.kernel.org,
	Sean Christopherson <seanjc@google.com>,
	 Vipin Sharma <vipinsh@google.com>
Subject: [PATCH v10 1/2] vfio: selftests: Require -f in setup.sh to unbind a bound device
Date: Tue,  1 Dec 2026 17:08:48 +0000	[thread overview]
Message-ID: <20261201170849.296378-2-jrhilke@google.com> (raw)
In-Reply-To: <20261201170849.296378-1-jrhilke@google.com>

Make setup.sh refuse to set up a device that is currently bound to a
driver other than vfio-pci unless -f is passed. Devices that are unbound
or already bound to vfio-pci do not require -f.

Validate all device arguments before modifying any device so a refused
device does not leave partial state behind.

Also add -h to print usage and exit 0, and document -f in
Documentation/driver-api/vfio-selftests.rst.

Suggested-by: David Matlack <dmatlack@google.com>
Signed-off-by: Josh Hilke <jrhilke@google.com>
---
 Documentation/driver-api/vfio-selftests.rst   |  9 ++--
 tools/testing/selftests/vfio/scripts/setup.sh | 41 +++++++++++++++++--
 2 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/Documentation/driver-api/vfio-selftests.rst b/Documentation/driver-api/vfio-selftests.rst
index 9d39a22519d6..cfe7a3a06602 100644
--- a/Documentation/driver-api/vfio-selftests.rst
+++ b/Documentation/driver-api/vfio-selftests.rst
@@ -48,12 +48,13 @@ Setting up Devices
 
 .. code-block:: sh
 
-    tools/testing/selftests/vfio/scripts/setup.sh 0000:01:00.0
+    tools/testing/selftests/vfio/scripts/setup.sh [-f] 0000:01:00.0
 
 The script handles unbinding the device from its current driver and binding it
-to ``vfio-pci``. Metadata about this device is stored in
-``/tmp/vfio-selftests-devices`` so that the device can be cleaned up later
-(bound back to its original driver).
+to ``vfio-pci``. If a device is currently bound to a driver other than
+``vfio-pci``, ``setup.sh`` refuses to unbind it unless ``-f`` is passed.
+Metadata about this device is stored in ``/tmp/vfio-selftests-devices`` so that
+the device can be cleaned up later (bound back to its original driver).
 
 Execution
 ---------
diff --git a/tools/testing/selftests/vfio/scripts/setup.sh b/tools/testing/selftests/vfio/scripts/setup.sh
index 49a499e51cbe..7eacd301f0c7 100755
--- a/tools/testing/selftests/vfio/scripts/setup.sh
+++ b/tools/testing/selftests/vfio/scripts/setup.sh
@@ -3,20 +3,55 @@ set -e
 
 source $(dirname -- "${BASH_SOURCE[0]}")/lib.sh
 
+function usage() {
+	cat >&2 <<EOF
+Usage: $0 [-f] [-h] segment:bus:device.function ...
+
+Options:
+  -f  Unbind devices that are currently bound to a driver other than vfio-pci.
+  -h  Print this help message.
+EOF
+}
+
 function main() {
+	local device_bdfs=()
 	local device_bdf
 	local device_dir
+	local force=""
 	local numvfs
 	local driver
+	local opt
 
-	if [ $# = 0 ]; then
-		echo "usage: $0 segment:bus:device.function ..." >&2
+	while getopts "fh" opt; do
+		case "${opt}" in
+		f) force="true" ;;
+		h) usage; exit 0 ;;
+		*) usage; exit 1 ;;
+		esac
+	done
+	shift $((OPTIND - 1))
+
+	device_bdfs=("$@")
+	if [ ${#device_bdfs[@]} -eq 0 ]; then
+		usage
 		exit 1
 	fi
 
-	for device_bdf in "$@"; do
+	for device_bdf in "${device_bdfs[@]}"; do
 		test -d /sys/bus/pci/devices/${device_bdf}
 
+		if [ "${force}" ]; then
+			continue
+		fi
+
+		driver=$(get_driver ${device_bdf})
+		if [ "${driver}" ] && [ "${driver}" != "vfio-pci" ]; then
+			echo "${device_bdf} is bound to ${driver}, pass -f to unbind it." >&2
+			exit 1
+		fi
+	done
+
+	for device_bdf in "${device_bdfs[@]}"; do
 		device_dir=${DEVICES_DIR}/${device_bdf}
 		if [ -d "${device_dir}" ]; then
 			echo "${device_bdf} has already been set up, exiting."

-- 
2.56.1.839.g22a8290db9-goog


  reply	other threads:[~2026-12-01 17:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 17:08 [PATCH v10 0/2] vfio: selftests: Find supported devices for testing Josh Hilke
2026-12-01 17:08 ` Josh Hilke [this message]
2026-12-02 22:32   ` [PATCH v10 1/2] vfio: selftests: Require -f in setup.sh to unbind a bound device David Matlack
2026-12-01 17:08 ` [PATCH v10 2/2] vfio: selftests: Find devices that have VFIO selftest drivers Josh Hilke
2026-12-02 22:34   ` David Matlack

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=20261201170849.296378-2-jrhilke@google.com \
    --to=jrhilke@google.com \
    --cc=alex@shazbot.org \
    --cc=dmatlack@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=seanjc@google.com \
    --cc=vipinsh@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