kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: David Matlack <dmatlack@google.com>
Cc: Shuah Khan <shuah@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Vinod Koul <vkoul@kernel.org>, Fenghua Yu <fenghua.yu@intel.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Jiri Olsa <jolsa@kernel.org>, Andrii Nakryiko <andrii@kernel.org>,
	Wei Yang <richard.weiyang@gmail.com>,
	Bjorn Helgaas <bhelgaas@google.com>, Takashi Iwai <tiwai@suse.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	FUJITA Tomonori <fujita.tomonori@gmail.com>,
	WangYuli <wangyuli@uniontech.com>,
	Sean Christopherson <seanjc@google.com>,
	Andrew Jones <ajones@ventanamicro.com>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	Eric Auger <eric.auger@redhat.com>,
	Josh Hilke <jrhilke@google.com>,
	linux-kselftest@vger.kernel.org, kvm@vger.kernel.org,
	Jason Gunthorpe <jgg@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Vipin Sharma <vipinsh@google.com>,
	Pasha Tatashin <pasha.tatashin@soleen.com>,
	Saeed Mahameed <saeedm@nvidia.com>,
	Adithya Jayachandran <ajayachandra@nvidia.com>,
	Parav Pandit <parav@nvidia.com>,
	Leon Romanovsky <leonro@nvidia.com>,
	Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	Dan Williams <dan.j.williams@intel.com>
Subject: Re: [RFC PATCH 30/33] vfio: selftests: Add a script to help with running VFIO selftests
Date: Mon, 2 Jun 2025 17:07:59 -0600	[thread overview]
Message-ID: <20250602170759.6031b4f4.alex.williamson@redhat.com> (raw)
In-Reply-To: <20250523233018.1702151-31-dmatlack@google.com>

On Fri, 23 May 2025 23:30:15 +0000
David Matlack <dmatlack@google.com> wrote:

> 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
>   ... bind to vfio-pci and launch $SHELL ...
>   $ echo $BDF
>   0000:6a:01.0
>   $ ./vfio_pci_device_test $BDF
>   ...
>   $ exit
>   ... unbind from vfio-pci ...
>   $
> 
> 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.
> 
> Signed-off-by: David Matlack <dmatlack@google.com>
> ---
>  tools/testing/selftests/vfio/Makefile |   1 +
>  tools/testing/selftests/vfio/run.sh   | 110 ++++++++++++++++++++++++++
>  2 files changed, 111 insertions(+)
>  create mode 100755 tools/testing/selftests/vfio/run.sh
> 
> diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftests/vfio/Makefile
> index 21fb1809035e..2ab86bd930b0 100644
> --- a/tools/testing/selftests/vfio/Makefile
> +++ b/tools/testing/selftests/vfio/Makefile
> @@ -3,6 +3,7 @@ TEST_GEN_PROGS_EXTENDED += vfio_dma_mapping_test
>  TEST_GEN_PROGS_EXTENDED += vfio_iommufd_setup_test
>  TEST_GEN_PROGS_EXTENDED += vfio_pci_device_test
>  TEST_GEN_PROGS_EXTENDED += vfio_pci_driver_test
> +TEST_PROGS_EXTENDED := run.sh
>  include ../lib.mk
>  include lib/libvfio.mk
>  
> diff --git a/tools/testing/selftests/vfio/run.sh b/tools/testing/selftests/vfio/run.sh
> new file mode 100755
> index 000000000000..b461cc1b2f11
> --- /dev/null
> +++ b/tools/testing/selftests/vfio/run.sh
> @@ -0,0 +1,110 @@
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +
> +readonly VFIO_PCI_DRIVER=/sys/bus/pci/drivers/vfio-pci
> +
> +function bind() {
> +	echo "Binding ${1} to ${2}"
> +	echo "${1}" > "${2}/bind"
> +}
> +
> +function unbind() {
> +	echo "Unbinding ${1} from ${2}"
> +	echo "${1}" > "${2}/unbind"
> +}
> +
> +function set_sriov_numvfs() {
> +	echo "Setting ${1} sriov_numvfs to ${2}"
> +	echo ${2} > /sys/bus/pci/devices/${1}/sriov_numvfs
> +}
> +
> +function add_id() {
> +	if echo $(echo ${1} | tr : ' ') > ${2}/new_id 2> /dev/null; then
> +		echo "Added ${1} to ${2}"
> +		return 0
> +	fi
> +
> +	return 1
> +}

I'd suggest using the newer driver_override mechanism rather than
new_id/remove_id.

I appreciate the work here, I think this could be really useful.  As
Jason notes a few times, I'd also like to see this automatically
iterate through all the combinations of options.  Maybe we could also
think about whether we can define some build environment options to
specify devices to use and this could all be run automatically from
'make kselftest'.  Thanks,

Alex

> +
> +function remove_id() {
> +	echo "Removing ${1} from ${2}"
> +	echo $(echo ${1} | tr : ' ') > ${2}/remove_id
> +}
> +
> +function cleanup() {
> +	if [ "${new_driver}" ]; then unbind ${bdf} ${new_driver} ; fi
> +	if [ "${new_id}"     ]; then remove_id ${device_id} ${VFIO_PCI_DRIVER} ; fi
> +	if [ "${old_driver}" ]; then bind ${bdf} ${old_driver} ; fi
> +	if [ "${old_numvfs}" ]; then set_sriov_numvfs ${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() {
> +	while getopts "d:hs" opt; do
> +		case $opt in
> +			d) 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.
> +	[ "${bdf}" ] || usage
> +
> +	trap cleanup EXIT
> +	set -e
> +
> +	test -d /sys/bus/pci/devices/${bdf}
> +
> +	device_id=$(lspci -s ${bdf} -n | cut -d' ' -f3)
> +
> +	if [ -f /sys/bus/pci/devices/${bdf}/sriov_numvfs ]; then
> +		old_numvfs=$(cat /sys/bus/pci/devices/${bdf}/sriov_numvfs)
> +		set_sriov_numvfs ${bdf} 0
> +	fi
> +
> +	if [ -L /sys/bus/pci/devices/${bdf}/driver ]; then
> +		old_driver=$(readlink -m /sys/bus/pci/devices/${bdf}/driver)
> +		unbind ${bdf} ${old_driver}
> +	fi
> +
> +	# Add the device ID to vfio-pci. If it hasn't already been added, this will
> +	# succeed and bind the device to vfio-pci. If it has already been added, this
> +	# will fail and we have to manually bind the device.
> +	if add_id ${device_id} ${VFIO_PCI_DRIVER}; then
> +		new_id=true
> +	else
> +		bind ${bdf} ${VFIO_PCI_DRIVER}
> +	fi
> +
> +	new_driver=${VFIO_PCI_DRIVER}
> +
> +	echo
> +	if [ "${shell}" ]; then
> +		echo "Dropping into ${SHELL} with BDF=${bdf}"
> +		BDF=${bdf} ${SHELL}
> +	else
> +		"$@" ${bdf}
> +	fi
> +	echo
> +}
> +
> +main "$@"


  reply	other threads:[~2025-06-02 23:08 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-23 23:29 [RFC PATCH 00/33] vfio: Introduce selftests for VFIO David Matlack
2025-05-23 23:29 ` [RFC PATCH 01/33] selftests: Create tools/testing/selftests/vfio David Matlack
2025-05-23 23:29 ` [RFC PATCH 02/33] vfio: selftests: Add a helper library for VFIO selftests David Matlack
2025-06-03  7:52   ` liulongfang
2025-05-23 23:29 ` [RFC PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test David Matlack
2025-05-23 23:29 ` [RFC PATCH 04/33] vfio: selftests: Test basic VFIO and IOMMUFD integration David Matlack
2025-06-02 23:08   ` Alex Williamson
2025-06-03 15:53     ` David Matlack
2025-05-23 23:29 ` [RFC PATCH 05/33] vfio: selftests: Move vfio dma mapping test to their own file David Matlack
2025-05-23 23:29 ` [RFC PATCH 06/33] vfio: selftests: Add test to reset vfio device David Matlack
2025-05-23 23:29 ` [RFC PATCH 07/33] vfio: selftests: Use command line to set hugepage size for DMA mapping test David Matlack
2025-05-26 17:15   ` Jason Gunthorpe
2025-05-30 16:50     ` David Matlack
2025-05-30 17:25       ` Jason Gunthorpe
2025-06-06 23:05         ` David Matlack
2025-06-09  0:23           ` Jason Gunthorpe
2025-05-23 23:29 ` [RFC PATCH 08/33] vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU David Matlack
2025-05-26 17:16   ` Jason Gunthorpe
2025-05-27 23:04     ` David Matlack
2025-05-30 21:12   ` David Matlack
2025-05-31 19:09     ` Andy Shevchenko
2025-06-01  7:45       ` Greg Kroah-Hartman
2025-06-03 17:28         ` David Matlack
2025-05-23 23:29 ` [RFC PATCH 09/33] tools headers: Add stub definition for __iomem David Matlack
2025-05-23 23:29 ` [RFC PATCH 10/33] tools headers: Import asm-generic MMIO helpers David Matlack
2025-05-23 23:29 ` [RFC PATCH 11/33] tools headers: Import x86 MMIO helper overrides David Matlack
2025-05-23 23:29 ` [RFC PATCH 12/33] tools headers: Import iosubmit_cmds512() David Matlack
2025-05-23 23:29 ` [RFC PATCH 13/33] tools headers: Import drivers/dma/ioat/{hw.h,registers.h} David Matlack
2025-05-26 17:18   ` Jason Gunthorpe
2025-05-27 23:10     ` David Matlack
2025-05-23 23:29 ` [RFC PATCH 14/33] tools headers: Import drivers/dma/idxd/registers.h David Matlack
2025-05-23 23:30 ` [RFC PATCH 15/33] tools headers: Import linux/pci_ids.h David Matlack
2025-05-23 23:30 ` [RFC PATCH 16/33] vfio: selftests: Keep track of DMA regions mapped into the device David Matlack
2025-05-23 23:30 ` [RFC PATCH 17/33] vfio: selftests: Enable asserting MSI eventfds not firing David Matlack
2025-06-30 12:24   ` [PATCH RFC " Sairaj Kodilkar
2025-07-07 17:43     ` David Matlack
2025-05-23 23:30 ` [RFC PATCH 18/33] vfio: selftests: Add a helper for matching vendor+device IDs David Matlack
2025-05-23 23:30 ` [RFC PATCH 19/33] vfio: selftests: Add driver framework David Matlack
2025-05-23 23:30 ` [RFC PATCH 20/33] vfio: sefltests: Add vfio_pci_driver_test David Matlack
2025-05-23 23:30 ` [RFC PATCH 21/33] vfio: selftests: Add driver for Intel CBDMA David Matlack
2025-05-23 23:30 ` [RFC PATCH 22/33] vfio: selftests: Add driver for Intel DSA David Matlack
2025-05-23 23:30 ` [RFC PATCH 23/33] vfio: selftests: Move helper to get cdev path to libvfio David Matlack
2025-05-23 23:30 ` [RFC PATCH 24/33] vfio: selftests: Encapsulate IOMMU mode David Matlack
2025-05-23 23:30 ` [RFC PATCH 25/33] vfio: selftests: Add [-i iommu_mode] option to all tests David Matlack
2025-05-23 23:30 ` [RFC PATCH 26/33] vfio: selftests: Add vfio_type1v2_mode David Matlack
2025-05-23 23:30 ` [RFC PATCH 27/33] vfio: selftests: Add iommufd_compat_type1{,v2} modes David Matlack
2025-05-23 23:30 ` [RFC PATCH 28/33] vfio: selftests: Add iommufd mode David Matlack
2025-05-23 23:30 ` [RFC PATCH 29/33] vfio: selftests: Make iommufd the default iommu_mode David Matlack
2025-05-26 17:20   ` Jason Gunthorpe
2025-06-03 17:24     ` David Matlack
2025-05-23 23:30 ` [RFC PATCH 30/33] vfio: selftests: Add a script to help with running VFIO selftests David Matlack
2025-06-02 23:07   ` Alex Williamson [this message]
2025-06-03 15:51     ` David Matlack
2025-05-23 23:30 ` [RFC PATCH 31/33] KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests David Matlack
2025-05-23 23:30 ` [RFC PATCH 32/33] KVM: selftests: Test sending a vfio-pci device IRQ to a VM David Matlack
2025-05-23 23:30 ` [RFC PATCH 33/33] KVM: selftests: Use real device MSIs in vfio_pci_device_irq_test David Matlack
2025-05-26  9:12 ` [RFC PATCH 00/33] vfio: Introduce selftests for VFIO Yi Liu
2025-05-27 17:26   ` David Matlack
2025-05-26 17:09 ` Jason Gunthorpe
2025-05-27 23:01   ` David Matlack
2025-05-28  0:11     ` Jason Gunthorpe
2025-05-28 21:52       ` David Matlack
2025-06-03  7:11     ` Joel Granados
2025-06-02 17:12 ` Cédric Le Goater

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=20250602170759.6031b4f4.alex.williamson@redhat.com \
    --to=alex.williamson@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=ajayachandra@nvidia.com \
    --cc=ajones@ventanamicro.com \
    --cc=andrii@kernel.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bhelgaas@google.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dmatlack@google.com \
    --cc=eric.auger@redhat.com \
    --cc=fenghua.yu@intel.com \
    --cc=fujita.tomonori@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=imbrenda@linux.ibm.com \
    --cc=jgg@nvidia.com \
    --cc=jolsa@kernel.org \
    --cc=jrhilke@google.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=leonro@nvidia.com \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=parav@nvidia.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=pbonzini@redhat.com \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=richard.weiyang@gmail.com \
    --cc=saeedm@nvidia.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=tiwai@suse.de \
    --cc=vinicius.gomes@intel.com \
    --cc=vipinsh@google.com \
    --cc=vkoul@kernel.org \
    --cc=wangyuli@uniontech.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;
as well as URLs for NNTP newsgroup(s).