[PATCH v2 2/8] vfio/pci: Annotate lockless accesses to vdev->sriov_active
From: David Matlack
Date: Wed Nov 18 2026 - 19:02:17 EST
Use 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 is
benign: at worst it causes a spurious -EBUSY while the device stays in
D0. Mark the accesses anyway to document the intentional data race and
avoid KCSAN reports.
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.
Fixes: 40ef3edf151e ("vfio/pci: Use a private flag to prevent power state change with VFs")
Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
drivers/vfio/pci/vfio_pci_core.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 4289ec9bc32f..511dcb49d2f6 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -329,7 +329,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat
lockdep_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) {
@@ -2393,7 +2393,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);
--
2.56.1.708.g77570a6a1d-goog