Skip to contentSkip to conversation

vfio: selftests: Support for multi-device tests

PATCH Superseded David Matlack 4 participants 25 messages Oct 8, 2025 – Nov 10, 2025

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

David Matlack

author maintainer
#0 lore raw

cover vfio: selftests: Support for multi-device tests

This series adds support for tests that use multiple devices, and adds one new test, vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").

This series also breaks apart the monolithic vfio_util.h and vfio_pci_device.c into separate files, to account for all the new code. This required some code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.

C files:
- tools/testing/selftests/vfio/lib/libvfio.c
- tools/testing/selftests/vfio/lib/iommu.c
- tools/testing/selftests/vfio/lib/vfio_pci_device.c
- tools/testing/selftests/vfio/lib/vfio_pci_driver.c

H files:
- tools/testing/selftests/vfio/lib/include/libvfio.h
- tools/testing/selftests/vfio/lib/include/libvfio/assert.h
- tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
- tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
- tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h

Notably, vfio_util.h is now gone and replaced with libvfio.h.

This series is based on vfio next, and can be found on GitHub:

https://github.com/dmatlack/linux/tree/vfio/selftests/init_perf_test/v1

Cc Jason Gunthorpe <[email protected]>
Cc Vipin Sharma <[email protected]>
Cc Josh Hilke <[email protected]>

David Matlack (12):
vfio: selftests: Split run.sh into separate scripts
vfio: selftests: Allow passing multiple BDFs on the command line
vfio: selftests: Rename struct vfio_iommu_mode to iommu_mode
vfio: selftests: Introduce struct iommu
vfio: selftests: Support multiple devices in the same
container/iommufd
vfio: selftests: Eliminate overly chatty logging
vfio: selftests: Prefix logs with device BDF where relevant
vfio: selftests: Rename struct vfio_dma_region to dma_region
vfio: selftests: Move iommu_*() functions into iommu.c
vfio: selftests: Rename vfio_util.h to libvfio.h
vfio: selftests: Split libvfio.h into separate header files
vfio: selftests: Add vfio_pci_device_init_perf_test

 tools/testing/selftests/vfio/Makefile         |   3 +
 .../selftests/vfio/lib/drivers/dsa/dsa.c      |  36 +--
 .../selftests/vfio/lib/drivers/ioat/ioat.c    |  18 +-
 .../selftests/vfio/lib/include/libvfio.h      |  25 ++
 .../vfio/lib/include/libvfio/assert.h         |  53 ++++
 .../vfio/lib/include/libvfio/iommu.h          |  53 ++++
 .../lib/include/libvfio/vfio_pci_device.h     | 143 +++++++++
 .../lib/include/libvfio/vfio_pci_driver.h     |  98 ++++++
 .../selftests/vfio/lib/include/vfio_util.h    | 295 ------------------
 tools/testing/selftests/vfio/lib/iommu.c      | 219 +++++++++++++
 tools/testing/selftests/vfio/lib/libvfio.c    |  77 +++++
 tools/testing/selftests/vfio/lib/libvfio.mk   |   4 +-
 .../selftests/vfio/lib/vfio_pci_device.c      | 264 ++--------------
 .../selftests/vfio/lib/vfio_pci_driver.c      |  16 +-
 tools/testing/selftests/vfio/run.sh           | 109 -------
 .../testing/selftests/vfio/scripts/cleanup.sh |  42 +++
 tools/testing/selftests/vfio/scripts/lib.sh   |  42 +++
 tools/testing/selftests/vfio/scripts/run.sh   |  16 +
 tools/testing/selftests/vfio/scripts/setup.sh |  48 +++
 .../selftests/vfio/vfio_dma_mapping_test.c    |   4 +-
 .../selftests/vfio/vfio_iommufd_setup_test.c  |   2 +-
 .../vfio/vfio_pci_device_init_perf_test.c     | 163 ++++++++++
 .../selftests/vfio/vfio_pci_device_test.c     |   2 +-
 .../selftests/vfio/vfio_pci_driver_test.c     |   8 +-
 24 files changed, 1051 insertions(+), 689 deletions(-)
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/assert.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h
 delete mode 100644 tools/testing/selftests/vfio/lib/include/vfio_util.h
 create mode 100644 tools/testing/selftests/vfio/lib/iommu.c
 create mode 100644 tools/testing/selftests/vfio/lib/libvfio.c
 delete mode 100755 tools/testing/selftests/vfio/run.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/cleanup.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/lib.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/run.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/setup.sh
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c

base-commit: 451bb96328981808463405d436bd58de16dd967d
prerequisite-patch-id: 2284290c5021a6efe82f911f0d86311b3f0c5794

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#1 lore raw

PATCH 1/12 vfio: selftests: Split run.sh into separate scripts

Split run.sh into separate scripts (setup.sh, run.sh, cleanup.sh) to enable multi-device testing, and prepare for VFIO selftests automatically detecting which devices to use for testing by storing device metadata on the filesystem.

- setup.sh takes one or more BDFs as arguments and sets up each device. Metadata about each device is stored on the filesystem in the directory:

	   ${TMPDIR:-/tmp}/vfio-selftests-devices

Within this directory is a directory for each BDF, and then files in those directories that cleanup.sh uses to cleanup the device.

- run.sh runs a selftest by passing it the BDFs of all set up devices.

- cleanup.sh takes zero or more BDFs as arguments and cleans up each device. If no BDFs are provided, it cleans up all devices.

This split enables multi-device testing by allowing multiple BDFs to be set up and passed into tests:

For example:

  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF1> <BDF2>
  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF3>
  $ tools/testing/selftests/vfio/scripts/run.sh echo
  <BDF1> <BDF2> <BDF3>
  $ tools/testing/selftests/vfio/scripts/cleanup.sh

If the future, VFIO selftests can automatically detect set up devices by inspecting ${TMPDIR:-/tmp}/vfio-selftests-devices. This will avoid the need for the run.sh script.

---
 tools/testing/selftests/vfio/run.sh           | 109 ------------------
 .../testing/selftests/vfio/scripts/cleanup.sh |  42 +++++++
 tools/testing/selftests/vfio/scripts/lib.sh   |  42 +++++++
 tools/testing/selftests/vfio/scripts/run.sh   |  16 +++
 tools/testing/selftests/vfio/scripts/setup.sh |  48 ++++++++
 5 files changed, 148 insertions(+), 109 deletions(-)
 delete mode 100755 tools/testing/selftests/vfio/run.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/cleanup.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/lib.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/run.sh
 create mode 100755 tools/testing/selftests/vfio/scripts/setup.sh
tools/testing/selftests/vfio/run.sh deleted +0 -109
deleted file mode 100755
index 0476b6d7adc3..000000000000
@@ -1,109 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-or-later
-
-# Global variables initialized in main() and then used during cleanup() when
-# the script exits.
-declare DEVICE_BDF
-declare NEW_DRIVER
-declare OLD_DRIVER
-declare OLD_NUMVFS
-declare DRIVER_OVERRIDE
-
-function write_to() {
-	# Unfortunately set -x does not show redirects so use echo to manually
-	# tell the user what commands are being run.
-	echo "+ echo \"${2}\" > ${1}"
-	echo "${2}" > ${1}
-}
-
-function bind() {
-	write_to /sys/bus/pci/drivers/${2}/bind ${1}
-}
-
-function unbind() {
-	write_to /sys/bus/pci/drivers/${2}/unbind ${1}
-}
-
-function set_sriov_numvfs() {
-	write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2}
-}
-
-function set_driver_override() {
-	write_to /sys/bus/pci/devices/${1}/driver_override ${2}
-}
-
-function clear_driver_override() {
-	set_driver_override ${1} ""
-}
-
-function cleanup() {
-	if [ "${NEW_DRIVER}"      ]; then unbind ${DEVICE_BDF} ${NEW_DRIVER} ; fi
-	if [ "${DRIVER_OVERRIDE}" ]; then clear_driver_override ${DEVICE_BDF} ; fi
-	if [ "${OLD_DRIVER}"      ]; then bind ${DEVICE_BDF} ${OLD_DRIVER} ; fi
-	if [ "${OLD_NUMVFS}"      ]; then set_sriov_numvfs ${DEVICE_BDF} ${OLD_NUMVFS} ; fi
-}
-
-function usage() {
-	echo "usage: $0 [-d segment:bus:device.function] [-s] [-h] [cmd ...]" >&2
-	echo >&2
-	echo "  -d: The BDF of the device to use for the test (required)" >&2
-	echo "  -h: Show this help message" >&2
-	echo "  -s: Drop into a shell rather than running a command" >&2
-	echo >&2
-	echo "   cmd: The command to run and arguments to pass to it." >&2
-	echo "        Required when not using -s. The SBDF will be " >&2
-	echo "        appended to the argument list." >&2
-	exit 1
-}
-
-function main() {
-	local shell
-
-	while getopts "d:hs" opt; do
-		case $opt in
-			d) DEVICE_BDF="$OPTARG" ;;
-			s) shell=true ;;
-			*) usage ;;
-		esac
-	done
-
-	# Shift past all optional arguments.
-	shift $((OPTIND - 1))
-
-	# Check that the user passed in the command to run.
-	[ ! "${shell}" ] && [ $# = 0 ] && usage
-
-	# Check that the user passed in a BDF.
-	[ "${DEVICE_BDF}" ] || usage
-
-	trap cleanup EXIT
-	set -e
-
-	test -d /sys/bus/pci/devices/${DEVICE_BDF}
-
-	if [ -f /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs ]; then
-		OLD_NUMVFS=$(cat /sys/bus/pci/devices/${DEVICE_BDF}/sriov_numvfs)
-		set_sriov_numvfs ${DEVICE_BDF} 0
-	fi
-
-	if [ -L /sys/bus/pci/devices/${DEVICE_BDF}/driver ]; then
-		OLD_DRIVER=$(basename $(readlink -m /sys/bus/pci/devices/${DEVICE_BDF}/driver))
-		unbind ${DEVICE_BDF} ${OLD_DRIVER}
-	fi
-
-	set_driver_override ${DEVICE_BDF} vfio-pci
-	DRIVER_OVERRIDE=true
-
-	bind ${DEVICE_BDF} vfio-pci
-	NEW_DRIVER=vfio-pci
-
-	echo
-	if [ "${shell}" ]; then
-		echo "Dropping into ${SHELL} with VFIO_SELFTESTS_BDF=${DEVICE_BDF}"
-		VFIO_SELFTESTS_BDF=${DEVICE_BDF} ${SHELL}
-	else
-		"$@" ${DEVICE_BDF}
-	fi
-	echo
-}
-
-main "$@"
tools/testing/selftests/vfio/scripts/cleanup.sh new file +42 -0

42 lines not on this page · raw message

tools/testing/selftests/vfio/scripts/lib.sh new file +42 -0

42 lines not on this page · raw message

tools/testing/selftests/vfio/scripts/run.sh new file +16 -0
new file mode 100755
index 000000000000..da08753892c4
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+source $(dirname ${BASH_SOURCE[0]})/lib.sh
+
+function main() {
+	local device_bdfs=$(ls ${DEVICES_DIR})
+
+	if [ -z "${device_bdfs}" ]; then
+		echo "No devices found, skipping."
+		exit 4
+	fi
+
+	"$@" ${device_bdfs}
+}
+
+main "$@"
tools/testing/selftests/vfio/scripts/setup.sh new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
#2 lore raw

Re: [PATCH 01/12] vfio: selftests: Split run.sh into separate scripts

Quoted 25 lines · David Matlack

Split run.sh into separate scripts (setup.sh, run.sh, cleanup.sh) to enable multi-device testing, and prepare for VFIO selftests automatically detecting which devices to use for testing by storing device metadata on the filesystem.

- setup.sh takes one or more BDFs as arguments and sets up each device. Metadata about each device is stored on the filesystem in the directory:

           ${TMPDIR:-/tmp}/vfio-selftests-devices

Within this directory is a directory for each BDF, and then files in those directories that cleanup.sh uses to cleanup the device.

- run.sh runs a selftest by passing it the BDFs of all set up devices.

- cleanup.sh takes zero or more BDFs as arguments and cleans up each device. If no BDFs are provided, it cleans up all devices.

This split enables multi-device testing by allowing multiple BDFs to be set up and passed into tests:

For example:

Quoted 7 lines
  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF1> <BDF2>
  $ tools/testing/selftests/vfio/scripts/setup.sh <BDF3>
  $ tools/testing/selftests/vfio/scripts/run.sh echo
  <BDF1> <BDF2> <BDF3>
  $ tools/testing/selftests/vfio/scripts/cleanup.sh

If the future, VFIO selftests can automatically detect set up devices by

nit: s/If/In

Quoted 17 lines
tools/testing/selftests/vfio/scripts/cleanup.sh new file +17 -0
+function cleanup_devices() {
+       local device_bdf
+       local device_dir
+
+       for device_bdf in "$@"; do
+               device_dir=${DEVICES_DIR}/${device_bdf}
+
+               if [ -f ${device_dir}/vfio-pci ]; then
+                       unbind ${device_bdf} vfio-pci
+               fi
+
+               if [ -f ${device_dir}/driver_override ]; then
+                       clear_driver_override ${device_bdf}
+               fi
+
+               if [ -f ${device_dir}/driver ]; then
+                       bind ${device_bdf} $(cat ${device_dir}/driver)

tools/testing/selftests/vfio/scripts/cleanup.sh line 31

Quoted 10 lines
tools/testing/selftests/vfio/scripts/cleanup.sh new file +10 -0
+               fi
+
+               if [ -f ${device_dir}/sriov_numvfs ]; then
+                       set_sriov_numvfs ${device_bdf} $(cat ${device_dir}/sriov_numvfs)
+               fi
+
+               rm -rf ${device_dir}
+       done
+
+       rm -rf ${DEVICES_DIR}

Since the cleanup.sh can potentially be run against a single device, we should probably check if no devices exist under ${DEVICES_DIR} before removing the entire dir.

Quoted 28 lines
tools/testing/selftests/vfio/scripts/cleanup.sh new file +11 -0
+}
+
+function main() {
+       if [ $# = 0 ]; then
+               cleanup_devices $(ls ${DEVICES_DIR})
+       else
+               cleanup_devices "$@"
+       fi
+}
+
+main "$@"
tools/testing/selftests/vfio/scripts/lib.sh new file +10 -0
new file mode 100755
index 000000000000..9f05f29c7b86
@@ -0,0 +1,42 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+readonly DEVICES_DIR="${TMPDIR:-/tmp}/vfio-selftests-devices"
+
+function write_to() {
+       # Unfortunately set -x does not show redirects so use echo to manually
+       # tell the user what commands are being run.
+       echo "+ echo \"${2}\" > ${1}"
+       echo "${2}" > ${1}
+}

tools/testing/selftests/vfio/scripts/lib.sh line 20

Quoted 10 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +10 -0
+
+function get_driver() {
+       if [ -L /sys/bus/pci/devices/${1}/driver ]; then
+               basename $(readlink -m /sys/bus/pci/devices/${1}/driver)
+       fi
+}
+
+function bind() {
+       write_to /sys/bus/pci/drivers/${2}/bind ${1}
+}

Since these scripts reside within the selftests/vfio/ dir, and most of these functions target PCI aspects, should we hardcode vfio-pci as the driver instead of passing the arg around? This is a general question/suggestion for the patch. We can even rename it to <op>_vfio_pci() (bind_vfio_pci() for example) for better clarity.

tools/testing/selftests/vfio/scripts/lib.sh line 29

Quoted 9 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +9 -0
+
+function unbind() {
+       write_to /sys/bus/pci/drivers/${2}/unbind ${1}
+}
+
+function set_sriov_numvfs() {
+       write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2}
+}
+

Similar to get_sriov_numvfs(), shall we check the existence of the 'sriov_numvfs' first? In the current workflow, we indirectly check for the file using get_sriov_numvfs() or look for 'sriov_numvfs' in the ${device_bdf}. However, an independent check would be cleaner and safer (for any future references). WDYT?

Quoted 29 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +28 -0
+function get_sriov_numvfs() {
+       if [ -f /sys/bus/pci/devices/${1}/sriov_numvfs ]; then
+               cat /sys/bus/pci/devices/${1}/sriov_numvfs
+       fi
+}
+
 
+function main() {
+       local device_bdf
+       local device_dir
+       local numvfs
+       local driver
+
+       if [ $# = 0 ]; then
+               echo "usage: $0 segment:bus:device.function ..." >&2
+               exit 1
+       fi
+
+       for device_bdf in "$@"; do
+               test -d /sys/bus/pci/devices/${device_bdf}
+
+               device_dir=${DEVICES_DIR}/${device_bdf}
+               if [ -d "${device_dir}" ]; then
+                       echo "${device_bdf} has already been set up, exiting."
+                       exit 0
+               fi
+
+               mkdir -p ${device_dir}
+

tools/testing/selftests/vfio/scripts/lib.sh

Quoted 10 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +10 -0
+               numvfs=$(get_sriov_numvfs ${device_bdf})
+               if [ "${numvfs}" ]; then
+                       set_sriov_numvfs ${device_bdf} 0
+                       echo ${numvfs} > ${device_dir}/sriov_numvfs
+               fi
+
+               driver=$(get_driver ${device_bdf})
+               if [ "${driver}" ]; then
+                       unbind ${device_bdf} ${driver}
+                       echo ${driver} > ${device_dir}/driver

Sorry, what is the purpose of writing the driver's name to the "driver" file? Isn't "unbind" sufficient?

tools/testing/selftests/vfio/scripts/lib.sh

Quoted 2 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +2 -0
+               fi
+

Since vfio-pci can be built as a module that may not be loaded yet (or even disabled), do you think it's worth checking before binding the device to the driver? Of course, these operations would fail, but would it be better if we informed users why?

tools/testing/selftests/vfio/scripts/lib.sh

Quoted 10 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +9 -1
+               set_driver_override ${device_bdf} vfio-pci
+               touch ${device_dir}/driver_override
+
+               bind ${device_bdf} vfio-pci
+               touch ${device_dir}/vfio-pci
+       done
+}
+
+main "$@"
--

Thank you.
Raghavendra

author maintainer
#3 lore raw

Re: [PATCH 01/12] vfio: selftests: Split run.sh into separate scripts

Quoted 4 lines · Raghavendra Rao Ananta
Quoted 2 lines · David Matlack

If the future, VFIO selftests can automatically detect set up devices by

nit: s/If/In

Ack

Quoted 4 lines
Quoted 1 line

+ rm -rf ${DEVICES_DIR}

Since the cleanup.sh can potentially be run against a single device, we should probably check if no devices exist under ${DEVICES_DIR} before removing the entire dir.

Oof, good catch. Will fix in v2.

Quoted 8 lines

tools/testing/selftests/vfio/scripts/lib.sh line 20

Quoted 3 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +3 -0
+function bind() {
+       write_to /sys/bus/pci/drivers/${2}/bind ${1}
+}

Since these scripts reside within the selftests/vfio/ dir, and most of these functions target PCI aspects, should we hardcode vfio-pci as the driver instead of passing the arg around? This is a general question/suggestion for the patch. We can even rename it to <op>_vfio_pci() (bind_vfio_pci() for example) for better clarity.

This function is also used to bind the device back to whatever driver it was originally bound to in cleanup.sh. So "${2}" is not always "vfio-pci".

Quoted 9 lines

tools/testing/selftests/vfio/scripts/lib.sh line 29

Quoted 4 lines
tools/testing/selftests/vfio/scripts/lib.sh new file +4 -0
+function set_sriov_numvfs() {
+       write_to /sys/bus/pci/devices/${1}/sriov_numvfs ${2}
+}
+

Similar to get_sriov_numvfs(), shall we check the existence of the 'sriov_numvfs' first? In the current workflow, we indirectly check for the file using get_sriov_numvfs() or look for 'sriov_numvfs' in the ${device_bdf}. However, an independent check would be cleaner and safer (for any future references). WDYT?

I don't think set_sriov_numvfs() should silently do nothing if sriov_numvfs file does not exist. Calling set_sriov_numvfs() on a device that does not support SR-IOV indicates a bug in the caller, so it should fail IMO.

Quoted 6 lines

tools/testing/selftests/vfio/scripts/setup.sh line 37

Quoted 4 lines
tools/testing/selftests/vfio/scripts/setup.sh new file +4 -0
+               driver=$(get_driver ${device_bdf})
+               if [ "${driver}" ]; then
+                       unbind ${device_bdf} ${driver}
+                       echo ${driver} > ${device_dir}/driver

Sorry, what is the purpose of writing the driver's name to the "driver" file? Isn't "unbind" sufficient?

So that cleanup.sh can bind the device back to this driver. The goal is that cleanup.sh returns the device back to the state it was in before setup.sh. i.e. cleanup.sh needs to undo everything done by setup.sh.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+               fi
+

Since vfio-pci can be built as a module that may not be loaded yet (or even disabled), do you think it's worth checking before binding the device to the driver? Of course, these operations would fail, but would it be better if we informed users why?

If that were to happen, I think it will be debuggable by the user.

write_to() in lib.sh always prints out what setup.sh and cleanup.sh are trying to do. So the user will see something like:

echo BDF > /sys/bus/pci/drivers/vfio-pci/bind
Error: No such file or directory

And then the user can go investigate why /sys/bus/pci/drivers/ does not exist. I think if we try to make the script diagnose why something failed, that will be an endless game of whack-a-mole and the scripts will start getting long and complex.

author maintainer
#4 lore raw

PATCH 2/12 vfio: selftests: Allow passing multiple BDFs on the command line

In reply to David Matlack

Add support for passing multiple device BDFs to a test via the command line. This is a prerequisite for multi-device tests.

Single-device tests can continue using vfio_selftests_get_bdf(), which will continue to return argv[argc - 1] (if it is a BDF string), or the environment variable $VFIO_SELFTESTS_BDF otherwise.

For multi-device tests, a new helper called vfio_selftests_get_bdfs() is introduced which will return an array of all BDFs found at the end of argv[], as well as the number of BDFs found (passed back to the caller via argument). The array of BDFs returned does not need to be freed by the caller.

The environment variable VFIO_SELFTESTS_BDF continues to support only a single BDF for the time being.

---
 .../selftests/vfio/lib/include/vfio_util.h    |  2 +
 .../selftests/vfio/lib/vfio_pci_device.c      | 58 +++++++++++++++----
 2 files changed, 48 insertions(+), 12 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +2 -0
index ed31606e01b7..2acf119cbedb 100644
@@ -198,6 +198,8 @@ struct vfio_pci_device {
  * If BDF cannot be determined then the test will exit with KSFT_SKIP.
  */
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
+char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
+
 const char *vfio_pci_get_cdev_path(const char *bdf);
 
 extern const char *default_iommu_mode;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +46 -12

75 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
#5 lore raw

Re: [PATCH 02/12] vfio: selftests: Allow passing multiple BDFs on the command line

tools/testing/selftests/vfio/lib/vfio_pci_device.c static bool is_bdf(const char *str) · line 578

Quoted 12 lines · David Matlack
tools/testing/selftests/vfio/lib/vfio_pci_device.c +9 -2
-const char *vfio_selftests_get_bdf(int *argc, char *argv[])
+static char **vfio_selftests_get_bdfs_cmdline(int *argc, char *argv[], int *nr_bdfs)
 {
-       char *bdf;
+       int i;
+
+       for (i = *argc - 1; i > 0 && is_bdf(argv[i]); i--)
+               continue;
+
+       i++;
+       *nr_bdfs = *argc - i;
+       *argc -= *nr_bdfs;

Just curious, why update 'argc' (I know we had this before as well)?

Quoted 13 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6 -4
+
+       return *nr_bdfs ? &argv[i] : NULL;
+}
 
-       if (*argc > 1 && is_bdf(argv[*argc - 1]))
-               return argv[--(*argc)];
+static char **vfio_selftests_get_bdfs_env(int *argc, char *argv[], int *nr_bdfs)
+{
+       static char *bdf;
 
        bdf = getenv("VFIO_SELFTESTS_BDF");
-       if (bdf) {
-               VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);

tools/testing/selftests/vfio/lib/vfio_pci_device.c static bool is_bdf(const char *str) · line 595

Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +8 -2
-               return bdf;
-       }
+       if (!bdf)
+               return NULL;
+
+       *nr_bdfs = 1;
+       VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);
+
+       return &bdf;
+}

nit: Since vfio_selftests_get_bdfs_env() still returns a single BDF, perhaps add a comment, as it contradicts the plurality in the function's name?

Thank you.
Raghavendra

author maintainer
#6 lore raw

Re: [PATCH 02/12] vfio: selftests: Allow passing multiple BDFs on the command line

Quoted 14 lines · Raghavendra Rao Ananta

tools/testing/selftests/vfio/lib/vfio_pci_device.c static bool is_bdf(const char *str) · line 578

Quoted 12 lines · David Matlack
tools/testing/selftests/vfio/lib/vfio_pci_device.c +9 -2
-const char *vfio_selftests_get_bdf(int *argc, char *argv[])
+static char **vfio_selftests_get_bdfs_cmdline(int *argc, char *argv[], int *nr_bdfs)
 {
-       char *bdf;
+       int i;
+
+       for (i = *argc - 1; i > 0 && is_bdf(argv[i]); i--)
+               continue;
+
+       i++;
+       *nr_bdfs = *argc - i;
+       *argc -= *nr_bdfs;

Just curious, why update 'argc' (I know we had this before as well)?

The idea is to parse out the BDFs from the command line, and then let the test parse the rest of the command line. So we modify argc to remove the BDFs from the command line, so the test doesn't try to use them for something else.

Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +3 -0
+static char **vfio_selftests_get_bdfs_env(int *argc, char *argv[], int *nr_bdfs)
+{
+       static char *bdf;
Quoted 16 lines

tools/testing/selftests/vfio/lib/vfio_pci_device.c static bool is_bdf(const char *str) · line 595

Quoted 13 lines
        bdf = getenv("VFIO_SELFTESTS_BDF");
tools/testing/selftests/vfio/lib/vfio_pci_device.c +8 -4
-       if (bdf) {
-               VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);
-               return bdf;
-       }
+       if (!bdf)
+               return NULL;
+
+       *nr_bdfs = 1;
+       VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf);
+
+       return &bdf;
+}

nit: Since vfio_selftests_get_bdfs_env() still returns a single BDF, perhaps add a comment, as it contradicts the plurality in the function's name?

Good point. I'll drop the plurality and make this function only return a single BDF.

author maintainer
#7 lore raw

PATCH 3/12 vfio: selftests: Rename struct vfio_iommu_mode to iommu_mode

In reply to David Matlack

Rename struct vfio_iommu_mode to struct iommu_mode since the mode can include iommufd. This also prepares for splitting out all the IOMMU code into its own structs/helpers/files which are independent from the vfio_pci_device code.

No function change intended.

---
 tools/testing/selftests/vfio/lib/include/vfio_util.h | 4 ++--
 tools/testing/selftests/vfio/lib/vfio_pci_device.c   | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +2 -2
index 2acf119cbedb..5b8d5444f105 100644
@@ -47,7 +47,7 @@
 	VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__);			\
 } while (0)
 
-struct vfio_iommu_mode {
+struct iommu_mode {
 	const char *name;
 	const char *container_path;
 	unsigned long iommu_type;
@@ -163,7 +163,7 @@ struct vfio_pci_driver {
 struct vfio_pci_device {
 	int fd;
 
-	const struct vfio_iommu_mode *iommu_mode;
+	const struct iommu_mode *iommu_mode;
 	int group_fd;
 	int container_fd;
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -2
index f2fc5a52902b..da8edf297a4d 100644
@@ -411,7 +411,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
 }
 
 /* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */
-static const struct vfio_iommu_mode iommu_modes[] = {
+static const struct iommu_mode iommu_modes[] = {
 	{
 		.name = "vfio_type1_iommu",
 		.container_path = "/dev/vfio/vfio",
@@ -439,7 +439,7 @@ static const struct vfio_iommu_mode iommu_modes[] = {
 
 const char *default_iommu_mode = "iommufd";
 
-static const struct vfio_iommu_mode *lookup_iommu_mode(const char *iommu_mode)
+static const struct iommu_mode *lookup_iommu_mode(const char *iommu_mode)
 {
 	int i;
 
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#8 lore raw

PATCH 4/12 vfio: selftests: Introduce struct iommu

In reply to David Matlack

Introduce struct iommu, which logically represents either a VFIO container or an iommufd IOAS, depending on which IOMMU mode is used by the test.

This will be used in a subsequent commit to allow devices to be added to the same container/iommufd.

---
 .../selftests/vfio/lib/include/vfio_util.h    | 16 ++---
 .../selftests/vfio/lib/vfio_pci_device.c      | 62 ++++++++++---------
 2 files changed, 42 insertions(+), 36 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +9 -7
index 5b8d5444f105..14cd0bec45c0 100644
@@ -160,15 +160,19 @@ struct vfio_pci_driver {
 	int msi;
 };
 
+struct iommu {
+	const struct iommu_mode *mode;
+	int container_fd;
+	int iommufd;
+	u32 ioas_id;
+	struct list_head dma_regions;
+};
+
 struct vfio_pci_device {
 	int fd;
-
-	const struct iommu_mode *iommu_mode;
 	int group_fd;
-	int container_fd;
 
-	int iommufd;
-	u32 ioas_id;
+	struct iommu *iommu;
 
 	struct vfio_device_info info;
 	struct vfio_region_info config_space;
@@ -177,8 +181,6 @@ struct vfio_pci_device {
 	struct vfio_irq_info msi_info;
 	struct vfio_irq_info msix_info;
 
-	struct list_head dma_regions;
-
 	/* eventfds for MSI and MSI-x interrupts */
 	int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1];
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +33 -29

155 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#9 lore raw

PATCH 5/12 vfio: selftests: Support multiple devices in the same container/iommufd

In reply to David Matlack

Support tests that want to add multiple devices to the same container/iommufd by decoupling struct vfio_pci_device from struct iommu.

For backwards compatibility with existing tests, and to keep single-device tests simple, vfio_pci_device_init() and vfio_pci_device_cleanup() remain unchanged.

Multi-devices tests can now put multiple devices in the same container/iommufd like so:

  iommu = iommu_init(iommu_mode);

  device1 = __vfio_pci_device_init(bdf1, iommu);
  device2 = __vfio_pci_device_init(bdf2, iommu);
  device3 = __vfio_pci_device_init(bdf3, iommu);

...

  __vfio_pci_device_cleanup(device3);
  __vfio_pci_device_cleanup(device2);
  __vfio_pci_device_cleanup(device1);

  iommu_cleanup(iommu);
---
 .../selftests/vfio/lib/include/vfio_util.h    |   7 ++
 .../selftests/vfio/lib/vfio_pci_device.c      | 107 ++++++++++++------
 2 files changed, 80 insertions(+), 34 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +7 -0
index 14cd0bec45c0..8a01bcaa3ee8 100644
@@ -206,8 +206,15 @@ const char *vfio_pci_get_cdev_path(const char *bdf);
 
 extern const char *default_iommu_mode;
 
+struct iommu *iommu_init(const char *iommu_mode);
+void iommu_cleanup(struct iommu *iommu);
+
+struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, struct iommu *iommu);
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
+
+void __vfio_pci_device_cleanup(struct vfio_pci_device *device);
 void vfio_pci_device_cleanup(struct vfio_pci_device *device);
+
 void vfio_pci_device_reset(struct vfio_pci_device *device);
 
 void vfio_pci_dma_map(struct vfio_pci_device *device,
tools/testing/selftests/vfio/lib/vfio_pci_device.c +73 -34

152 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#10 lore raw

Re: [PATCH 05/12] vfio: selftests: Support multiple devices in the same container/iommufd

Quoted 4 lines · David Matlack

For backwards compatibility with existing tests, and to keep single-device tests simple, vfio_pci_device_init() and vfio_pci_device_cleanup() remain unchanged.

Quoted 8 lines

Multi-devices tests can now put multiple devices in the same container/iommufd like so:

  iommu = iommu_init(iommu_mode);

  device1 = __vfio_pci_device_init(bdf1, iommu);
  device2 = __vfio_pci_device_init(bdf2, iommu);
  device3 = __vfio_pci_device_init(bdf3, iommu);

After using this code internally for a few months, I think it would be better to just require all tests to call iommu_init() and then vfio_pci_device_init(). It is not really that much new code to add to tests, and that will leave the function name __vfio_pci_device_init() available for other use-cases (like [1]).

[1] https://lore.kernel.org/kvm/[email protected]/

#11 lore raw

Re: [PATCH 05/12] vfio: selftests: Support multiple devices in the same container/iommufd

Quoted 15 lines · David Matlack
Quoted 4 lines · David Matlack

For backwards compatibility with existing tests, and to keep single-device tests simple, vfio_pci_device_init() and vfio_pci_device_cleanup() remain unchanged.

Quoted 8 lines

Multi-devices tests can now put multiple devices in the same container/iommufd like so:

  iommu = iommu_init(iommu_mode);

  device1 = __vfio_pci_device_init(bdf1, iommu);
  device2 = __vfio_pci_device_init(bdf2, iommu);
  device3 = __vfio_pci_device_init(bdf3, iommu);
Quoted 7 lines

After using this code internally for a few months, I think it would be better to just require all tests to call iommu_init() and then vfio_pci_device_init(). It is not really that much new code to add to tests, and that will leave the function name __vfio_pci_device_init() available for other use-cases (like [1]).

[1] https://lore.kernel.org/kvm/[email protected]/

I'm fine with that. I see what you're saying regarding [1].

author maintainer
#12 lore raw

PATCH 6/12 vfio: selftests: Eliminate overly chatty logging

In reply to David Matlack

Eliminate overly chatty logs that are printed during almost every test. These logs are adding more noise than value. If a test cares about this information it can log it itself. This is especially true as the VFIO selftests gains support for multiple devices in a single test (which multiplies all these logs).

---
 tools/testing/selftests/vfio/lib/vfio_pci_driver.c | 14 --------------
 1 file changed, 14 deletions(-)
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +0 -14
index e5e8723ecb41..abb7a62a03ea 100644
@@ -1,6 +1,4 @@
 // SPDX-License-Identifier: GPL-2.0-only
-#include <stdio.h>
-
 #include "../../../kselftest.h"
 #include <vfio_util.h>
 
@@ -29,7 +27,6 @@ void vfio_pci_driver_probe(struct vfio_pci_device *device)
 		if (ops->probe(device))
 			continue;
 
-		printf("Driver found: %s\n", ops->name);
 		device->driver.ops = ops;
 	}
 }
@@ -58,17 +55,6 @@ void vfio_pci_driver_init(struct vfio_pci_device *device)
 	driver->ops->init(device);
 
 	driver->initialized = true;
-
-	printf("%s: region: vaddr %p, iova 0x%lx, size 0x%lx\n",
-	       driver->ops->name,
-	       driver->region.vaddr,
-	       driver->region.iova,
-	       driver->region.size);
-
-	printf("%s: max_memcpy_size 0x%lx, max_memcpy_count 0x%lx\n",
-	       driver->ops->name,
-	       driver->max_memcpy_size,
-	       driver->max_memcpy_count);
 }
 
 void vfio_pci_driver_remove(struct vfio_pci_device *device)
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#13 lore raw

PATCH 7/12 vfio: selftests: Prefix logs with device BDF where relevant

In reply to David Matlack

Prefix log messages with the device's BDF where relevant. This will help understanding VFIO selftests logs when tests are run with multiple devices.

---
 .../selftests/vfio/lib/drivers/dsa/dsa.c      | 34 +++++++++----------
 .../selftests/vfio/lib/drivers/ioat/ioat.c    | 16 ++++-----
 .../selftests/vfio/lib/include/vfio_util.h    |  4 +++
 .../selftests/vfio/lib/vfio_pci_device.c      |  1 +
 4 files changed, 30 insertions(+), 25 deletions(-)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +17 -17
index 0ca2cbc2a316..8d667be80229 100644
@@ -70,7 +70,7 @@ static int dsa_probe(struct vfio_pci_device *device)
 		return -EINVAL;
 
 	if (dsa_int_handle_request_required(device)) {
-		printf("Device requires requesting interrupt handles\n");
+		dev_info(device, "Device requires requesting interrupt handles\n");
 		return -EINVAL;
 	}
 
@@ -91,23 +91,23 @@ static void dsa_check_sw_err(struct vfio_pci_device *device)
 			return;
 	}
 
-	fprintf(stderr, "SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
+	dev_err(device, "SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
 		err.bits[0], err.bits[1], err.bits[2], err.bits[3]);
 
-	fprintf(stderr, "  valid: 0x%x\n", err.valid);
-	fprintf(stderr, "  overflow: 0x%x\n", err.overflow);
-	fprintf(stderr, "  desc_valid: 0x%x\n", err.desc_valid);
-	fprintf(stderr, "  wq_idx_valid: 0x%x\n", err.wq_idx_valid);
-	fprintf(stderr, "  batch: 0x%x\n", err.batch);
-	fprintf(stderr, "  fault_rw: 0x%x\n", err.fault_rw);
-	fprintf(stderr, "  priv: 0x%x\n", err.priv);
-	fprintf(stderr, "  error: 0x%x\n", err.error);
-	fprintf(stderr, "  wq_idx: 0x%x\n", err.wq_idx);
-	fprintf(stderr, "  operation: 0x%x\n", err.operation);
-	fprintf(stderr, "  pasid: 0x%x\n", err.pasid);
-	fprintf(stderr, "  batch_idx: 0x%x\n", err.batch_idx);
-	fprintf(stderr, "  invalid_flags: 0x%x\n", err.invalid_flags);
-	fprintf(stderr, "  fault_addr: 0x%lx\n", err.fault_addr);
+	dev_err(device, "  valid: 0x%x\n", err.valid);
+	dev_err(device, "  overflow: 0x%x\n", err.overflow);
+	dev_err(device, "  desc_valid: 0x%x\n", err.desc_valid);
+	dev_err(device, "  wq_idx_valid: 0x%x\n", err.wq_idx_valid);
+	dev_err(device, "  batch: 0x%x\n", err.batch);
+	dev_err(device, "  fault_rw: 0x%x\n", err.fault_rw);
+	dev_err(device, "  priv: 0x%x\n", err.priv);
+	dev_err(device, "  error: 0x%x\n", err.error);
+	dev_err(device, "  wq_idx: 0x%x\n", err.wq_idx);
+	dev_err(device, "  operation: 0x%x\n", err.operation);
+	dev_err(device, "  pasid: 0x%x\n", err.pasid);
+	dev_err(device, "  batch_idx: 0x%x\n", err.batch_idx);
+	dev_err(device, "  invalid_flags: 0x%x\n", err.invalid_flags);
+	dev_err(device, "  fault_addr: 0x%lx\n", err.fault_addr);
 
 	VFIO_FAIL("Software Error Detected!\n");
 }
@@ -256,7 +256,7 @@ static int dsa_completion_wait(struct vfio_pci_device *device,
 	if (status == DSA_COMP_SUCCESS)
 		return 0;
 
-	printf("Error detected during memcpy operation: 0x%x\n", status);
+	dev_info(device, "Error detected during memcpy operation: 0x%x\n", status);
 	return -1;
 }
 
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +8 -8
index c3b91d9b1f59..e04dce1d544c 100644
@@ -51,7 +51,7 @@ static int ioat_probe(struct vfio_pci_device *device)
 		r = 0;
 		break;
 	default:
-		printf("ioat: Unsupported version: 0x%x\n", version);
+		dev_info(device, "ioat: Unsupported version: 0x%x\n", version);
 		r = -EINVAL;
 	}
 	return r;
@@ -135,13 +135,13 @@ static void ioat_handle_error(struct vfio_pci_device *device)
 {
 	void *registers = ioat_channel_registers(device);
 
-	printf("Error detected during memcpy operation!\n"
-	       "  CHANERR: 0x%x\n"
-	       "  CHANERR_INT: 0x%x\n"
-	       "  DMAUNCERRSTS: 0x%x\n",
-	       readl(registers + IOAT_CHANERR_OFFSET),
-	       vfio_pci_config_readl(device, IOAT_PCI_CHANERR_INT_OFFSET),
-	       vfio_pci_config_readl(device, IOAT_PCI_DMAUNCERRSTS_OFFSET));
+	dev_info(device, "Error detected during memcpy operation!\n"
+		 "  CHANERR: 0x%x\n"
+		 "  CHANERR_INT: 0x%x\n"
+		 "  DMAUNCERRSTS: 0x%x\n",
+		 readl(registers + IOAT_CHANERR_OFFSET),
+		 vfio_pci_config_readl(device, IOAT_PCI_CHANERR_INT_OFFSET),
+		 vfio_pci_config_readl(device, IOAT_PCI_DMAUNCERRSTS_OFFSET));
 
 	ioat_reset(device);
 }
tools/testing/selftests/vfio/lib/include/vfio_util.h +4 -0
index 8a01bcaa3ee8..b7175d4c2132 100644
@@ -47,6 +47,9 @@
 	VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__);			\
 } while (0)
 
+#define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+#define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+
 struct iommu_mode {
 	const char *name;
 	const char *container_path;
@@ -169,6 +172,7 @@ struct iommu {
 };
 
 struct vfio_pci_device {
+	const char *bdf;
 	int fd;
 	int group_fd;
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -0
index de3a8d4d74f0..1788e7892ee3 100644
@@ -538,6 +538,7 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, struct iommu *io
 	device = calloc(1, sizeof(*device));
 	VFIO_ASSERT_NOT_NULL(device);
 
+	device->bdf = bdf;
 	device->iommu = iommu;
 
 	if (device->iommu->mode->container_path)
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
#14 lore raw

Re: [PATCH 07/12] vfio: selftests: Prefix logs with device BDF where relevant

Quoted 113 lines · David Matlack

Prefix log messages with the device's BDF where relevant. This will help understanding VFIO selftests logs when tests are run with multiple devices.

---
 .../selftests/vfio/lib/drivers/dsa/dsa.c      | 34 +++++++++----------
 .../selftests/vfio/lib/drivers/ioat/ioat.c    | 16 ++++-----
 .../selftests/vfio/lib/include/vfio_util.h    |  4 +++
 .../selftests/vfio/lib/vfio_pci_device.c      |  1 +
 4 files changed, 30 insertions(+), 25 deletions(-)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +17 -17
index 0ca2cbc2a316..8d667be80229 100644
@@ -70,7 +70,7 @@ static int dsa_probe(struct vfio_pci_device *device)
                return -EINVAL;
 
        if (dsa_int_handle_request_required(device)) {
-               printf("Device requires requesting interrupt handles\n");
+               dev_info(device, "Device requires requesting interrupt handles\n");
                return -EINVAL;
        }
 
@@ -91,23 +91,23 @@ static void dsa_check_sw_err(struct vfio_pci_device *device)
                        return;
        }
 
-       fprintf(stderr, "SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
+       dev_err(device, "SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
                err.bits[0], err.bits[1], err.bits[2], err.bits[3]);
 
-       fprintf(stderr, "  valid: 0x%x\n", err.valid);
-       fprintf(stderr, "  overflow: 0x%x\n", err.overflow);
-       fprintf(stderr, "  desc_valid: 0x%x\n", err.desc_valid);
-       fprintf(stderr, "  wq_idx_valid: 0x%x\n", err.wq_idx_valid);
-       fprintf(stderr, "  batch: 0x%x\n", err.batch);
-       fprintf(stderr, "  fault_rw: 0x%x\n", err.fault_rw);
-       fprintf(stderr, "  priv: 0x%x\n", err.priv);
-       fprintf(stderr, "  error: 0x%x\n", err.error);
-       fprintf(stderr, "  wq_idx: 0x%x\n", err.wq_idx);
-       fprintf(stderr, "  operation: 0x%x\n", err.operation);
-       fprintf(stderr, "  pasid: 0x%x\n", err.pasid);
-       fprintf(stderr, "  batch_idx: 0x%x\n", err.batch_idx);
-       fprintf(stderr, "  invalid_flags: 0x%x\n", err.invalid_flags);
-       fprintf(stderr, "  fault_addr: 0x%lx\n", err.fault_addr);
+       dev_err(device, "  valid: 0x%x\n", err.valid);
+       dev_err(device, "  overflow: 0x%x\n", err.overflow);
+       dev_err(device, "  desc_valid: 0x%x\n", err.desc_valid);
+       dev_err(device, "  wq_idx_valid: 0x%x\n", err.wq_idx_valid);
+       dev_err(device, "  batch: 0x%x\n", err.batch);
+       dev_err(device, "  fault_rw: 0x%x\n", err.fault_rw);
+       dev_err(device, "  priv: 0x%x\n", err.priv);
+       dev_err(device, "  error: 0x%x\n", err.error);
+       dev_err(device, "  wq_idx: 0x%x\n", err.wq_idx);
+       dev_err(device, "  operation: 0x%x\n", err.operation);
+       dev_err(device, "  pasid: 0x%x\n", err.pasid);
+       dev_err(device, "  batch_idx: 0x%x\n", err.batch_idx);
+       dev_err(device, "  invalid_flags: 0x%x\n", err.invalid_flags);
+       dev_err(device, "  fault_addr: 0x%lx\n", err.fault_addr);
 
        VFIO_FAIL("Software Error Detected!\n");
 }
@@ -256,7 +256,7 @@ static int dsa_completion_wait(struct vfio_pci_device *device,
        if (status == DSA_COMP_SUCCESS)
                return 0;
 
-       printf("Error detected during memcpy operation: 0x%x\n", status);
+       dev_info(device, "Error detected during memcpy operation: 0x%x\n", status);
        return -1;
 }
 
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +8 -8
index c3b91d9b1f59..e04dce1d544c 100644
@@ -51,7 +51,7 @@ static int ioat_probe(struct vfio_pci_device *device)
                r = 0;
                break;
        default:
-               printf("ioat: Unsupported version: 0x%x\n", version);
+               dev_info(device, "ioat: Unsupported version: 0x%x\n", version);
                r = -EINVAL;
        }
        return r;
@@ -135,13 +135,13 @@ static void ioat_handle_error(struct vfio_pci_device *device)
 {
        void *registers = ioat_channel_registers(device);
 
-       printf("Error detected during memcpy operation!\n"
-              "  CHANERR: 0x%x\n"
-              "  CHANERR_INT: 0x%x\n"
-              "  DMAUNCERRSTS: 0x%x\n",
-              readl(registers + IOAT_CHANERR_OFFSET),
-              vfio_pci_config_readl(device, IOAT_PCI_CHANERR_INT_OFFSET),
-              vfio_pci_config_readl(device, IOAT_PCI_DMAUNCERRSTS_OFFSET));
+       dev_info(device, "Error detected during memcpy operation!\n"
+                "  CHANERR: 0x%x\n"
+                "  CHANERR_INT: 0x%x\n"
+                "  DMAUNCERRSTS: 0x%x\n",
+                readl(registers + IOAT_CHANERR_OFFSET),
+                vfio_pci_config_readl(device, IOAT_PCI_CHANERR_INT_OFFSET),
+                vfio_pci_config_readl(device, IOAT_PCI_DMAUNCERRSTS_OFFSET));
 
        ioat_reset(device);
 }
tools/testing/selftests/vfio/lib/include/vfio_util.h +0 -0
index 8a01bcaa3ee8..b7175d4c2132 100644

tools/testing/selftests/vfio/lib/include/vfio_util.h line 52

Quoted 6 lines
tools/testing/selftests/vfio/lib/include/vfio_util.h +3 -0
@@ -47,6 +47,9 @@
        VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__);                 \
 } while (0)
 
+#define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+#define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+

nit: For all the dev_info() replacements in this patch, the messages sound like something went bad/wrong. Shouldn't they be dev_err() instead, or were you just aiming for a 1:1 conversion?

Thank you.
Raghavendra

author maintainer
#15 lore raw

Re: [PATCH 07/12] vfio: selftests: Prefix logs with device BDF where relevant

Quoted 7 lines · Raghavendra Rao Ananta

tools/testing/selftests/vfio/lib/include/vfio_util.h line 52

Quoted 3 lines · David Matlack
tools/testing/selftests/vfio/lib/include/vfio_util.h +3 -0
+#define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+#define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
+

nit: For all the dev_info() replacements in this patch, the messages sound like something went bad/wrong. Shouldn't they be dev_err() instead, or were you just aiming for a 1:1 conversion?

Yeah I just wanted to do a 1:1 conversion in this patch:

printf(...) -> dev_info(...)
fprintf(stderr, ...) -> dev_err(...)

I can upgrade them in a separate patch in v2.

author maintainer
#16 lore raw

PATCH 8/12 vfio: selftests: Rename struct vfio_dma_region to dma_region

In reply to David Matlack

Rename struct vfio_dma_region to dma_region. This is in preparation for separating the VFIO PCI device library code from the IOMMU library code. This name change also better reflects the fact that DMA mappings can be managed by either VFIO or IOMMUFD. i.e. the "vfio_" prefix is misleading.

---
 .../testing/selftests/vfio/lib/include/vfio_util.h |  8 ++++----
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 14 +++++++-------
 .../testing/selftests/vfio/vfio_dma_mapping_test.c |  2 +-
 .../testing/selftests/vfio/vfio_pci_driver_test.c  |  6 +++---
 4 files changed, 15 insertions(+), 15 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +4 -4
index b7175d4c2132..cce521212348 100644
@@ -77,7 +77,7 @@ typedef u64 iova_t;
 
 #define INVALID_IOVA UINT64_MAX
 
-struct vfio_dma_region {
+struct dma_region {
 	struct list_head link;
 	void *vaddr;
 	iova_t iova;
@@ -151,7 +151,7 @@ struct vfio_pci_driver {
 	bool memcpy_in_progress;
 
 	/* Region to be used by the driver (e.g. for in-memory descriptors) */
-	struct vfio_dma_region region;
+	struct dma_region region;
 
 	/* The maximum size that can be passed to memcpy_start(). */
 	u64 max_memcpy_size;
@@ -222,9 +222,9 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device);
 void vfio_pci_device_reset(struct vfio_pci_device *device);
 
 void vfio_pci_dma_map(struct vfio_pci_device *device,
-		      struct vfio_dma_region *region);
+		      struct dma_region *region);
 void vfio_pci_dma_unmap(struct vfio_pci_device *device,
-			struct vfio_dma_region *region);
+			struct dma_region *region);
 
 void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
 			    size_t config, size_t size, void *data);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +7 -7
index 1788e7892ee3..c9cfba1dc62c 100644
@@ -30,7 +30,7 @@
 
 iova_t __to_iova(struct vfio_pci_device *device, void *vaddr)
 {
-	struct vfio_dma_region *region;
+	struct dma_region *region;
 
 	list_for_each_entry(region, &device->iommu->dma_regions, link) {
 		if (vaddr < region->vaddr)
@@ -142,7 +142,7 @@ static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index,
 }
 
 static void vfio_iommu_dma_map(struct vfio_pci_device *device,
-			       struct vfio_dma_region *region)
+			       struct dma_region *region)
 {
 	struct vfio_iommu_type1_dma_map args = {
 		.argsz = sizeof(args),
@@ -156,7 +156,7 @@ static void vfio_iommu_dma_map(struct vfio_pci_device *device,
 }
 
 static void iommufd_dma_map(struct vfio_pci_device *device,
-			    struct vfio_dma_region *region)
+			    struct dma_region *region)
 {
 	struct iommu_ioas_map args = {
 		.size = sizeof(args),
@@ -173,7 +173,7 @@ static void iommufd_dma_map(struct vfio_pci_device *device,
 }
 
 void vfio_pci_dma_map(struct vfio_pci_device *device,
-		      struct vfio_dma_region *region)
+		      struct dma_region *region)
 {
 	if (device->iommu->iommufd)
 		iommufd_dma_map(device, region);
@@ -184,7 +184,7 @@ void vfio_pci_dma_map(struct vfio_pci_device *device,
 }
 
 static void vfio_iommu_dma_unmap(struct vfio_pci_device *device,
-				 struct vfio_dma_region *region)
+				 struct dma_region *region)
 {
 	struct vfio_iommu_type1_dma_unmap args = {
 		.argsz = sizeof(args),
@@ -196,7 +196,7 @@ static void vfio_iommu_dma_unmap(struct vfio_pci_device *device,
 }
 
 static void iommufd_dma_unmap(struct vfio_pci_device *device,
-			      struct vfio_dma_region *region)
+			      struct dma_region *region)
 {
 	struct iommu_ioas_unmap args = {
 		.size = sizeof(args),
@@ -209,7 +209,7 @@ static void iommufd_dma_unmap(struct vfio_pci_device *device,
 }
 
 void vfio_pci_dma_unmap(struct vfio_pci_device *device,
-			struct vfio_dma_region *region)
+			struct dma_region *region)
 {
 	if (device->iommu->iommufd)
 		iommufd_dma_unmap(device, region);
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
index ab19c54a774d..680232777839 100644
@@ -126,7 +126,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
 {
 	const u64 size = variant->size ?: getpagesize();
 	const int flags = variant->mmap_flags;
-	struct vfio_dma_region region;
+	struct dma_region region;
 	struct iommu_mapping mapping;
 	u64 mapping_size = size;
 	int rc;
tools/testing/selftests/vfio/vfio_pci_driver_test.c +3 -3
index 2dbd70b7db62..79128a0c278d 100644
@@ -19,7 +19,7 @@ static const char *device_bdf;
 } while (0)
 
 static void region_setup(struct vfio_pci_device *device,
-			 struct vfio_dma_region *region, u64 size)
+			 struct dma_region *region, u64 size)
 {
 	const int flags = MAP_SHARED | MAP_ANONYMOUS;
 	const int prot = PROT_READ | PROT_WRITE;
@@ -36,7 +36,7 @@ static void region_setup(struct vfio_pci_device *device,
 }
 
 static void region_teardown(struct vfio_pci_device *device,
-			    struct vfio_dma_region *region)
+			    struct dma_region *region)
 {
 	vfio_pci_dma_unmap(device, region);
 	VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0);
@@ -44,7 +44,7 @@ static void region_teardown(struct vfio_pci_device *device,
 
 FIXTURE(vfio_pci_driver_test) {
 	struct vfio_pci_device *device;
-	struct vfio_dma_region memcpy_region;
+	struct dma_region memcpy_region;
 	void *vaddr;
 	int msi_fd;
 
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#17 lore raw

PATCH 9/12 vfio: selftests: Move iommu_*() functions into iommu.c

In reply to David Matlack

Move all the iommu_*() helper functions into their own file iommu.c. This provides a better separation between the vfio_pci_device helper code and the iommu code.

No function change intended.

---
 .../selftests/vfio/lib/include/vfio_util.h    |  37 ++-
 tools/testing/selftests/vfio/lib/iommu.c      | 219 ++++++++++++++++++
 tools/testing/selftests/vfio/lib/libvfio.mk   |   3 +-
 .../selftests/vfio/lib/vfio_pci_device.c      | 212 -----------------
 4 files changed, 252 insertions(+), 219 deletions(-)
 create mode 100644 tools/testing/selftests/vfio/lib/iommu.c
tools/testing/selftests/vfio/lib/include/vfio_util.h +31 -6
index cce521212348..c7932096ac2e 100644
@@ -7,6 +7,7 @@
 #include <linux/vfio.h>
 #include <linux/list.h>
 #include <linux/pci_regs.h>
+#include <sys/ioctl.h>
 
 #include "../../../kselftest.h"
 
@@ -47,6 +48,12 @@
 	VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__);			\
 } while (0)
 
+#define ioctl_assert(_fd, _op, _arg) do {						       \
+	void *__arg = (_arg);								       \
+	int __ret = ioctl((_fd), (_op), (__arg));					       \
+	VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
+} while (0)
+
 #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
 #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
 
@@ -212,6 +219,10 @@ extern const char *default_iommu_mode;
 
 struct iommu *iommu_init(const char *iommu_mode);
 void iommu_cleanup(struct iommu *iommu);
+iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr);
+iova_t __iommu_hva2iova(struct iommu *iommu, void *vaddr);
+void iommu_map(struct iommu *iommu, struct dma_region *region);
+void iommu_unmap(struct iommu *iommu, struct dma_region *region);
 
 struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, struct iommu *iommu);
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
@@ -221,10 +232,17 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device);
 
 void vfio_pci_device_reset(struct vfio_pci_device *device);
 
-void vfio_pci_dma_map(struct vfio_pci_device *device,
-		      struct dma_region *region);
-void vfio_pci_dma_unmap(struct vfio_pci_device *device,
-			struct dma_region *region);
+static inline void vfio_pci_dma_map(struct vfio_pci_device *device,
+				    struct dma_region *region)
+{
+	return iommu_map(device->iommu, region);
+}
+
+static inline void vfio_pci_dma_unmap(struct vfio_pci_device *device,
+				      struct dma_region *region)
+{
+	return iommu_unmap(device->iommu, region);
+}
 
 void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
 			    size_t config, size_t size, void *data);
@@ -286,8 +304,15 @@ static inline void vfio_pci_msix_disable(struct vfio_pci_device *device)
 	vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX);
 }
 
-iova_t __to_iova(struct vfio_pci_device *device, void *vaddr);
-iova_t to_iova(struct vfio_pci_device *device, void *vaddr);
+static inline iova_t __to_iova(struct vfio_pci_device *device, void *vaddr)
+{
+	return __iommu_hva2iova(device->iommu, vaddr);
+}
+
+static inline iova_t to_iova(struct vfio_pci_device *device, void *vaddr)
+{
+	return iommu_hva2iova(device->iommu, vaddr);
+}
 
 static inline bool vfio_pci_device_match(struct vfio_pci_device *device,
 					 u16 vendor_id, u16 device_id)
tools/testing/selftests/vfio/lib/iommu.c new file +219 -0

219 lines not on this page · raw message

tools/testing/selftests/vfio/lib/libvfio.mk +2 -1
index 5d11c3a89a28..1d53311e2610 100644
@@ -3,7 +3,8 @@ ARCH ?= $(SUBARCH)
 
 VFIO_DIR := $(selfdir)/vfio
 
-LIBVFIO_C := lib/vfio_pci_device.c
+LIBVFIO_C := lib/iommu.c
+LIBVFIO_C += lib/vfio_pci_device.c
 LIBVFIO_C += lib/vfio_pci_driver.c
 
 ifeq ($(ARCH:x86_64=x86),x86)
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0 -212

248 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#18 lore raw

PATCH 10/12 vfio: selftests: Rename vfio_util.h to libvfio.h

In reply to David Matlack

Rename vfio_util.h to libvfio.h to match the name of libvfio.mk.

No functional change intended.

---
 tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c          | 2 +-
 tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c        | 2 +-
 .../selftests/vfio/lib/include/{vfio_util.h => libvfio.h}   | 6 +++---
 tools/testing/selftests/vfio/lib/iommu.c                    | 2 +-
 tools/testing/selftests/vfio/lib/vfio_pci_device.c          | 2 +-
 tools/testing/selftests/vfio/lib/vfio_pci_driver.c          | 2 +-
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c        | 2 +-
 tools/testing/selftests/vfio/vfio_iommufd_setup_test.c      | 2 +-
 tools/testing/selftests/vfio/vfio_pci_device_test.c         | 2 +-
 tools/testing/selftests/vfio/vfio_pci_driver_test.c         | 2 +-
 10 files changed, 12 insertions(+), 12 deletions(-)
 rename tools/testing/selftests/vfio/lib/include/{vfio_util.h => libvfio.h} (98%)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c +1 -1
index 8d667be80229..a75d8ea46562 100644
@@ -9,7 +9,7 @@
 #include <linux/pci_ids.h>
 #include <linux/sizes.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #include "registers.h"
 
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c +1 -1
index e04dce1d544c..ce147839b31f 100644
@@ -7,7 +7,7 @@
 #include <linux/pci_ids.h>
 #include <linux/sizes.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #include "hw.h"
 #include "registers.h"
tools/testing/selftests/vfio/lib/include/libvfio.h renamed from tools/testing/selftests/vfio/lib/include/vfio_util.h +3 -3
similarity index 98%
rename from tools/testing/selftests/vfio/lib/include/vfio_util.h
rename to tools/testing/selftests/vfio/lib/include/libvfio.h
index c7932096ac2e..8b72d9c62404 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
-#ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H
-#define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H
+#ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H
+#define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H
 
 #include <fcntl.h>
 #include <string.h>
@@ -332,4 +332,4 @@ void vfio_pci_driver_memcpy_start(struct vfio_pci_device *device,
 int vfio_pci_driver_memcpy_wait(struct vfio_pci_device *device);
 void vfio_pci_driver_send_msi(struct vfio_pci_device *device);
 
-#endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */
+#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/iommu.c +1 -1
index a835b0d29abf..682b4c9da625 100644
@@ -18,7 +18,7 @@
 #include <linux/iommufd.h>
 
 #include "../../../kselftest.h"
-#include <vfio_util.h>
+#include <libvfio.h>
 
 const char *default_iommu_mode = "iommufd";
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -1
index b026b908dcd2..909c951eb52f 100644
@@ -18,7 +18,7 @@
 #include <linux/iommufd.h>
 
 #include "../../../kselftest.h"
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #define PCI_SYSFS_PATH	"/sys/bus/pci/devices"
 
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +1 -1
index abb7a62a03ea..ca0e25efbfa1 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include "../../../kselftest.h"
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #ifdef __x86_64__
 extern struct vfio_pci_driver_ops dsa_ops;
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
index 680232777839..5092c55a76e3 100644
@@ -8,7 +8,7 @@
 #include <linux/sizes.h>
 #include <linux/vfio.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #include "../kselftest_harness.h"
 
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c +1 -1
index 3655106b912d..caf1c6291f3d 100644
@@ -10,7 +10,7 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 #include "../kselftest_harness.h"
 
 static const char iommu_dev_path[] = "/dev/iommu";
tools/testing/selftests/vfio/vfio_pci_device_test.c +1 -1
index 7a270698e4d2..a44ab8041cb6 100644
@@ -10,7 +10,7 @@
 #include <linux/sizes.h>
 #include <linux/vfio.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #include "../kselftest_harness.h"
 
tools/testing/selftests/vfio/vfio_pci_driver_test.c +1 -1
index 79128a0c278d..fe107ba129d1 100644
@@ -5,7 +5,7 @@
 #include <linux/sizes.h>
 #include <linux/vfio.h>
 
-#include <vfio_util.h>
+#include <libvfio.h>
 
 #include "../kselftest_harness.h"
 
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#19 lore raw

PATCH 11/12 vfio: selftests: Split libvfio.h into separate header files

In reply to David Matlack

Split out the contents of libvfio.h into separate header files, but keep libvfio.h as the top-level include that all tests can use.

Put all new header files into a libvfio/ subdirectory to avoid future name conflicts in include paths when libvfio is used by other selftests like KVM.

No functional change intended.

---
 .../selftests/vfio/lib/include/libvfio.h      | 318 +-----------------
 .../vfio/lib/include/libvfio/assert.h         |  53 +++
 .../vfio/lib/include/libvfio/iommu.h          |  53 +++
 .../lib/include/libvfio/vfio_pci_device.h     | 143 ++++++++
 .../lib/include/libvfio/vfio_pci_driver.h     |  98 ++++++
 tools/testing/selftests/vfio/lib/libvfio.c    |  77 +++++
 tools/testing/selftests/vfio/lib/libvfio.mk   |   3 +-
 .../selftests/vfio/lib/vfio_pci_device.c      |  70 ----
 8 files changed, 430 insertions(+), 385 deletions(-)
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/assert.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h
 create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h
 create mode 100644 tools/testing/selftests/vfio/lib/libvfio.c
tools/testing/selftests/vfio/lib/include/libvfio.h +4 -314
index 8b72d9c62404..ddb0b3bf60e7 100644
@@ -2,201 +2,10 @@
 #ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H
 #define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H
 
-#include <fcntl.h>
-#include <string.h>
-#include <linux/vfio.h>
-#include <linux/list.h>
-#include <linux/pci_regs.h>
-#include <sys/ioctl.h>
-
-#include "../../../kselftest.h"
-
-#define VFIO_LOG_AND_EXIT(...) do {		\
-	fprintf(stderr, "  " __VA_ARGS__);	\
-	fprintf(stderr, "\n");			\
-	exit(KSFT_FAIL);			\
-} while (0)
-
-#define VFIO_ASSERT_OP(_lhs, _rhs, _op, ...) do {				\
-	typeof(_lhs) __lhs = (_lhs);						\
-	typeof(_rhs) __rhs = (_rhs);						\
-										\
-	if (__lhs _op __rhs)							\
-		break;								\
-										\
-	fprintf(stderr, "%s:%u: Assertion Failure\n\n", __FILE__, __LINE__);	\
-	fprintf(stderr, "  Expression: " #_lhs " " #_op " " #_rhs "\n");	\
-	fprintf(stderr, "  Observed: %#lx %s %#lx\n",				\
-			(u64)__lhs, #_op, (u64)__rhs);				\
-	fprintf(stderr, "  [errno: %d - %s]\n", errno, strerror(errno));	\
-	VFIO_LOG_AND_EXIT(__VA_ARGS__);						\
-} while (0)
-
-#define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
-#define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
-#define VFIO_ASSERT_LT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <, ##__VA_ARGS__)
-#define VFIO_ASSERT_LE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, <=, ##__VA_ARGS__)
-#define VFIO_ASSERT_GT(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >, ##__VA_ARGS__)
-#define VFIO_ASSERT_GE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, >=, ##__VA_ARGS__)
-#define VFIO_ASSERT_TRUE(_a, ...) VFIO_ASSERT_NE(false, (_a), ##__VA_ARGS__)
-#define VFIO_ASSERT_FALSE(_a, ...) VFIO_ASSERT_EQ(false, (_a), ##__VA_ARGS__)
-#define VFIO_ASSERT_NULL(_a, ...) VFIO_ASSERT_EQ(NULL, _a, ##__VA_ARGS__)
-#define VFIO_ASSERT_NOT_NULL(_a, ...) VFIO_ASSERT_NE(NULL, _a, ##__VA_ARGS__)
-
-#define VFIO_FAIL(_fmt, ...) do {				\
-	fprintf(stderr, "%s:%u: FAIL\n\n", __FILE__, __LINE__);	\
-	VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__);			\
-} while (0)
-
-#define ioctl_assert(_fd, _op, _arg) do {						       \
-	void *__arg = (_arg);								       \
-	int __ret = ioctl((_fd), (_op), (__arg));					       \
-	VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
-} while (0)
-
-#define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
-#define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
-
-struct iommu_mode {
-	const char *name;
-	const char *container_path;
-	unsigned long iommu_type;
-};
-
-/*
- * Generator for VFIO selftests fixture variants that replicate across all
- * possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE()
- * which should then use FIXTURE_VARIANT_ADD() to create the variant.
- */
-#define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(...) \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__); \
-FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd, ##__VA_ARGS__)
-
-struct vfio_pci_bar {
-	struct vfio_region_info info;
-	void *vaddr;
-};
-
-typedef u64 iova_t;
-
-#define INVALID_IOVA UINT64_MAX
-
-struct dma_region {
-	struct list_head link;
-	void *vaddr;
-	iova_t iova;
-	u64 size;
-};
-
-struct vfio_pci_device;
-
-struct vfio_pci_driver_ops {
-	const char *name;
-
-	/**
-	 * @probe() - Check if the driver supports the given device.
-	 *
-	 * Return: 0 on success, non-0 on failure.
-	 */
-	int (*probe)(struct vfio_pci_device *device);
-
-	/**
-	 * @init() - Initialize the driver for @device.
-	 *
-	 * Must be called after device->driver.region has been initialized.
-	 */
-	void (*init)(struct vfio_pci_device *device);
-
-	/**
-	 * remove() - Deinitialize the driver for @device.
-	 */
-	void (*remove)(struct vfio_pci_device *device);
-
-	/**
-	 * memcpy_start() - Kick off @count repeated memcpy operations from
-	 * [@src, @src + @size) to [@dst, @dst + @size).
-	 *
-	 * Guarantees:
-	 *  - The device will attempt DMA reads on [src, src + size).
-	 *  - The device will attempt DMA writes on [dst, dst + size).
-	 *  - The device will not generate any interrupts.
-	 *
-	 * memcpy_start() returns immediately, it does not wait for the
-	 * copies to complete.
-	 */
-	void (*memcpy_start)(struct vfio_pci_device *device,
-			     iova_t src, iova_t dst, u64 size, u64 count);
-
-	/**
-	 * memcpy_wait() - Wait until the memcpy operations started by
-	 * memcpy_start() have finished.
-	 *
-	 * Guarantees:
-	 *  - All in-flight DMAs initiated by memcpy_start() are fully complete
-	 *    before memcpy_wait() returns.
-	 *
-	 * Returns non-0 if the driver detects that an error occurred during the
-	 * memcpy, 0 otherwise.
-	 */
-	int (*memcpy_wait)(struct vfio_pci_device *device);
-
-	/**
-	 * send_msi() - Make the device send the MSI device->driver.msi.
-	 *
-	 * Guarantees:
-	 *  - The device will send the MSI once.
-	 */
-	void (*send_msi)(struct vfio_pci_device *device);
-};
-
-struct vfio_pci_driver {
-	const struct vfio_pci_driver_ops *ops;
-	bool initialized;
-	bool memcpy_in_progress;
-
-	/* Region to be used by the driver (e.g. for in-memory descriptors) */
-	struct dma_region region;
-
-	/* The maximum size that can be passed to memcpy_start(). */
-	u64 max_memcpy_size;
-
-	/* The maximum count that can be passed to memcpy_start(). */
-	u64 max_memcpy_count;
-
-	/* The MSI vector the device will signal in ops->send_msi(). */
-	int msi;
-};
-
-struct iommu {
-	const struct iommu_mode *mode;
-	int container_fd;
-	int iommufd;
-	u32 ioas_id;
-	struct list_head dma_regions;
-};
-
-struct vfio_pci_device {
-	const char *bdf;
-	int fd;
-	int group_fd;
-
-	struct iommu *iommu;
-
-	struct vfio_device_info info;
-	struct vfio_region_info config_space;
-	struct vfio_pci_bar bars[PCI_STD_NUM_BARS];
-
-	struct vfio_irq_info msi_info;
-	struct vfio_irq_info msix_info;
-
-	/* eventfds for MSI and MSI-x interrupts */
-	int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1];
-
-	struct vfio_pci_driver driver;
-};
+#include <libvfio/assert.h>
+#include <libvfio/iommu.h>
+#include <libvfio/vfio_pci_driver.h>
+#include <libvfio/vfio_pci_device.h>
 
 /*
  * Return the BDF string of the device that the test should use.
@@ -213,123 +22,4 @@ struct vfio_pci_device {
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
 char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
 
-const char *vfio_pci_get_cdev_path(const char *bdf);
-
-extern const char *default_iommu_mode;
-
-struct iommu *iommu_init(const char *iommu_mode);
-void iommu_cleanup(struct iommu *iommu);
-iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr);
-iova_t __iommu_hva2iova(struct iommu *iommu, void *vaddr);
-void iommu_map(struct iommu *iommu, struct dma_region *region);
-void iommu_unmap(struct iommu *iommu, struct dma_region *region);
-
-struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, struct iommu *iommu);
-struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode);
-
-void __vfio_pci_device_cleanup(struct vfio_pci_device *device);
-void vfio_pci_device_cleanup(struct vfio_pci_device *device);
-
-void vfio_pci_device_reset(struct vfio_pci_device *device);
-
-static inline void vfio_pci_dma_map(struct vfio_pci_device *device,
-				    struct dma_region *region)
-{
-	return iommu_map(device->iommu, region);
-}
-
-static inline void vfio_pci_dma_unmap(struct vfio_pci_device *device,
-				      struct dma_region *region)
-{
-	return iommu_unmap(device->iommu, region);
-}
-
-void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
-			    size_t config, size_t size, void *data);
-
-#define vfio_pci_config_read(_device, _offset, _type) ({			    \
-	_type __data;								    \
-	vfio_pci_config_access((_device), false, _offset, sizeof(__data), &__data); \
-	__data;									    \
-})
-
-#define vfio_pci_config_readb(_d, _o) vfio_pci_config_read(_d, _o, u8)
-#define vfio_pci_config_readw(_d, _o) vfio_pci_config_read(_d, _o, u16)
-#define vfio_pci_config_readl(_d, _o) vfio_pci_config_read(_d, _o, u32)
-
-#define vfio_pci_config_write(_device, _offset, _value, _type) do {		  \
-	_type __data = (_value);						  \
-	vfio_pci_config_access((_device), true, _offset, sizeof(_type), &__data); \
-} while (0)
-
-#define vfio_pci_config_writeb(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u8)
-#define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)
-#define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32)
-
-void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,
-			 u32 vector, int count);
-void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index);
-void vfio_pci_irq_trigger(struct vfio_pci_device *device, u32 index, u32 vector);
-
-static inline void fcntl_set_nonblock(int fd)
-{
-	int r;
-
-	r = fcntl(fd, F_GETFL, 0);
-	VFIO_ASSERT_NE(r, -1, "F_GETFL failed for fd %d\n", fd);
-
-	r = fcntl(fd, F_SETFL, r | O_NONBLOCK);
-	VFIO_ASSERT_NE(r, -1, "F_SETFL O_NONBLOCK failed for fd %d\n", fd);
-}
-
-static inline void vfio_pci_msi_enable(struct vfio_pci_device *device,
-				       u32 vector, int count)
-{
-	vfio_pci_irq_enable(device, VFIO_PCI_MSI_IRQ_INDEX, vector, count);
-}
-
-static inline void vfio_pci_msi_disable(struct vfio_pci_device *device)
-{
-	vfio_pci_irq_disable(device, VFIO_PCI_MSI_IRQ_INDEX);
-}
-
-static inline void vfio_pci_msix_enable(struct vfio_pci_device *device,
-					u32 vector, int count)
-{
-	vfio_pci_irq_enable(device, VFIO_PCI_MSIX_IRQ_INDEX, vector, count);
-}
-
-static inline void vfio_pci_msix_disable(struct vfio_pci_device *device)
-{
-	vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX);
-}
-
-static inline iova_t __to_iova(struct vfio_pci_device *device, void *vaddr)
-{
-	return __iommu_hva2iova(device->iommu, vaddr);
-}
-
-static inline iova_t to_iova(struct vfio_pci_device *device, void *vaddr)
-{
-	return iommu_hva2iova(device->iommu, vaddr);
-}
-
-static inline bool vfio_pci_device_match(struct vfio_pci_device *device,
-					 u16 vendor_id, u16 device_id)
-{
-	return (vendor_id == vfio_pci_config_readw(device, PCI_VENDOR_ID)) &&
-		(device_id == vfio_pci_config_readw(device, PCI_DEVICE_ID));
-}
-
-void vfio_pci_driver_probe(struct vfio_pci_device *device);
-void vfio_pci_driver_init(struct vfio_pci_device *device);
-void vfio_pci_driver_remove(struct vfio_pci_device *device);
-int vfio_pci_driver_memcpy(struct vfio_pci_device *device,
-			   iova_t src, iova_t dst, u64 size);
-void vfio_pci_driver_memcpy_start(struct vfio_pci_device *device,
-				  iova_t src, iova_t dst, u64 size,
-				  u64 count);
-int vfio_pci_driver_memcpy_wait(struct vfio_pci_device *device);
-void vfio_pci_driver_send_msi(struct vfio_pci_device *device);
-
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/include/libvfio/assert.h new file +53 -0

53 lines not on this page · raw message

tools/testing/selftests/vfio/lib/include/libvfio/iommu.h new file +53 -0

53 lines not on this page · raw message

tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h new file +143 -0

143 lines not on this page · raw message

tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_driver.h new file +98 -0

98 lines not on this page · raw message

tools/testing/selftests/vfio/lib/libvfio.c new file +77 -0

77 lines not on this page · raw message

tools/testing/selftests/vfio/lib/libvfio.mk +2 -1
index 1d53311e2610..6bcd89f7f8ee 100644
@@ -3,7 +3,8 @@ ARCH ?= $(SUBARCH)
 
 VFIO_DIR := $(selfdir)/vfio
 
-LIBVFIO_C := lib/iommu.c
+LIBVFIO_C := lib/libvfio.c
+LIBVFIO_C += lib/iommu.c
 LIBVFIO_C += lib/vfio_pci_device.c
 LIBVFIO_C += lib/vfio_pci_driver.c
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0 -70

73 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#20 lore raw

PATCH 12/12 vfio: selftests: Add vfio_pci_device_init_perf_test

In reply to David Matlack

Add a new VFIO selftest for measuring the time it takes to run vfio_pci_device_init() in parallel for one or more devices.

This test serves as manual regression test for the performance improvement of commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set"). For example, when running this test with 64 VFs under the same PF:

Before:

  $ ./vfio_pci_device_init_perf_test -r vfio_pci_device_init_perf_test.iommufd.init 0000:1a:00.0 0000:1a:00.1 ...
  ...
  Wall time: 6.653234463s
  Min init time (per device): 0.101215344s
  Max init time (per device): 6.652755941s
  Avg init time (per device): 3.377609608s

After:

  $ ./vfio_pci_device_init_perf_test -r vfio_pci_device_init_perf_test.iommufd.init 0000:1a:00.0 0000:1a:00.1 ...
  ...
  Wall time: 0.122978332s
  Min init time (per device): 0.108121915s
  Max init time (per device): 0.122762761s
  Avg init time (per device): 0.113816748s

This test does not make any assertions about performance, since any such assertion is likely to be flaky due to system differences and random noise. However this test can be fed into automation to detect regressions, and can be used by developers in the future to measure performance optimizations.

---
 tools/testing/selftests/vfio/Makefile         |   3 +
 .../vfio/vfio_pci_device_init_perf_test.c     | 163 ++++++++++++++++++
 2 files changed, 166 insertions(+)
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c
tools/testing/selftests/vfio/Makefile +3 -0
index 324ba0175a33..7b69375ee6ea 100644
@@ -2,6 +2,7 @@ CFLAGS = $(KHDR_INCLUDES)
 TEST_GEN_PROGS += vfio_dma_mapping_test
 TEST_GEN_PROGS += vfio_iommufd_setup_test
 TEST_GEN_PROGS += vfio_pci_device_test
+TEST_GEN_PROGS += vfio_pci_device_init_perf_test
 TEST_GEN_PROGS += vfio_pci_driver_test
 TEST_PROGS_EXTENDED := run.sh
 include ../lib.mk
@@ -11,6 +12,8 @@ CFLAGS += -I$(top_srcdir)/tools/include
 CFLAGS += -MD
 CFLAGS += $(EXTRA_CFLAGS)
 
+LDFLAGS += -pthread
+
 $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O)
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@
 
tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c new file +163 -0

163 lines not on this page · raw message

Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author maintainer
#21 lore raw

Re: [PATCH 12/12] vfio: selftests: Add vfio_pci_device_init_perf_test

Quoted 10 lines · David Matlack

This test serves as manual regression test for the performance improvement of commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").

This test does not make any assertions about performance, since any such assertion is likely to be flaky due to system differences and random noise. However this test can be fed into automation to detect regressions, and can be used by developers in the future to measure performance optimizations.

Perhaps this test can compare the time it takes to initialize a single device against the time it takes to initialize all devices provided to the test, and assert that they are within some range of each other. In other words, assert that initializing N devices does not scale with the number of devices. To avoid false negatives, the test can collect each measurement N times and discard the outliers (e.g. only look at the median or min time).

maintainer
#22 lore raw
In reply to David Matlack
Quoted 10 lines · On Wed, 8 Oct 2025 23:25:19 +0000 David Matlack <[email protected]> wrote:

This series adds support for tests that use multiple devices, and adds one new test, vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").

This series also breaks apart the monolithic vfio_util.h and vfio_pci_device.c into separate files, to account for all the new code. This required some code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.

Hi David,

This series doesn't apply to mainline currently and I see you have some self-comments that suggests this is still a WIP, so I'll drop it and look for a v2. I believe
https://lore.kernel.org/kvm/[email protected]/
is still in play though and does apply. Thanks,

Alex

author maintainer
#23 lore raw
Quoted 15 lines · Alex Williamson
Quoted 10 lines · On Wed, 8 Oct 2025 23:25:19 +0000 David Matlack <[email protected]> wrote:

This series adds support for tests that use multiple devices, and adds one new test, vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").

This series also breaks apart the monolithic vfio_util.h and vfio_pci_device.c into separate files, to account for all the new code. This required some code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.

Quoted 5 lines

Hi David,

This series doesn't apply to mainline currently and I see you have some self-comments that suggests this is still a WIP, so I'll drop it and look for a v2.

Yes, I am going to send a v2 to address my self-comments. I'm also looking at getting reviews of this series from some fellow Googlers on the mailing list. Feel free to hold off for now.

Quoted 3 lines

I believe
https://lore.kernel.org/kvm/[email protected]/
is still in play though and does apply. Thanks,

This series will need another revision as well to address Sean's feedback, and I think can be merged via KVM maintainers.

That being said,
https://lore.kernel.org/kvm/[email protected]/
should apply and be ready to merge. It is a small prep patch for the KVM selftests integration.

maintainer
#24 lore raw
Quoted 30 lines · On Wed, 5 Nov 2025 13:03:51 -0800 David Matlack <[email protected]> wrote:
Quoted 15 lines · Alex Williamson
Quoted 10 lines · On Wed, 8 Oct 2025 23:25:19 +0000 David Matlack <[email protected]> wrote:

This series adds support for tests that use multiple devices, and adds one new test, vfio_pci_device_init_perf_test, which measures parallel device initialization time to demonstrate the improvement from commit e908f58b6beb ("vfio/pci: Separate SR-IOV VF dev_set").

This series also breaks apart the monolithic vfio_util.h and vfio_pci_device.c into separate files, to account for all the new code. This required some code motion so the diffstat looks large. The final layout is more granular and provides a better separation of the IOMMU code from the device code.

Quoted 5 lines

Hi David,

This series doesn't apply to mainline currently and I see you have some self-comments that suggests this is still a WIP, so I'll drop it and look for a v2.

Yes, I am going to send a v2 to address my self-comments. I'm also looking at getting reviews of this series from some fellow Googlers on the mailing list. Feel free to hold off for now.

Quoted 3 lines

I believe
https://lore.kernel.org/kvm/[email protected]/
is still in play though and does apply. Thanks,

Quoted 7 lines

This series will need another revision as well to address Sean's feedback, and I think can be merged via KVM maintainers.

That being said,
https://lore.kernel.org/kvm/[email protected]/
should apply and be ready to merge. It is a small prep patch for the KVM selftests integration.

Oops, yes, that's the one I meant to reference. It links to the prior one and I grabbed the wrong link from my local branch. Thanks for the confirmation.

Alex