selftests/liveupdate: Add PCI Live Update selftests
Subsystems (5)
cover selftests/liveupdate: Add PCI Live Update selftests
This series can be found on GitHub:
https://github.com/dmatlack/linux/tree/liveupdate/pci/base/selftests/v1
This series adds selftests for the PCI core support for Live Update, which is now in liveupdate.git's next branch [1]. Each test preserves a PCI device across a Live Update in a QEMU VM and checks that the PCI core keeps the device, and every bridge above it, running: same BDF and bus numbers, Bus Master Enable left set, and ARI Forwarding Enable and ACS controls adopted from the previous kernel. The tests run on x86_64 (q35), arm64 (virt) and riscv64 (virt) as part of tools/testing/selftests/liveupdate/run-vmtests.sh.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git/log/?h=next
Why PCI-specific tests?
-----------------------
The PCI core support for Live Update is also exercised by the VFIO selftests (vfio_pci_liveupdate_uapi_test and vfio_pci_liveupdate_kexec_test). Those tests check that VFIO works across a Live Update. These tests check that the PCI core does, and both are needed.
In the short term:
- The PCI core support is queued in liveupdate.git ahead of the drivers that use it. These tests let it be tested in-tree, and by anyone reviewing or changing it, without the rest of the VFIO and iommufd Live Update support.
- Changes to the PCI core need fast, focused feedback. These tests run on x86_64, arm64 and riscv64 in a few minutes, without any host hardware, IOMMU setup, or VFIO userspace.
In the long term:
- The guarantees for preserved devices belong to the PCI core, not to any one driver: a preserved device and the bridges above it keep their bus numbers, Bus Master Enable, ARI Forwarding Enable and ACS controls. VFIO is only the first user of the PCI Live Update APIs, and the guarantees must hold for every driver that preserves a device. They should be tested at the layer that provides them, with a driver that does nothing but call those APIs.
- Driver tests cannot isolate the PCI core. A failure could be in the driver, iommufd, LUO, or the PCI core, and a pass only shows that the driver did not notice a problem. A driver might even hide one, e.g. by reprogramming the device after it is retrieved.
- The cases that matter to the PCI core depend on the topology and on the next kernel's configuration, not on the device: endpoints on the root bus or below root ports and switches, bridges shared with non-preserved devices, and command lines that would renumber buses or change ARI and ACS. Driver tests run on whatever hardware is at hand, whereas these tests build each topology and configuration in QEMU.
- Checking the end state is not enough. A device that loses Bus Master Enable or gets reset partway through the Live Update can look fine afterwards, but its DMA was interrupted. Tracing every config write in QEMU catches these transient disruptions.
- The PCI core support will grow (e.g. preserving BARs and bridge windows for P2PDMA, and preserving VFs), and changes to enumeration, resource assignment, ACS, ARI, or the shutdown path can break Live Update without touching drivers/pci/liveupdate.c. These tests give PCI developers regression tests to run, and to extend with each new guarantee.
Series Overview
---------------
1. Cherry-pick "liveupdate: Export symbols needed by modules" from the VFIO series [2] so that the test-only driver can be built as a module (patch 1).
2. Fix vmtest.sh to report skipped tests to run-vmtests.sh as skipped rather than passed (patch 2).
3. Make the liveupdate selftests Makefile build objects in $(OUTPUT) rather than the source directory, and add a NOLIBC=1 mode that builds the kexec tests and vmtest.sh's init process as static binaries against nolibc. Use it to build vmtest.sh's binaries, so that the Makefile is the only description of how the tests are built (patches 3-5).
4. Extend vmtest.sh so that tests can be described by a <test>.vmtest.sh file, which can set the test binary and its arguments, kernel modules to build and load, extra QEMU arguments, separate command lines for each kernel, and a host-side check to run after the test passes in the VM. Make run-vmtests.sh run every test defined in a subdirectory (patches 6-9).
5. Add a test-only driver, luo_pci_test_driver.ko, that binds to IGB devices and hands out files that can be preserved in a Live Update session. It lives in tools/testing/selftests/liveupdate/modules/ and is built through TEST_GEN_MODS_DIR (patch 10).
6. Add the test binary and the first test, which preserves an IGB endpoint below a root port and binds another (the control) to the test driver without preserving it. Add the Kconfig options that the PCI tests need to the Live Update selftests' config files (patch 11).
7. 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. As a positive control, the trace must show Bus Master Enable getting cleared on the control endpoint during kexec (patch 12).
8. Add tests with an endpoint on the root bus and below a switch, one
per patch (patches 13-14).
9. Add tests that boot the next kernel with a configuration that would change preserved devices if the PCI core did not adopt their state, one per patch: pci=assign-busses, pci=hpbussize=16, pci=noari in the first kernel only, and different pci=config_acs= in each kernel. The ARI and ACS tests also check that the bridge above the control endpoint does change, i.e. that the command line takes effect (patches 15-18).
[2] https://lore.kernel.org/kvm/[email protected]/
Building
--------
make -C tools/testing/selftests/liveupdate
builds the PCI test binary and the test driver along with the other Live Update tests. Like other selftests modules, the test driver is built against the running kernel by default, or against the kernel build directory given with KDIR=.
vmtest.sh builds the test binaries with the same Makefile in NOLIBC=1 mode, and builds the test driver against the kernel it boots.
Merge Plan
----------
This series applies on top of liveupdate.git's next branch [1], and should be merged through liveupdate.git, matching the agreed-upon tree in the MAINTAINERS entry for PCI LIVE UPDATE.
Patch 1 is also part of the VFIO series [2], with a commit message here that doesn't refer to vfio-pci. It can be dropped from whichever series is merged second.
Testing
-------
All tests pass on x86_64, arm64 and riscv64 with run-vmtests.sh (assign_busses is skipped on arm64 and riscv64, where the generic host bridge driver used by QEMU's virt machine reassigns all bus numbers), along with the existing LUO tests:
SUMMARY: PASS=31 SKIP=2 FAIL=0
Each new test was also run on x86_64 and arm64 at the patch that adds it, and every patch builds without warnings with and without NOLIBC=1.
To check that the tests detect breakage, they were also run against deliberately broken kernels:
- Clearing Bus Master Enable on preserved devices during kexec makes pci/root_port fail on all three architectures, both in the next kernel and in the trace check.
- Not preserving bus numbers in the next kernel makes pci/switch fail on arm64 and riscv64, and pci/hpbussize fail on x86_64, both in the next kernel and in the trace check, which shows the root port's bus numbers being reset.
Future Work
-----------
Please see the PCI core Live Update Roadmap:
https://lore.kernel.org/linux-pci/[email protected]/
Possible extensions to these tests include preserving multiple devices, keeping the IGB device performing DMA throughout the Live Update, and checking BARs and bridge windows once they are preserved.
David Matlack (17):
selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test selftests/liveupdate: Build objects in $(OUTPUT) selftests/liveupdate: Support building the kexec tests against nolibc selftests/liveupdate: Build vmtest.sh's binaries with the Makefile selftests/liveupdate: Support test definitions in vmtest.sh selftests/liveupdate: Support kernel modules in vmtest.sh selftests/liveupdate: Support a different command line for each kernel selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh selftests/liveupdate: Add a test driver for PCI Live Update selftests/liveupdate: pci: Add a test with an endpoint below a root
port
selftests/liveupdate: pci: Check PCI config writes with QEMU tracing selftests/liveupdate: pci: Add a test with an endpoint on the root bus selftests/liveupdate: pci: Add a test with an endpoint below a switch selftests/liveupdate: pci: Test bus numbers with pci=assign-busses selftests/liveupdate: pci: Test bus numbers with pci=hpbussize selftests/liveupdate: pci: Test ARI Forwarding with pci=noari selftests/liveupdate: pci: Test ACS with pci=config_acs
Vipin Sharma (1):
liveupdate: Export symbols needed by modules
MAINTAINERS | 2 + kernel/liveupdate/luo_file.c | 2 + tools/testing/selftests/liveupdate/.gitignore | 4 + tools/testing/selftests/liveupdate/Makefile | 22 +- tools/testing/selftests/liveupdate/config | 4 + .../selftests/liveupdate/config.aarch64 | 1 + .../selftests/liveupdate/config.riscv64 | 1 + tools/testing/selftests/liveupdate/init.c | 86 +- .../selftests/liveupdate/modules/Makefile | 20 + .../liveupdate/modules/luo_pci_test_driver.c | 279 ++++++ .../selftests/liveupdate/pci/acs.vmtest.sh | 14 + .../liveupdate/pci/assign_busses.vmtest.sh | 16 + .../liveupdate/pci/check_cfg_trace.py | 238 ++++++ .../liveupdate/pci/hpbussize.vmtest.sh | 17 + .../selftests/liveupdate/pci/luo_pci_test.c | 794 ++++++++++++++++++ .../liveupdate/pci/luo_pci_test_driver.h | 52 ++ .../selftests/liveupdate/pci/noari.vmtest.sh | 13 + .../liveupdate/pci/pci_vmtest_lib.sh | 115 +++ .../liveupdate/pci/root_bus.vmtest.sh | 13 + .../liveupdate/pci/root_port.vmtest.sh | 7 + .../selftests/liveupdate/pci/switch.vmtest.sh | 8 + .../selftests/liveupdate/run-vmtests.sh | 9 +- tools/testing/selftests/liveupdate/vmtest.sh | 128 ++- 23 files changed, 1803 insertions(+), 42 deletions(-) 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/acs.vmtest.sh create mode 100644 tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh create mode 100755 tools/testing/selftests/liveupdate/pci/check_cfg_trace.py create mode 100644 tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test.c create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h create mode 100644 tools/testing/selftests/liveupdate/pci/noari.vmtest.sh create mode 100644 tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh create mode 100644 tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh create mode 100644 tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh create mode 100644 tools/testing/selftests/liveupdate/pci/switch.vmtest.sh
base-commit: ec459d99ed222228b3d601818fd1e24822a450d6
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 1/18 liveupdate: Export symbols needed by modules
From: Vipin Sharma <[email protected]>
Export liveupdate_register_file_handler() and liveupdate_unregister_file_handler() so that drivers built as modules can register Live Update file handlers.
--- kernel/liveupdate/luo_file.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/liveupdate/luo_file.c +2 -0
@@ -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.
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH 01/18] liveupdate: Export symbols needed by modules
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] liveupdate: null pointer dereference in…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] liveupdate: null pointer dereference in liveupdate_register_file_handler() [1]
--
commit 342376e758785ba8c67f5c05baf9a2d35602079b
Author: Vipin Sharma <[email protected]>
liveupdate: Export symbols needed by modules
This commit exports liveupdate_register_file_handler() and liveupdate_unregister_file_handler() so modules can register Live Update file handlers.
kernel/liveupdate/luo_file.c int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) · line 939
Quoted 9 lines
kernel/liveupdate/luo_file.c +1
-0@@ -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);
[Severity: High]
This problem wasn't introduced by this patch, but can liveupdate_register_file_handler() trigger a NULL pointer dereference
[1] if a caller registers a handler with a NULL fh->ops pointer? The callback sanity check dereferences fh->ops (!fh->ops->preserve, etc.) without checking !fh->ops first.
[1] https://sashiko.dev/#/bug/linux-0427ad16-7f67-4a5d-96e7-93d10ba13d6c
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 01/18] liveupdate: Export symbols needed by modules
In reply to David MatlackQuoted 5 lines · David Matlack
From: Vipin Sharma <[email protected]>
Export
liveupdate_register_file_handler()andliveupdate_unregister_file_handler()so that drivers built as modules can register Live Update file handlers.
I don't see any such driver in this series. And the diea of allowing reandom drivers to register this information feels wrong.
Re: [PATCH 01/18] liveupdate: Export symbols needed by modules
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · David Matlack
From: Vipin Sharma <[email protected]>
Export
liveupdate_register_file_handler()andliveupdate_unregister_file_handler()so that drivers built as modules can register Live Update file handlers.I don't see any such driver in this series. And the diea of allowing reandom drivers to register this information feels wrong.
The user in this series is the test driver added in patch 10 (tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c), which the selftests build as a module and load in the kernel. The other user will be vfio-pci, from the VFIO Live Update series, which is where this patch comes from:
https://lore.kernel.org/kvm/[email protected]/
To address the concern about random drivers, I see two options:
A. Restrict these exports to the modules that need them, i.e.
EXPORT_SYMBOL_FOR_MODULES(..., "vfio-pci,luo_pci_test_driver").
B. Make the test driver built-in, behind a Kconfig option like the
existing CONFIG_LIVEUPDATE_TEST, and drop this patch from this series. vfio-pci would still need exports though.
Can you say more about what your concern is? Is it mainly about which modules can call these functions, or about drivers registering Live Update file handlers at all? If it's the latter, I'd like to understand it, since that's how drivers like vfio-pci are expected to preserve their devices across a Live Update.
PATCH 2/18 selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test
In reply to David MatlackExit 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.
--- tools/testing/selftests/liveupdate/vmtest.sh | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/testing/selftests/liveupdate/vmtest.sh +8 -0
@@ -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 }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewRe: [PATCH 02/18] selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/liveupdate: missing Fixes trailer for cleanup() KSFT_SKIP fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/liveupdate: missing Fixes trailer for cleanup() KSFT_SKIP fix
--
commit 8b3910582f94959e85b9a8eb97ef2dfb6fa3b7a8
Author: David Matlack <[email protected]>
Quoted 10 lines
selftests/liveupdate: Exit with
KSFT_SKIPwhenvmtest.shskips a testExit
vmtest.shwithKSFT_SKIPwhen a test is skipped, so thatrun-vmtests.shreports it as skipped rather than passed.
skip()exits withKSFT_SKIP, but the EXIT trap then callsktap_finished(), which exits withKSFT_PASSsince every planned test either passed or was skipped.Signed-off-by David Matlack <[email protected]>
[Severity: Low]
This fixes cleanup() overwriting the KSFT_SKIP exit status from skip(). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/18 selftests/liveupdate: Build objects in $(OUTPUT)
In reply to David MatlackBuild 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.
--- tools/testing/selftests/liveupdate/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/liveupdate/Makefile +5 -1
@@ -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)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 4/18 selftests/liveupdate: Support building the kexec tests against nolibc
In reply to David MatlackAdd 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.
--- tools/testing/selftests/liveupdate/Makefile | 13 +++++++++++++ tools/testing/selftests/liveupdate/init.c | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-)
tools/testing/selftests/liveupdate/Makefile +13 -0
@@ -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)
tools/testing/selftests/liveupdate/init.c +2 -2
@@ -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;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 5/18 selftests/liveupdate: Build vmtest.sh's binaries with the Makefile
In reply to David MatlackBuild 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.
--- tools/testing/selftests/liveupdate/vmtest.sh | 32 ++++++++------------ 1 file changed, 13 insertions(+), 19 deletions(-)
tools/testing/selftests/liveupdate/vmtest.sh +13 -19
@@ -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"
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 6/18 selftests/liveupdate: Support test definitions in vmtest.sh
In reply to David MatlackAllow 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".
--- tools/testing/selftests/liveupdate/init.c | 49 +++++++++++++++++++- tools/testing/selftests/liveupdate/vmtest.sh | 44 +++++++++++++++--- 2 files changed, 84 insertions(+), 9 deletions(-)
tools/testing/selftests/liveupdate/init.c +47 -2
@@ -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 32static 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 );
tools/testing/selftests/liveupdate/vmtest.sh +37 -7
107 lines not on this page · raw message
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 7/18 selftests/liveupdate: Support kernel modules in vmtest.sh
In reply to David MatlackAdd 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.
--- tools/testing/selftests/liveupdate/vmtest.sh | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+)
tools/testing/selftests/liveupdate/vmtest.sh +24 -0
@@ -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"
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 8/18 selftests/liveupdate: Support a different command line for each kernel
In reply to David MatlackAdd 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.
--- tools/testing/selftests/liveupdate/init.c | 33 ++++++++++++++------ tools/testing/selftests/liveupdate/vmtest.sh | 26 ++++++++++++--- 2 files changed, 46 insertions(+), 13 deletions(-)
tools/testing/selftests/liveupdate/init.c +24 -9
@@ -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 );
tools/testing/selftests/liveupdate/vmtest.sh +22 -4
@@ -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" }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 9/18 selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh
In reply to David MatlackMake 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.
--- tools/testing/selftests/liveupdate/run-vmtests.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/liveupdate/run-vmtests.sh +8 -1
@@ -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"
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 10/18 selftests/liveupdate: Add a test driver for PCI Live Update
In reply to David MatlackAdd 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.
--- 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
MAINTAINERS +2 -0
@@ -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: [email protected]
tools/testing/selftests/liveupdate/.gitignore +3 -0
@@ -9,3 +9,6 @@ !config !config.* !Makefile + +# Generated by kbuild when building modules/ in place +*.mod.c
tools/testing/selftests/liveupdate/Makefile +2 -0
@@ -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
tools/testing/selftests/liveupdate/modules/Makefile new file +20 -0
@@ -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
tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c new file +279 -0
279 lines not on this page · raw message
tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 11/18 selftests/liveupdate: pci: Add a test with an endpoint below a root port
In reply to David MatlackAdd 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.
--- 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
tools/testing/selftests/liveupdate/Makefile +1 -0
@@ -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
tools/testing/selftests/liveupdate/config +4 -0
@@ -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
tools/testing/selftests/liveupdate/config.aarch64 +1 -0
@@ -1,2 +1,3 @@ +CONFIG_PCI_HOST_GENERIC=y CONFIG_SERIAL_AMBA_PL011=y CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
tools/testing/selftests/liveupdate/config.riscv64 +1 -0
@@ -1,3 +1,4 @@ +CONFIG_PCI_HOST_GENERIC=y CONFIG_SERIAL_8250=y CONFIG_SERIAL_8250_CONSOLE=y CONFIG_SERIAL_OF_PLATFORM=y
tools/testing/selftests/liveupdate/pci/luo_pci_test.c new file +707 -0
707 lines not on this page · raw message
tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh new file +64 -0
64 lines not on this page · raw message
tools/testing/selftests/liveupdate/pci/root_port.vmtest.sh new file +7 -0
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 12/18 selftests/liveupdate: pci: Check PCI config writes with QEMU tracing
In reply to David MatlackTrace 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.
--- 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
tools/testing/selftests/liveupdate/.gitignore +1 -0
@@ -4,6 +4,7 @@ !*.c !*.h !*.mk +!*.py !*.sh !.gitignore !config
tools/testing/selftests/liveupdate/pci/check_cfg_trace.py new file +238 -0
238 lines not on this page · raw message
tools/testing/selftests/liveupdate/pci/luo_pci_test.c +51 -3
120 lines not on this page · raw message
tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh +24 -2
@@ -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" +}
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 13/18 selftests/liveupdate: pci: Add a test with an endpoint on the root bus
In reply to David MatlackAdd 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.
--- .../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
tools/testing/selftests/liveupdate/pci/root_bus.vmtest.sh new file +13 -0
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 14/18 selftests/liveupdate: pci: Add a test with an endpoint below a switch
In reply to David MatlackAdd 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.
--- .../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
tools/testing/selftests/liveupdate/pci/pci_vmtest_lib.sh +29 -0
@@ -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"
tools/testing/selftests/liveupdate/pci/switch.vmtest.sh new file +8 -0
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 15/18 selftests/liveupdate: pci: Test bus numbers with pci=assign-busses
In reply to David MatlackAdd 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.
--- .../liveupdate/pci/assign_busses.vmtest.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh
tools/testing/selftests/liveupdate/pci/assign_busses.vmtest.sh new file +16 -0
@@ -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"
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 16/18 selftests/liveupdate: pci: Test bus numbers with pci=hpbussize
In reply to David MatlackAdd 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.
--- .../liveupdate/pci/hpbussize.vmtest.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh
tools/testing/selftests/liveupdate/pci/hpbussize.vmtest.sh new file +17 -0
@@ -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
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 17/18 selftests/liveupdate: pci: Test ARI Forwarding with pci=noari
In reply to David MatlackAdd 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.
--- .../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
tools/testing/selftests/liveupdate/pci/luo_pci_test.c +34 -1
@@ -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 ; }
tools/testing/selftests/liveupdate/pci/noari.vmtest.sh new file +13 -0
@@ -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"
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
PATCH 18/18 selftests/liveupdate: pci: Test ACS with pci=config_acs
In reply to David MatlackAdd 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.
--- .../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
tools/testing/selftests/liveupdate/pci/acs.vmtest.sh new file +14 -0
@@ -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"
tools/testing/selftests/liveupdate/pci/luo_pci_test.c +8 -2
@@ -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 ||
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
[email protected]
AI reviewSashiko has finished reviewing this 18-patch series:
Sashiko has finished reviewing this 18-patch series:
- Patches 3-18: no issues found.
- Patch 1 ("liveupdate: Export symbols needed by modules"): 1 related pre-existing bug noted
- Patch 2 ("selftests/liveupdate: Exit with KSFT_SKIP when vmtes..."): 1 potential issue (1 Low)
Detailed reviews have been sent as separate replies to patches 1 and 2.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]