[PATCH v2 10/10] PCI: pci-lu-stub: Add a stub driver for Live Update testing

Chris Li
2025-09-16T07:45:23Z

Introduce a new driver, pci-lu-stub, that can be bound to any PCI device
and used to test the PCI subsystem support for Live Update. This driver
gives developers a way to opt-in a device for Live Update and driver
interaction with the PCI subsystem. This driver is only intended for
testing purposes.

In the future this driver can be extended to test other scenarios (such
as failing prepare() on purpose).

Signed-off-by: David Matlack <dmatlack@google.com>
Signed-off-by: Chris Li <chrisl@kernel.org>
---
MAINTAINERS | 1 +
drivers/pci/Kconfig | 10 ++++
drivers/pci/Makefile | 1 +
drivers/pci/pci-lu-stub.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 152 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 1ae3d166cd35ec5c7818f202079ed5d10c09144b..43e5813e6f030a80c2c109b38e332eef536707d6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -14021,6 +14021,7 @@ F: Documentation/ABI/testing/sysfs-kernel-liveupdate
F: Documentation/admin-guide/liveupdate.rst
F: drivers/misc/liveupdate/
F: drivers/pci/liveupdate/
+F: drivers/pci/pci-lu-stub.c
F: include/linux/dev_liveupdate.h
F: include/linux/liveupdate.h
F: include/uapi/linux/liveupdate.h
diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
index 9c0e4aaf4e8cb7fecd9f80ac6289b8d854ce03aa..37e44782fa35c64c2eba6a0f6942d44d8003a499 100644
--- a/drivers/pci/Kconfig
+++ b/drivers/pci/Kconfig
@@ -327,3 +327,13 @@ source "drivers/pci/switch/Kconfig"
source "drivers/pci/pwrctrl/Kconfig"

endif
+
+config PCI_LU_STUB
+ tristate "PCI Live Update Stub Driver"
+ depends on LIVEUPDATE
+ help
+ Say Y or M here if you want to enable support for the Live Update stub
+ driver. This driver can be used to test the PCI subsystem support for
+ Live Updates.
+
+ When in doubt, say N.
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index aa1bac7aed7d12c641a6b55e56176fb3cdde4c91..061e98d0411a951573e1996c61ce5a98f2775e53 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o
obj-$(CONFIG_PCI_NPEM) += npem.o
obj-$(CONFIG_PCIE_TPH) += tph.o
obj-$(CONFIG_LIVEUPDATE) += liveupdate.o
+obj-$(CONFIG_PCI_LU_STUB) += pci-lu-stub.o

# Endpoint library must be initialized before its users
obj-$(CONFIG_PCI_ENDPOINT) += endpoint/
diff --git a/drivers/pci/pci-lu-stub.c b/drivers/pci/pci-lu-stub.c
new file mode 100644
index 0000000000000000000000000000000000000000..aa0404d16336278d76b062d8126dc5f45732403e
--- /dev/null
+++ b/drivers/pci/pci-lu-stub.c
@@ -0,0 +1,140 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/kexec_handover.h>
+#include <linux/liveupdate.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+
+struct pci_lu_stub_ser {
+ u16 dev_id;
+} __packed;
+
+static const struct pci_device_id pci_lu_stub_id_table[] = {
+ /* Allow binding to any device but only via driver_override. */
+ { PCI_DEVICE_DRIVER_OVERRIDE(PCI_ANY_ID, PCI_ANY_ID, 1) },
+ {},
+};
+
+static int validate_folio(struct pci_dev *dev, struct folio *folio)
+{
+ const struct pci_lu_stub_ser *ser = folio_address(folio);
+
+ if (folio_order(folio) != get_order(sizeof(*ser))) {
+ pci_err(dev, "Restored folio has unexpected order %u\n", folio_order(folio));
+ return -ERANGE;
+ }
+
+ if (ser->dev_id != pci_dev_id(dev)) {
+ pci_err(dev, "Restored folio contains unexpected dev_id: 0x%x\n", ser->dev_id);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int pci_lu_stub_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{
+ struct folio *folio;
+ u64 data;
+ int ret;
+
+ if (liveupdate_state_normal()) {
+ pci_set_master(dev);
+ pci_info(dev, "Marking device liveupdate busmaster\n");
+ dev->dev.lu.flags = LU_BUSMASTER;
+ return 0;
+ }
+
+ if (!liveupdate_state_updated()) {
+ pci_err(dev, "Unable to handle probe() outside of normal and updated states.\n");
+ return -EOPNOTSUPP;
+ }
+
+ ret = pci_liveupdate_get_driver_data(dev, &data);
+ if (ret) {
+ pci_err(dev, "Failed to get driver data for device (%d)\n", ret);
+ return ret;
+ }
+
+ pci_info(dev, "%s(): data: 0x%llx\n", __func__, data);
+
+ folio = kho_restore_folio(data);
+ if (!folio) {
+ pci_err(dev, "Failed to restore folio at 0x%llx.\n", data);
+ return -ENOENT;
+ }
+
+ return validate_folio(dev, folio);
+}
+
+static void pci_lu_stub_remove(struct pci_dev *dev)
+{
+ WARN_ON(!liveupdate_state_normal());
+ dev->dev.lu.flags = 0;
+}
+
+static int pci_lu_stub_prepare(struct device *dev, u64 *data)
+{
+ struct pci_lu_stub_ser *ser;
+ struct folio *folio;
+ int ret;
+
+ folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, get_order(sizeof(*ser)));
+ if (!folio)
+ return -ENOMEM;
+
+ ret = kho_preserve_folio(folio);
+ if (ret) {
+ dev_err(dev, "Failed to preserve folio (%d)\n", ret);
+ folio_put(folio);
+ return ret;
+ }
+
+ ser = folio_address(folio);
+ ser->dev_id = pci_dev_id(to_pci_dev(dev));
+
+ *data = virt_to_phys(ser);
+ dev_info(dev, "%s(): data: 0x%llx\n", __func__, *data);
+ return 0;
+}
+
+static int pci_lu_stub_freeze(struct device *dev, u64 *data)
+{
+ struct folio *folio = pfn_folio(PHYS_PFN(*data));
+
+ dev_info(dev, "%s(): data: 0x%llx\n", __func__, *data);
+ return validate_folio(to_pci_dev(dev), folio);
+}
+
+static void pci_lu_stub_finish(struct device *dev, u64 data)
+{
+ struct folio *folio = pfn_folio(PHYS_PFN(data));
+
+ dev_info(dev, "%s(): data: 0x%llx\n", __func__, data);
+ WARN_ON(validate_folio(to_pci_dev(dev), folio));
+ folio_put(folio);
+}
+
+static void pci_lu_stub_cancel(struct device *dev, u64 data)
+{
+ dev_info(dev, "%s(): data: 0x%llx\n", __func__, data);
+ pci_lu_stub_finish(dev, data);
+}
+
+static struct dev_liveupdate_ops liveupdate_ops = {
+ .prepare = pci_lu_stub_prepare,
+ .freeze = pci_lu_stub_freeze,
+ .finish = pci_lu_stub_finish,
+ .cancel = pci_lu_stub_cancel,
+};
+
+static struct pci_driver pci_lu_stub_driver = {
+ .name = "pci-lu-stub",
+ .id_table = pci_lu_stub_id_table,
+ .probe = pci_lu_stub_probe,
+ .remove = pci_lu_stub_remove,
+ .driver.lu = &liveupdate_ops,
+};
+
+module_pci_driver(pci_lu_stub_driver);
+MODULE_LICENSE("GPL");

--
2.51.0.384.g4c02a37b29-goog