vfio/pci: Base Live Update support for VFIO
Subsystems (8)
v4 cover vfio/pci: Base Live Update support for VFIO
Hello,
This series can be found on Github:
https://github.com/shvipin/linux vfio/liveupdate/v4
It adds the base support to enable Live Update feature for VFIO devices.
In v3 of this series, both core PCI subsystem and VFIO changes were together. Based on the feedbacks on the patches, increasing complexities and the overall number of patches, we have split the effort in two parts, PCI and VFIO.
v4 version of PCI work is here:
https://lore.kernel.org/linux-pci/[email protected]/
This series is the VFIO part, built on top of PCI v4. Going forward, work on both subsystem will be independent and we may not be making same version releases in sync. However, VFIO changes will always be on the top of some version of PCI changes.
Overview
--------
This series made following changes to support VFIO live update:
1. VFIO Live Update feature is put behind VFIO_PCI_LIVEUPDATE config option. It also requires that VFIO_DEVICE_CDEV config is enabled.
1. To keep things simple in this series, enabling this option will disable VFIO_PCI_DMABUF. This is temporary.
1. A VFIO cdev FD can be preserved via Live Update session and retrieved after kexec via the same session.
2. Getting the preserved device FD after kexec via legacy VFIO group APIs or by directly opening /dev/vfio/devices/vfioX will error out. User has to get the FD from Live Update subsystem.
3. For now, only FD is preserved, underlying device and its states are not.
4. To keep the next kernel safe, just before the kexec, VFIO resets the device, disables BME and restores the original state of the device. This is temporary and will be removed when we have added full support of the Live Update.
5. New VFIO selftests have been added which interacts with VFIO
- vfio_pci_liveupdate_uapi_test
- vfio_pci_liveupdate_kexec_test
liveupdate.
6. Documentations have been added for kernel docs.
Dependencies
------------
This series has following dependencies as shown in the diagram
|---------------------------|
| VFIO v4 |
|---------------------------|
| Liveupdate Selftests[1] |
|---------------------------|
| PCI v4 [2] |
|---------------------------|
| Liveupdate Refcount [3] |
|---------------------------|[1] https://lore.kernel.org/all/[email protected]/
These selftests changes are needed by other selftests also (iommu, kvm), so, I have sent them separately.
[2] https://lore.kernel.org/linux-pci/[email protected]/
This is the PCI v4, which adds the live update support in PCI core.
[3] https://lore.kernel.org/lkml/[email protected]/
PCI v4 is dependent on this.
Github branch I have mentioned at the top has all of these dependencies.
Testing
-------
Series has been tested using new VFIO live update selftests in both qemu and bare-metal environment. In bare-metal environment, it was ran using the Intel DSA PCIe device.
vfio_pci_liveupdate_kexec_test test is executed as following
$ ./setup.sh 0000:00:04.0 $ ./vfio_pci_liveupdate_kexec_test --stage 1 0000:6a:01.0 $ kexec --reuse-cmdline -l -s /boot/vmlinuz $ kexec -e $ ./setup.sh 0000:00:04.0 $ ./vfio_pci_liveupdate_kexec_test --stage 2 0000:6a:01.0
Kernel commandline used for booting the VM and testing had following options passed
- liveupdate=on
- kho=on
- vfio_iommu_type1.allow_unsafe_interrupts=Y
- vfio_pci.enable_sriov=Y
- vfio_pci.disable_denylist=Y
- vfio.enable_unsafe_noiommu_mode=Y
Changelog
---------
v4:
- Addressed sashiko feedback regarding issues in resetting the devize in freeze
- Dropped the patch which was skipping reset of preserved VFIO device after kexec when user opens the device. This will be added back in future but for now not needed as freeze() is also resetting the device before kexec.
- Kernel doc and comment updates based on feedbacks.
v3: https://lore.kernel.org/kvm/[email protected]/
v2: https://lore.kernel.org/kvm/[email protected]/
v1: https://lore.kernel.org/kvm/[email protected]/
rfc: https://lore.kernel.org/kvm/[email protected]/
David Matlack (10):
vfio/pci: Notify PCI subsystem about devices preserved across Live
Update
vfio: Enforce preserved devices are retrieved via
LIVEUPDATE_SESSION_RETRIEVE_FD
vfio/pci: Store incoming Live Update state in struct
vfio_pci_core_device
docs: liveupdate: Add documentation for VFIO PCI
vfio: selftests: Add vfio_pci_liveupdate_uapi_test
vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED
vfio: selftests: Expose iommu_modes to tests
vfio: selftests: Expose low-level helper routines for setting up
struct vfio_pci_device
vfio: selftests: Verify that opening VFIO device fails during Live
Update
vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test
Vipin Sharma (6):
vfio/pci: Register a file handler with Live Update Orchestrator vfio/pci: Preserve vfio-pci device files across Live Update vfio/pci: Retrieve preserved device files after Live Update
vfio: selftests: Build liveupdate library in VFIO selftests
vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD
vfio: selftests: Add vfio_pci_liveupdate_kexec_test
Documentation/core-api/liveupdate.rst | 1 + .../driver-api/vfio_pci_liveupdate.rst | 23 ++ MAINTAINERS | 2 + drivers/vfio/device_cdev.c | 67 +++- drivers/vfio/group.c | 9 + drivers/vfio/pci/Kconfig | 16 +- drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/vfio_pci.c | 13 +- drivers/vfio/pci/vfio_pci_core.c | 62 ++-- drivers/vfio/pci/vfio_pci_liveupdate.c | 349 ++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 19 + drivers/vfio/vfio.h | 18 + drivers/vfio/vfio_main.c | 16 +- include/linux/kho/abi/vfio_pci.h | 43 +++ include/linux/vfio.h | 13 + include/linux/vfio_pci_core.h | 2 + tools/testing/selftests/vfio/Makefile | 23 +- .../vfio/lib/include/libvfio/iommu.h | 2 + .../lib/include/libvfio/vfio_pci_device.h | 8 + tools/testing/selftests/vfio/lib/iommu.c | 4 +- .../selftests/vfio/lib/vfio_pci_device.c | 60 ++- .../vfio/vfio_pci_liveupdate_kexec_test.c | 256 +++++++++++++ .../vfio/vfio_pci_liveupdate_uapi_test.c | 93 +++++ 23 files changed, 1041 insertions(+), 59 deletions(-) create mode 100644 Documentation/driver-api/vfio_pci_liveupdate.rst create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c create mode 100644 include/linux/kho/abi/vfio_pci.h create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c
base-commit: 2a4c0c11c0193889446cdb6f1540cc2b9aff97dd
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 1/16 vfio/pci: Register a file handler with Live Update Orchestrator
Register a live update file handler for vfio-pci device files. Add stub implementations of all required callbacks so that registration does not fail (i.e. to avoid breaking git-bisect).
This file handler will be extended in subsequent commits to enable a device bound to vfio-pci to run without interruption while the host is going through a kexec Live Update.
Put this support behind a new Kconfig VFIO_PCI_LIVEUPDATE that is marked experimental and default-disabled until more of the device preservation support has landed in the kernel.
--- MAINTAINERS | 1 + drivers/vfio/pci/Kconfig | 12 +++++ drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/vfio_pci.c | 11 +++- drivers/vfio/pci/vfio_pci_liveupdate.c | 70 ++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 14 ++++++ include/linux/kho/abi/vfio_pci.h | 28 +++++++++++ 7 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c create mode 100644 include/linux/kho/abi/vfio_pci.h
MAINTAINERS +1 -0
@@ -27980,6 +27980,7 @@ F: Documentation/ABI/testing/debugfs-vfio F: Documentation/ABI/testing/sysfs-devices-vfio-dev F: Documentation/driver-api/vfio.rst F: drivers/vfio/ +F: include/linux/kho/abi/vfio_pci.h F: include/linux/vfio.h F: include/linux/vfio_pci_core.h F: include/uapi/linux/vfio.h
drivers/vfio/pci/Kconfig +12 -0
@@ -42,6 +42,18 @@ config VFIO_PCI_IGD and LPC bridge config space. To enable Intel IGD assignment through vfio-pci, say Y. + +config VFIO_PCI_LIVEUPDATE + bool "VFIO PCI support for Live Update (EXPERIMENTAL)" + depends on PCI_LIVEUPDATE + help + Support for preserving devices bound to vfio-pci across a Live + Update. This option should only be enabled by developers working on + implementing this support. Once enough support has landed in the + kernel, this option will no longer be marked EXPERIMENTAL. + + If you don't know what to do here, say N. + endif config VFIO_PCI_ZDEV_KVM
drivers/vfio/pci/Makefile +1 -0
@@ -7,6 +7,7 @@ obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o vfio-pci-y := vfio_pci.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o +vfio-pci-$(CONFIG_VFIO_PCI_LIVEUPDATE) += vfio_pci_liveupdate.o obj-$(CONFIG_VFIO_PCI) += vfio-pci.o obj-$(CONFIG_MLX5_VFIO_PCI) += mlx5/
drivers/vfio/pci/vfio_pci.c +10 -1
@@ -264,10 +264,14 @@ static int __init vfio_pci_init(void)vfio_pci_core_set_params (nointxmask, is_disable_vga, disable_idle_d3); + ret =vfio_pci_liveupdate_init (); +if (ret) +return ret; +/* Register and scan for devices */ ret =pci_register_driver (&vfio_pci_driver);if (ret) -return ret; + goto err_liveupdate_cleanup;vfio_pci_fill_ids ();
@@ -275,12 +279,17 @@ static int __init vfio_pci_init(void)pr_warn ("device denylist disabled.\n" );return 0 ; + +err_liveupdate_cleanup: +vfio_pci_liveupdate_cleanup (); +return ret; }module_init (vfio_pci_init); static void __exit vfio_pci_cleanup(void) {pci_unregister_driver (&vfio_pci_driver); +vfio_pci_liveupdate_cleanup (); }module_exit (vfio_pci_cleanup);
drivers/vfio/pci/vfio_pci_liveupdate.c new file +70 -0
70 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +14 -0
@@ -137,4 +137,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, }#endif +#ifdef CONFIG_VFIO_PCI_LIVEUPDATE +int __init vfio_pci_liveupdate_init (void ); +void vfio_pci_liveupdate_cleanup (void ); +#else +static inline int vfio_pci_liveupdate_init (void ) +{ +return 0 ; +} + +static inline void vfio_pci_liveupdate_cleanup (void ) +{ +} +#endif /* CONFIG_VFIO_PCI_LIVEUPDATE */ +#endif
include/linux/kho/abi/vfio_pci.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* +* Copyright (c) 2026, Google LLC. +* Vipin Sharma <[email protected]> +* David Matlack <[email protected]> +*/ + +#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H +#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H + +/** +* DOC: VFIO PCI Live Update ABI +* +* VFIO uses the ABI defined below for preserving device files across a kexec +* reboot using LUO. +* +* Device metadata is serialized into memory which is then handed to the next +* kernel via KHO. +* +* This interface is a contract. Any modification to any of the serialization +* structs defined here constitutes a breaking change. Such changes require +* incrementing the version number in the VFIO_PCI_LUO_FH_COMPATIBLE string. +*/ + +#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" + +#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 01/16] vfio/pci: Register a file handler with Live Update Orchestrator
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The Kconfig symbol VFIO_PCI_LIVEUPDATE depends on a non-existent symbol PCI_LIVEUPDATE. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The Kconfig symbol VFIO_PCI_LIVEUPDATE depends on a non-existent symbol PCI_LIVEUPDATE. --
commit 0dbf63e191b4b1a239db36bedc83313305585f3a
Author: Vipin Sharma <[email protected]>
vfio/pci: Register a file handler with Live Update Orchestrator
This commit registers a live update file handler for vfio-pci device files with stub implementations. It also adds a new experimental Kconfig option VFIO_PCI_LIVEUPDATE to gate this support.
[ ... ]
drivers/vfio/pci/Kconfig config VFIO_PCI_IGD · line 48
Quoted 12 lines
drivers/vfio/pci/Kconfig +4
-0@@ -42,6 +42,18 @@ config VFIO_PCI_IGD and LPC bridge config space. To enable Intel IGD assignment through vfio-pci, say Y. + +config VFIO_PCI_LIVEUPDATE + bool "VFIO PCI support for Live Update (EXPERIMENTAL)" + depends on PCI_LIVEUPDATE
Does PCI_LIVEUPDATE exist in the kernel tree?
Referencing a non-existent config symbol in a depends on clause means the dependency can never be satisfied. As a result, VFIO_PCI_LIVEUPDATE can never be enabled.
Was the intended dependency LIVEUPDATE or something similar?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 2/16 vfio/pci: Preserve vfio-pci device files across Live Update
In reply to Vipin SharmaImplement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update.
Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files).
State about each device is serialized into a new ABI struct vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct and the Live Update Orchestrator (LUO) to preserve the physical address of the struct.
For now the only contents of struct vfio_pci_core_device_ser the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update.
Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel.
Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer.
Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device until userspace has unpreserved the file using LUO.
--- drivers/vfio/pci/Kconfig | 6 +- drivers/vfio/pci/vfio_pci.c | 2 +- drivers/vfio/pci/vfio_pci_core.c | 60 +++++---- drivers/vfio/pci/vfio_pci_liveupdate.c | 178 ++++++++++++++++++++++++- drivers/vfio/pci/vfio_pci_priv.h | 5 + drivers/vfio/vfio_main.c | 3 +- include/linux/kho/abi/vfio_pci.h | 15 +++ include/linux/vfio.h | 2 + 8 files changed, 242 insertions(+), 29 deletions(-)
drivers/vfio/pci/Kconfig +4 -2
@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif
@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y. config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATE source "drivers/vfio/pci/mlx5/Kconfig"
drivers/vfio/pci/vfio_pci.c +1 -1
@@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)return 0 ; } -static const struct vfio_device_ops vfio_pci_ops = { +const struct vfio_device_ops vfio_pci_ops = { .name ="vfio-pci" , .init = vfio_pci_core_init_dev, .release = vfio_pci_core_release_dev,
drivers/vfio/pci/vfio_pci_core.c +37 -23
86 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_liveupdate.c +176 -2
210 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +5 -0
@@ -11,6 +11,10 @@/* Cap maximum number of ioeventfds per device (arbitrary) */ #define VFIO_PCI_IOEVENTFD_MAX 1000 +extern const struct vfio_device_ops vfio_pci_ops; + +void vfio_pci_core_try_reset (struct vfio_pci_core_device *vdev); +struct vfio_pci_ioeventfd {struct list_head next;struct vfio_pci_core_device *vdev;
@@ -72,6 +76,7 @@ void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev);void vfio_pci_memory_unlock_and_restore (struct vfio_pci_core_device *vdev,u16 cmd); +void vfio_pci_zap_bars (struct vfio_pci_core_device *vdev);#ifdef CONFIG_VFIO_PCI_IGD bool vfio_pci_is_intel_display (struct pci_dev *pdev);
drivers/vfio/vfio_main.c +2 -1
@@ -1457,7 +1457,7 @@ const struct file_operations vfio_device_fops = {#endif }; -static struct vfio_device *vfio_device_from_file (struct file *file) +struct vfio_device *vfio_device_from_file (struct file *file) {struct vfio_device_file *df = file->private_data;
@@ -1465,6 +1465,7 @@ static struct vfio_device *vfio_device_from_file(struct file *file)return NULL ;return df->device; } +EXPORT_SYMBOL_GPL (vfio_device_from_file);/** * vfio_file_is_valid - True if the file is valid vfio file
include/linux/kho/abi/vfio_pci.h +15 -0
@@ -9,6 +9,9 @@#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H +#include <linux/compiler.h> +#include <linux/types.h> +/** * DOC: VFIO PCI Live Update ABI *
@@ -25,4 +28,16 @@#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1" +/** +* struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI +* device. +* +* @domain: The device's PCI domain number (segment). +* @bdf: The device's PCI bus, device, and function number. +*/ +struct vfio_pci_core_device_ser { +u32 domain; +u16 bdf; +} __packed; +#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio.h +2 -0
@@ -81,6 +81,8 @@ struct vfio_device {struct dentry *debug_root; }; +struct vfio_device *vfio_device_from_file (struct file *file); +/** * struct vfio_device_ops - VFIO bus driver device callbacks *
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
David Matlack
maintainerRe: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
Quoted 22 lines · Vipin Sharma
drivers/vfio/pci/Kconfig +3
-1@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif
drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 73
Quoted 5 lines
drivers/vfio/pci/Kconfig +1
-1@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y. config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATE
Why does enabling VFIO_PCI_LIVEUPDATE require disabling VFIO_PCI_DMABUF? I saw the cover letter says "to keep things simple", but what specific problem does this solve or simplify?
Re: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
Quoted 26 lines · David Matlack
Quoted 24 lines · Vipin Sharma
drivers/vfio/pci/Kconfig +3
-1@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y.
Quoted 7 lines
drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 73
Quoted 3 lines
drivers/vfio/pci/Kconfig +1
-1config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATEWhy does enabling
VFIO_PCI_LIVEUPDATErequire disablingVFIO_PCI_DMABUF? I saw the cover letter says "to keep things simple", but what specific problem does this solve or simplify?
I should have provided more details there.
When device is getting reset in vfio_pci_liveupdate_freeze(), we are zapping userspace mapped bars, we also need to use vfio_pci_dma_buf_move() to revoke dma buffer access or vfio_pci_dma_buf_cleanup() combination. Cleanup takes the memory lock which freeze already takes, and there are some refcounts which are managed in both of these APIs. This was causing complexities with code flow based on result of pci_load_saved_state(). All this was adding more refactoring than I wanted in the series.
I decided to just drop the change and disable the support of DMA Buffer for now to keep number of patches less in the series. This will go away once we remove reset condition in freeze.
Re: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
Quoted 37 lines · Vipin Sharma
Quoted 26 lines · David Matlack
Quoted 24 lines · Vipin Sharma
drivers/vfio/pci/Kconfig +3
-1@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y.Quoted 7 lines
drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 73
Quoted 3 lines
drivers/vfio/pci/Kconfig +1
-1config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATEWhy does enabling
VFIO_PCI_LIVEUPDATErequire disablingVFIO_PCI_DMABUF? I saw the cover letter says "to keep things simple", but what specific problem does this solve or simplify?I should have provided more details there.
Quoted 8 lines
When device is getting reset in
vfio_pci_liveupdate_freeze(), we are zapping userspace mapped bars, we also need to usevfio_pci_dma_buf_move()to revoke dma buffer access orvfio_pci_dma_buf_cleanup()combination. Cleanup takes the memory lock which freeze already takes, and there are some refcounts which are managed in both of these APIs. This was causing complexities with code flow based on result ofpci_load_saved_state(). All this was adding more refactoring than I wanted in the series.
Maybe we can return -EOPNOTSUPP if any dmabufs for this vfio cdev are exported during preserve?
Quoted 4 lines
I decided to just drop the change and disable the support of DMA Buffer for now to keep number of patches less in the series. This will go away once we remove reset condition in freeze.
Pratyush Yadav
maintainerRe: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
Quoted 47 lines · Samiullah Khawaja
Quoted 37 lines · Vipin Sharma
Quoted 26 lines · David Matlack
Quoted 24 lines · Vipin Sharma
drivers/vfio/pci/Kconfig +3
-1@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y.Quoted 7 lines
drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 73
Quoted 3 lines
drivers/vfio/pci/Kconfig +1
-1config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATEWhy does enabling
VFIO_PCI_LIVEUPDATErequire disablingVFIO_PCI_DMABUF? I saw the cover letter says "to keep things simple", but what specific problem does this solve or simplify?I should have provided more details there.
Quoted 8 lines
When device is getting reset in
vfio_pci_liveupdate_freeze(), we are zapping userspace mapped bars, we also need to usevfio_pci_dma_buf_move()to revoke dma buffer access orvfio_pci_dma_buf_cleanup()combination. Cleanup takes the memory lock which freeze already takes, and there are some refcounts which are managed in both of these APIs. This was causing complexities with code flow based on result ofpci_load_saved_state(). All this was adding more refactoring than I wanted in the series.
Quoted 2 lines
Maybe we can return
-EOPNOTSUPPif any dmabufs for this vfio cdev are exported during preserve?
Whichever way you go with, a TODO/comment would be nice to have so someone (including future you) looking at this code knows why this restriction exists.
Signature · 2 lines
Regards, Pratyush Yadav
Re: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
Quoted 49 lines · Pratyush Yadav
Quoted 47 lines · Samiullah Khawaja
Quoted 45 lines · Vipin Sharma
Quoted 26 lines · David Matlack
Quoted 24 lines · Vipin Sharma
drivers/vfio/pci/Kconfig +3
-1@@ -45,13 +45,15 @@ config VFIO_PCI_IGD config VFIO_PCI_LIVEUPDATE bool "VFIO PCI support for Live Update (EXPERIMENTAL)" - depends on PCI_LIVEUPDATE + depends on PCI_LIVEUPDATE && VFIO_DEVICE_CDEV help Support for preserving devices bound to vfio-pci across a Live Update. This option should only be enabled by developers working on implementing this support. Once enough support has landed in the kernel, this option will no longer be marked EXPERIMENTAL. + Enabling this will disable support for VFIO PCI DMA buffer. + If you don't know what to do here, say N. endif@@ -68,7 +70,7 @@ config VFIO_PCI_ZDEV_KVM To enable s390x KVM vfio-pci extensions, say Y.Quoted 7 lines
drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 73
Quoted 3 lines
drivers/vfio/pci/Kconfig +1
-1config VFIO_PCI_DMABUF - def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER + def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER && !VFIO_PCI_LIVEUPDATEWhy does enabling
VFIO_PCI_LIVEUPDATErequire disablingVFIO_PCI_DMABUF? I saw the cover letter says "to keep things simple", but what specific problem does this solve or simplify?I should have provided more details there.
When device is getting reset in
vfio_pci_liveupdate_freeze(), we are zapping userspace mapped bars, we also need to usevfio_pci_dma_buf_move()to revoke dma buffer access orvfio_pci_dma_buf_cleanup()combination. Cleanup takes the memory lock which freeze already takes, and there are some refcounts which are managed in both of these APIs. This was causing complexities with code flow based on result ofpci_load_saved_state(). All this was adding more refactoring than I wanted in the series.
Quoted 2 lines
Quoted 2 lines
Maybe we can return
-EOPNOTSUPPif any dmabufs for this vfio cdev are exported during preserve?
Currently, no APIs are present to fetch if dmabufs are exported or not. I will add one patch to this series to return EOPNOTSUPP and remove condition from the config.
Quoted 5 lines
Whichever way you go with, a TODO/comment would be nice to have so someone (including future you) looking at this code knows why this restriction exists.
I will add comment in the next version.
[email protected]
AI reviewRe: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
In reply to Vipin SharmaThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] Time-of-Check to Time-of-Use (TOCTOU) race condition when validating `vdev->irq_type`. - [High]…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] Time-of-Check to Time-of-Use (TOCTOU) race condition when validating vdev->irq_type.
- [High] Post-freeze access vulnerability allows userspace to re-activate the device.
- [High] Silent failure of vfio_pci_core_try_reset() leaves the device in an inconsistent state.
- [Medium] Missing kho_is_enabled() check before calling KHO preservation APIs.
- [Medium] Missing INTx disable before device reset may cause spurious interrupts. --
commit 8744f868d8d8c3c92253e4db7b371be095529dce
Author: Vipin Sharma <[email protected]>
vfio/pci: Preserve vfio-pci device files across Live Update
Implement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. State about each device is serialized into a new ABI struct vfio_pci_core_device_ser and preserved using KHO and LUO.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0
-0
[ ... ]
drivers/vfio/pci/vfio_pci_liveupdate.c line 141
Quoted 12 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10
-1+static int vfio_pci_liveupdate_preserve (struct liveupdate_file_op_args *args) { -return -EOPNOTSUPP ; +struct vfio_device *device =vfio_device_from_file (args->file); +struct vfio_pci_core_device_ser *ser; +struct vfio_pci_core_device *vdev; +struct pci_dev *pdev; + + vdev = container_of(device, struct vfio_pci_core_device, vdev); + pdev = vdev->pdev; + + ser =kho_alloc_preserve (sizeof (*ser));
Is it safe to call kho_alloc_preserve() without first checking if the KHO subsystem is enabled via kho_is_enabled()?
Unconditionally allocating and preserving memory might add tracking state to folios that will never be utilized if the subsystem is disabled.
[ ... ]
Quoted 8 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +8
-0+static int vfio_pci_liveupdate_freeze (struct liveupdate_file_op_args *args) +{ +struct vfio_device *device =vfio_device_from_file (args->file); +struct vfio_pci_core_device *vdev; +struct pci_dev *pdev; +int ret; + + vdev = container_of(device, struct vfio_pci_core_device, vdev);
drivers/vfio/pci/vfio_pci_liveupdate.c line 174
Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10
-0+ pdev = vdev->pdev; + +guard (mutex)(&device->dev_set->lock); + +/* +* Userspace must disable interrupts on the device prior to freeze so +* that the device does not send any interrupts until new interrupt +* handlers have been established by the next kernel. +*/ +if (vdev->irq_type !=VFIO_PCI_NUM_IRQS ) {
Does this check need to hold vdev->igate?
Without the mutex, could a concurrent ioctl() such as VFIO_DEVICE_SET_IRQS modify the irq_type immediately after this check passes, allowing the device to generate interrupts during kexec?
Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +5
-0+pci_err (pdev,"Freeze failed! Interrupts are still enabled.\n" ); +return -EINVAL ; + } + +guard (rwsem_write)(&vdev->memory_lock);
[ ... ]
drivers/vfio/pci/vfio_pci_liveupdate.c line 200
Quoted 11 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +11
-0+/* +* Reset is a temporary measure to provide kernel after kexec a clean +* device while VFIO live update work is under development and not +* fully supported. It will go away once continuous DMA support is +* added to device preservation. +*/ +vfio_pci_zap_bars (vdev); + ret =pci_load_saved_state (pdev, vdev->pci_saved_state); +if (ret) +return ret; +pci_clear_master (pdev);
Does the PCI_COMMAND register need to have PCI_COMMAND_INTX_DISABLE explicitly written prior to calling vfio_pci_core_try_reset()?
I noticed that the standard disable path in vfio_pci_core_disable() does this to prevent spurious interrupts from firing during the hardware reset.
drivers/vfio/pci/vfio_pci_liveupdate.c
Quoted 1 line
drivers/vfio/pci/vfio_pci_liveupdate.c +1
-0+vfio_pci_core_try_reset (vdev);
If vfio_pci_core_try_reset() silently fails because it cannot acquire the PCI locks or because the device does not support reset, the kexec will still proceed.
Could this allow the next kernel to inherit a device that still has active DMA engines or internal state?
drivers/vfio/pci/vfio_pci_liveupdate.c
Quoted 3 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +3
-0+pci_restore_state (pdev); +return 0 ; +}
Once vfio_pci_liveupdate_freeze() completes and releases the locks, what prevents userspace threads from issuing new write(), mmap(), or ioctl() calls on the device file descriptor?
Because kexec doesn't freeze userspace tasks, could a concurrent thread re-enable DMA and interrupts on the device right before the kexec jump, risking memory corruption in the successor kernel?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
David Matlack
maintainerRe: [PATCH v4 02/16] vfio/pci: Preserve vfio-pci device files across Live Update
In reply to Vipin SharmaQuoted 35 lines · Vipin Sharma
drivers/vfio/pci/vfio_pci_liveupdate.c +35
-0+static int vfio_pci_liveupdate_freeze (struct liveupdate_file_op_args *args) +{ +struct vfio_device *device =vfio_device_from_file (args->file); +struct vfio_pci_core_device *vdev; +struct pci_dev *pdev; +int ret; + + vdev = container_of(device, struct vfio_pci_core_device, vdev); + pdev = vdev->pdev; + +guard (mutex)(&device->dev_set->lock); + +/* +* Userspace must disable interrupts on the device prior to freeze so +* that the device does not send any interrupts until new interrupt +* handlers have been established by the next kernel. +*/ +if (vdev->irq_type !=VFIO_PCI_NUM_IRQS ) { +pci_err (pdev,"Freeze failed! Interrupts are still enabled.\n" ); +return -EINVAL ; + } + +guard (rwsem_write)(&vdev->memory_lock); + +/* +* Userspace must make sure device is not in the lower power state for +* live update. We may relax this in future. +*/ +if (pdev->current_state !=PCI_D0 ) { +pci_err (pdev,"Freeze failed! Device not in D0 state.\n" ); +return -EINVAL ; + } + +/* +* Reset is a temporary measure to provide kernel after kexec a clean
drivers/vfio/pci/vfio_pci_liveupdate.c line 201
Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10
-0+* device while VFIO live update work is under development and not +* fully supported. It will go away once continuous DMA support is +* added to device preservation. +*/ +vfio_pci_zap_bars (vdev); + ret =pci_load_saved_state (pdev, vdev->pci_saved_state); +if (ret) +return ret; +pci_clear_master (pdev); +vfio_pci_core_try_reset (vdev);
I am seeing the following lockdep splat get triggered by this reset when testing with this commit using vfio_pci_liveupdate_kexec_test. It seems to be related to taking memory_lock above.
[ 2710.299017][T75672] ======================================================
[ 2710.305908][T75672] WARNING: possible circular locking dependency detected
[ 2710.312797][T75672] 7.1.0-dbg-DEV #59 Tainted: G S
[ 2710.319077][T75672] ------------------------------------------------------
[ 2710.325967][T75672] kexec/75672 is trying to acquire lock:
[ 2710.331474][T75672] ff46fd4fdbaeef08 (&group->mutex){+.+.}-{4:4}, at: pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.341336][T75672]
[ 2710.341336][T75672] but task is already holding lock:
[ 2710.348574][T75672] ff46fd501f9a19a8 (&vdev->memory_lock){++++}-{4:4}, at: vfio_pci_liveupdate_freeze+0x51/0x100
[ 2710.358764][T75672]
[ 2710.358764][T75672] which lock already depends on the new lock.
[ 2710.358764][T75672]
[ 2710.369031][T75672]
[ 2710.369031][T75672] the existing dependency chain (in reverse order) is:
[ 2710.377916][T75672]
[ 2710.377916][T75672] -> #4 (&vdev->memory_lock){++++}-{4:4}:
[ 2710.385675][T75672] down_read+0x3d/0x150
[ 2710.390235][T75672] vfio_pci_mmap_huge_fault+0xb9/0x160
[ 2710.396091][T75672] __do_fault+0x46/0x140
[ 2710.400734][T75672] do_pte_missing+0x4c3/0xff0
[ 2710.405803][T75672] handle_mm_fault+0x7c4/0xb30
[ 2710.410961][T75672] fixup_user_fault+0x115/0x270
[ 2710.416209][T75672] vaddr_get_pfns+0x1a1/0x390
[ 2710.421286][T75672] vfio_pin_pages_remote+0x148/0x4d0
[ 2710.426959][T75672] vfio_pin_map_dma+0xcc/0x260
[ 2710.432116][T75672] vfio_iommu_type1_ioctl+0xda4/0xec0
[ 2710.437884][T75672] __se_sys_ioctl+0x71/0xc0
[ 2710.442790][T75672] do_syscall_64+0x15f/0x710
[ 2710.447788][T75672] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.454074][T75672]
[ 2710.454074][T75672] -> #3 (&mm->mmap_lock){++++}-{4:4}:
[ 2710.461489][T75672] down_read_killable+0x48/0x180
[ 2710.466821][T75672] mmap_read_lock_killable+0x12/0x50
[ 2710.472505][T75672] lock_mm_and_find_vma+0x11d/0x130
[ 2710.478093][T75672] do_user_addr_fault+0x3a0/0x6c0
[ 2710.483521][T75672] exc_page_fault+0x68/0xa0
[ 2710.488423][T75672] asm_exc_page_fault+0x26/0x30
[ 2710.493669][T75672] filldir+0xe2/0x190
[ 2710.498047][T75672] ext4_readdir+0xb47/0xcf0
[ 2710.502950][T75672] iterate_dir+0x84/0x160
[ 2710.507677][T75672] __se_sys_getdents+0x74/0x120
[ 2710.512929][T75672] do_syscall_64+0x15f/0x710
[ 2710.517919][T75672] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.524202][T75672]
[ 2710.524202][T75672] -> #2 (&type->i_mutex_dir_key#4){++++}-{4:4}:
[ 2710.532478][T75672] down_read+0x3d/0x150
[ 2710.537030][T75672] lookup_slow+0x26/0x50
[ 2710.541675][T75672] link_path_walk+0x42c/0x580
[ 2710.546743][T75672] path_openat+0xd1/0xde0
[ 2710.551466][T75672] do_file_open_root+0x114/0x250
[ 2710.556798][T75672] file_open_root+0x89/0xb0
[ 2710.561703][T75672] kernel_read_file_from_path_initns+0xba/0x130
[ 2710.568342][T75672] _request_firmware+0x4ab/0x8c0
[ 2710.573677][T75672] request_firmware_direct+0x36/0x50
[ 2710.579356][T75672] request_microcode_fw+0xf2/0x510
[ 2710.584869][T75672] reload_store+0x197/0x230
[ 2710.589766][T75672] kernfs_fop_write_iter+0x13f/0x1d0
[ 2710.595452][T75672] vfs_write+0x2be/0x3b0
[ 2710.600097][T75672] ksys_write+0x73/0x100
[ 2710.604735][T75672] do_syscall_64+0x15f/0x710
[ 2710.609723][T75672] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.616009][T75672]
[ 2710.616009][T75672] -> #1 (cpu_hotplug_lock){++++}-{0:0}:
[ 2710.623591][T75672] cpus_read_lock+0x3b/0xd0
[ 2710.628499][T75672] __cpuhp_state_add_instance+0x19/0x40
[ 2710.634443][T75672] iova_domain_init_rcaches+0x1ef/0x230
[ 2710.640385][T75672] iommu_setup_dma_ops+0x175/0x540
[ 2710.645891][T75672] iommu_device_register+0x188/0x220
[ 2710.651564][T75672] intel_iommu_init+0x35a/0x440
[ 2710.656811][T75672] pci_iommu_init+0x16/0x40
[ 2710.661713][T75672] do_one_initcall+0xf5/0x3a0
[ 2710.666786][T75672] do_initcall_level+0x82/0xa0
[ 2710.671953][T75672] do_initcalls+0x43/0x70
[ 2710.676672][T75672] kernel_init_freeable+0x152/0x1d0
[ 2710.682266][T75672] kernel_init+0x1a/0x130
[ 2710.686996][T75672] ret_from_fork+0x16b/0x310
[ 2710.691991][T75672] ret_from_fork_asm+0x1a/0x30
[ 2710.697151][T75672]
[ 2710.697151][T75672] -> #0 (&group->mutex){+.+.}-{4:4}:
[ 2710.704478][T75672] __lock_acquire+0x14c6/0x2800
[ 2710.709729][T75672] lock_acquire+0xd3/0x2c0
[ 2710.714542][T75672] __mutex_lock+0x8f/0xcd0
[ 2710.719349][T75672] pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.725461][T75672] pcie_flr+0x32/0xc0
[ 2710.729842][T75672] __pci_reset_function_locked+0x84/0x120
[ 2710.735954][T75672] vfio_pci_core_try_reset+0x96/0xe0
[ 2710.741630][T75672] vfio_pci_liveupdate_freeze+0x89/0x100
[ 2710.747653][T75672] luo_file_freeze+0xba/0x280
[ 2710.752725][T75672] luo_session_serialize+0x69/0x190
[ 2710.758321][T75672] liveupdate_reboot+0x19/0x30
[ 2710.763490][T75672] kernel_kexec+0x2f/0xa0
[ 2710.768220][T75672] __se_sys_reboot+0xfd/0x210
[ 2710.773301][T75672] do_syscall_64+0x15f/0x710
[ 2710.778284][T75672] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.784568][T75672]
[ 2710.784568][T75672] other info that might help us debug this:
[ 2710.784568][T75672]
[ 2710.794663][T75672] Chain exists of:
[ 2710.794663][T75672] &group->mutex --> &mm->mmap_lock --> &vdev->memory_lock
[ 2710.794663][T75672]
[ 2710.807543][T75672] Possible unsafe locking scenario:
[ 2710.807543][T75672]
[ 2710.814863][T75672] CPU0 CPU1
[ 2710.820106][T75672] ---- ----
[ 2710.825352][T75672] lock(&vdev->memory_lock);
[ 2710.829904][T75672] lock(&mm->mmap_lock);
[ 2710.836620][T75672] lock(&vdev->memory_lock);
[ 2710.843682][T75672] lock(&group->mutex);
[ 2710.847798][T75672]
[ 2710.847798][T75672] *** DEADLOCK ***
[ 2710.847798][T75672]
[ 2710.855818][T75672] 7 locks held by kexec/75672:
[ 2710.860457][T75672] #0: ffffffff90a81330 (system_transition_mutex){+.+.}-{4:4}, at: __se_sys_reboot+0xe4/0x210
[ 2710.870554][T75672] #1: ffffffff90e1d0c0 (luo_session_global.outgoing.rwsem){+.+.}-{4:4}, at: luo_session_serialize+0x1f/0x190
[ 2710.882043][T75672] #2: ff46fd50602b7ae0 (&session->mutex){+.+.}-{4:4}, at: luo_session_serialize+0x4f/0x190
[ 2710.891972][T75672] #3: ff46fd500bec0788 (&luo_file->mutex){+.+.}-{4:4}, at: luo_file_freeze+0x65/0x280
[ 2710.901463][T75672] #4: ff46fd509d8106a8 (&new_dev_set->lock){+.+.}-{4:4}, at: vfio_pci_liveupdate_freeze+0x36/0x100
[ 2710.912086][T75672] #5: ff46fd501f9a19a8 (&vdev->memory_lock){++++}-{4:4}, at: vfio_pci_liveupdate_freeze+0x51/0x100
[ 2710.922701][T75672] #6: ff46fd4fd416c1f0 (&dev->mutex){....}-{4:4}, at: pci_dev_trylock+0x25/0x60
[ 2710.931676][T75672]
[ 2710.931676][T75672] stack backtrace:
[ 2710.937439][T75672] CPU: 193 UID: 0 PID: 75672 Comm: kexec Tainted: G S 7.1.0-dbg-DEV #59 PREEMPTLAZY
[ 2710.937442][T75672] Tainted: [S]=CPU_OUT_OF_SPEC
[ 2710.937442][T75672] Hardware name: Google Izumi-EMR/izumi, BIOS 0.20251023.0-0 10/23/2025
[ 2710.937443][T75672] Call Trace:
[ 2710.937446][T75672] <TASK>
[ 2710.937448][T75672] dump_stack_lvl+0x54/0x70
[ 2710.937453][T75672] print_circular_bug+0x2e1/0x300
[ 2710.937455][T75672] check_noncircular+0xf9/0x120
[ 2710.937456][T75672] ? __bfs+0x129/0x200
[ 2710.937458][T75672] __lock_acquire+0x14c6/0x2800
[ 2710.937460][T75672] ? __lock_acquire+0x1240/0x2800
[ 2710.937463][T75672] ? pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.937465][T75672] lock_acquire+0xd3/0x2c0
[ 2710.937466][T75672] ? pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.937468][T75672] ? lock_is_held_type+0x76/0x100
[ 2710.937471][T75672] ? pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.937473][T75672] __mutex_lock+0x8f/0xcd0
[ 2710.937473][T75672] ? pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.937475][T75672] ? lockdep_hardirqs_on_prepare+0x151/0x210
[ 2710.937477][T75672] ? _raw_spin_unlock_irqrestore+0x35/0x50
[ 2710.937482][T75672] pci_dev_reset_iommu_prepare+0x6e/0x1a0
[ 2710.937484][T75672] pcie_flr+0x32/0xc0
[ 2710.937485][T75672] __pci_reset_function_locked+0x84/0x120
[ 2710.937487][T75672] vfio_pci_core_try_reset+0x96/0xe0
[ 2710.937489][T75672] vfio_pci_liveupdate_freeze+0x89/0x100
[ 2710.937490][T75672] luo_file_freeze+0xba/0x280
[ 2710.937492][T75672] luo_session_serialize+0x69/0x190
[ 2710.937493][T75672] liveupdate_reboot+0x19/0x30
[ 2710.937495][T75672] kernel_kexec+0x2f/0xa0
[ 2710.937496][T75672] __se_sys_reboot+0xfd/0x210
[ 2710.937497][T75672] ? check_object+0x1ee/0x390
[ 2710.937500][T75672] ? lock_release+0xef/0x350
[ 2710.937501][T75672] ? kmem_cache_free+0x1b5/0x520
[ 2710.937506][T75672] ? _raw_spin_unlock_irqrestore+0x35/0x50
[ 2710.937508][T75672] ? kmem_cache_free+0x1b5/0x520
[ 2710.937509][T75672] ? __x64_sys_close+0x3d/0x80
[ 2710.937510][T75672] ? entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.937511][T75672] ? entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.937512][T75672] do_syscall_64+0x15f/0x710
[ 2710.937514][T75672] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 2710.937515][T75672] RIP: 0033:0x7fa57e4f2513
[ 2710.937519][T75672] Code: cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc 89 fa b8 a9 00 00 00 bf ad de e1 fe be 69 19 12 28 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 f7 d8 48 8b 0d db 2c 07 00 64 89 01 48
[ 2710.937520][T75672] RSP: 002b:00007ffd16943748 EFLAGS: 00000246 ORIG_RAX: 00000000000000a9
[ 2710.937523][T75672] RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fa57e4f2513
[ 2710.937524][T75672] RDX: 0000000045584543 RSI: 0000000028121969 RDI: 00000000fee1dead
[ 2710.937526][T75672] RBP: 00007ffd16943a60 R08: 0000000000000009 R09: 00007fa57e5672e0
[ 2710.937527][T75672] R10: 0000000000000008 R11: 0000000000000246 R12: 00007ffd169438e0
[ 2710.937528][T75672] R13: 0000000000000000 R14: 00007ffd169438e0 R15: 0000000000000001
[ 2710.937532][T75672] </TASK>Quoted 3 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +2
-0+pci_restore_state (pdev); +return 0 ; }
PATCH v4 3/16 vfio/pci: Retrieve preserved device files after Live Update
In reply to Vipin SharmaEnable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks.
Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice.
For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update.
--- drivers/vfio/device_cdev.c | 59 ++++++++++++++++++++++---- drivers/vfio/pci/vfio_pci_liveupdate.c | 52 ++++++++++++++++++++++- drivers/vfio/vfio_main.c | 13 ++++++ include/linux/vfio.h | 11 +++++ 4 files changed, 124 insertions(+), 11 deletions(-)
drivers/vfio/device_cdev.c +50 -9
@@ -2,6 +2,7 @@/* * Copyright (c) 2023 Intel Corporation. */ +#include <linux/anon_inodes.h> #include <linux/vfio.h> #include <linux/iommufd.h>
@@ -16,15 +17,10 @@ void vfio_init_device_cdev(struct vfio_device *device) device->cdev.owner =THIS_MODULE ; } -/* -* device access via the fd opened by this function is blocked until -* .open_device() is called successfully during BIND_IOMMUFD. -*/ -int vfio_device_fops_cdev_open (struct inode *inode,struct file *filep) +static int vfio_device_cdev_open (struct vfio_device *device,struct file **filep) { -struct vfio_device *device =container_of (inode->i_cdev, - struct vfio_device, cdev);struct vfio_device_file *df; +struct file *file = *filep;int ret;/* Paired with the put in vfio_device_fops_release() */
@@ -37,22 +33,67 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) goto err_put_registration; } - filep->private_data = df; +/* +* Simulate opening the character device using an anonymous inode. The +* returned file has the same properties as a cdev file (e.g. operations +* are blocked until BIND_IOMMUFD is called). +*/ +if (!file) { + file =anon_inode_getfile_fmode ("[vfio-device-liveupdate]" , + &vfio_device_fops,NULL , +O_RDWR ,FMODE_PREAD |FMODE_PWRITE ); + +if (IS_ERR (file)) { + ret =PTR_ERR (file); + goto err_free_device_file; + } + + *filep = file; + } + + file->private_data = df;/* * Use the pseudo fs inode on the device to link all mmaps * to the same address space, allowing us to unmap all vmas * associated to this device using unmap_mapping_range(). */ - filep->f_mapping = device->inode->i_mapping; + file->f_mapping = device->inode->i_mapping;return 0 ; +err_free_device_file: +kfree (df); err_put_registration:vfio_device_put_registration (device);return ret; } +struct file *vfio_device_liveupdate_cdev_open (struct vfio_device *device) +{ +struct file *file =NULL ; +int ret; + + ret =vfio_device_cdev_open (device, &file); +if (ret) +return ERR_PTR (ret); + +return file; +} +EXPORT_SYMBOL_GPL (vfio_device_liveupdate_cdev_open); + +/* +* device access via the fd opened by this function is blocked until +* .open_device() is called successfully during BIND_IOMMUFD. +*/ +int vfio_device_fops_cdev_open (struct inode *inode,struct file *file) +{ +struct vfio_device *device =container_of (inode->i_cdev, + struct vfio_device, cdev); + +return vfio_device_cdev_open (device, &file); +} +static void vfio_df_get_kvm_safe (struct vfio_device_file *df) {spin_lock (&df->kvm_ref_lock);
drivers/vfio/pci/vfio_pci_liveupdate.c +50 -2
82 lines not on this page · raw message
drivers/vfio/vfio_main.c +13 -0
@@ -13,6 +13,7 @@#include <linux/cdev.h> #include <linux/compat.h> #include <linux/device.h> +#include <linux/device/class.h> #include <linux/fs.h> #include <linux/idr.h> #include <linux/iommu.h>
@@ -1787,6 +1788,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, }EXPORT_SYMBOL (vfio_dma_rw); +struct vfio_device *vfio_find_device (const void *data,device_match_t match) +{ +struct device *device; + + device =class_find_device (vfio.device_class,NULL , data, match); +if (!device) +return NULL ; + + return container_of(device, struct vfio_device, device); +} +EXPORT_SYMBOL_GPL (vfio_find_device); +/* * Module/class support */
include/linux/vfio.h +11 -0
@@ -431,4 +431,15 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),void vfio_virqfd_disable (struct virqfd **pvirqfd);void vfio_virqfd_flush_thread (struct virqfd **pvirqfd); +#if IS_ENABLED (CONFIG_VFIO_DEVICE_CDEV ) +struct file *vfio_device_liveupdate_cdev_open (struct vfio_device *device); +#else +static inline struct file *vfio_device_liveupdate_cdev_open (struct vfio_device *device) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */ + +struct vfio_device *vfio_find_device (const void *data,device_match_t match); +#endif /* VFIO_H */
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 03/16] vfio/pci: Retrieve preserved device files after Live Update
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] vfio_pci_liveupdate_can_finish logic error permanently leaks preserved KHO memory and wedges the…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] vfio_pci_liveupdate_can_finish logic error permanently leaks preserved KHO memory and wedges the session.
- [High] TOCTOU race in vfio_pci_liveupdate_freeze allows device reset while interrupts are active. --
commit aeb30af9099697f73a40eb35a514bc4eaae76916
Author: Vipin Sharma <[email protected]>
vfio/pci: Retrieve preserved device files after Live Update
This commit implements the retrieve() and finish() file handler callbacks for VFIO, enabling userspace to retrieve preserved VFIO device files after a Live Update.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0
-0
[ ... ]
Quoted 1 line
drivers/vfio/pci/vfio_pci_liveupdate.c +0
-0@@ -203,13 +210,53 @@ static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
[ ... ]
drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +4
-0+static bool vfio_pci_liveupdate_can_finish (struct liveupdate_file_op_args *args) +{ +return args->retrieve_status >0 ; +}
Will this permanently leak preserved KHO memory and wedge the session if retrieval fails or is never attempted?
If the Live Update Orchestrator (LUO) sets retrieve_status to 0 (userspace never attempted retrieval) or to a negative error code (retrieval failed), can_finish() will evaluate to false.
This will cause the LUO core's luo_file_finish() routine to abort with -EBUSY, bypassing the finish() callback and leaking the KHO memory allocated for args->serialized_data.
drivers/vfio/pci/vfio_pci_liveupdate.c static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = { · line 266
Quoted 9 lines
static void
vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args){drivers/vfio/pci/vfio_pci_liveupdate.c +1
-0+kho_restore_free (phys_to_virt (args->serialized_data)); }static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {@@ -218,6 +265,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = { .unpreserve = vfio_pci_liveupdate_unpreserve, .freeze = vfio_pci_liveupdate_freeze,
I realize this problem wasn't introduced by this patch, but existed in the immediately preceding patch in the series.
Is there a TOCTOU race in vfio_pci_liveupdate_freeze() that allows a device reset while interrupts are active?
vfio_pci_liveupdate_freeze() reads vdev->irq_type to verify that interrupts are disabled:
if (vdev->irq_type != VFIO_PCI_NUM_IRQS) {
pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n");
return -EINVAL;
}It does so without holding vdev->igate, which is the lock that protects VFIO interrupt configuration.
Could a concurrent VFIO_DEVICE_SET_IRQS ioctl acquire vdev->igate, enable interrupts, and update vdev->irq_type immediately after the safety check in freeze() passes?
freeze() would then acquire vdev->memory_lock and proceed to reset the PCI device while interrupts are unsafely active, potentially leading to interrupt storms or state corruption.
Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1
-0.retrieve = vfio_pci_liveupdate_retrieve, + .can_finish = vfio_pci_liveupdate_can_finish, .finish = vfio_pci_liveupdate_finish, .owner = THIS_MODULE, };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v4 4/16 vfio/pci: Notify PCI subsystem about devices preserved across Live Update
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Notify the PCI subsystem about devices vfio-pci is preserving across Live Update by registering the vfio-pci liveupdate file handler with the PCI subsystem's FLB handler.
Notably this will ensure that devices preserved through vfio-pci will have their PCI bus numbers preserved across Live Update, allowing VFIO to use BDF as a key to identify the device across the Live Update and (in the future) allow the device to continue DMA operations across the Live Update.
This also enables VFIO to detect that a device was preserved before userspace first retrieves the file from it, which will be used in subsequent commits.
--- drivers/vfio/pci/vfio_pci_liveupdate.c | 44 +++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-)
drivers/vfio/pci/vfio_pci_liveupdate.c +40 -4
@@ -71,6 +71,9 @@* interrupts on the device will cause the ``reboot(LINUX_REBOOT_CMD_KEXEC)`` * syscall (to initiate the kexec) to fail. * +* In addition, the device must meet all of the restrictions imposed by the +* core PCI layer documented at :doc:`/PCI/liveupdate`. +* * Preservation Behavior * ===================== *
@@ -141,23 +144,37 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)struct vfio_pci_core_device_ser *ser;struct vfio_pci_core_device *vdev;struct pci_dev *pdev; +int ret; vdev = container_of(device, struct vfio_pci_core_device, vdev); pdev = vdev->pdev; + ret =pci_liveupdate_preserve (pdev); +if (ret) +return ret; + ser =kho_alloc_preserve (sizeof (*ser)); -if (IS_ERR (ser)) -return PTR_ERR (ser); +if (IS_ERR (ser)) { + ret =PTR_ERR (ser); + goto err_unpreserve; + } ser->bdf =pci_dev_id (pdev); ser->domain =pci_domain_nr (pdev->bus); args->serialized_data =virt_to_phys (ser);return 0 ; + +err_unpreserve: +pci_liveupdate_unpreserve (pdev); +return ret; }static void vfio_pci_liveupdate_unpreserve (struct liveupdate_file_op_args *args) { +struct vfio_device *device =vfio_device_from_file (args->file); + +pci_liveupdate_unpreserve (to_pci_dev (device->dev));kho_unpreserve_free (phys_to_virt (args->serialized_data)); }
@@ -256,6 +273,15 @@ static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)static void vfio_pci_liveupdate_finish (struct liveupdate_file_op_args *args) { +struct vfio_device *device; + +if (!args->file) { +pr_err ("Finish called on an unretrieved file.\n" ); +return ; + } + + device =vfio_device_from_file (args->file); +pci_liveupdate_finish (to_pci_dev (device->dev));kho_restore_free (phys_to_virt (args->serialized_data)); }
@@ -280,13 +306,23 @@ int __init vfio_pci_liveupdate_init(void)int ret; ret =liveupdate_register_file_handler (&vfio_pci_liveupdate_fh); -if (ret && ret != -EOPNOTSUPP ) -return ret; +if (ret) + goto err_return; + + ret =pci_liveupdate_register_flb (&vfio_pci_liveupdate_fh); +if (ret) + goto err_unregister;return 0 ; + +err_unregister: +liveupdate_unregister_file_handler (&vfio_pci_liveupdate_fh); +err_return: +return (ret == -EOPNOTSUPP ) ?0 : ret; }void vfio_pci_liveupdate_cleanup (void ) { +pci_liveupdate_unregister_flb (&vfio_pci_liveupdate_fh);liveupdate_unregister_file_handler (&vfio_pci_liveupdate_fh); }
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 5/16 vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD rather than by opening the corresponding VFIO character device or via VFIO_GROUP_GET_DEVICE_FD.
Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.
--- drivers/vfio/device_cdev.c | 8 ++++++++ drivers/vfio/group.c | 9 +++++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 6 ++++++ drivers/vfio/vfio.h | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+)
drivers/vfio/device_cdev.c +8 -0
@@ -49,6 +49,14 @@ static int vfio_device_cdev_open(struct vfio_device *device, struct file **filep } *filep = file; + }else if (vfio_liveupdate_incoming_is_preserved (device)) { +/* +* Since it is live update preserved device, it must be +* retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD instead of +* opening /dev/vfio/devices/vfioX. +*/ + ret = -EBUSY ; + goto err_free_device_file; } file->private_data = df;
drivers/vfio/group.c +9 -0
@@ -316,6 +316,15 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,if (IS_ERR (device))return PTR_ERR (device); +/* +* This device was preserved across a Live Update. Accessing it via +* VFIO_GROUP_GET_DEVICE_FD is not allowed. +*/ +if (vfio_liveupdate_incoming_is_preserved (device)) { +vfio_device_put_registration (device); +return -EBUSY ; + } + fd =FD_ADD (O_CLOEXEC ,vfio_device_open_file (device));if (fd <0 )vfio_device_put_registration (device);
drivers/vfio/pci/vfio_pci_liveupdate.c +6 -0
@@ -47,6 +47,12 @@* ... * ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...); * +* .. note:: +* After kexec, if a device was preserved by the previous kernel, attempting +* to open a new file for the device via its character device +* (``/dev/vfio/devices/X``) or via ``VFIO_GROUP_GET_DEVICE_FD`` will fail +* with ``-EBUSY``. +* * Restrictions * ============ *
drivers/vfio/vfio.h +18 -0
@@ -11,6 +11,7 @@#include <linux/cdev.h> #include <linux/module.h> #include <linux/vfio.h> +#include <linux/pci.h> struct iommufd_ctx ;struct iommu_group ;
@@ -461,4 +462,21 @@ static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }static inline void vfio_device_debugfs_exit (struct vfio_device *vdev) { }#endif /* CONFIG_VFIO_DEBUGFS */ +#ifdef CONFIG_PCI_LIVEUPDATE +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +struct device *d = device->dev; + +if (dev_is_pci (d)) +return to_pci_dev (d)->liveupdate_incoming; + +return false; +} +#else +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +return false; +} +#endif /* CONFIG_PCI_LIVEUPDATE */ +#endif
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 05/16] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Bus-specific (PCI) knowledge is hardcoded into the generic VFIO core. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Bus-specific (PCI) knowledge is hardcoded into the generic VFIO core. --
commit 15f88d928c47afcb61541ee71ab7eb8d8e0b7766
Author: David Matlack <[email protected]>
vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD
This commit enforces that incoming VFIO devices preserved across a kexec must be retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD. Opening them via their character device or VFIO_GROUP_GET_DEVICE_FD will fail with -EBUSY to prevent initializing the device without the preserved state.
Quoted 9 lines
drivers/vfio/vfio.h +1
-0@@ -11,6 +11,7 @@#include <linux/cdev.h> #include <linux/module.h> #include <linux/vfio.h> +#include <linux/pci.h>
[ ... ]
drivers/vfio/vfio.h static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { } · line 471
Quoted 11 lines
drivers/vfio/vfio.h +7
-0@@ -461,4 +462,21 @@ static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }static inline void vfio_device_debugfs_exit (struct vfio_device *vdev) { }#endif /* CONFIG_VFIO_DEBUGFS */ +#ifdef CONFIG_PCI_LIVEUPDATE +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +struct device *d = device->dev; + +if (dev_is_pci (d)) +return to_pci_dev (d)->liveupdate_incoming;
Is it acceptable to hardcode bus-specific PCI knowledge into the generic VFIO core?
The VFIO subsystem is designed to be bus-agnostic and relies on struct vfio_device to abstract bus-specific details. By including linux/pci.h in the generic drivers/vfio/vfio.h header and explicitly calling dev_is_pci() and to_pci_dev(), this introduces bus-specific logic into the core framework.
Could a generic state flag be set on the struct vfio_device object by the bus driver (like vfio-pci) during registration or initialization instead?
Quoted 11 lines
drivers/vfio/vfio.h +10
-0+ +return false; +} +#else +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +return false; +} +#endif /* CONFIG_PCI_LIVEUPDATE */ +#endif
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v4 05/16] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD
In reply to Vipin SharmaQuoted 28 lines · Vipin Sharma
From: David Matlack <[email protected]>
Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via
LIVEUPDATE_SESSION_RETRIEVE_FDrather than by opening the corresponding VFIO character device or viaVFIO_GROUP_GET_DEVICE_FD.Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.
Reviewed-by Pranjal Shrivastava <[email protected]>Signed-off-by David Matlack <[email protected]>Co-developed-by Vipin Sharma <[email protected]>Signed-off-by Vipin Sharma <[email protected]>--- drivers/vfio/device_cdev.c | 8 ++++++++ drivers/vfio/group.c | 9 +++++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 6 ++++++ drivers/vfio/vfio.h | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+)drivers/vfio/device_cdev.c +0
-0@@ -49,6 +49,14 @@ static int vfio_device_cdev_open(struct vfio_device *device, struct file **filep }
drivers/vfio/device_cdev.c static int vfio_device_cdev_open(struct vfio_device *device, struct file **filep · line 59
Quoted 10 lines
drivers/vfio/device_cdev.c +8
-0*filep = file; + }else if (vfio_liveupdate_incoming_is_preserved (device)) { +/* +* Since it is live update preserved device, it must be +* retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD instead of +* opening /dev/vfio/devices/vfioX. +*/ + ret = -EBUSY ; + goto err_free_device_file;
When vfio_liveupdate_incoming_is_preserved(device) returns true, vfio_device_put_registration(device) is not called in this path.
Is vfio_device_put_registration(device) instead invoked from the err_free_device_file error handling path?
Zhu Yanjun
Quoted 74 lines
drivers/vfio/device_cdev.c +0
-0} file->private_data = df;drivers/vfio/group.c +9
-0@@ -316,6 +316,15 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,if (IS_ERR (device))return PTR_ERR (device); +/* +* This device was preserved across a Live Update. Accessing it via +* VFIO_GROUP_GET_DEVICE_FD is not allowed. +*/ +if (vfio_liveupdate_incoming_is_preserved (device)) { +vfio_device_put_registration (device); +return -EBUSY ; + } + fd =FD_ADD (O_CLOEXEC ,vfio_device_open_file (device));if (fd <0 )vfio_device_put_registration (device);drivers/vfio/pci/vfio_pci_liveupdate.c +6
-0@@ -47,6 +47,12 @@* ... * ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...); * +* .. note:: +* After kexec, if a device was preserved by the previous kernel, attempting +* to open a new file for the device via its character device +* (``/dev/vfio/devices/X``) or via ``VFIO_GROUP_GET_DEVICE_FD`` will fail +* with ``-EBUSY``. +* * Restrictions * ============ * drivers/vfio/vfio.h +18
-0@@ -11,6 +11,7 @@#include <linux/cdev.h> #include <linux/module.h> #include <linux/vfio.h> +#include <linux/pci.h> struct iommufd_ctx ;struct iommu_group ;@@ -461,4 +462,21 @@ static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }static inline void vfio_device_debugfs_exit (struct vfio_device *vdev) { }#endif /* CONFIG_VFIO_DEBUGFS */ +#ifdef CONFIG_PCI_LIVEUPDATE +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +struct device *d = device->dev; + +if (dev_is_pci (d)) +return to_pci_dev (d)->liveupdate_incoming; + +return false; +} +#else +static inline bool vfio_liveupdate_incoming_is_preserved (struct vfio_device *device) +{ +return false; +} +#endif /* CONFIG_PCI_LIVEUPDATE */ +#endif
Signature · 2 lines
Best Regards, Yanjun.Zhu
Re: [PATCH v4 05/16] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD
Quoted 43 lines · Zhu Yanjun
Quoted 27 lines · Vipin Sharma
From: David Matlack <[email protected]>
Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via
LIVEUPDATE_SESSION_RETRIEVE_FDrather than by opening the corresponding VFIO character device or viaVFIO_GROUP_GET_DEVICE_FD.Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.
Reviewed-by Pranjal Shrivastava <[email protected]>Signed-off-by David Matlack <[email protected]>Co-developed-by Vipin Sharma <[email protected]>Signed-off-by Vipin Sharma <[email protected]>--- drivers/vfio/device_cdev.c | 8 ++++++++ drivers/vfio/group.c | 9 +++++++++ drivers/vfio/pci/vfio_pci_liveupdate.c | 6 ++++++ drivers/vfio/vfio.h | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+)drivers/vfio/device_cdev.c +0
-0drivers/vfio/device_cdev.c static int vfio_device_cdev_open(struct vfio_device *device, struct file **filep · line 58
Quoted 10 lines
drivers/vfio/device_cdev.c +8
-0@@ -49,6 +49,14 @@ static int vfio_device_cdev_open(struct vfio_device *device, struct file **filep } *filep = file; + }else if (vfio_liveupdate_incoming_is_preserved (device)) { +/* +* Since it is live update preserved device, it must be +* retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD instead of +* opening /dev/vfio/devices/vfioX. +*/ + ret = -EBUSY ; + goto err_free_device_file;When
vfio_liveupdate_incoming_is_preserved(device)returns true,vfio_device_put_registration(device)is not called in this path.
Quoted 2 lines
Is
vfio_device_put_registration(device)instead invoked from theerr_free_device_fileerror handling path?
Yes, at the end of vfio_device_cdev_open(), goto label first frees the device file object and then calls the vfio_device_put_registration(). This is the same error handlign flow as in the if(!file) {} code in the above function.
PATCH v4 6/16 vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Stash a pointer to a device's incoming Live Updated state in struct vfio_pci_core_device. This will enable subsequent commits to use the preserved state when initializing the device.
To enable VFIO to safely access this pointer during device enablement, require that the device is fully enabled before returning true from can_finish(). This is synchronized by vfio_pci_core.c setting vdev->liveupdate_incoming_state to NULL under dev_set lock once it's done using it.
--- drivers/vfio/pci/vfio_pci_core.c | 2 +- drivers/vfio/pci/vfio_pci_liveupdate.c | 17 ++++++++++++++++- include/linux/vfio_pci_core.h | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +1 -1
@@ -568,7 +568,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)if (!vfio_vga_disabled () &&vfio_pci_is_vga (pdev)) vdev->has_vga = true; - + vdev->liveupdate_incoming_state =NULL ;return 0 ; out_free_zdev:
drivers/vfio/pci/vfio_pci_liveupdate.c +16 -1
@@ -249,6 +249,7 @@ static int match_device(struct device *dev, const void *arg)static int vfio_pci_liveupdate_retrieve (struct liveupdate_file_op_args *args) {struct vfio_pci_core_device_ser *ser; +struct vfio_pci_core_device *vdev;struct vfio_device *device;struct file *file;int ret =0 ;
@@ -265,6 +266,9 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args) goto out; } + vdev = container_of(device, struct vfio_pci_core_device, vdev); + vdev->liveupdate_incoming_state = ser; + args->file = file; out:/* Drop the reference from vfio_find_device() */
@@ -274,7 +278,18 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)static bool vfio_pci_liveupdate_can_finish (struct liveupdate_file_op_args *args) { -return args->retrieve_status >0 ; +struct vfio_pci_core_device *vdev; +struct vfio_device *device; + +if (args->retrieve_status <=0 ) +return false; + + device =vfio_device_from_file (args->file); + vdev = container_of(device, struct vfio_pci_core_device, vdev); + +/* Check that vdev->liveupdate_incoming_state is no longer in use. */ +guard (mutex)(&device->dev_set->lock); +return !vdev->liveupdate_incoming_state; }static void vfio_pci_liveupdate_finish (struct liveupdate_file_op_args *args)
include/linux/vfio_pci_core.h +2 -0
@@ -26,6 +26,7 @@#define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)struct vfio_pci_core_device ; +struct vfio_pci_core_device_ser ;struct vfio_pci_region ;struct p2pdma_provider ;struct dma_buf_attachment ;
@@ -142,6 +143,7 @@ struct vfio_pci_core_device {struct notifier_block nb;struct rw_semaphore memory_lock;struct list_head dmabufs; +struct vfio_pci_core_device_ser *liveupdate_incoming_state; };enum vfio_pci_io_width {
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 06/16] vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing cleanup of `liveupdate_incoming_state` on error paths and device closure - [High] Data…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing cleanup of liveupdate_incoming_state on error paths and device closure
- [High] Data race on liveupdate_incoming_state modification
--
commit d8de06a6f6edeb0c9ca334c98f9b961bfa08a639
Author: David Matlack <[email protected]>
vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device
This commit adds a pointer to the incoming Live Update state in struct vfio_pci_core_device. It modifies vfio_pci_core_enable() to clear this pointer on the success path to signal that the state has been consumed. It also updates vfio_pci_liveupdate_can_finish() to wait for this pointer to become NULL under the dev_set lock.
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 574
Quoted 13 lines
drivers/vfio/pci/vfio_pci_core.c +1
-1@@ -568,7 +568,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)if (!vfio_vga_disabled () &&vfio_pci_is_vga (pdev)) vdev->has_vga = true; - + vdev->liveupdate_incoming_state =NULL ;return 0 ; out_free_zdev:
If device enablement fails and the code jumps to an error label like out_free_zdev, liveupdate_incoming_state will not be cleared. Will this cause vfio_pci_liveupdate_can_finish() to stall indefinitely waiting for this pointer to become NULL?
Additionally, if the orchestrator times out and frees the KHO memory, could this leave a dangling pointer that results in a use-after-free on subsequent attempts to open the device?
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0
-0
[ ... ]
drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args) · line 270
Quoted 6 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +2
-0@@ -265,6 +266,9 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args) goto out; } + vdev = container_of(device, struct vfio_pci_core_device, vdev); + vdev->liveupdate_incoming_state = ser;
The commit message states "This is synchronized by vfio_pci_core.c setting vdev->liveupdate_incoming_state to NULL under dev_set lock once it's done using it."
However, the assignment here does not appear to take device->dev_set->lock.
If userspace triggers this retrieve operation concurrently with device enablement, does this create a data race? An unprotected write here could clobber the NULL value just set by a successful device enablement, leaving the pointer non-NULL and causing vfio_pci_liveupdate_can_finish() to wait forever.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1
-0+ args->file = file; out:/* Drop the reference from vfio_find_device() */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v4 7/16 docs: liveupdate: Add documentation for VFIO PCI
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Add documentation for preserving VFIO device files across a Live Update, generated from the kernel-doc comments in the code.
--- Documentation/core-api/liveupdate.rst | 1 + .../driver-api/vfio_pci_liveupdate.rst | 23 +++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 25 insertions(+) create mode 100644 Documentation/driver-api/vfio_pci_liveupdate.rst
Documentation/core-api/liveupdate.rst +1 -0
@@ -34,6 +34,7 @@ The following types of file descriptors can be preserved :maxdepth: 1 ../mm/memfd_preservation + ../driver-api/vfio_pci_liveupdate Public API ==========
Documentation/driver-api/vfio_pci_liveupdate.rst new file +23 -0
@@ -0,0 +1,23 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +==================================== +VFIO PCI Device Preservation via LUO +==================================== + +.. kernel-doc:: drivers/vfio/pci/vfio_pci_liveupdate.c + :doc: VFIO PCI Preservation via LUO + +VFIO PCI Preservation ABI +========================= + +.. kernel-doc:: include/linux/kho/abi/vfio_pci.h + :doc: VFIO PCI Live Update ABI + +.. kernel-doc:: include/linux/kho/abi/vfio_pci.h + :internal: + +See Also +======== + +- :doc:`/core-api/liveupdate` +- :doc:`/core-api/kho/index`
MAINTAINERS +1 -0
@@ -27979,6 +27979,7 @@ T: git https://github.com/awilliam/linux-vfio.git F: Documentation/ABI/testing/debugfs-vfio F: Documentation/ABI/testing/sysfs-devices-vfio-dev F: Documentation/driver-api/vfio.rst +F: Documentation/driver-api/vfio_pci_liveupdate.rst F: drivers/vfio/ F: include/linux/kho/abi/vfio_pci.h F: include/linux/vfio.h
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 8/16 vfio: selftests: Build liveupdate library in VFIO selftests
In reply to Vipin SharmaImport and build liveupdate selftest library in VFIO selftests.
It allows to use liveupdate ioctls in VFIO selftests
--- tools/testing/selftests/vfio/Makefile | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/Makefile +8 -3
@@ -20,6 +20,7 @@ TEST_FILES += scripts/setup.sh include ../lib.mk include lib/libvfio.mk +include ../liveupdate/lib/libliveupdate.mk CFLAGS += -I$(top_srcdir)/tools/include CFLAGS += -MD
@@ -27,11 +28,15 @@ CFLAGS += $(EXTRA_CFLAGS) LDFLAGS += -pthread -$(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) - $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ +LIBS_O := $(LIBVFIO_O) +LIBS_O += $(LIBLIVEUPDATE_O) + +$(TEST_GEN_PROGS): %: %.o $(LIBS_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLIBS) -o $@ TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS)) -TEST_DEP_FILES = $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O)) +TEST_DEP_FILES := $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O)) +TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBS_O)) -include $(TEST_DEP_FILES) EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES)
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 08/16] vfio: selftests: Build liveupdate library in VFIO selftests
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The VFIO selftest Makefile includes `../liveupdate/lib/libliveupdate.mk`, which does not exist…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The VFIO selftest Makefile includes ../liveupdate/lib/libliveupdate.mk, which does not exist, causing a build failure and leaving LIBLIVEUPDATE_O undefined. --
commit d3eae23e3b6d5e95208bbaa8caae8ddebad58cb5
Author: Vipin Sharma <[email protected]>
vfio: selftests: Build liveupdate library in VFIO selftests
Import and build the liveupdate selftest library in VFIO selftests to allow using liveupdate ioctls.
tools/testing/selftests/vfio/Makefile TEST_FILES += scripts/setup.sh · line 23
Quoted 9 lines
tools/testing/selftests/vfio/Makefile +1
-0@@ -20,6 +20,7 @@ TEST_FILES += scripts/setup.sh include ../lib.mk include lib/libvfio.mk +include ../liveupdate/lib/libliveupdate.mk
Is it possible this file is missing from the tree?
Neither the lib directory nor the libliveupdate.mk file appear to exist under tools/testing/selftests/liveupdate/. This will cause a build failure since make requires the included file to be present.
tools/testing/selftests/vfio/Makefile CFLAGS += $(EXTRA_CFLAGS) · line 32
Quoted 11 lines
tools/testing/selftests/vfio/Makefile +2
-2CFLAGS += -I$(top_srcdir)/tools/include CFLAGS += -MD@@ -27,11 +28,15 @@ CFLAGS += $(EXTRA_CFLAGS) LDFLAGS += -pthread -$(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) - $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ +LIBS_O := $(LIBVFIO_O) +LIBS_O += $(LIBLIVEUPDATE_O)
Since the libliveupdate.mk inclusion fails, LIBLIVEUPDATE_O remains undefined here. Does this also cause missing object files during linking?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v4 9/16 vfio: selftests: Add vfio_pci_liveupdate_uapi_test
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Add a selftest to exercise preserving a various VFIO files through /dev/liveupdate. Ensure that VFIO cdev device files can be preserved and everything else (group-based device files, group files, and container files) all fail.
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_pci_liveupdate_uapi_test.c | 93 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -12,6 +12,7 @@ TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test TEST_GEN_PROGS += vfio_pci_driver_test +TEST_GEN_PROGS += vfio_pci_liveupdate_uapi_test TEST_FILES += scripts/cleanup.sh TEST_FILES += scripts/lib.sh
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 09/16] vfio: selftests: Add vfio_pci_liveupdate_uapi_test
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] FIXTURE_TEARDOWN unconditionally dereferences NULL pointers and closes stdin if FIXTURE_SETUP…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] FIXTURE_TEARDOWN unconditionally dereferences NULL pointers and closes stdin if FIXTURE_SETUP fails.
- [Low] The test file fails to build due to missing dependencies, headers, and undefined functions. --
commit 483a2948059fa012eaf9d233845cda79f05f7acb
Author: David Matlack <[email protected]>
vfio: selftests: Add vfio_pci_liveupdate_uapi_test
Add a selftest to exercise preserving a various VFIO files through /dev/liveupdate. Ensure that VFIO cdev device files can be preserved and everything else (group-based device files, group files, and container files) all fail.
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 3
Quoted 9 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +3
-0@@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <libliveupdate.h>
Are there missing files or dependencies in this commit? This test fails to build because <libliveupdate.h> does not exist anywhere in the source tree.
Also, luo_session_preserve_fd() has no userspace implementation available (it only exists as an internal static kernel function).
Additionally, the vfio selftest Makefile depends on ../liveupdate/lib/libliveupdate.mk (introduced by a missing commit), which is completely missing from the tree. This results in immediate compilation and linker failures.
[ ... ]
Quoted 8 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +8
-0+FIXTURE_SETUP (vfio_pci_liveupdate_uapi_test) +{ + self->luo_fd =luo_open_device (); +ASSERT_GE (self->luo_fd,0 ); + + self->session_fd =luo_create_session (self->luo_fd,"session" ); +ASSERT_GE (self->session_fd,0 ); +
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 45
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +10
-0+ self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); +} + +FIXTURE_TEARDOWN (vfio_pci_liveupdate_uapi_test) +{ +vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +close (self->session_fd); +close (self->luo_fd);
Will this cause a segmentation fault if an assertion fails during setup?
In the kselftest framework, the fixture state (self) is zero-initialized via mmap. If an assertion in FIXTURE_SETUP fails (for example, ASSERT_GE(self->luo_fd, 0)), the framework aborts setup and immediately calls FIXTURE_TEARDOWN.
Since self->device and self->iommu would be unassigned and thus NULL in this scenario, vfio_pci_device_cleanup() and iommu_cleanup() will dereference NULL pointers.
Additionally, since self->session_fd would be 0, close(self->session_fd) will inadvertently close stdin.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v4 10/16 vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD
In reply to Vipin SharmaUse the given VFIO cdev FD to initialize vfio_pci_device in VFIO selftests. Add the assertion to make sure that passed cdev FD is not used with legacy VFIO APIs. If VFIO cdev FD is provided then do not open the device instead use the FD for any interaction with the device.
This API will allow to write selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec.
--- .../lib/include/libvfio/vfio_pci_device.h | 3 ++ .../selftests/vfio/lib/vfio_pci_device.c | 33 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +3 -0
@@ -38,6 +38,9 @@ struct vfio_pci_device {#define dev_info (_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)#define dev_err (_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__) +struct vfio_pci_device *__vfio_pci_device_init (const char *bdf, +struct iommu *iommu, +int device_fd);struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu);void vfio_pci_device_cleanup (struct vfio_pci_device *device);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +24 -9
@@ -342,19 +342,27 @@ static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)ioctl_assert (device_fd,VFIO_DEVICE_ATTACH_IOMMUFD_PT , &args); } -static void vfio_pci_iommufd_setup (struct vfio_pci_device *device,const char *bdf) +static void vfio_pci_iommufd_setup (struct vfio_pci_device *device, +const char *bdf,int device_fd) { -const char *cdev_path =vfio_pci_get_cdev_path (bdf); +const char *cdev_path; - device->fd =open (cdev_path,O_RDWR ); -VFIO_ASSERT_GE (device->fd,0 ); -free ((void *)cdev_path); +if (device_fd >=0 ) { + device->fd = device_fd; + }else { + cdev_path =vfio_pci_get_cdev_path (bdf); + device->fd =open (cdev_path,O_RDWR ); +VFIO_ASSERT_GE (device->fd,0 ); +free ((void *)cdev_path); + }vfio_device_bind_iommufd (device->fd, device->iommu->iommufd);vfio_device_attach_iommufd_pt (device->fd, device->iommu->ioas_id); } -struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu) +struct vfio_pci_device *__vfio_pci_device_init (const char *bdf, +struct iommu *iommu, +int device_fd) {struct vfio_pci_device *device;
@@ -365,10 +373,12 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm device->iommu = iommu; device->bdf = bdf; -if (iommu->mode->container_path) +if (iommu->mode->container_path) { +VFIO_ASSERT_EQ (device_fd,-1 );vfio_pci_container_setup (device, bdf); -else -vfio_pci_iommufd_setup (device, bdf); + }else { +vfio_pci_iommufd_setup (device, bdf, device_fd); + }vfio_pci_device_setup (device);vfio_pci_driver_probe (device);
@@ -376,6 +386,11 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommreturn device; } +struct vfio_pci_device *vfio_pci_device_init (const char *bdf,struct iommu *iommu) +{ +return __vfio_pci_device_init (bdf, iommu,/*device_fd=*/ -1 ); +} +void vfio_pci_device_cleanup (struct vfio_pci_device *device) {int i;
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 11/16 vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Add Makefile support for TEST_GEN_PROGS_EXTENDED targets. These tests are not run by default.
TEST_GEN_PROGS_EXTENDED will be used for Live Update selftests in subsequent commits. These selftests must be run manually because they require the user/runner to perform additional actions, such as kexec, during the test.
--- tools/testing/selftests/vfio/Makefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/Makefile +7 -4
@@ -32,14 +32,17 @@ LDFLAGS += -pthread LIBS_O := $(LIBVFIO_O) LIBS_O += $(LIBLIVEUPDATE_O) -$(TEST_GEN_PROGS): %: %.o $(LIBS_O) +$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBS_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLIBS) -o $@ -TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS)) -TEST_DEP_FILES := $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O)) +TESTS_O := $(patsubst %, %.o, $(TEST_GEN_PROGS)) +TESTS_O += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) + +TEST_DEP_FILES := $(patsubst %.o, %.d, $(TESTS_O)) TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBS_O)) -include $(TEST_DEP_FILES) -EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES) +EXTRA_CLEAN += $(TESTS_O) +EXTRA_CLEAN += $(TEST_DEP_FILES) endif
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 12/16 vfio: selftests: Add vfio_pci_liveupdate_kexec_test
In reply to Vipin SharmaAdd a selftest to exercise preserving a vfio-pci device across a Live Update. For now the test is extremely simple and just verifies that the device file can be preserved and retrieved. In the future this test will be extended to verify more parts about device preservation as they are implemented.
This test is added to TEST_GEN_PROGS_EXTENDED since it must be run manually along with a kexec.
To run this test manually:
$ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 1 0000:00:04.0
$ kexec ... # NOTE: Exact method will be distro-dependent
$ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 2 0000:00:04.0
The second call to setup.sh is necessary because preserved devices are not bound to a driver after Live Update. Such devices must be manually bound by userspace after Live Update via driver_override.
This test is considered passing if all commands exit with 0.
--- tools/testing/selftests/vfio/Makefile | 4 + .../vfio/vfio_pci_liveupdate_kexec_test.c | 89 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c
tools/testing/selftests/vfio/Makefile +4 -0
@@ -14,6 +14,10 @@ TEST_GEN_PROGS += vfio_pci_device_init_perf_test TEST_GEN_PROGS += vfio_pci_driver_test TEST_GEN_PROGS += vfio_pci_liveupdate_uapi_test +# This test must be run manually since it requires the user/automation to +# perform a kexec during the test. +TEST_GEN_PROGS_EXTENDED += vfio_pci_liveupdate_kexec_test + TEST_FILES += scripts/cleanup.sh TEST_FILES += scripts/lib.sh TEST_FILES += scripts/run.sh
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 13/16 vfio: selftests: Expose iommu_modes to tests
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Expose the list of iommu_modes to enable tests that want to iterate through all possible iommu modes.
--- tools/testing/selftests/vfio/lib/include/libvfio/iommu.h | 2 ++ tools/testing/selftests/vfio/lib/iommu.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
@@ -15,6 +15,8 @@ struct iommu_mode {unsigned long iommu_type; }; +extern const struct iommu_mode iommu_modes[]; +extern const int nr_iommu_modes;extern const char *default_iommu_mode;struct dma_region {
tools/testing/selftests/vfio/lib/iommu.c +3 -1
@@ -23,7 +23,7 @@const char *default_iommu_mode =MODE_IOMMUFD ;/* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */ -static const struct iommu_mode iommu_modes[] = { +const struct iommu_mode iommu_modes[] = { { .name =MODE_VFIO_TYPE1_IOMMU , .container_path ="/dev/vfio/vfio" ,
@@ -49,6 +49,8 @@ static const struct iommu_mode iommu_modes[] = { }, }; +const int nr_iommu_modes =ARRAY_SIZE (iommu_modes); +static const struct iommu_mode *lookup_iommu_mode (const char *iommu_mode) {int i;
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 14/16 vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Expose a few low-level helper routings for setting up vfio_pci_device structs. These routines will be used in a subsequent commit to assert that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions.
--- .../lib/include/libvfio/vfio_pci_device.h | 5 +++ .../selftests/vfio/lib/vfio_pci_device.c | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +5 -0
@@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device,const char *vfio_pci_get_cdev_path (const char *bdf); +/* Low-level routines for setting up a struct vfio_pci_device */ +struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu); +void vfio_pci_group_setup (struct vfio_pci_device *device); +void vfio_pci_iommu_setup (struct vfio_pci_device *device); +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */
tools/testing/selftests/vfio/lib/vfio_pci_device.c +23 -10
@@ -222,7 +222,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)return group; } -static void vfio_pci_group_setup (struct vfio_pci_device *device,const char *bdf) +void vfio_pci_group_setup (struct vfio_pci_device *device) {struct vfio_group_status group_status = { .argsz =sizeof (group_status),
@@ -230,7 +230,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdfchar group_path[32 ];int group; - group =vfio_pci_get_group_from_dev (bdf); + group =vfio_pci_get_group_from_dev (device->bdf);snprintf (group_path,sizeof (group_path),"/dev/vfio/%d" , group); device->group_fd =open (group_path,O_RDWR );
@@ -242,14 +242,12 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdfioctl_assert (device->group_fd,VFIO_GROUP_SET_CONTAINER , &device->iommu->container_fd); } -static void vfio_pci_container_setup (struct vfio_pci_device *device,const char *bdf) +void vfio_pci_iommu_setup (struct vfio_pci_device *device) {struct iommu *iommu = device->iommu;unsigned long iommu_type = iommu->mode->iommu_type;int ret; -vfio_pci_group_setup (device, bdf); - ret =ioctl (iommu->container_fd,VFIO_CHECK_EXTENSION , iommu_type);VFIO_ASSERT_GT (ret,0 ,"VFIO IOMMU type %lu not supported\n" , iommu_type);
@@ -259,8 +257,14 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, const char* because the IOMMU type is already set. */ (void )ioctl (iommu->container_fd,VFIO_SET_IOMMU , (void *)iommu_type); +} - device->fd =ioctl (device->group_fd,VFIO_GROUP_GET_DEVICE_FD , bdf); +static void vfio_pci_container_setup (struct vfio_pci_device *device) +{ +vfio_pci_group_setup (device); +vfio_pci_iommu_setup (device); + + device->fd =ioctl (device->group_fd,VFIO_GROUP_GET_DEVICE_FD , device->bdf);VFIO_ASSERT_GE (device->fd,0 ); }
@@ -360,9 +364,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,vfio_device_attach_iommufd_pt (device->fd, device->iommu->ioas_id); } -struct vfio_pci_device *__vfio_pci_device_init (const char *bdf, -struct iommu *iommu, -int device_fd) +struct vfio_pci_device *vfio_pci_device_alloc (const char *bdf,struct iommu *iommu) {struct vfio_pci_device *device;
@@ -373,9 +375,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, device->iommu = iommu; device->bdf = bdf; +return device; +} + +struct vfio_pci_device *__vfio_pci_device_init (const char *bdf, +struct iommu *iommu, +int device_fd) +{ +struct vfio_pci_device *device; + + device =vfio_pci_device_alloc (bdf, iommu); +if (iommu->mode->container_path) {VFIO_ASSERT_EQ (device_fd,-1 ); -vfio_pci_container_setup (device, bdf); +vfio_pci_container_setup (device); }else {vfio_pci_iommufd_setup (device, bdf, device_fd); }
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
PATCH v4 15/16 vfio: selftests: Verify that opening VFIO device fails during Live Update
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Verify that opening a VFIO device through its cdev file and via VFIO_GROUP_GET_DEVICE_FD both fail with -EBUSY if the device was preserved across a Live Update. When a device file is preserve across a Live Update, the file must be retrieved from /dev/liveupdate, not from VFIO directly.
--- .../vfio/vfio_pci_liveupdate_kexec_test.c | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+)
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +38 -0
@@ -36,6 +36,42 @@ static void before_kexec(int luo_fd)daemonize_and_wait (); } +static void check_open_vfio_device_fails (void ) +{ +const char *cdev_path =vfio_pci_get_cdev_path (device_bdf); +struct vfio_pci_device *device; +struct iommu *iommu; +int ret, i; + +printf ("Checking open(%s) fails\n" , cdev_path); + ret =open (cdev_path,O_RDWR ); +VFIO_ASSERT_EQ (ret,-1 ); +VFIO_ASSERT_EQ (errno,EBUSY ); +free ((void *)cdev_path); + +for (i =0 ; i < nr_iommu_modes; i++) { +if (!iommu_modes[i].container_path) +continue ; + + iommu =iommu_init (iommu_modes[i].name); + + device =vfio_pci_device_alloc (device_bdf, iommu); +vfio_pci_group_setup (device); +vfio_pci_iommu_setup (device); + +printf ("Checking ioctl(group_fd, VFIO_GROUP_GET_DEVICE_FD, \"%s\") fails (%s)\n" , + device_bdf, iommu_modes[i].name); + + ret =ioctl (device->group_fd,VFIO_GROUP_GET_DEVICE_FD , device->bdf); +VFIO_ASSERT_EQ (ret,-1 ); +VFIO_ASSERT_EQ (errno,EBUSY ); + +close (device->group_fd); +free (device); +iommu_cleanup (iommu); + } +} +static void after_kexec (int luo_fd,int state_session_fd) {struct vfio_pci_device *device;
@@ -44,6 +80,8 @@ static void after_kexec(int luo_fd, int state_session_fd)int device_fd;int stage; +check_open_vfio_device_fails (); +restore_and_read_stage (state_session_fd,STATE_TOKEN , &stage);VFIO_ASSERT_EQ (stage,2 );
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 15/16] vfio: selftests: Verify that opening VFIO device fails during Live Update
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unconditionally testing all legacy IOMMU modes will cause the selftest to crash on kernels…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unconditionally testing all legacy IOMMU modes will cause the selftest to crash on kernels lacking legacy VFIO support. --
commit 15d06d7e17051c89eae8198f7b5a759b9968ab87
Author: David Matlack <[email protected]>
vfio: selftests: Verify that opening VFIO device fails during Live Update
Verify that opening a VFIO device through its cdev file and via VFIO_GROUP_GET_DEVICE_FD both fail with -EBUSY if the device was preserved across a Live Update.
Quoted 4 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +0
-0
[ ... ]
Quoted 6 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +2
-0@@ -36,6 +36,42 @@ static void before_kexec(int luo_fd)daemonize_and_wait (); } +static void check_open_vfio_device_fails (void ) +{
[ ... ]
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c static void before_kexec(int luo_fd) · line 56
Quoted 5 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +5
-0+for (i =0 ; i < nr_iommu_modes; i++) { +if (!iommu_modes[i].container_path) +continue ; + + iommu =iommu_init (iommu_modes[i].name);
Does this introduce a regression on kernels lacking legacy VFIO support?
If the kernel is compiled without legacy VFIO support (CONFIG_VFIO_GROUP=n), calling iommu_init() on a legacy container path like /dev/vfio/vfio will fail.
This causes iommu_init() to fail its internal assertion, calling VFIO_LOG_AND_EXIT() and aborting the process. Because this standalone test program does not use kselftest_harness process isolation, crashing here prevents it from verifying the core live update functionality.
Could we verify the container path is accessible before calling iommu_init() so unsupported modes can be gracefully skipped?
Quoted 2 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +2
-0+ + device =vfio_pci_device_alloc (device_bdf, iommu);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
PATCH v4 16/16 vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test
In reply to Vipin SharmaFrom: David Matlack <[email protected]>
Add a long-running DMA memcpy operation to vfio_pci_liveupdate_kexec_test so that the device attempts to perform DMAs continuously during the Live Update.
At this point iommufd preservation is not supported and bus mastering is not kept enabled on the device during across the kexec, so most of these DMAs will be dropped. However this test ensures that the current device preservation support does not lead to system instability or crashes if the device is active. And once iommufd and bus mastering are preserved, this test can be relaxed to check that the DMA operations completed successfully.
--- .../vfio/vfio_pci_liveupdate_kexec_test.c | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+)
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +129 -0
@@ -1,8 +1,16 @@// SPDX-License-Identifier: GPL-2.0-only +#include <linux/sizes.h> +#include <sys/mman.h> +#include <libliveupdate.h> #include <libvfio.h> +#define MEMCPY_SIZE SZ_1G +#define DRIVER_SIZE SZ_1M +#define MEMFD_SIZE (MEMCPY_SIZE + DRIVER_SIZE) + +static struct dma_region memcpy_region;static const char *device_bdf;static char state_session[LIVEUPDATE_SESSION_NAME_LENGTH ];
@@ -11,8 +19,89 @@ static char device_session[LIVEUPDATE_SESSION_NAME_LENGTH];enum {STATE_TOKEN ,DEVICE_TOKEN , +MEMFD_TOKEN , }; +static void dma_memcpy_one (struct vfio_pci_device *device) +{ +void *src = memcpy_region.vaddr, *dst; +u64 size; + + size =min_t (u64, memcpy_region.size /2 , device->driver.max_memcpy_size); + dst = src + size; + +memset (src,1 , size); +memset (dst,0 , size); + +printf ("Kicking off 1 DMA memcpy operations of size 0x%lx...\n" , size); +vfio_pci_driver_memcpy (device, +to_iova (device, src), +to_iova (device, dst), + size); + +VFIO_ASSERT_EQ (memcmp (src, dst, size),0 ); +} + +static void dma_memcpy_start (struct vfio_pci_device *device) +{ +void *src = memcpy_region.vaddr, *dst; +u64 count, size; + + size =min_t (u64, memcpy_region.size /2 , device->driver.max_memcpy_size); + dst = src + size; + +/* +* Rough Math: If we assume the device will perform memcpy at a rate of +* 30GB/s then 7200GB of transfers will run for about 4 minutes. +*/ + count = (u64 )7200 *SZ_1G / size; + count =min_t (u64, count, device->driver.max_memcpy_count); + +memset (src,1 , size /2 ); +memset (dst,0 , size /2 ); + +printf ("Kicking off %lu DMA memcpy operations of size 0x%lx...\n" , count, size); +vfio_pci_driver_memcpy_start (device, +to_iova (device, src), +to_iova (device, dst), + size, count); +} + +static void dma_memfd_map (struct vfio_pci_device *device,int fd) +{ +void *vaddr; + + vaddr =mmap (NULL ,MEMFD_SIZE ,PROT_WRITE ,MAP_SHARED , fd,0 ); +VFIO_ASSERT_NE (vaddr,MAP_FAILED ); + + memcpy_region.iova =SZ_4G ; + memcpy_region.size =MEMCPY_SIZE ; + memcpy_region.vaddr = vaddr; +iommu_map (device->iommu, &memcpy_region); + + device->driver.region.iova = memcpy_region.iova + memcpy_region.size; + device->driver.region.size =DRIVER_SIZE ; + device->driver.region.vaddr = vaddr + memcpy_region.size; +iommu_map (device->iommu, &device->driver.region); +} + +static void dma_memfd_setup (struct vfio_pci_device *device,int session_fd) +{ +int fd, ret; + + fd =memfd_create ("dma-buffer" ,0 ); +VFIO_ASSERT_GE (fd,0 ); + + ret =fallocate (fd,0 ,0 ,MEMFD_SIZE ); +VFIO_ASSERT_EQ (ret,0 ); + +printf ("Preserving memfd of size 0x%x in session\n" ,MEMFD_SIZE ); + ret =luo_session_preserve_fd (session_fd, fd,MEMFD_TOKEN ); +VFIO_ASSERT_EQ (ret,0 ); + +dma_memfd_map (device, fd); +} +static void before_kexec (int luo_fd) {struct vfio_pci_device *device;
@@ -32,6 +121,27 @@ static void before_kexec(int luo_fd) ret =luo_session_preserve_fd (session_fd, device->fd,DEVICE_TOKEN );VFIO_ASSERT_EQ (ret,0 ); +dma_memfd_setup (device, session_fd); + +/* +* If the device has a selftests driver, kick off a long-running DMA +* operation to exercise the device trying to DMA during a Live Update. +* Since iommufd preservation is not supported yet, these DMAs should be +* dropped. So this is just looking to verify that the system does not +* fall over and crash as a result of a busy device being preserved. +*/ +if (device->driver.ops) { +vfio_pci_driver_init (device); +dma_memcpy_start (device); + +/* +* Disable interrupts on the device or freeze() will fail. +* Unfortunately there isn't a way to easily have a test for +* that here since the check happens during shutdown. +*/ +vfio_pci_msix_disable (device); + } +close (luo_fd);daemonize_and_wait (); }
@@ -78,6 +188,7 @@ static void after_kexec(int luo_fd, int state_session_fd)struct iommu *iommu;int session_fd;int device_fd; +int memfd;int stage;check_open_vfio_device_fails ();
@@ -88,6 +199,10 @@ static void after_kexec(int luo_fd, int state_session_fd) session_fd =luo_retrieve_session (luo_fd, device_session);VFIO_ASSERT_GE (session_fd,0 ); +printf ("Retrieving memfd from LUO\n" ); + memfd =luo_session_retrieve_fd (session_fd,MEMFD_TOKEN ); +VFIO_ASSERT_GE (memfd,0 ); +printf ("Finishing the session before retrieving the device (should fail)\n" );VFIO_ASSERT_NE (luo_session_finish (session_fd),0 );
@@ -109,9 +224,23 @@ static void after_kexec(int luo_fd, int state_session_fd)*/ device =__vfio_pci_device_init (device_bdf, iommu, device_fd); +dma_memfd_map (device, memfd); +printf ("Finishing the session\n" );VFIO_ASSERT_EQ (luo_session_finish (session_fd),0 ); +/* +* Once iommufd preservation is supported and the device is kept fully +* running across the Live Update, this should wait for the long- +* running DMA memcpy operation kicked off in before_kexec() to +* complete. But for now we expect the device to be reset so just +* trigger a single memcpy to make sure it's still functional. +*/ +if (device->driver.ops) { +vfio_pci_driver_init (device); +dma_memcpy_one (device); + } +vfio_pci_device_cleanup (device);iommu_cleanup (iommu); }
Signature · 1 line
2.54.0.563.g4f69b47b94-goog
[email protected]
AI reviewRe: [PATCH v4 16/16] vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing PROT_READ flag in mmap() causes segmentation fault on read. - [Low] Incorrect memset…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing PROT_READ flag in mmap() causes segmentation fault on read.
- [Low] Incorrect memset size in dma_memcpy_start() leaves half of the transfer buffer uninitialized. --
commit 4781683698837fa3bf4dcf765b9c536579e8545a
Author: David Matlack <[email protected]>
vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test
This commit adds a long-running DMA memcpy operation to the Live Update kexec test to ensure continuous DMAs do not lead to system instability.
Quoted 4 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +7
-0+static void dma_memcpy_start (struct vfio_pci_device *device) +{ +void *src = memcpy_region.vaddr, *dst; +u64 count, size; + + size =min_t (u64, memcpy_region.size /2 , device->driver.max_memcpy_size); + dst = src + size;
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c static char device_session[LIVEUPDATE_SESSION_NAME_LENGTH]; · line 61
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +10
-0+ +/* +* Rough Math: If we assume the device will perform memcpy at a rate of +* 30GB/s then 7200GB of transfers will run for about 4 minutes. +*/ + count = (u64 )7200 *SZ_1G / size; + count =min_t (u64, count, device->driver.max_memcpy_count); + +memset (src,1 , size /2 ); +memset (dst,0 , size /2 );
Does this leave half of the transfer buffer uninitialized?
The transfer size is calculated as size, but the initialization loop only covers size / 2 bytes. When vfio_pci_driver_memcpy_start() initiates the DMA transfer of size bytes, the device will read from the uninitialized second half of the buffer.
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c static char device_session[LIVEUPDATE_SESSION_NAME_LENGTH]; · line 74
Quoted 13 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +13
-0+ +printf ("Kicking off %lu DMA memcpy operations of size 0x%lx...\n" , count, size); +vfio_pci_driver_memcpy_start (device, +to_iova (device, src), +to_iova (device, dst), + size, count); +} + +static void dma_memfd_map (struct vfio_pci_device *device,int fd) +{ +void *vaddr; + + vaddr =mmap (NULL ,MEMFD_SIZE ,PROT_WRITE ,MAP_SHARED , fd,0 );
Does this mmap() call need the PROT_READ flag?
The DMA buffer is mapped with only PROT_WRITE, but the test subsequently performs read operations on this mapped memory:
dma_memcpy_one()
VFIO_ASSERT_EQ(memcmp(src, dst, size), 0);
On architectures that enforce strict memory permissions and do not implicitly grant read access for write-only mappings, this will cause a segmentation fault.
Quoted 1 line
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +1
-0+VFIO_ASSERT_NE (vaddr,MAP_FAILED );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16