Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: David Matlack <dmatlack@google.com>
To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org, linux-pci@vger.kernel.org,
	 linux-riscv@lists.infradead.org
Cc: Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	 Alex Williamson <alex@shazbot.org>,
	Bjorn Helgaas <bhelgaas@google.com>,
	 David Matlack <dmatlack@google.com>,
	Jacob Pan <jacob.pan@linux.microsoft.com>,
	 Jason Gunthorpe <jgg@nvidia.com>,
	Josh Hilke <jrhilke@google.com>, Lukas Wunner <lukas@wunner.de>,
	 Mike Rapoport <rppt@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Parav Pandit <parav@nvidia.com>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	Paul Walmsley <pjw@kernel.org>,
	 Pranjal Shrivastava <praan@google.com>,
	Pratyush Yadav <pratyush@kernel.org>,
	 Samiullah Khawaja <skhawaja@google.com>,
	Shuah Khan <shuah@kernel.org>,  Vipin Sharma <vipinsh@google.com>
Subject: [PATCH 06/18] selftests/liveupdate: Support test definitions in vmtest.sh
Date: Wed,  2 Dec 2026 00:39:43 +0000	[thread overview]
Message-ID: <20261202003955.3607156-7-dmatlack@google.com> (raw)
In-Reply-To: <20261202003955.3607156-1-dmatlack@google.com>

Allow a test to be described by a <test>.vmtest.sh file, which
vmtest.sh sources. A test definition can set the test binary to run and
the arguments to pass to it, extra QEMU arguments, the QEMU timeout, and
a host-side check to run after the test passes in the VM. vmtest.sh
writes the arguments to /test_args in the initrds, and init passes them
to the test binary in both stages.

This lets tests that need more than the default VM, e.g. a specific PCI
topology, keep their setup out of vmtest.sh. Test definitions can also
live in subdirectories, e.g. "vmtest.sh -T pci/root_port".

Signed-off-by: David Matlack <dmatlack@google.com>
---
 tools/testing/selftests/liveupdate/init.c    | 49 +++++++++++++++++++-
 tools/testing/selftests/liveupdate/vmtest.sh | 44 +++++++++++++++---
 2 files changed, 84 insertions(+), 9 deletions(-)

diff --git a/tools/testing/selftests/liveupdate/init.c b/tools/testing/selftests/liveupdate/init.c
index 824c9da2facf..bcb401295801 100644
--- a/tools/testing/selftests/liveupdate/init.c
+++ b/tools/testing/selftests/liveupdate/init.c
@@ -19,6 +19,8 @@
 #define KERNEL_IMAGE "/kernel"
 #define INITRD_IMAGE "/initrd.img"
 #define TEST_BINARY "/test_binary"
+#define TEST_ARGS "/test_args"
+#define MAX_TEST_ARGS 32
 
 static int mount_filesystems(void)
 {
@@ -95,21 +97,64 @@ static int kexec_load(void)
 	return err;
 }
 
+/*
+ * Split the contents of TEST_ARGS, if it exists, into @args at whitespace.
+ * Returns the number of arguments, or -1 on error.
+ */
+static int read_test_args(char *buf, size_t size, char **args, int max)
+{
+	int fd, nr_args = 0;
+	ssize_t len;
+	char *p;
+
+	fd = open(TEST_ARGS, O_RDONLY);
+	if (fd < 0)
+		return 0;
+
+	len = read(fd, buf, size - 1);
+	close(fd);
+	if (len < 0)
+		return -1;
+	buf[len] = 0;
+
+	for (p = buf; *p; ) {
+		if (*p == ' ' || *p == '\n') {
+			*p++ = 0;
+			continue;
+		}
+
+		if (nr_args == max)
+			return -1;
+
+		args[nr_args++] = p;
+		while (*p && *p != ' ' && *p != '\n')
+			p++;
+	}
+
+	return nr_args;
+}
+
 static int run_test(int stage)
 {
+	char *argv[3 + MAX_TEST_ARGS + 1] = {TEST_BINARY, "-s"};
 	char stage_arg[32];
+	char args[1024];
 	int status;
 	pid_t pid;
 
 	snprintf(stage_arg, sizeof(stage_arg), "%d", stage);
+	argv[2] = stage_arg;
+
+	if (read_test_args(args, sizeof(args), &argv[3], MAX_TEST_ARGS) < 0) {
+		fprintf(stderr, "INIT: Failed to read %s\n", TEST_ARGS);
+		return -1;
+	}
 
 	pid = fork();
 	if (pid < 0)
 		return -1;
 
 	if (!pid) {
-		char *const argv[] = {TEST_BINARY, "-s", stage_arg, NULL};
-
 		execve(TEST_BINARY, argv, NULL);
 		fprintf(stderr, "INIT: execve failed\n");
 		_exit(1);
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index b24a8bb774ca..797190d4b47c 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -13,6 +13,25 @@ headers_dir=""
 initrd=""
 KEEP_WORKSPACE=0
 
+# A test named "foo" either runs the binary "foo" built by the Makefile, or is
+# described by foo.vmtest.sh. A test definition is sourced after $arch,
+# $target, $test_dir and $workspace_dir are set, and may override any of the
+# following:
+#
+#   TEST_BINARY          Test binary to run, as named in the Makefile
+#                        (default: the test name)
+#   TEST_ARGS            Arguments for the test binary in both stages
+#   TEST_QEMU_ARGS       Extra QEMU arguments
+#   TEST_TIMEOUT         Timeout for the QEMU run
+#   test_check()         Host-side check, run after the test passes in the VM
+TEST_ARGS=()
+TEST_QEMU_ARGS=()
+TEST_TIMEOUT="30s"
+
+function test_check() {
+	return 0
+}
+
 source "$test_dir/../kselftest/ktap_helpers.sh"
 
 function get_arch_conf() {
@@ -42,7 +61,7 @@ Options:
 	-d)	path to the kernel build directory (default: .luo_test_build.<arch>)
 	-j)	number of jobs for compilation
 	-t)	run test for target_arch (aarch64, x86_64, riscv64)
-	-T)	test name to run (default: luo_kexec_simple)
+	-T)	test name to run, e.g. luo_kexec_simple or pci/root_port (default: luo_kexec_simple)
 	-w)	custom workspace directory (default: creates temp dir)
 	-k)	keep workspace directory
 	-h)	display this help
@@ -138,17 +157,19 @@ function build_kernel() {
 function mkinitrd() {
 	local build_dir=$1
 	local kernel_path=$2
-	local test_name=$3
-	local make_cmd=$4
+	local make_cmd=$3
 	local output="$workspace_dir/build"
 	local init="$output/init"
-	local test_binary="$output/$test_name"
+	local test_binary="$output/$TEST_BINARY"
 
 	# Build the test binary and the init process
 	mkdir -p "$output"
 	$make_cmd -C "$test_dir" NOLIBC=1 OUTPUT="$output" \
 		KHDR_INCLUDES="-I$headers_dir/include" "$init" "$test_binary"
 
+	# init passes these to the test binary in both stages
+	echo "${TEST_ARGS[*]}" > "$workspace_dir/test_args"
+
 	cat > "$workspace_dir/cpio_list_inner" <<EOF
 dir /dev 0755 0 0
 dir /proc 0755 0 0
@@ -156,6 +177,7 @@ dir /debugfs 0755 0 0
 nod /dev/console 0600 0 0 c 5 1
 file /init $init 0755 0 0
 file /test_binary $test_binary 0755 0 0
+file /test_args $workspace_dir/test_args 0644 0 0
 EOF
 
 	# Generate inner_initrd.cpio
@@ -169,6 +191,7 @@ nod /dev/console 0600 0 0 c 5 1
 file /init $init 0755 0 0
 file /kernel $kernel_path 0644 0 0
 file /test_binary $test_binary 0755 0 0
+file /test_args $workspace_dir/test_args 0644 0 0
 file /initrd.img $workspace_dir/inner_initrd.cpio 0644 0 0
 EOF
 
@@ -185,15 +208,17 @@ function run_qemu() {
 	cmdline="$cmdline liveupdate=on panic=-1"
 
 	echo "# Serial Log: $serial"
-	timeout 30s \
+	timeout "$TEST_TIMEOUT" \
 	$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \
 		  -accel kvm -accel hvf -accel tcg \
 		  -serial file:"$serial" \
 		  -append "$cmdline" \
 		  -kernel "$kernel_path" \
-		  -initrd "$initrd"
+		  -initrd "$initrd" \
+		  "${TEST_QEMU_ARGS[@]}"
 
 	grep "TEST PASSED" "$serial" &> /dev/null || fail "Liveupdate failed"
+	test_check || fail "Host-side check failed"
 }
 
 function target_to_arch() {
@@ -248,6 +273,11 @@ function main() {
 
 	local arch=$(target_to_arch "$target")
 
+	TEST_BINARY="$test_name"
+	if [ -f "$test_dir/$test_name.vmtest.sh" ]; then
+		source "$test_dir/$test_name.vmtest.sh"
+	fi
+
 	if [ -z "$build_dir" ]; then
 		build_dir="$kernel_dir/.luo_test_build.$arch"
 	fi
@@ -262,7 +292,7 @@ function main() {
 	build_kernel "$build_dir" "$make_cmd_build" "$KERNEL_IMAGE" "$target"
 
 	local final_kernel="$build_dir/arch/$arch/boot/$KERNEL_IMAGE"
-	mkinitrd "$build_dir" "$final_kernel" "$test_name" "$make_cmd"
+	mkinitrd "$build_dir" "$final_kernel" "$make_cmd"
 
 	run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$final_kernel"
 	ktap_test_pass "$test_name succeeded"
-- 
2.56.1.911.g883d8bcb20-goog


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2026-12-02  0:40 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
2026-12-02  0:39 ` [PATCH 01/18] liveupdate: Export symbols needed by modules David Matlack
2026-12-02  0:39 ` [PATCH 02/18] selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test David Matlack
2026-12-02  0:39 ` [PATCH 03/18] selftests/liveupdate: Build objects in $(OUTPUT) David Matlack
2026-12-02  0:39 ` [PATCH 04/18] selftests/liveupdate: Support building the kexec tests against nolibc David Matlack
2026-12-02  0:39 ` [PATCH 05/18] selftests/liveupdate: Build vmtest.sh's binaries with the Makefile David Matlack
2026-12-02  0:39 ` David Matlack [this message]
2026-12-02  0:39 ` [PATCH 07/18] selftests/liveupdate: Support kernel modules in vmtest.sh David Matlack
2026-12-02  0:39 ` [PATCH 08/18] selftests/liveupdate: Support a different command line for each kernel David Matlack
2026-12-02  0:39 ` [PATCH 09/18] selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh David Matlack
2026-12-02  0:39 ` [PATCH 10/18] selftests/liveupdate: Add a test driver for PCI Live Update David Matlack
2026-12-02  0:39 ` [PATCH 11/18] selftests/liveupdate: pci: Add a test with an endpoint below a root port David Matlack
2026-12-02  0:39 ` [PATCH 12/18] selftests/liveupdate: pci: Check PCI config writes with QEMU tracing David Matlack
2026-12-02  0:39 ` [PATCH 13/18] selftests/liveupdate: pci: Add a test with an endpoint on the root bus David Matlack
2026-12-02  0:39 ` [PATCH 14/18] selftests/liveupdate: pci: Add a test with an endpoint below a switch David Matlack
2026-12-02  0:39 ` [PATCH 15/18] selftests/liveupdate: pci: Test bus numbers with pci=assign-busses David Matlack
2026-12-02  0:39 ` [PATCH 16/18] selftests/liveupdate: pci: Test bus numbers with pci=hpbussize David Matlack
2026-12-02  0:39 ` [PATCH 17/18] selftests/liveupdate: pci: Test ARI Forwarding with pci=noari David Matlack
2026-12-02  0:39 ` [PATCH 18/18] selftests/liveupdate: pci: Test ACS with pci=config_acs David Matlack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202003955.3607156-7-dmatlack@google.com \
    --to=dmatlack@google.com \
    --cc=alex@ghiti.fr \
    --cc=alex@shazbot.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=bhelgaas@google.com \
    --cc=jacob.pan@linux.microsoft.com \
    --cc=jgg@nvidia.com \
    --cc=jrhilke@google.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=lukas@wunner.de \
    --cc=palmer@dabbelt.com \
    --cc=parav@nvidia.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=pjw@kernel.org \
    --cc=praan@google.com \
    --cc=pratyush@kernel.org \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=skhawaja@google.com \
    --cc=vipinsh@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox