From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796172012; b=NVPYYfxdXWMCYWPuOla8ZtwFdIufFA58om/Q9Yc+Xx3GJwwNsdeGCJkbMiz9Ue1LFxe+ FufQkOLkB8PDCm5jRVt1zI12eqjmey82IeDrUZVfdu6W7Snx3Zebnx+eRNZn6AIOBtwU6 9TKqN5WOOfmfQhGUiys17LlP7VjwUWI48lQDhsPt5VAuv9soMtn8XgCi17EX3hqP6malv 2BHNjlsPYrqio3863y/aqc1uCQFh5NI6/vwa2zqTZsj0bYd40FJl8H8rGdbuel8ibhB5K CJeJYBzSa4zUeewzduJxHToMKPIjmuamzDVX3ey36ITpHObWCCxofv6NKI7PKsrIyqw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796172012; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=bd+HvVQtnsLSkOpb9xsNUpo6bGcW9+ZavNP3z2AVSB4=; b=goipMWk2yN2YY1ihWF8BhUl/NDvmeR3fkql1nPACCtjrFZf4hZXeJ9fEHyqeWrERJinB lngn3nJeZhHLxFBMYv8qvVNWe8DEJlDD/6a8BrUQac97u9QYtulwSrl2802Vo+LgcgrQr Gmx0R3IRuuH33xD8p/YIIz/TghdoUvd4jorAATkE4OV2xmRsHMyq6/pLyV7RTSA6l54r7 1euNMbMFE62CJHIKc490ubGwWbGCudB9ZXjrdi0yqgsWttxMFrAv2Z34VdLz3NkmX+RA3 C810VF3HRBJPc57sZoprdErOBmgjFpLd3hzQNs3d6rTwIsFvof58Ht8qVucshvB2g+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PDLikWkx; arc=none smtp.remote-ip=209.85.167.199 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B54460A3815 for ; Wed, 2 Dec 2026 00:40:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0B54460A3815 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=PDLikWkx Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-57e469f8312so5399561b6e.1 for ; Tue, 01 Dec 2026 16:40:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796172010; x=1796776810; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=bd+HvVQtnsLSkOpb9xsNUpo6bGcW9+ZavNP3z2AVSB4=; b=PDLikWkxtJGCs1kSRcuw0ikbXymdpeOnlRrqpLVp2+G8EqcSmkc0K4pI4lEJn0Bh9g cC44NAwmDqk09HWlHedKr8UFS2WKn2oYOS2eZ4Rd6nhFMhX2/CSil1b1ImEwojR2/Z5t 9gxZdHyqMdxirjal+JjoeDb7vFTFyVcVv/Bulg2fY2sWc31gIxnQmTZlGIRfyrjMS7S4 I9O910Ty9xvtMHrS7BUJtXkk8GwsXgNqKvk1E4pfp+S3qNJCL84i1Jb/jWnn5PWwftIe R9QESEbR/RVF5WBV4ZxDHkvPEQ6fGEOPr6XWWs/WKoTkpC9kER1wW56PlM/B+egmdT5b g+xA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796172010; x=1796776810; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bd+HvVQtnsLSkOpb9xsNUpo6bGcW9+ZavNP3z2AVSB4=; b=LiIycpVeHAkDt5MUroIUQ+D04uMlFm6QNsehyuISzHa8IgnyzbgqQIoETWjXA6w4Rq TkTkSwCLT68u86zOlASa3BFEHSRgrzqCzTGQ8UFlJJaoXwRYb7QQuTd7/63JbPen9M4U 2fcORjSlWysf/vnWbicP1vvCJ2c+te+uWw4TrLFJlOO2TYaiRZMTyCxpvcY1pdtSW7mm s2I+7yBkhuhOTBWhodkiRPj56ZW1dBW4FU2h06FSMeRPXh0YC4afacyRonO3c0ngnPrH f6dPqTVy4qg5SC+XD3j0g+2Y3S6Mwa3h82stxu5yYbK3rQlOyzGWtr9jbTvndIxWIaV9 YIQw== X-Forwarded-Encrypted: i=1; AL+hErECXw5XLipT98zrF4m2VW+vLTvkCw8PRUOvG2YHynLAFAgTj2GCXY7mslLHBwrzR3g9AKGkorIx/O2quCc=@vger.kernel.org X-Gm-Message-State: APuU4Z9/khqWKgvZ2YUtuBdhnfZ02G0KYs90e7+3vpGt1qpIQtkr57S2 Ew3TMqk+umiftAgB8fRIEl24lmFeA+BF3S8YCGnezUgCgcAac/fe1BrGunVngsZwYCwRiMpxlqW DjyFpCLIkbTLl6A== X-Received: from iocrb10-n2.prod.google.com ([2002:a05:6602:a58a:20b0:9c7:4d20:1320]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1314:b0:53d:c86f:d331 with SMTP id 5614622812f47-57a06b2181bmr37361651b6e.5.1796172009877; Tue, 01 Dec 2026 16:40:09 -0800 (PST) Date: Wed, 2 Dec 2026 00:39:47 +0000 In-Reply-To: <20261202003955.3607156-1-dmatlack@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202003955.3607156-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202003955.3607156-11-dmatlack@google.com> Subject: [PATCH 10/18] selftests/liveupdate: Add a test driver for PCI Live Update From: David Matlack 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 , Alexandre Ghiti , Alex Williamson , Bjorn Helgaas , David Matlack , Jacob Pan , Jason Gunthorpe , Josh Hilke , Lukas Wunner , Mike Rapoport , Palmer Dabbelt , Parav Pandit , Pasha Tatashin , Paul Walmsley , Pranjal Shrivastava , Pratyush Yadav , Samiullah Khawaja , Shuah Khan , Vipin Sharma Content-Type: text/plain; charset="UTF-8" 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 --- 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 + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +#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 + */ +#ifndef SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H +#define SELFTESTS_LIVEUPDATE_PCI_LUO_PCI_TEST_DRIVER_H + +#include +#include + +#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