From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BE9E2FDEE4D for ; Thu, 23 Apr 2026 21:23:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=eZtY+EWHg3/BRWz3H3Wx+cRr9MZM3880uCt4Dp7O+PI=; b=NYIjywRK/3EE/Vmb+yqzl0/lKZ We1frhqPS1Iaw0gdf7lMyASm7GUVTpoXhMolm8GDShhOKQAuPHtswYMMF5IbV0ho8cep4RiAfpdQ8 dDNdrr9wvSysBPNkgI2MS5qUoAYYWRCJxtEvmgPe/1I+7z1KbwaMXG0Rc6Tv8biRswiIk5DJ+F4Oi hbEPTjuAZVm76xha9KSvK7SaseF14/KV+0DrIqR12vVmDatdoi7Jv6dazA2CK7KsGLfE/tMeoDcqg O7nQgtRv6A1sISHR0Jl1rc0PPZM+IMiCBqnfl16144f29JmSKl8G5pZiRzY1dPLG80DV/wakocQ3i mFRnKwRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wG1WJ-0000000CLAd-29RE; Thu, 23 Apr 2026 21:23:35 +0000 Received: from mail-pl1-x64a.google.com ([2607:f8b0:4864:20::64a]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wG1WG-0000000CL77-061q for kexec@lists.infradead.org; Thu, 23 Apr 2026 21:23:33 +0000 Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-2b2e06219cbso90189545ad.3 for ; Thu, 23 Apr 2026 14:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776979411; x=1777584211; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=eZtY+EWHg3/BRWz3H3Wx+cRr9MZM3880uCt4Dp7O+PI=; b=ufL+69G2A1HPpXLY9go0URex/R0XVxDdPjaT2cOJ8qg8ftgDWAjtmk2LoaLn/y7jN+ p3NUsMYOZB1+OW0HPLxFPeEVc1qINYyd0VJww55V3hT3VvyicZiLfOwPjpGW12T1RyCh qL/4/PoK80QvPesvOLC5g/siVy4ntoEXY/sheao7KaLE0eX8bkVIAZzMOsZbV/BgvxZm zjNFnGFOiPz10MyaaBgH3iLVcBIunOc+ktyyDhDCdS0CP0PyJyfXrIsbHWexrt9jSqlp S+IuQYrwfxtWju6ULM55lVvgasVSwGxzpvzjjbLIp80NVWIVurIEj+HJSiewmzmumV66 R2rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776979411; x=1777584211; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eZtY+EWHg3/BRWz3H3Wx+cRr9MZM3880uCt4Dp7O+PI=; b=OUvBKafw0QPsD9q8N61EhhvvnQIxhUSj2bgnLF9TBvm8J1vKvkhrHXbCnWWO3EPxMX nRdB95zdobCVVVRXgpbPyzy+Mz0i6fPKhDJSYNMFBjt1uDuoiwZ9tcbmrABm9BdiKDCl ApCeLRURx9vy/W50Q3LKm167Zw4sglrz5NaXpcqTN4Wa8y2gr05bKIjoTZsgqZoC8nZf fkpGE6mP5yDZBP2hNBSTpDCHhSu7BXNdcychiHz4OhO3A7cVwZkkkTM3FeLquaUVqztz UA5DDFbmYNnOgwz3YuL62vqGO5BkdPvDse9ZxcynGssPFDjboihW2pALxqCKD7LOKMUv HkQg== X-Forwarded-Encrypted: i=1; AFNElJ9aY7OLDfmvPwMNMFwpoKmecFFpvaM0vKOLIt7S+oNRSpEfqcW5A42eblNb3pUajF+ZO6807g==@lists.infradead.org X-Gm-Message-State: AOJu0Yx6AFQgb7dmlkM1v6SB33jU65bYmvZkSG+AKV4MhMASwNqJD/pq 5Q8dk3OZVLp+Vo5BRWxK5nADQ2lEaUlJWJadgnfKgIMLyW6xCGO/OAYRC3/9nnF/1RRxVTLpG8G Iz1tuMFN/PU+mKA== X-Received: from pghu17.prod.google.com ([2002:a63:ef11:0:b0:c74:321:201a]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3949:b0:39f:27ab:2438 with SMTP id adf61e73a8af0-3a08d90bdbamr33243244637.48.1776979410524; Thu, 23 Apr 2026 14:23:30 -0700 (PDT) Date: Thu, 23 Apr 2026 21:23:10 +0000 In-Reply-To: <20260423212316.3431746-1-dmatlack@google.com> Mime-Version: 1.0 References: <20260423212316.3431746-1-dmatlack@google.com> X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260423212316.3431746-7-dmatlack@google.com> Subject: [PATCH v4 06/11] PCI: liveupdate: Auto-preserve upstream bridges across Live Update From: David Matlack To: iommu@lists.linux.dev, kexec@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org Cc: Adithya Jayachandran , Alexander Graf , Alex Williamson , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Joerg Roedel , Jonathan Corbet , Josh Hilke , Leon Romanovsky , Lukas Wunner , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Robin Murphy , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , Will Deacon , William Tu , Yi Liu Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260423_142332_078716_6A1BBC88 X-CRM114-Status: GOOD ( 22.17 ) X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" Errors-To: kexec-bounces+kexec=archiver.kernel.org@lists.infradead.org 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 --- drivers/pci/liveupdate.c | 149 +++++++++++++++++++++++++++++---------- 1 file changed, 111 insertions(+), 38 deletions(-) diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c index cf8cff134a75..88125f9a2c6b 100644 --- a/drivers/pci/liveupdate.c +++ b/drivers/pci/liveupdate.c @@ -106,6 +106,18 @@ * If a misconfigured or unconfigured bridge is encountered during enumeration * while there are incoming preserved devices, it's 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 @@ -233,25 +245,14 @@ static struct liveupdate_flb pci_liveupdate_flb = { .compatible = PCI_LUO_FLB_COMPATIBLE, }; -int pci_liveupdate_preserve(struct pci_dev *dev) +static int pci_liveupdate_preserve_device(struct pci_ser *ser, struct pci_dev *dev) { - struct pci_ser *ser; - int i, ret; - - guard(mutex)(&pci_flb_outgoing_lock); - - ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser); - if (ret) - return ret; + int i; - if (!ser) - return -ENOENT; - - if (dev->is_virtfn) - return -EINVAL; - - if (dev->liveupdate_outgoing) - return -EBUSY; + if (dev->liveupdate_outgoing) { + dev->liveupdate_outgoing->refcount++; + return 0; + } if (ser->nr_devices == ser->max_nr_devices) return -ENOSPC; @@ -281,11 +282,82 @@ int pci_liveupdate_preserve(struct pci_dev *dev) return -ENOSPC; } + +static void pci_liveupdate_unpreserve_path(struct pci_ser *ser, struct pci_dev *dev) +{ + struct pci_dev *upstream_bridge = dev->bus->self; + struct pci_dev_ser *dev_ser; + + if (upstream_bridge) + pci_liveupdate_unpreserve_path(ser, upstream_bridge); + + dev_ser = dev->liveupdate_outgoing; + if (!dev_ser) { + pci_warn(dev, "Cannot unpreserve device that is not preserved\n"); + return; + } + + if (--dev_ser->refcount == 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; + } +} + +static int pci_liveupdate_preserve_path(struct pci_ser *ser, struct pci_dev *dev) +{ + struct pci_dev *upstream_bridge = dev->bus->self; + int ret = 0; + + if (upstream_bridge) { + ret = pci_liveupdate_preserve_path(ser, upstream_bridge); + if (ret) + return ret; + } else if (!pci_is_root_bus(dev->bus)) { + pci_err(dev, "Failed to preserve up to root port\n"); + return -EINVAL; + } + + ret = pci_liveupdate_preserve_device(ser, dev); + if (ret) + goto err; + + return 0; + +err: + if (upstream_bridge) + pci_liveupdate_unpreserve_path(ser, upstream_bridge); + + return ret; +} + +int pci_liveupdate_preserve(struct pci_dev *dev) +{ + struct pci_ser *ser; + int ret; + + guard(mutex)(&pci_flb_outgoing_lock); + + ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser); + if (ret) + return ret; + + if (!ser) + return -ENOENT; + + if (dev->is_virtfn) + return -EINVAL; + + if (dev->liveupdate_outgoing) + return -EBUSY; + + return pci_liveupdate_preserve_path(ser, dev); +} EXPORT_SYMBOL_GPL(pci_liveupdate_preserve); void pci_liveupdate_unpreserve(struct pci_dev *dev) { - struct pci_dev_ser *dev_ser; struct pci_ser *ser = NULL; int ret; @@ -296,19 +368,9 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev) if (ret || !ser) { pci_warn(dev, "Cannot unpreserve device without outgoing Live Update state\n"); return; - - } - - 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"); - ser->nr_devices--; - memset(dev_ser, 0, sizeof(*dev_ser)); - dev->liveupdate_outgoing = NULL; + pci_liveupdate_unpreserve_path(ser, dev); } EXPORT_SYMBOL_GPL(pci_liveupdate_unpreserve); @@ -428,6 +490,25 @@ void pci_liveupdate_cleanup_device(struct pci_dev *dev) pci_liveupdate_flb_put_incoming(); } +static void pci_liveupdate_finish_path(struct pci_dev *dev) +{ + struct pci_dev *upstream_bridge = dev->bus->self; + + if (upstream_bridge) + pci_liveupdate_finish_path(upstream_bridge); + + /* + * 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 becase the device is hot-plugged. + */ + if (--dev->liveupdate_incoming->refcount) + return; + + pci_info(dev, "Device is finished participating in Live Update\n"); + dev->liveupdate_incoming = NULL; +} + void pci_liveupdate_finish(struct pci_dev *dev) { if (!dev->liveupdate_incoming) { @@ -435,15 +516,7 @@ void pci_liveupdate_finish(struct pci_dev *dev) return; } - pci_info(dev, "Device is finished participating in Live Update\n"); - - /* - * 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 becase the device is hot-plugged. - */ - dev->liveupdate_incoming->refcount = 0; - dev->liveupdate_incoming = NULL; + pci_liveupdate_finish_path(dev); /* Drop this device's reference on the incoming FLB. */ pci_liveupdate_flb_put_incoming(); -- 2.54.0.rc2.544.gc7ae2d5bb8-goog