From: Vipin Sharma <vipinsh@google.com>
To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org, kvm@vger.kernel.org,
linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
linux-pci@vger.kernel.org
Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com,
ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com,
chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net,
dmatlack@google.com, graf@amazon.com,
jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca,
jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org,
kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com,
lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com,
parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com,
pratyush@kernel.org, rananta@google.com, rdunlap@infradead.org,
rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org,
saeedm@nvidia.com, schnelle@linux.ibm.com,
skhan@linuxfoundation.org, skhawaja@google.com,
vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com,
yanjun.zhu@linux.dev, yi.l.liu@intel.com
Subject: [PATCH v6 14/27] vfio/pci: Take over preserved devices without resetting them
Date: Mon, 2 Nov 2026 07:36:43 -0800 [thread overview]
Message-ID: <20261102153656.1431701-15-vipinsh@google.com> (raw)
In-Reply-To: <20261102153656.1431701-1-vipinsh@google.com>
When userspace binds a retrieved VFIO cdev file descriptor via
VFIO_DEVICE_BIND_IOMMUFD after kexec, take over the running device in
vfio_pci_core_enable() via vfio_pci_liveupdate_take_over() without
resetting it or clearing bus mastering:
- Skip pci_clear_master(), pci_try_reset_function(), and
pci_save_state().
- Save PCI_COMMAND before pci_enable_device() and restore the memory and
I/O decode bits afterwards so pci_enable_device() does not re-enable
BAR decode (such as I/O port decode) that was disabled before kexec.
- Also restore PCI_COMMAND_INTX_DISABLE, which freeze() set before
kexec, when vfio-pci uses PCI 2.3 INTx masking for the device
(vdev->pci_2_3) or masks its broken INTx (vdev->nointx), so INTx stays
masked until userspace configures interrupts. Otherwise vfio-pci masks
INTx at the interrupt controller (e.g. with vfio_pci.nointxmask=1), so
leave DisINTx cleared by pci_enable_device() as a normal open does.
- Build pci_config_map via vfio_config_init(), verify that it matches
the preserved config map snapshot, and overlay the preserved vconfig
snapshot onto vdev->vconfig.
- Perform all fallible take-over setup before adopting the preserved
state. If take-over fails before the device is claimed, leave the
running device untouched (keeping bus mastering, decode bits, and
liveupdate_incoming intact) so userspace can retry binding the
retrieved FD or let the session finish/close reset the unclaimed
device.
- Once vfio_pci_liveupdate_take_over() succeeds, adopt the preserved
pci_saved_state and reset_works flag, clear liveupdate_kept_running,
and free the temporary incoming state; any subsequent error or final
device close follows the normal vfio_pci_core_disable() path and
resets the device.
Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
drivers/vfio/pci/vfio_pci_core.c | 117 +++++++++++++++++++++----
drivers/vfio/pci/vfio_pci_liveupdate.c | 23 ++++-
drivers/vfio/pci/vfio_pci_priv.h | 23 ++++-
3 files changed, 141 insertions(+), 22 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 828d620cf836..63f484c0654c 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -595,8 +595,68 @@ static const struct dev_pm_ops vfio_pci_core_pm_ops = {
NULL)
};
+/*
+ * Take over a device that was kept running across a Live Update. The device
+ * is enabled only after every step that can fail, because unwinding with
+ * pci_disable_device() would clear bus mastering on the running device. A
+ * failed take-over thus leaves the device untouched and can be retried.
+ */
+static int vfio_pci_liveupdate_take_over(struct vfio_pci_core_device *vdev,
+ struct vfio_pci_liveupdate_incoming *in)
+{
+ const u8 *vconfig = in->data;
+ const u8 *config_map = in->data + in->cfg_size;
+ struct pci_dev *pdev = vdev->pdev;
+ u16 orig, cmd, keep;
+ int ret;
+
+ if (in->cfg_size != pdev->cfg_size ||
+ memcmp(config_map, vdev->pci_config_map, in->cfg_size)) {
+ pci_warn(pdev,
+ "Config map mismatch during Live Update take-over\n");
+ return -EINVAL;
+ }
+
+ pci_read_config_word(pdev, PCI_COMMAND, &orig);
+ ret = pci_enable_device(pdev);
+
+ /*
+ * pci_enable_device() enables memory and I/O decode for all assigned
+ * BARs and clears PCI_COMMAND_INTX_DISABLE. Restore the decode settings
+ * the device was running with and keep INTx masked, as freeze() left
+ * it, until userspace configures interrupts. If vfio-pci does not use
+ * PCI 2.3 INTx masking for the device, it masks INTx at the interrupt
+ * controller instead, so leave PCI_COMMAND_INTX_DISABLE as a normal
+ * open does. If enabling failed, restore everything.
+ */
+ keep = PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
+ if (ret || vdev->pci_2_3 || vdev->nointx)
+ keep |= PCI_COMMAND_INTX_DISABLE;
+ pci_read_config_word(pdev, PCI_COMMAND, &cmd);
+ if ((cmd ^ orig) & keep)
+ pci_write_config_word(pdev, PCI_COMMAND,
+ (cmd & ~keep) | (orig & keep));
+ if (ret)
+ return ret;
+
+ memcpy(vdev->vconfig, vconfig, in->cfg_size);
+ /*
+ * Redo the virtual BAR fixup: the snapshot may hold a BAR sizing write
+ * not fixed up yet, or a fixup half done by a config read racing with
+ * freeze(), as config reads do not take vdev->igate.
+ */
+ vdev->bardirty = true;
+ cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
+ vdev->virq_disabled = !!(cmd & PCI_COMMAND_INTX_DISABLE);
+ vdev->reset_works = in->reset_works;
+
+ return 0;
+}
+
int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
{
+ struct vfio_pci_liveupdate_incoming *in =
+ vfio_pci_liveupdate_incoming(vdev);
struct pci_dev *pdev = vdev->pdev;
int ret;
u16 cmd;
@@ -608,23 +668,30 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
return ret;
}
- /* Don't allow our initial saved state to include busmaster */
- pci_clear_master(pdev);
+ /*
+ * A device kept running across a Live Update is enabled later, without
+ * a reset, by vfio_pci_liveupdate_take_over().
+ */
+ if (!in) {
+ /* Don't allow our initial saved state to include busmaster */
+ pci_clear_master(pdev);
- ret = pci_enable_device(pdev);
- if (ret)
- goto out_power;
+ ret = pci_enable_device(pdev);
+ if (ret)
+ goto out_power;
- /* If reset fails because of the device lock, fail this path entirely */
- ret = pci_try_reset_function(pdev);
- if (ret == -EAGAIN)
- goto out_disable_device;
+ /* If reset fails because of the device lock, fail this path entirely */
+ ret = pci_try_reset_function(pdev);
+ if (ret == -EAGAIN)
+ goto out_disable_device;
- vdev->reset_works = !ret;
- pci_save_state(pdev);
- vdev->pci_saved_state = pci_store_saved_state(pdev);
- if (!vdev->pci_saved_state)
- pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__);
+ vdev->reset_works = !ret;
+ pci_save_state(pdev);
+ vdev->pci_saved_state = pci_store_saved_state(pdev);
+ if (!vdev->pci_saved_state)
+ pci_dbg(pdev, "%s: Couldn't store saved state\n",
+ __func__);
+ }
if (likely(!vdev->nointxmask)) {
if (vfio_pci_nointx(pdev)) {
@@ -635,10 +702,12 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
vdev->pci_2_3 = pci_intx_mask_supported(pdev);
}
- pci_read_config_word(pdev, PCI_COMMAND, &cmd);
- if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
- cmd &= ~PCI_COMMAND_INTX_DISABLE;
- pci_write_config_word(pdev, PCI_COMMAND, cmd);
+ if (!in) {
+ pci_read_config_word(pdev, PCI_COMMAND, &cmd);
+ if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
+ cmd &= ~PCI_COMMAND_INTX_DISABLE;
+ pci_write_config_word(pdev, PCI_COMMAND, cmd);
+ }
}
ret = vfio_pci_zdev_open_device(vdev);
@@ -649,6 +718,12 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_free_zdev;
+ if (in) {
+ ret = vfio_pci_liveupdate_take_over(vdev, in);
+ if (ret)
+ goto out_free_config;
+ }
+
msix_pos = pdev->msix_cap;
if (msix_pos) {
u16 flags;
@@ -675,13 +750,17 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
return 0;
+out_free_config:
+ vfio_config_free(vdev);
out_free_zdev:
vfio_pci_zdev_close_device(vdev);
out_free_state:
kfree(vdev->pci_saved_state);
vdev->pci_saved_state = NULL;
out_disable_device:
- pci_disable_device(pdev);
+ /* A take-over enables the device only once nothing can fail. */
+ if (!in)
+ pci_disable_device(pdev);
out_power:
if (!vdev->disable_idle_d3)
pm_runtime_put(&pdev->dev);
diff --git a/drivers/vfio/pci/vfio_pci_liveupdate.c b/drivers/vfio/pci/vfio_pci_liveupdate.c
index ad757acac400..00c008117259 100644
--- a/drivers/vfio/pci/vfio_pci_liveupdate.c
+++ b/drivers/vfio/pci/vfio_pci_liveupdate.c
@@ -120,7 +120,28 @@
*
* ``LIVEUPDATE_SESSION_RETRIEVE_FD`` validates the handed-over serialized state
* and returns a new unbound VFIO cdev file descriptor. The device is claimed on
- * the first ``VFIO_DEVICE_BIND_IOMMUFD`` on that file descriptor.
+ * the first ``VFIO_DEVICE_BIND_IOMMUFD`` on that file descriptor without
+ * resetting it or clearing ``PCI_COMMAND_MASTER``:
+ *
+ * * ``vfio_pci_core_enable()`` skips ``pci_clear_master()``,
+ * ``pci_try_reset_function()``, and ``pci_save_state()``. It builds
+ * ``pci_config_map`` normally via ``vfio_config_init()`` and verifies that
+ * it matches the preserved snapshot before it enables the device. It keeps
+ * the memory and I/O decode settings and ``PCI_COMMAND_MASTER`` the device
+ * was running with, and keeps physical INTx masked until userspace
+ * configures interrupts. On devices for which vfio-pci does not use PCI
+ * 2.3 INTx masking (e.g. with ``vfio_pci.nointxmask=1``), vfio-pci masks
+ * INTx at the interrupt controller, so ``PCI_COMMAND_INTX_DISABLE`` is left
+ * as on a normal open. It then overlays the preserved ``vconfig`` snapshot
+ * and adopts the preserved ``pci_saved_state`` and ``reset_works`` flag for
+ * subsequent resets or device close.
+ *
+ * * If the take-over in ``vfio_pci_core_enable()`` fails,
+ * ``VFIO_DEVICE_BIND_IOMMUFD`` fails and leaves the device running
+ * untouched, and userspace can retry the bind. If the ioctl fails after
+ * the device was claimed, it closes the device, which resets it like any
+ * other close. A device that is never claimed is reset when the session
+ * is finished or closed, as described below.
*
* ``LIVEUPDATE_SESSION_FINISH`` (or closing a retrieved session file
* descriptor) always completes:
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 39ea26402312..2549a5321fce 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -203,6 +203,12 @@ vfio_pci_liveupdate_check_noiommu(struct vfio_pci_core_device *vdev)
}
}
+static inline struct vfio_pci_liveupdate_incoming *
+vfio_pci_liveupdate_incoming(struct vfio_pci_core_device *vdev)
+{
+ return vdev->liveupdate_incoming;
+}
+
static inline void
vfio_pci_liveupdate_free_incoming(struct vfio_pci_core_device *vdev)
{
@@ -219,6 +225,8 @@ vfio_pci_liveupdate_free_incoming(struct vfio_pci_core_device *vdev)
static inline void
vfio_pci_liveupdate_claim_incoming(struct vfio_pci_core_device *vdev)
{
+ struct vfio_pci_liveupdate_incoming *in = vdev->liveupdate_incoming;
+
/*
* A reset that finish() left for unbind is not needed once the device
* is opened after finish(): the open resets the device if it can, and
@@ -226,10 +234,15 @@ vfio_pci_liveupdate_claim_incoming(struct vfio_pci_core_device *vdev)
*/
vdev->liveupdate_reset_pending = false;
- if (!vdev->liveupdate_incoming)
+ if (!in)
return;
- vfio_pci_liveupdate_free_incoming(vdev);
+ vdev->pci_saved_state = in->saved_state;
+ if (!vdev->pci_saved_state)
+ pci_dbg(vdev->pdev, "%s: Couldn't store saved state\n",
+ __func__);
+ kfree(in);
+ vdev->liveupdate_incoming = NULL;
vdev->liveupdate_kept_running = false;
}
@@ -288,6 +301,12 @@ vfio_pci_liveupdate_check_noiommu(struct vfio_pci_core_device *vdev)
{
}
+static inline struct vfio_pci_liveupdate_incoming *
+vfio_pci_liveupdate_incoming(struct vfio_pci_core_device *vdev)
+{
+ return NULL;
+}
+
static inline void
vfio_pci_liveupdate_free_incoming(struct vfio_pci_core_device *vdev)
{
--
2.56.0.317.g454f8302e1-goog
next prev parent reply other threads:[~2026-11-02 15:37 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 15:36 [PATCH v6 00/27] VFIO PCI Live Update (noiommu mode) Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 01/27] vfio/pci: Factor out the reset logic in VFIO PCI device close path Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 02/27] vfio: Export various helpers from VFIO Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 03/27] liveupdate: Export symbols needed by modules Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 04/27] vfio/pci: Register a file handler with Live Update Orchestrator Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 05/27] PCI: liveupdate: Add helpers to preserve struct pci_saved_state Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 06/27] vfio: Add helpers to identify device files that can be preserved Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 07/27] vfio/pci: Preserve vfio-pci device files across Live Update Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 08/27] vfio: Introduce vfio_find_device() helper Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 09/27] vfio: Refactor vfio_device_fops_cdev_open() Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 10/27] vfio: Add API to open cdev device for Live Update restore Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 11/27] vfio/pci: Retrieve preserved device files after Live Update Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 12/27] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 13/27] vfio/pci: Protect incoming preserved devices until they finish Live Update Vipin Sharma
2026-11-02 15:36 ` Vipin Sharma [this message]
2026-11-02 15:36 ` [PATCH v6 15/27] docs: liveupdate: Add documentation for VFIO PCI Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 16/27] vfio: selftests: Build liveupdate library in VFIO selftests Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 17/27] vfio: selftests: Add vfio_pci_liveupdate_uapi_test Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 18/27] vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 19/27] vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 20/27] vfio: selftests: Add iommufd noiommu mode Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 21/27] vfio: selftests: Add helpers to map DMA memory at IOVA == PA in " Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 22/27] vfio: selftests: Add helpers for Live Update DMA memory and sysfs state Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 23/27] vfio: selftests: Add an attach() driver op Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 24/27] vfio: selftests: Test preserving noiommu devices in vfio_pci_liveupdate_uapi_test Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 25/27] vfio: selftests: Add vfio_pci_liveupdate_kexec_test Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 26/27] vfio: selftests: Verify that opening VFIO device fails during Live Update Vipin Sharma
2026-11-02 15:36 ` [PATCH v6 27/27] vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test Vipin Sharma
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261102153656.1431701-15-vipinsh@google.com \
--to=vipinsh@google.com \
--cc=ajayachandra@nvidia.com \
--cc=alex@shazbot.org \
--cc=amastro@fb.com \
--cc=ankita@nvidia.com \
--cc=apopple@nvidia.com \
--cc=bhelgaas@google.com \
--cc=chrisl@kernel.org \
--cc=christian.koenig@amd.com \
--cc=corbet@lwn.net \
--cc=dmatlack@google.com \
--cc=graf@amazon.com \
--cc=jacob.pan@linux.microsoft.com \
--cc=jgg@nvidia.com \
--cc=jgg@ziepe.ca \
--cc=jrhilke@google.com \
--cc=julianr@linux.ibm.com \
--cc=kees@kernel.org \
--cc=kevin.tian@intel.com \
--cc=kexec@lists.infradead.org \
--cc=kvm@vger.kernel.org \
--cc=leon@kernel.org \
--cc=leonro@nvidia.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-pci@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=mattev@meta.com \
--cc=michal.winiarski@intel.com \
--cc=parav@nvidia.com \
--cc=pasha.tatashin@soleen.com \
--cc=praan@google.com \
--cc=pratyush@kernel.org \
--cc=rananta@google.com \
--cc=rdunlap@infradead.org \
--cc=rientjes@google.com \
--cc=rodrigo.vivi@intel.com \
--cc=rppt@kernel.org \
--cc=saeedm@nvidia.com \
--cc=schnelle@linux.ibm.com \
--cc=skhan@linuxfoundation.org \
--cc=skhawaja@google.com \
--cc=vivek.kasireddy@intel.com \
--cc=witu@nvidia.com \
--cc=yanjun.zhu@linux.dev \
--cc=yi.l.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®