[PATCH 10/18] selftests/liveupdate: Add a test driver for PCI Live Update

From: David Matlack

Date: Tue Dec 01 2026 - 19:40:47 EST


Add luo_pci_test_driver.ko, a test-only PCI driver that the PCI Live
Update selftests use to preserve PCI devices. The driver binds to Intel
82576 (IGB) devices, such as QEMU's igb device, and hands out a file for
each of its devices through /dev/luo_pci_test_driver. Userspace can
preserve these files in a Live Update session, and the driver's Live
Update file handler then preserves, unpreserves, retrieves and finishes
the device with the PCI Live Update APIs. Other than enabling the device
and bus mastering, the driver leaves the device alone, so that tests can
check how the PCI core treats it.

The driver lives in modules/, a directory for the kernel modules used
by the Live Update selftests, which the selftests build through
TEST_GEN_MODS_DIR.

Add the driver, and tools/testing/selftests/liveupdate/pci/ where its
interface header lives, to the PCI LIVE UPDATE entry in MAINTAINERS.

Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
MAINTAINERS | 2 +
tools/testing/selftests/liveupdate/.gitignore | 3 +
tools/testing/selftests/liveupdate/Makefile | 2 +
.../selftests/liveupdate/modules/Makefile | 20 ++
.../liveupdate/modules/luo_pci_test_driver.c | 279 ++++++++++++++++++
.../liveupdate/pci/luo_pci_test_driver.h | 52 ++++
6 files changed, 358 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/modules/Makefile
create mode 100644 tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h

diff --git a/MAINTAINERS b/MAINTAINERS
index ea273cec96e9..f704877b8a38 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -21652,6 +21652,8 @@ F: drivers/pci/liveupdate.c
F: drivers/pci/liveupdate.h
F: include/linux/kho/abi/pci.h
F: include/linux/pci_liveupdate.h
+F: tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
+F: tools/testing/selftests/liveupdate/pci/

PCI MSI DRIVER FOR ALTERA MSI IP
L: linux-pci@xxxxxxxxxxxxxxx
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
index 47c670bf532b..9bf744e4f138 100644
--- a/tools/testing/selftests/liveupdate/.gitignore
+++ b/tools/testing/selftests/liveupdate/.gitignore
@@ -9,3 +9,6 @@
!config
!config.*
!Makefile
+
+# Generated by kbuild when building modules/ in place
+*.mod.c
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index df0567e8ca15..2547af3c5353 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -16,6 +16,8 @@ TEST_GEN_PROGS_EXTENDED += luo_stress_files

TEST_FILES += do_kexec.sh

+TEST_GEN_MODS_DIR := modules
+
include ../lib.mk
include lib/libliveupdate.mk

diff --git a/tools/testing/selftests/liveupdate/modules/Makefile b/tools/testing/selftests/liveupdate/modules/Makefile
new file mode 100644
index 000000000000..6efbbd21ecc5
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/Makefile
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel modules used by the Live Update selftests.
+
+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= /lib/modules/$(shell uname -r)/build
+
+obj-m += luo_pci_test_driver.o
+
+# Ensure that KDIR exists, otherwise skip the compilation
+modules:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) modules M=$(TESTMODS_DIR)
+endif
+
+# Ensure that KDIR exists, otherwise skip the clean target
+clean:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) clean M=$(TESTMODS_DIR)
+endif
diff --git a/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
new file mode 100644
index 000000000000..7c595c9b4653
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
@@ -0,0 +1,279 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test-only PCI driver for the PCI Live Update selftests.
+ *
+ * Binds to Intel 82576 (IGB) devices and lets userspace preserve them across a
+ * Live Update. Userspace gets a file for a bound device from the
+ * /dev/luo_pci_test_driver misc device, and preserves that file in a Live
+ * Update session. Other than enabling the device and bus mastering, the driver
+ * leaves the device alone so that the selftests can check how the PCI core
+ * treats it.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@xxxxxxxxxx>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/fs.h>
+#include <linux/liveupdate.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/pci_liveupdate.h>
+#include <linux/uaccess.h>
+
+#include "../pci/luo_pci_test_driver.h"
+
+#define LUPT_FILE_NAME "[luo_pci_test_driver]"
+
+struct lupt_device {
+ bool incoming_at_probe;
+};
+
+static struct pci_driver lupt_driver;
+
+/* Returns a reference to a device bound to this driver, or NULL. */
+static struct pci_dev *lupt_get_bound_device(u32 domain, u16 bdf)
+{
+ struct pci_dev *pdev;
+
+ pdev = pci_get_domain_bus_and_slot(domain, PCI_BUS_NUM(bdf), bdf & 0xff);
+ if (!pdev)
+ return NULL;
+
+ if (READ_ONCE(pdev->driver) != &lupt_driver) {
+ pci_dev_put(pdev);
+ return NULL;
+ }
+
+ return pdev;
+}
+
+static long lupt_device_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev = {};
+ struct pci_dev *pdev = file->private_data;
+ struct lupt_device *ldev;
+
+ if (cmd != LUO_PCI_TEST_GET_STATUS)
+ return -ENOTTY;
+
+ dev.domain = pci_domain_nr(pdev->bus);
+ dev.bdf = pci_dev_id(pdev);
+
+ device_lock(&pdev->dev);
+ if (pdev->driver == &lupt_driver) {
+ ldev = pci_get_drvdata(pdev);
+ if (ldev->incoming_at_probe)
+ dev.flags |= LUO_PCI_TEST_INCOMING_AT_PROBE;
+ }
+ device_unlock(&pdev->dev);
+
+ if (pci_liveupdate_is_incoming(pdev))
+ dev.flags |= LUO_PCI_TEST_INCOMING;
+
+ if (copy_to_user((void __user *)arg, &dev, sizeof(dev)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int lupt_device_release(struct inode *inode, struct file *file)
+{
+ pci_dev_put(file->private_data);
+ return 0;
+}
+
+static const struct file_operations lupt_device_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_device_ioctl,
+ .release = lupt_device_release,
+};
+
+static long lupt_ctl_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev;
+ struct pci_dev *pdev;
+ int fd;
+
+ if (cmd != LUO_PCI_TEST_GET_DEVICE_FD)
+ return -ENOTTY;
+
+ if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
+ return -EFAULT;
+
+ pdev = lupt_get_bound_device(dev.domain, dev.bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ /* On success, releasing the file drops the reference to pdev. */
+ fd = anon_inode_getfd(LUPT_FILE_NAME, &lupt_device_fops, pdev,
+ O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ pci_dev_put(pdev);
+
+ return fd;
+}
+
+static const struct file_operations lupt_ctl_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_ctl_ioctl,
+};
+
+static struct miscdevice lupt_miscdev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "luo_pci_test_driver",
+ .fops = &lupt_ctl_fops,
+};
+
+static bool lupt_can_preserve(struct liveupdate_file_handler *handler,
+ struct file *file)
+{
+ return file->f_op == &lupt_device_fops;
+}
+
+static int lupt_preserve(struct liveupdate_file_op_args *args)
+{
+ struct pci_dev *pdev = args->file->private_data;
+ int ret;
+
+ ret = pci_liveupdate_preserve(pdev);
+ if (ret)
+ return ret;
+
+ args->serialized_data = (u64)pci_domain_nr(pdev->bus) << 16 | pci_dev_id(pdev);
+ return 0;
+}
+
+static void lupt_unpreserve(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_unpreserve(args->file->private_data);
+}
+
+static int lupt_retrieve(struct liveupdate_file_op_args *args)
+{
+ u32 domain = args->serialized_data >> 16;
+ u16 bdf = args->serialized_data & 0xffff;
+ struct pci_dev *pdev;
+ struct file *file;
+
+ /* Userspace must load this driver before retrieving the device. */
+ pdev = lupt_get_bound_device(domain, bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ file = anon_inode_getfile(LUPT_FILE_NAME, &lupt_device_fops, pdev, O_RDWR);
+ if (IS_ERR(file)) {
+ pci_dev_put(pdev);
+ return PTR_ERR(file);
+ }
+
+ args->file = file;
+ return 0;
+}
+
+static bool lupt_can_finish(struct liveupdate_file_op_args *args)
+{
+ return args->retrieve_status > 0;
+}
+
+static void lupt_finish(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_finish(args->file->private_data);
+}
+
+static const struct liveupdate_file_ops lupt_file_ops = {
+ .can_preserve = lupt_can_preserve,
+ .preserve = lupt_preserve,
+ .unpreserve = lupt_unpreserve,
+ .retrieve = lupt_retrieve,
+ .can_finish = lupt_can_finish,
+ .finish = lupt_finish,
+ .owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler lupt_fh = {
+ .ops = &lupt_file_ops,
+ .compatible = "luo-pci-test-driver-v1",
+};
+
+static int lupt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct lupt_device *ldev;
+ int ret;
+
+ ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
+ if (!ldev)
+ return -ENOMEM;
+
+ ldev->incoming_at_probe = pci_liveupdate_is_incoming(pdev);
+
+ ret = pcim_enable_device(pdev);
+ if (ret)
+ return ret;
+
+ pci_set_master(pdev);
+ pci_set_drvdata(pdev, ldev);
+
+ pci_info(pdev, "Bound to %s device\n",
+ ldev->incoming_at_probe ? "incoming-preserved" : "non-preserved");
+ return 0;
+}
+
+static const struct pci_device_id lupt_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x10c9) }, /* 82576 (QEMU igb) */
+ {}
+};
+
+static struct pci_driver lupt_driver = {
+ .name = "luo_pci_test_driver",
+ .id_table = lupt_ids,
+ .probe = lupt_probe,
+};
+
+static int __init lupt_init(void)
+{
+ int ret;
+
+ ret = liveupdate_register_file_handler(&lupt_fh);
+ if (ret)
+ return ret;
+
+ ret = pci_liveupdate_register_flb(&lupt_fh);
+ if (ret)
+ goto err_unregister_fh;
+
+ ret = misc_register(&lupt_miscdev);
+ if (ret)
+ goto err_unregister_flb;
+
+ ret = pci_register_driver(&lupt_driver);
+ if (ret)
+ goto err_deregister_misc;
+
+ return 0;
+
+err_deregister_misc:
+ misc_deregister(&lupt_miscdev);
+err_unregister_flb:
+ pci_liveupdate_unregister_flb(&lupt_fh);
+err_unregister_fh:
+ liveupdate_unregister_file_handler(&lupt_fh);
+ return ret;
+}
+module_init(lupt_init);
+
+static void __exit lupt_exit(void)
+{
+ pci_unregister_driver(&lupt_driver);
+ misc_deregister(&lupt_miscdev);
+ pci_liveupdate_unregister_flb(&lupt_fh);
+ liveupdate_unregister_file_handler(&lupt_fh);
+}
+module_exit(lupt_exit);
+
+MODULE_DESCRIPTION("Test-only PCI driver for the PCI Live Update selftests");
+MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
new file mode 100644
index 000000000000..c5c00e4eda02
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Interface between the PCI Live Update selftests and the luo_pci_test_driver
+ * kernel module.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@xxxxxxxxxx>
+ */
+#ifndef SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+#define SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define LUO_PCI_TEST_DEVICE "/dev/luo_pci_test_driver"
+#define LUO_PCI_TEST_MODULE "/luo_pci_test_driver.ko"
+
+/* The device was incoming-preserved when the driver probed it. */
+#define LUO_PCI_TEST_INCOMING_AT_PROBE (1 << 0)
+/* The device is currently incoming-preserved. */
+#define LUO_PCI_TEST_INCOMING (1 << 1)
+
+/**
+ * struct luo_pci_test_device - A PCI device and its status
+ * @domain: PCI domain (segment) number
+ * @bdf: PCI bus, device and function number
+ * @flags: LUO_PCI_TEST_* flags, set by LUO_PCI_TEST_GET_STATUS
+ */
+struct luo_pci_test_device {
+ __u32 domain;
+ __u16 bdf;
+ __u16 flags;
+};
+
+#define LUO_PCI_TEST_IOC_TYPE 0xB8
+
+/*
+ * Issued on LUO_PCI_TEST_DEVICE. Returns a new file descriptor for a
+ * device bound to luo_pci_test_driver, which can be preserved in a Live
+ * Update session.
+ */
+#define LUO_PCI_TEST_GET_DEVICE_FD \
+ _IOW(LUO_PCI_TEST_IOC_TYPE, 0, struct luo_pci_test_device)
+
+/*
+ * Issued on a device file descriptor. Returns the device's domain, BDF and
+ * LUO_PCI_TEST_* flags.
+ */
+#define LUO_PCI_TEST_GET_STATUS \
+ _IOR(LUO_PCI_TEST_IOC_TYPE, 1, struct luo_pci_test_device)
+
+#endif /* SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H */
--
2.56.1.911.g883d8bcb20-goog