Skip to contentSkip to conversation

PCI: liveupdate: PCI core support for Live Update

PATCH Superseded David Matlack 7 participants 74 messages Jul 10 – Jul 27

Subsystems (5)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 cover PCI: liveupdate: PCI core support for Live Update

This series can be found on GitHub:

https://github.com/dmatlack/linux/tree/liveupdate/pci/base/v7

This series introduces initial support in the PCI core for Live Update, enabling drivers to preserve PCI devices across a kexec-based kernel update without interrupting the device. This functionality is critical for minimizing downtime in environments where PCI devices (e.g., those assigned to VMs via VFIO) must continue operating or maintain state across a host kernel upgrade.

Specifically, this series allows preserved PCI devices to perform uninterrupted memory transactions (DMA) to/from system memory across a Live Update. These devices can be behind a bridge but must not be VFs. Support for P2P and VF preservation will be addressed in future series.

Series Overview
---------------

This series implements the following to support PCI device preservation across Live Update:

1. Set up a File-Lifecycle-Bound (FLB) handler to track and preserve PCI-specific state (struct pci_ser) across Live Update using Kexec Handover (KHO).

2. Add APIs for drivers to register devices for preservation (outgoing) and for the PCI core to identify preserved devices during enumeration (incoming)

3. Automatically preserve all upstream bridges for any preserved endpoint. Use reference counting to ensure bridges remain preserved as long as any downstream device is preserved.

4. Guarantee that preserved devices retain the same RequesterID (bus, device, function) for the duration of their preservation by inheriting secondary bus numbers, subordinate bus numbers, and ARI Forwarding Enable on preserved bridges.

5. Guarantee that memory transactions to/from preserved devices are routed consistently by inheriting Access Control Services (ACS) flags across a Live Update, from the endpoint up to the root port.

6. Modify the PCI shutdown path to avoid disabling bus mastering on preserved devices, thereby allowing preserved devices to perform uninterrupted during kexec for Live Update.

7. Provide comprehensive documentation for the FLB API, device tracking mechanisms, and the division of responsibilities between the PCI core, drivers, and userspace.

Dependencies
------------

This series is built on top of the next branch of the liveupdate.git tree:

https://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git/log/?h=next

Testing
-------

This series was tested in conjunction with v4 of the VFIO PCI driver series:

https://lore.kernel.org/kvm/[email protected]/

The full set of patches used for testing can be found on GitHub. Some conflict resolution was required to rebase VFIO v4 patchs on top of the 7.2-rc1 base.

https://github.com/dmatlack/linux/tree/liveupdate/pci/base/v7-with-vfio

Testing was performed using the new VFIO selftests:

- vfio_pci_liveupdate_uapi_test
- vfio_pci_liveupdate_kexec_test

Both tests were run in a QEMU-based VM environment (using a single virtio-net PCIe device connected to a root port to exercise bridge support) and on bare metal using an Intel EMR server with 8x Intel DSA PCIe devices and 1x NVMe device.

Future Work
-----------

Following this series, we expect to make further improvements to the PCI core support for Live Update:

- Allow P2P across Live Update by avoiding resizing or moving preserved device BARs and preserving all upstream bridge windows.

- Support preserving Virtual Functions by preserving SR-IOV configuration on PFs and enumerating VFs after Live Update.

Changelog
---------

v7:
- Use kho_block_set for dynamic device preservation, redefining ABI struct pci_ser to use a physical address instead of a fixed-size flexible array (Pasha Tatashin)
- Add kernel-doc comments for pci_liveupdate_scan_bridge_begin() and pci_liveupdate_scan_bridge_end() (Pasha Tatashin)
- Move refcount to patch 1 and adjust KHO ABI versioning progression accordingly (Pasha Tatashin)
- Sphinx documentation improvements: add _luo and _flb labels and use acronyms with :ref: links (Pasha Tatashin)
- Panic on failure to retrieve incoming PCI FLB data to avoid ongoing DMA from preserved devices causing memory corruption (Sashiko, Samiullah Khawaja)
- Clarify comment about device-specific ACS quirks in incoming kernel (Pranjal Shrivastava)
- Sort includes alphabetically in liveupdate.c (Pasha Tatashin)

v6: https://lore.kernel.org/linux-pci/[email protected]/
v5: https://lore.kernel.org/linux-pci/[email protected]/
v4: https://lore.kernel.org/linux-pci/[email protected]/
v3: https://lore.kernel.org/kvm/[email protected]/
v2: https://lore.kernel.org/kvm/[email protected]/
v1: https://lore.kernel.org/kvm/[email protected]/
rfc: https://lore.kernel.org/kvm/[email protected]/

David Matlack (12):
PCI: liveupdate: Set up FLB handler for the PCI core
PCI: liveupdate: Track outgoing preserved PCI devices
PCI: liveupdate: Track incoming preserved PCI devices
PCI: liveupdate: Document driver binding responsibilities
PCI: liveupdate: Keep bus numbers constant during Live Update
PCI: liveupdate: Auto-preserve upstream bridges across Live Update
PCI: Refactor matching logic for pci_dev_acs_ops
PCI: liveupdate: Inherit ACS flags in incoming preserved devices
PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges
PCI: liveupdate: Freeze preservation status during shutdown
PCI: liveupdate: Do not disable bus mastering on preserved devices
during kexec
Documentation: PCI: Add documentation for Live Update

 Documentation/PCI/index.rst                   |   1 +
 Documentation/PCI/liveupdate.rst              |  29 +
 .../admin-guide/kernel-parameters.txt         |   6 +-
 Documentation/core-api/liveupdate.rst         |   5 +
 MAINTAINERS                                   |  15 +
 drivers/pci/Kconfig                           |  15 +
 drivers/pci/Makefile                          |   1 +
 drivers/pci/liveupdate.c                      | 910 ++++++++++++++++++
 drivers/pci/liveupdate.h                      |  68 ++
 drivers/pci/pci-driver.c                      |   9 +-
 drivers/pci/pci.c                             |  14 +-
 drivers/pci/pci.h                             |   5 +
 drivers/pci/probe.c                           |  22 +-
 drivers/pci/quirks.c                          |  58 +-
 include/linux/kho/abi/pci.h                   |  59 ++
 include/linux/pci.h                           |   4 +
 include/linux/pci_liveupdate.h                |  77 ++
 17 files changed, 1260 insertions(+), 38 deletions(-)
 create mode 100644 Documentation/PCI/liveupdate.rst
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 drivers/pci/liveupdate.h
 create mode 100644 include/linux/kho/abi/pci.h
 create mode 100644 include/linux/pci_liveupdate.h

base-commit: 1354afac14579ad38279f12d660df961b5f20246

Signature · 1 line
2.55.0.795.g602f6c329a-goog
#1 lore raw

PATCH v7 1/12 PCI: liveupdate: Set up FLB handler for the PCI core

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.

Preserving PCI devices across Live Update is built on top of the Live Update Orchestrator's (LUO) support for file preservation. Drivers are expected to expose a file to userspace to represent a single PCI device and support preservation of that file. This is intended primarily to support preservation of PCI devices bound to VFIO drivers.

This commit enables drivers to register their liveupdate_file_handler with the PCI core so that the PCI core can do its own tracking and enforcement of which devices are preserved.

  pci_liveupdate_register_flb(driver_file_handler);
  pci_liveupdate_unregister_flb(driver_file_handler);

When the first file (with a handler registered with the PCI core) is preserved, the PCI core will be notified to allocate its tracking struct (pci_ser). When the last file is unpreserved (i.e. preservation cancelled) the PCI core will be notified to free struct pci_ser.

This struct is preserved across a Live Update using KHO and can be fetched by the PCI core during early boot (e.g. during device enumeration) so that it knows which devices were preserved.

Note: This commit only allocates struct pci_ser and preserves it across Live Update. A subsequent commit will add an API for drivers to tell the PCI core exactly which devices are being preserved.

Note: There is no reason to check for kho_is_enabled() since it can be assumed to return true. If KHO was not enabled then Live Update would not be enabled and these routines would never run.

---
 Documentation/core-api/liveupdate.rst |   4 +
 MAINTAINERS                           |  13 +++
 drivers/pci/Kconfig                   |  15 +++
 drivers/pci/Makefile                  |   1 +
 drivers/pci/liveupdate.c              | 155 ++++++++++++++++++++++++++
 include/linux/kho/abi/pci.h           |  58 ++++++++++
 include/linux/pci.h                   |   1 +
 include/linux/pci_liveupdate.h        |  30 +++++
 8 files changed, 277 insertions(+)
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 include/linux/kho/abi/pci.h
 create mode 100644 include/linux/pci_liveupdate.h
Documentation/core-api/liveupdate.rst +4 -0
index 5a292d0f3706..b3c689e633c1 100644
@@ -1,5 +1,7 @@
 .. SPDX-License-Identifier: GPL-2.0
 
+.. _luo:
+
 ========================
 Live Update Orchestrator
 ========================
@@ -18,6 +20,8 @@ LUO Preserving File Descriptors
 .. kernel-doc:: kernel/liveupdate/luo_file.c
    :doc: LUO File Descriptors
 
+.. _flb:
+
 LUO File Lifecycle Bound Global Data
 ====================================
 .. kernel-doc:: kernel/liveupdate/luo_flb.c
MAINTAINERS +13 -0
index a3ed337e827d..9cc7b9291ace 100644
@@ -20824,6 +20824,19 @@ L:	[email protected]
 S:	Supported
 F:	Documentation/PCI/pci-error-recovery.rst
 
+PCI LIVE UPDATE
+M:	David Matlack <[email protected]>
+R:	Pasha Tatashin <[email protected]>
+R:	Mike Rapoport <[email protected]>
+R:	Pratyush Yadav <[email protected]>
+L:	[email protected]
+L:	[email protected]
+S:	Maintained
+T:	git git://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git
+F:	drivers/pci/liveupdate.c
+F:	include/linux/kho/abi/pci.h
+F:	include/linux/pci_liveupdate.h
+
 PCI MSI DRIVER FOR ALTERA MSI IP
 L:	[email protected]
 S:	Orphan
drivers/pci/Kconfig +15 -0
index 0c7408509ba2..3781e2b5f095 100644
@@ -271,6 +271,21 @@ config VGA_ARB_MAX_GPUS
 	  Reserves space in the kernel to maintain resource locking for
 	  multiple GPUS.  The overhead for each GPU is very small.
 
+config PCI_LIVEUPDATE
+	bool "PCI Live Update Support"
+	depends on PCI && LIVEUPDATE
+	help
+	  Enable PCI core support for preserving PCI devices across Live
+	  Update. This, in combination with support in a device's driver,
+	  enables PCI devices to run and perform memory transactions
+	  uninterrupted during a kexec for Live Update.
+
+	  This option should only be enabled by users who plan to use Live
+	  Update for kernel upgrades and require preserving PCI devices during
+	  those upgrades.
+
+	  If unsure, say N.
+
 source "drivers/pci/hotplug/Kconfig"
 source "drivers/pci/controller/Kconfig"
 source "drivers/pci/endpoint/Kconfig"
drivers/pci/Makefile +1 -0
index 41ebc3b9a518..e8d003cb6757 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o
 obj-$(CONFIG_SYSFS)		+= pci-sysfs.o slot.o
 obj-$(CONFIG_ACPI)		+= pci-acpi.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_PCI_LIVEUPDATE)	+= liveupdate.o
 endif
 
 obj-$(CONFIG_OF)		+= of.o
drivers/pci/liveupdate.c new file +155 -0

155 lines not on this page · raw message

include/linux/kho/abi/pci.h new file +58 -0

58 lines not on this page · raw message

include/linux/pci.h +1 -0
index ebb5b9d76360..da58aa101e4c 100644
@@ -43,6 +43,7 @@
 #include <uapi/linux/pci.h>
 
 #include <linux/pci_ids.h>
+#include <linux/pci_liveupdate.h>
 
 #define PCI_STATUS_ERROR_BITS (PCI_STATUS_DETECTED_PARITY  | \
 			       PCI_STATUS_SIG_SYSTEM_ERROR | \
include/linux/pci_liveupdate.h new file +30 -0
new file mode 100644
index 000000000000..8ec98beefcb4
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PCI Live Update support (Public/Driver API)
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <[email protected]>
+ */
+#ifndef LINUX_PCI_LIVEUPDATE_H
+#define LINUX_PCI_LIVEUPDATE_H
+
+#include <linux/liveupdate.h>
+#include <linux/types.h>
+
+struct pci_dev;
+
+#ifdef CONFIG_PCI_LIVEUPDATE
+int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh);
+void pci_liveupdate_unregister_flb(struct liveupdate_file_handler *fh);
+#else
+static inline int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void pci_liveupdate_unregister_flb(struct liveupdate_file_handler *fh)
+{
+}
+#endif
+
+#endif /* LINUX_PCI_LIVEUPDATE_H */
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#11 lore raw

PATCH v7 2/12 PCI: liveupdate: Track outgoing preserved PCI devices

In reply to David Matlack

Add APIs to allow drivers to notify the PCI core of which devices are being preserved across a Live Update for the next kernel, i.e. "outgoing" devices.

Drivers must notify the PCI core when devices are preserved so that the PCI core can update its FLB data (struct pci_ser) and track the list of outgoing devices. pci_liveupdate_preserve() notifies the PCI core that a device must be preserved across Live Update. pci_liveupdate_unpreserve() reverses this (cancels the preservation of the device).

This tracking ensures the PCI core is fully aware of which devices may need special handling during shutdown and kexec, and so that it can be handed off to the next kernel.

---
 drivers/pci/liveupdate.c       | 186 +++++++++++++++++++++++++++++++++
 drivers/pci/liveupdate.h       |  21 ++++
 drivers/pci/probe.c            |   2 +
 include/linux/pci.h            |   3 +
 include/linux/pci_liveupdate.h |  21 ++++
 5 files changed, 233 insertions(+)
 create mode 100644 drivers/pci/liveupdate.h
drivers/pci/liveupdate.c +186 -0
index 899758883dd5..03075ce06ac9 100644
@@ -36,6 +36,26 @@
  *
  *  * ``pci_liveupdate_register_flb(driver_file_handler)``
  *  * ``pci_liveupdate_unregister_flb(driver_file_handler)``
+ *
+ * Device Tracking
+ * ===============
+ *
+ * Drivers must notify the PCI core when specific devices are preserved or
+ * unpreserved with the following APIs:
+ *
+ *  * ``pci_liveupdate_preserve(pci_dev)``
+ *  * ``pci_liveupdate_unpreserve(pci_dev)``
+ *
+ * This allows the PCI core to keep its FLB data (struct pci_ser) up to date
+ * with the list of **outgoing** preserved devices for the next kernel.
+ *
+ * Restrictions
+ * ============
+ *
+ * The PCI core enforces the following restrictions on which devices can be
+ * preserved. These may be relaxed in the future:
+ *
+ *  * The device cannot be a Virtual Function (VF).
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -50,6 +70,21 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 
+#include "liveupdate.h"
+
+/**
+ * struct pci_liveupdate_global - Global state for PCI Live Update support
+ * @rwsem: Reader/writer semaphore used to protect the incoming and outgoing
+ *         FLBs, and the references to them in struct pci_dev.
+ */
+struct pci_liveupdate_global {
+	struct rw_semaphore rwsem;
+};
+
+static struct pci_liveupdate_global pci_liveupdate = {
+	.rwsem = __RWSEM_INITIALIZER(pci_liveupdate.rwsem),
+};
+
 /**
  * struct pci_flb_outgoing - Outgoing PCI FLB object
  * @ser: Pointer to the preserved struct pci_ser.
@@ -125,6 +160,157 @@ static struct liveupdate_flb pci_liveupdate_flb = {
 	.compatible = PCI_LUO_FLB_COMPATIBLE,
 };
 
+static struct pci_flb_outgoing *pci_liveupdate_flb_get_outgoing(void)
+{
+	struct pci_flb_outgoing *outgoing = NULL;
+	int ret;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&outgoing);
+	if (ret)
+		return ERR_PTR(ret);
+
+	if (!outgoing)
+		return ERR_PTR(-ENOENT);
+
+	return outgoing;
+}
+
+static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outgoing)
+{
+	struct pci_dev_ser *dev_ser, *found = NULL;
+	struct kho_block_set_it it;
+	int err;
+	u32 count = 0;
+
+	kho_block_set_it_init(&it, &outgoing->block_set);
+	while ((dev_ser = kho_block_set_it_read_entry(&it))) {
+		count++;
+		if (dev_ser->refcount == 0 && !found)
+			found = dev_ser;
+	}
+
+	if (found)
+		return found;
+
+	err = kho_block_set_grow(&outgoing->block_set, count + 1);
+	if (err)
+		return ERR_PTR(err);
+
+	if (count == 0)
+		kho_block_set_it_init(&it, &outgoing->block_set);
+
+	dev_ser = kho_block_set_it_reserve_entry(&it);
+	if (!dev_ser)
+		return ERR_PTR(-ENOSPC);
+
+	return dev_ser;
+}
+
+static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;
+
+	if (!dev_ser) {
+		pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
+		return;
+	}
+
+	pci_info(dev, "Device will no longer be preserved across next Live Update\n");
+	outgoing->ser->nr_devices--;
+	memset(dev_ser, 0, sizeof(*dev_ser));
+	dev->liveupdate.outgoing = NULL;
+}
+
+static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+
+	if (dev->liveupdate.outgoing)
+		return -EBUSY;
+
+	dev_ser = pci_get_empty_or_append(outgoing);
+	if (IS_ERR(dev_ser))
+		return PTR_ERR(dev_ser);
+
+	pci_info(dev, "Device will be preserved across next Live Update\n");
+	outgoing->ser->nr_devices++;
+	outgoing->ser->devices = kho_block_set_head_pa(&outgoing->block_set);
+
+	dev_ser->domain = pci_domain_nr(dev->bus);
+	dev_ser->bdf = pci_dev_id(dev);
+	dev_ser->refcount = 1;
+
+	dev->liveupdate.outgoing = dev_ser;
+	return 0;
+}
+
+/**
+ * pci_liveupdate_preserve() - Preserve a PCI device across Live Update
+ * @dev: The PCI device to preserve.
+ *
+ * pci_liveupdate_preserve() notifies the PCI core that a PCI device should be
+ * preserved across the next Live Update. Drivers are expected to call
+ * pci_liveupdate_preserve() from their struct liveupdate_file_handler
+ * preserve() callback to ensure the outgoing struct pci_ser is already set up.
+ *
+ * Returns: 0 on success, <0 on failure.
+ */
+int pci_liveupdate_preserve(struct pci_dev *dev)
+{
+	struct pci_flb_outgoing *outgoing = NULL;
+
+	if (dev->is_virtfn)
+		return -EINVAL;
+
+	guard(rwsem_write)(&pci_liveupdate.rwsem);
+
+	outgoing = pci_liveupdate_flb_get_outgoing();
+	if (IS_ERR(outgoing))
+		return PTR_ERR(outgoing);
+
+	return pci_liveupdate_preserve_device(outgoing, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
+
+/**
+ * pci_liveupdate_unpreserve() - Cancel preservation of a PCI device
+ * @dev: The PCI device to unpreserve.
+ *
+ * pci_liveupdate_unpreserve() notifies the PCI core that a PCI device should no
+ * longer be preserved across the next Live Update. Drivers are expected to call
+ * pci_liveupdate_unpreserve() from their struct liveupdate_file_handler
+ * unpreserve() callback to ensure the outgoing struct pci_ser is already set
+ * up.
+ */
+void pci_liveupdate_unpreserve(struct pci_dev *dev)
+{
+	struct pci_flb_outgoing *outgoing = NULL;
+
+	guard(rwsem_write)(&pci_liveupdate.rwsem);
+
+	outgoing = pci_liveupdate_flb_get_outgoing();
+	if (IS_ERR(outgoing)) {
+		pci_warn(dev, "Cannot unpreserve device without outgoing Live Update state\n");
+		return;
+	}
+
+	pci_liveupdate_unpreserve_device(outgoing, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
+
+void pci_liveupdate_cleanup_device(struct pci_dev *dev)
+{
+	/*
+	 * It should be safe to READ_ONCE() outside of the rwsem during cleanup
+	 * since there should no longer be any references to @dev on the system.
+	 *
+	 * This should never happen in practice. Drivers should block removal
+	 * while a device is preserved.
+	 */
+	if (READ_ONCE(dev->liveupdate.outgoing))
+		pci_WARN(dev, 1, "Destroying outgoing-preserved device!\n");
+}
+
 /**
  * pci_liveupdate_register_flb() - Register a file handler with the PCI core
  * @fh: The file handler to register.
drivers/pci/liveupdate.h new file +21 -0
new file mode 100644
index 000000000000..b2335581f8d0
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PCI Live Update support (core API)
+ *
+ * Copyright (c) 2026, Google LLC.
+ * David Matlack <[email protected]>
+ */
+#ifndef DRIVERS_PCI_LIVEUPDATE_H
+#define DRIVERS_PCI_LIVEUPDATE_H
+
+#include <linux/pci.h>
+
+#ifdef CONFIG_PCI_LIVEUPDATE
+void pci_liveupdate_cleanup_device(struct pci_dev *dev);
+#else
+static inline void pci_liveupdate_cleanup_device(struct pci_dev *dev)
+{
+}
+#endif
+
+#endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/probe.c +2 -0
index dd0abbc63e18..14b66acbdb15 100644
@@ -24,6 +24,7 @@
 #include <linux/pm_runtime.h>
 #include <linux/bitfield.h>
 #include <trace/events/pci.h>
+#include "liveupdate.h"
 #include "pci.h"
 
 static struct resource busn_resource = {
@@ -2485,6 +2486,7 @@ static void pci_release_dev(struct device *dev)
 
 	pci_dev = to_pci_dev(dev);
 	pci_release_capabilities(pci_dev);
+	pci_liveupdate_cleanup_device(pci_dev);
 	pci_release_of_node(pci_dev);
 	pcibios_release_device(pci_dev);
 	pci_bus_put(pci_dev->bus);
include/linux/pci.h +3 -0
index da58aa101e4c..b41dd572a2d6 100644
@@ -593,6 +593,9 @@ struct pci_dev {
 	u8		tph_mode;	/* TPH mode */
 	u8		tph_req_type;	/* TPH requester type */
 #endif
+#ifdef CONFIG_PCI_LIVEUPDATE
+	struct pci_liveupdate liveupdate;
+#endif
 };
 
 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
include/linux/pci_liveupdate.h +21 -0
index 8ec98beefcb4..cfcfbfa73af7 100644
@@ -8,14 +8,26 @@
 #ifndef LINUX_PCI_LIVEUPDATE_H
 #define LINUX_PCI_LIVEUPDATE_H
 
+#include <linux/kho/abi/pci.h>
 #include <linux/liveupdate.h>
 #include <linux/types.h>
+#include <linux/spinlock_types.h>
+
+/**
+ * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
+ * @outgoing: State preserved for the next kernel.
+ */
+struct pci_liveupdate {
+	struct pci_dev_ser *outgoing;
+};
 
 struct pci_dev;
 
 #ifdef CONFIG_PCI_LIVEUPDATE
 int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh);
 void pci_liveupdate_unregister_flb(struct liveupdate_file_handler *fh);
+int pci_liveupdate_preserve(struct pci_dev *dev);
+void pci_liveupdate_unpreserve(struct pci_dev *dev);
 #else
 static inline int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
 {
@@ -25,6 +37,15 @@ static inline int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh
 static inline void pci_liveupdate_unregister_flb(struct liveupdate_file_handler *fh)
 {
 }
+
+static inline int pci_liveupdate_preserve(struct pci_dev *dev)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void pci_liveupdate_unpreserve(struct pci_dev *dev)
+{
+}
 #endif
 
 #endif /* LINUX_PCI_LIVEUPDATE_H */
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#17 lore raw

PATCH v7 3/12 PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack

During PCI enumeration, the previous kernel might have passed state about devices that were preserved across kexec. The PCI core needs to fetch this state to identify which devices are "incoming" and require special handling.

Add pci_liveupdate_setup_device() which is called during device setup to fetch the serialized state (struct pci_ser) from the Live Update Orchestrator. The first time this happens, pci_flb_retrieve() will run and convert the array of pci_dev_ser structs into an xarray so that it can be looked up efficiently.

If a device is found in the xarray, the PCI core stores a pointer to its state in dev->liveupdate_incoming and holds a reference to the incoming FLB until pci_liveupdate_finish() is called by the driver.

This ensures proper lifecycle management for incoming preserved devices and allows the PCI core and drivers to apply specific Live Update logic to them in subsequent commits.

Drivers can check if a device is an incoming preserved device (e.g. during probe) by calling pci_liveupdate_is_incoming().

CONFIG_64BIT is now required to enable CONFIG_PCI_LIVEUPDATE so that the domain and bdf can be guaranteed to fit in an unsigned long and be used as the xarray key.

---
 MAINTAINERS                    |   1 +
 drivers/pci/Kconfig            |   2 +-
 drivers/pci/liveupdate.c       | 240 ++++++++++++++++++++++++++++++++-
 drivers/pci/liveupdate.h       |   5 +
 drivers/pci/probe.c            |   3 +
 include/linux/pci_liveupdate.h |  13 ++
 6 files changed, 261 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 9cc7b9291ace..08a724b860dc 100644
@@ -20834,6 +20834,7 @@ L:	[email protected]
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git
 F:	drivers/pci/liveupdate.c
+F:	drivers/pci/liveupdate.h
 F:	include/linux/kho/abi/pci.h
 F:	include/linux/pci_liveupdate.h
 
drivers/pci/Kconfig +1 -1
index 3781e2b5f095..8af20f558086 100644
@@ -273,7 +273,7 @@ config VGA_ARB_MAX_GPUS
 
 config PCI_LIVEUPDATE
 	bool "PCI Live Update Support"
-	depends on PCI && LIVEUPDATE
+	depends on PCI && LIVEUPDATE && 64BIT
 	help
 	  Enable PCI core support for preserving PCI devices across Live
 	  Update. This, in combination with support in a device's driver,
drivers/pci/liveupdate.c +238 -2

276 lines not on this page · raw message

drivers/pci/liveupdate.h +5 -0
index b2335581f8d0..eaaa3559fd77 100644
@@ -11,8 +11,13 @@
 #include <linux/pci.h>
 
 #ifdef CONFIG_PCI_LIVEUPDATE
+void pci_liveupdate_setup_device(struct pci_dev *dev);
 void pci_liveupdate_cleanup_device(struct pci_dev *dev);
 #else
+static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+}
+
 static inline void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 {
 }
drivers/pci/probe.c +3 -0
index 14b66acbdb15..5dc9d86e3597 100644
@@ -2065,6 +2065,8 @@ int pci_setup_device(struct pci_dev *dev)
 	if (pci_early_dump)
 		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
 	/* Need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
@@ -2188,6 +2190,7 @@ int pci_setup_device(struct pci_dev *dev)
 	default:				    /* unknown header */
 		pci_err(dev, "unknown header type %02x, ignoring device\n",
 			dev->hdr_type);
+		pci_liveupdate_cleanup_device(dev);
 		pci_release_of_node(dev);
 		return -EIO;
 
include/linux/pci_liveupdate.h +13 -0
index cfcfbfa73af7..cfdc3d62ec02 100644
@@ -16,9 +16,11 @@
 /**
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
+ * @incoming: State preserved by the previous kernel.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
+	struct pci_dev_ser *incoming;
 };
 
 struct pci_dev;
@@ -28,6 +30,8 @@ int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh);
 void pci_liveupdate_unregister_flb(struct liveupdate_file_handler *fh);
 int pci_liveupdate_preserve(struct pci_dev *dev);
 void pci_liveupdate_unpreserve(struct pci_dev *dev);
+void pci_liveupdate_finish(struct pci_dev *dev);
+bool pci_liveupdate_is_incoming(struct pci_dev *dev);
 #else
 static inline int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
 {
@@ -46,6 +50,15 @@ static inline int pci_liveupdate_preserve(struct pci_dev *dev)
 static inline void pci_liveupdate_unpreserve(struct pci_dev *dev)
 {
 }
+
+static inline void pci_liveupdate_finish(struct pci_dev *dev)
+{
+}
+
+static inline bool pci_liveupdate_is_incoming(struct pci_dev *dev)
+{
+	return false;
+}
 #endif
 
 #endif /* LINUX_PCI_LIVEUPDATE_H */
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#35 lore raw

PATCH v7 4/12 PCI: liveupdate: Document driver binding responsibilities

In reply to David Matlack

Document how driver binding works during a Live Update and what the PCI core expects of drivers and users. Note that this is only a description of the current division of responsibilities. These can change in the future if we decide.

---
 drivers/pci/liveupdate.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/pci/liveupdate.c +16 -0
index df6a02240aa4..a067632e70d1 100644
@@ -70,6 +70,22 @@
  * preserved. These may be relaxed in the future:
  *
  *  * The device cannot be a Virtual Function (VF).
+ *
+ * Driver Binding
+ * ==============
+ *
+ * In the outgoing kernel, it is the driver's responsibility to ensure that it
+ * does not release a device between pci_liveupdate_preserve() and
+ * pci_liveupdate_unpreserve().
+ *
+ * In the incoming kernel, it is the driver's responsibility to ensure that it
+ * does not release a preserved device between probe() and
+ * pci_liveupdate_finish().
+ *
+ * It is the user's responsibility to ensure that incoming preserved devices are
+ * bound to the correct driver. i.e. The PCI core does not protect against a
+ * device getting preserved by driver A in the outgoing kernel and then getting
+ * bound to driver B in the incoming kernel. This may change in the future.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#37 lore raw

PATCH v7 5/12 PCI: liveupdate: Keep bus numbers constant during Live Update

In reply to David Matlack

During a Live Update, preserved devices must be allowed to continue performing memory transactions so the kernel cannot change the fabric topology, including bus numbers, since that would require disabling and flushing any memory transactions first.

To keep bus numbers constant, always inherit the secondary and subordinate bus numbers assigned to bridges during scanning, instead of assigning new ones, if any PCI devices are being preserved. Note that the kernel inherits bus numbers even on bridges without any downstream endpoints that were preserved. This avoids accidentally assigning a bridge a new window that overlaps with a preserved device that is downstream of a different bridge.

If a bridge is scanned with a broken topology or has no bus numbers set during a Live Update, refuse to assign it new bus numbers and refuse to enumerate devices below it until the Live Update is finished. This is a safety measure to prevent topology conflicts.

Require that CONFIG_CARDBUS is not enabled to enable CONFIG_PCI_LIVEUPDATE since inheriting bus numbers on PCI-to-CardBus bridges requires additional work but is not a priority at the moment.

---
 .../admin-guide/kernel-parameters.txt         |   6 +-
 drivers/pci/Kconfig                           |   2 +-
 drivers/pci/liveupdate.c                      | 102 ++++++++++++++++++
 drivers/pci/liveupdate.h                      |  14 +++
 drivers/pci/probe.c                           |  17 ++-
 include/linux/pci_liveupdate.h                |   4 +
 6 files changed, 139 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -1
index 4f65b2a37521..ed9eef8de64c 100644
@@ -5105,7 +5105,11 @@ Kernel parameters
 				explicitly which ones they are.
 		assign-busses	[X86] Always assign all PCI bus
 				numbers ourselves, overriding
-				whatever the firmware may have done.
+				whatever the firmware may have done. Ignored
+				during a Live Update, where the kernel must
+				inherit the PCI topology (including bus numbers)
+				to avoid interrupting ongoing memory
+				transactions of preserved devices.
 		usepirqmask	[X86] Honor the possible IRQ mask stored
 				in the BIOS $PIR table. This is needed on
 				some systems with broken BIOSes, notably
drivers/pci/Kconfig +1 -1
index 8af20f558086..16fbd4212e0f 100644
@@ -273,7 +273,7 @@ config VGA_ARB_MAX_GPUS
 
 config PCI_LIVEUPDATE
 	bool "PCI Live Update Support"
-	depends on PCI && LIVEUPDATE && 64BIT
+	depends on PCI && LIVEUPDATE && 64BIT && !CARDBUS
 	help
 	  Enable PCI core support for preserving PCI devices across Live
 	  Update. This, in combination with support in a device's driver,
drivers/pci/liveupdate.c +102 -0

114 lines not on this page · raw message

drivers/pci/liveupdate.h +14 -0
index eaaa3559fd77..c763255a8de4 100644
@@ -13,6 +13,9 @@
 #ifdef CONFIG_PCI_LIVEUPDATE
 void pci_liveupdate_setup_device(struct pci_dev *dev);
 void pci_liveupdate_cleanup_device(struct pci_dev *dev);
+bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
+				      int pass);
+void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -21,6 +24,17 @@ static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 static inline void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 {
 }
+
+static inline bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus,
+						    struct pci_dev *dev,
+						    int pass)
+{
+	return false;
+}
+
+static inline void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass)
+{
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/probe.c +13 -4
index 5dc9d86e3597..165973bc92aa 100644
@@ -1397,6 +1397,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 				  int max, unsigned int available_buses,
 				  int pass)
 {
+	bool liveupdate, assign_new_buses = pcibios_assign_all_busses();
 	struct pci_bus *child;
 	u32 buses;
 	u16 bctl;
@@ -1406,6 +1407,10 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	u8 fixed_sec, fixed_sub;
 	int next_busnr;
 
+	liveupdate = pci_liveupdate_scan_bridge_begin(bus, dev, pass);
+	if (liveupdate)
+		assign_new_buses = false;
+
 	/*
 	 * Make sure the bridge is powered on to be able to access config
 	 * space of devices below it.
@@ -1449,8 +1454,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		goto out;
 	}
 
-	if ((secondary || subordinate) &&
-	    !pcibios_assign_all_busses() && !broken) {
+	if ((secondary || subordinate) && !assign_new_buses && !broken) {
 		unsigned int cmax, buses;
 
 		/*
@@ -1492,8 +1496,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		 * do in the second pass.
 		 */
 		if (!pass) {
-			if (pcibios_assign_all_busses() || broken)
-
+			if (assign_new_buses || broken)
 				/*
 				 * Temporarily disable forwarding of the
 				 * configuration cycles on all bridges in
@@ -1507,6 +1510,11 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 			goto out;
 		}
 
+		if (liveupdate) {
+			pci_err(dev, "Cannot reconfigure bridge during Live Update, skipping\n");
+			goto out;
+		}
+
 		/* Clear errors */
 		pci_write_config_word(dev, PCI_STATUS, 0xffff);
 
@@ -1567,6 +1575,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
 
 	pm_runtime_put(&dev->dev);
+	pci_liveupdate_scan_bridge_end(dev, pass);
 
 	return max;
 }
include/linux/pci_liveupdate.h +4 -0
index cfdc3d62ec02..2be98819e313 100644
@@ -17,10 +17,14 @@
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
  * @incoming: State preserved by the previous kernel.
+ * @inherit_buses: True if the PCI core should inherit the secondary and
+ *                 subordinate bus numbers assigned to this device due to
+ *                 an ongoing Live Update.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
 	struct pci_dev_ser *incoming;
+	bool inherit_buses;
 };
 
 struct pci_dev;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#41 lore raw

PATCH v7 6/12 PCI: liveupdate: Auto-preserve upstream bridges across Live Update

In reply to David Matlack

When a PCI device is preserved across a Live Update, all of its upstream bridges up to the root port must also be preserved. This enables the PCI core and any drivers bound to the bridges to manage bridges correctly across a Live Update.

Notably, this will be used in subsequent commits to ensure that preserved devices can continue performing memory transactions without a disruption or change in routing.

To preserve bridges, the PCI core tracks the number of downstream devices preserved under each bridge using a reference count in struct pci_dev_ser. This allows a bridge to remain preserved until all its downstream preserved devices are unpreserved or finish their participation in the Live Update.

---
 drivers/pci/liveupdate.c    | 136 +++++++++++++++++++++++++++++++-----
 include/linux/kho/abi/pci.h |   5 +-
 2 files changed, 122 insertions(+), 19 deletions(-)
drivers/pci/liveupdate.c +119 -17
index b2b950d71657..7f7710cb1da0 100644
@@ -101,6 +101,18 @@
  * If a misconfigured or unconfigured bridge is encountered during enumeration
  * while there are preserved devices, its secondary and subordinate bus numbers
  * will be cleared and devices below it will not be enumerated.
+ *
+ * PCI-to-PCI Bridges
+ * ==================
+ *
+ * Any PCI-to-PCI bridges upstream of a preserved device are automatically
+ * preserved when the device is preserved. The PCI core keeps track of the
+ * number of downstream devices that are preserved under a bridge so that the
+ * bridge is only unpreserved once all downstream devices are unpreserved.
+ *
+ * This enables the PCI core and any drivers bound to the bridge to participate
+ * in the Live Update so that preserved endpoints can continue issuing memory
+ * transactions during the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -316,28 +328,52 @@ static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outg
 	return dev_ser;
 }
 
-static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+static int pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
 {
 	struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;
 
 	if (!dev_ser) {
 		pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
-		return;
+		return -EINVAL;
+	}
+
+	if (!dev_ser->refcount) {
+		pci_WARN(dev, 1, "Preserved device has a 0 refcount!\n");
+		return -EINVAL;
 	}
 
+	if (--dev_ser->refcount)
+		return 0;
+
 	pci_info(dev, "Device will no longer be preserved across next Live Update\n");
 	outgoing->ser->nr_devices--;
 	memset(dev_ser, 0, sizeof(*dev_ser));
 	dev->liveupdate.outgoing = NULL;
+	return 0;
 }
 
-static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+static int pci_liveupdate_preserve_device_again(struct pci_dev *dev)
 {
-	struct pci_dev_ser *dev_ser;
+	if (!dev->liveupdate.outgoing->refcount) {
+		pci_WARN(dev, 1, "Preserved device with 0 refcount!\n");
+		return -EINVAL;
+	}
 
-	if (dev->liveupdate.outgoing)
+	/*
+	 * Endpoint devices should not be preserved more than once. Bridges are
+	 * preserved once for every downstream device that is preserved.
+	 */
+	if (!dev->subordinate)
 		return -EBUSY;
 
+	dev->liveupdate.outgoing->refcount++;
+	return 0;
+}
+
+static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+
 	dev_ser = pci_get_empty_or_append(outgoing);
 	if (IS_ERR(dev_ser))
 		return PTR_ERR(dev_ser);
@@ -354,6 +390,52 @@ static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str
 	return 0;
 }
 
+static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	if (dev->liveupdate.outgoing)
+		return pci_liveupdate_preserve_device_again(dev);
+
+	return __pci_liveupdate_preserve_device(outgoing, dev);
+}
+
+#define for_each_pci_dev_in_path(_d, _start, _end) \
+	for ((_d) = (_start); (_d) != (_end); (_d) = (_d)->bus->self)
+
+static void __pci_liveupdate_unpreserve_path(struct pci_flb_outgoing *outgoing,
+					     struct pci_dev *start,
+					     struct pci_dev *end)
+{
+	struct pci_dev *dev;
+
+	for_each_pci_dev_in_path(dev, start, end) {
+		if (pci_liveupdate_unpreserve_device(outgoing, dev))
+			return;
+	}
+}
+
+static void pci_liveupdate_unpreserve_path(struct pci_flb_outgoing *outgoing,
+					   struct pci_dev *start)
+{
+	__pci_liveupdate_unpreserve_path(outgoing, start, /*end=*/NULL);
+}
+
+static int pci_liveupdate_preserve_path(struct pci_flb_outgoing *outgoing,
+					struct pci_dev *start)
+{
+	struct pci_dev *dev;
+	int ret;
+
+	for_each_pci_dev_in_path(dev, start, NULL) {
+		ret = pci_liveupdate_preserve_device(outgoing, dev);
+		if (ret) {
+			__pci_liveupdate_unpreserve_path(outgoing, start, dev);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
 /**
  * pci_liveupdate_preserve() - Preserve a PCI device across Live Update
  * @dev: The PCI device to preserve.
@@ -363,6 +445,9 @@ static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str
  * pci_liveupdate_preserve() from their struct liveupdate_file_handler
  * preserve() callback to ensure the outgoing struct pci_ser is already set up.
  *
+ * pci_liveupdate_preserve() automatically preserves all bridges upstream of
+ * @dev.
+ *
  * Returns: 0 on success, <0 on failure.
  */
 int pci_liveupdate_preserve(struct pci_dev *dev)
@@ -378,7 +463,7 @@ int pci_liveupdate_preserve(struct pci_dev *dev)
 	if (IS_ERR(outgoing))
 		return PTR_ERR(outgoing);
 
-	return pci_liveupdate_preserve_device(outgoing, dev);
+	return pci_liveupdate_preserve_path(outgoing, dev);
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
 
@@ -391,6 +476,9 @@ EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
  * pci_liveupdate_unpreserve() from their struct liveupdate_file_handler
  * unpreserve() callback to ensure the outgoing struct pci_ser is already set
  * up.
+ *
+ * pci_liveupdate_unpreserve() automatically unpreserves all bridges upstream of
+ * @dev.
  */
 void pci_liveupdate_unpreserve(struct pci_dev *dev)
 {
@@ -404,7 +492,7 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
 		return;
 	}
 
-	pci_liveupdate_unpreserve_device(outgoing, dev);
+	pci_liveupdate_unpreserve_path(outgoing, dev);
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
 
@@ -594,29 +682,41 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 	}
 }
 
-static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
+static int pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
 {
 	if (!dev->liveupdate.incoming) {
 		pci_warn(dev, "Cannot finish preserving an unpreserved device\n");
-		return;
+		return -EINVAL;
 	}
 
-	if (dev->liveupdate.incoming->refcount != 1) {
-		pci_WARN(dev, 1, "Preserved device has a corrupted refcount!\n");
-		return;
+	if (!dev->liveupdate.incoming->refcount) {
+		pci_WARN(dev, 1, "Preserved device has a 0 refcount!\n");
+		return -EINVAL;
 	}
 
 	/*
-	 * Drop the refcount so this device does not get treated as an incoming
-	 * device again, e.g. in case pci_liveupdate_setup_device() gets called
-	 * again because the device is hot-plugged.
+	 * Decrement the refcount so this device does not get treated as an
+	 * incoming device again, e.g. in case pci_liveupdate_setup_device()
+	 * gets called again because the device is hot-plugged.
 	 */
-	dev->liveupdate.incoming->refcount = 0;
+	if (--dev->liveupdate.incoming->refcount)
+		return 0;
 
 	pci_info(dev, "Device is finished participating in Live Update\n");
 	dev->liveupdate.incoming = NULL;
 	ser->nr_devices--;
 	pci_liveupdate_flb_put_incoming();
+	return 0;
+}
+
+static void pci_liveupdate_finish_path(struct pci_ser *ser, struct pci_dev *start)
+{
+	struct pci_dev *dev;
+
+	for_each_pci_dev_in_path(dev, start, NULL) {
+		if (pci_liveupdate_finish_device(ser, dev))
+			return;
+	}
 }
 
 /**
@@ -628,6 +728,8 @@ static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *de
  * Update. Drivers must call pci_liveupdate_finish() from their struct
  * liveupdate_file_handler finish() callback to ensure the incoming struct
  * pci_ser is allocated.
+ *
+ * pci_liveupdate_finish() automatically finishes all bridges upstream of @dev.
  */
 void pci_liveupdate_finish(struct pci_dev *dev)
 {
@@ -641,7 +743,7 @@ void pci_liveupdate_finish(struct pci_dev *dev)
 		return;
 	}
 
-	pci_liveupdate_finish_device(incoming->ser, dev);
+	pci_liveupdate_finish_path(incoming->ser, dev);
 	pci_liveupdate_flb_put_incoming();
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_finish);
include/linux/kho/abi/pci.h +3 -2
index de549016807a..acf1b38dff02 100644
@@ -23,7 +23,7 @@
  * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
  */
 
-#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+#define PCI_LUO_FLB_COMPATIBLE "pci-v2"
 
 /**
  * struct pci_dev_ser - Serialized state about a single PCI device.
@@ -32,7 +32,8 @@
  * @bdf: The device's PCI bus, device, and function number.
  * @refcount: Reference count used by the PCI core to keep track of whether it
  *            is done using a device's struct pci_dev_ser. The value of the
- *            refcount is equal to 1 when the struct pci_dev_ser is in use, and
+ *            refcount is equal to the number of preserved devices at or below
+ *            it in the PCI hierarchy when the struct pci_dev_ser is in use, and
  *            0 otherwise.
  */
 struct pci_dev_ser {
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#46 lore raw

PATCH v7 7/12 PCI: Refactor matching logic for pci_dev_acs_ops

In reply to David Matlack

Refactor the logic to match devices to pci_dev_acs_ops by factoring out the loop and device matching into its own routine. This eliminates some duplicate code between pci_dev_specific_enable_acs() and pci_dev_specific_disable_acs_redir(), and will also be used in a subsequent commit to check if a device requires device-specific enable_acs() during a Live Update.

No functional change intended.

---
 drivers/pci/quirks.c | 51 ++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)
drivers/pci/quirks.c +21 -30
index b09f27f7846f..7ac39ec2843e 100644
@@ -5374,9 +5374,6 @@ static void pci_quirk_enable_intel_rp_mpc_acs(struct pci_dev *dev)
  */
 static int pci_quirk_enable_intel_pch_acs(struct pci_dev *dev)
 {
-	if (!pci_quirk_intel_pch_acs_match(dev))
-		return -ENOTTY;
-
 	if (pci_quirk_enable_intel_lpc_acs(dev)) {
 		pci_warn(dev, "Failed to enable Intel PCH ACS quirk\n");
 		return 0;
@@ -5396,9 +5393,6 @@ static int pci_quirk_enable_intel_spt_pch_acs(struct pci_dev *dev)
 	int pos;
 	u32 cap, ctrl;
 
-	if (!pci_quirk_intel_spt_pch_acs_match(dev))
-		return -ENOTTY;
-
 	pos = dev->acs_cap;
 	if (!pos)
 		return -ENOTTY;
@@ -5426,9 +5420,6 @@ static int pci_quirk_disable_intel_spt_pch_acs_redir(struct pci_dev *dev)
 	int pos;
 	u32 cap, ctrl;
 
-	if (!pci_quirk_intel_spt_pch_acs_match(dev))
-		return -ENOTTY;
-
 	pos = dev->acs_cap;
 	if (!pos)
 		return -ENOTTY;
@@ -5448,56 +5439,56 @@ static int pci_quirk_disable_intel_spt_pch_acs_redir(struct pci_dev *dev)
 static const struct pci_dev_acs_ops {
 	u16 vendor;
 	u16 device;
+	bool (*match)(struct pci_dev *dev);
 	int (*enable_acs)(struct pci_dev *dev);
 	int (*disable_acs_redir)(struct pci_dev *dev);
 } pci_dev_acs_ops[] = {
 	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
+	    .match = pci_quirk_intel_pch_acs_match,
 	    .enable_acs = pci_quirk_enable_intel_pch_acs,
 	},
 	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
+	    .match = pci_quirk_intel_spt_pch_acs_match,
 	    .enable_acs = pci_quirk_enable_intel_spt_pch_acs,
 	    .disable_acs_redir = pci_quirk_disable_intel_spt_pch_acs_redir,
 	},
 };
 
-int pci_dev_specific_enable_acs(struct pci_dev *dev)
+static const struct pci_dev_acs_ops *pci_dev_acs_ops_get(struct pci_dev *dev)
 {
 	const struct pci_dev_acs_ops *p;
-	int i, ret;
+	int i;
 
 	for (i = 0; i < ARRAY_SIZE(pci_dev_acs_ops); i++) {
 		p = &pci_dev_acs_ops[i];
 		if ((p->vendor == dev->vendor ||
 		     p->vendor == (u16)PCI_ANY_ID) &&
 		    (p->device == dev->device ||
-		     p->device == (u16)PCI_ANY_ID) &&
-		    p->enable_acs) {
-			ret = p->enable_acs(dev);
-			if (ret >= 0)
-				return ret;
+		     p->device == (u16)PCI_ANY_ID)) {
+			if (!p->match || p->match(dev))
+				return p;
 		}
 	}
 
+	return NULL;
+}
+
+int pci_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
+
+	if (p && p->enable_acs)
+		return p->enable_acs(dev);
+
 	return -ENOTTY;
 }
 
 int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
 {
-	const struct pci_dev_acs_ops *p;
-	int i, ret;
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
 
-	for (i = 0; i < ARRAY_SIZE(pci_dev_acs_ops); i++) {
-		p = &pci_dev_acs_ops[i];
-		if ((p->vendor == dev->vendor ||
-		     p->vendor == (u16)PCI_ANY_ID) &&
-		    (p->device == dev->device ||
-		     p->device == (u16)PCI_ANY_ID) &&
-		    p->disable_acs_redir) {
-			ret = p->disable_acs_redir(dev);
-			if (ret >= 0)
-				return ret;
-		}
-	}
+	if (p && p->disable_acs_redir)
+		return p->disable_acs_redir(dev);
 
 	return -ENOTTY;
 }
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#50 lore raw

PATCH v7 8/12 PCI: liveupdate: Inherit ACS flags in incoming preserved devices

In reply to David Matlack

Inherit Access Control Services (ACS) flags on all incoming preserved devices (endpoints and upstream bridges) during a Live Update.

Inheriting ACS flags avoids changing routing rules while memory transactions are in flight from preserved devices. This is also strictly necessary to ensure that IOMMU group assignments do not change across a Live Update for preserved devices, as changing ACS configurations can split or merge IOMMU groups.

Cache the inherited ACS controls established by the previous kernel in struct pci_dev so that ACS controls do not change after a reset (pci_restore_state() calls pci_enable_acs()).

To simplify ACS inheritance, reject preserving any devices that require quirks to enable ACS as those quirks would also have to take Live Update into account.

---
 drivers/pci/liveupdate.c       | 68 ++++++++++++++++++++++++++++++++++
 drivers/pci/liveupdate.h       | 11 ++++++
 drivers/pci/pci.c              |  6 +++
 drivers/pci/pci.h              |  5 +++
 drivers/pci/quirks.c           |  7 ++++
 include/linux/pci_liveupdate.h |  6 +++
 6 files changed, 103 insertions(+)
drivers/pci/liveupdate.c +68 -0
index 7f7710cb1da0..a95bfe5eff77 100644
@@ -71,6 +71,9 @@
  *
  *  * The device cannot be a Virtual Function (VF).
  *
+ *  * The device cannot require device-specific quirks to enable Access
+ *    Control Services (ACS).
+ *
  * Driver Binding
  * ==============
  *
@@ -113,6 +116,18 @@
  * This enables the PCI core and any drivers bound to the bridge to participate
  * in the Live Update so that preserved endpoints can continue issuing memory
  * transactions during the Live Update.
+ *
+ * Handling Preserved Devices
+ * ==========================
+ *
+ * The PCI core treats preserved devices differently than non-preserved devices.
+ * This section enumerates those differences.
+ *
+ *  * The PCI core inherits all ACS flags enabled on incoming preserved devices
+ *    rather than assigning new ones. This ensures that TLPs are routed the same
+ *    way after Live Update and ensures that IOMMU groups do not change. Note
+ *    that a device will use its inherited ACS flags for the lifetime of its
+ *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -128,6 +143,7 @@
 #include <linux/slab.h>
 
 #include "liveupdate.h"
+#include "pci.h"
 
 /**
  * struct pci_liveupdate_global - Global state for PCI Live Update support
@@ -374,6 +390,16 @@ static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, s
 {
 	struct pci_dev_ser *dev_ser;
 
+	/*
+	 * Do not preserve devices that rely on device-specific ACS equivalents
+	 * (for now) since that would complicate keeping ACS constant across
+	 * Live Update.
+	 */
+	if (pci_need_dev_specific_enable_acs(dev)) {
+		pci_warn(dev, "Refusing to preserve device that relies on ACS quirks\n");
+		return -EINVAL;
+	}
+
 	dev_ser = pci_get_empty_or_append(outgoing);
 	if (IS_ERR(dev_ser))
 		return PTR_ERR(dev_ser);
@@ -655,6 +681,7 @@ void pci_liveupdate_setup_device(struct pci_dev *dev)
 
 	pci_info(dev, "Device was preserved by previous kernel across Live Update\n");
 	dev->liveupdate.incoming = dev_ser;
+	dev->liveupdate.was_preserved = true;
 
 	/*
 	 * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
@@ -748,6 +775,47 @@ void pci_liveupdate_finish(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_finish);
 
+void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->acs_cap || !dev->liveupdate.incoming)
+		return;
+
+	pci_read_config_word(dev, dev->acs_cap + PCI_ACS_CTRL, &dev->liveupdate.acs_ctrl);
+}
+
+int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{
+	u16 acs_ctrl = dev->liveupdate.acs_ctrl;
+	u16 acs_cap = dev->acs_cap;
+
+	/*
+	 * Use liveupdate.was_preserved instead of liveupdate.incoming since the
+	 * device's ACS controls should not change even after the device is
+	 * finished participating in the Live Update.
+	 */
+	if (!dev->liveupdate.was_preserved)
+		return -EINVAL;
+
+	/*
+	 * The previous kernel should not have preserved any devices that
+	 * require device-specific quirks to enable ACS, but if such a device is
+	 * detected (e.g. new device-specific ACS quirk in the current kernel),
+	 * log a big warning and fall back to the normal enable ACS path.
+	 */
+	if (pci_need_dev_specific_enable_acs(dev)) {
+		pci_warn(dev, "Device-specific quirk required to enable ACS!\n");
+		WARN_ON_ONCE(true);
+		return -EINVAL;
+	}
+
+	if (acs_cap)
+		pci_write_config_word(dev, acs_cap + PCI_ACS_CTRL, acs_ctrl);
+
+	return 0;
+}
+
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +11 -0
index c763255a8de4..4e8a01bcb4bb 100644
@@ -16,6 +16,8 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev);
 bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 				      int pass);
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
+void pci_liveupdate_init_acs(struct pci_dev *dev);
+int pci_liveupdate_enable_acs(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -35,6 +37,15 @@ static inline bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus,
 static inline void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass)
 {
 }
+
+static inline void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{
+	return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +6 -0
index 77b17b13ee61..739ecaab2e76 100644
@@ -34,6 +34,8 @@
 #include <linux/aer.h>
 #include <linux/bitfield.h>
 #include <linux/suspend.h>
+
+#include "liveupdate.h"
 #include "pci.h"
 
 DEFINE_MUTEX(pci_slot_mutex);
@@ -1008,6 +1010,9 @@ void pci_enable_acs(struct pci_dev *dev)
 	bool enable_acs = false;
 	int pos;
 
+	if (!pci_liveupdate_enable_acs(dev))
+		return;
+
 	/* If an iommu is present we start with kernel default caps */
 	if (pci_acs_enable) {
 		if (pci_dev_specific_enable_acs(dev))
@@ -3689,6 +3694,7 @@ void pci_acs_init(struct pci_dev *dev)
 
 	pci_read_config_word(dev, pos + PCI_ACS_CAP, &dev->acs_capabilities);
 	pci_disable_broken_acs_cap(dev);
+	pci_liveupdate_init_acs(dev);
 }
 
 /**
drivers/pci/pci.h +5 -0
index 4469e1a77f3c..988a18b3204a 100644
@@ -1047,6 +1047,7 @@ void pci_acs_init(struct pci_dev *dev);
 void pci_enable_acs(struct pci_dev *dev);
 #ifdef CONFIG_PCI_QUIRKS
 int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
 void pci_disable_broken_acs_cap(struct pci_dev *pdev);
@@ -1057,6 +1058,10 @@ static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
 {
 	return -ENOTTY;
 }
+static inline bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	return false;
+}
 static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
 	return -ENOTTY;
drivers/pci/quirks.c +7 -0
index 7ac39ec2843e..99b819f38e49 100644
@@ -5473,6 +5473,13 @@ static const struct pci_dev_acs_ops *pci_dev_acs_ops_get(struct pci_dev *dev)
 	return NULL;
 }
 
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
+
+	return p && p->enable_acs;
+}
+
 int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
 	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
include/linux/pci_liveupdate.h +6 -0
index 2be98819e313..2446c6d237ca 100644
@@ -17,14 +17,20 @@
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
  * @incoming: State preserved by the previous kernel.
+ * @acs_ctrl: ACS features established by the previous kernel.
  * @inherit_buses: True if the PCI core should inherit the secondary and
  *                 subordinate bus numbers assigned to this device due to
  *                 an ongoing Live Update.
+ * @was_preserved: True if this struct pci_dev was preserved by the previous
+ *                 kernel. Unlike @incoming, this field is not cleared after
+ *                 the device is finished participating in Live Update.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
 	struct pci_dev_ser *incoming;
+	u16 acs_ctrl;
 	bool inherit_buses;
+	bool was_preserved;
 };
 
 struct pci_dev;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#55 lore raw

PATCH v7 9/12 PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to David Matlack

Inherit the ARI Forwarding Enable on preserved bridges and update pci_dev->ari_enabled accordingly during a Live Update. This ensures that the preserved devices on the bridge's secondary bus can be identified with the same expanded 8-bit function number after a Live Update.

---
 drivers/pci/liveupdate.c | 18 ++++++++++++++++++
 drivers/pci/liveupdate.h |  6 ++++++
 drivers/pci/pci.c        |  8 +++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)
drivers/pci/liveupdate.c +18 -0
index a95bfe5eff77..74a11e520f0d 100644
@@ -128,6 +128,10 @@
  *    way after Live Update and ensures that IOMMU groups do not change. Note
  *    that a device will use its inherited ACS flags for the lifetime of its
  *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
+ *
+ *  * The PCI core inherits ARI Forwarding Enable on all bridges with downstream
+ *    preserved devices to ensure that all preserved devices on the bridge's
+ *    secondary bus are addressable after the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
 	return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+	u16 val;
+
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->liveupdate.incoming)
+		return -EINVAL;
+
+	pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+	dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);
+	return 0;
+}
+
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +6 -0
index 4e8a01bcb4bb..6f21ec50927b 100644
@@ -18,6 +18,7 @@ bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
 void pci_liveupdate_init_acs(struct pci_dev *dev);
 int pci_liveupdate_enable_acs(struct pci_dev *dev);
+int pci_liveupdate_configure_ari(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -46,6 +47,11 @@ static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
 {
 	return -EINVAL;
 }
+
+static inline int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+	return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +7 -1
index 739ecaab2e76..e0c133b66a35 100644
@@ -3528,7 +3528,7 @@ void pci_configure_ari(struct pci_dev *dev)
 	u32 cap;
 	struct pci_dev *bridge;
 
-	if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
+	if (!pci_is_pcie(dev) || dev->devfn)
 		return;
 
 	bridge = dev->bus->self;
@@ -3539,6 +3539,12 @@ void pci_configure_ari(struct pci_dev *dev)
 	if (!(cap & PCI_EXP_DEVCAP2_ARI))
 		return;
 
+	if (!pci_liveupdate_configure_ari(bridge))
+		return;
+
+	if (pcie_ari_disabled)
+		return;
+
 	if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
 		pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
 					 PCI_EXP_DEVCTL2_ARI);
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#64 lore raw

PATCH v7 10/12 PCI: liveupdate: Freeze preservation status during shutdown

In reply to David Matlack

Freeze a device's outgoing preservation status (preserved or not preserved) during shutdown. This enables the PCI core and drivers to safely make decisions based on the device's preservation status during shutdown.

Note that pci_liveupdate_freeze() is triggered by the PCI core rather than from drivers participating in Live Update so that all devices can have their status frozen (i.e. prevent non-preserved devices from getting preserved late).

---
 drivers/pci/liveupdate.c       | 16 ++++++++++++++++
 drivers/pci/liveupdate.h       |  5 +++++
 drivers/pci/pci-driver.c       |  2 ++
 include/linux/pci_liveupdate.h |  3 +++
 4 files changed, 26 insertions(+)
drivers/pci/liveupdate.c +16 -0
index 74a11e520f0d..64052892ea84 100644
@@ -352,6 +352,11 @@ static int pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, s
 {
 	struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;
 
+	if (dev->liveupdate.frozen) {
+		pci_warn(dev, "Cannot unpreserve device after it is frozen!\n");
+		return -EINVAL;
+	}
+
 	if (!dev_ser) {
 		pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
 		return -EINVAL;
@@ -422,6 +427,11 @@ static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, s
 
 static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
 {
+	if (dev->liveupdate.frozen) {
+		pci_warn(dev, "Cannot preserve device after it is frozen!\n");
+		return -EINVAL;
+	}
+
 	if (dev->liveupdate.outgoing)
 		return pci_liveupdate_preserve_device_again(dev);
 
@@ -713,6 +723,12 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 	}
 }
 
+void pci_liveupdate_freeze(struct pci_dev *dev)
+{
+	guard(rwsem_write)(&pci_liveupdate.rwsem);
+	dev->liveupdate.frozen = 1;
+}
+
 static int pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
 {
 	if (!dev->liveupdate.incoming) {
drivers/pci/liveupdate.h +5 -0
index 6f21ec50927b..bcb0bc73d684 100644
@@ -13,6 +13,7 @@
 #ifdef CONFIG_PCI_LIVEUPDATE
 void pci_liveupdate_setup_device(struct pci_dev *dev);
 void pci_liveupdate_cleanup_device(struct pci_dev *dev);
+void pci_liveupdate_freeze(struct pci_dev *dev);
 bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 				      int pass);
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
@@ -28,6 +29,10 @@ static inline void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 {
 }
 
+static inline void pci_liveupdate_freeze(struct pci_dev *dev)
+{
+}
+
 static inline bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus,
 						    struct pci_dev *dev,
 						    int pass)
drivers/pci/pci-driver.c +2 -0
index f36778e62ac1..51616e4327d3 100644
@@ -21,6 +21,7 @@
 #include <linux/acpi.h>
 #include <linux/dma-map-ops.h>
 #include <linux/iommu.h>
+#include "liveupdate.h"
 #include "pci.h"
 #include "pcie/portdrv.h"
 
@@ -536,6 +537,7 @@ static void pci_device_shutdown(struct device *dev)
 	struct pci_dev *pci_dev = to_pci_dev(dev);
 	struct pci_driver *drv = pci_dev->driver;
 
+	pci_liveupdate_freeze(pci_dev);
 	pm_runtime_resume(dev);
 
 	if (drv && drv->shutdown)
include/linux/pci_liveupdate.h +3 -0
index 2446c6d237ca..150993405754 100644
@@ -24,6 +24,8 @@
  * @was_preserved: True if this struct pci_dev was preserved by the previous
  *                 kernel. Unlike @incoming, this field is not cleared after
  *                 the device is finished participating in Live Update.
+ * @frozen: True if the outgoing preservation status of this device is frozen
+ *          and thus cannot be changed.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
@@ -31,6 +33,7 @@ struct pci_liveupdate {
 	u16 acs_ctrl;
 	bool inherit_buses;
 	bool was_preserved;
+	bool frozen;
 };
 
 struct pci_dev;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#67 lore raw

PATCH v7 11/12 PCI: liveupdate: Do not disable bus mastering on preserved devices during kexec

In reply to David Matlack

Do not disable bus mastering on outgoing preserved devices during pci_device_shutdown() for kexec.

Preserved devices must be allowed to perform memory transactions during a Live Update to ensure continuous operation. Clearing the bus mastering bit would prevent these devices from issuing any memory requests while the new kernel boots.

Because bridges upstream of preserved endpoint devices are also automatically preserved, this change also avoids clearing bus mastering on them. This is critical because clearing bus mastering on an upstream bridge prevents the bridge from forwarding memory requests upstream (i.e. it would prevent the endpoint device from accessing system RAM and doing peer-to-peer transactions with devices not downstream of the bridge).

---
 drivers/pci/liveupdate.c | 11 +++++++++++
 drivers/pci/liveupdate.h |  6 ++++++
 drivers/pci/pci-driver.c |  7 +++++--
 3 files changed, 22 insertions(+), 2 deletions(-)
drivers/pci/liveupdate.c +11 -0
index 64052892ea84..5935e88de073 100644
@@ -132,6 +132,10 @@
  *  * The PCI core inherits ARI Forwarding Enable on all bridges with downstream
  *    preserved devices to ensure that all preserved devices on the bridge's
  *    secondary bus are addressable after the Live Update.
+ *
+ *  * The PCI core does not disable bus mastering on outgoing preserved devices
+ *    during kexec. This allows preserved devices to issue memory transactions
+ *    throughout the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -850,6 +854,13 @@ int pci_liveupdate_configure_ari(struct pci_dev *dev)
 	return 0;
 }
 
+bool pci_liveupdate_is_outgoing(struct pci_dev *dev)
+{
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+	pci_WARN_ONCE(dev, !dev->liveupdate.frozen, "Preservation status is unstable!\n");
+	return dev->liveupdate.outgoing;
+}
+
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +6 -0
index bcb0bc73d684..b266406aaac8 100644
@@ -20,6 +20,7 @@ void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
 void pci_liveupdate_init_acs(struct pci_dev *dev);
 int pci_liveupdate_enable_acs(struct pci_dev *dev);
 int pci_liveupdate_configure_ari(struct pci_dev *dev);
+bool pci_liveupdate_is_outgoing(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -57,6 +58,11 @@ static inline int pci_liveupdate_configure_ari(struct pci_dev *dev)
 {
 	return -EINVAL;
 }
+
+static inline bool pci_liveupdate_is_outgoing(struct pci_dev *dev)
+{
+	return false;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci-driver.c +5 -2
index 51616e4327d3..47cb3dcaa927 100644
@@ -546,11 +546,14 @@ static void pci_device_shutdown(struct device *dev)
 	/*
 	 * If this is a kexec reboot, turn off Bus Master bit on the
 	 * device to tell it to not continue to do DMA. Don't touch
-	 * devices in D3cold or unknown states.
+	 * devices being preserved for Live Update or in D3cold or
+	 * unknown states.
+	 *
 	 * If it is not a kexec reboot, firmware will hit the PCI
 	 * devices with big hammer and stop their DMA any way.
 	 */
-	if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
+	if (kexec_in_progress && !pci_liveupdate_is_outgoing(pci_dev) &&
+	    pci_dev->current_state <= PCI_D3hot)
 		pci_clear_master(pci_dev);
 }
 
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#70 lore raw

PATCH v7 12/12 Documentation: PCI: Add documentation for Live Update

In reply to David Matlack

Add documentation files for the PCI subsystem's participation in Live Update.

These documentation files are generated from the kernel-doc comments in the PCI Live Update source code. They describe the File-Lifecycle Bound (FLB) API, the device tracking API, and the specific policies applied to preserved devices (such as bus number inheritance and bus mastering preservation).

---
 Documentation/PCI/index.rst           |  1 +
 Documentation/PCI/liveupdate.rst      | 29 +++++++++++++++++++++++++++
 Documentation/core-api/liveupdate.rst |  1 +
 MAINTAINERS                           |  1 +
 4 files changed, 32 insertions(+)
 create mode 100644 Documentation/PCI/liveupdate.rst
Documentation/PCI/index.rst +1 -0
index 5d720d2a415e..23fb737ac969 100644
@@ -20,3 +20,4 @@ PCI Bus Subsystem
    controller/index
    boot-interrupts
    tph
+   liveupdate
Documentation/PCI/liveupdate.rst new file +29 -0
new file mode 100644
index 000000000000..eba55f8a92ae
@@ -0,0 +1,29 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+===========================
+PCI Support for Live Update
+===========================
+
+.. kernel-doc:: drivers/pci/liveupdate.c
+   :doc: PCI Live Update
+
+Driver API
+==========
+
+.. kernel-doc:: drivers/pci/liveupdate.c
+   :export:
+
+Live Update ABI
+===============
+
+.. kernel-doc:: include/linux/kho/abi/pci.h
+   :doc: PCI File-Lifecycle Bound (FLB) Live Update ABI
+
+.. kernel-doc:: include/linux/kho/abi/pci.h
+   :internal:
+
+See Also
+========
+
+ * :doc:`/core-api/liveupdate`
+ * :doc:`/core-api/kho/index`
Documentation/core-api/liveupdate.rst +1 -0
index b3c689e633c1..2bce2644eba2 100644
@@ -74,3 +74,4 @@ See Also
 
 - :doc:`Live Update uAPI </userspace-api/liveupdate>`
 - :doc:`/core-api/kho/index`
+- :doc:`PCI </PCI/liveupdate>`
MAINTAINERS +1 -0
index 08a724b860dc..347c435ca404 100644
@@ -20833,6 +20833,7 @@ L:	[email protected]
 L:	[email protected]
 S:	Maintained
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git
+F:	Documentation/PCI/liveupdate.rst
 F:	drivers/pci/liveupdate.c
 F:	drivers/pci/liveupdate.h
 F:	include/linux/kho/abi/pci.h
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#36 lore raw

Re: [PATCH v7 04/12] PCI: liveupdate: Document driver binding responsibilities

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

Document how driver binding works during a Live Update and what the PCI core expects of drivers and users. Note that this is only a description of the current division of responsibilities. These can change in the future if we decide.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#38 lore raw

Re: [PATCH v7 05/12] PCI: liveupdate: Keep bus numbers constant during Live Update

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines

During a Live Update, preserved devices must be allowed to continue performing memory transactions so the kernel cannot change the fabric topology, including bus numbers, since that would require disabling and flushing any memory transactions first.

To keep bus numbers constant, always inherit the secondary and subordinate bus numbers assigned to bridges during scanning, instead of assigning new ones, if any PCI devices are being preserved. Note that the kernel inherits bus numbers even on bridges without any downstream endpoints that were preserved. This avoids accidentally assigning a bridge a new window that overlaps with a preserved device that is downstream of a different bridge.

If a bridge is scanned with a broken topology or has no bus numbers set during a Live Update, refuse to assign it new bus numbers and refuse to enumerate devices below it until the Live Update is finished. This is a safety measure to prevent topology conflicts.

Quoted 7 lines

Require that CONFIG_CARDBUS is not enabled to enable CONFIG_PCI_LIVEUPDATE since inheriting bus numbers on PCI-to-CardBus bridges requires additional work but is not a priority at the moment.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#42 lore raw

Re: [PATCH v7 06/12] PCI: liveupdate: Auto-preserve upstream bridges across Live Update

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

When a PCI device is preserved across a Live Update, all of its upstream bridges up to the root port must also be preserved. This enables the PCI core and any drivers bound to the bridges to manage bridges correctly across a Live Update.

Notably, this will be used in subsequent commits to ensure that preserved devices can continue performing memory transactions without a disruption or change in routing.

Quoted 7 lines

To preserve bridges, the PCI core tracks the number of downstream devices preserved under each bridge using a reference count in struct pci_dev_ser. This allows a bridge to remain preserved until all its downstream preserved devices are unpreserved or finish their participation in the Live Update.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#47 lore raw

Re: [PATCH v7 07/12] PCI: Refactor matching logic for pci_dev_acs_ops

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Refactor the logic to match devices to pci_dev_acs_ops by factoring out the loop and device matching into its own routine. This eliminates some duplicate code between pci_dev_specific_enable_acs() and pci_dev_specific_disable_acs_redir(), and will also be used in a subsequent commit to check if a device requires device-specific enable_acs() during a Live Update.

No functional change intended.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#51 lore raw

Re: [PATCH v7 08/12] PCI: liveupdate: Inherit ACS flags in incoming preserved devices

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

Inherit Access Control Services (ACS) flags on all incoming preserved devices (endpoints and upstream bridges) during a Live Update.

Inheriting ACS flags avoids changing routing rules while memory transactions are in flight from preserved devices. This is also strictly necessary to ensure that IOMMU group assignments do not change across a Live Update for preserved devices, as changing ACS configurations can split or merge IOMMU groups.

Cache the inherited ACS controls established by the previous kernel in struct pci_dev so that ACS controls do not change after a reset (pci_restore_state() calls pci_enable_acs()).

Quoted 5 lines

To simplify ACS inheritance, reject preserving any devices that require quirks to enable ACS as those quirks would also have to take Live Update into account.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#65 lore raw

Re: [PATCH v7 10/12] PCI: liveupdate: Freeze preservation status during shutdown

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Freeze a device's outgoing preservation status (preserved or not preserved) during shutdown. This enables the PCI core and drivers to safely make decisions based on the device's preservation status during shutdown.

Quoted 7 lines

Note that pci_liveupdate_freeze() is triggered by the PCI core rather than from drivers participating in Live Update so that all devices can have their status frozen (i.e. prevent non-preserved devices from getting preserved late).

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#2 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.

Preserving PCI devices across Live Update is built on top of the Live Update Orchestrator's (LUO) support for file preservation. Drivers are expected to expose a file to userspace to represent a single PCI device and support preservation of that file. This is intended primarily to support preservation of PCI devices bound to VFIO drivers.

This commit enables drivers to register their liveupdate_file_handler with the PCI core so that the PCI core can do its own tracking and enforcement of which devices are preserved.
[ ... ]
assumed to return true. If KHO was not enabled then Live Update would not be enabled and these routines would never run.

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#71 lore raw

Re: [PATCH v7 12/12] Documentation: PCI: Add documentation for Live Update

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Add documentation files for the PCI subsystem's participation in Live Update.

These documentation files are generated from the kernel-doc comments in the PCI Live Update source code. They describe the File-Lifecycle Bound (FLB) API, the device tracking API, and the specific policies applied to preserved devices (such as bus number inheritance and bus mastering preservation).

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#56 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to David Matlack
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Data race on the `ari_enabled` bitfield of the bridge device. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Data race on the ari_enabled bitfield of the bridge device.
--

--- Patch [9]: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges --- commit dba41feb80b848815c664a662c635fefb4ca7403
Author: David Matlack <[email protected]>

PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

This commit inherits the ARI Forwarding Enable on preserved bridges during a Live Update. It updates the pci_dev->ari_enabled flag so preserved devices can be identified with the expanded 8-bit function number.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index a95bfe5eff770..74a11e520f0d3 100644

[ ... ]

Quoted 5 lines
drivers/pci/liveupdate.c +1 -0
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
 	return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 833

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+{
+	u16 val;
+
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->liveupdate.incoming)
+		return -EINVAL;
+
+	pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+	dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);

[Severity: Medium]
Can this bitfield modification race with concurrent updates to other bitfields in the same word?

In pci_liveupdate_configure_ari(), dev->ari_enabled is updated. Since bitfield modifications compile to read-modify-write operations on the underlying word, concurrent updates to other flags in the same word (like is_busmaster, is_managed, or msi_enabled) could be silently lost.

The pci_liveupdate.rwsem protects liveupdate structures but does not appear to provide mutual exclusion for the pci_dev bitfields.

Quoted 2 lines
drivers/pci/liveupdate.c +2 -0
+	return 0;
+}

[ ... ]

Quoted 4 lines
drivers/pci/pci.c +0 -0
index 739ecaab2e76f..e0c133b66a359 100644

[ ... ]

Quoted 13 lines
drivers/pci/pci.c +6 -0
@@ -3539,6 +3539,12 @@ void pci_configure_ari(struct pci_dev *dev)
 	if (!(cap & PCI_EXP_DEVCAP2_ARI))
 		return;
 
+	if (!pci_liveupdate_configure_ari(bridge))
+		return;
+
+	if (pcie_ari_disabled)
+		return;
+
 	if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
 		pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
 					 PCI_EXP_DEVCTL2_ARI);
Quoted 1 line

bridge->ari_enabled = 1;

[Severity: Medium]
This isn't a bug introduced by this patch, but this preexisting fallback path in pci_configure_ari() also modifies the bridge->ari_enabled bitfield.

Could this also be susceptible to the same read-modify-write data race against other flags in the same word?

Quoted 2 lines
drivers/pci/pci.c +0 -0
 	} else {
 		pcie_capability_clear_word(bridge, PCI_EXP_DEVCTL2,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#68 lore raw

Re: [PATCH v7 11/12] PCI: liveupdate: Do not disable bus mastering on preserved devices during kexec

In reply to David Matlack
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Do not disable bus mastering on outgoing preserved devices during pci_device_shutdown() for kexec.

Preserved devices must be allowed to perform memory transactions during a Live Update to ensure continuous operation. Clearing the bus mastering bit would prevent these devices from issuing any memory requests while the new kernel boots.

Because bridges upstream of preserved endpoint devices are also automatically preserved, this change also avoids clearing bus mastering on them. This is critical because clearing bus mastering on an upstream bridge prevents the bridge from forwarding memory requests upstream (i.e. it would prevent the endpoint device from accessing system RAM and doing peer-to-peer transactions with devices not downstream of the bridge).

Quoted 2 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#12 lore raw

Re: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices

In reply to David Matlack
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing lock acquisition in FLB destruction callbacks allows concurrent operations to trigger a…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing lock acquisition in FLB destruction callbacks allows concurrent operations to trigger a Use-After-Free.
- [High] A dangling dev->liveupdate.outgoing pointer after an aborted Live Update leads to a Use-After-Free if a subsequent Live Update is attempted. --

--- Patch [2]: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices --- commit 9a74c06de23d913aa84d2cbcc2aa3409bb1d2eb3
Author: David Matlack <[email protected]>

PCI: liveupdate: Track outgoing preserved PCI devices

This patch adds tracking for outgoing preserved PCI devices across Kexec Handover (Live Update). It introduces APIs for drivers to notify the core when devices are preserved or unpreserved, updating the PCI core's FLB data.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index 899758883dd5d..03075ce06ac98 100644

[ ... ]

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 211

Quoted 3 lines
drivers/pci/liveupdate.c +3 -0
+static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;

[Severity: High]
If a previous Live Update was aborted without explicitly unpreserving all devices, will dev->liveupdate.outgoing point to freed memory here?

During an abort, pci_flb_unpreserve() executes:

    WARN_ON(outgoing->ser->nr_devices);
    kho_block_set_destroy(&outgoing->block_set);

This destroys the block set backing the dev_ser allocations, but the dev->liveupdate.outgoing pointers on the actual pci_dev structs are never cleared.

If a subsequent Live Update is started and pci_liveupdate_unpreserve() is called on one of those devices, could we dereference that dangling dev_ser pointer and corrupt memory when clearing it below?

Quoted 11 lines
drivers/pci/liveupdate.c +11 -0
+
+	if (!dev_ser) {
+		pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
+		return;
+	}
+
+	pci_info(dev, "Device will no longer be preserved across next Live Update\n");
+	outgoing->ser->nr_devices--;
+	memset(dev_ser, 0, sizeof(*dev_ser));
+	dev->liveupdate.outgoing = NULL;
+}

[ ... ]

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 291

Quoted 7 lines
drivers/pci/liveupdate.c +7 -0
+void pci_liveupdate_unpreserve(struct pci_dev *dev)
+{
+	struct pci_flb_outgoing *outgoing = NULL;
+
+	guard(rwsem_write)(&pci_liveupdate.rwsem);
+
+	outgoing = pci_liveupdate_flb_get_outgoing();

[Severity: High]
Does the destruction path in pci_flb_unpreserve() also need to acquire pci_liveupdate.rwsem?

If a PCI device is hot-removed concurrently with a Live Update abort:

Thread 1 (driver remove):
pci_liveupdate_unpreserve()

    guard(rwsem_write)(&pci_liveupdate.rwsem);
    outgoing = pci_liveupdate_flb_get_outgoing();
    ... begins modifying outgoing and dev_ser

Thread 2 (Live Update core abort):
pci_flb_unpreserve()

    kho_block_set_destroy(&outgoing->block_set);
    kho_unpreserve_free(outgoing->ser);
    kfree(outgoing);

If pci_flb_unpreserve() destroys the outgoing FLB without acquiring the semaphore, could Thread 1 end up writing to freed memory?

Quoted 8 lines
drivers/pci/liveupdate.c +8 -0
+	if (IS_ERR(outgoing)) {
+		pci_warn(dev, "Cannot unpreserve device without outgoing Live Update state\n");
+		return;
+	}
+
+	pci_liveupdate_unpreserve_device(outgoing, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#18 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] pci_liveupdate_finish_device() incorrectly aborts if refcount != 1, completely breaking the…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] pci_liveupdate_finish_device() incorrectly aborts if refcount != 1, completely breaking the preservation lifecycle for parent bridges and leaking their FLB references.
- [Medium] pci_liveupdate_cleanup_device() fails to clear dev_ser->refcount, leading to state contamination for subsequently hot-plugged devices.
- [Medium] Memory allocated for incoming preserved PCI devices (the KHO block set) is leaked in pci_flb_retrieve() if kzalloc_obj() fails.
--

--- Patch [3]: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices --- commit 4fcd6c9f661309adcb1a2b891b9c5a34af9e319f
Author: David Matlack <[email protected]>

PCI: liveupdate: Track incoming preserved PCI devices

This patch adds support to the PCI core for tracking preserved PCI devices across Live Update. It introduces pci_liveupdate_setup_device() to fetch serialized state from the Live Update Orchestrator and exposes APIs for drivers to notify the PCI core when preservation is finished.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac98..df6a02240aa47 100644

[ ... ]

Quoted 14 lines
drivers/pci/liveupdate.c +9 -1
@@ -138,13 +172,58 @@ static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
 
 static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
 {
-	args->obj = phys_to_virt(args->data);
+	struct pci_ser *ser = phys_to_virt(args->data);
+	struct pci_flb_incoming *incoming;
+	struct pci_dev_ser *dev_ser;
+	struct kho_block_set_it it;
+	int ret = -ENOMEM;
+
+	incoming = kzalloc_obj(*incoming);
+	if (!incoming)
+		goto err_restore_free;

[ ... ]

drivers/pci/liveupdate.c static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args) · line 217

Quoted 9 lines
drivers/pci/liveupdate.c +8 -0
+err_block_set_destroy:
+	kho_block_set_destroy(&incoming->block_set);
+err_free_incoming:
+	xa_destroy(&incoming->xa);
+	kfree(incoming);
+err_restore_free:
+	kho_restore_free(ser);
+	return ret;
 }

[Severity: Medium]
If kzalloc_obj() fails, the code jumps to err_restore_free and calls kho_restore_free(ser).

Because the underlying memory pages backing ser->devices (the KHO block set) have not been restored or explicitly freed, does this error path permanently leak the memory reserved for the block set?

[ ... ]

Quoted 1 line
drivers/pci/liveupdate.c +0 -0
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... ]

Quoted 2 lines
drivers/pci/liveupdate.c +2 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{

[ ... ]

Quoted 12 lines
drivers/pci/liveupdate.c +12 -0
+	dev_ser = xa_load(&incoming->xa, key);
+
+	/*
+	 * This device was not preserved across Live Update, or it was preserved
+	 * but has already been probed and gone through pci_liveupdate_finish(),
+	 * e.g. due to removing and re-adding the device. Either way, it's not
+	 * treated as incoming-preserved.
+	 */
+	if (!dev_ser || !dev_ser->refcount) {
+		pci_liveupdate_flb_put_incoming();
+		return;
+	}

[ ... ]

drivers/pci/liveupdate.c void pci_liveupdate_cleanup_device(struct pci_dev *dev) · line 477

Quoted 13 lines

void pci_liveupdate_cleanup_device(struct pci_dev *dev) {

drivers/pci/liveupdate.c +5 -0
 	/*
@@ -309,7 +469,83 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 	 */
 	if (READ_ONCE(dev->liveupdate.outgoing))
 		pci_WARN(dev, 1, "Destroying outgoing-preserved device!\n");
+
+	if (READ_ONCE(dev->liveupdate.incoming)) {
+		pci_WARN(dev, 1, "Destroying incoming-preserved device!\n");
+		pci_liveupdate_flb_put_incoming();
+	}
 }

[Severity: Medium]
In pci_liveupdate_cleanup_device(), the FLB reference is dropped but dev_ser->refcount is left unmodified.

If a preserved device is removed or destroyed before its driver calls pci_liveupdate_finish() (for instance, if probe fails), and a new device is subsequently hot-plugged at the same BDF, won't pci_liveupdate_setup_device() see the stale dev_ser with a non-zero refcount and incorrectly associate the new device with the stale preserved state?

drivers/pci/liveupdate.c void pci_liveupdate_cleanup_device(struct pci_dev *dev) · line 489

Quoted 11 lines
drivers/pci/liveupdate.c +11 -0
+static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
+{
+	if (!dev->liveupdate.incoming) {
+		pci_warn(dev, "Cannot finish preserving an unpreserved device\n");
+		return;
+	}
+
+	if (dev->liveupdate.incoming->refcount != 1) {
+		pci_WARN(dev, 1, "Preserved device has a corrupted refcount!\n");
+		return;
+	}

[Severity: High]
Is this strict refcount != 1 check correct for PCI bridges?

Looking at the outgoing path in pci_liveupdate_preserve_device_again(), a bridge legitimately has its refcount incremented for each preserved downstream device, making it greater than 1.

When the bridge driver calls pci_liveupdate_finish() on the incoming side, won't this check fail, abort the finish process, and permanently leak the FLB reference for the bridge?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#57 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to [email protected]
Quoted 42 lines · On Fri, Jul 10, 2026 at 2:37 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Data race on the ari_enabled bitfield of the bridge device.
--

--- Patch [9]: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges --- commit dba41feb80b848815c664a662c635fefb4ca7403
Author: David Matlack <[email protected]>

PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

This commit inherits the ARI Forwarding Enable on preserved bridges during a Live Update. It updates the pci_dev->ari_enabled flag so preserved devices can be identified with the expanded 8-bit function number.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index a95bfe5eff770..74a11e520f0d3 100644

[ ... ]

Quoted 5 lines
drivers/pci/liveupdate.c +1 -0
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
      return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 833

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+{
+     u16 val;
+
+     guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+     if (!dev->liveupdate.incoming)
+             return -EINVAL;
+
+     pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+     dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);

[Severity: Medium]
Can this bitfield modification race with concurrent updates to other bitfields in the same word?

Quoted 7 lines

In pci_liveupdate_configure_ari(), dev->ari_enabled is updated. Since bitfield modifications compile to read-modify-write operations on the underlying word, concurrent updates to other flags in the same word (like is_busmaster, is_managed, or msi_enabled) could be silently lost.

The pci_liveupdate.rwsem protects liveupdate structures but does not appear to provide mutual exclusion for the pci_dev bitfields.

pci_liveupdate_configure_ari() is called from pci_configure_ari() which also sets dev->ari_enabled=1 and is pre-existing code.

If writing to dev->ari_enabled in this path is indeed unsafe then that is a pre-existing bug.

Quoted 32 lines
Quoted 3 lines
drivers/pci/liveupdate.c +2 -0
+     return 0;
+}

[ ... ]

Quoted 4 lines
drivers/pci/pci.c +0 -0
index 739ecaab2e76f..e0c133b66a359 100644

[ ... ]

Quoted 13 lines
drivers/pci/pci.c +6 -0
@@ -3539,6 +3539,12 @@ void pci_configure_ari(struct pci_dev *dev)
      if (!(cap & PCI_EXP_DEVCAP2_ARI))
              return;
 
+     if (!pci_liveupdate_configure_ari(bridge))
+             return;
+
+     if (pcie_ari_disabled)
+             return;
+
      if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
              pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
                                       PCI_EXP_DEVCTL2_ARI);
Quoted 1 line
              bridge->ari_enabled = 1;

[Severity: Medium]
This isn't a bug introduced by this patch, but this preexisting fallback path in pci_configure_ari() also modifies the bridge->ari_enabled bitfield.

Quoted 2 lines

Could this also be susceptible to the same read-modify-write data race against other flags in the same word?

See above.

#13 lore raw

Re: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices

In reply to [email protected]
Quoted 36 lines · On Fri, Jul 10, 2026 at 2:39 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing lock acquisition in FLB destruction callbacks allows concurrent operations to trigger a Use-After-Free.
- [High] A dangling dev->liveupdate.outgoing pointer after an aborted Live Update leads to a Use-After-Free if a subsequent Live Update is attempted. --

--- Patch [2]: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices --- commit 9a74c06de23d913aa84d2cbcc2aa3409bb1d2eb3
Author: David Matlack <[email protected]>

PCI: liveupdate: Track outgoing preserved PCI devices

This patch adds tracking for outgoing preserved PCI devices across Kexec Handover (Live Update). It introduces APIs for drivers to notify the core when devices are preserved or unpreserved, updating the PCI core's FLB data.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index 899758883dd5d..03075ce06ac98 100644

[ ... ]

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 211

Quoted 3 lines
drivers/pci/liveupdate.c +3 -0
+static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+     struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;

[Severity: High]
If a previous Live Update was aborted without explicitly unpreserving all devices, will dev->liveupdate.outgoing point to freed memory here?

During an abort, pci_flb_unpreserve() executes:

    WARN_ON(outgoing->ser->nr_devices);
    kho_block_set_destroy(&outgoing->block_set);
Quoted 7 lines

This destroys the block set backing the dev_ser allocations, but the dev->liveupdate.outgoing pointers on the actual pci_dev structs are never cleared.

If a subsequent Live Update is started and pci_liveupdate_unpreserve() is called on one of those devices, could we dereference that dangling dev_ser pointer and corrupt memory when clearing it below?

This would only be possible if a driver violated its contract with the PCI core to call pci_liveupdate_unpreserve() when the device is unpreserved.

Quoted 35 lines
Quoted 12 lines
drivers/pci/liveupdate.c +11 -0
+
+     if (!dev_ser) {
+             pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
+             return;
+     }
+
+     pci_info(dev, "Device will no longer be preserved across next Live Update\n");
+     outgoing->ser->nr_devices--;
+     memset(dev_ser, 0, sizeof(*dev_ser));
+     dev->liveupdate.outgoing = NULL;
+}

[ ... ]

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 291

Quoted 7 lines
drivers/pci/liveupdate.c +7 -0
+void pci_liveupdate_unpreserve(struct pci_dev *dev)
+{
+     struct pci_flb_outgoing *outgoing = NULL;
+
+     guard(rwsem_write)(&pci_liveupdate.rwsem);
+
+     outgoing = pci_liveupdate_flb_get_outgoing();

[Severity: High]
Does the destruction path in pci_flb_unpreserve() also need to acquire pci_liveupdate.rwsem?

If a PCI device is hot-removed concurrently with a Live Update abort:

Thread 1 (driver remove):
pci_liveupdate_unpreserve()

    guard(rwsem_write)(&pci_liveupdate.rwsem);
    outgoing = pci_liveupdate_flb_get_outgoing();
    ... begins modifying outgoing and dev_ser
Quoted 8 lines

Thread 2 (Live Update core abort):
pci_flb_unpreserve()

    kho_block_set_destroy(&outgoing->block_set);
    kho_unpreserve_free(outgoing->ser);
    kfree(outgoing);

If pci_flb_unpreserve() destroys the outgoing FLB without acquiring the semaphore, could Thread 1 end up writing to freed memory?

No. Drivers must call pci_liveupdate_{,un}preserve() only from their file handler callbacks, which cannot race with FLB handler callbacks (enforced by LUO).

Quoted 12 lines

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 299

Quoted 9 lines
drivers/pci/liveupdate.c +8 -0
+     if (IS_ERR(outgoing)) {
+             pci_warn(dev, "Cannot unpreserve device without outgoing Live Update state\n");
+             return;
+     }
+
+     pci_liveupdate_unpreserve_device(outgoing, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

#19 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to [email protected]
Quoted 56 lines · On Fri, Jul 10, 2026 at 2:39 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] pci_liveupdate_finish_device() incorrectly aborts if refcount != 1, completely breaking the preservation lifecycle for parent bridges and leaking their FLB references.
- [Medium] pci_liveupdate_cleanup_device() fails to clear dev_ser->refcount, leading to state contamination for subsequently hot-plugged devices.
- [Medium] Memory allocated for incoming preserved PCI devices (the KHO block set) is leaked in pci_flb_retrieve() if kzalloc_obj() fails.
--

--- Patch [3]: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices --- commit 4fcd6c9f661309adcb1a2b891b9c5a34af9e319f
Author: David Matlack <[email protected]>

PCI: liveupdate: Track incoming preserved PCI devices

This patch adds support to the PCI core for tracking preserved PCI devices across Live Update. It introduces pci_liveupdate_setup_device() to fetch serialized state from the Live Update Orchestrator and exposes APIs for drivers to notify the PCI core when preservation is finished.

Quoted 4 lines
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac98..df6a02240aa47 100644

[ ... ]

Quoted 14 lines
drivers/pci/liveupdate.c +9 -1
@@ -138,13 +172,58 @@ static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
 
 static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
 {
-     args->obj = phys_to_virt(args->data);
+     struct pci_ser *ser = phys_to_virt(args->data);
+     struct pci_flb_incoming *incoming;
+     struct pci_dev_ser *dev_ser;
+     struct kho_block_set_it it;
+     int ret = -ENOMEM;
+
+     incoming = kzalloc_obj(*incoming);
+     if (!incoming)
+             goto err_restore_free;

[ ... ]

drivers/pci/liveupdate.c static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args) · line 217

Quoted 9 lines
drivers/pci/liveupdate.c +8 -0
+err_block_set_destroy:
+     kho_block_set_destroy(&incoming->block_set);
+err_free_incoming:
+     xa_destroy(&incoming->xa);
+     kfree(incoming);
+err_restore_free:
+     kho_restore_free(ser);
+     return ret;
 }

[Severity: Medium]
If kzalloc_obj() fails, the code jumps to err_restore_free and calls kho_restore_free(ser).

Quoted 3 lines

Because the underlying memory pages backing ser->devices (the KHO block set) have not been restored or explicitly freed, does this error path permanently leak the memory reserved for the block set?

Yes. If retrieve fails, it permanently free the preserved memory and then panic sin the caller (pci_liveupdate_flb_get_incoming()).

Quoted 45 lines

[ ... ]

Quoted 1 line
drivers/pci/liveupdate.c +0 -0
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... ]

Quoted 2 lines
drivers/pci/liveupdate.c +2 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{

[ ... ]

Quoted 12 lines
drivers/pci/liveupdate.c +12 -0
+     dev_ser = xa_load(&incoming->xa, key);
+
+     /*
+      * This device was not preserved across Live Update, or it was preserved
+      * but has already been probed and gone through pci_liveupdate_finish(),
+      * e.g. due to removing and re-adding the device. Either way, it's not
+      * treated as incoming-preserved.
+      */
+     if (!dev_ser || !dev_ser->refcount) {
+             pci_liveupdate_flb_put_incoming();
+             return;
+     }

[ ... ]

drivers/pci/liveupdate.c void pci_liveupdate_cleanup_device(struct pci_dev *dev) · line 477

Quoted 13 lines

void pci_liveupdate_cleanup_device(struct pci_dev *dev)

 {
      /*
drivers/pci/liveupdate.c +5 -0
@@ -309,7 +469,83 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev)
       */
      if (READ_ONCE(dev->liveupdate.outgoing))
              pci_WARN(dev, 1, "Destroying outgoing-preserved device!\n");
+
+     if (READ_ONCE(dev->liveupdate.incoming)) {
+             pci_WARN(dev, 1, "Destroying incoming-preserved device!\n");
+             pci_liveupdate_flb_put_incoming();
+     }
 }

[Severity: Medium]
In pci_liveupdate_cleanup_device(), the FLB reference is dropped but dev_ser->refcount is left unmodified.

Quoted 5 lines

If a preserved device is removed or destroyed before its driver calls pci_liveupdate_finish() (for instance, if probe fails), and a new device is subsequently hot-plugged at the same BDF, won't pci_liveupdate_setup_device() see the stale dev_ser with a non-zero refcount and incorrectly associate the new device with the stale preserved state?

Same thing can happen if the user hot-plugs a device during Live Update before the new kernel enumerates that slot. The user should not hot-plug devices they are preserving across Live Update.

Also, probe() failing does not cause the device to be removed. probe() can still be retried.

The only way this would happen is if the initial setup of the device fails, e.g. the kernel can't allocate memory during scanning. The reason I opted to keep the refcount elevated is it at least gives the user the possibility of trying another scan and recovering the device if the issue was transient.

Quoted 16 lines

drivers/pci/liveupdate.c void pci_liveupdate_cleanup_device(struct pci_dev *dev) · line 489

Quoted 12 lines
drivers/pci/liveupdate.c +11 -0
+static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
+{
+     if (!dev->liveupdate.incoming) {
+             pci_warn(dev, "Cannot finish preserving an unpreserved device\n");
+             return;
+     }
+
+     if (dev->liveupdate.incoming->refcount != 1) {
+             pci_WARN(dev, 1, "Preserved device has a corrupted refcount!\n");
+             return;
+     }

[Severity: High]
Is this strict refcount != 1 check correct for PCI bridges?

Quoted 7 lines

Looking at the outgoing path in pci_liveupdate_preserve_device_again(), a bridge legitimately has its refcount incremented for each preserved downstream device, making it greater than 1.

When the bridge driver calls pci_liveupdate_finish() on the incoming side, won't this check fail, abort the finish process, and permanently leak the FLB reference for the bridge?

No, the refcount can only be 0 or 1 in this patch. Bridge preservation and pci_liveupdate_preserve_device_again() are not introduced until a later patch in this series, and that patch updates this code to decrement the refcount instead of checking for 1.

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

maintainer
#3 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to David Matlack

drivers/pci/liveupdate.c line 56

Quoted 13 lines · David Matlack
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..899758883dd5
@@ -0,0 +1,155 @@

[ ... skip 50 lines ... ]

drivers/pci/liveupdate.c new file +6 -0
+#include <linux/slab.h>
+
+/**
+ * struct pci_flb_outgoing - Outgoing PCI FLB object
+ * @ser: Pointer to the preserved struct pci_ser.
+ * @block_set: The KHO block set holding the outgoing devices.

AFAIK, these should be aligned after column:
@ser: Pointer to ...
@block_set: The KHO block ...

drivers/pci/liveupdate.c line 88

Quoted 7 lines

[ ... skip 26 lines ... ]

drivers/pci/liveupdate.c new file +6 -0
+	ser->devices = 0;
+
+	outgoing->ser = ser;
+	kho_block_set_init(&outgoing->block_set, sizeof(struct pci_dev_ser));
+
+	args->obj = outgoing;

Nit:
You could re-write the above:

args->obj = no_free_ptr(outgoing);

And declare outgoing like this:
struct pci_flb_outgoing *outgoing __free(kfree) = kzalloc_obj(*outgoing);

Also, remove kfree(outgoing) call.

Quoted 11 lines
include/linux/kho/abi/pci.h new file +0 -0
new file mode 100644
index 000000000000..de549016807a
@@ -0,0 +1,58 @@

[ ... skip 30 lines ... ]
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ * @refcount: Reference count used by the PCI core to keep track of whether it

Quoted 3 lines
+ *            is done using a device's struct pci_dev_ser. The value of the
+ *            refcount is equal to 1 when the struct pci_dev_ser is in use, and
+ *            0 otherwise.

I believe descriptions must be aligned.

Quoted 5 lines

+ */

include/linux/kho/abi/pci.h new file +4 -0
+struct pci_dev_ser {
+	u32 domain;
+	u16 bdf;
+	u16 refcount;

include/linux/kho/abi/pci.h line 51

Quoted 10 lines
include/linux/kho/abi/pci.h new file +10 -0
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Physical address of the first KHO block containing pci_dev_ser.

I believe descriptions must be aligned.

Nit: Please order the included headers alphabetically.

Nit: Please order the included headers alphabetically.

Signature · 1 line
Pasha Tatashin <[email protected]>
#4 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

Quoted 10 lines · Pasha Tatashin
Quoted 9 lines · David Matlack
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..899758883dd5
@@ -0,0 +1,155 @@

[ ... skip 50 lines ... ]

drivers/pci/liveupdate.c new file +2 -0
+#include <linux/slab.h>
+
Quoted 8 lines
Quoted 4 lines

+/**
+ * struct pci_flb_outgoing - Outgoing PCI FLB object
+ * @ser: Pointer to the preserved struct pci_ser.
+ * @block_set: The KHO block set holding the outgoing devices.

AFAIK, these should be aligned after column:
@ser: Pointer to ...
@block_set: The KHO block ...

Gosh I hope not, I never do that. The horizontal alignment is such a PITA to maintain.

Jason

maintainer Reviewed-by on v7 2/12
#14 lore raw

Re: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices

In reply to David Matlack

drivers/pci/liveupdate.c static struct liveupdate_flb pci_liveupdate_flb = { · line 183

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 899758883dd5..03075ce06ac9 100644
@@ -125,6 +160,157 @@ static struct liveupdate_flb pci_liveupdate_flb = {

[ ... skip 18 lines ... ]

drivers/pci/liveupdate.c +6 -0
+static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outgoing)
+{
+	struct pci_dev_ser *dev_ser, *found = NULL;
+	struct kho_block_set_it it;
+	int err;
+	u32 count = 0;

Nit, please move count above err, to follow reverse-christmas-tree order for declarations.

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer
#5 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to Jason Gunthorpe
Quoted 20 lines · Jason Gunthorpe
Quoted 10 lines · Pasha Tatashin
Quoted 9 lines · David Matlack
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..899758883dd5
@@ -0,0 +1,155 @@

[ ... skip 50 lines ... ]

drivers/pci/liveupdate.c new file +2 -0
+#include <linux/slab.h>
+
Quoted 8 lines
Quoted 4 lines

+/**
+ * struct pci_flb_outgoing - Outgoing PCI FLB object
+ * @ser: Pointer to the preserved struct pci_ser.
+ * @block_set: The KHO block set holding the outgoing devices.

AFAIK, these should be aligned after column:
@ser: Pointer to ...
@block_set: The KHO block ...

Quoted 2 lines

Gosh I hope not, I never do that. The horizontal alignment is such a PITA to maintain.

Agreed. I just see that everywhere, so I thought it was the rule. If not, that is great; I hate them myself.

Quoted 2 lines

Jason

maintainer
#6 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to Pasha Tatashin
Quoted 72 lines · Pasha Tatashin

drivers/pci/liveupdate.c line 56

Quoted 13 lines · David Matlack
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..899758883dd5
@@ -0,0 +1,155 @@

[ ... skip 50 lines ... ]

drivers/pci/liveupdate.c new file +6 -0
+#include <linux/slab.h>
+
+/**
+ * struct pci_flb_outgoing - Outgoing PCI FLB object
+ * @ser: Pointer to the preserved struct pci_ser.
+ * @block_set: The KHO block set holding the outgoing devices.

AFAIK, these should be aligned after column:
@ser: Pointer to ...
@block_set: The KHO block ...

drivers/pci/liveupdate.c line 88

Quoted 7 lines

[ ... skip 26 lines ... ]

drivers/pci/liveupdate.c new file +6 -0
+	ser->devices = 0;
+
+	outgoing->ser = ser;
+	kho_block_set_init(&outgoing->block_set, sizeof(struct pci_dev_ser));
+
+	args->obj = outgoing;

Nit:
You could re-write the above:

args->obj = no_free_ptr(outgoing);

And declare outgoing like this:
struct pci_flb_outgoing *outgoing __free(kfree) = kzalloc_obj(*outgoing);

Also, remove kfree(outgoing) call.

Quoted 11 lines
include/linux/kho/abi/pci.h new file +0 -0
new file mode 100644
index 000000000000..de549016807a
@@ -0,0 +1,58 @@

[ ... skip 30 lines ... ]
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ * @refcount: Reference count used by the PCI core to keep track of whether it

Quoted 3 lines
+ *            is done using a device's struct pci_dev_ser. The value of the
+ *            refcount is equal to 1 when the struct pci_dev_ser is in use, and
+ *            0 otherwise.

I believe descriptions must be aligned.

Quoted 5 lines

+ */

include/linux/kho/abi/pci.h new file +4 -0
+struct pci_dev_ser {
+	u32 domain;
+	u16 bdf;
+	u16 refcount;

include/linux/kho/abi/pci.h line 51

Quoted 10 lines
include/linux/kho/abi/pci.h new file +10 -0
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Physical address of the first KHO block containing pci_dev_ser.

I believe descriptions must be aligned.

Quoted 3 lines

Nit: Please order the included headers alphabetically.

Nit: Please order the included headers alphabetically.

The above was supposed to be right after #include, but b4 had a mind of its own.

Quoted 3 lines
Signature · 2 lines
-- 
Pasha Tatashin <[email protected]>
maintainer
#20 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+	/*
+	 * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+	 * that dev->liveupdate.incoming cannot get freed while the PCI core
+	 * has a pointer to it. It's better to leak the incoming FLB than do a
+	 * use-after-free if driver does not call pci_liveupdate_finish().
+	 */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 5/12
#39 lore raw

Re: [PATCH v7 05/12] PCI: liveupdate: Keep bus numbers constant during Live Update

In reply to David Matlack
Quoted 13 lines · David Matlack

During a Live Update, preserved devices must be allowed to continue performing memory transactions so the kernel cannot change the fabric topology, including bus numbers, since that would require disabling and flushing any memory transactions first.

To keep bus numbers constant, always inherit the secondary and subordinate bus numbers assigned to bridges during scanning, instead of assigning new ones, if any PCI devices are being preserved. Note that the kernel inherits bus numbers even on bridges without any downstream endpoints that were preserved. This avoids accidentally assigning a bridge a new window that overlaps with a preserved device that is downstream of a different bridge.

Quoted 1 line

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 6/12
#43 lore raw

Re: [PATCH v7 06/12] PCI: liveupdate: Auto-preserve upstream bridges across Live Update

In reply to David Matlack
Quoted 10 lines · David Matlack

When a PCI device is preserved across a Live Update, all of its upstream bridges up to the root port must also be preserved. This enables the PCI core and any drivers bound to the bridges to manage bridges correctly across a Live Update.

Notably, this will be used in subsequent commits to ensure that preserved devices can continue performing memory transactions without a disruption or change in routing.

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 7/12
#48 lore raw

Re: [PATCH v7 07/12] PCI: Refactor matching logic for pci_dev_acs_ops

In reply to David Matlack
Quoted 8 lines · David Matlack

Refactor the logic to match devices to pci_dev_acs_ops by factoring out the loop and device matching into its own routine. This eliminates some duplicate code between pci_dev_specific_enable_acs() and pci_dev_specific_disable_acs_redir(), and will also be used in a subsequent commit to check if a device requires device-specific enable_acs() during a Live Update.

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 8/12
#52 lore raw

Re: [PATCH v7 08/12] PCI: liveupdate: Inherit ACS flags in incoming preserved devices

In reply to David Matlack
Quoted 10 lines · David Matlack

Inherit Access Control Services (ACS) flags on all incoming preserved devices (endpoints and upstream bridges) during a Live Update.

Inheriting ACS flags avoids changing routing rules while memory transactions are in flight from preserved devices. This is also strictly necessary to ensure that IOMMU group assignments do not change across a Live Update for preserved devices, as changing ACS configurations can split or merge IOMMU groups.

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
#32 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 31 lines · On Fri, 10 Jul 2026 21:26:06 +0000 David Matlack <[email protected]> wrote:
drivers/pci/liveupdate.c +27 -0
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
 
+static struct pci_flb_incoming *pci_liveupdate_flb_get_incoming(void)
+{
+	struct pci_flb_incoming *incoming = NULL;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&incoming);
+
+	/* Live Update is not enabled. */
+	if (ret == -EOPNOTSUPP)
+		return NULL;
+
+	/* Live Update is enabled, but there is no incoming FLB data. */
+	if (ret == -ENODATA)
+		return NULL;
+
+	/*
+	 * Live Update is enabled and there is incoming FLB data, but none of it
+	 * matches pci_liveupdate_flb.compatible.
+	 *
+	 * This could mean that no PCI FLB data was passed by the previous
+	 * kernel, but it could also mean the previous kernel used a different
+	 * compatibility string (i.e. a different ABI).
+	 */
+	if (ret == -ENOENT) {
+		pr_info_once("No incoming FLB matched %s\n", pci_liveupdate_flb.compatible);
+		return NULL;
+	}

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 416

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+
+	/*
+	 * There is incoming FLB data that matches pci_liveupdate_flb.compatible
+	 * but it cannot be retrieved.
+	 */
+	if (ret)
+		panic("Failed to retrieve incoming FLB data (%d)\n", ret);
+
+	return incoming;
+}

I'm having trouble following the error escalation here. What's fundamentally the difference between FLB data being provided and not compatible (subtle log message) versus FLB data being provided and compatible but we cannot access it (panic!)?

Don't both suggest devices are running but we can't get their FLB data to continue letting them run?

The errno interpretation is also slightly different than the comment above liveupdate_flb_get_incoming():

* Return: 0 on success, or a negative errno on failure. -ENODATA means no
* incoming FLB data, -ENOENT means specific flb not found in the incoming
* data, -ENODEV if the FLB's module is unloading, and -EOPNOTSUPP when
* live update is disabled or not configured.

Thanks,
Alex

maintainer
#58 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to David Matlack
Quoted 9 lines · David Matlack
drivers/pci/liveupdate.c +1 -0
index a95bfe5eff77..74a11e520f0d 100644
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
 	return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 833

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+{
+	u16 val;
+
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->liveupdate.incoming)
+		return -EINVAL;
+
+	pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+	dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);

unsigned int ari_enabled:1;

Sashiko asks a valid question, what protects other bits in this word during modication? At a very list a comment is needed.

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 10/12
#66 lore raw

Re: [PATCH v7 10/12] PCI: liveupdate: Freeze preservation status during shutdown

In reply to David Matlack
Quoted 11 lines · David Matlack

Freeze a device's outgoing preservation status (preserved or not preserved) during shutdown. This enables the PCI core and drivers to safely make decisions based on the device's preservation status during shutdown.

Note that pci_liveupdate_freeze() is triggered by the PCI core rather than from drivers participating in Live Update so that all devices can have their status frozen (i.e. prevent non-preserved devices from getting preserved late).

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer Reviewed-by on v7 11/12
#69 lore raw

Re: [PATCH v7 11/12] PCI: liveupdate: Do not disable bus mastering on preserved devices during kexec

In reply to David Matlack
Quoted 9 lines · David Matlack

Do not disable bus mastering on outgoing preserved devices during pci_device_shutdown() for kexec.

Preserved devices must be allowed to perform memory transactions during a Live Update to ensure continuous operation. Clearing the bus mastering bit would prevent these devices from issuing any memory requests while the new kernel boots.

[...]

Signature · 1 line
Pasha Tatashin <[email protected]>
maintainer
#72 lore raw

Re: [PATCH v7 12/12] Documentation: PCI: Add documentation for Live Update

In reply to David Matlack
Quoted 10 lines · David Matlack
Documentation/PCI/liveupdate.rst new file +4 -0
new file mode 100644
index 000000000000..eba55f8a92ae
@@ -0,0 +1,29 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+===========================
+PCI Support for Live Update

Documentation/PCI/liveupdate.rst line 14

Quoted 10 lines
Documentation/PCI/liveupdate.rst new file +10 -0
+===========================
+
+.. kernel-doc:: drivers/pci/liveupdate.c
+   :doc: PCI Live Update
+
+Driver API
+==========
+
+.. kernel-doc:: drivers/pci/liveupdate.c
+   :export:

I think you need:

+Internal API
+============
+
+.. kernel-doc:: drivers/pci/liveupdate.c
+ :internal:
+

In order to include docs for structs such as pci_flb_outgoing, etc.

Signature · 1 line
Pasha Tatashin <[email protected]>
#21 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to Pasha Tatashin
Quoted 21 lines · Pasha Tatashin

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

Quoted 6 lines

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

We could avoid this by deleteing dev->liveupdate.incoming and, instead, fetching the incoming FLB and doing the xarray lookup every atime the PCI core needs to access the device's incoming ser struct, but that would be inefficient.

#33 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to Alex Williamson
Quoted 42 lines · Alex Williamson
Quoted 38 lines · On Fri, 10 Jul 2026 21:26:06 +0000 David Matlack <[email protected]> wrote:
drivers/pci/liveupdate.c +34 -0
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
 
+static struct pci_flb_incoming *pci_liveupdate_flb_get_incoming(void)
+{
+     struct pci_flb_incoming *incoming = NULL;
+     int ret;
+
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&incoming);
+
+     /* Live Update is not enabled. */
+     if (ret == -EOPNOTSUPP)
+             return NULL;
+
+     /* Live Update is enabled, but there is no incoming FLB data. */
+     if (ret == -ENODATA)
+             return NULL;
+
+     /*
+      * Live Update is enabled and there is incoming FLB data, but none of it
+      * matches pci_liveupdate_flb.compatible.
+      *
+      * This could mean that no PCI FLB data was passed by the previous
+      * kernel, but it could also mean the previous kernel used a different
+      * compatibility string (i.e. a different ABI).
+      */
+     if (ret == -ENOENT) {
+             pr_info_once("No incoming FLB matched %s\n", pci_liveupdate_flb.compatible);
+             return NULL;
+     }
+
+     /*
+      * There is incoming FLB data that matches pci_liveupdate_flb.compatible
+      * but it cannot be retrieved.
+      */
+     if (ret)
+             panic("Failed to retrieve incoming FLB data (%d)\n", ret);
Quoted 8 lines

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 416

Quoted 3 lines
drivers/pci/liveupdate.c +3 -0
+
+     return incoming;
+}

I'm having trouble following the error escalation here. What's fundamentally the difference between FLB data being provided and not compatible (subtle log message) versus FLB data being provided and compatible but we cannot access it (panic!)?

There is no fundamental difference IMO and I think we should handle them the same way (panic). The problem is the PCI core cannot actually distinguish between "PCI FLB provided and not compatible" (should panic) and "no PCI FLB provided" (should log). I would like to propose changes to LUO so that the FLB handlers can detect the former situation but that requires some non-trivial work that I didn't want to block on.

For now, I guess you could say it is up to the user to not perform Live Update between incompatible versions. I can update the documentation.

Quoted 11 lines

Don't both suggest devices are running but we can't get their FLB data to continue letting them run?

The errno interpretation is also slightly different than the comment above liveupdate_flb_get_incoming():

* Return: 0 on success, or a negative errno on failure. -ENODATA means no
* incoming FLB data, -ENOENT means specific flb not found in the incoming
* data, -ENODEV if the FLB's module is unloading, and -EOPNOTSUPP when
* live update is disabled or not configured.

I think the only disagreement is the lack of specific -ENODEV handling. Is that what you are referring to?

Quoted 3 lines

Thanks,
Alex

maintainer
#22 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 23 lines · David Matlack
Quoted 8 lines · Pasha Tatashin

On Fri, 10 Jul 2026 21:26:06 +0000, David Matlack <[email protected]> wrote:

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

Quoted 6 lines

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

Quoted 4 lines

We could avoid this by deleteing dev->liveupdate.incoming and, instead, fetching the incoming FLB and doing the xarray lookup every atime the PCI core needs to access the device's incoming ser struct, but that would be inefficient.

Thanks for the explanation. As I understand, the most straightforward way to avoid holding the permanent reference is indeed to delete dev->liveupdate.incoming entirely and perform an xarray lookup on every access, like this:

bool pci_liveupdate_is_incoming(struct pci_dev *dev)

{
	...
	incoming = pci_liveupdate_flb_get_incoming();
	...
	dev_ser = xa_load(&incoming->xa, key);
	...
	pci_liveupdate_flb_put_incoming();
	return dev_ser && dev_ser->refcount > 0;
}

However, as you note, this is inefficient because it affects every single device and adds lookup overhead to every access (not sure about the actual cost though, xarray access is pretty fast!).

We can, however, still avoid tinkering with the lifecycle of the FLB, and instead treat dev->liveupdate.incoming as a "hint" that we validate on access with a fast, liveness check:

1. At Setup: In pci_liveupdate_setup_device(), we do the xarray lookup once, cache the pointer in dev->liveupdate.incoming, and immediately call pci_liveupdate_flb_put_incoming(). We do not hold a permanent reference.

2. On Access: When an accessor runs, instead of doing a full xarray lookup, it just validates the cached pointer's liveness by temporarily securing the FLB:

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
	struct pci_flb_incoming *incoming;

	incoming = pci_liveupdate_flb_get_incoming();
	if (!incoming)
		return NULL;

	if (dev->liveupdate.incoming)
		return incoming;

	pci_liveupdate_flb_put_incoming();
	return NULL;
}

* If get_incoming() returns NULL (the FLB has already finished/freed), the hint is invalid and the device is no longer incoming.

* If it returns a valid pointer, the FLB is guaranteed to be alive, so we can immediately use the cached dev->liveupdate.incoming pointer without xarray lookups. The accessor then puts the transient reference when done.

3. In Finish/Cleanup: We can simply clear dev->liveupdate.incoming, and decrement ser->nr_devices.

#23 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

Quoted 46 lines · Pasha Tatashin
Quoted 23 lines · David Matlack
Quoted 8 lines · Pasha Tatashin

On Fri, 10 Jul 2026 21:26:06 +0000, David Matlack <[email protected]> wrote:

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

Quoted 6 lines

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

Quoted 4 lines

We could avoid this by deleteing dev->liveupdate.incoming and, instead, fetching the incoming FLB and doing the xarray lookup every atime the PCI core needs to access the device's incoming ser struct, but that would be inefficient.

Thanks for the explanation. As I understand, the most straightforward way to avoid holding the permanent reference is indeed to delete dev->liveupdate.incoming entirely and perform an xarray lookup on every access, like this:

bool pci_liveupdate_is_incoming(struct pci_dev *dev)

{
        ...
        incoming = pci_liveupdate_flb_get_incoming();
        ...
        dev_ser = xa_load(&incoming->xa, key);
        ...
        pci_liveupdate_flb_put_incoming();
        return dev_ser && dev_ser->refcount > 0;
}
Quoted 7 lines

However, as you note, this is inefficient because it affects every single device and adds lookup overhead to every access (not sure about the actual cost though, xarray access is pretty fast!).

We can, however, still avoid tinkering with the lifecycle of the FLB, and instead treat dev->liveupdate.incoming as a "hint" that we validate on access with a fast, liveness check:

Can you tell me more about your concern about FLB lifetime?

The lifetime of the FLB will not be affected by this reference unless there is a bug in the driver where it fails to call pci_liveupdate_finish() during it's file handler finish callback.

Quoted 25 lines

1. At Setup: In pci_liveupdate_setup_device(), we do the xarray lookup once, cache the pointer in dev->liveupdate.incoming, and immediately call pci_liveupdate_flb_put_incoming(). We do not hold a permanent reference.

2. On Access: When an accessor runs, instead of doing a full xarray lookup, it just validates the cached pointer's liveness by temporarily securing the FLB:

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
        struct pci_flb_incoming *incoming;

        incoming = pci_liveupdate_flb_get_incoming();
        if (!incoming)
                return NULL;

        if (dev->liveupdate.incoming)
                return incoming;

        pci_liveupdate_flb_put_incoming();
        return NULL;
}
Quoted 2 lines

* If get_incoming() returns NULL (the FLB has already finished/freed), the hint is invalid and the device is no longer incoming.

This avoids the xarray lookup but still requires taking the incoming FLB mutex twice (once for get and once for put) on every access. And if there's no incoming PCI FLB, the LUO will iterate over all incoming FLBs under the mutex to find it.

Quoted 9 lines

* If it returns a valid pointer, the FLB is guaranteed to be alive, so we can immediately use the cached dev->liveupdate.incoming pointer without xarray lookups. The accessor then puts the transient reference when done.

3. In Finish/Cleanup: We can simply clear dev->liveupdate.incoming, and decrement ser->nr_devices.

#59 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to Pasha Tatashin
Quoted 24 lines · Pasha Tatashin
Quoted 9 lines · David Matlack
drivers/pci/liveupdate.c +1 -0
index a95bfe5eff77..74a11e520f0d 100644
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
      return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 833

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+{
+     u16 val;
+
+     guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+     if (!dev->liveupdate.incoming)
+             return -EINVAL;
+
+     pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+     dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);

unsigned int ari_enabled:1;

Quoted 2 lines

Sashiko asks a valid question, what protects other bits in this word during modication? At a very list a comment is needed.

This was my reply to Sashiko, not sure if you saw it:

. pci_liveupdate_configure_ari() is called from pci_configure_ari() . which also sets dev->ari_enabled=1 and is pre-existing code. .
. If writing to dev->ari_enabled in this path is indeed unsafe then that . is a pre-existing bug.

I figured that a comment wouldn't be needed for continuing an established precedent (it's ok to write to ari_enabled during this path).

Quoted 3 lines

--
Pasha Tatashin <[email protected]>

maintainer
#24 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 29 lines · David Matlack
Quoted 18 lines · Pasha Tatashin

On 2026-07-20 14:54:51-07:00, David Matlack wrote:

Thanks for the explanation. As I understand, the most straightforward way to avoid holding the permanent reference is indeed to delete dev->liveupdate.incoming entirely and perform an xarray lookup on every access, like this:

bool pci_liveupdate_is_incoming(struct pci_dev *dev)

{
        ...
        incoming = pci_liveupdate_flb_get_incoming();
        ...
        dev_ser = xa_load(&incoming->xa, key);
        ...
        pci_liveupdate_flb_put_incoming();
        return dev_ser && dev_ser->refcount > 0;
}
Quoted 7 lines

However, as you note, this is inefficient because it affects every single device and adds lookup overhead to every access (not sure about the actual cost though, xarray access is pretty fast!).

We can, however, still avoid tinkering with the lifecycle of the FLB, and instead treat dev->liveupdate.incoming as a "hint" that we validate on access with a fast, liveness check:

Quoted 5 lines

Can you tell me more about your concern about FLB lifetime?

The lifetime of the FLB will not be affected by this reference unless there is a bug in the driver where it fails to call pci_liveupdate_finish() during it's file handler finish callback.

From a design perspective, liveupdate_flb_get/put_incoming() is a logical read-lock/unlock pair on the FLB data. We use a refcount for optimization and sharing, but holding a get over a long asynchronous gap (from boot-time device setup to driver probe) is essentially holding an unbound lock.

Unbound locks make it difficult to trace refcount leaks or debug lifecycle issues.

Quoted 25 lines
Quoted 25 lines

1. At Setup: In pci_liveupdate_setup_device(), we do the xarray lookup once, cache the pointer in dev->liveupdate.incoming, and immediately call pci_liveupdate_flb_put_incoming(). We do not hold a permanent reference.

2. On Access: When an accessor runs, instead of doing a full xarray lookup, it just validates the cached pointer's liveness by temporarily securing the FLB:

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
        struct pci_flb_incoming *incoming;

        incoming = pci_liveupdate_flb_get_incoming();
        if (!incoming)
                return NULL;

        if (dev->liveupdate.incoming)
                return incoming;

        pci_liveupdate_flb_put_incoming();
        return NULL;
}
Quoted 7 lines
Quoted 2 lines

* If get_incoming() returns NULL (the FLB has already finished/freed), the hint is invalid and the device is no longer incoming.

This avoids the xarray lookup but still requires taking the incoming FLB mutex twice (once for get and once for put) on every access. And if there's no incoming PCI FLB, the LUO will iterate over all incoming FLBs under the mutex to find it.

Can we do a fast-path check first?

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
	struct pci_flb_incoming *incoming;

	/* Fast-path to avoid unnecessary FLB querying */
	if (!dev->liveupdate.incoming)
		return NULL;

	incoming = pci_liveupdate_flb_get_incoming();
	if (!incoming)
		return NULL;

	/* Check again, now that FLB is acquired */
	if (dev->liveupdate.incoming)
		return incoming;

	pci_liveupdate_flb_put_incoming();
	return NULL;
}

This seems to gives us the best of both worlds: robust refcount hygiene and a sane fast path.

What do you think?

maintainer
#60 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to David Matlack
Quoted 18 lines · David Matlack
Quoted 6 lines · Pasha Tatashin

On Fri, 10 Jul 2026 21:26:12 +0000, David Matlack <[email protected]> wrote:

unsigned int ari_enabled:1;

Sashiko asks a valid question, what protects other bits in this word during modication? At a very list a comment is needed.

This was my reply to Sashiko, not sure if you saw it:

. pci_liveupdate_configure_ari() is called from pci_configure_ari() . which also sets dev->ari_enabled=1 and is pre-existing code. .
. If writing to dev->ari_enabled in this path is indeed unsafe then that . is a pre-existing bug.

Quoted 3 lines

I figured that a comment wouldn't be needed for continuing an established precedent (it's ok to write to ari_enabled during this path).

Overall, I agree that if there is a bug it is pre-existing. But we should also take opportunities to improve existing code and make it safer. In my opinion, Sashiko raises a valid concern that is not obvious, since there is no comment explaining the access of non-atomic bitfields in the header before the struct pci_dev definition.

In this path, it is safe because we run inside pci_device_add(), where dev is brand new and has not yet been added to the public devices list.

However, it would be great to:

1. Review the access patterns to ensure there are no concurrent writers on any other paths.
2. Add a brief comment in this patch right before setting ari:
/* Safe to write without locking; device is not yet publicly visible */

or:

3. Create a new separate patch that adds a comment before the struct pci_dev definition explaining the bitfield access pattern, and how concurrent writes are avoided.

#25 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to Pasha Tatashin
Quoted 36 lines · Pasha Tatashin
Quoted 29 lines · David Matlack
Quoted 18 lines · Pasha Tatashin

On 2026-07-20 14:54:51-07:00, David Matlack wrote:

Thanks for the explanation. As I understand, the most straightforward way to avoid holding the permanent reference is indeed to delete dev->liveupdate.incoming entirely and perform an xarray lookup on every access, like this:

bool pci_liveupdate_is_incoming(struct pci_dev *dev)

{
        ...
        incoming = pci_liveupdate_flb_get_incoming();
        ...
        dev_ser = xa_load(&incoming->xa, key);
        ...
        pci_liveupdate_flb_put_incoming();
        return dev_ser && dev_ser->refcount > 0;
}
Quoted 7 lines

However, as you note, this is inefficient because it affects every single device and adds lookup overhead to every access (not sure about the actual cost though, xarray access is pretty fast!).

We can, however, still avoid tinkering with the lifecycle of the FLB, and instead treat dev->liveupdate.incoming as a "hint" that we validate on access with a fast, liveness check:

Quoted 5 lines

Can you tell me more about your concern about FLB lifetime?

The lifetime of the FLB will not be affected by this reference unless there is a bug in the driver where it fails to call pci_liveupdate_finish() during it's file handler finish callback.

Quoted 8 lines

From a design perspective, liveupdate_flb_get/put_incoming() is a logical read-lock/unlock pair on the FLB data. We use a refcount for optimization and sharing, but holding a get over a long asynchronous gap (from boot-time device setup to driver probe) is essentially holding an unbound lock.

Unbound locks make it difficult to trace refcount leaks or debug lifecycle issues.

It's very standard for refcounts to be held for a long time, e.g. file refcounts and device refcounts. LUO FLBs themselves already have long-lived refcounts taken by each file that depends on themm, which aren't released until that file handler's finish callback runs. The PCI core is just doing the same thing.

Quoted 56 lines
Quoted 26 lines
Quoted 26 lines

1. At Setup: In pci_liveupdate_setup_device(), we do the xarray lookup once, cache the pointer in dev->liveupdate.incoming, and immediately call pci_liveupdate_flb_put_incoming(). We do not hold a permanent reference.

2. On Access: When an accessor runs, instead of doing a full xarray lookup, it just validates the cached pointer's liveness by temporarily securing the FLB:

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
        struct pci_flb_incoming *incoming;

        incoming = pci_liveupdate_flb_get_incoming();
        if (!incoming)
                return NULL;

        if (dev->liveupdate.incoming)
                return incoming;

        pci_liveupdate_flb_put_incoming();
        return NULL;
}
Quoted 7 lines
Quoted 2 lines

* If get_incoming() returns NULL (the FLB has already finished/freed), the hint is invalid and the device is no longer incoming.

This avoids the xarray lookup but still requires taking the incoming FLB mutex twice (once for get and once for put) on every access. And if there's no incoming PCI FLB, the LUO will iterate over all incoming FLBs under the mutex to find it.

Can we do a fast-path check first?

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
	struct pci_flb_incoming *incoming;

	/* Fast-path to avoid unnecessary FLB querying */
	if (!dev->liveupdate.incoming)
		return NULL;

	incoming = pci_liveupdate_flb_get_incoming();
	if (!incoming)
		return NULL;

	/* Check again, now that FLB is acquired */
	if (dev->liveupdate.incoming)
		return incoming;

	pci_liveupdate_flb_put_incoming();
	return NULL;
}
Quoted 4 lines

This seems to gives us the best of both worlds: robust refcount hygiene and a sane fast path.

What do you think?

It still seems like an overall worse approach to me.

* From a performance perspective, the PCI core would still have to acquire the FLB mutex twice every time it needs to use the device's serialized state (once to acquire a new reference and once to release it).

* From a locking perspective, the refcount doesn't actually protect against the device itself going through finish. So we still need the pci_liveupdate.rwsem.

* From a hygiene perspective, each reference is held for less time yes, but there will more places in the code that need to acquire and release a refcount. That is more room for bugs to leak a refcount. So I'm not sure taking more small refcounts is an improvement.

With the current approach there is just one refcount with a very clear lifetime (that aligns with the device file's refcount) and no extra LUO mutexes required to use the device's serialized state during device enumeration & setup.

#61 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to Pasha Tatashin
Quoted 32 lines · Pasha Tatashin
Quoted 18 lines · David Matlack
Quoted 6 lines · Pasha Tatashin

On Fri, 10 Jul 2026 21:26:12 +0000, David Matlack <[email protected]> wrote:

unsigned int ari_enabled:1;

Sashiko asks a valid question, what protects other bits in this word during modication? At a very list a comment is needed.

This was my reply to Sashiko, not sure if you saw it:

. pci_liveupdate_configure_ari() is called from pci_configure_ari() . which also sets dev->ari_enabled=1 and is pre-existing code. .
. If writing to dev->ari_enabled in this path is indeed unsafe then that . is a pre-existing bug.

Quoted 3 lines

I figured that a comment wouldn't be needed for continuing an established precedent (it's ok to write to ari_enabled during this path).

Overall, I agree that if there is a bug it is pre-existing. But we should also take opportunities to improve existing code and make it safer. In my opinion, Sashiko raises a valid concern that is not obvious, since there is no comment explaining the access of non-atomic bitfields in the header before the struct pci_dev definition.

In this path, it is safe because we run inside pci_device_add(), where dev is brand new and has not yet been added to the public devices list.

Quoted 6 lines

However, it would be great to:

1. Review the access patterns to ensure there are no concurrent writers on any other paths.
2. Add a brief comment in this patch right before setting ari:
/* Safe to write without locking; device is not yet publicly visible */

Ack, will do this one in v8.

Quoted 6 lines

or:

3. Create a new separate patch that adds a comment before the struct pci_dev definition explaining the bitfield access pattern, and how concurrent writes are avoided.

maintainer
#26 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 10 lines · David Matlack
Quoted 8 lines · Pasha Tatashin

On 2026-07-20 16:07:47-07:00, David Matlack wrote:

From a design perspective, liveupdate_flb_get/put_incoming() is a logical read-lock/unlock pair on the FLB data. We use a refcount for optimization and sharing, but holding a get over a long asynchronous gap (from boot-time device setup to driver probe) is essentially holding an unbound lock.

Quoted 8 lines
Quoted 2 lines

Unbound locks make it difficult to trace refcount leaks or debug lifecycle issues.

It's very standard for refcounts to be held for a long time, e.g. file refcounts and device refcounts. LUO FLBs themselves already have long-lived refcounts taken by each file that depends on themm, which aren't released until that file handler's finish callback runs. The PCI core is just doing the same thing.

That is right, but that refcount is tracked internally by LUO and follows the design of File-Lifecycle-Bound objects, not the lifecycle of the FLB's users.

It really seems backward to me to force FLB objects to have a different lifecycle. Let's start with a concise lifecycle without setting a precedent where clients of FLB try to control the duration for which objects exist.

Quoted 26 lines
Quoted 26 lines

Can we do a fast-path check first?

static struct pci_flb_incoming *pci_liveupdate_get_incoming(struct pci_dev *dev)

{
	struct pci_flb_incoming *incoming;

	/* Fast-path to avoid unnecessary FLB querying */
	if (!dev->liveupdate.incoming)
		return NULL;

	incoming = pci_liveupdate_flb_get_incoming();
	if (!incoming)
		return NULL;

	/* Check again, now that FLB is acquired */
	if (dev->liveupdate.incoming)
		return incoming;

	pci_liveupdate_flb_put_incoming();
	return NULL;
}

This seems to gives us the best of both worlds: robust refcount hygiene and a sane fast path.

Quoted 8 lines
Quoted 1 line

What do you think?

It still seems like an overall worse approach to me.

* From a performance perspective, the PCI core would still have to acquire the FLB mutex twice every time it needs to use the device's serialized state (once to acquire a new reference and once to release it).

I suspect, acquiring/releasing the FLB mutex in this case is trivial and won't be noticeable in any real-world scenario.

Quoted 9 lines

* From a locking perspective, the refcount doesn't actually protect against the device itself going through finish. So we still need the pci_liveupdate.rwsem.

* From a hygiene perspective, each reference is held for less time yes, but there will more places in the code that need to acquire and release a refcount. That is more room for bugs to leak a refcount. So I'm not sure taking more small refcounts is an improvement.

IMO, short-scoped acquire/release helpers around actual usage are much easier to audit than holding a refcount open across an arbitrary, asynchronous gap waiting for driver probe.

#27 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 27 lines · David Matlack
Quoted 25 lines · Pasha Tatashin

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

Quoted 7 lines
Quoted 2 lines

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

I think LUO gives you guarantee that this pointer remains valid until the FLB finish(), because all the vfiofd that were preserved have not finished. And when all vfiofd have finished, LUO lets you know that the pointer is becoming invalid so you can unset these in the finish() callback from LUO?

If I understand this correctly, I think by keeping long references until finish you are replicating the vfiofd bound lifecycle that LUO already gives you.

Quoted 5 lines

We could avoid this by deleteing dev->liveupdate.incoming and, instead, fetching the incoming FLB and doing the xarray lookup every atime the PCI core needs to access the device's incoming ser struct, but that would be inefficient.

Sami

#28 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

Quoted 43 lines · Samiullah Khawaja
Quoted 27 lines · David Matlack
Quoted 25 lines · Pasha Tatashin

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

Quoted 7 lines
Quoted 2 lines

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

I think LUO gives you guarantee that this pointer remains valid until the FLB finish(), because all the vfiofd that were preserved have not finished. And when all vfiofd have finished, LUO lets you know that the pointer is becoming invalid so you can unset these in the finish() callback from LUO?

Quoted 3 lines

If I understand this correctly, I think by keeping long references until finish you are replicating the vfiofd bound lifecycle that LUO already gives you.

Yes that's right. I am taking the approach of not trusting drivers (i.e. VFIO) and having the PCI core maintain it's own references so that FLB finish() is guaranteed to never be called while a device has device->liveupdate.incoming set.

Alternatively, the PCI core could avoid taking a reference and instead check for driver bugs in FLB finish(). I previously explored that approach and ran into a circular lock dependency between the FLB mutex and pci_liveupdate.rwsem. But I can look into that again, or just do a lockless walk of all PCI devices and panic if any still has dev->liveupdate.incoming set. It will only happen if the driver is completely broken so panicking seems fine.

#29 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

Quoted 54 lines · David Matlack
Quoted 43 lines · Samiullah Khawaja
Quoted 27 lines · David Matlack
Quoted 25 lines · Pasha Tatashin

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

Quoted 7 lines
Quoted 2 lines

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

I think LUO gives you guarantee that this pointer remains valid until the FLB finish(), because all the vfiofd that were preserved have not finished. And when all vfiofd have finished, LUO lets you know that the pointer is becoming invalid so you can unset these in the finish() callback from LUO?

Quoted 3 lines

If I understand this correctly, I think by keeping long references until finish you are replicating the vfiofd bound lifecycle that LUO already gives you.

Yes that's right. I am taking the approach of not trusting drivers (i.e. VFIO) and having the PCI core maintain it's own references so that FLB finish() is guaranteed to never be called while a device has device->liveupdate.incoming set.

Quoted 7 lines

Alternatively, the PCI core could avoid taking a reference and instead check for driver bugs in FLB finish(). I previously explored that approach and ran into a circular lock dependency between the FLB mutex and pci_liveupdate.rwsem. But I can look into that again, or just do a lockless walk of all PCI devices and panic if any still has dev->liveupdate.incoming set. It will only happen if the driver is completely broken so panicking seems fine.

Pasha what do you think of this approach?

It avoids the PCI core taking a long reference (your conern) and also avoids the PCI core needing to take lots of short references to access dev->liveupdate.incoming (my concern). So seems like a good compromise.

maintainer
#30 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

Quoted 63 lines · David Matlack
Quoted 54 lines · David Matlack
Quoted 43 lines · Samiullah Khawaja
Quoted 27 lines · David Matlack
Quoted 25 lines · Pasha Tatashin

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 458

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

Quoted 7 lines
Quoted 2 lines

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

I think LUO gives you guarantee that this pointer remains valid until the FLB finish(), because all the vfiofd that were preserved have not finished. And when all vfiofd have finished, LUO lets you know that the pointer is becoming invalid so you can unset these in the finish() callback from LUO?

Quoted 3 lines

If I understand this correctly, I think by keeping long references until finish you are replicating the vfiofd bound lifecycle that LUO already gives you.

Yes that's right. I am taking the approach of not trusting drivers (i.e. VFIO) and having the PCI core maintain it's own references so that FLB finish() is guaranteed to never be called while a device has device->liveupdate.incoming set.

Quoted 7 lines

Alternatively, the PCI core could avoid taking a reference and instead check for driver bugs in FLB finish(). I previously explored that approach and ran into a circular lock dependency between the FLB mutex and pci_liveupdate.rwsem. But I can look into that again, or just do a lockless walk of all PCI devices and panic if any still has dev->liveupdate.incoming set. It will only happen if the driver is completely broken so panicking seems fine.

Quoted 1 line

Pasha what do you think of this approach?

I like it, let's do lockless sanity checking in flb finish callback, and avoid messing with references.

Quoted 5 lines

It avoids the PCI core taking a long reference (your conern) and also avoids the PCI core needing to take lots of short references to access dev->liveupdate.incoming (my concern). So seems like a good compromise.

#34 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to David Matlack
Quoted 61 lines · David Matlack
Quoted 42 lines · Alex Williamson
Quoted 38 lines · On Fri, 10 Jul 2026 21:26:06 +0000 David Matlack <[email protected]> wrote:
drivers/pci/liveupdate.c +34 -0
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
 
+static struct pci_flb_incoming *pci_liveupdate_flb_get_incoming(void)
+{
+     struct pci_flb_incoming *incoming = NULL;
+     int ret;
+
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&incoming);
+
+     /* Live Update is not enabled. */
+     if (ret == -EOPNOTSUPP)
+             return NULL;
+
+     /* Live Update is enabled, but there is no incoming FLB data. */
+     if (ret == -ENODATA)
+             return NULL;
+
+     /*
+      * Live Update is enabled and there is incoming FLB data, but none of it
+      * matches pci_liveupdate_flb.compatible.
+      *
+      * This could mean that no PCI FLB data was passed by the previous
+      * kernel, but it could also mean the previous kernel used a different
+      * compatibility string (i.e. a different ABI).
+      */
+     if (ret == -ENOENT) {
+             pr_info_once("No incoming FLB matched %s\n", pci_liveupdate_flb.compatible);
+             return NULL;
+     }
+
+     /*
+      * There is incoming FLB data that matches pci_liveupdate_flb.compatible
+      * but it cannot be retrieved.
+      */
+     if (ret)
+             panic("Failed to retrieve incoming FLB data (%d)\n", ret);
Quoted 8 lines

drivers/pci/liveupdate.c void pci_liveupdate_unpreserve(struct pci_dev *dev) · line 416

Quoted 3 lines
drivers/pci/liveupdate.c +3 -0
+
+     return incoming;
+}

I'm having trouble following the error escalation here. What's fundamentally the difference between FLB data being provided and not compatible (subtle log message) versus FLB data being provided and compatible but we cannot access it (panic!)?

There is no fundamental difference IMO and I think we should handle them the same way (panic). The problem is the PCI core cannot actually distinguish between "PCI FLB provided and not compatible" (should panic) and "no PCI FLB provided" (should log). I would like to propose changes to LUO so that the FLB handlers can detect the former situation but that requires some non-trivial work that I didn't want to block on.

Quoted 3 lines

For now, I guess you could say it is up to the user to not perform Live Update between incompatible versions. I can update the documentation.

An alternative approach would be to make the PCI compatibility string just "pci" and then put the version number as the first field in struct pci_ser. That would ensure the PCI core can always fetch its FLB data and panic on a version mismatch. I think first-class handling in LUO would be better, but that is a larger problem to solve; it also needs to be solved for file handlers.

Pasha and Sami, what do you think?

Quoted 18 lines
Quoted 11 lines

Don't both suggest devices are running but we can't get their FLB data to continue letting them run?

The errno interpretation is also slightly different than the comment above liveupdate_flb_get_incoming():

* Return: 0 on success, or a negative errno on failure. -ENODATA means no
* incoming FLB data, -ENOENT means specific flb not found in the incoming
* data, -ENODEV if the FLB's module is unloading, and -EOPNOTSUPP when
* live update is disabled or not configured.

I think the only disagreement is the lack of specific -ENODEV handling. Is that what you are referring to?

Quoted 3 lines

Thanks,
Alex

#31 lore raw

Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

In reply to Pasha Tatashin
Quoted 65 lines · Pasha Tatashin
Quoted 63 lines · David Matlack
Quoted 61 lines · David Matlack
Quoted 43 lines · Samiullah Khawaja
Quoted 27 lines · David Matlack
Quoted 25 lines · Pasha Tatashin

drivers/pci/liveupdate.c

Quoted 12 lines · David Matlack
drivers/pci/liveupdate.c +0 -0
index 03075ce06ac9..df6a02240aa4 100644
@@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)

[ ... skip 76 lines ... ]

drivers/pci/liveupdate.c +6 -0
+     /*
+      * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
+      * that dev->liveupdate.incoming cannot get freed while the PCI core
+      * has a pointer to it. It's better to leak the incoming FLB than do a
+      * use-after-free if driver does not call pci_liveupdate_finish().
+      */

I am confused by this. Each preserved PCI device must have an associated FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If the vfiofd was not reclaimed, and finish is not possible, that vfiofd would still be owned by LUO, and therefore PCI FLB refcount would stay positive.

However, if finish is possible, and this is the last vfiofd that is finished, FLB will be freed as soon as the reference count reaches zero, which I would think is the expected behavior.

Quoted 7 lines
Quoted 2 lines

What is the point of holding a reference here, instead of only for the duration of FLB access, i.e. to make sure we are accessing a valid data?

The duration of the access is from here until pci_liveupdate_finish() because that it when the pointer (dev->liveupdate.incoming) is cleared. So that is why the PCI core holds the reference from here until pci_liveupdate_finish().

I think LUO gives you guarantee that this pointer remains valid until the FLB finish(), because all the vfiofd that were preserved have not finished. And when all vfiofd have finished, LUO lets you know that the pointer is becoming invalid so you can unset these in the finish() callback from LUO?

Quoted 3 lines

If I understand this correctly, I think by keeping long references until finish you are replicating the vfiofd bound lifecycle that LUO already gives you.

Yes that's right. I am taking the approach of not trusting drivers (i.e. VFIO) and having the PCI core maintain it's own references so that FLB finish() is guaranteed to never be called while a device has device->liveupdate.incoming set.

Alternatively, the PCI core could avoid taking a reference and instead check for driver bugs in FLB finish(). I previously explored that approach and ran into a circular lock dependency between the FLB mutex and pci_liveupdate.rwsem. But I can look into that again, or just do a lockless walk of all PCI devices and panic if any still has dev->liveupdate.incoming set. It will only happen if the driver is completely broken so panicking seems fine.

Quoted 4 lines
Quoted 1 line

Pasha what do you think of this approach?

I like it, let's do lockless sanity checking in flb finish callback, and avoid messing with references.

Great! I'll take this approach in v8.

Quoted 5 lines
Quoted 5 lines

It avoids the PCI core taking a long reference (your conern) and also avoids the PCI core needing to take lots of short references to access dev->liveupdate.incoming (my concern). So seems like a good compromise.

#7 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to David Matlack
Quoted 8 lines · David Matlack

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.
...

+#define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt

Is KBUILD_BASENAME buying us something important here? Just from code-reading perspective, it's more work because without some digging I don't know what it will end up looking like in a dmesg log.

drivers/pci/liveupdate.c line 101

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_flb_outgoing *outgoing = args->obj;
+
+	WARN_ON(outgoing->ser->nr_devices);
+	kho_block_set_destroy(&outgoing->block_set);
+	kho_unpreserve_free(outgoing->ser);
+	kfree(outgoing);
+	pr_debug("Unpreserved struct pci_ser\n");

If this pr_debug() is useful, maybe it should include an address and there should be a corresponding pr_debug() on the preserve side so we could find things in a crashdump?

drivers/pci/liveupdate.c line 141

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
+{
+	pr_debug("Registering file handler \"%s\"\n", fh->compatible);

Is fh->compatible enough to identify a driver?

Reviewed-by on v7 2/12
#15 lore raw

Re: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices

In reply to David Matlack
Quoted 10 lines · David Matlack

Add APIs to allow drivers to notify the PCI core of which devices are being preserved across a Live Update for the next kernel, i.e. "outgoing" devices.

Drivers must notify the PCI core when devices are preserved so that the PCI core can update its FLB data (struct pci_ser) and track the list of outgoing devices. pci_liveupdate_preserve() notifies the PCI core that a device must be preserved across Live Update. pci_liveupdate_unpreserve() reverses this (cancels the preservation of the device).

Quoted 3 lines

This tracking ensures the PCI core is fully aware of which devices may need special handling during shutdown and kexec, and so that it can be handed off to the next kernel.

I guess the antecedent of "it" is a device? Maybe s/it/they/?

Quoted 1 line

+static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)

Wrap parameter list to fit in 80 columns.

#40 lore raw

Re: [PATCH v7 05/12] PCI: liveupdate: Keep bus numbers constant during Live Update

In reply to David Matlack
Quoted 5 lines · David Matlack

During a Live Update, preserved devices must be allowed to continue performing memory transactions so the kernel cannot change the fabric topology, including bus numbers, since that would require disabling and flushing any memory transactions first.

Quoted 7 lines

To keep bus numbers constant, always inherit the secondary and subordinate bus numbers assigned to bridges during scanning, instead of assigning new ones, if any PCI devices are being preserved. Note that the kernel inherits bus numbers even on bridges without any downstream endpoints that were preserved. This avoids accidentally assigning a bridge a new window that overlaps with a preserved device that is downstream of a different bridge.

I know there are many references already, but I think "preserve" conveys more of the meaning here than "inherit".

Quoted 1 line

+ * pci_liveupdate_scan_bridge_begin() - Determine if a bridge should inherit bus numbers

Wrap to fit in 80 columns.

drivers/pci/liveupdate.c static void pci_liveupdate_flb_put_incoming(void) · line 468

Quoted 13 lines

+ * @bus: The parent bus of the bridge.
+ * @dev: The PCI bridge device.
+ * @pass: The scan pass (0 for first pass, 1 for second pass).
+ *
+ * This function is called by the PCI core when it begins scanning a bridge.
+ * It determines whether the bridge should inherit the secondary and subordinate
+ * bus numbers assigned to it by the previous kernel. This is necessary to
+ * keep bus numbers constant for preserved devices downstream of the bridge.
+ *
+ * Return: True if bus numbers should be inherited, false otherwise.
+ */

drivers/pci/liveupdate.c +2 -0
+bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
+				      int pass)

I see that you named it this way to have a begin/end pair, but I think this function name should be a predicate, i.e., it should ask a question with a yes/no or true/false answer. Something like "preserve_bus_numbers".

Quoted 8 lines

+ * pci_liveupdate_scan_bridge_end() - Finish scanning a PCI bridge
+ * @dev: The PCI bridge device.
+ * @pass: The scan pass (0 for first pass, 1 for second pass).
+ *
+ * This function is called by the PCI core when it finishes scanning a bridge.
+ * It clears the inheritance status after the second pass so it can be
+ * re-evaluated on future scans.
+ */

drivers/pci/liveupdate.c static void pci_liveupdate_flb_put_incoming(void) · line 537

Quoted 7 lines
drivers/pci/liveupdate.c +7 -0
+void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass)
+{
+	/*
+	 * Clear inherit_buses after the second pass so it can be re-evaluated
+	 * on future scans.
+	 */
+	if (pass)

I think the decision based on "pass" belongs in the caller because it's really an implementation detail of the pci_scan_bridge*() code, and I think it's important that readers of that code understand that we no longer need to preserve bus numbers without having to follow the chain into liveupdate.

I guess the point here is that we've always known to preserve bus numbers of existing devices, so after we've enumerated all the liveupdate-preserved devices, it's safe to assign new bus numbers for hot-adds, bus rescans, etc. Right?

drivers/pci/probe.c static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, · line 1400

Quoted 5 lines
drivers/pci/probe.c +1 -0
@@ -1397,6 +1397,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 				  int max, unsigned int available_buses,
 				  int pass)
 {
+	bool liveupdate, assign_new_buses = pcibios_assign_all_busses();

I have a pretty strong preference for non-negated booleans, but I also think "assign_buses" is an unfortunate historical choice. By default Linux owns all resources, including bus numbers, so the exception case is that we need to *preserve* things that were previously configured. Especially in liveupdate, "assign buses" is the wrong sense -- the requirement is that we preserve them.

So I think here a name like "preserve_buses" will express the intent better even though it will lead to negation here and in tests of "!preserve_buses" below.

Quoted 76 lines
drivers/pci/probe.c +12 -4
 	struct pci_bus *child;
 	u32 buses;
 	u16 bctl;
@@ -1406,6 +1407,10 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	u8 fixed_sec, fixed_sub;
 	int next_busnr;
 
+	liveupdate = pci_liveupdate_scan_bridge_begin(bus, dev, pass);
+	if (liveupdate)
+		assign_new_buses = false;
+
 	/*
 	 * Make sure the bridge is powered on to be able to access config
 	 * space of devices below it.
@@ -1449,8 +1454,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		goto out;
 	}
 
-	if ((secondary || subordinate) &&
-	    !pcibios_assign_all_busses() && !broken) {
+	if ((secondary || subordinate) && !assign_new_buses && !broken) {
 		unsigned int cmax, buses;
 
 		/*
@@ -1492,8 +1496,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		 * do in the second pass.
 		 */
 		if (!pass) {
-			if (pcibios_assign_all_busses() || broken)
-
+			if (assign_new_buses || broken)
 				/*
 				 * Temporarily disable forwarding of the
 				 * configuration cycles on all bridges in
@@ -1507,6 +1510,11 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 			goto out;
 		}
 
+		if (liveupdate) {
+			pci_err(dev, "Cannot reconfigure bridge during Live Update, skipping\n");
+			goto out;
+		}
+
 		/* Clear errors */
 		pci_write_config_word(dev, PCI_STATUS, 0xffff);
 
@@ -1567,6 +1575,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
 
 	pm_runtime_put(&dev->dev);
+	pci_liveupdate_scan_bridge_end(dev, pass);
 
 	return max;
 }
include/linux/pci_liveupdate.h +4 -0
index cfdc3d62ec02..2be98819e313 100644
@@ -17,10 +17,14 @@
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
  * @incoming: State preserved by the previous kernel.
+ * @inherit_buses: True if the PCI core should inherit the secondary and
+ *                 subordinate bus numbers assigned to this device due to
+ *                 an ongoing Live Update.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
 	struct pci_dev_ser *incoming;
+	bool inherit_buses;
 };
 
 struct pci_dev;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#16 lore raw

Re: [PATCH v7 02/12] PCI: liveupdate: Track outgoing preserved PCI devices

In reply to David Matlack

include/linux/pci_liveupdate.h line 14

Quoted 11 lines · David Matlack

Add APIs to allow drivers to notify the PCI core of which devices are being preserved across a Live Update for the next kernel, i.e. "outgoing" devices.
...

+++ b/include/linux/pci_liveupdate.h

include/linux/pci_liveupdate.h +2 -0
+#include <linux/kho/abi/pci.h>
 #include <linux/liveupdate.h>
 #include <linux/types.h>
+#include <linux/spinlock_types.h>

Alpha order unless there's a special rule about spinlock_types.h.

#44 lore raw

Re: [PATCH v7 06/12] PCI: liveupdate: Auto-preserve upstream bridges across Live Update

In reply to David Matlack
Quoted 66 lines · David Matlack

When a PCI device is preserved across a Live Update, all of its upstream bridges up to the root port must also be preserved. This enables the PCI core and any drivers bound to the bridges to manage bridges correctly across a Live Update.

Notably, this will be used in subsequent commits to ensure that preserved devices can continue performing memory transactions without a disruption or change in routing.

To preserve bridges, the PCI core tracks the number of downstream devices preserved under each bridge using a reference count in struct pci_dev_ser. This allows a bridge to remain preserved until all its downstream preserved devices are unpreserved or finish their participation in the Live Update.

---
 drivers/pci/liveupdate.c    | 136 +++++++++++++++++++++++++++++++-----
 include/linux/kho/abi/pci.h |   5 +-
 2 files changed, 122 insertions(+), 19 deletions(-)
drivers/pci/liveupdate.c +21 -2
index b2b950d71657..7f7710cb1da0 100644
@@ -101,6 +101,18 @@
  * If a misconfigured or unconfigured bridge is encountered during enumeration
  * while there are preserved devices, its secondary and subordinate bus numbers
  * will be cleared and devices below it will not be enumerated.
+ *
+ * PCI-to-PCI Bridges
+ * ==================
+ *
+ * Any PCI-to-PCI bridges upstream of a preserved device are automatically
+ * preserved when the device is preserved. The PCI core keeps track of the
+ * number of downstream devices that are preserved under a bridge so that the
+ * bridge is only unpreserved once all downstream devices are unpreserved.
+ *
+ * This enables the PCI core and any drivers bound to the bridge to participate
+ * in the Live Update so that preserved endpoints can continue issuing memory
+ * transactions during the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -316,28 +328,52 @@ static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outg
 	return dev_ser;
 }
 
-static void pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+static int pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
 {
 	struct pci_dev_ser *dev_ser = dev->liveupdate.outgoing;
 
 	if (!dev_ser) {
 		pci_warn(dev, "Cannot unpreserve device that is not preserved\n");
-		return;
+		return -EINVAL;
+	}
+
+	if (!dev_ser->refcount) {
+		pci_WARN(dev, 1, "Preserved device has a 0 refcount!\n");
+		return -EINVAL;
 	}
 
+	if (--dev_ser->refcount)
+		return 0;

drivers/pci/liveupdate.c static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outg · line 355

Quoted 10 lines
drivers/pci/liveupdate.c +3 -1
+
 	pci_info(dev, "Device will no longer be preserved across next Live Update\n");
 	outgoing->ser->nr_devices--;
 	memset(dev_ser, 0, sizeof(*dev_ser));
 	dev->liveupdate.outgoing = NULL;
+	return 0;
 }
 
-static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+static int pci_liveupdate_preserve_device_again(struct pci_dev *dev)

Could pci_liveupdate_preserve_device_again() be folded into pci_liveupdate_preserve_device()? That's the only caller, and having so many *liveupdate_preserve*() functions gets to be a bit of a maze of small functions:

__pci_liveupdate_preserve_device
pci_liveupdate_preserve_device_again
pci_liveupdate_preserve_device
pci_liveupdate_preserve_path
pci_liveupdate_preserve

Maybe even more could be squashed; pci_liveupdate_preserve() is the only caller of pci_liveupdate_preserve_path(), which is itself the only caller of pci_liveupdate_preserve_device().

As a reader, I think there's some advantage in collecting the whole "preserve" flow in one place, but maybe nesting depth would be an issue, especially for the unpreserve cleanup in the failure path. Maybe a "goto" error path could mitigate part of that.

If there's any squashing, I guess the place to start would be in earlier patches, e.g., "PCI: liveupdate: Track outgoing preserved PCI devices" and "PCI: liveupdate: Track incoming preserved PCI devices".

Quoted 30 lines
drivers/pci/liveupdate.c +17 -2
 {
-	struct pci_dev_ser *dev_ser;
+	if (!dev->liveupdate.outgoing->refcount) {
+		pci_WARN(dev, 1, "Preserved device with 0 refcount!\n");
+		return -EINVAL;
+	}
 
-	if (dev->liveupdate.outgoing)
+	/*
+	 * Endpoint devices should not be preserved more than once. Bridges are
+	 * preserved once for every downstream device that is preserved.
+	 */
+	if (!dev->subordinate)
 		return -EBUSY;
 
+	dev->liveupdate.outgoing->refcount++;
+	return 0;
+}
+
+static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+
 	dev_ser = pci_get_empty_or_append(outgoing);
 	if (IS_ERR(dev_ser))
 		return PTR_ERR(dev_ser);
@@ -354,6 +390,52 @@ static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str
 	return 0;
 }
 

drivers/pci/liveupdate.c static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str · line 402

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+{
+	if (dev->liveupdate.outgoing)
+		return pci_liveupdate_preserve_device_again(dev);
+
+	return __pci_liveupdate_preserve_device(outgoing, dev);
+}
+
+#define for_each_pci_dev_in_path(_d, _start, _end) \
+	for ((_d) = (_start); (_d) != (_end); (_d) = (_d)->bus->self)

I think you might want pci_upstream_bridge() instead of (_d)->bus->self; see https://git.kernel.org/linus/c6bde215acfd ("PCI: Add pci_upstream_bridge()"). I think VFs are currently out of scope, but no harm in making this future-proof.

Other than "_end", this looks fairly generic and might be a candidate for putting in include/linux/pci.h. I think there might be other potential users, e.g., calc_map_type_and_dist(), pci_enable_atomic_ops_to_root(), pcie_bandwidth_available(), get_upstream_port(), pcie_find_root_port(), pci_is_thunderbolt_attached().

The "_end" termination case could perhaps be done in the body instead of the macro since it depends on the use case and several of the other loops that iterate over the path already include terminations in the body.

Quoted 171 lines
drivers/pci/liveupdate.c +67 -12
+static void __pci_liveupdate_unpreserve_path(struct pci_flb_outgoing *outgoing,
+					     struct pci_dev *start,
+					     struct pci_dev *end)
+{
+	struct pci_dev *dev;
+
+	for_each_pci_dev_in_path(dev, start, end) {
+		if (pci_liveupdate_unpreserve_device(outgoing, dev))
+			return;
+	}
+}
+
+static void pci_liveupdate_unpreserve_path(struct pci_flb_outgoing *outgoing,
+					   struct pci_dev *start)
+{
+	__pci_liveupdate_unpreserve_path(outgoing, start, /*end=*/NULL);
+}
+
+static int pci_liveupdate_preserve_path(struct pci_flb_outgoing *outgoing,
+					struct pci_dev *start)
+{
+	struct pci_dev *dev;
+	int ret;
+
+	for_each_pci_dev_in_path(dev, start, NULL) {
+		ret = pci_liveupdate_preserve_device(outgoing, dev);
+		if (ret) {
+			__pci_liveupdate_unpreserve_path(outgoing, start, dev);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
 /**
  * pci_liveupdate_preserve() - Preserve a PCI device across Live Update
  * @dev: The PCI device to preserve.
@@ -363,6 +445,9 @@ static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str
  * pci_liveupdate_preserve() from their struct liveupdate_file_handler
  * preserve() callback to ensure the outgoing struct pci_ser is already set up.
  *
+ * pci_liveupdate_preserve() automatically preserves all bridges upstream of
+ * @dev.
+ *
  * Returns: 0 on success, <0 on failure.
  */
 int pci_liveupdate_preserve(struct pci_dev *dev)
@@ -378,7 +463,7 @@ int pci_liveupdate_preserve(struct pci_dev *dev)
 	if (IS_ERR(outgoing))
 		return PTR_ERR(outgoing);
 
-	return pci_liveupdate_preserve_device(outgoing, dev);
+	return pci_liveupdate_preserve_path(outgoing, dev);
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
 
@@ -391,6 +476,9 @@ EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
  * pci_liveupdate_unpreserve() from their struct liveupdate_file_handler
  * unpreserve() callback to ensure the outgoing struct pci_ser is already set
  * up.
+ *
+ * pci_liveupdate_unpreserve() automatically unpreserves all bridges upstream of
+ * @dev.
  */
 void pci_liveupdate_unpreserve(struct pci_dev *dev)
 {
@@ -404,7 +492,7 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
 		return;
 	}
 
-	pci_liveupdate_unpreserve_device(outgoing, dev);
+	pci_liveupdate_unpreserve_path(outgoing, dev);
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
 
@@ -594,29 +682,41 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev)
 	}
 }
 
-static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
+static int pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *dev)
 {
 	if (!dev->liveupdate.incoming) {
 		pci_warn(dev, "Cannot finish preserving an unpreserved device\n");
-		return;
+		return -EINVAL;
 	}
 
-	if (dev->liveupdate.incoming->refcount != 1) {
-		pci_WARN(dev, 1, "Preserved device has a corrupted refcount!\n");
-		return;
+	if (!dev->liveupdate.incoming->refcount) {
+		pci_WARN(dev, 1, "Preserved device has a 0 refcount!\n");
+		return -EINVAL;
 	}
 
 	/*
-	 * Drop the refcount so this device does not get treated as an incoming
-	 * device again, e.g. in case pci_liveupdate_setup_device() gets called
-	 * again because the device is hot-plugged.
+	 * Decrement the refcount so this device does not get treated as an
+	 * incoming device again, e.g. in case pci_liveupdate_setup_device()
+	 * gets called again because the device is hot-plugged.
 	 */
-	dev->liveupdate.incoming->refcount = 0;
+	if (--dev->liveupdate.incoming->refcount)
+		return 0;
 
 	pci_info(dev, "Device is finished participating in Live Update\n");
 	dev->liveupdate.incoming = NULL;
 	ser->nr_devices--;
 	pci_liveupdate_flb_put_incoming();
+	return 0;
+}
+
+static void pci_liveupdate_finish_path(struct pci_ser *ser, struct pci_dev *start)
+{
+	struct pci_dev *dev;
+
+	for_each_pci_dev_in_path(dev, start, NULL) {
+		if (pci_liveupdate_finish_device(ser, dev))
+			return;
+	}
 }
 
 /**
@@ -628,6 +728,8 @@ static void pci_liveupdate_finish_device(struct pci_ser *ser, struct pci_dev *de
  * Update. Drivers must call pci_liveupdate_finish() from their struct
  * liveupdate_file_handler finish() callback to ensure the incoming struct
  * pci_ser is allocated.
+ *
+ * pci_liveupdate_finish() automatically finishes all bridges upstream of @dev.
  */
 void pci_liveupdate_finish(struct pci_dev *dev)
 {
@@ -641,7 +743,7 @@ void pci_liveupdate_finish(struct pci_dev *dev)
 		return;
 	}
 
-	pci_liveupdate_finish_device(incoming->ser, dev);
+	pci_liveupdate_finish_path(incoming->ser, dev);
 	pci_liveupdate_flb_put_incoming();
 }
 EXPORT_SYMBOL_GPL(pci_liveupdate_finish);
include/linux/kho/abi/pci.h +3 -2
index de549016807a..acf1b38dff02 100644
@@ -23,7 +23,7 @@
  * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
  */
 
-#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+#define PCI_LUO_FLB_COMPATIBLE "pci-v2"
 
 /**
  * struct pci_dev_ser - Serialized state about a single PCI device.
@@ -32,7 +32,8 @@
  * @bdf: The device's PCI bus, device, and function number.
  * @refcount: Reference count used by the PCI core to keep track of whether it
  *            is done using a device's struct pci_dev_ser. The value of the
- *            refcount is equal to 1 when the struct pci_dev_ser is in use, and
+ *            refcount is equal to the number of preserved devices at or below
+ *            it in the PCI hierarchy when the struct pci_dev_ser is in use, and
  *            0 otherwise.
  */
 struct pci_dev_ser {
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Reviewed-by on v7 7/12
#49 lore raw

Re: [PATCH v7 07/12] PCI: Refactor matching logic for pci_dev_acs_ops

In reply to David Matlack
Quoted 2 lines · David Matlack

Refactor the logic to match devices to pci_dev_acs_ops by factoring out the loop and device matching into its own routine. This eliminates some

Can you mention the name of the new routine here? I guess it's pci_dev_acs_ops_get()?

Quoted 9 lines

duplicate code between pci_dev_specific_enable_acs() and pci_dev_specific_disable_acs_redir(), and will also be used in a subsequent commit to check if a device requires device-specific enable_acs() during a Live Update.

No functional change intended.

Quoted 119 lines
---
 drivers/pci/quirks.c | 51 ++++++++++++++++++--------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)
drivers/pci/quirks.c +21 -30
index b09f27f7846f..7ac39ec2843e 100644
@@ -5374,9 +5374,6 @@ static void pci_quirk_enable_intel_rp_mpc_acs(struct pci_dev *dev)
  */
 static int pci_quirk_enable_intel_pch_acs(struct pci_dev *dev)
 {
-	if (!pci_quirk_intel_pch_acs_match(dev))
-		return -ENOTTY;
-
 	if (pci_quirk_enable_intel_lpc_acs(dev)) {
 		pci_warn(dev, "Failed to enable Intel PCH ACS quirk\n");
 		return 0;
@@ -5396,9 +5393,6 @@ static int pci_quirk_enable_intel_spt_pch_acs(struct pci_dev *dev)
 	int pos;
 	u32 cap, ctrl;
 
-	if (!pci_quirk_intel_spt_pch_acs_match(dev))
-		return -ENOTTY;
-
 	pos = dev->acs_cap;
 	if (!pos)
 		return -ENOTTY;
@@ -5426,9 +5420,6 @@ static int pci_quirk_disable_intel_spt_pch_acs_redir(struct pci_dev *dev)
 	int pos;
 	u32 cap, ctrl;
 
-	if (!pci_quirk_intel_spt_pch_acs_match(dev))
-		return -ENOTTY;
-
 	pos = dev->acs_cap;
 	if (!pos)
 		return -ENOTTY;
@@ -5448,56 +5439,56 @@ static int pci_quirk_disable_intel_spt_pch_acs_redir(struct pci_dev *dev)
 static const struct pci_dev_acs_ops {
 	u16 vendor;
 	u16 device;
+	bool (*match)(struct pci_dev *dev);
 	int (*enable_acs)(struct pci_dev *dev);
 	int (*disable_acs_redir)(struct pci_dev *dev);
 } pci_dev_acs_ops[] = {
 	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
+	    .match = pci_quirk_intel_pch_acs_match,
 	    .enable_acs = pci_quirk_enable_intel_pch_acs,
 	},
 	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
+	    .match = pci_quirk_intel_spt_pch_acs_match,
 	    .enable_acs = pci_quirk_enable_intel_spt_pch_acs,
 	    .disable_acs_redir = pci_quirk_disable_intel_spt_pch_acs_redir,
 	},
 };
 
-int pci_dev_specific_enable_acs(struct pci_dev *dev)
+static const struct pci_dev_acs_ops *pci_dev_acs_ops_get(struct pci_dev *dev)
 {
 	const struct pci_dev_acs_ops *p;
-	int i, ret;
+	int i;
 
 	for (i = 0; i < ARRAY_SIZE(pci_dev_acs_ops); i++) {
 		p = &pci_dev_acs_ops[i];
 		if ((p->vendor == dev->vendor ||
 		     p->vendor == (u16)PCI_ANY_ID) &&
 		    (p->device == dev->device ||
-		     p->device == (u16)PCI_ANY_ID) &&
-		    p->enable_acs) {
-			ret = p->enable_acs(dev);
-			if (ret >= 0)
-				return ret;
+		     p->device == (u16)PCI_ANY_ID)) {
+			if (!p->match || p->match(dev))
+				return p;
 		}
 	}
 
+	return NULL;
+}
+
+int pci_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
+
+	if (p && p->enable_acs)
+		return p->enable_acs(dev);
+
 	return -ENOTTY;
 }
 
 int pci_dev_specific_disable_acs_redir(struct pci_dev *dev)
 {
-	const struct pci_dev_acs_ops *p;
-	int i, ret;
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
 
-	for (i = 0; i < ARRAY_SIZE(pci_dev_acs_ops); i++) {
-		p = &pci_dev_acs_ops[i];
-		if ((p->vendor == dev->vendor ||
-		     p->vendor == (u16)PCI_ANY_ID) &&
-		    (p->device == dev->device ||
-		     p->device == (u16)PCI_ANY_ID) &&
-		    p->disable_acs_redir) {
-			ret = p->disable_acs_redir(dev);
-			if (ret >= 0)
-				return ret;
-		}
-	}
+	if (p && p->disable_acs_redir)
+		return p->disable_acs_redir(dev);
 
 	return -ENOTTY;
 }
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#45 lore raw

Re: [PATCH v7 06/12] PCI: liveupdate: Auto-preserve upstream bridges across Live Update

In reply to Bjorn Helgaas
Quoted 21 lines · Bjorn Helgaas

drivers/pci/liveupdate.c static struct pci_dev_ser *pci_get_empty_or_append(struct pci_flb_outgoing *outg · line 355

Quoted 2 lines · David Matlack
drivers/pci/liveupdate.c +1 -1
-static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, struct pci_dev *dev)
+static int pci_liveupdate_preserve_device_again(struct pci_dev *dev)

Could pci_liveupdate_preserve_device_again() be folded into pci_liveupdate_preserve_device()? That's the only caller, and having so many *liveupdate_preserve*() functions gets to be a bit of a maze of small functions:

__pci_liveupdate_preserve_device
pci_liveupdate_preserve_device_again
pci_liveupdate_preserve_device
pci_liveupdate_preserve_path
pci_liveupdate_preserve

Maybe even more could be squashed; pci_liveupdate_preserve() is the only caller of pci_liveupdate_preserve_path(), which is itself the only caller of pci_liveupdate_preserve_device().

Quoted 8 lines

As a reader, I think there's some advantage in collecting the whole "preserve" flow in one place, but maybe nesting depth would be an issue, especially for the unpreserve cleanup in the failure path. Maybe a "goto" error path could mitigate part of that.

If there's any squashing, I guess the place to start would be in earlier patches, e.g., "PCI: liveupdate: Track outgoing preserved PCI devices" and "PCI: liveupdate: Track incoming preserved PCI devices".

I'll see what I can do in v8 to avoid splitting the preserve/unpreserve logic across so many functions. I suspect it can go from 5 to 2-3 while still having a reasonable amount of nesting.

Quoted 7 lines

drivers/pci/liveupdate.c static int pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, str · line 402

Quoted 2 lines
drivers/pci/liveupdate.c +2 -0
+#define for_each_pci_dev_in_path(_d, _start, _end) \
+     for ((_d) = (_start); (_d) != (_end); (_d) = (_d)->bus->self)

I think you might want pci_upstream_bridge() instead of (_d)->bus->self; see https://git.kernel.org/linus/c6bde215acfd ("PCI: Add pci_upstream_bridge()"). I think VFs are currently out of scope, but no harm in making this future-proof.

Yes in fact I noticed pci_upstream_bridge() while working on other changes in v8 and already swapped to it :). It also makes the code more readable.

Quoted 11 lines

Other than "_end", this looks fairly generic and might be a candidate for putting in include/linux/pci.h. I think there might be other potential users, e.g., calc_map_type_and_dist(), pci_enable_atomic_ops_to_root(), pcie_bandwidth_available(), get_upstream_port(), pcie_find_root_port(), pci_is_thunderbolt_attached().

The "_end" termination case could perhaps be done in the body instead of the macro since it depends on the use case and several of the other loops that iterate over the path already include terminations in the body.

Sounds good. I'll move it to include/linux/pci.h and and handle _end in the loop bodies. Then once this series lands I can send a follow up to convert those users to use the new iterator.

#8 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to Bjorn Helgaas
Quoted 29 lines · Bjorn Helgaas
Quoted 8 lines · David Matlack

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.
...

+#define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt

Is KBUILD_BASENAME buying us something important here? Just from code-reading perspective, it's more work because without some digging I don't know what it will end up looking like in a dmesg log.

drivers/pci/liveupdate.c line 101

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+     struct pci_flb_outgoing *outgoing = args->obj;
+
+     WARN_ON(outgoing->ser->nr_devices);
+     kho_block_set_destroy(&outgoing->block_set);
+     kho_unpreserve_free(outgoing->ser);
+     kfree(outgoing);
+     pr_debug("Unpreserved struct pci_ser\n");

If this pr_debug() is useful, maybe it should include an address and there should be a corresponding pr_debug() on the preserve side so we could find things in a crashdump?

Quoted 5 lines

drivers/pci/liveupdate.c line 141

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
+{
+     pr_debug("Registering file handler \"%s\"\n", fh->compatible);

Is fh->compatible enough to identify a driver?

Not necessarily. VFIO could theoretically have one file handler that handles all the vfio-pci variant drivers. But I suspect we'll end up with 1 fh per driver in practice.

Are you thinking about how we can ensure devices are bound to compatible drivers after Live Update?

BTW, thank you for feedback on the rest of the series. I am addressing all your comments in v8.

#73 lore raw
In reply to David Matlack
Quoted 46 lines · David Matlack

This series can be found on GitHub:

https://github.com/dmatlack/linux/tree/liveupdate/pci/base/v7

This series introduces initial support in the PCI core for Live Update, enabling drivers to preserve PCI devices across a kexec-based kernel update without interrupting the device. This functionality is critical for minimizing downtime in environments where PCI devices (e.g., those assigned to VMs via VFIO) must continue operating or maintain state across a host kernel upgrade.

Specifically, this series allows preserved PCI devices to perform uninterrupted memory transactions (DMA) to/from system memory across a Live Update. These devices can be behind a bridge but must not be VFs. Support for P2P and VF preservation will be addressed in future series.

David Matlack (12):
PCI: liveupdate: Set up FLB handler for the PCI core
PCI: liveupdate: Track outgoing preserved PCI devices
PCI: liveupdate: Track incoming preserved PCI devices
PCI: liveupdate: Document driver binding responsibilities
PCI: liveupdate: Keep bus numbers constant during Live Update
PCI: liveupdate: Auto-preserve upstream bridges across Live Update
PCI: Refactor matching logic for pci_dev_acs_ops
PCI: liveupdate: Inherit ACS flags in incoming preserved devices
PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges
PCI: liveupdate: Freeze preservation status during shutdown
PCI: liveupdate: Do not disable bus mastering on preserved devices
during kexec
Documentation: PCI: Add documentation for Live Update

 Documentation/PCI/index.rst                   |   1 +
 Documentation/PCI/liveupdate.rst              |  29 +
 .../admin-guide/kernel-parameters.txt         |   6 +-
 Documentation/core-api/liveupdate.rst         |   5 +
 MAINTAINERS                                   |  15 +
 drivers/pci/Kconfig                           |  15 +
 drivers/pci/Makefile                          |   1 +
 drivers/pci/liveupdate.c                      | 910 ++++++++++++++++++
 drivers/pci/liveupdate.h                      |  68 ++
 drivers/pci/pci-driver.c                      |   9 +-
 drivers/pci/pci.c                             |  14 +-
 drivers/pci/pci.h                             |   5 +
 drivers/pci/probe.c                           |  22 +-
 drivers/pci/quirks.c                          |  58 +-
 include/linux/kho/abi/pci.h                   |  59 ++
Quoted 8 lines
 include/linux/pci.h                           |   4 +
 include/linux/pci_liveupdate.h                |  77 ++
 17 files changed, 1260 insertions(+), 38 deletions(-)
 create mode 100644 Documentation/PCI/liveupdate.rst
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 drivers/pci/liveupdate.h
 create mode 100644 include/linux/kho/abi/pci.h
 create mode 100644 include/linux/pci_liveupdate.h

Bjorn,

Do you have a preference of whether to merge this series through liveupdate.git or pci.git once it's ready? (Mike Rapoport asked if we could make a decision on the merge plan.)

I am leaning toward liveupdate.git:

1. There are some LUO core changes around how FLBs are refcounted in the next branch of liveupdate.git that this series depends on.

2. We had previously agreed that the files covered by the PCI LIVE UPDATE maintainer entry would be merged through the liveupdate.git tree (which is the majority of this series), so this would be following that process.

PCI LIVE UPDATE
...
T: git git://git.kernel.org/pub/scm/linux/kernel/git/liveupdate/linux.git
F: Documentation/PCI/liveupdate.rst
F: drivers/pci/liveupdate.c
F: drivers/pci/liveupdate.h
F: include/linux/kho/abi/pci.h
F: include/linux/pci_liveupdate.h

But if you prefer landing the initial series in pci.git I think we can expose a stable branch in liveupdate.git to handle (1).

What would be your preference?

#9 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

In reply to David Matlack
Quoted 35 lines · David Matlack
Quoted 28 lines · Bjorn Helgaas
Quoted 8 lines · David Matlack

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.
...

+#define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt

Is KBUILD_BASENAME buying us something important here? Just from code-reading perspective, it's more work because without some digging I don't know what it will end up looking like in a dmesg log.

drivers/pci/liveupdate.c line 101

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+     struct pci_flb_outgoing *outgoing = args->obj;
+
+     WARN_ON(outgoing->ser->nr_devices);
+     kho_block_set_destroy(&outgoing->block_set);
+     kho_unpreserve_free(outgoing->ser);
+     kfree(outgoing);
+     pr_debug("Unpreserved struct pci_ser\n");

If this pr_debug() is useful, maybe it should include an address and there should be a corresponding pr_debug() on the preserve side so we could find things in a crashdump?

Quoted 5 lines

drivers/pci/liveupdate.c line 141

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
+{
+     pr_debug("Registering file handler \"%s\"\n", fh->compatible);

Is fh->compatible enough to identify a driver?

Quoted 6 lines

Not necessarily. VFIO could theoretically have one file handler that handles all the vfio-pci variant drivers. But I suspect we'll end up with 1 fh per driver in practice.

Are you thinking about how we can ensure devices are bound to compatible drivers after Live Update?

No, I don't understand enough to worry about things like that. I'm just wondering if there's enough info in the message to be useful, since fh->compatible is the only text that's variable. Same reason I asked about the "Unpreserved struct pci_ser" message above. E.g., if a user reports these messages, will we need more information to debug it, or is the message itself enough?

#10 lore raw

Re: [PATCH v7 01/12] PCI: liveupdate: Set up FLB handler for the PCI core

Quoted 44 lines · Bjorn Helgaas
Quoted 35 lines · David Matlack
Quoted 28 lines · Bjorn Helgaas
Quoted 8 lines · David Matlack

Set up a File-Lifecycle-Bound (FLB) handler for the PCI core to enable it to participate in the preservation of PCI devices across Live Update. Essentially, this commit enables the PCI core to allocate a struct (struct pci_ser) and preserve it across a Live Update whenever at least one device is preserved.
...

+#define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt

Is KBUILD_BASENAME buying us something important here? Just from code-reading perspective, it's more work because without some digging I don't know what it will end up looking like in a dmesg log.

drivers/pci/liveupdate.c line 101

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+     struct pci_flb_outgoing *outgoing = args->obj;
+
+     WARN_ON(outgoing->ser->nr_devices);
+     kho_block_set_destroy(&outgoing->block_set);
+     kho_unpreserve_free(outgoing->ser);
+     kfree(outgoing);
+     pr_debug("Unpreserved struct pci_ser\n");

If this pr_debug() is useful, maybe it should include an address and there should be a corresponding pr_debug() on the preserve side so we could find things in a crashdump?

Quoted 5 lines

drivers/pci/liveupdate.c line 141

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+int pci_liveupdate_register_flb(struct liveupdate_file_handler *fh)
+{
+     pr_debug("Registering file handler \"%s\"\n", fh->compatible);

Is fh->compatible enough to identify a driver?

Quoted 6 lines

Not necessarily. VFIO could theoretically have one file handler that handles all the vfio-pci variant drivers. But I suspect we'll end up with 1 fh per driver in practice.

Are you thinking about how we can ensure devices are bound to compatible drivers after Live Update?

Quoted 6 lines

No, I don't understand enough to worry about things like that. I'm just wondering if there's enough info in the message to be useful, since fh->compatible is the only text that's variable. Same reason I asked about the "Unpreserved struct pci_ser" message above. E.g., if a user reports these messages, will we need more information to debug it, or is the message itself enough?

Ah ok. From that perspective, I think this be sufficient to confirm that VFIO has registered it's file handler for devices bound to vfio-pci, which is all this log needs to do IMO.

#53 lore raw

Re: [PATCH v7 08/12] PCI: liveupdate: Inherit ACS flags in incoming preserved devices

In reply to David Matlack
Quoted 95 lines · David Matlack

Inherit Access Control Services (ACS) flags on all incoming preserved devices (endpoints and upstream bridges) during a Live Update.

Inheriting ACS flags avoids changing routing rules while memory transactions are in flight from preserved devices. This is also strictly necessary to ensure that IOMMU group assignments do not change across a Live Update for preserved devices, as changing ACS configurations can split or merge IOMMU groups.

Cache the inherited ACS controls established by the previous kernel in struct pci_dev so that ACS controls do not change after a reset (pci_restore_state() calls pci_enable_acs()).

To simplify ACS inheritance, reject preserving any devices that require quirks to enable ACS as those quirks would also have to take Live Update into account.

---
 drivers/pci/liveupdate.c       | 68 ++++++++++++++++++++++++++++++++++
 drivers/pci/liveupdate.h       | 11 ++++++
 drivers/pci/pci.c              |  6 +++
 drivers/pci/pci.h              |  5 +++
 drivers/pci/quirks.c           |  7 ++++
 include/linux/pci_liveupdate.h |  6 +++
 6 files changed, 103 insertions(+)
drivers/pci/liveupdate.c +27 -0
index 7f7710cb1da0..a95bfe5eff77 100644
@@ -71,6 +71,9 @@
  *
  *  * The device cannot be a Virtual Function (VF).
  *
+ *  * The device cannot require device-specific quirks to enable Access
+ *    Control Services (ACS).
+ *
  * Driver Binding
  * ==============
  *
@@ -113,6 +116,18 @@
  * This enables the PCI core and any drivers bound to the bridge to participate
  * in the Live Update so that preserved endpoints can continue issuing memory
  * transactions during the Live Update.
+ *
+ * Handling Preserved Devices
+ * ==========================
+ *
+ * The PCI core treats preserved devices differently than non-preserved devices.
+ * This section enumerates those differences.
+ *
+ *  * The PCI core inherits all ACS flags enabled on incoming preserved devices
+ *    rather than assigning new ones. This ensures that TLPs are routed the same
+ *    way after Live Update and ensures that IOMMU groups do not change. Note
+ *    that a device will use its inherited ACS flags for the lifetime of its
+ *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -128,6 +143,7 @@
 #include <linux/slab.h>
 
 #include "liveupdate.h"
+#include "pci.h"
 
 /**
  * struct pci_liveupdate_global - Global state for PCI Live Update support
@@ -374,6 +390,16 @@ static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, s
 {
 	struct pci_dev_ser *dev_ser;
 
+	/*
+	 * Do not preserve devices that rely on device-specific ACS equivalents
+	 * (for now) since that would complicate keeping ACS constant across
+	 * Live Update.
+	 */
+	if (pci_need_dev_specific_enable_acs(dev)) {
+		pci_warn(dev, "Refusing to preserve device that relies on ACS quirks\n");
+		return -EINVAL;
+	}
+
 	dev_ser = pci_get_empty_or_append(outgoing);
 	if (IS_ERR(dev_ser))
 		return PTR_ERR(dev_ser);
@@ -655,6 +681,7 @@ void pci_liveupdate_setup_device(struct pci_dev *dev)
 
 	pci_info(dev, "Device was preserved by previous kernel across Live Update\n");
 	dev->liveupdate.incoming = dev_ser;
+	dev->liveupdate.was_preserved = true;
 
 	/*
 	 * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
@@ -748,6 +775,47 @@ void pci_liveupdate_finish(struct pci_dev *dev)
 }

drivers/pci/liveupdate.c void pci_liveupdate_finish(struct pci_dev *dev) · line 785

Quoted 10 lines
drivers/pci/liveupdate.c +8 -0
 EXPORT_SYMBOL_GPL(pci_liveupdate_finish);
 
+void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->acs_cap || !dev->liveupdate.incoming)
+		return;
+
+	pci_read_config_word(dev, dev->acs_cap + PCI_ACS_CTRL, &dev->liveupdate.acs_ctrl);

This is called from pci_acs_init(), which is called from pci_init_capabilities() when we first enumerate a device, so it captures PCI_ACS_CTRL at boot-time, which is before any pci_enable_acs() calls. I don't think it will include the effect of pci_std_enable_acs() (if an IOMMU driver called pci_request_acs() and there was no device-specific quirk) or any command-line parameters ("pci=config_acs=").

I thought you would want to save the
PCI_ACS_CTRL value at the time of the liveupdate?

I'm having a hard time parsing pci_enable_acs(), so I'm sure I'm missing something here.

Quoted 4 lines
drivers/pci/liveupdate.c +4 -0
+}
+
+int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{

drivers/pci/liveupdate.c void pci_liveupdate_finish(struct pci_dev *dev) · line 799

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+	u16 acs_ctrl = dev->liveupdate.acs_ctrl;
+	u16 acs_cap = dev->acs_cap;
+
+	/*
+	 * Use liveupdate.was_preserved instead of liveupdate.incoming since the
+	 * device's ACS controls should not change even after the device is
+	 * finished participating in the Live Update.
+	 */
+	if (!dev->liveupdate.was_preserved)
+		return -EINVAL;

I don't quite understand what's going on here.

Partly it's because the function name and return values don't seem obvious to me. I guess returning 0 means "this device was preserved across a liveupdate and we restored its previous ACS CTRL value, so pci_enable_acs() doesn't need to do anything else."

Anything else means "device has not been preserved across a liveupdate (or it was preserved but the new kernel added a device-specific ACS quirk for it)."

But more fundamentally, after a liveupdate has completed, why does pci_enable_acs() need to work differently than it would if there had never been a liveupdate?

Maybe it's the comment that's confusing me. Returning -EINVAL means pci_enable_acs() will continue on and potentially update ACS CTRL. The comment suggests "ACS controls shouldn't change even after liveupdate completes", but if we don't want pci_enable_acs() to do anything, wouldn't we return 0 here?

I guess this part will be exercised by pci_restore_state(), e.g., during resume after suspend. I can't remember why we use pci_enable_acs() there instead of saving ACS state in pci_save_state() and then restoring it.

Quoted 150 lines
drivers/pci/liveupdate.c +18 -0
+	/*
+	 * The previous kernel should not have preserved any devices that
+	 * require device-specific quirks to enable ACS, but if such a device is
+	 * detected (e.g. new device-specific ACS quirk in the current kernel),
+	 * log a big warning and fall back to the normal enable ACS path.
+	 */
+	if (pci_need_dev_specific_enable_acs(dev)) {
+		pci_warn(dev, "Device-specific quirk required to enable ACS!\n");
+		WARN_ON_ONCE(true);
+		return -EINVAL;
+	}
+
+	if (acs_cap)
+		pci_write_config_word(dev, acs_cap + PCI_ACS_CTRL, acs_ctrl);
+
+	return 0;
+}
+
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +11 -0
index c763255a8de4..4e8a01bcb4bb 100644
@@ -16,6 +16,8 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev);
 bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 				      int pass);
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
+void pci_liveupdate_init_acs(struct pci_dev *dev);
+int pci_liveupdate_enable_acs(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -35,6 +37,15 @@ static inline bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus,
 static inline void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass)
 {
 }
+
+static inline void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{
+	return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +6 -0
index 77b17b13ee61..739ecaab2e76 100644
@@ -34,6 +34,8 @@
 #include <linux/aer.h>
 #include <linux/bitfield.h>
 #include <linux/suspend.h>
+
+#include "liveupdate.h"
 #include "pci.h"
 
 DEFINE_MUTEX(pci_slot_mutex);
@@ -1008,6 +1010,9 @@ void pci_enable_acs(struct pci_dev *dev)
 	bool enable_acs = false;
 	int pos;
 
+	if (!pci_liveupdate_enable_acs(dev))
+		return;
+
 	/* If an iommu is present we start with kernel default caps */
 	if (pci_acs_enable) {
 		if (pci_dev_specific_enable_acs(dev))
@@ -3689,6 +3694,7 @@ void pci_acs_init(struct pci_dev *dev)
 
 	pci_read_config_word(dev, pos + PCI_ACS_CAP, &dev->acs_capabilities);
 	pci_disable_broken_acs_cap(dev);
+	pci_liveupdate_init_acs(dev);
 }
 
 /**
drivers/pci/pci.h +5 -0
index 4469e1a77f3c..988a18b3204a 100644
@@ -1047,6 +1047,7 @@ void pci_acs_init(struct pci_dev *dev);
 void pci_enable_acs(struct pci_dev *dev);
 #ifdef CONFIG_PCI_QUIRKS
 int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
 void pci_disable_broken_acs_cap(struct pci_dev *pdev);
@@ -1057,6 +1058,10 @@ static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
 {
 	return -ENOTTY;
 }
+static inline bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	return false;
+}
 static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
 	return -ENOTTY;
drivers/pci/quirks.c +7 -0
index 7ac39ec2843e..99b819f38e49 100644
@@ -5473,6 +5473,13 @@ static const struct pci_dev_acs_ops *pci_dev_acs_ops_get(struct pci_dev *dev)
 	return NULL;
 }
 
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
+
+	return p && p->enable_acs;
+}
+
 int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
 	const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
include/linux/pci_liveupdate.h +6 -0
index 2be98819e313..2446c6d237ca 100644
@@ -17,14 +17,20 @@
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
  * @incoming: State preserved by the previous kernel.
+ * @acs_ctrl: ACS features established by the previous kernel.
  * @inherit_buses: True if the PCI core should inherit the secondary and
  *                 subordinate bus numbers assigned to this device due to
  *                 an ongoing Live Update.
+ * @was_preserved: True if this struct pci_dev was preserved by the previous
+ *                 kernel. Unlike @incoming, this field is not cleared after
+ *                 the device is finished participating in Live Update.
  */
 struct pci_liveupdate {
 	struct pci_dev_ser *outgoing;
 	struct pci_dev_ser *incoming;
+	u16 acs_ctrl;
 	bool inherit_buses;
+	bool was_preserved;
 };
 
 struct pci_dev;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#62 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to David Matlack
Quoted 34 lines · David Matlack

Inherit the ARI Forwarding Enable on preserved bridges and update pci_dev->ari_enabled accordingly during a Live Update. This ensures that the preserved devices on the bridge's secondary bus can be identified with the same expanded 8-bit function number after a Live Update.

---
 drivers/pci/liveupdate.c | 18 ++++++++++++++++++
 drivers/pci/liveupdate.h |  6 ++++++
 drivers/pci/pci.c        |  8 +++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)
drivers/pci/liveupdate.c +7 -0
index a95bfe5eff77..74a11e520f0d 100644
@@ -128,6 +128,10 @@
  *    way after Live Update and ensures that IOMMU groups do not change. Note
  *    that a device will use its inherited ACS flags for the lifetime of its
  *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
+ *
+ *  * The PCI core inherits ARI Forwarding Enable on all bridges with downstream
+ *    preserved devices to ensure that all preserved devices on the bridge's
+ *    secondary bus are addressable after the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
 	return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+	u16 val;

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 835

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+
+	guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+	if (!dev->liveupdate.incoming)
+		return -EINVAL;
+
+	pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+	dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);
+	return 0;
+}

I'm guessing we're going to see a lot of this pattern, so it will eventually become familiar, but it's not familiar yet ;) This doesn't "configure" anything (I do understand that avoiding configuration in the new kernel is really the main point of liveupdate).

Maybe a comment is the solution. Or maybe a rename to "pci_liveupdate_ari_preserved" or something? That would have to reverse the sense of return values, but I think something like this in the callers would read better:

if (pci_liveupdate_ari_preserved(dev))

    return;

or maybe:

if (pci_liveupdate_preserve_ari(dev))

    return;
Quoted 56 lines
drivers/pci/liveupdate.c +0 -0
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +6 -0
index 4e8a01bcb4bb..6f21ec50927b 100644
@@ -18,6 +18,7 @@ bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
 void pci_liveupdate_init_acs(struct pci_dev *dev);
 int pci_liveupdate_enable_acs(struct pci_dev *dev);
+int pci_liveupdate_configure_ari(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -46,6 +47,11 @@ static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
 {
 	return -EINVAL;
 }
+
+static inline int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+	return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +7 -1
index 739ecaab2e76..e0c133b66a35 100644
@@ -3528,7 +3528,7 @@ void pci_configure_ari(struct pci_dev *dev)
 	u32 cap;
 	struct pci_dev *bridge;
 
-	if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
+	if (!pci_is_pcie(dev) || dev->devfn)
 		return;
 
 	bridge = dev->bus->self;
@@ -3539,6 +3539,12 @@ void pci_configure_ari(struct pci_dev *dev)
 	if (!(cap & PCI_EXP_DEVCAP2_ARI))
 		return;
 
+	if (!pci_liveupdate_configure_ari(bridge))
+		return;
+
+	if (pcie_ari_disabled)
+		return;
+
 	if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
 		pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
 					 PCI_EXP_DEVCTL2_ARI);
Signature · 1 line
2.55.0.795.g602f6c329a-goog
#54 lore raw

Re: [PATCH v7 08/12] PCI: liveupdate: Inherit ACS flags in incoming preserved devices

In reply to Bjorn Helgaas
Quoted 116 lines · Bjorn Helgaas
Quoted 95 lines · David Matlack

Inherit Access Control Services (ACS) flags on all incoming preserved devices (endpoints and upstream bridges) during a Live Update.

Inheriting ACS flags avoids changing routing rules while memory transactions are in flight from preserved devices. This is also strictly necessary to ensure that IOMMU group assignments do not change across a Live Update for preserved devices, as changing ACS configurations can split or merge IOMMU groups.

Cache the inherited ACS controls established by the previous kernel in struct pci_dev so that ACS controls do not change after a reset (pci_restore_state() calls pci_enable_acs()).

To simplify ACS inheritance, reject preserving any devices that require quirks to enable ACS as those quirks would also have to take Live Update into account.

---
 drivers/pci/liveupdate.c       | 68 ++++++++++++++++++++++++++++++++++
 drivers/pci/liveupdate.h       | 11 ++++++
 drivers/pci/pci.c              |  6 +++
 drivers/pci/pci.h              |  5 +++
 drivers/pci/quirks.c           |  7 ++++
 include/linux/pci_liveupdate.h |  6 +++
 6 files changed, 103 insertions(+)
drivers/pci/liveupdate.c +27 -0
index 7f7710cb1da0..a95bfe5eff77 100644
@@ -71,6 +71,9 @@
  *
  *  * The device cannot be a Virtual Function (VF).
  *
+ *  * The device cannot require device-specific quirks to enable Access
+ *    Control Services (ACS).
+ *
  * Driver Binding
  * ==============
  *
@@ -113,6 +116,18 @@
  * This enables the PCI core and any drivers bound to the bridge to participate
  * in the Live Update so that preserved endpoints can continue issuing memory
  * transactions during the Live Update.
+ *
+ * Handling Preserved Devices
+ * ==========================
+ *
+ * The PCI core treats preserved devices differently than non-preserved devices.
+ * This section enumerates those differences.
+ *
+ *  * The PCI core inherits all ACS flags enabled on incoming preserved devices
+ *    rather than assigning new ones. This ensures that TLPs are routed the same
+ *    way after Live Update and ensures that IOMMU groups do not change. Note
+ *    that a device will use its inherited ACS flags for the lifetime of its
+ *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -128,6 +143,7 @@
 #include <linux/slab.h>
 
 #include "liveupdate.h"
+#include "pci.h"
 
 /**
  * struct pci_liveupdate_global - Global state for PCI Live Update support
@@ -374,6 +390,16 @@ static int __pci_liveupdate_preserve_device(struct pci_flb_outgoing *outgoing, s
 {
      struct pci_dev_ser *dev_ser;
 
+     /*
+      * Do not preserve devices that rely on device-specific ACS equivalents
+      * (for now) since that would complicate keeping ACS constant across
+      * Live Update.
+      */
+     if (pci_need_dev_specific_enable_acs(dev)) {
+             pci_warn(dev, "Refusing to preserve device that relies on ACS quirks\n");
+             return -EINVAL;
+     }
+
      dev_ser = pci_get_empty_or_append(outgoing);
      if (IS_ERR(dev_ser))
              return PTR_ERR(dev_ser);
@@ -655,6 +681,7 @@ void pci_liveupdate_setup_device(struct pci_dev *dev)
 
      pci_info(dev, "Device was preserved by previous kernel across Live Update\n");
      dev->liveupdate.incoming = dev_ser;
+     dev->liveupdate.was_preserved = true;
 
      /*
       * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
@@ -748,6 +775,47 @@ void pci_liveupdate_finish(struct pci_dev *dev)
 }

drivers/pci/liveupdate.c void pci_liveupdate_finish(struct pci_dev *dev) · line 785

Quoted 10 lines
drivers/pci/liveupdate.c +8 -0
 EXPORT_SYMBOL_GPL(pci_liveupdate_finish);
 
+void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+     guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+     if (!dev->acs_cap || !dev->liveupdate.incoming)
+             return;
+
+     pci_read_config_word(dev, dev->acs_cap + PCI_ACS_CTRL, &dev->liveupdate.acs_ctrl);

This is called from pci_acs_init(), which is called from pci_init_capabilities() when we first enumerate a device, so it captures PCI_ACS_CTRL at boot-time, which is before any pci_enable_acs() calls. I don't think it will include the effect of pci_std_enable_acs() (if an IOMMU driver called pci_request_acs() and there was no device-specific quirk) or any command-line parameters ("pci=config_acs=").

Quoted 5 lines

I thought you would want to save the
PCI_ACS_CTRL value at the time of the liveupdate?

I'm having a hard time parsing pci_enable_acs(), so I'm sure I'm missing something here.

Since dev->liveupdate.incoming is non-NULL (see check prior to the pci_read_config_word()), this device was preserved by the previous kernel. The intent of the pci_read_config_word() here is to capture the value of PCI_ACS_CTRL that was established by the previous kernel, so that it can continue to be used in this kernel whenever this kernel needs to re-enable ACS controls (e.g. pci_restore_state() -> pci_enable_acs()).

Quoted 37 lines
Quoted 5 lines
drivers/pci/liveupdate.c +4 -0
+}
+
+int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{

drivers/pci/liveupdate.c void pci_liveupdate_finish(struct pci_dev *dev) · line 799

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+     u16 acs_ctrl = dev->liveupdate.acs_ctrl;
+     u16 acs_cap = dev->acs_cap;
+
+     /*
+      * Use liveupdate.was_preserved instead of liveupdate.incoming since the
+      * device's ACS controls should not change even after the device is
+      * finished participating in the Live Update.
+      */
+     if (!dev->liveupdate.was_preserved)
+             return -EINVAL;

I don't quite understand what's going on here.

Partly it's because the function name and return values don't seem obvious to me. I guess returning 0 means "this device was preserved across a liveupdate and we restored its previous ACS CTRL value, so pci_enable_acs() doesn't need to do anything else."

Anything else means "device has not been preserved across a liveupdate (or it was preserved but the new kernel added a device-specific ACS quirk for it)."

But more fundamentally, after a liveupdate has completed, why does pci_enable_acs() need to work differently than it would if there had never been a liveupdate?

Maybe it's the comment that's confusing me. Returning -EINVAL means pci_enable_acs() will continue on and potentially update ACS CTRL. The comment suggests "ACS controls shouldn't change even after liveupdate completes", but if we don't want pci_enable_acs() to do anything, wouldn't we return 0 here?

Quoted 4 lines

I guess this part will be exercised by pci_restore_state(), e.g., during resume after suspend. I can't remember why we use pci_enable_acs() there instead of saving ACS state in pci_save_state() and then restoring it.

I think that's it. Instead of saving and restoring the ACS controls, pci_restore_state() calls pci_enable_acs().

If we did not cache the ACS controls established by the previous kernel in pci_liveupdate_init_acs() (during pci_acs_init()), and then the device later goes through pci_restore_state() (e.g. reset) then the device will get whatever ACS controls _this_ kernel decides it should have, which may not match what the previous kernel established. Since ACS controls affect things link IOMMU groups, I don't think it's safe to change them.

So the intent of this patch is to ensure that the ACS controls established by the previous kernel are preserved in the next.

Quoted 151 lines
Quoted 151 lines
drivers/pci/liveupdate.c +18 -0
+     /*
+      * The previous kernel should not have preserved any devices that
+      * require device-specific quirks to enable ACS, but if such a device is
+      * detected (e.g. new device-specific ACS quirk in the current kernel),
+      * log a big warning and fall back to the normal enable ACS path.
+      */
+     if (pci_need_dev_specific_enable_acs(dev)) {
+             pci_warn(dev, "Device-specific quirk required to enable ACS!\n");
+             WARN_ON_ONCE(true);
+             return -EINVAL;
+     }
+
+     if (acs_cap)
+             pci_write_config_word(dev, acs_cap + PCI_ACS_CTRL, acs_ctrl);
+
+     return 0;
+}
+
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +11 -0
index c763255a8de4..4e8a01bcb4bb 100644
@@ -16,6 +16,8 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev);
 bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
                                    int pass);
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
+void pci_liveupdate_init_acs(struct pci_dev *dev);
+int pci_liveupdate_enable_acs(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -35,6 +37,15 @@ static inline bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus,
 static inline void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass)
 {
 }
+
+static inline void pci_liveupdate_init_acs(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
+{
+     return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +6 -0
index 77b17b13ee61..739ecaab2e76 100644
@@ -34,6 +34,8 @@
 #include <linux/aer.h>
 #include <linux/bitfield.h>
 #include <linux/suspend.h>
+
+#include "liveupdate.h"
 #include "pci.h"
 
 DEFINE_MUTEX(pci_slot_mutex);
@@ -1008,6 +1010,9 @@ void pci_enable_acs(struct pci_dev *dev)
      bool enable_acs = false;
      int pos;
 
+     if (!pci_liveupdate_enable_acs(dev))
+             return;
+
      /* If an iommu is present we start with kernel default caps */
      if (pci_acs_enable) {
              if (pci_dev_specific_enable_acs(dev))
@@ -3689,6 +3694,7 @@ void pci_acs_init(struct pci_dev *dev)
 
      pci_read_config_word(dev, pos + PCI_ACS_CAP, &dev->acs_capabilities);
      pci_disable_broken_acs_cap(dev);
+     pci_liveupdate_init_acs(dev);
 }
 
 /**
drivers/pci/pci.h +5 -0
index 4469e1a77f3c..988a18b3204a 100644
@@ -1047,6 +1047,7 @@ void pci_acs_init(struct pci_dev *dev);
 void pci_enable_acs(struct pci_dev *dev);
 #ifdef CONFIG_PCI_QUIRKS
 int pci_dev_specific_acs_enabled(struct pci_dev *dev, u16 acs_flags);
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_enable_acs(struct pci_dev *dev);
 int pci_dev_specific_disable_acs_redir(struct pci_dev *dev);
 void pci_disable_broken_acs_cap(struct pci_dev *pdev);
@@ -1057,6 +1058,10 @@ static inline int pci_dev_specific_acs_enabled(struct pci_dev *dev,
 {
      return -ENOTTY;
 }
+static inline bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+     return false;
+}
 static inline int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
      return -ENOTTY;
drivers/pci/quirks.c +7 -0
index 7ac39ec2843e..99b819f38e49 100644
@@ -5473,6 +5473,13 @@ static const struct pci_dev_acs_ops *pci_dev_acs_ops_get(struct pci_dev *dev)
      return NULL;
 }
 
+bool pci_need_dev_specific_enable_acs(struct pci_dev *dev)
+{
+     const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
+
+     return p && p->enable_acs;
+}
+
 int pci_dev_specific_enable_acs(struct pci_dev *dev)
 {
      const struct pci_dev_acs_ops *p = pci_dev_acs_ops_get(dev);
include/linux/pci_liveupdate.h +6 -1
index 2be98819e313..2446c6d237ca 100644
@@ -17,14 +17,20 @@
  * struct pci_liveupdate - PCI Live Update state for a struct pci_dev
  * @outgoing: State preserved for the next kernel.
  * @incoming: State preserved by the previous kernel.
+ * @acs_ctrl: ACS features established by the previous kernel.
  * @inherit_buses: True if the PCI core should inherit the secondary and
  *                 subordinate bus numbers assigned to this device due to
  *                 an ongoing Live Update.
+ * @was_preserved: True if this struct pci_dev was preserved by the previous
+ *                 kernel. Unlike @incoming, this field is not cleared after
+ *                 the device is finished participating in Live Update.
  */
 struct pci_liveupdate {
      struct pci_dev_ser *outgoing;
      struct pci_dev_ser *incoming;
+     u16 acs_ctrl;
      bool inherit_buses;
+     bool was_preserved;
 };
 
 struct pci_dev;
--

2.55.0.795.g602f6c329a-goog

#63 lore raw

Re: [PATCH v7 09/12] PCI: liveupdate: Inherit ARI Forwarding Enable on preserved bridges

In reply to Bjorn Helgaas
Quoted 57 lines · Bjorn Helgaas
Quoted 34 lines · David Matlack

Inherit the ARI Forwarding Enable on preserved bridges and update pci_dev->ari_enabled accordingly during a Live Update. This ensures that the preserved devices on the bridge's secondary bus can be identified with the same expanded 8-bit function number after a Live Update.

---
 drivers/pci/liveupdate.c | 18 ++++++++++++++++++
 drivers/pci/liveupdate.h |  6 ++++++
 drivers/pci/pci.c        |  8 +++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)
drivers/pci/liveupdate.c +7 -0
index a95bfe5eff77..74a11e520f0d 100644
@@ -128,6 +128,10 @@
  *    way after Live Update and ensures that IOMMU groups do not change. Note
  *    that a device will use its inherited ACS flags for the lifetime of its
  *    struct pci_dev (i.e. even after pci_liveupdate_finish()).
+ *
+ *  * The PCI core inherits ARI Forwarding Enable on all bridges with downstream
+ *    preserved devices to ensure that all preserved devices on the bridge's
+ *    secondary bus are addressable after the Live Update.
  */
 
 #define pr_fmt(fmt) "PCI: " KBUILD_BASENAME ": " fmt
@@ -816,6 +820,20 @@ int pci_liveupdate_enable_acs(struct pci_dev *dev)
      return 0;
 }
 
+int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+     u16 val;

drivers/pci/liveupdate.c int pci_liveupdate_enable_acs(struct pci_dev *dev) · line 835

Quoted 10 lines
drivers/pci/liveupdate.c +10 -0
+
+     guard(rwsem_read)(&pci_liveupdate.rwsem);
+
+     if (!dev->liveupdate.incoming)
+             return -EINVAL;
+
+     pcie_capability_read_word(dev, PCI_EXP_DEVCTL2, &val);
+     dev->ari_enabled = !!(val & PCI_EXP_DEVCTL2_ARI);
+     return 0;
+}

I'm guessing we're going to see a lot of this pattern, so it will eventually become familiar, but it's not familiar yet ;) This doesn't "configure" anything (I do understand that avoiding configuration in the new kernel is really the main point of liveupdate).

Maybe a comment is the solution. Or maybe a rename to "pci_liveupdate_ari_preserved" or something? That would have to reverse the sense of return values, but I think something like this in the callers would read better:

Quoted 7 lines

if (pci_liveupdate_ari_preserved(dev))

    return;

or maybe:

if (pci_liveupdate_preserve_ari(dev))

    return;

I do think a better naming and return value convention is needed.

Perhaps "adopt" or "inherit" as the verb? i.e. "adopt/inherit the state of the device established by the previous kernel"

if (pci_liveupdate_adopt_ari(dev))
return;

or

if (pci_liveupdate_inherit_ari(dev))
return?

Quoted 57 lines
Quoted 57 lines
drivers/pci/liveupdate.c +0 -0
 /**
  * pci_liveupdate_is_incoming() - Check if a device is incoming-preserved
  * @dev: The PCI device to check
drivers/pci/liveupdate.h +6 -0
index 4e8a01bcb4bb..6f21ec50927b 100644
@@ -18,6 +18,7 @@ bool pci_liveupdate_scan_bridge_begin(struct pci_bus *bus, struct pci_dev *dev,
 void pci_liveupdate_scan_bridge_end(struct pci_dev *dev, int pass);
 void pci_liveupdate_init_acs(struct pci_dev *dev);
 int pci_liveupdate_enable_acs(struct pci_dev *dev);
+int pci_liveupdate_configure_ari(struct pci_dev *dev);
 #else
 static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
 {
@@ -46,6 +47,11 @@ static inline int pci_liveupdate_enable_acs(struct pci_dev *dev)
 {
      return -EINVAL;
 }
+
+static inline int pci_liveupdate_configure_ari(struct pci_dev *dev)
+{
+     return -EINVAL;
+}
 #endif
 
 #endif /* DRIVERS_PCI_LIVEUPDATE_H */
drivers/pci/pci.c +7 -2
index 739ecaab2e76..e0c133b66a35 100644
@@ -3528,7 +3528,7 @@ void pci_configure_ari(struct pci_dev *dev)
      u32 cap;
      struct pci_dev *bridge;
 
-     if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
+     if (!pci_is_pcie(dev) || dev->devfn)
              return;
 
      bridge = dev->bus->self;
@@ -3539,6 +3539,12 @@ void pci_configure_ari(struct pci_dev *dev)
      if (!(cap & PCI_EXP_DEVCAP2_ARI))
              return;
 
+     if (!pci_liveupdate_configure_ari(bridge))
+             return;
+
+     if (pcie_ari_disabled)
+             return;
+
      if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI)) {
              pcie_capability_set_word(bridge, PCI_EXP_DEVCTL2,
                                       PCI_EXP_DEVCTL2_ARI);
--

2.55.0.795.g602f6c329a-goog