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

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


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

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

Reply instructions:

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

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

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

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

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

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

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

all inboxes | Powered by JetHome®