vfio/pci: Fix power state race with SR-IOV enablement
Subsystems (5)
David Matlack
maintainercover vfio/pci: Fix power state race with SR-IOV enablement
This series fixes a race in VFIO's handling of device power state transitions and a related missing data race annotation, both found while reviewing Raghavendra's sriov_active patch [1]. It also adds a selftest to exercise VFIO's handling of device power state transitions.
Patch 1 fixes a race between vfio_pci_core_disable() and vfio_pci_core_sriov_configure(), both of which transition the device to D0, but only the latter holds memory_lock. If userspace puts the device into D3hot via config space and then closes the device while concurrently enabling SR-IOV, both threads can transition the device from D3hot to D0 at the same time. For devices with NoSoftRst-, this can result in VFIO restoring the PF's BARs after VFs have been enabled (triggering a WARN in pci_iov_update_resource()), or in a double-free of vdev->pm_save. The fix is to hold memory_lock for all calls to vfio_pci_set_power_state() and to assert it unconditionally.
Patch 2 annotates the intentional lockless accesses to vdev->sriov_active with READ_ONCE() and WRITE_ONCE().
Patches 3-5 add vfio_pci_power_state_test, which covers closing a device in D3hot, enabling SR-IOV while the PF is in D3hot, and racing the two. Since power state bugs often only manifest as kernel warnings, the test fails if /sys/kernel/warn_count changes during a test case. Patches 3 and 4 add the library helpers the test needs.
Testing
-----------------------------------------------------------------------
Tested in a QEMU VM with an emulated igb device (NoSoftRst-, SR-IOV capable) bound to vfio-pci:
- Without patches 1-2 (KASAN kernel), close_in_d3hot_races_sriov_enable fails reliably, hitting the pci_iov_update_resource() WARN on 9/10 iterations.
- With patches 1-2 (KASAN and lockdep kernels), all VFIO selftests pass with no warnings, KASAN reports, or lockdep splats.
[1] https://lore.kernel.org/r/[email protected]
David Matlack (5):
vfio/pci: Hold memory_lock for all calls to vfio_pci_set_power_state() vfio/pci: Annotate lockless accesses to vdev->sriov_active
vfio: selftests: Add vfio_pci_find_capability()
vfio: selftests: Add sysfs_kernel_warn_count_get()
vfio: selftests: Add test for device power state transitions
drivers/vfio/pci/vfio_pci_core.c | 19 +- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/sysfs.h | 7 + .../lib/include/libvfio/vfio_pci_device.h | 7 + tools/testing/selftests/vfio/lib/sysfs.c | 19 +- .../selftests/vfio/lib/vfio_pci_device.c | 25 +++ .../vfio/vfio_pci_power_state_test.c | 189 ++++++++++++++++++ 7 files changed, 257 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/vfio/vfio_pci_power_state_test.c
base-commit: 076d7e989cb69f6ded7d60f2a080eb909930061b
Signature · 1 line
2.56.0.317.g454f8302e1-goog
David Matlack
maintainerPATCH 1/5 vfio/pci: Hold memory_lock for all calls to vfio_pci_set_power_state()
Hold vdev->memory_lock for write when calling vfio_pci_set_power_state() from vfio_pci_core_disable() and vfio_pci_core_register_device(), and assert that the lock is held for all power state transitions, not just transitions out of D0.
vfio_pci_core_disable() calls vfio_pci_set_power_state(vdev, PCI_D0) without holding memory_lock, which allows it to race with vfio_pci_core_sriov_configure() when userspace writes to sriov_numvfs while the device is being closed. If the user left the device in D3hot via a config space write (which leaves the device RPM_ACTIVE, so pm_runtime_resume() does not bring it back to D0), both threads can transition the device from D3hot to D0 concurrently.
For devices that need_pm_restore (NoSoftRst-), this can result in:
- vfio_pci_core_disable() restoring the PF's BARs after vfio_pci_core_sriov_configure() has enabled VFs, since the D3hot->D0 transition sleeps before restoring device state. This is easy to hit and triggers the following warning:
vfio-pci 0000:01:00.0: can't update enabled VF BAR0 [mem 0xe0200000000-0xe020001ffff 64bit pref]
WARNING: drivers/pci/iov.c:1062 at pci_iov_update_resource+0x170/0x3b0
Call Trace:
pci_update_resource+0x1a3/0x5b0
__pci_set_power_state+0x597/0xb90
vfio_pci_core_disable+0x13b/0x1180
vfio_pci_core_close_device+0x144/0x260
vfio_df_close+0xf2/0x210
vfio_df_unbind_iommufd+0x89/0x140
vfio_device_fops_release+0x85/0xf0
__fput+0x2d6/0x720
fput_close_sync+0xe8/0x180
__x64_sys_close+0x7f/0xc0
do_syscall_64+0xbc/0x320
entry_SYSCALL_64_after_hwframe+0x76/0x7e - Both threads observing pdev->current_state >= PCI_D3hot and calling pci_load_and_free_saved_state(pdev, &vdev->pm_save) concurrently, resulting in a double-free of vdev->pm_save.
vfio_pci_core_register_device() cannot race with other callers since the device is not yet registered and device_lock is held across probe, but take the lock there as well so that the lockdep assertion can be made unconditional and catch any future unlocked callers.
Taking memory_lock in these paths does not introduce any new lock dependencies. vfio_pci_core_disable() already acquires memory_lock under dev_set->lock via vfio_pci_runtime_pm_exit(), and vfio_pci_core_sriov_configure() already acquires memory_lock under device_lock.
--- drivers/vfio/pci/vfio_pci_core.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +12 -5
@@ -325,12 +325,15 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t statbool needs_restore = false, needs_save = false;int ret; +/* +* Serialize power state changes and accesses to vdev->pm_save, and +* protect the check of vdev->sriov_active below. +*/ +lockdep_assert_held_write (&vdev->memory_lock); +/* Prevent changing power state for PFs with VFs enabled */ -if (state >PCI_D0 ) { -lockdep_assert_held_write (&vdev->memory_lock); -if (vdev->sriov_active) -return -EBUSY ; - } +if (state >PCI_D0 && vdev->sriov_active) +return -EBUSY ;if (vdev->needs_pm_restore) {if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) {
@@ -699,7 +702,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* reset without restoring the original state (saved locally in * 'vdev->pm_save'). */ +down_write (&vdev->memory_lock);vfio_pci_set_power_state (vdev,PCI_D0 ); +up_write (&vdev->memory_lock);/* Stop the device from further DMA */ pci_clear_master (pdev);
@@ -2273,7 +2278,9 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)* be able to get to D3. Therefore first do a D0 transition * before enabling runtime PM. */ +down_write (&vdev->memory_lock);vfio_pci_set_power_state (vdev,PCI_D0 ); +up_write (&vdev->memory_lock);pm_runtime_allow (dev);if (!vdev->disable_idle_d3)
Signature · 1 line
2.56.0.317.g454f8302e1-goog
David Matlack
maintainerPATCH 2/5 vfio/pci: Annotate lockless accesses to vdev->sriov_active
In reply to David MatlackUse READ_ONCE() and WRITE_ONCE() to annotate the read of vdev->sriov_active in vfio_pci_set_power_state() and the lockless write that clears it in vfio_pci_core_sriov_configure().
vfio_pci_core_sriov_configure() intentionally clears sriov_active without holding memory_lock, so the write can race with the read in vfio_pci_set_power_state() (which runs under memory_lock). The race itself is benign, at worst it results in a spurious -EBUSY while the device stays in D0, but the accesses should be marked to prevent the compiler from tearing or otherwise optimizing them and to document the intentional data race (e.g. for KCSAN).
The write that sets sriov_active is done under memory_lock and is serialized against the lockless write by device_lock, so it does not need to be annotated.
--- drivers/vfio/pci/vfio_pci_core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +2 -2
@@ -332,7 +332,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t statlockdep_assert_held_write (&vdev->memory_lock);/* Prevent changing power state for PFs with VFs enabled */ -if (state >PCI_D0 && vdev->sriov_active) +if (state >PCI_D0 &&READ_ONCE (vdev->sriov_active))return -EBUSY ;if (vdev->needs_pm_restore) {
@@ -2396,7 +2396,7 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev,* state transition would at most result in an -EBUSY, leaving the * device in PCI_D0. */ - vdev->sriov_active = false; +WRITE_ONCE (vdev->sriov_active, false);mutex_lock (&vfio_pci_sriov_pfs_mutex);list_del_init (&vdev->sriov_pfs_item);
Signature · 1 line
2.56.0.317.g454f8302e1-goog
David Matlack
maintainerPATCH 3/5 vfio: selftests: Add vfio_pci_find_capability()
In reply to David MatlackAdd a helper to find the config space offset of a capability in a device's standard capability list, mirroring the kernel's pci_find_capability(). Return 0 if the device does not have the capability.
This will be used in a subsequent commit to locate the PM capability.
--- .../lib/include/libvfio/vfio_pci_device.h | 7 ++++++ .../selftests/vfio/lib/vfio_pci_device.c | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +7 -0
@@ -79,6 +79,13 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write,#define vfio_pci_config_writew (_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)#define vfio_pci_config_writel (_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32) +/* +* Return the config space offset of the first capability with the given ID +* (PCI_CAP_ID_*) in the device's standard capability list, or 0 if the device +* does not have the capability. +*/ +u8 vfio_pci_find_capability (struct vfio_pci_device *device,u8 cap_id); +static inline void vfio_pci_cmd_set (struct vfio_pci_device *device,u16 bits) {u16 cmd =vfio_pci_config_readw (device,PCI_COMMAND );
tools/testing/selftests/vfio/lib/vfio_pci_device.c +25 -0
@@ -260,6 +260,31 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write, write ? "write to" : "read from", config); } +/* Bound the capability list walk in case the list contains a loop. */ +#define PCI_FIND_CAP_TTL 48 + +u8 vfio_pci_find_capability (struct vfio_pci_device *device,u8 cap_id) +{ +int ttl =PCI_FIND_CAP_TTL ; +u8 pos; + +if (!(vfio_pci_config_readw (device,PCI_STATUS ) &PCI_STATUS_CAP_LIST )) +return 0 ; + + pos =vfio_pci_config_readb (device,PCI_CAPABILITY_LIST ); + +while (pos >=PCI_STD_HEADER_SIZEOF && ttl--) { + pos &= ~3 ; + +if (vfio_pci_config_readb (device, pos +PCI_CAP_LIST_ID ) == cap_id) +return pos; + + pos =vfio_pci_config_readb (device, pos +PCI_CAP_LIST_NEXT ); + } + +return 0 ; +} +int __vfio_pci_device_reset (struct vfio_pci_device *device) {if (ioctl (device->fd,VFIO_DEVICE_RESET ,NULL ))
Signature · 1 line
2.56.0.317.g454f8302e1-goog
David Matlack
maintainerPATCH 4/5 vfio: selftests: Add sysfs_kernel_warn_count_get()
In reply to David MatlackAdd a helper to read /sys/kernel/warn_count, which counts the number of warnings (e.g. WARN(), KASAN, and UBSAN reports) the kernel has encountered since boot. Return a negative value if the file does not exist, e.g. on kernels that predate it.
Factor the code to read an integer from a sysfs file out of sysfs_val_get_int() so that it can be shared by both helpers.
This will be used in a subsequent commit to detect kernel warnings triggered by tests.
--- .../vfio/lib/include/libvfio/sysfs.h | 7 +++++++ tools/testing/selftests/vfio/lib/sysfs.c | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h +7 -0
@@ -9,4 +9,11 @@ char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i);int sysfs_iommu_group_get (const char *bdf);char *sysfs_driver_get (const char *bdf); +/* +* Return the number of warnings the kernel has encountered since boot (e.g. +* WARN(), KASAN, and UBSAN reports), or a negative value if the kernel does +* not support /sys/kernel/warn_count. +*/ +int sysfs_kernel_warn_count_get (void ); +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_SYSFS_H */
tools/testing/selftests/vfio/lib/sysfs.c +15 -4
@@ -33,15 +33,12 @@ static void readlink_base(const char *path, const char *data_fmt, void *out_dataVFIO_ASSERT_EQ (ret,1 ); } -static int sysfs_val_get_int (const char *component,const char *name, -const char *file) +static int sysfs_path_get_int (const char *path) { -char path[PATH_MAX ];char buf[32 ];int ret;int fd; -snprintf_assert (path,PATH_MAX ,"/sys/bus/pci/%s/%s/%s" , component, name, file); fd =open (path,O_RDONLY );if (fd <0 )return fd;
@@ -56,6 +53,15 @@ static int sysfs_val_get_int(const char *component, const char *name,return ret; } +static int sysfs_val_get_int (const char *component,const char *name, +const char *file) +{ +char path[PATH_MAX ]; + +snprintf_assert (path,PATH_MAX ,"/sys/bus/pci/%s/%s/%s" , component, name, file); +return sysfs_path_get_int (path); +} +static void sysfs_val_set (const char *component,const char *name,const char *file,const char *val) {
@@ -148,3 +154,8 @@ char *sysfs_driver_get(const char *bdf)return out_driver; } + +int sysfs_kernel_warn_count_get (void ) +{ +return sysfs_path_get_int ("/sys/kernel/warn_count" ); +}
Signature · 1 line
2.56.0.317.g454f8302e1-goog
David Matlack
maintainerPATCH 5/5 vfio: selftests: Add test for device power state transitions
In reply to David MatlackAdd vfio_pci_power_state_test to exercise VFIO's handling of device power state changes initiated by userspace through the PM capability in config space.
The test covers:
- Closing a device while it is in D3hot, which must return the device to D0.
- Enabling SR-IOV while the PF is in D3hot, which must return the PF to D0 and prevent it from leaving D0 while VFs are enabled.
- Racing a close of the device while it is in D3hot against enabling SR-IOV, both of which transition the device back to D0.
The SR-IOV tests are skipped if the device does not support SR-IOV or already has VFs enabled.
Bugs in VFIO's power state handling, such as the race above, often only manifest as kernel warnings (e.g. WARN() or KASAN reports). So fail the test if /sys/kernel/warn_count changes between fixture setup and teardown.
--- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_pci_power_state_test.c | 189 ++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_power_state_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_power_state_test TEST_GEN_PROGS += vfio_pci_sriov_uapi_test TEST_FILES += scripts/cleanup.sh
tools/testing/selftests/vfio/vfio_pci_power_state_test.c new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
2.56.0.317.g454f8302e1-goog
Alex Williamson
maintainerRe: [PATCH 5/5] vfio: selftests: Add test for device power state transitions
Quoted 4 lines · On Thu, 29 Oct 2026 20:33:24 +0000 David Matlack <[email protected]> wrote:
+/*
+ * Put the device into D3hot via config space and then close it. VFIO must
+ * return the device to D0 when it is closed.
+ */
But we're only testing that it's re-opened into D0, there are multiple ways that could have happened. It's pretty common that a guest shutdown might put devices into a low power state and it's brought back to D0 on close, on reset, on the next open, or even on the reset in open. So I don't think we're testing what we claim to test here. I'm not sure this specific D0 transition is observable, even if we were to resort to pci-sysfs. Thanks,
Alex
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_power_state_test.c new file +10
-0+TEST_F(vfio_pci_power_state_test, close_in_d3hot) +{ +power_state_set (self->device, self->pm_cap,D3HOT ); +ASSERT_EQ (power_state_get (self->device, self->pm_cap),D3HOT ); + +vfio_pci_device_cleanup (self->device); + + self->device =vfio_pci_device_init (device_bdf, self->iommu); +ASSERT_EQ (power_state_get (self->device, self->pm_cap),D0 ); +}
David Matlack
maintainerRe: [PATCH 5/5] vfio: selftests: Add test for device power state transitions
Quoted 7 lines · Alex Williamson
Quoted 4 lines · On Thu, 29 Oct 2026 20:33:24 +0000 David Matlack <[email protected]> wrote:
+/*
+ * Put the device into D3hot via config space and then close it. VFIO must
+ * return the device to D0 when it is closed.
+ */
Quoted 7 lines
But we're only testing that it's re-opened into D0, there are multiple ways that could have happened. It's pretty common that a guest shutdown might put devices into a low power state and it's brought back to D0 on close, on reset, on the next open, or even on the reset in open. So I don't think we're testing what we claim to test here. I'm not sure this specific D0 transition is observable, even if we were to resort to pci-sysfs.
You are right, this comment is not accurate. The goal of this test is to exercise closing a device while it is in D3hot. The close should succeed and the device should be in D0 after it is next opened. The transition from D3hot to D0 could happen during close, open, or anywhere in the middle, without changing the uAPI.
I'll fixup the comment, changelog, and test name in v2.
Alex Williamson
maintainerRe: [PATCH 4/5] vfio: selftests: Add sysfs_kernel_warn_count_get()
In reply to David MatlackQuoted 8 lines · On Thu, 29 Oct 2026 20:33:23 +0000 David Matlack <[email protected]> wrote:
Add a helper to read
/sys/kernel/warn_count, which counts the number of warnings (e.g.WARN(), KASAN, and UBSAN reports) the kernel has encountered since boot. Return a negative value if the file does not exist, e.g. on kernels that predate it.Factor the code to read an integer from a sysfs file out of
sysfs_val_get_int()so that it can be shared by both helpers.
Quoted 8 lines
This will be used in a subsequent commit to detect kernel warnings triggered by tests.
Signed-off-by David Matlack <[email protected]>--- .../vfio/lib/include/libvfio/sysfs.h | 7 +++++++ tools/testing/selftests/vfio/lib/sysfs.c | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-)
I have a concern with this approach. I run selftests in parallel as much as possible, using multiple devices, each targeted at specific tests, with other functional and performance tests running concurrently. When wiring this into my harness, I initially saw a failure, but can't reproduce and have no log from the original event.
I've updated my local CI to keep a console log so that I might have a chance of chasing the next instance, but I don't much like the fact that any warning occurring concurrently with this test can trigger a failure and the test doesn't provide anything to go on in the test logs themselves. Thanks,
Alex
David Matlack
maintainerRe: [PATCH 4/5] vfio: selftests: Add sysfs_kernel_warn_count_get()
Quoted 26 lines · Alex Williamson
Quoted 8 lines · On Thu, 29 Oct 2026 20:33:23 +0000 David Matlack <[email protected]> wrote:
Add a helper to read
/sys/kernel/warn_count, which counts the number of warnings (e.g.WARN(), KASAN, and UBSAN reports) the kernel has encountered since boot. Return a negative value if the file does not exist, e.g. on kernels that predate it.Factor the code to read an integer from a sysfs file out of
sysfs_val_get_int()so that it can be shared by both helpers.Quoted 8 lines
This will be used in a subsequent commit to detect kernel warnings triggered by tests.
Signed-off-by David Matlack <[email protected]>--- .../vfio/lib/include/libvfio/sysfs.h | 7 +++++++ tools/testing/selftests/vfio/lib/sysfs.c | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-)I have a concern with this approach. I run selftests in parallel as much as possible, using multiple devices, each targeted at specific tests, with other functional and performance tests running concurrently. When wiring this into my harness, I initially saw a failure, but can't reproduce and have no log from the original event.
Quoted 5 lines
I've updated my local CI to keep a console log so that I might have a chance of chasing the next instance, but I don't much like the fact that any warning occurring concurrently with this test can trigger a failure and the test doesn't provide anything to go on in the test logs themselves. Thanks,
Thanks for trying this out and the report.
What do you think about keeping warn_count to detect warnings, but also reading /dev/kmsg so the failure details are captured?
- In setup, record warn_count and open /dev/kmsg and seek to the end.
- In teardown, if warn_count changed, read the new /dev/kmsg records and print the kernel warning reports (WARNING:, BUG:, KASAN, UBSAN, etc.), including their call traces, into the test log before failing.
That way, if the check fails, the test log would show exactly what fired and you could tell right away whether it came from VFIO or from something else running concurrently.
To avoid breaking setups like yours that run lots of tests in parallel, we could also make failing on warnings opt-in via an environment variable, so by default the test only logs the reports.
Alternatively we could just say warn checking should be up to the user.