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 10/18] selftests/liveupdate: Add a test driver for PCI Live Update
Date: Wed, 2 Dec 2026 00:39:47 +0000 [thread overview]
Message-ID: <20261202003955.3607156-11-dmatlack@google.com> (raw)
In-Reply-To: <20261202003955.3607156-1-dmatlack@google.com>
Add luo_pci_test_driver.ko, a test-only PCI driver that the PCI Live
Update selftests use to preserve PCI devices. The driver binds to Intel
82576 (IGB) devices, such as QEMU's igb device, and hands out a file for
each of its devices through /dev/luo_pci_test_driver. Userspace can
preserve these files in a Live Update session, and the driver's Live
Update file handler then preserves, unpreserves, retrieves and finishes
the device with the PCI Live Update APIs. Other than enabling the device
and bus mastering, the driver leaves the device alone, so that tests can
check how the PCI core treats it.
The driver lives in modules/, a directory for the kernel modules used
by the Live Update selftests, which the selftests build through
TEST_GEN_MODS_DIR.
Add the driver, and tools/testing/selftests/liveupdate/pci/ where its
interface header lives, to the PCI LIVE UPDATE entry in MAINTAINERS.
Signed-off-by: David Matlack <dmatlack@google.com>
---
MAINTAINERS | 2 +
tools/testing/selftests/liveupdate/.gitignore | 3 +
tools/testing/selftests/liveupdate/Makefile | 2 +
.../selftests/liveupdate/modules/Makefile | 20 ++
.../liveupdate/modules/luo_pci_test_driver.c | 279 ++++++++++++++++++
.../liveupdate/pci/luo_pci_test_driver.h | 52 ++++
6 files changed, 358 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/modules/Makefile
create mode 100644 tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
diff --git a/MAINTAINERS b/MAINTAINERS
index ea273cec96e9..f704877b8a38 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -21652,6 +21652,8 @@ F: drivers/pci/liveupdate.c
F: drivers/pci/liveupdate.h
F: include/linux/kho/abi/pci.h
F: include/linux/pci_liveupdate.h
+F: tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
+F: tools/testing/selftests/liveupdate/pci/
PCI MSI DRIVER FOR ALTERA MSI IP
L: linux-pci@vger.kernel.org
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
index 47c670bf532b..9bf744e4f138 100644
--- a/tools/testing/selftests/liveupdate/.gitignore
+++ b/tools/testing/selftests/liveupdate/.gitignore
@@ -9,3 +9,6 @@
!config
!config.*
!Makefile
+
+# Generated by kbuild when building modules/ in place
+*.mod.c
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index df0567e8ca15..2547af3c5353 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -16,6 +16,8 @@ TEST_GEN_PROGS_EXTENDED += luo_stress_files
TEST_FILES += do_kexec.sh
+TEST_GEN_MODS_DIR := modules
+
include ../lib.mk
include lib/libliveupdate.mk
diff --git a/tools/testing/selftests/liveupdate/modules/Makefile b/tools/testing/selftests/liveupdate/modules/Makefile
new file mode 100644
index 000000000000..6efbbd21ecc5
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/Makefile
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel modules used by the Live Update selftests.
+
+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= /lib/modules/$(shell uname -r)/build
+
+obj-m += luo_pci_test_driver.o
+
+# Ensure that KDIR exists, otherwise skip the compilation
+modules:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) modules M=$(TESTMODS_DIR)
+endif
+
+# Ensure that KDIR exists, otherwise skip the clean target
+clean:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) clean M=$(TESTMODS_DIR)
+endif
diff --git a/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
new file mode 100644
index 000000000000..7c595c9b4653
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
@@ -0,0 +1,279 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test-only PCI driver for the PCI Live Update selftests.
+ *
+ * Binds to Intel 82576 (IGB) devices and lets userspace preserve them across a
+ * Live Update. Userspace gets a file for a bound device from the
+ * /dev/luo_pci_test_driver misc device, and preserves that file in a Live
+ * Update session. Other than enabling the device and bus mastering, the driver
+ * leaves the device alone so that the selftests can check how the PCI core
+ * treats it.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/fs.h>
+#include <linux/liveupdate.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/pci_liveupdate.h>
+#include <linux/uaccess.h>
+
+#include "../pci/luo_pci_test_driver.h"
+
+#define LUPT_FILE_NAME "[luo_pci_test_driver]"
+
+struct lupt_device {
+ bool incoming_at_probe;
+};
+
+static struct pci_driver lupt_driver;
+
+/* Returns a reference to a device bound to this driver, or NULL. */
+static struct pci_dev *lupt_get_bound_device(u32 domain, u16 bdf)
+{
+ struct pci_dev *pdev;
+
+ pdev = pci_get_domain_bus_and_slot(domain, PCI_BUS_NUM(bdf), bdf & 0xff);
+ if (!pdev)
+ return NULL;
+
+ if (READ_ONCE(pdev->driver) != &lupt_driver) {
+ pci_dev_put(pdev);
+ return NULL;
+ }
+
+ return pdev;
+}
+
+static long lupt_device_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev = {};
+ struct pci_dev *pdev = file->private_data;
+ struct lupt_device *ldev;
+
+ if (cmd != LUO_PCI_TEST_GET_STATUS)
+ return -ENOTTY;
+
+ dev.domain = pci_domain_nr(pdev->bus);
+ dev.bdf = pci_dev_id(pdev);
+
+ device_lock(&pdev->dev);
+ if (pdev->driver == &lupt_driver) {
+ ldev = pci_get_drvdata(pdev);
+ if (ldev->incoming_at_probe)
+ dev.flags |= LUO_PCI_TEST_INCOMING_AT_PROBE;
+ }
+ device_unlock(&pdev->dev);
+
+ if (pci_liveupdate_is_incoming(pdev))
+ dev.flags |= LUO_PCI_TEST_INCOMING;
+
+ if (copy_to_user((void __user *)arg, &dev, sizeof(dev)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int lupt_device_release(struct inode *inode, struct file *file)
+{
+ pci_dev_put(file->private_data);
+ return 0;
+}
+
+static const struct file_operations lupt_device_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_device_ioctl,
+ .release = lupt_device_release,
+};
+
+static long lupt_ctl_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev;
+ struct pci_dev *pdev;
+ int fd;
+
+ if (cmd != LUO_PCI_TEST_GET_DEVICE_FD)
+ return -ENOTTY;
+
+ if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
+ return -EFAULT;
+
+ pdev = lupt_get_bound_device(dev.domain, dev.bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ /* On success, releasing the file drops the reference to pdev. */
+ fd = anon_inode_getfd(LUPT_FILE_NAME, &lupt_device_fops, pdev,
+ O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ pci_dev_put(pdev);
+
+ return fd;
+}
+
+static const struct file_operations lupt_ctl_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_ctl_ioctl,
+};
+
+static struct miscdevice lupt_miscdev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "luo_pci_test_driver",
+ .fops = &lupt_ctl_fops,
+};
+
+static bool lupt_can_preserve(struct liveupdate_file_handler *handler,
+ struct file *file)
+{
+ return file->f_op == &lupt_device_fops;
+}
+
+static int lupt_preserve(struct liveupdate_file_op_args *args)
+{
+ struct pci_dev *pdev = args->file->private_data;
+ int ret;
+
+ ret = pci_liveupdate_preserve(pdev);
+ if (ret)
+ return ret;
+
+ args->serialized_data = (u64)pci_domain_nr(pdev->bus) << 16 | pci_dev_id(pdev);
+ return 0;
+}
+
+static void lupt_unpreserve(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_unpreserve(args->file->private_data);
+}
+
+static int lupt_retrieve(struct liveupdate_file_op_args *args)
+{
+ u32 domain = args->serialized_data >> 16;
+ u16 bdf = args->serialized_data & 0xffff;
+ struct pci_dev *pdev;
+ struct file *file;
+
+ /* Userspace must load this driver before retrieving the device. */
+ pdev = lupt_get_bound_device(domain, bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ file = anon_inode_getfile(LUPT_FILE_NAME, &lupt_device_fops, pdev, O_RDWR);
+ if (IS_ERR(file)) {
+ pci_dev_put(pdev);
+ return PTR_ERR(file);
+ }
+
+ args->file = file;
+ return 0;
+}
+
+static bool lupt_can_finish(struct liveupdate_file_op_args *args)
+{
+ return args->retrieve_status > 0;
+}
+
+static void lupt_finish(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_finish(args->file->private_data);
+}
+
+static const struct liveupdate_file_ops lupt_file_ops = {
+ .can_preserve = lupt_can_preserve,
+ .preserve = lupt_preserve,
+ .unpreserve = lupt_unpreserve,
+ .retrieve = lupt_retrieve,
+ .can_finish = lupt_can_finish,
+ .finish = lupt_finish,
+ .owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler lupt_fh = {
+ .ops = &lupt_file_ops,
+ .compatible = "luo-pci-test-driver-v1",
+};
+
+static int lupt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct lupt_device *ldev;
+ int ret;
+
+ ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
+ if (!ldev)
+ return -ENOMEM;
+
+ ldev->incoming_at_probe = pci_liveupdate_is_incoming(pdev);
+
+ ret = pcim_enable_device(pdev);
+ if (ret)
+ return ret;
+
+ pci_set_master(pdev);
+ pci_set_drvdata(pdev, ldev);
+
+ pci_info(pdev, "Bound to %s device\n",
+ ldev->incoming_at_probe ? "incoming-preserved" : "non-preserved");
+ return 0;
+}
+
+static const struct pci_device_id lupt_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x10c9) }, /* 82576 (QEMU igb) */
+ {}
+};
+
+static struct pci_driver lupt_driver = {
+ .name = "luo_pci_test_driver",
+ .id_table = lupt_ids,
+ .probe = lupt_probe,
+};
+
+static int __init lupt_init(void)
+{
+ int ret;
+
+ ret = liveupdate_register_file_handler(&lupt_fh);
+ if (ret)
+ return ret;
+
+ ret = pci_liveupdate_register_flb(&lupt_fh);
+ if (ret)
+ goto err_unregister_fh;
+
+ ret = misc_register(&lupt_miscdev);
+ if (ret)
+ goto err_unregister_flb;
+
+ ret = pci_register_driver(&lupt_driver);
+ if (ret)
+ goto err_deregister_misc;
+
+ return 0;
+
+err_deregister_misc:
+ misc_deregister(&lupt_miscdev);
+err_unregister_flb:
+ pci_liveupdate_unregister_flb(&lupt_fh);
+err_unregister_fh:
+ liveupdate_unregister_file_handler(&lupt_fh);
+ return ret;
+}
+module_init(lupt_init);
+
+static void __exit lupt_exit(void)
+{
+ pci_unregister_driver(&lupt_driver);
+ misc_deregister(&lupt_miscdev);
+ pci_liveupdate_unregister_flb(&lupt_fh);
+ liveupdate_unregister_file_handler(&lupt_fh);
+}
+module_exit(lupt_exit);
+
+MODULE_DESCRIPTION("Test-only PCI driver for the PCI Live Update selftests");
+MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
new file mode 100644
index 000000000000..c5c00e4eda02
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Interface between the PCI Live Update selftests and the luo_pci_test_driver
+ * kernel module.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#ifndef SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+#define SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define LUO_PCI_TEST_DEVICE "/dev/luo_pci_test_driver"
+#define LUO_PCI_TEST_MODULE "/luo_pci_test_driver.ko"
+
+/* The device was incoming-preserved when the driver probed it. */
+#define LUO_PCI_TEST_INCOMING_AT_PROBE (1 << 0)
+/* The device is currently incoming-preserved. */
+#define LUO_PCI_TEST_INCOMING (1 << 1)
+
+/**
+ * struct luo_pci_test_device - A PCI device and its status
+ * @domain: PCI domain (segment) number
+ * @bdf: PCI bus, device and function number
+ * @flags: LUO_PCI_TEST_* flags, set by LUO_PCI_TEST_GET_STATUS
+ */
+struct luo_pci_test_device {
+ __u32 domain;
+ __u16 bdf;
+ __u16 flags;
+};
+
+#define LUO_PCI_TEST_IOC_TYPE 0xB8
+
+/*
+ * Issued on LUO_PCI_TEST_DEVICE. Returns a new file descriptor for a
+ * device bound to luo_pci_test_driver, which can be preserved in a Live
+ * Update session.
+ */
+#define LUO_PCI_TEST_GET_DEVICE_FD \
+ _IOW(LUO_PCI_TEST_IOC_TYPE, 0, struct luo_pci_test_device)
+
+/*
+ * Issued on a device file descriptor. Returns the device's domain, BDF and
+ * LUO_PCI_TEST_* flags.
+ */
+#define LUO_PCI_TEST_GET_STATUS \
+ _IOR(LUO_PCI_TEST_IOC_TYPE, 1, struct luo_pci_test_device)
+
+#endif /* SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H */
--
2.56.1.911.g883d8bcb20-goog
WARNING: multiple messages have this Message-ID (diff)
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 10/18] selftests/liveupdate: Add a test driver for PCI Live Update
Date: Wed, 2 Dec 2026 00:39:47 +0000 [thread overview]
Message-ID: <20261202003955.3607156-11-dmatlack@google.com> (raw)
In-Reply-To: <20261202003955.3607156-1-dmatlack@google.com>
Add luo_pci_test_driver.ko, a test-only PCI driver that the PCI Live
Update selftests use to preserve PCI devices. The driver binds to Intel
82576 (IGB) devices, such as QEMU's igb device, and hands out a file for
each of its devices through /dev/luo_pci_test_driver. Userspace can
preserve these files in a Live Update session, and the driver's Live
Update file handler then preserves, unpreserves, retrieves and finishes
the device with the PCI Live Update APIs. Other than enabling the device
and bus mastering, the driver leaves the device alone, so that tests can
check how the PCI core treats it.
The driver lives in modules/, a directory for the kernel modules used
by the Live Update selftests, which the selftests build through
TEST_GEN_MODS_DIR.
Add the driver, and tools/testing/selftests/liveupdate/pci/ where its
interface header lives, to the PCI LIVE UPDATE entry in MAINTAINERS.
Signed-off-by: David Matlack <dmatlack@google.com>
---
MAINTAINERS | 2 +
tools/testing/selftests/liveupdate/.gitignore | 3 +
tools/testing/selftests/liveupdate/Makefile | 2 +
.../selftests/liveupdate/modules/Makefile | 20 ++
.../liveupdate/modules/luo_pci_test_driver.c | 279 ++++++++++++++++++
.../liveupdate/pci/luo_pci_test_driver.h | 52 ++++
6 files changed, 358 insertions(+)
create mode 100644 tools/testing/selftests/liveupdate/modules/Makefile
create mode 100644 tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
create mode 100644 tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
diff --git a/MAINTAINERS b/MAINTAINERS
index ea273cec96e9..f704877b8a38 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -21652,6 +21652,8 @@ F: drivers/pci/liveupdate.c
F: drivers/pci/liveupdate.h
F: include/linux/kho/abi/pci.h
F: include/linux/pci_liveupdate.h
+F: tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
+F: tools/testing/selftests/liveupdate/pci/
PCI MSI DRIVER FOR ALTERA MSI IP
L: linux-pci@vger.kernel.org
diff --git a/tools/testing/selftests/liveupdate/.gitignore b/tools/testing/selftests/liveupdate/.gitignore
index 47c670bf532b..9bf744e4f138 100644
--- a/tools/testing/selftests/liveupdate/.gitignore
+++ b/tools/testing/selftests/liveupdate/.gitignore
@@ -9,3 +9,6 @@
!config
!config.*
!Makefile
+
+# Generated by kbuild when building modules/ in place
+*.mod.c
diff --git a/tools/testing/selftests/liveupdate/Makefile b/tools/testing/selftests/liveupdate/Makefile
index df0567e8ca15..2547af3c5353 100644
--- a/tools/testing/selftests/liveupdate/Makefile
+++ b/tools/testing/selftests/liveupdate/Makefile
@@ -16,6 +16,8 @@ TEST_GEN_PROGS_EXTENDED += luo_stress_files
TEST_FILES += do_kexec.sh
+TEST_GEN_MODS_DIR := modules
+
include ../lib.mk
include lib/libliveupdate.mk
diff --git a/tools/testing/selftests/liveupdate/modules/Makefile b/tools/testing/selftests/liveupdate/modules/Makefile
new file mode 100644
index 000000000000..6efbbd21ecc5
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/Makefile
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel modules used by the Live Update selftests.
+
+TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
+KDIR ?= /lib/modules/$(shell uname -r)/build
+
+obj-m += luo_pci_test_driver.o
+
+# Ensure that KDIR exists, otherwise skip the compilation
+modules:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) modules M=$(TESTMODS_DIR)
+endif
+
+# Ensure that KDIR exists, otherwise skip the clean target
+clean:
+ifneq ("$(wildcard $(KDIR))", "")
+ $(Q)$(MAKE) -C $(KDIR) clean M=$(TESTMODS_DIR)
+endif
diff --git a/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
new file mode 100644
index 000000000000..7c595c9b4653
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/modules/luo_pci_test_driver.c
@@ -0,0 +1,279 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test-only PCI driver for the PCI Live Update selftests.
+ *
+ * Binds to Intel 82576 (IGB) devices and lets userspace preserve them across a
+ * Live Update. Userspace gets a file for a bound device from the
+ * /dev/luo_pci_test_driver misc device, and preserves that file in a Live
+ * Update session. Other than enabling the device and bus mastering, the driver
+ * leaves the device alone so that the selftests can check how the PCI core
+ * treats it.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/anon_inodes.h>
+#include <linux/fs.h>
+#include <linux/liveupdate.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/pci_liveupdate.h>
+#include <linux/uaccess.h>
+
+#include "../pci/luo_pci_test_driver.h"
+
+#define LUPT_FILE_NAME "[luo_pci_test_driver]"
+
+struct lupt_device {
+ bool incoming_at_probe;
+};
+
+static struct pci_driver lupt_driver;
+
+/* Returns a reference to a device bound to this driver, or NULL. */
+static struct pci_dev *lupt_get_bound_device(u32 domain, u16 bdf)
+{
+ struct pci_dev *pdev;
+
+ pdev = pci_get_domain_bus_and_slot(domain, PCI_BUS_NUM(bdf), bdf & 0xff);
+ if (!pdev)
+ return NULL;
+
+ if (READ_ONCE(pdev->driver) != &lupt_driver) {
+ pci_dev_put(pdev);
+ return NULL;
+ }
+
+ return pdev;
+}
+
+static long lupt_device_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev = {};
+ struct pci_dev *pdev = file->private_data;
+ struct lupt_device *ldev;
+
+ if (cmd != LUO_PCI_TEST_GET_STATUS)
+ return -ENOTTY;
+
+ dev.domain = pci_domain_nr(pdev->bus);
+ dev.bdf = pci_dev_id(pdev);
+
+ device_lock(&pdev->dev);
+ if (pdev->driver == &lupt_driver) {
+ ldev = pci_get_drvdata(pdev);
+ if (ldev->incoming_at_probe)
+ dev.flags |= LUO_PCI_TEST_INCOMING_AT_PROBE;
+ }
+ device_unlock(&pdev->dev);
+
+ if (pci_liveupdate_is_incoming(pdev))
+ dev.flags |= LUO_PCI_TEST_INCOMING;
+
+ if (copy_to_user((void __user *)arg, &dev, sizeof(dev)))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int lupt_device_release(struct inode *inode, struct file *file)
+{
+ pci_dev_put(file->private_data);
+ return 0;
+}
+
+static const struct file_operations lupt_device_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_device_ioctl,
+ .release = lupt_device_release,
+};
+
+static long lupt_ctl_ioctl(struct file *file, unsigned int cmd,
+ unsigned long arg)
+{
+ struct luo_pci_test_device dev;
+ struct pci_dev *pdev;
+ int fd;
+
+ if (cmd != LUO_PCI_TEST_GET_DEVICE_FD)
+ return -ENOTTY;
+
+ if (copy_from_user(&dev, (void __user *)arg, sizeof(dev)))
+ return -EFAULT;
+
+ pdev = lupt_get_bound_device(dev.domain, dev.bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ /* On success, releasing the file drops the reference to pdev. */
+ fd = anon_inode_getfd(LUPT_FILE_NAME, &lupt_device_fops, pdev,
+ O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ pci_dev_put(pdev);
+
+ return fd;
+}
+
+static const struct file_operations lupt_ctl_fops = {
+ .owner = THIS_MODULE,
+ .unlocked_ioctl = lupt_ctl_ioctl,
+};
+
+static struct miscdevice lupt_miscdev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "luo_pci_test_driver",
+ .fops = &lupt_ctl_fops,
+};
+
+static bool lupt_can_preserve(struct liveupdate_file_handler *handler,
+ struct file *file)
+{
+ return file->f_op == &lupt_device_fops;
+}
+
+static int lupt_preserve(struct liveupdate_file_op_args *args)
+{
+ struct pci_dev *pdev = args->file->private_data;
+ int ret;
+
+ ret = pci_liveupdate_preserve(pdev);
+ if (ret)
+ return ret;
+
+ args->serialized_data = (u64)pci_domain_nr(pdev->bus) << 16 | pci_dev_id(pdev);
+ return 0;
+}
+
+static void lupt_unpreserve(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_unpreserve(args->file->private_data);
+}
+
+static int lupt_retrieve(struct liveupdate_file_op_args *args)
+{
+ u32 domain = args->serialized_data >> 16;
+ u16 bdf = args->serialized_data & 0xffff;
+ struct pci_dev *pdev;
+ struct file *file;
+
+ /* Userspace must load this driver before retrieving the device. */
+ pdev = lupt_get_bound_device(domain, bdf);
+ if (!pdev)
+ return -ENODEV;
+
+ file = anon_inode_getfile(LUPT_FILE_NAME, &lupt_device_fops, pdev, O_RDWR);
+ if (IS_ERR(file)) {
+ pci_dev_put(pdev);
+ return PTR_ERR(file);
+ }
+
+ args->file = file;
+ return 0;
+}
+
+static bool lupt_can_finish(struct liveupdate_file_op_args *args)
+{
+ return args->retrieve_status > 0;
+}
+
+static void lupt_finish(struct liveupdate_file_op_args *args)
+{
+ pci_liveupdate_finish(args->file->private_data);
+}
+
+static const struct liveupdate_file_ops lupt_file_ops = {
+ .can_preserve = lupt_can_preserve,
+ .preserve = lupt_preserve,
+ .unpreserve = lupt_unpreserve,
+ .retrieve = lupt_retrieve,
+ .can_finish = lupt_can_finish,
+ .finish = lupt_finish,
+ .owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler lupt_fh = {
+ .ops = &lupt_file_ops,
+ .compatible = "luo-pci-test-driver-v1",
+};
+
+static int lupt_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+ struct lupt_device *ldev;
+ int ret;
+
+ ldev = devm_kzalloc(&pdev->dev, sizeof(*ldev), GFP_KERNEL);
+ if (!ldev)
+ return -ENOMEM;
+
+ ldev->incoming_at_probe = pci_liveupdate_is_incoming(pdev);
+
+ ret = pcim_enable_device(pdev);
+ if (ret)
+ return ret;
+
+ pci_set_master(pdev);
+ pci_set_drvdata(pdev, ldev);
+
+ pci_info(pdev, "Bound to %s device\n",
+ ldev->incoming_at_probe ? "incoming-preserved" : "non-preserved");
+ return 0;
+}
+
+static const struct pci_device_id lupt_ids[] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x10c9) }, /* 82576 (QEMU igb) */
+ {}
+};
+
+static struct pci_driver lupt_driver = {
+ .name = "luo_pci_test_driver",
+ .id_table = lupt_ids,
+ .probe = lupt_probe,
+};
+
+static int __init lupt_init(void)
+{
+ int ret;
+
+ ret = liveupdate_register_file_handler(&lupt_fh);
+ if (ret)
+ return ret;
+
+ ret = pci_liveupdate_register_flb(&lupt_fh);
+ if (ret)
+ goto err_unregister_fh;
+
+ ret = misc_register(&lupt_miscdev);
+ if (ret)
+ goto err_unregister_flb;
+
+ ret = pci_register_driver(&lupt_driver);
+ if (ret)
+ goto err_deregister_misc;
+
+ return 0;
+
+err_deregister_misc:
+ misc_deregister(&lupt_miscdev);
+err_unregister_flb:
+ pci_liveupdate_unregister_flb(&lupt_fh);
+err_unregister_fh:
+ liveupdate_unregister_file_handler(&lupt_fh);
+ return ret;
+}
+module_init(lupt_init);
+
+static void __exit lupt_exit(void)
+{
+ pci_unregister_driver(&lupt_driver);
+ misc_deregister(&lupt_miscdev);
+ pci_liveupdate_unregister_flb(&lupt_fh);
+ liveupdate_unregister_file_handler(&lupt_fh);
+}
+module_exit(lupt_exit);
+
+MODULE_DESCRIPTION("Test-only PCI driver for the PCI Live Update selftests");
+MODULE_LICENSE("GPL");
diff --git a/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
new file mode 100644
index 000000000000..c5c00e4eda02
--- /dev/null
+++ b/tools/testing/selftests/liveupdate/pci/luo_pci_test_driver.h
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Interface between the PCI Live Update selftests and the luo_pci_test_driver
+ * kernel module.
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <dmatlack@google.com>
+ */
+#ifndef SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+#define SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define LUO_PCI_TEST_DEVICE "/dev/luo_pci_test_driver"
+#define LUO_PCI_TEST_MODULE "/luo_pci_test_driver.ko"
+
+/* The device was incoming-preserved when the driver probed it. */
+#define LUO_PCI_TEST_INCOMING_AT_PROBE (1 << 0)
+/* The device is currently incoming-preserved. */
+#define LUO_PCI_TEST_INCOMING (1 << 1)
+
+/**
+ * struct luo_pci_test_device - A PCI device and its status
+ * @domain: PCI domain (segment) number
+ * @bdf: PCI bus, device and function number
+ * @flags: LUO_PCI_TEST_* flags, set by LUO_PCI_TEST_GET_STATUS
+ */
+struct luo_pci_test_device {
+ __u32 domain;
+ __u16 bdf;
+ __u16 flags;
+};
+
+#define LUO_PCI_TEST_IOC_TYPE 0xB8
+
+/*
+ * Issued on LUO_PCI_TEST_DEVICE. Returns a new file descriptor for a
+ * device bound to luo_pci_test_driver, which can be preserved in a Live
+ * Update session.
+ */
+#define LUO_PCI_TEST_GET_DEVICE_FD \
+ _IOW(LUO_PCI_TEST_IOC_TYPE, 0, struct luo_pci_test_device)
+
+/*
+ * Issued on a device file descriptor. Returns the device's domain, BDF and
+ * LUO_PCI_TEST_* flags.
+ */
+#define LUO_PCI_TEST_GET_STATUS \
+ _IOR(LUO_PCI_TEST_IOC_TYPE, 1, struct luo_pci_test_device)
+
+#endif /* SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H */
--
2.56.1.911.g883d8bcb20-goog
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2026-12-02 0:40 UTC|newest]
Thread overview: 43+ 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 ` David Matlack
2026-12-02 0:39 ` [PATCH 01/18] liveupdate: Export symbols needed by modules David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:45 ` sashiko-bot
2026-12-02 7:18 ` Christoph Hellwig
2026-12-02 7:18 ` Christoph Hellwig
2026-12-02 0:39 ` [PATCH 02/18] selftests/liveupdate: Exit with KSFT_SKIP when vmtest.sh skips a test David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:44 ` sashiko-bot
2026-12-02 0:39 ` [PATCH 03/18] selftests/liveupdate: Build objects in $(OUTPUT) David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 04/18] selftests/liveupdate: Support building the kexec tests against nolibc David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 05/18] selftests/liveupdate: Build vmtest.sh's binaries with the Makefile David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 06/18] selftests/liveupdate: Support test definitions in vmtest.sh David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 07/18] selftests/liveupdate: Support kernel modules " David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 08/18] selftests/liveupdate: Support a different command line for each kernel David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 09/18] selftests/liveupdate: Run tests in subdirectories from run-vmtests.sh David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` David Matlack [this message]
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 ` [PATCH 11/18] selftests/liveupdate: pci: Add a test with an endpoint below a root port David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 12/18] selftests/liveupdate: pci: Check PCI config writes with QEMU tracing David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 13/18] selftests/liveupdate: pci: Add a test with an endpoint on the root bus David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 14/18] selftests/liveupdate: pci: Add a test with an endpoint below a switch David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 15/18] selftests/liveupdate: pci: Test bus numbers with pci=assign-busses David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 16/18] selftests/liveupdate: pci: Test bus numbers with pci=hpbussize David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 17/18] selftests/liveupdate: pci: Test ARI Forwarding with pci=noari David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:39 ` [PATCH 18/18] selftests/liveupdate: pci: Test ACS with pci=config_acs David Matlack
2026-12-02 0:39 ` David Matlack
2026-12-02 0:50 ` [PATCH 00/18] selftests/liveupdate: Add PCI Live Update selftests sashiko-bot
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-11-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.