[PATCH v6 06/12] PCI: liveupdate: Auto-preserve upstream bridges across Live Update
David Matlack
dmatlack at google.com
Fri May 22 13:24:04 PDT 2026
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.
Signed-off-by: David Matlack <dmatlack at google.com>
---
drivers/pci/liveupdate.c | 136 +++++++++++++++++++++++++++++++-----
include/linux/kho/abi/pci.h | 5 +-
2 files changed, 122 insertions(+), 19 deletions(-)
diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c
index 2421bc218916..4c79e19b7f98 100644
--- a/drivers/pci/liveupdate.c
+++ b/drivers/pci/liveupdate.c
@@ -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: liveupdate: " fmt
@@ -261,28 +273,52 @@ static struct pci_ser *pci_liveupdate_flb_get_outgoing(void)
return ser;
}
-static void pci_liveupdate_unpreserve_device(struct pci_ser *ser, struct pci_dev *dev)
+static int pci_liveupdate_unpreserve_device(struct pci_ser *ser, 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");
ser->nr_devices--;
memset(dev_ser, 0, sizeof(*dev_ser));
dev->liveupdate.outgoing = NULL;
+ return 0;
}
-static int pci_liveupdate_preserve_device(struct pci_ser *ser, struct pci_dev *dev)
+static int pci_liveupdate_preserve_device_again(struct pci_dev *dev)
{
- int i;
+ 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_ser *ser, struct pci_dev *dev)
+{
+ int i;
+
if (ser->nr_devices == ser->max_nr_devices)
return -ENOSPC;
@@ -312,6 +348,52 @@ static int pci_liveupdate_preserve_device(struct pci_ser *ser, struct pci_dev *d
return -ENOSPC;
}
+static int pci_liveupdate_preserve_device(struct pci_ser *ser, struct pci_dev *dev)
+{
+ if (dev->liveupdate.outgoing)
+ return pci_liveupdate_preserve_device_again(dev);
+
+ return __pci_liveupdate_preserve_device(ser, 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_ser *ser,
+ 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(ser, dev))
+ return;
+ }
+}
+
+static void pci_liveupdate_unpreserve_path(struct pci_ser *ser,
+ struct pci_dev *start)
+{
+ __pci_liveupdate_unpreserve_path(ser, start, /*end=*/NULL);
+}
+
+static int pci_liveupdate_preserve_path(struct pci_ser *ser,
+ struct pci_dev *start)
+{
+ struct pci_dev *dev;
+ int ret;
+
+ for_each_pci_dev_in_path(dev, start, NULL) {
+ ret = pci_liveupdate_preserve_device(ser, dev);
+ if (ret) {
+ __pci_liveupdate_unpreserve_path(ser, start, dev);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
/**
* pci_liveupdate_preserve() - Preserve a PCI device across Live Update
* @dev: The PCI device to preserve.
@@ -321,6 +403,9 @@ static int pci_liveupdate_preserve_device(struct pci_ser *ser, struct pci_dev *d
* 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)
@@ -336,7 +421,7 @@ int pci_liveupdate_preserve(struct pci_dev *dev)
if (IS_ERR(ser))
return PTR_ERR(ser);
- return pci_liveupdate_preserve_device(ser, dev);
+ return pci_liveupdate_preserve_path(ser, dev);
}
EXPORT_SYMBOL_GPL(pci_liveupdate_preserve);
@@ -349,6 +434,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)
{
@@ -362,7 +450,7 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
return;
}
- pci_liveupdate_unpreserve_device(ser, dev);
+ pci_liveupdate_unpreserve_path(ser, dev);
}
EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve);
@@ -534,29 +622,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;
+ }
}
/**
@@ -568,6 +668,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)
{
@@ -581,7 +683,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);
diff --git a/include/linux/kho/abi/pci.h b/include/linux/kho/abi/pci.h
index 85def616703d..c86518be4ce7 100644
--- a/include/linux/kho/abi/pci.h
+++ b/include/linux/kho/abi/pci.h
@@ -23,7 +23,7 @@
* incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
*/
-#define PCI_LUO_FLB_COMPATIBLE "pci-v2"
+#define PCI_LUO_FLB_COMPATIBLE "pci-v3"
/**
* 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 {
--
2.54.0.746.g67dd491aae-goog
More information about the kexec
mailing list