LKML Archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio_pci: Wake device to D0 before resets
@ 2021-08-12 17:37 Alex Williamson
  0 siblings, 0 replies; only message in thread
From: Alex Williamson @ 2021-08-12 17:37 UTC (permalink / raw
  To: alex.williamson; +Cc: linux-kernel, kvm

pci_pm_reset() actually depends on the device starting in the D0 power
state, therefore any time we're using a flavor of pci_reset_function()
we should make sure the device is fully powered-up in case the PM reset
method is used.

It's not uncommon that shutdown of a VM will put the device into a D3
state such that vfio_pci_disable() is managing a device in this low
power state.  The reset state of a device is the D0 power state, so
it's also reasonable to put the device into this state prior to reset.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/pci/vfio_pci.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index a4f44ea52fa3..e3faa1eb1a8c 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -454,6 +454,15 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev)
 
 	vdev->needs_reset = true;
 
+	/*
+	 * Userspace may have left the device in a low power state which
+	 * affects our ability to trigger a PM reset, restore to D0 and
+	 * toss any saved state from the previous session.
+	 */
+	pci_set_power_state(pdev, PCI_D0);
+	kfree(vdev->pm_save);
+	vdev->pm_save = NULL;
+
 	/*
 	 * If we have saved state, restore it.  If we can reset the device,
 	 * even better.  Resetting with current state seems better than
@@ -1013,6 +1022,9 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev,
 		if (!vdev->reset_works)
 			return -EINVAL;
 
+		/* PM reset depends on the device not already being in D3 */
+		vfio_pci_set_power_state(vdev, PCI_D0);
+
 		vfio_pci_zap_and_down_write_memory_lock(vdev);
 		ret = pci_try_reset_function(vdev->pdev);
 		up_write(&vdev->memory_lock);



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-08-12 17:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-08-12 17:37 [PATCH] vfio_pci: Wake device to D0 before resets Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).