Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

vfio: selftests: Add a script to help with running VFIO selftests

Introduce run.sh, a script to help with running VFIO selftests. The
script is intended to be used for both humans manually running VFIO
selftests, and to incorporate into test automation where VFIO selftests
may run alongside other tests. As such the script aims to be hermetic,
returning the system to the state it was before the test started.

The script takes as input the BDF of a device to use and a command to
run (typically the command would be a VFIO selftest). e.g.

$ ./run.sh -d 0000:6a:01.0 ./vfio_pci_device_test

or

$ ./run.sh -d 0000:6a:01.0 -- ./vfio_pci_device_test

The script then handles unbinding device 0000:6a:01.0 from its current
driver, binding it to vfio-pci, running the test, unbinding from
vfio-pci, and binding back to the original driver.

When run.sh runs the provided test, it does so by appending the BDF as
the last parameter. For example:

$ ./run.sh -d 0000:6a:01.0 -- echo hello

Results in the following being printed to stdout:

hello 0000:6a:01.0

The script also supports a mode where it can break out into a shell so
that multiple tests can be run manually.

$ ./run.sh -d 0000:6a:01.0 -s
$ echo $VFIO_SELFTESTS_BDF
$ ./vfio_pci_device_test
$ exit

Choosing which device to use is up to the user.

In the future this script should be extensible to tests that want to use
multiple devices. The script can support accepting -d BDF multiple times
and parse them into an array, setup all the devices, pass the list of
BDFs to the test, and then cleanup all the devices.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-31-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

David Matlack and committed by
Alex Williamson
fd134b0f 8afcbe20

+110
+1
tools/testing/selftests/vfio/Makefile
··· 3 3 TEST_GEN_PROGS += vfio_iommufd_setup_test 4 4 TEST_GEN_PROGS += vfio_pci_device_test 5 5 TEST_GEN_PROGS += vfio_pci_driver_test 6 + TEST_PROGS_EXTENDED := run.sh 6 7 include ../lib.mk 7 8 include lib/libvfio.mk 8 9
+109
tools/testing/selftests/vfio/run.sh
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + # Global variables initialized in main() and then used during cleanup() when 4 + # the script exits. 5 + declare DEVICE_BDF 6 + declare NEW_DRIVER 7 + declare OLD_DRIVER 8 + declare OLD_NUMVFS 9 + declare DRIVER_OVERRIDE 10 + 11 + function write_to() { 12 + # Unfortunately set -x does not show redirects so use echo to manually 13 + # tell the user what commands are being run. 14 + echo "+ echo \"${2}\" > ${1}" 15 + echo "${2}" > ${1} 16 + } 17 + 18 + function bind() { 19 + write_to /sys/bus/pci/drivers/${2}/bind ${1} 20 + } 21 + 22 + function unbind() { 23 + write_to /sys/bus/pci/drivers/${2}/unbind ${1} 24 + } 25 + 26 + function set_sriov_numvfs() { 27 + write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2} 28 + } 29 + 30 + function set_driver_override() { 31 + write_to /sys/bus/pci/devices/${1}/driver_override ${2} 32 + } 33 + 34 + function clear_driver_override() { 35 + set_driver_override ${1} "" 36 + } 37 + 38 + function cleanup() { 39 + if [ "${NEW_DRIVER}" ]; then unbind ${DEVICE_BDF} ${NEW_DRIVER} ; fi 40 + if [ "${DRIVER_OVERRIDE}" ]; then clear_driver_override ${DEVICE_BDF} ; fi 41 + if [ "${OLD_DRIVER}" ]; then bind ${DEVICE_BDF} ${OLD_DRIVER} ; fi 42 + if [ "${OLD_NUMVFS}" ]; then set_sriov_numvfs ${DEVICE_BDF} ${OLD_NUMVFS} ; fi 43 + } 44 + 45 + function usage() { 46 + echo "usage: $0 [-d segment:bus:device.function] [-s] [-h] [cmd ...]" >&2 47 + echo >&2 48 + echo " -d: The BDF of the device to use for the test (required)" >&2 49 + echo " -h: Show this help message" >&2 50 + echo " -s: Drop into a shell rather than running a command" >&2 51 + echo >&2 52 + echo " cmd: The command to run and arguments to pass to it." >&2 53 + echo " Required when not using -s. The SBDF will be " >&2 54 + echo " appended to the argument list." >&2 55 + exit 1 56 + } 57 + 58 + function main() { 59 + local shell 60 + 61 + while getopts "d:hs" opt; do 62 + case $opt in 63 + d) DEVICE_BDF="$OPTARG" ;; 64 + s) shell=true ;; 65 + *) usage ;; 66 + esac 67 + done 68 + 69 + # Shift past all optional arguments. 70 + shift $((OPTIND - 1)) 71 + 72 + # Check that the user passed in the command to run. 73 + [ ! "${shell}" ] && [ $# = 0 ] && usage 74 + 75 + # Check that the user passed in a BDF. 76 + [ "${DEVICE_BDF}" ] || usage 77 + 78 + trap cleanup EXIT 79 + set -e 80 + 81 + test -d /sys/bus/pci/devices/${DEVICE_BDF} 82 + 83 + if [ -f /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs ]; then 84 + OLD_NUMVFS=$(cat /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs) 85 + set_sriov_numvfs ${DEVICE_BDF} 0 86 + fi 87 + 88 + if [ -L /sys/bus/pci/devices/${DEVICE_BDF}/driver ]; then 89 + OLD_DRIVER=$(basename $(readlink -m /sys/bus/pci/devices/${DEVICE_BDF}/driver)) 90 + unbind ${DEVICE_BDF} ${OLD_DRIVER} 91 + fi 92 + 93 + set_driver_override ${DEVICE_BDF} vfio-pci 94 + DRIVER_OVERRIDE=true 95 + 96 + bind ${DEVICE_BDF} vfio-pci 97 + NEW_DRIVER=vfio-pci 98 + 99 + echo 100 + if [ "${shell}" ]; then 101 + echo "Dropping into ${SHELL} with VFIO_SELFTESTS_BDF=${DEVICE_BDF}" 102 + VFIO_SELFTESTS_BDF=${DEVICE_BDF} ${SHELL} 103 + else 104 + "$@" ${DEVICE_BDF} 105 + fi 106 + echo 107 + } 108 + 109 + main "$@"