From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A696651B197 for ; Tue, 13 Oct 2026 16:25:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791908740; cv=none; b=ktndIx9DB0SH6IC0vFFP8GO0fg1KUSo3jaSfTYIWZ5cVzMZaqqntXyUy1eBwL2PiH4JCJu3AyjuDIsZZOniSJR0Cfx968Csgx49VRgscASGJ2clDyw5AEIKP5uFi2HWfNxjE6wlKW3cagsc34BmfKjQvXSAzIO+9CNJwPvL7d1w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791908740; c=relaxed/simple; bh=TuKbSTq52Iy9op9ckQZz1D5fIHuwIoe8HHzgG8Llz2o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jN/QrA0dG3OeSJOM03QvEEYnCaUtjAl9gMmS9cSqM3hzkS9Uf15BICIIXBhWpSp03Js9Dv4I6IB/OHPinq2rX9Liuy5Ir3W5fntmRRGTyqI7pHlk+2sMlvd9EvP9ZstVknaXgxwEXui69BwKi3f9KIxzfa/N7oqHSn+RJ6e2N/U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qv6+REqH; arc=none smtp.client-ip=209.85.214.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qv6+REqH" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2e8621795e7so30208625ad.3 for ; Tue, 13 Oct 2026 09:25:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791908737; x=1792513537; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fNADI9rSGSIXIntWZRaMsSHiModR/dpkujIQp/j5Buc=; b=qv6+REqHbeGMj1mQu29QrIKRqKsQPllFFHcjz4A8AqGRNf625aHP99Gy6XosH8UTwa Sih0LPBjjFELLm+7OflDVmUJsMJuPk3hOeALC/xhdL8PnNEfgk2mzZ4vyJ8ddMczgQ+b /d96+mxW4LCVw0J8VLwNwFPzunpWfMZ2hhcjQMlJ5oxa9dTY6jHBRIOOIHaUSXNz4T8B /hfD64lINswNHUoXHZR9mAuf4ZVtbLY2oOnRI3Q66bx4M3gB07fNOZbg/gtpWLLfyok0 RR/M3TLnP0BS++vKETtRs28wuWVLWnJzpX/5313jQ6evSTo0YWByxQCw8mH2dd9+wVAF 3YMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791908737; x=1792513537; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fNADI9rSGSIXIntWZRaMsSHiModR/dpkujIQp/j5Buc=; b=L/WVhPzkCUDsPy7jlIZyzFlmQ5I1XDlBXhKX1waBG8y44iEn8IQ1eQSofjEaDKbR3U wf+44enkSg6I5I+LR55yij+/hrMKiQmsY1Tn9nFBeUk/7HfTLkFvyNDgUuZoHYRF9/mr 6d3ksN2rHNgtQ4snc+kC8V8ZfyP46fsjURisfm1nBOoudYc1btKRSnNm0B7QgYgrDGS2 rw7qVQ4SC1ytcoyjGwG5oeWkV40pe67pJTHHmr2rzxcKtXLBsja08dxCJe/XRx91rrdB lB27HgjaWJhwvSKNPZFlKcHD1wMES/KgVOGoO9ODDAd1v49vbXLzIG0kSDvy0UMq3Sxa 8SxA== X-Forwarded-Encrypted: i=1; AKwUvBxvWh4fF3VHFMYcDt+LzT3A3ZxliOgFn+L4bK6NVHz355ONikkLZEcacsJbceT7RPC/aW1gwxr7IGQ=@vger.kernel.org X-Gm-Message-State: AFq9FYKLE3JvjM729A1lmpPFoNJf47DEmBiXn7b4UF6FRAg2oVOqVEIj yvs4KD7k+GWGeOStV+yMy4ldgY6VRjhwK2Cmp3nMjVQh9hOxc0iD+DZrt+mnpPBCR5zIc0tagML oEjPWqLbMIhhk1Q== X-Received: from pgbbq21.prod.google.com ([2002:a05:6a02:455:b0:cc7:d0a5:4738]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e54a:b0:2e6:dbf:c4a8 with SMTP id d9443c01a7336-2e842ab60d3mr118783575ad.12.1791908736252; Tue, 13 Oct 2026 09:25:36 -0700 (PDT) Date: Tue, 13 Oct 2026 16:25:18 +0000 In-Reply-To: <20261013162529.1928093-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20261013162529.1928093-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.0.385.gd3acb90ef8-goog Message-ID: <20261013162529.1928093-3-dmatlack@google.com> Subject: [PATCH v10 02/13] PCI: liveupdate: Track outgoing preserved PCI devices From: David Matlack To: 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 , Jonathan Corbet , Josh Hilke , Leon Romanovsky , Lukas Wunner , Mike Rapoport , Parav Pandit , Pasha Tatashin , Pranjal Shrivastava , Pratyush Yadav , Randy Dunlap , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , Vipin Sharma , William Tu , Yi Liu Content-Type: text/plain; charset="UTF-8" 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 the list of preserved devices can be handed off to the next kernel. For now, the API only supports preserving non-VF devices on a root bus (not behind an PCI-to-PCI bridges). Reviewed-by: Pranjal Shrivastava Reviewed-by: Pasha Tatashin Reviewed-by: Bjorn Helgaas Reviewed-by: Samiullah Khawaja Signed-off-by: David Matlack --- drivers/pci/liveupdate.c | 211 +++++++++++++++++++++++++++++++++ drivers/pci/liveupdate.h | 21 ++++ drivers/pci/probe.c | 2 + include/linux/pci.h | 3 + include/linux/pci_liveupdate.h | 21 ++++ 5 files changed, 258 insertions(+) create mode 100644 drivers/pci/liveupdate.h diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c index 5f065dccbd7b..22d5d177e104 100644 --- a/drivers/pci/liveupdate.c +++ b/drivers/pci/liveupdate.c @@ -59,6 +59,7 @@ * luo_flb_file_preserve_one() # first preserved file only * pci_flb_preserve() # alloc and preserve struct pci_ser * fh->ops->preserve() # driver callback + * pci_liveupdate_preserve(dev) # record this device in struct pci_ser * * # Userspace: preservation cancelled or session torn down * luo_file_unpreserve_files() @@ -79,6 +80,27 @@ * luo_flb_file_finish() * liveupdate_flb_put_incoming() # last incoming file only * pci_flb_finish() # free struct pci_ser + * + * 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). + * * The device cannot be behind a PCI-to-PCI bridge. */ #define pr_fmt(fmt) "PCI: liveupdate: " fmt @@ -93,6 +115,21 @@ #include #include +#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. @@ -173,6 +210,180 @@ static struct liveupdate_flb pci_liveupdate_flb = { .compatible = PCI_LUO_FLB_COMPATIBLE, }; +static void pci_liveupdate_flb_put_outgoing(void) +{ + liveupdate_flb_put_outgoing(&pci_liveupdate_flb); +} + +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_flb_alloc_dev_ser(struct pci_flb_outgoing *outgoing) +{ + struct pci_dev_ser *dev_ser; + struct kho_block_set_it it; + u64 count = 0; + int err; + + 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) + return dev_ser; + + count++; + } + + /* Otherwise grow the block set and reserve a new entry. */ + err = kho_block_set_grow(&outgoing->block_set, count + 1); + if (err) + return ERR_PTR(err); + + if (!count) + 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)) + 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->is_virtfn) { + pci_warn(dev, "Cannot preserve Virtual Functions\n"); + return -EINVAL; + } + + if (dev->liveupdate.outgoing) { + pci_warn(dev, "Device is already preserved\n"); + return -EBUSY; + } + + if (!pci_is_root_bus(dev->bus)) { + pci_warn(dev, "Cannot preserve devices behind bridges\n"); + return -EINVAL; + } + + dev_ser = pci_flb_alloc_dev_ser(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++; + + 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; + int ret; + + guard(rwsem_write)(&pci_liveupdate.rwsem); + + outgoing = pci_liveupdate_flb_get_outgoing(); + if (IS_ERR(outgoing)) + return PTR_ERR(outgoing); + + ret = pci_liveupdate_preserve_device(outgoing, dev); + + pci_liveupdate_flb_put_outgoing(); + return ret; +} +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); + pci_liveupdate_flb_put_outgoing(); +} +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. diff --git a/drivers/pci/liveupdate.h b/drivers/pci/liveupdate.h new file mode 100644 index 000000000000..b2335581f8d0 --- /dev/null +++ b/drivers/pci/liveupdate.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * PCI Live Update support (core API) + * + * Copyright (c) 2026, Google LLC. + * David Matlack + */ +#ifndef DRIVERS_PCI_LIVEUPDATE_H +#define DRIVERS_PCI_LIVEUPDATE_H + +#include + +#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 */ diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 27008e2ea5af..2a37e5d3e8e3 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -24,6 +24,7 @@ #include #include #include +#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); diff --git a/include/linux/pci.h b/include/linux/pci.h index 95b723aecb08..76abe884e3dc 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -599,6 +599,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) diff --git a/include/linux/pci_liveupdate.h b/include/linux/pci_liveupdate.h index 8ec98beefcb4..894052ad6961 100644 --- a/include/linux/pci_liveupdate.h +++ b/include/linux/pci_liveupdate.h @@ -8,14 +8,26 @@ #ifndef LINUX_PCI_LIVEUPDATE_H #define LINUX_PCI_LIVEUPDATE_H +#include #include +#include #include +/** + * 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 */ -- 2.56.0.385.gd3acb90ef8-goog