Precede each serialized PCI device entry in the PCI Live Update KHO block set with struct kho_ser_hdr via DECLARE_KHO_SER so future per-device PCI Live Update features can append fields to struct pci_dev_ser without breaking compatibility between adjacent kernels. Mark struct pci_dev_ser as 8-byte aligned since its largest member is u32, initialize the device block sets with kho_ser_size(pci_dev_ser), initialize each entry feature header in pci_flb_alloc_dev_ser(), and validate incoming device feature headers in pci_flb_retrieve() before accessing the struct pci_dev_ser payload. Signed-off-by: Tarun Sahu --- drivers/pci/liveupdate.c | 27 +++++++++++++++++++-------- include/linux/kho/abi/pci.h | 9 ++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c index 0bda94f4a7e4..f4c84bcc0629 100644 --- a/drivers/pci/liveupdate.c +++ b/drivers/pci/liveupdate.c @@ -288,7 +288,7 @@ static int pci_flb_preserve(struct liveupdate_flb_op_args *args) ser->devices = 0; outgoing->fhdr = fhdr; - kho_block_set_init(&outgoing->block_set, sizeof(struct pci_dev_ser)); + kho_block_set_init(&outgoing->block_set, kho_ser_size(pci_dev_ser)); args->obj = no_free_ptr(outgoing); args->data = virt_to_phys(fhdr); @@ -318,6 +318,7 @@ static int pci_flb_retrieve(struct liveupdate_flb_op_args *args) { DECLARE_KHO_SER_HDR(pci_ser, *fhdr) = phys_to_virt(args->data); struct pci_flb_incoming *incoming; + DECLARE_KHO_SER_HDR(pci_dev_ser, *dev_fhdr); struct pci_dev_ser *dev_ser; struct kho_block_set_it it; struct pci_ser *ser; @@ -338,15 +339,20 @@ static int pci_flb_retrieve(struct liveupdate_flb_op_args *args) incoming->fhdr = fhdr; xa_init(&incoming->xa); - kho_block_set_init(&incoming->block_set, sizeof(struct pci_dev_ser)); + kho_block_set_init(&incoming->block_set, kho_ser_size(pci_dev_ser)); ret = kho_block_set_restore(&incoming->block_set, ser->devices); if (ret) panic("Failed to restore devices KHO block set (%d)\n", ret); kho_block_set_it_init(&it, &incoming->block_set); - while ((dev_ser = kho_block_set_it_read_entry(&it))) { + while ((dev_fhdr = kho_block_set_it_read_entry(&it))) { unsigned long key; + ret = kho_ser_validate(dev_fhdr, pci_dev_ser); + if (ret) + panic("Failed to validate incoming PCI device feature header (%d)\n", ret); + + dev_ser = kho_ser_payload(dev_fhdr, pci_dev_ser); if (!dev_ser->refcount) continue; @@ -456,6 +462,7 @@ static struct pci_flb_outgoing *pci_liveupdate_flb_get_outgoing(void) static struct pci_dev_ser *pci_flb_alloc_dev_ser(struct pci_flb_outgoing *outgoing) { + DECLARE_KHO_SER_HDR(pci_dev_ser, *fhdr); struct pci_dev_ser *dev_ser; struct kho_block_set_it it; u64 count = 0; @@ -464,9 +471,12 @@ static struct pci_dev_ser *pci_flb_alloc_dev_ser(struct pci_flb_outgoing *outgoi kho_block_set_it_init(&it, &outgoing->block_set); /* Try to find an existing, previously unpreserved, entry. */ - while ((dev_ser = kho_block_set_it_read_entry(&it))) { - if (!dev_ser->refcount) + while ((fhdr = kho_block_set_it_read_entry(&it))) { + dev_ser = kho_ser_payload(fhdr, pci_dev_ser); + if (!dev_ser->refcount) { + kho_ser_init(fhdr, pci_dev_ser); return dev_ser; + } count++; } @@ -480,11 +490,12 @@ static struct pci_dev_ser *pci_flb_alloc_dev_ser(struct pci_flb_outgoing *outgoi kho_block_set_it_init(&it, &outgoing->block_set); /* This should always succeed since kho_block_set_grow() succeeded. */ - dev_ser = kho_block_set_it_reserve_entry(&it); - if (WARN_ON_ONCE(!dev_ser)) + fhdr = kho_block_set_it_reserve_entry(&it); + if (WARN_ON_ONCE(!fhdr)) return ERR_PTR(-ENOSPC); - return dev_ser; + kho_ser_init(fhdr, pci_dev_ser); + return kho_ser_payload(fhdr, pci_dev_ser); } static int pci_liveupdate_unpreserve_device(struct pci_flb_outgoing *outgoing, diff --git a/include/linux/kho/abi/pci.h b/include/linux/kho/abi/pci.h index f53f6350e53d..77ddf5938822 100644 --- a/include/linux/kho/abi/pci.h +++ b/include/linux/kho/abi/pci.h @@ -19,7 +19,8 @@ * File-Lifecycle Bound (FLB) data, identified by the compatible string * ``"pci-v2"`` and laid out as described by the structs below. * - * struct pci_ser is preceded by &struct kho_ser_hdr, which records the + * Both &struct pci_ser and each &struct pci_dev_ser entry are preceded by + * &struct kho_ser_hdr, which records the * total size of the serialization and feature flags. See * include/linux/kho/abi/feature.h for the feature header layout, feature * rules, and validation. @@ -42,14 +43,16 @@ * reference count 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. + * + * Preceded by &struct kho_ser_hdr. */ struct pci_dev_ser { u32 domain; u16 bdf; u16 refcount; -}; +} __aligned(8); -static_assert(sizeof(struct pci_dev_ser) == 8); +DECLARE_KHO_SER(pci_dev_ser, 8, 0, 0); /** * struct pci_ser - PCI Subsystem Live Update State -- 2.56.0.506.g506a7eabc5-goog