* [PATCH 01/18] liveupdate: Export symbols needed by modules
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 02/18] selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test David Matlack
` (16 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
From: Vipin Sharma <vipinsh@google.com>
Export liveupdate_register_file_handler() and
liveupdate_unregister_file_handler() so that drivers built as modules
can register Live Update file handlers.
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
Reviewed-by: Pranjal Shrivastava <praan@google.com>
Signed-off-by: Vipin Sharma <vipinsh@google.com>
Signed-off-by: David Matlack <dmatlack@google.com>
---
kernel/liveupdate/luo_file.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/kernel/liveupdate/luo_file.c b/kernel/liveupdate/luo_file.c
index 9bda37b0dc4f..de6c7fe6f4fa 100644
--- a/kernel/liveupdate/luo_file.c
+++ b/kernel/liveupdate/luo_file.c
@@ -936,6 +936,7 @@ int liveupdate_register_file_handler(struct liveupdate_file_handler *fh)
up_write(&luo_register_rwlock);
return err;
}
+EXPORT_SYMBOL_GPL(liveupdate_register_file_handler);
/**
* liveupdate_unregister_file_handler - Unregister a liveupdate file handler
@@ -953,6 +954,7 @@ void liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)
luo_flb_unregister_all(fh);
list_del(&ACCESS_PRIVATE(fh, list));
}
+EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler);
/**
* liveupdate_get_token_outgoing - Get the token for a preserved file.
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 02/18] selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 03/18] selftests/liveupdate: Build objects in $(OUTPUT) David Matlack
` (15 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Exit vmtest.sh with KSFT_SKIP when a test is skipped, so that
run-vmtests.sh reports it as skipped rather than passed.
skip() exits with KSFT_SKIP, but the EXIT trap then calls
ktap_finished(), which exits with KSFT_PASS since every planned test
either passed or was skipped.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/vmtest.sh | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index 70a8d978077a..6cbee44cd27e 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -50,12 +50,20 @@ EOF
}
function cleanup() {
+ local exit_code=$?
+
if [ "$KEEP_WORKSPACE" -eq 1 ]; then
echo "# Workspace preserved at: $workspace_dir"
else
rm -fr "$workspace_dir"
fi
+ # ktap_finished() exits with KSFT_PASS for skipped tests
+ if [ "$exit_code" -eq "$KSFT_SKIP" ]; then
+ ktap_print_totals
+ exit "$KSFT_SKIP"
+ fi
+
ktap_finished
}
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 03/18] selftests/liveupdate: Build objects in $(OUTPUT)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 04/18] selftests/liveupdate: Support building the kexec tests against nolibc David Matlack
` (14 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Build the objects of the test programs in $(OUTPUT), next to the
programs, rather than in the source directory.
The programs depend on %.o, which is relative to the source directory,
so make builds the objects there with its implicit rule even when
building with O= or OUTPUT=. Make the programs depend on
$(OUTPUT)/%.o instead, and add an explicit rule for it that also
creates the object's directory, so that programs can live in
subdirectories.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/Makefile | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index 634211c66652..31517a07f531 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -23,7 +23,11 @@ TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O))
TEST_DEP_FILES += $(patsubst %.o, %.d, $(TEST_O))
-include $(TEST_DEP_FILES)
-$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIBLIVEUPDATE_O)
+$(TEST_O): $(OUTPUT)/%.o: %.c
+ $(Q)mkdir -p $(@D)
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBLIVEUPDATE_O)
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@
EXTRA_CLEAN += $(TEST_O)
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 04/18] selftests/liveupdate: Support building the kexec tests against nolibc
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (2 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 03/18] selftests/liveupdate: Build objects in $(OUTPUT) David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 05/18] selftests/liveupdate: Build vmtest.sh's binaries with the Makefile David Matlack
` (13 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a NOLIBC=1 mode to the liveupdate selftests Makefile, which builds
the tests that run across kexec, and the init process that runs them in
vmtest.sh's VMs, as static binaries against nolibc.
vmtest.sh builds these binaries with its own compiler invocations
today, and this mode will let it use the Makefile instead. The
liveupdate test is not built in this mode since it uses the kselftest
harness, which requires libc. make clean removes the binaries of both
modes.
Fix two warnings in init.c that show up now that it is built with -Wall:
an unused variable, and status possibly being used uninitialized if
waitpid() fails.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/Makefile | 13 +++++++++++++
tools/testing/selftests/liveupdate/init.c | 4 ++--
2 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index 31517a07f531..df0567e8ca15 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -1,6 +1,13 @@
# SPDX-License-Identifier: GPL-2.0-only
+# With NOLIBC=1, build the tests that run across kexec, and the init process
+# that runs them, as static binaries against nolibc. vmtest.sh uses this to
+# build the initrds for its VMs.
+ifeq ($(NOLIBC),)
TEST_GEN_PROGS += liveupdate
+else
+TEST_GEN_FILES += init
+endif
TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
TEST_GEN_PROGS_EXTENDED += luo_multi_session
@@ -12,6 +19,9 @@ TEST_FILES += do_kexec.sh
include ../lib.mk
include lib/libliveupdate.mk
+ifneq ($(NOLIBC),)
+CFLAGS += -static -nostdlib -nostdinc -I$(top_srcdir)/tools/include/nolibc
+endif
CFLAGS += $(KHDR_INCLUDES)
CFLAGS += -Wall -O2 -Wno-unused-function
CFLAGS += -MD
@@ -31,4 +41,7 @@ $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBLI
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@
EXTRA_CLEAN += $(TEST_O)
+# Clean up the binaries of both modes regardless of NOLIBC.
+EXTRA_CLEAN += $(OUTPUT)/liveupdate $(OUTPUT)/liveupdate.o $(OUTPUT)/liveupdate.d
+EXTRA_CLEAN += $(OUTPUT)/init $(OUTPUT)/init.d
EXTRA_CLEAN += $(TEST_DEP_FILES)
diff --git a/tools/testing/selftests/liveupdate/init.c b/tools/testing/selftests/liveupdate/init.c
index fb08bd58b9b9..824c9da2facf 100644
--- a/tools/testing/selftests/liveupdate/init.c
+++ b/tools/testing/selftests/liveupdate/init.c
@@ -115,7 +115,8 @@ static int run_test(int stage)
_exit(1);
}
- waitpid(pid, &status, 0);
+ if (waitpid(pid, &status, 0) < 0)
+ return -1;
return (WIFEXITED(status) && WEXITSTATUS(status) == 0) ? 0 : -1;
}
@@ -144,7 +145,6 @@ static int get_current_stage(void)
int main(int argc, char *argv[])
{
int current_stage;
- int err;
if (mount_filesystems())
goto err_reboot;
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 05/18] selftests/liveupdate: Build vmtest.sh's binaries with the Makefile
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (3 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 04/18] selftests/liveupdate: Support building the kexec tests against nolibc David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 06/18] selftests/liveupdate: Support test definitions in vmtest.sh David Matlack
` (12 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Build the test binary and the init process for vmtest.sh's initrds with
the liveupdate selftests Makefile in NOLIBC=1 mode, rather than with
separate compiler invocations in vmtest.sh. The build output goes into
the workspace.
This leaves a single description of how the tests are built, so that
any test added to the Makefile can also be run with vmtest.sh,
including tests built from multiple source files.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/vmtest.sh | 32 ++++++++------------
1 file changed, 13 insertions(+), 19 deletions(-)
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index 6cbee44cd27e..b24a8bb774ca 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -139,29 +139,23 @@ function mkinitrd() {
local build_dir=$1
local kernel_path=$2
local test_name=$3
+ local make_cmd=$4
+ local output="$workspace_dir/build"
+ local init="$output/init"
+ local test_binary="$output/$test_name"
- # Compile the test binary and the init process
- "$CROSS_COMPILE"gcc -static -O2 -nostdinc -nostdlib \
- -I "$headers_dir/include" \
- -I "$kernel_dir/tools/include/nolibc" \
- -I "$test_dir/lib/include" \
- -o "$workspace_dir/test_binary" \
- "$test_dir/$test_name.c" "$test_dir/lib/lu_utils.c"
-
- "$CROSS_COMPILE"gcc -s -static -Os -nostdinc -nostdlib \
- -fno-asynchronous-unwind-tables -fno-ident \
- -fno-stack-protector \
- -I "$headers_dir/include" \
- -I "$kernel_dir/tools/include/nolibc" \
- -o "$workspace_dir/init" "$test_dir/init.c"
+ # 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"
cat > "$workspace_dir/cpio_list_inner" <<EOF
dir /dev 0755 0 0
dir /proc 0755 0 0
dir /debugfs 0755 0 0
nod /dev/console 0600 0 0 c 5 1
-file /init $workspace_dir/init 0755 0 0
-file /test_binary $workspace_dir/test_binary 0755 0 0
+file /init $init 0755 0 0
+file /test_binary $test_binary 0755 0 0
EOF
# Generate inner_initrd.cpio
@@ -172,9 +166,9 @@ dir /dev 0755 0 0
dir /proc 0755 0 0
dir /debugfs 0755 0 0
nod /dev/console 0600 0 0 c 5 1
-file /init $workspace_dir/init 0755 0 0
+file /init $init 0755 0 0
file /kernel $kernel_path 0644 0 0
-file /test_binary $workspace_dir/test_binary 0755 0 0
+file /test_binary $test_binary 0755 0 0
file /initrd.img $workspace_dir/inner_initrd.cpio 0644 0 0
EOF
@@ -268,7 +262,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"
+ mkinitrd "$build_dir" "$final_kernel" "$test_name" "$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
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 06/18] selftests/liveupdate: Support test definitions in vmtest.sh
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (4 preceding siblings ...)
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
2026-12-02 0:39 ` [PATCH 07/18] selftests/liveupdate: Support kernel modules " David Matlack
` (11 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
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
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 07/18] selftests/liveupdate: Support kernel modules in vmtest.sh
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (5 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 06/18] selftests/liveupdate: Support test definitions in vmtest.sh David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 08/18] selftests/liveupdate: Support a different command line for each kernel David Matlack
` (10 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add TEST_MODULES to vmtest.sh test definitions. vmtest.sh builds the
modules in the Makefile's TEST_GEN_MODS_DIR against the test kernel,
and places the listed modules at the root of both kernels' initrds,
where the test binary can load them.
The modules are built with MO= so that their build output goes into the
workspace rather than the source tree. The kernel's own modules are
built too, since external modules need Module.symvers.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/vmtest.sh | 24 ++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index 797190d4b47c..937f10fc9592 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -21,10 +21,13 @@ KEEP_WORKSPACE=0
# 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_MODULES Kernel modules to place at /<module>.ko in the
+# initrd, built from the Makefile's TEST_GEN_MODS_DIR
# 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_MODULES=()
TEST_QEMU_ARGS=()
TEST_TIMEOUT="30s"
@@ -152,6 +155,11 @@ function build_kernel() {
$make_cmd "$kimage"
$make_cmd headers_install INSTALL_HDR_PATH="$headers_dir"
+
+ # External modules need Module.symvers
+ if [ ${#TEST_MODULES[@]} -gt 0 ]; then
+ $make_cmd modules
+ fi
}
function mkinitrd() {
@@ -161,12 +169,26 @@ function mkinitrd() {
local output="$workspace_dir/build"
local init="$output/init"
local test_binary="$output/$TEST_BINARY"
+ local extra_files="$workspace_dir/cpio_list_extra"
+ local module ko
# 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"
+ # Build the test modules against the test kernel, and place the ones
+ # the test needs in both initrds
+ : > "$extra_files"
+ if [ ${#TEST_MODULES[@]} -gt 0 ]; then
+ $make_cmd -C "$test_dir" KDIR="$build_dir" MO="$output/modules" gen_mods_dir
+ fi
+ for module in "${TEST_MODULES[@]}"; do
+ ko="$output/modules/$module.ko"
+ [ -f "$ko" ] || fail "Module $module was not built"
+ echo "file /$module.ko $ko 0644 0 0" >> "$extra_files"
+ done
+
# init passes these to the test binary in both stages
echo "${TEST_ARGS[*]}" > "$workspace_dir/test_args"
@@ -179,6 +201,7 @@ 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
+ cat "$extra_files" >> "$workspace_dir/cpio_list_inner"
# Generate inner_initrd.cpio
"$build_dir/scripts/gen_init_cpio" "$workspace_dir/cpio_list_inner" > "$workspace_dir/inner_initrd.cpio"
@@ -194,6 +217,7 @@ 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
+ cat "$extra_files" >> "$workspace_dir/cpio_list"
# Generate the final initrd
"$build_dir/scripts/gen_init_cpio" "$workspace_dir/cpio_list" > "$initrd"
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 08/18] selftests/liveupdate: Support a different command line for each kernel
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (6 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 07/18] selftests/liveupdate: Support kernel modules " David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 09/18] selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh David Matlack
` (9 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add TEST_CMDLINE_STAGE1 and TEST_CMDLINE_STAGE2 to vmtest.sh test
definitions, which are only passed to the first and the kexec'd kernel
respectively.
When either is set, vmtest.sh writes the kexec'd kernel's full command
line to /cmdline_stage2 in the first kernel's initrd, and init uses it
instead of the current command line. This lets tests check how the next
kernel handles preserved state that was set up under a different
configuration.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/init.c | 33 ++++++++++++++------
tools/testing/selftests/liveupdate/vmtest.sh | 26 ++++++++++++---
2 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/tools/testing/selftests/liveupdate/init.c b/tools/testing/selftests/liveupdate/init.c
index bcb401295801..45abf45c206c 100644
--- a/tools/testing/selftests/liveupdate/init.c
+++ b/tools/testing/selftests/liveupdate/init.c
@@ -21,6 +21,7 @@
#define TEST_BINARY "/test_binary"
#define TEST_ARGS "/test_args"
#define MAX_TEST_ARGS 32
+#define CMDLINE_STAGE2 "/cmdline_stage2"
static int mount_filesystems(void)
{
@@ -50,21 +51,22 @@ static long kexec_file_load(int kernel_fd, int initrd_fd,
cmdline, flags);
}
-static int kexec_load(void)
+/*
+ * Read the kexec'd kernel's command line from CMDLINE_STAGE2 if it exists, or
+ * reuse the current command line otherwise.
+ */
+static int read_cmdline_stage2(char *cmdline, size_t size)
{
- char cmdline[COMMAND_LINE_SIZE];
- int kernel_fd, initrd_fd, err;
ssize_t len;
int fd;
- fd = open("/proc/cmdline", O_RDONLY);
- if (fd < 0) {
- fprintf(stderr, "INIT: Failed to read /proc/cmdline\n");
-
+ fd = open(CMDLINE_STAGE2, O_RDONLY);
+ if (fd < 0)
+ fd = open("/proc/cmdline", O_RDONLY);
+ if (fd < 0)
return -1;
- }
- len = read(fd, cmdline, sizeof(cmdline) - 1);
+ len = read(fd, cmdline, size - 1);
close(fd);
if (len < 0)
return -1;
@@ -73,6 +75,19 @@ static int kexec_load(void)
if (len > 0 && cmdline[len - 1] == '\n')
cmdline[len - 1] = 0;
+ return 0;
+}
+
+static int kexec_load(void)
+{
+ char cmdline[COMMAND_LINE_SIZE];
+ int kernel_fd, initrd_fd, err;
+
+ if (read_cmdline_stage2(cmdline, sizeof(cmdline))) {
+ fprintf(stderr, "INIT: Failed to read the command line\n");
+ return -1;
+ }
+
strncat(cmdline, " luo_stage=2", sizeof(cmdline) - strlen(cmdline) - 1);
kernel_fd = open(KERNEL_IMAGE, O_RDONLY);
diff --git a/tools/testing/selftests/liveupdate/vmtest.sh b/tools/testing/selftests/liveupdate/vmtest.sh
index 937f10fc9592..102261892829 100755
--- a/tools/testing/selftests/liveupdate/vmtest.sh
+++ b/tools/testing/selftests/liveupdate/vmtest.sh
@@ -24,11 +24,15 @@ KEEP_WORKSPACE=0
# TEST_MODULES Kernel modules to place at /<module>.ko in the
# initrd, built from the Makefile's TEST_GEN_MODS_DIR
# TEST_QEMU_ARGS Extra QEMU arguments
+# TEST_CMDLINE_STAGE1 Extra command line for the first kernel only
+# TEST_CMDLINE_STAGE2 Extra command line for the kexec'd kernel only
# TEST_TIMEOUT Timeout for the QEMU run
# test_check() Host-side check, run after the test passes in the VM
TEST_ARGS=()
TEST_MODULES=()
TEST_QEMU_ARGS=()
+TEST_CMDLINE_STAGE1=""
+TEST_CMDLINE_STAGE2=""
TEST_TIMEOUT="30s"
function test_check() {
@@ -166,6 +170,7 @@ function mkinitrd() {
local build_dir=$1
local kernel_path=$2
local make_cmd=$3
+ local cmdline_stage2=$4
local output="$workspace_dir/build"
local init="$output/init"
local test_binary="$output/$TEST_BINARY"
@@ -219,6 +224,13 @@ file /initrd.img $workspace_dir/inner_initrd.cpio 0644 0 0
EOF
cat "$extra_files" >> "$workspace_dir/cpio_list"
+ # Only the first kernel needs the command line for the kexec'd kernel
+ if [ -n "$cmdline_stage2" ]; then
+ echo "$cmdline_stage2" > "$workspace_dir/cmdline_stage2"
+ echo "file /cmdline_stage2 $workspace_dir/cmdline_stage2 0644 0 0" \
+ >> "$workspace_dir/cpio_list"
+ fi
+
# Generate the final initrd
"$build_dir/scripts/gen_init_cpio" "$workspace_dir/cpio_list" > "$initrd"
}
@@ -229,8 +241,6 @@ function run_qemu() {
local kernel_path=$3
local serial="$workspace_dir/qemu.serial"
- cmdline="$cmdline liveupdate=on panic=-1"
-
echo "# Serial Log: $serial"
timeout "$TEST_TIMEOUT" \
$qemu_cmd -m 1G -smp 2 -no-reboot -nographic -nodefaults \
@@ -316,9 +326,17 @@ 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" "$make_cmd"
+ local cmdline="$KERNEL_CMDLINE liveupdate=on panic=-1"
+ local cmdline_stage2=""
+
+ # By default, the kexec'd kernel reuses the first kernel's command line
+ if [ -n "$TEST_CMDLINE_STAGE1$TEST_CMDLINE_STAGE2" ]; then
+ cmdline_stage2="$cmdline $TEST_CMDLINE_STAGE2"
+ fi
+
+ mkinitrd "$build_dir" "$final_kernel" "$make_cmd" "$cmdline_stage2"
- run_qemu "$QEMU_CMD" "$KERNEL_CMDLINE" "$final_kernel"
+ run_qemu "$QEMU_CMD" "$cmdline $TEST_CMDLINE_STAGE1" "$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
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 09/18] selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (7 preceding siblings ...)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 10/18] selftests/liveupdate: Add a test driver for PCI Live Update David Matlack
` (8 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Make run-vmtests.sh also run every test defined by a
<dir>/<test>.vmtest.sh file, so that tests in subdirectories can be
added without changing the list of tests in run-vmtests.sh.
Replace slashes in test names with underscores when naming log files,
since such test names include the subdirectory.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/run-vmtests.sh | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/liveupdate/run-vmtests.sh b/tools/testing/selftests/liveupdate/run-vmtests.sh
index c3d828cfd63b..2460ac955511 100755
--- a/tools/testing/selftests/liveupdate/run-vmtests.sh
+++ b/tools/testing/selftests/liveupdate/run-vmtests.sh
@@ -22,6 +22,13 @@ TEST_NAMES=(
"luo_stress_sessions"
)
+# Tests in subdirectories are described by <dir>/<name>.vmtest.sh
+for test_def in "$SCRIPT_DIR"/*/*.vmtest.sh; do
+ [ -f "$test_def" ] || continue
+ test_def=${test_def#"$SCRIPT_DIR/"}
+ TEST_NAMES+=("${test_def%.vmtest.sh}")
+done
+
function usage() {
cat <<EOF
$0 [-k] [-o output_dir] [-h]
@@ -73,7 +80,7 @@ function main() {
for arch in "${TARGETS[@]}"; do
for test_name in "${TEST_NAMES[@]}"; do
- log="$output_dir/${arch}_${test_name}.log"
+ log="$output_dir/${arch}_${test_name//\//_}.log"
printf " -> %-8s %-24s ... " "$arch" "$test_name"
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 10/18] selftests/liveupdate: Add a test driver for PCI Live Update
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (8 preceding siblings ...)
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 ` 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
` (7 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add luo_pci_test_driver.ko, a test-only PCI driver that the PCI Live
Update selftests use to preserve PCI devices. The driver binds to Intel
82576 (IGB) devices, such as QEMU's igb device, and hands out a file for
each of its devices through /dev/luo_pci_test_driver. Userspace can
preserve these files in a Live Update session, and the driver's Live
Update file handler then preserves, unpreserves, retrieves and finishes
the device with the PCI Live Update APIs. Other than enabling the device
and bus mastering, the driver leaves the device alone, so that tests can
check how the PCI core treats it.
The driver lives in modules/, a directory for the kernel modules used
by the Live Update selftests, which the selftests build through
TEST_GEN_MODS_DIR.
Add the driver, and tools/testing/selftests/liveupdate/pci/ where its
interface header lives, to the PCI LIVE UPDATE entry in MAINTAINERS.
Signed-off-by: David Matlack <dmatlack@google.com>
---
MAINTAINERS | 2 +
tools/testing/selftests/liveupdate/.gitignore | 3 +
tools/testing/selftests/liveupdate/Makefile | 2 +
.../selftests/liveupdate/modules/Makefile | 20 ++
.../liveupdate/modules/luo_pci_test_driver.c | 279 ++++++++++++++++++
.../liveupdate/pci/luo_pci_test_driver.h | 52 ++++
6 files changed, 358 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/modules/Makefile
create mode 100644 tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
diff --git a/MAINTAINERS b/MAINTAINERS
index ea273cec96e9..f704877b8a38 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -21652,6 +21652,8 @@ F: drivers/pci/liveupdate.c
F: drivers/pci/liveupdate.h
F: include/linux/kho/abi/pci.h
F: include/linux/pci_liveupdate.h
+F: tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
+F: tools/testing/selftests/liveupdate/pci/
PCI MSI DRIVER FOR ALTERA MSI IP
L: linux-pci@vger.kernel.org
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
index 47c670bf532b..9bf744e4f138 100644
--- a/tools/testing/selftests/liveupdate/.gitignore
+++ b/tools/testing/selftests/liveupdate/.gitignore
@@ -9,3 +9,6 @@
!config
!config.*
!Makefile
+
+# Generated by kbuild when building modules/ in place
+*.mod.c
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index df0567e8ca15..2547af3c5353 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -16,6 +16,8 @@ TEST_GEN_PROGS_EXTENDED += luo_stress_files
TEST_FILES += do_kexec.sh
+TEST_GEN_MODS_DIR := modules
+
include ../lib.mk
include lib/libliveupdate.mk
diff --git a/tools/testing/selftests/liveupdate/modules/Makefile b/tools/testing/selftests/liveupdate/modules/Makefile
new file mode 100644
index 000000000000..6efbbd21ecc5
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/Makefile
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel modules used by the Live Update selftests.
+
+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= /lib/modules/$(shell uname -r)/build
+
+obj-m += luo_pci_test_driver.o
+
+# Ensure that KDIR exists, otherwise skip the compilation
+modules:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) modules M=$(TESTMODS_DIR)
+endif
+
+# Ensure that KDIR exists, otherwise skip the clean target
+clean:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) clean M=$(TESTMODS_DIR)
+endif
diff --git a/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
new file mode 100644
index 000000000000..7c595c9b4653
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
@@ -0,0 +1,279 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test-only PCI driver for the PCI Live Update selftests.
+ *
+ * Binds to Intel 82576 (IGB) devices and lets userspace preserve them across a
+ * Live Update. Userspace gets a file for a bound device from the
+ * /dev/luo_pci_test_driver misc device, and preserves that file in a Live
+ * Update session. Other than enabling the device and bus mastering, the driver
+ * leaves the device alone so that the selftests can check how the PCI core
+ * treats it.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/fs.h>
+#include <linux/liveupdate.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/pci_liveupdate.h>
+#include <linux/uaccess.h>
+
+#include "../pci/luo_pci_test_driver.h"
+
+#define LUPT_FILE_NAME "[luo_pci_test_driver]"
+
+struct lupt_device {
+ bool incoming_at_probe;
+};
+
+static struct pci_driver lupt_driver;
+
+/* Returns a reference to a device bound to this driver, or NULL. */
+static struct pci_dev *lupt_get_bound_device(u32 domain, u16 bdf)
+{
+ struct pci_dev *pdev;
+
+ pdev = pci_get_domain_bus_and_slot(domain, PCI_BUS_NUM(bdf), bdf & 0xff);
+ if (!pdev)
+ return NULL;
+
+ if (READ_ONCE(pdev->driver) != &lupt_driver) {
+ pci_dev_put(pdev);
+ return NULL;
+ }
+
+ return pdev;
+}
+
+static long lupt_device_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev = {};
+ struct pci_dev *pdev = file->private_data;
+ struct lupt_device *ldev;
+
+ if (cmd != LUO_PCI_TEST_GET_STATUS)
+ return -ENOTTY;
+
+ dev.domain = pci_domain_nr(pdev->bus);
+ dev.bdf = pci_dev_id(pdev);
+
+ device_lock(&pdev->dev);
+ if (pdev->driver == &lupt_driver) {
+ ldev = pci_get_drvdata(pdev);
+ if (ldev->incoming_at_probe)
+ dev.flags |= LUO_PCI_TEST_INCOMING_AT_PROBE;
+ }
+ device_unlock(&pdev->dev);
+
+ if (pci_liveupdate_is_incoming(pdev))
+ dev.flags |= LUO_PCI_TEST_INCOMING;
+
+ if (copy_to_user((void __user *)arg, &dev, sizeof(dev)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int lupt_device_release(struct inode *inode, struct file *file)
+{
+ pci_dev_put(file->private_data);
+ return 0;
+}
+
+static const struct file_operations lupt_device_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_device_ioctl,
+ .release = lupt_device_release,
+};
+
+static long lupt_ctl_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev;
+ struct pci_dev *pdev;
+ int fd;
+
+ if (cmd != LUO_PCI_TEST_GET_DEVICE_FD)
+ return -ENOTTY;
+
+ if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
+ return -EFAULT;
+
+ pdev = lupt_get_bound_device(dev.domain, dev.bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ /* On success, releasing the file drops the reference to pdev. */
+ fd = anon_inode_getfd(LUPT_FILE_NAME, &lupt_device_fops, pdev,
+ O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ pci_dev_put(pdev);
+
+ return fd;
+}
+
+static const struct file_operations lupt_ctl_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_ctl_ioctl,
+};
+
+static struct miscdevice lupt_miscdev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "luo_pci_test_driver",
+ .fops = &lupt_ctl_fops,
+};
+
+static bool lupt_can_preserve(struct liveupdate_file_handler *handler,
+ struct file *file)
+{
+ return file->f_op == &lupt_device_fops;
+}
+
+static int lupt_preserve(struct liveupdate_file_op_args *args)
+{
+ struct pci_dev *pdev = args->file->private_data;
+ int ret;
+
+ ret = pci_liveupdate_preserve(pdev);
+ if (ret)
+ return ret;
+
+ args->serialized_data = (u64)pci_domain_nr(pdev->bus) << 16 | pci_dev_id(pdev);
+ return 0;
+}
+
+static void lupt_unpreserve(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_unpreserve(args->file->private_data);
+}
+
+static int lupt_retrieve(struct liveupdate_file_op_args *args)
+{
+ u32 domain = args->serialized_data >> 16;
+ u16 bdf = args->serialized_data & 0xffff;
+ struct pci_dev *pdev;
+ struct file *file;
+
+ /* Userspace must load this driver before retrieving the device. */
+ pdev = lupt_get_bound_device(domain, bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ file = anon_inode_getfile(LUPT_FILE_NAME, &lupt_device_fops, pdev, O_RDWR);
+ if (IS_ERR(file)) {
+ pci_dev_put(pdev);
+ return PTR_ERR(file);
+ }
+
+ args->file = file;
+ return 0;
+}
+
+static bool lupt_can_finish(struct liveupdate_file_op_args *args)
+{
+ return args->retrieve_status > 0;
+}
+
+static void lupt_finish(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_finish(args->file->private_data);
+}
+
+static const struct liveupdate_file_ops lupt_file_ops = {
+ .can_preserve = lupt_can_preserve,
+ .preserve = lupt_preserve,
+ .unpreserve = lupt_unpreserve,
+ .retrieve = lupt_retrieve,
+ .can_finish = lupt_can_finish,
+ .finish = lupt_finish,
+ .owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler lupt_fh = {
+ .ops = &lupt_file_ops,
+ .compatible = "luo-pci-test-driver-v1",
+};
+
+static int lupt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct lupt_device *ldev;
+ int ret;
+
+ ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
+ if (!ldev)
+ return -ENOMEM;
+
+ ldev->incoming_at_probe = pci_liveupdate_is_incoming(pdev);
+
+ ret = pcim_enable_device(pdev);
+ if (ret)
+ return ret;
+
+ pci_set_master(pdev);
+ pci_set_drvdata(pdev, ldev);
+
+ pci_info(pdev, "Bound to %s device\n",
+ ldev->incoming_at_probe ? "incoming-preserved" : "non-preserved");
+ return 0;
+}
+
+static const struct pci_device_id lupt_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x10c9) }, /* 82576 (QEMU igb) */
+ {}
+};
+
+static struct pci_driver lupt_driver = {
+ .name = "luo_pci_test_driver",
+ .id_table = lupt_ids,
+ .probe = lupt_probe,
+};
+
+static int __init lupt_init(void)
+{
+ int ret;
+
+ ret = liveupdate_register_file_handler(&lupt_fh);
+ if (ret)
+ return ret;
+
+ ret = pci_liveupdate_register_flb(&lupt_fh);
+ if (ret)
+ goto err_unregister_fh;
+
+ ret = misc_register(&lupt_miscdev);
+ if (ret)
+ goto err_unregister_flb;
+
+ ret = pci_register_driver(&lupt_driver);
+ if (ret)
+ goto err_deregister_misc;
+
+ return 0;
+
+err_deregister_misc:
+ misc_deregister(&lupt_miscdev);
+err_unregister_flb:
+ pci_liveupdate_unregister_flb(&lupt_fh);
+err_unregister_fh:
+ liveupdate_unregister_file_handler(&lupt_fh);
+ return ret;
+}
+module_init(lupt_init);
+
+static void __exit lupt_exit(void)
+{
+ pci_unregister_driver(&lupt_driver);
+ misc_deregister(&lupt_miscdev);
+ pci_liveupdate_unregister_flb(&lupt_fh);
+ liveupdate_unregister_file_handler(&lupt_fh);
+}
+module_exit(lupt_exit);
+
+MODULE_DESCRIPTION("Test-only PCI driver for the PCI Live Update selftests");
+MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
new file mode 100644
index 000000000000..c5c00e4eda02
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Interface between the PCI Live Update selftests and the luo_pci_test_driver
+ * kernel module.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#ifndef SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+#define SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define LUO_PCI_TEST_DEVICE "/dev/luo_pci_test_driver"
+#define LUO_PCI_TEST_MODULE "/luo_pci_test_driver.ko"
+
+/* The device was incoming-preserved when the driver probed it. */
+#define LUO_PCI_TEST_INCOMING_AT_PROBE (1 << 0)
+/* The device is currently incoming-preserved. */
+#define LUO_PCI_TEST_INCOMING (1 << 1)
+
+/**
+ * struct luo_pci_test_device - A PCI device and its status
+ * @domain: PCI domain (segment) number
+ * @bdf: PCI bus, device and function number
+ * @flags: LUO_PCI_TEST_* flags, set by LUO_PCI_TEST_GET_STATUS
+ */
+struct luo_pci_test_device {
+ __u32 domain;
+ __u16 bdf;
+ __u16 flags;
+};
+
+#define LUO_PCI_TEST_IOC_TYPE 0xB8
+
+/*
+ * Issued on LUO_PCI_TEST_DEVICE. Returns a new file descriptor for a
+ * device bound to luo_pci_test_driver, which can be preserved in a Live
+ * Update session.
+ */
+#define LUO_PCI_TEST_GET_DEVICE_FD \
+ _IOW(LUO_PCI_TEST_IOC_TYPE, 0, struct luo_pci_test_device)
+
+/*
+ * Issued on a device file descriptor. Returns the device's domain, BDF and
+ * LUO_PCI_TEST_* flags.
+ */
+#define LUO_PCI_TEST_GET_STATUS \
+ _IOR(LUO_PCI_TEST_IOC_TYPE, 1, struct luo_pci_test_device)
+
+#endif /* SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H */
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 11/18] selftests/liveupdate: pci: Add a test with an endpoint below a root port
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (9 preceding siblings ...)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 12/18] selftests/liveupdate: pci: Check PCI config writes with QEMU tracing David Matlack
` (6 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add the first PCI Live Update selftest, which preserves a PCI device
across a Live Update in a QEMU VM and checks that the PCI core keeps
it, and the bridges above it, running.
The test builds a topology with two root ports and an IGB endpoint below
each. It preserves one endpoint with the test driver, and binds the
other (the control) to the test driver without preserving it. The next
kernel then checks that:
- The preserved endpoint and the root port above it keep their BDF,
bus numbers, Bus Master Enable, ARI Forwarding Enable and ACS
controls, both before and after the test driver probes and finishes
the endpoint.
- The test driver sees the preserved endpoint as incoming-preserved at
probe time, and no longer after the session is finished.
- The control endpoint had Bus Master Enable cleared during kexec.
The test binary, pci/luo_pci_test, takes the endpoints to preserve and
to use as a control as arguments. Endpoints are named by their path from
the root bus, so that tests do not depend on bus numbers.
pci_vmtest_lib.sh provides helpers to build QEMU topologies and name the
endpoints, for this test and the ones to follow.
Add the Kconfig options that the PCI tests need to the Live Update
selftests' config files.
The test runs on x86_64 (q35), arm64 (virt) and riscv64 (virt). On
arm64 and riscv64, where the generic host bridge driver reassigns all
bus numbers at boot, the bus number checks also show that the next
kernel keeps the bus numbers of the preserved root port.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/Makefile | 1 +
tools/testing/selftests/liveupdate/config | 4 +
.../selftests/liveupdate/config.aarch64 | 1 +
.../selftests/liveupdate/config.riscv64 | 1 +
.../selftests/liveupdate/pci/luo_pci_test.c | 707 ++++++++++++++++++
.../liveupdate/pci/pci_vmtest_lib.sh | 64 ++
.../liveupdate/pci/root_port.vmtest.sh | 7 +
7 files changed, 785 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test.c
create mode 100644 tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
create mode 100644 tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index 2547af3c5353..fd1ee09a0f0a 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -13,6 +13,7 @@ TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
TEST_GEN_PROGS_EXTENDED += luo_multi_session
TEST_GEN_PROGS_EXTENDED += luo_stress_sessions
TEST_GEN_PROGS_EXTENDED += luo_stress_files
+TEST_GEN_PROGS_EXTENDED += pci/luo_pci_test
TEST_FILES += do_kexec.sh
diff --git a/tools/testing/selftests/liveupdate/config b/tools/testing/selftests/liveupdate/config
index 016d009dba13..29fcfbcb7542 100644
--- a/tools/testing/selftests/liveupdate/config
+++ b/tools/testing/selftests/liveupdate/config
@@ -8,5 +8,9 @@ CONFIG_KEXEC_HANDOVER_DEBUG=y
CONFIG_LIVEUPDATE=y
CONFIG_LIVEUPDATE_TEST=y
CONFIG_MEMFD_CREATE=y
+CONFIG_MODULES=y
+CONFIG_PCI=y
+CONFIG_PCIEPORTBUS=y
+CONFIG_PCI_LIVEUPDATE=y
CONFIG_TMPFS=y
CONFIG_SHMEM=y
diff --git a/tools/testing/selftests/liveupdate/config.aarch64 b/tools/testing/selftests/liveupdate/config.aarch64
index 445716403925..3aaa919a80b6 100644
--- a/tools/testing/selftests/liveupdate/config.aarch64
+++ b/tools/testing/selftests/liveupdate/config.aarch64
@@ -1,2 +1,3 @@
+CONFIG_PCI_HOST_GENERIC=y
CONFIG_SERIAL_AMBA_PL011=y
CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
diff --git a/tools/testing/selftests/liveupdate/config.riscv64 b/tools/testing/selftests/liveupdate/config.riscv64
index edbc551199b0..ab5ade373f11 100644
--- a/tools/testing/selftests/liveupdate/config.riscv64
+++ b/tools/testing/selftests/liveupdate/config.riscv64
@@ -1,3 +1,4 @@
+CONFIG_PCI_HOST_GENERIC=y
CONFIG_SERIAL_8250=y
CONFIG_SERIAL_8250_CONSOLE=y
CONFIG_SERIAL_OF_PLATFORM=y
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
new file mode 100644
index 000000000000..7995c9e4252b
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
@@ -0,0 +1,707 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Preserve a PCI device across a Live Update and check that the PCI core keeps
+ * it, and the bridges above it, running.
+ *
+ * The test takes the following options, which must be the same in both
+ * stages except for -s:
+ *
+ * -s <stage> 1 before the Live Update, 2 after it.
+ * -p <path> Endpoint to preserve.
+ * -c <path> Optional. Endpoint to bind to the test driver but not
+ * preserve.
+ *
+ * A <path> identifies a device independently of bus numbers: a device on the
+ * root bus followed by the device.function of each device below it, e.g.
+ * 0000:00:02.0/00.0 is function 0 of device 0 below the bridge at 0000:00:02.0.
+ *
+ * Stage 1 snapshots the config space of the endpoints and of all bridges
+ * above them, and preserves the endpoint. Stage 2 checks the devices
+ * against the snapshot before and after the driver retrieves and
+ * finishes them.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include <linux/pci_regs.h>
+
+#include <libliveupdate.h>
+
+#include "luo_pci_test_driver.h"
+
+#define STATE_SESSION_NAME "lupci_state"
+#define STATE_MEMFD_TOKEN 1
+#define DATA_SESSION_NAME "lupci_data"
+#define DATA_MEMFD_TOKEN 1
+#define DEVICE_SESSION_NAME "lupci_devices"
+#define DEVICE_TOKEN 1
+
+#define CONFIG_SIZE 4096
+#define MAX_DEVICES 32
+#define BDF_LEN 12 /* DDDD:BB:dd.f */
+
+enum role {
+ ROLE_PRESERVED,
+ ROLE_CONTROL,
+};
+
+struct pci_snapshot {
+ char bdf[BDF_LEN + 1];
+ enum role role;
+ bool endpoint; /* Named by a path, not an upstream bridge */
+ bool bridge;
+ uint16_t vendor;
+ uint16_t device;
+ uint16_t command;
+ uint8_t secondary;
+ uint8_t subordinate;
+ uint16_t pcie; /* Capability offsets, 0 if not present */
+ uint16_t acs;
+ uint16_t devctl2;
+ uint16_t acs_ctrl;
+};
+
+static struct pci_snapshot devices[MAX_DEVICES];
+static int nr_devices;
+
+/* Stage 2 snapshot, taken before the test driver is loaded. */
+static struct pci_snapshot before[MAX_DEVICES];
+static int nr_before;
+
+static const char *opt_preserve = "";
+static const char *opt_control = "";
+
+/* strcspn(), which nolibc lacks. */
+static size_t span_until(const char *s, const char *reject)
+{
+ size_t len = 0;
+
+ while (s[len] && !strchr(reject, s[len]))
+ len++;
+
+ return len;
+}
+
+static int parse_args(int argc, char *argv[])
+{
+ int stage = 1;
+ int opt;
+
+ while ((opt = getopt(argc, argv, "s:p:c:")) != -1) {
+ switch (opt) {
+ case 's':
+ stage = atoi(optarg);
+ break;
+ case 'p':
+ opt_preserve = optarg;
+ break;
+ case 'c':
+ opt_control = optarg;
+ break;
+ default:
+ ksft_exit_fail_msg("Unknown option\n");
+ }
+ }
+
+ if (stage != 1 && stage != 2)
+ ksft_exit_fail_msg("Invalid stage (-s)\n");
+ if (!opt_preserve[0])
+ ksft_exit_fail_msg("No device to preserve (-p)\n");
+
+ return stage;
+}
+
+static void mount_sysfs(void)
+{
+ mkdir("/sys", 0755);
+ if (mount("sysfs", "/sys", "sysfs", 0, NULL) < 0 && errno != EBUSY)
+ fail_exit("mount sysfs");
+}
+
+static void load_module(void)
+{
+ int fd, ret;
+
+ fd = open(LUO_PCI_TEST_MODULE, O_RDONLY);
+ if (fd < 0)
+ fail_exit("open %s", LUO_PCI_TEST_MODULE);
+
+ ret = syscall(__NR_finit_module, fd, "", 0);
+ if (ret < 0 && errno != EEXIST)
+ fail_exit("finit_module %s", LUO_PCI_TEST_MODULE);
+
+ close(fd);
+}
+
+static int read_config(const char *bdf, uint8_t *cfg)
+{
+ char path[64];
+ ssize_t n;
+ int fd, len = 0;
+
+ memset(cfg, 0, CONFIG_SIZE);
+
+ snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/config", bdf);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ while (len < CONFIG_SIZE) {
+ n = read(fd, cfg + len, CONFIG_SIZE - len);
+ if (n <= 0)
+ break;
+ len += n;
+ }
+
+ close(fd);
+ return len;
+}
+
+static uint16_t cfg16(const uint8_t *cfg, int pos)
+{
+ return cfg[pos] | cfg[pos + 1] << 8;
+}
+
+static uint32_t cfg32(const uint8_t *cfg, int pos)
+{
+ return cfg16(cfg, pos) | (uint32_t)cfg16(cfg, pos + 2) << 16;
+}
+
+static uint16_t find_cap(const uint8_t *cfg, uint8_t id)
+{
+ int ttl = 48;
+ uint8_t pos;
+
+ if (!(cfg16(cfg, PCI_STATUS) & PCI_STATUS_CAP_LIST))
+ return 0;
+
+ pos = cfg[PCI_CAPABILITY_LIST] & ~3;
+ while (pos && ttl--) {
+ if (cfg[pos + PCI_CAP_LIST_ID] == id)
+ return pos;
+ pos = cfg[pos + PCI_CAP_LIST_NEXT] & ~3;
+ }
+
+ return 0;
+}
+
+static uint16_t find_ext_cap(const uint8_t *cfg, int len, uint16_t id)
+{
+ int ttl = (CONFIG_SIZE - PCI_CFG_SPACE_SIZE) / 8;
+ uint16_t pos = PCI_CFG_SPACE_SIZE;
+ uint32_t header;
+
+ while (pos && pos + 4 <= len && ttl--) {
+ header = cfg32(cfg, pos);
+ if (!header)
+ return 0;
+ if (PCI_EXT_CAP_ID(header) == id)
+ return pos;
+ pos = PCI_EXT_CAP_NEXT(header);
+ }
+
+ return 0;
+}
+
+static void take_snapshot(struct pci_snapshot *dev)
+{
+ uint8_t cfg[CONFIG_SIZE];
+ int len;
+
+ len = read_config(dev->bdf, cfg);
+ if (len < PCI_CFG_SPACE_SIZE)
+ fail_exit("Failed to read config space of %s", dev->bdf);
+
+ dev->vendor = cfg16(cfg, PCI_VENDOR_ID);
+ dev->device = cfg16(cfg, PCI_DEVICE_ID);
+ dev->command = cfg16(cfg, PCI_COMMAND);
+ dev->bridge = (cfg[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MASK) == PCI_HEADER_TYPE_BRIDGE;
+ dev->secondary = dev->bridge ? cfg[PCI_SECONDARY_BUS] : 0;
+ dev->subordinate = dev->bridge ? cfg[PCI_SUBORDINATE_BUS] : 0;
+
+ dev->pcie = find_cap(cfg, PCI_CAP_ID_EXP);
+ dev->devctl2 = dev->pcie ? cfg16(cfg, dev->pcie + PCI_EXP_DEVCTL2) : 0;
+
+ dev->acs = find_ext_cap(cfg, len, PCI_EXT_CAP_ID_ACS);
+ /* Ignore a truncated ACS capability. */
+ if (dev->acs + PCI_ACS_CTRL + 2 > len)
+ dev->acs = 0;
+ dev->acs_ctrl = dev->acs ? cfg16(cfg, dev->acs + PCI_ACS_CTRL) : 0;
+}
+
+static struct pci_snapshot *find_device(const char *bdf)
+{
+ int i;
+
+ for (i = 0; i < nr_devices; i++) {
+ if (!strcmp(devices[i].bdf, bdf))
+ return &devices[i];
+ }
+
+ return NULL;
+}
+
+static struct pci_snapshot *add_device(const char *bdf, enum role role)
+{
+ struct pci_snapshot *dev = find_device(bdf);
+
+ if (dev) {
+ /* A bridge shared with a preserved endpoint is preserved. */
+ if (role == ROLE_PRESERVED)
+ dev->role = ROLE_PRESERVED;
+ return dev;
+ }
+
+ if (nr_devices == MAX_DEVICES)
+ fail_exit("Too many devices");
+
+ dev = &devices[nr_devices++];
+ memset(dev, 0, sizeof(*dev));
+ snprintf(dev->bdf, sizeof(dev->bdf), "%s", bdf);
+ dev->role = role;
+ return dev;
+}
+
+/* Find the device at @devfn (e.g. "00.0") on the secondary bus of @parent. */
+static void find_child(const char *parent, const char *devfn, size_t devfn_len,
+ char *child)
+{
+ uint8_t cfg[CONFIG_SIZE];
+ char sysfs[64];
+
+ if (devfn_len != 4 || devfn[2] != '.')
+ fail_exit("Invalid device.function %.*s", (int)devfn_len, devfn);
+
+ if (read_config(parent, cfg) < PCI_CFG_SPACE_SIZE)
+ fail_exit("Failed to read config space of %s", parent);
+
+ if ((cfg[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_BRIDGE)
+ fail_exit("%s is not a bridge", parent);
+
+ /* A bridge's secondary bus must be numbered above its own bus. */
+ if (cfg[PCI_SECONDARY_BUS] <= strtoul(parent + 5, NULL, 16))
+ fail_exit("%s has an invalid secondary bus number (0x%02x)", parent,
+ cfg[PCI_SECONDARY_BUS]);
+
+ snprintf(child, BDF_LEN + 1, "%04lx:%02x:", strtoul(parent, NULL, 16),
+ cfg[PCI_SECONDARY_BUS]);
+ memcpy(child + 8, devfn, devfn_len);
+ child[BDF_LEN] = 0;
+
+ snprintf(sysfs, sizeof(sysfs), "/sys/bus/pci/devices/%s", child);
+ if (access(sysfs, F_OK))
+ fail_exit("No device %.*s below %s", (int)devfn_len, devfn, parent);
+}
+
+/* Add every device along @path, and mark the last one as an endpoint. */
+static void add_path(const char *path, enum role role)
+{
+ char bdf[BDF_LEN + 1];
+ const char *p = path;
+ char sysfs[64];
+ size_t n;
+
+ n = span_until(p, "/");
+ if (n != BDF_LEN)
+ fail_exit("Invalid device path %s", path);
+
+ memcpy(bdf, p, BDF_LEN);
+ bdf[BDF_LEN] = 0;
+
+ snprintf(sysfs, sizeof(sysfs), "/sys/bus/pci/devices/%s", bdf);
+ if (access(sysfs, F_OK))
+ fail_exit("No device %s", bdf);
+
+ add_device(bdf, role);
+ p += n;
+
+ while (*p == '/') {
+ char child[BDF_LEN + 1];
+
+ p++;
+ n = span_until(p, "/");
+ find_child(bdf, p, n, child);
+ memcpy(bdf, child, sizeof(bdf));
+ add_device(bdf, role);
+ p += n;
+ }
+
+ find_device(bdf)->endpoint = true;
+}
+
+static int format_snapshot(const struct pci_snapshot *dev, char *buf, size_t size)
+{
+ return snprintf(buf, size,
+ "bdf=%s role=%s endpoint=%d bridge=%d id=0x%04x device=0x%04x "
+ "cmd=0x%04x sec=0x%02x sub=0x%02x pcie=0x%x acs=0x%x "
+ "devctl2=0x%04x acsctrl=0x%04x\n",
+ dev->bdf, dev->role == ROLE_PRESERVED ? "preserved" : "control",
+ dev->endpoint, dev->bridge, dev->vendor, dev->device,
+ dev->command, dev->secondary, dev->subordinate, dev->pcie,
+ dev->acs, dev->devctl2, dev->acs_ctrl);
+}
+
+static unsigned long get_field(const char *line, const char *key)
+{
+ const char *p = strstr(line, key);
+
+ if (!p)
+ fail_exit("Snapshot is missing %s", key);
+
+ return strtoul(p + strlen(key), NULL, 0);
+}
+
+static void parse_snapshots(const char *text)
+{
+ struct pci_snapshot *dev;
+ const char *p = text;
+ char line[512];
+ size_t len;
+
+ while (*p) {
+ len = span_until(p, "\n");
+ if (len >= sizeof(line))
+ fail_exit("Snapshot line is too long");
+
+ memcpy(line, p, len);
+ line[len] = 0;
+ p += len;
+ if (*p == '\n')
+ p++;
+
+ if (!strstr(line, "bdf="))
+ continue;
+
+ if (nr_devices == MAX_DEVICES)
+ fail_exit("Too many devices in snapshot");
+
+ dev = &devices[nr_devices++];
+ memset(dev, 0, sizeof(*dev));
+ memcpy(dev->bdf, strstr(line, "bdf=") + 4, BDF_LEN);
+ dev->role = strstr(line, "role=preserved") ? ROLE_PRESERVED : ROLE_CONTROL;
+ dev->endpoint = get_field(line, "endpoint=");
+ dev->bridge = get_field(line, "bridge=");
+ dev->vendor = get_field(line, "id=");
+ dev->device = get_field(line, "device=");
+ dev->command = get_field(line, "cmd=");
+ dev->secondary = get_field(line, "sec=");
+ dev->subordinate = get_field(line, "sub=");
+ dev->pcie = get_field(line, "pcie=");
+ dev->acs = get_field(line, "acs=");
+ dev->devctl2 = get_field(line, "devctl2=");
+ dev->acs_ctrl = get_field(line, "acsctrl=");
+ }
+}
+
+static int get_device_fd(int ctl_fd, const char *bdf)
+{
+ struct luo_pci_test_device dev = {};
+ unsigned int domain, bus, slot, func;
+ int fd;
+
+ domain = strtoul(bdf, NULL, 16);
+ bus = strtoul(bdf + 5, NULL, 16);
+ slot = strtoul(bdf + 8, NULL, 16);
+ func = strtoul(bdf + 11, NULL, 16);
+
+ dev.domain = domain;
+ dev.bdf = bus << 8 | slot << 3 | func;
+
+ fd = ioctl(ctl_fd, LUO_PCI_TEST_GET_DEVICE_FD, &dev);
+ if (fd < 0)
+ fail_exit("Failed to get a file for %s. Is it bound to the test driver?", bdf);
+
+ return fd;
+}
+
+static uint16_t get_status(int fd, const char *bdf)
+{
+ struct luo_pci_test_device dev = {};
+ char actual[BDF_LEN + 1];
+
+ if (ioctl(fd, LUO_PCI_TEST_GET_STATUS, &dev) < 0)
+ fail_exit("LUO_PCI_TEST_GET_STATUS for %s", bdf);
+
+ snprintf(actual, sizeof(actual), "%04x:%02x:%02x.%x", dev.domain,
+ dev.bdf >> 8, (dev.bdf >> 3) & 0x1f, dev.bdf & 7);
+ if (strcmp(actual, bdf))
+ fail_exit("Expected device %s, got %s", bdf, actual);
+
+ return dev.flags;
+}
+
+static int nr_failures;
+
+#define check(cond, fmt, ...) do { \
+ if (!(cond)) { \
+ ksft_print_msg("FAIL: " fmt "\n", ##__VA_ARGS__); \
+ nr_failures++; \
+ } \
+} while (0)
+
+/*
+ * Check the devices against the stage 1 snapshot. If @taken is NULL, check
+ * their current state. Otherwise check the snapshots in @taken, which were
+ * taken in the same order as the stage 1 snapshot.
+ */
+static void check_devices(const char *when, const struct pci_snapshot *taken)
+{
+ struct pci_snapshot now;
+ int i;
+
+ ksft_print_msg("[STAGE 2] Checking devices %s\n", when);
+
+ for (i = 0; i < nr_devices; i++) {
+ const struct pci_snapshot *dev = &devices[i];
+
+ if (taken) {
+ now = taken[i];
+ check(!strcmp(now.bdf, dev->bdf), "%s: Device moved to %s",
+ dev->bdf, now.bdf);
+ } else {
+ now = *dev;
+ take_snapshot(&now);
+ }
+
+ check(now.vendor == dev->vendor && now.device == dev->device,
+ "%s: ID changed from %04x:%04x to %04x:%04x", dev->bdf,
+ dev->vendor, dev->device, now.vendor, now.device);
+
+ if (dev->role == ROLE_CONTROL) {
+ if (dev->endpoint && taken)
+ check(!(now.command & PCI_COMMAND_MASTER),
+ "%s: Bus Master still enabled on control device",
+ dev->bdf);
+ continue;
+ }
+
+ check(now.command & PCI_COMMAND_MASTER,
+ "%s: Bus Master disabled (Command 0x%04x)", dev->bdf, now.command);
+
+ if (dev->bridge)
+ check(now.secondary == dev->secondary &&
+ now.subordinate == dev->subordinate,
+ "%s: Bus numbers changed from [%02x-%02x] to [%02x-%02x]",
+ dev->bdf, dev->secondary, dev->subordinate,
+ now.secondary, now.subordinate);
+
+ if (dev->bridge && dev->pcie)
+ check((now.devctl2 & PCI_EXP_DEVCTL2_ARI) ==
+ (dev->devctl2 & PCI_EXP_DEVCTL2_ARI),
+ "%s: ARI Forwarding Enable changed (DEVCTL2 0x%04x -> 0x%04x)",
+ dev->bdf, dev->devctl2, now.devctl2);
+
+ if (dev->acs)
+ check(now.acs_ctrl == dev->acs_ctrl,
+ "%s: ACS controls changed from 0x%04x to 0x%04x",
+ dev->bdf, dev->acs_ctrl, now.acs_ctrl);
+ }
+
+ if (nr_failures)
+ ksft_exit_fail_msg("%d check(s) failed %s\n", nr_failures, when);
+}
+
+static void run_stage_1(int luo_fd)
+{
+ char text[4096] = {};
+ int session_fd, ctl_fd, fd, i;
+ size_t len = 0;
+
+ ksft_print_msg("[STAGE 1] Starting pre-kexec setup...\n");
+
+ for (i = 0; i < nr_devices; i++) {
+ struct pci_snapshot *dev = &devices[i];
+
+ if (dev->role == ROLE_PRESERVED || dev->endpoint) {
+ if (!(dev->command & PCI_COMMAND_MASTER))
+ fail_exit("%s: Bus Master is not enabled", dev->bdf);
+ }
+
+ len += format_snapshot(dev, text + len, sizeof(text) - len);
+ if (len >= sizeof(text))
+ fail_exit("Snapshot is too large");
+ }
+
+ create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2);
+
+ session_fd = luo_create_session(luo_fd, DATA_SESSION_NAME);
+ if (session_fd < 0)
+ fail_exit("luo_create_session %s", DATA_SESSION_NAME);
+ if (create_and_preserve_memfd(session_fd, DATA_MEMFD_TOKEN, text) < 0)
+ fail_exit("Failed to preserve snapshot");
+
+ session_fd = luo_create_session(luo_fd, DEVICE_SESSION_NAME);
+ if (session_fd < 0)
+ fail_exit("luo_create_session %s", DEVICE_SESSION_NAME);
+
+ ctl_fd = open(LUO_PCI_TEST_DEVICE, O_RDWR);
+ if (ctl_fd < 0)
+ fail_exit("open %s", LUO_PCI_TEST_DEVICE);
+
+ for (i = 0; i < nr_devices; i++) {
+ struct pci_snapshot *dev = &devices[i];
+
+ if (dev->role != ROLE_PRESERVED || !dev->endpoint)
+ continue;
+
+ ksft_print_msg("[STAGE 1] Preserving %s\n", dev->bdf);
+ fd = get_device_fd(ctl_fd, dev->bdf);
+ if (luo_session_preserve_fd(session_fd, fd, DEVICE_TOKEN) < 0)
+ fail_exit("Failed to preserve %s", dev->bdf);
+ close(fd);
+ }
+
+ close(ctl_fd);
+
+ close(luo_fd);
+ daemonize_and_wait();
+}
+
+static void run_stage_2(int luo_fd, int state_session_fd)
+{
+ int fds[MAX_DEVICES] = {};
+ int session_fd, ctl_fd, mfd, stage, i;
+ char text[4096] = {};
+ uint16_t flags;
+
+ ksft_print_msg("[STAGE 2] Starting post-kexec verification...\n");
+
+ restore_and_read_stage(state_session_fd, STATE_MEMFD_TOKEN, &stage);
+ if (stage != 2)
+ fail_exit("Expected stage 2, but state file contains %d", stage);
+
+ session_fd = luo_retrieve_session(luo_fd, DATA_SESSION_NAME);
+ if (session_fd < 0)
+ fail_exit("luo_retrieve_session %s", DATA_SESSION_NAME);
+
+ mfd = restore_and_verify_memfd(session_fd, DATA_MEMFD_TOKEN, NULL);
+ if (mfd < 0)
+ fail_exit("Failed to restore snapshot");
+ if (read(mfd, text, sizeof(text) - 1) < 0)
+ fail_exit("Failed to read snapshot");
+ close(mfd);
+
+ parse_snapshots(text);
+ if (luo_session_finish(session_fd) < 0)
+ fail_exit("luo_session_finish %s", DATA_SESSION_NAME);
+ close(session_fd);
+
+ if (nr_devices != nr_before)
+ fail_exit("Expected %d devices, found %d", nr_devices, nr_before);
+
+ check_devices("before probe", before);
+
+ session_fd = luo_retrieve_session(luo_fd, DEVICE_SESSION_NAME);
+ if (session_fd < 0)
+ fail_exit("luo_retrieve_session %s", DEVICE_SESSION_NAME);
+
+ ctl_fd = open(LUO_PCI_TEST_DEVICE, O_RDWR);
+ if (ctl_fd < 0)
+ fail_exit("open %s", LUO_PCI_TEST_DEVICE);
+
+ for (i = 0; i < nr_devices; i++) {
+ struct pci_snapshot *dev = &devices[i];
+
+ if (!dev->endpoint)
+ continue;
+
+ if (dev->role == ROLE_CONTROL) {
+ fds[i] = get_device_fd(ctl_fd, dev->bdf);
+ flags = get_status(fds[i], dev->bdf);
+ check(!(flags & LUO_PCI_TEST_INCOMING_AT_PROBE),
+ "%s: Control device was incoming-preserved", dev->bdf);
+ continue;
+ }
+
+ ksft_print_msg("[STAGE 2] Retrieving %s\n", dev->bdf);
+ fds[i] = luo_session_retrieve_fd(session_fd, DEVICE_TOKEN);
+ if (fds[i] < 0)
+ fail_exit("Failed to retrieve %s", dev->bdf);
+
+ flags = get_status(fds[i], dev->bdf);
+ check(flags & LUO_PCI_TEST_INCOMING_AT_PROBE,
+ "%s: Device was not incoming-preserved at probe", dev->bdf);
+ check(flags & LUO_PCI_TEST_INCOMING,
+ "%s: Device is not incoming-preserved", dev->bdf);
+ }
+
+ check_devices("after probe", NULL);
+
+ ksft_print_msg("[STAGE 2] Finishing %s...\n", DEVICE_SESSION_NAME);
+ if (luo_session_finish(session_fd) < 0)
+ fail_exit("luo_session_finish %s", DEVICE_SESSION_NAME);
+
+ for (i = 0; i < nr_devices; i++) {
+ struct pci_snapshot *dev = &devices[i];
+
+ if (!dev->endpoint || dev->role != ROLE_PRESERVED)
+ continue;
+
+ flags = get_status(fds[i], dev->bdf);
+ check(!(flags & LUO_PCI_TEST_INCOMING),
+ "%s: Device is still incoming-preserved after finish", dev->bdf);
+ }
+
+ check_devices("after finish", NULL);
+
+ if (luo_session_finish(state_session_fd) < 0)
+ fail_exit("luo_session_finish %s", STATE_SESSION_NAME);
+
+ ksft_print_msg("\n--- PCI LIVE UPDATE TEST PASSED ---\n");
+}
+
+int main(int argc, char *argv[])
+{
+ int stage = parse_args(argc, argv);
+ char stage_arg[] = { '0' + stage, 0 };
+ char *luo_argv[] = { argv[0], "-s", stage_arg, NULL };
+ int i;
+
+ mount_sysfs();
+
+ add_path(opt_preserve, ROLE_PRESERVED);
+ if (opt_control[0])
+ add_path(opt_control, ROLE_CONTROL);
+
+ /*
+ * In stage 2, snapshot the devices before the test driver is loaded to
+ * see how the PCI core left them after enumeration. They are checked
+ * against the stage 1 snapshot once that is retrieved.
+ */
+ if (stage == 2) {
+ for (i = 0; i < nr_devices; i++)
+ take_snapshot(&devices[i]);
+
+ memcpy(before, devices, sizeof(devices));
+ nr_before = nr_devices;
+ nr_devices = 0;
+ }
+
+ /*
+ * LUO deserializes all preserved files when /dev/liveupdate is first
+ * opened, which requires the test driver's file handler.
+ */
+ load_module();
+
+ /* In stage 1, snapshot the devices once the test driver is bound. */
+ if (stage == 1) {
+ for (i = 0; i < nr_devices; i++)
+ take_snapshot(&devices[i]);
+ }
+
+ /* luo_test() only takes -s. */
+ return luo_test(3, luo_argv, STATE_SESSION_NAME, run_stage_1, run_stage_2);
+}
diff --git a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
new file mode 100644
index 000000000000..ebed414510f4
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
@@ -0,0 +1,64 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Common setup for the PCI Live Update selftests.
+#
+# Sourced by pci/*.vmtest.sh, which are themselves sourced by ../vmtest.sh
+# after $arch, $target, $test_dir and $workspace_dir are set. See vmtest.sh for
+# the TEST_* variables.
+#
+# Each test builds a PCIe topology out of the helpers below, using the QEMU
+# "igb" device as the endpoint, and names the endpoints to preserve and the
+# control endpoints (bound to the test driver but not preserved) by their path
+# from the root bus. See luo_pci_test.c for the path format.
+
+PCI_CHASSIS=0
+
+TEST_BINARY=pci/luo_pci_test
+TEST_MODULES=(luo_pci_test_driver)
+TEST_TIMEOUT="180s"
+
+TEST_QEMU_ARGS=()
+
+# The default x86 machine (pc) does not support PCIe.
+if [[ "$arch" == "x86" ]]; then
+ TEST_QEMU_ARGS+=(-M q35)
+fi
+
+# pci_root_port <id> <addr>
+# Add a root port at device <addr> on the root bus.
+function pci_root_port() {
+ PCI_CHASSIS=$((PCI_CHASSIS + 1))
+ TEST_QEMU_ARGS+=(-device "pcie-root-port,id=$1,bus=pcie.0,addr=$2,chassis=$PCI_CHASSIS")
+}
+
+# pci_igb <id> <bus> [<addr>]
+# Add an IGB endpoint at device <addr> (default 0) on <bus>.
+function pci_igb() {
+ TEST_QEMU_ARGS+=(-device "igb,id=$1,bus=$2,addr=${3:-0}")
+}
+
+# pci_preserve <path>
+# Preserve the endpoint at <path>.
+function pci_preserve() {
+ TEST_ARGS+=(-p "$1")
+}
+
+# pci_control <path>
+# Bind the endpoint at <path> to the test driver, but don't preserve it.
+# Bus Master must be cleared on it during kexec.
+function pci_control() {
+ TEST_ARGS+=(-c "$1")
+}
+
+# pci_topology_root_ports
+# Two root ports, with an endpoint to preserve below one and a control
+# endpoint below the other.
+function pci_topology_root_ports() {
+ pci_root_port rp1 0x2
+ pci_igb igb1 rp1
+ pci_root_port rp2 0x3
+ pci_igb igb2 rp2
+
+ pci_preserve 0000:00:02.0/00.0
+ pci_control 0000:00:03.0/00.0
+}
diff --git a/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh b/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh
new file mode 100644
index 000000000000..05af30ddb296
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Preserve an endpoint below a root port.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+pci_topology_root_ports
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 12/18] selftests/liveupdate: pci: Check PCI config writes with QEMU tracing
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (10 preceding siblings ...)
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 ` 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
` (5 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Trace PCI config writes in QEMU, and check that none of them disrupts a
preserved device, or a bridge above it, between the end of stage 1 and
the end of stage 2, i.e. while the previous kernel shuts down, the next
kernel enumerates PCI, and the test driver probes and finishes the
device.
The checks in the next kernel only see the end state. The trace also
catches transient changes, e.g. Bus Master Enable being cleared and set
again, and resets that leave no trace in the end state, e.g. a
Secondary Bus Reset or Function Level Reset.
The test binary prints a manifest of the preserved and control
endpoints and the bridges above them to the console, and marks the
phases of the test by writing to the config space of a pci-testdev
device, which shows up in the trace. A new script, check_cfg_trace.py,
checks the trace against the manifest. As a positive control, it also
requires the trace to show Bus Master Enable being cleared on the
control endpoint during kexec, which shows that it is looking at the
right devices and phase.
Skip the PCI tests if python3 is not installed, and allow *.py in
.gitignore.
Signed-off-by: David Matlack <dmatlack@google.com>
---
tools/testing/selftests/liveupdate/.gitignore | 1 +
.../liveupdate/pci/check_cfg_trace.py | 238 ++++++++++++++++++
.../selftests/liveupdate/pci/luo_pci_test.c | 54 +++-
.../liveupdate/pci/pci_vmtest_lib.sh | 26 +-
4 files changed, 314 insertions(+), 5 deletions(-)
create mode 100755 tools/testing/selftests/liveupdate/pci/check_cfg_trace.py
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
index 9bf744e4f138..bdd6bfad9cc9 100644
--- a/tools/testing/selftests/liveupdate/.gitignore
+++ b/tools/testing/selftests/liveupdate/.gitignore
@@ -4,6 +4,7 @@
!*.c
!*.h
!*.mk
+!*.py
!*.sh
!.gitignore
!config
diff --git a/tools/testing/selftests/liveupdate/pci/check_cfg_trace.py b/tools/testing/selftests/liveupdate/pci/check_cfg_trace.py
new file mode 100755
index 000000000000..4000cc63f346
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/check_cfg_trace.py
@@ -0,0 +1,238 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (c) 2026, Google LLC.
+# David Matlack <dmatlack@google.com>
+"""Check PCI config writes against the devices preserved by luo_pci_test.
+
+QEMU records every PCI config write with the pci_cfg_write trace event:
+
+ pci_cfg_write igb 01:00.0 @0x4 <- 0x407
+
+luo_pci_test prints a manifest of the preserved and control endpoints and the
+bridges above them in stage 1, and writes markers to the config space of a
+marker device:
+
+ A: end of stage 1 (devices preserved, about to kexec)
+ B: start of the stage 2 test binary
+ C: end of stage 2 (devices finished)
+
+Between A and C, i.e. during kexec, the next kernel's enumeration and the
+driver's probe, no config write may disrupt a preserved device:
+
+ - Bus Master Enable must not be cleared
+ - The secondary and subordinate bus numbers of bridges must not change
+ - Secondary Bus Reset must not be set on bridges
+ - Function Level Reset must not be initiated
+ - ARI Forwarding Enable on bridges must not change
+ - The ACS controls must not change
+
+The trace only records the offset and value of each write, not its size. The
+checks assume naturally aligned writes of the sizes the PCI core uses for
+these registers.
+
+As a positive control, each control endpoint (bound to the test driver but not
+preserved) must have Bus Master Enable cleared between A and B, by the
+previous kernel during kexec.
+"""
+
+import argparse
+import re
+import sys
+
+MANIFEST_PREFIX = "LUPCI-MANIFEST: "
+MARKER_OFFSET = 0x40
+MARKERS = {0x4C550001: "A", 0x4C550002: "B", 0x4C550003: "C"}
+
+PCI_COMMAND = 0x04
+PCI_COMMAND_MASTER = 0x4
+PCI_PRIMARY_BUS = 0x18
+PCI_SECONDARY_BUS = 0x19
+PCI_SUBORDINATE_BUS = 0x1A
+PCI_INTERRUPT_LINE = 0x3C
+PCI_BRIDGE_CONTROL = 0x3E
+PCI_BRIDGE_CTL_BUS_RESET = 0x40
+PCI_EXP_DEVCTL = 0x08
+PCI_EXP_DEVCTL_BCR_FLR = 0x8000
+PCI_EXP_DEVCTL2 = 0x28
+PCI_EXP_DEVCTL2_ARI = 0x20
+PCI_ACS_CAP = 0x04
+PCI_ACS_CTRL = 0x06
+
+TRACE_RE = re.compile(
+ r"pci_cfg_write (\S+) ([0-9a-f]{2}):([0-9a-f]{2})\.([0-7]) "
+ r"@0x([0-9a-f]+) <- 0x([0-9a-f]+)"
+)
+
+
+class Device:
+ def __init__(self, fields):
+ self.bdf = fields["bdf"]
+ domain, bus, devfn = self.bdf.split(":")
+ slot, func = devfn.split(".")
+ if int(domain, 16) != 0:
+ raise ValueError(f"{self.bdf}: only domain 0 is supported")
+ self.key = (int(bus, 16), int(slot, 16), int(func, 16))
+ self.role = fields["role"]
+ self.endpoint = int(fields["endpoint"]) != 0
+ self.bridge = int(fields["bridge"]) != 0
+ self.secondary = int(fields["sec"], 0)
+ self.subordinate = int(fields["sub"], 0)
+ self.pcie = int(fields["pcie"], 0)
+ self.acs = int(fields["acs"], 0)
+ self.devctl2 = int(fields["devctl2"], 0)
+ self.acs_ctrl = int(fields["acsctrl"], 0)
+ self.nr_writes = 0
+ self.cleared_master = False
+
+
+def parse_manifest(path):
+ devices = {}
+ with open(path, errors="replace") as f:
+ for line in f:
+ idx = line.find(MANIFEST_PREFIX)
+ if idx < 0:
+ continue
+ fields = dict(
+ kv.split("=", 1)
+ for kv in line[idx + len(MANIFEST_PREFIX):].split()
+ if "=" in kv
+ )
+ dev = Device(fields)
+ devices[dev.key] = dev
+ return devices
+
+
+def parse_trace(path):
+ writes = []
+ with open(path, errors="replace") as f:
+ for line in f:
+ m = TRACE_RE.search(line)
+ if not m:
+ continue
+ name, bus, slot, func, offset, value = m.groups()
+ writes.append(
+ (
+ name,
+ (int(bus, 16), int(slot, 16), int(func, 16)),
+ int(offset, 16),
+ int(value, 16),
+ )
+ )
+ return writes
+
+
+def check_write(dev, offset, value):
+ """Return a description of how the write disrupts dev, or None."""
+ if offset == PCI_COMMAND and not value & PCI_COMMAND_MASTER:
+ return "clears Bus Master Enable"
+
+ if dev.bridge:
+ if offset == PCI_PRIMARY_BUS:
+ secondary = (value >> 8) & 0xFF
+ subordinate = (value >> 16) & 0xFF
+ elif offset == PCI_SECONDARY_BUS:
+ secondary, subordinate = value & 0xFF, dev.subordinate
+ elif offset == PCI_SUBORDINATE_BUS:
+ secondary, subordinate = dev.secondary, value & 0xFF
+ else:
+ secondary, subordinate = dev.secondary, dev.subordinate
+
+ if (secondary, subordinate) != (dev.secondary, dev.subordinate):
+ return (
+ f"changes bus numbers from [{dev.secondary:02x}-{dev.subordinate:02x}] "
+ f"to [{secondary:02x}-{subordinate:02x}]"
+ )
+
+ if offset == PCI_BRIDGE_CONTROL and value & PCI_BRIDGE_CTL_BUS_RESET:
+ return "sets Secondary Bus Reset"
+ if offset == PCI_INTERRUPT_LINE and (value >> 16) & PCI_BRIDGE_CTL_BUS_RESET:
+ return "sets Secondary Bus Reset"
+
+ if dev.pcie:
+ if offset == dev.pcie + PCI_EXP_DEVCTL and value & PCI_EXP_DEVCTL_BCR_FLR:
+ return "initiates Function Level Reset"
+ if (
+ dev.bridge
+ and offset == dev.pcie + PCI_EXP_DEVCTL2
+ and (value ^ dev.devctl2) & PCI_EXP_DEVCTL2_ARI
+ ):
+ return "changes ARI Forwarding Enable"
+
+ if dev.acs:
+ if offset == dev.acs + PCI_ACS_CTRL:
+ acs_ctrl = value & 0xFFFF
+ elif offset == dev.acs + PCI_ACS_CAP:
+ acs_ctrl = value >> 16
+ else:
+ acs_ctrl = dev.acs_ctrl
+ if acs_ctrl != dev.acs_ctrl:
+ return f"changes ACS controls from 0x{dev.acs_ctrl:04x} to 0x{acs_ctrl:04x}"
+
+ return None
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ parser.add_argument("--serial", required=True, help="Serial console log")
+ parser.add_argument("--trace", required=True, help="QEMU pci_cfg_write trace")
+ args = parser.parse_args()
+
+ devices = parse_manifest(args.serial)
+ if not devices:
+ print("# No device manifest found in the serial log")
+ return 1
+
+ phase = None
+ seen = set()
+ failures = []
+
+ for name, key, offset, value in parse_trace(args.trace):
+ if offset == MARKER_OFFSET and value in MARKERS:
+ phase = MARKERS[value]
+ seen.add(phase)
+ continue
+
+ if phase not in ("A", "B") or key not in devices:
+ continue
+
+ dev = devices[key]
+ if dev.role == "control":
+ if (
+ phase == "A"
+ and dev.endpoint
+ and offset == PCI_COMMAND
+ and not value & PCI_COMMAND_MASTER
+ ):
+ dev.cleared_master = True
+ continue
+
+ dev.nr_writes += 1
+ problem = check_write(dev, offset, value)
+ if problem:
+ failures.append(
+ f"{dev.bdf} ({name}): write 0x{value:x} @0x{offset:x} "
+ f"in phase {phase} {problem}"
+ )
+
+ for marker in MARKERS.values():
+ if marker not in seen:
+ failures.append(f"Marker {marker} not found in the trace")
+
+ for dev in devices.values():
+ if dev.role == "preserved":
+ print(f"# {dev.bdf}: checked {dev.nr_writes} config write(s)")
+ elif dev.endpoint and not dev.cleared_master:
+ failures.append(
+ f"{dev.bdf}: control device did not have Bus Master cleared "
+ "during kexec (is the trace being checked correctly?)"
+ )
+
+ for failure in failures:
+ print(f"# FAIL: {failure}")
+
+ return 1 if failures else 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
index 7995c9e4252b..177562d65e64 100644
--- a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
@@ -10,14 +10,17 @@
* -p <path> Endpoint to preserve.
* -c <path> Optional. Endpoint to bind to the test driver but not
* preserve.
+ * -m <bdf> Optional device whose config space is written to mark the
+ * phases of the test in a QEMU trace of config writes.
*
* A <path> identifies a device independently of bus numbers: a device on the
* root bus followed by the device.function of each device below it, e.g.
* 0000:00:02.0/00.0 is function 0 of device 0 below the bridge at 0000:00:02.0.
*
* Stage 1 snapshots the config space of the endpoints and of all bridges
- * above them, and preserves the endpoint. Stage 2 checks the devices
- * against the snapshot before and after the driver retrieves and
+ * above them, prints the snapshot so that a host-side checker can validate
+ * the QEMU trace, and preserves the endpoint. Stage 2 checks the
+ * devices against the snapshot before and after the driver retrieves and
* finishes them.
*
* Copyright (c) 2026, Google LLC.
@@ -49,6 +52,14 @@
#define DEVICE_SESSION_NAME "lupci_devices"
#define DEVICE_TOKEN 1
+/* Values written to the marker device. Keep in sync with check_cfg_trace.py. */
+#define MARKER_OFFSET 0x40
+#define MARKER_STAGE1_DONE 0x4c550001
+#define MARKER_STAGE2_START 0x4c550002
+#define MARKER_STAGE2_DONE 0x4c550003
+
+#define MANIFEST_PREFIX "LUPCI-MANIFEST: "
+
#define CONFIG_SIZE 4096
#define MAX_DEVICES 32
#define BDF_LEN 12 /* DDDD:BB:dd.f */
@@ -83,6 +94,7 @@ static int nr_before;
static const char *opt_preserve = "";
static const char *opt_control = "";
+static const char *opt_marker = "";
/* strcspn(), which nolibc lacks. */
static size_t span_until(const char *s, const char *reject)
@@ -100,7 +112,7 @@ static int parse_args(int argc, char *argv[])
int stage = 1;
int opt;
- while ((opt = getopt(argc, argv, "s:p:c:")) != -1) {
+ while ((opt = getopt(argc, argv, "s:p:c:m:")) != -1) {
switch (opt) {
case 's':
stage = atoi(optarg);
@@ -111,6 +123,9 @@ static int parse_args(int argc, char *argv[])
case 'c':
opt_control = optarg;
break;
+ case 'm':
+ opt_marker = optarg;
+ break;
default:
ksft_exit_fail_msg("Unknown option\n");
}
@@ -146,6 +161,26 @@ static void load_module(void)
close(fd);
}
+static void write_marker(uint32_t value)
+{
+ char path[64];
+ int fd;
+
+ if (!opt_marker[0])
+ return;
+
+ snprintf(path, sizeof(path), "/sys/bus/pci/devices/%.*s/config", BDF_LEN, opt_marker);
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ fail_exit("open %s", path);
+
+ if (lseek(fd, MARKER_OFFSET, SEEK_SET) != MARKER_OFFSET ||
+ write(fd, &value, sizeof(value)) != sizeof(value))
+ fail_exit("write marker 0x%x to %s", value, path);
+
+ close(fd);
+}
+
static int read_config(const char *bdf, uint8_t *cfg)
{
char path[64];
@@ -535,6 +570,14 @@ static void run_stage_1(int luo_fd)
fail_exit("Snapshot is too large");
}
+ /* Print the snapshot for the host-side checker. */
+ for (i = 0; i < nr_devices; i++) {
+ char line[512];
+
+ format_snapshot(&devices[i], line, sizeof(line));
+ ksft_print_msg(MANIFEST_PREFIX "%s", line);
+ }
+
create_state_file(luo_fd, STATE_SESSION_NAME, STATE_MEMFD_TOKEN, 2);
session_fd = luo_create_session(luo_fd, DATA_SESSION_NAME);
@@ -565,6 +608,7 @@ static void run_stage_1(int luo_fd)
}
close(ctl_fd);
+ write_marker(MARKER_STAGE1_DONE);
close(luo_fd);
daemonize_and_wait();
@@ -660,6 +704,7 @@ static void run_stage_2(int luo_fd, int state_session_fd)
if (luo_session_finish(state_session_fd) < 0)
fail_exit("luo_session_finish %s", STATE_SESSION_NAME);
+ write_marker(MARKER_STAGE2_DONE);
ksft_print_msg("\n--- PCI LIVE UPDATE TEST PASSED ---\n");
}
@@ -672,6 +717,9 @@ int main(int argc, char *argv[])
mount_sysfs();
+ if (stage == 2)
+ write_marker(MARKER_STAGE2_START);
+
add_path(opt_preserve, ROLE_PRESERVED);
if (opt_control[0])
add_path(opt_control, ROLE_CONTROL);
diff --git a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
index ebed414510f4..5babeb19fbc1 100644
--- a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
+++ b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
@@ -10,20 +10,36 @@
# "igb" device as the endpoint, and names the endpoints to preserve and the
# control endpoints (bound to the test driver but not preserved) by their path
# from the root bus. See luo_pci_test.c for the path format.
+#
+# QEMU traces every PCI config write, and check_cfg_trace.py checks that none
+# of them disrupts a preserved device between the end of stage 1 and the end
+# of stage 2.
+PCI_TRACE="$workspace_dir/qemu-pci-trace.log"
+PCI_MARKER_ADDR=1e
PCI_CHASSIS=0
TEST_BINARY=pci/luo_pci_test
TEST_MODULES=(luo_pci_test_driver)
TEST_TIMEOUT="180s"
-TEST_QEMU_ARGS=()
+# The marker device lets the test binary mark the phases of the test in the
+# trace by writing to its config space.
+TEST_QEMU_ARGS=(
+ -device "pci-testdev,bus=pcie.0,addr=0x$PCI_MARKER_ADDR"
+ -trace "enable=pci_cfg_write,file=$PCI_TRACE"
+)
+TEST_ARGS=(-m "0000:00:$PCI_MARKER_ADDR.0")
# The default x86 machine (pc) does not support PCIe.
if [[ "$arch" == "x86" ]]; then
TEST_QEMU_ARGS+=(-M q35)
fi
+if ! command -v python3 &> /dev/null; then
+ skip "python3 is required to check the QEMU trace"
+fi
+
# pci_root_port <id> <addr>
# Add a root port at device <addr> on the root bus.
function pci_root_port() {
@@ -45,7 +61,8 @@ function pci_preserve() {
# pci_control <path>
# Bind the endpoint at <path> to the test driver, but don't preserve it.
-# Bus Master must be cleared on it during kexec.
+# Bus Master must be cleared on it during kexec. The trace check also uses
+# this to confirm that it is looking at the right devices and phase.
function pci_control() {
TEST_ARGS+=(-c "$1")
}
@@ -62,3 +79,8 @@ function pci_topology_root_ports() {
pci_preserve 0000:00:02.0/00.0
pci_control 0000:00:03.0/00.0
}
+
+function test_check() {
+ python3 "$test_dir/pci/check_cfg_trace.py" \
+ --serial "$workspace_dir/qemu.serial" --trace "$PCI_TRACE"
+}
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 13/18] selftests/liveupdate: pci: Add a test with an endpoint on the root bus
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (11 preceding siblings ...)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 14/18] selftests/liveupdate: pci: Add a test with an endpoint below a switch David Matlack
` (4 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that preserves an endpoint on the root bus,
i.e. with no bridges above it, next to a control endpoint on the root
bus.
QEMU reserves the routing IDs of IGB's VFs (offset 0x80, stride 2) on
the same bus as the IGB itself, so the two endpoints are placed at
devices 2 and 4 to keep their VFs' routing IDs from overlapping.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../selftests/liveupdate/pci/root_bus.vmtest.sh | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh b/tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh
new file mode 100644
index 000000000000..e0d24a00aae7
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Preserve an endpoint on the root bus, i.e. with no bridges above it.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+# QEMU reserves the routing IDs of IGB's VFs (offset 0x80, stride 2) on the same
+# bus, i.e. devices 18-19 for the IGB at device 2 and 20-21 for device 4.
+pci_igb igb1 pcie.0 0x2
+pci_igb igb2 pcie.0 0x4
+
+pci_preserve 0000:00:02.0
+pci_control 0000:00:04.0
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 14/18] selftests/liveupdate: pci: Add a test with an endpoint below a switch
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (12 preceding siblings ...)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 15/18] selftests/liveupdate: pci: Test bus numbers with pci=assign-busses David Matlack
` (3 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that preserves an endpoint below a PCIe
switch, next to a control endpoint below another downstream port of the
same switch. The root port and the switch's upstream port are shared
between the preserved and the control endpoint, and so must be
preserved.
Add pci_switch to build switches, and pci_topology_switch for this
topology, which later tests reuse.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../liveupdate/pci/pci_vmtest_lib.sh | 29 +++++++++++++++++++
.../selftests/liveupdate/pci/switch.vmtest.sh | 8 +++++
2 files changed, 37 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/pci/switch.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
index 5babeb19fbc1..3038cc4ca53f 100644
--- a/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
+++ b/tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh
@@ -47,6 +47,22 @@ function pci_root_port() {
TEST_QEMU_ARGS+=(-device "pcie-root-port,id=$1,bus=pcie.0,addr=$2,chassis=$PCI_CHASSIS")
}
+# pci_switch <id> <bus> <downstream_id>...
+# Add a switch below <bus>, with one downstream port per <downstream_id>. The
+# downstream ports are devices 0, 1, ... on the switch's internal bus.
+function pci_switch() {
+ local id=$1 bus=$2 i=0 dsp
+ shift 2
+
+ TEST_QEMU_ARGS+=(-device "x3130-upstream,id=$id,bus=$bus,addr=0")
+ for dsp in "$@"; do
+ PCI_CHASSIS=$((PCI_CHASSIS + 1))
+ TEST_QEMU_ARGS+=(-device
+ "xio3130-downstream,id=$dsp,bus=$id,addr=$i,chassis=$PCI_CHASSIS,slot=0")
+ i=$((i + 1))
+ done
+}
+
# pci_igb <id> <bus> [<addr>]
# Add an IGB endpoint at device <addr> (default 0) on <bus>.
function pci_igb() {
@@ -80,6 +96,19 @@ function pci_topology_root_ports() {
pci_control 0000:00:03.0/00.0
}
+# pci_topology_switch
+# A switch below a root port, with an endpoint to preserve and a control
+# endpoint below different downstream ports.
+function pci_topology_switch() {
+ pci_root_port rp1 0x2
+ pci_switch sw1 rp1 dsp1 dsp2
+ pci_igb igb1 dsp1
+ pci_igb igb2 dsp2
+
+ pci_preserve 0000:00:02.0/00.0/00.0/00.0
+ pci_control 0000:00:02.0/00.0/01.0/00.0
+}
+
function test_check() {
python3 "$test_dir/pci/check_cfg_trace.py" \
--serial "$workspace_dir/qemu.serial" --trace "$PCI_TRACE"
diff --git a/tools/testing/selftests/liveupdate/pci/switch.vmtest.sh b/tools/testing/selftests/liveupdate/pci/switch.vmtest.sh
new file mode 100644
index 000000000000..5b851ddf0b2b
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/switch.vmtest.sh
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Preserve an endpoint below a switch, next to a control endpoint below another
+# downstream port of the same switch.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+pci_topology_switch
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 15/18] selftests/liveupdate: pci: Test bus numbers with pci=assign-busses
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (13 preceding siblings ...)
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 ` David Matlack
2026-12-02 0:39 ` [PATCH 16/18] selftests/liveupdate: pci: Test bus numbers with pci=hpbussize David Matlack
` (2 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that boots the next kernel with
pci=assign-busses, which makes x86 assign all bus numbers itself rather
than keep the ones it finds. The next kernel must still keep the bus
numbers of the bridges above a preserved endpoint, which the test
checks with the switch topology.
pci=assign-busses only exists on x86. On arm64 and riscv64, the generic
host bridge driver used by QEMU's virt machine reassigns all bus numbers
anyway, so every test there already covers this, and the test is
skipped.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../liveupdate/pci/assign_busses.vmtest.sh | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh b/tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh
new file mode 100644
index 000000000000..f02b1914baf2
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Ask the next kernel to assign all bus numbers itself, which it must not do for
+# bridges above a preserved endpoint.
+#
+# On arm64 and riscv64, the generic host bridge driver used by QEMU's virt
+# machine reassigns all bus numbers anyway, so every test there covers this.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+if [[ "$arch" != "x86" ]]; then
+ skip "pci=assign-busses is only supported on x86"
+fi
+
+pci_topology_switch
+TEST_CMDLINE_STAGE2="pci=assign-busses"
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 16/18] selftests/liveupdate: pci: Test bus numbers with pci=hpbussize
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (14 preceding siblings ...)
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 ` 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
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that boots the next kernel with
pci=hpbussize=16, which makes it reserve extra bus numbers below
hotplug bridges, such as QEMU's PCIe ports.
Reassigning bus numbers with the same settings as the first kernel
produces the same numbers again, so only the trace check would catch a
next kernel that fails to keep them. With hpbussize=16 the reassigned
numbers differ, so the checks in the next kernel catch it too. The test
uses the switch topology.
On x86, also pass pci=assign-busses so that the next kernel reassigns
bus numbers. The generic host bridge driver used on arm64 and riscv64
always does.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../liveupdate/pci/hpbussize.vmtest.sh | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh b/tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh
new file mode 100644
index 000000000000..423fdbde99c1
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Ask the next kernel to reserve extra bus numbers below hotplug bridges, which
+# would change the bus numbers of bridges above a preserved endpoint if they
+# were reassigned.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+pci_topology_switch
+
+# The generic host bridge driver used on arm64 and riscv64 reassigns all bus
+# numbers, but x86 must be asked to.
+if [[ "$arch" == "x86" ]]; then
+ TEST_CMDLINE_STAGE2="pci=assign-busses,hpbussize=16"
+else
+ TEST_CMDLINE_STAGE2="pci=hpbussize=16"
+fi
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 17/18] selftests/liveupdate: pci: Test ARI Forwarding with pci=noari
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (15 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 16/18] selftests/liveupdate: pci: Test bus numbers with pci=hpbussize David Matlack
@ 2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 18/18] selftests/liveupdate: pci: Test ACS with pci=config_acs David Matlack
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that boots the first kernel with pci=noari,
so that it leaves ARI Forwarding disabled on the root ports. IGB
implements ARI, so the next kernel would normally enable ARI Forwarding
on the root ports above the IGB endpoints, but it must leave it alone
on the root port above the preserved endpoint.
Add a -x option to the test so that it can also check that the next
kernel does enable ARI Forwarding on the root port above the control
endpoint, i.e. that pci=noari took effect.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../selftests/liveupdate/pci/luo_pci_test.c | 35 ++++++++++++++++++-
.../selftests/liveupdate/pci/noari.vmtest.sh | 13 +++++++
2 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/liveupdate/pci/noari.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
index 177562d65e64..4ee6f3a5f307 100644
--- a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
@@ -10,6 +10,10 @@
* -p <path> Endpoint to preserve.
* -c <path> Optional. Endpoint to bind to the test driver but not
* preserve.
+ * -x <reg>[,...] Optional. Registers that must change across the Live
+ * Update on bridges above the control endpoint (and not
+ * above the preserved endpoint): "ari" for ARI Forwarding
+ * Enable.
* -m <bdf> Optional device whose config space is written to mark the
* phases of the test in a QEMU trace of config writes.
*
@@ -95,6 +99,7 @@ static int nr_before;
static const char *opt_preserve = "";
static const char *opt_control = "";
static const char *opt_marker = "";
+static const char *opt_control_changes = "";
/* strcspn(), which nolibc lacks. */
static size_t span_until(const char *s, const char *reject)
@@ -112,7 +117,7 @@ static int parse_args(int argc, char *argv[])
int stage = 1;
int opt;
- while ((opt = getopt(argc, argv, "s:p:c:m:")) != -1) {
+ while ((opt = getopt(argc, argv, "s:p:c:x:m:")) != -1) {
switch (opt) {
case 's':
stage = atoi(optarg);
@@ -123,6 +128,9 @@ static int parse_args(int argc, char *argv[])
case 'c':
opt_control = optarg;
break;
+ case 'x':
+ opt_control_changes = optarg;
+ break;
case 'm':
opt_marker = optarg;
break;
@@ -480,6 +488,24 @@ static uint16_t get_status(int fd, const char *bdf)
static int nr_failures;
+/* Whether @reg is listed in -x. */
+static bool control_changes(const char *reg)
+{
+ const char *p = opt_control_changes;
+ size_t len;
+
+ while (*p) {
+ len = span_until(p, ",");
+ if (len == strlen(reg) && !strncmp(p, reg, len))
+ return true;
+ p += len;
+ if (*p == ',')
+ p++;
+ }
+
+ return false;
+}
+
#define check(cond, fmt, ...) do { \
if (!(cond)) { \
ksft_print_msg("FAIL: " fmt "\n", ##__VA_ARGS__); \
@@ -520,6 +546,13 @@ static void check_devices(const char *when, const struct pci_snapshot *taken)
check(!(now.command & PCI_COMMAND_MASTER),
"%s: Bus Master still enabled on control device",
dev->bdf);
+ if (dev->bridge && control_changes("ari")) {
+ check(dev->pcie, "%s: Not a PCIe device", dev->bdf);
+ check(!dev->pcie ||
+ ((now.devctl2 ^ dev->devctl2) & PCI_EXP_DEVCTL2_ARI),
+ "%s: ARI Forwarding Enable did not change (DEVCTL2 0x%04x)",
+ dev->bdf, now.devctl2);
+ }
continue;
}
diff --git a/tools/testing/selftests/liveupdate/pci/noari.vmtest.sh b/tools/testing/selftests/liveupdate/pci/noari.vmtest.sh
new file mode 100644
index 000000000000..f72d8add83be
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/noari.vmtest.sh
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Disable ARI in the first kernel only. IGB implements ARI, so the next kernel
+# enables ARI Forwarding on the root port above the control endpoint, but must
+# adopt it (i.e. leave it disabled) on the root port above the preserved
+# endpoint.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+pci_topology_root_ports
+
+TEST_ARGS+=(-x ari)
+TEST_CMDLINE_STAGE1="pci=noari"
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH 18/18] selftests/liveupdate: pci: Test ACS with pci=config_acs
2026-12-02 0:39 [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests David Matlack
` (16 preceding siblings ...)
2026-12-02 0:39 ` [PATCH 17/18] selftests/liveupdate: pci: Test ARI Forwarding with pci=noari David Matlack
@ 2026-12-02 0:39 ` David Matlack
17 siblings, 0 replies; 19+ messages in thread
From: David Matlack @ 2026-12-02 0:39 UTC (permalink / raw)
To: kexec, linux-kernel, linux-kselftest, linux-pci, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Alex Williamson, Bjorn Helgaas,
David Matlack, Jacob Pan, Jason Gunthorpe, Josh Hilke,
Lukas Wunner, Mike Rapoport, Palmer Dabbelt, Parav Pandit,
Pasha Tatashin, Paul Walmsley, Pranjal Shrivastava,
Pratyush Yadav, Samiullah Khawaja, Shuah Khan, Vipin Sharma
Add a PCI Live Update test that configures different ACS controls in
each kernel with pci=config_acs=. The next kernel must keep the ACS
controls of the root port above the preserved endpoint, and apply its
own to the root port above the control endpoint, which the test checks
with -x acs.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../testing/selftests/liveupdate/pci/acs.vmtest.sh | 14 ++++++++++++++
.../selftests/liveupdate/pci/luo_pci_test.c | 10 ++++++++--
2 files changed, 22 insertions(+), 2 deletions(-)
create mode 100644 tools/testing/selftests/liveupdate/pci/acs.vmtest.sh
diff --git a/tools/testing/selftests/liveupdate/pci/acs.vmtest.sh b/tools/testing/selftests/liveupdate/pci/acs.vmtest.sh
new file mode 100644
index 000000000000..6bc7f0980cbd
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/acs.vmtest.sh
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Configure different ACS controls in each kernel. The next kernel must adopt
+# the ACS controls of bridges above a preserved endpoint, and apply its own to
+# the root port above the control endpoint.
+
+source "$test_dir/pci/pci_vmtest_lib.sh"
+
+pci_topology_root_ports
+
+# Bits, from most to least significant: DT EC UF CR RR TB SV
+TEST_ARGS+=(-x acs)
+TEST_CMDLINE_STAGE1="pci=config_acs=xx11101@pci:0:0"
+TEST_CMDLINE_STAGE2="pci=config_acs=xx00000@pci:0:0"
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
index 4ee6f3a5f307..304f3b3ef1c9 100644
--- a/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test.c
@@ -12,8 +12,8 @@
* preserve.
* -x <reg>[,...] Optional. Registers that must change across the Live
* Update on bridges above the control endpoint (and not
- * above the preserved endpoint): "ari" for ARI Forwarding
- * Enable.
+ * above the preserved endpoint): "acs" for the ACS
+ * controls, "ari" for ARI Forwarding Enable.
* -m <bdf> Optional device whose config space is written to mark the
* phases of the test in a QEMU trace of config writes.
*
@@ -546,6 +546,12 @@ static void check_devices(const char *when, const struct pci_snapshot *taken)
check(!(now.command & PCI_COMMAND_MASTER),
"%s: Bus Master still enabled on control device",
dev->bdf);
+ if (dev->bridge && control_changes("acs")) {
+ check(dev->acs, "%s: No ACS capability", dev->bdf);
+ check(!dev->acs || now.acs_ctrl != dev->acs_ctrl,
+ "%s: ACS controls did not change (0x%04x)",
+ dev->bdf, now.acs_ctrl);
+ }
if (dev->bridge && control_changes("ari")) {
check(dev->pcie, "%s: Not a PCIe device", dev->bdf);
check(!dev->pcie ||
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 19+ messages in thread